Fix bug #14616 with unnecessary redrawing of TTY frames.
[emacs.git] / src / window.c
blob118c58522758c16b22cfc8da0e59eb2228c0738b
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2013 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #define WINDOW_INLINE EXTERN_INLINE
25 #include <stdio.h>
27 #include "lisp.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "keyboard.h"
31 #include "keymap.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "commands.h"
35 #include "indent.h"
36 #include "termchar.h"
37 #include "disptab.h"
38 #include "dispextern.h"
39 #include "blockinput.h"
40 #include "intervals.h"
41 #include "termhooks.h" /* For FRAME_TERMINAL. */
43 #ifdef HAVE_X_WINDOWS
44 #include "xterm.h"
45 #endif /* HAVE_X_WINDOWS */
46 #ifdef HAVE_NTGUI
47 #include "w32term.h"
48 #endif
49 #ifdef MSDOS
50 #include "msdos.h"
51 #endif
52 #ifdef HAVE_NS
53 #include "nsterm.h"
54 #endif
56 Lisp_Object Qwindowp, Qwindow_live_p;
57 static Lisp_Object Qwindow_valid_p;
58 static Lisp_Object Qwindow_configuration_p;
59 static Lisp_Object Qrecord_window_buffer;
60 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
61 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
62 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
63 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
64 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
66 static int displayed_window_lines (struct window *);
67 static int count_windows (struct window *);
68 static int get_leaf_windows (struct window *, struct window **, int);
69 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
70 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
71 static void window_scroll_line_based (Lisp_Object, int, bool, int);
72 static int freeze_window_start (struct window *, void *);
73 static Lisp_Object window_list (void);
74 static int add_window_to_list (struct window *, void *);
75 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
76 Lisp_Object, int);
77 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
78 Lisp_Object *);
79 static void foreach_window (struct frame *,
80 int (* fn) (struct window *, void *),
81 void *);
82 static int foreach_window_1 (struct window *,
83 int (* fn) (struct window *, void *),
84 void *);
85 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
86 static int window_resize_check (struct window *, bool);
87 static void window_resize_apply (struct window *, bool);
88 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
89 static void select_window_1 (Lisp_Object, bool);
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 /* Nonzero after init_window_once has finished. */
119 static int window_initialized;
121 /* Hook to run when window config changes. */
122 static Lisp_Object Qwindow_configuration_change_hook;
124 /* Used by the function window_scroll_pixel_based */
125 static int window_scroll_pixel_based_preserve_x;
126 static int window_scroll_pixel_based_preserve_y;
128 /* Same for window_scroll_line_based. */
129 static EMACS_INT window_scroll_preserve_hpos;
130 static EMACS_INT window_scroll_preserve_vpos;
132 static void
133 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
135 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
138 /* These setters are used only in this file, so they can be private. */
139 static void
140 wset_combination_limit (struct window *w, Lisp_Object val)
142 w->combination_limit = val;
144 static void
145 wset_dedicated (struct window *w, Lisp_Object val)
147 w->dedicated = val;
149 static void
150 wset_display_table (struct window *w, Lisp_Object val)
152 w->display_table = val;
154 static void
155 wset_left_fringe_width (struct window *w, Lisp_Object val)
157 w->left_fringe_width = val;
159 static void
160 wset_left_margin_cols (struct window *w, Lisp_Object val)
162 w->left_margin_cols = val;
164 static void
165 wset_new_normal (struct window *w, Lisp_Object val)
167 w->new_normal = val;
169 static void
170 wset_new_total (struct window *w, Lisp_Object val)
172 w->new_total = val;
174 static void
175 wset_normal_cols (struct window *w, Lisp_Object val)
177 w->normal_cols = val;
179 static void
180 wset_normal_lines (struct window *w, Lisp_Object val)
182 w->normal_lines = val;
184 static void
185 wset_parent (struct window *w, Lisp_Object val)
187 w->parent = val;
189 static void
190 wset_pointm (struct window *w, Lisp_Object val)
192 w->pointm = val;
194 static void
195 wset_right_fringe_width (struct window *w, Lisp_Object val)
197 w->right_fringe_width = val;
199 static void
200 wset_right_margin_cols (struct window *w, Lisp_Object val)
202 w->right_margin_cols = val;
204 static void
205 wset_scroll_bar_width (struct window *w, Lisp_Object val)
207 w->scroll_bar_width = val;
209 static void
210 wset_start (struct window *w, Lisp_Object val)
212 w->start = val;
214 static void
215 wset_temslot (struct window *w, Lisp_Object val)
217 w->temslot = val;
219 static void
220 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
222 w->vertical_scroll_bar_type = val;
224 static void
225 wset_window_parameters (struct window *w, Lisp_Object val)
227 w->window_parameters = val;
229 static void
230 wset_combination (struct window *w, bool horflag, Lisp_Object val)
232 /* Since leaf windows never becomes non-leaf, there should
233 be no buffer and markers in start and pointm fields of W. */
234 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
235 w->contents = val;
236 /* When an internal window is deleted and VAL is nil, HORFLAG
237 is meaningless. */
238 if (!NILP (val))
239 w->horizontal = horflag;
242 /* Nonzero if leaf window W doesn't reflect the actual state
243 of displayed buffer due to its text or overlays change. */
245 bool
246 window_outdated (struct window *w)
248 struct buffer *b = XBUFFER (w->contents);
249 return (w->last_modified < BUF_MODIFF (b)
250 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
253 struct window *
254 decode_live_window (register Lisp_Object window)
256 if (NILP (window))
257 return XWINDOW (selected_window);
259 CHECK_LIVE_WINDOW (window);
260 return XWINDOW (window);
263 struct window *
264 decode_any_window (register Lisp_Object window)
266 struct window *w;
268 if (NILP (window))
269 return XWINDOW (selected_window);
271 CHECK_WINDOW (window);
272 w = XWINDOW (window);
273 return w;
276 static struct window *
277 decode_valid_window (register Lisp_Object window)
279 struct window *w;
281 if (NILP (window))
282 return XWINDOW (selected_window);
284 CHECK_VALID_WINDOW (window);
285 w = XWINDOW (window);
286 return w;
289 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
290 cease its buffer, and 1 means that W is about to set up the new one. */
292 static void
293 adjust_window_count (struct window *w, int arg)
295 eassert (eabs (arg) == 1);
296 if (BUFFERP (w->contents))
298 struct buffer *b = XBUFFER (w->contents);
300 if (b->base_buffer)
301 b = b->base_buffer;
302 b->window_count += arg;
303 eassert (b->window_count >= 0);
304 /* These should be recalculated by redisplay code. */
305 w->window_end_valid = 0;
306 w->base_line_pos = 0;
310 /* Set W's buffer slot to VAL and recompute number
311 of windows showing VAL if it is a buffer. */
313 void
314 wset_buffer (struct window *w, Lisp_Object val)
316 adjust_window_count (w, -1);
317 if (BUFFERP (val))
318 /* Make sure that we do not assign the buffer
319 to an internal window. */
320 eassert (MARKERP (w->start) && MARKERP (w->pointm));
321 w->contents = val;
322 adjust_window_count (w, 1);
325 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
326 doc: /* Return t if OBJECT is a window and nil otherwise. */)
327 (Lisp_Object object)
329 return WINDOWP (object) ? Qt : Qnil;
332 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
333 doc: /* Return t if OBJECT is a valid window and nil otherwise.
334 A valid window is either a window that displays a buffer or an internal
335 window. Deleted windows are not live. */)
336 (Lisp_Object object)
338 return WINDOW_VALID_P (object) ? Qt : Qnil;
341 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
342 doc: /* Return t if OBJECT is a live window and nil otherwise.
343 A live window is a window that displays a buffer.
344 Internal windows and deleted windows are not live. */)
345 (Lisp_Object object)
347 return WINDOW_LIVE_P (object) ? Qt : Qnil;
350 /* Frames and windows. */
351 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
352 doc: /* Return the frame that window WINDOW is on.
353 WINDOW must be a valid window and defaults to the selected one. */)
354 (Lisp_Object window)
356 return decode_valid_window (window)->frame;
359 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
360 doc: /* Return the root window of FRAME-OR-WINDOW.
361 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
362 With a frame argument, return that frame's root window.
363 With a window argument, return the root window of that window's frame. */)
364 (Lisp_Object frame_or_window)
366 Lisp_Object window;
368 if (NILP (frame_or_window))
369 window = SELECTED_FRAME ()->root_window;
370 else if (WINDOW_VALID_P (frame_or_window))
371 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
372 else
374 CHECK_LIVE_FRAME (frame_or_window);
375 window = XFRAME (frame_or_window)->root_window;
378 return window;
381 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
382 doc: /* Return the minibuffer window for frame FRAME.
383 If FRAME is omitted or nil, it defaults to the selected frame. */)
384 (Lisp_Object frame)
386 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
389 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
390 Swindow_minibuffer_p, 0, 1, 0,
391 doc: /* Return non-nil if WINDOW is a minibuffer window.
392 WINDOW must be a valid window and defaults to the selected one. */)
393 (Lisp_Object window)
395 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
398 /* Don't move this to window.el - this must be a safe routine. */
399 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
400 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
401 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
402 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
403 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
404 the first window of that frame. */)
405 (Lisp_Object frame_or_window)
407 Lisp_Object window;
409 if (NILP (frame_or_window))
410 window = SELECTED_FRAME ()->root_window;
411 else if (WINDOW_VALID_P (frame_or_window))
412 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
413 else
415 CHECK_LIVE_FRAME (frame_or_window);
416 window = XFRAME (frame_or_window)->root_window;
419 while (WINDOWP (XWINDOW (window)->contents))
420 window = XWINDOW (window)->contents;
422 return window;
425 DEFUN ("frame-selected-window", Fframe_selected_window,
426 Sframe_selected_window, 0, 1, 0,
427 doc: /* Return the selected window of FRAME-OR-WINDOW.
428 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
429 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
430 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
431 return the selected window of that frame. */)
432 (Lisp_Object frame_or_window)
434 Lisp_Object window;
436 if (NILP (frame_or_window))
437 window = SELECTED_FRAME ()->selected_window;
438 else if (WINDOW_VALID_P (frame_or_window))
439 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
440 else
442 CHECK_LIVE_FRAME (frame_or_window);
443 window = XFRAME (frame_or_window)->selected_window;
446 return window;
449 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
450 Sset_frame_selected_window, 2, 3, 0,
451 doc: /* Set selected window of FRAME to WINDOW.
452 FRAME must be a live frame and defaults to the selected one. If FRAME
453 is the selected frame, this makes WINDOW the selected window. Optional
454 argument NORECORD non-nil means to neither change the order of recently
455 selected windows nor the buffer list. WINDOW must denote a live window.
456 Return WINDOW. */)
457 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
459 if (NILP (frame))
460 frame = selected_frame;
462 CHECK_LIVE_FRAME (frame);
463 CHECK_LIVE_WINDOW (window);
465 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
466 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
468 if (EQ (frame, selected_frame))
469 return Fselect_window (window, norecord);
470 else
472 fset_selected_window (XFRAME (frame), window);
473 return window;
477 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
478 doc: /* Return the selected window.
479 The selected window is the window in which the standard cursor for
480 selected windows appears and to which many commands apply. */)
481 (void)
483 return selected_window;
486 int window_select_count;
488 /* If select_window is called with inhibit_point_swap non-zero it will
489 not store point of the old selected window's buffer back into that
490 window's pointm slot. This is needed by Fset_window_configuration to
491 avoid that the display routine is called with selected_window set to
492 Qnil causing a subsequent crash. */
493 static Lisp_Object
494 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
496 register struct window *w;
497 struct frame *sf;
499 CHECK_LIVE_WINDOW (window);
501 w = XWINDOW (window);
502 w->frozen_window_start_p = 0;
504 /* Make the selected window's buffer current. */
505 Fset_buffer (w->contents);
507 if (EQ (window, selected_window) && !inhibit_point_swap)
508 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
509 way to call record_buffer from Elisp, so it's important that we call
510 record_buffer before returning here. */
511 goto record_and_return;
513 sf = SELECTED_FRAME ();
514 if (XFRAME (WINDOW_FRAME (w)) != sf)
516 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
517 /* Use this rather than Fhandle_switch_frame
518 so that FRAME_FOCUS_FRAME is moved appropriately as we
519 move around in the state where a minibuffer in a separate
520 frame is active. */
521 Fselect_frame (WINDOW_FRAME (w), norecord);
522 /* Fselect_frame called us back so we've done all the work already. */
523 eassert (EQ (window, selected_window));
524 return window;
526 else
527 fset_selected_window (sf, window);
529 select_window_1 (window, inhibit_point_swap);
530 bset_last_selected_window (XBUFFER (w->contents), window);
531 windows_or_buffers_changed++;
533 record_and_return:
534 /* record_buffer can run QUIT, so make sure it is run only after we have
535 re-established the invariant between selected_window and selected_frame,
536 otherwise the temporary broken invariant might "escape" (bug#14161). */
537 if (NILP (norecord))
539 w->use_time = ++window_select_count;
540 record_buffer (w->contents);
543 return window;
546 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
547 (not even for redisplay's benefit), and assume that the window's frame is
548 already selected. */
549 static void
550 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
552 /* Store the old selected window's buffer's point in pointm of the old
553 selected window. It belongs to that window, and when the window is
554 not selected, must be in the window. */
555 if (!inhibit_point_swap)
557 struct window *ow = XWINDOW (selected_window);
558 if (BUFFERP (ow->contents))
559 set_marker_both (ow->pointm, ow->contents,
560 BUF_PT (XBUFFER (ow->contents)),
561 BUF_PT_BYTE (XBUFFER (ow->contents)));
564 selected_window = window;
566 /* Go to the point recorded in the window.
567 This is important when the buffer is in more
568 than one window. It also matters when
569 redisplay_window has altered point after scrolling,
570 because it makes the change only in the window. */
572 register ptrdiff_t new_point = marker_position (XWINDOW (window)->pointm);
573 if (new_point < BEGV)
574 SET_PT (BEGV);
575 else if (new_point > ZV)
576 SET_PT (ZV);
577 else
578 SET_PT (new_point);
582 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
583 doc: /* Select WINDOW which must be a live window.
584 Also make WINDOW's frame the selected frame and WINDOW that frame's
585 selected window. In addition, make WINDOW's buffer current and set that
586 buffer's value of `point' to the value of WINDOW's `window-point'.
587 Return WINDOW.
589 Optional second arg NORECORD non-nil means do not put this buffer at the
590 front of the buffer list and do not make this window the most recently
591 selected one.
593 Note that the main editor command loop sets the current buffer to the
594 buffer of the selected window before each command. */)
595 (register Lisp_Object window, Lisp_Object norecord)
597 return select_window (window, norecord, 0);
600 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
601 doc: /* Return the buffer displayed in window WINDOW.
602 If WINDOW is omitted or nil, it defaults to the selected window.
603 Return nil for an internal window or a deleted window. */)
604 (Lisp_Object window)
606 struct window *w = decode_any_window (window);
607 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
610 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
611 doc: /* Return the parent window of window WINDOW.
612 WINDOW must be a valid window and defaults to the selected one.
613 Return nil for a window with no parent (e.g. a root window). */)
614 (Lisp_Object window)
616 return decode_valid_window (window)->parent;
619 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
620 doc: /* Return the topmost child window of window WINDOW.
621 WINDOW must be a valid window and defaults to the selected one.
622 Return nil if WINDOW is a live window (live windows have no children).
623 Return nil if WINDOW is an internal window whose children form a
624 horizontal combination. */)
625 (Lisp_Object window)
627 struct window *w = decode_valid_window (window);
628 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
631 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
632 doc: /* Return the leftmost child window of window WINDOW.
633 WINDOW must be a valid window and defaults to the selected one.
634 Return nil if WINDOW is a live window (live windows have no children).
635 Return nil if WINDOW is an internal window whose children form a
636 vertical combination. */)
637 (Lisp_Object window)
639 struct window *w = decode_valid_window (window);
640 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
643 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
644 doc: /* Return the next sibling window of window WINDOW.
645 WINDOW must be a valid window and defaults to the selected one.
646 Return nil if WINDOW has no next sibling. */)
647 (Lisp_Object window)
649 return decode_valid_window (window)->next;
652 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
653 doc: /* Return the previous sibling window of window WINDOW.
654 WINDOW must be a valid window and defaults to the selected one.
655 Return nil if WINDOW has no previous sibling. */)
656 (Lisp_Object window)
658 return decode_valid_window (window)->prev;
661 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
662 doc: /* Return combination limit of window WINDOW.
663 WINDOW must be a valid window used in horizontal or vertical combination.
664 If the return value is nil, child windows of WINDOW can be recombined with
665 WINDOW's siblings. A return value of t means that child windows of
666 WINDOW are never \(re-)combined with WINDOW's siblings. */)
667 (Lisp_Object window)
669 struct window *w;
671 CHECK_VALID_WINDOW (window);
672 w = XWINDOW (window);
673 if (WINDOW_LEAF_P (w))
674 error ("Combination limit is meaningful for internal windows only");
675 return w->combination_limit;
678 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
679 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
680 WINDOW must be a valid window used in horizontal or vertical combination.
681 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
682 siblings. LIMIT t means that child windows of WINDOW are never
683 \(re-)combined with WINDOW's siblings. Other values are reserved for
684 future use. */)
685 (Lisp_Object window, Lisp_Object limit)
687 struct window *w;
689 CHECK_VALID_WINDOW (window);
690 w = XWINDOW (window);
691 if (WINDOW_LEAF_P (w))
692 error ("Combination limit is meaningful for internal windows only");
693 wset_combination_limit (w, limit);
694 return limit;
697 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
698 doc: /* Return the use time of window WINDOW.
699 WINDOW must be a live window and defaults to the selected one.
700 The window with the highest use time is the most recently selected
701 one. The window with the lowest use time is the least recently
702 selected one. */)
703 (Lisp_Object window)
705 return make_number (decode_live_window (window)->use_time);
708 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
709 doc: /* Return the total height, in lines, of window WINDOW.
710 WINDOW must be a valid window and defaults to the selected one.
712 The return value includes the mode line and header line, if any.
713 If WINDOW is an internal window, the total height is the height
714 of the screen areas spanned by its children.
716 On a graphical display, this total height is reported as an
717 integer multiple of the default character height. */)
718 (Lisp_Object window)
720 return make_number (decode_valid_window (window)->total_lines);
723 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
724 doc: /* Return the total width, in columns, of window WINDOW.
725 WINDOW must be a valid window and defaults to the selected one.
727 The return value includes any vertical dividers or scroll bars
728 belonging to WINDOW. If WINDOW is an internal window, the total width
729 is the width of the screen areas spanned by its children.
731 On a graphical display, this total width is reported as an
732 integer multiple of the default character width. */)
733 (Lisp_Object window)
735 return make_number (decode_valid_window (window)->total_cols);
738 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
739 doc: /* Return the new total size of window WINDOW.
740 WINDOW must be a valid window and defaults to the selected one. */)
741 (Lisp_Object window)
743 return decode_valid_window (window)->new_total;
746 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
747 doc: /* Return the normal height of window WINDOW.
748 WINDOW must be a valid window and defaults to the selected one.
749 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
750 (Lisp_Object window, Lisp_Object horizontal)
752 struct window *w = decode_valid_window (window);
754 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
757 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
758 doc: /* Return new normal size of window WINDOW.
759 WINDOW must be a valid window and defaults to the selected one. */)
760 (Lisp_Object window)
762 return decode_valid_window (window)->new_normal;
765 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
766 doc: /* Return left column of window WINDOW.
767 This is the distance, in columns, between the left edge of WINDOW and
768 the left edge of the frame's window area. For instance, the return
769 value is 0 if there is no window to the left of WINDOW.
771 WINDOW must be a valid window and defaults to the selected one. */)
772 (Lisp_Object window)
774 return make_number (decode_valid_window (window)->left_col);
777 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
778 doc: /* Return top line of window WINDOW.
779 This is the distance, in lines, between the top of WINDOW and the top
780 of the frame's window area. For instance, the return value is 0 if
781 there is no window above WINDOW.
783 WINDOW must be a valid window and defaults to the selected one. */)
784 (Lisp_Object window)
786 return make_number (decode_valid_window (window)->top_line);
789 /* Return the number of lines of W's body. Don't count any mode or
790 header line of W. */
792 static int
793 window_body_lines (struct window *w)
795 int height = w->total_lines;
797 if (!MINI_WINDOW_P (w))
799 if (WINDOW_WANTS_MODELINE_P (w))
800 --height;
801 if (WINDOW_WANTS_HEADER_LINE_P (w))
802 --height;
805 return height;
808 /* Return the number of columns of W's body. Don't count columns
809 occupied by the scroll bar or the vertical bar separating W from its
810 right sibling. On window-systems don't count fringes or display
811 margins either. */
814 window_body_cols (struct window *w)
816 struct frame *f = XFRAME (WINDOW_FRAME (w));
817 int width = w->total_cols;
819 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
820 /* Scroll bars occupy a few columns. */
821 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
822 else if (!FRAME_WINDOW_P (f)
823 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
824 /* The column of `|' characters separating side-by-side windows
825 occupies one column only. */
826 width -= 1;
828 /* Display margins cannot be used for normal text. */
829 width -= WINDOW_LEFT_MARGIN_COLS (w) + WINDOW_RIGHT_MARGIN_COLS (w);
831 if (FRAME_WINDOW_P (f))
832 /* On window-systems, fringes cannot be used for normal text. */
833 width -= WINDOW_FRINGE_COLS (w);
835 return width;
838 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 1, 0,
839 doc: /* Return the height, in lines, of WINDOW's text area.
840 WINDOW must be a live window and defaults to the selected one.
842 The returned height does not include the mode line or header line.
843 On a graphical display, the height is expressed as an integer multiple
844 of the default character height. If a line at the bottom of the text
845 area is only partially visible, that counts as a whole line; to
846 exclude partially-visible lines, use `window-text-height'. */)
847 (Lisp_Object window)
849 return make_number (window_body_lines (decode_live_window (window)));
852 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 1, 0,
853 doc: /* Return the width, in columns, of WINDOW's text area.
854 WINDOW must be a live window and defaults to the selected one.
856 The return value does not include any vertical dividers, fringe or
857 marginal areas, or scroll bars. On a graphical display, the width is
858 expressed as an integer multiple of the default character width. */)
859 (Lisp_Object window)
861 return make_number (window_body_cols (decode_live_window (window)));
864 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
865 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
866 WINDOW must be a live window and defaults to the selected one. */)
867 (Lisp_Object window)
869 return make_number (decode_live_window (window)->hscroll);
872 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
873 range, returning the new amount as a fixnum. */
874 static Lisp_Object
875 set_window_hscroll (struct window *w, EMACS_INT hscroll)
877 /* Horizontal scrolling has problems with large scroll amounts.
878 It's too slow with long lines, and even with small lines the
879 display can be messed up. For now, though, impose only the limits
880 required by the internal representation: horizontal scrolling must
881 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
882 (since it's stored in a ptrdiff_t). */
883 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
884 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
886 /* Prevent redisplay shortcuts when changing the hscroll. */
887 if (w->hscroll != new_hscroll)
888 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
890 w->hscroll = new_hscroll;
891 return make_number (new_hscroll);
894 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
895 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
896 WINDOW must be a live window and defaults to the selected one.
897 Clip the number to a reasonable value if out of range.
898 Return the new number. NCOL should be zero or positive.
900 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
901 window so that the location of point moves off-window. */)
902 (Lisp_Object window, Lisp_Object ncol)
904 CHECK_NUMBER (ncol);
905 return set_window_hscroll (decode_live_window (window), XINT (ncol));
908 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
909 Swindow_redisplay_end_trigger, 0, 1, 0,
910 doc: /* Return WINDOW's redisplay end trigger value.
911 WINDOW must be a live window and defaults to the selected one.
912 See `set-window-redisplay-end-trigger' for more information. */)
913 (Lisp_Object window)
915 return decode_live_window (window)->redisplay_end_trigger;
918 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
919 Sset_window_redisplay_end_trigger, 2, 2, 0,
920 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
921 WINDOW must be a live window and defaults to the selected one. VALUE
922 should be a buffer position (typically a marker) or nil. If it is a
923 buffer position, then if redisplay in WINDOW reaches a position beyond
924 VALUE, the functions in `redisplay-end-trigger-functions' are called
925 with two arguments: WINDOW, and the end trigger value. Afterwards the
926 end-trigger value is reset to nil. */)
927 (register Lisp_Object window, Lisp_Object value)
929 wset_redisplay_end_trigger (decode_live_window (window), value);
930 return value;
933 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
934 doc: /* Return a list of the edge coordinates of WINDOW.
935 WINDOW must be a valid window and defaults to the selected one.
937 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
938 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
939 0 at top left corner of frame.
941 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
942 is one more than the bottommost row occupied by WINDOW. The edges
943 include the space used by WINDOW's scroll bar, display margins, fringes,
944 header line, and/or mode line. For the edges of just the text area, use
945 `window-inside-edges'. */)
946 (Lisp_Object window)
948 register struct window *w = decode_valid_window (window);
950 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
951 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
954 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
955 doc: /* Return a list of the edge pixel coordinates of WINDOW.
956 WINDOW must be a valid window and defaults to the selected one.
958 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
959 0, 0 at the top left corner of the frame.
961 RIGHT is one more than the rightmost x position occupied by WINDOW.
962 BOTTOM is one more than the bottommost y position occupied by WINDOW.
963 The pixel edges include the space used by WINDOW's scroll bar, display
964 margins, fringes, header line, and/or mode line. For the pixel edges
965 of just the text area, use `window-inside-pixel-edges'. */)
966 (Lisp_Object window)
968 register struct window *w = decode_valid_window (window);
970 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
971 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
974 static void
975 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
977 struct frame *f = XFRAME (w->frame);
978 *add_y = f->top_pos;
979 #ifdef FRAME_MENUBAR_HEIGHT
980 *add_y += FRAME_MENUBAR_HEIGHT (f);
981 #endif
982 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
983 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
984 #elif FRAME_TOOLBAR_HEIGHT
985 *add_y += FRAME_TOOLBAR_HEIGHT (f);
986 #endif
987 #ifdef FRAME_NS_TITLEBAR_HEIGHT
988 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
989 #endif
990 *add_x = f->left_pos;
991 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
992 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
993 #endif
996 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
997 Swindow_absolute_pixel_edges, 0, 1, 0,
998 doc: /* Return a list of the edge pixel coordinates of WINDOW.
999 WINDOW must be a valid window and defaults to the selected one.
1001 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1002 0, 0 at the top left corner of the display.
1004 RIGHT is one more than the rightmost x position occupied by WINDOW.
1005 BOTTOM is one more than the bottommost y position occupied by WINDOW.
1006 The pixel edges include the space used by WINDOW's scroll bar, display
1007 margins, fringes, header line, and/or mode line. For the pixel edges
1008 of just the text area, use `window-inside-absolute-pixel-edges'. */)
1009 (Lisp_Object window)
1011 register struct window *w = decode_valid_window (window);
1012 int add_x, add_y;
1014 calc_absolute_offset (w, &add_x, &add_y);
1016 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
1017 WINDOW_TOP_EDGE_Y (w) + add_y,
1018 WINDOW_RIGHT_EDGE_X (w) + add_x,
1019 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1022 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1023 doc: /* Return a list of the edge coordinates of WINDOW.
1024 WINDOW must be a live window and defaults to the selected one.
1026 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1027 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1028 0 at top left corner of frame.
1030 RIGHT is one more than the rightmost column of WINDOW's text area.
1031 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1032 inside edges do not include the space used by the WINDOW's scroll bar,
1033 display margins, fringes, header line, and/or mode line. */)
1034 (Lisp_Object window)
1036 register struct window *w = decode_live_window (window);
1038 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1039 + WINDOW_LEFT_MARGIN_COLS (w)
1040 + WINDOW_LEFT_FRINGE_COLS (w)),
1041 (WINDOW_TOP_EDGE_LINE (w)
1042 + WINDOW_HEADER_LINE_LINES (w)),
1043 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1044 - WINDOW_RIGHT_MARGIN_COLS (w)
1045 - WINDOW_RIGHT_FRINGE_COLS (w)),
1046 (WINDOW_BOTTOM_EDGE_LINE (w)
1047 - WINDOW_MODE_LINE_LINES (w)));
1050 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1051 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1052 WINDOW must be a live window and defaults to the selected one.
1054 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1055 (0,0) at the top left corner of the frame's window area.
1057 RIGHT is one more than the rightmost x position of WINDOW's text area.
1058 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1059 The inside edges do not include the space used by WINDOW's scroll bar,
1060 display margins, fringes, header line, and/or mode line. */)
1061 (Lisp_Object window)
1063 register struct window *w = decode_live_window (window);
1065 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1066 + WINDOW_LEFT_MARGIN_WIDTH (w)
1067 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1068 (WINDOW_TOP_EDGE_Y (w)
1069 + WINDOW_HEADER_LINE_HEIGHT (w)),
1070 (WINDOW_BOX_RIGHT_EDGE_X (w)
1071 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1072 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1073 (WINDOW_BOTTOM_EDGE_Y (w)
1074 - WINDOW_MODE_LINE_HEIGHT (w)));
1077 DEFUN ("window-inside-absolute-pixel-edges",
1078 Fwindow_inside_absolute_pixel_edges,
1079 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1080 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1081 WINDOW must be a live window and defaults to the selected one.
1083 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1084 (0,0) at the top left corner of the frame's window area.
1086 RIGHT is one more than the rightmost x position of WINDOW's text area.
1087 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1088 The inside edges do not include the space used by WINDOW's scroll bar,
1089 display margins, fringes, header line, and/or mode line. */)
1090 (Lisp_Object window)
1092 register struct window *w = decode_live_window (window);
1093 int add_x, add_y;
1095 calc_absolute_offset (w, &add_x, &add_y);
1097 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1098 + WINDOW_LEFT_MARGIN_WIDTH (w)
1099 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1100 (WINDOW_TOP_EDGE_Y (w)
1101 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1102 (WINDOW_BOX_RIGHT_EDGE_X (w)
1103 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1104 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1105 (WINDOW_BOTTOM_EDGE_Y (w)
1106 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1109 /* Test if the character at column X, row Y is within window W.
1110 If it is not, return ON_NOTHING;
1111 if it is in the window's text area, return ON_TEXT;
1112 if it is on the window's modeline, return ON_MODE_LINE;
1113 if it is on the border between the window and its right sibling,
1114 return ON_VERTICAL_BORDER.
1115 if it is on a scroll bar, return ON_SCROLL_BAR.
1116 if it is on the window's top line, return ON_HEADER_LINE;
1117 if it is in left or right fringe of the window,
1118 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1119 if it is in the marginal area to the left/right of the window,
1120 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1122 X and Y are frame relative pixel coordinates. */
1124 static enum window_part
1125 coordinates_in_window (register struct window *w, int x, int y)
1127 struct frame *f = XFRAME (WINDOW_FRAME (w));
1128 enum window_part part;
1129 int ux = FRAME_COLUMN_WIDTH (f);
1130 int left_x = WINDOW_LEFT_EDGE_X (w);
1131 int right_x = WINDOW_RIGHT_EDGE_X (w);
1132 int top_y = WINDOW_TOP_EDGE_Y (w);
1133 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1134 /* The width of the area where the vertical line can be dragged.
1135 (Between mode lines for instance. */
1136 int grabbable_width = ux;
1137 int lmargin_width, rmargin_width, text_left, text_right;
1139 /* Outside any interesting row or column? */
1140 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1141 return ON_NOTHING;
1143 /* On the mode line or header line? */
1144 if ((WINDOW_WANTS_MODELINE_P (w)
1145 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
1146 && (part = ON_MODE_LINE))
1147 || (WINDOW_WANTS_HEADER_LINE_P (w)
1148 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1149 && (part = ON_HEADER_LINE)))
1151 /* If it's under/over the scroll bar portion of the mode/header
1152 line, say it's on the vertical line. That's to be able to
1153 resize windows horizontally in case we're using toolkit scroll
1154 bars. Note: If scrollbars are on the left, the window that
1155 must be eventually resized is that on the left of WINDOW. */
1156 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1157 && !WINDOW_LEFTMOST_P (w)
1158 && eabs (x - left_x) < grabbable_width)
1159 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1160 && !WINDOW_RIGHTMOST_P (w)
1161 && eabs (x - right_x) < grabbable_width))
1162 return ON_VERTICAL_BORDER;
1163 else
1164 return part;
1167 /* In what's below, we subtract 1 when computing right_x because we
1168 want the rightmost pixel, which is given by left_pixel+width-1. */
1169 if (w->pseudo_window_p)
1171 left_x = 0;
1172 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
1174 else
1176 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1177 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1180 /* Outside any interesting column? */
1181 if (x < left_x || x > right_x)
1182 return ON_SCROLL_BAR;
1184 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1185 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1187 text_left = window_box_left (w, TEXT_AREA);
1188 text_right = text_left + window_box_width (w, TEXT_AREA);
1190 if (FRAME_WINDOW_P (f))
1192 if (!w->pseudo_window_p
1193 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1194 && !WINDOW_RIGHTMOST_P (w)
1195 && (eabs (x - right_x) < grabbable_width))
1196 return ON_VERTICAL_BORDER;
1198 /* Need to say "x > right_x" rather than >=, since on character
1199 terminals, the vertical line's x coordinate is right_x. */
1200 else if (!w->pseudo_window_p
1201 && !WINDOW_RIGHTMOST_P (w)
1202 && x > right_x - ux)
1203 return ON_VERTICAL_BORDER;
1205 if (x < text_left)
1207 if (lmargin_width > 0
1208 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1209 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1210 : (x < left_x + lmargin_width)))
1211 return ON_LEFT_MARGIN;
1213 return ON_LEFT_FRINGE;
1216 if (x >= text_right)
1218 if (rmargin_width > 0
1219 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1220 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1221 : (x >= right_x - rmargin_width)))
1222 return ON_RIGHT_MARGIN;
1224 return ON_RIGHT_FRINGE;
1227 /* Everything special ruled out - must be on text area */
1228 return ON_TEXT;
1231 /* Take X is the frame-relative pixel x-coordinate, and return the
1232 x-coordinate relative to part PART of window W. */
1234 window_relative_x_coord (struct window *w, enum window_part part, int x)
1236 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1238 switch (part)
1240 case ON_TEXT:
1241 return x - window_box_left (w, TEXT_AREA);
1243 case ON_LEFT_FRINGE:
1244 return x - left_x;
1246 case ON_RIGHT_FRINGE:
1247 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1249 case ON_LEFT_MARGIN:
1250 return (x - left_x
1251 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1252 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1254 case ON_RIGHT_MARGIN:
1255 return (x + 1
1256 - ((w->pseudo_window_p)
1257 ? WINDOW_TOTAL_WIDTH (w)
1258 : WINDOW_BOX_RIGHT_EDGE_X (w))
1259 + window_box_width (w, RIGHT_MARGIN_AREA)
1260 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1261 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1264 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1265 return 0;
1269 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1270 Scoordinates_in_window_p, 2, 2, 0,
1271 doc: /* Return non-nil if COORDINATES are in WINDOW.
1272 WINDOW must be a live window and defaults to the selected one.
1273 COORDINATES is a cons of the form (X . Y), X and Y being distances
1274 measured in characters from the upper-left corner of the frame.
1275 \(0 . 0) denotes the character in the upper left corner of the
1276 frame.
1277 If COORDINATES are in the text portion of WINDOW,
1278 the coordinates relative to the window are returned.
1279 If they are in the mode line of WINDOW, `mode-line' is returned.
1280 If they are in the top mode line of WINDOW, `header-line' is returned.
1281 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1282 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1283 If they are on the border between WINDOW and its right sibling,
1284 `vertical-line' is returned.
1285 If they are in the windows's left or right marginal areas, `left-margin'\n\
1286 or `right-margin' is returned. */)
1287 (register Lisp_Object coordinates, Lisp_Object window)
1289 struct window *w;
1290 struct frame *f;
1291 int x, y;
1292 Lisp_Object lx, ly;
1294 w = decode_live_window (window);
1295 f = XFRAME (w->frame);
1296 CHECK_CONS (coordinates);
1297 lx = Fcar (coordinates);
1298 ly = Fcdr (coordinates);
1299 CHECK_NUMBER_OR_FLOAT (lx);
1300 CHECK_NUMBER_OR_FLOAT (ly);
1301 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1302 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1304 switch (coordinates_in_window (w, x, y))
1306 case ON_NOTHING:
1307 return Qnil;
1309 case ON_TEXT:
1310 /* Convert X and Y to window relative pixel coordinates, and
1311 return the canonical char units. */
1312 x -= window_box_left (w, TEXT_AREA);
1313 y -= WINDOW_TOP_EDGE_Y (w);
1314 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1315 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1317 case ON_MODE_LINE:
1318 return Qmode_line;
1320 case ON_VERTICAL_BORDER:
1321 return Qvertical_line;
1323 case ON_HEADER_LINE:
1324 return Qheader_line;
1326 case ON_LEFT_FRINGE:
1327 return Qleft_fringe;
1329 case ON_RIGHT_FRINGE:
1330 return Qright_fringe;
1332 case ON_LEFT_MARGIN:
1333 return Qleft_margin;
1335 case ON_RIGHT_MARGIN:
1336 return Qright_margin;
1338 case ON_SCROLL_BAR:
1339 /* Historically we are supposed to return nil in this case. */
1340 return Qnil;
1342 default:
1343 emacs_abort ();
1348 /* Callback for foreach_window, used in window_from_coordinates.
1349 Check if window W contains coordinates specified by USER_DATA which
1350 is actually a pointer to a struct check_window_data CW.
1352 Check if window W contains coordinates *CW->x and *CW->y. If it
1353 does, return W in *CW->window, as Lisp_Object, and return in
1354 *CW->part the part of the window under coordinates *X,*Y. Return
1355 zero from this function to stop iterating over windows. */
1357 struct check_window_data
1359 Lisp_Object *window;
1360 int x, y;
1361 enum window_part *part;
1364 static int
1365 check_window_containing (struct window *w, void *user_data)
1367 struct check_window_data *cw = (struct check_window_data *) user_data;
1368 enum window_part found;
1369 int continue_p = 1;
1371 found = coordinates_in_window (w, cw->x, cw->y);
1372 if (found != ON_NOTHING)
1374 *cw->part = found;
1375 XSETWINDOW (*cw->window, w);
1376 continue_p = 0;
1379 return continue_p;
1383 /* Find the window containing frame-relative pixel position X/Y and
1384 return it as a Lisp_Object.
1386 If X, Y is on one of the window's special `window_part' elements,
1387 set *PART to the id of that element.
1389 If there is no window under X, Y return nil and leave *PART
1390 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1392 This function was previously implemented with a loop cycling over
1393 windows with Fnext_window, and starting with the frame's selected
1394 window. It turned out that this doesn't work with an
1395 implementation of next_window using Vwindow_list, because
1396 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1397 tree of F when this function is called asynchronously from
1398 note_mouse_highlight. The original loop didn't terminate in this
1399 case. */
1401 Lisp_Object
1402 window_from_coordinates (struct frame *f, int x, int y,
1403 enum window_part *part, bool tool_bar_p)
1405 Lisp_Object window;
1406 struct check_window_data cw;
1407 enum window_part dummy;
1409 if (part == 0)
1410 part = &dummy;
1412 window = Qnil;
1413 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1414 foreach_window (f, check_window_containing, &cw);
1416 /* If not found above, see if it's in the tool bar window, if a tool
1417 bar exists. */
1418 if (NILP (window)
1419 && tool_bar_p
1420 && WINDOWP (f->tool_bar_window)
1421 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1422 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1423 != ON_NOTHING))
1425 *part = ON_TEXT;
1426 window = f->tool_bar_window;
1429 return window;
1432 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1433 doc: /* Return window containing coordinates X and Y on FRAME.
1434 FRAME must be a live frame and defaults to the selected one.
1435 The top left corner of the frame is considered to be row 0,
1436 column 0. */)
1437 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1439 struct frame *f = decode_live_frame (frame);
1441 /* Check that arguments are integers or floats. */
1442 CHECK_NUMBER_OR_FLOAT (x);
1443 CHECK_NUMBER_OR_FLOAT (y);
1445 return window_from_coordinates (f,
1446 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1447 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1448 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1449 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1450 0, 0);
1453 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1454 doc: /* Return current value of point in WINDOW.
1455 WINDOW must be a live window and defaults to the selected one.
1457 For a nonselected window, this is the value point would have if that
1458 window were selected.
1460 Note that, when WINDOW is selected, the value returned is the same as
1461 that returned by `point' for WINDOW's buffer. It would be more strictly
1462 correct to return the top-level value of `point', outside of any
1463 `save-excursion' forms. But that is hard to define. */)
1464 (Lisp_Object window)
1466 register struct window *w = decode_live_window (window);
1468 if (w == XWINDOW (selected_window))
1469 return make_number (BUF_PT (XBUFFER (w->contents)));
1470 else
1471 return Fmarker_position (w->pointm);
1474 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1475 doc: /* Return position at which display currently starts in WINDOW.
1476 WINDOW must be a live window and defaults to the selected one.
1477 This is updated by redisplay or by calling `set-window-start'. */)
1478 (Lisp_Object window)
1480 return Fmarker_position (decode_live_window (window)->start);
1483 /* This is text temporarily removed from the doc string below.
1485 This function returns nil if the position is not currently known.
1486 That happens when redisplay is preempted and doesn't finish.
1487 If in that case you want to compute where the end of the window would
1488 have been if redisplay had finished, do this:
1489 (save-excursion
1490 (goto-char (window-start window))
1491 (vertical-motion (1- (window-height window)) window)
1492 (point))") */
1494 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1495 doc: /* Return position at which display currently ends in WINDOW.
1496 WINDOW must be a live window and defaults to the selected one.
1497 This is updated by redisplay, when it runs to completion.
1498 Simply changing the buffer text or setting `window-start'
1499 does not update this value.
1500 Return nil if there is no recorded value. (This can happen if the
1501 last redisplay of WINDOW was preempted, and did not finish.)
1502 If UPDATE is non-nil, compute the up-to-date position
1503 if it isn't already recorded. */)
1504 (Lisp_Object window, Lisp_Object update)
1506 Lisp_Object value;
1507 struct window *w = decode_live_window (window);
1508 Lisp_Object buf;
1509 struct buffer *b;
1511 buf = w->contents;
1512 CHECK_BUFFER (buf);
1513 b = XBUFFER (buf);
1515 if (! NILP (update)
1516 && (windows_or_buffers_changed
1517 || !w->window_end_valid
1518 || b->clip_changed
1519 || b->prevent_redisplay_optimizations_p
1520 || window_outdated (w))
1521 && !noninteractive)
1523 struct text_pos startp;
1524 ptrdiff_t charpos = marker_position (w->start);
1525 struct it it;
1526 struct buffer *old_buffer = NULL;
1527 void *itdata = NULL;
1529 /* Cannot use Fvertical_motion because that function doesn't
1530 cope with variable-height lines. */
1531 if (b != current_buffer)
1533 old_buffer = current_buffer;
1534 set_buffer_internal (b);
1537 /* In case W->start is out of the range, use something
1538 reasonable. This situation occurred when loading a file with
1539 `-l' containing a call to `rmail' with subsequent other
1540 commands. At the end, W->start happened to be BEG, while
1541 rmail had already narrowed the buffer. */
1542 if (charpos < BEGV)
1543 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1544 else if (charpos > ZV)
1545 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1546 else
1547 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1549 itdata = bidi_shelve_cache ();
1550 start_display (&it, w, startp);
1551 move_it_vertically (&it, window_box_height (w));
1552 if (it.current_y < it.last_visible_y)
1553 move_it_past_eol (&it);
1554 value = make_number (IT_CHARPOS (it));
1555 bidi_unshelve_cache (itdata, 0);
1557 if (old_buffer)
1558 set_buffer_internal (old_buffer);
1560 else
1561 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1563 return value;
1566 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1567 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1568 WINDOW must be a live window and defaults to the selected one.
1569 Return POS. */)
1570 (Lisp_Object window, Lisp_Object pos)
1572 register struct window *w = decode_live_window (window);
1574 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1576 if (w == XWINDOW (selected_window))
1578 if (XBUFFER (w->contents) == current_buffer)
1579 Fgoto_char (pos);
1580 else
1582 struct buffer *old_buffer = current_buffer;
1584 /* ... but here we want to catch type error before buffer change. */
1585 CHECK_NUMBER_COERCE_MARKER (pos);
1586 set_buffer_internal (XBUFFER (w->contents));
1587 Fgoto_char (pos);
1588 set_buffer_internal (old_buffer);
1591 else
1593 set_marker_restricted (w->pointm, pos, w->contents);
1594 /* We have to make sure that redisplay updates the window to show
1595 the new value of point. */
1596 ++windows_or_buffers_changed;
1599 return pos;
1602 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1603 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1604 WINDOW must be a live window and defaults to the selected one. Return
1605 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1606 overriding motion of point in order to display at this exact start. */)
1607 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1609 register struct window *w = decode_live_window (window);
1611 set_marker_restricted (w->start, pos, w->contents);
1612 /* This is not right, but much easier than doing what is right. */
1613 w->start_at_line_beg = 0;
1614 if (NILP (noforce))
1615 w->force_start = 1;
1616 w->update_mode_line = 1;
1617 w->last_modified = 0;
1618 w->last_overlay_modified = 0;
1619 if (!EQ (window, selected_window))
1620 windows_or_buffers_changed++;
1622 return pos;
1625 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1626 Spos_visible_in_window_p, 0, 3, 0,
1627 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1628 WINDOW must be a live window and defaults to the selected one.
1630 Return nil if that position is scrolled vertically out of view. If a
1631 character is only partially visible, nil is returned, unless the
1632 optional argument PARTIALLY is non-nil. If POS is only out of view
1633 because of horizontal scrolling, return non-nil. If POS is t, it
1634 specifies the position of the last visible glyph in WINDOW. POS
1635 defaults to point in WINDOW; WINDOW defaults to the selected window.
1637 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1638 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1639 where X and Y are the pixel coordinates relative to the top left corner
1640 of the window. The remaining elements are omitted if the character after
1641 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1642 off-window at the top and bottom of the screen line ("row") containing
1643 POS, ROWH is the visible height of that row, and VPOS is the row number
1644 \(zero-based). */)
1645 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1647 register struct window *w;
1648 register EMACS_INT posint;
1649 register struct buffer *buf;
1650 struct text_pos top;
1651 Lisp_Object in_window = Qnil;
1652 int rtop, rbot, rowh, vpos, fully_p = 1;
1653 int x, y;
1655 w = decode_live_window (window);
1656 buf = XBUFFER (w->contents);
1657 SET_TEXT_POS_FROM_MARKER (top, w->start);
1659 if (EQ (pos, Qt))
1660 posint = -1;
1661 else if (!NILP (pos))
1663 CHECK_NUMBER_COERCE_MARKER (pos);
1664 posint = XINT (pos);
1666 else if (w == XWINDOW (selected_window))
1667 posint = PT;
1668 else
1669 posint = marker_position (w->pointm);
1671 /* If position is above window start or outside buffer boundaries,
1672 or if window start is out of range, position is not visible. */
1673 if ((EQ (pos, Qt)
1674 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1675 && CHARPOS (top) >= BUF_BEGV (buf)
1676 && CHARPOS (top) <= BUF_ZV (buf)
1677 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1678 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1679 in_window = Qt;
1681 if (!NILP (in_window) && !NILP (partially))
1683 Lisp_Object part = Qnil;
1684 if (!fully_p)
1685 part = list4i (rtop, rbot, rowh, vpos);
1686 in_window = Fcons (make_number (x),
1687 Fcons (make_number (y), part));
1690 return in_window;
1693 DEFUN ("window-line-height", Fwindow_line_height,
1694 Swindow_line_height, 0, 2, 0,
1695 doc: /* Return height in pixels of text line LINE in window WINDOW.
1696 WINDOW must be a live window and defaults to the selected one.
1698 Return height of current line if LINE is omitted or nil. Return height of
1699 header or mode line if LINE is `header-line' or `mode-line'.
1700 Otherwise, LINE is a text line number starting from 0. A negative number
1701 counts from the end of the window.
1703 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1704 in pixels of the visible part of the line, VPOS and YPOS are the
1705 vertical position in lines and pixels of the line, relative to the top
1706 of the first text line, and OFFBOT is the number of off-window pixels at
1707 the bottom of the text line. If there are off-window pixels at the top
1708 of the (first) text line, YPOS is negative.
1710 Return nil if window display is not up-to-date. In that case, use
1711 `pos-visible-in-window-p' to obtain the information. */)
1712 (Lisp_Object line, Lisp_Object window)
1714 register struct window *w;
1715 register struct buffer *b;
1716 struct glyph_row *row, *end_row;
1717 int max_y, crop, i;
1718 EMACS_INT n;
1720 w = decode_live_window (window);
1722 if (noninteractive || w->pseudo_window_p)
1723 return Qnil;
1725 CHECK_BUFFER (w->contents);
1726 b = XBUFFER (w->contents);
1728 /* Fail if current matrix is not up-to-date. */
1729 if (!w->window_end_valid
1730 || windows_or_buffers_changed
1731 || b->clip_changed
1732 || b->prevent_redisplay_optimizations_p
1733 || window_outdated (w))
1734 return Qnil;
1736 if (NILP (line))
1738 i = w->cursor.vpos;
1739 if (i < 0 || i >= w->current_matrix->nrows
1740 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1741 return Qnil;
1742 max_y = window_text_bottom_y (w);
1743 goto found_row;
1746 if (EQ (line, Qheader_line))
1748 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1749 return Qnil;
1750 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1751 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1754 if (EQ (line, Qmode_line))
1756 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1757 return (row->enabled_p ?
1758 list4i (row->height,
1759 0, /* not accurate */
1760 (WINDOW_HEADER_LINE_HEIGHT (w)
1761 + window_text_bottom_y (w)),
1763 : Qnil);
1766 CHECK_NUMBER (line);
1767 n = XINT (line);
1769 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1770 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1771 max_y = window_text_bottom_y (w);
1772 i = 0;
1774 while ((n < 0 || i < n)
1775 && row <= end_row && row->enabled_p
1776 && row->y + row->height < max_y)
1777 row++, i++;
1779 if (row > end_row || !row->enabled_p)
1780 return Qnil;
1782 if (++n < 0)
1784 if (-n > i)
1785 return Qnil;
1786 row += n;
1787 i += n;
1790 found_row:
1791 crop = max (0, (row->y + row->height) - max_y);
1792 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1795 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1796 0, 1, 0,
1797 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1798 More precisely, return the value assigned by the last call of
1799 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1800 never called with WINDOW as its argument, or the value set by that
1801 function was internally reset since its last call. WINDOW must be a
1802 live window and defaults to the selected one.
1804 When a window is dedicated to its buffer, `display-buffer' will refrain
1805 from displaying another buffer in it. `get-lru-window' and
1806 `get-largest-window' treat dedicated windows specially.
1807 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1808 `kill-buffer' can delete a dedicated window and the containing frame.
1810 Functions like `set-window-buffer' may change the buffer displayed by a
1811 window, unless that window is "strongly" dedicated to its buffer, that
1812 is the value returned by `window-dedicated-p' is t. */)
1813 (Lisp_Object window)
1815 return decode_live_window (window)->dedicated;
1818 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1819 Sset_window_dedicated_p, 2, 2, 0,
1820 doc: /* Mark WINDOW as dedicated according to FLAG.
1821 WINDOW must be a live window and defaults to the selected one. FLAG
1822 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1823 mark WINDOW as non-dedicated. Return FLAG.
1825 When a window is dedicated to its buffer, `display-buffer' will refrain
1826 from displaying another buffer in it. `get-lru-window' and
1827 `get-largest-window' treat dedicated windows specially.
1828 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1829 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1830 and the containing frame.
1832 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1833 its buffer. Functions like `set-window-buffer' may change the buffer
1834 displayed by a window, unless that window is strongly dedicated to its
1835 buffer. If and when `set-window-buffer' displays another buffer in a
1836 window, it also makes sure that the window is no more dedicated. */)
1837 (Lisp_Object window, Lisp_Object flag)
1839 wset_dedicated (decode_live_window (window), flag);
1840 return flag;
1843 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1844 0, 1, 0,
1845 doc: /* Return buffers previously shown in WINDOW.
1846 WINDOW must be a live window and defaults to the selected one.
1848 The return value is a list of elements (BUFFER WINDOW-START POS),
1849 where BUFFER is a buffer, WINDOW-START is the start position of the
1850 window for that buffer, and POS is a window-specific point value. */)
1851 (Lisp_Object window)
1853 return decode_live_window (window)->prev_buffers;
1856 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1857 Sset_window_prev_buffers, 2, 2, 0,
1858 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1859 WINDOW must be a live window and defaults to the selected one.
1861 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1862 where BUFFER is a buffer, WINDOW-START is the start position of the
1863 window for that buffer, and POS is a window-specific point value. */)
1864 (Lisp_Object window, Lisp_Object prev_buffers)
1866 wset_prev_buffers (decode_live_window (window), prev_buffers);
1867 return prev_buffers;
1870 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1871 0, 1, 0,
1872 doc: /* Return list of buffers recently re-shown in WINDOW.
1873 WINDOW must be a live window and defaults to the selected one. */)
1874 (Lisp_Object window)
1876 return decode_live_window (window)->next_buffers;
1879 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1880 Sset_window_next_buffers, 2, 2, 0,
1881 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1882 WINDOW must be a live window and defaults to the selected one.
1883 NEXT-BUFFERS should be a list of buffers. */)
1884 (Lisp_Object window, Lisp_Object next_buffers)
1886 wset_next_buffers (decode_live_window (window), next_buffers);
1887 return next_buffers;
1890 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1891 0, 1, 0,
1892 doc: /* Return the parameters of WINDOW and their values.
1893 WINDOW must be a valid window and defaults to the selected one. The
1894 return value is a list of elements of the form (PARAMETER . VALUE). */)
1895 (Lisp_Object window)
1897 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1900 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1901 2, 2, 0,
1902 doc: /* Return WINDOW's value for PARAMETER.
1903 WINDOW can be any window and defaults to the selected one. */)
1904 (Lisp_Object window, Lisp_Object parameter)
1906 Lisp_Object result;
1908 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1909 return CDR_SAFE (result);
1912 DEFUN ("set-window-parameter", Fset_window_parameter,
1913 Sset_window_parameter, 3, 3, 0,
1914 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1915 WINDOW can be any window and defaults to the selected one.
1916 Return VALUE. */)
1917 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1919 register struct window *w = decode_any_window (window);
1920 Lisp_Object old_alist_elt;
1922 old_alist_elt = Fassq (parameter, w->window_parameters);
1923 if (NILP (old_alist_elt))
1924 wset_window_parameters
1925 (w, Fcons (Fcons (parameter, value), w->window_parameters));
1926 else
1927 Fsetcdr (old_alist_elt, value);
1928 return value;
1931 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1932 0, 1, 0,
1933 doc: /* Return the display-table that WINDOW is using.
1934 WINDOW must be a live window and defaults to the selected one. */)
1935 (Lisp_Object window)
1937 return decode_live_window (window)->display_table;
1940 /* Get the display table for use on window W. This is either W's
1941 display table or W's buffer's display table. Ignore the specified
1942 tables if they are not valid; if no valid table is specified,
1943 return 0. */
1945 struct Lisp_Char_Table *
1946 window_display_table (struct window *w)
1948 struct Lisp_Char_Table *dp = NULL;
1950 if (DISP_TABLE_P (w->display_table))
1951 dp = XCHAR_TABLE (w->display_table);
1952 else if (BUFFERP (w->contents))
1954 struct buffer *b = XBUFFER (w->contents);
1956 if (DISP_TABLE_P (BVAR (b, display_table)))
1957 dp = XCHAR_TABLE (BVAR (b, display_table));
1958 else if (DISP_TABLE_P (Vstandard_display_table))
1959 dp = XCHAR_TABLE (Vstandard_display_table);
1962 return dp;
1965 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1966 doc: /* Set WINDOW's display-table to TABLE.
1967 WINDOW must be a live window and defaults to the selected one. */)
1968 (register Lisp_Object window, Lisp_Object table)
1970 wset_display_table (decode_live_window (window), table);
1971 return table;
1974 /* Record info on buffer window W is displaying
1975 when it is about to cease to display that buffer. */
1976 static void
1977 unshow_buffer (register struct window *w)
1979 Lisp_Object buf = w->contents;
1980 struct buffer *b = XBUFFER (buf);
1982 eassert (b == XMARKER (w->pointm)->buffer);
1984 #if 0
1985 if (w == XWINDOW (selected_window)
1986 || ! EQ (buf, XWINDOW (selected_window)->contents))
1987 /* Do this except when the selected window's buffer
1988 is being removed from some other window. */
1989 #endif
1990 /* last_window_start records the start position that this buffer
1991 had in the last window to be disconnected from it.
1992 Now that this statement is unconditional,
1993 it is possible for the buffer to be displayed in the
1994 selected window, while last_window_start reflects another
1995 window which was recently showing the same buffer.
1996 Some people might say that might be a good thing. Let's see. */
1997 b->last_window_start = marker_position (w->start);
1999 /* Point in the selected window's buffer
2000 is actually stored in that buffer, and the window's pointm isn't used.
2001 So don't clobber point in that buffer. */
2002 if (! EQ (buf, XWINDOW (selected_window)->contents)
2003 /* Don't clobber point in current buffer either (this could be
2004 useful in connection with bug#12208).
2005 && XBUFFER (buf) != current_buffer */
2006 /* This line helps to fix Horsley's testbug.el bug. */
2007 && !(WINDOWP (BVAR (b, last_selected_window))
2008 && w != XWINDOW (BVAR (b, last_selected_window))
2009 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
2010 temp_set_point_both (b,
2011 clip_to_bounds (BUF_BEGV (b),
2012 marker_position (w->pointm),
2013 BUF_ZV (b)),
2014 clip_to_bounds (BUF_BEGV_BYTE (b),
2015 marker_byte_position (w->pointm),
2016 BUF_ZV_BYTE (b)));
2018 if (WINDOWP (BVAR (b, last_selected_window))
2019 && w == XWINDOW (BVAR (b, last_selected_window)))
2020 bset_last_selected_window (b, Qnil);
2023 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2024 means change window structure only. Otherwise store geometry and
2025 other settings as well. */
2026 static void
2027 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2029 register Lisp_Object tem;
2030 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2032 /* If OLD is its frame's root window, then NEW is the new
2033 root window for that frame. */
2034 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2035 fset_root_window (XFRAME (o->frame), new);
2037 if (setflag)
2039 n->left_col = o->left_col;
2040 n->top_line = o->top_line;
2041 n->total_cols = o->total_cols;
2042 n->total_lines = o->total_lines;
2043 wset_normal_cols (n, o->normal_cols);
2044 wset_normal_cols (o, make_float (1.0));
2045 wset_normal_lines (n, o->normal_lines);
2046 wset_normal_lines (o, make_float (1.0));
2047 n->desired_matrix = n->current_matrix = 0;
2048 n->vscroll = 0;
2049 memset (&n->cursor, 0, sizeof (n->cursor));
2050 memset (&n->last_cursor, 0, sizeof (n->last_cursor));
2051 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2052 n->phys_cursor_type = -1;
2053 n->phys_cursor_width = -1;
2054 n->must_be_updated_p = 0;
2055 n->pseudo_window_p = 0;
2056 wset_window_end_vpos (n, make_number (0));
2057 wset_window_end_pos (n, make_number (0));
2058 n->window_end_valid = 0;
2059 n->frozen_window_start_p = 0;
2062 tem = o->next;
2063 wset_next (n, tem);
2064 if (!NILP (tem))
2065 wset_prev (XWINDOW (tem), new);
2067 tem = o->prev;
2068 wset_prev (n, tem);
2069 if (!NILP (tem))
2070 wset_next (XWINDOW (tem), new);
2072 tem = o->parent;
2073 wset_parent (n, tem);
2074 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2075 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2078 /* If window WINDOW and its parent window are iso-combined, merge
2079 WINDOW's children into those of its parent window and mark WINDOW as
2080 deleted. */
2082 static void
2083 recombine_windows (Lisp_Object window)
2085 struct window *w, *p, *c;
2086 Lisp_Object parent, child;
2087 bool horflag;
2089 w = XWINDOW (window);
2090 parent = w->parent;
2091 if (!NILP (parent) && NILP (w->combination_limit))
2093 p = XWINDOW (parent);
2094 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2095 && p->horizontal == w->horizontal)
2096 /* WINDOW and PARENT are both either a vertical or a horizontal
2097 combination. */
2099 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2100 child = w->contents;
2101 c = XWINDOW (child);
2103 /* Splice WINDOW's children into its parent's children and
2104 assign new normal sizes. */
2105 if (NILP (w->prev))
2106 wset_combination (p, horflag, child);
2107 else
2109 wset_prev (c, w->prev);
2110 wset_next (XWINDOW (w->prev), child);
2113 while (c)
2115 wset_parent (c, parent);
2117 if (horflag)
2118 wset_normal_cols (c,
2119 make_float ((double) c->total_cols
2120 / (double) p->total_cols));
2121 else
2122 wset_normal_lines (c,
2123 make_float ((double) c->total_lines
2124 / (double) p->total_lines));
2126 if (NILP (c->next))
2128 if (!NILP (w->next))
2130 wset_next (c, w->next);
2131 wset_prev (XWINDOW (c->next), child);
2134 c = 0;
2136 else
2138 child = c->next;
2139 c = XWINDOW (child);
2143 /* WINDOW can be deleted now. */
2144 wset_combination (w, 0, Qnil);
2149 /* If WINDOW can be deleted, delete it. */
2150 static void
2151 delete_deletable_window (Lisp_Object window)
2153 if (!NILP (call1 (Qwindow_deletable_p, window)))
2154 call1 (Qdelete_window, window);
2157 /***********************************************************************
2158 Window List
2159 ***********************************************************************/
2161 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2162 pointer. This is a callback function for foreach_window, used in
2163 the window_list function. */
2165 static int
2166 add_window_to_list (struct window *w, void *user_data)
2168 Lisp_Object *list = (Lisp_Object *) user_data;
2169 Lisp_Object window;
2170 XSETWINDOW (window, w);
2171 *list = Fcons (window, *list);
2172 return 1;
2176 /* Return a list of all windows, for use by next_window. If
2177 Vwindow_list is a list, return that list. Otherwise, build a new
2178 list, cache it in Vwindow_list, and return that. */
2180 static Lisp_Object
2181 window_list (void)
2183 if (!CONSP (Vwindow_list))
2185 Lisp_Object tail, frame;
2187 Vwindow_list = Qnil;
2188 FOR_EACH_FRAME (tail, frame)
2190 Lisp_Object args[2];
2192 /* We are visiting windows in canonical order, and add
2193 new windows at the front of args[1], which means we
2194 have to reverse this list at the end. */
2195 args[1] = Qnil;
2196 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2197 args[0] = Vwindow_list;
2198 args[1] = Fnreverse (args[1]);
2199 Vwindow_list = Fnconc (2, args);
2203 return Vwindow_list;
2207 /* Value is non-zero if WINDOW satisfies the constraints given by
2208 OWINDOW, MINIBUF and ALL_FRAMES.
2210 MINIBUF t means WINDOW may be minibuffer windows.
2211 `lambda' means WINDOW may not be a minibuffer window.
2212 a window means a specific minibuffer window
2214 ALL_FRAMES t means search all frames,
2215 nil means search just current frame,
2216 `visible' means search just visible frames on the
2217 current terminal,
2218 0 means search visible and iconified frames on the
2219 current terminal,
2220 a window means search the frame that window belongs to,
2221 a frame means consider windows on that frame, only. */
2223 static bool
2224 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2225 Lisp_Object minibuf, Lisp_Object all_frames)
2227 struct window *w = XWINDOW (window);
2228 struct frame *f = XFRAME (w->frame);
2229 bool candidate_p = 1;
2231 if (!BUFFERP (w->contents))
2232 candidate_p = 0;
2233 else if (MINI_WINDOW_P (w)
2234 && (EQ (minibuf, Qlambda)
2235 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2237 /* If MINIBUF is `lambda' don't consider any mini-windows.
2238 If it is a window, consider only that one. */
2239 candidate_p = 0;
2241 else if (EQ (all_frames, Qt))
2242 candidate_p = 1;
2243 else if (NILP (all_frames))
2245 eassert (WINDOWP (owindow));
2246 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2248 else if (EQ (all_frames, Qvisible))
2250 candidate_p = FRAME_VISIBLE_P (f)
2251 && (FRAME_TERMINAL (XFRAME (w->frame))
2252 == FRAME_TERMINAL (XFRAME (selected_frame)));
2255 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2257 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2258 #ifdef HAVE_X_WINDOWS
2259 /* Yuck!! If we've just created the frame and the
2260 window-manager requested the user to place it
2261 manually, the window may still not be considered
2262 `visible'. I'd argue it should be at least
2263 something like `iconified', but don't know how to do
2264 that yet. --Stef */
2265 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2266 && !f->output_data.x->has_been_visible)
2267 #endif
2269 && (FRAME_TERMINAL (XFRAME (w->frame))
2270 == FRAME_TERMINAL (XFRAME (selected_frame)));
2272 else if (WINDOWP (all_frames))
2273 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2274 || EQ (XWINDOW (all_frames)->frame, w->frame)
2275 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2276 else if (FRAMEP (all_frames))
2277 candidate_p = EQ (all_frames, w->frame);
2279 return candidate_p;
2283 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2284 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2285 MINIBUF, and ALL_FRAMES. */
2287 static void
2288 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2290 struct window *w = decode_live_window (*window);
2292 XSETWINDOW (*window, w);
2293 /* MINIBUF nil may or may not include minibuffers. Decide if it
2294 does. */
2295 if (NILP (*minibuf))
2296 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2297 else if (!EQ (*minibuf, Qt))
2298 *minibuf = Qlambda;
2300 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2301 => count none of them, or a specific minibuffer window (the
2302 active one) to count. */
2304 /* ALL_FRAMES nil doesn't specify which frames to include. */
2305 if (NILP (*all_frames))
2306 *all_frames
2307 = (!EQ (*minibuf, Qlambda)
2308 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2309 : Qnil);
2310 else if (EQ (*all_frames, Qvisible))
2312 else if (EQ (*all_frames, make_number (0)))
2314 else if (FRAMEP (*all_frames))
2316 else if (!EQ (*all_frames, Qt))
2317 *all_frames = Qnil;
2321 /* Return the next or previous window of WINDOW in cyclic ordering
2322 of windows. NEXT_P non-zero means return the next window. See the
2323 documentation string of next-window for the meaning of MINIBUF and
2324 ALL_FRAMES. */
2326 static Lisp_Object
2327 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2329 decode_next_window_args (&window, &minibuf, &all_frames);
2331 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2332 return the first window on the frame. */
2333 if (FRAMEP (all_frames)
2334 && !EQ (all_frames, XWINDOW (window)->frame))
2335 return Fframe_first_window (all_frames);
2337 if (next_p)
2339 Lisp_Object list;
2341 /* Find WINDOW in the list of all windows. */
2342 list = Fmemq (window, window_list ());
2344 /* Scan forward from WINDOW to the end of the window list. */
2345 if (CONSP (list))
2346 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2347 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2348 break;
2350 /* Scan from the start of the window list up to WINDOW. */
2351 if (!CONSP (list))
2352 for (list = Vwindow_list;
2353 CONSP (list) && !EQ (XCAR (list), window);
2354 list = XCDR (list))
2355 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2356 break;
2358 if (CONSP (list))
2359 window = XCAR (list);
2361 else
2363 Lisp_Object candidate, list;
2365 /* Scan through the list of windows for candidates. If there are
2366 candidate windows in front of WINDOW, the last one of these
2367 is the one we want. If there are candidates following WINDOW
2368 in the list, again the last one of these is the one we want. */
2369 candidate = Qnil;
2370 for (list = window_list (); CONSP (list); list = XCDR (list))
2372 if (EQ (XCAR (list), window))
2374 if (WINDOWP (candidate))
2375 break;
2377 else if (candidate_window_p (XCAR (list), window, minibuf,
2378 all_frames))
2379 candidate = XCAR (list);
2382 if (WINDOWP (candidate))
2383 window = candidate;
2386 return window;
2390 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2391 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2392 WINDOW must be a live window and defaults to the selected one. The
2393 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2394 consider.
2396 MINIBUF nil or omitted means consider the minibuffer window only if the
2397 minibuffer is active. MINIBUF t means consider the minibuffer window
2398 even if the minibuffer is not active. Any other value means do not
2399 consider the minibuffer window even if the minibuffer is active.
2401 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2402 plus the minibuffer window if specified by the MINIBUF argument. If the
2403 minibuffer counts, consider all windows on all frames that share that
2404 minibuffer too. The following non-nil values of ALL-FRAMES have special
2405 meanings:
2407 - t means consider all windows on all existing frames.
2409 - `visible' means consider all windows on all visible frames.
2411 - 0 (the number zero) means consider all windows on all visible and
2412 iconified frames.
2414 - A frame means consider all windows on that frame only.
2416 Anything else means consider all windows on WINDOW's frame and no
2417 others.
2419 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2420 `next-window' to iterate through the entire cycle of acceptable
2421 windows, eventually ending up back at the window you started with.
2422 `previous-window' traverses the same cycle, in the reverse order. */)
2423 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2425 return next_window (window, minibuf, all_frames, 1);
2429 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2430 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2431 WINDOW must be a live window and defaults to the selected one. The
2432 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2433 consider.
2435 MINIBUF nil or omitted means consider the minibuffer window only if the
2436 minibuffer is active. MINIBUF t means consider the minibuffer window
2437 even if the minibuffer is not active. Any other value means do not
2438 consider the minibuffer window even if the minibuffer is active.
2440 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2441 plus the minibuffer window if specified by the MINIBUF argument. If the
2442 minibuffer counts, consider all windows on all frames that share that
2443 minibuffer too. The following non-nil values of ALL-FRAMES have special
2444 meanings:
2446 - t means consider all windows on all existing frames.
2448 - `visible' means consider all windows on all visible frames.
2450 - 0 (the number zero) means consider all windows on all visible and
2451 iconified frames.
2453 - A frame means consider all windows on that frame only.
2455 Anything else means consider all windows on WINDOW's frame and no
2456 others.
2458 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2459 use `previous-window' to iterate through the entire cycle of
2460 acceptable windows, eventually ending up back at the window you
2461 started with. `next-window' traverses the same cycle, in the
2462 reverse order. */)
2463 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2465 return next_window (window, minibuf, all_frames, 0);
2469 /* Return a list of windows in cyclic ordering. Arguments are like
2470 for `next-window'. */
2472 static Lisp_Object
2473 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2475 Lisp_Object tail, list, rest;
2477 decode_next_window_args (&window, &minibuf, &all_frames);
2478 list = Qnil;
2480 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2481 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2482 list = Fcons (XCAR (tail), list);
2484 /* Rotate the list to start with WINDOW. */
2485 list = Fnreverse (list);
2486 rest = Fmemq (window, list);
2487 if (!NILP (rest) && !EQ (rest, list))
2489 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2491 XSETCDR (tail, Qnil);
2492 list = nconc2 (rest, list);
2494 return list;
2498 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2499 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2500 FRAME nil or omitted means use the selected frame.
2501 WINDOW nil or omitted means use the window selected within FRAME.
2502 MINIBUF t means include the minibuffer window, even if it isn't active.
2503 MINIBUF nil or omitted means include the minibuffer window only
2504 if it's active.
2505 MINIBUF neither nil nor t means never include the minibuffer window. */)
2506 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2508 if (NILP (window))
2509 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2510 CHECK_WINDOW (window);
2511 if (NILP (frame))
2512 frame = selected_frame;
2514 if (!EQ (frame, XWINDOW (window)->frame))
2515 error ("Window is on a different frame");
2517 return window_list_1 (window, minibuf, frame);
2521 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2522 doc: /* Return a list of all live windows.
2523 WINDOW specifies the first window to list and defaults to the selected
2524 window.
2526 Optional argument MINIBUF nil or omitted means consider the minibuffer
2527 window only if the minibuffer is active. MINIBUF t means consider the
2528 minibuffer window even if the minibuffer is not active. Any other value
2529 means do not consider the minibuffer window even if the minibuffer is
2530 active.
2532 Optional argument ALL-FRAMES nil or omitted means consider all windows
2533 on WINDOW's frame, plus the minibuffer window if specified by the
2534 MINIBUF argument. If the minibuffer counts, consider all windows on all
2535 frames that share that minibuffer too. The following non-nil values of
2536 ALL-FRAMES have special meanings:
2538 - t means consider all windows on all existing frames.
2540 - `visible' means consider all windows on all visible frames.
2542 - 0 (the number zero) means consider all windows on all visible and
2543 iconified frames.
2545 - A frame means consider all windows on that frame only.
2547 Anything else means consider all windows on WINDOW's frame and no
2548 others.
2550 If WINDOW is not on the list of windows returned, some other window will
2551 be listed first but no error is signaled. */)
2552 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2554 return window_list_1 (window, minibuf, all_frames);
2557 /* Look at all windows, performing an operation specified by TYPE
2558 with argument OBJ.
2559 If FRAMES is Qt, look at all frames;
2560 Qnil, look at just the selected frame;
2561 Qvisible, look at visible frames;
2562 a frame, just look at windows on that frame.
2563 If MINI is non-zero, perform the operation on minibuffer windows too. */
2565 enum window_loop
2567 WINDOW_LOOP_UNUSED,
2568 GET_BUFFER_WINDOW, /* Arg is buffer */
2569 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2570 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2571 CHECK_ALL_WINDOWS /* Arg is ignored */
2574 static Lisp_Object
2575 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2577 Lisp_Object window, windows, best_window, frame_arg;
2578 int frame_best_window_flag = 0;
2579 struct frame *f;
2580 struct gcpro gcpro1;
2582 /* If we're only looping through windows on a particular frame,
2583 frame points to that frame. If we're looping through windows
2584 on all frames, frame is 0. */
2585 if (FRAMEP (frames))
2586 f = XFRAME (frames);
2587 else if (NILP (frames))
2588 f = SELECTED_FRAME ();
2589 else
2590 f = NULL;
2592 if (f)
2593 frame_arg = Qlambda;
2594 else if (EQ (frames, make_number (0)))
2595 frame_arg = frames;
2596 else if (EQ (frames, Qvisible))
2597 frame_arg = frames;
2598 else
2599 frame_arg = Qt;
2601 /* frame_arg is Qlambda to stick to one frame,
2602 Qvisible to consider all visible frames,
2603 or Qt otherwise. */
2605 /* Pick a window to start with. */
2606 if (WINDOWP (obj))
2607 window = obj;
2608 else if (f)
2609 window = FRAME_SELECTED_WINDOW (f);
2610 else
2611 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2613 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2614 GCPRO1 (windows);
2615 best_window = Qnil;
2617 for (; CONSP (windows); windows = XCDR (windows))
2619 struct window *w;
2621 window = XCAR (windows);
2622 w = XWINDOW (window);
2624 /* Note that we do not pay attention here to whether the frame
2625 is visible, since Fwindow_list skips non-visible frames if
2626 that is desired, under the control of frame_arg. */
2627 if (!MINI_WINDOW_P (w)
2628 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2629 consider all windows. */
2630 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2631 || (mini && minibuf_level > 0))
2632 switch (type)
2634 case GET_BUFFER_WINDOW:
2635 if (EQ (w->contents, obj)
2636 /* Don't find any minibuffer window except the one that
2637 is currently in use. */
2638 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2640 if (EQ (window, selected_window))
2641 /* Preferably return the selected window. */
2642 RETURN_UNGCPRO (window);
2643 else if (EQ (XWINDOW (window)->frame, selected_frame)
2644 && !frame_best_window_flag)
2645 /* Prefer windows on the current frame (but don't
2646 choose another one if we have one already). */
2648 best_window = window;
2649 frame_best_window_flag = 1;
2651 else if (NILP (best_window))
2652 best_window = window;
2654 break;
2656 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2657 /* We could simply check whether the buffer shown by window
2658 is live, and show another buffer in case it isn't. */
2659 if (EQ (w->contents, obj))
2661 /* Undedicate WINDOW. */
2662 wset_dedicated (w, Qnil);
2663 /* Make WINDOW show the buffer returned by
2664 other_buffer_safely, don't run any hooks. */
2665 set_window_buffer
2666 (window, other_buffer_safely (w->contents), 0, 0);
2667 /* If WINDOW is the selected window, make its buffer
2668 current. But do so only if the window shows the
2669 current buffer (Bug#6454). */
2670 if (EQ (window, selected_window)
2671 && XBUFFER (w->contents) == current_buffer)
2672 Fset_buffer (w->contents);
2674 break;
2676 case REDISPLAY_BUFFER_WINDOWS:
2677 if (EQ (w->contents, obj))
2679 mark_window_display_accurate (window, 0);
2680 w->update_mode_line = 1;
2681 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2682 ++update_mode_lines;
2683 best_window = window;
2685 break;
2687 /* Check for a leaf window that has a killed buffer
2688 or broken markers. */
2689 case CHECK_ALL_WINDOWS:
2690 if (BUFFERP (w->contents))
2692 struct buffer *b = XBUFFER (w->contents);
2694 if (!BUFFER_LIVE_P (b))
2695 emacs_abort ();
2696 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2697 emacs_abort ();
2698 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2699 emacs_abort ();
2701 break;
2703 case WINDOW_LOOP_UNUSED:
2704 break;
2708 UNGCPRO;
2709 return best_window;
2712 /* Used for debugging. Abort if any window has a dead buffer. */
2714 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2715 void
2716 check_all_windows (void)
2718 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2721 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2722 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2723 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2724 the current buffer.
2726 The optional argument ALL-FRAMES specifies the frames to consider:
2728 - t means consider all windows on all existing frames.
2730 - `visible' means consider all windows on all visible frames.
2732 - 0 (the number zero) means consider all windows on all visible
2733 and iconified frames.
2735 - A frame means consider all windows on that frame only.
2737 Any other value of ALL-FRAMES means consider all windows on the
2738 selected frame and no others. */)
2739 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2741 Lisp_Object buffer;
2743 if (NILP (buffer_or_name))
2744 buffer = Fcurrent_buffer ();
2745 else
2746 buffer = Fget_buffer (buffer_or_name);
2748 if (BUFFERP (buffer))
2749 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2750 else
2751 return Qnil;
2754 static Lisp_Object
2755 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2757 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2761 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2762 Sdelete_other_windows_internal, 0, 2, "",
2763 doc: /* Make WINDOW fill its frame.
2764 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2765 and defaults to the selected one.
2767 Optional argument ROOT, if non-nil, must specify an internal window such
2768 that WINDOW is in its window subtree. If this is the case, replace ROOT
2769 by WINDOW and leave alone any windows not part of ROOT's subtree.
2771 When WINDOW is live try to reduce display jumps by keeping the text
2772 previously visible in WINDOW in the same place on the frame. Doing this
2773 depends on the value of (window-start WINDOW), so if calling this
2774 function in a program gives strange scrolling, make sure the
2775 window-start value is reasonable when this function is called. */)
2776 (Lisp_Object window, Lisp_Object root)
2778 struct window *w, *r, *s;
2779 struct frame *f;
2780 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2781 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2782 int top IF_LINT (= 0), new_top, resize_failed;
2784 w = decode_valid_window (window);
2785 XSETWINDOW (window, w);
2786 f = XFRAME (w->frame);
2788 if (NILP (root))
2789 /* ROOT is the frame's root window. */
2791 root = FRAME_ROOT_WINDOW (f);
2792 r = XWINDOW (root);
2794 else
2795 /* ROOT must be an ancestor of WINDOW. */
2797 r = decode_valid_window (root);
2798 pwindow = XWINDOW (window)->parent;
2799 while (!NILP (pwindow))
2800 if (EQ (pwindow, root))
2801 break;
2802 else
2803 pwindow = XWINDOW (pwindow)->parent;
2804 if (!EQ (pwindow, root))
2805 error ("Specified root is not an ancestor of specified window");
2808 if (EQ (window, root))
2809 /* A noop. */
2810 return Qnil;
2811 /* I don't understand the "top > 0" part below. If we deal with a
2812 standalone minibuffer it would have been caught by the preceding
2813 test. */
2814 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2815 error ("Can't expand minibuffer to full frame");
2817 if (BUFFERP (w->contents))
2819 startpos = marker_position (w->start);
2820 startbyte = marker_byte_position (w->start);
2821 top = WINDOW_TOP_EDGE_LINE (w)
2822 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2823 /* Make sure WINDOW is the frame's selected window. */
2824 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2826 if (EQ (selected_frame, w->frame))
2827 Fselect_window (window, Qnil);
2828 else
2829 fset_selected_window (f, window);
2832 else
2834 /* See if the frame's selected window is a part of the window
2835 subtree rooted at WINDOW, by finding all the selected window's
2836 parents and comparing each one with WINDOW. If it isn't we
2837 need a new selected window for this frame. */
2838 swindow = FRAME_SELECTED_WINDOW (f);
2839 while (1)
2841 pwindow = swindow;
2842 while (!NILP (pwindow) && !EQ (window, pwindow))
2843 pwindow = XWINDOW (pwindow)->parent;
2845 if (EQ (window, pwindow))
2846 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2847 as the new selected window. */
2848 break;
2849 else
2850 /* Else try the previous window of SWINDOW. */
2851 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2854 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2856 if (EQ (selected_frame, w->frame))
2857 Fselect_window (swindow, Qnil);
2858 else
2859 fset_selected_window (f, swindow);
2863 block_input ();
2864 if (!FRAME_INITIAL_P (f))
2866 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2868 /* We are going to free the glyph matrices of WINDOW, and with
2869 that we might lose any information about glyph rows that have
2870 some of their glyphs highlighted in mouse face. (These rows
2871 are marked with a non-zero mouse_face_p flag.) If WINDOW
2872 indeed has some glyphs highlighted in mouse face, signal to
2873 frame's up-to-date hook that mouse highlight was overwritten,
2874 so that it will arrange for redisplaying the highlight. */
2875 if (EQ (hlinfo->mouse_face_window, window))
2877 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
2878 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
2879 hlinfo->mouse_face_window = Qnil;
2882 free_window_matrices (r);
2884 windows_or_buffers_changed++;
2885 Vwindow_list = Qnil;
2886 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2887 resize_failed = 0;
2889 if (!WINDOW_LEAF_P (w))
2891 /* Resize child windows vertically. */
2892 XSETINT (delta, r->total_lines - w->total_lines);
2893 w->top_line = r->top_line;
2894 resize_root_window (window, delta, Qnil, Qnil);
2895 if (window_resize_check (w, 0))
2896 window_resize_apply (w, 0);
2897 else
2899 resize_root_window (window, delta, Qnil, Qt);
2900 if (window_resize_check (w, 0))
2901 window_resize_apply (w, 0);
2902 else
2903 resize_failed = 1;
2906 /* Resize child windows horizontally. */
2907 if (!resize_failed)
2909 w->left_col = r->left_col;
2910 XSETINT (delta, r->total_cols - w->total_cols);
2911 resize_root_window (window, delta, Qt, Qnil);
2912 if (window_resize_check (w, 1))
2913 window_resize_apply (w, 1);
2914 else
2916 resize_root_window (window, delta, Qt, Qt);
2917 if (window_resize_check (w, 1))
2918 window_resize_apply (w, 1);
2919 else
2920 resize_failed = 1;
2924 if (resize_failed)
2925 /* Play safe, if we still can ... */
2927 window = swindow;
2928 w = XWINDOW (window);
2932 /* Cleanly unlink WINDOW from window-tree. */
2933 if (!NILP (w->prev))
2934 /* Get SIBLING above (on the left of) WINDOW. */
2936 sibling = w->prev;
2937 s = XWINDOW (sibling);
2938 wset_next (s, w->next);
2939 if (!NILP (s->next))
2940 wset_prev (XWINDOW (s->next), sibling);
2942 else
2943 /* Get SIBLING below (on the right of) WINDOW. */
2945 sibling = w->next;
2946 s = XWINDOW (sibling);
2947 wset_prev (s, Qnil);
2948 wset_combination (XWINDOW (w->parent),
2949 XWINDOW (w->parent)->horizontal, sibling);
2952 /* Delete ROOT and all child windows of ROOT. */
2953 if (WINDOWP (r->contents))
2955 delete_all_child_windows (r->contents);
2956 wset_combination (r, 0, Qnil);
2959 replace_window (root, window, 1);
2961 /* This must become SWINDOW anyway ....... */
2962 if (BUFFERP (w->contents) && !resize_failed)
2964 /* Try to minimize scrolling, by setting the window start to the
2965 point will cause the text at the old window start to be at the
2966 same place on the frame. But don't try to do this if the
2967 window start is outside the visible portion (as might happen
2968 when the display is not current, due to typeahead). */
2969 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2970 if (new_top != top
2971 && startpos >= BUF_BEGV (XBUFFER (w->contents))
2972 && startpos <= BUF_ZV (XBUFFER (w->contents)))
2974 struct position pos;
2975 struct buffer *obuf = current_buffer;
2977 Fset_buffer (w->contents);
2978 /* This computation used to temporarily move point, but that
2979 can have unwanted side effects due to text properties. */
2980 pos = *vmotion (startpos, startbyte, -top, w);
2982 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
2983 w->window_end_valid = 0;
2984 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
2985 || FETCH_BYTE (pos.bytepos - 1) == '\n');
2986 /* We need to do this, so that the window-scroll-functions
2987 get called. */
2988 w->optional_new_start = 1;
2990 set_buffer_internal (obuf);
2994 adjust_glyphs (f);
2995 unblock_input ();
2997 run_window_configuration_change_hook (f);
2999 return Qnil;
3003 void
3004 replace_buffer_in_windows (Lisp_Object buffer)
3006 call1 (Qreplace_buffer_in_windows, buffer);
3009 /* If BUFFER is shown in a window, safely replace it with some other
3010 buffer in all windows of all frames, even those on other keyboards. */
3012 void
3013 replace_buffer_in_windows_safely (Lisp_Object buffer)
3015 if (buffer_window_count (XBUFFER (buffer)))
3017 Lisp_Object tail, frame;
3019 /* A single call to window_loop won't do the job because it only
3020 considers frames on the current keyboard. So loop manually over
3021 frames, and handle each one. */
3022 FOR_EACH_FRAME (tail, frame)
3023 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3027 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
3028 minimum allowable size. */
3030 void
3031 check_frame_size (struct frame *frame, int *rows, int *cols)
3033 /* For height, we have to see:
3034 how many windows the frame has at minimum (one or two),
3035 and whether it has a menu bar or other special stuff at the top. */
3036 int min_height
3037 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3038 ? MIN_SAFE_WINDOW_HEIGHT
3039 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3041 if (FRAME_TOP_MARGIN (frame) > 0)
3042 min_height += FRAME_TOP_MARGIN (frame);
3044 if (*rows < min_height)
3045 *rows = min_height;
3046 if (*cols < MIN_SAFE_WINDOW_WIDTH)
3047 *cols = MIN_SAFE_WINDOW_WIDTH;
3050 /* Adjust the margins of window W if text area is too small.
3051 Return 1 if window width is ok after adjustment; 0 if window
3052 is still too narrow. */
3054 static int
3055 adjust_window_margins (struct window *w)
3057 int box_cols = (WINDOW_TOTAL_COLS (w)
3058 - WINDOW_FRINGE_COLS (w)
3059 - WINDOW_SCROLL_BAR_COLS (w));
3060 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
3061 + WINDOW_RIGHT_MARGIN_COLS (w));
3063 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
3064 return 1;
3066 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
3067 return 0;
3069 /* Window's text area is too narrow, but reducing the window
3070 margins will fix that. */
3071 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
3072 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
3074 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
3076 wset_left_margin_cols (w, make_number (margin_cols / 2));
3077 wset_right_margin_cols (w, make_number (margin_cols / 2));
3079 else
3080 wset_right_margin_cols (w, make_number (margin_cols));
3082 else
3083 wset_left_margin_cols (w, make_number (margin_cols));
3084 return 1;
3087 /* The following three routines are needed for running a window's
3088 configuration change hook. */
3089 static void
3090 run_funs (Lisp_Object funs)
3092 for (; CONSP (funs); funs = XCDR (funs))
3093 if (!EQ (XCAR (funs), Qt))
3094 call0 (XCAR (funs));
3097 static void
3098 select_window_norecord (Lisp_Object window)
3100 if (WINDOW_LIVE_P (window))
3101 Fselect_window (window, Qt);
3104 static void
3105 select_frame_norecord (Lisp_Object frame)
3107 if (FRAME_LIVE_P (XFRAME (frame)))
3108 Fselect_frame (frame, Qt);
3111 void
3112 run_window_configuration_change_hook (struct frame *f)
3114 ptrdiff_t count = SPECPDL_INDEX ();
3115 Lisp_Object frame, global_wcch
3116 = Fdefault_value (Qwindow_configuration_change_hook);
3117 XSETFRAME (frame, f);
3119 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3120 return;
3122 /* Use the right buffer. Matters when running the local hooks. */
3123 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3125 record_unwind_current_buffer ();
3126 Fset_buffer (Fwindow_buffer (Qnil));
3129 if (SELECTED_FRAME () != f)
3131 record_unwind_protect (select_frame_norecord, selected_frame);
3132 select_frame_norecord (frame);
3135 /* Look for buffer-local values. */
3137 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3138 for (; CONSP (windows); windows = XCDR (windows))
3140 Lisp_Object window = XCAR (windows);
3141 Lisp_Object buffer = Fwindow_buffer (window);
3142 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3143 buffer)))
3145 ptrdiff_t inner_count = SPECPDL_INDEX ();
3146 record_unwind_protect (select_window_norecord, selected_window);
3147 select_window_norecord (window);
3148 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3149 buffer));
3150 unbind_to (inner_count, Qnil);
3155 run_funs (global_wcch);
3156 unbind_to (count, Qnil);
3159 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3160 Srun_window_configuration_change_hook, 0, 1, 0,
3161 doc: /* Run `window-configuration-change-hook' for FRAME.
3162 If FRAME is omitted or nil, it defaults to the selected frame. */)
3163 (Lisp_Object frame)
3165 run_window_configuration_change_hook (decode_live_frame (frame));
3166 return Qnil;
3169 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3170 means it's allowed to run hooks. See make_frame for a case where
3171 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3172 margins, fringes, and scroll-bar settings of the window are not
3173 reset from the buffer's local settings. */
3175 void
3176 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3177 bool run_hooks_p, bool keep_margins_p)
3179 struct window *w = XWINDOW (window);
3180 struct buffer *b = XBUFFER (buffer);
3181 ptrdiff_t count = SPECPDL_INDEX ();
3182 int samebuf = EQ (buffer, w->contents);
3184 wset_buffer (w, buffer);
3186 if (EQ (window, selected_window))
3187 bset_last_selected_window (b, window);
3189 /* Let redisplay errors through. */
3190 b->display_error_modiff = 0;
3192 /* Update time stamps of buffer display. */
3193 if (INTEGERP (BVAR (b, display_count)))
3194 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3195 bset_display_time (b, Fcurrent_time ());
3197 wset_window_end_pos (w, make_number (0));
3198 wset_window_end_vpos (w, make_number (0));
3199 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3201 if (!(keep_margins_p && samebuf))
3202 { /* If we're not actually changing the buffer, don't reset hscroll and
3203 vscroll. This case happens for example when called from
3204 change_frame_size_1, where we use a dummy call to
3205 Fset_window_buffer on the frame's selected window (and no other)
3206 just in order to run window-configuration-change-hook.
3207 Resetting hscroll and vscroll here is problematic for things like
3208 image-mode and doc-view-mode since it resets the image's position
3209 whenever we resize the frame. */
3210 w->hscroll = w->min_hscroll = 0;
3211 w->vscroll = 0;
3212 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3213 set_marker_restricted (w->start,
3214 make_number (b->last_window_start),
3215 buffer);
3216 w->start_at_line_beg = 0;
3217 w->force_start = 0;
3218 w->last_modified = 0;
3219 w->last_overlay_modified = 0;
3221 /* Maybe we could move this into the `if' but it's not obviously safe and
3222 I doubt it's worth the trouble. */
3223 windows_or_buffers_changed++;
3225 /* We must select BUFFER for running the window-scroll-functions. */
3226 /* We can't check ! NILP (Vwindow_scroll_functions) here
3227 because that might itself be a local variable. */
3228 if (window_initialized)
3230 record_unwind_current_buffer ();
3231 Fset_buffer (buffer);
3234 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3236 if (!keep_margins_p)
3238 /* Set left and right marginal area width etc. from buffer. */
3240 /* This may call adjust_window_margins three times, so
3241 temporarily disable window margins. */
3242 Lisp_Object save_left = w->left_margin_cols;
3243 Lisp_Object save_right = w->right_margin_cols;
3245 wset_left_margin_cols (w, Qnil);
3246 wset_right_margin_cols (w, Qnil);
3248 Fset_window_fringes (window,
3249 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3250 BVAR (b, fringes_outside_margins));
3252 Fset_window_scroll_bars (window,
3253 BVAR (b, scroll_bar_width),
3254 BVAR (b, vertical_scroll_bar_type), Qnil);
3256 wset_left_margin_cols (w, save_left);
3257 wset_right_margin_cols (w, save_right);
3259 Fset_window_margins (window,
3260 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3263 if (run_hooks_p)
3265 if (! NILP (Vwindow_scroll_functions))
3266 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3267 Fmarker_position (w->start));
3268 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3271 unbind_to (count, Qnil);
3274 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3275 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3276 WINDOW must be a live window and defaults to the selected one.
3277 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3279 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3280 display margins, fringe widths, and scroll bar settings are preserved;
3281 the default is to reset these from the local settings for BUFFER-OR-NAME
3282 or the frame defaults. Return nil.
3284 This function throws an error when WINDOW is strongly dedicated to its
3285 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3286 already display BUFFER-OR-NAME.
3288 This function runs `window-scroll-functions' before running
3289 `window-configuration-change-hook'. */)
3290 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3292 register Lisp_Object tem, buffer;
3293 register struct window *w = decode_live_window (window);
3295 XSETWINDOW (window, w);
3296 buffer = Fget_buffer (buffer_or_name);
3297 CHECK_BUFFER (buffer);
3298 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3299 error ("Attempt to display deleted buffer");
3301 tem = w->contents;
3302 if (NILP (tem))
3303 error ("Window is deleted");
3304 else
3306 if (!EQ (tem, buffer))
3308 if (EQ (w->dedicated, Qt))
3309 /* WINDOW is strongly dedicated to its buffer, signal an
3310 error. */
3311 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3312 else
3313 /* WINDOW is weakly dedicated to its buffer, reset
3314 dedication. */
3315 wset_dedicated (w, Qnil);
3317 call1 (Qrecord_window_buffer, window);
3320 unshow_buffer (w);
3323 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3325 return Qnil;
3328 static Lisp_Object
3329 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3331 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3334 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3335 0, 1, 0,
3336 doc: /* Force all windows to be updated on next redisplay.
3337 If optional arg OBJECT is a window, force redisplay of that window only.
3338 If OBJECT is a buffer or buffer name, force redisplay of all windows
3339 displaying that buffer. */)
3340 (Lisp_Object object)
3342 if (NILP (object))
3344 windows_or_buffers_changed++;
3345 update_mode_lines++;
3346 return Qt;
3349 if (WINDOWP (object))
3351 struct window *w = XWINDOW (object);
3352 mark_window_display_accurate (object, 0);
3353 w->update_mode_line = 1;
3354 if (BUFFERP (w->contents))
3355 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3356 ++update_mode_lines;
3357 return Qt;
3360 if (STRINGP (object))
3361 object = Fget_buffer (object);
3362 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3363 && buffer_window_count (XBUFFER (object)))
3365 /* If buffer is live and shown in at least one window, find
3366 all windows showing this buffer and force update of them. */
3367 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3368 return NILP (object) ? Qnil : Qt;
3371 /* If nothing suitable was found, just return.
3372 We could signal an error, but this feature will typically be used
3373 asynchronously in timers or process sentinels, so we don't. */
3374 return Qnil;
3377 /* Obsolete since 24.3. */
3378 void
3379 temp_output_buffer_show (register Lisp_Object buf)
3381 register struct buffer *old = current_buffer;
3382 register Lisp_Object window;
3383 register struct window *w;
3385 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3387 Fset_buffer (buf);
3388 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3389 BEGV = BEG;
3390 ZV = Z;
3391 SET_PT (BEG);
3392 set_buffer_internal (old);
3394 if (!NILP (Vtemp_buffer_show_function))
3395 call1 (Vtemp_buffer_show_function, buf);
3396 else
3398 window = display_buffer (buf, Qnil, Qnil);
3400 if (!EQ (XWINDOW (window)->frame, selected_frame))
3401 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3402 Vminibuf_scroll_window = window;
3403 w = XWINDOW (window);
3404 w->hscroll = 0;
3405 w->min_hscroll = 0;
3406 set_marker_restricted_both (w->start, buf, BEG, BEG);
3407 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3409 /* Run temp-buffer-show-hook, with the chosen window selected
3410 and its buffer current. */
3412 ptrdiff_t count = SPECPDL_INDEX ();
3413 Lisp_Object prev_window, prev_buffer;
3414 prev_window = selected_window;
3415 XSETBUFFER (prev_buffer, old);
3417 /* Select the window that was chosen, for running the hook.
3418 Note: Both Fselect_window and select_window_norecord may
3419 set-buffer to the buffer displayed in the window,
3420 so we need to save the current buffer. --stef */
3421 record_unwind_protect (restore_buffer, prev_buffer);
3422 record_unwind_protect (select_window_norecord, prev_window);
3423 Fselect_window (window, Qt);
3424 Fset_buffer (w->contents);
3425 Frun_hooks (1, &Qtemp_buffer_show_hook);
3426 unbind_to (count, Qnil);
3431 /* Make new window, have it replace WINDOW in window-tree, and make
3432 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3433 horizontal child). */
3434 static void
3435 make_parent_window (Lisp_Object window, bool horflag)
3437 Lisp_Object parent;
3438 register struct window *o, *p;
3440 o = XWINDOW (window);
3441 p = allocate_window ();
3442 memcpy ((char *) p + sizeof (struct vectorlike_header),
3443 (char *) o + sizeof (struct vectorlike_header),
3444 word_size * VECSIZE (struct window));
3445 /* P's buffer slot may change from nil to a buffer... */
3446 adjust_window_count (p, 1);
3447 XSETWINDOW (parent, p);
3449 replace_window (window, parent, 1);
3451 wset_next (o, Qnil);
3452 wset_prev (o, Qnil);
3453 wset_parent (o, parent);
3454 /* ...but now P becomes an internal window. */
3455 wset_start (p, Qnil);
3456 wset_pointm (p, Qnil);
3457 wset_buffer (p, Qnil);
3458 wset_combination (p, horflag, window);
3459 wset_combination_limit (p, Qnil);
3460 wset_window_parameters (p, Qnil);
3463 /* Make new window from scratch. */
3464 Lisp_Object
3465 make_window (void)
3467 Lisp_Object window;
3468 register struct window *w;
3470 w = allocate_window ();
3471 /* Initialize Lisp data. Note that allocate_window initializes all
3472 Lisp data to nil, so do it only for slots which should not be nil. */
3473 wset_normal_lines (w, make_float (1.0));
3474 wset_normal_cols (w, make_float (1.0));
3475 wset_new_total (w, make_number (0));
3476 wset_new_normal (w, make_number (0));
3477 wset_start (w, Fmake_marker ());
3478 wset_pointm (w, Fmake_marker ());
3479 wset_vertical_scroll_bar_type (w, Qt);
3480 wset_window_end_pos (w, make_number (0));
3481 wset_window_end_vpos (w, make_number (0));
3482 /* These Lisp fields are marked specially so they're not set to nil by
3483 allocate_window. */
3484 wset_prev_buffers (w, Qnil);
3485 wset_next_buffers (w, Qnil);
3487 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3488 non-Lisp data, so do it only for slots which should not be zero. */
3489 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3490 w->phys_cursor_type = -1;
3491 w->phys_cursor_width = -1;
3492 w->column_number_displayed = -1;
3494 /* Reset window_list. */
3495 Vwindow_list = Qnil;
3496 /* Return window. */
3497 XSETWINDOW (window, w);
3498 return window;
3501 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3502 doc: /* Set new total size of WINDOW to SIZE.
3503 WINDOW must be a valid window and defaults to the selected one.
3504 Return SIZE.
3506 Optional argument ADD non-nil means add SIZE to the new total size of
3507 WINDOW and return the sum.
3509 Note: This function does not operate on any child windows of WINDOW. */)
3510 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3512 struct window *w = decode_valid_window (window);
3514 CHECK_NUMBER (size);
3515 if (NILP (add))
3516 wset_new_total (w, size);
3517 else
3518 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3520 return w->new_total;
3523 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3524 doc: /* Set new normal size of WINDOW to SIZE.
3525 WINDOW must be a valid window and defaults to the selected one.
3526 Return SIZE.
3528 Note: This function does not operate on any child windows of WINDOW. */)
3529 (Lisp_Object window, Lisp_Object size)
3531 wset_new_normal (decode_valid_window (window), size);
3532 return size;
3535 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3536 non-zero) to w->new_total would result in correct heights (widths)
3537 for window W and recursively all child windows of W.
3539 Note: This function does not check any of `window-fixed-size-p',
3540 `window-min-height' or `window-min-width'. It does check that window
3541 sizes do not drop below one line (two columns). */
3542 static int
3543 window_resize_check (struct window *w, bool horflag)
3545 struct window *c;
3547 if (WINDOW_VERTICAL_COMBINATION_P (w))
3548 /* W is a vertical combination. */
3550 c = XWINDOW (w->contents);
3551 if (horflag)
3552 /* All child windows of W must have the same width as W. */
3554 while (c)
3556 if ((XINT (c->new_total) != XINT (w->new_total))
3557 || !window_resize_check (c, horflag))
3558 return 0;
3559 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3561 return 1;
3563 else
3564 /* The sum of the heights of the child windows of W must equal
3565 W's height. */
3567 int sum_of_sizes = 0;
3568 while (c)
3570 if (!window_resize_check (c, horflag))
3571 return 0;
3572 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3573 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3575 return (sum_of_sizes == XINT (w->new_total));
3578 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3579 /* W is a horizontal combination. */
3581 c = XWINDOW (w->contents);
3582 if (horflag)
3583 /* The sum of the widths of the child windows of W must equal W's
3584 width. */
3586 int sum_of_sizes = 0;
3587 while (c)
3589 if (!window_resize_check (c, horflag))
3590 return 0;
3591 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3592 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3594 return (sum_of_sizes == XINT (w->new_total));
3596 else
3597 /* All child windows of W must have the same height as W. */
3599 while (c)
3601 if ((XINT (c->new_total) != XINT (w->new_total))
3602 || !window_resize_check (c, horflag))
3603 return 0;
3604 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3606 return 1;
3609 else
3610 /* A leaf window. Make sure it's not too small. The following
3611 hardcodes the values of `window-safe-min-width' (2) and
3612 `window-safe-min-height' (1) which are defined in window.el. */
3613 return XINT (w->new_total) >= (horflag ? 2 : 1);
3616 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3617 w->new_total for window W and recursively all child windows of W.
3618 Also calculate and assign the new vertical (horizontal) start
3619 positions of each of these windows.
3621 This function does not perform any error checks. Make sure you have
3622 run window_resize_check on W before applying this function. */
3623 static void
3624 window_resize_apply (struct window *w, bool horflag)
3626 struct window *c;
3627 int pos;
3629 /* Note: Assigning new_normal requires that the new total size of the
3630 parent window has been set *before*. */
3631 if (horflag)
3633 w->total_cols = XFASTINT (w->new_total);
3634 if (NUMBERP (w->new_normal))
3635 wset_normal_cols (w, w->new_normal);
3637 pos = w->left_col;
3639 else
3641 w->total_lines = XFASTINT (w->new_total);
3642 if (NUMBERP (w->new_normal))
3643 wset_normal_lines (w, w->new_normal);
3645 pos = w->top_line;
3648 if (WINDOW_VERTICAL_COMBINATION_P (w))
3649 /* W is a vertical combination. */
3651 c = XWINDOW (w->contents);
3652 while (c)
3654 if (horflag)
3655 c->left_col = pos;
3656 else
3657 c->top_line = pos;
3658 window_resize_apply (c, horflag);
3659 if (!horflag)
3660 pos = pos + c->total_lines;
3661 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3664 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3665 /* W is a horizontal combination. */
3667 c = XWINDOW (w->contents);
3668 while (c)
3670 if (horflag)
3671 c->left_col = pos;
3672 else
3673 c->top_line = pos;
3674 window_resize_apply (c, horflag);
3675 if (horflag)
3676 pos = pos + c->total_cols;
3677 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3681 /* Clear out some redisplay caches. */
3682 w->last_modified = 0;
3683 w->last_overlay_modified = 0;
3687 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3688 doc: /* Apply requested size values for window-tree of FRAME.
3689 If FRAME is omitted or nil, it defaults to the selected frame.
3691 Optional argument HORIZONTAL omitted or nil means apply requested
3692 height values. HORIZONTAL non-nil means apply requested width values.
3694 This function checks whether the requested values sum up to a valid
3695 window layout, recursively assigns the new sizes of all child windows
3696 and calculates and assigns the new start positions of these windows.
3698 Note: This function does not check any of `window-fixed-size-p',
3699 `window-min-height' or `window-min-width'. All these checks have to
3700 be applied on the Elisp level. */)
3701 (Lisp_Object frame, Lisp_Object horizontal)
3703 struct frame *f = decode_live_frame (frame);
3704 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3705 bool horflag = !NILP (horizontal);
3707 if (!window_resize_check (r, horflag)
3708 || (XINT (r->new_total)
3709 != (horflag ? r->total_cols : r->total_lines)))
3710 return Qnil;
3712 block_input ();
3713 window_resize_apply (r, horflag);
3715 windows_or_buffers_changed++;
3716 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3718 adjust_glyphs (f);
3719 unblock_input ();
3721 run_window_configuration_change_hook (f);
3723 return Qt;
3727 /* Resize frame F's windows when number of lines of F is set to SIZE.
3728 HORFLAG 1 means resize windows when number of columns of F is set to
3729 SIZE.
3731 This function can delete all windows but the selected one in order to
3732 satisfy the request. The result will be meaningful if and only if
3733 F's windows have meaningful sizes when you call this. */
3734 void
3735 resize_frame_windows (struct frame *f, int size, bool horflag)
3737 Lisp_Object root = f->root_window;
3738 struct window *r = XWINDOW (root);
3739 Lisp_Object mini = f->minibuffer_window;
3740 struct window *m;
3741 /* new_size is the new size of the frame's root window. */
3742 int new_size = (horflag
3743 ? size
3744 : (size
3745 - FRAME_TOP_MARGIN (f)
3746 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3747 ? 1 : 0)));
3749 r->top_line = FRAME_TOP_MARGIN (f);
3750 if (WINDOW_LEAF_P (r))
3751 /* For a leaf root window just set the size. */
3752 if (horflag)
3753 r->total_cols = new_size;
3754 else
3755 r->total_lines = new_size;
3756 else
3758 /* old_size is the old size of the frame's root window. */
3759 int old_size = horflag ? r->total_cols : r->total_lines;
3760 Lisp_Object delta;
3762 XSETINT (delta, new_size - old_size);
3763 /* Try a "normal" resize first. */
3764 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3765 if (window_resize_check (r, horflag)
3766 && new_size == XINT (r->new_total))
3767 window_resize_apply (r, horflag);
3768 else
3770 /* Try with "reasonable" minimum sizes next. */
3771 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3772 if (window_resize_check (r, horflag)
3773 && new_size == XINT (r->new_total))
3774 window_resize_apply (r, horflag);
3775 else
3777 /* Finally, try with "safe" minimum sizes. */
3778 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3779 if (window_resize_check (r, horflag)
3780 && new_size == XINT (r->new_total))
3781 window_resize_apply (r, horflag);
3782 else
3784 /* We lost. Delete all windows but the frame's
3785 selected one. */
3786 root = f->selected_window;
3787 Fdelete_other_windows_internal (root, Qnil);
3788 if (horflag)
3789 XWINDOW (root)->total_cols = new_size;
3790 else
3791 XWINDOW (root)->total_lines = new_size;
3797 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3799 m = XWINDOW (mini);
3800 if (horflag)
3801 m->total_cols = size;
3802 else
3804 /* Are we sure we always want 1 line here? */
3805 m->total_lines = 1;
3806 m->top_line = r->top_line + r->total_lines;
3810 windows_or_buffers_changed++;
3814 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3815 doc: /* Split window OLD.
3816 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3817 new window. In any case TOTAL-SIZE must be a positive integer.
3819 Third argument SIDE nil (or `below') specifies that the new window shall
3820 be located below WINDOW. SIDE `above' means the new window shall be
3821 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3822 lines of the new window including space reserved for the mode and/or
3823 header line.
3825 SIDE t (or `right') specifies that the new window shall be located on
3826 the right side of WINDOW. SIDE `left' means the new window shall be
3827 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3828 number of columns of the new window including space reserved for fringes
3829 and the scrollbar or a divider column.
3831 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3832 according to the SIDE argument.
3834 The new total and normal sizes of all involved windows must have been
3835 set correctly. See the code of `split-window' for how this is done. */)
3836 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3838 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3839 parent window or an internal window we have to install as OLD's new
3840 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3841 provided OLD is a leaf window, or to the frame's selected window.
3842 NEW (*n) is the new window created with some parameters taken from
3843 REFERENCE (*r). */
3844 register Lisp_Object new, frame, reference;
3845 register struct window *o, *p, *n, *r;
3846 struct frame *f;
3847 bool horflag
3848 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3849 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3850 int combination_limit = 0;
3852 CHECK_WINDOW (old);
3853 o = XWINDOW (old);
3854 frame = WINDOW_FRAME (o);
3855 f = XFRAME (frame);
3857 CHECK_NUMBER (total_size);
3859 /* Set combination_limit to 1 if we have to make a new parent window.
3860 We do that if either `window-combination-limit' is t, or OLD has no
3861 parent, or OLD is ortho-combined. */
3862 combination_limit =
3863 EQ (Vwindow_combination_limit, Qt)
3864 || NILP (o->parent)
3865 || (horflag
3866 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
3867 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
3869 /* We need a live reference window to initialize some parameters. */
3870 if (WINDOW_LIVE_P (old))
3871 /* OLD is live, use it as reference window. */
3872 reference = old;
3873 else
3874 /* Use the frame's selected window as reference window. */
3875 reference = FRAME_SELECTED_WINDOW (f);
3876 r = XWINDOW (reference);
3878 /* The following bugs are caught by `split-window'. */
3879 if (MINI_WINDOW_P (o))
3880 error ("Attempt to split minibuffer window");
3881 else if (XINT (total_size) < (horflag ? 2 : 1))
3882 error ("Size of new window too small (after split)");
3883 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3884 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3885 proportionally. */
3887 p = XWINDOW (o->parent);
3888 /* Temporarily pretend we split the parent window. */
3889 wset_new_total
3890 (p, make_number ((horflag ? p->total_cols : p->total_lines)
3891 - XINT (total_size)));
3892 if (!window_resize_check (p, horflag))
3893 error ("Window sizes don't fit");
3894 else
3895 /* Undo the temporary pretension. */
3896 wset_new_total (p, make_number
3897 (horflag ? p->total_cols : p->total_lines));
3899 else
3901 if (!window_resize_check (o, horflag))
3902 error ("Resizing old window failed");
3903 else if (XINT (total_size) + XINT (o->new_total)
3904 != (horflag ? o->total_cols : o->total_lines))
3905 error ("Sum of sizes of old and new window don't fit");
3908 /* This is our point of no return. */
3909 if (combination_limit)
3911 /* Save the old value of o->normal_cols/lines. It gets corrupted
3912 by make_parent_window and we need it below for assigning it to
3913 p->new_normal. */
3914 Lisp_Object new_normal
3915 = horflag ? o->normal_cols : o->normal_lines;
3917 make_parent_window (old, horflag);
3918 p = XWINDOW (o->parent);
3919 if (EQ (Vwindow_combination_limit, Qt))
3920 /* Store t in the new parent's combination_limit slot to avoid
3921 that its children get merged into another window. */
3922 wset_combination_limit (p, Qt);
3923 /* These get applied below. */
3924 wset_new_total (p, make_number
3925 (horflag ? o->total_cols : o->total_lines));
3926 wset_new_normal (p, new_normal);
3928 else
3929 p = XWINDOW (o->parent);
3931 windows_or_buffers_changed++;
3932 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3933 new = make_window ();
3934 n = XWINDOW (new);
3935 wset_frame (n, frame);
3936 wset_parent (n, o->parent);
3938 if (EQ (side, Qabove) || EQ (side, Qleft))
3940 wset_prev (n, o->prev);
3941 if (NILP (n->prev))
3942 wset_combination (p, horflag, new);
3943 else
3944 wset_next (XWINDOW (n->prev), new);
3945 wset_next (n, old);
3946 wset_prev (o, new);
3948 else
3950 wset_next (n, o->next);
3951 if (!NILP (n->next))
3952 wset_prev (XWINDOW (n->next), new);
3953 wset_prev (n, old);
3954 wset_next (o, new);
3957 n->window_end_valid = 0;
3958 memset (&n->last_cursor, 0, sizeof n->last_cursor);
3960 /* Get special geometry settings from reference window. */
3961 wset_left_margin_cols (n, r->left_margin_cols);
3962 wset_right_margin_cols (n, r->right_margin_cols);
3963 wset_left_fringe_width (n, r->left_fringe_width);
3964 wset_right_fringe_width (n, r->right_fringe_width);
3965 n->fringes_outside_margins = r->fringes_outside_margins;
3966 wset_scroll_bar_width (n, r->scroll_bar_width);
3967 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
3969 /* Directly assign orthogonal coordinates and sizes. */
3970 if (horflag)
3972 n->top_line = o->top_line;
3973 n->total_lines = o->total_lines;
3975 else
3977 n->left_col = o->left_col;
3978 n->total_cols = o->total_cols;
3981 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3982 get them ready here. */
3983 wset_new_total (n, total_size);
3984 wset_new_normal (n, normal_size);
3986 block_input ();
3987 window_resize_apply (p, horflag);
3988 adjust_glyphs (f);
3989 /* Set buffer of NEW to buffer of reference window. Don't run
3990 any hooks. */
3991 set_window_buffer (new, r->contents, 0, 1);
3992 unblock_input ();
3994 /* Maybe we should run the scroll functions in Elisp (which already
3995 runs the configuration change hook). */
3996 if (! NILP (Vwindow_scroll_functions))
3997 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3998 Fmarker_position (n->start));
3999 /* Return NEW. */
4000 return new;
4004 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
4005 doc: /* Remove WINDOW from its frame.
4006 WINDOW defaults to the selected window. Return nil.
4007 Signal an error when WINDOW is the only window on its frame. */)
4008 (register Lisp_Object window)
4010 register Lisp_Object parent, sibling, frame, root;
4011 struct window *w, *p, *s, *r;
4012 struct frame *f;
4013 bool horflag, before_sibling = 0;
4015 w = decode_any_window (window);
4016 XSETWINDOW (window, w);
4017 if (NILP (w->contents))
4018 /* It's a no-op to delete an already deleted window. */
4019 return Qnil;
4021 parent = w->parent;
4022 if (NILP (parent))
4023 /* Never delete a minibuffer or frame root window. */
4024 error ("Attempt to delete minibuffer or sole ordinary window");
4025 else if (NILP (w->prev) && NILP (w->next))
4026 /* Rather bow out here, this case should be handled on the Elisp
4027 level. */
4028 error ("Attempt to delete sole window of parent");
4030 p = XWINDOW (parent);
4031 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
4033 frame = WINDOW_FRAME (w);
4034 f = XFRAME (frame);
4036 root = FRAME_ROOT_WINDOW (f);
4037 r = XWINDOW (root);
4039 /* Unlink WINDOW from window tree. */
4040 if (NILP (w->prev))
4041 /* Get SIBLING below (on the right of) WINDOW. */
4043 /* before_sibling 1 means WINDOW is the first child of its
4044 parent and thus before the sibling. */
4045 before_sibling = 1;
4046 sibling = w->next;
4047 s = XWINDOW (sibling);
4048 wset_prev (s, Qnil);
4049 wset_combination (p, horflag, sibling);
4051 else
4052 /* Get SIBLING above (on the left of) WINDOW. */
4054 sibling = w->prev;
4055 s = XWINDOW (sibling);
4056 wset_next (s, w->next);
4057 if (!NILP (s->next))
4058 wset_prev (XWINDOW (s->next), sibling);
4061 if (window_resize_check (r, horflag)
4062 && (XINT (r->new_total)
4063 == (horflag ? r->total_cols : r->total_lines)))
4064 /* We can delete WINDOW now. */
4067 /* Block input. */
4068 block_input ();
4069 window_resize_apply (p, horflag);
4071 /* If this window is referred to by the dpyinfo's mouse
4072 highlight, invalidate that slot to be safe (Bug#9904). */
4073 if (!FRAME_INITIAL_P (f))
4075 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4077 if (EQ (hlinfo->mouse_face_window, window))
4078 hlinfo->mouse_face_window = Qnil;
4081 windows_or_buffers_changed++;
4082 Vwindow_list = Qnil;
4083 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4085 wset_next (w, Qnil); /* Don't delete w->next too. */
4086 free_window_matrices (w);
4088 if (WINDOWP (w->contents))
4090 delete_all_child_windows (w->contents);
4091 wset_combination (w, 0, Qnil);
4093 else
4095 unshow_buffer (w);
4096 unchain_marker (XMARKER (w->pointm));
4097 unchain_marker (XMARKER (w->start));
4098 wset_buffer (w, Qnil);
4101 if (NILP (s->prev) && NILP (s->next))
4102 /* A matrjoshka where SIBLING has become the only child of
4103 PARENT. */
4105 /* Put SIBLING into PARENT's place. */
4106 replace_window (parent, sibling, 0);
4107 /* Have SIBLING inherit the following three slot values from
4108 PARENT (the combination_limit slot is not inherited). */
4109 wset_normal_cols (s, p->normal_cols);
4110 wset_normal_lines (s, p->normal_lines);
4111 /* Mark PARENT as deleted. */
4112 wset_combination (p, 0, Qnil);
4113 /* Try to merge SIBLING into its new parent. */
4114 recombine_windows (sibling);
4117 adjust_glyphs (f);
4119 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4120 /* We deleted the frame's selected window. */
4122 /* Use the frame's first window as fallback ... */
4123 Lisp_Object new_selected_window = Fframe_first_window (frame);
4124 /* ... but preferably use its most recently used window. */
4125 Lisp_Object mru_window;
4127 /* `get-mru-window' might fail for some reason so play it safe
4128 - promote the first window _without recording it_ first. */
4129 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4130 Fselect_window (new_selected_window, Qt);
4131 else
4132 fset_selected_window (f, new_selected_window);
4134 unblock_input ();
4136 /* Now look whether `get-mru-window' gets us something. */
4137 mru_window = call1 (Qget_mru_window, frame);
4138 if (WINDOW_LIVE_P (mru_window)
4139 && EQ (XWINDOW (mru_window)->frame, frame))
4140 new_selected_window = mru_window;
4142 /* If all ended up well, we now promote the mru window. */
4143 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4144 Fselect_window (new_selected_window, Qnil);
4145 else
4146 fset_selected_window (f, new_selected_window);
4148 else
4149 unblock_input ();
4151 /* Must be run by the caller:
4152 run_window_configuration_change_hook (f); */
4154 else
4155 /* We failed: Relink WINDOW into window tree. */
4157 if (before_sibling)
4159 wset_prev (s, window);
4160 wset_combination (p, horflag, window);
4162 else
4164 wset_next (s, window);
4165 if (!NILP (w->next))
4166 wset_prev (XWINDOW (w->next), window);
4168 error ("Deletion failed");
4171 return Qnil;
4174 /***********************************************************************
4175 Resizing Mini-Windows
4176 ***********************************************************************/
4178 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4179 can. */
4180 void
4181 grow_mini_window (struct window *w, int delta)
4183 struct frame *f = XFRAME (w->frame);
4184 struct window *r;
4185 Lisp_Object root, value;
4187 eassert (MINI_WINDOW_P (w));
4188 eassert (delta >= 0);
4190 root = FRAME_ROOT_WINDOW (f);
4191 r = XWINDOW (root);
4192 value = call2 (Qwindow_resize_root_window_vertically,
4193 root, make_number (- delta));
4194 if (INTEGERP (value) && window_resize_check (r, 0))
4196 block_input ();
4197 window_resize_apply (r, 0);
4199 /* Grow the mini-window. */
4200 w->top_line = r->top_line + r->total_lines;
4201 w->total_lines -= XINT (value);
4202 w->last_modified = 0;
4203 w->last_overlay_modified = 0;
4205 windows_or_buffers_changed++;
4206 adjust_glyphs (f);
4207 unblock_input ();
4212 /* Shrink mini-window W. */
4213 void
4214 shrink_mini_window (struct window *w)
4216 struct frame *f = XFRAME (w->frame);
4217 struct window *r;
4218 Lisp_Object root, value;
4219 EMACS_INT size;
4221 eassert (MINI_WINDOW_P (w));
4223 size = w->total_lines;
4224 if (size > 1)
4226 root = FRAME_ROOT_WINDOW (f);
4227 r = XWINDOW (root);
4228 value = call2 (Qwindow_resize_root_window_vertically,
4229 root, make_number (size - 1));
4230 if (INTEGERP (value) && window_resize_check (r, 0))
4232 block_input ();
4233 window_resize_apply (r, 0);
4235 /* Shrink the mini-window. */
4236 w->top_line = r->top_line + r->total_lines;
4237 w->total_lines = 1;
4239 w->last_modified = 0;
4240 w->last_overlay_modified = 0;
4242 windows_or_buffers_changed++;
4243 adjust_glyphs (f);
4244 unblock_input ();
4246 /* If the above failed for whatever strange reason we must make a
4247 one window frame here. The same routine will be needed when
4248 shrinking the frame (and probably when making the initial
4249 *scratch* window). For the moment leave things as they are. */
4253 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4254 doc: /* Resize minibuffer window WINDOW. */)
4255 (Lisp_Object window)
4257 struct window *w = XWINDOW (window);
4258 struct window *r;
4259 struct frame *f;
4260 int height;
4262 CHECK_WINDOW (window);
4263 f = XFRAME (w->frame);
4265 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4266 error ("Not a valid minibuffer window");
4267 else if (FRAME_MINIBUF_ONLY_P (f))
4268 error ("Cannot resize a minibuffer-only frame");
4270 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4271 height = r->total_lines + w->total_lines;
4272 if (window_resize_check (r, 0)
4273 && XINT (w->new_total) > 0
4274 && height == XINT (r->new_total) + XINT (w->new_total))
4276 block_input ();
4277 window_resize_apply (r, 0);
4279 w->total_lines = XFASTINT (w->new_total);
4280 w->top_line = r->top_line + r->total_lines;
4282 windows_or_buffers_changed++;
4283 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4284 adjust_glyphs (f);
4285 unblock_input ();
4287 run_window_configuration_change_hook (f);
4288 return Qt;
4290 else error ("Failed to resize minibuffer window");
4293 /* Mark window cursors off for all windows in the window tree rooted
4294 at W by setting their phys_cursor_on_p flag to zero. Called from
4295 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4296 the frame are cleared. */
4298 void
4299 mark_window_cursors_off (struct window *w)
4301 while (w)
4303 if (WINDOWP (w->contents))
4304 mark_window_cursors_off (XWINDOW (w->contents));
4305 else
4306 w->phys_cursor_on_p = 0;
4308 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4313 /* Return number of lines of text (not counting mode lines) in W. */
4316 window_internal_height (struct window *w)
4318 int ht = w->total_lines;
4320 if (!MINI_WINDOW_P (w))
4322 if (!NILP (w->parent)
4323 || WINDOWP (w->contents)
4324 || !NILP (w->next)
4325 || !NILP (w->prev)
4326 || WINDOW_WANTS_MODELINE_P (w))
4327 --ht;
4329 if (WINDOW_WANTS_HEADER_LINE_P (w))
4330 --ht;
4333 return ht;
4336 /************************************************************************
4337 Window Scrolling
4338 ***********************************************************************/
4340 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4341 N screen-fulls, which is defined as the height of the window minus
4342 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4343 instead. Negative values of N mean scroll down. NOERROR non-zero
4344 means don't signal an error if we try to move over BEGV or ZV,
4345 respectively. */
4347 static void
4348 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4350 immediate_quit = 1;
4351 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4353 /* If we must, use the pixel-based version which is much slower than
4354 the line-based one but can handle varying line heights. */
4355 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4356 window_scroll_pixel_based (window, n, whole, noerror);
4357 else
4358 window_scroll_line_based (window, n, whole, noerror);
4360 immediate_quit = 0;
4364 /* Implementation of window_scroll that works based on pixel line
4365 heights. See the comment of window_scroll for parameter
4366 descriptions. */
4368 static void
4369 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4371 struct it it;
4372 struct window *w = XWINDOW (window);
4373 struct text_pos start;
4374 int this_scroll_margin;
4375 /* True if we fiddled the window vscroll field without really scrolling. */
4376 int vscrolled = 0;
4377 int x, y, rtop, rbot, rowh, vpos;
4378 void *itdata = NULL;
4379 int window_total_lines;
4380 int frame_line_height = default_line_pixel_height (w);
4382 SET_TEXT_POS_FROM_MARKER (start, w->start);
4383 /* Scrolling a minibuffer window via scroll bar when the echo area
4384 shows long text sometimes resets the minibuffer contents behind
4385 our backs. */
4386 if (CHARPOS (start) > ZV)
4387 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4389 /* If PT is not visible in WINDOW, move back one half of
4390 the screen. Allow PT to be partially visible, otherwise
4391 something like (scroll-down 1) with PT in the line before
4392 the partially visible one would recenter. */
4394 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4396 itdata = bidi_shelve_cache ();
4397 /* Move backward half the height of the window. Performance note:
4398 vmotion used here is about 10% faster, but would give wrong
4399 results for variable height lines. */
4400 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4401 it.current_y = it.last_visible_y;
4402 move_it_vertically_backward (&it, window_box_height (w) / 2);
4404 /* The function move_iterator_vertically may move over more than
4405 the specified y-distance. If it->w is small, e.g. a
4406 mini-buffer window, we may end up in front of the window's
4407 display area. This is the case when Start displaying at the
4408 start of the line containing PT in this case. */
4409 if (it.current_y <= 0)
4411 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4412 move_it_vertically_backward (&it, 0);
4413 it.current_y = 0;
4416 start = it.current.pos;
4417 bidi_unshelve_cache (itdata, 0);
4419 else if (auto_window_vscroll_p)
4421 if (rtop || rbot) /* partially visible */
4423 int px;
4424 int dy = frame_line_height;
4425 if (whole)
4426 dy = max ((window_box_height (w)
4427 - next_screen_context_lines * dy),
4428 dy);
4429 dy *= n;
4431 if (n < 0)
4433 /* Only vscroll backwards if already vscrolled forwards. */
4434 if (w->vscroll < 0 && rtop > 0)
4436 px = max (0, -w->vscroll - min (rtop, -dy));
4437 Fset_window_vscroll (window, make_number (px), Qt);
4438 return;
4441 if (n > 0)
4443 /* Do vscroll if already vscrolled or only display line. */
4444 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4446 px = max (0, -w->vscroll + min (rbot, dy));
4447 Fset_window_vscroll (window, make_number (px), Qt);
4448 return;
4451 /* Maybe modify window start instead of scrolling. */
4452 if (rbot > 0 || w->vscroll < 0)
4454 ptrdiff_t spos;
4456 Fset_window_vscroll (window, make_number (0), Qt);
4457 /* If there are other text lines above the current row,
4458 move window start to current row. Else to next row. */
4459 if (rbot > 0)
4460 spos = XINT (Fline_beginning_position (Qnil));
4461 else
4462 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4463 set_marker_restricted (w->start, make_number (spos),
4464 w->contents);
4465 w->start_at_line_beg = 1;
4466 w->update_mode_line = 1;
4467 w->last_modified = 0;
4468 w->last_overlay_modified = 0;
4469 /* Set force_start so that redisplay_window will run the
4470 window-scroll-functions. */
4471 w->force_start = 1;
4472 return;
4476 /* Cancel previous vscroll. */
4477 Fset_window_vscroll (window, make_number (0), Qt);
4480 itdata = bidi_shelve_cache ();
4481 /* If scroll_preserve_screen_position is non-nil, we try to set
4482 point in the same window line as it is now, so get that line. */
4483 if (!NILP (Vscroll_preserve_screen_position))
4485 /* We preserve the goal pixel coordinate across consecutive
4486 calls to scroll-up, scroll-down and other commands that
4487 have the `scroll-command' property. This avoids the
4488 possibility of point becoming "stuck" on a tall line when
4489 scrolling by one line. */
4490 if (window_scroll_pixel_based_preserve_y < 0
4491 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4492 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4494 start_display (&it, w, start);
4495 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4496 window_scroll_pixel_based_preserve_y = it.current_y;
4497 window_scroll_pixel_based_preserve_x = it.current_x;
4500 else
4501 window_scroll_pixel_based_preserve_y
4502 = window_scroll_pixel_based_preserve_x = -1;
4504 /* Move iterator it from start the specified distance forward or
4505 backward. The result is the new window start. */
4506 start_display (&it, w, start);
4507 if (whole)
4509 ptrdiff_t start_pos = IT_CHARPOS (it);
4510 int dy = frame_line_height;
4511 dy = max ((window_box_height (w)
4512 - next_screen_context_lines * dy),
4513 dy) * n;
4515 /* Note that move_it_vertically always moves the iterator to the
4516 start of a line. So, if the last line doesn't have a newline,
4517 we would end up at the start of the line ending at ZV. */
4518 if (dy <= 0)
4520 move_it_vertically_backward (&it, -dy);
4521 /* Ensure we actually do move, e.g. in case we are currently
4522 looking at an image that is taller that the window height. */
4523 while (start_pos == IT_CHARPOS (it)
4524 && start_pos > BEGV)
4525 move_it_by_lines (&it, -1);
4527 else if (dy > 0)
4529 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4530 MOVE_TO_POS | MOVE_TO_Y);
4531 /* Ensure we actually do move, e.g. in case we are currently
4532 looking at an image that is taller that the window height. */
4533 while (start_pos == IT_CHARPOS (it)
4534 && start_pos < ZV)
4535 move_it_by_lines (&it, 1);
4538 else
4539 move_it_by_lines (&it, n);
4541 /* We failed if we find ZV is already on the screen (scrolling up,
4542 means there's nothing past the end), or if we can't start any
4543 earlier (scrolling down, means there's nothing past the top). */
4544 if ((n > 0 && IT_CHARPOS (it) == ZV)
4545 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4547 if (IT_CHARPOS (it) == ZV)
4549 if (it.current_y < it.last_visible_y
4550 && (it.current_y + it.max_ascent + it.max_descent
4551 > it.last_visible_y))
4553 /* The last line was only partially visible, make it fully
4554 visible. */
4555 w->vscroll = (it.last_visible_y
4556 - it.current_y + it.max_ascent + it.max_descent);
4557 adjust_glyphs (it.f);
4559 else
4561 bidi_unshelve_cache (itdata, 0);
4562 if (noerror)
4563 return;
4564 else if (n < 0) /* could happen with empty buffers */
4565 xsignal0 (Qbeginning_of_buffer);
4566 else
4567 xsignal0 (Qend_of_buffer);
4570 else
4572 if (w->vscroll != 0)
4573 /* The first line was only partially visible, make it fully
4574 visible. */
4575 w->vscroll = 0;
4576 else
4578 bidi_unshelve_cache (itdata, 0);
4579 if (noerror)
4580 return;
4581 else
4582 xsignal0 (Qbeginning_of_buffer);
4586 /* If control gets here, then we vscrolled. */
4588 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
4590 /* Don't try to change the window start below. */
4591 vscrolled = 1;
4594 if (! vscrolled)
4596 ptrdiff_t pos = IT_CHARPOS (it);
4597 ptrdiff_t bytepos;
4599 /* If in the middle of a multi-glyph character move forward to
4600 the next character. */
4601 if (in_display_vector_p (&it))
4603 ++pos;
4604 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4607 /* Set the window start, and set up the window for redisplay. */
4608 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
4609 IT_BYTEPOS (it));
4610 bytepos = marker_byte_position (w->start);
4611 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4612 w->update_mode_line = 1;
4613 w->last_modified = 0;
4614 w->last_overlay_modified = 0;
4615 /* Set force_start so that redisplay_window will run the
4616 window-scroll-functions. */
4617 w->force_start = 1;
4620 /* The rest of this function uses current_y in a nonstandard way,
4621 not including the height of the header line if any. */
4622 it.current_y = it.vpos = 0;
4624 /* Move PT out of scroll margins.
4625 This code wants current_y to be zero at the window start position
4626 even if there is a header line. */
4627 window_total_lines
4628 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
4629 this_scroll_margin = max (0, scroll_margin);
4630 this_scroll_margin
4631 = min (this_scroll_margin, window_total_lines / 4);
4632 this_scroll_margin *= frame_line_height;
4634 if (n > 0)
4636 /* We moved the window start towards ZV, so PT may be now
4637 in the scroll margin at the top. */
4638 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4639 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4640 && (NILP (Vscroll_preserve_screen_position)
4641 || EQ (Vscroll_preserve_screen_position, Qt)))
4642 /* We found PT at a legitimate height. Leave it alone. */
4644 else if (window_scroll_pixel_based_preserve_y >= 0)
4646 /* If we have a header line, take account of it.
4647 This is necessary because we set it.current_y to 0, above. */
4648 move_it_to (&it, -1,
4649 window_scroll_pixel_based_preserve_x,
4650 window_scroll_pixel_based_preserve_y
4651 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4652 -1, MOVE_TO_Y | MOVE_TO_X);
4653 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4655 else
4657 while (it.current_y < this_scroll_margin)
4659 int prev = it.current_y;
4660 move_it_by_lines (&it, 1);
4661 if (prev == it.current_y)
4662 break;
4664 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4667 else if (n < 0)
4669 ptrdiff_t charpos, bytepos;
4670 int partial_p;
4672 /* Save our position, for the
4673 window_scroll_pixel_based_preserve_y case. */
4674 charpos = IT_CHARPOS (it);
4675 bytepos = IT_BYTEPOS (it);
4677 /* We moved the window start towards BEGV, so PT may be now
4678 in the scroll margin at the bottom. */
4679 move_it_to (&it, PT, -1,
4680 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4681 - this_scroll_margin - 1),
4683 MOVE_TO_POS | MOVE_TO_Y);
4685 /* Save our position, in case it's correct. */
4686 charpos = IT_CHARPOS (it);
4687 bytepos = IT_BYTEPOS (it);
4689 /* See if point is on a partially visible line at the end. */
4690 if (it.what == IT_EOB)
4691 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4692 else
4694 move_it_by_lines (&it, 1);
4695 partial_p = it.current_y > it.last_visible_y;
4698 if (charpos == PT && !partial_p
4699 && (NILP (Vscroll_preserve_screen_position)
4700 || EQ (Vscroll_preserve_screen_position, Qt)))
4701 /* We found PT before we found the display margin, so PT is ok. */
4703 else if (window_scroll_pixel_based_preserve_y >= 0)
4705 SET_TEXT_POS_FROM_MARKER (start, w->start);
4706 start_display (&it, w, start);
4707 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4708 here because we called start_display again and did not
4709 alter it.current_y this time. */
4710 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4711 window_scroll_pixel_based_preserve_y, -1,
4712 MOVE_TO_Y | MOVE_TO_X);
4713 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4715 else
4717 if (partial_p)
4718 /* The last line was only partially visible, so back up two
4719 lines to make sure we're on a fully visible line. */
4721 move_it_by_lines (&it, -2);
4722 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4724 else
4725 /* No, the position we saved is OK, so use it. */
4726 SET_PT_BOTH (charpos, bytepos);
4729 bidi_unshelve_cache (itdata, 0);
4733 /* Implementation of window_scroll that works based on screen lines.
4734 See the comment of window_scroll for parameter descriptions. */
4736 static void
4737 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
4739 register struct window *w = XWINDOW (window);
4740 /* Fvertical_motion enters redisplay, which can trigger
4741 fontification, which in turn can modify buffer text (e.g., if the
4742 fontification functions replace escape sequences with faces, as
4743 in `grep-mode-font-lock-keywords'). So we use a marker to record
4744 the old point position, to prevent crashes in SET_PT_BOTH. */
4745 Lisp_Object opoint_marker = Fpoint_marker ();
4746 register ptrdiff_t pos, pos_byte;
4747 register int ht = window_internal_height (w);
4748 register Lisp_Object tem;
4749 bool lose;
4750 Lisp_Object bolp;
4751 ptrdiff_t startpos = marker_position (w->start);
4752 ptrdiff_t startbyte = marker_byte_position (w->start);
4753 Lisp_Object original_pos = Qnil;
4755 /* If scrolling screen-fulls, compute the number of lines to
4756 scroll from the window's height. */
4757 if (whole)
4758 n *= max (1, ht - next_screen_context_lines);
4760 if (!NILP (Vscroll_preserve_screen_position))
4762 if (window_scroll_preserve_vpos <= 0
4763 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4764 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4766 struct position posit
4767 = *compute_motion (startpos, startbyte, 0, 0, 0,
4768 PT, ht, 0, -1, w->hscroll, 0, w);
4769 window_scroll_preserve_vpos = posit.vpos;
4770 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
4773 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4774 make_number (window_scroll_preserve_vpos));
4777 XSETFASTINT (tem, PT);
4778 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4780 if (NILP (tem))
4782 Fvertical_motion (make_number (- (ht / 2)), window);
4783 startpos = PT;
4784 startbyte = PT_BYTE;
4787 SET_PT_BOTH (startpos, startbyte);
4788 lose = n < 0 && PT == BEGV;
4789 Fvertical_motion (make_number (n), window);
4790 pos = PT;
4791 pos_byte = PT_BYTE;
4792 bolp = Fbolp ();
4793 SET_PT_BOTH (marker_position (opoint_marker),
4794 marker_byte_position (opoint_marker));
4796 if (lose)
4798 if (noerror)
4799 return;
4800 else
4801 xsignal0 (Qbeginning_of_buffer);
4804 if (pos < ZV)
4806 /* Don't use a scroll margin that is negative or too large. */
4807 int this_scroll_margin =
4808 max (0, min (scroll_margin, w->total_lines / 4));
4810 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
4811 w->start_at_line_beg = !NILP (bolp);
4812 w->update_mode_line = 1;
4813 w->last_modified = 0;
4814 w->last_overlay_modified = 0;
4815 /* Set force_start so that redisplay_window will run
4816 the window-scroll-functions. */
4817 w->force_start = 1;
4819 if (!NILP (Vscroll_preserve_screen_position)
4820 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4822 SET_PT_BOTH (pos, pos_byte);
4823 Fvertical_motion (original_pos, window);
4825 /* If we scrolled forward, put point enough lines down
4826 that it is outside the scroll margin. */
4827 else if (n > 0)
4829 int top_margin;
4831 if (this_scroll_margin > 0)
4833 SET_PT_BOTH (pos, pos_byte);
4834 Fvertical_motion (make_number (this_scroll_margin), window);
4835 top_margin = PT;
4837 else
4838 top_margin = pos;
4840 if (top_margin <= marker_position (opoint_marker))
4841 SET_PT_BOTH (marker_position (opoint_marker),
4842 marker_byte_position (opoint_marker));
4843 else if (!NILP (Vscroll_preserve_screen_position))
4845 SET_PT_BOTH (pos, pos_byte);
4846 Fvertical_motion (original_pos, window);
4848 else
4849 SET_PT (top_margin);
4851 else if (n < 0)
4853 int bottom_margin;
4855 /* If we scrolled backward, put point near the end of the window
4856 but not within the scroll margin. */
4857 SET_PT_BOTH (pos, pos_byte);
4858 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4859 if (XFASTINT (tem) == ht - this_scroll_margin)
4860 bottom_margin = PT;
4861 else
4862 bottom_margin = PT + 1;
4864 if (bottom_margin > marker_position (opoint_marker))
4865 SET_PT_BOTH (marker_position (opoint_marker),
4866 marker_byte_position (opoint_marker));
4867 else
4869 if (!NILP (Vscroll_preserve_screen_position))
4871 SET_PT_BOTH (pos, pos_byte);
4872 Fvertical_motion (original_pos, window);
4874 else
4875 Fvertical_motion (make_number (-1), window);
4879 else
4881 if (noerror)
4882 return;
4883 else
4884 xsignal0 (Qend_of_buffer);
4889 /* Scroll selected_window up or down. If N is nil, scroll a
4890 screen-full which is defined as the height of the window minus
4891 next_screen_context_lines. If N is the symbol `-', scroll.
4892 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4893 up. This is the guts of Fscroll_up and Fscroll_down. */
4895 static void
4896 scroll_command (Lisp_Object n, int direction)
4898 ptrdiff_t count = SPECPDL_INDEX ();
4900 eassert (eabs (direction) == 1);
4902 /* If selected window's buffer isn't current, make it current for
4903 the moment. But don't screw up if window_scroll gets an error. */
4904 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
4906 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4907 Fset_buffer (XWINDOW (selected_window)->contents);
4909 /* Make redisplay consider other windows than just selected_window. */
4910 ++windows_or_buffers_changed;
4913 if (NILP (n))
4914 window_scroll (selected_window, direction, 1, 0);
4915 else if (EQ (n, Qminus))
4916 window_scroll (selected_window, -direction, 1, 0);
4917 else
4919 n = Fprefix_numeric_value (n);
4920 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4923 unbind_to (count, Qnil);
4926 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4927 doc: /* Scroll text of selected window upward ARG lines.
4928 If ARG is omitted or nil, scroll upward by a near full screen.
4929 A near full screen is `next-screen-context-lines' less than a full screen.
4930 Negative ARG means scroll downward.
4931 If ARG is the atom `-', scroll downward by nearly full screen.
4932 When calling from a program, supply as argument a number, nil, or `-'. */)
4933 (Lisp_Object arg)
4935 scroll_command (arg, 1);
4936 return Qnil;
4939 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4940 doc: /* Scroll text of selected window down ARG lines.
4941 If ARG is omitted or nil, scroll down by a near full screen.
4942 A near full screen is `next-screen-context-lines' less than a full screen.
4943 Negative ARG means scroll upward.
4944 If ARG is the atom `-', scroll upward by nearly full screen.
4945 When calling from a program, supply as argument a number, nil, or `-'. */)
4946 (Lisp_Object arg)
4948 scroll_command (arg, -1);
4949 return Qnil;
4952 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4953 doc: /* Return the other window for \"other window scroll\" commands.
4954 If `other-window-scroll-buffer' is non-nil, a window
4955 showing that buffer is used.
4956 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4957 specifies the window. This takes precedence over
4958 `other-window-scroll-buffer'. */)
4959 (void)
4961 Lisp_Object window;
4963 if (MINI_WINDOW_P (XWINDOW (selected_window))
4964 && !NILP (Vminibuf_scroll_window))
4965 window = Vminibuf_scroll_window;
4966 /* If buffer is specified, scroll that buffer. */
4967 else if (!NILP (Vother_window_scroll_buffer))
4969 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4970 if (NILP (window))
4971 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4973 else
4975 /* Nothing specified; look for a neighboring window on the same
4976 frame. */
4977 window = Fnext_window (selected_window, Qnil, Qnil);
4979 if (EQ (window, selected_window))
4980 /* That didn't get us anywhere; look for a window on another
4981 visible frame. */
4983 window = Fnext_window (window, Qnil, Qt);
4984 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4985 && ! EQ (window, selected_window));
4988 CHECK_LIVE_WINDOW (window);
4990 if (EQ (window, selected_window))
4991 error ("There is no other window");
4993 return window;
4996 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4997 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4998 A near full screen is `next-screen-context-lines' less than a full screen.
4999 The next window is the one below the current one; or the one at the top
5000 if the current one is at the bottom. Negative ARG means scroll downward.
5001 If ARG is the atom `-', scroll downward by nearly full screen.
5002 When calling from a program, supply as argument a number, nil, or `-'.
5004 If `other-window-scroll-buffer' is non-nil, scroll the window
5005 showing that buffer, popping the buffer up if necessary.
5006 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5007 specifies the window to scroll. This takes precedence over
5008 `other-window-scroll-buffer'. */)
5009 (Lisp_Object arg)
5011 Lisp_Object window;
5012 struct window *w;
5013 ptrdiff_t count = SPECPDL_INDEX ();
5015 window = Fother_window_for_scrolling ();
5016 w = XWINDOW (window);
5018 /* Don't screw up if window_scroll gets an error. */
5019 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5020 ++windows_or_buffers_changed;
5022 Fset_buffer (w->contents);
5023 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
5025 if (NILP (arg))
5026 window_scroll (window, 1, 1, 1);
5027 else if (EQ (arg, Qminus))
5028 window_scroll (window, -1, 1, 1);
5029 else
5031 if (CONSP (arg))
5032 arg = XCAR (arg);
5033 CHECK_NUMBER (arg);
5034 window_scroll (window, XINT (arg), 0, 1);
5037 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5038 unbind_to (count, Qnil);
5040 return Qnil;
5043 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5044 doc: /* Scroll selected window display ARG columns left.
5045 Default for ARG is window width minus 2.
5046 Value is the total amount of leftward horizontal scrolling in
5047 effect after the change.
5048 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5049 lower bound for automatic scrolling, i.e. automatic scrolling
5050 will not scroll a window to a column less than the value returned
5051 by this function. This happens in an interactive call. */)
5052 (register Lisp_Object arg, Lisp_Object set_minimum)
5054 struct window *w = XWINDOW (selected_window);
5055 EMACS_INT requested_arg = (NILP (arg)
5056 ? window_body_cols (w) - 2
5057 : XINT (Fprefix_numeric_value (arg)));
5058 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5060 if (!NILP (set_minimum))
5061 w->min_hscroll = w->hscroll;
5063 return result;
5066 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5067 doc: /* Scroll selected window display ARG columns right.
5068 Default for ARG is window width minus 2.
5069 Value is the total amount of leftward horizontal scrolling in
5070 effect after the change.
5071 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5072 lower bound for automatic scrolling, i.e. automatic scrolling
5073 will not scroll a window to a column less than the value returned
5074 by this function. This happens in an interactive call. */)
5075 (register Lisp_Object arg, Lisp_Object set_minimum)
5077 struct window *w = XWINDOW (selected_window);
5078 EMACS_INT requested_arg = (NILP (arg)
5079 ? window_body_cols (w) - 2
5080 : XINT (Fprefix_numeric_value (arg)));
5081 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5083 if (!NILP (set_minimum))
5084 w->min_hscroll = w->hscroll;
5086 return result;
5089 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5090 doc: /* Return the window which was selected when entering the minibuffer.
5091 Returns nil, if selected window is not a minibuffer window. */)
5092 (void)
5094 if (minibuf_level > 0
5095 && MINI_WINDOW_P (XWINDOW (selected_window))
5096 && WINDOW_LIVE_P (minibuf_selected_window))
5097 return minibuf_selected_window;
5099 return Qnil;
5102 /* Value is the number of lines actually displayed in window W,
5103 as opposed to its height. */
5105 static int
5106 displayed_window_lines (struct window *w)
5108 struct it it;
5109 struct text_pos start;
5110 ptrdiff_t charpos = marker_position (w->start);
5111 int height = window_box_height (w);
5112 struct buffer *old_buffer;
5113 int bottom_y;
5114 void *itdata = NULL;
5116 if (XBUFFER (w->contents) != current_buffer)
5118 old_buffer = current_buffer;
5119 set_buffer_internal (XBUFFER (w->contents));
5121 else
5122 old_buffer = NULL;
5124 /* In case W->start is out of the accessible range, do something
5125 reasonable. This happens in Info mode when Info-scroll-down
5126 calls (recenter -1) while W->start is 1. */
5127 if (charpos < BEGV)
5128 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5129 else if (charpos > ZV)
5130 SET_TEXT_POS (start, ZV, ZV_BYTE);
5131 else
5132 SET_TEXT_POS_FROM_MARKER (start, w->start);
5134 itdata = bidi_shelve_cache ();
5135 start_display (&it, w, start);
5136 move_it_vertically (&it, height);
5137 bottom_y = line_bottom_y (&it);
5138 bidi_unshelve_cache (itdata, 0);
5140 /* rms: On a non-window display,
5141 the value of it.vpos at the bottom of the screen
5142 seems to be 1 larger than window_box_height (w).
5143 This kludge fixes a bug whereby (move-to-window-line -1)
5144 when ZV is on the last screen line
5145 moves to the previous screen line instead of the last one. */
5146 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5147 height++;
5149 /* Add in empty lines at the bottom of the window. */
5150 if (bottom_y < height)
5152 int uy = FRAME_LINE_HEIGHT (it.f);
5153 it.vpos += (height - bottom_y + uy - 1) / uy;
5156 if (old_buffer)
5157 set_buffer_internal (old_buffer);
5159 return it.vpos;
5163 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5164 doc: /* Center point in selected window and maybe redisplay frame.
5165 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5166 relative to the selected window. If ARG is negative, it counts up from the
5167 bottom of the window. (ARG should be less than the height of the window.)
5169 If ARG is omitted or nil, then recenter with point on the middle line of
5170 the selected window; if the variable `recenter-redisplay' is non-nil,
5171 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5172 is set to `grow-only', this resets the tool-bar's height to the minimum
5173 height needed); if `recenter-redisplay' has the special value `tty',
5174 then only tty frames are redrawn.
5176 Just C-u as prefix means put point in the center of the window
5177 and redisplay normally--don't erase and redraw the frame. */)
5178 (register Lisp_Object arg)
5180 struct window *w = XWINDOW (selected_window);
5181 struct buffer *buf = XBUFFER (w->contents);
5182 struct buffer *obuf = current_buffer;
5183 bool center_p = 0;
5184 ptrdiff_t charpos, bytepos;
5185 EMACS_INT iarg IF_LINT (= 0);
5186 int this_scroll_margin;
5188 /* If redisplay is suppressed due to an error, try again. */
5189 obuf->display_error_modiff = 0;
5191 if (NILP (arg))
5193 if (!NILP (Vrecenter_redisplay)
5194 && (!EQ (Vrecenter_redisplay, Qtty)
5195 || !NILP (Ftty_type (selected_frame))))
5197 ptrdiff_t i;
5199 /* Invalidate pixel data calculated for all compositions. */
5200 for (i = 0; i < n_compositions; i++)
5201 composition_table[i]->font = NULL;
5203 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5205 Fredraw_frame (WINDOW_FRAME (w));
5206 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5209 center_p = 1;
5211 else if (CONSP (arg)) /* Just C-u. */
5212 center_p = 1;
5213 else
5215 arg = Fprefix_numeric_value (arg);
5216 CHECK_NUMBER (arg);
5217 iarg = XINT (arg);
5220 set_buffer_internal (buf);
5222 /* Do this after making BUF current
5223 in case scroll_margin is buffer-local. */
5224 this_scroll_margin =
5225 max (0, min (scroll_margin, w->total_lines / 4));
5227 /* Handle centering on a graphical frame specially. Such frames can
5228 have variable-height lines and centering point on the basis of
5229 line counts would lead to strange effects. */
5230 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5232 if (center_p)
5234 struct it it;
5235 struct text_pos pt;
5236 void *itdata = bidi_shelve_cache ();
5238 SET_TEXT_POS (pt, PT, PT_BYTE);
5239 start_display (&it, w, pt);
5240 move_it_vertically_backward (&it, window_box_height (w) / 2);
5241 charpos = IT_CHARPOS (it);
5242 bytepos = IT_BYTEPOS (it);
5243 bidi_unshelve_cache (itdata, 0);
5245 else if (iarg < 0)
5247 struct it it;
5248 struct text_pos pt;
5249 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5250 int extra_line_spacing;
5251 int h = window_box_height (w);
5252 void *itdata = bidi_shelve_cache ();
5254 iarg = - max (-iarg, this_scroll_margin);
5256 SET_TEXT_POS (pt, PT, PT_BYTE);
5257 start_display (&it, w, pt);
5259 /* Be sure we have the exact height of the full line containing PT. */
5260 move_it_by_lines (&it, 0);
5262 /* The amount of pixels we have to move back is the window
5263 height minus what's displayed in the line containing PT,
5264 and the lines below. */
5265 it.current_y = 0;
5266 it.vpos = 0;
5267 move_it_by_lines (&it, nlines);
5269 if (it.vpos == nlines)
5270 h -= it.current_y;
5271 else
5273 /* Last line has no newline */
5274 h -= line_bottom_y (&it);
5275 it.vpos++;
5278 /* Don't reserve space for extra line spacing of last line. */
5279 extra_line_spacing = it.max_extra_line_spacing;
5281 /* If we can't move down NLINES lines because we hit
5282 the end of the buffer, count in some empty lines. */
5283 if (it.vpos < nlines)
5285 nlines -= it.vpos;
5286 extra_line_spacing = it.extra_line_spacing;
5287 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5289 if (h <= 0)
5291 bidi_unshelve_cache (itdata, 0);
5292 return Qnil;
5295 /* Now find the new top line (starting position) of the window. */
5296 start_display (&it, w, pt);
5297 it.current_y = 0;
5298 move_it_vertically_backward (&it, h);
5300 /* If extra line spacing is present, we may move too far
5301 back. This causes the last line to be only partially
5302 visible (which triggers redisplay to recenter that line
5303 in the middle), so move forward.
5304 But ignore extra line spacing on last line, as it is not
5305 considered to be part of the visible height of the line.
5307 h += extra_line_spacing;
5308 while (-it.current_y > h)
5309 move_it_by_lines (&it, 1);
5311 charpos = IT_CHARPOS (it);
5312 bytepos = IT_BYTEPOS (it);
5314 bidi_unshelve_cache (itdata, 0);
5316 else
5318 struct position pos;
5320 iarg = max (iarg, this_scroll_margin);
5322 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5323 charpos = pos.bufpos;
5324 bytepos = pos.bytepos;
5327 else
5329 struct position pos;
5330 int ht = window_internal_height (w);
5332 if (center_p)
5333 iarg = ht / 2;
5334 else if (iarg < 0)
5335 iarg += ht;
5337 /* Don't let it get into the margin at either top or bottom. */
5338 iarg = clip_to_bounds (this_scroll_margin, iarg,
5339 ht - this_scroll_margin - 1);
5341 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5342 charpos = pos.bufpos;
5343 bytepos = pos.bytepos;
5346 /* Set the new window start. */
5347 set_marker_both (w->start, w->contents, charpos, bytepos);
5348 w->window_end_valid = 0;
5350 w->optional_new_start = 1;
5352 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5353 FETCH_BYTE (bytepos - 1) == '\n');
5355 set_buffer_internal (obuf);
5356 return Qnil;
5359 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5360 0, 1, 0,
5361 doc: /* Return the height in lines of the text display area of WINDOW.
5362 WINDOW must be a live window and defaults to the selected one.
5364 The returned height does not include the mode line, any header line,
5365 nor any partial-height lines at the bottom of the text area. */)
5366 (Lisp_Object window)
5368 struct window *w = decode_live_window (window);
5369 int pixel_height = window_box_height (w);
5370 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5371 return make_number (line_height);
5376 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5377 1, 1, "P",
5378 doc: /* Position point relative to window.
5379 ARG nil means position point at center of window.
5380 Else, ARG specifies vertical position within the window;
5381 zero means top of window, negative means relative to bottom of window. */)
5382 (Lisp_Object arg)
5384 struct window *w = XWINDOW (selected_window);
5385 int lines, start;
5386 Lisp_Object window;
5387 #if 0
5388 int this_scroll_margin;
5389 #endif
5391 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5392 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5393 when passed below to set_marker_both. */
5394 error ("move-to-window-line called from unrelated buffer");
5396 window = selected_window;
5397 start = marker_position (w->start);
5398 if (start < BEGV || start > ZV)
5400 int height = window_internal_height (w);
5401 Fvertical_motion (make_number (- (height / 2)), window);
5402 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5403 w->start_at_line_beg = !NILP (Fbolp ());
5404 w->force_start = 1;
5406 else
5407 Fgoto_char (w->start);
5409 lines = displayed_window_lines (w);
5411 #if 0
5412 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5413 #endif
5415 if (NILP (arg))
5416 XSETFASTINT (arg, lines / 2);
5417 else
5419 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5421 if (iarg < 0)
5422 iarg = iarg + lines;
5424 #if 0 /* This code would prevent move-to-window-line from moving point
5425 to a place inside the scroll margins (which would cause the
5426 next redisplay to scroll). I wrote this code, but then concluded
5427 it is probably better not to install it. However, it is here
5428 inside #if 0 so as not to lose it. -- rms. */
5430 /* Don't let it get into the margin at either top or bottom. */
5431 iarg = max (iarg, this_scroll_margin);
5432 iarg = min (iarg, lines - this_scroll_margin - 1);
5433 #endif
5435 arg = make_number (iarg);
5438 /* Skip past a partially visible first line. */
5439 if (w->vscroll)
5440 XSETINT (arg, XINT (arg) + 1);
5442 return Fvertical_motion (arg, window);
5447 /***********************************************************************
5448 Window Configuration
5449 ***********************************************************************/
5451 struct save_window_data
5453 struct vectorlike_header header;
5454 Lisp_Object selected_frame;
5455 Lisp_Object current_window;
5456 Lisp_Object current_buffer;
5457 Lisp_Object minibuf_scroll_window;
5458 Lisp_Object minibuf_selected_window;
5459 Lisp_Object root_window;
5460 Lisp_Object focus_frame;
5461 /* A vector, each of whose elements is a struct saved_window
5462 for one window. */
5463 Lisp_Object saved_windows;
5465 /* All fields above are traced by the GC.
5466 From `fame-cols' down, the fields are ignored by the GC. */
5468 int frame_cols, frame_lines, frame_menu_bar_lines;
5469 int frame_tool_bar_lines;
5472 /* This is saved as a Lisp_Vector */
5473 struct saved_window
5475 struct vectorlike_header header;
5477 Lisp_Object window, buffer, start, pointm, mark;
5478 Lisp_Object left_col, top_line, total_cols, total_lines;
5479 Lisp_Object normal_cols, normal_lines;
5480 Lisp_Object hscroll, min_hscroll;
5481 Lisp_Object parent, prev;
5482 Lisp_Object start_at_line_beg;
5483 Lisp_Object display_table;
5484 Lisp_Object left_margin_cols, right_margin_cols;
5485 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5486 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5487 Lisp_Object combination_limit, window_parameters;
5490 #define SAVED_WINDOW_N(swv,n) \
5491 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5493 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5494 doc: /* Return t if OBJECT is a window-configuration object. */)
5495 (Lisp_Object object)
5497 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5500 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5501 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5502 (Lisp_Object config)
5504 register struct save_window_data *data;
5505 struct Lisp_Vector *saved_windows;
5507 CHECK_WINDOW_CONFIGURATION (config);
5509 data = (struct save_window_data *) XVECTOR (config);
5510 saved_windows = XVECTOR (data->saved_windows);
5511 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5514 DEFUN ("set-window-configuration", Fset_window_configuration,
5515 Sset_window_configuration, 1, 1, 0,
5516 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5517 CONFIGURATION must be a value previously returned
5518 by `current-window-configuration' (which see).
5519 If CONFIGURATION was made from a frame that is now deleted,
5520 only frame-independent values can be restored. In this case,
5521 the return value is nil. Otherwise the value is t. */)
5522 (Lisp_Object configuration)
5524 register struct save_window_data *data;
5525 struct Lisp_Vector *saved_windows;
5526 Lisp_Object new_current_buffer;
5527 Lisp_Object frame;
5528 struct frame *f;
5529 ptrdiff_t old_point = -1;
5531 CHECK_WINDOW_CONFIGURATION (configuration);
5533 data = (struct save_window_data *) XVECTOR (configuration);
5534 saved_windows = XVECTOR (data->saved_windows);
5536 new_current_buffer = data->current_buffer;
5537 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5538 new_current_buffer = Qnil;
5539 else
5541 if (XBUFFER (new_current_buffer) == current_buffer)
5542 /* The code further down "preserves point" by saving here PT in
5543 old_point and then setting it later back into PT. When the
5544 current-selected-window and the final-selected-window both show
5545 the current buffer, this suffers from the problem that the
5546 current PT is the window-point of the current-selected-window,
5547 while the final PT is the point of the final-selected-window, so
5548 this copy from one PT to the other would end up moving the
5549 window-point of the final-selected-window to the window-point of
5550 the current-selected-window. So we have to be careful which
5551 point of the current-buffer we copy into old_point. */
5552 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5553 && WINDOWP (selected_window)
5554 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
5555 && !EQ (selected_window, data->current_window))
5556 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5557 else
5558 old_point = PT;
5559 else
5560 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5561 point in new_current_buffer as of the last time this buffer was
5562 used. This can be non-deterministic since it can be changed by
5563 things like jit-lock by mere temporary selection of some random
5564 window that happens to show this buffer.
5565 So if possible we want this arbitrary choice of "which point" to
5566 be the one from the to-be-selected-window so as to prevent this
5567 window's cursor from being copied from another window. */
5568 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5569 /* If current_window = selected_window, its point is in BUF_PT. */
5570 && !EQ (selected_window, data->current_window))
5571 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5572 else
5573 old_point = BUF_PT (XBUFFER (new_current_buffer));
5576 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5577 f = XFRAME (frame);
5579 /* If f is a dead frame, don't bother rebuilding its window tree.
5580 However, there is other stuff we should still try to do below. */
5581 if (FRAME_LIVE_P (f))
5583 Lisp_Object window;
5584 Lisp_Object dead_windows = Qnil;
5585 register Lisp_Object tem, par, pers;
5586 register struct window *w;
5587 register struct saved_window *p;
5588 struct window *root_window;
5589 struct window **leaf_windows;
5590 int n_leaf_windows;
5591 ptrdiff_t k;
5592 int i, n;
5594 /* If the frame has been resized since this window configuration was
5595 made, we change the frame to the size specified in the
5596 configuration, restore the configuration, and then resize it
5597 back. We keep track of the prevailing height in these variables. */
5598 int previous_frame_lines = FRAME_LINES (f);
5599 int previous_frame_cols = FRAME_COLS (f);
5600 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5601 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5603 /* Don't do this within the main loop below: This may call Lisp
5604 code and is thus potentially unsafe while input is blocked. */
5605 for (k = 0; k < saved_windows->header.size; k++)
5607 p = SAVED_WINDOW_N (saved_windows, k);
5608 window = p->window;
5609 w = XWINDOW (window);
5610 if (BUFFERP (w->contents)
5611 && !EQ (w->contents, p->buffer)
5612 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5613 /* If a window we restore gets another buffer, record the
5614 window's old buffer. */
5615 call1 (Qrecord_window_buffer, window);
5618 /* The mouse highlighting code could get screwed up
5619 if it runs during this. */
5620 block_input ();
5622 if (data->frame_lines != previous_frame_lines
5623 || data->frame_cols != previous_frame_cols)
5624 change_frame_size (f, data->frame_lines,
5625 data->frame_cols, 0, 0, 0);
5626 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5627 if (data->frame_menu_bar_lines
5628 != previous_frame_menu_bar_lines)
5629 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5630 make_number (0));
5631 #ifdef HAVE_WINDOW_SYSTEM
5632 if (data->frame_tool_bar_lines
5633 != previous_frame_tool_bar_lines)
5634 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5635 make_number (0));
5636 #endif
5637 #endif
5639 /* "Swap out" point from the selected window's buffer
5640 into the window itself. (Normally the pointm of the selected
5641 window holds garbage.) We do this now, before
5642 restoring the window contents, and prevent it from
5643 being done later on when we select a new window. */
5644 if (! NILP (XWINDOW (selected_window)->contents))
5646 w = XWINDOW (selected_window);
5647 set_marker_both (w->pointm,
5648 w->contents,
5649 BUF_PT (XBUFFER (w->contents)),
5650 BUF_PT_BYTE (XBUFFER (w->contents)));
5653 windows_or_buffers_changed++;
5654 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5656 /* Problem: Freeing all matrices and later allocating them again
5657 is a serious redisplay flickering problem. What we would
5658 really like to do is to free only those matrices not reused
5659 below. */
5660 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5661 leaf_windows = alloca (count_windows (root_window)
5662 * sizeof *leaf_windows);
5663 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5665 /* Kludge Alert!
5666 Mark all windows now on frame as "deleted".
5667 Restoring the new configuration "undeletes" any that are in it.
5669 Save their current buffers in their height fields, since we may
5670 need it later, if a buffer saved in the configuration is now
5671 dead. */
5672 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5674 for (k = 0; k < saved_windows->header.size; k++)
5676 p = SAVED_WINDOW_N (saved_windows, k);
5677 window = p->window;
5678 w = XWINDOW (window);
5679 wset_next (w, Qnil);
5681 if (!NILP (p->parent))
5682 wset_parent
5683 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
5684 else
5685 wset_parent (w, Qnil);
5687 if (!NILP (p->prev))
5689 wset_prev
5690 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
5691 wset_next (XWINDOW (w->prev), p->window);
5693 else
5695 wset_prev (w, Qnil);
5696 if (!NILP (w->parent))
5697 wset_combination (XWINDOW (w->parent),
5698 (XINT (p->total_cols)
5699 != XWINDOW (w->parent)->total_cols),
5700 p->window);
5703 /* If we squirreled away the buffer, restore it now. */
5704 if (BUFFERP (w->combination_limit))
5705 wset_buffer (w, w->combination_limit);
5706 w->left_col = XFASTINT (p->left_col);
5707 w->top_line = XFASTINT (p->top_line);
5708 w->total_cols = XFASTINT (p->total_cols);
5709 w->total_lines = XFASTINT (p->total_lines);
5710 wset_normal_cols (w, p->normal_cols);
5711 wset_normal_lines (w, p->normal_lines);
5712 w->hscroll = XFASTINT (p->hscroll);
5713 w->min_hscroll = XFASTINT (p->min_hscroll);
5714 wset_display_table (w, p->display_table);
5715 wset_left_margin_cols (w, p->left_margin_cols);
5716 wset_right_margin_cols (w, p->right_margin_cols);
5717 wset_left_fringe_width (w, p->left_fringe_width);
5718 wset_right_fringe_width (w, p->right_fringe_width);
5719 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
5720 wset_scroll_bar_width (w, p->scroll_bar_width);
5721 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
5722 wset_dedicated (w, p->dedicated);
5723 wset_combination_limit (w, p->combination_limit);
5724 /* Restore any window parameters that have been saved.
5725 Parameters that have not been saved are left alone. */
5726 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5728 pers = XCAR (tem);
5729 if (CONSP (pers))
5731 if (NILP (XCDR (pers)))
5733 par = Fassq (XCAR (pers), w->window_parameters);
5734 if (CONSP (par) && !NILP (XCDR (par)))
5735 /* Reset a parameter to nil if and only if it
5736 has a non-nil association. Don't make new
5737 associations. */
5738 Fsetcdr (par, Qnil);
5740 else
5741 /* Always restore a non-nil value. */
5742 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5746 w->last_modified = 0;
5747 w->last_overlay_modified = 0;
5749 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5750 /* If saved buffer is alive, install it. */
5752 wset_buffer (w, p->buffer);
5753 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5754 set_marker_restricted (w->start, p->start, w->contents);
5755 set_marker_restricted (w->pointm, p->pointm,
5756 w->contents);
5757 Fset_marker (BVAR (XBUFFER (w->contents), mark),
5758 p->mark, w->contents);
5760 /* As documented in Fcurrent_window_configuration, don't
5761 restore the location of point in the buffer which was
5762 current when the window configuration was recorded. */
5763 if (!EQ (p->buffer, new_current_buffer)
5764 && XBUFFER (p->buffer) == current_buffer)
5765 Fgoto_char (w->pointm);
5767 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
5768 /* Keep window's old buffer; make sure the markers are real. */
5770 /* Set window markers at start of visible range. */
5771 if (XMARKER (w->start)->buffer == 0)
5772 set_marker_restricted_both (w->start, w->contents, 0, 0);
5773 if (XMARKER (w->pointm)->buffer == 0)
5774 set_marker_restricted_both
5775 (w->pointm, w->contents,
5776 BUF_PT (XBUFFER (w->contents)),
5777 BUF_PT_BYTE (XBUFFER (w->contents)));
5778 w->start_at_line_beg = 1;
5780 else if (!NILP (w->start))
5781 /* Leaf window has no live buffer, get one. */
5783 /* Get the buffer via other_buffer_safely in order to
5784 avoid showing an unimportant buffer and, if necessary, to
5785 recreate *scratch* in the course (part of Juanma's bs-show
5786 scenario from March 2011). */
5787 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
5788 /* This will set the markers to beginning of visible
5789 range. */
5790 set_marker_restricted_both (w->start, w->contents, 0, 0);
5791 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
5792 w->start_at_line_beg = 1;
5793 if (!NILP (w->dedicated))
5794 /* Record this window as dead. */
5795 dead_windows = Fcons (window, dead_windows);
5796 /* Make sure window is no more dedicated. */
5797 wset_dedicated (w, Qnil);
5801 fset_root_window (f, data->root_window);
5802 /* Arrange *not* to restore point in the buffer that was
5803 current when the window configuration was saved. */
5804 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
5805 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5806 make_number (old_point),
5807 XWINDOW (data->current_window)->contents);
5809 /* In the following call to `select-window', prevent "swapping out
5810 point" in the old selected window using the buffer that has
5811 been restored into it. We already swapped out that point from
5812 that window's old buffer. */
5813 select_window (data->current_window, Qnil, 1);
5814 BVAR (XBUFFER (XWINDOW (selected_window)->contents), last_selected_window)
5815 = selected_window;
5817 if (NILP (data->focus_frame)
5818 || (FRAMEP (data->focus_frame)
5819 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5820 Fredirect_frame_focus (frame, data->focus_frame);
5822 /* Set the screen height to the value it had before this function. */
5823 if (previous_frame_lines != FRAME_LINES (f)
5824 || previous_frame_cols != FRAME_COLS (f))
5825 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5826 0, 0, 0);
5827 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5828 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5829 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5830 make_number (0));
5831 #ifdef HAVE_WINDOW_SYSTEM
5832 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5833 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5834 make_number (0));
5835 #endif
5836 #endif
5838 /* Now, free glyph matrices in windows that were not reused. */
5839 for (i = n = 0; i < n_leaf_windows; ++i)
5841 if (NILP (leaf_windows[i]->contents))
5842 free_window_matrices (leaf_windows[i]);
5843 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
5844 ++n;
5847 adjust_glyphs (f);
5848 unblock_input ();
5850 /* Scan dead buffer windows. */
5851 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5853 window = XCAR (dead_windows);
5854 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5855 delete_deletable_window (window);
5858 /* Fselect_window will have made f the selected frame, so we
5859 reselect the proper frame here. Fhandle_switch_frame will change the
5860 selected window too, but that doesn't make the call to
5861 Fselect_window above totally superfluous; it still sets f's
5862 selected window. */
5863 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5864 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5866 run_window_configuration_change_hook (f);
5869 if (!NILP (new_current_buffer))
5871 Fset_buffer (new_current_buffer);
5872 /* If the new current buffer doesn't appear in the selected
5873 window, go to its old point (see bug#12208). */
5874 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
5875 Fgoto_char (make_number (old_point));
5878 Vminibuf_scroll_window = data->minibuf_scroll_window;
5879 minibuf_selected_window = data->minibuf_selected_window;
5881 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5884 void
5885 restore_window_configuration (Lisp_Object configuration)
5887 Fset_window_configuration (configuration);
5891 /* If WINDOW is an internal window, recursively delete all child windows
5892 reachable via the next and contents slots of WINDOW. Otherwise setup
5893 WINDOW to not show any buffer. */
5895 void
5896 delete_all_child_windows (Lisp_Object window)
5898 register struct window *w;
5900 w = XWINDOW (window);
5902 if (!NILP (w->next))
5903 /* Delete WINDOW's siblings (we traverse postorderly). */
5904 delete_all_child_windows (w->next);
5906 if (WINDOWP (w->contents))
5908 delete_all_child_windows (w->contents);
5909 wset_combination (w, 0, Qnil);
5911 else if (BUFFERP (w->contents))
5913 unshow_buffer (w);
5914 unchain_marker (XMARKER (w->pointm));
5915 unchain_marker (XMARKER (w->start));
5916 /* Since combination limit makes sense for an internal windows
5917 only, we use this slot to save the buffer for the sake of
5918 possible resurrection in Fset_window_configuration. */
5919 wset_combination_limit (w, w->contents);
5920 wset_buffer (w, Qnil);
5923 Vwindow_list = Qnil;
5926 static int
5927 count_windows (register struct window *window)
5929 register int count = 1;
5930 if (!NILP (window->next))
5931 count += count_windows (XWINDOW (window->next));
5932 if (WINDOWP (window->contents))
5933 count += count_windows (XWINDOW (window->contents));
5934 return count;
5938 /* Fill vector FLAT with leaf windows under W, starting at index I.
5939 Value is last index + 1. */
5940 static int
5941 get_leaf_windows (struct window *w, struct window **flat, int i)
5943 while (w)
5945 if (WINDOWP (w->contents))
5946 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
5947 else
5948 flat[i++] = w;
5950 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5953 return i;
5957 /* Return a pointer to the glyph W's physical cursor is on. Value is
5958 null if W's current matrix is invalid, so that no meaningful glyph
5959 can be returned. */
5960 struct glyph *
5961 get_phys_cursor_glyph (struct window *w)
5963 struct glyph_row *row;
5964 struct glyph *glyph;
5965 int hpos = w->phys_cursor.hpos;
5967 if (!(w->phys_cursor.vpos >= 0
5968 && w->phys_cursor.vpos < w->current_matrix->nrows))
5969 return NULL;
5971 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5972 if (!row->enabled_p)
5973 return NULL;
5975 if (w->hscroll)
5977 /* When the window is hscrolled, cursor hpos can legitimately be
5978 out of bounds, but we draw the cursor at the corresponding
5979 window margin in that case. */
5980 if (!row->reversed_p && hpos < 0)
5981 hpos = 0;
5982 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
5983 hpos = row->used[TEXT_AREA] - 1;
5986 if (0 <= hpos && hpos < row->used[TEXT_AREA])
5987 glyph = row->glyphs[TEXT_AREA] + hpos;
5988 else
5989 glyph = NULL;
5991 return glyph;
5995 static int
5996 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
5998 register struct saved_window *p;
5999 register struct window *w;
6000 register Lisp_Object tem, pers, par;
6002 for (; !NILP (window); window = w->next)
6004 p = SAVED_WINDOW_N (vector, i);
6005 w = XWINDOW (window);
6007 wset_temslot (w, make_number (i)); i++;
6008 p->window = window;
6009 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
6010 p->left_col = make_number (w->left_col);
6011 p->top_line = make_number (w->top_line);
6012 p->total_cols = make_number (w->total_cols);
6013 p->total_lines = make_number (w->total_lines);
6014 p->normal_cols = w->normal_cols;
6015 p->normal_lines = w->normal_lines;
6016 XSETFASTINT (p->hscroll, w->hscroll);
6017 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6018 p->display_table = w->display_table;
6019 p->left_margin_cols = w->left_margin_cols;
6020 p->right_margin_cols = w->right_margin_cols;
6021 p->left_fringe_width = w->left_fringe_width;
6022 p->right_fringe_width = w->right_fringe_width;
6023 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6024 p->scroll_bar_width = w->scroll_bar_width;
6025 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6026 p->dedicated = w->dedicated;
6027 p->combination_limit = w->combination_limit;
6028 p->window_parameters = Qnil;
6030 if (!NILP (Vwindow_persistent_parameters))
6032 /* Run cycle detection on Vwindow_persistent_parameters. */
6033 Lisp_Object tortoise, hare;
6035 hare = tortoise = Vwindow_persistent_parameters;
6036 while (CONSP (hare))
6038 hare = XCDR (hare);
6039 if (!CONSP (hare))
6040 break;
6042 hare = XCDR (hare);
6043 tortoise = XCDR (tortoise);
6045 if (EQ (hare, tortoise))
6046 /* Reset Vwindow_persistent_parameters to Qnil. */
6048 Vwindow_persistent_parameters = Qnil;
6049 break;
6053 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6054 tem = XCDR (tem))
6056 pers = XCAR (tem);
6057 /* Save values for persistent window parameters. */
6058 if (CONSP (pers) && !NILP (XCDR (pers)))
6060 par = Fassq (XCAR (pers), w->window_parameters);
6061 if (NILP (par))
6062 /* If the window has no value for the parameter,
6063 make one. */
6064 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6065 p->window_parameters);
6066 else
6067 /* If the window has a value for the parameter,
6068 save it. */
6069 p->window_parameters = Fcons (Fcons (XCAR (par),
6070 XCDR (par)),
6071 p->window_parameters);
6076 if (BUFFERP (w->contents))
6078 /* Save w's value of point in the window configuration. If w
6079 is the selected window, then get the value of point from
6080 the buffer; pointm is garbage in the selected window. */
6081 if (EQ (window, selected_window))
6082 p->pointm = build_marker (XBUFFER (w->contents),
6083 BUF_PT (XBUFFER (w->contents)),
6084 BUF_PT_BYTE (XBUFFER (w->contents)));
6085 else
6086 p->pointm = Fcopy_marker (w->pointm, Qnil);
6087 XMARKER (p->pointm)->insertion_type
6088 = !NILP (Vwindow_point_insertion_type);
6090 p->start = Fcopy_marker (w->start, Qnil);
6091 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6093 tem = BVAR (XBUFFER (w->contents), mark);
6094 p->mark = Fcopy_marker (tem, Qnil);
6096 else
6098 p->pointm = Qnil;
6099 p->start = Qnil;
6100 p->mark = Qnil;
6101 p->start_at_line_beg = Qnil;
6104 if (NILP (w->parent))
6105 p->parent = Qnil;
6106 else
6107 p->parent = XWINDOW (w->parent)->temslot;
6109 if (NILP (w->prev))
6110 p->prev = Qnil;
6111 else
6112 p->prev = XWINDOW (w->prev)->temslot;
6114 if (WINDOWP (w->contents))
6115 i = save_window_save (w->contents, vector, i);
6118 return i;
6121 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6122 Scurrent_window_configuration, 0, 1, 0,
6123 doc: /* Return an object representing the current window configuration of FRAME.
6124 If FRAME is nil or omitted, use the selected frame.
6125 This describes the number of windows, their sizes and current buffers,
6126 and for each displayed buffer, where display starts, and the positions of
6127 point and mark. An exception is made for point in the current buffer:
6128 its value is -not- saved.
6129 This also records the currently selected frame, and FRAME's focus
6130 redirection (see `redirect-frame-focus'). The variable
6131 `window-persistent-parameters' specifies which window parameters are
6132 saved by this function. */)
6133 (Lisp_Object frame)
6135 register Lisp_Object tem;
6136 register int n_windows;
6137 register struct save_window_data *data;
6138 register int i;
6139 struct frame *f = decode_live_frame (frame);
6141 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6142 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6143 PVEC_WINDOW_CONFIGURATION);
6145 data->frame_cols = FRAME_COLS (f);
6146 data->frame_lines = FRAME_LINES (f);
6147 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6148 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6149 data->selected_frame = selected_frame;
6150 data->current_window = FRAME_SELECTED_WINDOW (f);
6151 XSETBUFFER (data->current_buffer, current_buffer);
6152 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6153 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6154 data->root_window = FRAME_ROOT_WINDOW (f);
6155 data->focus_frame = FRAME_FOCUS_FRAME (f);
6156 tem = make_uninit_vector (n_windows);
6157 data->saved_windows = tem;
6158 for (i = 0; i < n_windows; i++)
6159 ASET (tem, i,
6160 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6161 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6162 XSETWINDOW_CONFIGURATION (tem, data);
6163 return (tem);
6166 /***********************************************************************
6167 Marginal Areas
6168 ***********************************************************************/
6170 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6171 2, 3, 0,
6172 doc: /* Set width of marginal areas of window WINDOW.
6173 WINDOW must be a live window and defaults to the selected one.
6175 Second arg LEFT-WIDTH specifies the number of character cells to
6176 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6177 does the same for the right marginal area. A nil width parameter
6178 means no margin. */)
6179 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6181 struct window *w = decode_live_window (window);
6183 /* Translate negative or zero widths to nil.
6184 Margins that are too wide have to be checked elsewhere. */
6186 if (!NILP (left_width))
6188 CHECK_NUMBER (left_width);
6189 if (XINT (left_width) <= 0)
6190 left_width = Qnil;
6193 if (!NILP (right_width))
6195 CHECK_NUMBER (right_width);
6196 if (XINT (right_width) <= 0)
6197 right_width = Qnil;
6200 if (!EQ (w->left_margin_cols, left_width)
6201 || !EQ (w->right_margin_cols, right_width))
6203 wset_left_margin_cols (w, left_width);
6204 wset_right_margin_cols (w, right_width);
6206 adjust_window_margins (w);
6208 ++windows_or_buffers_changed;
6209 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6212 return Qnil;
6216 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6217 0, 1, 0,
6218 doc: /* Get width of marginal areas of window WINDOW.
6219 WINDOW must be a live window and defaults to the selected one.
6221 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6222 If a marginal area does not exist, its width will be returned
6223 as nil. */)
6224 (Lisp_Object window)
6226 struct window *w = decode_live_window (window);
6227 return Fcons (w->left_margin_cols, w->right_margin_cols);
6232 /***********************************************************************
6233 Fringes
6234 ***********************************************************************/
6236 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6237 2, 4, 0,
6238 doc: /* Set the fringe widths of window WINDOW.
6239 WINDOW must be a live window and defaults to the selected one.
6241 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6242 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6243 fringe width. If a fringe width arg is nil, that means to use the
6244 frame's default fringe width. Default fringe widths can be set with
6245 the command `set-fringe-style'.
6246 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6247 outside of the display margins. By default, fringes are drawn between
6248 display marginal areas and the text area. */)
6249 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6251 struct window *w = decode_live_window (window);
6252 int outside = !NILP (outside_margins);
6254 if (!NILP (left_width))
6255 CHECK_NATNUM (left_width);
6256 if (!NILP (right_width))
6257 CHECK_NATNUM (right_width);
6259 /* Do nothing on a tty. */
6260 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6261 && (!EQ (w->left_fringe_width, left_width)
6262 || !EQ (w->right_fringe_width, right_width)
6263 || w->fringes_outside_margins != outside))
6265 wset_left_fringe_width (w, left_width);
6266 wset_right_fringe_width (w, right_width);
6267 w->fringes_outside_margins = outside;
6269 adjust_window_margins (w);
6271 clear_glyph_matrix (w->current_matrix);
6272 w->window_end_valid = 0;
6274 ++windows_or_buffers_changed;
6275 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6278 return Qnil;
6282 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6283 0, 1, 0,
6284 doc: /* Get width of fringes of window WINDOW.
6285 WINDOW must be a live window and defaults to the selected one.
6287 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6288 (Lisp_Object window)
6290 struct window *w = decode_live_window (window);
6292 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6293 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6294 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6299 /***********************************************************************
6300 Scroll bars
6301 ***********************************************************************/
6303 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6304 Sset_window_scroll_bars, 2, 4, 0,
6305 doc: /* Set width and type of scroll bars of window WINDOW.
6306 WINDOW must be a live window and defaults to the selected one.
6308 Second parameter WIDTH specifies the pixel width for the scroll bar;
6309 this is automatically adjusted to a multiple of the frame column width.
6310 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6311 bar: left, right, or nil.
6312 If WIDTH is nil, use the frame's scroll-bar width.
6313 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6314 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6315 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6317 struct window *w = decode_live_window (window);
6319 if (!NILP (width))
6321 CHECK_RANGED_INTEGER (width, 0, INT_MAX);
6323 if (XINT (width) == 0)
6324 vertical_type = Qnil;
6327 if (!(NILP (vertical_type)
6328 || EQ (vertical_type, Qleft)
6329 || EQ (vertical_type, Qright)
6330 || EQ (vertical_type, Qt)))
6331 error ("Invalid type of vertical scroll bar");
6333 if (!EQ (w->scroll_bar_width, width)
6334 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6336 wset_scroll_bar_width (w, width);
6337 wset_vertical_scroll_bar_type (w, vertical_type);
6339 adjust_window_margins (w);
6341 clear_glyph_matrix (w->current_matrix);
6342 w->window_end_valid = 0;
6344 ++windows_or_buffers_changed;
6345 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6348 return Qnil;
6352 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6353 0, 1, 0,
6354 doc: /* Get width and type of scroll bars of window WINDOW.
6355 WINDOW must be a live window and defaults to the selected one.
6357 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6358 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6359 value. */)
6360 (Lisp_Object window)
6362 struct window *w = decode_live_window (window);
6364 return list4 (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6365 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6366 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6367 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6368 w->vertical_scroll_bar_type, Qnil);
6373 /***********************************************************************
6374 Smooth scrolling
6375 ***********************************************************************/
6377 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6378 doc: /* Return the amount by which WINDOW is scrolled vertically.
6379 If WINDOW is omitted or nil, it defaults to the selected window.
6380 Normally, value is a multiple of the canonical character height of WINDOW;
6381 optional second arg PIXELS-P means value is measured in pixels. */)
6382 (Lisp_Object window, Lisp_Object pixels_p)
6384 Lisp_Object result;
6385 struct window *w = decode_live_window (window);
6386 struct frame *f = XFRAME (w->frame);
6388 if (FRAME_WINDOW_P (f))
6389 result = (NILP (pixels_p)
6390 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6391 : make_number (-w->vscroll));
6392 else
6393 result = make_number (0);
6394 return result;
6398 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6399 2, 3, 0,
6400 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6401 WINDOW nil means use the selected window. Normally, VSCROLL is a
6402 non-negative multiple of the canonical character height of WINDOW;
6403 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6404 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6405 corresponds to an integral number of pixels. The return value is the
6406 result of this rounding.
6407 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6408 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6410 struct window *w = decode_live_window (window);
6411 struct frame *f = XFRAME (w->frame);
6413 CHECK_NUMBER_OR_FLOAT (vscroll);
6415 if (FRAME_WINDOW_P (f))
6417 int old_dy = w->vscroll;
6419 w->vscroll = - (NILP (pixels_p)
6420 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6421 : XFLOATINT (vscroll));
6422 w->vscroll = min (w->vscroll, 0);
6424 if (w->vscroll != old_dy)
6426 /* Adjust glyph matrix of the frame if the virtual display
6427 area becomes larger than before. */
6428 if (w->vscroll < 0 && w->vscroll < old_dy)
6429 adjust_glyphs (f);
6431 /* Prevent redisplay shortcuts. */
6432 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
6436 return Fwindow_vscroll (window, pixels_p);
6440 /* Call FN for all leaf windows on frame F. FN is called with the
6441 first argument being a pointer to the leaf window, and with
6442 additional argument USER_DATA. Stops when FN returns 0. */
6444 static void
6445 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6446 void *user_data)
6448 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6449 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6450 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6454 /* Helper function for foreach_window. Call FN for all leaf windows
6455 reachable from W. FN is called with the first argument being a
6456 pointer to the leaf window, and with additional argument USER_DATA.
6457 Stop when FN returns 0. Value is 0 if stopped by FN. */
6459 static int
6460 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6462 int cont;
6464 for (cont = 1; w && cont;)
6466 if (WINDOWP (w->contents))
6467 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
6468 else
6469 cont = fn (w, user_data);
6471 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6474 return cont;
6478 /* Freeze or unfreeze the window start of W unless it is a
6479 mini-window or the selected window. FREEZE_P non-null means freeze
6480 the window start. */
6482 static int
6483 freeze_window_start (struct window *w, void *freeze_p)
6485 if (MINI_WINDOW_P (w)
6486 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6487 && (w == XWINDOW (selected_window)
6488 || (MINI_WINDOW_P (XWINDOW (selected_window))
6489 && ! NILP (Vminibuf_scroll_window)
6490 && w == XWINDOW (Vminibuf_scroll_window)))))
6491 freeze_p = NULL;
6493 w->frozen_window_start_p = freeze_p != NULL;
6494 return 1;
6498 /* Freeze or unfreeze the window starts of all leaf windows on frame
6499 F, except the selected window and a mini-window. FREEZE_P non-zero
6500 means freeze the window start. */
6502 void
6503 freeze_window_starts (struct frame *f, bool freeze_p)
6505 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6509 /***********************************************************************
6510 Initialization
6511 ***********************************************************************/
6513 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6514 describe the same state of affairs. This is used by Fequal.
6516 IGNORE_POSITIONS means ignore non-matching scroll positions
6517 and the like.
6519 This ignores a couple of things like the dedication status of
6520 window, combination_limit and the like. This might have to be
6521 fixed. */
6523 bool
6524 compare_window_configurations (Lisp_Object configuration1,
6525 Lisp_Object configuration2,
6526 bool ignore_positions)
6528 register struct save_window_data *d1, *d2;
6529 struct Lisp_Vector *sws1, *sws2;
6530 ptrdiff_t i;
6532 CHECK_WINDOW_CONFIGURATION (configuration1);
6533 CHECK_WINDOW_CONFIGURATION (configuration2);
6535 d1 = (struct save_window_data *) XVECTOR (configuration1);
6536 d2 = (struct save_window_data *) XVECTOR (configuration2);
6537 sws1 = XVECTOR (d1->saved_windows);
6538 sws2 = XVECTOR (d2->saved_windows);
6540 /* Frame settings must match. */
6541 if (d1->frame_cols != d2->frame_cols
6542 || d1->frame_lines != d2->frame_lines
6543 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6544 || !EQ (d1->selected_frame, d2->selected_frame)
6545 || !EQ (d1->current_buffer, d2->current_buffer)
6546 || (!ignore_positions
6547 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6548 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6549 || !EQ (d1->focus_frame, d2->focus_frame)
6550 /* Verify that the two configurations have the same number of windows. */
6551 || sws1->header.size != sws2->header.size)
6552 return 0;
6554 for (i = 0; i < sws1->header.size; i++)
6556 struct saved_window *sw1, *sw2;
6558 sw1 = SAVED_WINDOW_N (sws1, i);
6559 sw2 = SAVED_WINDOW_N (sws2, i);
6561 if (
6562 /* The "current" windows in the two configurations must
6563 correspond to each other. */
6564 EQ (d1->current_window, sw1->window)
6565 != EQ (d2->current_window, sw2->window)
6566 /* Windows' buffers must match. */
6567 || !EQ (sw1->buffer, sw2->buffer)
6568 || !EQ (sw1->left_col, sw2->left_col)
6569 || !EQ (sw1->top_line, sw2->top_line)
6570 || !EQ (sw1->total_cols, sw2->total_cols)
6571 || !EQ (sw1->total_lines, sw2->total_lines)
6572 || !EQ (sw1->display_table, sw2->display_table)
6573 /* The next two disjuncts check the window structure for
6574 equality. */
6575 || !EQ (sw1->parent, sw2->parent)
6576 || !EQ (sw1->prev, sw2->prev)
6577 || (!ignore_positions
6578 && (!EQ (sw1->hscroll, sw2->hscroll)
6579 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6580 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6581 || NILP (Fequal (sw1->start, sw2->start))
6582 || NILP (Fequal (sw1->pointm, sw2->pointm))
6583 || NILP (Fequal (sw1->mark, sw2->mark))))
6584 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6585 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6586 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6587 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6588 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6589 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6590 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6591 return 0;
6594 return 1;
6597 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6598 Scompare_window_configurations, 2, 2, 0,
6599 doc: /* Compare two window configurations as regards the structure of windows.
6600 This function ignores details such as the values of point and mark
6601 and scrolling positions. */)
6602 (Lisp_Object x, Lisp_Object y)
6604 if (compare_window_configurations (x, y, 1))
6605 return Qt;
6606 return Qnil;
6609 void
6610 init_window_once (void)
6612 struct frame *f = make_initial_frame ();
6613 XSETFRAME (selected_frame, f);
6614 Vterminal_frame = selected_frame;
6615 minibuf_window = f->minibuffer_window;
6616 selected_window = f->selected_window;
6617 last_nonminibuf_frame = f;
6619 window_initialized = 1;
6622 void
6623 init_window (void)
6625 Vwindow_list = Qnil;
6628 void
6629 syms_of_window (void)
6631 DEFSYM (Qscroll_up, "scroll-up");
6632 DEFSYM (Qscroll_down, "scroll-down");
6633 DEFSYM (Qscroll_command, "scroll-command");
6635 Fput (Qscroll_up, Qscroll_command, Qt);
6636 Fput (Qscroll_down, Qscroll_command, Qt);
6638 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6639 DEFSYM (Qwindowp, "windowp");
6640 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6641 DEFSYM (Qwindow_live_p, "window-live-p");
6642 DEFSYM (Qwindow_valid_p, "window-valid-p");
6643 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6644 DEFSYM (Qdelete_window, "delete-window");
6645 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6646 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6647 DEFSYM (Qsafe, "safe");
6648 DEFSYM (Qdisplay_buffer, "display-buffer");
6649 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6650 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6651 DEFSYM (Qget_mru_window, "get-mru-window");
6652 DEFSYM (Qwindow_size, "window-size");
6653 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6654 DEFSYM (Qabove, "above");
6655 DEFSYM (Qbelow, "below");
6656 DEFSYM (Qclone_of, "clone-of");
6658 staticpro (&Vwindow_list);
6660 minibuf_selected_window = Qnil;
6661 staticpro (&minibuf_selected_window);
6663 window_scroll_pixel_based_preserve_x = -1;
6664 window_scroll_pixel_based_preserve_y = -1;
6665 window_scroll_preserve_hpos = -1;
6666 window_scroll_preserve_vpos = -1;
6668 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6669 doc: /* Non-nil means call as function to display a help buffer.
6670 The function is called with one argument, the buffer to be displayed.
6671 Used by `with-output-to-temp-buffer'.
6672 If this function is used, then it must do the entire job of showing
6673 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6674 Vtemp_buffer_show_function = Qnil;
6676 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6677 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6678 Vminibuf_scroll_window = Qnil;
6680 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6681 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6682 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6683 is displayed in the `mode-line' face. */);
6684 mode_line_in_non_selected_windows = 1;
6686 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6687 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6688 Vother_window_scroll_buffer = Qnil;
6690 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6691 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6692 auto_window_vscroll_p = 1;
6694 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6695 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6696 next_screen_context_lines = 2;
6698 DEFVAR_LISP ("scroll-preserve-screen-position",
6699 Vscroll_preserve_screen_position,
6700 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6701 A value of nil means point does not keep its screen position except
6702 at the scroll margin or window boundary respectively.
6703 A value of t means point keeps its screen position if the scroll
6704 command moved it vertically out of the window, e.g. when scrolling
6705 by full screens.
6706 Any other value means point always keeps its screen position.
6707 Scroll commands should have the `scroll-command' property
6708 on their symbols to be controlled by this variable. */);
6709 Vscroll_preserve_screen_position = Qnil;
6711 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6712 doc: /* Type of marker to use for `window-point'. */);
6713 Vwindow_point_insertion_type = Qnil;
6715 DEFVAR_LISP ("window-configuration-change-hook",
6716 Vwindow_configuration_change_hook,
6717 doc: /* Functions to call when window configuration changes.
6718 The buffer-local part is run once per window, with the relevant window
6719 selected; while the global part is run only once for the modified frame,
6720 with the relevant frame selected. */);
6721 Vwindow_configuration_change_hook = Qnil;
6723 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6724 doc: /* Non-nil means `recenter' redraws entire frame.
6725 If this option is non-nil, then the `recenter' command with a nil
6726 argument will redraw the entire frame; the special value `tty' causes
6727 the frame to be redrawn only if it is a tty frame. */);
6728 Vrecenter_redisplay = Qtty;
6730 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6731 doc: /* If t, resize window combinations proportionally.
6732 If this variable is nil, splitting a window gets the entire screen space
6733 for displaying the new window from the window to split. Deleting and
6734 resizing a window preferably resizes one adjacent window only.
6736 If this variable is t, splitting a window tries to get the space
6737 proportionally from all windows in the same combination. This also
6738 allows to split a window that is otherwise too small or of fixed size.
6739 Resizing and deleting a window proportionally resize all windows in the
6740 same combination.
6742 Other values are reserved for future use.
6744 This variable takes no effect if the variable `window-combination-limit' is
6745 non-nil. */);
6746 Vwindow_combination_resize = Qnil;
6748 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6749 doc: /* If non-nil, splitting a window makes a new parent window.
6750 The following values are recognized:
6752 nil means splitting a window will create a new parent window only if the
6753 window has no parent window or the window shall become part of a
6754 combination orthogonal to the one it is part of.
6756 `window-size' means that splitting a window for displaying a buffer
6757 makes a new parent window provided `display-buffer' is supposed to
6758 explicitly set the window's size due to the presence of a
6759 `window-height' or `window-width' entry in the alist used by
6760 `display-buffer'. Otherwise, this value is handled like nil.
6762 `temp-buffer' means that splitting a window for displaying a temporary
6763 buffer always makes a new parent window. Otherwise, this value is
6764 handled like nil.
6766 `display-buffer' means that splitting a window for displaying a buffer
6767 always makes a new parent window. Since temporary buffers are
6768 displayed by the function `display-buffer', this value is stronger
6769 than `temp-buffer'. Splitting a window for other purpose makes a
6770 new parent window only if needed.
6772 t means that splitting a window always creates a new parent window. If
6773 all splits behave this way, each frame's window tree is a binary
6774 tree and every window but the frame's root window has exactly one
6775 sibling.
6777 Other values are reserved for future use. */);
6778 Vwindow_combination_limit = Qwindow_size;
6780 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6781 doc: /* Alist of persistent window parameters.
6782 This alist specifies which window parameters shall get saved by
6783 `current-window-configuration' and `window-state-get' and subsequently
6784 restored to their previous values by `set-window-configuration' and
6785 `window-state-put'.
6787 The car of each entry of this alist is the symbol specifying the
6788 parameter. The cdr is one of the following:
6790 nil means the parameter is neither saved by `window-state-get' nor by
6791 `current-window-configuration'.
6793 t means the parameter is saved by `current-window-configuration' and,
6794 provided its WRITABLE argument is nil, by `window-state-get'.
6796 The symbol `writable' means the parameter is saved unconditionally by
6797 both `current-window-configuration' and `window-state-get'. Do not use
6798 this value for parameters without read syntax (like windows or frames).
6800 Parameters not saved by `current-window-configuration' or
6801 `window-state-get' are left alone by `set-window-configuration'
6802 respectively are not installed by `window-state-put'. */);
6803 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6805 defsubr (&Sselected_window);
6806 defsubr (&Sminibuffer_window);
6807 defsubr (&Swindow_minibuffer_p);
6808 defsubr (&Swindowp);
6809 defsubr (&Swindow_valid_p);
6810 defsubr (&Swindow_live_p);
6811 defsubr (&Swindow_frame);
6812 defsubr (&Sframe_root_window);
6813 defsubr (&Sframe_first_window);
6814 defsubr (&Sframe_selected_window);
6815 defsubr (&Sset_frame_selected_window);
6816 defsubr (&Spos_visible_in_window_p);
6817 defsubr (&Swindow_line_height);
6818 defsubr (&Swindow_buffer);
6819 defsubr (&Swindow_parent);
6820 defsubr (&Swindow_top_child);
6821 defsubr (&Swindow_left_child);
6822 defsubr (&Swindow_next_sibling);
6823 defsubr (&Swindow_prev_sibling);
6824 defsubr (&Swindow_combination_limit);
6825 defsubr (&Sset_window_combination_limit);
6826 defsubr (&Swindow_use_time);
6827 defsubr (&Swindow_top_line);
6828 defsubr (&Swindow_left_column);
6829 defsubr (&Swindow_total_height);
6830 defsubr (&Swindow_total_width);
6831 defsubr (&Swindow_normal_size);
6832 defsubr (&Swindow_new_total);
6833 defsubr (&Swindow_new_normal);
6834 defsubr (&Sset_window_new_total);
6835 defsubr (&Sset_window_new_normal);
6836 defsubr (&Swindow_resize_apply);
6837 defsubr (&Swindow_body_height);
6838 defsubr (&Swindow_body_width);
6839 defsubr (&Swindow_hscroll);
6840 defsubr (&Sset_window_hscroll);
6841 defsubr (&Swindow_redisplay_end_trigger);
6842 defsubr (&Sset_window_redisplay_end_trigger);
6843 defsubr (&Swindow_edges);
6844 defsubr (&Swindow_pixel_edges);
6845 defsubr (&Swindow_absolute_pixel_edges);
6846 defsubr (&Swindow_inside_edges);
6847 defsubr (&Swindow_inside_pixel_edges);
6848 defsubr (&Swindow_inside_absolute_pixel_edges);
6849 defsubr (&Scoordinates_in_window_p);
6850 defsubr (&Swindow_at);
6851 defsubr (&Swindow_point);
6852 defsubr (&Swindow_start);
6853 defsubr (&Swindow_end);
6854 defsubr (&Sset_window_point);
6855 defsubr (&Sset_window_start);
6856 defsubr (&Swindow_dedicated_p);
6857 defsubr (&Sset_window_dedicated_p);
6858 defsubr (&Swindow_display_table);
6859 defsubr (&Sset_window_display_table);
6860 defsubr (&Snext_window);
6861 defsubr (&Sprevious_window);
6862 defsubr (&Sget_buffer_window);
6863 defsubr (&Sdelete_other_windows_internal);
6864 defsubr (&Sdelete_window_internal);
6865 defsubr (&Sresize_mini_window_internal);
6866 defsubr (&Sset_window_buffer);
6867 defsubr (&Srun_window_configuration_change_hook);
6868 defsubr (&Sselect_window);
6869 defsubr (&Sforce_window_update);
6870 defsubr (&Ssplit_window_internal);
6871 defsubr (&Sscroll_up);
6872 defsubr (&Sscroll_down);
6873 defsubr (&Sscroll_left);
6874 defsubr (&Sscroll_right);
6875 defsubr (&Sother_window_for_scrolling);
6876 defsubr (&Sscroll_other_window);
6877 defsubr (&Sminibuffer_selected_window);
6878 defsubr (&Srecenter);
6879 defsubr (&Swindow_text_height);
6880 defsubr (&Smove_to_window_line);
6881 defsubr (&Swindow_configuration_p);
6882 defsubr (&Swindow_configuration_frame);
6883 defsubr (&Sset_window_configuration);
6884 defsubr (&Scurrent_window_configuration);
6885 defsubr (&Sset_window_margins);
6886 defsubr (&Swindow_margins);
6887 defsubr (&Sset_window_fringes);
6888 defsubr (&Swindow_fringes);
6889 defsubr (&Sset_window_scroll_bars);
6890 defsubr (&Swindow_scroll_bars);
6891 defsubr (&Swindow_vscroll);
6892 defsubr (&Sset_window_vscroll);
6893 defsubr (&Scompare_window_configurations);
6894 defsubr (&Swindow_list);
6895 defsubr (&Swindow_list_1);
6896 defsubr (&Swindow_prev_buffers);
6897 defsubr (&Sset_window_prev_buffers);
6898 defsubr (&Swindow_next_buffers);
6899 defsubr (&Sset_window_next_buffers);
6900 defsubr (&Swindow_parameters);
6901 defsubr (&Swindow_parameter);
6902 defsubr (&Sset_window_parameter);
6905 void
6906 keys_of_window (void)
6908 initial_define_key (control_x_map, '<', "scroll-left");
6909 initial_define_key (control_x_map, '>', "scroll-right");
6911 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6912 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6913 initial_define_key (meta_map, 'v', "scroll-down-command");