merge from trunk
[emacs.git] / src / window.c
blobb02b30cd0b2257c3ff1647b009ad0f77ef4d717a
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2013 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #define WINDOW_INLINE EXTERN_INLINE
25 #include <stdio.h>
27 #include "lisp.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "keyboard.h"
31 #include "keymap.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "commands.h"
35 #include "indent.h"
36 #include "termchar.h"
37 #include "disptab.h"
38 #include "dispextern.h"
39 #include "blockinput.h"
40 #include "intervals.h"
41 #include "termhooks.h" /* For FRAME_TERMINAL. */
43 #ifdef HAVE_X_WINDOWS
44 #include "xterm.h"
45 #endif /* HAVE_X_WINDOWS */
46 #ifdef HAVE_NTGUI
47 #include "w32term.h"
48 #endif
49 #ifdef MSDOS
50 #include "msdos.h"
51 #endif
52 #ifdef HAVE_NS
53 #include "nsterm.h"
54 #endif
56 Lisp_Object Qwindowp, Qwindow_live_p;
57 static Lisp_Object Qwindow_valid_p;
58 static Lisp_Object Qwindow_configuration_p;
59 static Lisp_Object Qrecord_window_buffer;
60 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
61 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
62 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
63 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
64 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
66 static int displayed_window_lines (struct window *);
67 static int count_windows (struct window *);
68 static int get_leaf_windows (struct window *, struct window **, int);
69 static void window_scroll (Lisp_Object, EMACS_INT, bool, int);
70 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
71 static void window_scroll_line_based (Lisp_Object, int, bool, int);
72 static Lisp_Object window_list (void);
73 static int add_window_to_list (struct window *, void *);
74 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
75 Lisp_Object, int);
76 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
77 Lisp_Object *);
78 static void foreach_window (struct frame *,
79 int (* fn) (struct window *, void *),
80 void *);
81 static int foreach_window_1 (struct window *,
82 int (* fn) (struct window *, void *),
83 void *);
84 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
85 static int window_resize_check (struct window *, bool);
86 static void window_resize_apply (struct window *, bool);
87 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
88 static void select_window_1 (Lisp_Object, bool);
90 static struct window *set_window_fringes (struct window *, Lisp_Object,
91 Lisp_Object, Lisp_Object);
92 static struct window *set_window_margins (struct window *, Lisp_Object,
93 Lisp_Object);
94 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
95 Lisp_Object, Lisp_Object);
96 static void apply_window_adjustment (struct window *);
98 /* This is the window in which the terminal's cursor should
99 be left when nothing is being done with it. This must
100 always be a leaf window, and its buffer is selected by
101 the top level editing loop at the end of each command.
103 This value is always the same as
104 FRAME_SELECTED_WINDOW (selected_frame). */
105 Lisp_Object selected_window;
107 /* A list of all windows for use by next_window and Fwindow_list.
108 Functions creating or deleting windows should invalidate this cache
109 by setting it to nil. */
110 Lisp_Object Vwindow_list;
112 /* The mini-buffer window of the selected frame.
113 Note that you cannot test for mini-bufferness of an arbitrary window
114 by comparing against this; but you can test for mini-bufferness of
115 the selected window. */
116 Lisp_Object minibuf_window;
118 /* Non-nil means it is the window whose mode line should be
119 shown as the selected window when the minibuffer is selected. */
120 Lisp_Object minibuf_selected_window;
122 /* Hook run at end of temp_output_buffer_show. */
123 static Lisp_Object Qtemp_buffer_show_hook;
125 /* Nonzero after init_window_once has finished. */
126 static int window_initialized;
128 /* Hook to run when window config changes. */
129 static Lisp_Object Qwindow_configuration_change_hook;
131 /* Used by the function window_scroll_pixel_based */
132 static int window_scroll_pixel_based_preserve_x;
133 static int window_scroll_pixel_based_preserve_y;
135 /* Same for window_scroll_line_based. */
136 static EMACS_INT window_scroll_preserve_hpos;
137 static EMACS_INT window_scroll_preserve_vpos;
139 static void
140 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
142 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
145 /* These setters are used only in this file, so they can be private. */
146 static void
147 wset_combination_limit (struct window *w, Lisp_Object val)
149 w->combination_limit = val;
151 static void
152 wset_dedicated (struct window *w, Lisp_Object val)
154 w->dedicated = val;
156 static void
157 wset_display_table (struct window *w, Lisp_Object val)
159 w->display_table = val;
161 static void
162 wset_new_normal (struct window *w, Lisp_Object val)
164 w->new_normal = val;
166 static void
167 wset_new_total (struct window *w, Lisp_Object val)
169 w->new_total = val;
171 static void
172 wset_normal_cols (struct window *w, Lisp_Object val)
174 w->normal_cols = val;
176 static void
177 wset_normal_lines (struct window *w, Lisp_Object val)
179 w->normal_lines = val;
181 static void
182 wset_parent (struct window *w, Lisp_Object val)
184 w->parent = val;
186 static void
187 wset_pointm (struct window *w, Lisp_Object val)
189 w->pointm = val;
191 static void
192 wset_start (struct window *w, Lisp_Object val)
194 w->start = val;
196 static void
197 wset_temslot (struct window *w, Lisp_Object val)
199 w->temslot = val;
201 static void
202 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
204 w->vertical_scroll_bar_type = val;
206 static void
207 wset_window_parameters (struct window *w, Lisp_Object val)
209 w->window_parameters = val;
211 static void
212 wset_combination (struct window *w, bool horflag, Lisp_Object val)
214 /* Since leaf windows never becomes non-leaf, there should
215 be no buffer and markers in start and pointm fields of W. */
216 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
217 w->contents = val;
218 /* When an internal window is deleted and VAL is nil, HORFLAG
219 is meaningless. */
220 if (!NILP (val))
221 w->horizontal = horflag;
224 /* Nonzero if leaf window W doesn't reflect the actual state
225 of displayed buffer due to its text or overlays change. */
227 bool
228 window_outdated (struct window *w)
230 struct buffer *b = XBUFFER (w->contents);
231 return (w->last_modified < BUF_MODIFF (b)
232 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
235 struct window *
236 decode_live_window (register Lisp_Object window)
238 if (NILP (window))
239 return XWINDOW (selected_window);
241 CHECK_LIVE_WINDOW (window);
242 return XWINDOW (window);
245 struct window *
246 decode_any_window (register Lisp_Object window)
248 struct window *w;
250 if (NILP (window))
251 return XWINDOW (selected_window);
253 CHECK_WINDOW (window);
254 w = XWINDOW (window);
255 return w;
258 static struct window *
259 decode_valid_window (register Lisp_Object window)
261 struct window *w;
263 if (NILP (window))
264 return XWINDOW (selected_window);
266 CHECK_VALID_WINDOW (window);
267 w = XWINDOW (window);
268 return w;
271 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
272 cease its buffer, and 1 means that W is about to set up the new one. */
274 static void
275 adjust_window_count (struct window *w, int arg)
277 eassert (eabs (arg) == 1);
278 if (BUFFERP (w->contents))
280 struct buffer *b = XBUFFER (w->contents);
282 if (b->base_buffer)
283 b = b->base_buffer;
284 b->window_count += arg;
285 eassert (b->window_count >= 0);
286 /* These should be recalculated by redisplay code. */
287 w->window_end_valid = 0;
288 w->base_line_pos = 0;
292 /* Set W's buffer slot to VAL and recompute number
293 of windows showing VAL if it is a buffer. */
295 void
296 wset_buffer (struct window *w, Lisp_Object val)
298 adjust_window_count (w, -1);
299 if (BUFFERP (val))
300 /* Make sure that we do not assign the buffer
301 to an internal window. */
302 eassert (MARKERP (w->start) && MARKERP (w->pointm));
303 w->contents = val;
304 adjust_window_count (w, 1);
307 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
308 doc: /* Return t if OBJECT is a window and nil otherwise. */)
309 (Lisp_Object object)
311 return WINDOWP (object) ? Qt : Qnil;
314 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
315 doc: /* Return t if OBJECT is a valid window and nil otherwise.
316 A valid window is either a window that displays a buffer or an internal
317 window. Deleted windows are not live. */)
318 (Lisp_Object object)
320 return WINDOW_VALID_P (object) ? Qt : Qnil;
323 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
324 doc: /* Return t if OBJECT is a live window and nil otherwise.
325 A live window is a window that displays a buffer.
326 Internal windows and deleted windows are not live. */)
327 (Lisp_Object object)
329 return WINDOW_LIVE_P (object) ? Qt : Qnil;
332 /* Frames and windows. */
333 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
334 doc: /* Return the frame that window WINDOW is on.
335 WINDOW must be a valid window and defaults to the selected one. */)
336 (Lisp_Object window)
338 return decode_valid_window (window)->frame;
341 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
342 doc: /* Return the root window of FRAME-OR-WINDOW.
343 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
344 With a frame argument, return that frame's root window.
345 With a window argument, return the root window of that window's frame. */)
346 (Lisp_Object frame_or_window)
348 Lisp_Object window;
350 if (NILP (frame_or_window))
351 window = SELECTED_FRAME ()->root_window;
352 else if (WINDOW_VALID_P (frame_or_window))
353 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
354 else
356 CHECK_LIVE_FRAME (frame_or_window);
357 window = XFRAME (frame_or_window)->root_window;
360 return window;
363 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
364 doc: /* Return the minibuffer window for frame FRAME.
365 If FRAME is omitted or nil, it defaults to the selected frame. */)
366 (Lisp_Object frame)
368 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
371 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
372 Swindow_minibuffer_p, 0, 1, 0,
373 doc: /* Return non-nil if WINDOW is a minibuffer window.
374 WINDOW must be a valid window and defaults to the selected one. */)
375 (Lisp_Object window)
377 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
380 /* Don't move this to window.el - this must be a safe routine. */
381 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
382 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
383 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
384 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
385 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
386 the first window of that frame. */)
387 (Lisp_Object frame_or_window)
389 Lisp_Object window;
391 if (NILP (frame_or_window))
392 window = SELECTED_FRAME ()->root_window;
393 else if (WINDOW_VALID_P (frame_or_window))
394 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
395 else
397 CHECK_LIVE_FRAME (frame_or_window);
398 window = XFRAME (frame_or_window)->root_window;
401 while (WINDOWP (XWINDOW (window)->contents))
402 window = XWINDOW (window)->contents;
404 return window;
407 DEFUN ("frame-selected-window", Fframe_selected_window,
408 Sframe_selected_window, 0, 1, 0,
409 doc: /* Return the selected window of FRAME-OR-WINDOW.
410 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
411 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
412 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
413 return the selected window of that frame. */)
414 (Lisp_Object frame_or_window)
416 Lisp_Object window;
418 if (NILP (frame_or_window))
419 window = SELECTED_FRAME ()->selected_window;
420 else if (WINDOW_VALID_P (frame_or_window))
421 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
422 else
424 CHECK_LIVE_FRAME (frame_or_window);
425 window = XFRAME (frame_or_window)->selected_window;
428 return window;
431 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
432 Sset_frame_selected_window, 2, 3, 0,
433 doc: /* Set selected window of FRAME to WINDOW.
434 FRAME must be a live frame and defaults to the selected one. If FRAME
435 is the selected frame, this makes WINDOW the selected window. Optional
436 argument NORECORD non-nil means to neither change the order of recently
437 selected windows nor the buffer list. WINDOW must denote a live window.
438 Return WINDOW. */)
439 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
441 if (NILP (frame))
442 frame = selected_frame;
444 CHECK_LIVE_FRAME (frame);
445 CHECK_LIVE_WINDOW (window);
447 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
448 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
450 if (EQ (frame, selected_frame))
451 return Fselect_window (window, norecord);
452 else
454 fset_selected_window (XFRAME (frame), window);
455 return window;
459 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
460 doc: /* Return the selected window.
461 The selected window is the window in which the standard cursor for
462 selected windows appears and to which many commands apply. */)
463 (void)
465 return selected_window;
468 int window_select_count;
470 /* If select_window is called with inhibit_point_swap non-zero it will
471 not store point of the old selected window's buffer back into that
472 window's pointm slot. This is needed by Fset_window_configuration to
473 avoid that the display routine is called with selected_window set to
474 Qnil causing a subsequent crash. */
475 static Lisp_Object
476 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
478 register struct window *w;
479 struct frame *sf;
481 CHECK_LIVE_WINDOW (window);
483 w = XWINDOW (window);
485 /* Make the selected window's buffer current. */
486 Fset_buffer (w->contents);
488 if (EQ (window, selected_window) && !inhibit_point_swap)
489 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
490 way to call record_buffer from Elisp, so it's important that we call
491 record_buffer before returning here. */
492 goto record_and_return;
494 sf = SELECTED_FRAME ();
495 if (XFRAME (WINDOW_FRAME (w)) != sf)
497 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
498 /* Use this rather than Fhandle_switch_frame
499 so that FRAME_FOCUS_FRAME is moved appropriately as we
500 move around in the state where a minibuffer in a separate
501 frame is active. */
502 Fselect_frame (WINDOW_FRAME (w), norecord);
503 /* Fselect_frame called us back so we've done all the work already. */
504 eassert (EQ (window, selected_window));
505 return window;
507 else
508 fset_selected_window (sf, window);
510 select_window_1 (window, inhibit_point_swap);
511 bset_last_selected_window (XBUFFER (w->contents), window);
512 windows_or_buffers_changed++;
514 record_and_return:
515 /* record_buffer can run QUIT, so make sure it is run only after we have
516 re-established the invariant between selected_window and selected_frame,
517 otherwise the temporary broken invariant might "escape" (bug#14161). */
518 if (NILP (norecord))
520 w->use_time = ++window_select_count;
521 record_buffer (w->contents);
524 return window;
527 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
528 (not even for redisplay's benefit), and assume that the window's frame is
529 already selected. */
530 static void
531 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
533 /* Store the old selected window's buffer's point in pointm of the old
534 selected window. It belongs to that window, and when the window is
535 not selected, must be in the window. */
536 if (!inhibit_point_swap)
538 struct window *ow = XWINDOW (selected_window);
539 if (BUFFERP (ow->contents))
540 set_marker_both (ow->pointm, ow->contents,
541 BUF_PT (XBUFFER (ow->contents)),
542 BUF_PT_BYTE (XBUFFER (ow->contents)));
545 selected_window = window;
547 /* Go to the point recorded in the window.
548 This is important when the buffer is in more
549 than one window. It also matters when
550 redisplay_window has altered point after scrolling,
551 because it makes the change only in the window. */
553 register ptrdiff_t new_point = marker_position (XWINDOW (window)->pointm);
554 if (new_point < BEGV)
555 SET_PT (BEGV);
556 else if (new_point > ZV)
557 SET_PT (ZV);
558 else
559 SET_PT (new_point);
563 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
564 doc: /* Select WINDOW which must be a live window.
565 Also make WINDOW's frame the selected frame and WINDOW that frame's
566 selected window. In addition, make WINDOW's buffer current and set that
567 buffer's value of `point' to the value of WINDOW's `window-point'.
568 Return WINDOW.
570 Optional second arg NORECORD non-nil means do not put this buffer at the
571 front of the buffer list and do not make this window the most recently
572 selected one.
574 Note that the main editor command loop sets the current buffer to the
575 buffer of the selected window before each command. */)
576 (register Lisp_Object window, Lisp_Object norecord)
578 return select_window (window, norecord, 0);
581 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
582 doc: /* Return the buffer displayed in window WINDOW.
583 If WINDOW is omitted or nil, it defaults to the selected window.
584 Return nil for an internal window or a deleted window. */)
585 (Lisp_Object window)
587 struct window *w = decode_any_window (window);
588 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
591 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
592 doc: /* Return the parent window of window WINDOW.
593 WINDOW must be a valid window and defaults to the selected one.
594 Return nil for a window with no parent (e.g. a root window). */)
595 (Lisp_Object window)
597 return decode_valid_window (window)->parent;
600 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
601 doc: /* Return the topmost child window of window WINDOW.
602 WINDOW must be a valid window and defaults to the selected one.
603 Return nil if WINDOW is a live window (live windows have no children).
604 Return nil if WINDOW is an internal window whose children form a
605 horizontal combination. */)
606 (Lisp_Object window)
608 struct window *w = decode_valid_window (window);
609 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
612 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
613 doc: /* Return the leftmost child window of window WINDOW.
614 WINDOW must be a valid window and defaults to the selected one.
615 Return nil if WINDOW is a live window (live windows have no children).
616 Return nil if WINDOW is an internal window whose children form a
617 vertical combination. */)
618 (Lisp_Object window)
620 struct window *w = decode_valid_window (window);
621 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
624 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
625 doc: /* Return the next sibling window of window WINDOW.
626 WINDOW must be a valid window and defaults to the selected one.
627 Return nil if WINDOW has no next sibling. */)
628 (Lisp_Object window)
630 return decode_valid_window (window)->next;
633 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
634 doc: /* Return the previous sibling window of window WINDOW.
635 WINDOW must be a valid window and defaults to the selected one.
636 Return nil if WINDOW has no previous sibling. */)
637 (Lisp_Object window)
639 return decode_valid_window (window)->prev;
642 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
643 doc: /* Return combination limit of window WINDOW.
644 WINDOW must be a valid window used in horizontal or vertical combination.
645 If the return value is nil, child windows of WINDOW can be recombined with
646 WINDOW's siblings. A return value of t means that child windows of
647 WINDOW are never \(re-)combined with WINDOW's siblings. */)
648 (Lisp_Object window)
650 struct window *w;
652 CHECK_VALID_WINDOW (window);
653 w = XWINDOW (window);
654 if (WINDOW_LEAF_P (w))
655 error ("Combination limit is meaningful for internal windows only");
656 return w->combination_limit;
659 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
660 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
661 WINDOW must be a valid window used in horizontal or vertical combination.
662 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
663 siblings. LIMIT t means that child windows of WINDOW are never
664 \(re-)combined with WINDOW's siblings. Other values are reserved for
665 future use. */)
666 (Lisp_Object window, Lisp_Object limit)
668 struct window *w;
670 CHECK_VALID_WINDOW (window);
671 w = XWINDOW (window);
672 if (WINDOW_LEAF_P (w))
673 error ("Combination limit is meaningful for internal windows only");
674 wset_combination_limit (w, limit);
675 return limit;
678 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
679 doc: /* Return the use time of window WINDOW.
680 WINDOW must be a live window and defaults to the selected one.
681 The window with the highest use time is the most recently selected
682 one. The window with the lowest use time is the least recently
683 selected one. */)
684 (Lisp_Object window)
686 return make_number (decode_live_window (window)->use_time);
689 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
690 doc: /* Return the total height, in lines, of window WINDOW.
691 WINDOW must be a valid window and defaults to the selected one.
693 The return value includes the mode line and header line, if any.
694 If WINDOW is an internal window, the total height is the height
695 of the screen areas spanned by its children.
697 On a graphical display, this total height is reported as an
698 integer multiple of the default character height. */)
699 (Lisp_Object window)
701 return make_number (decode_valid_window (window)->total_lines);
704 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
705 doc: /* Return the total width, in columns, of window WINDOW.
706 WINDOW must be a valid window and defaults to the selected one.
708 The return value includes any vertical dividers or scroll bars
709 belonging to WINDOW. If WINDOW is an internal window, the total width
710 is the width of the screen areas spanned by its children.
712 On a graphical display, this total width is reported as an
713 integer multiple of the default character width. */)
714 (Lisp_Object window)
716 return make_number (decode_valid_window (window)->total_cols);
719 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
720 doc: /* Return the new total size of window WINDOW.
721 WINDOW must be a valid window and defaults to the selected one. */)
722 (Lisp_Object window)
724 return decode_valid_window (window)->new_total;
727 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
728 doc: /* Return the normal height of window WINDOW.
729 WINDOW must be a valid window and defaults to the selected one.
730 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
731 (Lisp_Object window, Lisp_Object horizontal)
733 struct window *w = decode_valid_window (window);
735 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
738 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
739 doc: /* Return new normal size of window WINDOW.
740 WINDOW must be a valid window and defaults to the selected one. */)
741 (Lisp_Object window)
743 return decode_valid_window (window)->new_normal;
746 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
747 doc: /* Return left column of window WINDOW.
748 This is the distance, in columns, between the left edge of WINDOW and
749 the left edge of the frame's window area. For instance, the return
750 value is 0 if there is no window to the left of WINDOW.
752 WINDOW must be a valid window and defaults to the selected one. */)
753 (Lisp_Object window)
755 return make_number (decode_valid_window (window)->left_col);
758 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
759 doc: /* Return top line of window WINDOW.
760 This is the distance, in lines, between the top of WINDOW and the top
761 of the frame's window area. For instance, the return value is 0 if
762 there is no window above WINDOW.
764 WINDOW must be a valid window and defaults to the selected one. */)
765 (Lisp_Object window)
767 return make_number (decode_valid_window (window)->top_line);
770 /* Return the number of lines of W's body. Don't count any mode or
771 header line of W. */
773 static int
774 window_body_lines (struct window *w)
776 int height = w->total_lines;
778 if (!MINI_WINDOW_P (w))
780 if (WINDOW_WANTS_MODELINE_P (w))
781 --height;
782 if (WINDOW_WANTS_HEADER_LINE_P (w))
783 --height;
786 return height;
789 /* Return the number of columns of W's body. Don't count columns
790 occupied by the scroll bar or the vertical bar separating W from its
791 right sibling. On window-systems don't count fringes or display
792 margins either. */
795 window_body_cols (struct window *w)
797 struct frame *f = XFRAME (WINDOW_FRAME (w));
798 int width = w->total_cols;
800 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
801 /* Scroll bars occupy a few columns. */
802 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
803 else if (!FRAME_WINDOW_P (f)
804 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
805 /* The column of `|' characters separating side-by-side windows
806 occupies one column only. */
807 width -= 1;
809 /* Display margins cannot be used for normal text. */
810 width -= WINDOW_LEFT_MARGIN_COLS (w) + WINDOW_RIGHT_MARGIN_COLS (w);
812 if (FRAME_WINDOW_P (f))
813 /* On window-systems, fringes cannot be used for normal text. */
814 width -= WINDOW_FRINGE_COLS (w);
816 return width;
819 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 1, 0,
820 doc: /* Return the height, in lines, of WINDOW's text area.
821 WINDOW must be a live window and defaults to the selected one.
823 The returned height does not include the mode line or header line.
824 On a graphical display, the height is expressed as an integer multiple
825 of the default character height. If a line at the bottom of the text
826 area is only partially visible, that counts as a whole line; to
827 exclude partially-visible lines, use `window-text-height'. */)
828 (Lisp_Object window)
830 return make_number (window_body_lines (decode_live_window (window)));
833 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 1, 0,
834 doc: /* Return the width, in columns, of WINDOW's text area.
835 WINDOW must be a live window and defaults to the selected one.
837 The return value does not include any vertical dividers, fringe or
838 marginal areas, or scroll bars. On a graphical display, the width is
839 expressed as an integer multiple of the default character width. */)
840 (Lisp_Object window)
842 return make_number (window_body_cols (decode_live_window (window)));
845 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
846 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
847 WINDOW must be a live window and defaults to the selected one. */)
848 (Lisp_Object window)
850 return make_number (decode_live_window (window)->hscroll);
853 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
854 range, returning the new amount as a fixnum. */
855 static Lisp_Object
856 set_window_hscroll (struct window *w, EMACS_INT hscroll)
858 /* Horizontal scrolling has problems with large scroll amounts.
859 It's too slow with long lines, and even with small lines the
860 display can be messed up. For now, though, impose only the limits
861 required by the internal representation: horizontal scrolling must
862 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
863 (since it's stored in a ptrdiff_t). */
864 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
865 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
867 /* Prevent redisplay shortcuts when changing the hscroll. */
868 if (w->hscroll != new_hscroll)
869 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
871 w->hscroll = new_hscroll;
872 return make_number (new_hscroll);
875 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
876 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
877 WINDOW must be a live window and defaults to the selected one.
878 Clip the number to a reasonable value if out of range.
879 Return the new number. NCOL should be zero or positive.
881 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
882 window so that the location of point moves off-window. */)
883 (Lisp_Object window, Lisp_Object ncol)
885 CHECK_NUMBER (ncol);
886 return set_window_hscroll (decode_live_window (window), XINT (ncol));
889 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
890 Swindow_redisplay_end_trigger, 0, 1, 0,
891 doc: /* Return WINDOW's redisplay end trigger value.
892 WINDOW must be a live window and defaults to the selected one.
893 See `set-window-redisplay-end-trigger' for more information. */)
894 (Lisp_Object window)
896 return decode_live_window (window)->redisplay_end_trigger;
899 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
900 Sset_window_redisplay_end_trigger, 2, 2, 0,
901 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
902 WINDOW must be a live window and defaults to the selected one. VALUE
903 should be a buffer position (typically a marker) or nil. If it is a
904 buffer position, then if redisplay in WINDOW reaches a position beyond
905 VALUE, the functions in `redisplay-end-trigger-functions' are called
906 with two arguments: WINDOW, and the end trigger value. Afterwards the
907 end-trigger value is reset to nil. */)
908 (register Lisp_Object window, Lisp_Object value)
910 wset_redisplay_end_trigger (decode_live_window (window), value);
911 return value;
914 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
915 doc: /* Return a list of the edge coordinates of WINDOW.
916 WINDOW must be a valid window and defaults to the selected one.
918 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
919 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
920 0 at top left corner of frame.
922 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
923 is one more than the bottommost row occupied by WINDOW. The edges
924 include the space used by WINDOW's scroll bar, display margins, fringes,
925 header line, and/or mode line. For the edges of just the text area, use
926 `window-inside-edges'. */)
927 (Lisp_Object window)
929 register struct window *w = decode_valid_window (window);
931 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
932 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
935 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
936 doc: /* Return a list of the edge pixel coordinates of WINDOW.
937 WINDOW must be a valid window and defaults to the selected one.
939 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
940 0, 0 at the top left corner of the frame.
942 RIGHT is one more than the rightmost x position occupied by WINDOW.
943 BOTTOM is one more than the bottommost y position occupied by WINDOW.
944 The pixel edges include the space used by WINDOW's scroll bar, display
945 margins, fringes, header line, and/or mode line. For the pixel edges
946 of just the text area, use `window-inside-pixel-edges'. */)
947 (Lisp_Object window)
949 register struct window *w = decode_valid_window (window);
951 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
952 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
955 static void
956 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
958 struct frame *f = XFRAME (w->frame);
959 *add_y = f->top_pos;
960 #ifdef FRAME_MENUBAR_HEIGHT
961 *add_y += FRAME_MENUBAR_HEIGHT (f);
962 #endif
963 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
964 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
965 #elif FRAME_TOOLBAR_HEIGHT
966 *add_y += FRAME_TOOLBAR_HEIGHT (f);
967 #endif
968 #ifdef FRAME_NS_TITLEBAR_HEIGHT
969 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
970 #endif
971 *add_x = f->left_pos;
972 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
973 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
974 #endif
977 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
978 Swindow_absolute_pixel_edges, 0, 1, 0,
979 doc: /* Return a list of the edge pixel coordinates of WINDOW.
980 WINDOW must be a valid window and defaults to the selected one.
982 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
983 0, 0 at the top left corner of the display.
985 RIGHT is one more than the rightmost x position occupied by WINDOW.
986 BOTTOM is one more than the bottommost y position occupied by WINDOW.
987 The pixel edges include the space used by WINDOW's scroll bar, display
988 margins, fringes, header line, and/or mode line. For the pixel edges
989 of just the text area, use `window-inside-absolute-pixel-edges'. */)
990 (Lisp_Object window)
992 register struct window *w = decode_valid_window (window);
993 int add_x, add_y;
995 calc_absolute_offset (w, &add_x, &add_y);
997 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
998 WINDOW_TOP_EDGE_Y (w) + add_y,
999 WINDOW_RIGHT_EDGE_X (w) + add_x,
1000 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
1003 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
1004 doc: /* Return a list of the edge coordinates of WINDOW.
1005 WINDOW must be a live window and defaults to the selected one.
1007 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
1008 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1009 0 at top left corner of frame.
1011 RIGHT is one more than the rightmost column of WINDOW's text area.
1012 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1013 inside edges do not include the space used by the WINDOW's scroll bar,
1014 display margins, fringes, header line, and/or mode line. */)
1015 (Lisp_Object window)
1017 register struct window *w = decode_live_window (window);
1019 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1020 + WINDOW_LEFT_MARGIN_COLS (w)
1021 + WINDOW_LEFT_FRINGE_COLS (w)),
1022 (WINDOW_TOP_EDGE_LINE (w)
1023 + WINDOW_HEADER_LINE_LINES (w)),
1024 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1025 - WINDOW_RIGHT_MARGIN_COLS (w)
1026 - WINDOW_RIGHT_FRINGE_COLS (w)),
1027 (WINDOW_BOTTOM_EDGE_LINE (w)
1028 - WINDOW_MODE_LINE_LINES (w)));
1031 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1032 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1033 WINDOW must be a live window and defaults to the selected one.
1035 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1036 (0,0) at the top left corner of the frame's window area.
1038 RIGHT is one more than the rightmost x position of WINDOW's text area.
1039 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1040 The inside edges do not include the space used by WINDOW's scroll bar,
1041 display margins, fringes, header line, and/or mode line. */)
1042 (Lisp_Object window)
1044 register struct window *w = decode_live_window (window);
1046 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1047 + WINDOW_LEFT_MARGIN_WIDTH (w)
1048 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1049 (WINDOW_TOP_EDGE_Y (w)
1050 + WINDOW_HEADER_LINE_HEIGHT (w)),
1051 (WINDOW_BOX_RIGHT_EDGE_X (w)
1052 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1053 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1054 (WINDOW_BOTTOM_EDGE_Y (w)
1055 - WINDOW_MODE_LINE_HEIGHT (w)));
1058 DEFUN ("window-inside-absolute-pixel-edges",
1059 Fwindow_inside_absolute_pixel_edges,
1060 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1061 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1062 WINDOW must be a live window and defaults to the selected one.
1064 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1065 (0,0) at the top left corner of the frame's window area.
1067 RIGHT is one more than the rightmost x position of WINDOW's text area.
1068 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1069 The inside edges do not include the space used by WINDOW's scroll bar,
1070 display margins, fringes, header line, and/or mode line. */)
1071 (Lisp_Object window)
1073 register struct window *w = decode_live_window (window);
1074 int add_x, add_y;
1076 calc_absolute_offset (w, &add_x, &add_y);
1078 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1079 + WINDOW_LEFT_MARGIN_WIDTH (w)
1080 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1081 (WINDOW_TOP_EDGE_Y (w)
1082 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1083 (WINDOW_BOX_RIGHT_EDGE_X (w)
1084 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1085 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1086 (WINDOW_BOTTOM_EDGE_Y (w)
1087 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1090 /* Test if the character at column X, row Y is within window W.
1091 If it is not, return ON_NOTHING;
1092 if it is in the window's text area, return ON_TEXT;
1093 if it is on the window's modeline, return ON_MODE_LINE;
1094 if it is on the border between the window and its right sibling,
1095 return ON_VERTICAL_BORDER.
1096 if it is on a scroll bar, return ON_SCROLL_BAR.
1097 if it is on the window's top line, return ON_HEADER_LINE;
1098 if it is in left or right fringe of the window,
1099 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1100 if it is in the marginal area to the left/right of the window,
1101 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1103 X and Y are frame relative pixel coordinates. */
1105 static enum window_part
1106 coordinates_in_window (register struct window *w, int x, int y)
1108 struct frame *f = XFRAME (WINDOW_FRAME (w));
1109 enum window_part part;
1110 int ux = FRAME_COLUMN_WIDTH (f);
1111 int left_x = WINDOW_LEFT_EDGE_X (w);
1112 int right_x = WINDOW_RIGHT_EDGE_X (w);
1113 int top_y = WINDOW_TOP_EDGE_Y (w);
1114 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1115 /* The width of the area where the vertical line can be dragged.
1116 (Between mode lines for instance. */
1117 int grabbable_width = ux;
1118 int lmargin_width, rmargin_width, text_left, text_right;
1120 /* Outside any interesting row or column? */
1121 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1122 return ON_NOTHING;
1124 /* On the mode line or header line? */
1125 if ((WINDOW_WANTS_MODELINE_P (w)
1126 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
1127 && (part = ON_MODE_LINE))
1128 || (WINDOW_WANTS_HEADER_LINE_P (w)
1129 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1130 && (part = ON_HEADER_LINE)))
1132 /* If it's under/over the scroll bar portion of the mode/header
1133 line, say it's on the vertical line. That's to be able to
1134 resize windows horizontally in case we're using toolkit scroll
1135 bars. Note: If scrollbars are on the left, the window that
1136 must be eventually resized is that on the left of WINDOW. */
1137 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1138 && !WINDOW_LEFTMOST_P (w)
1139 && eabs (x - left_x) < grabbable_width)
1140 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1141 && !WINDOW_RIGHTMOST_P (w)
1142 && eabs (x - right_x) < grabbable_width))
1143 return ON_VERTICAL_BORDER;
1144 else
1145 return part;
1148 /* In what's below, we subtract 1 when computing right_x because we
1149 want the rightmost pixel, which is given by left_pixel+width-1. */
1150 if (w->pseudo_window_p)
1152 left_x = 0;
1153 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
1155 else
1157 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1158 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1161 /* Outside any interesting column? */
1162 if (x < left_x || x > right_x)
1163 return ON_SCROLL_BAR;
1165 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1166 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1168 text_left = window_box_left (w, TEXT_AREA);
1169 text_right = text_left + window_box_width (w, TEXT_AREA);
1171 if (FRAME_WINDOW_P (f))
1173 if (!w->pseudo_window_p
1174 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1175 && !WINDOW_RIGHTMOST_P (w)
1176 && (eabs (x - right_x) < grabbable_width))
1177 return ON_VERTICAL_BORDER;
1179 /* Need to say "x > right_x" rather than >=, since on character
1180 terminals, the vertical line's x coordinate is right_x. */
1181 else if (!w->pseudo_window_p
1182 && !WINDOW_RIGHTMOST_P (w)
1183 && x > right_x - ux)
1184 return ON_VERTICAL_BORDER;
1186 if (x < text_left)
1188 if (lmargin_width > 0
1189 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1190 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1191 : (x < left_x + lmargin_width)))
1192 return ON_LEFT_MARGIN;
1194 return ON_LEFT_FRINGE;
1197 if (x >= text_right)
1199 if (rmargin_width > 0
1200 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1201 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1202 : (x >= right_x - rmargin_width)))
1203 return ON_RIGHT_MARGIN;
1205 return ON_RIGHT_FRINGE;
1208 /* Everything special ruled out - must be on text area */
1209 return ON_TEXT;
1212 /* Take X is the frame-relative pixel x-coordinate, and return the
1213 x-coordinate relative to part PART of window W. */
1215 window_relative_x_coord (struct window *w, enum window_part part, int x)
1217 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1219 switch (part)
1221 case ON_TEXT:
1222 return x - window_box_left (w, TEXT_AREA);
1224 case ON_LEFT_FRINGE:
1225 return x - left_x;
1227 case ON_RIGHT_FRINGE:
1228 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1230 case ON_LEFT_MARGIN:
1231 return (x - left_x
1232 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1233 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1235 case ON_RIGHT_MARGIN:
1236 return (x + 1
1237 - ((w->pseudo_window_p)
1238 ? WINDOW_TOTAL_WIDTH (w)
1239 : WINDOW_BOX_RIGHT_EDGE_X (w))
1240 + window_box_width (w, RIGHT_MARGIN_AREA)
1241 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1242 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1245 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1246 return 0;
1250 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1251 Scoordinates_in_window_p, 2, 2, 0,
1252 doc: /* Return non-nil if COORDINATES are in WINDOW.
1253 WINDOW must be a live window and defaults to the selected one.
1254 COORDINATES is a cons of the form (X . Y), X and Y being distances
1255 measured in characters from the upper-left corner of the frame.
1256 \(0 . 0) denotes the character in the upper left corner of the
1257 frame.
1258 If COORDINATES are in the text portion of WINDOW,
1259 the coordinates relative to the window are returned.
1260 If they are in the mode line of WINDOW, `mode-line' is returned.
1261 If they are in the top mode line of WINDOW, `header-line' is returned.
1262 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1263 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1264 If they are on the border between WINDOW and its right sibling,
1265 `vertical-line' is returned.
1266 If they are in the windows's left or right marginal areas, `left-margin'\n\
1267 or `right-margin' is returned. */)
1268 (register Lisp_Object coordinates, Lisp_Object window)
1270 struct window *w;
1271 struct frame *f;
1272 int x, y;
1273 Lisp_Object lx, ly;
1275 w = decode_live_window (window);
1276 f = XFRAME (w->frame);
1277 CHECK_CONS (coordinates);
1278 lx = Fcar (coordinates);
1279 ly = Fcdr (coordinates);
1280 CHECK_NUMBER_OR_FLOAT (lx);
1281 CHECK_NUMBER_OR_FLOAT (ly);
1282 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1283 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1285 switch (coordinates_in_window (w, x, y))
1287 case ON_NOTHING:
1288 return Qnil;
1290 case ON_TEXT:
1291 /* Convert X and Y to window relative pixel coordinates, and
1292 return the canonical char units. */
1293 x -= window_box_left (w, TEXT_AREA);
1294 y -= WINDOW_TOP_EDGE_Y (w);
1295 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1296 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1298 case ON_MODE_LINE:
1299 return Qmode_line;
1301 case ON_VERTICAL_BORDER:
1302 return Qvertical_line;
1304 case ON_HEADER_LINE:
1305 return Qheader_line;
1307 case ON_LEFT_FRINGE:
1308 return Qleft_fringe;
1310 case ON_RIGHT_FRINGE:
1311 return Qright_fringe;
1313 case ON_LEFT_MARGIN:
1314 return Qleft_margin;
1316 case ON_RIGHT_MARGIN:
1317 return Qright_margin;
1319 case ON_SCROLL_BAR:
1320 /* Historically we are supposed to return nil in this case. */
1321 return Qnil;
1323 default:
1324 emacs_abort ();
1329 /* Callback for foreach_window, used in window_from_coordinates.
1330 Check if window W contains coordinates specified by USER_DATA which
1331 is actually a pointer to a struct check_window_data CW.
1333 Check if window W contains coordinates *CW->x and *CW->y. If it
1334 does, return W in *CW->window, as Lisp_Object, and return in
1335 *CW->part the part of the window under coordinates *X,*Y. Return
1336 zero from this function to stop iterating over windows. */
1338 struct check_window_data
1340 Lisp_Object *window;
1341 int x, y;
1342 enum window_part *part;
1345 static int
1346 check_window_containing (struct window *w, void *user_data)
1348 struct check_window_data *cw = user_data;
1349 enum window_part found;
1350 int continue_p = 1;
1352 found = coordinates_in_window (w, cw->x, cw->y);
1353 if (found != ON_NOTHING)
1355 *cw->part = found;
1356 XSETWINDOW (*cw->window, w);
1357 continue_p = 0;
1360 return continue_p;
1364 /* Find the window containing frame-relative pixel position X/Y and
1365 return it as a Lisp_Object.
1367 If X, Y is on one of the window's special `window_part' elements,
1368 set *PART to the id of that element.
1370 If there is no window under X, Y return nil and leave *PART
1371 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1373 This function was previously implemented with a loop cycling over
1374 windows with Fnext_window, and starting with the frame's selected
1375 window. It turned out that this doesn't work with an
1376 implementation of next_window using Vwindow_list, because
1377 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1378 tree of F when this function is called asynchronously from
1379 note_mouse_highlight. The original loop didn't terminate in this
1380 case. */
1382 Lisp_Object
1383 window_from_coordinates (struct frame *f, int x, int y,
1384 enum window_part *part, bool tool_bar_p)
1386 Lisp_Object window;
1387 struct check_window_data cw;
1388 enum window_part dummy;
1390 if (part == 0)
1391 part = &dummy;
1393 window = Qnil;
1394 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1395 foreach_window (f, check_window_containing, &cw);
1397 /* If not found above, see if it's in the tool bar window, if a tool
1398 bar exists. */
1399 if (NILP (window)
1400 && tool_bar_p
1401 && WINDOWP (f->tool_bar_window)
1402 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1403 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1404 != ON_NOTHING))
1406 *part = ON_TEXT;
1407 window = f->tool_bar_window;
1410 return window;
1413 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1414 doc: /* Return window containing coordinates X and Y on FRAME.
1415 FRAME must be a live frame and defaults to the selected one.
1416 The top left corner of the frame is considered to be row 0,
1417 column 0. */)
1418 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1420 struct frame *f = decode_live_frame (frame);
1422 /* Check that arguments are integers or floats. */
1423 CHECK_NUMBER_OR_FLOAT (x);
1424 CHECK_NUMBER_OR_FLOAT (y);
1426 return window_from_coordinates (f,
1427 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1428 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1429 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1430 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1431 0, 0);
1434 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1435 doc: /* Return current value of point in WINDOW.
1436 WINDOW must be a live window and defaults to the selected one.
1438 For a nonselected window, this is the value point would have if that
1439 window were selected.
1441 Note that, when WINDOW is selected, the value returned is the same as
1442 that returned by `point' for WINDOW's buffer. It would be more strictly
1443 correct to return the top-level value of `point', outside of any
1444 `save-excursion' forms. But that is hard to define. */)
1445 (Lisp_Object window)
1447 register struct window *w = decode_live_window (window);
1449 if (w == XWINDOW (selected_window))
1450 return make_number (BUF_PT (XBUFFER (w->contents)));
1451 else
1452 return Fmarker_position (w->pointm);
1455 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1456 doc: /* Return position at which display currently starts in WINDOW.
1457 WINDOW must be a live window and defaults to the selected one.
1458 This is updated by redisplay or by calling `set-window-start'. */)
1459 (Lisp_Object window)
1461 return Fmarker_position (decode_live_window (window)->start);
1464 /* This is text temporarily removed from the doc string below.
1466 This function returns nil if the position is not currently known.
1467 That happens when redisplay is preempted and doesn't finish.
1468 If in that case you want to compute where the end of the window would
1469 have been if redisplay had finished, do this:
1470 (save-excursion
1471 (goto-char (window-start window))
1472 (vertical-motion (1- (window-height window)) window)
1473 (point))") */
1475 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1476 doc: /* Return position at which display currently ends in WINDOW.
1477 WINDOW must be a live window and defaults to the selected one.
1478 This is updated by redisplay, when it runs to completion.
1479 Simply changing the buffer text or setting `window-start'
1480 does not update this value.
1481 Return nil if there is no recorded value. (This can happen if the
1482 last redisplay of WINDOW was preempted, and did not finish.)
1483 If UPDATE is non-nil, compute the up-to-date position
1484 if it isn't already recorded. */)
1485 (Lisp_Object window, Lisp_Object update)
1487 Lisp_Object value;
1488 struct window *w = decode_live_window (window);
1489 Lisp_Object buf;
1490 struct buffer *b;
1492 buf = w->contents;
1493 CHECK_BUFFER (buf);
1494 b = XBUFFER (buf);
1496 if (! NILP (update)
1497 && (windows_or_buffers_changed
1498 || !w->window_end_valid
1499 || b->clip_changed
1500 || b->prevent_redisplay_optimizations_p
1501 || window_outdated (w))
1502 && !noninteractive)
1504 struct text_pos startp;
1505 ptrdiff_t charpos = marker_position (w->start);
1506 struct it it;
1507 struct buffer *old_buffer = NULL;
1508 void *itdata = NULL;
1510 /* Cannot use Fvertical_motion because that function doesn't
1511 cope with variable-height lines. */
1512 if (b != current_buffer)
1514 old_buffer = current_buffer;
1515 set_buffer_internal (b);
1518 /* In case W->start is out of the range, use something
1519 reasonable. This situation occurred when loading a file with
1520 `-l' containing a call to `rmail' with subsequent other
1521 commands. At the end, W->start happened to be BEG, while
1522 rmail had already narrowed the buffer. */
1523 if (charpos < BEGV)
1524 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1525 else if (charpos > ZV)
1526 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1527 else
1528 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1530 itdata = bidi_shelve_cache ();
1531 start_display (&it, w, startp);
1532 move_it_vertically (&it, window_box_height (w));
1533 if (it.current_y < it.last_visible_y)
1534 move_it_past_eol (&it);
1535 value = make_number (IT_CHARPOS (it));
1536 bidi_unshelve_cache (itdata, 0);
1538 if (old_buffer)
1539 set_buffer_internal (old_buffer);
1541 else
1542 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1544 return value;
1547 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1548 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1549 WINDOW must be a live window and defaults to the selected one.
1550 Return POS. */)
1551 (Lisp_Object window, Lisp_Object pos)
1553 register struct window *w = decode_live_window (window);
1555 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1557 if (w == XWINDOW (selected_window))
1559 if (XBUFFER (w->contents) == current_buffer)
1560 Fgoto_char (pos);
1561 else
1563 struct buffer *old_buffer = current_buffer;
1565 /* ... but here we want to catch type error before buffer change. */
1566 CHECK_NUMBER_COERCE_MARKER (pos);
1567 set_buffer_internal (XBUFFER (w->contents));
1568 Fgoto_char (pos);
1569 set_buffer_internal (old_buffer);
1572 else
1574 set_marker_restricted (w->pointm, pos, w->contents);
1575 /* We have to make sure that redisplay updates the window to show
1576 the new value of point. */
1577 ++windows_or_buffers_changed;
1580 return pos;
1583 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1584 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1585 WINDOW must be a live window and defaults to the selected one. Return
1586 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1587 overriding motion of point in order to display at this exact start. */)
1588 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1590 register struct window *w = decode_live_window (window);
1592 set_marker_restricted (w->start, pos, w->contents);
1593 /* This is not right, but much easier than doing what is right. */
1594 w->start_at_line_beg = 0;
1595 if (NILP (noforce))
1596 w->force_start = 1;
1597 w->update_mode_line = 1;
1598 if (w != XWINDOW (selected_window))
1599 /* Enforce full redisplay. FIXME: make it more selective. */
1600 windows_or_buffers_changed++;
1602 return pos;
1605 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1606 Spos_visible_in_window_p, 0, 3, 0,
1607 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1608 WINDOW must be a live window and defaults to the selected one.
1610 Return nil if that position is scrolled vertically out of view. If a
1611 character is only partially visible, nil is returned, unless the
1612 optional argument PARTIALLY is non-nil. If POS is only out of view
1613 because of horizontal scrolling, return non-nil. If POS is t, it
1614 specifies the position of the last visible glyph in WINDOW. POS
1615 defaults to point in WINDOW; WINDOW defaults to the selected window.
1617 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1618 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1619 where X and Y are the pixel coordinates relative to the top left corner
1620 of the window. The remaining elements are omitted if the character after
1621 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1622 off-window at the top and bottom of the screen line ("row") containing
1623 POS, ROWH is the visible height of that row, and VPOS is the row number
1624 \(zero-based). */)
1625 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1627 register struct window *w;
1628 register EMACS_INT posint;
1629 register struct buffer *buf;
1630 struct text_pos top;
1631 Lisp_Object in_window = Qnil;
1632 int rtop, rbot, rowh, vpos, fully_p = 1;
1633 int x, y;
1635 w = decode_live_window (window);
1636 buf = XBUFFER (w->contents);
1637 SET_TEXT_POS_FROM_MARKER (top, w->start);
1639 if (EQ (pos, Qt))
1640 posint = -1;
1641 else if (!NILP (pos))
1643 CHECK_NUMBER_COERCE_MARKER (pos);
1644 posint = XINT (pos);
1646 else if (w == XWINDOW (selected_window))
1647 posint = PT;
1648 else
1649 posint = marker_position (w->pointm);
1651 /* If position is above window start or outside buffer boundaries,
1652 or if window start is out of range, position is not visible. */
1653 if ((EQ (pos, Qt)
1654 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1655 && CHARPOS (top) >= BUF_BEGV (buf)
1656 && CHARPOS (top) <= BUF_ZV (buf)
1657 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1658 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1659 in_window = Qt;
1661 if (!NILP (in_window) && !NILP (partially))
1663 Lisp_Object part = Qnil;
1664 if (!fully_p)
1665 part = list4i (rtop, rbot, rowh, vpos);
1666 in_window = Fcons (make_number (x),
1667 Fcons (make_number (y), part));
1670 return in_window;
1673 DEFUN ("window-line-height", Fwindow_line_height,
1674 Swindow_line_height, 0, 2, 0,
1675 doc: /* Return height in pixels of text line LINE in window WINDOW.
1676 WINDOW must be a live window and defaults to the selected one.
1678 Return height of current line if LINE is omitted or nil. Return height of
1679 header or mode line if LINE is `header-line' or `mode-line'.
1680 Otherwise, LINE is a text line number starting from 0. A negative number
1681 counts from the end of the window.
1683 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1684 in pixels of the visible part of the line, VPOS and YPOS are the
1685 vertical position in lines and pixels of the line, relative to the top
1686 of the first text line, and OFFBOT is the number of off-window pixels at
1687 the bottom of the text line. If there are off-window pixels at the top
1688 of the (first) text line, YPOS is negative.
1690 Return nil if window display is not up-to-date. In that case, use
1691 `pos-visible-in-window-p' to obtain the information. */)
1692 (Lisp_Object line, Lisp_Object window)
1694 register struct window *w;
1695 register struct buffer *b;
1696 struct glyph_row *row, *end_row;
1697 int max_y, crop, i;
1698 EMACS_INT n;
1700 w = decode_live_window (window);
1702 if (noninteractive || w->pseudo_window_p)
1703 return Qnil;
1705 CHECK_BUFFER (w->contents);
1706 b = XBUFFER (w->contents);
1708 /* Fail if current matrix is not up-to-date. */
1709 if (!w->window_end_valid
1710 || windows_or_buffers_changed
1711 || b->clip_changed
1712 || b->prevent_redisplay_optimizations_p
1713 || window_outdated (w))
1714 return Qnil;
1716 if (NILP (line))
1718 i = w->cursor.vpos;
1719 if (i < 0 || i >= w->current_matrix->nrows
1720 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1721 return Qnil;
1722 max_y = window_text_bottom_y (w);
1723 goto found_row;
1726 if (EQ (line, Qheader_line))
1728 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1729 return Qnil;
1730 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1731 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1734 if (EQ (line, Qmode_line))
1736 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1737 return (row->enabled_p ?
1738 list4i (row->height,
1739 0, /* not accurate */
1740 (WINDOW_HEADER_LINE_HEIGHT (w)
1741 + window_text_bottom_y (w)),
1743 : Qnil);
1746 CHECK_NUMBER (line);
1747 n = XINT (line);
1749 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1750 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1751 max_y = window_text_bottom_y (w);
1752 i = 0;
1754 while ((n < 0 || i < n)
1755 && row <= end_row && row->enabled_p
1756 && row->y + row->height < max_y)
1757 row++, i++;
1759 if (row > end_row || !row->enabled_p)
1760 return Qnil;
1762 if (++n < 0)
1764 if (-n > i)
1765 return Qnil;
1766 row += n;
1767 i += n;
1770 found_row:
1771 crop = max (0, (row->y + row->height) - max_y);
1772 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1775 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1776 0, 1, 0,
1777 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1778 More precisely, return the value assigned by the last call of
1779 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1780 never called with WINDOW as its argument, or the value set by that
1781 function was internally reset since its last call. WINDOW must be a
1782 live window and defaults to the selected one.
1784 When a window is dedicated to its buffer, `display-buffer' will refrain
1785 from displaying another buffer in it. `get-lru-window' and
1786 `get-largest-window' treat dedicated windows specially.
1787 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1788 `kill-buffer' can delete a dedicated window and the containing frame.
1790 Functions like `set-window-buffer' may change the buffer displayed by a
1791 window, unless that window is "strongly" dedicated to its buffer, that
1792 is the value returned by `window-dedicated-p' is t. */)
1793 (Lisp_Object window)
1795 return decode_live_window (window)->dedicated;
1798 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1799 Sset_window_dedicated_p, 2, 2, 0,
1800 doc: /* Mark WINDOW as dedicated according to FLAG.
1801 WINDOW must be a live window and defaults to the selected one. FLAG
1802 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1803 mark WINDOW as non-dedicated. Return FLAG.
1805 When a window is dedicated to its buffer, `display-buffer' will refrain
1806 from displaying another buffer in it. `get-lru-window' and
1807 `get-largest-window' treat dedicated windows specially.
1808 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1809 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1810 and the containing frame.
1812 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1813 its buffer. Functions like `set-window-buffer' may change the buffer
1814 displayed by a window, unless that window is strongly dedicated to its
1815 buffer. If and when `set-window-buffer' displays another buffer in a
1816 window, it also makes sure that the window is no more dedicated. */)
1817 (Lisp_Object window, Lisp_Object flag)
1819 wset_dedicated (decode_live_window (window), flag);
1820 return flag;
1823 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1824 0, 1, 0,
1825 doc: /* Return buffers previously shown in WINDOW.
1826 WINDOW must be a live window and defaults to the selected one.
1828 The return value is a list of elements (BUFFER WINDOW-START POS),
1829 where BUFFER is a buffer, WINDOW-START is the start position of the
1830 window for that buffer, and POS is a window-specific point value. */)
1831 (Lisp_Object window)
1833 return decode_live_window (window)->prev_buffers;
1836 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1837 Sset_window_prev_buffers, 2, 2, 0,
1838 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1839 WINDOW must be a live window and defaults to the selected one.
1841 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1842 where BUFFER is a buffer, WINDOW-START is the start position of the
1843 window for that buffer, and POS is a window-specific point value. */)
1844 (Lisp_Object window, Lisp_Object prev_buffers)
1846 wset_prev_buffers (decode_live_window (window), prev_buffers);
1847 return prev_buffers;
1850 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1851 0, 1, 0,
1852 doc: /* Return list of buffers recently re-shown in WINDOW.
1853 WINDOW must be a live window and defaults to the selected one. */)
1854 (Lisp_Object window)
1856 return decode_live_window (window)->next_buffers;
1859 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1860 Sset_window_next_buffers, 2, 2, 0,
1861 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1862 WINDOW must be a live window and defaults to the selected one.
1863 NEXT-BUFFERS should be a list of buffers. */)
1864 (Lisp_Object window, Lisp_Object next_buffers)
1866 wset_next_buffers (decode_live_window (window), next_buffers);
1867 return next_buffers;
1870 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1871 0, 1, 0,
1872 doc: /* Return the parameters of WINDOW and their values.
1873 WINDOW must be a valid window and defaults to the selected one. The
1874 return value is a list of elements of the form (PARAMETER . VALUE). */)
1875 (Lisp_Object window)
1877 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1880 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1881 2, 2, 0,
1882 doc: /* Return WINDOW's value for PARAMETER.
1883 WINDOW can be any window and defaults to the selected one. */)
1884 (Lisp_Object window, Lisp_Object parameter)
1886 Lisp_Object result;
1888 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1889 return CDR_SAFE (result);
1892 DEFUN ("set-window-parameter", Fset_window_parameter,
1893 Sset_window_parameter, 3, 3, 0,
1894 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1895 WINDOW can be any window and defaults to the selected one.
1896 Return VALUE. */)
1897 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1899 register struct window *w = decode_any_window (window);
1900 Lisp_Object old_alist_elt;
1902 old_alist_elt = Fassq (parameter, w->window_parameters);
1903 if (NILP (old_alist_elt))
1904 wset_window_parameters
1905 (w, Fcons (Fcons (parameter, value), w->window_parameters));
1906 else
1907 Fsetcdr (old_alist_elt, value);
1908 return value;
1911 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1912 0, 1, 0,
1913 doc: /* Return the display-table that WINDOW is using.
1914 WINDOW must be a live window and defaults to the selected one. */)
1915 (Lisp_Object window)
1917 return decode_live_window (window)->display_table;
1920 /* Get the display table for use on window W. This is either W's
1921 display table or W's buffer's display table. Ignore the specified
1922 tables if they are not valid; if no valid table is specified,
1923 return 0. */
1925 struct Lisp_Char_Table *
1926 window_display_table (struct window *w)
1928 struct Lisp_Char_Table *dp = NULL;
1930 if (DISP_TABLE_P (w->display_table))
1931 dp = XCHAR_TABLE (w->display_table);
1932 else if (BUFFERP (w->contents))
1934 struct buffer *b = XBUFFER (w->contents);
1936 if (DISP_TABLE_P (BVAR (b, display_table)))
1937 dp = XCHAR_TABLE (BVAR (b, display_table));
1938 else if (DISP_TABLE_P (Vstandard_display_table))
1939 dp = XCHAR_TABLE (Vstandard_display_table);
1942 return dp;
1945 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1946 doc: /* Set WINDOW's display-table to TABLE.
1947 WINDOW must be a live window and defaults to the selected one. */)
1948 (register Lisp_Object window, Lisp_Object table)
1950 wset_display_table (decode_live_window (window), table);
1951 return table;
1954 /* Record info on buffer window W is displaying
1955 when it is about to cease to display that buffer. */
1956 static void
1957 unshow_buffer (register struct window *w)
1959 Lisp_Object buf = w->contents;
1960 struct buffer *b = XBUFFER (buf);
1962 eassert (b == XMARKER (w->pointm)->buffer);
1964 #if 0
1965 if (w == XWINDOW (selected_window)
1966 || ! EQ (buf, XWINDOW (selected_window)->contents))
1967 /* Do this except when the selected window's buffer
1968 is being removed from some other window. */
1969 #endif
1970 /* last_window_start records the start position that this buffer
1971 had in the last window to be disconnected from it.
1972 Now that this statement is unconditional,
1973 it is possible for the buffer to be displayed in the
1974 selected window, while last_window_start reflects another
1975 window which was recently showing the same buffer.
1976 Some people might say that might be a good thing. Let's see. */
1977 b->last_window_start = marker_position (w->start);
1979 /* Point in the selected window's buffer
1980 is actually stored in that buffer, and the window's pointm isn't used.
1981 So don't clobber point in that buffer. */
1982 if (! EQ (buf, XWINDOW (selected_window)->contents)
1983 /* Don't clobber point in current buffer either (this could be
1984 useful in connection with bug#12208).
1985 && XBUFFER (buf) != current_buffer */
1986 /* This line helps to fix Horsley's testbug.el bug. */
1987 && !(WINDOWP (BVAR (b, last_selected_window))
1988 && w != XWINDOW (BVAR (b, last_selected_window))
1989 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
1990 temp_set_point_both (b,
1991 clip_to_bounds (BUF_BEGV (b),
1992 marker_position (w->pointm),
1993 BUF_ZV (b)),
1994 clip_to_bounds (BUF_BEGV_BYTE (b),
1995 marker_byte_position (w->pointm),
1996 BUF_ZV_BYTE (b)));
1998 if (WINDOWP (BVAR (b, last_selected_window))
1999 && w == XWINDOW (BVAR (b, last_selected_window)))
2000 bset_last_selected_window (b, Qnil);
2003 /* Put NEW into the window structure in place of OLD. SETFLAG zero
2004 means change window structure only. Otherwise store geometry and
2005 other settings as well. */
2006 static void
2007 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2009 register Lisp_Object tem;
2010 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2012 /* If OLD is its frame's root window, then NEW is the new
2013 root window for that frame. */
2014 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2015 fset_root_window (XFRAME (o->frame), new);
2017 if (setflag)
2019 n->left_col = o->left_col;
2020 n->top_line = o->top_line;
2021 n->total_cols = o->total_cols;
2022 n->total_lines = o->total_lines;
2023 wset_normal_cols (n, o->normal_cols);
2024 wset_normal_cols (o, make_float (1.0));
2025 wset_normal_lines (n, o->normal_lines);
2026 wset_normal_lines (o, make_float (1.0));
2027 n->desired_matrix = n->current_matrix = 0;
2028 n->vscroll = 0;
2029 memset (&n->cursor, 0, sizeof (n->cursor));
2030 memset (&n->last_cursor, 0, sizeof (n->last_cursor));
2031 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2032 n->phys_cursor_type = -1;
2033 n->phys_cursor_width = -1;
2034 n->must_be_updated_p = 0;
2035 n->pseudo_window_p = 0;
2036 n->window_end_vpos = 0;
2037 n->window_end_pos = 0;
2038 n->window_end_valid = 0;
2041 tem = o->next;
2042 wset_next (n, tem);
2043 if (!NILP (tem))
2044 wset_prev (XWINDOW (tem), new);
2046 tem = o->prev;
2047 wset_prev (n, tem);
2048 if (!NILP (tem))
2049 wset_next (XWINDOW (tem), new);
2051 tem = o->parent;
2052 wset_parent (n, tem);
2053 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2054 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2057 /* If window WINDOW and its parent window are iso-combined, merge
2058 WINDOW's children into those of its parent window and mark WINDOW as
2059 deleted. */
2061 static void
2062 recombine_windows (Lisp_Object window)
2064 struct window *w, *p, *c;
2065 Lisp_Object parent, child;
2066 bool horflag;
2068 w = XWINDOW (window);
2069 parent = w->parent;
2070 if (!NILP (parent) && NILP (w->combination_limit))
2072 p = XWINDOW (parent);
2073 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2074 && p->horizontal == w->horizontal)
2075 /* WINDOW and PARENT are both either a vertical or a horizontal
2076 combination. */
2078 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2079 child = w->contents;
2080 c = XWINDOW (child);
2082 /* Splice WINDOW's children into its parent's children and
2083 assign new normal sizes. */
2084 if (NILP (w->prev))
2085 wset_combination (p, horflag, child);
2086 else
2088 wset_prev (c, w->prev);
2089 wset_next (XWINDOW (w->prev), child);
2092 while (c)
2094 wset_parent (c, parent);
2096 if (horflag)
2097 wset_normal_cols (c,
2098 make_float ((double) c->total_cols
2099 / (double) p->total_cols));
2100 else
2101 wset_normal_lines (c,
2102 make_float ((double) c->total_lines
2103 / (double) p->total_lines));
2105 if (NILP (c->next))
2107 if (!NILP (w->next))
2109 wset_next (c, w->next);
2110 wset_prev (XWINDOW (c->next), child);
2113 c = 0;
2115 else
2117 child = c->next;
2118 c = XWINDOW (child);
2122 /* WINDOW can be deleted now. */
2123 wset_combination (w, 0, Qnil);
2128 /* If WINDOW can be deleted, delete it. */
2129 static void
2130 delete_deletable_window (Lisp_Object window)
2132 if (!NILP (call1 (Qwindow_deletable_p, window)))
2133 call1 (Qdelete_window, window);
2136 /***********************************************************************
2137 Window List
2138 ***********************************************************************/
2140 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2141 pointer. This is a callback function for foreach_window, used in
2142 the window_list function. */
2144 static int
2145 add_window_to_list (struct window *w, void *user_data)
2147 Lisp_Object *list = user_data;
2148 Lisp_Object window;
2149 XSETWINDOW (window, w);
2150 *list = Fcons (window, *list);
2151 return 1;
2155 /* Return a list of all windows, for use by next_window. If
2156 Vwindow_list is a list, return that list. Otherwise, build a new
2157 list, cache it in Vwindow_list, and return that. */
2159 static Lisp_Object
2160 window_list (void)
2162 if (!CONSP (Vwindow_list))
2164 Lisp_Object tail, frame;
2166 Vwindow_list = Qnil;
2167 FOR_EACH_FRAME (tail, frame)
2169 Lisp_Object args[2];
2171 /* We are visiting windows in canonical order, and add
2172 new windows at the front of args[1], which means we
2173 have to reverse this list at the end. */
2174 args[1] = Qnil;
2175 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2176 args[0] = Vwindow_list;
2177 args[1] = Fnreverse (args[1]);
2178 Vwindow_list = Fnconc (2, args);
2182 return Vwindow_list;
2186 /* Value is non-zero if WINDOW satisfies the constraints given by
2187 OWINDOW, MINIBUF and ALL_FRAMES.
2189 MINIBUF t means WINDOW may be minibuffer windows.
2190 `lambda' means WINDOW may not be a minibuffer window.
2191 a window means a specific minibuffer window
2193 ALL_FRAMES t means search all frames,
2194 nil means search just current frame,
2195 `visible' means search just visible frames on the
2196 current terminal,
2197 0 means search visible and iconified frames on the
2198 current terminal,
2199 a window means search the frame that window belongs to,
2200 a frame means consider windows on that frame, only. */
2202 static bool
2203 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2204 Lisp_Object minibuf, Lisp_Object all_frames)
2206 struct window *w = XWINDOW (window);
2207 struct frame *f = XFRAME (w->frame);
2208 bool candidate_p = 1;
2210 if (!BUFFERP (w->contents))
2211 candidate_p = 0;
2212 else if (MINI_WINDOW_P (w)
2213 && (EQ (minibuf, Qlambda)
2214 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2216 /* If MINIBUF is `lambda' don't consider any mini-windows.
2217 If it is a window, consider only that one. */
2218 candidate_p = 0;
2220 else if (EQ (all_frames, Qt))
2221 candidate_p = 1;
2222 else if (NILP (all_frames))
2224 eassert (WINDOWP (owindow));
2225 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2227 else if (EQ (all_frames, Qvisible))
2229 candidate_p = FRAME_VISIBLE_P (f)
2230 && (FRAME_TERMINAL (XFRAME (w->frame))
2231 == FRAME_TERMINAL (XFRAME (selected_frame)));
2234 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2236 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2237 #ifdef HAVE_X_WINDOWS
2238 /* Yuck!! If we've just created the frame and the
2239 window-manager requested the user to place it
2240 manually, the window may still not be considered
2241 `visible'. I'd argue it should be at least
2242 something like `iconified', but don't know how to do
2243 that yet. --Stef */
2244 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2245 && !f->output_data.x->has_been_visible)
2246 #endif
2248 && (FRAME_TERMINAL (XFRAME (w->frame))
2249 == FRAME_TERMINAL (XFRAME (selected_frame)));
2251 else if (WINDOWP (all_frames))
2252 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2253 || EQ (XWINDOW (all_frames)->frame, w->frame)
2254 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2255 else if (FRAMEP (all_frames))
2256 candidate_p = EQ (all_frames, w->frame);
2258 return candidate_p;
2262 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2263 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2264 MINIBUF, and ALL_FRAMES. */
2266 static void
2267 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2269 struct window *w = decode_live_window (*window);
2271 XSETWINDOW (*window, w);
2272 /* MINIBUF nil may or may not include minibuffers. Decide if it
2273 does. */
2274 if (NILP (*minibuf))
2275 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2276 else if (!EQ (*minibuf, Qt))
2277 *minibuf = Qlambda;
2279 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2280 => count none of them, or a specific minibuffer window (the
2281 active one) to count. */
2283 /* ALL_FRAMES nil doesn't specify which frames to include. */
2284 if (NILP (*all_frames))
2285 *all_frames
2286 = (!EQ (*minibuf, Qlambda)
2287 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2288 : Qnil);
2289 else if (EQ (*all_frames, Qvisible))
2291 else if (EQ (*all_frames, make_number (0)))
2293 else if (FRAMEP (*all_frames))
2295 else if (!EQ (*all_frames, Qt))
2296 *all_frames = Qnil;
2300 /* Return the next or previous window of WINDOW in cyclic ordering
2301 of windows. NEXT_P non-zero means return the next window. See the
2302 documentation string of next-window for the meaning of MINIBUF and
2303 ALL_FRAMES. */
2305 static Lisp_Object
2306 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2308 decode_next_window_args (&window, &minibuf, &all_frames);
2310 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2311 return the first window on the frame. */
2312 if (FRAMEP (all_frames)
2313 && !EQ (all_frames, XWINDOW (window)->frame))
2314 return Fframe_first_window (all_frames);
2316 if (next_p)
2318 Lisp_Object list;
2320 /* Find WINDOW in the list of all windows. */
2321 list = Fmemq (window, window_list ());
2323 /* Scan forward from WINDOW to the end of the window list. */
2324 if (CONSP (list))
2325 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2326 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2327 break;
2329 /* Scan from the start of the window list up to WINDOW. */
2330 if (!CONSP (list))
2331 for (list = Vwindow_list;
2332 CONSP (list) && !EQ (XCAR (list), window);
2333 list = XCDR (list))
2334 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2335 break;
2337 if (CONSP (list))
2338 window = XCAR (list);
2340 else
2342 Lisp_Object candidate, list;
2344 /* Scan through the list of windows for candidates. If there are
2345 candidate windows in front of WINDOW, the last one of these
2346 is the one we want. If there are candidates following WINDOW
2347 in the list, again the last one of these is the one we want. */
2348 candidate = Qnil;
2349 for (list = window_list (); CONSP (list); list = XCDR (list))
2351 if (EQ (XCAR (list), window))
2353 if (WINDOWP (candidate))
2354 break;
2356 else if (candidate_window_p (XCAR (list), window, minibuf,
2357 all_frames))
2358 candidate = XCAR (list);
2361 if (WINDOWP (candidate))
2362 window = candidate;
2365 return window;
2369 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2370 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2371 WINDOW must be a live window and defaults to the selected one. The
2372 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2373 consider.
2375 MINIBUF nil or omitted means consider the minibuffer window only if the
2376 minibuffer is active. MINIBUF t means consider the minibuffer window
2377 even if the minibuffer is not active. Any other value means do not
2378 consider the minibuffer window even if the minibuffer is active.
2380 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2381 plus the minibuffer window if specified by the MINIBUF argument. If the
2382 minibuffer counts, consider all windows on all frames that share that
2383 minibuffer too. The following non-nil values of ALL-FRAMES have special
2384 meanings:
2386 - t means consider all windows on all existing frames.
2388 - `visible' means consider all windows on all visible frames.
2390 - 0 (the number zero) means consider all windows on all visible and
2391 iconified frames.
2393 - A frame means consider all windows on that frame only.
2395 Anything else means consider all windows on WINDOW's frame and no
2396 others.
2398 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2399 `next-window' to iterate through the entire cycle of acceptable
2400 windows, eventually ending up back at the window you started with.
2401 `previous-window' traverses the same cycle, in the reverse order. */)
2402 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2404 return next_window (window, minibuf, all_frames, 1);
2408 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2409 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2410 WINDOW must be a live window and defaults to the selected one. The
2411 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2412 consider.
2414 MINIBUF nil or omitted means consider the minibuffer window only if the
2415 minibuffer is active. MINIBUF t means consider the minibuffer window
2416 even if the minibuffer is not active. Any other value means do not
2417 consider the minibuffer window even if the minibuffer is active.
2419 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2420 plus the minibuffer window if specified by the MINIBUF argument. If the
2421 minibuffer counts, consider all windows on all frames that share that
2422 minibuffer too. The following non-nil values of ALL-FRAMES have special
2423 meanings:
2425 - t means consider all windows on all existing frames.
2427 - `visible' means consider all windows on all visible frames.
2429 - 0 (the number zero) means consider all windows on all visible and
2430 iconified frames.
2432 - A frame means consider all windows on that frame only.
2434 Anything else means consider all windows on WINDOW's frame and no
2435 others.
2437 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2438 use `previous-window' to iterate through the entire cycle of
2439 acceptable windows, eventually ending up back at the window you
2440 started with. `next-window' traverses the same cycle, in the
2441 reverse order. */)
2442 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2444 return next_window (window, minibuf, all_frames, 0);
2448 /* Return a list of windows in cyclic ordering. Arguments are like
2449 for `next-window'. */
2451 static Lisp_Object
2452 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2454 Lisp_Object tail, list, rest;
2456 decode_next_window_args (&window, &minibuf, &all_frames);
2457 list = Qnil;
2459 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2460 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2461 list = Fcons (XCAR (tail), list);
2463 /* Rotate the list to start with WINDOW. */
2464 list = Fnreverse (list);
2465 rest = Fmemq (window, list);
2466 if (!NILP (rest) && !EQ (rest, list))
2468 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2470 XSETCDR (tail, Qnil);
2471 list = nconc2 (rest, list);
2473 return list;
2477 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2478 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2479 FRAME nil or omitted means use the selected frame.
2480 WINDOW nil or omitted means use the window selected within FRAME.
2481 MINIBUF t means include the minibuffer window, even if it isn't active.
2482 MINIBUF nil or omitted means include the minibuffer window only
2483 if it's active.
2484 MINIBUF neither nil nor t means never include the minibuffer window. */)
2485 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2487 if (NILP (window))
2488 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2489 CHECK_WINDOW (window);
2490 if (NILP (frame))
2491 frame = selected_frame;
2493 if (!EQ (frame, XWINDOW (window)->frame))
2494 error ("Window is on a different frame");
2496 return window_list_1 (window, minibuf, frame);
2500 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2501 doc: /* Return a list of all live windows.
2502 WINDOW specifies the first window to list and defaults to the selected
2503 window.
2505 Optional argument MINIBUF nil or omitted means consider the minibuffer
2506 window only if the minibuffer is active. MINIBUF t means consider the
2507 minibuffer window even if the minibuffer is not active. Any other value
2508 means do not consider the minibuffer window even if the minibuffer is
2509 active.
2511 Optional argument ALL-FRAMES nil or omitted means consider all windows
2512 on WINDOW's frame, plus the minibuffer window if specified by the
2513 MINIBUF argument. If the minibuffer counts, consider all windows on all
2514 frames that share that minibuffer too. The following non-nil values of
2515 ALL-FRAMES have special meanings:
2517 - t means consider all windows on all existing frames.
2519 - `visible' means consider all windows on all visible frames.
2521 - 0 (the number zero) means consider all windows on all visible and
2522 iconified frames.
2524 - A frame means consider all windows on that frame only.
2526 Anything else means consider all windows on WINDOW's frame and no
2527 others.
2529 If WINDOW is not on the list of windows returned, some other window will
2530 be listed first but no error is signaled. */)
2531 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2533 return window_list_1 (window, minibuf, all_frames);
2536 /* Look at all windows, performing an operation specified by TYPE
2537 with argument OBJ.
2538 If FRAMES is Qt, look at all frames;
2539 Qnil, look at just the selected frame;
2540 Qvisible, look at visible frames;
2541 a frame, just look at windows on that frame.
2542 If MINI is non-zero, perform the operation on minibuffer windows too. */
2544 enum window_loop
2546 WINDOW_LOOP_UNUSED,
2547 GET_BUFFER_WINDOW, /* Arg is buffer */
2548 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2549 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2550 CHECK_ALL_WINDOWS /* Arg is ignored */
2553 static Lisp_Object
2554 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2556 Lisp_Object window, windows, best_window, frame_arg;
2557 int frame_best_window_flag = 0;
2558 struct frame *f;
2559 struct gcpro gcpro1;
2561 /* If we're only looping through windows on a particular frame,
2562 frame points to that frame. If we're looping through windows
2563 on all frames, frame is 0. */
2564 if (FRAMEP (frames))
2565 f = XFRAME (frames);
2566 else if (NILP (frames))
2567 f = SELECTED_FRAME ();
2568 else
2569 f = NULL;
2571 if (f)
2572 frame_arg = Qlambda;
2573 else if (EQ (frames, make_number (0)))
2574 frame_arg = frames;
2575 else if (EQ (frames, Qvisible))
2576 frame_arg = frames;
2577 else
2578 frame_arg = Qt;
2580 /* frame_arg is Qlambda to stick to one frame,
2581 Qvisible to consider all visible frames,
2582 or Qt otherwise. */
2584 /* Pick a window to start with. */
2585 if (WINDOWP (obj))
2586 window = obj;
2587 else if (f)
2588 window = FRAME_SELECTED_WINDOW (f);
2589 else
2590 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2592 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2593 GCPRO1 (windows);
2594 best_window = Qnil;
2596 for (; CONSP (windows); windows = XCDR (windows))
2598 struct window *w;
2600 window = XCAR (windows);
2601 w = XWINDOW (window);
2603 /* Note that we do not pay attention here to whether the frame
2604 is visible, since Fwindow_list skips non-visible frames if
2605 that is desired, under the control of frame_arg. */
2606 if (!MINI_WINDOW_P (w)
2607 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2608 consider all windows. */
2609 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2610 || (mini && minibuf_level > 0))
2611 switch (type)
2613 case GET_BUFFER_WINDOW:
2614 if (EQ (w->contents, obj)
2615 /* Don't find any minibuffer window except the one that
2616 is currently in use. */
2617 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2619 if (EQ (window, selected_window))
2620 /* Preferably return the selected window. */
2621 RETURN_UNGCPRO (window);
2622 else if (EQ (XWINDOW (window)->frame, selected_frame)
2623 && !frame_best_window_flag)
2624 /* Prefer windows on the current frame (but don't
2625 choose another one if we have one already). */
2627 best_window = window;
2628 frame_best_window_flag = 1;
2630 else if (NILP (best_window))
2631 best_window = window;
2633 break;
2635 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2636 /* We could simply check whether the buffer shown by window
2637 is live, and show another buffer in case it isn't. */
2638 if (EQ (w->contents, obj))
2640 /* Undedicate WINDOW. */
2641 wset_dedicated (w, Qnil);
2642 /* Make WINDOW show the buffer returned by
2643 other_buffer_safely, don't run any hooks. */
2644 set_window_buffer
2645 (window, other_buffer_safely (w->contents), 0, 0);
2646 /* If WINDOW is the selected window, make its buffer
2647 current. But do so only if the window shows the
2648 current buffer (Bug#6454). */
2649 if (EQ (window, selected_window)
2650 && XBUFFER (w->contents) == current_buffer)
2651 Fset_buffer (w->contents);
2653 break;
2655 case REDISPLAY_BUFFER_WINDOWS:
2656 if (EQ (w->contents, obj))
2658 mark_window_display_accurate (window, 0);
2659 w->update_mode_line = 1;
2660 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2661 ++update_mode_lines;
2662 best_window = window;
2664 break;
2666 /* Check for a leaf window that has a killed buffer
2667 or broken markers. */
2668 case CHECK_ALL_WINDOWS:
2669 if (BUFFERP (w->contents))
2671 struct buffer *b = XBUFFER (w->contents);
2673 if (!BUFFER_LIVE_P (b))
2674 emacs_abort ();
2675 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2676 emacs_abort ();
2677 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2678 emacs_abort ();
2680 break;
2682 case WINDOW_LOOP_UNUSED:
2683 break;
2687 UNGCPRO;
2688 return best_window;
2691 /* Used for debugging. Abort if any window has a dead buffer. */
2693 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2694 void
2695 check_all_windows (void)
2697 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2700 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2701 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2702 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2703 the current buffer.
2705 The optional argument ALL-FRAMES specifies the frames to consider:
2707 - t means consider all windows on all existing frames.
2709 - `visible' means consider all windows on all visible frames.
2711 - 0 (the number zero) means consider all windows on all visible
2712 and iconified frames.
2714 - A frame means consider all windows on that frame only.
2716 Any other value of ALL-FRAMES means consider all windows on the
2717 selected frame and no others. */)
2718 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2720 Lisp_Object buffer;
2722 if (NILP (buffer_or_name))
2723 buffer = Fcurrent_buffer ();
2724 else
2725 buffer = Fget_buffer (buffer_or_name);
2727 if (BUFFERP (buffer))
2728 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2729 else
2730 return Qnil;
2733 static Lisp_Object
2734 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2736 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2740 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2741 Sdelete_other_windows_internal, 0, 2, "",
2742 doc: /* Make WINDOW fill its frame.
2743 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2744 and defaults to the selected one.
2746 Optional argument ROOT, if non-nil, must specify an internal window such
2747 that WINDOW is in its window subtree. If this is the case, replace ROOT
2748 by WINDOW and leave alone any windows not part of ROOT's subtree.
2750 When WINDOW is live try to reduce display jumps by keeping the text
2751 previously visible in WINDOW in the same place on the frame. Doing this
2752 depends on the value of (window-start WINDOW), so if calling this
2753 function in a program gives strange scrolling, make sure the
2754 window-start value is reasonable when this function is called. */)
2755 (Lisp_Object window, Lisp_Object root)
2757 struct window *w, *r, *s;
2758 struct frame *f;
2759 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2760 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2761 int top IF_LINT (= 0), new_top, resize_failed;
2763 w = decode_valid_window (window);
2764 XSETWINDOW (window, w);
2765 f = XFRAME (w->frame);
2767 if (NILP (root))
2768 /* ROOT is the frame's root window. */
2770 root = FRAME_ROOT_WINDOW (f);
2771 r = XWINDOW (root);
2773 else
2774 /* ROOT must be an ancestor of WINDOW. */
2776 r = decode_valid_window (root);
2777 pwindow = XWINDOW (window)->parent;
2778 while (!NILP (pwindow))
2779 if (EQ (pwindow, root))
2780 break;
2781 else
2782 pwindow = XWINDOW (pwindow)->parent;
2783 if (!EQ (pwindow, root))
2784 error ("Specified root is not an ancestor of specified window");
2787 if (EQ (window, root))
2788 /* A noop. */
2789 return Qnil;
2790 /* I don't understand the "top > 0" part below. If we deal with a
2791 standalone minibuffer it would have been caught by the preceding
2792 test. */
2793 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2794 error ("Can't expand minibuffer to full frame");
2796 if (BUFFERP (w->contents))
2798 startpos = marker_position (w->start);
2799 startbyte = marker_byte_position (w->start);
2800 top = WINDOW_TOP_EDGE_LINE (w)
2801 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2802 /* Make sure WINDOW is the frame's selected window. */
2803 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2805 if (EQ (selected_frame, w->frame))
2806 Fselect_window (window, Qnil);
2807 else
2808 fset_selected_window (f, window);
2811 else
2813 /* See if the frame's selected window is a part of the window
2814 subtree rooted at WINDOW, by finding all the selected window's
2815 parents and comparing each one with WINDOW. If it isn't we
2816 need a new selected window for this frame. */
2817 swindow = FRAME_SELECTED_WINDOW (f);
2818 while (1)
2820 pwindow = swindow;
2821 while (!NILP (pwindow) && !EQ (window, pwindow))
2822 pwindow = XWINDOW (pwindow)->parent;
2824 if (EQ (window, pwindow))
2825 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2826 as the new selected window. */
2827 break;
2828 else
2829 /* Else try the previous window of SWINDOW. */
2830 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2833 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2835 if (EQ (selected_frame, w->frame))
2836 Fselect_window (swindow, Qnil);
2837 else
2838 fset_selected_window (f, swindow);
2842 block_input ();
2843 if (!FRAME_INITIAL_P (f))
2845 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2847 /* We are going to free the glyph matrices of WINDOW, and with
2848 that we might lose any information about glyph rows that have
2849 some of their glyphs highlighted in mouse face. (These rows
2850 are marked with a non-zero mouse_face_p flag.) If WINDOW
2851 indeed has some glyphs highlighted in mouse face, signal to
2852 frame's up-to-date hook that mouse highlight was overwritten,
2853 so that it will arrange for redisplaying the highlight. */
2854 if (EQ (hlinfo->mouse_face_window, window))
2856 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
2857 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
2858 hlinfo->mouse_face_window = Qnil;
2861 free_window_matrices (r);
2863 windows_or_buffers_changed++;
2864 Vwindow_list = Qnil;
2865 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2866 resize_failed = 0;
2868 if (!WINDOW_LEAF_P (w))
2870 /* Resize child windows vertically. */
2871 XSETINT (delta, r->total_lines - w->total_lines);
2872 w->top_line = r->top_line;
2873 resize_root_window (window, delta, Qnil, Qnil);
2874 if (window_resize_check (w, 0))
2875 window_resize_apply (w, 0);
2876 else
2878 resize_root_window (window, delta, Qnil, Qt);
2879 if (window_resize_check (w, 0))
2880 window_resize_apply (w, 0);
2881 else
2882 resize_failed = 1;
2885 /* Resize child windows horizontally. */
2886 if (!resize_failed)
2888 w->left_col = r->left_col;
2889 XSETINT (delta, r->total_cols - w->total_cols);
2890 resize_root_window (window, delta, Qt, Qnil);
2891 if (window_resize_check (w, 1))
2892 window_resize_apply (w, 1);
2893 else
2895 resize_root_window (window, delta, Qt, Qt);
2896 if (window_resize_check (w, 1))
2897 window_resize_apply (w, 1);
2898 else
2899 resize_failed = 1;
2903 if (resize_failed)
2904 /* Play safe, if we still can ... */
2906 window = swindow;
2907 w = XWINDOW (window);
2911 /* Cleanly unlink WINDOW from window-tree. */
2912 if (!NILP (w->prev))
2913 /* Get SIBLING above (on the left of) WINDOW. */
2915 sibling = w->prev;
2916 s = XWINDOW (sibling);
2917 wset_next (s, w->next);
2918 if (!NILP (s->next))
2919 wset_prev (XWINDOW (s->next), sibling);
2921 else
2922 /* Get SIBLING below (on the right of) WINDOW. */
2924 sibling = w->next;
2925 s = XWINDOW (sibling);
2926 wset_prev (s, Qnil);
2927 wset_combination (XWINDOW (w->parent),
2928 XWINDOW (w->parent)->horizontal, sibling);
2931 /* Delete ROOT and all child windows of ROOT. */
2932 if (WINDOWP (r->contents))
2934 delete_all_child_windows (r->contents);
2935 wset_combination (r, 0, Qnil);
2938 replace_window (root, window, 1);
2940 /* This must become SWINDOW anyway ....... */
2941 if (BUFFERP (w->contents) && !resize_failed)
2943 /* Try to minimize scrolling, by setting the window start to the
2944 point will cause the text at the old window start to be at the
2945 same place on the frame. But don't try to do this if the
2946 window start is outside the visible portion (as might happen
2947 when the display is not current, due to typeahead). */
2948 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2949 if (new_top != top
2950 && startpos >= BUF_BEGV (XBUFFER (w->contents))
2951 && startpos <= BUF_ZV (XBUFFER (w->contents)))
2953 struct position pos;
2954 struct buffer *obuf = current_buffer;
2956 Fset_buffer (w->contents);
2957 /* This computation used to temporarily move point, but that
2958 can have unwanted side effects due to text properties. */
2959 pos = *vmotion (startpos, startbyte, -top, w);
2961 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
2962 w->window_end_valid = 0;
2963 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
2964 || FETCH_BYTE (pos.bytepos - 1) == '\n');
2965 /* We need to do this, so that the window-scroll-functions
2966 get called. */
2967 w->optional_new_start = 1;
2969 set_buffer_internal (obuf);
2973 adjust_glyphs (f);
2974 unblock_input ();
2976 run_window_configuration_change_hook (f);
2978 return Qnil;
2982 void
2983 replace_buffer_in_windows (Lisp_Object buffer)
2985 call1 (Qreplace_buffer_in_windows, buffer);
2988 /* If BUFFER is shown in a window, safely replace it with some other
2989 buffer in all windows of all frames, even those on other keyboards. */
2991 void
2992 replace_buffer_in_windows_safely (Lisp_Object buffer)
2994 if (buffer_window_count (XBUFFER (buffer)))
2996 Lisp_Object tail, frame;
2998 /* A single call to window_loop won't do the job because it only
2999 considers frames on the current keyboard. So loop manually over
3000 frames, and handle each one. */
3001 FOR_EACH_FRAME (tail, frame)
3002 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3006 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
3007 minimum allowable size. */
3009 void
3010 check_frame_size (struct frame *frame, int *rows, int *cols)
3012 /* For height, we have to see:
3013 how many windows the frame has at minimum (one or two),
3014 and whether it has a menu bar or other special stuff at the top. */
3015 int min_height
3016 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3017 ? MIN_SAFE_WINDOW_HEIGHT
3018 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3020 if (FRAME_TOP_MARGIN (frame) > 0)
3021 min_height += FRAME_TOP_MARGIN (frame);
3023 if (*rows < min_height)
3024 *rows = min_height;
3025 if (*cols < MIN_SAFE_WINDOW_WIDTH)
3026 *cols = MIN_SAFE_WINDOW_WIDTH;
3029 /* Adjust the margins of window W if text area is too small.
3030 Return 1 if window width is ok after adjustment; 0 if window
3031 is still too narrow. */
3033 static int
3034 adjust_window_margins (struct window *w)
3036 int box_cols = (WINDOW_TOTAL_COLS (w)
3037 - WINDOW_FRINGE_COLS (w)
3038 - WINDOW_SCROLL_BAR_COLS (w));
3039 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
3040 + WINDOW_RIGHT_MARGIN_COLS (w));
3042 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
3043 return 1;
3045 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
3046 return 0;
3048 /* Window's text area is too narrow, but reducing the window
3049 margins will fix that. */
3050 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
3051 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
3053 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
3054 w->left_margin_cols = w->right_margin_cols = margin_cols / 2;
3055 else
3056 w->right_margin_cols = margin_cols;
3058 else
3059 w->left_margin_cols = margin_cols;
3060 return 1;
3063 /* The following three routines are needed for running a window's
3064 configuration change hook. */
3065 static void
3066 run_funs (Lisp_Object funs)
3068 for (; CONSP (funs); funs = XCDR (funs))
3069 if (!EQ (XCAR (funs), Qt))
3070 call0 (XCAR (funs));
3073 static void
3074 select_window_norecord (Lisp_Object window)
3076 if (WINDOW_LIVE_P (window))
3077 Fselect_window (window, Qt);
3080 static void
3081 select_frame_norecord (Lisp_Object frame)
3083 if (FRAME_LIVE_P (XFRAME (frame)))
3084 Fselect_frame (frame, Qt);
3087 void
3088 run_window_configuration_change_hook (struct frame *f)
3090 ptrdiff_t count = SPECPDL_INDEX ();
3091 Lisp_Object frame, global_wcch
3092 = Fdefault_value (Qwindow_configuration_change_hook);
3093 XSETFRAME (frame, f);
3095 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3096 return;
3098 /* Use the right buffer. Matters when running the local hooks. */
3099 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3101 record_unwind_current_buffer ();
3102 Fset_buffer (Fwindow_buffer (Qnil));
3105 if (SELECTED_FRAME () != f)
3107 record_unwind_protect (select_frame_norecord, selected_frame);
3108 select_frame_norecord (frame);
3111 /* Look for buffer-local values. */
3113 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3114 for (; CONSP (windows); windows = XCDR (windows))
3116 Lisp_Object window = XCAR (windows);
3117 Lisp_Object buffer = Fwindow_buffer (window);
3118 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3119 buffer)))
3121 ptrdiff_t inner_count = SPECPDL_INDEX ();
3122 record_unwind_protect (select_window_norecord, selected_window);
3123 select_window_norecord (window);
3124 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3125 buffer));
3126 unbind_to (inner_count, Qnil);
3131 run_funs (global_wcch);
3132 unbind_to (count, Qnil);
3135 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3136 Srun_window_configuration_change_hook, 0, 1, 0,
3137 doc: /* Run `window-configuration-change-hook' for FRAME.
3138 If FRAME is omitted or nil, it defaults to the selected frame. */)
3139 (Lisp_Object frame)
3141 run_window_configuration_change_hook (decode_live_frame (frame));
3142 return Qnil;
3145 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3146 means it's allowed to run hooks. See make_frame for a case where
3147 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3148 margins, fringes, and scroll-bar settings of the window are not
3149 reset from the buffer's local settings. */
3151 void
3152 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3153 bool run_hooks_p, bool keep_margins_p)
3155 struct window *w = XWINDOW (window);
3156 struct buffer *b = XBUFFER (buffer);
3157 ptrdiff_t count = SPECPDL_INDEX ();
3158 int samebuf = EQ (buffer, w->contents);
3160 wset_buffer (w, buffer);
3162 if (EQ (window, selected_window))
3163 bset_last_selected_window (b, window);
3165 /* Let redisplay errors through. */
3166 b->display_error_modiff = 0;
3168 /* Update time stamps of buffer display. */
3169 if (INTEGERP (BVAR (b, display_count)))
3170 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3171 bset_display_time (b, Fcurrent_time ());
3173 w->window_end_pos = 0;
3174 w->window_end_vpos = 0;
3175 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3177 if (!(keep_margins_p && samebuf))
3178 { /* If we're not actually changing the buffer, don't reset hscroll and
3179 vscroll. This case happens for example when called from
3180 change_frame_size_1, where we use a dummy call to
3181 Fset_window_buffer on the frame's selected window (and no other)
3182 just in order to run window-configuration-change-hook.
3183 Resetting hscroll and vscroll here is problematic for things like
3184 image-mode and doc-view-mode since it resets the image's position
3185 whenever we resize the frame. */
3186 w->hscroll = w->min_hscroll = 0;
3187 w->vscroll = 0;
3188 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3189 set_marker_restricted (w->start,
3190 make_number (b->last_window_start),
3191 buffer);
3192 w->start_at_line_beg = 0;
3193 w->force_start = 0;
3195 /* Maybe we could move this into the `if' but it's not obviously safe and
3196 I doubt it's worth the trouble. */
3197 windows_or_buffers_changed++;
3199 /* We must select BUFFER for running the window-scroll-functions. */
3200 /* We can't check ! NILP (Vwindow_scroll_functions) here
3201 because that might itself be a local variable. */
3202 if (window_initialized)
3204 record_unwind_current_buffer ();
3205 Fset_buffer (buffer);
3208 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3210 if (!keep_margins_p)
3212 /* Set left and right marginal area width etc. from buffer. */
3213 set_window_fringes (w, BVAR (b, left_fringe_width),
3214 BVAR (b, right_fringe_width),
3215 BVAR (b, fringes_outside_margins));
3216 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3217 BVAR (b, vertical_scroll_bar_type), Qnil);
3218 set_window_margins (w, BVAR (b, left_margin_cols),
3219 BVAR (b, right_margin_cols));
3220 apply_window_adjustment (w);
3223 if (run_hooks_p)
3225 if (! NILP (Vwindow_scroll_functions))
3226 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3227 Fmarker_position (w->start));
3228 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3231 unbind_to (count, Qnil);
3234 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3235 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3236 WINDOW must be a live window and defaults to the selected one.
3237 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3239 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3240 display margins, fringe widths, and scroll bar settings are preserved;
3241 the default is to reset these from the local settings for BUFFER-OR-NAME
3242 or the frame defaults. Return nil.
3244 This function throws an error when WINDOW is strongly dedicated to its
3245 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3246 already display BUFFER-OR-NAME.
3248 This function runs `window-scroll-functions' before running
3249 `window-configuration-change-hook'. */)
3250 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3252 register Lisp_Object tem, buffer;
3253 register struct window *w = decode_live_window (window);
3255 XSETWINDOW (window, w);
3256 buffer = Fget_buffer (buffer_or_name);
3257 CHECK_BUFFER (buffer);
3258 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3259 error ("Attempt to display deleted buffer");
3261 tem = w->contents;
3262 if (NILP (tem))
3263 error ("Window is deleted");
3264 else
3266 if (!EQ (tem, buffer))
3268 if (EQ (w->dedicated, Qt))
3269 /* WINDOW is strongly dedicated to its buffer, signal an
3270 error. */
3271 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3272 else
3273 /* WINDOW is weakly dedicated to its buffer, reset
3274 dedication. */
3275 wset_dedicated (w, Qnil);
3277 call1 (Qrecord_window_buffer, window);
3280 unshow_buffer (w);
3283 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3285 return Qnil;
3288 static Lisp_Object
3289 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3291 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3294 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3295 0, 1, 0,
3296 doc: /* Force all windows to be updated on next redisplay.
3297 If optional arg OBJECT is a window, force redisplay of that window only.
3298 If OBJECT is a buffer or buffer name, force redisplay of all windows
3299 displaying that buffer. */)
3300 (Lisp_Object object)
3302 if (NILP (object))
3304 windows_or_buffers_changed++;
3305 update_mode_lines++;
3306 return Qt;
3309 if (WINDOWP (object))
3311 struct window *w = XWINDOW (object);
3312 mark_window_display_accurate (object, 0);
3313 w->update_mode_line = 1;
3314 if (BUFFERP (w->contents))
3315 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3316 ++update_mode_lines;
3317 return Qt;
3320 if (STRINGP (object))
3321 object = Fget_buffer (object);
3322 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3323 && buffer_window_count (XBUFFER (object)))
3325 /* If buffer is live and shown in at least one window, find
3326 all windows showing this buffer and force update of them. */
3327 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3328 return NILP (object) ? Qnil : Qt;
3331 /* If nothing suitable was found, just return.
3332 We could signal an error, but this feature will typically be used
3333 asynchronously in timers or process sentinels, so we don't. */
3334 return Qnil;
3337 /* Obsolete since 24.3. */
3338 void
3339 temp_output_buffer_show (register Lisp_Object buf)
3341 register struct buffer *old = current_buffer;
3342 register Lisp_Object window;
3343 register struct window *w;
3345 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3347 Fset_buffer (buf);
3348 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3349 BEGV = BEG;
3350 ZV = Z;
3351 SET_PT (BEG);
3352 set_buffer_internal (old);
3354 if (!NILP (Vtemp_buffer_show_function))
3355 call1 (Vtemp_buffer_show_function, buf);
3356 else
3358 window = display_buffer (buf, Qnil, Qnil);
3360 if (!EQ (XWINDOW (window)->frame, selected_frame))
3361 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3362 Vminibuf_scroll_window = window;
3363 w = XWINDOW (window);
3364 w->hscroll = 0;
3365 w->min_hscroll = 0;
3366 set_marker_restricted_both (w->start, buf, BEG, BEG);
3367 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3369 /* Run temp-buffer-show-hook, with the chosen window selected
3370 and its buffer current. */
3372 ptrdiff_t count = SPECPDL_INDEX ();
3373 Lisp_Object prev_window, prev_buffer;
3374 prev_window = selected_window;
3375 XSETBUFFER (prev_buffer, old);
3377 /* Select the window that was chosen, for running the hook.
3378 Note: Both Fselect_window and select_window_norecord may
3379 set-buffer to the buffer displayed in the window,
3380 so we need to save the current buffer. --stef */
3381 record_unwind_protect (restore_buffer, prev_buffer);
3382 record_unwind_protect (select_window_norecord, prev_window);
3383 Fselect_window (window, Qt);
3384 Fset_buffer (w->contents);
3385 Frun_hooks (1, &Qtemp_buffer_show_hook);
3386 unbind_to (count, Qnil);
3391 /* Make new window, have it replace WINDOW in window-tree, and make
3392 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3393 horizontal child). */
3394 static void
3395 make_parent_window (Lisp_Object window, bool horflag)
3397 Lisp_Object parent;
3398 register struct window *o, *p;
3400 o = XWINDOW (window);
3401 p = allocate_window ();
3402 memcpy ((char *) p + sizeof (struct vectorlike_header),
3403 (char *) o + sizeof (struct vectorlike_header),
3404 word_size * VECSIZE (struct window));
3405 /* P's buffer slot may change from nil to a buffer... */
3406 adjust_window_count (p, 1);
3407 XSETWINDOW (parent, p);
3409 replace_window (window, parent, 1);
3411 wset_next (o, Qnil);
3412 wset_prev (o, Qnil);
3413 wset_parent (o, parent);
3414 /* ...but now P becomes an internal window. */
3415 wset_start (p, Qnil);
3416 wset_pointm (p, Qnil);
3417 wset_buffer (p, Qnil);
3418 wset_combination (p, horflag, window);
3419 wset_combination_limit (p, Qnil);
3420 wset_window_parameters (p, Qnil);
3423 /* Make new window from scratch. */
3424 Lisp_Object
3425 make_window (void)
3427 Lisp_Object window;
3428 register struct window *w;
3430 w = allocate_window ();
3431 /* Initialize Lisp data. Note that allocate_window initializes all
3432 Lisp data to nil, so do it only for slots which should not be nil. */
3433 wset_normal_lines (w, make_float (1.0));
3434 wset_normal_cols (w, make_float (1.0));
3435 wset_new_total (w, make_number (0));
3436 wset_new_normal (w, make_number (0));
3437 wset_start (w, Fmake_marker ());
3438 wset_pointm (w, Fmake_marker ());
3439 wset_vertical_scroll_bar_type (w, Qt);
3440 /* These Lisp fields are marked specially so they're not set to nil by
3441 allocate_window. */
3442 wset_prev_buffers (w, Qnil);
3443 wset_next_buffers (w, Qnil);
3445 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3446 non-Lisp data, so do it only for slots which should not be zero. */
3447 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3448 w->left_fringe_width = w->right_fringe_width = -1;
3449 w->phys_cursor_type = -1;
3450 w->phys_cursor_width = -1;
3451 w->scroll_bar_width = -1;
3452 w->column_number_displayed = -1;
3454 /* Reset window_list. */
3455 Vwindow_list = Qnil;
3456 /* Return window. */
3457 XSETWINDOW (window, w);
3458 return window;
3461 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3462 doc: /* Set new total size of WINDOW to SIZE.
3463 WINDOW must be a valid window and defaults to the selected one.
3464 Return SIZE.
3466 Optional argument ADD non-nil means add SIZE to the new total size of
3467 WINDOW and return the sum.
3469 Note: This function does not operate on any child windows of WINDOW. */)
3470 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3472 struct window *w = decode_valid_window (window);
3474 CHECK_NUMBER (size);
3475 if (NILP (add))
3476 wset_new_total (w, size);
3477 else
3478 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3480 return w->new_total;
3483 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3484 doc: /* Set new normal size of WINDOW to SIZE.
3485 WINDOW must be a valid window and defaults to the selected one.
3486 Return SIZE.
3488 Note: This function does not operate on any child windows of WINDOW. */)
3489 (Lisp_Object window, Lisp_Object size)
3491 wset_new_normal (decode_valid_window (window), size);
3492 return size;
3495 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3496 non-zero) to w->new_total would result in correct heights (widths)
3497 for window W and recursively all child windows of W.
3499 Note: This function does not check any of `window-fixed-size-p',
3500 `window-min-height' or `window-min-width'. It does check that window
3501 sizes do not drop below one line (two columns). */
3502 static int
3503 window_resize_check (struct window *w, bool horflag)
3505 struct window *c;
3507 if (WINDOW_VERTICAL_COMBINATION_P (w))
3508 /* W is a vertical combination. */
3510 c = XWINDOW (w->contents);
3511 if (horflag)
3512 /* All child windows of W must have the same width as W. */
3514 while (c)
3516 if ((XINT (c->new_total) != XINT (w->new_total))
3517 || !window_resize_check (c, horflag))
3518 return 0;
3519 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3521 return 1;
3523 else
3524 /* The sum of the heights of the child windows of W must equal
3525 W's height. */
3527 int sum_of_sizes = 0;
3528 while (c)
3530 if (!window_resize_check (c, horflag))
3531 return 0;
3532 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3533 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3535 return (sum_of_sizes == XINT (w->new_total));
3538 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3539 /* W is a horizontal combination. */
3541 c = XWINDOW (w->contents);
3542 if (horflag)
3543 /* The sum of the widths of the child windows of W must equal W's
3544 width. */
3546 int sum_of_sizes = 0;
3547 while (c)
3549 if (!window_resize_check (c, horflag))
3550 return 0;
3551 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3552 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3554 return (sum_of_sizes == XINT (w->new_total));
3556 else
3557 /* All child windows of W must have the same height as W. */
3559 while (c)
3561 if ((XINT (c->new_total) != XINT (w->new_total))
3562 || !window_resize_check (c, horflag))
3563 return 0;
3564 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3566 return 1;
3569 else
3570 /* A leaf window. Make sure it's not too small. The following
3571 hardcodes the values of `window-safe-min-width' (2) and
3572 `window-safe-min-height' (1) which are defined in window.el. */
3573 return XINT (w->new_total) >= (horflag ? 2 : 1);
3576 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3577 w->new_total for window W and recursively all child windows of W.
3578 Also calculate and assign the new vertical (horizontal) start
3579 positions of each of these windows.
3581 This function does not perform any error checks. Make sure you have
3582 run window_resize_check on W before applying this function. */
3583 static void
3584 window_resize_apply (struct window *w, bool horflag)
3586 struct window *c;
3587 int pos;
3589 /* Note: Assigning new_normal requires that the new total size of the
3590 parent window has been set *before*. */
3591 if (horflag)
3593 w->total_cols = XFASTINT (w->new_total);
3594 if (NUMBERP (w->new_normal))
3595 wset_normal_cols (w, w->new_normal);
3597 pos = w->left_col;
3599 else
3601 w->total_lines = XFASTINT (w->new_total);
3602 if (NUMBERP (w->new_normal))
3603 wset_normal_lines (w, w->new_normal);
3605 pos = w->top_line;
3608 if (WINDOW_VERTICAL_COMBINATION_P (w))
3609 /* W is a vertical combination. */
3611 c = XWINDOW (w->contents);
3612 while (c)
3614 if (horflag)
3615 c->left_col = pos;
3616 else
3617 c->top_line = pos;
3618 window_resize_apply (c, horflag);
3619 if (!horflag)
3620 pos = pos + c->total_lines;
3621 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3624 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3625 /* W is a horizontal combination. */
3627 c = XWINDOW (w->contents);
3628 while (c)
3630 if (horflag)
3631 c->left_col = pos;
3632 else
3633 c->top_line = pos;
3634 window_resize_apply (c, horflag);
3635 if (horflag)
3636 pos = pos + c->total_cols;
3637 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3643 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3644 doc: /* Apply requested size values for window-tree of FRAME.
3645 If FRAME is omitted or nil, it defaults to the selected frame.
3647 Optional argument HORIZONTAL omitted or nil means apply requested
3648 height values. HORIZONTAL non-nil means apply requested width values.
3650 This function checks whether the requested values sum up to a valid
3651 window layout, recursively assigns the new sizes of all child windows
3652 and calculates and assigns the new start positions of these windows.
3654 Note: This function does not check any of `window-fixed-size-p',
3655 `window-min-height' or `window-min-width'. All these checks have to
3656 be applied on the Elisp level. */)
3657 (Lisp_Object frame, Lisp_Object horizontal)
3659 struct frame *f = decode_live_frame (frame);
3660 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3661 bool horflag = !NILP (horizontal);
3663 if (!window_resize_check (r, horflag)
3664 || (XINT (r->new_total)
3665 != (horflag ? r->total_cols : r->total_lines)))
3666 return Qnil;
3668 block_input ();
3669 window_resize_apply (r, horflag);
3671 windows_or_buffers_changed++;
3672 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3674 adjust_glyphs (f);
3675 unblock_input ();
3677 run_window_configuration_change_hook (f);
3679 return Qt;
3683 /* Resize frame F's windows when number of lines of F is set to SIZE.
3684 HORFLAG 1 means resize windows when number of columns of F is set to
3685 SIZE.
3687 This function can delete all windows but the selected one in order to
3688 satisfy the request. The result will be meaningful if and only if
3689 F's windows have meaningful sizes when you call this. */
3690 void
3691 resize_frame_windows (struct frame *f, int size, bool horflag)
3693 Lisp_Object root = f->root_window;
3694 struct window *r = XWINDOW (root);
3695 Lisp_Object mini = f->minibuffer_window;
3696 struct window *m;
3697 /* new_size is the new size of the frame's root window. */
3698 int new_size = (horflag
3699 ? size
3700 : (size
3701 - FRAME_TOP_MARGIN (f)
3702 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3703 ? 1 : 0)));
3705 r->top_line = FRAME_TOP_MARGIN (f);
3706 if (WINDOW_LEAF_P (r))
3707 /* For a leaf root window just set the size. */
3708 if (horflag)
3709 r->total_cols = new_size;
3710 else
3711 r->total_lines = new_size;
3712 else
3714 /* old_size is the old size of the frame's root window. */
3715 int old_size = horflag ? r->total_cols : r->total_lines;
3716 Lisp_Object delta;
3718 XSETINT (delta, new_size - old_size);
3719 /* Try a "normal" resize first. */
3720 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3721 if (window_resize_check (r, horflag)
3722 && new_size == XINT (r->new_total))
3723 window_resize_apply (r, horflag);
3724 else
3726 /* Try with "reasonable" minimum sizes next. */
3727 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3728 if (window_resize_check (r, horflag)
3729 && new_size == XINT (r->new_total))
3730 window_resize_apply (r, horflag);
3731 else
3733 /* Finally, try with "safe" minimum sizes. */
3734 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3735 if (window_resize_check (r, horflag)
3736 && new_size == XINT (r->new_total))
3737 window_resize_apply (r, horflag);
3738 else
3740 /* We lost. Delete all windows but the frame's
3741 selected one. */
3742 root = f->selected_window;
3743 Fdelete_other_windows_internal (root, Qnil);
3744 if (horflag)
3745 XWINDOW (root)->total_cols = new_size;
3746 else
3747 XWINDOW (root)->total_lines = new_size;
3753 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3755 m = XWINDOW (mini);
3756 if (horflag)
3757 m->total_cols = size;
3758 else
3760 /* Are we sure we always want 1 line here? */
3761 m->total_lines = 1;
3762 m->top_line = r->top_line + r->total_lines;
3766 windows_or_buffers_changed++;
3770 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3771 doc: /* Split window OLD.
3772 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3773 new window. In any case TOTAL-SIZE must be a positive integer.
3775 Third argument SIDE nil (or `below') specifies that the new window shall
3776 be located below WINDOW. SIDE `above' means the new window shall be
3777 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3778 lines of the new window including space reserved for the mode and/or
3779 header line.
3781 SIDE t (or `right') specifies that the new window shall be located on
3782 the right side of WINDOW. SIDE `left' means the new window shall be
3783 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3784 number of columns of the new window including space reserved for fringes
3785 and the scrollbar or a divider column.
3787 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3788 according to the SIDE argument.
3790 The new total and normal sizes of all involved windows must have been
3791 set correctly. See the code of `split-window' for how this is done. */)
3792 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3794 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3795 parent window or an internal window we have to install as OLD's new
3796 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3797 provided OLD is a leaf window, or to the frame's selected window.
3798 NEW (*n) is the new window created with some parameters taken from
3799 REFERENCE (*r). */
3800 register Lisp_Object new, frame, reference;
3801 register struct window *o, *p, *n, *r;
3802 struct frame *f;
3803 bool horflag
3804 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3805 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3806 int combination_limit = 0;
3808 CHECK_WINDOW (old);
3809 o = XWINDOW (old);
3810 frame = WINDOW_FRAME (o);
3811 f = XFRAME (frame);
3813 CHECK_NUMBER (total_size);
3815 /* Set combination_limit to 1 if we have to make a new parent window.
3816 We do that if either `window-combination-limit' is t, or OLD has no
3817 parent, or OLD is ortho-combined. */
3818 combination_limit =
3819 EQ (Vwindow_combination_limit, Qt)
3820 || NILP (o->parent)
3821 || (horflag
3822 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
3823 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
3825 /* We need a live reference window to initialize some parameters. */
3826 if (WINDOW_LIVE_P (old))
3827 /* OLD is live, use it as reference window. */
3828 reference = old;
3829 else
3830 /* Use the frame's selected window as reference window. */
3831 reference = FRAME_SELECTED_WINDOW (f);
3832 r = XWINDOW (reference);
3834 /* The following bugs are caught by `split-window'. */
3835 if (MINI_WINDOW_P (o))
3836 error ("Attempt to split minibuffer window");
3837 else if (XINT (total_size) < (horflag ? 2 : 1))
3838 error ("Size of new window too small (after split)");
3839 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3840 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3841 proportionally. */
3843 p = XWINDOW (o->parent);
3844 /* Temporarily pretend we split the parent window. */
3845 wset_new_total
3846 (p, make_number ((horflag ? p->total_cols : p->total_lines)
3847 - XINT (total_size)));
3848 if (!window_resize_check (p, horflag))
3849 error ("Window sizes don't fit");
3850 else
3851 /* Undo the temporary pretension. */
3852 wset_new_total (p, make_number
3853 (horflag ? p->total_cols : p->total_lines));
3855 else
3857 if (!window_resize_check (o, horflag))
3858 error ("Resizing old window failed");
3859 else if (XINT (total_size) + XINT (o->new_total)
3860 != (horflag ? o->total_cols : o->total_lines))
3861 error ("Sum of sizes of old and new window don't fit");
3864 /* This is our point of no return. */
3865 if (combination_limit)
3867 /* Save the old value of o->normal_cols/lines. It gets corrupted
3868 by make_parent_window and we need it below for assigning it to
3869 p->new_normal. */
3870 Lisp_Object new_normal
3871 = horflag ? o->normal_cols : o->normal_lines;
3873 make_parent_window (old, horflag);
3874 p = XWINDOW (o->parent);
3875 if (EQ (Vwindow_combination_limit, Qt))
3876 /* Store t in the new parent's combination_limit slot to avoid
3877 that its children get merged into another window. */
3878 wset_combination_limit (p, Qt);
3879 /* These get applied below. */
3880 wset_new_total (p, make_number
3881 (horflag ? o->total_cols : o->total_lines));
3882 wset_new_normal (p, new_normal);
3884 else
3885 p = XWINDOW (o->parent);
3887 windows_or_buffers_changed++;
3888 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3889 new = make_window ();
3890 n = XWINDOW (new);
3891 wset_frame (n, frame);
3892 wset_parent (n, o->parent);
3894 if (EQ (side, Qabove) || EQ (side, Qleft))
3896 wset_prev (n, o->prev);
3897 if (NILP (n->prev))
3898 wset_combination (p, horflag, new);
3899 else
3900 wset_next (XWINDOW (n->prev), new);
3901 wset_next (n, old);
3902 wset_prev (o, new);
3904 else
3906 wset_next (n, o->next);
3907 if (!NILP (n->next))
3908 wset_prev (XWINDOW (n->next), new);
3909 wset_prev (n, old);
3910 wset_next (o, new);
3913 n->window_end_valid = 0;
3914 memset (&n->last_cursor, 0, sizeof n->last_cursor);
3916 /* Get special geometry settings from reference window. */
3917 n->left_margin_cols = r->left_margin_cols;
3918 n->right_margin_cols = r->right_margin_cols;
3919 n->left_fringe_width = r->left_fringe_width;
3920 n->right_fringe_width = r->right_fringe_width;
3921 n->fringes_outside_margins = r->fringes_outside_margins;
3922 n->scroll_bar_width = r->scroll_bar_width;
3923 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
3925 /* Directly assign orthogonal coordinates and sizes. */
3926 if (horflag)
3928 n->top_line = o->top_line;
3929 n->total_lines = o->total_lines;
3931 else
3933 n->left_col = o->left_col;
3934 n->total_cols = o->total_cols;
3937 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3938 get them ready here. */
3939 wset_new_total (n, total_size);
3940 wset_new_normal (n, normal_size);
3942 block_input ();
3943 window_resize_apply (p, horflag);
3944 adjust_glyphs (f);
3945 /* Set buffer of NEW to buffer of reference window. Don't run
3946 any hooks. */
3947 set_window_buffer (new, r->contents, 0, 1);
3948 unblock_input ();
3950 /* Maybe we should run the scroll functions in Elisp (which already
3951 runs the configuration change hook). */
3952 if (! NILP (Vwindow_scroll_functions))
3953 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3954 Fmarker_position (n->start));
3955 /* Return NEW. */
3956 return new;
3960 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3961 doc: /* Remove WINDOW from its frame.
3962 WINDOW defaults to the selected window. Return nil.
3963 Signal an error when WINDOW is the only window on its frame. */)
3964 (register Lisp_Object window)
3966 register Lisp_Object parent, sibling, frame, root;
3967 struct window *w, *p, *s, *r;
3968 struct frame *f;
3969 bool horflag, before_sibling = 0;
3971 w = decode_any_window (window);
3972 XSETWINDOW (window, w);
3973 if (NILP (w->contents))
3974 /* It's a no-op to delete an already deleted window. */
3975 return Qnil;
3977 parent = w->parent;
3978 if (NILP (parent))
3979 /* Never delete a minibuffer or frame root window. */
3980 error ("Attempt to delete minibuffer or sole ordinary window");
3981 else if (NILP (w->prev) && NILP (w->next))
3982 /* Rather bow out here, this case should be handled on the Elisp
3983 level. */
3984 error ("Attempt to delete sole window of parent");
3986 p = XWINDOW (parent);
3987 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
3989 frame = WINDOW_FRAME (w);
3990 f = XFRAME (frame);
3992 root = FRAME_ROOT_WINDOW (f);
3993 r = XWINDOW (root);
3995 /* Unlink WINDOW from window tree. */
3996 if (NILP (w->prev))
3997 /* Get SIBLING below (on the right of) WINDOW. */
3999 /* before_sibling 1 means WINDOW is the first child of its
4000 parent and thus before the sibling. */
4001 before_sibling = 1;
4002 sibling = w->next;
4003 s = XWINDOW (sibling);
4004 wset_prev (s, Qnil);
4005 wset_combination (p, horflag, sibling);
4007 else
4008 /* Get SIBLING above (on the left of) WINDOW. */
4010 sibling = w->prev;
4011 s = XWINDOW (sibling);
4012 wset_next (s, w->next);
4013 if (!NILP (s->next))
4014 wset_prev (XWINDOW (s->next), sibling);
4017 if (window_resize_check (r, horflag)
4018 && (XINT (r->new_total)
4019 == (horflag ? r->total_cols : r->total_lines)))
4020 /* We can delete WINDOW now. */
4023 /* Block input. */
4024 block_input ();
4025 window_resize_apply (p, horflag);
4027 /* If this window is referred to by the dpyinfo's mouse
4028 highlight, invalidate that slot to be safe (Bug#9904). */
4029 if (!FRAME_INITIAL_P (f))
4031 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4033 if (EQ (hlinfo->mouse_face_window, window))
4034 hlinfo->mouse_face_window = Qnil;
4037 windows_or_buffers_changed++;
4038 Vwindow_list = Qnil;
4039 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4041 wset_next (w, Qnil); /* Don't delete w->next too. */
4042 free_window_matrices (w);
4044 if (WINDOWP (w->contents))
4046 delete_all_child_windows (w->contents);
4047 wset_combination (w, 0, Qnil);
4049 else
4051 unshow_buffer (w);
4052 unchain_marker (XMARKER (w->pointm));
4053 unchain_marker (XMARKER (w->start));
4054 wset_buffer (w, Qnil);
4057 if (NILP (s->prev) && NILP (s->next))
4058 /* A matrjoshka where SIBLING has become the only child of
4059 PARENT. */
4061 /* Put SIBLING into PARENT's place. */
4062 replace_window (parent, sibling, 0);
4063 /* Have SIBLING inherit the following three slot values from
4064 PARENT (the combination_limit slot is not inherited). */
4065 wset_normal_cols (s, p->normal_cols);
4066 wset_normal_lines (s, p->normal_lines);
4067 /* Mark PARENT as deleted. */
4068 wset_combination (p, 0, Qnil);
4069 /* Try to merge SIBLING into its new parent. */
4070 recombine_windows (sibling);
4073 adjust_glyphs (f);
4075 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4076 /* We deleted the frame's selected window. */
4078 /* Use the frame's first window as fallback ... */
4079 Lisp_Object new_selected_window = Fframe_first_window (frame);
4080 /* ... but preferably use its most recently used window. */
4081 Lisp_Object mru_window;
4083 /* `get-mru-window' might fail for some reason so play it safe
4084 - promote the first window _without recording it_ first. */
4085 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4086 Fselect_window (new_selected_window, Qt);
4087 else
4088 fset_selected_window (f, new_selected_window);
4090 unblock_input ();
4092 /* Now look whether `get-mru-window' gets us something. */
4093 mru_window = call1 (Qget_mru_window, frame);
4094 if (WINDOW_LIVE_P (mru_window)
4095 && EQ (XWINDOW (mru_window)->frame, frame))
4096 new_selected_window = mru_window;
4098 /* If all ended up well, we now promote the mru window. */
4099 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4100 Fselect_window (new_selected_window, Qnil);
4101 else
4102 fset_selected_window (f, new_selected_window);
4104 else
4105 unblock_input ();
4107 /* Must be run by the caller:
4108 run_window_configuration_change_hook (f); */
4110 else
4111 /* We failed: Relink WINDOW into window tree. */
4113 if (before_sibling)
4115 wset_prev (s, window);
4116 wset_combination (p, horflag, window);
4118 else
4120 wset_next (s, window);
4121 if (!NILP (w->next))
4122 wset_prev (XWINDOW (w->next), window);
4124 error ("Deletion failed");
4127 return Qnil;
4130 /***********************************************************************
4131 Resizing Mini-Windows
4132 ***********************************************************************/
4134 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4135 can. */
4136 void
4137 grow_mini_window (struct window *w, int delta)
4139 struct frame *f = XFRAME (w->frame);
4140 struct window *r;
4141 Lisp_Object root, value;
4143 eassert (MINI_WINDOW_P (w));
4144 eassert (delta >= 0);
4146 root = FRAME_ROOT_WINDOW (f);
4147 r = XWINDOW (root);
4148 value = call2 (Qwindow_resize_root_window_vertically,
4149 root, make_number (- delta));
4150 if (INTEGERP (value) && window_resize_check (r, 0))
4152 block_input ();
4153 window_resize_apply (r, 0);
4155 /* Grow the mini-window. */
4156 w->top_line = r->top_line + r->total_lines;
4157 w->total_lines -= XINT (value);
4158 /* Enforce full redisplay. FIXME: make it more selective. */
4159 windows_or_buffers_changed++;
4160 adjust_glyphs (f);
4161 unblock_input ();
4166 /* Shrink mini-window W. */
4167 void
4168 shrink_mini_window (struct window *w)
4170 struct frame *f = XFRAME (w->frame);
4171 struct window *r;
4172 Lisp_Object root, value;
4173 EMACS_INT size;
4175 eassert (MINI_WINDOW_P (w));
4177 size = w->total_lines;
4178 if (size > 1)
4180 root = FRAME_ROOT_WINDOW (f);
4181 r = XWINDOW (root);
4182 value = call2 (Qwindow_resize_root_window_vertically,
4183 root, make_number (size - 1));
4184 if (INTEGERP (value) && window_resize_check (r, 0))
4186 block_input ();
4187 window_resize_apply (r, 0);
4189 /* Shrink the mini-window. */
4190 w->top_line = r->top_line + r->total_lines;
4191 w->total_lines = 1;
4192 /* Enforce full redisplay. FIXME: make it more selective. */
4193 windows_or_buffers_changed++;
4194 adjust_glyphs (f);
4195 unblock_input ();
4197 /* If the above failed for whatever strange reason we must make a
4198 one window frame here. The same routine will be needed when
4199 shrinking the frame (and probably when making the initial
4200 *scratch* window). For the moment leave things as they are. */
4204 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4205 doc: /* Resize minibuffer window WINDOW. */)
4206 (Lisp_Object window)
4208 struct window *w = XWINDOW (window);
4209 struct window *r;
4210 struct frame *f;
4211 int height;
4213 CHECK_WINDOW (window);
4214 f = XFRAME (w->frame);
4216 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4217 error ("Not a valid minibuffer window");
4218 else if (FRAME_MINIBUF_ONLY_P (f))
4219 error ("Cannot resize a minibuffer-only frame");
4221 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4222 height = r->total_lines + w->total_lines;
4223 if (window_resize_check (r, 0)
4224 && XINT (w->new_total) > 0
4225 && height == XINT (r->new_total) + XINT (w->new_total))
4227 block_input ();
4228 window_resize_apply (r, 0);
4230 w->total_lines = XFASTINT (w->new_total);
4231 w->top_line = r->top_line + r->total_lines;
4233 windows_or_buffers_changed++;
4234 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4235 adjust_glyphs (f);
4236 unblock_input ();
4238 run_window_configuration_change_hook (f);
4239 return Qt;
4241 else error ("Failed to resize minibuffer window");
4244 /* Mark window cursors off for all windows in the window tree rooted
4245 at W by setting their phys_cursor_on_p flag to zero. Called from
4246 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4247 the frame are cleared. */
4249 void
4250 mark_window_cursors_off (struct window *w)
4252 while (w)
4254 if (WINDOWP (w->contents))
4255 mark_window_cursors_off (XWINDOW (w->contents));
4256 else
4257 w->phys_cursor_on_p = 0;
4259 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4264 /* Return number of lines of text (not counting mode lines) in W. */
4267 window_internal_height (struct window *w)
4269 int ht = w->total_lines;
4271 if (!MINI_WINDOW_P (w))
4273 if (!NILP (w->parent)
4274 || WINDOWP (w->contents)
4275 || !NILP (w->next)
4276 || !NILP (w->prev)
4277 || WINDOW_WANTS_MODELINE_P (w))
4278 --ht;
4280 if (WINDOW_WANTS_HEADER_LINE_P (w))
4281 --ht;
4284 return ht;
4287 /************************************************************************
4288 Window Scrolling
4289 ***********************************************************************/
4291 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4292 N screen-fulls, which is defined as the height of the window minus
4293 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4294 instead. Negative values of N mean scroll down. NOERROR non-zero
4295 means don't signal an error if we try to move over BEGV or ZV,
4296 respectively. */
4298 static void
4299 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4301 immediate_quit = 1;
4302 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4304 /* If we must, use the pixel-based version which is much slower than
4305 the line-based one but can handle varying line heights. */
4306 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4307 window_scroll_pixel_based (window, n, whole, noerror);
4308 else
4309 window_scroll_line_based (window, n, whole, noerror);
4311 immediate_quit = 0;
4315 /* Implementation of window_scroll that works based on pixel line
4316 heights. See the comment of window_scroll for parameter
4317 descriptions. */
4319 static void
4320 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4322 struct it it;
4323 struct window *w = XWINDOW (window);
4324 struct text_pos start;
4325 int this_scroll_margin;
4326 /* True if we fiddled the window vscroll field without really scrolling. */
4327 int vscrolled = 0;
4328 int x, y, rtop, rbot, rowh, vpos;
4329 void *itdata = NULL;
4330 int window_total_lines;
4331 int frame_line_height = default_line_pixel_height (w);
4333 SET_TEXT_POS_FROM_MARKER (start, w->start);
4334 /* Scrolling a minibuffer window via scroll bar when the echo area
4335 shows long text sometimes resets the minibuffer contents behind
4336 our backs. */
4337 if (CHARPOS (start) > ZV)
4338 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4340 /* If PT is not visible in WINDOW, move back one half of
4341 the screen. Allow PT to be partially visible, otherwise
4342 something like (scroll-down 1) with PT in the line before
4343 the partially visible one would recenter. */
4345 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4347 itdata = bidi_shelve_cache ();
4348 /* Move backward half the height of the window. Performance note:
4349 vmotion used here is about 10% faster, but would give wrong
4350 results for variable height lines. */
4351 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4352 it.current_y = it.last_visible_y;
4353 move_it_vertically_backward (&it, window_box_height (w) / 2);
4355 /* The function move_iterator_vertically may move over more than
4356 the specified y-distance. If it->w is small, e.g. a
4357 mini-buffer window, we may end up in front of the window's
4358 display area. This is the case when Start displaying at the
4359 start of the line containing PT in this case. */
4360 if (it.current_y <= 0)
4362 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4363 move_it_vertically_backward (&it, 0);
4364 it.current_y = 0;
4367 start = it.current.pos;
4368 bidi_unshelve_cache (itdata, 0);
4370 else if (auto_window_vscroll_p)
4372 if (rtop || rbot) /* partially visible */
4374 int px;
4375 int dy = frame_line_height;
4376 if (whole)
4377 dy = max ((window_box_height (w)
4378 - next_screen_context_lines * dy),
4379 dy);
4380 dy *= n;
4382 if (n < 0)
4384 /* Only vscroll backwards if already vscrolled forwards. */
4385 if (w->vscroll < 0 && rtop > 0)
4387 px = max (0, -w->vscroll - min (rtop, -dy));
4388 Fset_window_vscroll (window, make_number (px), Qt);
4389 return;
4392 if (n > 0)
4394 /* Do vscroll if already vscrolled or only display line. */
4395 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4397 px = max (0, -w->vscroll + min (rbot, dy));
4398 Fset_window_vscroll (window, make_number (px), Qt);
4399 return;
4402 /* Maybe modify window start instead of scrolling. */
4403 if (rbot > 0 || w->vscroll < 0)
4405 ptrdiff_t spos;
4407 Fset_window_vscroll (window, make_number (0), Qt);
4408 /* If there are other text lines above the current row,
4409 move window start to current row. Else to next row. */
4410 if (rbot > 0)
4411 spos = XINT (Fline_beginning_position (Qnil));
4412 else
4413 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4414 set_marker_restricted (w->start, make_number (spos),
4415 w->contents);
4416 w->start_at_line_beg = 1;
4417 w->update_mode_line = 1;
4418 /* Set force_start so that redisplay_window will run the
4419 window-scroll-functions. */
4420 w->force_start = 1;
4421 return;
4425 /* Cancel previous vscroll. */
4426 Fset_window_vscroll (window, make_number (0), Qt);
4429 itdata = bidi_shelve_cache ();
4430 /* If scroll_preserve_screen_position is non-nil, we try to set
4431 point in the same window line as it is now, so get that line. */
4432 if (!NILP (Vscroll_preserve_screen_position))
4434 /* We preserve the goal pixel coordinate across consecutive
4435 calls to scroll-up, scroll-down and other commands that
4436 have the `scroll-command' property. This avoids the
4437 possibility of point becoming "stuck" on a tall line when
4438 scrolling by one line. */
4439 if (window_scroll_pixel_based_preserve_y < 0
4440 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4441 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4443 start_display (&it, w, start);
4444 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4445 window_scroll_pixel_based_preserve_y = it.current_y;
4446 window_scroll_pixel_based_preserve_x = it.current_x;
4449 else
4450 window_scroll_pixel_based_preserve_y
4451 = window_scroll_pixel_based_preserve_x = -1;
4453 /* Move iterator it from start the specified distance forward or
4454 backward. The result is the new window start. */
4455 start_display (&it, w, start);
4456 if (whole)
4458 ptrdiff_t start_pos = IT_CHARPOS (it);
4459 int dy = frame_line_height;
4460 dy = max ((window_box_height (w)
4461 - next_screen_context_lines * dy),
4462 dy) * n;
4464 /* Note that move_it_vertically always moves the iterator to the
4465 start of a line. So, if the last line doesn't have a newline,
4466 we would end up at the start of the line ending at ZV. */
4467 if (dy <= 0)
4469 move_it_vertically_backward (&it, -dy);
4470 /* Ensure we actually do move, e.g. in case we are currently
4471 looking at an image that is taller that the window height. */
4472 while (start_pos == IT_CHARPOS (it)
4473 && start_pos > BEGV)
4474 move_it_by_lines (&it, -1);
4476 else if (dy > 0)
4478 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4479 MOVE_TO_POS | MOVE_TO_Y);
4480 /* Ensure we actually do move, e.g. in case we are currently
4481 looking at an image that is taller that the window height. */
4482 while (start_pos == IT_CHARPOS (it)
4483 && start_pos < ZV)
4484 move_it_by_lines (&it, 1);
4487 else
4488 move_it_by_lines (&it, n);
4490 /* We failed if we find ZV is already on the screen (scrolling up,
4491 means there's nothing past the end), or if we can't start any
4492 earlier (scrolling down, means there's nothing past the top). */
4493 if ((n > 0 && IT_CHARPOS (it) == ZV)
4494 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4496 if (IT_CHARPOS (it) == ZV)
4498 if (it.current_y < it.last_visible_y
4499 && (it.current_y + it.max_ascent + it.max_descent
4500 > it.last_visible_y))
4502 /* The last line was only partially visible, make it fully
4503 visible. */
4504 w->vscroll = (it.last_visible_y
4505 - it.current_y + it.max_ascent + it.max_descent);
4506 adjust_glyphs (it.f);
4508 else
4510 bidi_unshelve_cache (itdata, 0);
4511 if (noerror)
4512 return;
4513 else if (n < 0) /* could happen with empty buffers */
4514 xsignal0 (Qbeginning_of_buffer);
4515 else
4516 xsignal0 (Qend_of_buffer);
4519 else
4521 if (w->vscroll != 0)
4522 /* The first line was only partially visible, make it fully
4523 visible. */
4524 w->vscroll = 0;
4525 else
4527 bidi_unshelve_cache (itdata, 0);
4528 if (noerror)
4529 return;
4530 else
4531 xsignal0 (Qbeginning_of_buffer);
4535 /* If control gets here, then we vscrolled. */
4537 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
4539 /* Don't try to change the window start below. */
4540 vscrolled = 1;
4543 if (! vscrolled)
4545 ptrdiff_t pos = IT_CHARPOS (it);
4546 ptrdiff_t bytepos;
4548 /* If in the middle of a multi-glyph character move forward to
4549 the next character. */
4550 if (in_display_vector_p (&it))
4552 ++pos;
4553 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4556 /* Set the window start, and set up the window for redisplay. */
4557 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
4558 IT_BYTEPOS (it));
4559 bytepos = marker_byte_position (w->start);
4560 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4561 w->update_mode_line = 1;
4562 /* Set force_start so that redisplay_window will run the
4563 window-scroll-functions. */
4564 w->force_start = 1;
4567 /* The rest of this function uses current_y in a nonstandard way,
4568 not including the height of the header line if any. */
4569 it.current_y = it.vpos = 0;
4571 /* Move PT out of scroll margins.
4572 This code wants current_y to be zero at the window start position
4573 even if there is a header line. */
4574 window_total_lines
4575 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
4576 this_scroll_margin = max (0, scroll_margin);
4577 this_scroll_margin
4578 = min (this_scroll_margin, window_total_lines / 4);
4579 this_scroll_margin *= frame_line_height;
4581 if (n > 0)
4583 /* We moved the window start towards ZV, so PT may be now
4584 in the scroll margin at the top. */
4585 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4586 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4587 && (NILP (Vscroll_preserve_screen_position)
4588 || EQ (Vscroll_preserve_screen_position, Qt)))
4589 /* We found PT at a legitimate height. Leave it alone. */
4591 else if (window_scroll_pixel_based_preserve_y >= 0)
4593 /* If we have a header line, take account of it.
4594 This is necessary because we set it.current_y to 0, above. */
4595 move_it_to (&it, -1,
4596 window_scroll_pixel_based_preserve_x,
4597 window_scroll_pixel_based_preserve_y
4598 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4599 -1, MOVE_TO_Y | MOVE_TO_X);
4600 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4602 else
4604 while (it.current_y < this_scroll_margin)
4606 int prev = it.current_y;
4607 move_it_by_lines (&it, 1);
4608 if (prev == it.current_y)
4609 break;
4611 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4614 else if (n < 0)
4616 ptrdiff_t charpos, bytepos;
4617 int partial_p;
4619 /* Save our position, for the
4620 window_scroll_pixel_based_preserve_y case. */
4621 charpos = IT_CHARPOS (it);
4622 bytepos = IT_BYTEPOS (it);
4624 /* We moved the window start towards BEGV, so PT may be now
4625 in the scroll margin at the bottom. */
4626 move_it_to (&it, PT, -1,
4627 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4628 - this_scroll_margin - 1),
4630 MOVE_TO_POS | MOVE_TO_Y);
4632 /* Save our position, in case it's correct. */
4633 charpos = IT_CHARPOS (it);
4634 bytepos = IT_BYTEPOS (it);
4636 /* See if point is on a partially visible line at the end. */
4637 if (it.what == IT_EOB)
4638 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4639 else
4641 move_it_by_lines (&it, 1);
4642 partial_p = it.current_y > it.last_visible_y;
4645 if (charpos == PT && !partial_p
4646 && (NILP (Vscroll_preserve_screen_position)
4647 || EQ (Vscroll_preserve_screen_position, Qt)))
4648 /* We found PT before we found the display margin, so PT is ok. */
4650 else if (window_scroll_pixel_based_preserve_y >= 0)
4652 SET_TEXT_POS_FROM_MARKER (start, w->start);
4653 start_display (&it, w, start);
4654 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4655 here because we called start_display again and did not
4656 alter it.current_y this time. */
4657 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4658 window_scroll_pixel_based_preserve_y, -1,
4659 MOVE_TO_Y | MOVE_TO_X);
4660 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4662 else
4664 if (partial_p)
4665 /* The last line was only partially visible, so back up two
4666 lines to make sure we're on a fully visible line. */
4668 move_it_by_lines (&it, -2);
4669 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4671 else
4672 /* No, the position we saved is OK, so use it. */
4673 SET_PT_BOTH (charpos, bytepos);
4676 bidi_unshelve_cache (itdata, 0);
4680 /* Implementation of window_scroll that works based on screen lines.
4681 See the comment of window_scroll for parameter descriptions. */
4683 static void
4684 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
4686 register struct window *w = XWINDOW (window);
4687 /* Fvertical_motion enters redisplay, which can trigger
4688 fontification, which in turn can modify buffer text (e.g., if the
4689 fontification functions replace escape sequences with faces, as
4690 in `grep-mode-font-lock-keywords'). So we use a marker to record
4691 the old point position, to prevent crashes in SET_PT_BOTH. */
4692 Lisp_Object opoint_marker = Fpoint_marker ();
4693 register ptrdiff_t pos, pos_byte;
4694 register int ht = window_internal_height (w);
4695 register Lisp_Object tem;
4696 bool lose;
4697 Lisp_Object bolp;
4698 ptrdiff_t startpos = marker_position (w->start);
4699 ptrdiff_t startbyte = marker_byte_position (w->start);
4700 Lisp_Object original_pos = Qnil;
4702 /* If scrolling screen-fulls, compute the number of lines to
4703 scroll from the window's height. */
4704 if (whole)
4705 n *= max (1, ht - next_screen_context_lines);
4707 if (!NILP (Vscroll_preserve_screen_position))
4709 if (window_scroll_preserve_vpos <= 0
4710 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4711 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4713 struct position posit
4714 = *compute_motion (startpos, startbyte, 0, 0, 0,
4715 PT, ht, 0, -1, w->hscroll, 0, w);
4716 window_scroll_preserve_vpos = posit.vpos;
4717 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
4720 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4721 make_number (window_scroll_preserve_vpos));
4724 XSETFASTINT (tem, PT);
4725 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4727 if (NILP (tem))
4729 Fvertical_motion (make_number (- (ht / 2)), window);
4730 startpos = PT;
4731 startbyte = PT_BYTE;
4734 SET_PT_BOTH (startpos, startbyte);
4735 lose = n < 0 && PT == BEGV;
4736 Fvertical_motion (make_number (n), window);
4737 pos = PT;
4738 pos_byte = PT_BYTE;
4739 bolp = Fbolp ();
4740 SET_PT_BOTH (marker_position (opoint_marker),
4741 marker_byte_position (opoint_marker));
4743 if (lose)
4745 if (noerror)
4746 return;
4747 else
4748 xsignal0 (Qbeginning_of_buffer);
4751 if (pos < ZV)
4753 /* Don't use a scroll margin that is negative or too large. */
4754 int this_scroll_margin =
4755 max (0, min (scroll_margin, w->total_lines / 4));
4757 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
4758 w->start_at_line_beg = !NILP (bolp);
4759 w->update_mode_line = 1;
4760 /* Set force_start so that redisplay_window will run
4761 the window-scroll-functions. */
4762 w->force_start = 1;
4764 if (!NILP (Vscroll_preserve_screen_position)
4765 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4767 SET_PT_BOTH (pos, pos_byte);
4768 Fvertical_motion (original_pos, window);
4770 /* If we scrolled forward, put point enough lines down
4771 that it is outside the scroll margin. */
4772 else if (n > 0)
4774 int top_margin;
4776 if (this_scroll_margin > 0)
4778 SET_PT_BOTH (pos, pos_byte);
4779 Fvertical_motion (make_number (this_scroll_margin), window);
4780 top_margin = PT;
4782 else
4783 top_margin = pos;
4785 if (top_margin <= marker_position (opoint_marker))
4786 SET_PT_BOTH (marker_position (opoint_marker),
4787 marker_byte_position (opoint_marker));
4788 else if (!NILP (Vscroll_preserve_screen_position))
4790 SET_PT_BOTH (pos, pos_byte);
4791 Fvertical_motion (original_pos, window);
4793 else
4794 SET_PT (top_margin);
4796 else if (n < 0)
4798 int bottom_margin;
4800 /* If we scrolled backward, put point near the end of the window
4801 but not within the scroll margin. */
4802 SET_PT_BOTH (pos, pos_byte);
4803 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4804 if (XFASTINT (tem) == ht - this_scroll_margin)
4805 bottom_margin = PT;
4806 else
4807 bottom_margin = PT + 1;
4809 if (bottom_margin > marker_position (opoint_marker))
4810 SET_PT_BOTH (marker_position (opoint_marker),
4811 marker_byte_position (opoint_marker));
4812 else
4814 if (!NILP (Vscroll_preserve_screen_position))
4816 SET_PT_BOTH (pos, pos_byte);
4817 Fvertical_motion (original_pos, window);
4819 else
4820 Fvertical_motion (make_number (-1), window);
4824 else
4826 if (noerror)
4827 return;
4828 else
4829 xsignal0 (Qend_of_buffer);
4834 /* Scroll selected_window up or down. If N is nil, scroll a
4835 screen-full which is defined as the height of the window minus
4836 next_screen_context_lines. If N is the symbol `-', scroll.
4837 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4838 up. This is the guts of Fscroll_up and Fscroll_down. */
4840 static void
4841 scroll_command (Lisp_Object n, int direction)
4843 ptrdiff_t count = SPECPDL_INDEX ();
4845 eassert (eabs (direction) == 1);
4847 /* If selected window's buffer isn't current, make it current for
4848 the moment. But don't screw up if window_scroll gets an error. */
4849 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
4851 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4852 Fset_buffer (XWINDOW (selected_window)->contents);
4854 /* Make redisplay consider other windows than just selected_window. */
4855 ++windows_or_buffers_changed;
4858 if (NILP (n))
4859 window_scroll (selected_window, direction, 1, 0);
4860 else if (EQ (n, Qminus))
4861 window_scroll (selected_window, -direction, 1, 0);
4862 else
4864 n = Fprefix_numeric_value (n);
4865 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4868 unbind_to (count, Qnil);
4871 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4872 doc: /* Scroll text of selected window upward ARG lines.
4873 If ARG is omitted or nil, scroll upward by a near full screen.
4874 A near full screen is `next-screen-context-lines' less than a full screen.
4875 Negative ARG means scroll downward.
4876 If ARG is the atom `-', scroll downward by nearly full screen.
4877 When calling from a program, supply as argument a number, nil, or `-'. */)
4878 (Lisp_Object arg)
4880 scroll_command (arg, 1);
4881 return Qnil;
4884 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4885 doc: /* Scroll text of selected window down ARG lines.
4886 If ARG is omitted or nil, scroll down by a near full screen.
4887 A near full screen is `next-screen-context-lines' less than a full screen.
4888 Negative ARG means scroll upward.
4889 If ARG is the atom `-', scroll upward by nearly full screen.
4890 When calling from a program, supply as argument a number, nil, or `-'. */)
4891 (Lisp_Object arg)
4893 scroll_command (arg, -1);
4894 return Qnil;
4897 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4898 doc: /* Return the other window for \"other window scroll\" commands.
4899 If `other-window-scroll-buffer' is non-nil, a window
4900 showing that buffer is used.
4901 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4902 specifies the window. This takes precedence over
4903 `other-window-scroll-buffer'. */)
4904 (void)
4906 Lisp_Object window;
4908 if (MINI_WINDOW_P (XWINDOW (selected_window))
4909 && !NILP (Vminibuf_scroll_window))
4910 window = Vminibuf_scroll_window;
4911 /* If buffer is specified, scroll that buffer. */
4912 else if (!NILP (Vother_window_scroll_buffer))
4914 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4915 if (NILP (window))
4916 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4918 else
4920 /* Nothing specified; look for a neighboring window on the same
4921 frame. */
4922 window = Fnext_window (selected_window, Qnil, Qnil);
4924 if (EQ (window, selected_window))
4925 /* That didn't get us anywhere; look for a window on another
4926 visible frame. */
4928 window = Fnext_window (window, Qnil, Qt);
4929 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4930 && ! EQ (window, selected_window));
4933 CHECK_LIVE_WINDOW (window);
4935 if (EQ (window, selected_window))
4936 error ("There is no other window");
4938 return window;
4941 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4942 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4943 A near full screen is `next-screen-context-lines' less than a full screen.
4944 The next window is the one below the current one; or the one at the top
4945 if the current one is at the bottom. Negative ARG means scroll downward.
4946 If ARG is the atom `-', scroll downward by nearly full screen.
4947 When calling from a program, supply as argument a number, nil, or `-'.
4949 If `other-window-scroll-buffer' is non-nil, scroll the window
4950 showing that buffer, popping the buffer up if necessary.
4951 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4952 specifies the window to scroll. This takes precedence over
4953 `other-window-scroll-buffer'. */)
4954 (Lisp_Object arg)
4956 Lisp_Object window;
4957 struct window *w;
4958 ptrdiff_t count = SPECPDL_INDEX ();
4960 window = Fother_window_for_scrolling ();
4961 w = XWINDOW (window);
4963 /* Don't screw up if window_scroll gets an error. */
4964 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4965 ++windows_or_buffers_changed;
4967 Fset_buffer (w->contents);
4968 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
4970 if (NILP (arg))
4971 window_scroll (window, 1, 1, 1);
4972 else if (EQ (arg, Qminus))
4973 window_scroll (window, -1, 1, 1);
4974 else
4976 if (CONSP (arg))
4977 arg = XCAR (arg);
4978 CHECK_NUMBER (arg);
4979 window_scroll (window, XINT (arg), 0, 1);
4982 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4983 unbind_to (count, Qnil);
4985 return Qnil;
4988 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
4989 doc: /* Scroll selected window display ARG columns left.
4990 Default for ARG is window width minus 2.
4991 Value is the total amount of leftward horizontal scrolling in
4992 effect after the change.
4993 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4994 lower bound for automatic scrolling, i.e. automatic scrolling
4995 will not scroll a window to a column less than the value returned
4996 by this function. This happens in an interactive call. */)
4997 (register Lisp_Object arg, Lisp_Object set_minimum)
4999 struct window *w = XWINDOW (selected_window);
5000 EMACS_INT requested_arg = (NILP (arg)
5001 ? window_body_cols (w) - 2
5002 : XINT (Fprefix_numeric_value (arg)));
5003 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5005 if (!NILP (set_minimum))
5006 w->min_hscroll = w->hscroll;
5008 return result;
5011 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5012 doc: /* Scroll selected window display ARG columns right.
5013 Default for ARG is window width minus 2.
5014 Value is the total amount of leftward horizontal scrolling in
5015 effect after the change.
5016 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5017 lower bound for automatic scrolling, i.e. automatic scrolling
5018 will not scroll a window to a column less than the value returned
5019 by this function. This happens in an interactive call. */)
5020 (register Lisp_Object arg, Lisp_Object set_minimum)
5022 struct window *w = XWINDOW (selected_window);
5023 EMACS_INT requested_arg = (NILP (arg)
5024 ? window_body_cols (w) - 2
5025 : XINT (Fprefix_numeric_value (arg)));
5026 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5028 if (!NILP (set_minimum))
5029 w->min_hscroll = w->hscroll;
5031 return result;
5034 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5035 doc: /* Return the window which was selected when entering the minibuffer.
5036 Returns nil, if selected window is not a minibuffer window. */)
5037 (void)
5039 if (minibuf_level > 0
5040 && MINI_WINDOW_P (XWINDOW (selected_window))
5041 && WINDOW_LIVE_P (minibuf_selected_window))
5042 return minibuf_selected_window;
5044 return Qnil;
5047 /* Value is the number of lines actually displayed in window W,
5048 as opposed to its height. */
5050 static int
5051 displayed_window_lines (struct window *w)
5053 struct it it;
5054 struct text_pos start;
5055 ptrdiff_t charpos = marker_position (w->start);
5056 int height = window_box_height (w);
5057 struct buffer *old_buffer;
5058 int bottom_y;
5059 void *itdata = NULL;
5061 if (XBUFFER (w->contents) != current_buffer)
5063 old_buffer = current_buffer;
5064 set_buffer_internal (XBUFFER (w->contents));
5066 else
5067 old_buffer = NULL;
5069 /* In case W->start is out of the accessible range, do something
5070 reasonable. This happens in Info mode when Info-scroll-down
5071 calls (recenter -1) while W->start is 1. */
5072 if (charpos < BEGV)
5073 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5074 else if (charpos > ZV)
5075 SET_TEXT_POS (start, ZV, ZV_BYTE);
5076 else
5077 SET_TEXT_POS_FROM_MARKER (start, w->start);
5079 itdata = bidi_shelve_cache ();
5080 start_display (&it, w, start);
5081 move_it_vertically (&it, height);
5082 bottom_y = line_bottom_y (&it);
5083 bidi_unshelve_cache (itdata, 0);
5085 /* rms: On a non-window display,
5086 the value of it.vpos at the bottom of the screen
5087 seems to be 1 larger than window_box_height (w).
5088 This kludge fixes a bug whereby (move-to-window-line -1)
5089 when ZV is on the last screen line
5090 moves to the previous screen line instead of the last one. */
5091 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5092 height++;
5094 /* Add in empty lines at the bottom of the window. */
5095 if (bottom_y < height)
5097 int uy = FRAME_LINE_HEIGHT (it.f);
5098 it.vpos += (height - bottom_y + uy - 1) / uy;
5101 if (old_buffer)
5102 set_buffer_internal (old_buffer);
5104 return it.vpos;
5108 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5109 doc: /* Center point in selected window and maybe redisplay frame.
5110 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5111 relative to the selected window. If ARG is negative, it counts up from the
5112 bottom of the window. (ARG should be less than the height of the window.)
5114 If ARG is omitted or nil, then recenter with point on the middle line of
5115 the selected window; if the variable `recenter-redisplay' is non-nil,
5116 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5117 is set to `grow-only', this resets the tool-bar's height to the minimum
5118 height needed); if `recenter-redisplay' has the special value `tty',
5119 then only tty frames are redrawn.
5121 Just C-u as prefix means put point in the center of the window
5122 and redisplay normally--don't erase and redraw the frame. */)
5123 (register Lisp_Object arg)
5125 struct window *w = XWINDOW (selected_window);
5126 struct buffer *buf = XBUFFER (w->contents);
5127 struct buffer *obuf = current_buffer;
5128 bool center_p = 0;
5129 ptrdiff_t charpos, bytepos;
5130 EMACS_INT iarg IF_LINT (= 0);
5131 int this_scroll_margin;
5133 /* If redisplay is suppressed due to an error, try again. */
5134 obuf->display_error_modiff = 0;
5136 if (NILP (arg))
5138 if (!NILP (Vrecenter_redisplay)
5139 && (!EQ (Vrecenter_redisplay, Qtty)
5140 || !NILP (Ftty_type (selected_frame))))
5142 ptrdiff_t i;
5144 /* Invalidate pixel data calculated for all compositions. */
5145 for (i = 0; i < n_compositions; i++)
5146 composition_table[i]->font = NULL;
5148 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5150 Fredraw_frame (WINDOW_FRAME (w));
5151 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5154 center_p = 1;
5156 else if (CONSP (arg)) /* Just C-u. */
5157 center_p = 1;
5158 else
5160 arg = Fprefix_numeric_value (arg);
5161 CHECK_NUMBER (arg);
5162 iarg = XINT (arg);
5165 set_buffer_internal (buf);
5167 /* Do this after making BUF current
5168 in case scroll_margin is buffer-local. */
5169 this_scroll_margin =
5170 max (0, min (scroll_margin, w->total_lines / 4));
5172 /* Handle centering on a graphical frame specially. Such frames can
5173 have variable-height lines and centering point on the basis of
5174 line counts would lead to strange effects. */
5175 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5177 if (center_p)
5179 struct it it;
5180 struct text_pos pt;
5181 void *itdata = bidi_shelve_cache ();
5183 SET_TEXT_POS (pt, PT, PT_BYTE);
5184 start_display (&it, w, pt);
5185 move_it_vertically_backward (&it, window_box_height (w) / 2);
5186 charpos = IT_CHARPOS (it);
5187 bytepos = IT_BYTEPOS (it);
5188 bidi_unshelve_cache (itdata, 0);
5190 else if (iarg < 0)
5192 struct it it;
5193 struct text_pos pt;
5194 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5195 int extra_line_spacing;
5196 int h = window_box_height (w);
5197 void *itdata = bidi_shelve_cache ();
5199 iarg = - max (-iarg, this_scroll_margin);
5201 SET_TEXT_POS (pt, PT, PT_BYTE);
5202 start_display (&it, w, pt);
5204 /* Be sure we have the exact height of the full line containing PT. */
5205 move_it_by_lines (&it, 0);
5207 /* The amount of pixels we have to move back is the window
5208 height minus what's displayed in the line containing PT,
5209 and the lines below. */
5210 it.current_y = 0;
5211 it.vpos = 0;
5212 move_it_by_lines (&it, nlines);
5214 if (it.vpos == nlines)
5215 h -= it.current_y;
5216 else
5218 /* Last line has no newline */
5219 h -= line_bottom_y (&it);
5220 it.vpos++;
5223 /* Don't reserve space for extra line spacing of last line. */
5224 extra_line_spacing = it.max_extra_line_spacing;
5226 /* If we can't move down NLINES lines because we hit
5227 the end of the buffer, count in some empty lines. */
5228 if (it.vpos < nlines)
5230 nlines -= it.vpos;
5231 extra_line_spacing = it.extra_line_spacing;
5232 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5234 if (h <= 0)
5236 bidi_unshelve_cache (itdata, 0);
5237 return Qnil;
5240 /* Now find the new top line (starting position) of the window. */
5241 start_display (&it, w, pt);
5242 it.current_y = 0;
5243 move_it_vertically_backward (&it, h);
5245 /* If extra line spacing is present, we may move too far
5246 back. This causes the last line to be only partially
5247 visible (which triggers redisplay to recenter that line
5248 in the middle), so move forward.
5249 But ignore extra line spacing on last line, as it is not
5250 considered to be part of the visible height of the line.
5252 h += extra_line_spacing;
5253 while (-it.current_y > h)
5254 move_it_by_lines (&it, 1);
5256 charpos = IT_CHARPOS (it);
5257 bytepos = IT_BYTEPOS (it);
5259 bidi_unshelve_cache (itdata, 0);
5261 else
5263 struct position pos;
5265 iarg = max (iarg, this_scroll_margin);
5267 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5268 charpos = pos.bufpos;
5269 bytepos = pos.bytepos;
5272 else
5274 struct position pos;
5275 int ht = window_internal_height (w);
5277 if (center_p)
5278 iarg = ht / 2;
5279 else if (iarg < 0)
5280 iarg += ht;
5282 /* Don't let it get into the margin at either top or bottom. */
5283 iarg = clip_to_bounds (this_scroll_margin, iarg,
5284 ht - this_scroll_margin - 1);
5286 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5287 charpos = pos.bufpos;
5288 bytepos = pos.bytepos;
5291 /* Set the new window start. */
5292 set_marker_both (w->start, w->contents, charpos, bytepos);
5293 w->window_end_valid = 0;
5295 w->optional_new_start = 1;
5297 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5298 FETCH_BYTE (bytepos - 1) == '\n');
5300 set_buffer_internal (obuf);
5301 return Qnil;
5304 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5305 0, 1, 0,
5306 doc: /* Return the height in lines of the text display area of WINDOW.
5307 WINDOW must be a live window and defaults to the selected one.
5309 The returned height does not include the mode line, any header line,
5310 nor any partial-height lines at the bottom of the text area. */)
5311 (Lisp_Object window)
5313 struct window *w = decode_live_window (window);
5314 int pixel_height = window_box_height (w);
5315 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5316 return make_number (line_height);
5321 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5322 1, 1, "P",
5323 doc: /* Position point relative to window.
5324 ARG nil means position point at center of window.
5325 Else, ARG specifies vertical position within the window;
5326 zero means top of window, negative means relative to bottom of window. */)
5327 (Lisp_Object arg)
5329 struct window *w = XWINDOW (selected_window);
5330 int lines, start;
5331 Lisp_Object window;
5332 #if 0
5333 int this_scroll_margin;
5334 #endif
5336 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5337 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5338 when passed below to set_marker_both. */
5339 error ("move-to-window-line called from unrelated buffer");
5341 window = selected_window;
5342 start = marker_position (w->start);
5343 if (start < BEGV || start > ZV)
5345 int height = window_internal_height (w);
5346 Fvertical_motion (make_number (- (height / 2)), window);
5347 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5348 w->start_at_line_beg = !NILP (Fbolp ());
5349 w->force_start = 1;
5351 else
5352 Fgoto_char (w->start);
5354 lines = displayed_window_lines (w);
5356 #if 0
5357 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5358 #endif
5360 if (NILP (arg))
5361 XSETFASTINT (arg, lines / 2);
5362 else
5364 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5366 if (iarg < 0)
5367 iarg = iarg + lines;
5369 #if 0 /* This code would prevent move-to-window-line from moving point
5370 to a place inside the scroll margins (which would cause the
5371 next redisplay to scroll). I wrote this code, but then concluded
5372 it is probably better not to install it. However, it is here
5373 inside #if 0 so as not to lose it. -- rms. */
5375 /* Don't let it get into the margin at either top or bottom. */
5376 iarg = max (iarg, this_scroll_margin);
5377 iarg = min (iarg, lines - this_scroll_margin - 1);
5378 #endif
5380 arg = make_number (iarg);
5383 /* Skip past a partially visible first line. */
5384 if (w->vscroll)
5385 XSETINT (arg, XINT (arg) + 1);
5387 return Fvertical_motion (arg, window);
5392 /***********************************************************************
5393 Window Configuration
5394 ***********************************************************************/
5396 struct save_window_data
5398 struct vectorlike_header header;
5399 Lisp_Object selected_frame;
5400 Lisp_Object current_window;
5401 Lisp_Object f_current_buffer;
5402 Lisp_Object minibuf_scroll_window;
5403 Lisp_Object minibuf_selected_window;
5404 Lisp_Object root_window;
5405 Lisp_Object focus_frame;
5406 /* A vector, each of whose elements is a struct saved_window
5407 for one window. */
5408 Lisp_Object saved_windows;
5410 /* All fields above are traced by the GC.
5411 From `frame-cols' down, the fields are ignored by the GC. */
5413 int frame_cols, frame_lines, frame_menu_bar_lines;
5414 int frame_tool_bar_lines;
5417 /* This is saved as a Lisp_Vector */
5418 struct saved_window
5420 struct vectorlike_header header;
5422 Lisp_Object window, buffer, start, pointm, mark;
5423 Lisp_Object left_col, top_line, total_cols, total_lines;
5424 Lisp_Object normal_cols, normal_lines;
5425 Lisp_Object hscroll, min_hscroll;
5426 Lisp_Object parent, prev;
5427 Lisp_Object start_at_line_beg;
5428 Lisp_Object display_table;
5429 Lisp_Object left_margin_cols, right_margin_cols;
5430 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5431 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5432 Lisp_Object combination_limit, window_parameters;
5435 #define SAVED_WINDOW_N(swv,n) \
5436 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5438 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5439 doc: /* Return t if OBJECT is a window-configuration object. */)
5440 (Lisp_Object object)
5442 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5445 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5446 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5447 (Lisp_Object config)
5449 register struct save_window_data *data;
5450 struct Lisp_Vector *saved_windows;
5452 CHECK_WINDOW_CONFIGURATION (config);
5454 data = (struct save_window_data *) XVECTOR (config);
5455 saved_windows = XVECTOR (data->saved_windows);
5456 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5459 DEFUN ("set-window-configuration", Fset_window_configuration,
5460 Sset_window_configuration, 1, 1, 0,
5461 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5462 CONFIGURATION must be a value previously returned
5463 by `current-window-configuration' (which see).
5464 If CONFIGURATION was made from a frame that is now deleted,
5465 only frame-independent values can be restored. In this case,
5466 the return value is nil. Otherwise the value is t. */)
5467 (Lisp_Object configuration)
5469 register struct save_window_data *data;
5470 struct Lisp_Vector *saved_windows;
5471 Lisp_Object new_current_buffer;
5472 Lisp_Object frame;
5473 struct frame *f;
5474 ptrdiff_t old_point = -1;
5476 CHECK_WINDOW_CONFIGURATION (configuration);
5478 data = (struct save_window_data *) XVECTOR (configuration);
5479 saved_windows = XVECTOR (data->saved_windows);
5481 new_current_buffer = data->f_current_buffer;
5482 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5483 new_current_buffer = Qnil;
5484 else
5486 if (XBUFFER (new_current_buffer) == current_buffer)
5487 /* The code further down "preserves point" by saving here PT in
5488 old_point and then setting it later back into PT. When the
5489 current-selected-window and the final-selected-window both show
5490 the current buffer, this suffers from the problem that the
5491 current PT is the window-point of the current-selected-window,
5492 while the final PT is the point of the final-selected-window, so
5493 this copy from one PT to the other would end up moving the
5494 window-point of the final-selected-window to the window-point of
5495 the current-selected-window. So we have to be careful which
5496 point of the current-buffer we copy into old_point. */
5497 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5498 && WINDOWP (selected_window)
5499 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
5500 && !EQ (selected_window, data->current_window))
5501 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5502 else
5503 old_point = PT;
5504 else
5505 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5506 point in new_current_buffer as of the last time this buffer was
5507 used. This can be non-deterministic since it can be changed by
5508 things like jit-lock by mere temporary selection of some random
5509 window that happens to show this buffer.
5510 So if possible we want this arbitrary choice of "which point" to
5511 be the one from the to-be-selected-window so as to prevent this
5512 window's cursor from being copied from another window. */
5513 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5514 /* If current_window = selected_window, its point is in BUF_PT. */
5515 && !EQ (selected_window, data->current_window))
5516 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5517 else
5518 old_point = BUF_PT (XBUFFER (new_current_buffer));
5521 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5522 f = XFRAME (frame);
5524 /* If f is a dead frame, don't bother rebuilding its window tree.
5525 However, there is other stuff we should still try to do below. */
5526 if (FRAME_LIVE_P (f))
5528 Lisp_Object window;
5529 Lisp_Object dead_windows = Qnil;
5530 register Lisp_Object tem, par, pers;
5531 register struct window *w;
5532 register struct saved_window *p;
5533 struct window *root_window;
5534 struct window **leaf_windows;
5535 int n_leaf_windows;
5536 ptrdiff_t k;
5537 int i, n;
5539 /* If the frame has been resized since this window configuration was
5540 made, we change the frame to the size specified in the
5541 configuration, restore the configuration, and then resize it
5542 back. We keep track of the prevailing height in these variables. */
5543 int previous_frame_lines = FRAME_LINES (f);
5544 int previous_frame_cols = FRAME_COLS (f);
5545 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5546 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5548 /* Don't do this within the main loop below: This may call Lisp
5549 code and is thus potentially unsafe while input is blocked. */
5550 for (k = 0; k < saved_windows->header.size; k++)
5552 p = SAVED_WINDOW_N (saved_windows, k);
5553 window = p->window;
5554 w = XWINDOW (window);
5555 if (BUFFERP (w->contents)
5556 && !EQ (w->contents, p->buffer)
5557 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5558 /* If a window we restore gets another buffer, record the
5559 window's old buffer. */
5560 call1 (Qrecord_window_buffer, window);
5563 /* The mouse highlighting code could get screwed up
5564 if it runs during this. */
5565 block_input ();
5567 if (data->frame_lines != previous_frame_lines
5568 || data->frame_cols != previous_frame_cols)
5569 change_frame_size (f, data->frame_lines,
5570 data->frame_cols, 0, 0, 0);
5571 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5572 if (data->frame_menu_bar_lines
5573 != previous_frame_menu_bar_lines)
5574 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5575 make_number (0));
5576 #ifdef HAVE_WINDOW_SYSTEM
5577 if (data->frame_tool_bar_lines
5578 != previous_frame_tool_bar_lines)
5579 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5580 make_number (0));
5581 #endif
5582 #endif
5584 /* "Swap out" point from the selected window's buffer
5585 into the window itself. (Normally the pointm of the selected
5586 window holds garbage.) We do this now, before
5587 restoring the window contents, and prevent it from
5588 being done later on when we select a new window. */
5589 if (! NILP (XWINDOW (selected_window)->contents))
5591 w = XWINDOW (selected_window);
5592 set_marker_both (w->pointm,
5593 w->contents,
5594 BUF_PT (XBUFFER (w->contents)),
5595 BUF_PT_BYTE (XBUFFER (w->contents)));
5598 windows_or_buffers_changed++;
5599 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5601 /* Problem: Freeing all matrices and later allocating them again
5602 is a serious redisplay flickering problem. What we would
5603 really like to do is to free only those matrices not reused
5604 below. */
5605 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5606 leaf_windows = alloca (count_windows (root_window)
5607 * sizeof *leaf_windows);
5608 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5610 /* Kludge Alert!
5611 Mark all windows now on frame as "deleted".
5612 Restoring the new configuration "undeletes" any that are in it.
5614 Save their current buffers in their height fields, since we may
5615 need it later, if a buffer saved in the configuration is now
5616 dead. */
5617 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5619 for (k = 0; k < saved_windows->header.size; k++)
5621 p = SAVED_WINDOW_N (saved_windows, k);
5622 window = p->window;
5623 w = XWINDOW (window);
5624 wset_next (w, Qnil);
5626 if (!NILP (p->parent))
5627 wset_parent
5628 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
5629 else
5630 wset_parent (w, Qnil);
5632 if (!NILP (p->prev))
5634 wset_prev
5635 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
5636 wset_next (XWINDOW (w->prev), p->window);
5638 else
5640 wset_prev (w, Qnil);
5641 if (!NILP (w->parent))
5642 wset_combination (XWINDOW (w->parent),
5643 (XINT (p->total_cols)
5644 != XWINDOW (w->parent)->total_cols),
5645 p->window);
5648 /* If we squirreled away the buffer, restore it now. */
5649 if (BUFFERP (w->combination_limit))
5650 wset_buffer (w, w->combination_limit);
5651 w->left_col = XFASTINT (p->left_col);
5652 w->top_line = XFASTINT (p->top_line);
5653 w->total_cols = XFASTINT (p->total_cols);
5654 w->total_lines = XFASTINT (p->total_lines);
5655 wset_normal_cols (w, p->normal_cols);
5656 wset_normal_lines (w, p->normal_lines);
5657 w->hscroll = XFASTINT (p->hscroll);
5658 w->min_hscroll = XFASTINT (p->min_hscroll);
5659 wset_display_table (w, p->display_table);
5660 w->left_margin_cols = XINT (p->left_margin_cols);
5661 w->right_margin_cols = XINT (p->right_margin_cols);
5662 w->left_fringe_width = XINT (p->left_fringe_width);
5663 w->right_fringe_width = XINT (p->right_fringe_width);
5664 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
5665 w->scroll_bar_width = XINT (p->scroll_bar_width);
5666 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
5667 wset_dedicated (w, p->dedicated);
5668 wset_combination_limit (w, p->combination_limit);
5669 /* Restore any window parameters that have been saved.
5670 Parameters that have not been saved are left alone. */
5671 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5673 pers = XCAR (tem);
5674 if (CONSP (pers))
5676 if (NILP (XCDR (pers)))
5678 par = Fassq (XCAR (pers), w->window_parameters);
5679 if (CONSP (par) && !NILP (XCDR (par)))
5680 /* Reset a parameter to nil if and only if it
5681 has a non-nil association. Don't make new
5682 associations. */
5683 Fsetcdr (par, Qnil);
5685 else
5686 /* Always restore a non-nil value. */
5687 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5691 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5692 /* If saved buffer is alive, install it. */
5694 wset_buffer (w, p->buffer);
5695 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5696 set_marker_restricted (w->start, p->start, w->contents);
5697 set_marker_restricted (w->pointm, p->pointm,
5698 w->contents);
5699 Fset_marker (BVAR (XBUFFER (w->contents), mark),
5700 p->mark, w->contents);
5702 /* As documented in Fcurrent_window_configuration, don't
5703 restore the location of point in the buffer which was
5704 current when the window configuration was recorded. */
5705 if (!EQ (p->buffer, new_current_buffer)
5706 && XBUFFER (p->buffer) == current_buffer)
5707 Fgoto_char (w->pointm);
5709 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
5710 /* Keep window's old buffer; make sure the markers are real. */
5712 /* Set window markers at start of visible range. */
5713 if (XMARKER (w->start)->buffer == 0)
5714 set_marker_restricted_both (w->start, w->contents, 0, 0);
5715 if (XMARKER (w->pointm)->buffer == 0)
5716 set_marker_restricted_both
5717 (w->pointm, w->contents,
5718 BUF_PT (XBUFFER (w->contents)),
5719 BUF_PT_BYTE (XBUFFER (w->contents)));
5720 w->start_at_line_beg = 1;
5722 else if (!NILP (w->start))
5723 /* Leaf window has no live buffer, get one. */
5725 /* Get the buffer via other_buffer_safely in order to
5726 avoid showing an unimportant buffer and, if necessary, to
5727 recreate *scratch* in the course (part of Juanma's bs-show
5728 scenario from March 2011). */
5729 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
5730 /* This will set the markers to beginning of visible
5731 range. */
5732 set_marker_restricted_both (w->start, w->contents, 0, 0);
5733 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
5734 w->start_at_line_beg = 1;
5735 if (!NILP (w->dedicated))
5736 /* Record this window as dead. */
5737 dead_windows = Fcons (window, dead_windows);
5738 /* Make sure window is no more dedicated. */
5739 wset_dedicated (w, Qnil);
5743 fset_root_window (f, data->root_window);
5744 /* Arrange *not* to restore point in the buffer that was
5745 current when the window configuration was saved. */
5746 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
5747 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5748 make_number (old_point),
5749 XWINDOW (data->current_window)->contents);
5751 /* In the following call to `select-window', prevent "swapping out
5752 point" in the old selected window using the buffer that has
5753 been restored into it. We already swapped out that point from
5754 that window's old buffer. */
5755 select_window (data->current_window, Qnil, 1);
5756 BVAR (XBUFFER (XWINDOW (selected_window)->contents), last_selected_window)
5757 = selected_window;
5759 if (NILP (data->focus_frame)
5760 || (FRAMEP (data->focus_frame)
5761 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5762 Fredirect_frame_focus (frame, data->focus_frame);
5764 /* Set the screen height to the value it had before this function. */
5765 if (previous_frame_lines != FRAME_LINES (f)
5766 || previous_frame_cols != FRAME_COLS (f))
5767 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5768 0, 0, 0);
5769 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5770 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5771 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5772 make_number (0));
5773 #ifdef HAVE_WINDOW_SYSTEM
5774 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5775 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5776 make_number (0));
5777 #endif
5778 #endif
5780 /* Now, free glyph matrices in windows that were not reused. */
5781 for (i = n = 0; i < n_leaf_windows; ++i)
5783 if (NILP (leaf_windows[i]->contents))
5784 free_window_matrices (leaf_windows[i]);
5785 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
5786 ++n;
5789 adjust_glyphs (f);
5790 unblock_input ();
5792 /* Scan dead buffer windows. */
5793 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5795 window = XCAR (dead_windows);
5796 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5797 delete_deletable_window (window);
5800 /* Fselect_window will have made f the selected frame, so we
5801 reselect the proper frame here. Fhandle_switch_frame will change the
5802 selected window too, but that doesn't make the call to
5803 Fselect_window above totally superfluous; it still sets f's
5804 selected window. */
5805 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5806 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5808 run_window_configuration_change_hook (f);
5811 if (!NILP (new_current_buffer))
5813 Fset_buffer (new_current_buffer);
5814 /* If the new current buffer doesn't appear in the selected
5815 window, go to its old point (see bug#12208). */
5816 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
5817 Fgoto_char (make_number (old_point));
5820 Vminibuf_scroll_window = data->minibuf_scroll_window;
5821 minibuf_selected_window = data->minibuf_selected_window;
5823 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5826 void
5827 restore_window_configuration (Lisp_Object configuration)
5829 Fset_window_configuration (configuration);
5833 /* If WINDOW is an internal window, recursively delete all child windows
5834 reachable via the next and contents slots of WINDOW. Otherwise setup
5835 WINDOW to not show any buffer. */
5837 void
5838 delete_all_child_windows (Lisp_Object window)
5840 register struct window *w;
5842 w = XWINDOW (window);
5844 if (!NILP (w->next))
5845 /* Delete WINDOW's siblings (we traverse postorderly). */
5846 delete_all_child_windows (w->next);
5848 if (WINDOWP (w->contents))
5850 delete_all_child_windows (w->contents);
5851 wset_combination (w, 0, Qnil);
5853 else if (BUFFERP (w->contents))
5855 unshow_buffer (w);
5856 unchain_marker (XMARKER (w->pointm));
5857 unchain_marker (XMARKER (w->start));
5858 /* Since combination limit makes sense for an internal windows
5859 only, we use this slot to save the buffer for the sake of
5860 possible resurrection in Fset_window_configuration. */
5861 wset_combination_limit (w, w->contents);
5862 wset_buffer (w, Qnil);
5865 Vwindow_list = Qnil;
5868 static int
5869 count_windows (register struct window *window)
5871 register int count = 1;
5872 if (!NILP (window->next))
5873 count += count_windows (XWINDOW (window->next));
5874 if (WINDOWP (window->contents))
5875 count += count_windows (XWINDOW (window->contents));
5876 return count;
5880 /* Fill vector FLAT with leaf windows under W, starting at index I.
5881 Value is last index + 1. */
5882 static int
5883 get_leaf_windows (struct window *w, struct window **flat, int i)
5885 while (w)
5887 if (WINDOWP (w->contents))
5888 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
5889 else
5890 flat[i++] = w;
5892 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5895 return i;
5899 /* Return a pointer to the glyph W's physical cursor is on. Value is
5900 null if W's current matrix is invalid, so that no meaningful glyph
5901 can be returned. */
5902 struct glyph *
5903 get_phys_cursor_glyph (struct window *w)
5905 struct glyph_row *row;
5906 struct glyph *glyph;
5907 int hpos = w->phys_cursor.hpos;
5909 if (!(w->phys_cursor.vpos >= 0
5910 && w->phys_cursor.vpos < w->current_matrix->nrows))
5911 return NULL;
5913 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5914 if (!row->enabled_p)
5915 return NULL;
5917 if (w->hscroll)
5919 /* When the window is hscrolled, cursor hpos can legitimately be
5920 out of bounds, but we draw the cursor at the corresponding
5921 window margin in that case. */
5922 if (!row->reversed_p && hpos < 0)
5923 hpos = 0;
5924 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
5925 hpos = row->used[TEXT_AREA] - 1;
5928 if (0 <= hpos && hpos < row->used[TEXT_AREA])
5929 glyph = row->glyphs[TEXT_AREA] + hpos;
5930 else
5931 glyph = NULL;
5933 return glyph;
5937 static int
5938 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
5940 register struct saved_window *p;
5941 register struct window *w;
5942 register Lisp_Object tem, pers, par;
5944 for (; !NILP (window); window = w->next)
5946 p = SAVED_WINDOW_N (vector, i);
5947 w = XWINDOW (window);
5949 wset_temslot (w, make_number (i)); i++;
5950 p->window = window;
5951 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
5952 p->left_col = make_number (w->left_col);
5953 p->top_line = make_number (w->top_line);
5954 p->total_cols = make_number (w->total_cols);
5955 p->total_lines = make_number (w->total_lines);
5956 p->normal_cols = w->normal_cols;
5957 p->normal_lines = w->normal_lines;
5958 XSETFASTINT (p->hscroll, w->hscroll);
5959 XSETFASTINT (p->min_hscroll, w->min_hscroll);
5960 p->display_table = w->display_table;
5961 p->left_margin_cols = make_number (w->left_margin_cols);
5962 p->right_margin_cols = make_number (w->right_margin_cols);
5963 p->left_fringe_width = make_number (w->left_fringe_width);
5964 p->right_fringe_width = make_number (w->right_fringe_width);
5965 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
5966 p->scroll_bar_width = make_number (w->scroll_bar_width);
5967 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5968 p->dedicated = w->dedicated;
5969 p->combination_limit = w->combination_limit;
5970 p->window_parameters = Qnil;
5972 if (!NILP (Vwindow_persistent_parameters))
5974 /* Run cycle detection on Vwindow_persistent_parameters. */
5975 Lisp_Object tortoise, hare;
5977 hare = tortoise = Vwindow_persistent_parameters;
5978 while (CONSP (hare))
5980 hare = XCDR (hare);
5981 if (!CONSP (hare))
5982 break;
5984 hare = XCDR (hare);
5985 tortoise = XCDR (tortoise);
5987 if (EQ (hare, tortoise))
5988 /* Reset Vwindow_persistent_parameters to Qnil. */
5990 Vwindow_persistent_parameters = Qnil;
5991 break;
5995 for (tem = Vwindow_persistent_parameters; CONSP (tem);
5996 tem = XCDR (tem))
5998 pers = XCAR (tem);
5999 /* Save values for persistent window parameters. */
6000 if (CONSP (pers) && !NILP (XCDR (pers)))
6002 par = Fassq (XCAR (pers), w->window_parameters);
6003 if (NILP (par))
6004 /* If the window has no value for the parameter,
6005 make one. */
6006 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6007 p->window_parameters);
6008 else
6009 /* If the window has a value for the parameter,
6010 save it. */
6011 p->window_parameters = Fcons (Fcons (XCAR (par),
6012 XCDR (par)),
6013 p->window_parameters);
6018 if (BUFFERP (w->contents))
6020 /* Save w's value of point in the window configuration. If w
6021 is the selected window, then get the value of point from
6022 the buffer; pointm is garbage in the selected window. */
6023 if (EQ (window, selected_window))
6024 p->pointm = build_marker (XBUFFER (w->contents),
6025 BUF_PT (XBUFFER (w->contents)),
6026 BUF_PT_BYTE (XBUFFER (w->contents)));
6027 else
6028 p->pointm = Fcopy_marker (w->pointm, Qnil);
6029 XMARKER (p->pointm)->insertion_type
6030 = !NILP (Vwindow_point_insertion_type);
6032 p->start = Fcopy_marker (w->start, Qnil);
6033 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6035 tem = BVAR (XBUFFER (w->contents), mark);
6036 p->mark = Fcopy_marker (tem, Qnil);
6038 else
6040 p->pointm = Qnil;
6041 p->start = Qnil;
6042 p->mark = Qnil;
6043 p->start_at_line_beg = Qnil;
6046 if (NILP (w->parent))
6047 p->parent = Qnil;
6048 else
6049 p->parent = XWINDOW (w->parent)->temslot;
6051 if (NILP (w->prev))
6052 p->prev = Qnil;
6053 else
6054 p->prev = XWINDOW (w->prev)->temslot;
6056 if (WINDOWP (w->contents))
6057 i = save_window_save (w->contents, vector, i);
6060 return i;
6063 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6064 Scurrent_window_configuration, 0, 1, 0,
6065 doc: /* Return an object representing the current window configuration of FRAME.
6066 If FRAME is nil or omitted, use the selected frame.
6067 This describes the number of windows, their sizes and current buffers,
6068 and for each displayed buffer, where display starts, and the positions of
6069 point and mark. An exception is made for point in the current buffer:
6070 its value is -not- saved.
6071 This also records the currently selected frame, and FRAME's focus
6072 redirection (see `redirect-frame-focus'). The variable
6073 `window-persistent-parameters' specifies which window parameters are
6074 saved by this function. */)
6075 (Lisp_Object frame)
6077 register Lisp_Object tem;
6078 register int n_windows;
6079 register struct save_window_data *data;
6080 register int i;
6081 struct frame *f = decode_live_frame (frame);
6083 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6084 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6085 PVEC_WINDOW_CONFIGURATION);
6087 data->frame_cols = FRAME_COLS (f);
6088 data->frame_lines = FRAME_LINES (f);
6089 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6090 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6091 data->selected_frame = selected_frame;
6092 data->current_window = FRAME_SELECTED_WINDOW (f);
6093 XSETBUFFER (data->f_current_buffer, current_buffer);
6094 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6095 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6096 data->root_window = FRAME_ROOT_WINDOW (f);
6097 data->focus_frame = FRAME_FOCUS_FRAME (f);
6098 tem = make_uninit_vector (n_windows);
6099 data->saved_windows = tem;
6100 for (i = 0; i < n_windows; i++)
6101 ASET (tem, i,
6102 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6103 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6104 XSETWINDOW_CONFIGURATION (tem, data);
6105 return (tem);
6108 /* Called after W's margins, fringes or scroll bars was adjusted. */
6110 static void
6111 apply_window_adjustment (struct window *w)
6113 eassert (w);
6114 adjust_window_margins (w);
6115 clear_glyph_matrix (w->current_matrix);
6116 w->window_end_valid = 0;
6117 windows_or_buffers_changed++;
6118 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6122 /***********************************************************************
6123 Marginal Areas
6124 ***********************************************************************/
6126 static struct window *
6127 set_window_margins (struct window *w, Lisp_Object left_width,
6128 Lisp_Object right_width)
6130 int left, right;
6132 /* FIXME: what about margins that are too wide? */
6133 left = (NILP (left_width) ? 0
6134 : (CHECK_NATNUM (left_width), XINT (left_width)));
6135 right = (NILP (right_width) ? 0
6136 : (CHECK_NATNUM (right_width), XINT (right_width)));
6138 if (w->left_margin_cols != left || w->right_margin_cols != right)
6140 w->left_margin_cols = left;
6141 w->right_margin_cols = right;
6142 return w;
6144 return NULL;
6147 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6148 2, 3, 0,
6149 doc: /* Set width of marginal areas of window WINDOW.
6150 WINDOW must be a live window and defaults to the selected one.
6152 Second arg LEFT-WIDTH specifies the number of character cells to
6153 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6154 does the same for the right marginal area. A nil width parameter
6155 means no margin.
6157 Return t if any margin was actually changed and nil otherwise. */)
6158 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6160 struct window *w = set_window_margins (decode_live_window (window),
6161 left_width, right_width);
6162 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6166 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6167 0, 1, 0,
6168 doc: /* Get width of marginal areas of window WINDOW.
6169 WINDOW must be a live window and defaults to the selected one.
6171 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6172 If a marginal area does not exist, its width will be returned
6173 as nil. */)
6174 (Lisp_Object window)
6176 struct window *w = decode_live_window (window);
6177 return Fcons (w->left_margin_cols ? make_number (w->left_margin_cols) : Qnil,
6178 w->right_margin_cols ? make_number (w->right_margin_cols) : Qnil);
6183 /***********************************************************************
6184 Fringes
6185 ***********************************************************************/
6187 static struct window *
6188 set_window_fringes (struct window *w, Lisp_Object left_width,
6189 Lisp_Object right_width, Lisp_Object outside_margins)
6191 int left, right, outside = !NILP (outside_margins);
6193 left = (NILP (left_width) ? -1
6194 : (CHECK_NATNUM (left_width), XINT (left_width)));
6195 right = (NILP (right_width) ? -1
6196 : (CHECK_NATNUM (right_width), XINT (right_width)));
6198 /* Do nothing on a tty or if nothing to actually change. */
6199 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6200 && (w->left_fringe_width != left
6201 || w->right_fringe_width != right
6202 || w->fringes_outside_margins != outside))
6204 w->left_fringe_width = left;
6205 w->right_fringe_width = right;
6206 w->fringes_outside_margins = outside;
6207 return w;
6209 return NULL;
6212 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6213 2, 4, 0,
6214 doc: /* Set the fringe widths of window WINDOW.
6215 WINDOW must be a live window and defaults to the selected one.
6217 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6218 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6219 fringe width. If a fringe width arg is nil, that means to use the
6220 frame's default fringe width. Default fringe widths can be set with
6221 the command `set-fringe-style'.
6222 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6223 outside of the display margins. By default, fringes are drawn between
6224 display marginal areas and the text area.
6226 Return t if any fringe was actually changed and nil otherwise. */)
6227 (Lisp_Object window, Lisp_Object left_width,
6228 Lisp_Object right_width, Lisp_Object outside_margins)
6230 struct window *w
6231 = set_window_fringes (decode_live_window (window),
6232 left_width, right_width, outside_margins);
6233 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6237 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6238 0, 1, 0,
6239 doc: /* Get width of fringes of window WINDOW.
6240 WINDOW must be a live window and defaults to the selected one.
6242 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6243 (Lisp_Object window)
6245 struct window *w = decode_live_window (window);
6247 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6248 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6249 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6254 /***********************************************************************
6255 Scroll bars
6256 ***********************************************************************/
6258 static struct window *
6259 set_window_scroll_bars (struct window *w, Lisp_Object width,
6260 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6262 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6264 if (iwidth == 0)
6265 vertical_type = Qnil;
6267 if (!(NILP (vertical_type)
6268 || EQ (vertical_type, Qleft)
6269 || EQ (vertical_type, Qright)
6270 || EQ (vertical_type, Qt)))
6271 error ("Invalid type of vertical scroll bar");
6273 if (w->scroll_bar_width != iwidth
6274 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6276 w->scroll_bar_width = iwidth;
6277 wset_vertical_scroll_bar_type (w, vertical_type);
6278 return w;
6280 return NULL;
6283 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6284 Sset_window_scroll_bars, 2, 4, 0,
6285 doc: /* Set width and type of scroll bars of window WINDOW.
6286 WINDOW must be a live window and defaults to the selected one.
6288 Second parameter WIDTH specifies the pixel width for the scroll bar;
6289 this is automatically adjusted to a multiple of the frame column width.
6290 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6291 bar: left, right, or nil.
6292 If WIDTH is nil, use the frame's scroll-bar width.
6293 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6294 Fourth parameter HORIZONTAL-TYPE is currently unused.
6296 Return t if scroll bars was actually changed and nil otherwise. */)
6297 (Lisp_Object window, Lisp_Object width,
6298 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6300 struct window *w
6301 = set_window_scroll_bars (decode_live_window (window),
6302 width, vertical_type, horizontal_type);
6303 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6307 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6308 0, 1, 0,
6309 doc: /* Get width and type of scroll bars of window WINDOW.
6310 WINDOW must be a live window and defaults to the selected one.
6312 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6313 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6314 value. */)
6315 (Lisp_Object window)
6317 struct window *w = decode_live_window (window);
6319 return list4 (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6320 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6321 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6322 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6323 w->vertical_scroll_bar_type, Qnil);
6328 /***********************************************************************
6329 Smooth scrolling
6330 ***********************************************************************/
6332 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6333 doc: /* Return the amount by which WINDOW is scrolled vertically.
6334 If WINDOW is omitted or nil, it defaults to the selected window.
6335 Normally, value is a multiple of the canonical character height of WINDOW;
6336 optional second arg PIXELS-P means value is measured in pixels. */)
6337 (Lisp_Object window, Lisp_Object pixels_p)
6339 Lisp_Object result;
6340 struct window *w = decode_live_window (window);
6341 struct frame *f = XFRAME (w->frame);
6343 if (FRAME_WINDOW_P (f))
6344 result = (NILP (pixels_p)
6345 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6346 : make_number (-w->vscroll));
6347 else
6348 result = make_number (0);
6349 return result;
6353 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6354 2, 3, 0,
6355 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6356 WINDOW nil means use the selected window. Normally, VSCROLL is a
6357 non-negative multiple of the canonical character height of WINDOW;
6358 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6359 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6360 corresponds to an integral number of pixels. The return value is the
6361 result of this rounding.
6362 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6363 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6365 struct window *w = decode_live_window (window);
6366 struct frame *f = XFRAME (w->frame);
6368 CHECK_NUMBER_OR_FLOAT (vscroll);
6370 if (FRAME_WINDOW_P (f))
6372 int old_dy = w->vscroll;
6374 w->vscroll = - (NILP (pixels_p)
6375 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6376 : XFLOATINT (vscroll));
6377 w->vscroll = min (w->vscroll, 0);
6379 if (w->vscroll != old_dy)
6381 /* Adjust glyph matrix of the frame if the virtual display
6382 area becomes larger than before. */
6383 if (w->vscroll < 0 && w->vscroll < old_dy)
6384 adjust_glyphs (f);
6386 /* Prevent redisplay shortcuts. */
6387 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
6391 return Fwindow_vscroll (window, pixels_p);
6395 /* Call FN for all leaf windows on frame F. FN is called with the
6396 first argument being a pointer to the leaf window, and with
6397 additional argument USER_DATA. Stops when FN returns 0. */
6399 static void
6400 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6401 void *user_data)
6403 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6404 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6405 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6409 /* Helper function for foreach_window. Call FN for all leaf windows
6410 reachable from W. FN is called with the first argument being a
6411 pointer to the leaf window, and with additional argument USER_DATA.
6412 Stop when FN returns 0. Value is 0 if stopped by FN. */
6414 static int
6415 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6417 int cont;
6419 for (cont = 1; w && cont;)
6421 if (WINDOWP (w->contents))
6422 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
6423 else
6424 cont = fn (w, user_data);
6426 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6429 return cont;
6432 /***********************************************************************
6433 Initialization
6434 ***********************************************************************/
6436 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6437 describe the same state of affairs. This is used by Fequal.
6439 IGNORE_POSITIONS means ignore non-matching scroll positions
6440 and the like.
6442 This ignores a couple of things like the dedication status of
6443 window, combination_limit and the like. This might have to be
6444 fixed. */
6446 bool
6447 compare_window_configurations (Lisp_Object configuration1,
6448 Lisp_Object configuration2,
6449 bool ignore_positions)
6451 register struct save_window_data *d1, *d2;
6452 struct Lisp_Vector *sws1, *sws2;
6453 ptrdiff_t i;
6455 CHECK_WINDOW_CONFIGURATION (configuration1);
6456 CHECK_WINDOW_CONFIGURATION (configuration2);
6458 d1 = (struct save_window_data *) XVECTOR (configuration1);
6459 d2 = (struct save_window_data *) XVECTOR (configuration2);
6460 sws1 = XVECTOR (d1->saved_windows);
6461 sws2 = XVECTOR (d2->saved_windows);
6463 /* Frame settings must match. */
6464 if (d1->frame_cols != d2->frame_cols
6465 || d1->frame_lines != d2->frame_lines
6466 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6467 || !EQ (d1->selected_frame, d2->selected_frame)
6468 || !EQ (d1->f_current_buffer, d2->f_current_buffer)
6469 || (!ignore_positions
6470 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6471 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6472 || !EQ (d1->focus_frame, d2->focus_frame)
6473 /* Verify that the two configurations have the same number of windows. */
6474 || sws1->header.size != sws2->header.size)
6475 return 0;
6477 for (i = 0; i < sws1->header.size; i++)
6479 struct saved_window *sw1, *sw2;
6481 sw1 = SAVED_WINDOW_N (sws1, i);
6482 sw2 = SAVED_WINDOW_N (sws2, i);
6484 if (
6485 /* The "current" windows in the two configurations must
6486 correspond to each other. */
6487 EQ (d1->current_window, sw1->window)
6488 != EQ (d2->current_window, sw2->window)
6489 /* Windows' buffers must match. */
6490 || !EQ (sw1->buffer, sw2->buffer)
6491 || !EQ (sw1->left_col, sw2->left_col)
6492 || !EQ (sw1->top_line, sw2->top_line)
6493 || !EQ (sw1->total_cols, sw2->total_cols)
6494 || !EQ (sw1->total_lines, sw2->total_lines)
6495 || !EQ (sw1->display_table, sw2->display_table)
6496 /* The next two disjuncts check the window structure for
6497 equality. */
6498 || !EQ (sw1->parent, sw2->parent)
6499 || !EQ (sw1->prev, sw2->prev)
6500 || (!ignore_positions
6501 && (!EQ (sw1->hscroll, sw2->hscroll)
6502 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6503 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6504 || NILP (Fequal (sw1->start, sw2->start))
6505 || NILP (Fequal (sw1->pointm, sw2->pointm))
6506 || NILP (Fequal (sw1->mark, sw2->mark))))
6507 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6508 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6509 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6510 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6511 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6512 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6513 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6514 return 0;
6517 return 1;
6520 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6521 Scompare_window_configurations, 2, 2, 0,
6522 doc: /* Compare two window configurations as regards the structure of windows.
6523 This function ignores details such as the values of point and mark
6524 and scrolling positions. */)
6525 (Lisp_Object x, Lisp_Object y)
6527 if (compare_window_configurations (x, y, 1))
6528 return Qt;
6529 return Qnil;
6532 void
6533 init_window_once (void)
6535 struct frame *f = make_initial_frame ();
6536 XSETFRAME (selected_frame, f);
6537 Vterminal_frame = selected_frame;
6538 minibuf_window = f->minibuffer_window;
6539 selected_window = f->selected_window;
6540 last_nonminibuf_frame = f;
6542 window_initialized = 1;
6545 void
6546 init_window (void)
6548 Vwindow_list = Qnil;
6551 void
6552 syms_of_window (void)
6554 DEFSYM (Qscroll_up, "scroll-up");
6555 DEFSYM (Qscroll_down, "scroll-down");
6556 DEFSYM (Qscroll_command, "scroll-command");
6558 Fput (Qscroll_up, Qscroll_command, Qt);
6559 Fput (Qscroll_down, Qscroll_command, Qt);
6561 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6562 DEFSYM (Qwindowp, "windowp");
6563 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6564 DEFSYM (Qwindow_live_p, "window-live-p");
6565 DEFSYM (Qwindow_valid_p, "window-valid-p");
6566 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6567 DEFSYM (Qdelete_window, "delete-window");
6568 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6569 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6570 DEFSYM (Qsafe, "safe");
6571 DEFSYM (Qdisplay_buffer, "display-buffer");
6572 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6573 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6574 DEFSYM (Qget_mru_window, "get-mru-window");
6575 DEFSYM (Qwindow_size, "window-size");
6576 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6577 DEFSYM (Qabove, "above");
6578 DEFSYM (Qbelow, "below");
6579 DEFSYM (Qclone_of, "clone-of");
6581 staticpro (&Vwindow_list);
6583 minibuf_selected_window = Qnil;
6584 staticpro (&minibuf_selected_window);
6586 window_scroll_pixel_based_preserve_x = -1;
6587 window_scroll_pixel_based_preserve_y = -1;
6588 window_scroll_preserve_hpos = -1;
6589 window_scroll_preserve_vpos = -1;
6591 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6592 doc: /* Non-nil means call as function to display a help buffer.
6593 The function is called with one argument, the buffer to be displayed.
6594 Used by `with-output-to-temp-buffer'.
6595 If this function is used, then it must do the entire job of showing
6596 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6597 Vtemp_buffer_show_function = Qnil;
6599 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6600 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6601 Vminibuf_scroll_window = Qnil;
6603 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6604 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6605 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6606 is displayed in the `mode-line' face. */);
6607 mode_line_in_non_selected_windows = 1;
6609 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6610 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6611 Vother_window_scroll_buffer = Qnil;
6613 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6614 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6615 auto_window_vscroll_p = 1;
6617 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6618 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6619 next_screen_context_lines = 2;
6621 DEFVAR_LISP ("scroll-preserve-screen-position",
6622 Vscroll_preserve_screen_position,
6623 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6624 A value of nil means point does not keep its screen position except
6625 at the scroll margin or window boundary respectively.
6626 A value of t means point keeps its screen position if the scroll
6627 command moved it vertically out of the window, e.g. when scrolling
6628 by full screens.
6629 Any other value means point always keeps its screen position.
6630 Scroll commands should have the `scroll-command' property
6631 on their symbols to be controlled by this variable. */);
6632 Vscroll_preserve_screen_position = Qnil;
6634 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6635 doc: /* Type of marker to use for `window-point'. */);
6636 Vwindow_point_insertion_type = Qnil;
6638 DEFVAR_LISP ("window-configuration-change-hook",
6639 Vwindow_configuration_change_hook,
6640 doc: /* Functions to call when window configuration changes.
6641 The buffer-local part is run once per window, with the relevant window
6642 selected; while the global part is run only once for the modified frame,
6643 with the relevant frame selected. */);
6644 Vwindow_configuration_change_hook = Qnil;
6646 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6647 doc: /* Non-nil means `recenter' redraws entire frame.
6648 If this option is non-nil, then the `recenter' command with a nil
6649 argument will redraw the entire frame; the special value `tty' causes
6650 the frame to be redrawn only if it is a tty frame. */);
6651 Vrecenter_redisplay = Qtty;
6653 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6654 doc: /* If t, resize window combinations proportionally.
6655 If this variable is nil, splitting a window gets the entire screen space
6656 for displaying the new window from the window to split. Deleting and
6657 resizing a window preferably resizes one adjacent window only.
6659 If this variable is t, splitting a window tries to get the space
6660 proportionally from all windows in the same combination. This also
6661 allows to split a window that is otherwise too small or of fixed size.
6662 Resizing and deleting a window proportionally resize all windows in the
6663 same combination.
6665 Other values are reserved for future use.
6667 This variable takes no effect if the variable `window-combination-limit' is
6668 non-nil. */);
6669 Vwindow_combination_resize = Qnil;
6671 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6672 doc: /* If non-nil, splitting a window makes a new parent window.
6673 The following values are recognized:
6675 nil means splitting a window will create a new parent window only if the
6676 window has no parent window or the window shall become part of a
6677 combination orthogonal to the one it is part of.
6679 `window-size' means that splitting a window for displaying a buffer
6680 makes a new parent window provided `display-buffer' is supposed to
6681 explicitly set the window's size due to the presence of a
6682 `window-height' or `window-width' entry in the alist used by
6683 `display-buffer'. Otherwise, this value is handled like nil.
6685 `temp-buffer' means that splitting a window for displaying a temporary
6686 buffer always makes a new parent window. Otherwise, this value is
6687 handled like nil.
6689 `display-buffer' means that splitting a window for displaying a buffer
6690 always makes a new parent window. Since temporary buffers are
6691 displayed by the function `display-buffer', this value is stronger
6692 than `temp-buffer'. Splitting a window for other purpose makes a
6693 new parent window only if needed.
6695 t means that splitting a window always creates a new parent window. If
6696 all splits behave this way, each frame's window tree is a binary
6697 tree and every window but the frame's root window has exactly one
6698 sibling.
6700 Other values are reserved for future use. */);
6701 Vwindow_combination_limit = Qwindow_size;
6703 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6704 doc: /* Alist of persistent window parameters.
6705 This alist specifies which window parameters shall get saved by
6706 `current-window-configuration' and `window-state-get' and subsequently
6707 restored to their previous values by `set-window-configuration' and
6708 `window-state-put'.
6710 The car of each entry of this alist is the symbol specifying the
6711 parameter. The cdr is one of the following:
6713 nil means the parameter is neither saved by `window-state-get' nor by
6714 `current-window-configuration'.
6716 t means the parameter is saved by `current-window-configuration' and,
6717 provided its WRITABLE argument is nil, by `window-state-get'.
6719 The symbol `writable' means the parameter is saved unconditionally by
6720 both `current-window-configuration' and `window-state-get'. Do not use
6721 this value for parameters without read syntax (like windows or frames).
6723 Parameters not saved by `current-window-configuration' or
6724 `window-state-get' are left alone by `set-window-configuration'
6725 respectively are not installed by `window-state-put'. */);
6726 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6728 defsubr (&Sselected_window);
6729 defsubr (&Sminibuffer_window);
6730 defsubr (&Swindow_minibuffer_p);
6731 defsubr (&Swindowp);
6732 defsubr (&Swindow_valid_p);
6733 defsubr (&Swindow_live_p);
6734 defsubr (&Swindow_frame);
6735 defsubr (&Sframe_root_window);
6736 defsubr (&Sframe_first_window);
6737 defsubr (&Sframe_selected_window);
6738 defsubr (&Sset_frame_selected_window);
6739 defsubr (&Spos_visible_in_window_p);
6740 defsubr (&Swindow_line_height);
6741 defsubr (&Swindow_buffer);
6742 defsubr (&Swindow_parent);
6743 defsubr (&Swindow_top_child);
6744 defsubr (&Swindow_left_child);
6745 defsubr (&Swindow_next_sibling);
6746 defsubr (&Swindow_prev_sibling);
6747 defsubr (&Swindow_combination_limit);
6748 defsubr (&Sset_window_combination_limit);
6749 defsubr (&Swindow_use_time);
6750 defsubr (&Swindow_top_line);
6751 defsubr (&Swindow_left_column);
6752 defsubr (&Swindow_total_height);
6753 defsubr (&Swindow_total_width);
6754 defsubr (&Swindow_normal_size);
6755 defsubr (&Swindow_new_total);
6756 defsubr (&Swindow_new_normal);
6757 defsubr (&Sset_window_new_total);
6758 defsubr (&Sset_window_new_normal);
6759 defsubr (&Swindow_resize_apply);
6760 defsubr (&Swindow_body_height);
6761 defsubr (&Swindow_body_width);
6762 defsubr (&Swindow_hscroll);
6763 defsubr (&Sset_window_hscroll);
6764 defsubr (&Swindow_redisplay_end_trigger);
6765 defsubr (&Sset_window_redisplay_end_trigger);
6766 defsubr (&Swindow_edges);
6767 defsubr (&Swindow_pixel_edges);
6768 defsubr (&Swindow_absolute_pixel_edges);
6769 defsubr (&Swindow_inside_edges);
6770 defsubr (&Swindow_inside_pixel_edges);
6771 defsubr (&Swindow_inside_absolute_pixel_edges);
6772 defsubr (&Scoordinates_in_window_p);
6773 defsubr (&Swindow_at);
6774 defsubr (&Swindow_point);
6775 defsubr (&Swindow_start);
6776 defsubr (&Swindow_end);
6777 defsubr (&Sset_window_point);
6778 defsubr (&Sset_window_start);
6779 defsubr (&Swindow_dedicated_p);
6780 defsubr (&Sset_window_dedicated_p);
6781 defsubr (&Swindow_display_table);
6782 defsubr (&Sset_window_display_table);
6783 defsubr (&Snext_window);
6784 defsubr (&Sprevious_window);
6785 defsubr (&Sget_buffer_window);
6786 defsubr (&Sdelete_other_windows_internal);
6787 defsubr (&Sdelete_window_internal);
6788 defsubr (&Sresize_mini_window_internal);
6789 defsubr (&Sset_window_buffer);
6790 defsubr (&Srun_window_configuration_change_hook);
6791 defsubr (&Sselect_window);
6792 defsubr (&Sforce_window_update);
6793 defsubr (&Ssplit_window_internal);
6794 defsubr (&Sscroll_up);
6795 defsubr (&Sscroll_down);
6796 defsubr (&Sscroll_left);
6797 defsubr (&Sscroll_right);
6798 defsubr (&Sother_window_for_scrolling);
6799 defsubr (&Sscroll_other_window);
6800 defsubr (&Sminibuffer_selected_window);
6801 defsubr (&Srecenter);
6802 defsubr (&Swindow_text_height);
6803 defsubr (&Smove_to_window_line);
6804 defsubr (&Swindow_configuration_p);
6805 defsubr (&Swindow_configuration_frame);
6806 defsubr (&Sset_window_configuration);
6807 defsubr (&Scurrent_window_configuration);
6808 defsubr (&Sset_window_margins);
6809 defsubr (&Swindow_margins);
6810 defsubr (&Sset_window_fringes);
6811 defsubr (&Swindow_fringes);
6812 defsubr (&Sset_window_scroll_bars);
6813 defsubr (&Swindow_scroll_bars);
6814 defsubr (&Swindow_vscroll);
6815 defsubr (&Sset_window_vscroll);
6816 defsubr (&Scompare_window_configurations);
6817 defsubr (&Swindow_list);
6818 defsubr (&Swindow_list_1);
6819 defsubr (&Swindow_prev_buffers);
6820 defsubr (&Sset_window_prev_buffers);
6821 defsubr (&Swindow_next_buffers);
6822 defsubr (&Sset_window_next_buffers);
6823 defsubr (&Swindow_parameters);
6824 defsubr (&Swindow_parameter);
6825 defsubr (&Sset_window_parameter);
6828 void
6829 keys_of_window (void)
6831 initial_define_key (control_x_map, '<', "scroll-left");
6832 initial_define_key (control_x_map, '>', "scroll-right");
6834 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6835 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6836 initial_define_key (meta_map, 'v', "scroll-down-command");