Fix a bug in the state cache mechanism. Refactor this a bit.
[emacs.git] / src / window.c
blob467c0861044c669aa80500e21e6e154599048685
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, Qrecord_window_buffer;
59 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
60 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
61 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
62 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
63 static Lisp_Object Qsafe, Qabove, Qbelow, Qwindow_size, Qclone_of;
65 static int displayed_window_lines (struct window *);
66 static int count_windows (struct window *);
67 static int get_leaf_windows (struct window *, struct window **, int);
68 static void window_scroll (Lisp_Object, EMACS_INT, int, int);
69 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
70 static void window_scroll_line_based (Lisp_Object, int, int, int);
71 static int freeze_window_start (struct window *, void *);
72 static Lisp_Object window_list (void);
73 static int add_window_to_list (struct window *, void *);
74 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
75 Lisp_Object);
76 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
77 Lisp_Object, int);
78 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
79 Lisp_Object *);
80 static void foreach_window (struct frame *,
81 int (* fn) (struct window *, void *),
82 void *);
83 static int foreach_window_1 (struct window *,
84 int (* fn) (struct window *, void *),
85 void *);
86 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
87 static int window_resize_check (struct window *, int);
88 static void window_resize_apply (struct window *, int);
89 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
91 /* This is the window in which the terminal's cursor should
92 be left when nothing is being done with it. This must
93 always be a leaf window, and its buffer is selected by
94 the top level editing loop at the end of each command.
96 This value is always the same as
97 FRAME_SELECTED_WINDOW (selected_frame). */
98 Lisp_Object selected_window;
100 /* A list of all windows for use by next_window and Fwindow_list.
101 Functions creating or deleting windows should invalidate this cache
102 by setting it to nil. */
103 Lisp_Object Vwindow_list;
105 /* The mini-buffer window of the selected frame.
106 Note that you cannot test for mini-bufferness of an arbitrary window
107 by comparing against this; but you can test for mini-bufferness of
108 the selected window. */
109 Lisp_Object minibuf_window;
111 /* Non-nil means it is the window whose mode line should be
112 shown as the selected window when the minibuffer is selected. */
113 Lisp_Object minibuf_selected_window;
115 /* Hook run at end of temp_output_buffer_show. */
116 static Lisp_Object Qtemp_buffer_show_hook;
118 /* Incremented for each window created. */
119 static int sequence_number;
121 /* Nonzero after init_window_once has finished. */
122 static int window_initialized;
124 /* Hook to run when window config changes. */
125 static Lisp_Object Qwindow_configuration_change_hook;
127 /* Used by the function window_scroll_pixel_based */
128 static int window_scroll_pixel_based_preserve_x;
129 static int window_scroll_pixel_based_preserve_y;
131 /* Same for window_scroll_line_based. */
132 static EMACS_INT window_scroll_preserve_hpos;
133 static EMACS_INT window_scroll_preserve_vpos;
135 /* These setters are used only in this file, so they can be private. */
136 static void
137 wset_combination_limit (struct window *w, Lisp_Object val)
139 w->combination_limit = val;
141 static void
142 wset_dedicated (struct window *w, Lisp_Object val)
144 w->dedicated = val;
146 static void
147 wset_display_table (struct window *w, Lisp_Object val)
149 w->display_table = val;
151 static void
152 wset_hchild (struct window *w, Lisp_Object val)
154 w->hchild = val;
156 static void
157 wset_left_fringe_width (struct window *w, Lisp_Object val)
159 w->left_fringe_width = val;
161 static void
162 wset_left_margin_cols (struct window *w, Lisp_Object val)
164 w->left_margin_cols = val;
166 static void
167 wset_new_normal (struct window *w, Lisp_Object val)
169 w->new_normal = val;
171 static void
172 wset_new_total (struct window *w, Lisp_Object val)
174 w->new_total = val;
176 static void
177 wset_normal_cols (struct window *w, Lisp_Object val)
179 w->normal_cols = val;
181 static void
182 wset_normal_lines (struct window *w, Lisp_Object val)
184 w->normal_lines = val;
186 static void
187 wset_parent (struct window *w, Lisp_Object val)
189 w->parent = val;
191 static void
192 wset_pointm (struct window *w, Lisp_Object val)
194 w->pointm = val;
196 static void
197 wset_right_fringe_width (struct window *w, Lisp_Object val)
199 w->right_fringe_width = val;
201 static void
202 wset_right_margin_cols (struct window *w, Lisp_Object val)
204 w->right_margin_cols = val;
206 static void
207 wset_scroll_bar_width (struct window *w, Lisp_Object val)
209 w->scroll_bar_width = val;
211 static void
212 wset_start (struct window *w, Lisp_Object val)
214 w->start = val;
216 static void
217 wset_temslot (struct window *w, Lisp_Object val)
219 w->temslot = val;
221 static void
222 wset_vchild (struct window *w, Lisp_Object val)
224 w->vchild = val;
226 static void
227 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
229 w->vertical_scroll_bar_type = val;
231 static void
232 wset_window_parameters (struct window *w, Lisp_Object val)
234 w->window_parameters = val;
237 struct window *
238 decode_live_window (register Lisp_Object window)
240 if (NILP (window))
241 return XWINDOW (selected_window);
243 CHECK_LIVE_WINDOW (window);
244 return XWINDOW (window);
247 static struct window *
248 decode_any_window (register Lisp_Object window)
250 struct window *w;
252 if (NILP (window))
253 return XWINDOW (selected_window);
255 CHECK_WINDOW (window);
256 w = XWINDOW (window);
257 return w;
260 static struct window *
261 decode_valid_window (register Lisp_Object window)
263 struct window *w;
265 if (NILP (window))
266 return XWINDOW (selected_window);
268 CHECK_VALID_WINDOW (window);
269 w = XWINDOW (window);
270 return w;
273 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
274 doc: /* Return t if OBJECT is a window and nil otherwise. */)
275 (Lisp_Object object)
277 return WINDOWP (object) ? Qt : Qnil;
280 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
281 doc: /* Return t if OBJECT is a valid window and nil otherwise.
282 A valid window is either a window that displays a buffer or an internal
283 window. Deleted windows are not live. */)
284 (Lisp_Object object)
286 return WINDOW_VALID_P (object) ? Qt : Qnil;
289 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
290 doc: /* Return t if OBJECT is a live window and nil otherwise.
291 A live window is a window that displays a buffer.
292 Internal windows and deleted windows are not live. */)
293 (Lisp_Object object)
295 return WINDOW_LIVE_P (object) ? Qt : Qnil;
298 /* Frames and windows. */
299 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 1, 1, 0,
300 doc: /* Return the frame that window WINDOW is on.
301 WINDOW must be a valid window and defaults to the selected one. */)
302 (Lisp_Object window)
304 return decode_valid_window (window)->frame;
307 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
308 doc: /* Return the root window of FRAME-OR-WINDOW.
309 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
310 With a frame argument, return that frame's root window.
311 With a window argument, return the root window of that window's frame. */)
312 (Lisp_Object frame_or_window)
314 Lisp_Object window;
316 if (NILP (frame_or_window))
317 window = SELECTED_FRAME ()->root_window;
318 else if (WINDOW_VALID_P (frame_or_window))
319 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
320 else
322 CHECK_LIVE_FRAME (frame_or_window);
323 window = XFRAME (frame_or_window)->root_window;
326 return window;
329 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
330 doc: /* Return the minibuffer window for frame FRAME.
331 If FRAME is omitted or nil, it defaults to the selected frame. */)
332 (Lisp_Object frame)
334 if (NILP (frame))
335 frame = selected_frame;
336 CHECK_LIVE_FRAME (frame);
337 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
340 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
341 Swindow_minibuffer_p, 0, 1, 0,
342 doc: /* Return non-nil if WINDOW is a minibuffer window.
343 WINDOW must be a valid window and defaults to the selected one. */)
344 (Lisp_Object window)
346 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
349 /* Don't move this to window.el - this must be a safe routine. */
350 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
351 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
352 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
353 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
354 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
355 the first window of that frame. */)
356 (Lisp_Object frame_or_window)
358 Lisp_Object window;
360 if (NILP (frame_or_window))
361 window = SELECTED_FRAME ()->root_window;
362 else if (WINDOW_VALID_P (frame_or_window))
363 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
364 else
366 CHECK_LIVE_FRAME (frame_or_window);
367 window = XFRAME (frame_or_window)->root_window;
370 while (NILP (XWINDOW (window)->buffer))
372 if (! NILP (XWINDOW (window)->hchild))
373 window = XWINDOW (window)->hchild;
374 else if (! NILP (XWINDOW (window)->vchild))
375 window = XWINDOW (window)->vchild;
376 else
377 emacs_abort ();
380 return window;
383 DEFUN ("frame-selected-window", Fframe_selected_window,
384 Sframe_selected_window, 0, 1, 0,
385 doc: /* Return the selected window of FRAME-OR-WINDOW.
386 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
387 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
388 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
389 return the selected window of that frame. */)
390 (Lisp_Object frame_or_window)
392 Lisp_Object window;
394 if (NILP (frame_or_window))
395 window = SELECTED_FRAME ()->selected_window;
396 else if (WINDOW_VALID_P (frame_or_window))
397 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
398 else
400 CHECK_LIVE_FRAME (frame_or_window);
401 window = XFRAME (frame_or_window)->selected_window;
404 return window;
407 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
408 Sset_frame_selected_window, 2, 3, 0,
409 doc: /* Set selected window of FRAME to WINDOW.
410 FRAME must be a live frame and defaults to the selected one. If FRAME
411 is the selected frame, this makes WINDOW the selected window. Optional
412 argument NORECORD non-nil means to neither change the order of recently
413 selected windows nor the buffer list. WINDOW must denote a live window.
414 Return WINDOW. */)
415 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
417 if (NILP (frame))
418 frame = selected_frame;
420 CHECK_LIVE_FRAME (frame);
421 CHECK_LIVE_WINDOW (window);
423 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
424 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
426 if (EQ (frame, selected_frame))
427 return Fselect_window (window, norecord);
428 else
430 fset_selected_window (XFRAME (frame), window);
431 return window;
435 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
436 doc: /* Return the selected window.
437 The selected window is the window in which the standard cursor for
438 selected windows appears and to which many commands apply. */)
439 (void)
441 return selected_window;
444 int window_select_count;
446 /* If select_window is called with inhibit_point_swap non-zero it will
447 not store point of the old selected window's buffer back into that
448 window's pointm slot. This is needed by Fset_window_configuration to
449 avoid that the display routine is called with selected_window set to
450 Qnil causing a subsequent crash. */
451 static Lisp_Object
452 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
454 register struct window *w;
455 register struct window *ow;
456 struct frame *sf;
458 CHECK_LIVE_WINDOW (window);
460 w = XWINDOW (window);
461 w->frozen_window_start_p = 0;
463 if (NILP (norecord))
465 w->use_time = ++window_select_count;
466 record_buffer (w->buffer);
469 /* Make the selected window's buffer current. */
470 Fset_buffer (w->buffer);
472 if (EQ (window, selected_window) && !inhibit_point_swap)
473 return window;
475 sf = SELECTED_FRAME ();
476 if (XFRAME (WINDOW_FRAME (w)) != sf)
478 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
479 /* Use this rather than Fhandle_switch_frame
480 so that FRAME_FOCUS_FRAME is moved appropriately as we
481 move around in the state where a minibuffer in a separate
482 frame is active. */
483 Fselect_frame (WINDOW_FRAME (w), norecord);
484 /* Fselect_frame called us back so we've done all the work already. */
485 eassert (EQ (window, selected_window));
486 return window;
488 else
489 fset_selected_window (sf, window);
491 /* Store the old selected window's buffer's point in pointm of the old
492 selected window. It belongs to that window, and when the window is
493 not selected, must be in the window. */
494 if (!inhibit_point_swap)
496 ow = XWINDOW (selected_window);
497 if (! NILP (ow->buffer))
498 set_marker_both (ow->pointm, ow->buffer,
499 BUF_PT (XBUFFER (ow->buffer)),
500 BUF_PT_BYTE (XBUFFER (ow->buffer)));
503 selected_window = window;
504 bset_last_selected_window (XBUFFER (w->buffer), window);
506 /* Go to the point recorded in the window.
507 This is important when the buffer is in more
508 than one window. It also matters when
509 redisplay_window has altered point after scrolling,
510 because it makes the change only in the window. */
512 register ptrdiff_t new_point = marker_position (w->pointm);
513 if (new_point < BEGV)
514 SET_PT (BEGV);
515 else if (new_point > ZV)
516 SET_PT (ZV);
517 else
518 SET_PT (new_point);
521 windows_or_buffers_changed++;
522 return window;
525 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
526 doc: /* Select WINDOW which must be a live window.
527 Also make WINDOW's frame the selected frame and WINDOW that frame's
528 selected window. In addition, make WINDOW's buffer current and set that
529 buffer's value of `point' to the value of WINDOW's `window-point'.
530 Return WINDOW.
532 Optional second arg NORECORD non-nil means do not put this buffer at the
533 front of the buffer list and do not make this window the most recently
534 selected one.
536 Note that the main editor command loop sets the current buffer to the
537 buffer of the selected window before each command. */)
538 (register Lisp_Object window, Lisp_Object norecord)
540 return select_window (window, norecord, 0);
543 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
544 doc: /* Return the buffer displayed in window WINDOW.
545 If WINDOW is omitted or nil, it defaults to the selected window.
546 Return nil for an internal window or a deleted window. */)
547 (Lisp_Object window)
549 return decode_any_window (window)->buffer;
552 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
553 doc: /* Return the parent window of window WINDOW.
554 WINDOW must be a valid window and defaults to the selected one.
555 Return nil for a window with no parent (e.g. a root window). */)
556 (Lisp_Object window)
558 return decode_valid_window (window)->parent;
561 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 1, 1, 0,
562 doc: /* Return the topmost child window of window WINDOW.
563 WINDOW must be a valid window and defaults to the selected one.
564 Return nil if WINDOW is a live window (live windows have no children).
565 Return nil if WINDOW is an internal window whose children form a
566 horizontal combination. */)
567 (Lisp_Object window)
569 CHECK_WINDOW (window);
570 return decode_valid_window (window)->vchild;
573 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 1, 1, 0,
574 doc: /* Return the leftmost child window of window WINDOW.
575 WINDOW must be a valid window and defaults to the selected one.
576 Return nil if WINDOW is a live window (live windows have no children).
577 Return nil if WINDOW is an internal window whose children form a
578 vertical combination. */)
579 (Lisp_Object window)
581 CHECK_WINDOW (window);
582 return decode_valid_window (window)->hchild;
585 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
586 doc: /* Return the next sibling window of window WINDOW.
587 WINDOW must be a valid window and defaults to the selected one.
588 Return nil if WINDOW has no next sibling. */)
589 (Lisp_Object window)
591 return decode_valid_window (window)->next;
594 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
595 doc: /* Return the previous sibling window of window WINDOW.
596 WINDOW must be a valid window and defaults to the selected one.
597 Return nil if WINDOW has no previous sibling. */)
598 (Lisp_Object window)
600 return decode_valid_window (window)->prev;
603 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
604 doc: /* Return combination limit of window WINDOW.
605 If the return value is nil, child windows of WINDOW can be recombined
606 with WINDOW's siblings. A return value of t means that child windows of
607 WINDOW are never \(re-)combined with WINDOW's siblings.
609 WINDOW must be a valid window. The return value is meaningful for
610 internal windows only. */)
611 (Lisp_Object window)
613 return decode_valid_window (window)->combination_limit;
616 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
617 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
618 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
619 siblings. LIMIT t means that child windows of WINDOW are never
620 \(re-)combined with WINDOW's siblings. Other values are reserved for
621 future use.
623 WINDOW must be a valid window. Setting the combination limit is
624 meaningful for internal windows only. */)
625 (Lisp_Object window, Lisp_Object limit)
627 wset_combination_limit (decode_valid_window (window), limit);
628 return limit;
631 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
632 doc: /* Return the use time of window WINDOW.
633 WINDOW must be a live window and defaults to the selected one.
634 The window with the highest use time is the most recently selected
635 one. The window with the lowest use time is the least recently
636 selected one. */)
637 (Lisp_Object window)
639 return make_number (decode_live_window (window)->use_time);
642 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
643 doc: /* Return the total height, in lines, of window WINDOW.
644 WINDOW must be a valid window and defaults to the selected one.
646 The return value includes the mode line and header line, if any.
647 If WINDOW is an internal window, the total height is the height
648 of the screen areas spanned by its children.
650 On a graphical display, this total height is reported as an
651 integer multiple of the default character height. */)
652 (Lisp_Object window)
654 return decode_valid_window (window)->total_lines;
657 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
658 doc: /* Return the total width, in columns, of window WINDOW.
659 WINDOW must be a valid window and defaults to the selected one.
661 The return value includes any vertical dividers or scroll bars
662 belonging to WINDOW. If WINDOW is an internal window, the total width
663 is the width of the screen areas spanned by its children.
665 On a graphical display, this total width is reported as an
666 integer multiple of the default character width. */)
667 (Lisp_Object window)
669 return decode_valid_window (window)->total_cols;
672 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
673 doc: /* Return the new total size of window WINDOW.
674 WINDOW must be a valid window and defaults to the selected one. */)
675 (Lisp_Object window)
677 return decode_valid_window (window)->new_total;
680 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
681 doc: /* Return the normal height of window WINDOW.
682 WINDOW must be a valid window and defaults to the selected one.
683 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
684 (Lisp_Object window, Lisp_Object horizontal)
686 struct window *w = decode_valid_window (window);
688 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
691 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
692 doc: /* Return new normal size of window WINDOW.
693 WINDOW must be a valid window and defaults to the selected one. */)
694 (Lisp_Object window)
696 return decode_valid_window (window)->new_normal;
699 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
700 doc: /* Return left column of window WINDOW.
701 This is the distance, in columns, between the left edge of WINDOW and
702 the left edge of the frame's window area. For instance, the return
703 value is 0 if there is no window to the left of WINDOW.
705 WINDOW must be a valid window and defaults to the selected one. */)
706 (Lisp_Object window)
708 return decode_valid_window (window)->left_col;
711 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
712 doc: /* Return top line of window WINDOW.
713 This is the distance, in lines, between the top of WINDOW and the top
714 of the frame's window area. For instance, the return value is 0 if
715 there is no window above WINDOW.
717 WINDOW must be a valid window and defaults to the selected one. */)
718 (Lisp_Object window)
720 return decode_valid_window (window)->top_line;
723 /* Return the number of lines of W's body. Don't count any mode or
724 header line of W. */
726 static int
727 window_body_lines (struct window *w)
729 int height = XFASTINT (w->total_lines);
731 if (!MINI_WINDOW_P (w))
733 if (WINDOW_WANTS_MODELINE_P (w))
734 --height;
735 if (WINDOW_WANTS_HEADER_LINE_P (w))
736 --height;
739 return height;
742 /* Return the number of columns of W's body. Don't count columns
743 occupied by the scroll bar or the vertical bar separating W from its
744 right sibling. On window-systems don't count fringes or display
745 margins either. */
748 window_body_cols (struct window *w)
750 struct frame *f = XFRAME (WINDOW_FRAME (w));
751 int width = XINT (w->total_cols);
753 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
754 /* Scroll bars occupy a few columns. */
755 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
756 else if (!FRAME_WINDOW_P (f)
757 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
758 /* The column of `|' characters separating side-by-side windows
759 occupies one column only. */
760 width -= 1;
762 if (FRAME_WINDOW_P (f))
763 /* On window-systems, fringes and display margins cannot be
764 used for normal text. */
765 width -= (WINDOW_FRINGE_COLS (w)
766 + WINDOW_LEFT_MARGIN_COLS (w)
767 + WINDOW_RIGHT_MARGIN_COLS (w));
769 return width;
772 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 1, 0,
773 doc: /* Return the height, in lines, of WINDOW's text area.
774 WINDOW must be a live window and defaults to the selected one.
776 The returned height does not include the mode line or header line.
777 On a graphical display, the height is expressed as an integer multiple
778 of the default character height. If a line at the bottom of the text
779 area is only partially visible, that counts as a whole line; to
780 exclude partially-visible lines, use `window-text-height'. */)
781 (Lisp_Object window)
783 struct window *w = decode_live_window (window);
784 return make_number (window_body_lines (w));
787 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 1, 0,
788 doc: /* Return the width, in columns, of WINDOW's text area.
789 WINDOW must be a live window and defaults to the selected one.
791 The return value does not include any vertical dividers, fringe or
792 marginal areas, or scroll bars. On a graphical display, the width is
793 expressed as an integer multiple of the default character width. */)
794 (Lisp_Object window)
796 struct window *w = decode_live_window (window);
797 return make_number (window_body_cols (w));
800 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
801 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
802 WINDOW must be a live window and defaults to the selected one. */)
803 (Lisp_Object window)
805 return make_number (decode_live_window (window)->hscroll);
808 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
809 range, returning the new amount as a fixnum. */
810 static Lisp_Object
811 set_window_hscroll (struct window *w, EMACS_INT hscroll)
813 /* Horizontal scrolling has problems with large scroll amounts.
814 It's too slow with long lines, and even with small lines the
815 display can be messed up. For now, though, impose only the limits
816 required by the internal representation: horizontal scrolling must
817 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
818 (since it's stored in a ptrdiff_t). */
819 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
820 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
822 /* Prevent redisplay shortcuts when changing the hscroll. */
823 if (w->hscroll != new_hscroll)
824 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
826 w->hscroll = new_hscroll;
827 return make_number (new_hscroll);
830 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
831 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
832 WINDOW must be a live window and defaults to the selected one.
833 Clip the number to a reasonable value if out of range.
834 Return the new number. NCOL should be zero or positive.
836 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
837 window so that the location of point moves off-window. */)
838 (Lisp_Object window, Lisp_Object ncol)
840 struct window *w = decode_live_window (window);
842 CHECK_NUMBER (ncol);
843 return set_window_hscroll (w, XINT (ncol));
846 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
847 Swindow_redisplay_end_trigger, 0, 1, 0,
848 doc: /* Return WINDOW's redisplay end trigger value.
849 WINDOW must be a live window and defaults to the selected one.
850 See `set-window-redisplay-end-trigger' for more information. */)
851 (Lisp_Object window)
853 return decode_live_window (window)->redisplay_end_trigger;
856 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
857 Sset_window_redisplay_end_trigger, 2, 2, 0,
858 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
859 WINDOW must be a live window and defaults to the selected one. VALUE
860 should be a buffer position (typically a marker) or nil. If it is a
861 buffer position, then if redisplay in WINDOW reaches a position beyond
862 VALUE, the functions in `redisplay-end-trigger-functions' are called
863 with two arguments: WINDOW, and the end trigger value. Afterwards the
864 end-trigger value is reset to nil. */)
865 (register Lisp_Object window, Lisp_Object value)
867 wset_redisplay_end_trigger (decode_live_window (window), value);
868 return value;
871 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
872 doc: /* Return a list of the edge coordinates of WINDOW.
873 WINDOW must be a valid window and defaults to the selected one.
875 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
876 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
877 0 at top left corner of frame.
879 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
880 is one more than the bottommost row occupied by WINDOW. The edges
881 include the space used by WINDOW's scroll bar, display margins, fringes,
882 header line, and/or mode line. For the edges of just the text area, use
883 `window-inside-edges'. */)
884 (Lisp_Object window)
886 register struct window *w = decode_valid_window (window);
888 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
889 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
890 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
891 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
892 Qnil))));
895 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
896 doc: /* Return a list of the edge pixel coordinates of WINDOW.
897 WINDOW must be a valid window and defaults to the selected one.
899 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
900 0, 0 at the top left corner of the frame.
902 RIGHT is one more than the rightmost x position occupied by WINDOW.
903 BOTTOM is one more than the bottommost y position occupied by WINDOW.
904 The pixel edges include the space used by WINDOW's scroll bar, display
905 margins, fringes, header line, and/or mode line. For the pixel edges
906 of just the text area, use `window-inside-pixel-edges'. */)
907 (Lisp_Object window)
909 register struct window *w = decode_valid_window (window);
911 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
912 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
913 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
914 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
915 Qnil))));
918 static void
919 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
921 struct frame *f = XFRAME (w->frame);
922 *add_y = f->top_pos;
923 #ifdef FRAME_MENUBAR_HEIGHT
924 *add_y += FRAME_MENUBAR_HEIGHT (f);
925 #endif
926 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
927 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
928 #elif FRAME_TOOLBAR_HEIGHT
929 *add_y += FRAME_TOOLBAR_HEIGHT (f);
930 #endif
931 #ifdef FRAME_NS_TITLEBAR_HEIGHT
932 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
933 #endif
934 *add_x = f->left_pos;
935 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
936 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
937 #endif
940 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
941 Swindow_absolute_pixel_edges, 0, 1, 0,
942 doc: /* Return a list of the edge pixel coordinates of WINDOW.
943 WINDOW must be a valid window and defaults to the selected one.
945 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
946 0, 0 at the top left corner of the display.
948 RIGHT is one more than the rightmost x position occupied by WINDOW.
949 BOTTOM is one more than the bottommost y position occupied by WINDOW.
950 The pixel edges include the space used by WINDOW's scroll bar, display
951 margins, fringes, header line, and/or mode line. For the pixel edges
952 of just the text area, use `window-inside-absolute-pixel-edges'. */)
953 (Lisp_Object window)
955 register struct window *w = decode_valid_window (window);
956 int add_x, add_y;
957 calc_absolute_offset (w, &add_x, &add_y);
959 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
960 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
961 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
962 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
963 Qnil))));
966 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
967 doc: /* Return a list of the edge coordinates of WINDOW.
968 WINDOW must be a live window and defaults to the selected one.
970 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
971 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
972 0 at top left corner of frame.
974 RIGHT is one more than the rightmost column of WINDOW's text area.
975 BOTTOM is one more than the bottommost row of WINDOW's text area. The
976 inside edges do not include the space used by the WINDOW's scroll bar,
977 display margins, fringes, header line, and/or mode line. */)
978 (Lisp_Object window)
980 register struct window *w = decode_live_window (window);
982 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
983 + WINDOW_LEFT_MARGIN_COLS (w)
984 + WINDOW_LEFT_FRINGE_COLS (w)),
985 make_number (WINDOW_TOP_EDGE_LINE (w)
986 + WINDOW_HEADER_LINE_LINES (w)),
987 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
988 - WINDOW_RIGHT_MARGIN_COLS (w)
989 - WINDOW_RIGHT_FRINGE_COLS (w)),
990 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
991 - WINDOW_MODE_LINE_LINES (w)));
994 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
995 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
996 WINDOW must be a live window and defaults to the selected one.
998 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
999 (0,0) at the top left corner of the frame's window area.
1001 RIGHT is one more than the rightmost x position of WINDOW's text area.
1002 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1003 The inside edges do not include the space used by WINDOW's scroll bar,
1004 display margins, fringes, header line, and/or mode line. */)
1005 (Lisp_Object window)
1007 register struct window *w = decode_live_window (window);
1009 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
1010 + WINDOW_LEFT_MARGIN_WIDTH (w)
1011 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1012 make_number (WINDOW_TOP_EDGE_Y (w)
1013 + WINDOW_HEADER_LINE_HEIGHT (w)),
1014 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
1015 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1016 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1017 make_number (WINDOW_BOTTOM_EDGE_Y (w)
1018 - WINDOW_MODE_LINE_HEIGHT (w)));
1021 DEFUN ("window-inside-absolute-pixel-edges",
1022 Fwindow_inside_absolute_pixel_edges,
1023 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1024 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1025 WINDOW must be a live window and defaults to the selected one.
1027 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1028 (0,0) at the top left corner of the frame's window area.
1030 RIGHT is one more than the rightmost x position of WINDOW's text area.
1031 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1032 The inside edges do not include the space used by WINDOW's scroll bar,
1033 display margins, fringes, header line, and/or mode line. */)
1034 (Lisp_Object window)
1036 register struct window *w = decode_live_window (window);
1037 int add_x, add_y;
1038 calc_absolute_offset (w, &add_x, &add_y);
1040 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
1041 + WINDOW_LEFT_MARGIN_WIDTH (w)
1042 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1043 make_number (WINDOW_TOP_EDGE_Y (w)
1044 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1045 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
1046 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1047 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1048 make_number (WINDOW_BOTTOM_EDGE_Y (w)
1049 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1052 /* Test if the character at column X, row Y is within window W.
1053 If it is not, return ON_NOTHING;
1054 if it is in the window's text area, return ON_TEXT;
1055 if it is on the window's modeline, return ON_MODE_LINE;
1056 if it is on the border between the window and its right sibling,
1057 return ON_VERTICAL_BORDER.
1058 if it is on a scroll bar, return ON_SCROLL_BAR.
1059 if it is on the window's top line, return ON_HEADER_LINE;
1060 if it is in left or right fringe of the window,
1061 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1062 if it is in the marginal area to the left/right of the window,
1063 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1065 X and Y are frame relative pixel coordinates. */
1067 static enum window_part
1068 coordinates_in_window (register struct window *w, int x, int y)
1070 struct frame *f = XFRAME (WINDOW_FRAME (w));
1071 enum window_part part;
1072 int ux = FRAME_COLUMN_WIDTH (f);
1073 int left_x = WINDOW_LEFT_EDGE_X (w);
1074 int right_x = WINDOW_RIGHT_EDGE_X (w);
1075 int top_y = WINDOW_TOP_EDGE_Y (w);
1076 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1077 /* The width of the area where the vertical line can be dragged.
1078 (Between mode lines for instance. */
1079 int grabbable_width = ux;
1080 int lmargin_width, rmargin_width, text_left, text_right;
1082 /* Outside any interesting row or column? */
1083 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1084 return ON_NOTHING;
1086 /* On the mode line or header line? */
1087 if ((WINDOW_WANTS_MODELINE_P (w)
1088 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
1089 && (part = ON_MODE_LINE))
1090 || (WINDOW_WANTS_HEADER_LINE_P (w)
1091 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1092 && (part = ON_HEADER_LINE)))
1094 /* If it's under/over the scroll bar portion of the mode/header
1095 line, say it's on the vertical line. That's to be able to
1096 resize windows horizontally in case we're using toolkit scroll
1097 bars. Note: If scrollbars are on the left, the window that
1098 must be eventually resized is that on the left of WINDOW. */
1099 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1100 && !WINDOW_LEFTMOST_P (w)
1101 && eabs (x - left_x) < grabbable_width)
1102 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1103 && !WINDOW_RIGHTMOST_P (w)
1104 && eabs (x - right_x) < grabbable_width))
1105 return ON_VERTICAL_BORDER;
1106 else
1107 return part;
1110 /* In what's below, we subtract 1 when computing right_x because we
1111 want the rightmost pixel, which is given by left_pixel+width-1. */
1112 if (w->pseudo_window_p)
1114 left_x = 0;
1115 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
1117 else
1119 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1120 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1123 /* Outside any interesting column? */
1124 if (x < left_x || x > right_x)
1125 return ON_SCROLL_BAR;
1127 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1128 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1130 text_left = window_box_left (w, TEXT_AREA);
1131 text_right = text_left + window_box_width (w, TEXT_AREA);
1133 if (FRAME_WINDOW_P (f))
1135 if (!w->pseudo_window_p
1136 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1137 && !WINDOW_RIGHTMOST_P (w)
1138 && (eabs (x - right_x) < grabbable_width))
1139 return ON_VERTICAL_BORDER;
1141 /* Need to say "x > right_x" rather than >=, since on character
1142 terminals, the vertical line's x coordinate is right_x. */
1143 else if (!w->pseudo_window_p
1144 && !WINDOW_RIGHTMOST_P (w)
1145 && x > right_x - ux)
1146 return ON_VERTICAL_BORDER;
1148 if (x < text_left)
1150 if (lmargin_width > 0
1151 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1152 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1153 : (x < left_x + lmargin_width)))
1154 return ON_LEFT_MARGIN;
1156 return ON_LEFT_FRINGE;
1159 if (x >= text_right)
1161 if (rmargin_width > 0
1162 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1163 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1164 : (x >= right_x - rmargin_width)))
1165 return ON_RIGHT_MARGIN;
1167 return ON_RIGHT_FRINGE;
1170 /* Everything special ruled out - must be on text area */
1171 return ON_TEXT;
1174 /* Take X is the frame-relative pixel x-coordinate, and return the
1175 x-coordinate relative to part PART of window W. */
1177 window_relative_x_coord (struct window *w, enum window_part part, int x)
1179 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1181 switch (part)
1183 case ON_TEXT:
1184 return x - window_box_left (w, TEXT_AREA);
1186 case ON_LEFT_FRINGE:
1187 return x - left_x;
1189 case ON_RIGHT_FRINGE:
1190 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1192 case ON_LEFT_MARGIN:
1193 return (x - left_x
1194 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1195 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1197 case ON_RIGHT_MARGIN:
1198 return (x + 1
1199 - ((w->pseudo_window_p)
1200 ? WINDOW_TOTAL_WIDTH (w)
1201 : WINDOW_BOX_RIGHT_EDGE_X (w))
1202 + window_box_width (w, RIGHT_MARGIN_AREA)
1203 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1204 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1207 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1208 return 0;
1212 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1213 Scoordinates_in_window_p, 2, 2, 0,
1214 doc: /* Return non-nil if COORDINATES are in WINDOW.
1215 WINDOW must be a live window and defaults to the selected one.
1216 COORDINATES is a cons of the form (X . Y), X and Y being distances
1217 measured in characters from the upper-left corner of the frame.
1218 \(0 . 0) denotes the character in the upper left corner of the
1219 frame.
1220 If COORDINATES are in the text portion of WINDOW,
1221 the coordinates relative to the window are returned.
1222 If they are in the mode line of WINDOW, `mode-line' is returned.
1223 If they are in the top mode line of WINDOW, `header-line' is returned.
1224 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1225 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1226 If they are on the border between WINDOW and its right sibling,
1227 `vertical-line' is returned.
1228 If they are in the windows's left or right marginal areas, `left-margin'\n\
1229 or `right-margin' is returned. */)
1230 (register Lisp_Object coordinates, Lisp_Object window)
1232 struct window *w;
1233 struct frame *f;
1234 int x, y;
1235 Lisp_Object lx, ly;
1237 w = decode_live_window (window);
1238 f = XFRAME (w->frame);
1239 CHECK_CONS (coordinates);
1240 lx = Fcar (coordinates);
1241 ly = Fcdr (coordinates);
1242 CHECK_NUMBER_OR_FLOAT (lx);
1243 CHECK_NUMBER_OR_FLOAT (ly);
1244 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1245 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1247 switch (coordinates_in_window (w, x, y))
1249 case ON_NOTHING:
1250 return Qnil;
1252 case ON_TEXT:
1253 /* Convert X and Y to window relative pixel coordinates, and
1254 return the canonical char units. */
1255 x -= window_box_left (w, TEXT_AREA);
1256 y -= WINDOW_TOP_EDGE_Y (w);
1257 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1258 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1260 case ON_MODE_LINE:
1261 return Qmode_line;
1263 case ON_VERTICAL_BORDER:
1264 return Qvertical_line;
1266 case ON_HEADER_LINE:
1267 return Qheader_line;
1269 case ON_LEFT_FRINGE:
1270 return Qleft_fringe;
1272 case ON_RIGHT_FRINGE:
1273 return Qright_fringe;
1275 case ON_LEFT_MARGIN:
1276 return Qleft_margin;
1278 case ON_RIGHT_MARGIN:
1279 return Qright_margin;
1281 case ON_SCROLL_BAR:
1282 /* Historically we are supposed to return nil in this case. */
1283 return Qnil;
1285 default:
1286 emacs_abort ();
1291 /* Callback for foreach_window, used in window_from_coordinates.
1292 Check if window W contains coordinates specified by USER_DATA which
1293 is actually a pointer to a struct check_window_data CW.
1295 Check if window W contains coordinates *CW->x and *CW->y. If it
1296 does, return W in *CW->window, as Lisp_Object, and return in
1297 *CW->part the part of the window under coordinates *X,*Y. Return
1298 zero from this function to stop iterating over windows. */
1300 struct check_window_data
1302 Lisp_Object *window;
1303 int x, y;
1304 enum window_part *part;
1307 static int
1308 check_window_containing (struct window *w, void *user_data)
1310 struct check_window_data *cw = (struct check_window_data *) user_data;
1311 enum window_part found;
1312 int continue_p = 1;
1314 found = coordinates_in_window (w, cw->x, cw->y);
1315 if (found != ON_NOTHING)
1317 *cw->part = found;
1318 XSETWINDOW (*cw->window, w);
1319 continue_p = 0;
1322 return continue_p;
1326 /* Find the window containing frame-relative pixel position X/Y and
1327 return it as a Lisp_Object.
1329 If X, Y is on one of the window's special `window_part' elements,
1330 set *PART to the id of that element.
1332 If there is no window under X, Y return nil and leave *PART
1333 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1335 This function was previously implemented with a loop cycling over
1336 windows with Fnext_window, and starting with the frame's selected
1337 window. It turned out that this doesn't work with an
1338 implementation of next_window using Vwindow_list, because
1339 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1340 tree of F when this function is called asynchronously from
1341 note_mouse_highlight. The original loop didn't terminate in this
1342 case. */
1344 Lisp_Object
1345 window_from_coordinates (struct frame *f, int x, int y,
1346 enum window_part *part, int tool_bar_p)
1348 Lisp_Object window;
1349 struct check_window_data cw;
1350 enum window_part dummy;
1352 if (part == 0)
1353 part = &dummy;
1355 window = Qnil;
1356 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1357 foreach_window (f, check_window_containing, &cw);
1359 /* If not found above, see if it's in the tool bar window, if a tool
1360 bar exists. */
1361 if (NILP (window)
1362 && tool_bar_p
1363 && WINDOWP (f->tool_bar_window)
1364 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1365 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1366 != ON_NOTHING))
1368 *part = ON_TEXT;
1369 window = f->tool_bar_window;
1372 return window;
1375 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1376 doc: /* Return window containing coordinates X and Y on FRAME.
1377 FRAME must be a live frame and defaults to the selected one.
1378 The top left corner of the frame is considered to be row 0,
1379 column 0. */)
1380 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1382 struct frame *f;
1384 if (NILP (frame))
1385 frame = selected_frame;
1386 CHECK_LIVE_FRAME (frame);
1387 f = XFRAME (frame);
1389 /* Check that arguments are integers or floats. */
1390 CHECK_NUMBER_OR_FLOAT (x);
1391 CHECK_NUMBER_OR_FLOAT (y);
1393 return window_from_coordinates (f,
1394 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1395 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1396 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1397 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1398 0, 0);
1401 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1402 doc: /* Return current value of point in WINDOW.
1403 WINDOW must be a live window and defaults to the selected one.
1405 For a nonselected window, this is the value point would have if that
1406 window were selected.
1408 Note that, when WINDOW is selected, the value returned is the same as
1409 that returned by `point' for WINDOW's buffer. It would be more strictly
1410 correct to return the `top-level' value of `point', outside of any
1411 `save-excursion' forms. But that is hard to define. */)
1412 (Lisp_Object window)
1414 register struct window *w = decode_live_window (window);
1416 if (w == XWINDOW (selected_window))
1417 return make_number (BUF_PT (XBUFFER (w->buffer)));
1418 else
1419 return Fmarker_position (w->pointm);
1422 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1423 doc: /* Return position at which display currently starts in WINDOW.
1424 WINDOW must be a live window and defaults to the selected one.
1425 This is updated by redisplay or by calling `set-window-start'. */)
1426 (Lisp_Object window)
1428 return Fmarker_position (decode_live_window (window)->start);
1431 /* This is text temporarily removed from the doc string below.
1433 This function returns nil if the position is not currently known.
1434 That happens when redisplay is preempted and doesn't finish.
1435 If in that case you want to compute where the end of the window would
1436 have been if redisplay had finished, do this:
1437 (save-excursion
1438 (goto-char (window-start window))
1439 (vertical-motion (1- (window-height window)) window)
1440 (point))") */
1442 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1443 doc: /* Return position at which display currently ends in WINDOW.
1444 WINDOW must be a live window and defaults to the selected one.
1445 This is updated by redisplay, when it runs to completion.
1446 Simply changing the buffer text or setting `window-start'
1447 does not update this value.
1448 Return nil if there is no recorded value. (This can happen if the
1449 last redisplay of WINDOW was preempted, and did not finish.)
1450 If UPDATE is non-nil, compute the up-to-date position
1451 if it isn't already recorded. */)
1452 (Lisp_Object window, Lisp_Object update)
1454 Lisp_Object value;
1455 struct window *w = decode_live_window (window);
1456 Lisp_Object buf;
1457 struct buffer *b;
1459 buf = w->buffer;
1460 CHECK_BUFFER (buf);
1461 b = XBUFFER (buf);
1463 #if 0 /* This change broke some things. We should make it later. */
1464 /* If we don't know the end position, return nil.
1465 The user can compute it with vertical-motion if he wants to.
1466 It would be nicer to do it automatically,
1467 but that's so slow that it would probably bother people. */
1468 if (NILP (w->window_end_valid))
1469 return Qnil;
1470 #endif
1472 if (! NILP (update)
1473 && (windows_or_buffers_changed || NILP (w->window_end_valid))
1474 && !noninteractive)
1476 struct text_pos startp;
1477 struct it it;
1478 struct buffer *old_buffer = NULL;
1479 void *itdata = NULL;
1481 /* Cannot use Fvertical_motion because that function doesn't
1482 cope with variable-height lines. */
1483 if (b != current_buffer)
1485 old_buffer = current_buffer;
1486 set_buffer_internal (b);
1489 /* In case W->start is out of the range, use something
1490 reasonable. This situation occurred when loading a file with
1491 `-l' containing a call to `rmail' with subsequent other
1492 commands. At the end, W->start happened to be BEG, while
1493 rmail had already narrowed the buffer. */
1494 if (XMARKER (w->start)->charpos < BEGV)
1495 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1496 else if (XMARKER (w->start)->charpos > ZV)
1497 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1498 else
1499 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1501 itdata = bidi_shelve_cache ();
1502 start_display (&it, w, startp);
1503 move_it_vertically (&it, window_box_height (w));
1504 if (it.current_y < it.last_visible_y)
1505 move_it_past_eol (&it);
1506 value = make_number (IT_CHARPOS (it));
1507 bidi_unshelve_cache (itdata, 0);
1509 if (old_buffer)
1510 set_buffer_internal (old_buffer);
1512 else
1513 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1515 return value;
1518 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1519 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1520 WINDOW must be a live window and defaults to the selected one.
1521 Return POS. */)
1522 (Lisp_Object window, Lisp_Object pos)
1524 register struct window *w = decode_live_window (window);
1526 CHECK_NUMBER_COERCE_MARKER (pos);
1528 if (w == XWINDOW (selected_window))
1530 if (XBUFFER (w->buffer) == current_buffer)
1531 Fgoto_char (pos);
1532 else
1534 struct buffer *old_buffer = current_buffer;
1536 set_buffer_internal (XBUFFER (w->buffer));
1537 Fgoto_char (pos);
1538 set_buffer_internal (old_buffer);
1541 else
1543 set_marker_restricted (w->pointm, pos, w->buffer);
1544 /* We have to make sure that redisplay updates the window to show
1545 the new value of point. */
1546 ++windows_or_buffers_changed;
1549 return pos;
1552 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1553 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1554 WINDOW must be a live window and defaults to the selected one. Return
1555 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1556 overriding motion of point in order to display at this exact start. */)
1557 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1559 register struct window *w = decode_live_window (window);
1561 CHECK_NUMBER_COERCE_MARKER (pos);
1562 set_marker_restricted (w->start, pos, w->buffer);
1563 /* this is not right, but much easier than doing what is right. */
1564 w->start_at_line_beg = 0;
1565 if (NILP (noforce))
1566 w->force_start = 1;
1567 w->update_mode_line = 1;
1568 w->last_modified = 0;
1569 w->last_overlay_modified = 0;
1570 if (!EQ (window, selected_window))
1571 windows_or_buffers_changed++;
1573 return pos;
1576 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1577 Spos_visible_in_window_p, 0, 3, 0,
1578 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1579 WINDOW must be a live window and defaults to the selected one.
1581 Return nil if that position is scrolled vertically out of view. If a
1582 character is only partially visible, nil is returned, unless the
1583 optional argument PARTIALLY is non-nil. If POS is only out of view
1584 because of horizontal scrolling, return non-nil. If POS is t, it
1585 specifies the position of the last visible glyph in WINDOW. POS
1586 defaults to point in WINDOW; WINDOW defaults to the selected window.
1588 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1589 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1590 where X and Y are the pixel coordinates relative to the top left corner
1591 of the window. The remaining elements are omitted if the character after
1592 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1593 off-window at the top and bottom of the row, ROWH is the height of the
1594 display row, and VPOS is the row number (0-based) containing POS. */)
1595 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1597 register struct window *w;
1598 register EMACS_INT posint;
1599 register struct buffer *buf;
1600 struct text_pos top;
1601 Lisp_Object in_window = Qnil;
1602 int rtop, rbot, rowh, vpos, fully_p = 1;
1603 int x, y;
1605 w = decode_live_window (window);
1606 buf = XBUFFER (w->buffer);
1607 SET_TEXT_POS_FROM_MARKER (top, w->start);
1609 if (EQ (pos, Qt))
1610 posint = -1;
1611 else if (!NILP (pos))
1613 CHECK_NUMBER_COERCE_MARKER (pos);
1614 posint = XINT (pos);
1616 else if (w == XWINDOW (selected_window))
1617 posint = PT;
1618 else
1619 posint = XMARKER (w->pointm)->charpos;
1621 /* If position is above window start or outside buffer boundaries,
1622 or if window start is out of range, position is not visible. */
1623 if ((EQ (pos, Qt)
1624 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1625 && CHARPOS (top) >= BUF_BEGV (buf)
1626 && CHARPOS (top) <= BUF_ZV (buf)
1627 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1628 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1629 in_window = Qt;
1631 if (!NILP (in_window) && !NILP (partially))
1633 Lisp_Object part = Qnil;
1634 if (!fully_p)
1635 part = list4 (make_number (rtop), make_number (rbot),
1636 make_number (rowh), make_number (vpos));
1637 in_window = Fcons (make_number (x),
1638 Fcons (make_number (y), part));
1641 return in_window;
1644 DEFUN ("window-line-height", Fwindow_line_height,
1645 Swindow_line_height, 0, 2, 0,
1646 doc: /* Return height in pixels of text line LINE in window WINDOW.
1647 WINDOW must be a live window and defaults to the selected one.
1649 Return height of current line if LINE is omitted or nil. Return height of
1650 header or mode line if LINE is `header-line' or `mode-line'.
1651 Otherwise, LINE is a text line number starting from 0. A negative number
1652 counts from the end of the window.
1654 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1655 in pixels of the visible part of the line, VPOS and YPOS are the
1656 vertical position in lines and pixels of the line, relative to the top
1657 of the first text line, and OFFBOT is the number of off-window pixels at
1658 the bottom of the text line. If there are off-window pixels at the top
1659 of the (first) text line, YPOS is negative.
1661 Return nil if window display is not up-to-date. In that case, use
1662 `pos-visible-in-window-p' to obtain the information. */)
1663 (Lisp_Object line, Lisp_Object window)
1665 register struct window *w;
1666 register struct buffer *b;
1667 struct glyph_row *row, *end_row;
1668 int max_y, crop, i;
1669 EMACS_INT n;
1671 w = decode_live_window (window);
1673 if (noninteractive || w->pseudo_window_p)
1674 return Qnil;
1676 CHECK_BUFFER (w->buffer);
1677 b = XBUFFER (w->buffer);
1679 /* Fail if current matrix is not up-to-date. */
1680 if (NILP (w->window_end_valid)
1681 || current_buffer->clip_changed
1682 || current_buffer->prevent_redisplay_optimizations_p
1683 || w->last_modified < BUF_MODIFF (b)
1684 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b))
1685 return Qnil;
1687 if (NILP (line))
1689 i = w->cursor.vpos;
1690 if (i < 0 || i >= w->current_matrix->nrows
1691 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1692 return Qnil;
1693 max_y = window_text_bottom_y (w);
1694 goto found_row;
1697 if (EQ (line, Qheader_line))
1699 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1700 return Qnil;
1701 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1702 if (!row->enabled_p)
1703 return Qnil;
1704 return list4 (make_number (row->height),
1705 make_number (0), make_number (0),
1706 make_number (0));
1709 if (EQ (line, Qmode_line))
1711 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1712 if (!row->enabled_p)
1713 return Qnil;
1714 return list4 (make_number (row->height),
1715 make_number (0), /* not accurate */
1716 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
1717 + window_text_bottom_y (w)),
1718 make_number (0));
1721 CHECK_NUMBER (line);
1722 n = XINT (line);
1724 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1725 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1726 max_y = window_text_bottom_y (w);
1727 i = 0;
1729 while ((n < 0 || i < n)
1730 && row <= end_row && row->enabled_p
1731 && row->y + row->height < max_y)
1732 row++, i++;
1734 if (row > end_row || !row->enabled_p)
1735 return Qnil;
1737 if (++n < 0)
1739 if (-n > i)
1740 return Qnil;
1741 row += n;
1742 i += n;
1745 found_row:
1746 crop = max (0, (row->y + row->height) - max_y);
1747 return list4 (make_number (row->height + min (0, row->y) - crop),
1748 make_number (i),
1749 make_number (row->y),
1750 make_number (crop));
1753 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1754 0, 1, 0,
1755 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1756 More precisely, return the value assigned by the last call of
1757 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1758 never called with WINDOW as its argument, or the value set by that
1759 function was internally reset since its last call. WINDOW must be a
1760 live window and defaults to the selected one.
1762 When a window is dedicated to its buffer, `display-buffer' will refrain
1763 from displaying another buffer in it. `get-lru-window' and
1764 `get-largest-window' treat dedicated windows specially.
1765 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1766 `kill-buffer' can delete a dedicated window and the containing frame.
1768 Functions like `set-window-buffer' may change the buffer displayed by a
1769 window, unless that window is "strongly" dedicated to its buffer, that
1770 is the value returned by `window-dedicated-p' is t. */)
1771 (Lisp_Object window)
1773 return decode_live_window (window)->dedicated;
1776 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1777 Sset_window_dedicated_p, 2, 2, 0,
1778 doc: /* Mark WINDOW as dedicated according to FLAG.
1779 WINDOW must be a live window and defaults to the selected one. FLAG
1780 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1781 mark WINDOW as non-dedicated. Return FLAG.
1783 When a window is dedicated to its buffer, `display-buffer' will refrain
1784 from displaying another buffer in it. `get-lru-window' and
1785 `get-largest-window' treat dedicated windows specially.
1786 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1787 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1788 and the containing frame.
1790 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1791 its buffer. Functions like `set-window-buffer' may change the buffer
1792 displayed by a window, unless that window is strongly dedicated to its
1793 buffer. If and when `set-window-buffer' displays another buffer in a
1794 window, it also makes sure that the window is no more dedicated. */)
1795 (Lisp_Object window, Lisp_Object flag)
1797 wset_dedicated (decode_live_window (window), flag);
1798 return flag;
1801 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1802 0, 1, 0,
1803 doc: /* Return buffers previously shown in WINDOW.
1804 WINDOW must be a live window and defaults to the selected one.
1806 The return value is a list of elements (BUFFER WINDOW-START POS),
1807 where BUFFER is a buffer, WINDOW-START is the start position of the
1808 window for that buffer, and POS is a window-specific point value. */)
1809 (Lisp_Object window)
1811 return decode_live_window (window)->prev_buffers;
1814 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1815 Sset_window_prev_buffers, 2, 2, 0,
1816 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1817 WINDOW must be a live window and defaults to the selected one.
1819 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1820 where BUFFER is a buffer, WINDOW-START is the start position of the
1821 window for that buffer, and POS is a window-specific point value. */)
1822 (Lisp_Object window, Lisp_Object prev_buffers)
1824 wset_prev_buffers (decode_live_window (window), prev_buffers);
1825 return prev_buffers;
1828 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1829 0, 1, 0,
1830 doc: /* Return list of buffers recently re-shown in WINDOW.
1831 WINDOW must be a live window and defaults to the selected one. */)
1832 (Lisp_Object window)
1834 return decode_live_window (window)->next_buffers;
1837 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1838 Sset_window_next_buffers, 2, 2, 0,
1839 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1840 WINDOW must be a live window and defaults to the selected one.
1841 NEXT-BUFFERS should be a list of buffers. */)
1842 (Lisp_Object window, Lisp_Object next_buffers)
1844 wset_next_buffers (decode_live_window (window), next_buffers);
1845 return next_buffers;
1848 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1849 0, 1, 0,
1850 doc: /* Return the parameters of WINDOW and their values.
1851 WINDOW must be a valid window and defaults to the selected one. The
1852 return value is a list of elements of the form (PARAMETER . VALUE). */)
1853 (Lisp_Object window)
1855 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1858 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1859 2, 2, 0,
1860 doc: /* Return WINDOW's value for PARAMETER.
1861 WINDOW can be any window and defaults to the selected one. */)
1862 (Lisp_Object window, Lisp_Object parameter)
1864 Lisp_Object result;
1866 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1867 return CDR_SAFE (result);
1870 DEFUN ("set-window-parameter", Fset_window_parameter,
1871 Sset_window_parameter, 3, 3, 0,
1872 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1873 WINDOW can be any window and defaults to the selected one.
1874 Return VALUE. */)
1875 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1877 register struct window *w = decode_any_window (window);
1878 Lisp_Object old_alist_elt;
1880 old_alist_elt = Fassq (parameter, w->window_parameters);
1881 if (NILP (old_alist_elt))
1882 wset_window_parameters
1883 (w, Fcons (Fcons (parameter, value), w->window_parameters));
1884 else
1885 Fsetcdr (old_alist_elt, value);
1886 return value;
1889 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1890 0, 1, 0,
1891 doc: /* Return the display-table that WINDOW is using.
1892 WINDOW must be a live window and defaults to the selected one. */)
1893 (Lisp_Object window)
1895 return decode_live_window (window)->display_table;
1898 /* Get the display table for use on window W. This is either W's
1899 display table or W's buffer's display table. Ignore the specified
1900 tables if they are not valid; if no valid table is specified,
1901 return 0. */
1903 struct Lisp_Char_Table *
1904 window_display_table (struct window *w)
1906 struct Lisp_Char_Table *dp = NULL;
1908 if (DISP_TABLE_P (w->display_table))
1909 dp = XCHAR_TABLE (w->display_table);
1910 else if (BUFFERP (w->buffer))
1912 struct buffer *b = XBUFFER (w->buffer);
1914 if (DISP_TABLE_P (BVAR (b, display_table)))
1915 dp = XCHAR_TABLE (BVAR (b, display_table));
1916 else if (DISP_TABLE_P (Vstandard_display_table))
1917 dp = XCHAR_TABLE (Vstandard_display_table);
1920 return dp;
1923 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1924 doc: /* Set WINDOW's display-table to TABLE.
1925 WINDOW must be a live window and defaults to the selected one. */)
1926 (register Lisp_Object window, Lisp_Object table)
1928 wset_display_table (decode_live_window (window), table);
1929 return table;
1932 /* Record info on buffer window W is displaying
1933 when it is about to cease to display that buffer. */
1934 static void
1935 unshow_buffer (register struct window *w)
1937 Lisp_Object buf;
1938 struct buffer *b;
1940 buf = w->buffer;
1941 b = XBUFFER (buf);
1942 if (b != XMARKER (w->pointm)->buffer)
1943 emacs_abort ();
1945 #if 0
1946 if (w == XWINDOW (selected_window)
1947 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1948 /* Do this except when the selected window's buffer
1949 is being removed from some other window. */
1950 #endif
1951 /* last_window_start records the start position that this buffer
1952 had in the last window to be disconnected from it.
1953 Now that this statement is unconditional,
1954 it is possible for the buffer to be displayed in the
1955 selected window, while last_window_start reflects another
1956 window which was recently showing the same buffer.
1957 Some people might say that might be a good thing. Let's see. */
1958 b->last_window_start = marker_position (w->start);
1960 /* Point in the selected window's buffer
1961 is actually stored in that buffer, and the window's pointm isn't used.
1962 So don't clobber point in that buffer. */
1963 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1964 /* Don't clobber point in current buffer either (this could be
1965 useful in connection with bug#12208).
1966 && XBUFFER (buf) != current_buffer */
1967 /* This line helps to fix Horsley's testbug.el bug. */
1968 && !(WINDOWP (BVAR (b, last_selected_window))
1969 && w != XWINDOW (BVAR (b, last_selected_window))
1970 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1971 temp_set_point_both (b,
1972 clip_to_bounds (BUF_BEGV (b),
1973 XMARKER (w->pointm)->charpos,
1974 BUF_ZV (b)),
1975 clip_to_bounds (BUF_BEGV_BYTE (b),
1976 marker_byte_position (w->pointm),
1977 BUF_ZV_BYTE (b)));
1979 if (WINDOWP (BVAR (b, last_selected_window))
1980 && w == XWINDOW (BVAR (b, last_selected_window)))
1981 bset_last_selected_window (b, Qnil);
1984 /* Put NEW into the window structure in place of OLD. SETFLAG zero
1985 means change window structure only. Otherwise store geometry and
1986 other settings as well. */
1987 static void
1988 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
1990 register Lisp_Object tem;
1991 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
1993 /* If OLD is its frame's root window, then NEW is the new
1994 root window for that frame. */
1995 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1996 fset_root_window (XFRAME (o->frame), new);
1998 if (setflag)
2000 wset_left_col (n, o->left_col);
2001 wset_top_line (n, o->top_line);
2002 wset_total_cols (n, o->total_cols);
2003 wset_total_lines (n, o->total_lines);
2004 wset_normal_cols (n, o->normal_cols);
2005 wset_normal_cols (o, make_float (1.0));
2006 wset_normal_lines (n, o->normal_lines);
2007 wset_normal_lines (o, make_float (1.0));
2008 n->desired_matrix = n->current_matrix = 0;
2009 n->vscroll = 0;
2010 memset (&n->cursor, 0, sizeof (n->cursor));
2011 memset (&n->last_cursor, 0, sizeof (n->last_cursor));
2012 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2013 n->phys_cursor_type = -1;
2014 n->phys_cursor_width = -1;
2015 n->must_be_updated_p = 0;
2016 n->pseudo_window_p = 0;
2017 wset_window_end_vpos (n, make_number (0));
2018 wset_window_end_pos (n, make_number (0));
2019 wset_window_end_valid (n, Qnil);
2020 n->frozen_window_start_p = 0;
2023 tem = o->next;
2024 wset_next (n, tem);
2025 if (!NILP (tem))
2026 wset_prev (XWINDOW (tem), new);
2028 tem = o->prev;
2029 wset_prev (n, tem);
2030 if (!NILP (tem))
2031 wset_next (XWINDOW (tem), new);
2033 tem = o->parent;
2034 wset_parent (n, tem);
2035 if (!NILP (tem))
2037 if (EQ (XWINDOW (tem)->vchild, old))
2038 wset_vchild (XWINDOW (tem), new);
2039 if (EQ (XWINDOW (tem)->hchild, old))
2040 wset_hchild (XWINDOW (tem), new);
2044 /* If window WINDOW and its parent window are iso-combined, merge
2045 WINDOW's children into those of its parent window and mark WINDOW as
2046 deleted. */
2048 static void
2049 recombine_windows (Lisp_Object window)
2051 struct window *w, *p, *c;
2052 Lisp_Object parent, child;
2053 int horflag;
2055 w = XWINDOW (window);
2056 parent = w->parent;
2057 if (!NILP (parent) && NILP (w->combination_limit))
2059 p = XWINDOW (parent);
2060 if (((!NILP (p->vchild) && !NILP (w->vchild))
2061 || (!NILP (p->hchild) && !NILP (w->hchild))))
2062 /* WINDOW and PARENT are both either a vertical or a horizontal
2063 combination. */
2065 horflag = NILP (w->vchild);
2066 child = horflag ? w->hchild : w->vchild;
2067 c = XWINDOW (child);
2069 /* Splice WINDOW's children into its parent's children and
2070 assign new normal sizes. */
2071 if (NILP (w->prev))
2072 if (horflag)
2073 wset_hchild (p, child);
2074 else
2075 wset_vchild (p, child);
2076 else
2078 wset_prev (c, w->prev);
2079 wset_next (XWINDOW (w->prev), child);
2082 while (c)
2084 wset_parent (c, parent);
2086 if (horflag)
2087 wset_normal_cols (c,
2088 make_float (XFLOATINT (c->total_cols)
2089 / XFLOATINT (p->total_cols)));
2090 else
2091 wset_normal_lines (c,
2092 make_float (XFLOATINT (c->total_lines)
2093 / XFLOATINT (p->total_lines)));
2095 if (NILP (c->next))
2097 if (!NILP (w->next))
2099 wset_next (c, w->next);
2100 wset_prev (XWINDOW (c->next), child);
2103 c = 0;
2105 else
2107 child = c->next;
2108 c = XWINDOW (child);
2112 /* WINDOW can be deleted now. */
2113 wset_vchild (w, Qnil);
2114 wset_hchild (w, Qnil);
2119 /* If WINDOW can be deleted, delete it. */
2120 static void
2121 delete_deletable_window (Lisp_Object window)
2123 if (!NILP (call1 (Qwindow_deletable_p, window)))
2124 call1 (Qdelete_window, window);
2127 /***********************************************************************
2128 Window List
2129 ***********************************************************************/
2131 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2132 pointer. This is a callback function for foreach_window, used in
2133 the window_list function. */
2135 static int
2136 add_window_to_list (struct window *w, void *user_data)
2138 Lisp_Object *list = (Lisp_Object *) user_data;
2139 Lisp_Object window;
2140 XSETWINDOW (window, w);
2141 *list = Fcons (window, *list);
2142 return 1;
2146 /* Return a list of all windows, for use by next_window. If
2147 Vwindow_list is a list, return that list. Otherwise, build a new
2148 list, cache it in Vwindow_list, and return that. */
2150 static Lisp_Object
2151 window_list (void)
2153 if (!CONSP (Vwindow_list))
2155 Lisp_Object tail;
2157 Vwindow_list = Qnil;
2158 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
2160 Lisp_Object args[2];
2162 /* We are visiting windows in canonical order, and add
2163 new windows at the front of args[1], which means we
2164 have to reverse this list at the end. */
2165 args[1] = Qnil;
2166 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
2167 args[0] = Vwindow_list;
2168 args[1] = Fnreverse (args[1]);
2169 Vwindow_list = Fnconc (2, args);
2173 return Vwindow_list;
2177 /* Value is non-zero if WINDOW satisfies the constraints given by
2178 OWINDOW, MINIBUF and ALL_FRAMES.
2180 MINIBUF t means WINDOW may be minibuffer windows.
2181 `lambda' means WINDOW may not be a minibuffer window.
2182 a window means a specific minibuffer window
2184 ALL_FRAMES t means search all frames,
2185 nil means search just current frame,
2186 `visible' means search just visible frames on the
2187 current terminal,
2188 0 means search visible and iconified frames on the
2189 current terminal,
2190 a window means search the frame that window belongs to,
2191 a frame means consider windows on that frame, only. */
2193 static int
2194 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
2196 struct window *w = XWINDOW (window);
2197 struct frame *f = XFRAME (w->frame);
2198 int candidate_p = 1;
2200 if (!BUFFERP (w->buffer))
2201 candidate_p = 0;
2202 else if (MINI_WINDOW_P (w)
2203 && (EQ (minibuf, Qlambda)
2204 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2206 /* If MINIBUF is `lambda' don't consider any mini-windows.
2207 If it is a window, consider only that one. */
2208 candidate_p = 0;
2210 else if (EQ (all_frames, Qt))
2211 candidate_p = 1;
2212 else if (NILP (all_frames))
2214 eassert (WINDOWP (owindow));
2215 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2217 else if (EQ (all_frames, Qvisible))
2219 FRAME_SAMPLE_VISIBILITY (f);
2220 candidate_p = FRAME_VISIBLE_P (f)
2221 && (FRAME_TERMINAL (XFRAME (w->frame))
2222 == FRAME_TERMINAL (XFRAME (selected_frame)));
2225 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2227 FRAME_SAMPLE_VISIBILITY (f);
2228 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2229 #ifdef HAVE_X_WINDOWS
2230 /* Yuck!! If we've just created the frame and the
2231 window-manager requested the user to place it
2232 manually, the window may still not be considered
2233 `visible'. I'd argue it should be at least
2234 something like `iconified', but don't know how to do
2235 that yet. --Stef */
2236 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2237 && !f->output_data.x->has_been_visible)
2238 #endif
2240 && (FRAME_TERMINAL (XFRAME (w->frame))
2241 == FRAME_TERMINAL (XFRAME (selected_frame)));
2243 else if (WINDOWP (all_frames))
2244 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2245 || EQ (XWINDOW (all_frames)->frame, w->frame)
2246 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2247 else if (FRAMEP (all_frames))
2248 candidate_p = EQ (all_frames, w->frame);
2250 return candidate_p;
2254 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2255 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2256 MINIBUF, and ALL_FRAMES. */
2258 static void
2259 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2261 if (NILP (*window))
2262 *window = selected_window;
2263 else
2264 CHECK_LIVE_WINDOW (*window);
2266 /* MINIBUF nil may or may not include minibuffers. Decide if it
2267 does. */
2268 if (NILP (*minibuf))
2269 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2270 else if (!EQ (*minibuf, Qt))
2271 *minibuf = Qlambda;
2273 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2274 => count none of them, or a specific minibuffer window (the
2275 active one) to count. */
2277 /* ALL_FRAMES nil doesn't specify which frames to include. */
2278 if (NILP (*all_frames))
2279 *all_frames
2280 = (!EQ (*minibuf, Qlambda)
2281 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
2282 : Qnil);
2283 else if (EQ (*all_frames, Qvisible))
2285 else if (EQ (*all_frames, make_number (0)))
2287 else if (FRAMEP (*all_frames))
2289 else if (!EQ (*all_frames, Qt))
2290 *all_frames = Qnil;
2294 /* Return the next or previous window of WINDOW in cyclic ordering
2295 of windows. NEXT_P non-zero means return the next window. See the
2296 documentation string of next-window for the meaning of MINIBUF and
2297 ALL_FRAMES. */
2299 static Lisp_Object
2300 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2302 decode_next_window_args (&window, &minibuf, &all_frames);
2304 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2305 return the first window on the frame. */
2306 if (FRAMEP (all_frames)
2307 && !EQ (all_frames, XWINDOW (window)->frame))
2308 return Fframe_first_window (all_frames);
2310 if (next_p)
2312 Lisp_Object list;
2314 /* Find WINDOW in the list of all windows. */
2315 list = Fmemq (window, window_list ());
2317 /* Scan forward from WINDOW to the end of the window list. */
2318 if (CONSP (list))
2319 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2320 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2321 break;
2323 /* Scan from the start of the window list up to WINDOW. */
2324 if (!CONSP (list))
2325 for (list = Vwindow_list;
2326 CONSP (list) && !EQ (XCAR (list), window);
2327 list = XCDR (list))
2328 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2329 break;
2331 if (CONSP (list))
2332 window = XCAR (list);
2334 else
2336 Lisp_Object candidate, list;
2338 /* Scan through the list of windows for candidates. If there are
2339 candidate windows in front of WINDOW, the last one of these
2340 is the one we want. If there are candidates following WINDOW
2341 in the list, again the last one of these is the one we want. */
2342 candidate = Qnil;
2343 for (list = window_list (); CONSP (list); list = XCDR (list))
2345 if (EQ (XCAR (list), window))
2347 if (WINDOWP (candidate))
2348 break;
2350 else if (candidate_window_p (XCAR (list), window, minibuf,
2351 all_frames))
2352 candidate = XCAR (list);
2355 if (WINDOWP (candidate))
2356 window = candidate;
2359 return window;
2363 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2364 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2365 WINDOW must be a live window and defaults to the selected one. The
2366 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2367 consider.
2369 MINIBUF nil or omitted means consider the minibuffer window only if the
2370 minibuffer is active. MINIBUF t means consider the minibuffer window
2371 even if the minibuffer is not active. Any other value means do not
2372 consider the minibuffer window even if the minibuffer is active.
2374 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2375 plus the minibuffer window if specified by the MINIBUF argument. If the
2376 minibuffer counts, consider all windows on all frames that share that
2377 minibuffer too. The following non-nil values of ALL-FRAMES have special
2378 meanings:
2380 - t means consider all windows on all existing frames.
2382 - `visible' means consider all windows on all visible frames.
2384 - 0 (the number zero) means consider all windows on all visible and
2385 iconified frames.
2387 - A frame means consider all windows on that frame only.
2389 Anything else means consider all windows on WINDOW's frame and no
2390 others.
2392 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2393 `next-window' to iterate through the entire cycle of acceptable
2394 windows, eventually ending up back at the window you started with.
2395 `previous-window' traverses the same cycle, in the reverse order. */)
2396 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2398 return next_window (window, minibuf, all_frames, 1);
2402 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2403 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2404 WINDOW must be a live window and defaults to the selected one. The
2405 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2406 consider.
2408 MINIBUF nil or omitted means consider the minibuffer window only if the
2409 minibuffer is active. MINIBUF t means consider the minibuffer window
2410 even if the minibuffer is not active. Any other value means do not
2411 consider the minibuffer window even if the minibuffer is active.
2413 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2414 plus the minibuffer window if specified by the MINIBUF argument. If the
2415 minibuffer counts, consider all windows on all frames that share that
2416 minibuffer too. The following non-nil values of ALL-FRAMES have special
2417 meanings:
2419 - t means consider all windows on all existing frames.
2421 - `visible' means consider all windows on all visible frames.
2423 - 0 (the number zero) means consider all windows on all visible and
2424 iconified frames.
2426 - A frame means consider all windows on that frame only.
2428 Anything else means consider all windows on WINDOW's frame and no
2429 others.
2431 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2432 use `previous-window' to iterate through the entire cycle of
2433 acceptable windows, eventually ending up back at the window you
2434 started with. `next-window' traverses the same cycle, in the
2435 reverse order. */)
2436 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2438 return next_window (window, minibuf, all_frames, 0);
2442 /* Return a list of windows in cyclic ordering. Arguments are like
2443 for `next-window'. */
2445 static Lisp_Object
2446 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2448 Lisp_Object tail, list, rest;
2450 decode_next_window_args (&window, &minibuf, &all_frames);
2451 list = Qnil;
2453 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2454 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2455 list = Fcons (XCAR (tail), list);
2457 /* Rotate the list to start with WINDOW. */
2458 list = Fnreverse (list);
2459 rest = Fmemq (window, list);
2460 if (!NILP (rest) && !EQ (rest, list))
2462 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2464 XSETCDR (tail, Qnil);
2465 list = nconc2 (rest, list);
2467 return list;
2471 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2472 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2473 FRAME nil or omitted means use the selected frame.
2474 WINDOW nil or omitted means use the window selected within FRAME.
2475 MINIBUF t means include the minibuffer window, even if it isn't active.
2476 MINIBUF nil or omitted means include the minibuffer window only
2477 if it's active.
2478 MINIBUF neither nil nor t means never include the minibuffer window. */)
2479 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2481 if (NILP (window))
2482 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2483 CHECK_WINDOW (window);
2484 if (NILP (frame))
2485 frame = selected_frame;
2487 if (!EQ (frame, XWINDOW (window)->frame))
2488 error ("Window is on a different frame");
2490 return window_list_1 (window, minibuf, frame);
2494 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2495 doc: /* Return a list of all live windows.
2496 WINDOW specifies the first window to list and defaults to the selected
2497 window.
2499 Optional argument MINIBUF nil or omitted means consider the minibuffer
2500 window only if the minibuffer is active. MINIBUF t means consider the
2501 minibuffer window even if the minibuffer is not active. Any other value
2502 means do not consider the minibuffer window even if the minibuffer is
2503 active.
2505 Optional argument ALL-FRAMES nil or omitted means consider all windows
2506 on WINDOW's frame, plus the minibuffer window if specified by the
2507 MINIBUF argument. If the minibuffer counts, consider all windows on all
2508 frames that share that minibuffer too. The following non-nil values of
2509 ALL-FRAMES have special meanings:
2511 - t means consider all windows on all existing frames.
2513 - `visible' means consider all windows on all visible frames.
2515 - 0 (the number zero) means consider all windows on all visible and
2516 iconified frames.
2518 - A frame means consider all windows on that frame only.
2520 Anything else means consider all windows on WINDOW's frame and no
2521 others.
2523 If WINDOW is not on the list of windows returned, some other window will
2524 be listed first but no error is signaled. */)
2525 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2527 return window_list_1 (window, minibuf, all_frames);
2530 /* Look at all windows, performing an operation specified by TYPE
2531 with argument OBJ.
2532 If FRAMES is Qt, look at all frames;
2533 Qnil, look at just the selected frame;
2534 Qvisible, look at visible frames;
2535 a frame, just look at windows on that frame.
2536 If MINI is non-zero, perform the operation on minibuffer windows too. */
2538 enum window_loop
2540 WINDOW_LOOP_UNUSED,
2541 GET_BUFFER_WINDOW, /* Arg is buffer */
2542 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2543 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2544 CHECK_ALL_WINDOWS
2547 static Lisp_Object
2548 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2550 Lisp_Object window, windows, best_window, frame_arg;
2551 int frame_best_window_flag = 0;
2552 struct frame *f;
2553 struct gcpro gcpro1;
2555 /* If we're only looping through windows on a particular frame,
2556 frame points to that frame. If we're looping through windows
2557 on all frames, frame is 0. */
2558 if (FRAMEP (frames))
2559 f = XFRAME (frames);
2560 else if (NILP (frames))
2561 f = SELECTED_FRAME ();
2562 else
2563 f = NULL;
2565 if (f)
2566 frame_arg = Qlambda;
2567 else if (EQ (frames, make_number (0)))
2568 frame_arg = frames;
2569 else if (EQ (frames, Qvisible))
2570 frame_arg = frames;
2571 else
2572 frame_arg = Qt;
2574 /* frame_arg is Qlambda to stick to one frame,
2575 Qvisible to consider all visible frames,
2576 or Qt otherwise. */
2578 /* Pick a window to start with. */
2579 if (WINDOWP (obj))
2580 window = obj;
2581 else if (f)
2582 window = FRAME_SELECTED_WINDOW (f);
2583 else
2584 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2586 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2587 GCPRO1 (windows);
2588 best_window = Qnil;
2590 for (; CONSP (windows); windows = XCDR (windows))
2592 struct window *w;
2594 window = XCAR (windows);
2595 w = XWINDOW (window);
2597 /* Note that we do not pay attention here to whether the frame
2598 is visible, since Fwindow_list skips non-visible frames if
2599 that is desired, under the control of frame_arg. */
2600 if (!MINI_WINDOW_P (w)
2601 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2602 consider all windows. */
2603 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2604 || (mini && minibuf_level > 0))
2605 switch (type)
2607 case GET_BUFFER_WINDOW:
2608 if (EQ (w->buffer, obj)
2609 /* Don't find any minibuffer window except the one that
2610 is currently in use. */
2611 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2613 if (EQ (window, selected_window))
2614 /* Preferably return the selected window. */
2615 RETURN_UNGCPRO (window);
2616 else if (EQ (XWINDOW (window)->frame, selected_frame)
2617 && !frame_best_window_flag)
2618 /* Prefer windows on the current frame (but don't
2619 choose another one if we have one already). */
2621 best_window = window;
2622 frame_best_window_flag = 1;
2624 else if (NILP (best_window))
2625 best_window = window;
2627 break;
2629 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2630 /* We could simply check whether the buffer shown by window
2631 is live, and show another buffer in case it isn't. */
2632 if (EQ (w->buffer, obj))
2634 /* Undedicate WINDOW. */
2635 wset_dedicated (w, Qnil);
2636 /* Make WINDOW show the buffer returned by
2637 other_buffer_safely, don't run any hooks. */
2638 set_window_buffer
2639 (window, other_buffer_safely (w->buffer), 0, 0);
2640 /* If WINDOW is the selected window, make its buffer
2641 current. But do so only if the window shows the
2642 current buffer (Bug#6454). */
2643 if (EQ (window, selected_window)
2644 && XBUFFER (w->buffer) == current_buffer)
2645 Fset_buffer (w->buffer);
2647 break;
2649 case REDISPLAY_BUFFER_WINDOWS:
2650 if (EQ (w->buffer, obj))
2652 mark_window_display_accurate (window, 0);
2653 w->update_mode_line = 1;
2654 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2655 ++update_mode_lines;
2656 best_window = window;
2658 break;
2660 /* Check for a window that has a killed buffer. */
2661 case CHECK_ALL_WINDOWS:
2662 if (! NILP (w->buffer)
2663 && !BUFFER_LIVE_P (XBUFFER (w->buffer)))
2664 emacs_abort ();
2665 break;
2667 case WINDOW_LOOP_UNUSED:
2668 break;
2672 UNGCPRO;
2673 return best_window;
2676 /* Used for debugging. Abort if any window has a dead buffer. */
2678 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2679 void
2680 check_all_windows (void)
2682 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2685 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2686 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2687 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2688 the current buffer.
2690 The optional argument ALL-FRAMES specifies the frames to consider:
2692 - t means consider all windows on all existing frames.
2694 - `visible' means consider all windows on all visible frames.
2696 - 0 (the number zero) means consider all windows on all visible
2697 and iconified frames.
2699 - A frame means consider all windows on that frame only.
2701 Any other value of ALL-FRAMES means consider all windows on the
2702 selected frame and no others. */)
2703 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2705 Lisp_Object buffer;
2707 if (NILP (buffer_or_name))
2708 buffer = Fcurrent_buffer ();
2709 else
2710 buffer = Fget_buffer (buffer_or_name);
2712 if (BUFFERP (buffer))
2713 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2714 else
2715 return Qnil;
2718 static Lisp_Object
2719 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2721 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2725 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2726 Sdelete_other_windows_internal, 0, 2, "",
2727 doc: /* Make WINDOW fill its frame.
2728 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2729 and defaults to the selected one.
2731 Optional argument ROOT, if non-nil, must specify an internal window such
2732 that WINDOW is in its window subtree. If this is the case, replace ROOT
2733 by WINDOW and leave alone any windows not part of ROOT's subtree.
2735 When WINDOW is live try to reduce display jumps by keeping the text
2736 previously visible in WINDOW in the same place on the frame. Doing this
2737 depends on the value of (window-start WINDOW), so if calling this
2738 function in a program gives strange scrolling, make sure the
2739 window-start value is reasonable when this function is called. */)
2740 (Lisp_Object window, Lisp_Object root)
2742 struct window *w, *r, *s;
2743 struct frame *f;
2744 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2745 ptrdiff_t startpos IF_LINT (= 0);
2746 int top IF_LINT (= 0), new_top, resize_failed;
2748 w = decode_valid_window (window);
2749 XSETWINDOW (window, w);
2750 f = XFRAME (w->frame);
2752 if (NILP (root))
2753 /* ROOT is the frame's root window. */
2755 root = FRAME_ROOT_WINDOW (f);
2756 r = XWINDOW (root);
2758 else
2759 /* ROOT must be an ancestor of WINDOW. */
2761 r = decode_valid_window (root);
2762 pwindow = XWINDOW (window)->parent;
2763 while (!NILP (pwindow))
2764 if (EQ (pwindow, root))
2765 break;
2766 else
2767 pwindow = XWINDOW (pwindow)->parent;
2768 if (!EQ (pwindow, root))
2769 error ("Specified root is not an ancestor of specified window");
2772 if (EQ (window, root))
2773 /* A noop. */
2774 return Qnil;
2775 /* I don't understand the "top > 0" part below. If we deal with a
2776 standalone minibuffer it would have been caught by the preceding
2777 test. */
2778 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2779 error ("Can't expand minibuffer to full frame");
2781 if (!NILP (w->buffer))
2783 startpos = marker_position (w->start);
2784 top = WINDOW_TOP_EDGE_LINE (w)
2785 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2786 /* Make sure WINDOW is the frame's selected window. */
2787 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2789 if (EQ (selected_frame, w->frame))
2790 Fselect_window (window, Qnil);
2791 else
2792 fset_selected_window (f, window);
2795 else
2797 /* See if the frame's selected window is a part of the window
2798 subtree rooted at WINDOW, by finding all the selected window's
2799 parents and comparing each one with WINDOW. If it isn't we
2800 need a new selected window for this frame. */
2801 swindow = FRAME_SELECTED_WINDOW (f);
2802 while (1)
2804 pwindow = swindow;
2805 while (!NILP (pwindow) && !EQ (window, pwindow))
2806 pwindow = XWINDOW (pwindow)->parent;
2808 if (EQ (window, pwindow))
2809 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2810 as the new selected window. */
2811 break;
2812 else
2813 /* Else try the previous window of SWINDOW. */
2814 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2817 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2819 if (EQ (selected_frame, w->frame))
2820 Fselect_window (swindow, Qnil);
2821 else
2822 fset_selected_window (f, swindow);
2826 block_input ();
2827 if (!FRAME_INITIAL_P (f))
2829 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2831 /* We are going to free the glyph matrices of WINDOW, and with
2832 that we might lose any information about glyph rows that have
2833 some of their glyphs highlighted in mouse face. (These rows
2834 are marked with a non-zero mouse_face_p flag.) If WINDOW
2835 indeed has some glyphs highlighted in mouse face, signal to
2836 frame's up-to-date hook that mouse highlight was overwritten,
2837 so that it will arrange for redisplaying the highlight. */
2838 if (EQ (hlinfo->mouse_face_window, window))
2840 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
2841 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
2842 hlinfo->mouse_face_window = Qnil;
2845 free_window_matrices (r);
2847 windows_or_buffers_changed++;
2848 Vwindow_list = Qnil;
2849 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2850 resize_failed = 0;
2852 if (NILP (w->buffer))
2854 /* Resize child windows vertically. */
2855 XSETINT (delta, XINT (r->total_lines)
2856 - XINT (w->total_lines));
2857 wset_top_line (w, r->top_line);
2858 resize_root_window (window, delta, Qnil, Qnil);
2859 if (window_resize_check (w, 0))
2860 window_resize_apply (w, 0);
2861 else
2863 resize_root_window (window, delta, Qnil, Qt);
2864 if (window_resize_check (w, 0))
2865 window_resize_apply (w, 0);
2866 else
2867 resize_failed = 1;
2870 /* Resize child windows horizontally. */
2871 if (!resize_failed)
2873 wset_left_col (w, r->left_col);
2874 XSETINT (delta,
2875 XINT (r->total_cols) - XINT (w->total_cols));
2876 wset_left_col (w, r->left_col);
2877 resize_root_window (window, delta, Qt, Qnil);
2878 if (window_resize_check (w, 1))
2879 window_resize_apply (w, 1);
2880 else
2882 resize_root_window (window, delta, Qt, Qt);
2883 if (window_resize_check (w, 1))
2884 window_resize_apply (w, 1);
2885 else
2886 resize_failed = 1;
2890 if (resize_failed)
2891 /* Play safe, if we still can ... */
2893 window = swindow;
2894 w = XWINDOW (window);
2898 /* Cleanly unlink WINDOW from window-tree. */
2899 if (!NILP (w->prev))
2900 /* Get SIBLING above (on the left of) WINDOW. */
2902 sibling = w->prev;
2903 s = XWINDOW (sibling);
2904 wset_next (s, w->next);
2905 if (!NILP (s->next))
2906 wset_prev (XWINDOW (s->next), sibling);
2908 else
2909 /* Get SIBLING below (on the right of) WINDOW. */
2911 sibling = w->next;
2912 s = XWINDOW (sibling);
2913 wset_prev (s, Qnil);
2914 if (!NILP (XWINDOW (w->parent)->vchild))
2915 wset_vchild (XWINDOW (w->parent), sibling);
2916 else
2917 wset_hchild (XWINDOW (w->parent), sibling);
2920 /* Delete ROOT and all child windows of ROOT. */
2921 if (!NILP (r->vchild))
2923 delete_all_child_windows (r->vchild);
2924 wset_vchild (r, Qnil);
2926 else if (!NILP (r->hchild))
2928 delete_all_child_windows (r->hchild);
2929 wset_hchild (r, Qnil);
2932 replace_window (root, window, 1);
2934 /* This must become SWINDOW anyway ....... */
2935 if (!NILP (w->buffer) && !resize_failed)
2937 /* Try to minimize scrolling, by setting the window start to the
2938 point will cause the text at the old window start to be at the
2939 same place on the frame. But don't try to do this if the
2940 window start is outside the visible portion (as might happen
2941 when the display is not current, due to typeahead). */
2942 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2943 if (new_top != top
2944 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2945 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2947 struct position pos;
2948 struct buffer *obuf = current_buffer;
2950 Fset_buffer (w->buffer);
2951 /* This computation used to temporarily move point, but that
2952 can have unwanted side effects due to text properties. */
2953 pos = *vmotion (startpos, -top, w);
2955 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2956 wset_window_end_valid (w, Qnil);
2957 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
2958 || FETCH_BYTE (pos.bytepos - 1) == '\n');
2959 /* We need to do this, so that the window-scroll-functions
2960 get called. */
2961 w->optional_new_start = 1;
2963 set_buffer_internal (obuf);
2967 adjust_glyphs (f);
2968 unblock_input ();
2970 run_window_configuration_change_hook (f);
2972 return Qnil;
2976 void
2977 replace_buffer_in_windows (Lisp_Object buffer)
2979 call1 (Qreplace_buffer_in_windows, buffer);
2983 /* Safely replace BUFFER with some other buffer in all windows of all
2984 frames, even those on other keyboards. */
2986 void
2987 replace_buffer_in_windows_safely (Lisp_Object buffer)
2989 Lisp_Object tail, frame;
2991 /* A single call to window_loop won't do the job because it only
2992 considers frames on the current keyboard. So loop manually over
2993 frames, and handle each one. */
2994 FOR_EACH_FRAME (tail, frame)
2995 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
2998 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2999 minimum allowable size. */
3001 void
3002 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
3004 /* For height, we have to see:
3005 how many windows the frame has at minimum (one or two),
3006 and whether it has a menu bar or other special stuff at the top. */
3007 int min_height
3008 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3009 ? MIN_SAFE_WINDOW_HEIGHT
3010 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3012 if (FRAME_TOP_MARGIN (frame) > 0)
3013 min_height += FRAME_TOP_MARGIN (frame);
3015 if (*rows < min_height)
3016 *rows = min_height;
3017 if (*cols < MIN_SAFE_WINDOW_WIDTH)
3018 *cols = MIN_SAFE_WINDOW_WIDTH;
3021 /* Adjust the margins of window W if text area is too small.
3022 Return 1 if window width is ok after adjustment; 0 if window
3023 is still too narrow. */
3025 static int
3026 adjust_window_margins (struct window *w)
3028 int box_cols = (WINDOW_TOTAL_COLS (w)
3029 - WINDOW_FRINGE_COLS (w)
3030 - WINDOW_SCROLL_BAR_COLS (w));
3031 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
3032 + WINDOW_RIGHT_MARGIN_COLS (w));
3034 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
3035 return 1;
3037 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
3038 return 0;
3040 /* Window's text area is too narrow, but reducing the window
3041 margins will fix that. */
3042 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
3043 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
3045 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
3047 wset_left_margin_cols (w, make_number (margin_cols / 2));
3048 wset_right_margin_cols (w, make_number (margin_cols / 2));
3050 else
3051 wset_right_margin_cols (w, make_number (margin_cols));
3053 else
3054 wset_left_margin_cols (w, make_number (margin_cols));
3055 return 1;
3058 /* The following three routines are needed for running a window's
3059 configuration change hook. */
3060 static void
3061 run_funs (Lisp_Object funs)
3063 for (; CONSP (funs); funs = XCDR (funs))
3064 if (!EQ (XCAR (funs), Qt))
3065 call0 (XCAR (funs));
3068 static Lisp_Object
3069 select_window_norecord (Lisp_Object window)
3071 return WINDOW_LIVE_P (window)
3072 ? Fselect_window (window, Qt) : selected_window;
3075 static Lisp_Object
3076 select_frame_norecord (Lisp_Object frame)
3078 return FRAME_LIVE_P (XFRAME (frame))
3079 ? Fselect_frame (frame, Qt) : selected_frame;
3082 void
3083 run_window_configuration_change_hook (struct frame *f)
3085 ptrdiff_t count = SPECPDL_INDEX ();
3086 Lisp_Object frame, global_wcch
3087 = Fdefault_value (Qwindow_configuration_change_hook);
3088 XSETFRAME (frame, f);
3090 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3091 return;
3093 /* Use the right buffer. Matters when running the local hooks. */
3094 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3096 record_unwind_current_buffer ();
3097 Fset_buffer (Fwindow_buffer (Qnil));
3100 if (SELECTED_FRAME () != f)
3102 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3103 select_frame_norecord (frame);
3106 /* Look for buffer-local values. */
3108 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3109 for (; CONSP (windows); windows = XCDR (windows))
3111 Lisp_Object window = XCAR (windows);
3112 Lisp_Object buffer = Fwindow_buffer (window);
3113 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3114 buffer)))
3116 ptrdiff_t inner_count = SPECPDL_INDEX ();
3117 record_unwind_protect (select_window_norecord, Fselected_window ());
3118 select_window_norecord (window);
3119 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3120 buffer));
3121 unbind_to (inner_count, Qnil);
3126 run_funs (global_wcch);
3127 unbind_to (count, Qnil);
3130 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3131 Srun_window_configuration_change_hook, 1, 1, 0,
3132 doc: /* Run `window-configuration-change-hook' for FRAME. */)
3133 (Lisp_Object frame)
3135 CHECK_LIVE_FRAME (frame);
3136 run_window_configuration_change_hook (XFRAME (frame));
3137 return Qnil;
3140 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3141 means it's allowed to run hooks. See make_frame for a case where
3142 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3143 margins, fringes, and scroll-bar settings of the window are not
3144 reset from the buffer's local settings. */
3146 void
3147 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
3149 struct window *w = XWINDOW (window);
3150 struct buffer *b = XBUFFER (buffer);
3151 ptrdiff_t count = SPECPDL_INDEX ();
3152 int samebuf = EQ (buffer, w->buffer);
3154 wset_buffer (w, buffer);
3156 if (EQ (window, selected_window))
3157 bset_last_selected_window (b, window);
3159 /* Let redisplay errors through. */
3160 b->display_error_modiff = 0;
3162 /* Update time stamps of buffer display. */
3163 if (INTEGERP (BVAR (b, display_count)))
3164 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3165 bset_display_time (b, Fcurrent_time ());
3167 wset_window_end_pos (w, make_number (0));
3168 wset_window_end_vpos (w, make_number (0));
3169 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3170 wset_window_end_valid (w, Qnil);
3171 if (!(keep_margins_p && samebuf))
3172 { /* If we're not actually changing the buffer, don't reset hscroll and
3173 vscroll. This case happens for example when called from
3174 change_frame_size_1, where we use a dummy call to
3175 Fset_window_buffer on the frame's selected window (and no other)
3176 just in order to run window-configuration-change-hook.
3177 Resetting hscroll and vscroll here is problematic for things like
3178 image-mode and doc-view-mode since it resets the image's position
3179 whenever we resize the frame. */
3180 w->hscroll = w->min_hscroll = 0;
3181 w->vscroll = 0;
3182 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3183 set_marker_restricted (w->start,
3184 make_number (b->last_window_start),
3185 buffer);
3186 w->start_at_line_beg = 0;
3187 w->force_start = 0;
3188 w->last_modified = 0;
3189 w->last_overlay_modified = 0;
3191 /* Maybe we could move this into the `if' but it's not obviously safe and
3192 I doubt it's worth the trouble. */
3193 windows_or_buffers_changed++;
3195 /* We must select BUFFER for running the window-scroll-functions. */
3196 /* We can't check ! NILP (Vwindow_scroll_functions) here
3197 because that might itself be a local variable. */
3198 if (window_initialized)
3200 record_unwind_current_buffer ();
3201 Fset_buffer (buffer);
3204 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3206 if (!keep_margins_p)
3208 /* Set left and right marginal area width etc. from buffer. */
3210 /* This may call adjust_window_margins three times, so
3211 temporarily disable window margins. */
3212 Lisp_Object save_left = w->left_margin_cols;
3213 Lisp_Object save_right = w->right_margin_cols;
3215 wset_left_margin_cols (w, Qnil);
3216 wset_right_margin_cols (w, Qnil);
3218 Fset_window_fringes (window,
3219 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3220 BVAR (b, fringes_outside_margins));
3222 Fset_window_scroll_bars (window,
3223 BVAR (b, scroll_bar_width),
3224 BVAR (b, vertical_scroll_bar_type), Qnil);
3226 wset_left_margin_cols (w, save_left);
3227 wset_right_margin_cols (w, save_right);
3229 Fset_window_margins (window,
3230 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3233 if (run_hooks_p)
3235 if (! NILP (Vwindow_scroll_functions))
3236 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3237 Fmarker_position (w->start));
3238 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3241 unbind_to (count, Qnil);
3244 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3245 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3246 WINDOW must be a live window and defaults to the selected one.
3247 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3249 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3250 display margins, fringe widths, and scroll bar settings are preserved;
3251 the default is to reset these from the local settings for BUFFER-OR-NAME
3252 or the frame defaults. Return nil.
3254 This function throws an error when WINDOW is strongly dedicated to its
3255 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3256 already display BUFFER-OR-NAME.
3258 This function runs `window-scroll-functions' before running
3259 `window-configuration-change-hook'. */)
3260 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3262 register Lisp_Object tem, buffer;
3263 register struct window *w = decode_live_window (window);
3265 XSETWINDOW (window, w);
3266 buffer = Fget_buffer (buffer_or_name);
3267 CHECK_BUFFER (buffer);
3268 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3269 error ("Attempt to display deleted buffer");
3271 tem = w->buffer;
3272 if (NILP (tem))
3273 error ("Window is deleted");
3274 else if (!EQ (tem, Qt))
3275 /* w->buffer is t when the window is first being set up. */
3277 if (!EQ (tem, buffer))
3279 if (EQ (w->dedicated, Qt))
3280 /* WINDOW is strongly dedicated to its buffer, signal an
3281 error. */
3282 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3283 else
3284 /* WINDOW is weakly dedicated to its buffer, reset
3285 dedication. */
3286 wset_dedicated (w, Qnil);
3288 call1 (Qrecord_window_buffer, window);
3291 unshow_buffer (w);
3294 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3296 return Qnil;
3299 static Lisp_Object
3300 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3302 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3305 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3306 0, 1, 0,
3307 doc: /* Force all windows to be updated on next redisplay.
3308 If optional arg OBJECT is a window, force redisplay of that window only.
3309 If OBJECT is a buffer or buffer name, force redisplay of all windows
3310 displaying that buffer. */)
3311 (Lisp_Object object)
3313 if (NILP (object))
3315 windows_or_buffers_changed++;
3316 update_mode_lines++;
3317 return Qt;
3320 if (WINDOWP (object))
3322 struct window *w = XWINDOW (object);
3323 mark_window_display_accurate (object, 0);
3324 w->update_mode_line = 1;
3325 if (BUFFERP (w->buffer))
3326 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3327 ++update_mode_lines;
3328 return Qt;
3331 if (STRINGP (object))
3332 object = Fget_buffer (object);
3333 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object)))
3335 /* Walk all windows looking for buffer, and force update
3336 of each of those windows. */
3338 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3339 return NILP (object) ? Qnil : Qt;
3342 /* If nothing suitable was found, just return.
3343 We could signal an error, but this feature will typically be used
3344 asynchronously in timers or process sentinels, so we don't. */
3345 return Qnil;
3348 /* Obsolete since 24.3. */
3349 void
3350 temp_output_buffer_show (register Lisp_Object buf)
3352 register struct buffer *old = current_buffer;
3353 register Lisp_Object window;
3354 register struct window *w;
3356 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3358 Fset_buffer (buf);
3359 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3360 BEGV = BEG;
3361 ZV = Z;
3362 SET_PT (BEG);
3363 set_buffer_internal (old);
3365 if (!NILP (Vtemp_buffer_show_function))
3366 call1 (Vtemp_buffer_show_function, buf);
3367 else
3369 window = display_buffer (buf, Qnil, Qnil);
3371 if (!EQ (XWINDOW (window)->frame, selected_frame))
3372 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3373 Vminibuf_scroll_window = window;
3374 w = XWINDOW (window);
3375 w->hscroll = 0;
3376 w->min_hscroll = 0;
3377 set_marker_restricted_both (w->start, buf, BEG, BEG);
3378 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3380 /* Run temp-buffer-show-hook, with the chosen window selected
3381 and its buffer current. */
3383 ptrdiff_t count = SPECPDL_INDEX ();
3384 Lisp_Object prev_window, prev_buffer;
3385 prev_window = selected_window;
3386 XSETBUFFER (prev_buffer, old);
3388 /* Select the window that was chosen, for running the hook.
3389 Note: Both Fselect_window and select_window_norecord may
3390 set-buffer to the buffer displayed in the window,
3391 so we need to save the current buffer. --stef */
3392 record_unwind_protect (Fset_buffer, prev_buffer);
3393 record_unwind_protect (select_window_norecord, prev_window);
3394 Fselect_window (window, Qt);
3395 Fset_buffer (w->buffer);
3396 Frun_hooks (1, &Qtemp_buffer_show_hook);
3397 unbind_to (count, Qnil);
3402 /* Make new window, have it replace WINDOW in window-tree, and make
3403 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3404 horizontal child). */
3405 static void
3406 make_parent_window (Lisp_Object window, int horflag)
3408 Lisp_Object parent;
3409 register struct window *o, *p;
3411 o = XWINDOW (window);
3412 p = allocate_window ();
3413 memcpy ((char *) p + sizeof (struct vectorlike_header),
3414 (char *) o + sizeof (struct vectorlike_header),
3415 word_size * VECSIZE (struct window));
3416 XSETWINDOW (parent, p);
3418 p->sequence_number = ++sequence_number;
3420 replace_window (window, parent, 1);
3422 wset_next (o, Qnil);
3423 wset_prev (o, Qnil);
3424 wset_parent (o, parent);
3426 wset_hchild (p, horflag ? window : Qnil);
3427 wset_vchild (p, horflag ? Qnil : window);
3428 wset_start (p, Qnil);
3429 wset_pointm (p, Qnil);
3430 wset_buffer (p, Qnil);
3431 wset_combination_limit (p, Qnil);
3432 wset_window_parameters (p, Qnil);
3435 /* Make new window from scratch. */
3436 Lisp_Object
3437 make_window (void)
3439 Lisp_Object window;
3440 register struct window *w;
3442 w = allocate_window ();
3443 /* Initialize Lisp data. Note that allocate_window initializes all
3444 Lisp data to nil, so do it only for slots which should not be nil. */
3445 wset_left_col (w, make_number (0));
3446 wset_top_line (w, make_number (0));
3447 wset_total_lines (w, make_number (0));
3448 wset_total_cols (w, make_number (0));
3449 wset_normal_lines (w, make_float (1.0));
3450 wset_normal_cols (w, make_float (1.0));
3451 wset_new_total (w, make_number (0));
3452 wset_new_normal (w, make_number (0));
3453 wset_start (w, Fmake_marker ());
3454 wset_pointm (w, Fmake_marker ());
3455 wset_vertical_scroll_bar_type (w, Qt);
3456 wset_window_end_pos (w, make_number (0));
3457 wset_window_end_vpos (w, make_number (0));
3458 /* These Lisp fields are marked specially so they're not set to nil by
3459 allocate_window. */
3460 wset_prev_buffers (w, Qnil);
3461 wset_next_buffers (w, Qnil);
3463 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3464 non-Lisp data, so do it only for slots which should not be zero. */
3465 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3466 w->phys_cursor_type = -1;
3467 w->phys_cursor_width = -1;
3468 w->sequence_number = ++sequence_number;
3470 /* Reset window_list. */
3471 Vwindow_list = Qnil;
3472 /* Return window. */
3473 XSETWINDOW (window, w);
3474 return window;
3477 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3478 doc: /* Set new total size of WINDOW to SIZE.
3479 WINDOW must be a valid window and defaults to the selected one.
3480 Return SIZE.
3482 Optional argument ADD non-nil means add SIZE to the new total size of
3483 WINDOW and return the sum.
3485 Note: This function does not operate on any child windows of WINDOW. */)
3486 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3488 struct window *w = decode_valid_window (window);
3490 CHECK_NUMBER (size);
3491 if (NILP (add))
3492 wset_new_total (w, size);
3493 else
3494 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3496 return w->new_total;
3499 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3500 doc: /* Set new normal size of WINDOW to SIZE.
3501 WINDOW must be a valid window and defaults to the selected one.
3502 Return SIZE.
3504 Note: This function does not operate on any child windows of WINDOW. */)
3505 (Lisp_Object window, Lisp_Object size)
3507 wset_new_normal (decode_valid_window (window), size);
3508 return size;
3511 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3512 non-zero) to w->new_total would result in correct heights (widths)
3513 for window W and recursively all child windows of W.
3515 Note: This function does not check any of `window-fixed-size-p',
3516 `window-min-height' or `window-min-width'. It does check that window
3517 sizes do not drop below one line (two columns). */
3518 static int
3519 window_resize_check (struct window *w, int horflag)
3521 struct window *c;
3523 if (!NILP (w->vchild))
3524 /* W is a vertical combination. */
3526 c = XWINDOW (w->vchild);
3527 if (horflag)
3528 /* All child windows of W must have the same width as W. */
3530 while (c)
3532 if ((XINT (c->new_total) != XINT (w->new_total))
3533 || !window_resize_check (c, horflag))
3534 return 0;
3535 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3537 return 1;
3539 else
3540 /* The sum of the heights of the child windows of W must equal
3541 W's height. */
3543 int sum_of_sizes = 0;
3544 while (c)
3546 if (!window_resize_check (c, horflag))
3547 return 0;
3548 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3549 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3551 return (sum_of_sizes == XINT (w->new_total));
3554 else if (!NILP (w->hchild))
3555 /* W is a horizontal combination. */
3557 c = XWINDOW (w->hchild);
3558 if (horflag)
3559 /* The sum of the widths of the child windows of W must equal W's
3560 width. */
3562 int sum_of_sizes = 0;
3563 while (c)
3565 if (!window_resize_check (c, horflag))
3566 return 0;
3567 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3568 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3570 return (sum_of_sizes == XINT (w->new_total));
3572 else
3573 /* All child windows of W must have the same height as W. */
3575 while (c)
3577 if ((XINT (c->new_total) != XINT (w->new_total))
3578 || !window_resize_check (c, horflag))
3579 return 0;
3580 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3582 return 1;
3585 else
3586 /* A leaf window. Make sure it's not too small. The following
3587 hardcodes the values of `window-safe-min-width' (2) and
3588 `window-safe-min-height' (1) which are defined in window.el. */
3589 return XINT (w->new_total) >= (horflag ? 2 : 1);
3592 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3593 w->new_total for window W and recursively all child windows of W.
3594 Also calculate and assign the new vertical (horizontal) start
3595 positions of each of these windows.
3597 This function does not perform any error checks. Make sure you have
3598 run window_resize_check on W before applying this function. */
3599 static void
3600 window_resize_apply (struct window *w, int horflag)
3602 struct window *c;
3603 int pos;
3605 /* Note: Assigning new_normal requires that the new total size of the
3606 parent window has been set *before*. */
3607 if (horflag)
3609 wset_total_cols (w, w->new_total);
3610 if (NUMBERP (w->new_normal))
3611 wset_normal_cols (w, w->new_normal);
3613 pos = XINT (w->left_col);
3615 else
3617 wset_total_lines (w, w->new_total);
3618 if (NUMBERP (w->new_normal))
3619 wset_normal_lines (w, w->new_normal);
3621 pos = XINT (w->top_line);
3624 if (!NILP (w->vchild))
3625 /* W is a vertical combination. */
3627 c = XWINDOW (w->vchild);
3628 while (c)
3630 if (horflag)
3631 wset_left_col (c, make_number (pos));
3632 else
3633 wset_top_line (c, make_number (pos));
3634 window_resize_apply (c, horflag);
3635 if (!horflag)
3636 pos = pos + XINT (c->total_lines);
3637 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3640 else if (!NILP (w->hchild))
3641 /* W is a horizontal combination. */
3643 c = XWINDOW (w->hchild);
3644 while (c)
3646 if (horflag)
3647 wset_left_col (c, make_number (pos));
3648 else
3649 wset_top_line (c, make_number (pos));
3650 window_resize_apply (c, horflag);
3651 if (horflag)
3652 pos = pos + XINT (c->total_cols);
3653 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3657 /* Clear out some redisplay caches. */
3658 w->last_modified = 0;
3659 w->last_overlay_modified = 0;
3663 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 1, 2, 0,
3664 doc: /* Apply requested size values for window-tree of FRAME.
3665 Optional argument HORIZONTAL omitted or nil means apply requested height
3666 values. HORIZONTAL non-nil means apply requested width values.
3668 This function checks whether the requested values sum up to a valid
3669 window layout, recursively assigns the new sizes of all child windows
3670 and calculates and assigns the new start positions of these windows.
3672 Note: This function does not check any of `window-fixed-size-p',
3673 `window-min-height' or `window-min-width'. All these checks have to
3674 be applied on the Elisp level. */)
3675 (Lisp_Object frame, Lisp_Object horizontal)
3677 struct frame *f;
3678 struct window *r;
3679 int horflag = !NILP (horizontal);
3681 if (NILP (frame))
3682 frame = selected_frame;
3683 CHECK_LIVE_FRAME (frame);
3685 f = XFRAME (frame);
3686 r = XWINDOW (FRAME_ROOT_WINDOW (f));
3688 if (!window_resize_check (r, horflag)
3689 || ! EQ (r->new_total,
3690 (horflag ? r->total_cols : r->total_lines)))
3691 return Qnil;
3693 block_input ();
3694 window_resize_apply (r, horflag);
3696 windows_or_buffers_changed++;
3697 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3699 adjust_glyphs (f);
3700 unblock_input ();
3702 run_window_configuration_change_hook (f);
3704 return Qt;
3708 /* Resize frame F's windows when number of lines of F is set to SIZE.
3709 HORFLAG 1 means resize windows when number of columns of F is set to
3710 SIZE.
3712 This function can delete all windows but the selected one in order to
3713 satisfy the request. The result will be meaningful if and only if
3714 F's windows have meaningful sizes when you call this. */
3715 void
3716 resize_frame_windows (struct frame *f, int size, int horflag)
3718 Lisp_Object root = f->root_window;
3719 struct window *r = XWINDOW (root);
3720 Lisp_Object mini = f->minibuffer_window;
3721 struct window *m;
3722 /* new_size is the new size of the frame's root window. */
3723 int new_size = (horflag
3724 ? size
3725 : (size
3726 - FRAME_TOP_MARGIN (f)
3727 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3728 ? 1 : 0)));
3730 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
3731 if (NILP (r->vchild) && NILP (r->hchild))
3732 /* For a leaf root window just set the size. */
3733 if (horflag)
3734 wset_total_cols (r, make_number (new_size));
3735 else
3736 wset_total_lines (r, make_number (new_size));
3737 else
3739 /* old_size is the old size of the frame's root window. */
3740 int old_size = XFASTINT (horflag ? r->total_cols
3741 : r->total_lines);
3742 Lisp_Object delta;
3744 XSETINT (delta, new_size - old_size);
3745 /* Try a "normal" resize first. */
3746 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3747 if (window_resize_check (r, horflag)
3748 && new_size == XINT (r->new_total))
3749 window_resize_apply (r, horflag);
3750 else
3752 /* Try with "reasonable" minimum sizes next. */
3753 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3754 if (window_resize_check (r, horflag)
3755 && new_size == XINT (r->new_total))
3756 window_resize_apply (r, horflag);
3757 else
3759 /* Finally, try with "safe" minimum sizes. */
3760 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3761 if (window_resize_check (r, horflag)
3762 && new_size == XINT (r->new_total))
3763 window_resize_apply (r, horflag);
3764 else
3766 /* We lost. Delete all windows but the frame's
3767 selected one. */
3768 root = f->selected_window;
3769 Fdelete_other_windows_internal (root, Qnil);
3770 if (horflag)
3771 wset_total_cols (XWINDOW (root), make_number (new_size));
3772 else
3773 wset_total_lines (XWINDOW (root), make_number (new_size));
3779 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3781 m = XWINDOW (mini);
3782 if (horflag)
3783 wset_total_cols (m, make_number (size));
3784 else
3786 /* Are we sure we always want 1 line here? */
3787 wset_total_lines (m, make_number (1));
3788 wset_top_line
3789 (m, make_number (XINT (r->top_line) + XINT (r->total_lines)));
3793 windows_or_buffers_changed++;
3797 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3798 doc: /* Split window OLD.
3799 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3800 new window. In any case TOTAL-SIZE must be a positive integer.
3802 Third argument SIDE nil (or `below') specifies that the new window shall
3803 be located below WINDOW. SIDE `above' means the new window shall be
3804 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3805 lines of the new window including space reserved for the mode and/or
3806 header line.
3808 SIDE t (or `right') specifies that the new window shall be located on
3809 the right side of WINDOW. SIDE `left' means the new window shall be
3810 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3811 number of columns of the new window including space reserved for fringes
3812 and the scrollbar or a divider column.
3814 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3815 according to the SIDE argument.
3817 The new total and normal sizes of all involved windows must have been
3818 set correctly. See the code of `split-window' for how this is done. */)
3819 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3821 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3822 parent window or an internal window we have to install as OLD's new
3823 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3824 provided OLD is a leaf window, or to the frame's selected window.
3825 NEW (*n) is the new window created with some parameters taken from
3826 REFERENCE (*r). */
3827 register Lisp_Object new, frame, reference;
3828 register struct window *o, *p, *n, *r;
3829 struct frame *f;
3830 int horflag
3831 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3832 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3833 int combination_limit = 0;
3835 CHECK_WINDOW (old);
3836 o = XWINDOW (old);
3837 frame = WINDOW_FRAME (o);
3838 f = XFRAME (frame);
3840 CHECK_NUMBER (total_size);
3842 /* Set combination_limit to 1 if we have to make a new parent window.
3843 We do that if either `window-combination-limit' is t, or OLD has no
3844 parent, or OLD is ortho-combined. */
3845 combination_limit =
3846 EQ (Vwindow_combination_limit, Qt)
3847 || NILP (o->parent)
3848 || NILP (horflag
3849 ? (XWINDOW (o->parent)->hchild)
3850 : (XWINDOW (o->parent)->vchild));
3852 /* We need a live reference window to initialize some parameters. */
3853 if (WINDOW_LIVE_P (old))
3854 /* OLD is live, use it as reference window. */
3855 reference = old;
3856 else
3857 /* Use the frame's selected window as reference window. */
3858 reference = FRAME_SELECTED_WINDOW (f);
3859 r = XWINDOW (reference);
3861 /* The following bugs are caught by `split-window'. */
3862 if (MINI_WINDOW_P (o))
3863 error ("Attempt to split minibuffer window");
3864 else if (XINT (total_size) < (horflag ? 2 : 1))
3865 error ("Size of new window too small (after split)");
3866 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3867 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3868 proportionally. */
3870 p = XWINDOW (o->parent);
3871 /* Temporarily pretend we split the parent window. */
3872 wset_new_total
3873 (p, make_number (XINT (horflag ? p->total_cols : p->total_lines)
3874 - XINT (total_size)));
3875 if (!window_resize_check (p, horflag))
3876 error ("Window sizes don't fit");
3877 else
3878 /* Undo the temporary pretension. */
3879 wset_new_total (p, horflag ? p->total_cols : p->total_lines);
3881 else
3883 if (!window_resize_check (o, horflag))
3884 error ("Resizing old window failed");
3885 else if (XINT (total_size) + XINT (o->new_total)
3886 != XINT (horflag ? o->total_cols : o->total_lines))
3887 error ("Sum of sizes of old and new window don't fit");
3890 /* This is our point of no return. */
3891 if (combination_limit)
3893 /* Save the old value of o->normal_cols/lines. It gets corrupted
3894 by make_parent_window and we need it below for assigning it to
3895 p->new_normal. */
3896 Lisp_Object new_normal
3897 = horflag ? o->normal_cols : o->normal_lines;
3899 make_parent_window (old, horflag);
3900 p = XWINDOW (o->parent);
3901 if (EQ (Vwindow_combination_limit, Qt))
3902 /* Store t in the new parent's combination_limit slot to avoid
3903 that its children get merged into another window. */
3904 wset_combination_limit (p, Qt);
3905 /* These get applied below. */
3906 wset_new_total (p, horflag ? o->total_cols : o->total_lines);
3907 wset_new_normal (p, new_normal);
3909 else
3910 p = XWINDOW (o->parent);
3912 windows_or_buffers_changed++;
3913 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3914 new = make_window ();
3915 n = XWINDOW (new);
3916 wset_frame (n, frame);
3917 wset_parent (n, o->parent);
3918 wset_vchild (n, Qnil);
3919 wset_hchild (n, Qnil);
3921 if (EQ (side, Qabove) || EQ (side, Qleft))
3923 wset_prev (n, o->prev);
3924 if (NILP (n->prev))
3925 if (horflag)
3926 wset_hchild (p, new);
3927 else
3928 wset_vchild (p, new);
3929 else
3930 wset_next (XWINDOW (n->prev), new);
3931 wset_next (n, old);
3932 wset_prev (o, new);
3934 else
3936 wset_next (n, o->next);
3937 if (!NILP (n->next))
3938 wset_prev (XWINDOW (n->next), new);
3939 wset_prev (n, old);
3940 wset_next (o, new);
3943 wset_buffer (n, Qt);
3944 wset_window_end_valid (n, Qnil);
3945 memset (&n->last_cursor, 0, sizeof n->last_cursor);
3947 /* Get special geometry settings from reference window. */
3948 wset_left_margin_cols (n, r->left_margin_cols);
3949 wset_right_margin_cols (n, r->right_margin_cols);
3950 wset_left_fringe_width (n, r->left_fringe_width);
3951 wset_right_fringe_width (n, r->right_fringe_width);
3952 n->fringes_outside_margins = r->fringes_outside_margins;
3953 wset_scroll_bar_width (n, r->scroll_bar_width);
3954 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
3956 /* Directly assign orthogonal coordinates and sizes. */
3957 if (horflag)
3959 wset_top_line (n, o->top_line);
3960 wset_total_lines (n, o->total_lines);
3962 else
3964 wset_left_col (n, o->left_col);
3965 wset_total_cols (n, o->total_cols);
3968 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3969 get them ready here. */
3970 wset_new_total (n, total_size);
3971 wset_new_normal (n, normal_size);
3973 block_input ();
3974 window_resize_apply (p, horflag);
3975 adjust_glyphs (f);
3976 /* Set buffer of NEW to buffer of reference window. Don't run
3977 any hooks. */
3978 set_window_buffer (new, r->buffer, 0, 1);
3979 unblock_input ();
3981 /* Maybe we should run the scroll functions in Elisp (which already
3982 runs the configuration change hook). */
3983 if (! NILP (Vwindow_scroll_functions))
3984 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3985 Fmarker_position (n->start));
3986 /* Return NEW. */
3987 return new;
3991 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3992 doc: /* Remove WINDOW from its frame.
3993 WINDOW defaults to the selected window. Return nil.
3994 Signal an error when WINDOW is the only window on its frame. */)
3995 (register Lisp_Object window)
3997 register Lisp_Object parent, sibling, frame, root;
3998 struct window *w, *p, *s, *r;
3999 struct frame *f;
4000 int horflag;
4001 int before_sibling = 0;
4003 w = decode_any_window (window);
4004 XSETWINDOW (window, w);
4005 if (NILP (w->buffer)
4006 && NILP (w->hchild) && NILP (w->vchild))
4007 /* It's a no-op to delete an already deleted window. */
4008 return Qnil;
4010 parent = w->parent;
4011 if (NILP (parent))
4012 /* Never delete a minibuffer or frame root window. */
4013 error ("Attempt to delete minibuffer or sole ordinary window");
4014 else if (NILP (w->prev) && NILP (w->next))
4015 /* Rather bow out here, this case should be handled on the Elisp
4016 level. */
4017 error ("Attempt to delete sole window of parent");
4019 p = XWINDOW (parent);
4020 horflag = NILP (p->vchild);
4022 frame = WINDOW_FRAME (w);
4023 f = XFRAME (frame);
4025 root = FRAME_ROOT_WINDOW (f);
4026 r = XWINDOW (root);
4028 /* Unlink WINDOW from window tree. */
4029 if (NILP (w->prev))
4030 /* Get SIBLING below (on the right of) WINDOW. */
4032 /* before_sibling 1 means WINDOW is the first child of its
4033 parent and thus before the sibling. */
4034 before_sibling = 1;
4035 sibling = w->next;
4036 s = XWINDOW (sibling);
4037 wset_prev (s, Qnil);
4038 if (horflag)
4039 wset_hchild (p, sibling);
4040 else
4041 wset_vchild (p, sibling);
4043 else
4044 /* Get SIBLING above (on the left of) WINDOW. */
4046 sibling = w->prev;
4047 s = XWINDOW (sibling);
4048 wset_next (s, w->next);
4049 if (!NILP (s->next))
4050 wset_prev (XWINDOW (s->next), sibling);
4053 if (window_resize_check (r, horflag)
4054 && EQ (r->new_total,
4055 (horflag ? r->total_cols : r->total_lines)))
4056 /* We can delete WINDOW now. */
4059 /* Block input. */
4060 block_input ();
4061 window_resize_apply (p, horflag);
4063 /* If this window is referred to by the dpyinfo's mouse
4064 highlight, invalidate that slot to be safe (Bug#9904). */
4065 if (!FRAME_INITIAL_P (f))
4067 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4069 if (EQ (hlinfo->mouse_face_window, window))
4070 hlinfo->mouse_face_window = Qnil;
4073 windows_or_buffers_changed++;
4074 Vwindow_list = Qnil;
4075 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4077 wset_next (w, Qnil); /* Don't delete w->next too. */
4078 free_window_matrices (w);
4080 if (!NILP (w->vchild))
4082 delete_all_child_windows (w->vchild);
4083 wset_vchild (w, Qnil);
4085 else if (!NILP (w->hchild))
4087 delete_all_child_windows (w->hchild);
4088 wset_hchild (w, Qnil);
4090 else if (!NILP (w->buffer))
4092 unshow_buffer (w);
4093 unchain_marker (XMARKER (w->pointm));
4094 unchain_marker (XMARKER (w->start));
4095 wset_buffer (w, Qnil);
4098 if (NILP (s->prev) && NILP (s->next))
4099 /* A matrjoshka where SIBLING has become the only child of
4100 PARENT. */
4102 /* Put SIBLING into PARENT's place. */
4103 replace_window (parent, sibling, 0);
4104 /* Have SIBLING inherit the following three slot values from
4105 PARENT (the combination_limit slot is not inherited). */
4106 wset_normal_cols (s, p->normal_cols);
4107 wset_normal_lines (s, p->normal_lines);
4108 /* Mark PARENT as deleted. */
4109 wset_vchild (p, Qnil);
4110 wset_hchild (p, Qnil);
4111 /* Try to merge SIBLING into its new parent. */
4112 recombine_windows (sibling);
4115 adjust_glyphs (f);
4117 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4118 /* We deleted the frame's selected window. */
4120 /* Use the frame's first window as fallback ... */
4121 Lisp_Object new_selected_window = Fframe_first_window (frame);
4122 /* ... but preferably use its most recently used window. */
4123 Lisp_Object mru_window;
4125 /* `get-mru-window' might fail for some reason so play it safe
4126 - promote the first window _without recording it_ first. */
4127 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4128 Fselect_window (new_selected_window, Qt);
4129 else
4130 fset_selected_window (f, new_selected_window);
4132 unblock_input ();
4134 /* Now look whether `get-mru-window' gets us something. */
4135 mru_window = call1 (Qget_mru_window, frame);
4136 if (WINDOW_LIVE_P (mru_window)
4137 && EQ (XWINDOW (mru_window)->frame, frame))
4138 new_selected_window = mru_window;
4140 /* If all ended up well, we now promote the mru window. */
4141 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4142 Fselect_window (new_selected_window, Qnil);
4143 else
4144 fset_selected_window (f, new_selected_window);
4146 else
4147 unblock_input ();
4149 /* Must be run by the caller:
4150 run_window_configuration_change_hook (f); */
4152 else
4153 /* We failed: Relink WINDOW into window tree. */
4155 if (before_sibling)
4157 wset_prev (s, window);
4158 if (horflag)
4159 wset_hchild (p, window);
4160 else
4161 wset_vchild (p, window);
4163 else
4165 wset_next (s, window);
4166 if (!NILP (w->next))
4167 wset_prev (XWINDOW (w->next), window);
4169 error ("Deletion failed");
4172 return Qnil;
4175 /***********************************************************************
4176 Resizing Mini-Windows
4177 ***********************************************************************/
4179 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4180 can. */
4181 void
4182 grow_mini_window (struct window *w, int delta)
4184 struct frame *f = XFRAME (w->frame);
4185 struct window *r;
4186 Lisp_Object root, value;
4188 eassert (MINI_WINDOW_P (w));
4189 eassert (delta >= 0);
4191 root = FRAME_ROOT_WINDOW (f);
4192 r = XWINDOW (root);
4193 value = call2 (Qwindow_resize_root_window_vertically,
4194 root, make_number (- delta));
4195 if (INTEGERP (value) && window_resize_check (r, 0))
4197 block_input ();
4198 window_resize_apply (r, 0);
4200 /* Grow the mini-window. */
4201 wset_top_line
4202 (w, make_number (XFASTINT (r->top_line) + XFASTINT (r->total_lines)));
4203 wset_total_lines
4204 (w, make_number (XFASTINT (w->total_lines) - XINT (value)));
4205 w->last_modified = 0;
4206 w->last_overlay_modified = 0;
4208 windows_or_buffers_changed++;
4209 adjust_glyphs (f);
4210 unblock_input ();
4215 /* Shrink mini-window W. */
4216 void
4217 shrink_mini_window (struct window *w)
4219 struct frame *f = XFRAME (w->frame);
4220 struct window *r;
4221 Lisp_Object root, value;
4222 EMACS_INT size;
4224 eassert (MINI_WINDOW_P (w));
4226 size = XINT (w->total_lines);
4227 if (size > 1)
4229 root = FRAME_ROOT_WINDOW (f);
4230 r = XWINDOW (root);
4231 value = call2 (Qwindow_resize_root_window_vertically,
4232 root, make_number (size - 1));
4233 if (INTEGERP (value) && window_resize_check (r, 0))
4235 block_input ();
4236 window_resize_apply (r, 0);
4238 /* Shrink the mini-window. */
4239 wset_top_line (w, make_number (XFASTINT (r->top_line)
4240 + XFASTINT (r->total_lines)));
4241 wset_total_lines (w, make_number (1));
4243 w->last_modified = 0;
4244 w->last_overlay_modified = 0;
4246 windows_or_buffers_changed++;
4247 adjust_glyphs (f);
4248 unblock_input ();
4250 /* If the above failed for whatever strange reason we must make a
4251 one window frame here. The same routine will be needed when
4252 shrinking the frame (and probably when making the initial
4253 *scratch* window). For the moment leave things as they are. */
4257 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4258 doc: /* Resize minibuffer window WINDOW. */)
4259 (Lisp_Object window)
4261 struct window *w = XWINDOW (window);
4262 struct window *r;
4263 struct frame *f;
4264 int height;
4266 CHECK_WINDOW (window);
4267 f = XFRAME (w->frame);
4269 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4270 error ("Not a valid minibuffer window");
4271 else if (FRAME_MINIBUF_ONLY_P (f))
4272 error ("Cannot resize a minibuffer-only frame");
4274 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4275 height = XINT (r->total_lines) + XINT (w->total_lines);
4276 if (window_resize_check (r, 0)
4277 && XINT (w->new_total) > 0
4278 && height == XINT (r->new_total) + XINT (w->new_total))
4280 block_input ();
4281 window_resize_apply (r, 0);
4283 wset_total_lines (w, w->new_total);
4284 wset_top_line (w, make_number (XINT (r->top_line)
4285 + XINT (r->total_lines)));
4287 windows_or_buffers_changed++;
4288 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4289 adjust_glyphs (f);
4290 unblock_input ();
4292 run_window_configuration_change_hook (f);
4293 return Qt;
4295 else error ("Failed to resize minibuffer window");
4298 /* Mark window cursors off for all windows in the window tree rooted
4299 at W by setting their phys_cursor_on_p flag to zero. Called from
4300 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4301 the frame are cleared. */
4303 void
4304 mark_window_cursors_off (struct window *w)
4306 while (w)
4308 if (!NILP (w->hchild))
4309 mark_window_cursors_off (XWINDOW (w->hchild));
4310 else if (!NILP (w->vchild))
4311 mark_window_cursors_off (XWINDOW (w->vchild));
4312 else
4313 w->phys_cursor_on_p = 0;
4315 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4320 /* Return number of lines of text (not counting mode lines) in W. */
4323 window_internal_height (struct window *w)
4325 int ht = XFASTINT (w->total_lines);
4327 if (!MINI_WINDOW_P (w))
4329 if (!NILP (w->parent)
4330 || !NILP (w->vchild)
4331 || !NILP (w->hchild)
4332 || !NILP (w->next)
4333 || !NILP (w->prev)
4334 || WINDOW_WANTS_MODELINE_P (w))
4335 --ht;
4337 if (WINDOW_WANTS_HEADER_LINE_P (w))
4338 --ht;
4341 return ht;
4344 /************************************************************************
4345 Window Scrolling
4346 ***********************************************************************/
4348 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4349 N screen-fulls, which is defined as the height of the window minus
4350 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4351 instead. Negative values of N mean scroll down. NOERROR non-zero
4352 means don't signal an error if we try to move over BEGV or ZV,
4353 respectively. */
4355 static void
4356 window_scroll (Lisp_Object window, EMACS_INT n, int whole, int noerror)
4358 immediate_quit = 1;
4359 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4361 /* If we must, use the pixel-based version which is much slower than
4362 the line-based one but can handle varying line heights. */
4363 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4364 window_scroll_pixel_based (window, n, whole, noerror);
4365 else
4366 window_scroll_line_based (window, n, whole, noerror);
4368 immediate_quit = 0;
4372 /* Implementation of window_scroll that works based on pixel line
4373 heights. See the comment of window_scroll for parameter
4374 descriptions. */
4376 static void
4377 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4379 struct it it;
4380 struct window *w = XWINDOW (window);
4381 struct text_pos start;
4382 int this_scroll_margin;
4383 /* True if we fiddled the window vscroll field without really scrolling. */
4384 int vscrolled = 0;
4385 int x, y, rtop, rbot, rowh, vpos;
4386 void *itdata = NULL;
4388 SET_TEXT_POS_FROM_MARKER (start, w->start);
4389 /* Scrolling a minibuffer window via scroll bar when the echo area
4390 shows long text sometimes resets the minibuffer contents behind
4391 our backs. */
4392 if (CHARPOS (start) > ZV)
4393 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4395 /* If PT is not visible in WINDOW, move back one half of
4396 the screen. Allow PT to be partially visible, otherwise
4397 something like (scroll-down 1) with PT in the line before
4398 the partially visible one would recenter. */
4400 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4402 itdata = bidi_shelve_cache ();
4403 /* Move backward half the height of the window. Performance note:
4404 vmotion used here is about 10% faster, but would give wrong
4405 results for variable height lines. */
4406 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4407 it.current_y = it.last_visible_y;
4408 move_it_vertically_backward (&it, window_box_height (w) / 2);
4410 /* The function move_iterator_vertically may move over more than
4411 the specified y-distance. If it->w is small, e.g. a
4412 mini-buffer window, we may end up in front of the window's
4413 display area. This is the case when Start displaying at the
4414 start of the line containing PT in this case. */
4415 if (it.current_y <= 0)
4417 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4418 move_it_vertically_backward (&it, 0);
4419 it.current_y = 0;
4422 start = it.current.pos;
4423 bidi_unshelve_cache (itdata, 0);
4425 else if (auto_window_vscroll_p)
4427 if (rtop || rbot) /* partially visible */
4429 int px;
4430 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4431 if (whole)
4432 dy = max ((window_box_height (w)
4433 - next_screen_context_lines * dy),
4434 dy);
4435 dy *= n;
4437 if (n < 0)
4439 /* Only vscroll backwards if already vscrolled forwards. */
4440 if (w->vscroll < 0 && rtop > 0)
4442 px = max (0, -w->vscroll - min (rtop, -dy));
4443 Fset_window_vscroll (window, make_number (px), Qt);
4444 return;
4447 if (n > 0)
4449 /* Do vscroll if already vscrolled or only display line. */
4450 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4452 px = max (0, -w->vscroll + min (rbot, dy));
4453 Fset_window_vscroll (window, make_number (px), Qt);
4454 return;
4457 /* Maybe modify window start instead of scrolling. */
4458 if (rbot > 0 || w->vscroll < 0)
4460 ptrdiff_t spos;
4462 Fset_window_vscroll (window, make_number (0), Qt);
4463 /* If there are other text lines above the current row,
4464 move window start to current row. Else to next row. */
4465 if (rbot > 0)
4466 spos = XINT (Fline_beginning_position (Qnil));
4467 else
4468 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4469 set_marker_restricted (w->start, make_number (spos),
4470 w->buffer);
4471 w->start_at_line_beg = 1;
4472 w->update_mode_line = 1;
4473 w->last_modified = 0;
4474 w->last_overlay_modified = 0;
4475 /* Set force_start so that redisplay_window will run the
4476 window-scroll-functions. */
4477 w->force_start = 1;
4478 return;
4482 /* Cancel previous vscroll. */
4483 Fset_window_vscroll (window, make_number (0), Qt);
4486 itdata = bidi_shelve_cache ();
4487 /* If scroll_preserve_screen_position is non-nil, we try to set
4488 point in the same window line as it is now, so get that line. */
4489 if (!NILP (Vscroll_preserve_screen_position))
4491 /* We preserve the goal pixel coordinate across consecutive
4492 calls to scroll-up, scroll-down and other commands that
4493 have the `scroll-command' property. This avoids the
4494 possibility of point becoming "stuck" on a tall line when
4495 scrolling by one line. */
4496 if (window_scroll_pixel_based_preserve_y < 0
4497 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4498 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4500 start_display (&it, w, start);
4501 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4502 window_scroll_pixel_based_preserve_y = it.current_y;
4503 window_scroll_pixel_based_preserve_x = it.current_x;
4506 else
4507 window_scroll_pixel_based_preserve_y
4508 = window_scroll_pixel_based_preserve_x = -1;
4510 /* Move iterator it from start the specified distance forward or
4511 backward. The result is the new window start. */
4512 start_display (&it, w, start);
4513 if (whole)
4515 ptrdiff_t start_pos = IT_CHARPOS (it);
4516 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4517 dy = max ((window_box_height (w)
4518 - next_screen_context_lines * dy),
4519 dy) * n;
4521 /* Note that move_it_vertically always moves the iterator to the
4522 start of a line. So, if the last line doesn't have a newline,
4523 we would end up at the start of the line ending at ZV. */
4524 if (dy <= 0)
4526 move_it_vertically_backward (&it, -dy);
4527 /* Ensure we actually do move, e.g. in case we are currently
4528 looking at an image that is taller that the window height. */
4529 while (start_pos == IT_CHARPOS (it)
4530 && start_pos > BEGV)
4531 move_it_by_lines (&it, -1);
4533 else if (dy > 0)
4535 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4536 MOVE_TO_POS | MOVE_TO_Y);
4537 /* Ensure we actually do move, e.g. in case we are currently
4538 looking at an image that is taller that the window height. */
4539 while (start_pos == IT_CHARPOS (it)
4540 && start_pos < ZV)
4541 move_it_by_lines (&it, 1);
4544 else
4545 move_it_by_lines (&it, n);
4547 /* We failed if we find ZV is already on the screen (scrolling up,
4548 means there's nothing past the end), or if we can't start any
4549 earlier (scrolling down, means there's nothing past the top). */
4550 if ((n > 0 && IT_CHARPOS (it) == ZV)
4551 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4553 if (IT_CHARPOS (it) == ZV)
4555 if (it.current_y < it.last_visible_y
4556 && (it.current_y + it.max_ascent + it.max_descent
4557 > it.last_visible_y))
4559 /* The last line was only partially visible, make it fully
4560 visible. */
4561 w->vscroll = (it.last_visible_y
4562 - it.current_y + it.max_ascent + it.max_descent);
4563 adjust_glyphs (it.f);
4565 else
4567 bidi_unshelve_cache (itdata, 0);
4568 if (noerror)
4569 return;
4570 else if (n < 0) /* could happen with empty buffers */
4571 xsignal0 (Qbeginning_of_buffer);
4572 else
4573 xsignal0 (Qend_of_buffer);
4576 else
4578 if (w->vscroll != 0)
4579 /* The first line was only partially visible, make it fully
4580 visible. */
4581 w->vscroll = 0;
4582 else
4584 bidi_unshelve_cache (itdata, 0);
4585 if (noerror)
4586 return;
4587 else
4588 xsignal0 (Qbeginning_of_buffer);
4592 /* If control gets here, then we vscrolled. */
4594 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4596 /* Don't try to change the window start below. */
4597 vscrolled = 1;
4600 if (! vscrolled)
4602 ptrdiff_t pos = IT_CHARPOS (it);
4603 ptrdiff_t bytepos;
4605 /* If in the middle of a multi-glyph character move forward to
4606 the next character. */
4607 if (in_display_vector_p (&it))
4609 ++pos;
4610 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4613 /* Set the window start, and set up the window for redisplay. */
4614 set_marker_restricted (w->start, make_number (pos),
4615 w->buffer);
4616 bytepos = XMARKER (w->start)->bytepos;
4617 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4618 w->update_mode_line = 1;
4619 w->last_modified = 0;
4620 w->last_overlay_modified = 0;
4621 /* Set force_start so that redisplay_window will run the
4622 window-scroll-functions. */
4623 w->force_start = 1;
4626 /* The rest of this function uses current_y in a nonstandard way,
4627 not including the height of the header line if any. */
4628 it.current_y = it.vpos = 0;
4630 /* Move PT out of scroll margins.
4631 This code wants current_y to be zero at the window start position
4632 even if there is a header line. */
4633 this_scroll_margin = max (0, scroll_margin);
4634 this_scroll_margin
4635 = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4636 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4638 if (n > 0)
4640 /* We moved the window start towards ZV, so PT may be now
4641 in the scroll margin at the top. */
4642 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4643 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4644 && (NILP (Vscroll_preserve_screen_position)
4645 || EQ (Vscroll_preserve_screen_position, Qt)))
4646 /* We found PT at a legitimate height. Leave it alone. */
4648 else if (window_scroll_pixel_based_preserve_y >= 0)
4650 /* If we have a header line, take account of it.
4651 This is necessary because we set it.current_y to 0, above. */
4652 move_it_to (&it, -1,
4653 window_scroll_pixel_based_preserve_x,
4654 window_scroll_pixel_based_preserve_y
4655 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4656 -1, MOVE_TO_Y | MOVE_TO_X);
4657 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4659 else
4661 while (it.current_y < this_scroll_margin)
4663 int prev = it.current_y;
4664 move_it_by_lines (&it, 1);
4665 if (prev == it.current_y)
4666 break;
4668 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4671 else if (n < 0)
4673 ptrdiff_t charpos, bytepos;
4674 int partial_p;
4676 /* Save our position, for the
4677 window_scroll_pixel_based_preserve_y case. */
4678 charpos = IT_CHARPOS (it);
4679 bytepos = IT_BYTEPOS (it);
4681 /* We moved the window start towards BEGV, so PT may be now
4682 in the scroll margin at the bottom. */
4683 move_it_to (&it, PT, -1,
4684 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4685 - this_scroll_margin - 1),
4687 MOVE_TO_POS | MOVE_TO_Y);
4689 /* Save our position, in case it's correct. */
4690 charpos = IT_CHARPOS (it);
4691 bytepos = IT_BYTEPOS (it);
4693 /* See if point is on a partially visible line at the end. */
4694 if (it.what == IT_EOB)
4695 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4696 else
4698 move_it_by_lines (&it, 1);
4699 partial_p = it.current_y > it.last_visible_y;
4702 if (charpos == PT && !partial_p
4703 && (NILP (Vscroll_preserve_screen_position)
4704 || EQ (Vscroll_preserve_screen_position, Qt)))
4705 /* We found PT before we found the display margin, so PT is ok. */
4707 else if (window_scroll_pixel_based_preserve_y >= 0)
4709 SET_TEXT_POS_FROM_MARKER (start, w->start);
4710 start_display (&it, w, start);
4711 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4712 here because we called start_display again and did not
4713 alter it.current_y this time. */
4714 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4715 window_scroll_pixel_based_preserve_y, -1,
4716 MOVE_TO_Y | MOVE_TO_X);
4717 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4719 else
4721 if (partial_p)
4722 /* The last line was only partially visible, so back up two
4723 lines to make sure we're on a fully visible line. */
4725 move_it_by_lines (&it, -2);
4726 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4728 else
4729 /* No, the position we saved is OK, so use it. */
4730 SET_PT_BOTH (charpos, bytepos);
4733 bidi_unshelve_cache (itdata, 0);
4737 /* Implementation of window_scroll that works based on screen lines.
4738 See the comment of window_scroll for parameter descriptions. */
4740 static void
4741 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
4743 register struct window *w = XWINDOW (window);
4744 /* Fvertical_motion enters redisplay, which can trigger
4745 fontification, which in turn can modify buffer text (e.g., if the
4746 fontification functions replace escape sequences with faces, as
4747 in `grep-mode-font-lock-keywords'). So we use a marker to record
4748 the old point position, to prevent crashes in SET_PT_BOTH. */
4749 Lisp_Object opoint_marker = Fpoint_marker ();
4750 register ptrdiff_t pos, pos_byte;
4751 register int ht = window_internal_height (w);
4752 register Lisp_Object tem;
4753 int lose;
4754 Lisp_Object bolp;
4755 ptrdiff_t startpos;
4756 Lisp_Object original_pos = Qnil;
4758 /* If scrolling screen-fulls, compute the number of lines to
4759 scroll from the window's height. */
4760 if (whole)
4761 n *= max (1, ht - next_screen_context_lines);
4763 startpos = marker_position (w->start);
4765 if (!NILP (Vscroll_preserve_screen_position))
4767 if (window_scroll_preserve_vpos <= 0
4768 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4769 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4771 struct position posit
4772 = *compute_motion (startpos, 0, 0, 0,
4773 PT, ht, 0,
4774 -1, w->hscroll,
4775 0, w);
4776 window_scroll_preserve_vpos = posit.vpos;
4777 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
4780 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4781 make_number (window_scroll_preserve_vpos));
4784 XSETFASTINT (tem, PT);
4785 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4787 if (NILP (tem))
4789 Fvertical_motion (make_number (- (ht / 2)), window);
4790 startpos = PT;
4793 SET_PT (startpos);
4794 lose = n < 0 && PT == BEGV;
4795 Fvertical_motion (make_number (n), window);
4796 pos = PT;
4797 pos_byte = PT_BYTE;
4798 bolp = Fbolp ();
4799 SET_PT_BOTH (marker_position (opoint_marker),
4800 marker_byte_position (opoint_marker));
4802 if (lose)
4804 if (noerror)
4805 return;
4806 else
4807 xsignal0 (Qbeginning_of_buffer);
4810 if (pos < ZV)
4812 /* Don't use a scroll margin that is negative or too large. */
4813 int this_scroll_margin =
4814 max (0, min (scroll_margin, XINT (w->total_lines) / 4));
4816 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4817 w->start_at_line_beg = !NILP (bolp);
4818 w->update_mode_line = 1;
4819 w->last_modified = 0;
4820 w->last_overlay_modified = 0;
4821 /* Set force_start so that redisplay_window will run
4822 the window-scroll-functions. */
4823 w->force_start = 1;
4825 if (!NILP (Vscroll_preserve_screen_position)
4826 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4828 SET_PT_BOTH (pos, pos_byte);
4829 Fvertical_motion (original_pos, window);
4831 /* If we scrolled forward, put point enough lines down
4832 that it is outside the scroll margin. */
4833 else if (n > 0)
4835 int top_margin;
4837 if (this_scroll_margin > 0)
4839 SET_PT_BOTH (pos, pos_byte);
4840 Fvertical_motion (make_number (this_scroll_margin), window);
4841 top_margin = PT;
4843 else
4844 top_margin = pos;
4846 if (top_margin <= marker_position (opoint_marker))
4847 SET_PT_BOTH (marker_position (opoint_marker),
4848 marker_byte_position (opoint_marker));
4849 else if (!NILP (Vscroll_preserve_screen_position))
4851 SET_PT_BOTH (pos, pos_byte);
4852 Fvertical_motion (original_pos, window);
4854 else
4855 SET_PT (top_margin);
4857 else if (n < 0)
4859 int bottom_margin;
4861 /* If we scrolled backward, put point near the end of the window
4862 but not within the scroll margin. */
4863 SET_PT_BOTH (pos, pos_byte);
4864 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4865 if (XFASTINT (tem) == ht - this_scroll_margin)
4866 bottom_margin = PT;
4867 else
4868 bottom_margin = PT + 1;
4870 if (bottom_margin > marker_position (opoint_marker))
4871 SET_PT_BOTH (marker_position (opoint_marker),
4872 marker_byte_position (opoint_marker));
4873 else
4875 if (!NILP (Vscroll_preserve_screen_position))
4877 SET_PT_BOTH (pos, pos_byte);
4878 Fvertical_motion (original_pos, window);
4880 else
4881 Fvertical_motion (make_number (-1), window);
4885 else
4887 if (noerror)
4888 return;
4889 else
4890 xsignal0 (Qend_of_buffer);
4895 /* Scroll selected_window up or down. If N is nil, scroll a
4896 screen-full which is defined as the height of the window minus
4897 next_screen_context_lines. If N is the symbol `-', scroll.
4898 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4899 up. This is the guts of Fscroll_up and Fscroll_down. */
4901 static void
4902 scroll_command (Lisp_Object n, int direction)
4904 ptrdiff_t count = SPECPDL_INDEX ();
4906 eassert (eabs (direction) == 1);
4908 /* If selected window's buffer isn't current, make it current for
4909 the moment. But don't screw up if window_scroll gets an error. */
4910 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4912 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4913 Fset_buffer (XWINDOW (selected_window)->buffer);
4915 /* Make redisplay consider other windows than just selected_window. */
4916 ++windows_or_buffers_changed;
4919 if (NILP (n))
4920 window_scroll (selected_window, direction, 1, 0);
4921 else if (EQ (n, Qminus))
4922 window_scroll (selected_window, -direction, 1, 0);
4923 else
4925 n = Fprefix_numeric_value (n);
4926 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4929 unbind_to (count, Qnil);
4932 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4933 doc: /* Scroll text of selected window upward ARG lines.
4934 If ARG is omitted or nil, scroll upward by a near full screen.
4935 A near full screen is `next-screen-context-lines' less than a full screen.
4936 Negative ARG means scroll downward.
4937 If ARG is the atom `-', scroll downward by nearly full screen.
4938 When calling from a program, supply as argument a number, nil, or `-'. */)
4939 (Lisp_Object arg)
4941 scroll_command (arg, 1);
4942 return Qnil;
4945 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4946 doc: /* Scroll text of selected window down ARG lines.
4947 If ARG is omitted or nil, scroll down by a near full screen.
4948 A near full screen is `next-screen-context-lines' less than a full screen.
4949 Negative ARG means scroll upward.
4950 If ARG is the atom `-', scroll upward by nearly full screen.
4951 When calling from a program, supply as argument a number, nil, or `-'. */)
4952 (Lisp_Object arg)
4954 scroll_command (arg, -1);
4955 return Qnil;
4958 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4959 doc: /* Return the other window for \"other window scroll\" commands.
4960 If `other-window-scroll-buffer' is non-nil, a window
4961 showing that buffer is used.
4962 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4963 specifies the window. This takes precedence over
4964 `other-window-scroll-buffer'. */)
4965 (void)
4967 Lisp_Object window;
4969 if (MINI_WINDOW_P (XWINDOW (selected_window))
4970 && !NILP (Vminibuf_scroll_window))
4971 window = Vminibuf_scroll_window;
4972 /* If buffer is specified, scroll that buffer. */
4973 else if (!NILP (Vother_window_scroll_buffer))
4975 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4976 if (NILP (window))
4977 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4979 else
4981 /* Nothing specified; look for a neighboring window on the same
4982 frame. */
4983 window = Fnext_window (selected_window, Qnil, Qnil);
4985 if (EQ (window, selected_window))
4986 /* That didn't get us anywhere; look for a window on another
4987 visible frame. */
4989 window = Fnext_window (window, Qnil, Qt);
4990 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4991 && ! EQ (window, selected_window));
4994 CHECK_LIVE_WINDOW (window);
4996 if (EQ (window, selected_window))
4997 error ("There is no other window");
4999 return window;
5002 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5003 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5004 A near full screen is `next-screen-context-lines' less than a full screen.
5005 The next window is the one below the current one; or the one at the top
5006 if the current one is at the bottom. Negative ARG means scroll downward.
5007 If ARG is the atom `-', scroll downward by nearly full screen.
5008 When calling from a program, supply as argument a number, nil, or `-'.
5010 If `other-window-scroll-buffer' is non-nil, scroll the window
5011 showing that buffer, popping the buffer up if necessary.
5012 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5013 specifies the window to scroll. This takes precedence over
5014 `other-window-scroll-buffer'. */)
5015 (Lisp_Object arg)
5017 Lisp_Object window;
5018 struct window *w;
5019 ptrdiff_t count = SPECPDL_INDEX ();
5021 window = Fother_window_for_scrolling ();
5022 w = XWINDOW (window);
5024 /* Don't screw up if window_scroll gets an error. */
5025 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5026 ++windows_or_buffers_changed;
5028 Fset_buffer (w->buffer);
5029 SET_PT (marker_position (w->pointm));
5031 if (NILP (arg))
5032 window_scroll (window, 1, 1, 1);
5033 else if (EQ (arg, Qminus))
5034 window_scroll (window, -1, 1, 1);
5035 else
5037 if (CONSP (arg))
5038 arg = XCAR (arg);
5039 CHECK_NUMBER (arg);
5040 window_scroll (window, XINT (arg), 0, 1);
5043 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5044 unbind_to (count, Qnil);
5046 return Qnil;
5049 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5050 doc: /* Scroll selected window display ARG columns left.
5051 Default for ARG is window width minus 2.
5052 Value is the total amount of leftward horizontal scrolling in
5053 effect after the change.
5054 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5055 lower bound for automatic scrolling, i.e. automatic scrolling
5056 will not scroll a window to a column less than the value returned
5057 by this function. This happens in an interactive call. */)
5058 (register Lisp_Object arg, Lisp_Object set_minimum)
5060 struct window *w = XWINDOW (selected_window);
5061 EMACS_INT requested_arg = (NILP (arg)
5062 ? window_body_cols (w) - 2
5063 : XINT (Fprefix_numeric_value (arg)));
5064 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5066 if (!NILP (set_minimum))
5067 w->min_hscroll = w->hscroll;
5069 return result;
5072 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5073 doc: /* Scroll selected window display ARG columns right.
5074 Default for ARG is window width minus 2.
5075 Value is the total amount of leftward horizontal scrolling in
5076 effect after the change.
5077 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5078 lower bound for automatic scrolling, i.e. automatic scrolling
5079 will not scroll a window to a column less than the value returned
5080 by this function. This happens in an interactive call. */)
5081 (register Lisp_Object arg, Lisp_Object set_minimum)
5083 struct window *w = XWINDOW (selected_window);
5084 EMACS_INT requested_arg = (NILP (arg)
5085 ? window_body_cols (w) - 2
5086 : XINT (Fprefix_numeric_value (arg)));
5087 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5089 if (!NILP (set_minimum))
5090 w->min_hscroll = w->hscroll;
5092 return result;
5095 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5096 doc: /* Return the window which was selected when entering the minibuffer.
5097 Returns nil, if selected window is not a minibuffer window. */)
5098 (void)
5100 if (minibuf_level > 0
5101 && MINI_WINDOW_P (XWINDOW (selected_window))
5102 && WINDOW_LIVE_P (minibuf_selected_window))
5103 return minibuf_selected_window;
5105 return Qnil;
5108 /* Value is the number of lines actually displayed in window W,
5109 as opposed to its height. */
5111 static int
5112 displayed_window_lines (struct window *w)
5114 struct it it;
5115 struct text_pos start;
5116 int height = window_box_height (w);
5117 struct buffer *old_buffer;
5118 int bottom_y;
5119 void *itdata = NULL;
5121 if (XBUFFER (w->buffer) != current_buffer)
5123 old_buffer = current_buffer;
5124 set_buffer_internal (XBUFFER (w->buffer));
5126 else
5127 old_buffer = NULL;
5129 /* In case W->start is out of the accessible range, do something
5130 reasonable. This happens in Info mode when Info-scroll-down
5131 calls (recenter -1) while W->start is 1. */
5132 if (XMARKER (w->start)->charpos < BEGV)
5133 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5134 else if (XMARKER (w->start)->charpos > ZV)
5135 SET_TEXT_POS (start, ZV, ZV_BYTE);
5136 else
5137 SET_TEXT_POS_FROM_MARKER (start, w->start);
5139 itdata = bidi_shelve_cache ();
5140 start_display (&it, w, start);
5141 move_it_vertically (&it, height);
5142 bottom_y = line_bottom_y (&it);
5143 bidi_unshelve_cache (itdata, 0);
5145 /* rms: On a non-window display,
5146 the value of it.vpos at the bottom of the screen
5147 seems to be 1 larger than window_box_height (w).
5148 This kludge fixes a bug whereby (move-to-window-line -1)
5149 when ZV is on the last screen line
5150 moves to the previous screen line instead of the last one. */
5151 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5152 height++;
5154 /* Add in empty lines at the bottom of the window. */
5155 if (bottom_y < height)
5157 int uy = FRAME_LINE_HEIGHT (it.f);
5158 it.vpos += (height - bottom_y + uy - 1) / uy;
5161 if (old_buffer)
5162 set_buffer_internal (old_buffer);
5164 return it.vpos;
5168 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5169 doc: /* Center point in selected window and maybe redisplay frame.
5170 With prefix argument ARG, recenter putting point on screen line ARG
5171 relative to the selected window. If ARG is negative, it counts up from the
5172 bottom of the window. (ARG should be less than the height of the window.)
5174 If ARG is omitted or nil, then recenter with point on the middle line of
5175 the selected window; if the variable `recenter-redisplay' is non-nil,
5176 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5177 is set to `grow-only', this resets the tool-bar's height to the minimum
5178 height needed); if `recenter-redisplay' has the special value `tty',
5179 then only tty frames are redrawn.
5181 Just C-u as prefix means put point in the center of the window
5182 and redisplay normally--don't erase and redraw the frame. */)
5183 (register Lisp_Object arg)
5185 struct window *w = XWINDOW (selected_window);
5186 struct buffer *buf = XBUFFER (w->buffer);
5187 struct buffer *obuf = current_buffer;
5188 int center_p = 0;
5189 ptrdiff_t charpos, bytepos;
5190 EMACS_INT iarg IF_LINT (= 0);
5191 int this_scroll_margin;
5193 /* If redisplay is suppressed due to an error, try again. */
5194 obuf->display_error_modiff = 0;
5196 if (NILP (arg))
5198 if (!NILP (Vrecenter_redisplay)
5199 && (!EQ (Vrecenter_redisplay, Qtty)
5200 || !NILP (Ftty_type (selected_frame))))
5202 ptrdiff_t i;
5204 /* Invalidate pixel data calculated for all compositions. */
5205 for (i = 0; i < n_compositions; i++)
5206 composition_table[i]->font = NULL;
5208 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5210 Fredraw_frame (WINDOW_FRAME (w));
5211 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5214 center_p = 1;
5216 else if (CONSP (arg)) /* Just C-u. */
5217 center_p = 1;
5218 else
5220 arg = Fprefix_numeric_value (arg);
5221 CHECK_NUMBER (arg);
5222 iarg = XINT (arg);
5225 set_buffer_internal (buf);
5227 /* Do this after making BUF current
5228 in case scroll_margin is buffer-local. */
5229 this_scroll_margin =
5230 max (0, min (scroll_margin, XFASTINT (w->total_lines) / 4));
5232 /* Handle centering on a graphical frame specially. Such frames can
5233 have variable-height lines and centering point on the basis of
5234 line counts would lead to strange effects. */
5235 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5237 if (center_p)
5239 struct it it;
5240 struct text_pos pt;
5241 void *itdata = bidi_shelve_cache ();
5243 SET_TEXT_POS (pt, PT, PT_BYTE);
5244 start_display (&it, w, pt);
5245 move_it_vertically_backward (&it, window_box_height (w) / 2);
5246 charpos = IT_CHARPOS (it);
5247 bytepos = IT_BYTEPOS (it);
5248 bidi_unshelve_cache (itdata, 0);
5250 else if (iarg < 0)
5252 struct it it;
5253 struct text_pos pt;
5254 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5255 int extra_line_spacing;
5256 int h = window_box_height (w);
5257 void *itdata = bidi_shelve_cache ();
5259 iarg = - max (-iarg, this_scroll_margin);
5261 SET_TEXT_POS (pt, PT, PT_BYTE);
5262 start_display (&it, w, pt);
5264 /* Be sure we have the exact height of the full line containing PT. */
5265 move_it_by_lines (&it, 0);
5267 /* The amount of pixels we have to move back is the window
5268 height minus what's displayed in the line containing PT,
5269 and the lines below. */
5270 it.current_y = 0;
5271 it.vpos = 0;
5272 move_it_by_lines (&it, nlines);
5274 if (it.vpos == nlines)
5275 h -= it.current_y;
5276 else
5278 /* Last line has no newline */
5279 h -= line_bottom_y (&it);
5280 it.vpos++;
5283 /* Don't reserve space for extra line spacing of last line. */
5284 extra_line_spacing = it.max_extra_line_spacing;
5286 /* If we can't move down NLINES lines because we hit
5287 the end of the buffer, count in some empty lines. */
5288 if (it.vpos < nlines)
5290 nlines -= it.vpos;
5291 extra_line_spacing = it.extra_line_spacing;
5292 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5294 if (h <= 0)
5296 bidi_unshelve_cache (itdata, 0);
5297 return Qnil;
5300 /* Now find the new top line (starting position) of the window. */
5301 start_display (&it, w, pt);
5302 it.current_y = 0;
5303 move_it_vertically_backward (&it, h);
5305 /* If extra line spacing is present, we may move too far
5306 back. This causes the last line to be only partially
5307 visible (which triggers redisplay to recenter that line
5308 in the middle), so move forward.
5309 But ignore extra line spacing on last line, as it is not
5310 considered to be part of the visible height of the line.
5312 h += extra_line_spacing;
5313 while (-it.current_y > h)
5314 move_it_by_lines (&it, 1);
5316 charpos = IT_CHARPOS (it);
5317 bytepos = IT_BYTEPOS (it);
5319 bidi_unshelve_cache (itdata, 0);
5321 else
5323 struct position pos;
5325 iarg = max (iarg, this_scroll_margin);
5327 pos = *vmotion (PT, -iarg, w);
5328 charpos = pos.bufpos;
5329 bytepos = pos.bytepos;
5332 else
5334 struct position pos;
5335 int ht = window_internal_height (w);
5337 if (center_p)
5338 iarg = ht / 2;
5339 else if (iarg < 0)
5340 iarg += ht;
5342 /* Don't let it get into the margin at either top or bottom. */
5343 iarg = max (iarg, this_scroll_margin);
5344 iarg = min (iarg, ht - this_scroll_margin - 1);
5346 pos = *vmotion (PT, - iarg, w);
5347 charpos = pos.bufpos;
5348 bytepos = pos.bytepos;
5351 /* Set the new window start. */
5352 set_marker_both (w->start, w->buffer, charpos, bytepos);
5353 wset_window_end_valid (w, Qnil);
5355 w->optional_new_start = 1;
5357 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5358 FETCH_BYTE (bytepos - 1) == '\n');
5360 set_buffer_internal (obuf);
5361 return Qnil;
5364 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5365 0, 1, 0,
5366 doc: /* Return the height in lines of the text display area of WINDOW.
5367 WINDOW must be a live window and defaults to the selected one.
5369 The returned height does not include the mode line, any header line,
5370 nor any partial-height lines at the bottom of the text area. */)
5371 (Lisp_Object window)
5373 struct window *w = decode_live_window (window);
5374 int pixel_height = window_box_height (w);
5375 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5376 return make_number (line_height);
5381 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5382 1, 1, "P",
5383 doc: /* Position point relative to window.
5384 ARG nil means position point at center of window.
5385 Else, ARG specifies vertical position within the window;
5386 zero means top of window, negative means relative to bottom of window. */)
5387 (Lisp_Object arg)
5389 struct window *w = XWINDOW (selected_window);
5390 int lines, start;
5391 Lisp_Object window;
5392 #if 0
5393 int this_scroll_margin;
5394 #endif
5396 if (!(BUFFERP (w->buffer)
5397 && XBUFFER (w->buffer) == current_buffer))
5398 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5399 when passed below to set_marker_both. */
5400 error ("move-to-window-line called from unrelated buffer");
5402 window = selected_window;
5403 start = marker_position (w->start);
5404 if (start < BEGV || start > ZV)
5406 int height = window_internal_height (w);
5407 Fvertical_motion (make_number (- (height / 2)), window);
5408 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5409 w->start_at_line_beg = !NILP (Fbolp ());
5410 w->force_start = 1;
5412 else
5413 Fgoto_char (w->start);
5415 lines = displayed_window_lines (w);
5417 #if 0
5418 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5419 #endif
5421 if (NILP (arg))
5422 XSETFASTINT (arg, lines / 2);
5423 else
5425 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5427 if (iarg < 0)
5428 iarg = iarg + lines;
5430 #if 0 /* This code would prevent move-to-window-line from moving point
5431 to a place inside the scroll margins (which would cause the
5432 next redisplay to scroll). I wrote this code, but then concluded
5433 it is probably better not to install it. However, it is here
5434 inside #if 0 so as not to lose it. -- rms. */
5436 /* Don't let it get into the margin at either top or bottom. */
5437 iarg = max (iarg, this_scroll_margin);
5438 iarg = min (iarg, lines - this_scroll_margin - 1);
5439 #endif
5441 arg = make_number (iarg);
5444 /* Skip past a partially visible first line. */
5445 if (w->vscroll)
5446 XSETINT (arg, XINT (arg) + 1);
5448 return Fvertical_motion (arg, window);
5453 /***********************************************************************
5454 Window Configuration
5455 ***********************************************************************/
5457 struct save_window_data
5459 struct vectorlike_header header;
5460 Lisp_Object selected_frame;
5461 Lisp_Object current_window;
5462 Lisp_Object current_buffer;
5463 Lisp_Object minibuf_scroll_window;
5464 Lisp_Object minibuf_selected_window;
5465 Lisp_Object root_window;
5466 Lisp_Object focus_frame;
5467 /* A vector, each of whose elements is a struct saved_window
5468 for one window. */
5469 Lisp_Object saved_windows;
5471 /* All fields above are traced by the GC.
5472 From `fame-cols' down, the fields are ignored by the GC. */
5474 int frame_cols, frame_lines, frame_menu_bar_lines;
5475 int frame_tool_bar_lines;
5478 /* This is saved as a Lisp_Vector */
5479 struct saved_window
5481 struct vectorlike_header header;
5483 Lisp_Object window, buffer, start, pointm, mark;
5484 Lisp_Object left_col, top_line, total_cols, total_lines;
5485 Lisp_Object normal_cols, normal_lines;
5486 Lisp_Object hscroll, min_hscroll;
5487 Lisp_Object parent, prev;
5488 Lisp_Object start_at_line_beg;
5489 Lisp_Object display_table;
5490 Lisp_Object left_margin_cols, right_margin_cols;
5491 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5492 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5493 Lisp_Object combination_limit, window_parameters;
5496 #define SAVED_WINDOW_N(swv,n) \
5497 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5499 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5500 doc: /* Return t if OBJECT is a window-configuration object. */)
5501 (Lisp_Object object)
5503 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5506 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5507 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5508 (Lisp_Object config)
5510 register struct save_window_data *data;
5511 struct Lisp_Vector *saved_windows;
5513 CHECK_WINDOW_CONFIGURATION (config);
5515 data = (struct save_window_data *) XVECTOR (config);
5516 saved_windows = XVECTOR (data->saved_windows);
5517 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5520 DEFUN ("set-window-configuration", Fset_window_configuration,
5521 Sset_window_configuration, 1, 1, 0,
5522 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5523 CONFIGURATION must be a value previously returned
5524 by `current-window-configuration' (which see).
5525 If CONFIGURATION was made from a frame that is now deleted,
5526 only frame-independent values can be restored. In this case,
5527 the return value is nil. Otherwise the value is t. */)
5528 (Lisp_Object configuration)
5530 register struct save_window_data *data;
5531 struct Lisp_Vector *saved_windows;
5532 Lisp_Object new_current_buffer;
5533 Lisp_Object frame;
5534 FRAME_PTR f;
5535 ptrdiff_t old_point = -1;
5537 CHECK_WINDOW_CONFIGURATION (configuration);
5539 data = (struct save_window_data *) XVECTOR (configuration);
5540 saved_windows = XVECTOR (data->saved_windows);
5542 new_current_buffer = data->current_buffer;
5543 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5544 new_current_buffer = Qnil;
5545 else
5547 if (XBUFFER (new_current_buffer) == current_buffer)
5548 /* The code further down "preserves point" by saving here PT in
5549 old_point and then setting it later back into PT. When the
5550 current-selected-window and the final-selected-window both show
5551 the current buffer, this suffers from the problem that the
5552 current PT is the window-point of the current-selected-window,
5553 while the final PT is the point of the final-selected-window, so
5554 this copy from one PT to the other would end up moving the
5555 window-point of the final-selected-window to the window-point of
5556 the current-selected-window. So we have to be careful which
5557 point of the current-buffer we copy into old_point. */
5558 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5559 && WINDOWP (selected_window)
5560 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5561 && !EQ (selected_window, data->current_window))
5562 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5563 else
5564 old_point = PT;
5565 else
5566 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5567 point in new_current_buffer as of the last time this buffer was
5568 used. This can be non-deterministic since it can be changed by
5569 things like jit-lock by mere temporary selection of some random
5570 window that happens to show this buffer.
5571 So if possible we want this arbitrary choice of "which point" to
5572 be the one from the to-be-selected-window so as to prevent this
5573 window's cursor from being copied from another window. */
5574 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5575 /* If current_window = selected_window, its point is in BUF_PT. */
5576 && !EQ (selected_window, data->current_window))
5577 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5578 else
5579 old_point = BUF_PT (XBUFFER (new_current_buffer));
5582 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5583 f = XFRAME (frame);
5585 /* If f is a dead frame, don't bother rebuilding its window tree.
5586 However, there is other stuff we should still try to do below. */
5587 if (FRAME_LIVE_P (f))
5589 Lisp_Object window;
5590 Lisp_Object dead_windows = Qnil;
5591 register Lisp_Object tem, par, pers;
5592 register struct window *w;
5593 register struct saved_window *p;
5594 struct window *root_window;
5595 struct window **leaf_windows;
5596 int n_leaf_windows;
5597 ptrdiff_t k;
5598 int i, n;
5600 /* If the frame has been resized since this window configuration was
5601 made, we change the frame to the size specified in the
5602 configuration, restore the configuration, and then resize it
5603 back. We keep track of the prevailing height in these variables. */
5604 int previous_frame_lines = FRAME_LINES (f);
5605 int previous_frame_cols = FRAME_COLS (f);
5606 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5607 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5609 /* Don't do this within the main loop below: This may call Lisp
5610 code and is thus potentially unsafe while input is blocked. */
5611 for (k = 0; k < saved_windows->header.size; k++)
5613 p = SAVED_WINDOW_N (saved_windows, k);
5614 window = p->window;
5615 w = XWINDOW (window);
5616 if (!NILP (w->buffer)
5617 && !EQ (w->buffer, p->buffer)
5618 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5619 /* If a window we restore gets another buffer, record the
5620 window's old buffer. */
5621 call1 (Qrecord_window_buffer, window);
5624 /* The mouse highlighting code could get screwed up
5625 if it runs during this. */
5626 block_input ();
5628 if (data->frame_lines != previous_frame_lines
5629 || data->frame_cols != previous_frame_cols)
5630 change_frame_size (f, data->frame_lines,
5631 data->frame_cols, 0, 0, 0);
5632 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5633 if (data->frame_menu_bar_lines
5634 != previous_frame_menu_bar_lines)
5635 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5636 make_number (0));
5637 #ifdef HAVE_WINDOW_SYSTEM
5638 if (data->frame_tool_bar_lines
5639 != previous_frame_tool_bar_lines)
5640 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5641 make_number (0));
5642 #endif
5643 #endif
5645 /* "Swap out" point from the selected window's buffer
5646 into the window itself. (Normally the pointm of the selected
5647 window holds garbage.) We do this now, before
5648 restoring the window contents, and prevent it from
5649 being done later on when we select a new window. */
5650 if (! NILP (XWINDOW (selected_window)->buffer))
5652 w = XWINDOW (selected_window);
5653 set_marker_both (w->pointm,
5654 w->buffer,
5655 BUF_PT (XBUFFER (w->buffer)),
5656 BUF_PT_BYTE (XBUFFER (w->buffer)));
5659 windows_or_buffers_changed++;
5660 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5662 /* Problem: Freeing all matrices and later allocating them again
5663 is a serious redisplay flickering problem. What we would
5664 really like to do is to free only those matrices not reused
5665 below. */
5666 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5667 leaf_windows = alloca (count_windows (root_window)
5668 * sizeof *leaf_windows);
5669 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5671 /* Kludge Alert!
5672 Mark all windows now on frame as "deleted".
5673 Restoring the new configuration "undeletes" any that are in it.
5675 Save their current buffers in their height fields, since we may
5676 need it later, if a buffer saved in the configuration is now
5677 dead. */
5678 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5680 for (k = 0; k < saved_windows->header.size; k++)
5682 p = SAVED_WINDOW_N (saved_windows, k);
5683 window = p->window;
5684 w = XWINDOW (window);
5685 wset_next (w, Qnil);
5687 if (!NILP (p->parent))
5688 wset_parent
5689 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
5690 else
5691 wset_parent (w, Qnil);
5693 if (!NILP (p->prev))
5695 wset_prev
5696 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
5697 wset_next (XWINDOW (w->prev), p->window);
5699 else
5701 wset_prev (w, Qnil);
5702 if (!NILP (w->parent))
5704 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5706 wset_vchild (XWINDOW (w->parent), p->window);
5707 wset_hchild (XWINDOW (w->parent), Qnil);
5709 else
5711 wset_hchild (XWINDOW (w->parent), p->window);
5712 wset_vchild (XWINDOW (w->parent), Qnil);
5717 /* If we squirreled away the buffer in the window's height,
5718 restore it now. */
5719 if (BUFFERP (w->total_lines))
5720 wset_buffer (w, w->total_lines);
5721 wset_left_col (w, p->left_col);
5722 wset_top_line (w, p->top_line);
5723 wset_total_cols (w, p->total_cols);
5724 wset_total_lines (w, p->total_lines);
5725 wset_normal_cols (w, p->normal_cols);
5726 wset_normal_lines (w, p->normal_lines);
5727 w->hscroll = XFASTINT (p->hscroll);
5728 w->min_hscroll = XFASTINT (p->min_hscroll);
5729 wset_display_table (w, p->display_table);
5730 wset_left_margin_cols (w, p->left_margin_cols);
5731 wset_right_margin_cols (w, p->right_margin_cols);
5732 wset_left_fringe_width (w, p->left_fringe_width);
5733 wset_right_fringe_width (w, p->right_fringe_width);
5734 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
5735 wset_scroll_bar_width (w, p->scroll_bar_width);
5736 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
5737 wset_dedicated (w, p->dedicated);
5738 wset_combination_limit (w, p->combination_limit);
5739 /* Restore any window parameters that have been saved.
5740 Parameters that have not been saved are left alone. */
5741 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5743 pers = XCAR (tem);
5744 if (CONSP (pers))
5746 if (NILP (XCDR (pers)))
5748 par = Fassq (XCAR (pers), w->window_parameters);
5749 if (CONSP (par) && !NILP (XCDR (par)))
5750 /* Reset a parameter to nil if and only if it
5751 has a non-nil association. Don't make new
5752 associations. */
5753 Fsetcdr (par, Qnil);
5755 else
5756 /* Always restore a non-nil value. */
5757 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5761 w->last_modified = 0;
5762 w->last_overlay_modified = 0;
5764 /* Reinstall the saved buffer and pointers into it. */
5765 if (NILP (p->buffer))
5766 /* An internal window. */
5767 wset_buffer (w, p->buffer);
5768 else if (BUFFER_LIVE_P (XBUFFER (p->buffer)))
5769 /* If saved buffer is alive, install it. */
5771 wset_buffer (w, p->buffer);
5772 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5773 set_marker_restricted (w->start, p->start, w->buffer);
5774 set_marker_restricted (w->pointm, p->pointm,
5775 w->buffer);
5776 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
5777 p->mark, w->buffer);
5779 /* As documented in Fcurrent_window_configuration, don't
5780 restore the location of point in the buffer which was
5781 current when the window configuration was recorded. */
5782 if (!EQ (p->buffer, new_current_buffer)
5783 && XBUFFER (p->buffer) == current_buffer)
5784 Fgoto_char (w->pointm);
5786 else if (!NILP (w->buffer)
5787 && BUFFER_LIVE_P (XBUFFER (w->buffer)))
5788 /* Keep window's old buffer; make sure the markers are
5789 real. */
5791 /* Set window markers at start of visible range. */
5792 if (XMARKER (w->start)->buffer == 0)
5793 set_marker_restricted (w->start, make_number (0),
5794 w->buffer);
5795 if (XMARKER (w->pointm)->buffer == 0)
5796 set_marker_restricted_both
5797 (w->pointm, w->buffer,
5798 BUF_PT (XBUFFER (w->buffer)),
5799 BUF_PT_BYTE (XBUFFER (w->buffer)));
5800 w->start_at_line_beg = 1;
5802 else
5803 /* Window has no live buffer, get one. */
5805 /* Get the buffer via other_buffer_safely in order to
5806 avoid showing an unimportant buffer and, if necessary, to
5807 recreate *scratch* in the course (part of Juanma's bs-show
5808 scenario from March 2011). */
5809 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
5810 /* This will set the markers to beginning of visible
5811 range. */
5812 set_marker_restricted (w->start,
5813 make_number (0), w->buffer);
5814 set_marker_restricted (w->pointm,
5815 make_number (0), w->buffer);
5816 w->start_at_line_beg = 1;
5817 if (!NILP (w->dedicated))
5818 /* Record this window as dead. */
5819 dead_windows = Fcons (window, dead_windows);
5820 /* Make sure window is no more dedicated. */
5821 wset_dedicated (w, Qnil);
5825 fset_root_window (f, data->root_window);
5826 /* Arrange *not* to restore point in the buffer that was
5827 current when the window configuration was saved. */
5828 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5829 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5830 make_number (old_point),
5831 XWINDOW (data->current_window)->buffer);
5833 /* In the following call to `select-window', prevent "swapping out
5834 point" in the old selected window using the buffer that has
5835 been restored into it. We already swapped out that point from
5836 that window's old buffer. */
5837 select_window (data->current_window, Qnil, 1);
5838 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
5839 = selected_window;
5841 if (NILP (data->focus_frame)
5842 || (FRAMEP (data->focus_frame)
5843 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5844 Fredirect_frame_focus (frame, data->focus_frame);
5846 /* Set the screen height to the value it had before this function. */
5847 if (previous_frame_lines != FRAME_LINES (f)
5848 || previous_frame_cols != FRAME_COLS (f))
5849 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5850 0, 0, 0);
5851 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5852 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5853 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5854 make_number (0));
5855 #ifdef HAVE_WINDOW_SYSTEM
5856 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5857 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5858 make_number (0));
5859 #endif
5860 #endif
5862 /* Now, free glyph matrices in windows that were not reused. */
5863 for (i = n = 0; i < n_leaf_windows; ++i)
5865 if (NILP (leaf_windows[i]->buffer))
5867 /* Assert it's not reused as a combination. */
5868 eassert (NILP (leaf_windows[i]->hchild)
5869 && NILP (leaf_windows[i]->vchild));
5870 free_window_matrices (leaf_windows[i]);
5872 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5873 ++n;
5876 adjust_glyphs (f);
5877 unblock_input ();
5879 /* Scan dead buffer windows. */
5880 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5882 window = XCAR (dead_windows);
5883 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5884 delete_deletable_window (window);
5887 /* Fselect_window will have made f the selected frame, so we
5888 reselect the proper frame here. Fhandle_switch_frame will change the
5889 selected window too, but that doesn't make the call to
5890 Fselect_window above totally superfluous; it still sets f's
5891 selected window. */
5892 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5893 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5895 run_window_configuration_change_hook (f);
5898 if (!NILP (new_current_buffer))
5900 Fset_buffer (new_current_buffer);
5901 /* If the new current buffer doesn't appear in the selected
5902 window, go to its old point (see bug#12208). */
5903 if (!EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5904 Fgoto_char (make_number (old_point));
5907 Vminibuf_scroll_window = data->minibuf_scroll_window;
5908 minibuf_selected_window = data->minibuf_selected_window;
5910 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5914 /* Recursively delete all child windows reachable via the next, vchild,
5915 and hchild slots of WINDOW. */
5916 void
5917 delete_all_child_windows (Lisp_Object window)
5919 register struct window *w;
5921 w = XWINDOW (window);
5923 if (!NILP (w->next))
5924 /* Delete WINDOW's siblings (we traverse postorderly). */
5925 delete_all_child_windows (w->next);
5927 /* See Fset_window_configuration for excuse. */
5928 wset_total_lines (w, w->buffer);
5930 if (!NILP (w->vchild))
5932 delete_all_child_windows (w->vchild);
5933 wset_vchild (w, Qnil);
5935 else if (!NILP (w->hchild))
5937 delete_all_child_windows (w->hchild);
5938 wset_hchild (w, Qnil);
5940 else if (!NILP (w->buffer))
5942 unshow_buffer (w);
5943 unchain_marker (XMARKER (w->pointm));
5944 unchain_marker (XMARKER (w->start));
5945 wset_buffer (w, Qnil);
5948 Vwindow_list = Qnil;
5951 static int
5952 count_windows (register struct window *window)
5954 register int count = 1;
5955 if (!NILP (window->next))
5956 count += count_windows (XWINDOW (window->next));
5957 if (!NILP (window->vchild))
5958 count += count_windows (XWINDOW (window->vchild));
5959 if (!NILP (window->hchild))
5960 count += count_windows (XWINDOW (window->hchild));
5961 return count;
5965 /* Fill vector FLAT with leaf windows under W, starting at index I.
5966 Value is last index + 1. */
5967 static int
5968 get_leaf_windows (struct window *w, struct window **flat, int i)
5970 while (w)
5972 if (!NILP (w->hchild))
5973 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5974 else if (!NILP (w->vchild))
5975 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5976 else
5977 flat[i++] = w;
5979 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5982 return i;
5986 /* Return a pointer to the glyph W's physical cursor is on. Value is
5987 null if W's current matrix is invalid, so that no meaningful glyph
5988 can be returned. */
5989 struct glyph *
5990 get_phys_cursor_glyph (struct window *w)
5992 struct glyph_row *row;
5993 struct glyph *glyph;
5994 int hpos = w->phys_cursor.hpos;
5996 if (!(w->phys_cursor.vpos >= 0
5997 && w->phys_cursor.vpos < w->current_matrix->nrows))
5998 return NULL;
6000 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6001 if (!row->enabled_p)
6002 return NULL;
6004 if (w->hscroll)
6006 /* When the window is hscrolled, cursor hpos can legitimately be
6007 out of bounds, but we draw the cursor at the corresponding
6008 window margin in that case. */
6009 if (!row->reversed_p && hpos < 0)
6010 hpos = 0;
6011 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6012 hpos = row->used[TEXT_AREA] - 1;
6015 if (row->used[TEXT_AREA] > hpos
6016 && 0 <= hpos)
6017 glyph = row->glyphs[TEXT_AREA] + hpos;
6018 else
6019 glyph = NULL;
6021 return glyph;
6025 static int
6026 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6028 register struct saved_window *p;
6029 register struct window *w;
6030 register Lisp_Object tem, pers, par;
6032 for (;!NILP (window); window = w->next)
6034 p = SAVED_WINDOW_N (vector, i);
6035 w = XWINDOW (window);
6037 wset_temslot (w, make_number (i)); i++;
6038 p->window = window;
6039 p->buffer = w->buffer;
6040 p->left_col = w->left_col;
6041 p->top_line = w->top_line;
6042 p->total_cols = w->total_cols;
6043 p->total_lines = w->total_lines;
6044 p->normal_cols = w->normal_cols;
6045 p->normal_lines = w->normal_lines;
6046 XSETFASTINT (p->hscroll, w->hscroll);
6047 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6048 p->display_table = w->display_table;
6049 p->left_margin_cols = w->left_margin_cols;
6050 p->right_margin_cols = w->right_margin_cols;
6051 p->left_fringe_width = w->left_fringe_width;
6052 p->right_fringe_width = w->right_fringe_width;
6053 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6054 p->scroll_bar_width = w->scroll_bar_width;
6055 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6056 p->dedicated = w->dedicated;
6057 p->combination_limit = w->combination_limit;
6058 p->window_parameters = Qnil;
6060 if (!NILP (Vwindow_persistent_parameters))
6062 /* Run cycle detection on Vwindow_persistent_parameters. */
6063 Lisp_Object tortoise, hare;
6065 hare = tortoise = Vwindow_persistent_parameters;
6066 while (CONSP (hare))
6068 hare = XCDR (hare);
6069 if (!CONSP (hare))
6070 break;
6072 hare = XCDR (hare);
6073 tortoise = XCDR (tortoise);
6075 if (EQ (hare, tortoise))
6076 /* Reset Vwindow_persistent_parameters to Qnil. */
6078 Vwindow_persistent_parameters = Qnil;
6079 break;
6083 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6084 tem = XCDR (tem))
6086 pers = XCAR (tem);
6087 /* Save values for persistent window parameters. */
6088 if (CONSP (pers) && !NILP (XCDR (pers)))
6090 par = Fassq (XCAR (pers), w->window_parameters);
6091 if (NILP (par))
6092 /* If the window has no value for the parameter,
6093 make one. */
6094 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6095 p->window_parameters);
6096 else
6097 /* If the window has a value for the parameter,
6098 save it. */
6099 p->window_parameters = Fcons (Fcons (XCAR (par),
6100 XCDR (par)),
6101 p->window_parameters);
6106 if (!NILP (w->buffer))
6108 /* Save w's value of point in the window configuration. If w
6109 is the selected window, then get the value of point from
6110 the buffer; pointm is garbage in the selected window. */
6111 if (EQ (window, selected_window))
6112 p->pointm = build_marker (XBUFFER (w->buffer),
6113 BUF_PT (XBUFFER (w->buffer)),
6114 BUF_PT_BYTE (XBUFFER (w->buffer)));
6115 else
6116 p->pointm = Fcopy_marker (w->pointm, Qnil);
6117 XMARKER (p->pointm)->insertion_type
6118 = !NILP (Vwindow_point_insertion_type);
6120 p->start = Fcopy_marker (w->start, Qnil);
6121 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6123 tem = BVAR (XBUFFER (w->buffer), mark);
6124 p->mark = Fcopy_marker (tem, Qnil);
6126 else
6128 p->pointm = Qnil;
6129 p->start = Qnil;
6130 p->mark = Qnil;
6131 p->start_at_line_beg = Qnil;
6134 if (NILP (w->parent))
6135 p->parent = Qnil;
6136 else
6137 p->parent = XWINDOW (w->parent)->temslot;
6139 if (NILP (w->prev))
6140 p->prev = Qnil;
6141 else
6142 p->prev = XWINDOW (w->prev)->temslot;
6144 if (!NILP (w->vchild))
6145 i = save_window_save (w->vchild, vector, i);
6146 if (!NILP (w->hchild))
6147 i = save_window_save (w->hchild, vector, i);
6150 return i;
6153 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6154 Scurrent_window_configuration, 0, 1, 0,
6155 doc: /* Return an object representing the current window configuration of FRAME.
6156 If FRAME is nil or omitted, use the selected frame.
6157 This describes the number of windows, their sizes and current buffers,
6158 and for each displayed buffer, where display starts, and the positions of
6159 point and mark. An exception is made for point in the current buffer:
6160 its value is -not- saved.
6161 This also records the currently selected frame, and FRAME's focus
6162 redirection (see `redirect-frame-focus'). The variable
6163 `window-persistent-parameters' specifies which window parameters are
6164 saved by this function. */)
6165 (Lisp_Object frame)
6167 register Lisp_Object tem;
6168 register int n_windows;
6169 register struct save_window_data *data;
6170 register int i;
6171 FRAME_PTR f;
6173 if (NILP (frame))
6174 frame = selected_frame;
6175 CHECK_LIVE_FRAME (frame);
6176 f = XFRAME (frame);
6178 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6179 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6180 PVEC_WINDOW_CONFIGURATION);
6182 data->frame_cols = FRAME_COLS (f);
6183 data->frame_lines = FRAME_LINES (f);
6184 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6185 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6186 data->selected_frame = selected_frame;
6187 data->current_window = FRAME_SELECTED_WINDOW (f);
6188 XSETBUFFER (data->current_buffer, current_buffer);
6189 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6190 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6191 data->root_window = FRAME_ROOT_WINDOW (f);
6192 data->focus_frame = FRAME_FOCUS_FRAME (f);
6193 tem = Fmake_vector (make_number (n_windows), Qnil);
6194 data->saved_windows = tem;
6195 for (i = 0; i < n_windows; i++)
6196 ASET (tem, i,
6197 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6198 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6199 XSETWINDOW_CONFIGURATION (tem, data);
6200 return (tem);
6203 /***********************************************************************
6204 Marginal Areas
6205 ***********************************************************************/
6207 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6208 2, 3, 0,
6209 doc: /* Set width of marginal areas of window WINDOW.
6210 WINDOW must be a live window and defaults to the selected one.
6212 Second arg LEFT-WIDTH specifies the number of character cells to
6213 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6214 does the same for the right marginal area. A nil width parameter
6215 means no margin. */)
6216 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6218 struct window *w = decode_live_window (window);
6220 /* Translate negative or zero widths to nil.
6221 Margins that are too wide have to be checked elsewhere. */
6223 if (!NILP (left_width))
6225 CHECK_NUMBER (left_width);
6226 if (XINT (left_width) <= 0)
6227 left_width = Qnil;
6230 if (!NILP (right_width))
6232 CHECK_NUMBER (right_width);
6233 if (XINT (right_width) <= 0)
6234 right_width = Qnil;
6237 if (!EQ (w->left_margin_cols, left_width)
6238 || !EQ (w->right_margin_cols, right_width))
6240 wset_left_margin_cols (w, left_width);
6241 wset_right_margin_cols (w, right_width);
6243 adjust_window_margins (w);
6245 ++windows_or_buffers_changed;
6246 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6249 return Qnil;
6253 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6254 0, 1, 0,
6255 doc: /* Get width of marginal areas of window WINDOW.
6256 WINDOW must be a live window and defaults to the selected one.
6258 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6259 If a marginal area does not exist, its width will be returned
6260 as nil. */)
6261 (Lisp_Object window)
6263 struct window *w = decode_live_window (window);
6264 return Fcons (w->left_margin_cols, w->right_margin_cols);
6269 /***********************************************************************
6270 Fringes
6271 ***********************************************************************/
6273 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6274 2, 4, 0,
6275 doc: /* Set the fringe widths of window WINDOW.
6276 WINDOW must be a live window and defaults to the selected one.
6278 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6279 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6280 fringe width. If a fringe width arg is nil, that means to use the
6281 frame's default fringe width. Default fringe widths can be set with
6282 the command `set-fringe-style'.
6283 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6284 outside of the display margins. By default, fringes are drawn between
6285 display marginal areas and the text area. */)
6286 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6288 struct window *w = decode_live_window (window);
6289 int outside = !NILP (outside_margins);
6291 if (!NILP (left_width))
6292 CHECK_NATNUM (left_width);
6293 if (!NILP (right_width))
6294 CHECK_NATNUM (right_width);
6296 /* Do nothing on a tty. */
6297 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6298 && (!EQ (w->left_fringe_width, left_width)
6299 || !EQ (w->right_fringe_width, right_width)
6300 || w->fringes_outside_margins != outside))
6302 wset_left_fringe_width (w, left_width);
6303 wset_right_fringe_width (w, right_width);
6304 w->fringes_outside_margins = outside;
6306 adjust_window_margins (w);
6308 clear_glyph_matrix (w->current_matrix);
6309 wset_window_end_valid (w, Qnil);
6311 ++windows_or_buffers_changed;
6312 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6315 return Qnil;
6319 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6320 0, 1, 0,
6321 doc: /* Get width of fringes of window WINDOW.
6322 WINDOW must be a live window and defaults to the selected one.
6324 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6325 (Lisp_Object window)
6327 struct window *w = decode_live_window (window);
6329 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6330 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6331 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6332 ? Qt : Qnil), Qnil)));
6337 /***********************************************************************
6338 Scroll bars
6339 ***********************************************************************/
6341 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6342 Sset_window_scroll_bars, 2, 4, 0,
6343 doc: /* Set width and type of scroll bars of window WINDOW.
6344 WINDOW must be a live window and defaults to the selected one.
6346 Second parameter WIDTH specifies the pixel width for the scroll bar;
6347 this is automatically adjusted to a multiple of the frame column width.
6348 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6349 bar: left, right, or nil.
6350 If WIDTH is nil, use the frame's scroll-bar width.
6351 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6352 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6353 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6355 struct window *w = decode_live_window (window);
6357 if (!NILP (width))
6359 CHECK_RANGED_INTEGER (width, 0, INT_MAX);
6361 if (XINT (width) == 0)
6362 vertical_type = Qnil;
6365 if (!(NILP (vertical_type)
6366 || EQ (vertical_type, Qleft)
6367 || EQ (vertical_type, Qright)
6368 || EQ (vertical_type, Qt)))
6369 error ("Invalid type of vertical scroll bar");
6371 if (!EQ (w->scroll_bar_width, width)
6372 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6374 wset_scroll_bar_width (w, width);
6375 wset_vertical_scroll_bar_type (w, vertical_type);
6377 adjust_window_margins (w);
6379 clear_glyph_matrix (w->current_matrix);
6380 wset_window_end_valid (w, Qnil);
6382 ++windows_or_buffers_changed;
6383 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6386 return Qnil;
6390 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6391 0, 1, 0,
6392 doc: /* Get width and type of scroll bars of window WINDOW.
6393 WINDOW must be a live window and defaults to the selected one.
6395 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6396 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6397 value. */)
6398 (Lisp_Object window)
6400 struct window *w = decode_live_window (window);
6401 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6402 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6403 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6404 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6405 Fcons (w->vertical_scroll_bar_type,
6406 Fcons (Qnil, Qnil))));
6411 /***********************************************************************
6412 Smooth scrolling
6413 ***********************************************************************/
6415 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6416 doc: /* Return the amount by which WINDOW is scrolled vertically.
6417 If WINDOW is omitted or nil, it defaults to the selected window.
6418 Normally, value is a multiple of the canonical character height of WINDOW;
6419 optional second arg PIXELS-P means value is measured in pixels. */)
6420 (Lisp_Object window, Lisp_Object pixels_p)
6422 Lisp_Object result;
6423 struct window *w = decode_live_window (window);
6424 struct frame *f = XFRAME (w->frame);
6426 if (FRAME_WINDOW_P (f))
6427 result = (NILP (pixels_p)
6428 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6429 : make_number (-w->vscroll));
6430 else
6431 result = make_number (0);
6432 return result;
6436 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6437 2, 3, 0,
6438 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6439 WINDOW nil means use the selected window. Normally, VSCROLL is a
6440 non-negative multiple of the canonical character height of WINDOW;
6441 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6442 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6443 corresponds to an integral number of pixels. The return value is the
6444 result of this rounding.
6445 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6446 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6448 struct window *w = decode_live_window (window);
6449 struct frame *f = XFRAME (w->frame);
6451 CHECK_NUMBER_OR_FLOAT (vscroll);
6453 if (FRAME_WINDOW_P (f))
6455 int old_dy = w->vscroll;
6457 w->vscroll = - (NILP (pixels_p)
6458 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6459 : XFLOATINT (vscroll));
6460 w->vscroll = min (w->vscroll, 0);
6462 if (w->vscroll != old_dy)
6464 /* Adjust glyph matrix of the frame if the virtual display
6465 area becomes larger than before. */
6466 if (w->vscroll < 0 && w->vscroll < old_dy)
6467 adjust_glyphs (f);
6469 /* Prevent redisplay shortcuts. */
6470 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6474 return Fwindow_vscroll (window, pixels_p);
6478 /* Call FN for all leaf windows on frame F. FN is called with the
6479 first argument being a pointer to the leaf window, and with
6480 additional argument USER_DATA. Stops when FN returns 0. */
6482 static void
6483 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6484 void *user_data)
6486 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6487 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6488 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6492 /* Helper function for foreach_window. Call FN for all leaf windows
6493 reachable from W. FN is called with the first argument being a
6494 pointer to the leaf window, and with additional argument USER_DATA.
6495 Stop when FN returns 0. Value is 0 if stopped by FN. */
6497 static int
6498 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6500 int cont;
6502 for (cont = 1; w && cont;)
6504 if (!NILP (w->hchild))
6505 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6506 else if (!NILP (w->vchild))
6507 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6508 else
6509 cont = fn (w, user_data);
6511 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6514 return cont;
6518 /* Freeze or unfreeze the window start of W unless it is a
6519 mini-window or the selected window. FREEZE_P non-null means freeze
6520 the window start. */
6522 static int
6523 freeze_window_start (struct window *w, void *freeze_p)
6525 if (MINI_WINDOW_P (w)
6526 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6527 && (w == XWINDOW (selected_window)
6528 || (MINI_WINDOW_P (XWINDOW (selected_window))
6529 && ! NILP (Vminibuf_scroll_window)
6530 && w == XWINDOW (Vminibuf_scroll_window)))))
6531 freeze_p = NULL;
6533 w->frozen_window_start_p = freeze_p != NULL;
6534 return 1;
6538 /* Freeze or unfreeze the window starts of all leaf windows on frame
6539 F, except the selected window and a mini-window. FREEZE_P non-zero
6540 means freeze the window start. */
6542 void
6543 freeze_window_starts (struct frame *f, int freeze_p)
6545 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6549 /***********************************************************************
6550 Initialization
6551 ***********************************************************************/
6553 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6554 describe the same state of affairs. This is used by Fequal.
6556 IGNORE_POSITIONS means ignore non-matching scroll positions
6557 and the like.
6559 This ignores a couple of things like the dedication status of
6560 window, combination_limit and the like. This might have to be
6561 fixed. */
6563 bool
6564 compare_window_configurations (Lisp_Object configuration1,
6565 Lisp_Object configuration2,
6566 bool ignore_positions)
6568 register struct save_window_data *d1, *d2;
6569 struct Lisp_Vector *sws1, *sws2;
6570 ptrdiff_t i;
6572 CHECK_WINDOW_CONFIGURATION (configuration1);
6573 CHECK_WINDOW_CONFIGURATION (configuration2);
6575 d1 = (struct save_window_data *) XVECTOR (configuration1);
6576 d2 = (struct save_window_data *) XVECTOR (configuration2);
6577 sws1 = XVECTOR (d1->saved_windows);
6578 sws2 = XVECTOR (d2->saved_windows);
6580 /* Frame settings must match. */
6581 if (d1->frame_cols != d2->frame_cols
6582 || d1->frame_lines != d2->frame_lines
6583 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6584 || !EQ (d1->selected_frame, d2->selected_frame)
6585 || !EQ (d1->current_buffer, d2->current_buffer)
6586 || (!ignore_positions
6587 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6588 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6589 || !EQ (d1->focus_frame, d2->focus_frame)
6590 /* Verify that the two configurations have the same number of windows. */
6591 || sws1->header.size != sws2->header.size)
6592 return 0;
6594 for (i = 0; i < sws1->header.size; i++)
6596 struct saved_window *sw1, *sw2;
6598 sw1 = SAVED_WINDOW_N (sws1, i);
6599 sw2 = SAVED_WINDOW_N (sws2, i);
6601 if (
6602 /* The "current" windows in the two configurations must
6603 correspond to each other. */
6604 EQ (d1->current_window, sw1->window)
6605 != EQ (d2->current_window, sw2->window)
6606 /* Windows' buffers must match. */
6607 || !EQ (sw1->buffer, sw2->buffer)
6608 || !EQ (sw1->left_col, sw2->left_col)
6609 || !EQ (sw1->top_line, sw2->top_line)
6610 || !EQ (sw1->total_cols, sw2->total_cols)
6611 || !EQ (sw1->total_lines, sw2->total_lines)
6612 || !EQ (sw1->display_table, sw2->display_table)
6613 /* The next two disjuncts check the window structure for
6614 equality. */
6615 || !EQ (sw1->parent, sw2->parent)
6616 || !EQ (sw1->prev, sw2->prev)
6617 || (!ignore_positions
6618 && (!EQ (sw1->hscroll, sw2->hscroll)
6619 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6620 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6621 || NILP (Fequal (sw1->start, sw2->start))
6622 || NILP (Fequal (sw1->pointm, sw2->pointm))
6623 || NILP (Fequal (sw1->mark, sw2->mark))))
6624 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6625 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6626 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6627 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6628 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6629 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6630 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6631 return 0;
6634 return 1;
6637 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6638 Scompare_window_configurations, 2, 2, 0,
6639 doc: /* Compare two window configurations as regards the structure of windows.
6640 This function ignores details such as the values of point and mark
6641 and scrolling positions. */)
6642 (Lisp_Object x, Lisp_Object y)
6644 if (compare_window_configurations (x, y, 1))
6645 return Qt;
6646 return Qnil;
6649 void
6650 init_window_once (void)
6652 struct frame *f = make_initial_frame ();
6653 XSETFRAME (selected_frame, f);
6654 Vterminal_frame = selected_frame;
6655 minibuf_window = f->minibuffer_window;
6656 selected_window = f->selected_window;
6657 last_nonminibuf_frame = f;
6659 window_initialized = 1;
6662 void
6663 init_window (void)
6665 Vwindow_list = Qnil;
6668 void
6669 syms_of_window (void)
6671 DEFSYM (Qscroll_up, "scroll-up");
6672 DEFSYM (Qscroll_down, "scroll-down");
6673 DEFSYM (Qscroll_command, "scroll-command");
6675 Fput (Qscroll_up, Qscroll_command, Qt);
6676 Fput (Qscroll_down, Qscroll_command, Qt);
6678 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6679 DEFSYM (Qwindowp, "windowp");
6680 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6681 DEFSYM (Qwindow_live_p, "window-live-p");
6682 DEFSYM (Qwindow_valid_p, "window-valid-p");
6683 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6684 DEFSYM (Qdelete_window, "delete-window");
6685 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6686 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6687 DEFSYM (Qsafe, "safe");
6688 DEFSYM (Qdisplay_buffer, "display-buffer");
6689 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6690 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6691 DEFSYM (Qget_mru_window, "get-mru-window");
6692 DEFSYM (Qwindow_size, "window-size");
6693 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6694 DEFSYM (Qabove, "above");
6695 DEFSYM (Qbelow, "below");
6696 DEFSYM (Qclone_of, "clone-of");
6698 staticpro (&Vwindow_list);
6700 minibuf_selected_window = Qnil;
6701 staticpro (&minibuf_selected_window);
6703 window_scroll_pixel_based_preserve_x = -1;
6704 window_scroll_pixel_based_preserve_y = -1;
6705 window_scroll_preserve_hpos = -1;
6706 window_scroll_preserve_vpos = -1;
6708 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6709 doc: /* Non-nil means call as function to display a help buffer.
6710 The function is called with one argument, the buffer to be displayed.
6711 Used by `with-output-to-temp-buffer'.
6712 If this function is used, then it must do the entire job of showing
6713 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6714 Vtemp_buffer_show_function = Qnil;
6716 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6717 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6718 Vminibuf_scroll_window = Qnil;
6720 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6721 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6722 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6723 is displayed in the `mode-line' face. */);
6724 mode_line_in_non_selected_windows = 1;
6726 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6727 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6728 Vother_window_scroll_buffer = Qnil;
6730 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6731 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6732 auto_window_vscroll_p = 1;
6734 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6735 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6736 next_screen_context_lines = 2;
6738 DEFVAR_LISP ("scroll-preserve-screen-position",
6739 Vscroll_preserve_screen_position,
6740 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6741 A value of nil means point does not keep its screen position except
6742 at the scroll margin or window boundary respectively.
6743 A value of t means point keeps its screen position if the scroll
6744 command moved it vertically out of the window, e.g. when scrolling
6745 by full screens.
6746 Any other value means point always keeps its screen position.
6747 Scroll commands should have the `scroll-command' property
6748 on their symbols to be controlled by this variable. */);
6749 Vscroll_preserve_screen_position = Qnil;
6751 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6752 doc: /* Type of marker to use for `window-point'. */);
6753 Vwindow_point_insertion_type = Qnil;
6755 DEFVAR_LISP ("window-configuration-change-hook",
6756 Vwindow_configuration_change_hook,
6757 doc: /* Functions to call when window configuration changes.
6758 The buffer-local part is run once per window, with the relevant window
6759 selected; while the global part is run only once for the modified frame,
6760 with the relevant frame selected. */);
6761 Vwindow_configuration_change_hook = Qnil;
6763 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6764 doc: /* Non-nil means `recenter' redraws entire frame.
6765 If this option is non-nil, then the `recenter' command with a nil
6766 argument will redraw the entire frame; the special value `tty' causes
6767 the frame to be redrawn only if it is a tty frame. */);
6768 Vrecenter_redisplay = Qtty;
6770 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6771 doc: /* If t, resize window combinations proportionally.
6772 If this variable is nil, splitting a window gets the entire screen space
6773 for displaying the new window from the window to split. Deleting and
6774 resizing a window preferably resizes one adjacent window only.
6776 If this variable is t, splitting a window tries to get the space
6777 proportionally from all windows in the same combination. This also
6778 allows to split a window that is otherwise too small or of fixed size.
6779 Resizing and deleting a window proportionally resize all windows in the
6780 same combination.
6782 Other values are reserved for future use.
6784 This variable takes no effect if `window-combination-limit' is non-nil. */);
6785 Vwindow_combination_resize = Qnil;
6787 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6788 doc: /* If non-nil, splitting a window makes a new parent window.
6789 The following values are recognized:
6791 nil means splitting a window will create a new parent window only if the
6792 window has no parent window or the window shall become part of a
6793 combination orthogonal to the one it is part of.
6795 `window-size' means that splitting a window for displaying a buffer
6796 makes a new parent window provided `display-buffer' is supposed to
6797 explicitly set the window's size due to the presence of a
6798 `window-height' or `window-width' entry in the alist used by
6799 `display-buffer'. Otherwise, this value is handled like nil.
6801 `temp-buffer' means that splitting a window for displaying a temporary
6802 buffer always makes a new parent window. Otherwise, this value is
6803 handled like nil.
6805 `display-buffer' means that splitting a window for displaying a buffer
6806 always makes a new parent window. Since temporary buffers are
6807 displayed by the function `display-buffer', this value is stronger
6808 than `temp-buffer'. Splitting a window for other purpose makes a
6809 new parent window only if needed.
6811 t means that splitting a window always creates a new parent window. If
6812 all splits behave this way, each frame's window tree is a binary
6813 tree and every window but the frame's root window has exactly one
6814 sibling.
6816 Other values are reserved for future use. */);
6817 Vwindow_combination_limit = Qwindow_size;
6819 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6820 doc: /* Alist of persistent window parameters.
6821 This alist specifies which window parameters shall get saved by
6822 `current-window-configuration' and `window-state-get' and subsequently
6823 restored to their previous values by `set-window-configuration' and
6824 `window-state-put'.
6826 The car of each entry of this alist is the symbol specifying the
6827 parameter. The cdr is one of the following:
6829 nil means the parameter is neither saved by `window-state-get' nor by
6830 `current-window-configuration'.
6832 t means the parameter is saved by `current-window-configuration' and,
6833 provided its WRITABLE argument is nil, by `window-state-get'.
6835 The symbol `writable' means the parameter is saved unconditionally by
6836 both `current-window-configuration' and `window-state-get'. Do not use
6837 this value for parameters without read syntax (like windows or frames).
6839 Parameters not saved by `current-window-configuration' or
6840 `window-state-get' are left alone by `set-window-configuration'
6841 respectively are not installed by `window-state-put'. */);
6842 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6844 defsubr (&Sselected_window);
6845 defsubr (&Sminibuffer_window);
6846 defsubr (&Swindow_minibuffer_p);
6847 defsubr (&Swindowp);
6848 defsubr (&Swindow_valid_p);
6849 defsubr (&Swindow_live_p);
6850 defsubr (&Swindow_frame);
6851 defsubr (&Sframe_root_window);
6852 defsubr (&Sframe_first_window);
6853 defsubr (&Sframe_selected_window);
6854 defsubr (&Sset_frame_selected_window);
6855 defsubr (&Spos_visible_in_window_p);
6856 defsubr (&Swindow_line_height);
6857 defsubr (&Swindow_buffer);
6858 defsubr (&Swindow_parent);
6859 defsubr (&Swindow_top_child);
6860 defsubr (&Swindow_left_child);
6861 defsubr (&Swindow_next_sibling);
6862 defsubr (&Swindow_prev_sibling);
6863 defsubr (&Swindow_combination_limit);
6864 defsubr (&Sset_window_combination_limit);
6865 defsubr (&Swindow_use_time);
6866 defsubr (&Swindow_top_line);
6867 defsubr (&Swindow_left_column);
6868 defsubr (&Swindow_total_height);
6869 defsubr (&Swindow_total_width);
6870 defsubr (&Swindow_normal_size);
6871 defsubr (&Swindow_new_total);
6872 defsubr (&Swindow_new_normal);
6873 defsubr (&Sset_window_new_total);
6874 defsubr (&Sset_window_new_normal);
6875 defsubr (&Swindow_resize_apply);
6876 defsubr (&Swindow_body_height);
6877 defsubr (&Swindow_body_width);
6878 defsubr (&Swindow_hscroll);
6879 defsubr (&Sset_window_hscroll);
6880 defsubr (&Swindow_redisplay_end_trigger);
6881 defsubr (&Sset_window_redisplay_end_trigger);
6882 defsubr (&Swindow_edges);
6883 defsubr (&Swindow_pixel_edges);
6884 defsubr (&Swindow_absolute_pixel_edges);
6885 defsubr (&Swindow_inside_edges);
6886 defsubr (&Swindow_inside_pixel_edges);
6887 defsubr (&Swindow_inside_absolute_pixel_edges);
6888 defsubr (&Scoordinates_in_window_p);
6889 defsubr (&Swindow_at);
6890 defsubr (&Swindow_point);
6891 defsubr (&Swindow_start);
6892 defsubr (&Swindow_end);
6893 defsubr (&Sset_window_point);
6894 defsubr (&Sset_window_start);
6895 defsubr (&Swindow_dedicated_p);
6896 defsubr (&Sset_window_dedicated_p);
6897 defsubr (&Swindow_display_table);
6898 defsubr (&Sset_window_display_table);
6899 defsubr (&Snext_window);
6900 defsubr (&Sprevious_window);
6901 defsubr (&Sget_buffer_window);
6902 defsubr (&Sdelete_other_windows_internal);
6903 defsubr (&Sdelete_window_internal);
6904 defsubr (&Sresize_mini_window_internal);
6905 defsubr (&Sset_window_buffer);
6906 defsubr (&Srun_window_configuration_change_hook);
6907 defsubr (&Sselect_window);
6908 defsubr (&Sforce_window_update);
6909 defsubr (&Ssplit_window_internal);
6910 defsubr (&Sscroll_up);
6911 defsubr (&Sscroll_down);
6912 defsubr (&Sscroll_left);
6913 defsubr (&Sscroll_right);
6914 defsubr (&Sother_window_for_scrolling);
6915 defsubr (&Sscroll_other_window);
6916 defsubr (&Sminibuffer_selected_window);
6917 defsubr (&Srecenter);
6918 defsubr (&Swindow_text_height);
6919 defsubr (&Smove_to_window_line);
6920 defsubr (&Swindow_configuration_p);
6921 defsubr (&Swindow_configuration_frame);
6922 defsubr (&Sset_window_configuration);
6923 defsubr (&Scurrent_window_configuration);
6924 defsubr (&Sset_window_margins);
6925 defsubr (&Swindow_margins);
6926 defsubr (&Sset_window_fringes);
6927 defsubr (&Swindow_fringes);
6928 defsubr (&Sset_window_scroll_bars);
6929 defsubr (&Swindow_scroll_bars);
6930 defsubr (&Swindow_vscroll);
6931 defsubr (&Sset_window_vscroll);
6932 defsubr (&Scompare_window_configurations);
6933 defsubr (&Swindow_list);
6934 defsubr (&Swindow_list_1);
6935 defsubr (&Swindow_prev_buffers);
6936 defsubr (&Sset_window_prev_buffers);
6937 defsubr (&Swindow_next_buffers);
6938 defsubr (&Sset_window_next_buffers);
6939 defsubr (&Swindow_parameters);
6940 defsubr (&Swindow_parameter);
6941 defsubr (&Sset_window_parameter);
6944 void
6945 keys_of_window (void)
6947 initial_define_key (control_x_map, '<', "scroll-left");
6948 initial_define_key (control_x_map, '>', "scroll-right");
6950 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6951 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6952 initial_define_key (meta_map, 'v', "scroll-down-command");