Merge from emacs-24; up to 2013-01-03T02:31:36Z!rgm@gnu.org
[emacs.git] / src / window.c
blob677cb991025b23420a28eb70e81fb9a35b904f9a
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. */
42 #ifdef HAVE_WINDOW_SYSTEM
43 #include TERM_HEADER
44 #endif /* HAVE_WINDOW_SYSTEM */
45 #ifdef MSDOS
46 #include "msdos.h"
47 #endif
49 Lisp_Object Qwindowp, Qwindow_live_p;
50 static Lisp_Object Qwindow_valid_p;
51 static Lisp_Object Qwindow_configuration_p;
52 static Lisp_Object Qrecord_window_buffer;
53 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
54 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
55 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
56 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
57 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
59 static int displayed_window_lines (struct window *);
60 static int count_windows (struct window *);
61 static int get_leaf_windows (struct window *, struct window **, int);
62 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
63 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
64 static void window_scroll_line_based (Lisp_Object, int, bool, int);
65 static Lisp_Object window_list (void);
66 static int add_window_to_list (struct window *, void *);
67 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
68 Lisp_Object, int);
69 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
70 Lisp_Object *);
71 static void foreach_window (struct frame *,
72 int (* fn) (struct window *, void *),
73 void *);
74 static int foreach_window_1 (struct window *,
75 int (* fn) (struct window *, void *),
76 void *);
77 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
78 static int window_resize_check (struct window *, bool);
79 static void window_resize_apply (struct window *, bool);
80 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
81 static void select_window_1 (Lisp_Object, bool);
83 static struct window *set_window_fringes (struct window *, Lisp_Object,
84 Lisp_Object, Lisp_Object);
85 static struct window *set_window_margins (struct window *, Lisp_Object,
86 Lisp_Object);
87 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
88 Lisp_Object, Lisp_Object);
89 static void apply_window_adjustment (struct window *);
91 /* This is the window in which the terminal's cursor should
92 be left when nothing is being done with it. This must
93 always be a leaf window, and its buffer is selected by
94 the top level editing loop at the end of each command.
96 This value is always the same as
97 FRAME_SELECTED_WINDOW (selected_frame). */
98 Lisp_Object selected_window;
100 /* A list of all windows for use by next_window and Fwindow_list.
101 Functions creating or deleting windows should invalidate this cache
102 by setting it to nil. */
103 Lisp_Object Vwindow_list;
105 /* The mini-buffer window of the selected frame.
106 Note that you cannot test for mini-bufferness of an arbitrary window
107 by comparing against this; but you can test for mini-bufferness of
108 the selected window. */
109 Lisp_Object minibuf_window;
111 /* Non-nil means it is the window whose mode line should be
112 shown as the selected window when the minibuffer is selected. */
113 Lisp_Object minibuf_selected_window;
115 /* Hook run at end of temp_output_buffer_show. */
116 static Lisp_Object Qtemp_buffer_show_hook;
118 /* 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_new_normal (struct window *w, Lisp_Object val)
157 w->new_normal = val;
159 static void
160 wset_new_total (struct window *w, Lisp_Object val)
162 w->new_total = val;
164 static void
165 wset_normal_cols (struct window *w, Lisp_Object val)
167 w->normal_cols = val;
169 static void
170 wset_normal_lines (struct window *w, Lisp_Object val)
172 w->normal_lines = val;
174 static void
175 wset_parent (struct window *w, Lisp_Object val)
177 w->parent = val;
179 static void
180 wset_pointm (struct window *w, Lisp_Object val)
182 w->pointm = val;
184 static void
185 wset_start (struct window *w, Lisp_Object val)
187 w->start = val;
189 static void
190 wset_temslot (struct window *w, Lisp_Object val)
192 w->temslot = val;
194 static void
195 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
197 w->vertical_scroll_bar_type = val;
199 static void
200 wset_window_parameters (struct window *w, Lisp_Object val)
202 w->window_parameters = val;
204 static void
205 wset_combination (struct window *w, bool horflag, Lisp_Object val)
207 /* Since leaf windows never becomes non-leaf, there should
208 be no buffer and markers in start and pointm fields of W. */
209 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
210 w->contents = val;
211 /* When an internal window is deleted and VAL is nil, HORFLAG
212 is meaningless. */
213 if (!NILP (val))
214 w->horizontal = horflag;
217 /* Nonzero if leaf window W doesn't reflect the actual state
218 of displayed buffer due to its text or overlays change. */
220 bool
221 window_outdated (struct window *w)
223 struct buffer *b = XBUFFER (w->contents);
224 return (w->last_modified < BUF_MODIFF (b)
225 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
228 struct window *
229 decode_live_window (register Lisp_Object window)
231 if (NILP (window))
232 return XWINDOW (selected_window);
234 CHECK_LIVE_WINDOW (window);
235 return XWINDOW (window);
238 struct window *
239 decode_any_window (register Lisp_Object window)
241 struct window *w;
243 if (NILP (window))
244 return XWINDOW (selected_window);
246 CHECK_WINDOW (window);
247 w = XWINDOW (window);
248 return w;
251 static struct window *
252 decode_valid_window (register Lisp_Object window)
254 struct window *w;
256 if (NILP (window))
257 return XWINDOW (selected_window);
259 CHECK_VALID_WINDOW (window);
260 w = XWINDOW (window);
261 return w;
264 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
265 cease its buffer, and 1 means that W is about to set up the new one. */
267 static void
268 adjust_window_count (struct window *w, int arg)
270 eassert (eabs (arg) == 1);
271 if (BUFFERP (w->contents))
273 struct buffer *b = XBUFFER (w->contents);
275 if (b->base_buffer)
276 b = b->base_buffer;
277 b->window_count += arg;
278 eassert (b->window_count >= 0);
279 /* These should be recalculated by redisplay code. */
280 w->window_end_valid = 0;
281 w->base_line_pos = 0;
285 /* Set W's buffer slot to VAL and recompute number
286 of windows showing VAL if it is a buffer. */
288 void
289 wset_buffer (struct window *w, Lisp_Object val)
291 adjust_window_count (w, -1);
292 if (BUFFERP (val))
293 /* Make sure that we do not assign the buffer
294 to an internal window. */
295 eassert (MARKERP (w->start) && MARKERP (w->pointm));
296 w->contents = val;
297 adjust_window_count (w, 1);
300 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
301 doc: /* Return t if OBJECT is a window and nil otherwise. */)
302 (Lisp_Object object)
304 return WINDOWP (object) ? Qt : Qnil;
307 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
308 doc: /* Return t if OBJECT is a valid window and nil otherwise.
309 A valid window is either a window that displays a buffer or an internal
310 window. Deleted windows are not live. */)
311 (Lisp_Object object)
313 return WINDOW_VALID_P (object) ? Qt : Qnil;
316 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
317 doc: /* Return t if OBJECT is a live window and nil otherwise.
318 A live window is a window that displays a buffer.
319 Internal windows and deleted windows are not live. */)
320 (Lisp_Object object)
322 return WINDOW_LIVE_P (object) ? Qt : Qnil;
325 /* Frames and windows. */
326 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
327 doc: /* Return the frame that window WINDOW is on.
328 WINDOW must be a valid window and defaults to the selected one. */)
329 (Lisp_Object window)
331 return decode_valid_window (window)->frame;
334 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
335 doc: /* Return the root window of FRAME-OR-WINDOW.
336 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
337 With a frame argument, return that frame's root window.
338 With a window argument, return the root window of that window's frame. */)
339 (Lisp_Object frame_or_window)
341 Lisp_Object window;
343 if (NILP (frame_or_window))
344 window = SELECTED_FRAME ()->root_window;
345 else if (WINDOW_VALID_P (frame_or_window))
346 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
347 else
349 CHECK_LIVE_FRAME (frame_or_window);
350 window = XFRAME (frame_or_window)->root_window;
353 return window;
356 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
357 doc: /* Return the minibuffer window for frame FRAME.
358 If FRAME is omitted or nil, it defaults to the selected frame. */)
359 (Lisp_Object frame)
361 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
364 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
365 Swindow_minibuffer_p, 0, 1, 0,
366 doc: /* Return non-nil if WINDOW is a minibuffer window.
367 WINDOW must be a valid window and defaults to the selected one. */)
368 (Lisp_Object window)
370 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
373 /* Don't move this to window.el - this must be a safe routine. */
374 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
375 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
376 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
377 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
378 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
379 the first window of that frame. */)
380 (Lisp_Object frame_or_window)
382 Lisp_Object window;
384 if (NILP (frame_or_window))
385 window = SELECTED_FRAME ()->root_window;
386 else if (WINDOW_VALID_P (frame_or_window))
387 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
388 else
390 CHECK_LIVE_FRAME (frame_or_window);
391 window = XFRAME (frame_or_window)->root_window;
394 while (WINDOWP (XWINDOW (window)->contents))
395 window = XWINDOW (window)->contents;
397 return window;
400 DEFUN ("frame-selected-window", Fframe_selected_window,
401 Sframe_selected_window, 0, 1, 0,
402 doc: /* Return the selected window of FRAME-OR-WINDOW.
403 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
404 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
405 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
406 return the selected window of that frame. */)
407 (Lisp_Object frame_or_window)
409 Lisp_Object window;
411 if (NILP (frame_or_window))
412 window = SELECTED_FRAME ()->selected_window;
413 else if (WINDOW_VALID_P (frame_or_window))
414 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
415 else
417 CHECK_LIVE_FRAME (frame_or_window);
418 window = XFRAME (frame_or_window)->selected_window;
421 return window;
424 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
425 Sset_frame_selected_window, 2, 3, 0,
426 doc: /* Set selected window of FRAME to WINDOW.
427 FRAME must be a live frame and defaults to the selected one. If FRAME
428 is the selected frame, this makes WINDOW the selected window. Optional
429 argument NORECORD non-nil means to neither change the order of recently
430 selected windows nor the buffer list. WINDOW must denote a live window.
431 Return WINDOW. */)
432 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
434 if (NILP (frame))
435 frame = selected_frame;
437 CHECK_LIVE_FRAME (frame);
438 CHECK_LIVE_WINDOW (window);
440 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
441 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
443 if (EQ (frame, selected_frame))
444 return Fselect_window (window, norecord);
445 else
447 fset_selected_window (XFRAME (frame), window);
448 return window;
452 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
453 doc: /* Return the selected window.
454 The selected window is the window in which the standard cursor for
455 selected windows appears and to which many commands apply. */)
456 (void)
458 return selected_window;
461 int window_select_count;
463 /* If select_window is called with inhibit_point_swap non-zero it will
464 not store point of the old selected window's buffer back into that
465 window's pointm slot. This is needed by Fset_window_configuration to
466 avoid that the display routine is called with selected_window set to
467 Qnil causing a subsequent crash. */
468 static Lisp_Object
469 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
471 register struct window *w;
472 struct frame *sf;
474 CHECK_LIVE_WINDOW (window);
476 w = XWINDOW (window);
478 /* Make the selected window's buffer current. */
479 Fset_buffer (w->contents);
481 if (EQ (window, selected_window) && !inhibit_point_swap)
482 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
483 way to call record_buffer from Elisp, so it's important that we call
484 record_buffer before returning here. */
485 goto record_and_return;
487 sf = SELECTED_FRAME ();
488 if (XFRAME (WINDOW_FRAME (w)) != sf)
490 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
491 /* Use this rather than Fhandle_switch_frame
492 so that FRAME_FOCUS_FRAME is moved appropriately as we
493 move around in the state where a minibuffer in a separate
494 frame is active. */
495 Fselect_frame (WINDOW_FRAME (w), norecord);
496 /* Fselect_frame called us back so we've done all the work already. */
497 eassert (EQ (window, selected_window));
498 return window;
500 else
501 fset_selected_window (sf, window);
503 select_window_1 (window, inhibit_point_swap);
504 bset_last_selected_window (XBUFFER (w->contents), window);
505 windows_or_buffers_changed++;
507 record_and_return:
508 /* record_buffer can run QUIT, so make sure it is run only after we have
509 re-established the invariant between selected_window and selected_frame,
510 otherwise the temporary broken invariant might "escape" (bug#14161). */
511 if (NILP (norecord))
513 w->use_time = ++window_select_count;
514 record_buffer (w->contents);
517 return window;
520 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
521 (not even for redisplay's benefit), and assume that the window's frame is
522 already selected. */
523 static void
524 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
526 /* Store the old selected window's buffer's point in pointm of the old
527 selected window. It belongs to that window, and when the window is
528 not selected, must be in the window. */
529 if (!inhibit_point_swap)
531 struct window *ow = XWINDOW (selected_window);
532 if (BUFFERP (ow->contents))
533 set_marker_both (ow->pointm, ow->contents,
534 BUF_PT (XBUFFER (ow->contents)),
535 BUF_PT_BYTE (XBUFFER (ow->contents)));
538 selected_window = window;
540 /* Go to the point recorded in the window.
541 This is important when the buffer is in more
542 than one window. It also matters when
543 redisplay_window has altered point after scrolling,
544 because it makes the change only in the window. */
545 set_point_from_marker (XWINDOW (window)->pointm);
548 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
549 doc: /* Select WINDOW which must be a live window.
550 Also make WINDOW's frame the selected frame and WINDOW that frame's
551 selected window. In addition, make WINDOW's buffer current and set that
552 buffer's value of `point' to the value of WINDOW's `window-point'.
553 Return WINDOW.
555 Optional second arg NORECORD non-nil means do not put this buffer at the
556 front of the buffer list and do not make this window the most recently
557 selected one.
559 Note that the main editor command loop sets the current buffer to the
560 buffer of the selected window before each command. */)
561 (register Lisp_Object window, Lisp_Object norecord)
563 return select_window (window, norecord, 0);
566 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
567 doc: /* Return the buffer displayed in window WINDOW.
568 If WINDOW is omitted or nil, it defaults to the selected window.
569 Return nil for an internal window or a deleted window. */)
570 (Lisp_Object window)
572 struct window *w = decode_any_window (window);
573 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
576 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
577 doc: /* Return the parent window of window WINDOW.
578 WINDOW must be a valid window and defaults to the selected one.
579 Return nil for a window with no parent (e.g. a root window). */)
580 (Lisp_Object window)
582 return decode_valid_window (window)->parent;
585 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
586 doc: /* Return the topmost child window of window WINDOW.
587 WINDOW must be a valid window and defaults to the selected one.
588 Return nil if WINDOW is a live window (live windows have no children).
589 Return nil if WINDOW is an internal window whose children form a
590 horizontal combination. */)
591 (Lisp_Object window)
593 struct window *w = decode_valid_window (window);
594 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
597 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
598 doc: /* Return the leftmost child window of window WINDOW.
599 WINDOW must be a valid window and defaults to the selected one.
600 Return nil if WINDOW is a live window (live windows have no children).
601 Return nil if WINDOW is an internal window whose children form a
602 vertical combination. */)
603 (Lisp_Object window)
605 struct window *w = decode_valid_window (window);
606 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
609 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
610 doc: /* Return the next sibling window of window WINDOW.
611 WINDOW must be a valid window and defaults to the selected one.
612 Return nil if WINDOW has no next sibling. */)
613 (Lisp_Object window)
615 return decode_valid_window (window)->next;
618 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
619 doc: /* Return the previous sibling window of window WINDOW.
620 WINDOW must be a valid window and defaults to the selected one.
621 Return nil if WINDOW has no previous sibling. */)
622 (Lisp_Object window)
624 return decode_valid_window (window)->prev;
627 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
628 doc: /* Return combination limit of window WINDOW.
629 WINDOW must be a valid window used in horizontal or vertical combination.
630 If the return value is nil, child windows of WINDOW can be recombined with
631 WINDOW's siblings. A return value of t means that child windows of
632 WINDOW are never \(re-)combined with WINDOW's siblings. */)
633 (Lisp_Object window)
635 struct window *w;
637 CHECK_VALID_WINDOW (window);
638 w = XWINDOW (window);
639 if (WINDOW_LEAF_P (w))
640 error ("Combination limit is meaningful for internal windows only");
641 return w->combination_limit;
644 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
645 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
646 WINDOW must be a valid window used in horizontal or vertical combination.
647 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
648 siblings. LIMIT t means that child windows of WINDOW are never
649 \(re-)combined with WINDOW's siblings. Other values are reserved for
650 future use. */)
651 (Lisp_Object window, Lisp_Object limit)
653 struct window *w;
655 CHECK_VALID_WINDOW (window);
656 w = XWINDOW (window);
657 if (WINDOW_LEAF_P (w))
658 error ("Combination limit is meaningful for internal windows only");
659 wset_combination_limit (w, limit);
660 return limit;
663 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
664 doc: /* Return the use time of window WINDOW.
665 WINDOW must be a live window and defaults to the selected one.
666 The window with the highest use time is the most recently selected
667 one. The window with the lowest use time is the least recently
668 selected one. */)
669 (Lisp_Object window)
671 return make_number (decode_live_window (window)->use_time);
674 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
675 doc: /* Return the total height, in lines, of window WINDOW.
676 WINDOW must be a valid window and defaults to the selected one.
678 The return value includes the mode line and header line, if any.
679 If WINDOW is an internal window, the total height is the height
680 of the screen areas spanned by its children.
682 On a graphical display, this total height is reported as an
683 integer multiple of the default character height. */)
684 (Lisp_Object window)
686 return make_number (decode_valid_window (window)->total_lines);
689 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
690 doc: /* Return the total width, in columns, of window WINDOW.
691 WINDOW must be a valid window and defaults to the selected one.
693 The return value includes any vertical dividers or scroll bars
694 belonging to WINDOW. If WINDOW is an internal window, the total width
695 is the width of the screen areas spanned by its children.
697 On a graphical display, this total width is reported as an
698 integer multiple of the default character width. */)
699 (Lisp_Object window)
701 return make_number (decode_valid_window (window)->total_cols);
704 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
705 doc: /* Return the new total size of window WINDOW.
706 WINDOW must be a valid window and defaults to the selected one. */)
707 (Lisp_Object window)
709 return decode_valid_window (window)->new_total;
712 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
713 doc: /* Return the normal height of window WINDOW.
714 WINDOW must be a valid window and defaults to the selected one.
715 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
716 (Lisp_Object window, Lisp_Object horizontal)
718 struct window *w = decode_valid_window (window);
720 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
723 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
724 doc: /* Return new normal size of window WINDOW.
725 WINDOW must be a valid window and defaults to the selected one. */)
726 (Lisp_Object window)
728 return decode_valid_window (window)->new_normal;
731 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
732 doc: /* Return left column of window WINDOW.
733 This is the distance, in columns, between the left edge of WINDOW and
734 the left edge of the frame's window area. For instance, the return
735 value is 0 if there is no window to the left of WINDOW.
737 WINDOW must be a valid window and defaults to the selected one. */)
738 (Lisp_Object window)
740 return make_number (decode_valid_window (window)->left_col);
743 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
744 doc: /* Return top line of window WINDOW.
745 This is the distance, in lines, between the top of WINDOW and the top
746 of the frame's window area. For instance, the return value is 0 if
747 there is no window above WINDOW.
749 WINDOW must be a valid window and defaults to the selected one. */)
750 (Lisp_Object window)
752 return make_number (decode_valid_window (window)->top_line);
755 /* Return the number of lines of W's body. Don't count any mode or
756 header line of W. */
758 static int
759 window_body_lines (struct window *w)
761 int height = w->total_lines;
763 if (!MINI_WINDOW_P (w))
765 if (WINDOW_WANTS_MODELINE_P (w))
766 --height;
767 if (WINDOW_WANTS_HEADER_LINE_P (w))
768 --height;
771 return height;
774 /* Return the number of columns of W's body. Don't count columns
775 occupied by the scroll bar or the vertical bar separating W from its
776 right sibling. On window-systems don't count fringes or display
777 margins either. */
780 window_body_cols (struct window *w)
782 struct frame *f = XFRAME (WINDOW_FRAME (w));
783 int width = w->total_cols;
785 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
786 /* Scroll bars occupy a few columns. */
787 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
788 else if (!FRAME_WINDOW_P (f)
789 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
790 /* The column of `|' characters separating side-by-side windows
791 occupies one column only. */
792 width -= 1;
794 /* Display margins cannot be used for normal text. */
795 width -= WINDOW_LEFT_MARGIN_COLS (w) + WINDOW_RIGHT_MARGIN_COLS (w);
797 if (FRAME_WINDOW_P (f))
798 /* On window-systems, fringes cannot be used for normal text. */
799 width -= WINDOW_FRINGE_COLS (w);
801 return width;
804 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 1, 0,
805 doc: /* Return the height, in lines, of WINDOW's text area.
806 WINDOW must be a live window and defaults to the selected one.
808 The returned height does not include the mode line or header line.
809 On a graphical display, the height is expressed as an integer multiple
810 of the default character height. If a line at the bottom of the text
811 area is only partially visible, that counts as a whole line; to
812 exclude partially-visible lines, use `window-text-height'. */)
813 (Lisp_Object window)
815 return make_number (window_body_lines (decode_live_window (window)));
818 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 1, 0,
819 doc: /* Return the width, in columns, of WINDOW's text area.
820 WINDOW must be a live window and defaults to the selected one.
822 The return value does not include any vertical dividers, fringe or
823 marginal areas, or scroll bars. On a graphical display, the width is
824 expressed as an integer multiple of the default character width. */)
825 (Lisp_Object window)
827 return make_number (window_body_cols (decode_live_window (window)));
830 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
831 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
832 WINDOW must be a live window and defaults to the selected one. */)
833 (Lisp_Object window)
835 return make_number (decode_live_window (window)->hscroll);
838 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
839 range, returning the new amount as a fixnum. */
840 static Lisp_Object
841 set_window_hscroll (struct window *w, EMACS_INT hscroll)
843 /* Horizontal scrolling has problems with large scroll amounts.
844 It's too slow with long lines, and even with small lines the
845 display can be messed up. For now, though, impose only the limits
846 required by the internal representation: horizontal scrolling must
847 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
848 (since it's stored in a ptrdiff_t). */
849 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
850 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
852 /* Prevent redisplay shortcuts when changing the hscroll. */
853 if (w->hscroll != new_hscroll)
854 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
856 w->hscroll = new_hscroll;
857 return make_number (new_hscroll);
860 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
861 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
862 WINDOW must be a live window and defaults to the selected one.
863 Clip the number to a reasonable value if out of range.
864 Return the new number. NCOL should be zero or positive.
866 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
867 window so that the location of point moves off-window. */)
868 (Lisp_Object window, Lisp_Object ncol)
870 CHECK_NUMBER (ncol);
871 return set_window_hscroll (decode_live_window (window), XINT (ncol));
874 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
875 Swindow_redisplay_end_trigger, 0, 1, 0,
876 doc: /* Return WINDOW's redisplay end trigger value.
877 WINDOW must be a live window and defaults to the selected one.
878 See `set-window-redisplay-end-trigger' for more information. */)
879 (Lisp_Object window)
881 return decode_live_window (window)->redisplay_end_trigger;
884 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
885 Sset_window_redisplay_end_trigger, 2, 2, 0,
886 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
887 WINDOW must be a live window and defaults to the selected one. VALUE
888 should be a buffer position (typically a marker) or nil. If it is a
889 buffer position, then if redisplay in WINDOW reaches a position beyond
890 VALUE, the functions in `redisplay-end-trigger-functions' are called
891 with two arguments: WINDOW, and the end trigger value. Afterwards the
892 end-trigger value is reset to nil. */)
893 (register Lisp_Object window, Lisp_Object value)
895 wset_redisplay_end_trigger (decode_live_window (window), value);
896 return value;
899 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
900 doc: /* Return a list of the edge coordinates of WINDOW.
901 WINDOW must be a valid window and defaults to the selected one.
903 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
904 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
905 0 at top left corner of frame.
907 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
908 is one more than the bottommost row occupied by WINDOW. The edges
909 include the space used by WINDOW's scroll bar, display margins, fringes,
910 header line, and/or mode line. For the edges of just the text area, use
911 `window-inside-edges'. */)
912 (Lisp_Object window)
914 register struct window *w = decode_valid_window (window);
916 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
917 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
920 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
921 doc: /* Return a list of the edge pixel coordinates of WINDOW.
922 WINDOW must be a valid window and defaults to the selected one.
924 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
925 0, 0 at the top left corner of the frame.
927 RIGHT is one more than the rightmost x position occupied by WINDOW.
928 BOTTOM is one more than the bottommost y position occupied by WINDOW.
929 The pixel edges include the space used by WINDOW's scroll bar, display
930 margins, fringes, header line, and/or mode line. For the pixel edges
931 of just the text area, use `window-inside-pixel-edges'. */)
932 (Lisp_Object window)
934 register struct window *w = decode_valid_window (window);
936 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
937 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
940 static void
941 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
943 struct frame *f = XFRAME (w->frame);
944 *add_y = f->top_pos;
945 #ifdef FRAME_MENUBAR_HEIGHT
946 *add_y += FRAME_MENUBAR_HEIGHT (f);
947 #endif
948 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
949 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
950 #elif FRAME_TOOLBAR_HEIGHT
951 *add_y += FRAME_TOOLBAR_HEIGHT (f);
952 #endif
953 #ifdef FRAME_NS_TITLEBAR_HEIGHT
954 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
955 #endif
956 *add_x = f->left_pos;
957 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
958 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
959 #endif
962 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
963 Swindow_absolute_pixel_edges, 0, 1, 0,
964 doc: /* Return a list of the edge pixel coordinates of WINDOW.
965 WINDOW must be a valid window and defaults to the selected one.
967 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
968 0, 0 at the top left corner of the display.
970 RIGHT is one more than the rightmost x position occupied by WINDOW.
971 BOTTOM is one more than the bottommost y position occupied by WINDOW.
972 The pixel edges include the space used by WINDOW's scroll bar, display
973 margins, fringes, header line, and/or mode line. For the pixel edges
974 of just the text area, use `window-inside-absolute-pixel-edges'. */)
975 (Lisp_Object window)
977 register struct window *w = decode_valid_window (window);
978 int add_x, add_y;
980 calc_absolute_offset (w, &add_x, &add_y);
982 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
983 WINDOW_TOP_EDGE_Y (w) + add_y,
984 WINDOW_RIGHT_EDGE_X (w) + add_x,
985 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
988 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
989 doc: /* Return a list of the edge coordinates of WINDOW.
990 WINDOW must be a live window and defaults to the selected one.
992 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
993 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
994 0 at top left corner of frame.
996 RIGHT is one more than the rightmost column of WINDOW's text area.
997 BOTTOM is one more than the bottommost row of WINDOW's text area. The
998 inside edges do not include the space used by the WINDOW's scroll bar,
999 display margins, fringes, header line, and/or mode line. */)
1000 (Lisp_Object window)
1002 register struct window *w = decode_live_window (window);
1004 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1005 + WINDOW_LEFT_MARGIN_COLS (w)
1006 + WINDOW_LEFT_FRINGE_COLS (w)),
1007 (WINDOW_TOP_EDGE_LINE (w)
1008 + WINDOW_HEADER_LINE_LINES (w)),
1009 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1010 - WINDOW_RIGHT_MARGIN_COLS (w)
1011 - WINDOW_RIGHT_FRINGE_COLS (w)),
1012 (WINDOW_BOTTOM_EDGE_LINE (w)
1013 - WINDOW_MODE_LINE_LINES (w)));
1016 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1017 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1018 WINDOW must be a live window and defaults to the selected one.
1020 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1021 (0,0) at the top left corner of the frame's window area.
1023 RIGHT is one more than the rightmost x position of WINDOW's text area.
1024 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1025 The inside edges do not include the space used by WINDOW's scroll bar,
1026 display margins, fringes, header line, and/or mode line. */)
1027 (Lisp_Object window)
1029 register struct window *w = decode_live_window (window);
1031 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1032 + WINDOW_LEFT_MARGIN_WIDTH (w)
1033 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1034 (WINDOW_TOP_EDGE_Y (w)
1035 + WINDOW_HEADER_LINE_HEIGHT (w)),
1036 (WINDOW_BOX_RIGHT_EDGE_X (w)
1037 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1038 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1039 (WINDOW_BOTTOM_EDGE_Y (w)
1040 - WINDOW_MODE_LINE_HEIGHT (w)));
1043 DEFUN ("window-inside-absolute-pixel-edges",
1044 Fwindow_inside_absolute_pixel_edges,
1045 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1046 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1047 WINDOW must be a live window and defaults to the selected one.
1049 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1050 (0,0) at the top left corner of the frame's window area.
1052 RIGHT is one more than the rightmost x position of WINDOW's text area.
1053 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1054 The inside edges do not include the space used by WINDOW's scroll bar,
1055 display margins, fringes, header line, and/or mode line. */)
1056 (Lisp_Object window)
1058 register struct window *w = decode_live_window (window);
1059 int add_x, add_y;
1061 calc_absolute_offset (w, &add_x, &add_y);
1063 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1064 + WINDOW_LEFT_MARGIN_WIDTH (w)
1065 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1066 (WINDOW_TOP_EDGE_Y (w)
1067 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1068 (WINDOW_BOX_RIGHT_EDGE_X (w)
1069 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1070 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1071 (WINDOW_BOTTOM_EDGE_Y (w)
1072 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1075 /* Test if the character at column X, row Y is within window W.
1076 If it is not, return ON_NOTHING;
1077 if it is in the window's text area, return ON_TEXT;
1078 if it is on the window's modeline, return ON_MODE_LINE;
1079 if it is on the border between the window and its right sibling,
1080 return ON_VERTICAL_BORDER.
1081 if it is on a scroll bar, return ON_SCROLL_BAR.
1082 if it is on the window's top line, return ON_HEADER_LINE;
1083 if it is in left or right fringe of the window,
1084 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1085 if it is in the marginal area to the left/right of the window,
1086 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1088 X and Y are frame relative pixel coordinates. */
1090 static enum window_part
1091 coordinates_in_window (register struct window *w, int x, int y)
1093 struct frame *f = XFRAME (WINDOW_FRAME (w));
1094 enum window_part part;
1095 int ux = FRAME_COLUMN_WIDTH (f);
1096 int left_x = WINDOW_LEFT_EDGE_X (w);
1097 int right_x = WINDOW_RIGHT_EDGE_X (w);
1098 int top_y = WINDOW_TOP_EDGE_Y (w);
1099 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1100 /* The width of the area where the vertical line can be dragged.
1101 (Between mode lines for instance. */
1102 int grabbable_width = ux;
1103 int lmargin_width, rmargin_width, text_left, text_right;
1105 /* Outside any interesting row or column? */
1106 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1107 return ON_NOTHING;
1109 /* On the mode line or header line? */
1110 if ((WINDOW_WANTS_MODELINE_P (w)
1111 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
1112 && (part = ON_MODE_LINE))
1113 || (WINDOW_WANTS_HEADER_LINE_P (w)
1114 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1115 && (part = ON_HEADER_LINE)))
1117 /* If it's under/over the scroll bar portion of the mode/header
1118 line, say it's on the vertical line. That's to be able to
1119 resize windows horizontally in case we're using toolkit scroll
1120 bars. Note: If scrollbars are on the left, the window that
1121 must be eventually resized is that on the left of WINDOW. */
1122 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1123 && !WINDOW_LEFTMOST_P (w)
1124 && eabs (x - left_x) < grabbable_width)
1125 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1126 && !WINDOW_RIGHTMOST_P (w)
1127 && eabs (x - right_x) < grabbable_width))
1128 return ON_VERTICAL_BORDER;
1129 else
1130 return part;
1133 /* In what's below, we subtract 1 when computing right_x because we
1134 want the rightmost pixel, which is given by left_pixel+width-1. */
1135 if (w->pseudo_window_p)
1137 left_x = 0;
1138 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
1140 else
1142 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1143 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1146 /* Outside any interesting column? */
1147 if (x < left_x || x > right_x)
1148 return ON_SCROLL_BAR;
1150 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1151 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1153 text_left = window_box_left (w, TEXT_AREA);
1154 text_right = text_left + window_box_width (w, TEXT_AREA);
1156 if (FRAME_WINDOW_P (f))
1158 if (!w->pseudo_window_p
1159 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1160 && !WINDOW_RIGHTMOST_P (w)
1161 && (eabs (x - right_x) < grabbable_width))
1162 return ON_VERTICAL_BORDER;
1164 /* Need to say "x > right_x" rather than >=, since on character
1165 terminals, the vertical line's x coordinate is right_x. */
1166 else if (!w->pseudo_window_p
1167 && !WINDOW_RIGHTMOST_P (w)
1168 && x > right_x - ux)
1169 return ON_VERTICAL_BORDER;
1171 if (x < text_left)
1173 if (lmargin_width > 0
1174 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1175 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1176 : (x < left_x + lmargin_width)))
1177 return ON_LEFT_MARGIN;
1179 return ON_LEFT_FRINGE;
1182 if (x >= text_right)
1184 if (rmargin_width > 0
1185 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1186 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1187 : (x >= right_x - rmargin_width)))
1188 return ON_RIGHT_MARGIN;
1190 return ON_RIGHT_FRINGE;
1193 /* Everything special ruled out - must be on text area */
1194 return ON_TEXT;
1197 /* Take X is the frame-relative pixel x-coordinate, and return the
1198 x-coordinate relative to part PART of window W. */
1200 window_relative_x_coord (struct window *w, enum window_part part, int x)
1202 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1204 switch (part)
1206 case ON_TEXT:
1207 return x - window_box_left (w, TEXT_AREA);
1209 case ON_LEFT_FRINGE:
1210 return x - left_x;
1212 case ON_RIGHT_FRINGE:
1213 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1215 case ON_LEFT_MARGIN:
1216 return (x - left_x
1217 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1218 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1220 case ON_RIGHT_MARGIN:
1221 return (x + 1
1222 - ((w->pseudo_window_p)
1223 ? WINDOW_TOTAL_WIDTH (w)
1224 : WINDOW_BOX_RIGHT_EDGE_X (w))
1225 + window_box_width (w, RIGHT_MARGIN_AREA)
1226 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1227 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1230 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1231 return 0;
1235 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1236 Scoordinates_in_window_p, 2, 2, 0,
1237 doc: /* Return non-nil if COORDINATES are in WINDOW.
1238 WINDOW must be a live window and defaults to the selected one.
1239 COORDINATES is a cons of the form (X . Y), X and Y being distances
1240 measured in characters from the upper-left corner of the frame.
1241 \(0 . 0) denotes the character in the upper left corner of the
1242 frame.
1243 If COORDINATES are in the text portion of WINDOW,
1244 the coordinates relative to the window are returned.
1245 If they are in the mode line of WINDOW, `mode-line' is returned.
1246 If they are in the top mode line of WINDOW, `header-line' is returned.
1247 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1248 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1249 If they are on the border between WINDOW and its right sibling,
1250 `vertical-line' is returned.
1251 If they are in the windows's left or right marginal areas, `left-margin'\n\
1252 or `right-margin' is returned. */)
1253 (register Lisp_Object coordinates, Lisp_Object window)
1255 struct window *w;
1256 struct frame *f;
1257 int x, y;
1258 Lisp_Object lx, ly;
1260 w = decode_live_window (window);
1261 f = XFRAME (w->frame);
1262 CHECK_CONS (coordinates);
1263 lx = Fcar (coordinates);
1264 ly = Fcdr (coordinates);
1265 CHECK_NUMBER_OR_FLOAT (lx);
1266 CHECK_NUMBER_OR_FLOAT (ly);
1267 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1268 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1270 switch (coordinates_in_window (w, x, y))
1272 case ON_NOTHING:
1273 return Qnil;
1275 case ON_TEXT:
1276 /* Convert X and Y to window relative pixel coordinates, and
1277 return the canonical char units. */
1278 x -= window_box_left (w, TEXT_AREA);
1279 y -= WINDOW_TOP_EDGE_Y (w);
1280 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1281 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1283 case ON_MODE_LINE:
1284 return Qmode_line;
1286 case ON_VERTICAL_BORDER:
1287 return Qvertical_line;
1289 case ON_HEADER_LINE:
1290 return Qheader_line;
1292 case ON_LEFT_FRINGE:
1293 return Qleft_fringe;
1295 case ON_RIGHT_FRINGE:
1296 return Qright_fringe;
1298 case ON_LEFT_MARGIN:
1299 return Qleft_margin;
1301 case ON_RIGHT_MARGIN:
1302 return Qright_margin;
1304 case ON_SCROLL_BAR:
1305 /* Historically we are supposed to return nil in this case. */
1306 return Qnil;
1308 default:
1309 emacs_abort ();
1314 /* Callback for foreach_window, used in window_from_coordinates.
1315 Check if window W contains coordinates specified by USER_DATA which
1316 is actually a pointer to a struct check_window_data CW.
1318 Check if window W contains coordinates *CW->x and *CW->y. If it
1319 does, return W in *CW->window, as Lisp_Object, and return in
1320 *CW->part the part of the window under coordinates *X,*Y. Return
1321 zero from this function to stop iterating over windows. */
1323 struct check_window_data
1325 Lisp_Object *window;
1326 int x, y;
1327 enum window_part *part;
1330 static int
1331 check_window_containing (struct window *w, void *user_data)
1333 struct check_window_data *cw = user_data;
1334 enum window_part found;
1335 int continue_p = 1;
1337 found = coordinates_in_window (w, cw->x, cw->y);
1338 if (found != ON_NOTHING)
1340 *cw->part = found;
1341 XSETWINDOW (*cw->window, w);
1342 continue_p = 0;
1345 return continue_p;
1349 /* Find the window containing frame-relative pixel position X/Y and
1350 return it as a Lisp_Object.
1352 If X, Y is on one of the window's special `window_part' elements,
1353 set *PART to the id of that element.
1355 If there is no window under X, Y return nil and leave *PART
1356 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1358 This function was previously implemented with a loop cycling over
1359 windows with Fnext_window, and starting with the frame's selected
1360 window. It turned out that this doesn't work with an
1361 implementation of next_window using Vwindow_list, because
1362 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1363 tree of F when this function is called asynchronously from
1364 note_mouse_highlight. The original loop didn't terminate in this
1365 case. */
1367 Lisp_Object
1368 window_from_coordinates (struct frame *f, int x, int y,
1369 enum window_part *part, bool tool_bar_p)
1371 Lisp_Object window;
1372 struct check_window_data cw;
1373 enum window_part dummy;
1375 if (part == 0)
1376 part = &dummy;
1378 window = Qnil;
1379 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1380 foreach_window (f, check_window_containing, &cw);
1382 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1383 /* If not found above, see if it's in the tool bar window, if a tool
1384 bar exists. */
1385 if (NILP (window)
1386 && tool_bar_p
1387 && WINDOWP (f->tool_bar_window)
1388 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1389 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1390 != ON_NOTHING))
1392 *part = ON_TEXT;
1393 window = f->tool_bar_window;
1395 #endif
1397 return window;
1400 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1401 doc: /* Return window containing coordinates X and Y on FRAME.
1402 FRAME must be a live frame and defaults to the selected one.
1403 The top left corner of the frame is considered to be row 0,
1404 column 0. */)
1405 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1407 struct frame *f = decode_live_frame (frame);
1409 /* Check that arguments are integers or floats. */
1410 CHECK_NUMBER_OR_FLOAT (x);
1411 CHECK_NUMBER_OR_FLOAT (y);
1413 return window_from_coordinates (f,
1414 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1415 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1416 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1417 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1418 0, 0);
1421 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1422 doc: /* Return current value of point in WINDOW.
1423 WINDOW must be a live window and defaults to the selected one.
1425 For a nonselected window, this is the value point would have if that
1426 window were selected.
1428 Note that, when WINDOW is selected, the value returned is the same as
1429 that returned by `point' for WINDOW's buffer. It would be more strictly
1430 correct to return the top-level value of `point', outside of any
1431 `save-excursion' forms. But that is hard to define. */)
1432 (Lisp_Object window)
1434 register struct window *w = decode_live_window (window);
1436 if (w == XWINDOW (selected_window))
1437 return make_number (BUF_PT (XBUFFER (w->contents)));
1438 else
1439 return Fmarker_position (w->pointm);
1442 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1443 doc: /* Return position at which display currently starts in WINDOW.
1444 WINDOW must be a live window and defaults to the selected one.
1445 This is updated by redisplay or by calling `set-window-start'. */)
1446 (Lisp_Object window)
1448 return Fmarker_position (decode_live_window (window)->start);
1451 /* This is text temporarily removed from the doc string below.
1453 This function returns nil if the position is not currently known.
1454 That happens when redisplay is preempted and doesn't finish.
1455 If in that case you want to compute where the end of the window would
1456 have been if redisplay had finished, do this:
1457 (save-excursion
1458 (goto-char (window-start window))
1459 (vertical-motion (1- (window-height window)) window)
1460 (point))") */
1462 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1463 doc: /* Return position at which display currently ends in WINDOW.
1464 WINDOW must be a live window and defaults to the selected one.
1465 This is updated by redisplay, when it runs to completion.
1466 Simply changing the buffer text or setting `window-start'
1467 does not update this value.
1468 Return nil if there is no recorded value. (This can happen if the
1469 last redisplay of WINDOW was preempted, and did not finish.)
1470 If UPDATE is non-nil, compute the up-to-date position
1471 if it isn't already recorded. */)
1472 (Lisp_Object window, Lisp_Object update)
1474 Lisp_Object value;
1475 struct window *w = decode_live_window (window);
1476 Lisp_Object buf;
1477 struct buffer *b;
1479 buf = w->contents;
1480 CHECK_BUFFER (buf);
1481 b = XBUFFER (buf);
1483 if (! NILP (update)
1484 && (windows_or_buffers_changed
1485 || !w->window_end_valid
1486 || b->clip_changed
1487 || b->prevent_redisplay_optimizations_p
1488 || window_outdated (w))
1489 && !noninteractive)
1491 struct text_pos startp;
1492 struct it it;
1493 struct buffer *old_buffer = NULL;
1494 void *itdata = NULL;
1496 /* Cannot use Fvertical_motion because that function doesn't
1497 cope with variable-height lines. */
1498 if (b != current_buffer)
1500 old_buffer = current_buffer;
1501 set_buffer_internal (b);
1504 /* In case W->start is out of the range, use something
1505 reasonable. This situation occurred when loading a file with
1506 `-l' containing a call to `rmail' with subsequent other
1507 commands. At the end, W->start happened to be BEG, while
1508 rmail had already narrowed the buffer. */
1509 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1511 itdata = bidi_shelve_cache ();
1512 start_display (&it, w, startp);
1513 move_it_vertically (&it, window_box_height (w));
1514 if (it.current_y < it.last_visible_y)
1515 move_it_past_eol (&it);
1516 value = make_number (IT_CHARPOS (it));
1517 bidi_unshelve_cache (itdata, 0);
1519 if (old_buffer)
1520 set_buffer_internal (old_buffer);
1522 else
1523 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1525 return value;
1528 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1529 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1530 WINDOW must be a live window and defaults to the selected one.
1531 Return POS. */)
1532 (Lisp_Object window, Lisp_Object pos)
1534 register struct window *w = decode_live_window (window);
1536 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1538 if (w == XWINDOW (selected_window))
1540 if (XBUFFER (w->contents) == current_buffer)
1541 Fgoto_char (pos);
1542 else
1544 struct buffer *old_buffer = current_buffer;
1546 /* ... but here we want to catch type error before buffer change. */
1547 CHECK_NUMBER_COERCE_MARKER (pos);
1548 set_buffer_internal (XBUFFER (w->contents));
1549 Fgoto_char (pos);
1550 set_buffer_internal (old_buffer);
1553 else
1555 set_marker_restricted (w->pointm, pos, w->contents);
1556 /* We have to make sure that redisplay updates the window to show
1557 the new value of point. */
1558 ++windows_or_buffers_changed;
1561 return pos;
1564 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1565 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1566 WINDOW must be a live window and defaults to the selected one. Return
1567 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1568 overriding motion of point in order to display at this exact start. */)
1569 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1571 register struct window *w = decode_live_window (window);
1573 set_marker_restricted (w->start, pos, w->contents);
1574 /* This is not right, but much easier than doing what is right. */
1575 w->start_at_line_beg = 0;
1576 if (NILP (noforce))
1577 w->force_start = 1;
1578 w->update_mode_line = 1;
1579 if (w != XWINDOW (selected_window))
1580 /* Enforce full redisplay. FIXME: make it more selective. */
1581 windows_or_buffers_changed++;
1583 return pos;
1586 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1587 Spos_visible_in_window_p, 0, 3, 0,
1588 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1589 WINDOW must be a live window and defaults to the selected one.
1591 Return nil if that position is scrolled vertically out of view. If a
1592 character is only partially visible, nil is returned, unless the
1593 optional argument PARTIALLY is non-nil. If POS is only out of view
1594 because of horizontal scrolling, return non-nil. If POS is t, it
1595 specifies the position of the last visible glyph in WINDOW. POS
1596 defaults to point in WINDOW; WINDOW defaults to the selected window.
1598 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1599 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1600 where X and Y are the pixel coordinates relative to the top left corner
1601 of the window. The remaining elements are omitted if the character after
1602 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1603 off-window at the top and bottom of the screen line ("row") containing
1604 POS, ROWH is the visible height of that row, and VPOS is the row number
1605 \(zero-based). */)
1606 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1608 register struct window *w;
1609 register EMACS_INT posint;
1610 register struct buffer *buf;
1611 struct text_pos top;
1612 Lisp_Object in_window = Qnil;
1613 int rtop, rbot, rowh, vpos, fully_p = 1;
1614 int x, y;
1616 w = decode_live_window (window);
1617 buf = XBUFFER (w->contents);
1618 SET_TEXT_POS_FROM_MARKER (top, w->start);
1620 if (EQ (pos, Qt))
1621 posint = -1;
1622 else if (!NILP (pos))
1624 CHECK_NUMBER_COERCE_MARKER (pos);
1625 posint = XINT (pos);
1627 else if (w == XWINDOW (selected_window))
1628 posint = PT;
1629 else
1630 posint = marker_position (w->pointm);
1632 /* If position is above window start or outside buffer boundaries,
1633 or if window start is out of range, position is not visible. */
1634 if ((EQ (pos, Qt)
1635 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1636 && CHARPOS (top) >= BUF_BEGV (buf)
1637 && CHARPOS (top) <= BUF_ZV (buf)
1638 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1639 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1640 in_window = Qt;
1642 if (!NILP (in_window) && !NILP (partially))
1644 Lisp_Object part = Qnil;
1645 if (!fully_p)
1646 part = list4i (rtop, rbot, rowh, vpos);
1647 in_window = Fcons (make_number (x),
1648 Fcons (make_number (y), part));
1651 return in_window;
1654 DEFUN ("window-line-height", Fwindow_line_height,
1655 Swindow_line_height, 0, 2, 0,
1656 doc: /* Return height in pixels of text line LINE in window WINDOW.
1657 WINDOW must be a live window and defaults to the selected one.
1659 Return height of current line if LINE is omitted or nil. Return height of
1660 header or mode line if LINE is `header-line' or `mode-line'.
1661 Otherwise, LINE is a text line number starting from 0. A negative number
1662 counts from the end of the window.
1664 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1665 in pixels of the visible part of the line, VPOS and YPOS are the
1666 vertical position in lines and pixels of the line, relative to the top
1667 of the first text line, and OFFBOT is the number of off-window pixels at
1668 the bottom of the text line. If there are off-window pixels at the top
1669 of the (first) text line, YPOS is negative.
1671 Return nil if window display is not up-to-date. In that case, use
1672 `pos-visible-in-window-p' to obtain the information. */)
1673 (Lisp_Object line, Lisp_Object window)
1675 register struct window *w;
1676 register struct buffer *b;
1677 struct glyph_row *row, *end_row;
1678 int max_y, crop, i;
1679 EMACS_INT n;
1681 w = decode_live_window (window);
1683 if (noninteractive || w->pseudo_window_p)
1684 return Qnil;
1686 CHECK_BUFFER (w->contents);
1687 b = XBUFFER (w->contents);
1689 /* Fail if current matrix is not up-to-date. */
1690 if (!w->window_end_valid
1691 || windows_or_buffers_changed
1692 || b->clip_changed
1693 || b->prevent_redisplay_optimizations_p
1694 || window_outdated (w))
1695 return Qnil;
1697 if (NILP (line))
1699 i = w->cursor.vpos;
1700 if (i < 0 || i >= w->current_matrix->nrows
1701 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1702 return Qnil;
1703 max_y = window_text_bottom_y (w);
1704 goto found_row;
1707 if (EQ (line, Qheader_line))
1709 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1710 return Qnil;
1711 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1712 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1715 if (EQ (line, Qmode_line))
1717 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1718 return (row->enabled_p ?
1719 list4i (row->height,
1720 0, /* not accurate */
1721 (WINDOW_HEADER_LINE_HEIGHT (w)
1722 + window_text_bottom_y (w)),
1724 : Qnil);
1727 CHECK_NUMBER (line);
1728 n = XINT (line);
1730 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1731 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1732 max_y = window_text_bottom_y (w);
1733 i = 0;
1735 while ((n < 0 || i < n)
1736 && row <= end_row && row->enabled_p
1737 && row->y + row->height < max_y)
1738 row++, i++;
1740 if (row > end_row || !row->enabled_p)
1741 return Qnil;
1743 if (++n < 0)
1745 if (-n > i)
1746 return Qnil;
1747 row += n;
1748 i += n;
1751 found_row:
1752 crop = max (0, (row->y + row->height) - max_y);
1753 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1756 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1757 0, 1, 0,
1758 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1759 More precisely, return the value assigned by the last call of
1760 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1761 never called with WINDOW as its argument, or the value set by that
1762 function was internally reset since its last call. WINDOW must be a
1763 live window and defaults to the selected one.
1765 When a window is dedicated to its buffer, `display-buffer' will refrain
1766 from displaying another buffer in it. `get-lru-window' and
1767 `get-largest-window' treat dedicated windows specially.
1768 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1769 `kill-buffer' can delete a dedicated window and the containing frame.
1771 Functions like `set-window-buffer' may change the buffer displayed by a
1772 window, unless that window is "strongly" dedicated to its buffer, that
1773 is the value returned by `window-dedicated-p' is t. */)
1774 (Lisp_Object window)
1776 return decode_live_window (window)->dedicated;
1779 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1780 Sset_window_dedicated_p, 2, 2, 0,
1781 doc: /* Mark WINDOW as dedicated according to FLAG.
1782 WINDOW must be a live window and defaults to the selected one. FLAG
1783 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1784 mark WINDOW as non-dedicated. Return FLAG.
1786 When a window is dedicated to its buffer, `display-buffer' will refrain
1787 from displaying another buffer in it. `get-lru-window' and
1788 `get-largest-window' treat dedicated windows specially.
1789 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1790 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1791 and the containing frame.
1793 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1794 its buffer. Functions like `set-window-buffer' may change the buffer
1795 displayed by a window, unless that window is strongly dedicated to its
1796 buffer. If and when `set-window-buffer' displays another buffer in a
1797 window, it also makes sure that the window is no more dedicated. */)
1798 (Lisp_Object window, Lisp_Object flag)
1800 wset_dedicated (decode_live_window (window), flag);
1801 return flag;
1804 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1805 0, 1, 0,
1806 doc: /* Return buffers previously shown in WINDOW.
1807 WINDOW must be a live window and defaults to the selected one.
1809 The return value is a list of elements (BUFFER WINDOW-START POS),
1810 where BUFFER is a buffer, WINDOW-START is the start position of the
1811 window for that buffer, and POS is a window-specific point value. */)
1812 (Lisp_Object window)
1814 return decode_live_window (window)->prev_buffers;
1817 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1818 Sset_window_prev_buffers, 2, 2, 0,
1819 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1820 WINDOW must be a live window and defaults to the selected one.
1822 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1823 where BUFFER is a buffer, WINDOW-START is the start position of the
1824 window for that buffer, and POS is a window-specific point value. */)
1825 (Lisp_Object window, Lisp_Object prev_buffers)
1827 wset_prev_buffers (decode_live_window (window), prev_buffers);
1828 return prev_buffers;
1831 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1832 0, 1, 0,
1833 doc: /* Return list of buffers recently re-shown in WINDOW.
1834 WINDOW must be a live window and defaults to the selected one. */)
1835 (Lisp_Object window)
1837 return decode_live_window (window)->next_buffers;
1840 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1841 Sset_window_next_buffers, 2, 2, 0,
1842 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1843 WINDOW must be a live window and defaults to the selected one.
1844 NEXT-BUFFERS should be a list of buffers. */)
1845 (Lisp_Object window, Lisp_Object next_buffers)
1847 wset_next_buffers (decode_live_window (window), next_buffers);
1848 return next_buffers;
1851 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1852 0, 1, 0,
1853 doc: /* Return the parameters of WINDOW and their values.
1854 WINDOW must be a valid window and defaults to the selected one. The
1855 return value is a list of elements of the form (PARAMETER . VALUE). */)
1856 (Lisp_Object window)
1858 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1861 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1862 2, 2, 0,
1863 doc: /* Return WINDOW's value for PARAMETER.
1864 WINDOW can be any window and defaults to the selected one. */)
1865 (Lisp_Object window, Lisp_Object parameter)
1867 Lisp_Object result;
1869 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1870 return CDR_SAFE (result);
1873 DEFUN ("set-window-parameter", Fset_window_parameter,
1874 Sset_window_parameter, 3, 3, 0,
1875 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1876 WINDOW can be any window and defaults to the selected one.
1877 Return VALUE. */)
1878 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1880 register struct window *w = decode_any_window (window);
1881 Lisp_Object old_alist_elt;
1883 old_alist_elt = Fassq (parameter, w->window_parameters);
1884 if (NILP (old_alist_elt))
1885 wset_window_parameters
1886 (w, Fcons (Fcons (parameter, value), w->window_parameters));
1887 else
1888 Fsetcdr (old_alist_elt, value);
1889 return value;
1892 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1893 0, 1, 0,
1894 doc: /* Return the display-table that WINDOW is using.
1895 WINDOW must be a live window and defaults to the selected one. */)
1896 (Lisp_Object window)
1898 return decode_live_window (window)->display_table;
1901 /* Get the display table for use on window W. This is either W's
1902 display table or W's buffer's display table. Ignore the specified
1903 tables if they are not valid; if no valid table is specified,
1904 return 0. */
1906 struct Lisp_Char_Table *
1907 window_display_table (struct window *w)
1909 struct Lisp_Char_Table *dp = NULL;
1911 if (DISP_TABLE_P (w->display_table))
1912 dp = XCHAR_TABLE (w->display_table);
1913 else if (BUFFERP (w->contents))
1915 struct buffer *b = XBUFFER (w->contents);
1917 if (DISP_TABLE_P (BVAR (b, display_table)))
1918 dp = XCHAR_TABLE (BVAR (b, display_table));
1919 else if (DISP_TABLE_P (Vstandard_display_table))
1920 dp = XCHAR_TABLE (Vstandard_display_table);
1923 return dp;
1926 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1927 doc: /* Set WINDOW's display-table to TABLE.
1928 WINDOW must be a live window and defaults to the selected one. */)
1929 (register Lisp_Object window, Lisp_Object table)
1931 wset_display_table (decode_live_window (window), table);
1932 return table;
1935 /* Record info on buffer window W is displaying
1936 when it is about to cease to display that buffer. */
1937 static void
1938 unshow_buffer (register struct window *w)
1940 Lisp_Object buf = w->contents;
1941 struct buffer *b = XBUFFER (buf);
1943 eassert (b == XMARKER (w->pointm)->buffer);
1945 #if 0
1946 if (w == XWINDOW (selected_window)
1947 || ! EQ (buf, XWINDOW (selected_window)->contents))
1948 /* Do this except when the selected window's buffer
1949 is being removed from some other window. */
1950 #endif
1951 /* last_window_start records the start position that this buffer
1952 had in the last window to be disconnected from it.
1953 Now that this statement is unconditional,
1954 it is possible for the buffer to be displayed in the
1955 selected window, while last_window_start reflects another
1956 window which was recently showing the same buffer.
1957 Some people might say that might be a good thing. Let's see. */
1958 b->last_window_start = marker_position (w->start);
1960 /* Point in the selected window's buffer
1961 is actually stored in that buffer, and the window's pointm isn't used.
1962 So don't clobber point in that buffer. */
1963 if (! EQ (buf, XWINDOW (selected_window)->contents)
1964 /* Don't clobber point in current buffer either (this could be
1965 useful in connection with bug#12208).
1966 && XBUFFER (buf) != current_buffer */
1967 /* This line helps to fix Horsley's testbug.el bug. */
1968 && !(WINDOWP (BVAR (b, last_selected_window))
1969 && w != XWINDOW (BVAR (b, last_selected_window))
1970 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
1971 temp_set_point_both (b,
1972 clip_to_bounds (BUF_BEGV (b),
1973 marker_position (w->pointm),
1974 BUF_ZV (b)),
1975 clip_to_bounds (BUF_BEGV_BYTE (b),
1976 marker_byte_position (w->pointm),
1977 BUF_ZV_BYTE (b)));
1979 if (WINDOWP (BVAR (b, last_selected_window))
1980 && w == XWINDOW (BVAR (b, last_selected_window)))
1981 bset_last_selected_window (b, Qnil);
1984 /* Put NEW into the window structure in place of OLD. SETFLAG zero
1985 means change window structure only. Otherwise store geometry and
1986 other settings as well. */
1987 static void
1988 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
1990 register Lisp_Object tem;
1991 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
1993 /* If OLD is its frame's root window, then NEW is the new
1994 root window for that frame. */
1995 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1996 fset_root_window (XFRAME (o->frame), new);
1998 if (setflag)
2000 n->left_col = o->left_col;
2001 n->top_line = o->top_line;
2002 n->total_cols = o->total_cols;
2003 n->total_lines = o->total_lines;
2004 wset_normal_cols (n, o->normal_cols);
2005 wset_normal_cols (o, make_float (1.0));
2006 wset_normal_lines (n, o->normal_lines);
2007 wset_normal_lines (o, make_float (1.0));
2008 n->desired_matrix = n->current_matrix = 0;
2009 n->vscroll = 0;
2010 memset (&n->cursor, 0, sizeof (n->cursor));
2011 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2012 n->last_cursor_vpos = 0;
2013 n->phys_cursor_type = -1;
2014 n->phys_cursor_width = -1;
2015 n->must_be_updated_p = 0;
2016 n->pseudo_window_p = 0;
2017 n->window_end_vpos = 0;
2018 n->window_end_pos = 0;
2019 n->window_end_valid = 0;
2022 tem = o->next;
2023 wset_next (n, tem);
2024 if (!NILP (tem))
2025 wset_prev (XWINDOW (tem), new);
2027 tem = o->prev;
2028 wset_prev (n, tem);
2029 if (!NILP (tem))
2030 wset_next (XWINDOW (tem), new);
2032 tem = o->parent;
2033 wset_parent (n, tem);
2034 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2035 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2038 /* If window WINDOW and its parent window are iso-combined, merge
2039 WINDOW's children into those of its parent window and mark WINDOW as
2040 deleted. */
2042 static void
2043 recombine_windows (Lisp_Object window)
2045 struct window *w, *p, *c;
2046 Lisp_Object parent, child;
2047 bool horflag;
2049 w = XWINDOW (window);
2050 parent = w->parent;
2051 if (!NILP (parent) && NILP (w->combination_limit))
2053 p = XWINDOW (parent);
2054 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2055 && p->horizontal == w->horizontal)
2056 /* WINDOW and PARENT are both either a vertical or a horizontal
2057 combination. */
2059 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2060 child = w->contents;
2061 c = XWINDOW (child);
2063 /* Splice WINDOW's children into its parent's children and
2064 assign new normal sizes. */
2065 if (NILP (w->prev))
2066 wset_combination (p, horflag, child);
2067 else
2069 wset_prev (c, w->prev);
2070 wset_next (XWINDOW (w->prev), child);
2073 while (c)
2075 wset_parent (c, parent);
2077 if (horflag)
2078 wset_normal_cols (c,
2079 make_float ((double) c->total_cols
2080 / (double) p->total_cols));
2081 else
2082 wset_normal_lines (c,
2083 make_float ((double) c->total_lines
2084 / (double) p->total_lines));
2086 if (NILP (c->next))
2088 if (!NILP (w->next))
2090 wset_next (c, w->next);
2091 wset_prev (XWINDOW (c->next), child);
2094 c = 0;
2096 else
2098 child = c->next;
2099 c = XWINDOW (child);
2103 /* WINDOW can be deleted now. */
2104 wset_combination (w, 0, Qnil);
2109 /* If WINDOW can be deleted, delete it. */
2110 static void
2111 delete_deletable_window (Lisp_Object window)
2113 if (!NILP (call1 (Qwindow_deletable_p, window)))
2114 call1 (Qdelete_window, window);
2117 /***********************************************************************
2118 Window List
2119 ***********************************************************************/
2121 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2122 pointer. This is a callback function for foreach_window, used in
2123 the window_list function. */
2125 static int
2126 add_window_to_list (struct window *w, void *user_data)
2128 Lisp_Object *list = user_data;
2129 Lisp_Object window;
2130 XSETWINDOW (window, w);
2131 *list = Fcons (window, *list);
2132 return 1;
2136 /* Return a list of all windows, for use by next_window. If
2137 Vwindow_list is a list, return that list. Otherwise, build a new
2138 list, cache it in Vwindow_list, and return that. */
2140 static Lisp_Object
2141 window_list (void)
2143 if (!CONSP (Vwindow_list))
2145 Lisp_Object tail, frame;
2147 Vwindow_list = Qnil;
2148 FOR_EACH_FRAME (tail, frame)
2150 Lisp_Object args[2];
2152 /* We are visiting windows in canonical order, and add
2153 new windows at the front of args[1], which means we
2154 have to reverse this list at the end. */
2155 args[1] = Qnil;
2156 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2157 args[0] = Vwindow_list;
2158 args[1] = Fnreverse (args[1]);
2159 Vwindow_list = Fnconc (2, args);
2163 return Vwindow_list;
2167 /* Value is non-zero if WINDOW satisfies the constraints given by
2168 OWINDOW, MINIBUF and ALL_FRAMES.
2170 MINIBUF t means WINDOW may be minibuffer windows.
2171 `lambda' means WINDOW may not be a minibuffer window.
2172 a window means a specific minibuffer window
2174 ALL_FRAMES t means search all frames,
2175 nil means search just current frame,
2176 `visible' means search just visible frames on the
2177 current terminal,
2178 0 means search visible and iconified frames on the
2179 current terminal,
2180 a window means search the frame that window belongs to,
2181 a frame means consider windows on that frame, only. */
2183 static bool
2184 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2185 Lisp_Object minibuf, Lisp_Object all_frames)
2187 struct window *w = XWINDOW (window);
2188 struct frame *f = XFRAME (w->frame);
2189 bool candidate_p = 1;
2191 if (!BUFFERP (w->contents))
2192 candidate_p = 0;
2193 else if (MINI_WINDOW_P (w)
2194 && (EQ (minibuf, Qlambda)
2195 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2197 /* If MINIBUF is `lambda' don't consider any mini-windows.
2198 If it is a window, consider only that one. */
2199 candidate_p = 0;
2201 else if (EQ (all_frames, Qt))
2202 candidate_p = 1;
2203 else if (NILP (all_frames))
2205 eassert (WINDOWP (owindow));
2206 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2208 else if (EQ (all_frames, Qvisible))
2210 candidate_p = FRAME_VISIBLE_P (f)
2211 && (FRAME_TERMINAL (XFRAME (w->frame))
2212 == FRAME_TERMINAL (XFRAME (selected_frame)));
2215 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2217 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2218 #ifdef HAVE_X_WINDOWS
2219 /* Yuck!! If we've just created the frame and the
2220 window-manager requested the user to place it
2221 manually, the window may still not be considered
2222 `visible'. I'd argue it should be at least
2223 something like `iconified', but don't know how to do
2224 that yet. --Stef */
2225 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2226 && !f->output_data.x->has_been_visible)
2227 #endif
2229 && (FRAME_TERMINAL (XFRAME (w->frame))
2230 == FRAME_TERMINAL (XFRAME (selected_frame)));
2232 else if (WINDOWP (all_frames))
2233 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2234 || EQ (XWINDOW (all_frames)->frame, w->frame)
2235 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2236 else if (FRAMEP (all_frames))
2237 candidate_p = EQ (all_frames, w->frame);
2239 return candidate_p;
2243 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2244 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2245 MINIBUF, and ALL_FRAMES. */
2247 static void
2248 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2250 struct window *w = decode_live_window (*window);
2252 XSETWINDOW (*window, w);
2253 /* MINIBUF nil may or may not include minibuffers. Decide if it
2254 does. */
2255 if (NILP (*minibuf))
2256 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2257 else if (!EQ (*minibuf, Qt))
2258 *minibuf = Qlambda;
2260 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2261 => count none of them, or a specific minibuffer window (the
2262 active one) to count. */
2264 /* ALL_FRAMES nil doesn't specify which frames to include. */
2265 if (NILP (*all_frames))
2266 *all_frames
2267 = (!EQ (*minibuf, Qlambda)
2268 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2269 : Qnil);
2270 else if (EQ (*all_frames, Qvisible))
2272 else if (EQ (*all_frames, make_number (0)))
2274 else if (FRAMEP (*all_frames))
2276 else if (!EQ (*all_frames, Qt))
2277 *all_frames = Qnil;
2281 /* Return the next or previous window of WINDOW in cyclic ordering
2282 of windows. NEXT_P non-zero means return the next window. See the
2283 documentation string of next-window for the meaning of MINIBUF and
2284 ALL_FRAMES. */
2286 static Lisp_Object
2287 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2289 decode_next_window_args (&window, &minibuf, &all_frames);
2291 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2292 return the first window on the frame. */
2293 if (FRAMEP (all_frames)
2294 && !EQ (all_frames, XWINDOW (window)->frame))
2295 return Fframe_first_window (all_frames);
2297 if (next_p)
2299 Lisp_Object list;
2301 /* Find WINDOW in the list of all windows. */
2302 list = Fmemq (window, window_list ());
2304 /* Scan forward from WINDOW to the end of the window list. */
2305 if (CONSP (list))
2306 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2307 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2308 break;
2310 /* Scan from the start of the window list up to WINDOW. */
2311 if (!CONSP (list))
2312 for (list = Vwindow_list;
2313 CONSP (list) && !EQ (XCAR (list), window);
2314 list = XCDR (list))
2315 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2316 break;
2318 if (CONSP (list))
2319 window = XCAR (list);
2321 else
2323 Lisp_Object candidate, list;
2325 /* Scan through the list of windows for candidates. If there are
2326 candidate windows in front of WINDOW, the last one of these
2327 is the one we want. If there are candidates following WINDOW
2328 in the list, again the last one of these is the one we want. */
2329 candidate = Qnil;
2330 for (list = window_list (); CONSP (list); list = XCDR (list))
2332 if (EQ (XCAR (list), window))
2334 if (WINDOWP (candidate))
2335 break;
2337 else if (candidate_window_p (XCAR (list), window, minibuf,
2338 all_frames))
2339 candidate = XCAR (list);
2342 if (WINDOWP (candidate))
2343 window = candidate;
2346 return window;
2350 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2351 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2352 WINDOW must be a live window and defaults to the selected one. The
2353 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2354 consider.
2356 MINIBUF nil or omitted means consider the minibuffer window only if the
2357 minibuffer is active. MINIBUF t means consider the minibuffer window
2358 even if the minibuffer is not active. Any other value means do not
2359 consider the minibuffer window even if the minibuffer is active.
2361 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2362 plus the minibuffer window if specified by the MINIBUF argument. If the
2363 minibuffer counts, consider all windows on all frames that share that
2364 minibuffer too. The following non-nil values of ALL-FRAMES have special
2365 meanings:
2367 - t means consider all windows on all existing frames.
2369 - `visible' means consider all windows on all visible frames.
2371 - 0 (the number zero) means consider all windows on all visible and
2372 iconified frames.
2374 - A frame means consider all windows on that frame only.
2376 Anything else means consider all windows on WINDOW's frame and no
2377 others.
2379 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2380 `next-window' to iterate through the entire cycle of acceptable
2381 windows, eventually ending up back at the window you started with.
2382 `previous-window' traverses the same cycle, in the reverse order. */)
2383 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2385 return next_window (window, minibuf, all_frames, 1);
2389 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2390 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2391 WINDOW must be a live window and defaults to the selected one. The
2392 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2393 consider.
2395 MINIBUF nil or omitted means consider the minibuffer window only if the
2396 minibuffer is active. MINIBUF t means consider the minibuffer window
2397 even if the minibuffer is not active. Any other value means do not
2398 consider the minibuffer window even if the minibuffer is active.
2400 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2401 plus the minibuffer window if specified by the MINIBUF argument. If the
2402 minibuffer counts, consider all windows on all frames that share that
2403 minibuffer too. The following non-nil values of ALL-FRAMES have special
2404 meanings:
2406 - t means consider all windows on all existing frames.
2408 - `visible' means consider all windows on all visible frames.
2410 - 0 (the number zero) means consider all windows on all visible and
2411 iconified frames.
2413 - A frame means consider all windows on that frame only.
2415 Anything else means consider all windows on WINDOW's frame and no
2416 others.
2418 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2419 use `previous-window' to iterate through the entire cycle of
2420 acceptable windows, eventually ending up back at the window you
2421 started with. `next-window' traverses the same cycle, in the
2422 reverse order. */)
2423 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2425 return next_window (window, minibuf, all_frames, 0);
2429 /* Return a list of windows in cyclic ordering. Arguments are like
2430 for `next-window'. */
2432 static Lisp_Object
2433 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2435 Lisp_Object tail, list, rest;
2437 decode_next_window_args (&window, &minibuf, &all_frames);
2438 list = Qnil;
2440 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2441 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2442 list = Fcons (XCAR (tail), list);
2444 /* Rotate the list to start with WINDOW. */
2445 list = Fnreverse (list);
2446 rest = Fmemq (window, list);
2447 if (!NILP (rest) && !EQ (rest, list))
2449 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2451 XSETCDR (tail, Qnil);
2452 list = nconc2 (rest, list);
2454 return list;
2458 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2459 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2460 FRAME nil or omitted means use the selected frame.
2461 WINDOW nil or omitted means use the window selected within FRAME.
2462 MINIBUF t means include the minibuffer window, even if it isn't active.
2463 MINIBUF nil or omitted means include the minibuffer window only
2464 if it's active.
2465 MINIBUF neither nil nor t means never include the minibuffer window. */)
2466 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2468 if (NILP (window))
2469 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2470 CHECK_WINDOW (window);
2471 if (NILP (frame))
2472 frame = selected_frame;
2474 if (!EQ (frame, XWINDOW (window)->frame))
2475 error ("Window is on a different frame");
2477 return window_list_1 (window, minibuf, frame);
2481 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2482 doc: /* Return a list of all live windows.
2483 WINDOW specifies the first window to list and defaults to the selected
2484 window.
2486 Optional argument MINIBUF nil or omitted means consider the minibuffer
2487 window only if the minibuffer is active. MINIBUF t means consider the
2488 minibuffer window even if the minibuffer is not active. Any other value
2489 means do not consider the minibuffer window even if the minibuffer is
2490 active.
2492 Optional argument ALL-FRAMES nil or omitted means consider all windows
2493 on WINDOW's frame, plus the minibuffer window if specified by the
2494 MINIBUF argument. If the minibuffer counts, consider all windows on all
2495 frames that share that minibuffer too. The following non-nil values of
2496 ALL-FRAMES have special meanings:
2498 - t means consider all windows on all existing frames.
2500 - `visible' means consider all windows on all visible frames.
2502 - 0 (the number zero) means consider all windows on all visible and
2503 iconified frames.
2505 - A frame means consider all windows on that frame only.
2507 Anything else means consider all windows on WINDOW's frame and no
2508 others.
2510 If WINDOW is not on the list of windows returned, some other window will
2511 be listed first but no error is signaled. */)
2512 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2514 return window_list_1 (window, minibuf, all_frames);
2517 /* Look at all windows, performing an operation specified by TYPE
2518 with argument OBJ.
2519 If FRAMES is Qt, look at all frames;
2520 Qnil, look at just the selected frame;
2521 Qvisible, look at visible frames;
2522 a frame, just look at windows on that frame.
2523 If MINI is non-zero, perform the operation on minibuffer windows too. */
2525 enum window_loop
2527 WINDOW_LOOP_UNUSED,
2528 GET_BUFFER_WINDOW, /* Arg is buffer */
2529 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2530 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2531 CHECK_ALL_WINDOWS /* Arg is ignored */
2534 static Lisp_Object
2535 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2537 Lisp_Object window, windows, best_window, frame_arg;
2538 int frame_best_window_flag = 0;
2539 struct frame *f;
2540 struct gcpro gcpro1;
2542 /* If we're only looping through windows on a particular frame,
2543 frame points to that frame. If we're looping through windows
2544 on all frames, frame is 0. */
2545 if (FRAMEP (frames))
2546 f = XFRAME (frames);
2547 else if (NILP (frames))
2548 f = SELECTED_FRAME ();
2549 else
2550 f = NULL;
2552 if (f)
2553 frame_arg = Qlambda;
2554 else if (EQ (frames, make_number (0)))
2555 frame_arg = frames;
2556 else if (EQ (frames, Qvisible))
2557 frame_arg = frames;
2558 else
2559 frame_arg = Qt;
2561 /* frame_arg is Qlambda to stick to one frame,
2562 Qvisible to consider all visible frames,
2563 or Qt otherwise. */
2565 /* Pick a window to start with. */
2566 if (WINDOWP (obj))
2567 window = obj;
2568 else if (f)
2569 window = FRAME_SELECTED_WINDOW (f);
2570 else
2571 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2573 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2574 GCPRO1 (windows);
2575 best_window = Qnil;
2577 for (; CONSP (windows); windows = XCDR (windows))
2579 struct window *w;
2581 window = XCAR (windows);
2582 w = XWINDOW (window);
2584 /* Note that we do not pay attention here to whether the frame
2585 is visible, since Fwindow_list skips non-visible frames if
2586 that is desired, under the control of frame_arg. */
2587 if (!MINI_WINDOW_P (w)
2588 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2589 consider all windows. */
2590 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2591 || (mini && minibuf_level > 0))
2592 switch (type)
2594 case GET_BUFFER_WINDOW:
2595 if (EQ (w->contents, obj)
2596 /* Don't find any minibuffer window except the one that
2597 is currently in use. */
2598 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2600 if (EQ (window, selected_window))
2601 /* Preferably return the selected window. */
2602 RETURN_UNGCPRO (window);
2603 else if (EQ (XWINDOW (window)->frame, selected_frame)
2604 && !frame_best_window_flag)
2605 /* Prefer windows on the current frame (but don't
2606 choose another one if we have one already). */
2608 best_window = window;
2609 frame_best_window_flag = 1;
2611 else if (NILP (best_window))
2612 best_window = window;
2614 break;
2616 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2617 /* We could simply check whether the buffer shown by window
2618 is live, and show another buffer in case it isn't. */
2619 if (EQ (w->contents, obj))
2621 /* Undedicate WINDOW. */
2622 wset_dedicated (w, Qnil);
2623 /* Make WINDOW show the buffer returned by
2624 other_buffer_safely, don't run any hooks. */
2625 set_window_buffer
2626 (window, other_buffer_safely (w->contents), 0, 0);
2627 /* If WINDOW is the selected window, make its buffer
2628 current. But do so only if the window shows the
2629 current buffer (Bug#6454). */
2630 if (EQ (window, selected_window)
2631 && XBUFFER (w->contents) == current_buffer)
2632 Fset_buffer (w->contents);
2634 break;
2636 case REDISPLAY_BUFFER_WINDOWS:
2637 if (EQ (w->contents, obj))
2639 mark_window_display_accurate (window, 0);
2640 w->update_mode_line = 1;
2641 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2642 ++update_mode_lines;
2643 best_window = window;
2645 break;
2647 /* Check for a leaf window that has a killed buffer
2648 or broken markers. */
2649 case CHECK_ALL_WINDOWS:
2650 if (BUFFERP (w->contents))
2652 struct buffer *b = XBUFFER (w->contents);
2654 if (!BUFFER_LIVE_P (b))
2655 emacs_abort ();
2656 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2657 emacs_abort ();
2658 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2659 emacs_abort ();
2661 break;
2663 case WINDOW_LOOP_UNUSED:
2664 break;
2668 UNGCPRO;
2669 return best_window;
2672 /* Used for debugging. Abort if any window has a dead buffer. */
2674 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2675 void
2676 check_all_windows (void)
2678 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2681 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2682 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2683 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2684 the current buffer.
2686 The optional argument ALL-FRAMES specifies the frames to consider:
2688 - t means consider all windows on all existing frames.
2690 - `visible' means consider all windows on all visible frames.
2692 - 0 (the number zero) means consider all windows on all visible
2693 and iconified frames.
2695 - A frame means consider all windows on that frame only.
2697 Any other value of ALL-FRAMES means consider all windows on the
2698 selected frame and no others. */)
2699 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2701 Lisp_Object buffer;
2703 if (NILP (buffer_or_name))
2704 buffer = Fcurrent_buffer ();
2705 else
2706 buffer = Fget_buffer (buffer_or_name);
2708 if (BUFFERP (buffer))
2709 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2710 else
2711 return Qnil;
2714 static Lisp_Object
2715 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2717 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2721 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2722 Sdelete_other_windows_internal, 0, 2, "",
2723 doc: /* Make WINDOW fill its frame.
2724 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2725 and defaults to the selected one.
2727 Optional argument ROOT, if non-nil, must specify an internal window such
2728 that WINDOW is in its window subtree. If this is the case, replace ROOT
2729 by WINDOW and leave alone any windows not part of ROOT's subtree.
2731 When WINDOW is live try to reduce display jumps by keeping the text
2732 previously visible in WINDOW in the same place on the frame. Doing this
2733 depends on the value of (window-start WINDOW), so if calling this
2734 function in a program gives strange scrolling, make sure the
2735 window-start value is reasonable when this function is called. */)
2736 (Lisp_Object window, Lisp_Object root)
2738 struct window *w, *r, *s;
2739 struct frame *f;
2740 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2741 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2742 int top IF_LINT (= 0), new_top, resize_failed;
2744 w = decode_valid_window (window);
2745 XSETWINDOW (window, w);
2746 f = XFRAME (w->frame);
2748 if (NILP (root))
2749 /* ROOT is the frame's root window. */
2751 root = FRAME_ROOT_WINDOW (f);
2752 r = XWINDOW (root);
2754 else
2755 /* ROOT must be an ancestor of WINDOW. */
2757 r = decode_valid_window (root);
2758 pwindow = XWINDOW (window)->parent;
2759 while (!NILP (pwindow))
2760 if (EQ (pwindow, root))
2761 break;
2762 else
2763 pwindow = XWINDOW (pwindow)->parent;
2764 if (!EQ (pwindow, root))
2765 error ("Specified root is not an ancestor of specified window");
2768 if (EQ (window, root))
2769 /* A noop. */
2770 return Qnil;
2771 /* I don't understand the "top > 0" part below. If we deal with a
2772 standalone minibuffer it would have been caught by the preceding
2773 test. */
2774 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2775 error ("Can't expand minibuffer to full frame");
2777 if (BUFFERP (w->contents))
2779 startpos = marker_position (w->start);
2780 startbyte = marker_byte_position (w->start);
2781 top = WINDOW_TOP_EDGE_LINE (w)
2782 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2783 /* Make sure WINDOW is the frame's selected window. */
2784 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2786 if (EQ (selected_frame, w->frame))
2787 Fselect_window (window, Qnil);
2788 else
2789 fset_selected_window (f, window);
2792 else
2794 /* See if the frame's selected window is a part of the window
2795 subtree rooted at WINDOW, by finding all the selected window's
2796 parents and comparing each one with WINDOW. If it isn't we
2797 need a new selected window for this frame. */
2798 swindow = FRAME_SELECTED_WINDOW (f);
2799 while (1)
2801 pwindow = swindow;
2802 while (!NILP (pwindow) && !EQ (window, pwindow))
2803 pwindow = XWINDOW (pwindow)->parent;
2805 if (EQ (window, pwindow))
2806 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2807 as the new selected window. */
2808 break;
2809 else
2810 /* Else try the previous window of SWINDOW. */
2811 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2814 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2816 if (EQ (selected_frame, w->frame))
2817 Fselect_window (swindow, Qnil);
2818 else
2819 fset_selected_window (f, swindow);
2823 block_input ();
2824 if (!FRAME_INITIAL_P (f))
2826 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2828 /* We are going to free the glyph matrices of WINDOW, and with
2829 that we might lose any information about glyph rows that have
2830 some of their glyphs highlighted in mouse face. (These rows
2831 are marked with a non-zero mouse_face_p flag.) If WINDOW
2832 indeed has some glyphs highlighted in mouse face, signal to
2833 frame's up-to-date hook that mouse highlight was overwritten,
2834 so that it will arrange for redisplaying the highlight. */
2835 if (EQ (hlinfo->mouse_face_window, window))
2836 reset_mouse_highlight (hlinfo);
2838 free_window_matrices (r);
2840 windows_or_buffers_changed++;
2841 Vwindow_list = Qnil;
2842 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2843 resize_failed = 0;
2845 if (!WINDOW_LEAF_P (w))
2847 /* Resize child windows vertically. */
2848 XSETINT (delta, r->total_lines - w->total_lines);
2849 w->top_line = r->top_line;
2850 resize_root_window (window, delta, Qnil, Qnil);
2851 if (window_resize_check (w, 0))
2852 window_resize_apply (w, 0);
2853 else
2855 resize_root_window (window, delta, Qnil, Qt);
2856 if (window_resize_check (w, 0))
2857 window_resize_apply (w, 0);
2858 else
2859 resize_failed = 1;
2862 /* Resize child windows horizontally. */
2863 if (!resize_failed)
2865 w->left_col = r->left_col;
2866 XSETINT (delta, r->total_cols - w->total_cols);
2867 resize_root_window (window, delta, Qt, Qnil);
2868 if (window_resize_check (w, 1))
2869 window_resize_apply (w, 1);
2870 else
2872 resize_root_window (window, delta, Qt, Qt);
2873 if (window_resize_check (w, 1))
2874 window_resize_apply (w, 1);
2875 else
2876 resize_failed = 1;
2880 if (resize_failed)
2881 /* Play safe, if we still can ... */
2883 window = swindow;
2884 w = XWINDOW (window);
2888 /* Cleanly unlink WINDOW from window-tree. */
2889 if (!NILP (w->prev))
2890 /* Get SIBLING above (on the left of) WINDOW. */
2892 sibling = w->prev;
2893 s = XWINDOW (sibling);
2894 wset_next (s, w->next);
2895 if (!NILP (s->next))
2896 wset_prev (XWINDOW (s->next), sibling);
2898 else
2899 /* Get SIBLING below (on the right of) WINDOW. */
2901 sibling = w->next;
2902 s = XWINDOW (sibling);
2903 wset_prev (s, Qnil);
2904 wset_combination (XWINDOW (w->parent),
2905 XWINDOW (w->parent)->horizontal, sibling);
2908 /* Delete ROOT and all child windows of ROOT. */
2909 if (WINDOWP (r->contents))
2911 delete_all_child_windows (r->contents);
2912 wset_combination (r, 0, Qnil);
2915 replace_window (root, window, 1);
2917 /* This must become SWINDOW anyway ....... */
2918 if (BUFFERP (w->contents) && !resize_failed)
2920 /* Try to minimize scrolling, by setting the window start to the
2921 point will cause the text at the old window start to be at the
2922 same place on the frame. But don't try to do this if the
2923 window start is outside the visible portion (as might happen
2924 when the display is not current, due to typeahead). */
2925 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2926 if (new_top != top
2927 && startpos >= BUF_BEGV (XBUFFER (w->contents))
2928 && startpos <= BUF_ZV (XBUFFER (w->contents)))
2930 struct position pos;
2931 struct buffer *obuf = current_buffer;
2933 Fset_buffer (w->contents);
2934 /* This computation used to temporarily move point, but that
2935 can have unwanted side effects due to text properties. */
2936 pos = *vmotion (startpos, startbyte, -top, w);
2938 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
2939 w->window_end_valid = 0;
2940 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
2941 || FETCH_BYTE (pos.bytepos - 1) == '\n');
2942 /* We need to do this, so that the window-scroll-functions
2943 get called. */
2944 w->optional_new_start = 1;
2946 set_buffer_internal (obuf);
2950 adjust_frame_glyphs (f);
2951 unblock_input ();
2953 run_window_configuration_change_hook (f);
2955 return Qnil;
2959 void
2960 replace_buffer_in_windows (Lisp_Object buffer)
2962 call1 (Qreplace_buffer_in_windows, buffer);
2965 /* If BUFFER is shown in a window, safely replace it with some other
2966 buffer in all windows of all frames, even those on other keyboards. */
2968 void
2969 replace_buffer_in_windows_safely (Lisp_Object buffer)
2971 if (buffer_window_count (XBUFFER (buffer)))
2973 Lisp_Object tail, frame;
2975 /* A single call to window_loop won't do the job because it only
2976 considers frames on the current keyboard. So loop manually over
2977 frames, and handle each one. */
2978 FOR_EACH_FRAME (tail, frame)
2979 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
2983 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2984 minimum allowable size. */
2986 void
2987 check_frame_size (struct frame *frame, int *rows, int *cols)
2989 /* For height, we have to see:
2990 how many windows the frame has at minimum (one or two),
2991 and whether it has a menu bar or other special stuff at the top. */
2992 int min_height
2993 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2994 ? MIN_SAFE_WINDOW_HEIGHT
2995 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2997 if (FRAME_TOP_MARGIN (frame) > 0)
2998 min_height += FRAME_TOP_MARGIN (frame);
3000 if (*rows < min_height)
3001 *rows = min_height;
3002 if (*cols < MIN_SAFE_WINDOW_WIDTH)
3003 *cols = MIN_SAFE_WINDOW_WIDTH;
3006 /* Adjust the margins of window W if text area is too small.
3007 Return 1 if window width is ok after adjustment; 0 if window
3008 is still too narrow. */
3010 static int
3011 adjust_window_margins (struct window *w)
3013 int box_cols = (WINDOW_TOTAL_COLS (w)
3014 - WINDOW_FRINGE_COLS (w)
3015 - WINDOW_SCROLL_BAR_COLS (w));
3016 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
3017 + WINDOW_RIGHT_MARGIN_COLS (w));
3019 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
3020 return 1;
3022 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
3023 return 0;
3025 /* Window's text area is too narrow, but reducing the window
3026 margins will fix that. */
3027 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
3028 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
3030 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
3031 w->left_margin_cols = w->right_margin_cols = margin_cols / 2;
3032 else
3033 w->right_margin_cols = margin_cols;
3035 else
3036 w->left_margin_cols = margin_cols;
3037 return 1;
3040 /* The following three routines are needed for running a window's
3041 configuration change hook. */
3042 static void
3043 run_funs (Lisp_Object funs)
3045 for (; CONSP (funs); funs = XCDR (funs))
3046 if (!EQ (XCAR (funs), Qt))
3047 call0 (XCAR (funs));
3050 static void
3051 select_window_norecord (Lisp_Object window)
3053 if (WINDOW_LIVE_P (window))
3054 Fselect_window (window, Qt);
3057 static void
3058 select_frame_norecord (Lisp_Object frame)
3060 if (FRAME_LIVE_P (XFRAME (frame)))
3061 Fselect_frame (frame, Qt);
3064 void
3065 run_window_configuration_change_hook (struct frame *f)
3067 ptrdiff_t count = SPECPDL_INDEX ();
3068 Lisp_Object frame, global_wcch
3069 = Fdefault_value (Qwindow_configuration_change_hook);
3070 XSETFRAME (frame, f);
3072 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3073 return;
3075 /* Use the right buffer. Matters when running the local hooks. */
3076 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3078 record_unwind_current_buffer ();
3079 Fset_buffer (Fwindow_buffer (Qnil));
3082 if (SELECTED_FRAME () != f)
3084 record_unwind_protect (select_frame_norecord, selected_frame);
3085 select_frame_norecord (frame);
3088 /* Look for buffer-local values. */
3090 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3091 for (; CONSP (windows); windows = XCDR (windows))
3093 Lisp_Object window = XCAR (windows);
3094 Lisp_Object buffer = Fwindow_buffer (window);
3095 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3096 buffer)))
3098 ptrdiff_t inner_count = SPECPDL_INDEX ();
3099 record_unwind_protect (select_window_norecord, selected_window);
3100 select_window_norecord (window);
3101 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3102 buffer));
3103 unbind_to (inner_count, Qnil);
3108 run_funs (global_wcch);
3109 unbind_to (count, Qnil);
3112 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3113 Srun_window_configuration_change_hook, 0, 1, 0,
3114 doc: /* Run `window-configuration-change-hook' for FRAME.
3115 If FRAME is omitted or nil, it defaults to the selected frame. */)
3116 (Lisp_Object frame)
3118 run_window_configuration_change_hook (decode_live_frame (frame));
3119 return Qnil;
3122 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3123 means it's allowed to run hooks. See make_frame for a case where
3124 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3125 margins, fringes, and scroll-bar settings of the window are not
3126 reset from the buffer's local settings. */
3128 void
3129 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3130 bool run_hooks_p, bool keep_margins_p)
3132 struct window *w = XWINDOW (window);
3133 struct buffer *b = XBUFFER (buffer);
3134 ptrdiff_t count = SPECPDL_INDEX ();
3135 int samebuf = EQ (buffer, w->contents);
3137 wset_buffer (w, buffer);
3139 if (EQ (window, selected_window))
3140 bset_last_selected_window (b, window);
3142 /* Let redisplay errors through. */
3143 b->display_error_modiff = 0;
3145 /* Update time stamps of buffer display. */
3146 if (INTEGERP (BVAR (b, display_count)))
3147 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3148 bset_display_time (b, Fcurrent_time ());
3150 w->window_end_pos = 0;
3151 w->window_end_vpos = 0;
3152 w->last_cursor_vpos = 0;
3154 if (!(keep_margins_p && samebuf))
3155 { /* If we're not actually changing the buffer, don't reset hscroll and
3156 vscroll. This case happens for example when called from
3157 change_frame_size_1, where we use a dummy call to
3158 Fset_window_buffer on the frame's selected window (and no other)
3159 just in order to run window-configuration-change-hook.
3160 Resetting hscroll and vscroll here is problematic for things like
3161 image-mode and doc-view-mode since it resets the image's position
3162 whenever we resize the frame. */
3163 w->hscroll = w->min_hscroll = 0;
3164 w->vscroll = 0;
3165 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3166 set_marker_restricted (w->start,
3167 make_number (b->last_window_start),
3168 buffer);
3169 w->start_at_line_beg = 0;
3170 w->force_start = 0;
3172 /* Maybe we could move this into the `if' but it's not obviously safe and
3173 I doubt it's worth the trouble. */
3174 windows_or_buffers_changed++;
3176 /* We must select BUFFER for running the window-scroll-functions. */
3177 /* We can't check ! NILP (Vwindow_scroll_functions) here
3178 because that might itself be a local variable. */
3179 if (window_initialized)
3181 record_unwind_current_buffer ();
3182 Fset_buffer (buffer);
3185 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3187 if (!keep_margins_p)
3189 /* Set left and right marginal area width etc. from buffer. */
3190 set_window_fringes (w, BVAR (b, left_fringe_width),
3191 BVAR (b, right_fringe_width),
3192 BVAR (b, fringes_outside_margins));
3193 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3194 BVAR (b, vertical_scroll_bar_type), Qnil);
3195 set_window_margins (w, BVAR (b, left_margin_cols),
3196 BVAR (b, right_margin_cols));
3197 apply_window_adjustment (w);
3200 if (run_hooks_p)
3202 if (! NILP (Vwindow_scroll_functions))
3203 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3204 Fmarker_position (w->start));
3205 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3208 unbind_to (count, Qnil);
3211 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3212 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3213 WINDOW must be a live window and defaults to the selected one.
3214 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3216 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3217 display margins, fringe widths, and scroll bar settings are preserved;
3218 the default is to reset these from the local settings for BUFFER-OR-NAME
3219 or the frame defaults. Return nil.
3221 This function throws an error when WINDOW is strongly dedicated to its
3222 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3223 already display BUFFER-OR-NAME.
3225 This function runs `window-scroll-functions' before running
3226 `window-configuration-change-hook'. */)
3227 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3229 register Lisp_Object tem, buffer;
3230 register struct window *w = decode_live_window (window);
3232 XSETWINDOW (window, w);
3233 buffer = Fget_buffer (buffer_or_name);
3234 CHECK_BUFFER (buffer);
3235 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3236 error ("Attempt to display deleted buffer");
3238 tem = w->contents;
3239 if (NILP (tem))
3240 error ("Window is deleted");
3241 else
3243 if (!EQ (tem, buffer))
3245 if (EQ (w->dedicated, Qt))
3246 /* WINDOW is strongly dedicated to its buffer, signal an
3247 error. */
3248 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3249 else
3250 /* WINDOW is weakly dedicated to its buffer, reset
3251 dedication. */
3252 wset_dedicated (w, Qnil);
3254 call1 (Qrecord_window_buffer, window);
3257 unshow_buffer (w);
3260 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3262 return Qnil;
3265 static Lisp_Object
3266 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3268 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3271 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3272 0, 1, 0,
3273 doc: /* Force all windows to be updated on next redisplay.
3274 If optional arg OBJECT is a window, force redisplay of that window only.
3275 If OBJECT is a buffer or buffer name, force redisplay of all windows
3276 displaying that buffer. */)
3277 (Lisp_Object object)
3279 if (NILP (object))
3281 windows_or_buffers_changed++;
3282 update_mode_lines++;
3283 return Qt;
3286 if (WINDOWP (object))
3288 struct window *w = XWINDOW (object);
3289 mark_window_display_accurate (object, 0);
3290 w->update_mode_line = 1;
3291 if (BUFFERP (w->contents))
3292 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3293 ++update_mode_lines;
3294 return Qt;
3297 if (STRINGP (object))
3298 object = Fget_buffer (object);
3299 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3300 && buffer_window_count (XBUFFER (object)))
3302 /* If buffer is live and shown in at least one window, find
3303 all windows showing this buffer and force update of them. */
3304 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3305 return NILP (object) ? Qnil : Qt;
3308 /* If nothing suitable was found, just return.
3309 We could signal an error, but this feature will typically be used
3310 asynchronously in timers or process sentinels, so we don't. */
3311 return Qnil;
3314 /* Obsolete since 24.3. */
3315 void
3316 temp_output_buffer_show (register Lisp_Object buf)
3318 register struct buffer *old = current_buffer;
3319 register Lisp_Object window;
3320 register struct window *w;
3322 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3324 Fset_buffer (buf);
3325 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3326 BEGV = BEG;
3327 ZV = Z;
3328 SET_PT (BEG);
3329 set_buffer_internal (old);
3331 if (!NILP (Vtemp_buffer_show_function))
3332 call1 (Vtemp_buffer_show_function, buf);
3333 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3335 if (!EQ (XWINDOW (window)->frame, selected_frame))
3336 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3337 Vminibuf_scroll_window = window;
3338 w = XWINDOW (window);
3339 w->hscroll = 0;
3340 w->min_hscroll = 0;
3341 set_marker_restricted_both (w->start, buf, BEG, BEG);
3342 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3344 /* Run temp-buffer-show-hook, with the chosen window selected
3345 and its buffer current. */
3347 ptrdiff_t count = SPECPDL_INDEX ();
3348 Lisp_Object prev_window, prev_buffer;
3349 prev_window = selected_window;
3350 XSETBUFFER (prev_buffer, old);
3352 /* Select the window that was chosen, for running the hook.
3353 Note: Both Fselect_window and select_window_norecord may
3354 set-buffer to the buffer displayed in the window,
3355 so we need to save the current buffer. --stef */
3356 record_unwind_protect (restore_buffer, prev_buffer);
3357 record_unwind_protect (select_window_norecord, prev_window);
3358 Fselect_window (window, Qt);
3359 Fset_buffer (w->contents);
3360 Frun_hooks (1, &Qtemp_buffer_show_hook);
3361 unbind_to (count, Qnil);
3366 /* Make new window, have it replace WINDOW in window-tree, and make
3367 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3368 horizontal child). */
3369 static void
3370 make_parent_window (Lisp_Object window, bool horflag)
3372 Lisp_Object parent;
3373 register struct window *o, *p;
3375 o = XWINDOW (window);
3376 p = allocate_window ();
3377 memcpy ((char *) p + sizeof (struct vectorlike_header),
3378 (char *) o + sizeof (struct vectorlike_header),
3379 word_size * VECSIZE (struct window));
3380 /* P's buffer slot may change from nil to a buffer... */
3381 adjust_window_count (p, 1);
3382 XSETWINDOW (parent, p);
3384 replace_window (window, parent, 1);
3386 wset_next (o, Qnil);
3387 wset_prev (o, Qnil);
3388 wset_parent (o, parent);
3389 /* ...but now P becomes an internal window. */
3390 wset_start (p, Qnil);
3391 wset_pointm (p, Qnil);
3392 wset_buffer (p, Qnil);
3393 wset_combination (p, horflag, window);
3394 wset_combination_limit (p, Qnil);
3395 wset_window_parameters (p, Qnil);
3398 /* Make new window from scratch. */
3399 Lisp_Object
3400 make_window (void)
3402 Lisp_Object window;
3403 register struct window *w;
3405 w = allocate_window ();
3406 /* Initialize Lisp data. Note that allocate_window initializes all
3407 Lisp data to nil, so do it only for slots which should not be nil. */
3408 wset_normal_lines (w, make_float (1.0));
3409 wset_normal_cols (w, make_float (1.0));
3410 wset_new_total (w, make_number (0));
3411 wset_new_normal (w, make_number (0));
3412 wset_start (w, Fmake_marker ());
3413 wset_pointm (w, Fmake_marker ());
3414 wset_vertical_scroll_bar_type (w, Qt);
3415 /* These Lisp fields are marked specially so they're not set to nil by
3416 allocate_window. */
3417 wset_prev_buffers (w, Qnil);
3418 wset_next_buffers (w, Qnil);
3420 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3421 non-Lisp data, so do it only for slots which should not be zero. */
3422 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3423 w->left_fringe_width = w->right_fringe_width = -1;
3424 w->mode_line_height = w->header_line_height = -1;
3425 w->phys_cursor_type = -1;
3426 w->phys_cursor_width = -1;
3427 w->scroll_bar_width = -1;
3428 w->column_number_displayed = -1;
3430 /* Reset window_list. */
3431 Vwindow_list = Qnil;
3432 /* Return window. */
3433 XSETWINDOW (window, w);
3434 return window;
3437 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3438 doc: /* Set new total size of WINDOW to SIZE.
3439 WINDOW must be a valid window and defaults to the selected one.
3440 Return SIZE.
3442 Optional argument ADD non-nil means add SIZE to the new total size of
3443 WINDOW and return the sum.
3445 Note: This function does not operate on any child windows of WINDOW. */)
3446 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3448 struct window *w = decode_valid_window (window);
3450 CHECK_NUMBER (size);
3451 if (NILP (add))
3452 wset_new_total (w, size);
3453 else
3454 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3456 return w->new_total;
3459 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3460 doc: /* Set new normal size of WINDOW to SIZE.
3461 WINDOW must be a valid window and defaults to the selected one.
3462 Return SIZE.
3464 Note: This function does not operate on any child windows of WINDOW. */)
3465 (Lisp_Object window, Lisp_Object size)
3467 wset_new_normal (decode_valid_window (window), size);
3468 return size;
3471 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3472 non-zero) to w->new_total would result in correct heights (widths)
3473 for window W and recursively all child windows of W.
3475 Note: This function does not check any of `window-fixed-size-p',
3476 `window-min-height' or `window-min-width'. It does check that window
3477 sizes do not drop below one line (two columns). */
3478 static int
3479 window_resize_check (struct window *w, bool horflag)
3481 struct window *c;
3483 if (WINDOW_VERTICAL_COMBINATION_P (w))
3484 /* W is a vertical combination. */
3486 c = XWINDOW (w->contents);
3487 if (horflag)
3488 /* All child windows of W must have the same width as W. */
3490 while (c)
3492 if ((XINT (c->new_total) != XINT (w->new_total))
3493 || !window_resize_check (c, horflag))
3494 return 0;
3495 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3497 return 1;
3499 else
3500 /* The sum of the heights of the child windows of W must equal
3501 W's height. */
3503 int sum_of_sizes = 0;
3504 while (c)
3506 if (!window_resize_check (c, horflag))
3507 return 0;
3508 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3509 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3511 return (sum_of_sizes == XINT (w->new_total));
3514 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3515 /* W is a horizontal combination. */
3517 c = XWINDOW (w->contents);
3518 if (horflag)
3519 /* The sum of the widths of the child windows of W must equal W's
3520 width. */
3522 int sum_of_sizes = 0;
3523 while (c)
3525 if (!window_resize_check (c, horflag))
3526 return 0;
3527 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3528 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3530 return (sum_of_sizes == XINT (w->new_total));
3532 else
3533 /* All child windows of W must have the same height as W. */
3535 while (c)
3537 if ((XINT (c->new_total) != XINT (w->new_total))
3538 || !window_resize_check (c, horflag))
3539 return 0;
3540 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3542 return 1;
3545 else
3546 /* A leaf window. Make sure it's not too small. The following
3547 hardcodes the values of `window-safe-min-width' (2) and
3548 `window-safe-min-height' (1) which are defined in window.el. */
3549 return XINT (w->new_total) >= (horflag ? 2 : 1);
3552 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3553 w->new_total for window W and recursively all child windows of W.
3554 Also calculate and assign the new vertical (horizontal) start
3555 positions of each of these windows.
3557 This function does not perform any error checks. Make sure you have
3558 run window_resize_check on W before applying this function. */
3559 static void
3560 window_resize_apply (struct window *w, bool horflag)
3562 struct window *c;
3563 int pos;
3565 /* Note: Assigning new_normal requires that the new total size of the
3566 parent window has been set *before*. */
3567 if (horflag)
3569 w->total_cols = XFASTINT (w->new_total);
3570 if (NUMBERP (w->new_normal))
3571 wset_normal_cols (w, w->new_normal);
3573 pos = w->left_col;
3575 else
3577 w->total_lines = XFASTINT (w->new_total);
3578 if (NUMBERP (w->new_normal))
3579 wset_normal_lines (w, w->new_normal);
3581 pos = w->top_line;
3584 if (WINDOW_VERTICAL_COMBINATION_P (w))
3585 /* W is a vertical combination. */
3587 c = XWINDOW (w->contents);
3588 while (c)
3590 if (horflag)
3591 c->left_col = pos;
3592 else
3593 c->top_line = pos;
3594 window_resize_apply (c, horflag);
3595 if (!horflag)
3596 pos = pos + c->total_lines;
3597 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3600 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3601 /* W is a horizontal combination. */
3603 c = XWINDOW (w->contents);
3604 while (c)
3606 if (horflag)
3607 c->left_col = pos;
3608 else
3609 c->top_line = pos;
3610 window_resize_apply (c, horflag);
3611 if (horflag)
3612 pos = pos + c->total_cols;
3613 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3619 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3620 doc: /* Apply requested size values for window-tree of FRAME.
3621 If FRAME is omitted or nil, it defaults to the selected frame.
3623 Optional argument HORIZONTAL omitted or nil means apply requested
3624 height values. HORIZONTAL non-nil means apply requested width values.
3626 This function checks whether the requested values sum up to a valid
3627 window layout, recursively assigns the new sizes of all child windows
3628 and calculates and assigns the new start positions of these windows.
3630 Note: This function does not check any of `window-fixed-size-p',
3631 `window-min-height' or `window-min-width'. All these checks have to
3632 be applied on the Elisp level. */)
3633 (Lisp_Object frame, Lisp_Object horizontal)
3635 struct frame *f = decode_live_frame (frame);
3636 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3637 bool horflag = !NILP (horizontal);
3639 if (!window_resize_check (r, horflag)
3640 || (XINT (r->new_total)
3641 != (horflag ? r->total_cols : r->total_lines)))
3642 return Qnil;
3644 block_input ();
3645 window_resize_apply (r, horflag);
3647 windows_or_buffers_changed++;
3648 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3650 adjust_frame_glyphs (f);
3651 unblock_input ();
3653 run_window_configuration_change_hook (f);
3655 return Qt;
3659 /* Resize frame F's windows when number of lines of F is set to SIZE.
3660 HORFLAG 1 means resize windows when number of columns of F is set to
3661 SIZE.
3663 This function can delete all windows but the selected one in order to
3664 satisfy the request. The result will be meaningful if and only if
3665 F's windows have meaningful sizes when you call this. */
3666 void
3667 resize_frame_windows (struct frame *f, int size, bool horflag)
3669 Lisp_Object root = f->root_window;
3670 struct window *r = XWINDOW (root);
3671 Lisp_Object mini = f->minibuffer_window;
3672 struct window *m;
3673 /* new_size is the new size of the frame's root window. */
3674 int new_size = (horflag
3675 ? size
3676 : (size
3677 - FRAME_TOP_MARGIN (f)
3678 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3679 ? 1 : 0)));
3681 r->top_line = FRAME_TOP_MARGIN (f);
3682 if (WINDOW_LEAF_P (r))
3683 /* For a leaf root window just set the size. */
3684 if (horflag)
3685 r->total_cols = new_size;
3686 else
3687 r->total_lines = new_size;
3688 else
3690 /* old_size is the old size of the frame's root window. */
3691 int old_size = horflag ? r->total_cols : r->total_lines;
3692 Lisp_Object delta;
3694 XSETINT (delta, new_size - old_size);
3695 /* Try a "normal" resize first. */
3696 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3697 if (window_resize_check (r, horflag)
3698 && new_size == XINT (r->new_total))
3699 window_resize_apply (r, horflag);
3700 else
3702 /* Try with "reasonable" minimum sizes next. */
3703 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3704 if (window_resize_check (r, horflag)
3705 && new_size == XINT (r->new_total))
3706 window_resize_apply (r, horflag);
3707 else
3709 /* Finally, try with "safe" minimum sizes. */
3710 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3711 if (window_resize_check (r, horflag)
3712 && new_size == XINT (r->new_total))
3713 window_resize_apply (r, horflag);
3714 else
3716 /* We lost. Delete all windows but the frame's
3717 selected one. */
3718 root = f->selected_window;
3719 Fdelete_other_windows_internal (root, Qnil);
3720 if (horflag)
3721 XWINDOW (root)->total_cols = new_size;
3722 else
3723 XWINDOW (root)->total_lines = new_size;
3729 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3731 m = XWINDOW (mini);
3732 if (horflag)
3733 m->total_cols = size;
3734 else
3736 /* Are we sure we always want 1 line here? */
3737 m->total_lines = 1;
3738 m->top_line = r->top_line + r->total_lines;
3742 windows_or_buffers_changed++;
3746 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3747 doc: /* Split window OLD.
3748 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3749 new window. In any case TOTAL-SIZE must be a positive integer.
3751 Third argument SIDE nil (or `below') specifies that the new window shall
3752 be located below WINDOW. SIDE `above' means the new window shall be
3753 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3754 lines of the new window including space reserved for the mode and/or
3755 header line.
3757 SIDE t (or `right') specifies that the new window shall be located on
3758 the right side of WINDOW. SIDE `left' means the new window shall be
3759 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3760 number of columns of the new window including space reserved for fringes
3761 and the scrollbar or a divider column.
3763 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3764 according to the SIDE argument.
3766 The new total and normal sizes of all involved windows must have been
3767 set correctly. See the code of `split-window' for how this is done. */)
3768 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3770 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3771 parent window or an internal window we have to install as OLD's new
3772 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3773 provided OLD is a leaf window, or to the frame's selected window.
3774 NEW (*n) is the new window created with some parameters taken from
3775 REFERENCE (*r). */
3776 register Lisp_Object new, frame, reference;
3777 register struct window *o, *p, *n, *r;
3778 struct frame *f;
3779 bool horflag
3780 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3781 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3782 int combination_limit = 0;
3784 CHECK_WINDOW (old);
3785 o = XWINDOW (old);
3786 frame = WINDOW_FRAME (o);
3787 f = XFRAME (frame);
3789 CHECK_NUMBER (total_size);
3791 /* Set combination_limit to 1 if we have to make a new parent window.
3792 We do that if either `window-combination-limit' is t, or OLD has no
3793 parent, or OLD is ortho-combined. */
3794 combination_limit =
3795 EQ (Vwindow_combination_limit, Qt)
3796 || NILP (o->parent)
3797 || (horflag
3798 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
3799 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
3801 /* We need a live reference window to initialize some parameters. */
3802 if (WINDOW_LIVE_P (old))
3803 /* OLD is live, use it as reference window. */
3804 reference = old;
3805 else
3806 /* Use the frame's selected window as reference window. */
3807 reference = FRAME_SELECTED_WINDOW (f);
3808 r = XWINDOW (reference);
3810 /* The following bugs are caught by `split-window'. */
3811 if (MINI_WINDOW_P (o))
3812 error ("Attempt to split minibuffer window");
3813 else if (XINT (total_size) < (horflag ? 2 : 1))
3814 error ("Size of new window too small (after split)");
3815 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3816 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3817 proportionally. */
3819 p = XWINDOW (o->parent);
3820 /* Temporarily pretend we split the parent window. */
3821 wset_new_total
3822 (p, make_number ((horflag ? p->total_cols : p->total_lines)
3823 - XINT (total_size)));
3824 if (!window_resize_check (p, horflag))
3825 error ("Window sizes don't fit");
3826 else
3827 /* Undo the temporary pretension. */
3828 wset_new_total (p, make_number
3829 (horflag ? p->total_cols : p->total_lines));
3831 else
3833 if (!window_resize_check (o, horflag))
3834 error ("Resizing old window failed");
3835 else if (XINT (total_size) + XINT (o->new_total)
3836 != (horflag ? o->total_cols : o->total_lines))
3837 error ("Sum of sizes of old and new window don't fit");
3840 /* This is our point of no return. */
3841 if (combination_limit)
3843 /* Save the old value of o->normal_cols/lines. It gets corrupted
3844 by make_parent_window and we need it below for assigning it to
3845 p->new_normal. */
3846 Lisp_Object new_normal
3847 = horflag ? o->normal_cols : o->normal_lines;
3849 make_parent_window (old, horflag);
3850 p = XWINDOW (o->parent);
3851 if (EQ (Vwindow_combination_limit, Qt))
3852 /* Store t in the new parent's combination_limit slot to avoid
3853 that its children get merged into another window. */
3854 wset_combination_limit (p, Qt);
3855 /* These get applied below. */
3856 wset_new_total (p, make_number
3857 (horflag ? o->total_cols : o->total_lines));
3858 wset_new_normal (p, new_normal);
3860 else
3861 p = XWINDOW (o->parent);
3863 windows_or_buffers_changed++;
3864 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3865 new = make_window ();
3866 n = XWINDOW (new);
3867 wset_frame (n, frame);
3868 wset_parent (n, o->parent);
3870 if (EQ (side, Qabove) || EQ (side, Qleft))
3872 wset_prev (n, o->prev);
3873 if (NILP (n->prev))
3874 wset_combination (p, horflag, new);
3875 else
3876 wset_next (XWINDOW (n->prev), new);
3877 wset_next (n, old);
3878 wset_prev (o, new);
3880 else
3882 wset_next (n, o->next);
3883 if (!NILP (n->next))
3884 wset_prev (XWINDOW (n->next), new);
3885 wset_prev (n, old);
3886 wset_next (o, new);
3889 n->window_end_valid = 0;
3890 n->last_cursor_vpos = 0;
3892 /* Get special geometry settings from reference window. */
3893 n->left_margin_cols = r->left_margin_cols;
3894 n->right_margin_cols = r->right_margin_cols;
3895 n->left_fringe_width = r->left_fringe_width;
3896 n->right_fringe_width = r->right_fringe_width;
3897 n->fringes_outside_margins = r->fringes_outside_margins;
3898 n->scroll_bar_width = r->scroll_bar_width;
3899 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
3901 /* Directly assign orthogonal coordinates and sizes. */
3902 if (horflag)
3904 n->top_line = o->top_line;
3905 n->total_lines = o->total_lines;
3907 else
3909 n->left_col = o->left_col;
3910 n->total_cols = o->total_cols;
3913 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3914 get them ready here. */
3915 wset_new_total (n, total_size);
3916 wset_new_normal (n, normal_size);
3918 block_input ();
3919 window_resize_apply (p, horflag);
3920 adjust_frame_glyphs (f);
3921 /* Set buffer of NEW to buffer of reference window. Don't run
3922 any hooks. */
3923 set_window_buffer (new, r->contents, 0, 1);
3924 unblock_input ();
3926 /* Maybe we should run the scroll functions in Elisp (which already
3927 runs the configuration change hook). */
3928 if (! NILP (Vwindow_scroll_functions))
3929 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3930 Fmarker_position (n->start));
3931 /* Return NEW. */
3932 return new;
3936 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3937 doc: /* Remove WINDOW from its frame.
3938 WINDOW defaults to the selected window. Return nil.
3939 Signal an error when WINDOW is the only window on its frame. */)
3940 (register Lisp_Object window)
3942 register Lisp_Object parent, sibling, frame, root;
3943 struct window *w, *p, *s, *r;
3944 struct frame *f;
3945 bool horflag, before_sibling = 0;
3947 w = decode_any_window (window);
3948 XSETWINDOW (window, w);
3949 if (NILP (w->contents))
3950 /* It's a no-op to delete an already deleted window. */
3951 return Qnil;
3953 parent = w->parent;
3954 if (NILP (parent))
3955 /* Never delete a minibuffer or frame root window. */
3956 error ("Attempt to delete minibuffer or sole ordinary window");
3957 else if (NILP (w->prev) && NILP (w->next))
3958 /* Rather bow out here, this case should be handled on the Elisp
3959 level. */
3960 error ("Attempt to delete sole window of parent");
3962 p = XWINDOW (parent);
3963 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
3965 frame = WINDOW_FRAME (w);
3966 f = XFRAME (frame);
3968 root = FRAME_ROOT_WINDOW (f);
3969 r = XWINDOW (root);
3971 /* Unlink WINDOW from window tree. */
3972 if (NILP (w->prev))
3973 /* Get SIBLING below (on the right of) WINDOW. */
3975 /* before_sibling 1 means WINDOW is the first child of its
3976 parent and thus before the sibling. */
3977 before_sibling = 1;
3978 sibling = w->next;
3979 s = XWINDOW (sibling);
3980 wset_prev (s, Qnil);
3981 wset_combination (p, horflag, sibling);
3983 else
3984 /* Get SIBLING above (on the left of) WINDOW. */
3986 sibling = w->prev;
3987 s = XWINDOW (sibling);
3988 wset_next (s, w->next);
3989 if (!NILP (s->next))
3990 wset_prev (XWINDOW (s->next), sibling);
3993 if (window_resize_check (r, horflag)
3994 && (XINT (r->new_total)
3995 == (horflag ? r->total_cols : r->total_lines)))
3996 /* We can delete WINDOW now. */
3999 /* Block input. */
4000 block_input ();
4001 window_resize_apply (p, horflag);
4003 /* If this window is referred to by the dpyinfo's mouse
4004 highlight, invalidate that slot to be safe (Bug#9904). */
4005 if (!FRAME_INITIAL_P (f))
4007 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4009 if (EQ (hlinfo->mouse_face_window, window))
4010 hlinfo->mouse_face_window = Qnil;
4013 windows_or_buffers_changed++;
4014 Vwindow_list = Qnil;
4015 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4017 wset_next (w, Qnil); /* Don't delete w->next too. */
4018 free_window_matrices (w);
4020 if (WINDOWP (w->contents))
4022 delete_all_child_windows (w->contents);
4023 wset_combination (w, 0, Qnil);
4025 else
4027 unshow_buffer (w);
4028 unchain_marker (XMARKER (w->pointm));
4029 unchain_marker (XMARKER (w->start));
4030 wset_buffer (w, Qnil);
4033 if (NILP (s->prev) && NILP (s->next))
4034 /* A matrjoshka where SIBLING has become the only child of
4035 PARENT. */
4037 /* Put SIBLING into PARENT's place. */
4038 replace_window (parent, sibling, 0);
4039 /* Have SIBLING inherit the following three slot values from
4040 PARENT (the combination_limit slot is not inherited). */
4041 wset_normal_cols (s, p->normal_cols);
4042 wset_normal_lines (s, p->normal_lines);
4043 /* Mark PARENT as deleted. */
4044 wset_combination (p, 0, Qnil);
4045 /* Try to merge SIBLING into its new parent. */
4046 recombine_windows (sibling);
4049 adjust_frame_glyphs (f);
4051 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4052 /* We deleted the frame's selected window. */
4054 /* Use the frame's first window as fallback ... */
4055 Lisp_Object new_selected_window = Fframe_first_window (frame);
4056 /* ... but preferably use its most recently used window. */
4057 Lisp_Object mru_window;
4059 /* `get-mru-window' might fail for some reason so play it safe
4060 - promote the first window _without recording it_ first. */
4061 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4062 Fselect_window (new_selected_window, Qt);
4063 else
4064 fset_selected_window (f, new_selected_window);
4066 unblock_input ();
4068 /* Now look whether `get-mru-window' gets us something. */
4069 mru_window = call1 (Qget_mru_window, frame);
4070 if (WINDOW_LIVE_P (mru_window)
4071 && EQ (XWINDOW (mru_window)->frame, frame))
4072 new_selected_window = mru_window;
4074 /* If all ended up well, we now promote the mru window. */
4075 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4076 Fselect_window (new_selected_window, Qnil);
4077 else
4078 fset_selected_window (f, new_selected_window);
4080 else
4081 unblock_input ();
4083 /* Must be run by the caller:
4084 run_window_configuration_change_hook (f); */
4086 else
4087 /* We failed: Relink WINDOW into window tree. */
4089 if (before_sibling)
4091 wset_prev (s, window);
4092 wset_combination (p, horflag, window);
4094 else
4096 wset_next (s, window);
4097 if (!NILP (w->next))
4098 wset_prev (XWINDOW (w->next), window);
4100 error ("Deletion failed");
4103 return Qnil;
4106 /***********************************************************************
4107 Resizing Mini-Windows
4108 ***********************************************************************/
4110 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4111 can. */
4112 void
4113 grow_mini_window (struct window *w, int delta)
4115 struct frame *f = XFRAME (w->frame);
4116 struct window *r;
4117 Lisp_Object root, value;
4119 eassert (MINI_WINDOW_P (w));
4120 eassert (delta >= 0);
4122 root = FRAME_ROOT_WINDOW (f);
4123 r = XWINDOW (root);
4124 value = call2 (Qwindow_resize_root_window_vertically,
4125 root, make_number (- delta));
4126 if (INTEGERP (value) && window_resize_check (r, 0))
4128 block_input ();
4129 window_resize_apply (r, 0);
4131 /* Grow the mini-window. */
4132 w->top_line = r->top_line + r->total_lines;
4133 w->total_lines -= XINT (value);
4134 /* Enforce full redisplay. FIXME: make it more selective. */
4135 windows_or_buffers_changed++;
4136 adjust_frame_glyphs (f);
4137 unblock_input ();
4142 /* Shrink mini-window W. */
4143 void
4144 shrink_mini_window (struct window *w)
4146 struct frame *f = XFRAME (w->frame);
4147 struct window *r;
4148 Lisp_Object root, value;
4149 EMACS_INT size;
4151 eassert (MINI_WINDOW_P (w));
4153 size = w->total_lines;
4154 if (size > 1)
4156 root = FRAME_ROOT_WINDOW (f);
4157 r = XWINDOW (root);
4158 value = call2 (Qwindow_resize_root_window_vertically,
4159 root, make_number (size - 1));
4160 if (INTEGERP (value) && window_resize_check (r, 0))
4162 block_input ();
4163 window_resize_apply (r, 0);
4165 /* Shrink the mini-window. */
4166 w->top_line = r->top_line + r->total_lines;
4167 w->total_lines = 1;
4168 /* Enforce full redisplay. FIXME: make it more selective. */
4169 windows_or_buffers_changed++;
4170 adjust_frame_glyphs (f);
4171 unblock_input ();
4173 /* If the above failed for whatever strange reason we must make a
4174 one window frame here. The same routine will be needed when
4175 shrinking the frame (and probably when making the initial
4176 *scratch* window). For the moment leave things as they are. */
4180 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4181 doc: /* Resize minibuffer window WINDOW. */)
4182 (Lisp_Object window)
4184 struct window *w = XWINDOW (window);
4185 struct window *r;
4186 struct frame *f;
4187 int height;
4189 CHECK_WINDOW (window);
4190 f = XFRAME (w->frame);
4192 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4193 error ("Not a valid minibuffer window");
4194 else if (FRAME_MINIBUF_ONLY_P (f))
4195 error ("Cannot resize a minibuffer-only frame");
4197 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4198 height = r->total_lines + w->total_lines;
4199 if (window_resize_check (r, 0)
4200 && XINT (w->new_total) > 0
4201 && height == XINT (r->new_total) + XINT (w->new_total))
4203 block_input ();
4204 window_resize_apply (r, 0);
4206 w->total_lines = XFASTINT (w->new_total);
4207 w->top_line = r->top_line + r->total_lines;
4209 windows_or_buffers_changed++;
4210 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4211 adjust_frame_glyphs (f);
4212 unblock_input ();
4214 run_window_configuration_change_hook (f);
4215 return Qt;
4217 else error ("Failed to resize minibuffer window");
4220 /* Mark window cursors off for all windows in the window tree rooted
4221 at W by setting their phys_cursor_on_p flag to zero. Called from
4222 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4223 the frame are cleared. */
4225 void
4226 mark_window_cursors_off (struct window *w)
4228 while (w)
4230 if (WINDOWP (w->contents))
4231 mark_window_cursors_off (XWINDOW (w->contents));
4232 else
4233 w->phys_cursor_on_p = 0;
4235 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4240 /* Return number of lines of text (not counting mode lines) in W. */
4243 window_internal_height (struct window *w)
4245 int ht = w->total_lines;
4247 if (!MINI_WINDOW_P (w))
4249 if (!NILP (w->parent)
4250 || WINDOWP (w->contents)
4251 || !NILP (w->next)
4252 || !NILP (w->prev)
4253 || WINDOW_WANTS_MODELINE_P (w))
4254 --ht;
4256 if (WINDOW_WANTS_HEADER_LINE_P (w))
4257 --ht;
4260 return ht;
4263 /************************************************************************
4264 Window Scrolling
4265 ***********************************************************************/
4267 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4268 N screen-fulls, which is defined as the height of the window minus
4269 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4270 instead. Negative values of N mean scroll down. NOERROR non-zero
4271 means don't signal an error if we try to move over BEGV or ZV,
4272 respectively. */
4274 static void
4275 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4277 immediate_quit = 1;
4278 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4280 /* If we must, use the pixel-based version which is much slower than
4281 the line-based one but can handle varying line heights. */
4282 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4283 window_scroll_pixel_based (window, n, whole, noerror);
4284 else
4285 window_scroll_line_based (window, n, whole, noerror);
4287 immediate_quit = 0;
4291 /* Implementation of window_scroll that works based on pixel line
4292 heights. See the comment of window_scroll for parameter
4293 descriptions. */
4295 static void
4296 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4298 struct it it;
4299 struct window *w = XWINDOW (window);
4300 struct text_pos start;
4301 int this_scroll_margin;
4302 /* True if we fiddled the window vscroll field without really scrolling. */
4303 int vscrolled = 0;
4304 int x, y, rtop, rbot, rowh, vpos;
4305 void *itdata = NULL;
4306 int window_total_lines;
4307 int frame_line_height = default_line_pixel_height (w);
4309 SET_TEXT_POS_FROM_MARKER (start, w->start);
4310 /* Scrolling a minibuffer window via scroll bar when the echo area
4311 shows long text sometimes resets the minibuffer contents behind
4312 our backs. */
4313 if (CHARPOS (start) > ZV)
4314 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4316 /* If PT is not visible in WINDOW, move back one half of
4317 the screen. Allow PT to be partially visible, otherwise
4318 something like (scroll-down 1) with PT in the line before
4319 the partially visible one would recenter. */
4321 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4323 itdata = bidi_shelve_cache ();
4324 /* Move backward half the height of the window. Performance note:
4325 vmotion used here is about 10% faster, but would give wrong
4326 results for variable height lines. */
4327 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4328 it.current_y = it.last_visible_y;
4329 move_it_vertically_backward (&it, window_box_height (w) / 2);
4331 /* The function move_iterator_vertically may move over more than
4332 the specified y-distance. If it->w is small, e.g. a
4333 mini-buffer window, we may end up in front of the window's
4334 display area. This is the case when Start displaying at the
4335 start of the line containing PT in this case. */
4336 if (it.current_y <= 0)
4338 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4339 move_it_vertically_backward (&it, 0);
4340 it.current_y = 0;
4343 start = it.current.pos;
4344 bidi_unshelve_cache (itdata, 0);
4346 else if (auto_window_vscroll_p)
4348 if (rtop || rbot) /* partially visible */
4350 int px;
4351 int dy = frame_line_height;
4352 if (whole)
4353 dy = max ((window_box_height (w)
4354 - next_screen_context_lines * dy),
4355 dy);
4356 dy *= n;
4358 if (n < 0)
4360 /* Only vscroll backwards if already vscrolled forwards. */
4361 if (w->vscroll < 0 && rtop > 0)
4363 px = max (0, -w->vscroll - min (rtop, -dy));
4364 Fset_window_vscroll (window, make_number (px), Qt);
4365 return;
4368 if (n > 0)
4370 /* Do vscroll if already vscrolled or only display line. */
4371 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4373 px = max (0, -w->vscroll + min (rbot, dy));
4374 Fset_window_vscroll (window, make_number (px), Qt);
4375 return;
4378 /* Maybe modify window start instead of scrolling. */
4379 if (rbot > 0 || w->vscroll < 0)
4381 ptrdiff_t spos;
4383 Fset_window_vscroll (window, make_number (0), Qt);
4384 /* If there are other text lines above the current row,
4385 move window start to current row. Else to next row. */
4386 if (rbot > 0)
4387 spos = XINT (Fline_beginning_position (Qnil));
4388 else
4389 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4390 set_marker_restricted (w->start, make_number (spos),
4391 w->contents);
4392 w->start_at_line_beg = 1;
4393 w->update_mode_line = 1;
4394 /* Set force_start so that redisplay_window will run the
4395 window-scroll-functions. */
4396 w->force_start = 1;
4397 return;
4401 /* Cancel previous vscroll. */
4402 Fset_window_vscroll (window, make_number (0), Qt);
4405 itdata = bidi_shelve_cache ();
4406 /* If scroll_preserve_screen_position is non-nil, we try to set
4407 point in the same window line as it is now, so get that line. */
4408 if (!NILP (Vscroll_preserve_screen_position))
4410 /* We preserve the goal pixel coordinate across consecutive
4411 calls to scroll-up, scroll-down and other commands that
4412 have the `scroll-command' property. This avoids the
4413 possibility of point becoming "stuck" on a tall line when
4414 scrolling by one line. */
4415 if (window_scroll_pixel_based_preserve_y < 0
4416 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4417 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4419 start_display (&it, w, start);
4420 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4421 window_scroll_pixel_based_preserve_y = it.current_y;
4422 window_scroll_pixel_based_preserve_x = it.current_x;
4425 else
4426 window_scroll_pixel_based_preserve_y
4427 = window_scroll_pixel_based_preserve_x = -1;
4429 /* Move iterator it from start the specified distance forward or
4430 backward. The result is the new window start. */
4431 start_display (&it, w, start);
4432 if (whole)
4434 ptrdiff_t start_pos = IT_CHARPOS (it);
4435 int dy = frame_line_height;
4436 dy = max ((window_box_height (w)
4437 - next_screen_context_lines * dy),
4438 dy) * n;
4440 /* Note that move_it_vertically always moves the iterator to the
4441 start of a line. So, if the last line doesn't have a newline,
4442 we would end up at the start of the line ending at ZV. */
4443 if (dy <= 0)
4445 move_it_vertically_backward (&it, -dy);
4446 /* Ensure we actually do move, e.g. in case we are currently
4447 looking at an image that is taller that the window height. */
4448 while (start_pos == IT_CHARPOS (it)
4449 && start_pos > BEGV)
4450 move_it_by_lines (&it, -1);
4452 else if (dy > 0)
4454 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4455 MOVE_TO_POS | MOVE_TO_Y);
4456 /* Ensure we actually do move, e.g. in case we are currently
4457 looking at an image that is taller that the window height. */
4458 while (start_pos == IT_CHARPOS (it)
4459 && start_pos < ZV)
4460 move_it_by_lines (&it, 1);
4463 else
4464 move_it_by_lines (&it, n);
4466 /* We failed if we find ZV is already on the screen (scrolling up,
4467 means there's nothing past the end), or if we can't start any
4468 earlier (scrolling down, means there's nothing past the top). */
4469 if ((n > 0 && IT_CHARPOS (it) == ZV)
4470 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4472 if (IT_CHARPOS (it) == ZV)
4474 if (it.current_y < it.last_visible_y
4475 && (it.current_y + it.max_ascent + it.max_descent
4476 > it.last_visible_y))
4478 /* The last line was only partially visible, make it fully
4479 visible. */
4480 w->vscroll = (it.last_visible_y
4481 - it.current_y + it.max_ascent + it.max_descent);
4482 adjust_frame_glyphs (it.f);
4484 else
4486 bidi_unshelve_cache (itdata, 0);
4487 if (noerror)
4488 return;
4489 else if (n < 0) /* could happen with empty buffers */
4490 xsignal0 (Qbeginning_of_buffer);
4491 else
4492 xsignal0 (Qend_of_buffer);
4495 else
4497 if (w->vscroll != 0)
4498 /* The first line was only partially visible, make it fully
4499 visible. */
4500 w->vscroll = 0;
4501 else
4503 bidi_unshelve_cache (itdata, 0);
4504 if (noerror)
4505 return;
4506 else
4507 xsignal0 (Qbeginning_of_buffer);
4511 /* If control gets here, then we vscrolled. */
4513 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
4515 /* Don't try to change the window start below. */
4516 vscrolled = 1;
4519 if (! vscrolled)
4521 ptrdiff_t pos = IT_CHARPOS (it);
4522 ptrdiff_t bytepos;
4524 /* If in the middle of a multi-glyph character move forward to
4525 the next character. */
4526 if (in_display_vector_p (&it))
4528 ++pos;
4529 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4532 /* Set the window start, and set up the window for redisplay. */
4533 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
4534 IT_BYTEPOS (it));
4535 bytepos = marker_byte_position (w->start);
4536 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4537 w->update_mode_line = 1;
4538 /* Set force_start so that redisplay_window will run the
4539 window-scroll-functions. */
4540 w->force_start = 1;
4543 /* The rest of this function uses current_y in a nonstandard way,
4544 not including the height of the header line if any. */
4545 it.current_y = it.vpos = 0;
4547 /* Move PT out of scroll margins.
4548 This code wants current_y to be zero at the window start position
4549 even if there is a header line. */
4550 window_total_lines
4551 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
4552 this_scroll_margin = max (0, scroll_margin);
4553 this_scroll_margin
4554 = min (this_scroll_margin, window_total_lines / 4);
4555 this_scroll_margin *= frame_line_height;
4557 if (n > 0)
4559 /* We moved the window start towards ZV, so PT may be now
4560 in the scroll margin at the top. */
4561 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4562 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4563 && (NILP (Vscroll_preserve_screen_position)
4564 || EQ (Vscroll_preserve_screen_position, Qt)))
4565 /* We found PT at a legitimate height. Leave it alone. */
4567 else if (window_scroll_pixel_based_preserve_y >= 0)
4569 /* If we have a header line, take account of it.
4570 This is necessary because we set it.current_y to 0, above. */
4571 move_it_to (&it, -1,
4572 window_scroll_pixel_based_preserve_x,
4573 window_scroll_pixel_based_preserve_y
4574 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4575 -1, MOVE_TO_Y | MOVE_TO_X);
4576 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4578 else
4580 while (it.current_y < this_scroll_margin)
4582 int prev = it.current_y;
4583 move_it_by_lines (&it, 1);
4584 if (prev == it.current_y)
4585 break;
4587 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4590 else if (n < 0)
4592 ptrdiff_t charpos, bytepos;
4593 int partial_p;
4595 /* Save our position, for the
4596 window_scroll_pixel_based_preserve_y case. */
4597 charpos = IT_CHARPOS (it);
4598 bytepos = IT_BYTEPOS (it);
4600 /* We moved the window start towards BEGV, so PT may be now
4601 in the scroll margin at the bottom. */
4602 move_it_to (&it, PT, -1,
4603 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4604 - this_scroll_margin - 1),
4606 MOVE_TO_POS | MOVE_TO_Y);
4608 /* Save our position, in case it's correct. */
4609 charpos = IT_CHARPOS (it);
4610 bytepos = IT_BYTEPOS (it);
4612 /* See if point is on a partially visible line at the end. */
4613 if (it.what == IT_EOB)
4614 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4615 else
4617 move_it_by_lines (&it, 1);
4618 partial_p = it.current_y > it.last_visible_y;
4621 if (charpos == PT && !partial_p
4622 && (NILP (Vscroll_preserve_screen_position)
4623 || EQ (Vscroll_preserve_screen_position, Qt)))
4624 /* We found PT before we found the display margin, so PT is ok. */
4626 else if (window_scroll_pixel_based_preserve_y >= 0)
4628 SET_TEXT_POS_FROM_MARKER (start, w->start);
4629 start_display (&it, w, start);
4630 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4631 here because we called start_display again and did not
4632 alter it.current_y this time. */
4633 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4634 window_scroll_pixel_based_preserve_y, -1,
4635 MOVE_TO_Y | MOVE_TO_X);
4636 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4638 else
4640 if (partial_p)
4641 /* The last line was only partially visible, so back up two
4642 lines to make sure we're on a fully visible line. */
4644 move_it_by_lines (&it, -2);
4645 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4647 else
4648 /* No, the position we saved is OK, so use it. */
4649 SET_PT_BOTH (charpos, bytepos);
4652 bidi_unshelve_cache (itdata, 0);
4656 /* Implementation of window_scroll that works based on screen lines.
4657 See the comment of window_scroll for parameter descriptions. */
4659 static void
4660 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
4662 register struct window *w = XWINDOW (window);
4663 /* Fvertical_motion enters redisplay, which can trigger
4664 fontification, which in turn can modify buffer text (e.g., if the
4665 fontification functions replace escape sequences with faces, as
4666 in `grep-mode-font-lock-keywords'). So we use a marker to record
4667 the old point position, to prevent crashes in SET_PT_BOTH. */
4668 Lisp_Object opoint_marker = Fpoint_marker ();
4669 register ptrdiff_t pos, pos_byte;
4670 register int ht = window_internal_height (w);
4671 register Lisp_Object tem;
4672 bool lose;
4673 Lisp_Object bolp;
4674 ptrdiff_t startpos = marker_position (w->start);
4675 ptrdiff_t startbyte = marker_byte_position (w->start);
4676 Lisp_Object original_pos = Qnil;
4678 /* If scrolling screen-fulls, compute the number of lines to
4679 scroll from the window's height. */
4680 if (whole)
4681 n *= max (1, ht - next_screen_context_lines);
4683 if (!NILP (Vscroll_preserve_screen_position))
4685 if (window_scroll_preserve_vpos <= 0
4686 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4687 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4689 struct position posit
4690 = *compute_motion (startpos, startbyte, 0, 0, 0,
4691 PT, ht, 0, -1, w->hscroll, 0, w);
4692 window_scroll_preserve_vpos = posit.vpos;
4693 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
4696 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4697 make_number (window_scroll_preserve_vpos));
4700 XSETFASTINT (tem, PT);
4701 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4703 if (NILP (tem))
4705 Fvertical_motion (make_number (- (ht / 2)), window);
4706 startpos = PT;
4707 startbyte = PT_BYTE;
4710 SET_PT_BOTH (startpos, startbyte);
4711 lose = n < 0 && PT == BEGV;
4712 Fvertical_motion (make_number (n), window);
4713 pos = PT;
4714 pos_byte = PT_BYTE;
4715 bolp = Fbolp ();
4716 SET_PT_BOTH (marker_position (opoint_marker),
4717 marker_byte_position (opoint_marker));
4719 if (lose)
4721 if (noerror)
4722 return;
4723 else
4724 xsignal0 (Qbeginning_of_buffer);
4727 if (pos < ZV)
4729 /* Don't use a scroll margin that is negative or too large. */
4730 int this_scroll_margin =
4731 max (0, min (scroll_margin, w->total_lines / 4));
4733 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
4734 w->start_at_line_beg = !NILP (bolp);
4735 w->update_mode_line = 1;
4736 /* Set force_start so that redisplay_window will run
4737 the window-scroll-functions. */
4738 w->force_start = 1;
4740 if (!NILP (Vscroll_preserve_screen_position)
4741 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4743 SET_PT_BOTH (pos, pos_byte);
4744 Fvertical_motion (original_pos, window);
4746 /* If we scrolled forward, put point enough lines down
4747 that it is outside the scroll margin. */
4748 else if (n > 0)
4750 int top_margin;
4752 if (this_scroll_margin > 0)
4754 SET_PT_BOTH (pos, pos_byte);
4755 Fvertical_motion (make_number (this_scroll_margin), window);
4756 top_margin = PT;
4758 else
4759 top_margin = pos;
4761 if (top_margin <= marker_position (opoint_marker))
4762 SET_PT_BOTH (marker_position (opoint_marker),
4763 marker_byte_position (opoint_marker));
4764 else if (!NILP (Vscroll_preserve_screen_position))
4766 SET_PT_BOTH (pos, pos_byte);
4767 Fvertical_motion (original_pos, window);
4769 else
4770 SET_PT (top_margin);
4772 else if (n < 0)
4774 int bottom_margin;
4776 /* If we scrolled backward, put point near the end of the window
4777 but not within the scroll margin. */
4778 SET_PT_BOTH (pos, pos_byte);
4779 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4780 if (XFASTINT (tem) == ht - this_scroll_margin)
4781 bottom_margin = PT;
4782 else
4783 bottom_margin = PT + 1;
4785 if (bottom_margin > marker_position (opoint_marker))
4786 SET_PT_BOTH (marker_position (opoint_marker),
4787 marker_byte_position (opoint_marker));
4788 else
4790 if (!NILP (Vscroll_preserve_screen_position))
4792 SET_PT_BOTH (pos, pos_byte);
4793 Fvertical_motion (original_pos, window);
4795 else
4796 Fvertical_motion (make_number (-1), window);
4800 else
4802 if (noerror)
4803 return;
4804 else
4805 xsignal0 (Qend_of_buffer);
4810 /* Scroll selected_window up or down. If N is nil, scroll a
4811 screen-full which is defined as the height of the window minus
4812 next_screen_context_lines. If N is the symbol `-', scroll.
4813 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4814 up. This is the guts of Fscroll_up and Fscroll_down. */
4816 static void
4817 scroll_command (Lisp_Object n, int direction)
4819 ptrdiff_t count = SPECPDL_INDEX ();
4821 eassert (eabs (direction) == 1);
4823 /* If selected window's buffer isn't current, make it current for
4824 the moment. But don't screw up if window_scroll gets an error. */
4825 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
4827 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4828 Fset_buffer (XWINDOW (selected_window)->contents);
4830 /* Make redisplay consider other windows than just selected_window. */
4831 ++windows_or_buffers_changed;
4834 if (NILP (n))
4835 window_scroll (selected_window, direction, 1, 0);
4836 else if (EQ (n, Qminus))
4837 window_scroll (selected_window, -direction, 1, 0);
4838 else
4840 n = Fprefix_numeric_value (n);
4841 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4844 unbind_to (count, Qnil);
4847 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4848 doc: /* Scroll text of selected window upward ARG lines.
4849 If ARG is omitted or nil, scroll upward by a near full screen.
4850 A near full screen is `next-screen-context-lines' less than a full screen.
4851 Negative ARG means scroll downward.
4852 If ARG is the atom `-', scroll downward by nearly full screen.
4853 When calling from a program, supply as argument a number, nil, or `-'. */)
4854 (Lisp_Object arg)
4856 scroll_command (arg, 1);
4857 return Qnil;
4860 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4861 doc: /* Scroll text of selected window down ARG lines.
4862 If ARG is omitted or nil, scroll down by a near full screen.
4863 A near full screen is `next-screen-context-lines' less than a full screen.
4864 Negative ARG means scroll upward.
4865 If ARG is the atom `-', scroll upward by nearly full screen.
4866 When calling from a program, supply as argument a number, nil, or `-'. */)
4867 (Lisp_Object arg)
4869 scroll_command (arg, -1);
4870 return Qnil;
4873 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4874 doc: /* Return the other window for \"other window scroll\" commands.
4875 If `other-window-scroll-buffer' is non-nil, a window
4876 showing that buffer is used.
4877 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4878 specifies the window. This takes precedence over
4879 `other-window-scroll-buffer'. */)
4880 (void)
4882 Lisp_Object window;
4884 if (MINI_WINDOW_P (XWINDOW (selected_window))
4885 && !NILP (Vminibuf_scroll_window))
4886 window = Vminibuf_scroll_window;
4887 /* If buffer is specified, scroll that buffer. */
4888 else if (!NILP (Vother_window_scroll_buffer))
4890 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4891 if (NILP (window))
4892 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4894 else
4896 /* Nothing specified; look for a neighboring window on the same
4897 frame. */
4898 window = Fnext_window (selected_window, Qnil, Qnil);
4900 if (EQ (window, selected_window))
4901 /* That didn't get us anywhere; look for a window on another
4902 visible frame. */
4904 window = Fnext_window (window, Qnil, Qt);
4905 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4906 && ! EQ (window, selected_window));
4909 CHECK_LIVE_WINDOW (window);
4911 if (EQ (window, selected_window))
4912 error ("There is no other window");
4914 return window;
4917 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4918 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4919 A near full screen is `next-screen-context-lines' less than a full screen.
4920 The next window is the one below the current one; or the one at the top
4921 if the current one is at the bottom. Negative ARG means scroll downward.
4922 If ARG is the atom `-', scroll downward by nearly full screen.
4923 When calling from a program, supply as argument a number, nil, or `-'.
4925 If `other-window-scroll-buffer' is non-nil, scroll the window
4926 showing that buffer, popping the buffer up if necessary.
4927 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4928 specifies the window to scroll. This takes precedence over
4929 `other-window-scroll-buffer'. */)
4930 (Lisp_Object arg)
4932 Lisp_Object window;
4933 struct window *w;
4934 ptrdiff_t count = SPECPDL_INDEX ();
4936 window = Fother_window_for_scrolling ();
4937 w = XWINDOW (window);
4939 /* Don't screw up if window_scroll gets an error. */
4940 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4941 ++windows_or_buffers_changed;
4943 Fset_buffer (w->contents);
4944 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
4946 if (NILP (arg))
4947 window_scroll (window, 1, 1, 1);
4948 else if (EQ (arg, Qminus))
4949 window_scroll (window, -1, 1, 1);
4950 else
4952 if (CONSP (arg))
4953 arg = XCAR (arg);
4954 CHECK_NUMBER (arg);
4955 window_scroll (window, XINT (arg), 0, 1);
4958 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4959 unbind_to (count, Qnil);
4961 return Qnil;
4964 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
4965 doc: /* Scroll selected window display ARG columns left.
4966 Default for ARG is window width minus 2.
4967 Value is the total amount of leftward horizontal scrolling in
4968 effect after the change.
4969 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4970 lower bound for automatic scrolling, i.e. automatic scrolling
4971 will not scroll a window to a column less than the value returned
4972 by this function. This happens in an interactive call. */)
4973 (register Lisp_Object arg, Lisp_Object set_minimum)
4975 struct window *w = XWINDOW (selected_window);
4976 EMACS_INT requested_arg = (NILP (arg)
4977 ? window_body_cols (w) - 2
4978 : XINT (Fprefix_numeric_value (arg)));
4979 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
4981 if (!NILP (set_minimum))
4982 w->min_hscroll = w->hscroll;
4984 return result;
4987 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
4988 doc: /* Scroll selected window display ARG columns right.
4989 Default for ARG is window width minus 2.
4990 Value is the total amount of leftward horizontal scrolling in
4991 effect after the change.
4992 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4993 lower bound for automatic scrolling, i.e. automatic scrolling
4994 will not scroll a window to a column less than the value returned
4995 by this function. This happens in an interactive call. */)
4996 (register Lisp_Object arg, Lisp_Object set_minimum)
4998 struct window *w = XWINDOW (selected_window);
4999 EMACS_INT requested_arg = (NILP (arg)
5000 ? window_body_cols (w) - 2
5001 : XINT (Fprefix_numeric_value (arg)));
5002 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5004 if (!NILP (set_minimum))
5005 w->min_hscroll = w->hscroll;
5007 return result;
5010 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5011 doc: /* Return the window which was selected when entering the minibuffer.
5012 Returns nil, if selected window is not a minibuffer window. */)
5013 (void)
5015 if (minibuf_level > 0
5016 && MINI_WINDOW_P (XWINDOW (selected_window))
5017 && WINDOW_LIVE_P (minibuf_selected_window))
5018 return minibuf_selected_window;
5020 return Qnil;
5023 /* Value is the number of lines actually displayed in window W,
5024 as opposed to its height. */
5026 static int
5027 displayed_window_lines (struct window *w)
5029 struct it it;
5030 struct text_pos start;
5031 int height = window_box_height (w);
5032 struct buffer *old_buffer;
5033 int bottom_y;
5034 void *itdata = NULL;
5036 if (XBUFFER (w->contents) != current_buffer)
5038 old_buffer = current_buffer;
5039 set_buffer_internal (XBUFFER (w->contents));
5041 else
5042 old_buffer = NULL;
5044 /* In case W->start is out of the accessible range, do something
5045 reasonable. This happens in Info mode when Info-scroll-down
5046 calls (recenter -1) while W->start is 1. */
5047 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5049 itdata = bidi_shelve_cache ();
5050 start_display (&it, w, start);
5051 move_it_vertically (&it, height);
5052 bottom_y = line_bottom_y (&it);
5053 bidi_unshelve_cache (itdata, 0);
5055 /* rms: On a non-window display,
5056 the value of it.vpos at the bottom of the screen
5057 seems to be 1 larger than window_box_height (w).
5058 This kludge fixes a bug whereby (move-to-window-line -1)
5059 when ZV is on the last screen line
5060 moves to the previous screen line instead of the last one. */
5061 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5062 height++;
5064 /* Add in empty lines at the bottom of the window. */
5065 if (bottom_y < height)
5067 int uy = FRAME_LINE_HEIGHT (it.f);
5068 it.vpos += (height - bottom_y + uy - 1) / uy;
5071 if (old_buffer)
5072 set_buffer_internal (old_buffer);
5074 return it.vpos;
5078 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5079 doc: /* Center point in selected window and maybe redisplay frame.
5080 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5081 relative to the selected window. If ARG is negative, it counts up from the
5082 bottom of the window. (ARG should be less than the height of the window.)
5084 If ARG is omitted or nil, then recenter with point on the middle line of
5085 the selected window; if the variable `recenter-redisplay' is non-nil,
5086 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5087 is set to `grow-only', this resets the tool-bar's height to the minimum
5088 height needed); if `recenter-redisplay' has the special value `tty',
5089 then only tty frames are redrawn.
5091 Just C-u as prefix means put point in the center of the window
5092 and redisplay normally--don't erase and redraw the frame. */)
5093 (register Lisp_Object arg)
5095 struct window *w = XWINDOW (selected_window);
5096 struct buffer *buf = XBUFFER (w->contents);
5097 struct buffer *obuf = current_buffer;
5098 bool center_p = 0;
5099 ptrdiff_t charpos, bytepos;
5100 EMACS_INT iarg IF_LINT (= 0);
5101 int this_scroll_margin;
5103 /* If redisplay is suppressed due to an error, try again. */
5104 obuf->display_error_modiff = 0;
5106 if (NILP (arg))
5108 if (!NILP (Vrecenter_redisplay)
5109 && (!EQ (Vrecenter_redisplay, Qtty)
5110 || !NILP (Ftty_type (selected_frame))))
5112 ptrdiff_t i;
5114 /* Invalidate pixel data calculated for all compositions. */
5115 for (i = 0; i < n_compositions; i++)
5116 composition_table[i]->font = NULL;
5117 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5118 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5119 #endif
5120 Fredraw_frame (WINDOW_FRAME (w));
5121 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5124 center_p = 1;
5126 else if (CONSP (arg)) /* Just C-u. */
5127 center_p = 1;
5128 else
5130 arg = Fprefix_numeric_value (arg);
5131 CHECK_NUMBER (arg);
5132 iarg = XINT (arg);
5135 set_buffer_internal (buf);
5137 /* Do this after making BUF current
5138 in case scroll_margin is buffer-local. */
5139 this_scroll_margin =
5140 max (0, min (scroll_margin, w->total_lines / 4));
5142 /* Handle centering on a graphical frame specially. Such frames can
5143 have variable-height lines and centering point on the basis of
5144 line counts would lead to strange effects. */
5145 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5147 if (center_p)
5149 struct it it;
5150 struct text_pos pt;
5151 void *itdata = bidi_shelve_cache ();
5153 SET_TEXT_POS (pt, PT, PT_BYTE);
5154 start_display (&it, w, pt);
5155 move_it_vertically_backward (&it, window_box_height (w) / 2);
5156 charpos = IT_CHARPOS (it);
5157 bytepos = IT_BYTEPOS (it);
5158 bidi_unshelve_cache (itdata, 0);
5160 else if (iarg < 0)
5162 struct it it;
5163 struct text_pos pt;
5164 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5165 int extra_line_spacing;
5166 int h = window_box_height (w);
5167 void *itdata = bidi_shelve_cache ();
5169 iarg = - max (-iarg, this_scroll_margin);
5171 SET_TEXT_POS (pt, PT, PT_BYTE);
5172 start_display (&it, w, pt);
5174 /* Be sure we have the exact height of the full line containing PT. */
5175 move_it_by_lines (&it, 0);
5177 /* The amount of pixels we have to move back is the window
5178 height minus what's displayed in the line containing PT,
5179 and the lines below. */
5180 it.current_y = 0;
5181 it.vpos = 0;
5182 move_it_by_lines (&it, nlines);
5184 if (it.vpos == nlines)
5185 h -= it.current_y;
5186 else
5188 /* Last line has no newline */
5189 h -= line_bottom_y (&it);
5190 it.vpos++;
5193 /* Don't reserve space for extra line spacing of last line. */
5194 extra_line_spacing = it.max_extra_line_spacing;
5196 /* If we can't move down NLINES lines because we hit
5197 the end of the buffer, count in some empty lines. */
5198 if (it.vpos < nlines)
5200 nlines -= it.vpos;
5201 extra_line_spacing = it.extra_line_spacing;
5202 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5204 if (h <= 0)
5206 bidi_unshelve_cache (itdata, 0);
5207 return Qnil;
5210 /* Now find the new top line (starting position) of the window. */
5211 start_display (&it, w, pt);
5212 it.current_y = 0;
5213 move_it_vertically_backward (&it, h);
5215 /* If extra line spacing is present, we may move too far
5216 back. This causes the last line to be only partially
5217 visible (which triggers redisplay to recenter that line
5218 in the middle), so move forward.
5219 But ignore extra line spacing on last line, as it is not
5220 considered to be part of the visible height of the line.
5222 h += extra_line_spacing;
5223 while (-it.current_y > h)
5224 move_it_by_lines (&it, 1);
5226 charpos = IT_CHARPOS (it);
5227 bytepos = IT_BYTEPOS (it);
5229 bidi_unshelve_cache (itdata, 0);
5231 else
5233 struct position pos;
5235 iarg = max (iarg, this_scroll_margin);
5237 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5238 charpos = pos.bufpos;
5239 bytepos = pos.bytepos;
5242 else
5244 struct position pos;
5245 int ht = window_internal_height (w);
5247 if (center_p)
5248 iarg = ht / 2;
5249 else if (iarg < 0)
5250 iarg += ht;
5252 /* Don't let it get into the margin at either top or bottom. */
5253 iarg = clip_to_bounds (this_scroll_margin, iarg,
5254 ht - this_scroll_margin - 1);
5256 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5257 charpos = pos.bufpos;
5258 bytepos = pos.bytepos;
5261 /* Set the new window start. */
5262 set_marker_both (w->start, w->contents, charpos, bytepos);
5263 w->window_end_valid = 0;
5265 w->optional_new_start = 1;
5267 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5268 FETCH_BYTE (bytepos - 1) == '\n');
5270 set_buffer_internal (obuf);
5271 return Qnil;
5274 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5275 0, 1, 0,
5276 doc: /* Return the height in lines of the text display area of WINDOW.
5277 WINDOW must be a live window and defaults to the selected one.
5279 The returned height does not include the mode line, any header line,
5280 nor any partial-height lines at the bottom of the text area. */)
5281 (Lisp_Object window)
5283 struct window *w = decode_live_window (window);
5284 int pixel_height = window_box_height (w);
5285 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5286 return make_number (line_height);
5291 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5292 1, 1, "P",
5293 doc: /* Position point relative to window.
5294 ARG nil means position point at center of window.
5295 Else, ARG specifies vertical position within the window;
5296 zero means top of window, negative means relative to bottom of window. */)
5297 (Lisp_Object arg)
5299 struct window *w = XWINDOW (selected_window);
5300 int lines, start;
5301 Lisp_Object window;
5302 #if 0
5303 int this_scroll_margin;
5304 #endif
5306 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5307 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5308 when passed below to set_marker_both. */
5309 error ("move-to-window-line called from unrelated buffer");
5311 window = selected_window;
5312 start = marker_position (w->start);
5313 if (start < BEGV || start > ZV)
5315 int height = window_internal_height (w);
5316 Fvertical_motion (make_number (- (height / 2)), window);
5317 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5318 w->start_at_line_beg = !NILP (Fbolp ());
5319 w->force_start = 1;
5321 else
5322 Fgoto_char (w->start);
5324 lines = displayed_window_lines (w);
5326 #if 0
5327 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5328 #endif
5330 if (NILP (arg))
5331 XSETFASTINT (arg, lines / 2);
5332 else
5334 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5336 if (iarg < 0)
5337 iarg = iarg + lines;
5339 #if 0 /* This code would prevent move-to-window-line from moving point
5340 to a place inside the scroll margins (which would cause the
5341 next redisplay to scroll). I wrote this code, but then concluded
5342 it is probably better not to install it. However, it is here
5343 inside #if 0 so as not to lose it. -- rms. */
5345 /* Don't let it get into the margin at either top or bottom. */
5346 iarg = max (iarg, this_scroll_margin);
5347 iarg = min (iarg, lines - this_scroll_margin - 1);
5348 #endif
5350 arg = make_number (iarg);
5353 /* Skip past a partially visible first line. */
5354 if (w->vscroll)
5355 XSETINT (arg, XINT (arg) + 1);
5357 return Fvertical_motion (arg, window);
5362 /***********************************************************************
5363 Window Configuration
5364 ***********************************************************************/
5366 struct save_window_data
5368 struct vectorlike_header header;
5369 Lisp_Object selected_frame;
5370 Lisp_Object current_window;
5371 Lisp_Object current_buffer;
5372 Lisp_Object minibuf_scroll_window;
5373 Lisp_Object minibuf_selected_window;
5374 Lisp_Object root_window;
5375 Lisp_Object focus_frame;
5376 /* A vector, each of whose elements is a struct saved_window
5377 for one window. */
5378 Lisp_Object saved_windows;
5380 /* All fields above are traced by the GC.
5381 From `frame-cols' down, the fields are ignored by the GC. */
5383 int frame_cols, frame_lines, frame_menu_bar_lines;
5384 int frame_tool_bar_lines;
5387 /* This is saved as a Lisp_Vector */
5388 struct saved_window
5390 struct vectorlike_header header;
5392 Lisp_Object window, buffer, start, pointm, mark;
5393 Lisp_Object left_col, top_line, total_cols, total_lines;
5394 Lisp_Object normal_cols, normal_lines;
5395 Lisp_Object hscroll, min_hscroll;
5396 Lisp_Object parent, prev;
5397 Lisp_Object start_at_line_beg;
5398 Lisp_Object display_table;
5399 Lisp_Object left_margin_cols, right_margin_cols;
5400 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5401 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5402 Lisp_Object combination_limit, window_parameters;
5405 #define SAVED_WINDOW_N(swv,n) \
5406 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5408 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5409 doc: /* Return t if OBJECT is a window-configuration object. */)
5410 (Lisp_Object object)
5412 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5415 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5416 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5417 (Lisp_Object config)
5419 register struct save_window_data *data;
5420 struct Lisp_Vector *saved_windows;
5422 CHECK_WINDOW_CONFIGURATION (config);
5424 data = (struct save_window_data *) XVECTOR (config);
5425 saved_windows = XVECTOR (data->saved_windows);
5426 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5429 DEFUN ("set-window-configuration", Fset_window_configuration,
5430 Sset_window_configuration, 1, 1, 0,
5431 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5432 CONFIGURATION must be a value previously returned
5433 by `current-window-configuration' (which see).
5434 If CONFIGURATION was made from a frame that is now deleted,
5435 only frame-independent values can be restored. In this case,
5436 the return value is nil. Otherwise the value is t. */)
5437 (Lisp_Object configuration)
5439 register struct save_window_data *data;
5440 struct Lisp_Vector *saved_windows;
5441 Lisp_Object new_current_buffer;
5442 Lisp_Object frame;
5443 struct frame *f;
5444 ptrdiff_t old_point = -1;
5446 CHECK_WINDOW_CONFIGURATION (configuration);
5448 data = (struct save_window_data *) XVECTOR (configuration);
5449 saved_windows = XVECTOR (data->saved_windows);
5451 new_current_buffer = data->current_buffer;
5452 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5453 new_current_buffer = Qnil;
5454 else
5456 if (XBUFFER (new_current_buffer) == current_buffer)
5457 /* The code further down "preserves point" by saving here PT in
5458 old_point and then setting it later back into PT. When the
5459 current-selected-window and the final-selected-window both show
5460 the current buffer, this suffers from the problem that the
5461 current PT is the window-point of the current-selected-window,
5462 while the final PT is the point of the final-selected-window, so
5463 this copy from one PT to the other would end up moving the
5464 window-point of the final-selected-window to the window-point of
5465 the current-selected-window. So we have to be careful which
5466 point of the current-buffer we copy into old_point. */
5467 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5468 && WINDOWP (selected_window)
5469 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
5470 && !EQ (selected_window, data->current_window))
5471 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5472 else
5473 old_point = PT;
5474 else
5475 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5476 point in new_current_buffer as of the last time this buffer was
5477 used. This can be non-deterministic since it can be changed by
5478 things like jit-lock by mere temporary selection of some random
5479 window that happens to show this buffer.
5480 So if possible we want this arbitrary choice of "which point" to
5481 be the one from the to-be-selected-window so as to prevent this
5482 window's cursor from being copied from another window. */
5483 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5484 /* If current_window = selected_window, its point is in BUF_PT. */
5485 && !EQ (selected_window, data->current_window))
5486 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5487 else
5488 old_point = BUF_PT (XBUFFER (new_current_buffer));
5491 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5492 f = XFRAME (frame);
5494 /* If f is a dead frame, don't bother rebuilding its window tree.
5495 However, there is other stuff we should still try to do below. */
5496 if (FRAME_LIVE_P (f))
5498 Lisp_Object window;
5499 Lisp_Object dead_windows = Qnil;
5500 register Lisp_Object tem, par, pers;
5501 register struct window *w;
5502 register struct saved_window *p;
5503 struct window *root_window;
5504 struct window **leaf_windows;
5505 int n_leaf_windows;
5506 ptrdiff_t k;
5507 int i, n;
5509 /* If the frame has been resized since this window configuration was
5510 made, we change the frame to the size specified in the
5511 configuration, restore the configuration, and then resize it
5512 back. We keep track of the prevailing height in these variables. */
5513 int previous_frame_lines = FRAME_LINES (f);
5514 int previous_frame_cols = FRAME_COLS (f);
5515 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5516 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5518 /* Don't do this within the main loop below: This may call Lisp
5519 code and is thus potentially unsafe while input is blocked. */
5520 for (k = 0; k < saved_windows->header.size; k++)
5522 p = SAVED_WINDOW_N (saved_windows, k);
5523 window = p->window;
5524 w = XWINDOW (window);
5525 if (BUFFERP (w->contents)
5526 && !EQ (w->contents, p->buffer)
5527 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5528 /* If a window we restore gets another buffer, record the
5529 window's old buffer. */
5530 call1 (Qrecord_window_buffer, window);
5533 /* The mouse highlighting code could get screwed up
5534 if it runs during this. */
5535 block_input ();
5537 if (data->frame_lines != previous_frame_lines
5538 || data->frame_cols != previous_frame_cols)
5539 change_frame_size (f, data->frame_lines,
5540 data->frame_cols, 0, 0, 0);
5541 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5542 if (data->frame_menu_bar_lines
5543 != previous_frame_menu_bar_lines)
5544 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5545 make_number (0));
5546 #ifdef HAVE_WINDOW_SYSTEM
5547 if (data->frame_tool_bar_lines
5548 != previous_frame_tool_bar_lines)
5549 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5550 make_number (0));
5551 #endif
5552 #endif
5554 /* "Swap out" point from the selected window's buffer
5555 into the window itself. (Normally the pointm of the selected
5556 window holds garbage.) We do this now, before
5557 restoring the window contents, and prevent it from
5558 being done later on when we select a new window. */
5559 if (! NILP (XWINDOW (selected_window)->contents))
5561 w = XWINDOW (selected_window);
5562 set_marker_both (w->pointm,
5563 w->contents,
5564 BUF_PT (XBUFFER (w->contents)),
5565 BUF_PT_BYTE (XBUFFER (w->contents)));
5568 windows_or_buffers_changed++;
5569 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5571 /* Problem: Freeing all matrices and later allocating them again
5572 is a serious redisplay flickering problem. What we would
5573 really like to do is to free only those matrices not reused
5574 below. */
5575 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5576 leaf_windows = alloca (count_windows (root_window)
5577 * sizeof *leaf_windows);
5578 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5580 /* Kludge Alert!
5581 Mark all windows now on frame as "deleted".
5582 Restoring the new configuration "undeletes" any that are in it.
5584 Save their current buffers in their height fields, since we may
5585 need it later, if a buffer saved in the configuration is now
5586 dead. */
5587 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5589 for (k = 0; k < saved_windows->header.size; k++)
5591 p = SAVED_WINDOW_N (saved_windows, k);
5592 window = p->window;
5593 w = XWINDOW (window);
5594 wset_next (w, Qnil);
5596 if (!NILP (p->parent))
5597 wset_parent
5598 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
5599 else
5600 wset_parent (w, Qnil);
5602 if (!NILP (p->prev))
5604 wset_prev
5605 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
5606 wset_next (XWINDOW (w->prev), p->window);
5608 else
5610 wset_prev (w, Qnil);
5611 if (!NILP (w->parent))
5612 wset_combination (XWINDOW (w->parent),
5613 (XINT (p->total_cols)
5614 != XWINDOW (w->parent)->total_cols),
5615 p->window);
5618 /* If we squirreled away the buffer, restore it now. */
5619 if (BUFFERP (w->combination_limit))
5620 wset_buffer (w, w->combination_limit);
5621 w->left_col = XFASTINT (p->left_col);
5622 w->top_line = XFASTINT (p->top_line);
5623 w->total_cols = XFASTINT (p->total_cols);
5624 w->total_lines = XFASTINT (p->total_lines);
5625 wset_normal_cols (w, p->normal_cols);
5626 wset_normal_lines (w, p->normal_lines);
5627 w->hscroll = XFASTINT (p->hscroll);
5628 w->min_hscroll = XFASTINT (p->min_hscroll);
5629 wset_display_table (w, p->display_table);
5630 w->left_margin_cols = XINT (p->left_margin_cols);
5631 w->right_margin_cols = XINT (p->right_margin_cols);
5632 w->left_fringe_width = XINT (p->left_fringe_width);
5633 w->right_fringe_width = XINT (p->right_fringe_width);
5634 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
5635 w->scroll_bar_width = XINT (p->scroll_bar_width);
5636 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
5637 wset_dedicated (w, p->dedicated);
5638 wset_combination_limit (w, p->combination_limit);
5639 /* Restore any window parameters that have been saved.
5640 Parameters that have not been saved are left alone. */
5641 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5643 pers = XCAR (tem);
5644 if (CONSP (pers))
5646 if (NILP (XCDR (pers)))
5648 par = Fassq (XCAR (pers), w->window_parameters);
5649 if (CONSP (par) && !NILP (XCDR (par)))
5650 /* Reset a parameter to nil if and only if it
5651 has a non-nil association. Don't make new
5652 associations. */
5653 Fsetcdr (par, Qnil);
5655 else
5656 /* Always restore a non-nil value. */
5657 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5661 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5662 /* If saved buffer is alive, install it. */
5664 wset_buffer (w, p->buffer);
5665 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5666 set_marker_restricted (w->start, p->start, w->contents);
5667 set_marker_restricted (w->pointm, p->pointm,
5668 w->contents);
5669 Fset_marker (BVAR (XBUFFER (w->contents), mark),
5670 p->mark, w->contents);
5672 /* As documented in Fcurrent_window_configuration, don't
5673 restore the location of point in the buffer which was
5674 current when the window configuration was recorded. */
5675 if (!EQ (p->buffer, new_current_buffer)
5676 && XBUFFER (p->buffer) == current_buffer)
5677 Fgoto_char (w->pointm);
5679 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
5680 /* Keep window's old buffer; make sure the markers are real. */
5682 /* Set window markers at start of visible range. */
5683 if (XMARKER (w->start)->buffer == 0)
5684 set_marker_restricted_both (w->start, w->contents, 0, 0);
5685 if (XMARKER (w->pointm)->buffer == 0)
5686 set_marker_restricted_both
5687 (w->pointm, w->contents,
5688 BUF_PT (XBUFFER (w->contents)),
5689 BUF_PT_BYTE (XBUFFER (w->contents)));
5690 w->start_at_line_beg = 1;
5692 else if (!NILP (w->start))
5693 /* Leaf window has no live buffer, get one. */
5695 /* Get the buffer via other_buffer_safely in order to
5696 avoid showing an unimportant buffer and, if necessary, to
5697 recreate *scratch* in the course (part of Juanma's bs-show
5698 scenario from March 2011). */
5699 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
5700 /* This will set the markers to beginning of visible
5701 range. */
5702 set_marker_restricted_both (w->start, w->contents, 0, 0);
5703 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
5704 w->start_at_line_beg = 1;
5705 if (!NILP (w->dedicated))
5706 /* Record this window as dead. */
5707 dead_windows = Fcons (window, dead_windows);
5708 /* Make sure window is no more dedicated. */
5709 wset_dedicated (w, Qnil);
5713 fset_root_window (f, data->root_window);
5714 /* Arrange *not* to restore point in the buffer that was
5715 current when the window configuration was saved. */
5716 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
5717 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5718 make_number (old_point),
5719 XWINDOW (data->current_window)->contents);
5721 /* In the following call to `select-window', prevent "swapping out
5722 point" in the old selected window using the buffer that has
5723 been restored into it. We already swapped out that point from
5724 that window's old buffer. */
5725 select_window (data->current_window, Qnil, 1);
5726 BVAR (XBUFFER (XWINDOW (selected_window)->contents), last_selected_window)
5727 = selected_window;
5729 if (NILP (data->focus_frame)
5730 || (FRAMEP (data->focus_frame)
5731 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5732 Fredirect_frame_focus (frame, data->focus_frame);
5734 /* Set the screen height to the value it had before this function. */
5735 if (previous_frame_lines != FRAME_LINES (f)
5736 || previous_frame_cols != FRAME_COLS (f))
5737 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5738 0, 0, 0);
5739 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5740 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5741 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5742 make_number (0));
5743 #ifdef HAVE_WINDOW_SYSTEM
5744 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5745 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5746 make_number (0));
5747 #endif
5748 #endif
5750 /* Now, free glyph matrices in windows that were not reused. */
5751 for (i = n = 0; i < n_leaf_windows; ++i)
5753 if (NILP (leaf_windows[i]->contents))
5754 free_window_matrices (leaf_windows[i]);
5755 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
5756 ++n;
5759 adjust_frame_glyphs (f);
5760 unblock_input ();
5762 /* Scan dead buffer windows. */
5763 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5765 window = XCAR (dead_windows);
5766 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5767 delete_deletable_window (window);
5770 /* Fselect_window will have made f the selected frame, so we
5771 reselect the proper frame here. Fhandle_switch_frame will change the
5772 selected window too, but that doesn't make the call to
5773 Fselect_window above totally superfluous; it still sets f's
5774 selected window. */
5775 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5776 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5778 run_window_configuration_change_hook (f);
5781 if (!NILP (new_current_buffer))
5783 Fset_buffer (new_current_buffer);
5784 /* If the new current buffer doesn't appear in the selected
5785 window, go to its old point (see bug#12208). */
5786 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
5787 Fgoto_char (make_number (old_point));
5790 Vminibuf_scroll_window = data->minibuf_scroll_window;
5791 minibuf_selected_window = data->minibuf_selected_window;
5793 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5796 void
5797 restore_window_configuration (Lisp_Object configuration)
5799 Fset_window_configuration (configuration);
5803 /* If WINDOW is an internal window, recursively delete all child windows
5804 reachable via the next and contents slots of WINDOW. Otherwise setup
5805 WINDOW to not show any buffer. */
5807 void
5808 delete_all_child_windows (Lisp_Object window)
5810 register struct window *w;
5812 w = XWINDOW (window);
5814 if (!NILP (w->next))
5815 /* Delete WINDOW's siblings (we traverse postorderly). */
5816 delete_all_child_windows (w->next);
5818 if (WINDOWP (w->contents))
5820 delete_all_child_windows (w->contents);
5821 wset_combination (w, 0, Qnil);
5823 else if (BUFFERP (w->contents))
5825 unshow_buffer (w);
5826 unchain_marker (XMARKER (w->pointm));
5827 unchain_marker (XMARKER (w->start));
5828 /* Since combination limit makes sense for an internal windows
5829 only, we use this slot to save the buffer for the sake of
5830 possible resurrection in Fset_window_configuration. */
5831 wset_combination_limit (w, w->contents);
5832 wset_buffer (w, Qnil);
5835 Vwindow_list = Qnil;
5838 static int
5839 count_windows (register struct window *window)
5841 register int count = 1;
5842 if (!NILP (window->next))
5843 count += count_windows (XWINDOW (window->next));
5844 if (WINDOWP (window->contents))
5845 count += count_windows (XWINDOW (window->contents));
5846 return count;
5850 /* Fill vector FLAT with leaf windows under W, starting at index I.
5851 Value is last index + 1. */
5852 static int
5853 get_leaf_windows (struct window *w, struct window **flat, int i)
5855 while (w)
5857 if (WINDOWP (w->contents))
5858 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
5859 else
5860 flat[i++] = w;
5862 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5865 return i;
5869 /* Return a pointer to the glyph W's physical cursor is on. Value is
5870 null if W's current matrix is invalid, so that no meaningful glyph
5871 can be returned. */
5872 struct glyph *
5873 get_phys_cursor_glyph (struct window *w)
5875 struct glyph_row *row;
5876 struct glyph *glyph;
5877 int hpos = w->phys_cursor.hpos;
5879 if (!(w->phys_cursor.vpos >= 0
5880 && w->phys_cursor.vpos < w->current_matrix->nrows))
5881 return NULL;
5883 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5884 if (!row->enabled_p)
5885 return NULL;
5887 if (w->hscroll)
5889 /* When the window is hscrolled, cursor hpos can legitimately be
5890 out of bounds, but we draw the cursor at the corresponding
5891 window margin in that case. */
5892 if (!row->reversed_p && hpos < 0)
5893 hpos = 0;
5894 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
5895 hpos = row->used[TEXT_AREA] - 1;
5898 if (0 <= hpos && hpos < row->used[TEXT_AREA])
5899 glyph = row->glyphs[TEXT_AREA] + hpos;
5900 else
5901 glyph = NULL;
5903 return glyph;
5907 static int
5908 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
5910 register struct saved_window *p;
5911 register struct window *w;
5912 register Lisp_Object tem, pers, par;
5914 for (; !NILP (window); window = w->next)
5916 p = SAVED_WINDOW_N (vector, i);
5917 w = XWINDOW (window);
5919 wset_temslot (w, make_number (i)); i++;
5920 p->window = window;
5921 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
5922 p->left_col = make_number (w->left_col);
5923 p->top_line = make_number (w->top_line);
5924 p->total_cols = make_number (w->total_cols);
5925 p->total_lines = make_number (w->total_lines);
5926 p->normal_cols = w->normal_cols;
5927 p->normal_lines = w->normal_lines;
5928 XSETFASTINT (p->hscroll, w->hscroll);
5929 XSETFASTINT (p->min_hscroll, w->min_hscroll);
5930 p->display_table = w->display_table;
5931 p->left_margin_cols = make_number (w->left_margin_cols);
5932 p->right_margin_cols = make_number (w->right_margin_cols);
5933 p->left_fringe_width = make_number (w->left_fringe_width);
5934 p->right_fringe_width = make_number (w->right_fringe_width);
5935 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
5936 p->scroll_bar_width = make_number (w->scroll_bar_width);
5937 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5938 p->dedicated = w->dedicated;
5939 p->combination_limit = w->combination_limit;
5940 p->window_parameters = Qnil;
5942 if (!NILP (Vwindow_persistent_parameters))
5944 /* Run cycle detection on Vwindow_persistent_parameters. */
5945 Lisp_Object tortoise, hare;
5947 hare = tortoise = Vwindow_persistent_parameters;
5948 while (CONSP (hare))
5950 hare = XCDR (hare);
5951 if (!CONSP (hare))
5952 break;
5954 hare = XCDR (hare);
5955 tortoise = XCDR (tortoise);
5957 if (EQ (hare, tortoise))
5958 /* Reset Vwindow_persistent_parameters to Qnil. */
5960 Vwindow_persistent_parameters = Qnil;
5961 break;
5965 for (tem = Vwindow_persistent_parameters; CONSP (tem);
5966 tem = XCDR (tem))
5968 pers = XCAR (tem);
5969 /* Save values for persistent window parameters. */
5970 if (CONSP (pers) && !NILP (XCDR (pers)))
5972 par = Fassq (XCAR (pers), w->window_parameters);
5973 if (NILP (par))
5974 /* If the window has no value for the parameter,
5975 make one. */
5976 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
5977 p->window_parameters);
5978 else
5979 /* If the window has a value for the parameter,
5980 save it. */
5981 p->window_parameters = Fcons (Fcons (XCAR (par),
5982 XCDR (par)),
5983 p->window_parameters);
5988 if (BUFFERP (w->contents))
5990 /* Save w's value of point in the window configuration. If w
5991 is the selected window, then get the value of point from
5992 the buffer; pointm is garbage in the selected window. */
5993 if (EQ (window, selected_window))
5994 p->pointm = build_marker (XBUFFER (w->contents),
5995 BUF_PT (XBUFFER (w->contents)),
5996 BUF_PT_BYTE (XBUFFER (w->contents)));
5997 else
5998 p->pointm = Fcopy_marker (w->pointm, Qnil);
5999 XMARKER (p->pointm)->insertion_type
6000 = !NILP (Vwindow_point_insertion_type);
6002 p->start = Fcopy_marker (w->start, Qnil);
6003 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6005 tem = BVAR (XBUFFER (w->contents), mark);
6006 p->mark = Fcopy_marker (tem, Qnil);
6008 else
6010 p->pointm = Qnil;
6011 p->start = Qnil;
6012 p->mark = Qnil;
6013 p->start_at_line_beg = Qnil;
6016 if (NILP (w->parent))
6017 p->parent = Qnil;
6018 else
6019 p->parent = XWINDOW (w->parent)->temslot;
6021 if (NILP (w->prev))
6022 p->prev = Qnil;
6023 else
6024 p->prev = XWINDOW (w->prev)->temslot;
6026 if (WINDOWP (w->contents))
6027 i = save_window_save (w->contents, vector, i);
6030 return i;
6033 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6034 Scurrent_window_configuration, 0, 1, 0,
6035 doc: /* Return an object representing the current window configuration of FRAME.
6036 If FRAME is nil or omitted, use the selected frame.
6037 This describes the number of windows, their sizes and current buffers,
6038 and for each displayed buffer, where display starts, and the positions of
6039 point and mark. An exception is made for point in the current buffer:
6040 its value is -not- saved.
6041 This also records the currently selected frame, and FRAME's focus
6042 redirection (see `redirect-frame-focus'). The variable
6043 `window-persistent-parameters' specifies which window parameters are
6044 saved by this function. */)
6045 (Lisp_Object frame)
6047 register Lisp_Object tem;
6048 register int n_windows;
6049 register struct save_window_data *data;
6050 register int i;
6051 struct frame *f = decode_live_frame (frame);
6053 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6054 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6055 PVEC_WINDOW_CONFIGURATION);
6057 data->frame_cols = FRAME_COLS (f);
6058 data->frame_lines = FRAME_LINES (f);
6059 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6060 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6061 data->selected_frame = selected_frame;
6062 data->current_window = FRAME_SELECTED_WINDOW (f);
6063 XSETBUFFER (data->current_buffer, current_buffer);
6064 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6065 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6066 data->root_window = FRAME_ROOT_WINDOW (f);
6067 data->focus_frame = FRAME_FOCUS_FRAME (f);
6068 tem = make_uninit_vector (n_windows);
6069 data->saved_windows = tem;
6070 for (i = 0; i < n_windows; i++)
6071 ASET (tem, i,
6072 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6073 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6074 XSETWINDOW_CONFIGURATION (tem, data);
6075 return (tem);
6078 /* Called after W's margins, fringes or scroll bars was adjusted. */
6080 static void
6081 apply_window_adjustment (struct window *w)
6083 eassert (w);
6084 adjust_window_margins (w);
6085 clear_glyph_matrix (w->current_matrix);
6086 w->window_end_valid = 0;
6087 windows_or_buffers_changed++;
6088 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6092 /***********************************************************************
6093 Marginal Areas
6094 ***********************************************************************/
6096 static struct window *
6097 set_window_margins (struct window *w, Lisp_Object left_width,
6098 Lisp_Object right_width)
6100 int left, right;
6102 /* FIXME: what about margins that are too wide? */
6103 left = (NILP (left_width) ? 0
6104 : (CHECK_NATNUM (left_width), XINT (left_width)));
6105 right = (NILP (right_width) ? 0
6106 : (CHECK_NATNUM (right_width), XINT (right_width)));
6108 if (w->left_margin_cols != left || w->right_margin_cols != right)
6110 w->left_margin_cols = left;
6111 w->right_margin_cols = right;
6112 return w;
6114 return NULL;
6117 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6118 2, 3, 0,
6119 doc: /* Set width of marginal areas of window WINDOW.
6120 WINDOW must be a live window and defaults to the selected one.
6122 Second arg LEFT-WIDTH specifies the number of character cells to
6123 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6124 does the same for the right marginal area. A nil width parameter
6125 means no margin.
6127 Return t if any margin was actually changed and nil otherwise. */)
6128 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6130 struct window *w = set_window_margins (decode_live_window (window),
6131 left_width, right_width);
6132 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6136 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6137 0, 1, 0,
6138 doc: /* Get width of marginal areas of window WINDOW.
6139 WINDOW must be a live window and defaults to the selected one.
6141 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6142 If a marginal area does not exist, its width will be returned
6143 as nil. */)
6144 (Lisp_Object window)
6146 struct window *w = decode_live_window (window);
6147 return Fcons (w->left_margin_cols ? make_number (w->left_margin_cols) : Qnil,
6148 w->right_margin_cols ? make_number (w->right_margin_cols) : Qnil);
6153 /***********************************************************************
6154 Fringes
6155 ***********************************************************************/
6157 static struct window *
6158 set_window_fringes (struct window *w, Lisp_Object left_width,
6159 Lisp_Object right_width, Lisp_Object outside_margins)
6161 int left, right, outside = !NILP (outside_margins);
6163 left = (NILP (left_width) ? -1
6164 : (CHECK_NATNUM (left_width), XINT (left_width)));
6165 right = (NILP (right_width) ? -1
6166 : (CHECK_NATNUM (right_width), XINT (right_width)));
6168 /* Do nothing on a tty or if nothing to actually change. */
6169 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6170 && (w->left_fringe_width != left
6171 || w->right_fringe_width != right
6172 || w->fringes_outside_margins != outside))
6174 w->left_fringe_width = left;
6175 w->right_fringe_width = right;
6176 w->fringes_outside_margins = outside;
6177 return w;
6179 return NULL;
6182 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6183 2, 4, 0,
6184 doc: /* Set the fringe widths of window WINDOW.
6185 WINDOW must be a live window and defaults to the selected one.
6187 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6188 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6189 fringe width. If a fringe width arg is nil, that means to use the
6190 frame's default fringe width. Default fringe widths can be set with
6191 the command `set-fringe-style'.
6192 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6193 outside of the display margins. By default, fringes are drawn between
6194 display marginal areas and the text area.
6196 Return t if any fringe was actually changed and nil otherwise. */)
6197 (Lisp_Object window, Lisp_Object left_width,
6198 Lisp_Object right_width, Lisp_Object outside_margins)
6200 struct window *w
6201 = set_window_fringes (decode_live_window (window),
6202 left_width, right_width, outside_margins);
6203 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6207 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6208 0, 1, 0,
6209 doc: /* Get width of fringes of window WINDOW.
6210 WINDOW must be a live window and defaults to the selected one.
6212 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6213 (Lisp_Object window)
6215 struct window *w = decode_live_window (window);
6217 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6218 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6219 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6224 /***********************************************************************
6225 Scroll bars
6226 ***********************************************************************/
6228 static struct window *
6229 set_window_scroll_bars (struct window *w, Lisp_Object width,
6230 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6232 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6234 if (iwidth == 0)
6235 vertical_type = Qnil;
6237 if (!(NILP (vertical_type)
6238 || EQ (vertical_type, Qleft)
6239 || EQ (vertical_type, Qright)
6240 || EQ (vertical_type, Qt)))
6241 error ("Invalid type of vertical scroll bar");
6243 if (w->scroll_bar_width != iwidth
6244 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6246 w->scroll_bar_width = iwidth;
6247 wset_vertical_scroll_bar_type (w, vertical_type);
6248 return w;
6250 return NULL;
6253 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6254 Sset_window_scroll_bars, 2, 4, 0,
6255 doc: /* Set width and type of scroll bars of window WINDOW.
6256 WINDOW must be a live window and defaults to the selected one.
6258 Second parameter WIDTH specifies the pixel width for the scroll bar;
6259 this is automatically adjusted to a multiple of the frame column width.
6260 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6261 bar: left, right, or nil.
6262 If WIDTH is nil, use the frame's scroll-bar width.
6263 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6264 Fourth parameter HORIZONTAL-TYPE is currently unused.
6266 Return t if scroll bars was actually changed and nil otherwise. */)
6267 (Lisp_Object window, Lisp_Object width,
6268 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6270 struct window *w
6271 = set_window_scroll_bars (decode_live_window (window),
6272 width, vertical_type, horizontal_type);
6273 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6277 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6278 0, 1, 0,
6279 doc: /* Get width and type of scroll bars of window WINDOW.
6280 WINDOW must be a live window and defaults to the selected one.
6282 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6283 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6284 value. */)
6285 (Lisp_Object window)
6287 struct window *w = decode_live_window (window);
6289 return list4 (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6290 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6291 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6292 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6293 w->vertical_scroll_bar_type, Qnil);
6298 /***********************************************************************
6299 Smooth scrolling
6300 ***********************************************************************/
6302 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6303 doc: /* Return the amount by which WINDOW is scrolled vertically.
6304 If WINDOW is omitted or nil, it defaults to the selected window.
6305 Normally, value is a multiple of the canonical character height of WINDOW;
6306 optional second arg PIXELS-P means value is measured in pixels. */)
6307 (Lisp_Object window, Lisp_Object pixels_p)
6309 Lisp_Object result;
6310 struct window *w = decode_live_window (window);
6311 struct frame *f = XFRAME (w->frame);
6313 if (FRAME_WINDOW_P (f))
6314 result = (NILP (pixels_p)
6315 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6316 : make_number (-w->vscroll));
6317 else
6318 result = make_number (0);
6319 return result;
6323 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6324 2, 3, 0,
6325 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6326 WINDOW nil means use the selected window. Normally, VSCROLL is a
6327 non-negative multiple of the canonical character height of WINDOW;
6328 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6329 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6330 corresponds to an integral number of pixels. The return value is the
6331 result of this rounding.
6332 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6333 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6335 struct window *w = decode_live_window (window);
6336 struct frame *f = XFRAME (w->frame);
6338 CHECK_NUMBER_OR_FLOAT (vscroll);
6340 if (FRAME_WINDOW_P (f))
6342 int old_dy = w->vscroll;
6344 w->vscroll = - (NILP (pixels_p)
6345 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6346 : XFLOATINT (vscroll));
6347 w->vscroll = min (w->vscroll, 0);
6349 if (w->vscroll != old_dy)
6351 /* Adjust glyph matrix of the frame if the virtual display
6352 area becomes larger than before. */
6353 if (w->vscroll < 0 && w->vscroll < old_dy)
6354 adjust_frame_glyphs (f);
6356 /* Prevent redisplay shortcuts. */
6357 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
6361 return Fwindow_vscroll (window, pixels_p);
6365 /* Call FN for all leaf windows on frame F. FN is called with the
6366 first argument being a pointer to the leaf window, and with
6367 additional argument USER_DATA. Stops when FN returns 0. */
6369 static void
6370 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6371 void *user_data)
6373 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6374 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6375 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6379 /* Helper function for foreach_window. Call FN for all leaf windows
6380 reachable from W. FN is called with the first argument being a
6381 pointer to the leaf window, and with additional argument USER_DATA.
6382 Stop when FN returns 0. Value is 0 if stopped by FN. */
6384 static int
6385 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6387 int cont;
6389 for (cont = 1; w && cont;)
6391 if (WINDOWP (w->contents))
6392 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
6393 else
6394 cont = fn (w, user_data);
6396 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6399 return cont;
6402 /***********************************************************************
6403 Initialization
6404 ***********************************************************************/
6406 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6407 describe the same state of affairs. This is used by Fequal.
6409 IGNORE_POSITIONS means ignore non-matching scroll positions
6410 and the like.
6412 This ignores a couple of things like the dedication status of
6413 window, combination_limit and the like. This might have to be
6414 fixed. */
6416 bool
6417 compare_window_configurations (Lisp_Object configuration1,
6418 Lisp_Object configuration2,
6419 bool ignore_positions)
6421 register struct save_window_data *d1, *d2;
6422 struct Lisp_Vector *sws1, *sws2;
6423 ptrdiff_t i;
6425 CHECK_WINDOW_CONFIGURATION (configuration1);
6426 CHECK_WINDOW_CONFIGURATION (configuration2);
6428 d1 = (struct save_window_data *) XVECTOR (configuration1);
6429 d2 = (struct save_window_data *) XVECTOR (configuration2);
6430 sws1 = XVECTOR (d1->saved_windows);
6431 sws2 = XVECTOR (d2->saved_windows);
6433 /* Frame settings must match. */
6434 if (d1->frame_cols != d2->frame_cols
6435 || d1->frame_lines != d2->frame_lines
6436 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6437 || !EQ (d1->selected_frame, d2->selected_frame)
6438 || !EQ (d1->current_buffer, d2->current_buffer)
6439 || (!ignore_positions
6440 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6441 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6442 || !EQ (d1->focus_frame, d2->focus_frame)
6443 /* Verify that the two configurations have the same number of windows. */
6444 || sws1->header.size != sws2->header.size)
6445 return 0;
6447 for (i = 0; i < sws1->header.size; i++)
6449 struct saved_window *sw1, *sw2;
6451 sw1 = SAVED_WINDOW_N (sws1, i);
6452 sw2 = SAVED_WINDOW_N (sws2, i);
6454 if (
6455 /* The "current" windows in the two configurations must
6456 correspond to each other. */
6457 EQ (d1->current_window, sw1->window)
6458 != EQ (d2->current_window, sw2->window)
6459 /* Windows' buffers must match. */
6460 || !EQ (sw1->buffer, sw2->buffer)
6461 || !EQ (sw1->left_col, sw2->left_col)
6462 || !EQ (sw1->top_line, sw2->top_line)
6463 || !EQ (sw1->total_cols, sw2->total_cols)
6464 || !EQ (sw1->total_lines, sw2->total_lines)
6465 || !EQ (sw1->display_table, sw2->display_table)
6466 /* The next two disjuncts check the window structure for
6467 equality. */
6468 || !EQ (sw1->parent, sw2->parent)
6469 || !EQ (sw1->prev, sw2->prev)
6470 || (!ignore_positions
6471 && (!EQ (sw1->hscroll, sw2->hscroll)
6472 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6473 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6474 || NILP (Fequal (sw1->start, sw2->start))
6475 || NILP (Fequal (sw1->pointm, sw2->pointm))
6476 || NILP (Fequal (sw1->mark, sw2->mark))))
6477 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6478 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6479 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6480 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6481 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6482 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6483 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6484 return 0;
6487 return 1;
6490 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6491 Scompare_window_configurations, 2, 2, 0,
6492 doc: /* Compare two window configurations as regards the structure of windows.
6493 This function ignores details such as the values of point and mark
6494 and scrolling positions. */)
6495 (Lisp_Object x, Lisp_Object y)
6497 if (compare_window_configurations (x, y, 1))
6498 return Qt;
6499 return Qnil;
6502 void
6503 init_window_once (void)
6505 struct frame *f = make_initial_frame ();
6506 XSETFRAME (selected_frame, f);
6507 Vterminal_frame = selected_frame;
6508 minibuf_window = f->minibuffer_window;
6509 selected_window = f->selected_window;
6510 last_nonminibuf_frame = f;
6512 window_initialized = 1;
6515 void
6516 init_window (void)
6518 Vwindow_list = Qnil;
6521 void
6522 syms_of_window (void)
6524 DEFSYM (Qscroll_up, "scroll-up");
6525 DEFSYM (Qscroll_down, "scroll-down");
6526 DEFSYM (Qscroll_command, "scroll-command");
6528 Fput (Qscroll_up, Qscroll_command, Qt);
6529 Fput (Qscroll_down, Qscroll_command, Qt);
6531 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6532 DEFSYM (Qwindowp, "windowp");
6533 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6534 DEFSYM (Qwindow_live_p, "window-live-p");
6535 DEFSYM (Qwindow_valid_p, "window-valid-p");
6536 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6537 DEFSYM (Qdelete_window, "delete-window");
6538 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6539 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6540 DEFSYM (Qsafe, "safe");
6541 DEFSYM (Qdisplay_buffer, "display-buffer");
6542 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6543 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6544 DEFSYM (Qget_mru_window, "get-mru-window");
6545 DEFSYM (Qwindow_size, "window-size");
6546 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6547 DEFSYM (Qabove, "above");
6548 DEFSYM (Qbelow, "below");
6549 DEFSYM (Qclone_of, "clone-of");
6551 staticpro (&Vwindow_list);
6553 minibuf_selected_window = Qnil;
6554 staticpro (&minibuf_selected_window);
6556 window_scroll_pixel_based_preserve_x = -1;
6557 window_scroll_pixel_based_preserve_y = -1;
6558 window_scroll_preserve_hpos = -1;
6559 window_scroll_preserve_vpos = -1;
6561 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6562 doc: /* Non-nil means call as function to display a help buffer.
6563 The function is called with one argument, the buffer to be displayed.
6564 Used by `with-output-to-temp-buffer'.
6565 If this function is used, then it must do the entire job of showing
6566 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6567 Vtemp_buffer_show_function = Qnil;
6569 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6570 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6571 Vminibuf_scroll_window = Qnil;
6573 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6574 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6575 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6576 is displayed in the `mode-line' face. */);
6577 mode_line_in_non_selected_windows = 1;
6579 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6580 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6581 Vother_window_scroll_buffer = Qnil;
6583 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6584 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6585 auto_window_vscroll_p = 1;
6587 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6588 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6589 next_screen_context_lines = 2;
6591 DEFVAR_LISP ("scroll-preserve-screen-position",
6592 Vscroll_preserve_screen_position,
6593 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6594 A value of nil means point does not keep its screen position except
6595 at the scroll margin or window boundary respectively.
6596 A value of t means point keeps its screen position if the scroll
6597 command moved it vertically out of the window, e.g. when scrolling
6598 by full screens.
6599 Any other value means point always keeps its screen position.
6600 Scroll commands should have the `scroll-command' property
6601 on their symbols to be controlled by this variable. */);
6602 Vscroll_preserve_screen_position = Qnil;
6604 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6605 doc: /* Type of marker to use for `window-point'. */);
6606 Vwindow_point_insertion_type = Qnil;
6608 DEFVAR_LISP ("window-configuration-change-hook",
6609 Vwindow_configuration_change_hook,
6610 doc: /* Functions to call when window configuration changes.
6611 The buffer-local part is run once per window, with the relevant window
6612 selected; while the global part is run only once for the modified frame,
6613 with the relevant frame selected. */);
6614 Vwindow_configuration_change_hook = Qnil;
6616 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6617 doc: /* Non-nil means `recenter' redraws entire frame.
6618 If this option is non-nil, then the `recenter' command with a nil
6619 argument will redraw the entire frame; the special value `tty' causes
6620 the frame to be redrawn only if it is a tty frame. */);
6621 Vrecenter_redisplay = Qtty;
6623 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6624 doc: /* If t, resize window combinations proportionally.
6625 If this variable is nil, splitting a window gets the entire screen space
6626 for displaying the new window from the window to split. Deleting and
6627 resizing a window preferably resizes one adjacent window only.
6629 If this variable is t, splitting a window tries to get the space
6630 proportionally from all windows in the same combination. This also
6631 allows to split a window that is otherwise too small or of fixed size.
6632 Resizing and deleting a window proportionally resize all windows in the
6633 same combination.
6635 Other values are reserved for future use.
6637 This variable takes no effect if the variable `window-combination-limit' is
6638 non-nil. */);
6639 Vwindow_combination_resize = Qnil;
6641 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6642 doc: /* If non-nil, splitting a window makes a new parent window.
6643 The following values are recognized:
6645 nil means splitting a window will create a new parent window only if the
6646 window has no parent window or the window shall become part of a
6647 combination orthogonal to the one it is part of.
6649 `window-size' means that splitting a window for displaying a buffer
6650 makes a new parent window provided `display-buffer' is supposed to
6651 explicitly set the window's size due to the presence of a
6652 `window-height' or `window-width' entry in the alist used by
6653 `display-buffer'. Otherwise, this value is handled like nil.
6655 `temp-buffer' means that splitting a window for displaying a temporary
6656 buffer always makes a new parent window. Otherwise, this value is
6657 handled like nil.
6659 `display-buffer' means that splitting a window for displaying a buffer
6660 always makes a new parent window. Since temporary buffers are
6661 displayed by the function `display-buffer', this value is stronger
6662 than `temp-buffer'. Splitting a window for other purpose makes a
6663 new parent window only if needed.
6665 t means that splitting a window always creates a new parent window. If
6666 all splits behave this way, each frame's window tree is a binary
6667 tree and every window but the frame's root window has exactly one
6668 sibling.
6670 Other values are reserved for future use. */);
6671 Vwindow_combination_limit = Qwindow_size;
6673 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6674 doc: /* Alist of persistent window parameters.
6675 This alist specifies which window parameters shall get saved by
6676 `current-window-configuration' and `window-state-get' and subsequently
6677 restored to their previous values by `set-window-configuration' and
6678 `window-state-put'.
6680 The car of each entry of this alist is the symbol specifying the
6681 parameter. The cdr is one of the following:
6683 nil means the parameter is neither saved by `window-state-get' nor by
6684 `current-window-configuration'.
6686 t means the parameter is saved by `current-window-configuration' and,
6687 provided its WRITABLE argument is nil, by `window-state-get'.
6689 The symbol `writable' means the parameter is saved unconditionally by
6690 both `current-window-configuration' and `window-state-get'. Do not use
6691 this value for parameters without read syntax (like windows or frames).
6693 Parameters not saved by `current-window-configuration' or
6694 `window-state-get' are left alone by `set-window-configuration'
6695 respectively are not installed by `window-state-put'. */);
6696 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6698 defsubr (&Sselected_window);
6699 defsubr (&Sminibuffer_window);
6700 defsubr (&Swindow_minibuffer_p);
6701 defsubr (&Swindowp);
6702 defsubr (&Swindow_valid_p);
6703 defsubr (&Swindow_live_p);
6704 defsubr (&Swindow_frame);
6705 defsubr (&Sframe_root_window);
6706 defsubr (&Sframe_first_window);
6707 defsubr (&Sframe_selected_window);
6708 defsubr (&Sset_frame_selected_window);
6709 defsubr (&Spos_visible_in_window_p);
6710 defsubr (&Swindow_line_height);
6711 defsubr (&Swindow_buffer);
6712 defsubr (&Swindow_parent);
6713 defsubr (&Swindow_top_child);
6714 defsubr (&Swindow_left_child);
6715 defsubr (&Swindow_next_sibling);
6716 defsubr (&Swindow_prev_sibling);
6717 defsubr (&Swindow_combination_limit);
6718 defsubr (&Sset_window_combination_limit);
6719 defsubr (&Swindow_use_time);
6720 defsubr (&Swindow_top_line);
6721 defsubr (&Swindow_left_column);
6722 defsubr (&Swindow_total_height);
6723 defsubr (&Swindow_total_width);
6724 defsubr (&Swindow_normal_size);
6725 defsubr (&Swindow_new_total);
6726 defsubr (&Swindow_new_normal);
6727 defsubr (&Sset_window_new_total);
6728 defsubr (&Sset_window_new_normal);
6729 defsubr (&Swindow_resize_apply);
6730 defsubr (&Swindow_body_height);
6731 defsubr (&Swindow_body_width);
6732 defsubr (&Swindow_hscroll);
6733 defsubr (&Sset_window_hscroll);
6734 defsubr (&Swindow_redisplay_end_trigger);
6735 defsubr (&Sset_window_redisplay_end_trigger);
6736 defsubr (&Swindow_edges);
6737 defsubr (&Swindow_pixel_edges);
6738 defsubr (&Swindow_absolute_pixel_edges);
6739 defsubr (&Swindow_inside_edges);
6740 defsubr (&Swindow_inside_pixel_edges);
6741 defsubr (&Swindow_inside_absolute_pixel_edges);
6742 defsubr (&Scoordinates_in_window_p);
6743 defsubr (&Swindow_at);
6744 defsubr (&Swindow_point);
6745 defsubr (&Swindow_start);
6746 defsubr (&Swindow_end);
6747 defsubr (&Sset_window_point);
6748 defsubr (&Sset_window_start);
6749 defsubr (&Swindow_dedicated_p);
6750 defsubr (&Sset_window_dedicated_p);
6751 defsubr (&Swindow_display_table);
6752 defsubr (&Sset_window_display_table);
6753 defsubr (&Snext_window);
6754 defsubr (&Sprevious_window);
6755 defsubr (&Sget_buffer_window);
6756 defsubr (&Sdelete_other_windows_internal);
6757 defsubr (&Sdelete_window_internal);
6758 defsubr (&Sresize_mini_window_internal);
6759 defsubr (&Sset_window_buffer);
6760 defsubr (&Srun_window_configuration_change_hook);
6761 defsubr (&Sselect_window);
6762 defsubr (&Sforce_window_update);
6763 defsubr (&Ssplit_window_internal);
6764 defsubr (&Sscroll_up);
6765 defsubr (&Sscroll_down);
6766 defsubr (&Sscroll_left);
6767 defsubr (&Sscroll_right);
6768 defsubr (&Sother_window_for_scrolling);
6769 defsubr (&Sscroll_other_window);
6770 defsubr (&Sminibuffer_selected_window);
6771 defsubr (&Srecenter);
6772 defsubr (&Swindow_text_height);
6773 defsubr (&Smove_to_window_line);
6774 defsubr (&Swindow_configuration_p);
6775 defsubr (&Swindow_configuration_frame);
6776 defsubr (&Sset_window_configuration);
6777 defsubr (&Scurrent_window_configuration);
6778 defsubr (&Sset_window_margins);
6779 defsubr (&Swindow_margins);
6780 defsubr (&Sset_window_fringes);
6781 defsubr (&Swindow_fringes);
6782 defsubr (&Sset_window_scroll_bars);
6783 defsubr (&Swindow_scroll_bars);
6784 defsubr (&Swindow_vscroll);
6785 defsubr (&Sset_window_vscroll);
6786 defsubr (&Scompare_window_configurations);
6787 defsubr (&Swindow_list);
6788 defsubr (&Swindow_list_1);
6789 defsubr (&Swindow_prev_buffers);
6790 defsubr (&Sset_window_prev_buffers);
6791 defsubr (&Swindow_next_buffers);
6792 defsubr (&Sset_window_next_buffers);
6793 defsubr (&Swindow_parameters);
6794 defsubr (&Swindow_parameter);
6795 defsubr (&Sset_window_parameter);
6798 void
6799 keys_of_window (void)
6801 initial_define_key (control_x_map, '<', "scroll-left");
6802 initial_define_key (control_x_map, '>', "scroll-right");
6804 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6805 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6806 initial_define_key (meta_map, 'v', "scroll-down-command");