* doc/misc/calc.texi (ISO-8601): New section.
[emacs.git] / src / window.c
blob28c3bf93553f02c46bfa2b8ea34a8d4452b6b3b4
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2012
4 Free Software 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 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 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
274 cease its buffer, and 1 means that W is about to set up the new one. */
276 static void
277 adjust_window_count (struct window *w, int arg)
279 eassert (eabs (arg) == 1);
280 if (BUFFERP (w->buffer))
282 struct buffer *b = XBUFFER (w->buffer);
284 if (b->base_buffer)
285 b = b->base_buffer;
286 b->window_count += arg;
287 eassert (b->window_count >= 0);
291 /* Set W's buffer slot to VAL and recompute number
292 of windows showing VAL if it is a buffer. */
294 void
295 wset_buffer (struct window *w, Lisp_Object val)
297 adjust_window_count (w, -1);
298 w->buffer = val;
299 adjust_window_count (w, 1);
302 /* Build a frequently used 4-integer (X Y W H) list. */
304 static Lisp_Object
305 list4i (EMACS_INT x, EMACS_INT y, EMACS_INT w, EMACS_INT h)
307 return list4 (make_number (x), make_number (y),
308 make_number (w), make_number (h));
311 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
312 doc: /* Return t if OBJECT is a window and nil otherwise. */)
313 (Lisp_Object object)
315 return WINDOWP (object) ? Qt : Qnil;
318 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
319 doc: /* Return t if OBJECT is a valid window and nil otherwise.
320 A valid window is either a window that displays a buffer or an internal
321 window. Deleted windows are not live. */)
322 (Lisp_Object object)
324 return WINDOW_VALID_P (object) ? Qt : Qnil;
327 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
328 doc: /* Return t if OBJECT is a live window and nil otherwise.
329 A live window is a window that displays a buffer.
330 Internal windows and deleted windows are not live. */)
331 (Lisp_Object object)
333 return WINDOW_LIVE_P (object) ? Qt : Qnil;
336 /* Frames and windows. */
337 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
338 doc: /* Return the frame that window WINDOW is on.
339 WINDOW must be a valid window and defaults to the selected one. */)
340 (Lisp_Object window)
342 return decode_valid_window (window)->frame;
345 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
346 doc: /* Return the root window of FRAME-OR-WINDOW.
347 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
348 With a frame argument, return that frame's root window.
349 With a window argument, return the root window of that window's frame. */)
350 (Lisp_Object frame_or_window)
352 Lisp_Object window;
354 if (NILP (frame_or_window))
355 window = SELECTED_FRAME ()->root_window;
356 else if (WINDOW_VALID_P (frame_or_window))
357 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
358 else
360 CHECK_LIVE_FRAME (frame_or_window);
361 window = XFRAME (frame_or_window)->root_window;
364 return window;
367 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
368 doc: /* Return the minibuffer window for frame FRAME.
369 If FRAME is omitted or nil, it defaults to the selected frame. */)
370 (Lisp_Object frame)
372 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
375 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
376 Swindow_minibuffer_p, 0, 1, 0,
377 doc: /* Return non-nil if WINDOW is a minibuffer window.
378 WINDOW must be a valid window and defaults to the selected one. */)
379 (Lisp_Object window)
381 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
384 /* Don't move this to window.el - this must be a safe routine. */
385 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
386 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
387 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
388 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
389 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
390 the first window of that frame. */)
391 (Lisp_Object frame_or_window)
393 Lisp_Object window;
395 if (NILP (frame_or_window))
396 window = SELECTED_FRAME ()->root_window;
397 else if (WINDOW_VALID_P (frame_or_window))
398 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
399 else
401 CHECK_LIVE_FRAME (frame_or_window);
402 window = XFRAME (frame_or_window)->root_window;
405 while (NILP (XWINDOW (window)->buffer))
407 if (! NILP (XWINDOW (window)->hchild))
408 window = XWINDOW (window)->hchild;
409 else if (! NILP (XWINDOW (window)->vchild))
410 window = XWINDOW (window)->vchild;
411 else
412 emacs_abort ();
415 return window;
418 DEFUN ("frame-selected-window", Fframe_selected_window,
419 Sframe_selected_window, 0, 1, 0,
420 doc: /* Return the selected window of FRAME-OR-WINDOW.
421 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
422 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
423 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
424 return the selected window of that frame. */)
425 (Lisp_Object frame_or_window)
427 Lisp_Object window;
429 if (NILP (frame_or_window))
430 window = SELECTED_FRAME ()->selected_window;
431 else if (WINDOW_VALID_P (frame_or_window))
432 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
433 else
435 CHECK_LIVE_FRAME (frame_or_window);
436 window = XFRAME (frame_or_window)->selected_window;
439 return window;
442 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
443 Sset_frame_selected_window, 2, 3, 0,
444 doc: /* Set selected window of FRAME to WINDOW.
445 FRAME must be a live frame and defaults to the selected one. If FRAME
446 is the selected frame, this makes WINDOW the selected window. Optional
447 argument NORECORD non-nil means to neither change the order of recently
448 selected windows nor the buffer list. WINDOW must denote a live window.
449 Return WINDOW. */)
450 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
452 if (NILP (frame))
453 frame = selected_frame;
455 CHECK_LIVE_FRAME (frame);
456 CHECK_LIVE_WINDOW (window);
458 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
459 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
461 if (EQ (frame, selected_frame))
462 return Fselect_window (window, norecord);
463 else
465 fset_selected_window (XFRAME (frame), window);
466 return window;
470 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
471 doc: /* Return the selected window.
472 The selected window is the window in which the standard cursor for
473 selected windows appears and to which many commands apply. */)
474 (void)
476 return selected_window;
479 int window_select_count;
481 /* If select_window is called with inhibit_point_swap non-zero it will
482 not store point of the old selected window's buffer back into that
483 window's pointm slot. This is needed by Fset_window_configuration to
484 avoid that the display routine is called with selected_window set to
485 Qnil causing a subsequent crash. */
486 static Lisp_Object
487 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
489 register struct window *w;
490 register struct window *ow;
491 struct frame *sf;
493 CHECK_LIVE_WINDOW (window);
495 w = XWINDOW (window);
496 w->frozen_window_start_p = 0;
498 if (NILP (norecord))
500 w->use_time = ++window_select_count;
501 record_buffer (w->buffer);
504 /* Make the selected window's buffer current. */
505 Fset_buffer (w->buffer);
507 if (EQ (window, selected_window) && !inhibit_point_swap)
508 return window;
510 sf = SELECTED_FRAME ();
511 if (XFRAME (WINDOW_FRAME (w)) != sf)
513 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
514 /* Use this rather than Fhandle_switch_frame
515 so that FRAME_FOCUS_FRAME is moved appropriately as we
516 move around in the state where a minibuffer in a separate
517 frame is active. */
518 Fselect_frame (WINDOW_FRAME (w), norecord);
519 /* Fselect_frame called us back so we've done all the work already. */
520 eassert (EQ (window, selected_window));
521 return window;
523 else
524 fset_selected_window (sf, window);
526 /* Store the old selected window's buffer's point in pointm of the old
527 selected window. It belongs to that window, and when the window is
528 not selected, must be in the window. */
529 if (!inhibit_point_swap)
531 ow = XWINDOW (selected_window);
532 if (! NILP (ow->buffer))
533 set_marker_both (ow->pointm, ow->buffer,
534 BUF_PT (XBUFFER (ow->buffer)),
535 BUF_PT_BYTE (XBUFFER (ow->buffer)));
538 selected_window = window;
539 bset_last_selected_window (XBUFFER (w->buffer), window);
541 /* Go to the point recorded in the window.
542 This is important when the buffer is in more
543 than one window. It also matters when
544 redisplay_window has altered point after scrolling,
545 because it makes the change only in the window. */
547 register ptrdiff_t new_point = marker_position (w->pointm);
548 if (new_point < BEGV)
549 SET_PT (BEGV);
550 else if (new_point > ZV)
551 SET_PT (ZV);
552 else
553 SET_PT (new_point);
556 windows_or_buffers_changed++;
557 return window;
560 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
561 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
562 Also make WINDOW's buffer current and make WINDOW the frame's selected
563 window. Return WINDOW.
565 Optional second arg NORECORD non-nil means do not put this buffer at the
566 front of the buffer list and do not make this window the most recently
567 selected one.
569 Note that the main editor command loop sets the current buffer to the
570 buffer of the selected window before each command. */)
571 (register Lisp_Object window, Lisp_Object norecord)
573 return select_window (window, norecord, 0);
576 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
577 doc: /* Return the buffer displayed in window WINDOW.
578 If WINDOW is omitted or nil, it defaults to the selected window.
579 Return nil for an internal window or a deleted window. */)
580 (Lisp_Object window)
582 return decode_any_window (window)->buffer;
585 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
586 doc: /* Return the parent window of window WINDOW.
587 WINDOW must be a valid window and defaults to the selected one.
588 Return nil for a window with no parent (e.g. a root window). */)
589 (Lisp_Object window)
591 return decode_valid_window (window)->parent;
594 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
595 doc: /* Return the topmost child window of window WINDOW.
596 WINDOW must be a valid window and defaults to the selected one.
597 Return nil if WINDOW is a live window (live windows have no children).
598 Return nil if WINDOW is an internal window whose children form a
599 horizontal combination. */)
600 (Lisp_Object window)
602 return decode_valid_window (window)->vchild;
605 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
606 doc: /* Return the leftmost child window of window WINDOW.
607 WINDOW must be a valid window and defaults to the selected one.
608 Return nil if WINDOW is a live window (live windows have no children).
609 Return nil if WINDOW is an internal window whose children form a
610 vertical combination. */)
611 (Lisp_Object window)
613 return decode_valid_window (window)->hchild;
616 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
617 doc: /* Return the next sibling window of window WINDOW.
618 WINDOW must be a valid window and defaults to the selected one.
619 Return nil if WINDOW has no next sibling. */)
620 (Lisp_Object window)
622 return decode_valid_window (window)->next;
625 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
626 doc: /* Return the previous sibling window of window WINDOW.
627 WINDOW must be a valid window and defaults to the selected one.
628 Return nil if WINDOW has no previous sibling. */)
629 (Lisp_Object window)
631 return decode_valid_window (window)->prev;
634 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
635 doc: /* Return combination limit of window WINDOW.
636 If the return value is nil, child windows of WINDOW can be recombined with
637 WINDOW's siblings. A return value of t means that child windows of
638 WINDOW are never \(re-)combined with WINDOW's siblings.
640 WINDOW must be a valid window. The return value is meaningful for
641 internal windows only. */)
642 (Lisp_Object window)
644 CHECK_VALID_WINDOW (window);
645 return XWINDOW (window)->combination_limit;
648 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
649 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
650 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
651 siblings. LIMIT t means that child windows of WINDOW are never
652 \(re-)combined with WINDOW's siblings. Other values are reserved for
653 future use.
655 WINDOW must be a valid window. Setting the combination limit is
656 meaningful for internal windows only. */)
657 (Lisp_Object window, Lisp_Object limit)
659 wset_combination_limit (decode_valid_window (window), limit);
660 return limit;
663 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
664 doc: /* Return the use time of window WINDOW.
665 WINDOW must be a live window and defaults to the selected one.
666 The window with the highest use time is the most recently selected
667 one. The window with the lowest use time is the least recently
668 selected one. */)
669 (Lisp_Object window)
671 return make_number (decode_live_window (window)->use_time);
674 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
675 doc: /* Return the total height, in lines, of window WINDOW.
676 WINDOW must be a valid window and defaults to the selected one.
678 The return value includes the mode line and header line, if any.
679 If WINDOW is an internal window, the total height is the height
680 of the screen areas spanned by its children.
682 On a graphical display, this total height is reported as an
683 integer multiple of the default character height. */)
684 (Lisp_Object window)
686 return decode_valid_window (window)->total_lines;
689 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
690 doc: /* Return the total width, in columns, of window WINDOW.
691 WINDOW must be a valid window and defaults to the selected one.
693 The return value includes any vertical dividers or scroll bars
694 belonging to WINDOW. If WINDOW is an internal window, the total width
695 is the width of the screen areas spanned by its children.
697 On a graphical display, this total width is reported as an
698 integer multiple of the default character width. */)
699 (Lisp_Object window)
701 return decode_valid_window (window)->total_cols;
704 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
705 doc: /* Return the new total size of window WINDOW.
706 WINDOW must be a valid window and defaults to the selected one. */)
707 (Lisp_Object window)
709 return decode_valid_window (window)->new_total;
712 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
713 doc: /* Return the normal height of window WINDOW.
714 WINDOW must be a valid window and defaults to the selected one.
715 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
716 (Lisp_Object window, Lisp_Object horizontal)
718 struct window *w = decode_valid_window (window);
720 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
723 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
724 doc: /* Return new normal size of window WINDOW.
725 WINDOW must be a valid window and defaults to the selected one. */)
726 (Lisp_Object window)
728 return decode_valid_window (window)->new_normal;
731 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
732 doc: /* Return left column of window WINDOW.
733 This is the distance, in columns, between the left edge of WINDOW and
734 the left edge of the frame's window area. For instance, the return
735 value is 0 if there is no window to the left of WINDOW.
737 WINDOW must be a valid window and defaults to the selected one. */)
738 (Lisp_Object window)
740 return decode_valid_window (window)->left_col;
743 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
744 doc: /* Return top line of window WINDOW.
745 This is the distance, in lines, between the top of WINDOW and the top
746 of the frame's window area. For instance, the return value is 0 if
747 there is no window above WINDOW.
749 WINDOW must be a valid window and defaults to the selected one. */)
750 (Lisp_Object window)
752 return decode_valid_window (window)->top_line;
755 /* Return the number of lines of W's body. Don't count any mode or
756 header line of W. */
758 static int
759 window_body_lines (struct window *w)
761 int height = XFASTINT (w->total_lines);
763 if (!MINI_WINDOW_P (w))
765 if (WINDOW_WANTS_MODELINE_P (w))
766 --height;
767 if (WINDOW_WANTS_HEADER_LINE_P (w))
768 --height;
771 return height;
774 /* Return the number of columns of W's body. Don't count columns
775 occupied by the scroll bar or the vertical bar separating W from its
776 right sibling. On window-systems don't count fringes or display
777 margins either. */
780 window_body_cols (struct window *w)
782 struct frame *f = XFRAME (WINDOW_FRAME (w));
783 int width = XINT (w->total_cols);
785 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
786 /* Scroll bars occupy a few columns. */
787 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
788 else if (!FRAME_WINDOW_P (f)
789 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
790 /* The column of `|' characters separating side-by-side windows
791 occupies one column only. */
792 width -= 1;
794 if (FRAME_WINDOW_P (f))
795 /* On window-systems, fringes and display margins cannot be
796 used for normal text. */
797 width -= (WINDOW_FRINGE_COLS (w)
798 + WINDOW_LEFT_MARGIN_COLS (w)
799 + WINDOW_RIGHT_MARGIN_COLS (w));
801 return width;
804 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 1, 0,
805 doc: /* Return the height, in lines, of WINDOW's text area.
806 WINDOW must be a live window and defaults to the selected one.
808 The returned height does not include the mode line or header line.
809 On a graphical display, the height is expressed as an integer multiple
810 of the default character height. If a line at the bottom of the text
811 area is only partially visible, that counts as a whole line; to
812 exclude partially-visible lines, use `window-text-height'. */)
813 (Lisp_Object window)
815 return make_number (window_body_lines (decode_live_window (window)));
818 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 1, 0,
819 doc: /* Return the width, in columns, of WINDOW's text area.
820 WINDOW must be a live window and defaults to the selected one.
822 The return value does not include any vertical dividers, fringe or
823 marginal areas, or scroll bars. On a graphical display, the width is
824 expressed as an integer multiple of the default character width. */)
825 (Lisp_Object window)
827 return make_number (window_body_cols (decode_live_window (window)));
830 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
831 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
832 WINDOW must be a live window and defaults to the selected one. */)
833 (Lisp_Object window)
835 return make_number (decode_live_window (window)->hscroll);
838 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
839 range, returning the new amount as a fixnum. */
840 static Lisp_Object
841 set_window_hscroll (struct window *w, EMACS_INT hscroll)
843 /* Horizontal scrolling has problems with large scroll amounts.
844 It's too slow with long lines, and even with small lines the
845 display can be messed up. For now, though, impose only the limits
846 required by the internal representation: horizontal scrolling must
847 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
848 (since it's stored in a ptrdiff_t). */
849 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
850 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
852 /* Prevent redisplay shortcuts when changing the hscroll. */
853 if (w->hscroll != new_hscroll)
854 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
856 w->hscroll = new_hscroll;
857 return make_number (new_hscroll);
860 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
861 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
862 WINDOW must be a live window and defaults to the selected one.
863 Clip the number to a reasonable value if out of range.
864 Return the new number. NCOL should be zero or positive.
866 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
867 window so that the location of point moves off-window. */)
868 (Lisp_Object window, Lisp_Object ncol)
870 CHECK_NUMBER (ncol);
871 return set_window_hscroll (decode_live_window (window), XINT (ncol));
874 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
875 Swindow_redisplay_end_trigger, 0, 1, 0,
876 doc: /* Return WINDOW's redisplay end trigger value.
877 WINDOW must be a live window and defaults to the selected one.
878 See `set-window-redisplay-end-trigger' for more information. */)
879 (Lisp_Object window)
881 return decode_live_window (window)->redisplay_end_trigger;
884 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
885 Sset_window_redisplay_end_trigger, 2, 2, 0,
886 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
887 WINDOW must be a live window and defaults to the selected one. VALUE
888 should be a buffer position (typically a marker) or nil. If it is a
889 buffer position, then if redisplay in WINDOW reaches a position beyond
890 VALUE, the functions in `redisplay-end-trigger-functions' are called
891 with two arguments: WINDOW, and the end trigger value. Afterwards the
892 end-trigger value is reset to nil. */)
893 (register Lisp_Object window, Lisp_Object value)
895 wset_redisplay_end_trigger (decode_live_window (window), value);
896 return value;
899 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
900 doc: /* Return a list of the edge coordinates of WINDOW.
901 WINDOW must be a valid window and defaults to the selected one.
903 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
904 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
905 0 at top left corner of frame.
907 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
908 is one more than the bottommost row occupied by WINDOW. The edges
909 include the space used by WINDOW's scroll bar, display margins, fringes,
910 header line, and/or mode line. For the edges of just the text area, use
911 `window-inside-edges'. */)
912 (Lisp_Object window)
914 register struct window *w = decode_valid_window (window);
916 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
917 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
920 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
921 doc: /* Return a list of the edge pixel coordinates of WINDOW.
922 WINDOW must be a valid window and defaults to the selected one.
924 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
925 0, 0 at the top left corner of the frame.
927 RIGHT is one more than the rightmost x position occupied by WINDOW.
928 BOTTOM is one more than the bottommost y position occupied by WINDOW.
929 The pixel edges include the space used by WINDOW's scroll bar, display
930 margins, fringes, header line, and/or mode line. For the pixel edges
931 of just the text area, use `window-inside-pixel-edges'. */)
932 (Lisp_Object window)
934 register struct window *w = decode_valid_window (window);
936 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
937 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
940 static void
941 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
943 struct frame *f = XFRAME (w->frame);
944 *add_y = f->top_pos;
945 #ifdef FRAME_MENUBAR_HEIGHT
946 *add_y += FRAME_MENUBAR_HEIGHT (f);
947 #endif
948 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
949 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
950 #elif FRAME_TOOLBAR_HEIGHT
951 *add_y += FRAME_TOOLBAR_HEIGHT (f);
952 #endif
953 #ifdef FRAME_NS_TITLEBAR_HEIGHT
954 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
955 #endif
956 *add_x = f->left_pos;
957 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
958 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
959 #endif
962 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
963 Swindow_absolute_pixel_edges, 0, 1, 0,
964 doc: /* Return a list of the edge pixel coordinates of WINDOW.
965 WINDOW must be a valid window and defaults to the selected one.
967 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
968 0, 0 at the top left corner of the display.
970 RIGHT is one more than the rightmost x position occupied by WINDOW.
971 BOTTOM is one more than the bottommost y position occupied by WINDOW.
972 The pixel edges include the space used by WINDOW's scroll bar, display
973 margins, fringes, header line, and/or mode line. For the pixel edges
974 of just the text area, use `window-inside-absolute-pixel-edges'. */)
975 (Lisp_Object window)
977 register struct window *w = decode_valid_window (window);
978 int add_x, add_y;
980 calc_absolute_offset (w, &add_x, &add_y);
982 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
983 WINDOW_TOP_EDGE_Y (w) + add_y,
984 WINDOW_RIGHT_EDGE_X (w) + add_x,
985 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
988 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
989 doc: /* Return a list of the edge coordinates of WINDOW.
990 WINDOW must be a live window and defaults to the selected one.
992 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
993 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
994 0 at top left corner of frame.
996 RIGHT is one more than the rightmost column of WINDOW's text area.
997 BOTTOM is one more than the bottommost row of WINDOW's text area. The
998 inside edges do not include the space used by the WINDOW's scroll bar,
999 display margins, fringes, header line, and/or mode line. */)
1000 (Lisp_Object window)
1002 register struct window *w = decode_live_window (window);
1004 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1005 + WINDOW_LEFT_MARGIN_COLS (w)
1006 + WINDOW_LEFT_FRINGE_COLS (w)),
1007 (WINDOW_TOP_EDGE_LINE (w)
1008 + WINDOW_HEADER_LINE_LINES (w)),
1009 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1010 - WINDOW_RIGHT_MARGIN_COLS (w)
1011 - WINDOW_RIGHT_FRINGE_COLS (w)),
1012 (WINDOW_BOTTOM_EDGE_LINE (w)
1013 - WINDOW_MODE_LINE_LINES (w)));
1016 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1017 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1018 WINDOW must be a live window and defaults to the selected one.
1020 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1021 (0,0) at the top left corner of the frame's window area.
1023 RIGHT is one more than the rightmost x position of WINDOW's text area.
1024 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1025 The inside edges do not include the space used by WINDOW's scroll bar,
1026 display margins, fringes, header line, and/or mode line. */)
1027 (Lisp_Object window)
1029 register struct window *w = decode_live_window (window);
1031 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1032 + WINDOW_LEFT_MARGIN_WIDTH (w)
1033 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1034 (WINDOW_TOP_EDGE_Y (w)
1035 + WINDOW_HEADER_LINE_HEIGHT (w)),
1036 (WINDOW_BOX_RIGHT_EDGE_X (w)
1037 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1038 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1039 (WINDOW_BOTTOM_EDGE_Y (w)
1040 - WINDOW_MODE_LINE_HEIGHT (w)));
1043 DEFUN ("window-inside-absolute-pixel-edges",
1044 Fwindow_inside_absolute_pixel_edges,
1045 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1046 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1047 WINDOW must be a live window and defaults to the selected one.
1049 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1050 (0,0) at the top left corner of the frame's window area.
1052 RIGHT is one more than the rightmost x position of WINDOW's text area.
1053 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1054 The inside edges do not include the space used by WINDOW's scroll bar,
1055 display margins, fringes, header line, and/or mode line. */)
1056 (Lisp_Object window)
1058 register struct window *w = decode_live_window (window);
1059 int add_x, add_y;
1061 calc_absolute_offset (w, &add_x, &add_y);
1063 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1064 + WINDOW_LEFT_MARGIN_WIDTH (w)
1065 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1066 (WINDOW_TOP_EDGE_Y (w)
1067 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1068 (WINDOW_BOX_RIGHT_EDGE_X (w)
1069 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1070 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1071 (WINDOW_BOTTOM_EDGE_Y (w)
1072 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1075 /* Test if the character at column X, row Y is within window W.
1076 If it is not, return ON_NOTHING;
1077 if it is in the window's text area, return ON_TEXT;
1078 if it is on the window's modeline, return ON_MODE_LINE;
1079 if it is on the border between the window and its right sibling,
1080 return ON_VERTICAL_BORDER.
1081 if it is on a scroll bar, return ON_SCROLL_BAR.
1082 if it is on the window's top line, return ON_HEADER_LINE;
1083 if it is in left or right fringe of the window,
1084 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1085 if it is in the marginal area to the left/right of the window,
1086 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1088 X and Y are frame relative pixel coordinates. */
1090 static enum window_part
1091 coordinates_in_window (register struct window *w, int x, int y)
1093 struct frame *f = XFRAME (WINDOW_FRAME (w));
1094 enum window_part part;
1095 int ux = FRAME_COLUMN_WIDTH (f);
1096 int left_x = WINDOW_LEFT_EDGE_X (w);
1097 int right_x = WINDOW_RIGHT_EDGE_X (w);
1098 int top_y = WINDOW_TOP_EDGE_Y (w);
1099 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1100 /* The width of the area where the vertical line can be dragged.
1101 (Between mode lines for instance. */
1102 int grabbable_width = ux;
1103 int lmargin_width, rmargin_width, text_left, text_right;
1105 /* Outside any interesting row or column? */
1106 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1107 return ON_NOTHING;
1109 /* On the mode line or header line? */
1110 if ((WINDOW_WANTS_MODELINE_P (w)
1111 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
1112 && (part = ON_MODE_LINE))
1113 || (WINDOW_WANTS_HEADER_LINE_P (w)
1114 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1115 && (part = ON_HEADER_LINE)))
1117 /* If it's under/over the scroll bar portion of the mode/header
1118 line, say it's on the vertical line. That's to be able to
1119 resize windows horizontally in case we're using toolkit scroll
1120 bars. Note: If scrollbars are on the left, the window that
1121 must be eventually resized is that on the left of WINDOW. */
1122 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1123 && !WINDOW_LEFTMOST_P (w)
1124 && eabs (x - left_x) < grabbable_width)
1125 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1126 && !WINDOW_RIGHTMOST_P (w)
1127 && eabs (x - right_x) < grabbable_width))
1128 return ON_VERTICAL_BORDER;
1129 else
1130 return part;
1133 /* In what's below, we subtract 1 when computing right_x because we
1134 want the rightmost pixel, which is given by left_pixel+width-1. */
1135 if (w->pseudo_window_p)
1137 left_x = 0;
1138 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
1140 else
1142 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1143 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1146 /* Outside any interesting column? */
1147 if (x < left_x || x > right_x)
1148 return ON_SCROLL_BAR;
1150 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1151 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1153 text_left = window_box_left (w, TEXT_AREA);
1154 text_right = text_left + window_box_width (w, TEXT_AREA);
1156 if (FRAME_WINDOW_P (f))
1158 if (!w->pseudo_window_p
1159 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1160 && !WINDOW_RIGHTMOST_P (w)
1161 && (eabs (x - right_x) < grabbable_width))
1162 return ON_VERTICAL_BORDER;
1164 /* Need to say "x > right_x" rather than >=, since on character
1165 terminals, the vertical line's x coordinate is right_x. */
1166 else if (!w->pseudo_window_p
1167 && !WINDOW_RIGHTMOST_P (w)
1168 && x > right_x - ux)
1169 return ON_VERTICAL_BORDER;
1171 if (x < text_left)
1173 if (lmargin_width > 0
1174 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1175 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1176 : (x < left_x + lmargin_width)))
1177 return ON_LEFT_MARGIN;
1179 return ON_LEFT_FRINGE;
1182 if (x >= text_right)
1184 if (rmargin_width > 0
1185 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1186 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1187 : (x >= right_x - rmargin_width)))
1188 return ON_RIGHT_MARGIN;
1190 return ON_RIGHT_FRINGE;
1193 /* Everything special ruled out - must be on text area */
1194 return ON_TEXT;
1197 /* Take X is the frame-relative pixel x-coordinate, and return the
1198 x-coordinate relative to part PART of window W. */
1200 window_relative_x_coord (struct window *w, enum window_part part, int x)
1202 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1204 switch (part)
1206 case ON_TEXT:
1207 return x - window_box_left (w, TEXT_AREA);
1209 case ON_LEFT_FRINGE:
1210 return x - left_x;
1212 case ON_RIGHT_FRINGE:
1213 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1215 case ON_LEFT_MARGIN:
1216 return (x - left_x
1217 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1218 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1220 case ON_RIGHT_MARGIN:
1221 return (x + 1
1222 - ((w->pseudo_window_p)
1223 ? WINDOW_TOTAL_WIDTH (w)
1224 : WINDOW_BOX_RIGHT_EDGE_X (w))
1225 + window_box_width (w, RIGHT_MARGIN_AREA)
1226 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1227 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1230 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1231 return 0;
1235 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1236 Scoordinates_in_window_p, 2, 2, 0,
1237 doc: /* Return non-nil if COORDINATES are in WINDOW.
1238 WINDOW must be a live window and defaults to the selected one.
1239 COORDINATES is a cons of the form (X . Y), X and Y being distances
1240 measured in characters from the upper-left corner of the frame.
1241 \(0 . 0) denotes the character in the upper left corner of the
1242 frame.
1243 If COORDINATES are in the text portion of WINDOW,
1244 the coordinates relative to the window are returned.
1245 If they are in the mode line of WINDOW, `mode-line' is returned.
1246 If they are in the top mode line of WINDOW, `header-line' is returned.
1247 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1248 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1249 If they are on the border between WINDOW and its right sibling,
1250 `vertical-line' is returned.
1251 If they are in the windows's left or right marginal areas, `left-margin'\n\
1252 or `right-margin' is returned. */)
1253 (register Lisp_Object coordinates, Lisp_Object window)
1255 struct window *w;
1256 struct frame *f;
1257 int x, y;
1258 Lisp_Object lx, ly;
1260 w = decode_live_window (window);
1261 f = XFRAME (w->frame);
1262 CHECK_CONS (coordinates);
1263 lx = Fcar (coordinates);
1264 ly = Fcdr (coordinates);
1265 CHECK_NUMBER_OR_FLOAT (lx);
1266 CHECK_NUMBER_OR_FLOAT (ly);
1267 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1268 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1270 switch (coordinates_in_window (w, x, y))
1272 case ON_NOTHING:
1273 return Qnil;
1275 case ON_TEXT:
1276 /* Convert X and Y to window relative pixel coordinates, and
1277 return the canonical char units. */
1278 x -= window_box_left (w, TEXT_AREA);
1279 y -= WINDOW_TOP_EDGE_Y (w);
1280 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1281 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1283 case ON_MODE_LINE:
1284 return Qmode_line;
1286 case ON_VERTICAL_BORDER:
1287 return Qvertical_line;
1289 case ON_HEADER_LINE:
1290 return Qheader_line;
1292 case ON_LEFT_FRINGE:
1293 return Qleft_fringe;
1295 case ON_RIGHT_FRINGE:
1296 return Qright_fringe;
1298 case ON_LEFT_MARGIN:
1299 return Qleft_margin;
1301 case ON_RIGHT_MARGIN:
1302 return Qright_margin;
1304 case ON_SCROLL_BAR:
1305 /* Historically we are supposed to return nil in this case. */
1306 return Qnil;
1308 default:
1309 emacs_abort ();
1314 /* Callback for foreach_window, used in window_from_coordinates.
1315 Check if window W contains coordinates specified by USER_DATA which
1316 is actually a pointer to a struct check_window_data CW.
1318 Check if window W contains coordinates *CW->x and *CW->y. If it
1319 does, return W in *CW->window, as Lisp_Object, and return in
1320 *CW->part the part of the window under coordinates *X,*Y. Return
1321 zero from this function to stop iterating over windows. */
1323 struct check_window_data
1325 Lisp_Object *window;
1326 int x, y;
1327 enum window_part *part;
1330 static int
1331 check_window_containing (struct window *w, void *user_data)
1333 struct check_window_data *cw = (struct check_window_data *) user_data;
1334 enum window_part found;
1335 int continue_p = 1;
1337 found = coordinates_in_window (w, cw->x, cw->y);
1338 if (found != ON_NOTHING)
1340 *cw->part = found;
1341 XSETWINDOW (*cw->window, w);
1342 continue_p = 0;
1345 return continue_p;
1349 /* Find the window containing frame-relative pixel position X/Y and
1350 return it as a Lisp_Object.
1352 If X, Y is on one of the window's special `window_part' elements,
1353 set *PART to the id of that element.
1355 If there is no window under X, Y return nil and leave *PART
1356 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1358 This function was previously implemented with a loop cycling over
1359 windows with Fnext_window, and starting with the frame's selected
1360 window. It turned out that this doesn't work with an
1361 implementation of next_window using Vwindow_list, because
1362 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1363 tree of F when this function is called asynchronously from
1364 note_mouse_highlight. The original loop didn't terminate in this
1365 case. */
1367 Lisp_Object
1368 window_from_coordinates (struct frame *f, int x, int y,
1369 enum window_part *part, int tool_bar_p)
1371 Lisp_Object window;
1372 struct check_window_data cw;
1373 enum window_part dummy;
1375 if (part == 0)
1376 part = &dummy;
1378 window = Qnil;
1379 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1380 foreach_window (f, check_window_containing, &cw);
1382 /* If not found above, see if it's in the tool bar window, if a tool
1383 bar exists. */
1384 if (NILP (window)
1385 && tool_bar_p
1386 && WINDOWP (f->tool_bar_window)
1387 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1388 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1389 != ON_NOTHING))
1391 *part = ON_TEXT;
1392 window = f->tool_bar_window;
1395 return window;
1398 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1399 doc: /* Return window containing coordinates X and Y on FRAME.
1400 FRAME must be a live frame and defaults to the selected one.
1401 The top left corner of the frame is considered to be row 0,
1402 column 0. */)
1403 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1405 struct frame *f = decode_live_frame (frame);
1407 /* Check that arguments are integers or floats. */
1408 CHECK_NUMBER_OR_FLOAT (x);
1409 CHECK_NUMBER_OR_FLOAT (y);
1411 return window_from_coordinates (f,
1412 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1413 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1414 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1415 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1416 0, 0);
1419 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1420 doc: /* Return current value of point in WINDOW.
1421 WINDOW must be a live window and defaults to the selected one.
1423 For a nonselected window, this is the value point would have if that
1424 window were selected.
1426 Note that, when WINDOW is selected, the value returned is the same as
1427 that returned by `point' for WINDOW's buffer. It would be more strictly
1428 correct to return the `top-level' value of `point', outside of any
1429 `save-excursion' forms. But that is hard to define. */)
1430 (Lisp_Object window)
1432 register struct window *w = decode_live_window (window);
1434 if (w == XWINDOW (selected_window))
1435 return make_number (BUF_PT (XBUFFER (w->buffer)));
1436 else
1437 return Fmarker_position (w->pointm);
1440 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1441 doc: /* Return position at which display currently starts in WINDOW.
1442 WINDOW must be a live window and defaults to the selected one.
1443 This is updated by redisplay or by calling `set-window-start'. */)
1444 (Lisp_Object window)
1446 return Fmarker_position (decode_live_window (window)->start);
1449 /* This is text temporarily removed from the doc string below.
1451 This function returns nil if the position is not currently known.
1452 That happens when redisplay is preempted and doesn't finish.
1453 If in that case you want to compute where the end of the window would
1454 have been if redisplay had finished, do this:
1455 (save-excursion
1456 (goto-char (window-start window))
1457 (vertical-motion (1- (window-height window)) window)
1458 (point))") */
1460 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1461 doc: /* Return position at which display currently ends in WINDOW.
1462 WINDOW must be a live window and defaults to the selected one.
1463 This is updated by redisplay, when it runs to completion.
1464 Simply changing the buffer text or setting `window-start'
1465 does not update this value.
1466 Return nil if there is no recorded value. (This can happen if the
1467 last redisplay of WINDOW was preempted, and did not finish.)
1468 If UPDATE is non-nil, compute the up-to-date position
1469 if it isn't already recorded. */)
1470 (Lisp_Object window, Lisp_Object update)
1472 Lisp_Object value;
1473 struct window *w = decode_live_window (window);
1474 Lisp_Object buf;
1475 struct buffer *b;
1477 buf = w->buffer;
1478 CHECK_BUFFER (buf);
1479 b = XBUFFER (buf);
1481 #if 0 /* This change broke some things. We should make it later. */
1482 /* If we don't know the end position, return nil.
1483 The user can compute it with vertical-motion if he wants to.
1484 It would be nicer to do it automatically,
1485 but that's so slow that it would probably bother people. */
1486 if (NILP (w->window_end_valid))
1487 return Qnil;
1488 #endif
1490 if (! NILP (update)
1491 && (windows_or_buffers_changed || NILP (w->window_end_valid))
1492 && !noninteractive)
1494 struct text_pos startp;
1495 ptrdiff_t charpos = marker_position (w->start);
1496 struct it it;
1497 struct buffer *old_buffer = NULL;
1498 void *itdata = NULL;
1500 /* Cannot use Fvertical_motion because that function doesn't
1501 cope with variable-height lines. */
1502 if (b != current_buffer)
1504 old_buffer = current_buffer;
1505 set_buffer_internal (b);
1508 /* In case W->start is out of the range, use something
1509 reasonable. This situation occurred when loading a file with
1510 `-l' containing a call to `rmail' with subsequent other
1511 commands. At the end, W->start happened to be BEG, while
1512 rmail had already narrowed the buffer. */
1513 if (charpos < BEGV)
1514 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1515 else if (charpos > ZV)
1516 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1517 else
1518 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1520 itdata = bidi_shelve_cache ();
1521 start_display (&it, w, startp);
1522 move_it_vertically (&it, window_box_height (w));
1523 if (it.current_y < it.last_visible_y)
1524 move_it_past_eol (&it);
1525 value = make_number (IT_CHARPOS (it));
1526 bidi_unshelve_cache (itdata, 0);
1528 if (old_buffer)
1529 set_buffer_internal (old_buffer);
1531 else
1532 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1534 return value;
1537 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1538 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1539 WINDOW must be a live window and defaults to the selected one.
1540 Return POS. */)
1541 (Lisp_Object window, Lisp_Object pos)
1543 register struct window *w = decode_live_window (window);
1545 CHECK_NUMBER_COERCE_MARKER (pos);
1547 if (w == XWINDOW (selected_window))
1549 if (XBUFFER (w->buffer) == current_buffer)
1550 Fgoto_char (pos);
1551 else
1553 struct buffer *old_buffer = current_buffer;
1555 set_buffer_internal (XBUFFER (w->buffer));
1556 Fgoto_char (pos);
1557 set_buffer_internal (old_buffer);
1560 else
1562 set_marker_restricted (w->pointm, pos, w->buffer);
1563 /* We have to make sure that redisplay updates the window to show
1564 the new value of point. */
1565 ++windows_or_buffers_changed;
1568 return pos;
1571 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1572 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1573 WINDOW must be a live window and defaults to the selected one. Return
1574 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1575 overriding motion of point in order to display at this exact start. */)
1576 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1578 register struct window *w = decode_live_window (window);
1580 CHECK_NUMBER_COERCE_MARKER (pos);
1581 set_marker_restricted (w->start, pos, w->buffer);
1582 /* this is not right, but much easier than doing what is right. */
1583 w->start_at_line_beg = 0;
1584 if (NILP (noforce))
1585 w->force_start = 1;
1586 w->update_mode_line = 1;
1587 w->last_modified = 0;
1588 w->last_overlay_modified = 0;
1589 if (!EQ (window, selected_window))
1590 windows_or_buffers_changed++;
1592 return pos;
1595 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1596 Spos_visible_in_window_p, 0, 3, 0,
1597 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1598 WINDOW must be a live window and defaults to the selected one.
1600 Return nil if that position is scrolled vertically out of view. If a
1601 character is only partially visible, nil is returned, unless the
1602 optional argument PARTIALLY is non-nil. If POS is only out of view
1603 because of horizontal scrolling, return non-nil. If POS is t, it
1604 specifies the position of the last visible glyph in WINDOW. POS
1605 defaults to point in WINDOW; WINDOW defaults to the selected window.
1607 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1608 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1609 where X and Y are the pixel coordinates relative to the top left corner
1610 of the window. The remaining elements are omitted if the character after
1611 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1612 off-window at the top and bottom of the row, ROWH is the height of the
1613 display row, and VPOS is the row number (0-based) containing POS. */)
1614 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1616 register struct window *w;
1617 register EMACS_INT posint;
1618 register struct buffer *buf;
1619 struct text_pos top;
1620 Lisp_Object in_window = Qnil;
1621 int rtop, rbot, rowh, vpos, fully_p = 1;
1622 int x, y;
1624 w = decode_live_window (window);
1625 buf = XBUFFER (w->buffer);
1626 SET_TEXT_POS_FROM_MARKER (top, w->start);
1628 if (EQ (pos, Qt))
1629 posint = -1;
1630 else if (!NILP (pos))
1632 CHECK_NUMBER_COERCE_MARKER (pos);
1633 posint = XINT (pos);
1635 else if (w == XWINDOW (selected_window))
1636 posint = PT;
1637 else
1638 posint = marker_position (w->pointm);
1640 /* If position is above window start or outside buffer boundaries,
1641 or if window start is out of range, position is not visible. */
1642 if ((EQ (pos, Qt)
1643 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1644 && CHARPOS (top) >= BUF_BEGV (buf)
1645 && CHARPOS (top) <= BUF_ZV (buf)
1646 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1647 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1648 in_window = Qt;
1650 if (!NILP (in_window) && !NILP (partially))
1652 Lisp_Object part = Qnil;
1653 if (!fully_p)
1654 part = list4i (rtop, rbot, rowh, vpos);
1655 in_window = Fcons (make_number (x),
1656 Fcons (make_number (y), part));
1659 return in_window;
1662 DEFUN ("window-line-height", Fwindow_line_height,
1663 Swindow_line_height, 0, 2, 0,
1664 doc: /* Return height in pixels of text line LINE in window WINDOW.
1665 WINDOW must be a live window and defaults to the selected one.
1667 Return height of current line if LINE is omitted or nil. Return height of
1668 header or mode line if LINE is `header-line' or `mode-line'.
1669 Otherwise, LINE is a text line number starting from 0. A negative number
1670 counts from the end of the window.
1672 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1673 in pixels of the visible part of the line, VPOS and YPOS are the
1674 vertical position in lines and pixels of the line, relative to the top
1675 of the first text line, and OFFBOT is the number of off-window pixels at
1676 the bottom of the text line. If there are off-window pixels at the top
1677 of the (first) text line, YPOS is negative.
1679 Return nil if window display is not up-to-date. In that case, use
1680 `pos-visible-in-window-p' to obtain the information. */)
1681 (Lisp_Object line, Lisp_Object window)
1683 register struct window *w;
1684 register struct buffer *b;
1685 struct glyph_row *row, *end_row;
1686 int max_y, crop, i;
1687 EMACS_INT n;
1689 w = decode_live_window (window);
1691 if (noninteractive || w->pseudo_window_p)
1692 return Qnil;
1694 CHECK_BUFFER (w->buffer);
1695 b = XBUFFER (w->buffer);
1697 /* Fail if current matrix is not up-to-date. */
1698 if (NILP (w->window_end_valid)
1699 || current_buffer->clip_changed
1700 || current_buffer->prevent_redisplay_optimizations_p
1701 || w->last_modified < BUF_MODIFF (b)
1702 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b))
1703 return Qnil;
1705 if (NILP (line))
1707 i = w->cursor.vpos;
1708 if (i < 0 || i >= w->current_matrix->nrows
1709 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1710 return Qnil;
1711 max_y = window_text_bottom_y (w);
1712 goto found_row;
1715 if (EQ (line, Qheader_line))
1717 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1718 return Qnil;
1719 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1720 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1723 if (EQ (line, Qmode_line))
1725 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1726 return (row->enabled_p ?
1727 list4i (row->height,
1728 0, /* not accurate */
1729 (WINDOW_HEADER_LINE_HEIGHT (w)
1730 + window_text_bottom_y (w)),
1732 : Qnil);
1735 CHECK_NUMBER (line);
1736 n = XINT (line);
1738 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1739 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1740 max_y = window_text_bottom_y (w);
1741 i = 0;
1743 while ((n < 0 || i < n)
1744 && row <= end_row && row->enabled_p
1745 && row->y + row->height < max_y)
1746 row++, i++;
1748 if (row > end_row || !row->enabled_p)
1749 return Qnil;
1751 if (++n < 0)
1753 if (-n > i)
1754 return Qnil;
1755 row += n;
1756 i += n;
1759 found_row:
1760 crop = max (0, (row->y + row->height) - max_y);
1761 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1764 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1765 0, 1, 0,
1766 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1767 More precisely, return the value assigned by the last call of
1768 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1769 never called with WINDOW as its argument, or the value set by that
1770 function was internally reset since its last call. WINDOW must be a
1771 live window and defaults to the selected one.
1773 When a window is dedicated to its buffer, `display-buffer' will refrain
1774 from displaying another buffer in it. `get-lru-window' and
1775 `get-largest-window' treat dedicated windows specially.
1776 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1777 `kill-buffer' can delete a dedicated window and the containing frame.
1779 Functions like `set-window-buffer' may change the buffer displayed by a
1780 window, unless that window is "strongly" dedicated to its buffer, that
1781 is the value returned by `window-dedicated-p' is t. */)
1782 (Lisp_Object window)
1784 return decode_live_window (window)->dedicated;
1787 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1788 Sset_window_dedicated_p, 2, 2, 0,
1789 doc: /* Mark WINDOW as dedicated according to FLAG.
1790 WINDOW must be a live window and defaults to the selected one. FLAG
1791 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1792 mark WINDOW as non-dedicated. Return FLAG.
1794 When a window is dedicated to its buffer, `display-buffer' will refrain
1795 from displaying another buffer in it. `get-lru-window' and
1796 `get-largest-window' treat dedicated windows specially.
1797 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1798 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1799 and the containing frame.
1801 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1802 its buffer. Functions like `set-window-buffer' may change the buffer
1803 displayed by a window, unless that window is strongly dedicated to its
1804 buffer. If and when `set-window-buffer' displays another buffer in a
1805 window, it also makes sure that the window is no more dedicated. */)
1806 (Lisp_Object window, Lisp_Object flag)
1808 wset_dedicated (decode_live_window (window), flag);
1809 return flag;
1812 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1813 0, 1, 0,
1814 doc: /* Return buffers previously shown in WINDOW.
1815 WINDOW must be a live window and defaults to the selected one.
1817 The return value is a list of elements (BUFFER WINDOW-START POS),
1818 where BUFFER is a buffer, WINDOW-START is the start position of the
1819 window for that buffer, and POS is a window-specific point value. */)
1820 (Lisp_Object window)
1822 return decode_live_window (window)->prev_buffers;
1825 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1826 Sset_window_prev_buffers, 2, 2, 0,
1827 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1828 WINDOW must be a live window and defaults to the selected one.
1830 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1831 where BUFFER is a buffer, WINDOW-START is the start position of the
1832 window for that buffer, and POS is a window-specific point value. */)
1833 (Lisp_Object window, Lisp_Object prev_buffers)
1835 wset_prev_buffers (decode_live_window (window), prev_buffers);
1836 return prev_buffers;
1839 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1840 0, 1, 0,
1841 doc: /* Return list of buffers recently re-shown in WINDOW.
1842 WINDOW must be a live window and defaults to the selected one. */)
1843 (Lisp_Object window)
1845 return decode_live_window (window)->next_buffers;
1848 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1849 Sset_window_next_buffers, 2, 2, 0,
1850 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1851 WINDOW must be a live window and defaults to the selected one.
1852 NEXT-BUFFERS should be a list of buffers. */)
1853 (Lisp_Object window, Lisp_Object next_buffers)
1855 wset_next_buffers (decode_live_window (window), next_buffers);
1856 return next_buffers;
1859 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1860 0, 1, 0,
1861 doc: /* Return the parameters of WINDOW and their values.
1862 WINDOW must be a valid window and defaults to the selected one. The
1863 return value is a list of elements of the form (PARAMETER . VALUE). */)
1864 (Lisp_Object window)
1866 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1869 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1870 2, 2, 0,
1871 doc: /* Return WINDOW's value for PARAMETER.
1872 WINDOW can be any window and defaults to the selected one. */)
1873 (Lisp_Object window, Lisp_Object parameter)
1875 Lisp_Object result;
1877 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1878 return CDR_SAFE (result);
1881 DEFUN ("set-window-parameter", Fset_window_parameter,
1882 Sset_window_parameter, 3, 3, 0,
1883 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1884 WINDOW can be any window and defaults to the selected one.
1885 Return VALUE. */)
1886 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1888 register struct window *w = decode_any_window (window);
1889 Lisp_Object old_alist_elt;
1891 old_alist_elt = Fassq (parameter, w->window_parameters);
1892 if (NILP (old_alist_elt))
1893 wset_window_parameters
1894 (w, Fcons (Fcons (parameter, value), w->window_parameters));
1895 else
1896 Fsetcdr (old_alist_elt, value);
1897 return value;
1900 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1901 0, 1, 0,
1902 doc: /* Return the display-table that WINDOW is using.
1903 WINDOW must be a live window and defaults to the selected one. */)
1904 (Lisp_Object window)
1906 return decode_live_window (window)->display_table;
1909 /* Get the display table for use on window W. This is either W's
1910 display table or W's buffer's display table. Ignore the specified
1911 tables if they are not valid; if no valid table is specified,
1912 return 0. */
1914 struct Lisp_Char_Table *
1915 window_display_table (struct window *w)
1917 struct Lisp_Char_Table *dp = NULL;
1919 if (DISP_TABLE_P (w->display_table))
1920 dp = XCHAR_TABLE (w->display_table);
1921 else if (BUFFERP (w->buffer))
1923 struct buffer *b = XBUFFER (w->buffer);
1925 if (DISP_TABLE_P (BVAR (b, display_table)))
1926 dp = XCHAR_TABLE (BVAR (b, display_table));
1927 else if (DISP_TABLE_P (Vstandard_display_table))
1928 dp = XCHAR_TABLE (Vstandard_display_table);
1931 return dp;
1934 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1935 doc: /* Set WINDOW's display-table to TABLE.
1936 WINDOW must be a live window and defaults to the selected one. */)
1937 (register Lisp_Object window, Lisp_Object table)
1939 wset_display_table (decode_live_window (window), table);
1940 return table;
1943 /* Record info on buffer window W is displaying
1944 when it is about to cease to display that buffer. */
1945 static void
1946 unshow_buffer (register struct window *w)
1948 Lisp_Object buf;
1949 struct buffer *b;
1951 buf = w->buffer;
1952 b = XBUFFER (buf);
1953 if (b != XMARKER (w->pointm)->buffer)
1954 emacs_abort ();
1956 #if 0
1957 if (w == XWINDOW (selected_window)
1958 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1959 /* Do this except when the selected window's buffer
1960 is being removed from some other window. */
1961 #endif
1962 /* last_window_start records the start position that this buffer
1963 had in the last window to be disconnected from it.
1964 Now that this statement is unconditional,
1965 it is possible for the buffer to be displayed in the
1966 selected window, while last_window_start reflects another
1967 window which was recently showing the same buffer.
1968 Some people might say that might be a good thing. Let's see. */
1969 b->last_window_start = marker_position (w->start);
1971 /* Point in the selected window's buffer
1972 is actually stored in that buffer, and the window's pointm isn't used.
1973 So don't clobber point in that buffer. */
1974 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1975 /* Don't clobber point in current buffer either (this could be
1976 useful in connection with bug#12208).
1977 && XBUFFER (buf) != current_buffer */
1978 /* This line helps to fix Horsley's testbug.el bug. */
1979 && !(WINDOWP (BVAR (b, last_selected_window))
1980 && w != XWINDOW (BVAR (b, last_selected_window))
1981 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1982 temp_set_point_both (b,
1983 clip_to_bounds (BUF_BEGV (b),
1984 marker_position (w->pointm),
1985 BUF_ZV (b)),
1986 clip_to_bounds (BUF_BEGV_BYTE (b),
1987 marker_byte_position (w->pointm),
1988 BUF_ZV_BYTE (b)));
1990 if (WINDOWP (BVAR (b, last_selected_window))
1991 && w == XWINDOW (BVAR (b, last_selected_window)))
1992 bset_last_selected_window (b, Qnil);
1995 /* Put NEW into the window structure in place of OLD. SETFLAG zero
1996 means change window structure only. Otherwise store geometry and
1997 other settings as well. */
1998 static void
1999 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
2001 register Lisp_Object tem;
2002 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
2004 /* If OLD is its frame's root window, then NEW is the new
2005 root window for that frame. */
2006 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2007 fset_root_window (XFRAME (o->frame), new);
2009 if (setflag)
2011 wset_left_col (n, o->left_col);
2012 wset_top_line (n, o->top_line);
2013 wset_total_cols (n, o->total_cols);
2014 wset_total_lines (n, o->total_lines);
2015 wset_normal_cols (n, o->normal_cols);
2016 wset_normal_cols (o, make_float (1.0));
2017 wset_normal_lines (n, o->normal_lines);
2018 wset_normal_lines (o, make_float (1.0));
2019 n->desired_matrix = n->current_matrix = 0;
2020 n->vscroll = 0;
2021 memset (&n->cursor, 0, sizeof (n->cursor));
2022 memset (&n->last_cursor, 0, sizeof (n->last_cursor));
2023 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2024 n->phys_cursor_type = -1;
2025 n->phys_cursor_width = -1;
2026 n->must_be_updated_p = 0;
2027 n->pseudo_window_p = 0;
2028 wset_window_end_vpos (n, make_number (0));
2029 wset_window_end_pos (n, make_number (0));
2030 wset_window_end_valid (n, Qnil);
2031 n->frozen_window_start_p = 0;
2034 tem = o->next;
2035 wset_next (n, tem);
2036 if (!NILP (tem))
2037 wset_prev (XWINDOW (tem), new);
2039 tem = o->prev;
2040 wset_prev (n, tem);
2041 if (!NILP (tem))
2042 wset_next (XWINDOW (tem), new);
2044 tem = o->parent;
2045 wset_parent (n, tem);
2046 if (!NILP (tem))
2048 if (EQ (XWINDOW (tem)->vchild, old))
2049 wset_vchild (XWINDOW (tem), new);
2050 if (EQ (XWINDOW (tem)->hchild, old))
2051 wset_hchild (XWINDOW (tem), new);
2055 /* If window WINDOW and its parent window are iso-combined, merge
2056 WINDOW's children into those of its parent window and mark WINDOW as
2057 deleted. */
2059 static void
2060 recombine_windows (Lisp_Object window)
2062 struct window *w, *p, *c;
2063 Lisp_Object parent, child;
2064 int horflag;
2066 w = XWINDOW (window);
2067 parent = w->parent;
2068 if (!NILP (parent) && NILP (w->combination_limit))
2070 p = XWINDOW (parent);
2071 if (((!NILP (p->vchild) && !NILP (w->vchild))
2072 || (!NILP (p->hchild) && !NILP (w->hchild))))
2073 /* WINDOW and PARENT are both either a vertical or a horizontal
2074 combination. */
2076 horflag = NILP (w->vchild);
2077 child = horflag ? w->hchild : w->vchild;
2078 c = XWINDOW (child);
2080 /* Splice WINDOW's children into its parent's children and
2081 assign new normal sizes. */
2082 if (NILP (w->prev))
2083 if (horflag)
2084 wset_hchild (p, child);
2085 else
2086 wset_vchild (p, child);
2087 else
2089 wset_prev (c, w->prev);
2090 wset_next (XWINDOW (w->prev), child);
2093 while (c)
2095 wset_parent (c, parent);
2097 if (horflag)
2098 wset_normal_cols (c,
2099 make_float (XFLOATINT (c->total_cols)
2100 / XFLOATINT (p->total_cols)));
2101 else
2102 wset_normal_lines (c,
2103 make_float (XFLOATINT (c->total_lines)
2104 / XFLOATINT (p->total_lines)));
2106 if (NILP (c->next))
2108 if (!NILP (w->next))
2110 wset_next (c, w->next);
2111 wset_prev (XWINDOW (c->next), child);
2114 c = 0;
2116 else
2118 child = c->next;
2119 c = XWINDOW (child);
2123 /* WINDOW can be deleted now. */
2124 wset_vchild (w, Qnil);
2125 wset_hchild (w, Qnil);
2130 /* If WINDOW can be deleted, delete it. */
2131 static void
2132 delete_deletable_window (Lisp_Object window)
2134 if (!NILP (call1 (Qwindow_deletable_p, window)))
2135 call1 (Qdelete_window, window);
2138 /***********************************************************************
2139 Window List
2140 ***********************************************************************/
2142 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2143 pointer. This is a callback function for foreach_window, used in
2144 the window_list function. */
2146 static int
2147 add_window_to_list (struct window *w, void *user_data)
2149 Lisp_Object *list = (Lisp_Object *) user_data;
2150 Lisp_Object window;
2151 XSETWINDOW (window, w);
2152 *list = Fcons (window, *list);
2153 return 1;
2157 /* Return a list of all windows, for use by next_window. If
2158 Vwindow_list is a list, return that list. Otherwise, build a new
2159 list, cache it in Vwindow_list, and return that. */
2161 static Lisp_Object
2162 window_list (void)
2164 if (!CONSP (Vwindow_list))
2166 Lisp_Object tail, frame;
2168 Vwindow_list = Qnil;
2169 FOR_EACH_FRAME (tail, frame)
2171 Lisp_Object args[2];
2173 /* We are visiting windows in canonical order, and add
2174 new windows at the front of args[1], which means we
2175 have to reverse this list at the end. */
2176 args[1] = Qnil;
2177 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2178 args[0] = Vwindow_list;
2179 args[1] = Fnreverse (args[1]);
2180 Vwindow_list = Fnconc (2, args);
2184 return Vwindow_list;
2188 /* Value is non-zero if WINDOW satisfies the constraints given by
2189 OWINDOW, MINIBUF and ALL_FRAMES.
2191 MINIBUF t means WINDOW may be minibuffer windows.
2192 `lambda' means WINDOW may not be a minibuffer window.
2193 a window means a specific minibuffer window
2195 ALL_FRAMES t means search all frames,
2196 nil means search just current frame,
2197 `visible' means search just visible frames on the
2198 current terminal,
2199 0 means search visible and iconified frames on the
2200 current terminal,
2201 a window means search the frame that window belongs to,
2202 a frame means consider windows on that frame, only. */
2204 static int
2205 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
2207 struct window *w = XWINDOW (window);
2208 struct frame *f = XFRAME (w->frame);
2209 int candidate_p = 1;
2211 if (!BUFFERP (w->buffer))
2212 candidate_p = 0;
2213 else if (MINI_WINDOW_P (w)
2214 && (EQ (minibuf, Qlambda)
2215 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2217 /* If MINIBUF is `lambda' don't consider any mini-windows.
2218 If it is a window, consider only that one. */
2219 candidate_p = 0;
2221 else if (EQ (all_frames, Qt))
2222 candidate_p = 1;
2223 else if (NILP (all_frames))
2225 eassert (WINDOWP (owindow));
2226 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2228 else if (EQ (all_frames, Qvisible))
2230 FRAME_SAMPLE_VISIBILITY (f);
2231 candidate_p = FRAME_VISIBLE_P (f)
2232 && (FRAME_TERMINAL (XFRAME (w->frame))
2233 == FRAME_TERMINAL (XFRAME (selected_frame)));
2236 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2238 FRAME_SAMPLE_VISIBILITY (f);
2239 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2240 #ifdef HAVE_X_WINDOWS
2241 /* Yuck!! If we've just created the frame and the
2242 window-manager requested the user to place it
2243 manually, the window may still not be considered
2244 `visible'. I'd argue it should be at least
2245 something like `iconified', but don't know how to do
2246 that yet. --Stef */
2247 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2248 && !f->output_data.x->has_been_visible)
2249 #endif
2251 && (FRAME_TERMINAL (XFRAME (w->frame))
2252 == FRAME_TERMINAL (XFRAME (selected_frame)));
2254 else if (WINDOWP (all_frames))
2255 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2256 || EQ (XWINDOW (all_frames)->frame, w->frame)
2257 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2258 else if (FRAMEP (all_frames))
2259 candidate_p = EQ (all_frames, w->frame);
2261 return candidate_p;
2265 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2266 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2267 MINIBUF, and ALL_FRAMES. */
2269 static void
2270 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2272 struct window *w = decode_live_window (*window);
2274 XSETWINDOW (*window, w);
2275 /* MINIBUF nil may or may not include minibuffers. Decide if it
2276 does. */
2277 if (NILP (*minibuf))
2278 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2279 else if (!EQ (*minibuf, Qt))
2280 *minibuf = Qlambda;
2282 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2283 => count none of them, or a specific minibuffer window (the
2284 active one) to count. */
2286 /* ALL_FRAMES nil doesn't specify which frames to include. */
2287 if (NILP (*all_frames))
2288 *all_frames
2289 = (!EQ (*minibuf, Qlambda)
2290 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2291 : Qnil);
2292 else if (EQ (*all_frames, Qvisible))
2294 else if (EQ (*all_frames, make_number (0)))
2296 else if (FRAMEP (*all_frames))
2298 else if (!EQ (*all_frames, Qt))
2299 *all_frames = Qnil;
2303 /* Return the next or previous window of WINDOW in cyclic ordering
2304 of windows. NEXT_P non-zero means return the next window. See the
2305 documentation string of next-window for the meaning of MINIBUF and
2306 ALL_FRAMES. */
2308 static Lisp_Object
2309 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2311 decode_next_window_args (&window, &minibuf, &all_frames);
2313 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2314 return the first window on the frame. */
2315 if (FRAMEP (all_frames)
2316 && !EQ (all_frames, XWINDOW (window)->frame))
2317 return Fframe_first_window (all_frames);
2319 if (next_p)
2321 Lisp_Object list;
2323 /* Find WINDOW in the list of all windows. */
2324 list = Fmemq (window, window_list ());
2326 /* Scan forward from WINDOW to the end of the window list. */
2327 if (CONSP (list))
2328 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2329 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2330 break;
2332 /* Scan from the start of the window list up to WINDOW. */
2333 if (!CONSP (list))
2334 for (list = Vwindow_list;
2335 CONSP (list) && !EQ (XCAR (list), window);
2336 list = XCDR (list))
2337 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2338 break;
2340 if (CONSP (list))
2341 window = XCAR (list);
2343 else
2345 Lisp_Object candidate, list;
2347 /* Scan through the list of windows for candidates. If there are
2348 candidate windows in front of WINDOW, the last one of these
2349 is the one we want. If there are candidates following WINDOW
2350 in the list, again the last one of these is the one we want. */
2351 candidate = Qnil;
2352 for (list = window_list (); CONSP (list); list = XCDR (list))
2354 if (EQ (XCAR (list), window))
2356 if (WINDOWP (candidate))
2357 break;
2359 else if (candidate_window_p (XCAR (list), window, minibuf,
2360 all_frames))
2361 candidate = XCAR (list);
2364 if (WINDOWP (candidate))
2365 window = candidate;
2368 return window;
2372 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2373 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2374 WINDOW must be a live window and defaults to the selected one. The
2375 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2376 consider.
2378 MINIBUF nil or omitted means consider the minibuffer window only if the
2379 minibuffer is active. MINIBUF t means consider the minibuffer window
2380 even if the minibuffer is not active. Any other value means do not
2381 consider the minibuffer window even if the minibuffer is active.
2383 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2384 plus the minibuffer window if specified by the MINIBUF argument. If the
2385 minibuffer counts, consider all windows on all frames that share that
2386 minibuffer too. The following non-nil values of ALL-FRAMES have special
2387 meanings:
2389 - t means consider all windows on all existing frames.
2391 - `visible' means consider all windows on all visible frames.
2393 - 0 (the number zero) means consider all windows on all visible and
2394 iconified frames.
2396 - A frame means consider all windows on that frame only.
2398 Anything else means consider all windows on WINDOW's frame and no
2399 others.
2401 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2402 `next-window' to iterate through the entire cycle of acceptable
2403 windows, eventually ending up back at the window you started with.
2404 `previous-window' traverses the same cycle, in the reverse order. */)
2405 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2407 return next_window (window, minibuf, all_frames, 1);
2411 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2412 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2413 WINDOW must be a live window and defaults to the selected one. The
2414 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2415 consider.
2417 MINIBUF nil or omitted means consider the minibuffer window only if the
2418 minibuffer is active. MINIBUF t means consider the minibuffer window
2419 even if the minibuffer is not active. Any other value means do not
2420 consider the minibuffer window even if the minibuffer is active.
2422 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2423 plus the minibuffer window if specified by the MINIBUF argument. If the
2424 minibuffer counts, consider all windows on all frames that share that
2425 minibuffer too. The following non-nil values of ALL-FRAMES have special
2426 meanings:
2428 - t means consider all windows on all existing frames.
2430 - `visible' means consider all windows on all visible frames.
2432 - 0 (the number zero) means consider all windows on all visible and
2433 iconified frames.
2435 - A frame means consider all windows on that frame only.
2437 Anything else means consider all windows on WINDOW's frame and no
2438 others.
2440 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2441 use `previous-window' to iterate through the entire cycle of
2442 acceptable windows, eventually ending up back at the window you
2443 started with. `next-window' traverses the same cycle, in the
2444 reverse order. */)
2445 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2447 return next_window (window, minibuf, all_frames, 0);
2451 /* Return a list of windows in cyclic ordering. Arguments are like
2452 for `next-window'. */
2454 static Lisp_Object
2455 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2457 Lisp_Object tail, list, rest;
2459 decode_next_window_args (&window, &minibuf, &all_frames);
2460 list = Qnil;
2462 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2463 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2464 list = Fcons (XCAR (tail), list);
2466 /* Rotate the list to start with WINDOW. */
2467 list = Fnreverse (list);
2468 rest = Fmemq (window, list);
2469 if (!NILP (rest) && !EQ (rest, list))
2471 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2473 XSETCDR (tail, Qnil);
2474 list = nconc2 (rest, list);
2476 return list;
2480 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2481 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2482 FRAME nil or omitted means use the selected frame.
2483 WINDOW nil or omitted means use the window selected within FRAME.
2484 MINIBUF t means include the minibuffer window, even if it isn't active.
2485 MINIBUF nil or omitted means include the minibuffer window only
2486 if it's active.
2487 MINIBUF neither nil nor t means never include the minibuffer window. */)
2488 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2490 if (NILP (window))
2491 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2492 CHECK_WINDOW (window);
2493 if (NILP (frame))
2494 frame = selected_frame;
2496 if (!EQ (frame, XWINDOW (window)->frame))
2497 error ("Window is on a different frame");
2499 return window_list_1 (window, minibuf, frame);
2503 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2504 doc: /* Return a list of all live windows.
2505 WINDOW specifies the first window to list and defaults to the selected
2506 window.
2508 Optional argument MINIBUF nil or omitted means consider the minibuffer
2509 window only if the minibuffer is active. MINIBUF t means consider the
2510 minibuffer window even if the minibuffer is not active. Any other value
2511 means do not consider the minibuffer window even if the minibuffer is
2512 active.
2514 Optional argument ALL-FRAMES nil or omitted means consider all windows
2515 on WINDOW's frame, plus the minibuffer window if specified by the
2516 MINIBUF argument. If the minibuffer counts, consider all windows on all
2517 frames that share that minibuffer too. The following non-nil values of
2518 ALL-FRAMES have special meanings:
2520 - t means consider all windows on all existing frames.
2522 - `visible' means consider all windows on all visible frames.
2524 - 0 (the number zero) means consider all windows on all visible and
2525 iconified frames.
2527 - A frame means consider all windows on that frame only.
2529 Anything else means consider all windows on WINDOW's frame and no
2530 others.
2532 If WINDOW is not on the list of windows returned, some other window will
2533 be listed first but no error is signaled. */)
2534 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2536 return window_list_1 (window, minibuf, all_frames);
2539 /* Look at all windows, performing an operation specified by TYPE
2540 with argument OBJ.
2541 If FRAMES is Qt, look at all frames;
2542 Qnil, look at just the selected frame;
2543 Qvisible, look at visible frames;
2544 a frame, just look at windows on that frame.
2545 If MINI is non-zero, perform the operation on minibuffer windows too. */
2547 enum window_loop
2549 WINDOW_LOOP_UNUSED,
2550 GET_BUFFER_WINDOW, /* Arg is buffer */
2551 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2552 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2553 CHECK_ALL_WINDOWS
2556 static Lisp_Object
2557 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2559 Lisp_Object window, windows, best_window, frame_arg;
2560 int frame_best_window_flag = 0;
2561 struct frame *f;
2562 struct gcpro gcpro1;
2564 /* If we're only looping through windows on a particular frame,
2565 frame points to that frame. If we're looping through windows
2566 on all frames, frame is 0. */
2567 if (FRAMEP (frames))
2568 f = XFRAME (frames);
2569 else if (NILP (frames))
2570 f = SELECTED_FRAME ();
2571 else
2572 f = NULL;
2574 if (f)
2575 frame_arg = Qlambda;
2576 else if (EQ (frames, make_number (0)))
2577 frame_arg = frames;
2578 else if (EQ (frames, Qvisible))
2579 frame_arg = frames;
2580 else
2581 frame_arg = Qt;
2583 /* frame_arg is Qlambda to stick to one frame,
2584 Qvisible to consider all visible frames,
2585 or Qt otherwise. */
2587 /* Pick a window to start with. */
2588 if (WINDOWP (obj))
2589 window = obj;
2590 else if (f)
2591 window = FRAME_SELECTED_WINDOW (f);
2592 else
2593 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2595 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2596 GCPRO1 (windows);
2597 best_window = Qnil;
2599 for (; CONSP (windows); windows = XCDR (windows))
2601 struct window *w;
2603 window = XCAR (windows);
2604 w = XWINDOW (window);
2606 /* Note that we do not pay attention here to whether the frame
2607 is visible, since Fwindow_list skips non-visible frames if
2608 that is desired, under the control of frame_arg. */
2609 if (!MINI_WINDOW_P (w)
2610 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2611 consider all windows. */
2612 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2613 || (mini && minibuf_level > 0))
2614 switch (type)
2616 case GET_BUFFER_WINDOW:
2617 if (EQ (w->buffer, obj)
2618 /* Don't find any minibuffer window except the one that
2619 is currently in use. */
2620 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2622 if (EQ (window, selected_window))
2623 /* Preferably return the selected window. */
2624 RETURN_UNGCPRO (window);
2625 else if (EQ (XWINDOW (window)->frame, selected_frame)
2626 && !frame_best_window_flag)
2627 /* Prefer windows on the current frame (but don't
2628 choose another one if we have one already). */
2630 best_window = window;
2631 frame_best_window_flag = 1;
2633 else if (NILP (best_window))
2634 best_window = window;
2636 break;
2638 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2639 /* We could simply check whether the buffer shown by window
2640 is live, and show another buffer in case it isn't. */
2641 if (EQ (w->buffer, obj))
2643 /* Undedicate WINDOW. */
2644 wset_dedicated (w, Qnil);
2645 /* Make WINDOW show the buffer returned by
2646 other_buffer_safely, don't run any hooks. */
2647 set_window_buffer
2648 (window, other_buffer_safely (w->buffer), 0, 0);
2649 /* If WINDOW is the selected window, make its buffer
2650 current. But do so only if the window shows the
2651 current buffer (Bug#6454). */
2652 if (EQ (window, selected_window)
2653 && XBUFFER (w->buffer) == current_buffer)
2654 Fset_buffer (w->buffer);
2656 break;
2658 case REDISPLAY_BUFFER_WINDOWS:
2659 if (EQ (w->buffer, obj))
2661 mark_window_display_accurate (window, 0);
2662 w->update_mode_line = 1;
2663 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2664 ++update_mode_lines;
2665 best_window = window;
2667 break;
2669 /* Check for a window that has a killed buffer. */
2670 case CHECK_ALL_WINDOWS:
2671 if (! NILP (w->buffer)
2672 && !BUFFER_LIVE_P (XBUFFER (w->buffer)))
2673 emacs_abort ();
2674 break;
2676 case WINDOW_LOOP_UNUSED:
2677 break;
2681 UNGCPRO;
2682 return best_window;
2685 /* Used for debugging. Abort if any window has a dead buffer. */
2687 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2688 void
2689 check_all_windows (void)
2691 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2694 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2695 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2696 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2697 the current buffer.
2699 The optional argument ALL-FRAMES specifies the frames to consider:
2701 - t means consider all windows on all existing frames.
2703 - `visible' means consider all windows on all visible frames.
2705 - 0 (the number zero) means consider all windows on all visible
2706 and iconified frames.
2708 - A frame means consider all windows on that frame only.
2710 Any other value of ALL-FRAMES means consider all windows on the
2711 selected frame and no others. */)
2712 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2714 Lisp_Object buffer;
2716 if (NILP (buffer_or_name))
2717 buffer = Fcurrent_buffer ();
2718 else
2719 buffer = Fget_buffer (buffer_or_name);
2721 if (BUFFERP (buffer))
2722 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2723 else
2724 return Qnil;
2727 static Lisp_Object
2728 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2730 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2734 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2735 Sdelete_other_windows_internal, 0, 2, "",
2736 doc: /* Make WINDOW fill its frame.
2737 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2738 and defaults to the selected one.
2740 Optional argument ROOT, if non-nil, must specify an internal window such
2741 that WINDOW is in its window subtree. If this is the case, replace ROOT
2742 by WINDOW and leave alone any windows not part of ROOT's subtree.
2744 When WINDOW is live try to reduce display jumps by keeping the text
2745 previously visible in WINDOW in the same place on the frame. Doing this
2746 depends on the value of (window-start WINDOW), so if calling this
2747 function in a program gives strange scrolling, make sure the
2748 window-start value is reasonable when this function is called. */)
2749 (Lisp_Object window, Lisp_Object root)
2751 struct window *w, *r, *s;
2752 struct frame *f;
2753 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2754 ptrdiff_t startpos IF_LINT (= 0);
2755 int top IF_LINT (= 0), new_top, resize_failed;
2757 w = decode_valid_window (window);
2758 XSETWINDOW (window, w);
2759 f = XFRAME (w->frame);
2761 if (NILP (root))
2762 /* ROOT is the frame's root window. */
2764 root = FRAME_ROOT_WINDOW (f);
2765 r = XWINDOW (root);
2767 else
2768 /* ROOT must be an ancestor of WINDOW. */
2770 r = decode_valid_window (root);
2771 pwindow = XWINDOW (window)->parent;
2772 while (!NILP (pwindow))
2773 if (EQ (pwindow, root))
2774 break;
2775 else
2776 pwindow = XWINDOW (pwindow)->parent;
2777 if (!EQ (pwindow, root))
2778 error ("Specified root is not an ancestor of specified window");
2781 if (EQ (window, root))
2782 /* A noop. */
2783 return Qnil;
2784 /* I don't understand the "top > 0" part below. If we deal with a
2785 standalone minibuffer it would have been caught by the preceding
2786 test. */
2787 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2788 error ("Can't expand minibuffer to full frame");
2790 if (!NILP (w->buffer))
2792 startpos = marker_position (w->start);
2793 top = WINDOW_TOP_EDGE_LINE (w)
2794 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2795 /* Make sure WINDOW is the frame's selected window. */
2796 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2798 if (EQ (selected_frame, w->frame))
2799 Fselect_window (window, Qnil);
2800 else
2801 fset_selected_window (f, window);
2804 else
2806 /* See if the frame's selected window is a part of the window
2807 subtree rooted at WINDOW, by finding all the selected window's
2808 parents and comparing each one with WINDOW. If it isn't we
2809 need a new selected window for this frame. */
2810 swindow = FRAME_SELECTED_WINDOW (f);
2811 while (1)
2813 pwindow = swindow;
2814 while (!NILP (pwindow) && !EQ (window, pwindow))
2815 pwindow = XWINDOW (pwindow)->parent;
2817 if (EQ (window, pwindow))
2818 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2819 as the new selected window. */
2820 break;
2821 else
2822 /* Else try the previous window of SWINDOW. */
2823 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2826 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2828 if (EQ (selected_frame, w->frame))
2829 Fselect_window (swindow, Qnil);
2830 else
2831 fset_selected_window (f, swindow);
2835 block_input ();
2836 if (!FRAME_INITIAL_P (f))
2838 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2840 /* We are going to free the glyph matrices of WINDOW, and with
2841 that we might lose any information about glyph rows that have
2842 some of their glyphs highlighted in mouse face. (These rows
2843 are marked with a non-zero mouse_face_p flag.) If WINDOW
2844 indeed has some glyphs highlighted in mouse face, signal to
2845 frame's up-to-date hook that mouse highlight was overwritten,
2846 so that it will arrange for redisplaying the highlight. */
2847 if (EQ (hlinfo->mouse_face_window, window))
2849 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
2850 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
2851 hlinfo->mouse_face_window = Qnil;
2854 free_window_matrices (r);
2856 windows_or_buffers_changed++;
2857 Vwindow_list = Qnil;
2858 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2859 resize_failed = 0;
2861 if (NILP (w->buffer))
2863 /* Resize child windows vertically. */
2864 XSETINT (delta, XINT (r->total_lines)
2865 - XINT (w->total_lines));
2866 wset_top_line (w, r->top_line);
2867 resize_root_window (window, delta, Qnil, Qnil);
2868 if (window_resize_check (w, 0))
2869 window_resize_apply (w, 0);
2870 else
2872 resize_root_window (window, delta, Qnil, Qt);
2873 if (window_resize_check (w, 0))
2874 window_resize_apply (w, 0);
2875 else
2876 resize_failed = 1;
2879 /* Resize child windows horizontally. */
2880 if (!resize_failed)
2882 wset_left_col (w, r->left_col);
2883 XSETINT (delta,
2884 XINT (r->total_cols) - XINT (w->total_cols));
2885 wset_left_col (w, r->left_col);
2886 resize_root_window (window, delta, Qt, Qnil);
2887 if (window_resize_check (w, 1))
2888 window_resize_apply (w, 1);
2889 else
2891 resize_root_window (window, delta, Qt, Qt);
2892 if (window_resize_check (w, 1))
2893 window_resize_apply (w, 1);
2894 else
2895 resize_failed = 1;
2899 if (resize_failed)
2900 /* Play safe, if we still can ... */
2902 window = swindow;
2903 w = XWINDOW (window);
2907 /* Cleanly unlink WINDOW from window-tree. */
2908 if (!NILP (w->prev))
2909 /* Get SIBLING above (on the left of) WINDOW. */
2911 sibling = w->prev;
2912 s = XWINDOW (sibling);
2913 wset_next (s, w->next);
2914 if (!NILP (s->next))
2915 wset_prev (XWINDOW (s->next), sibling);
2917 else
2918 /* Get SIBLING below (on the right of) WINDOW. */
2920 sibling = w->next;
2921 s = XWINDOW (sibling);
2922 wset_prev (s, Qnil);
2923 if (!NILP (XWINDOW (w->parent)->vchild))
2924 wset_vchild (XWINDOW (w->parent), sibling);
2925 else
2926 wset_hchild (XWINDOW (w->parent), sibling);
2929 /* Delete ROOT and all child windows of ROOT. */
2930 if (!NILP (r->vchild))
2932 delete_all_child_windows (r->vchild);
2933 wset_vchild (r, Qnil);
2935 else if (!NILP (r->hchild))
2937 delete_all_child_windows (r->hchild);
2938 wset_hchild (r, Qnil);
2941 replace_window (root, window, 1);
2943 /* This must become SWINDOW anyway ....... */
2944 if (!NILP (w->buffer) && !resize_failed)
2946 /* Try to minimize scrolling, by setting the window start to the
2947 point will cause the text at the old window start to be at the
2948 same place on the frame. But don't try to do this if the
2949 window start is outside the visible portion (as might happen
2950 when the display is not current, due to typeahead). */
2951 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2952 if (new_top != top
2953 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2954 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2956 struct position pos;
2957 struct buffer *obuf = current_buffer;
2959 Fset_buffer (w->buffer);
2960 /* This computation used to temporarily move point, but that
2961 can have unwanted side effects due to text properties. */
2962 pos = *vmotion (startpos, -top, w);
2964 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2965 wset_window_end_valid (w, Qnil);
2966 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
2967 || FETCH_BYTE (pos.bytepos - 1) == '\n');
2968 /* We need to do this, so that the window-scroll-functions
2969 get called. */
2970 w->optional_new_start = 1;
2972 set_buffer_internal (obuf);
2976 adjust_glyphs (f);
2977 unblock_input ();
2979 run_window_configuration_change_hook (f);
2981 return Qnil;
2985 void
2986 replace_buffer_in_windows (Lisp_Object buffer)
2988 call1 (Qreplace_buffer_in_windows, buffer);
2991 /* If BUFFER is shown in a window, safely replace it with some other
2992 buffer in all windows of all frames, even those on other keyboards. */
2994 void
2995 replace_buffer_in_windows_safely (Lisp_Object buffer)
2997 if (buffer_window_count (XBUFFER (buffer)))
2999 Lisp_Object tail, frame;
3001 /* A single call to window_loop won't do the job because it only
3002 considers frames on the current keyboard. So loop manually over
3003 frames, and handle each one. */
3004 FOR_EACH_FRAME (tail, frame)
3005 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
3009 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
3010 minimum allowable size. */
3012 void
3013 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
3015 /* For height, we have to see:
3016 how many windows the frame has at minimum (one or two),
3017 and whether it has a menu bar or other special stuff at the top. */
3018 int min_height
3019 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3020 ? MIN_SAFE_WINDOW_HEIGHT
3021 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3023 if (FRAME_TOP_MARGIN (frame) > 0)
3024 min_height += FRAME_TOP_MARGIN (frame);
3026 if (*rows < min_height)
3027 *rows = min_height;
3028 if (*cols < MIN_SAFE_WINDOW_WIDTH)
3029 *cols = MIN_SAFE_WINDOW_WIDTH;
3032 /* Adjust the margins of window W if text area is too small.
3033 Return 1 if window width is ok after adjustment; 0 if window
3034 is still too narrow. */
3036 static int
3037 adjust_window_margins (struct window *w)
3039 int box_cols = (WINDOW_TOTAL_COLS (w)
3040 - WINDOW_FRINGE_COLS (w)
3041 - WINDOW_SCROLL_BAR_COLS (w));
3042 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
3043 + WINDOW_RIGHT_MARGIN_COLS (w));
3045 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
3046 return 1;
3048 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
3049 return 0;
3051 /* Window's text area is too narrow, but reducing the window
3052 margins will fix that. */
3053 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
3054 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
3056 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
3058 wset_left_margin_cols (w, make_number (margin_cols / 2));
3059 wset_right_margin_cols (w, make_number (margin_cols / 2));
3061 else
3062 wset_right_margin_cols (w, make_number (margin_cols));
3064 else
3065 wset_left_margin_cols (w, make_number (margin_cols));
3066 return 1;
3069 /* The following three routines are needed for running a window's
3070 configuration change hook. */
3071 static void
3072 run_funs (Lisp_Object funs)
3074 for (; CONSP (funs); funs = XCDR (funs))
3075 if (!EQ (XCAR (funs), Qt))
3076 call0 (XCAR (funs));
3079 static Lisp_Object
3080 select_window_norecord (Lisp_Object window)
3082 return WINDOW_LIVE_P (window)
3083 ? Fselect_window (window, Qt) : selected_window;
3086 static Lisp_Object
3087 select_frame_norecord (Lisp_Object frame)
3089 return FRAME_LIVE_P (XFRAME (frame))
3090 ? Fselect_frame (frame, Qt) : selected_frame;
3093 void
3094 run_window_configuration_change_hook (struct frame *f)
3096 ptrdiff_t count = SPECPDL_INDEX ();
3097 Lisp_Object frame, global_wcch
3098 = Fdefault_value (Qwindow_configuration_change_hook);
3099 XSETFRAME (frame, f);
3101 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3102 return;
3104 /* Use the right buffer. Matters when running the local hooks. */
3105 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3107 record_unwind_current_buffer ();
3108 Fset_buffer (Fwindow_buffer (Qnil));
3111 if (SELECTED_FRAME () != f)
3113 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3114 select_frame_norecord (frame);
3117 /* Look for buffer-local values. */
3119 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3120 for (; CONSP (windows); windows = XCDR (windows))
3122 Lisp_Object window = XCAR (windows);
3123 Lisp_Object buffer = Fwindow_buffer (window);
3124 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3125 buffer)))
3127 ptrdiff_t inner_count = SPECPDL_INDEX ();
3128 record_unwind_protect (select_window_norecord, Fselected_window ());
3129 select_window_norecord (window);
3130 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3131 buffer));
3132 unbind_to (inner_count, Qnil);
3137 run_funs (global_wcch);
3138 unbind_to (count, Qnil);
3141 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3142 Srun_window_configuration_change_hook, 0, 1, 0,
3143 doc: /* Run `window-configuration-change-hook' for FRAME.
3144 If FRAME is omitted or nil, it defaults to the selected frame. */)
3145 (Lisp_Object frame)
3147 run_window_configuration_change_hook (decode_live_frame (frame));
3148 return Qnil;
3151 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3152 means it's allowed to run hooks. See make_frame for a case where
3153 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3154 margins, fringes, and scroll-bar settings of the window are not
3155 reset from the buffer's local settings. */
3157 void
3158 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
3160 struct window *w = XWINDOW (window);
3161 struct buffer *b = XBUFFER (buffer);
3162 ptrdiff_t count = SPECPDL_INDEX ();
3163 int samebuf = EQ (buffer, w->buffer);
3165 wset_buffer (w, buffer);
3167 if (EQ (window, selected_window))
3168 bset_last_selected_window (b, window);
3170 /* Let redisplay errors through. */
3171 b->display_error_modiff = 0;
3173 /* Update time stamps of buffer display. */
3174 if (INTEGERP (BVAR (b, display_count)))
3175 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3176 bset_display_time (b, Fcurrent_time ());
3178 wset_window_end_pos (w, make_number (0));
3179 wset_window_end_vpos (w, make_number (0));
3180 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3181 wset_window_end_valid (w, Qnil);
3182 if (!(keep_margins_p && samebuf))
3183 { /* If we're not actually changing the buffer, don't reset hscroll and
3184 vscroll. This case happens for example when called from
3185 change_frame_size_1, where we use a dummy call to
3186 Fset_window_buffer on the frame's selected window (and no other)
3187 just in order to run window-configuration-change-hook.
3188 Resetting hscroll and vscroll here is problematic for things like
3189 image-mode and doc-view-mode since it resets the image's position
3190 whenever we resize the frame. */
3191 w->hscroll = w->min_hscroll = 0;
3192 w->vscroll = 0;
3193 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3194 set_marker_restricted (w->start,
3195 make_number (b->last_window_start),
3196 buffer);
3197 w->start_at_line_beg = 0;
3198 w->force_start = 0;
3199 w->last_modified = 0;
3200 w->last_overlay_modified = 0;
3202 /* Maybe we could move this into the `if' but it's not obviously safe and
3203 I doubt it's worth the trouble. */
3204 windows_or_buffers_changed++;
3206 /* We must select BUFFER for running the window-scroll-functions. */
3207 /* We can't check ! NILP (Vwindow_scroll_functions) here
3208 because that might itself be a local variable. */
3209 if (window_initialized)
3211 record_unwind_current_buffer ();
3212 Fset_buffer (buffer);
3215 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3217 if (!keep_margins_p)
3219 /* Set left and right marginal area width etc. from buffer. */
3221 /* This may call adjust_window_margins three times, so
3222 temporarily disable window margins. */
3223 Lisp_Object save_left = w->left_margin_cols;
3224 Lisp_Object save_right = w->right_margin_cols;
3226 wset_left_margin_cols (w, Qnil);
3227 wset_right_margin_cols (w, Qnil);
3229 Fset_window_fringes (window,
3230 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3231 BVAR (b, fringes_outside_margins));
3233 Fset_window_scroll_bars (window,
3234 BVAR (b, scroll_bar_width),
3235 BVAR (b, vertical_scroll_bar_type), Qnil);
3237 wset_left_margin_cols (w, save_left);
3238 wset_right_margin_cols (w, save_right);
3240 Fset_window_margins (window,
3241 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3244 if (run_hooks_p)
3246 if (! NILP (Vwindow_scroll_functions))
3247 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3248 Fmarker_position (w->start));
3249 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3252 unbind_to (count, Qnil);
3255 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3256 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3257 WINDOW must be a live window and defaults to the selected one.
3258 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3260 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3261 display margins, fringe widths, and scroll bar settings are preserved;
3262 the default is to reset these from the local settings for BUFFER-OR-NAME
3263 or the frame defaults. Return nil.
3265 This function throws an error when WINDOW is strongly dedicated to its
3266 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3267 already display BUFFER-OR-NAME.
3269 This function runs `window-scroll-functions' before running
3270 `window-configuration-change-hook'. */)
3271 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3273 register Lisp_Object tem, buffer;
3274 register struct window *w = decode_live_window (window);
3276 XSETWINDOW (window, w);
3277 buffer = Fget_buffer (buffer_or_name);
3278 CHECK_BUFFER (buffer);
3279 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3280 error ("Attempt to display deleted buffer");
3282 tem = w->buffer;
3283 if (NILP (tem))
3284 error ("Window is deleted");
3285 else
3287 if (!EQ (tem, buffer))
3289 if (EQ (w->dedicated, Qt))
3290 /* WINDOW is strongly dedicated to its buffer, signal an
3291 error. */
3292 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3293 else
3294 /* WINDOW is weakly dedicated to its buffer, reset
3295 dedication. */
3296 wset_dedicated (w, Qnil);
3298 call1 (Qrecord_window_buffer, window);
3301 unshow_buffer (w);
3304 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3306 return Qnil;
3309 static Lisp_Object
3310 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3312 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3315 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3316 0, 1, 0,
3317 doc: /* Force all windows to be updated on next redisplay.
3318 If optional arg OBJECT is a window, force redisplay of that window only.
3319 If OBJECT is a buffer or buffer name, force redisplay of all windows
3320 displaying that buffer. */)
3321 (Lisp_Object object)
3323 if (NILP (object))
3325 windows_or_buffers_changed++;
3326 update_mode_lines++;
3327 return Qt;
3330 if (WINDOWP (object))
3332 struct window *w = XWINDOW (object);
3333 mark_window_display_accurate (object, 0);
3334 w->update_mode_line = 1;
3335 if (BUFFERP (w->buffer))
3336 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3337 ++update_mode_lines;
3338 return Qt;
3341 if (STRINGP (object))
3342 object = Fget_buffer (object);
3343 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3344 && buffer_window_count (XBUFFER (object)))
3346 /* If buffer is live and shown in at least one window, find
3347 all windows showing this buffer and force update of them. */
3348 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3349 return NILP (object) ? Qnil : Qt;
3352 /* If nothing suitable was found, just return.
3353 We could signal an error, but this feature will typically be used
3354 asynchronously in timers or process sentinels, so we don't. */
3355 return Qnil;
3358 /* Obsolete since 24.3. */
3359 void
3360 temp_output_buffer_show (register Lisp_Object buf)
3362 register struct buffer *old = current_buffer;
3363 register Lisp_Object window;
3364 register struct window *w;
3366 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3368 Fset_buffer (buf);
3369 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3370 BEGV = BEG;
3371 ZV = Z;
3372 SET_PT (BEG);
3373 set_buffer_internal (old);
3375 if (!NILP (Vtemp_buffer_show_function))
3376 call1 (Vtemp_buffer_show_function, buf);
3377 else
3379 window = display_buffer (buf, Qnil, Qnil);
3381 if (!EQ (XWINDOW (window)->frame, selected_frame))
3382 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3383 Vminibuf_scroll_window = window;
3384 w = XWINDOW (window);
3385 w->hscroll = 0;
3386 w->min_hscroll = 0;
3387 set_marker_restricted_both (w->start, buf, BEG, BEG);
3388 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3390 /* Run temp-buffer-show-hook, with the chosen window selected
3391 and its buffer current. */
3393 ptrdiff_t count = SPECPDL_INDEX ();
3394 Lisp_Object prev_window, prev_buffer;
3395 prev_window = selected_window;
3396 XSETBUFFER (prev_buffer, old);
3398 /* Select the window that was chosen, for running the hook.
3399 Note: Both Fselect_window and select_window_norecord may
3400 set-buffer to the buffer displayed in the window,
3401 so we need to save the current buffer. --stef */
3402 record_unwind_protect (Fset_buffer, prev_buffer);
3403 record_unwind_protect (select_window_norecord, prev_window);
3404 Fselect_window (window, Qt);
3405 Fset_buffer (w->buffer);
3406 Frun_hooks (1, &Qtemp_buffer_show_hook);
3407 unbind_to (count, Qnil);
3412 /* Make new window, have it replace WINDOW in window-tree, and make
3413 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3414 horizontal child). */
3415 static void
3416 make_parent_window (Lisp_Object window, int horflag)
3418 Lisp_Object parent;
3419 register struct window *o, *p;
3421 o = XWINDOW (window);
3422 p = allocate_window ();
3423 memcpy ((char *) p + sizeof (struct vectorlike_header),
3424 (char *) o + sizeof (struct vectorlike_header),
3425 word_size * VECSIZE (struct window));
3426 /* P's buffer slot may change from nil to a buffer. */
3427 adjust_window_count (p, 1);
3428 XSETWINDOW (parent, p);
3430 p->sequence_number = ++sequence_number;
3432 replace_window (window, parent, 1);
3434 wset_next (o, Qnil);
3435 wset_prev (o, Qnil);
3436 wset_parent (o, parent);
3438 wset_hchild (p, horflag ? window : Qnil);
3439 wset_vchild (p, horflag ? Qnil : window);
3440 wset_start (p, Qnil);
3441 wset_pointm (p, Qnil);
3442 wset_buffer (p, Qnil);
3443 wset_combination_limit (p, Qnil);
3444 wset_window_parameters (p, Qnil);
3447 /* Make new window from scratch. */
3448 Lisp_Object
3449 make_window (void)
3451 Lisp_Object window;
3452 register struct window *w;
3454 w = allocate_window ();
3455 /* Initialize Lisp data. Note that allocate_window initializes all
3456 Lisp data to nil, so do it only for slots which should not be nil. */
3457 wset_left_col (w, make_number (0));
3458 wset_top_line (w, make_number (0));
3459 wset_total_lines (w, make_number (0));
3460 wset_total_cols (w, make_number (0));
3461 wset_normal_lines (w, make_float (1.0));
3462 wset_normal_cols (w, make_float (1.0));
3463 wset_new_total (w, make_number (0));
3464 wset_new_normal (w, make_number (0));
3465 wset_start (w, Fmake_marker ());
3466 wset_pointm (w, Fmake_marker ());
3467 wset_vertical_scroll_bar_type (w, Qt);
3468 wset_window_end_pos (w, make_number (0));
3469 wset_window_end_vpos (w, make_number (0));
3470 /* These Lisp fields are marked specially so they're not set to nil by
3471 allocate_window. */
3472 wset_prev_buffers (w, Qnil);
3473 wset_next_buffers (w, Qnil);
3475 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3476 non-Lisp data, so do it only for slots which should not be zero. */
3477 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3478 w->phys_cursor_type = -1;
3479 w->phys_cursor_width = -1;
3480 w->sequence_number = ++sequence_number;
3482 /* Reset window_list. */
3483 Vwindow_list = Qnil;
3484 /* Return window. */
3485 XSETWINDOW (window, w);
3486 return window;
3489 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3490 doc: /* Set new total size of WINDOW to SIZE.
3491 WINDOW must be a valid window and defaults to the selected one.
3492 Return SIZE.
3494 Optional argument ADD non-nil means add SIZE to the new total size of
3495 WINDOW and return the sum.
3497 Note: This function does not operate on any child windows of WINDOW. */)
3498 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3500 struct window *w = decode_valid_window (window);
3502 CHECK_NUMBER (size);
3503 if (NILP (add))
3504 wset_new_total (w, size);
3505 else
3506 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3508 return w->new_total;
3511 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3512 doc: /* Set new normal size of WINDOW to SIZE.
3513 WINDOW must be a valid window and defaults to the selected one.
3514 Return SIZE.
3516 Note: This function does not operate on any child windows of WINDOW. */)
3517 (Lisp_Object window, Lisp_Object size)
3519 wset_new_normal (decode_valid_window (window), size);
3520 return size;
3523 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3524 non-zero) to w->new_total would result in correct heights (widths)
3525 for window W and recursively all child windows of W.
3527 Note: This function does not check any of `window-fixed-size-p',
3528 `window-min-height' or `window-min-width'. It does check that window
3529 sizes do not drop below one line (two columns). */
3530 static int
3531 window_resize_check (struct window *w, int horflag)
3533 struct window *c;
3535 if (!NILP (w->vchild))
3536 /* W is a vertical combination. */
3538 c = XWINDOW (w->vchild);
3539 if (horflag)
3540 /* All child windows of W must have the same width as W. */
3542 while (c)
3544 if ((XINT (c->new_total) != XINT (w->new_total))
3545 || !window_resize_check (c, horflag))
3546 return 0;
3547 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3549 return 1;
3551 else
3552 /* The sum of the heights of the child windows of W must equal
3553 W's height. */
3555 int sum_of_sizes = 0;
3556 while (c)
3558 if (!window_resize_check (c, horflag))
3559 return 0;
3560 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3561 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3563 return (sum_of_sizes == XINT (w->new_total));
3566 else if (!NILP (w->hchild))
3567 /* W is a horizontal combination. */
3569 c = XWINDOW (w->hchild);
3570 if (horflag)
3571 /* The sum of the widths of the child windows of W must equal W's
3572 width. */
3574 int sum_of_sizes = 0;
3575 while (c)
3577 if (!window_resize_check (c, horflag))
3578 return 0;
3579 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3580 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3582 return (sum_of_sizes == XINT (w->new_total));
3584 else
3585 /* All child windows of W must have the same height as W. */
3587 while (c)
3589 if ((XINT (c->new_total) != XINT (w->new_total))
3590 || !window_resize_check (c, horflag))
3591 return 0;
3592 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3594 return 1;
3597 else
3598 /* A leaf window. Make sure it's not too small. The following
3599 hardcodes the values of `window-safe-min-width' (2) and
3600 `window-safe-min-height' (1) which are defined in window.el. */
3601 return XINT (w->new_total) >= (horflag ? 2 : 1);
3604 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3605 w->new_total for window W and recursively all child windows of W.
3606 Also calculate and assign the new vertical (horizontal) start
3607 positions of each of these windows.
3609 This function does not perform any error checks. Make sure you have
3610 run window_resize_check on W before applying this function. */
3611 static void
3612 window_resize_apply (struct window *w, int horflag)
3614 struct window *c;
3615 int pos;
3617 /* Note: Assigning new_normal requires that the new total size of the
3618 parent window has been set *before*. */
3619 if (horflag)
3621 wset_total_cols (w, w->new_total);
3622 if (NUMBERP (w->new_normal))
3623 wset_normal_cols (w, w->new_normal);
3625 pos = XINT (w->left_col);
3627 else
3629 wset_total_lines (w, w->new_total);
3630 if (NUMBERP (w->new_normal))
3631 wset_normal_lines (w, w->new_normal);
3633 pos = XINT (w->top_line);
3636 if (!NILP (w->vchild))
3637 /* W is a vertical combination. */
3639 c = XWINDOW (w->vchild);
3640 while (c)
3642 if (horflag)
3643 wset_left_col (c, make_number (pos));
3644 else
3645 wset_top_line (c, make_number (pos));
3646 window_resize_apply (c, horflag);
3647 if (!horflag)
3648 pos = pos + XINT (c->total_lines);
3649 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3652 else if (!NILP (w->hchild))
3653 /* W is a horizontal combination. */
3655 c = XWINDOW (w->hchild);
3656 while (c)
3658 if (horflag)
3659 wset_left_col (c, make_number (pos));
3660 else
3661 wset_top_line (c, make_number (pos));
3662 window_resize_apply (c, horflag);
3663 if (horflag)
3664 pos = pos + XINT (c->total_cols);
3665 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3669 /* Clear out some redisplay caches. */
3670 w->last_modified = 0;
3671 w->last_overlay_modified = 0;
3675 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3676 doc: /* Apply requested size values for window-tree of FRAME.
3677 If FRAME is omitted or nil, it defaults to the selected frame.
3679 Optional argument HORIZONTAL omitted or nil means apply requested
3680 height values. HORIZONTAL non-nil means apply requested width values.
3682 This function checks whether the requested values sum up to a valid
3683 window layout, recursively assigns the new sizes of all child windows
3684 and calculates and assigns the new start positions of these windows.
3686 Note: This function does not check any of `window-fixed-size-p',
3687 `window-min-height' or `window-min-width'. All these checks have to
3688 be applied on the Elisp level. */)
3689 (Lisp_Object frame, Lisp_Object horizontal)
3691 struct frame *f = decode_live_frame (frame);
3692 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3693 int horflag = !NILP (horizontal);
3695 if (!window_resize_check (r, horflag)
3696 || ! EQ (r->new_total,
3697 (horflag ? r->total_cols : r->total_lines)))
3698 return Qnil;
3700 block_input ();
3701 window_resize_apply (r, horflag);
3703 windows_or_buffers_changed++;
3704 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3706 adjust_glyphs (f);
3707 unblock_input ();
3709 run_window_configuration_change_hook (f);
3711 return Qt;
3715 /* Resize frame F's windows when number of lines of F is set to SIZE.
3716 HORFLAG 1 means resize windows when number of columns of F is set to
3717 SIZE.
3719 This function can delete all windows but the selected one in order to
3720 satisfy the request. The result will be meaningful if and only if
3721 F's windows have meaningful sizes when you call this. */
3722 void
3723 resize_frame_windows (struct frame *f, int size, int horflag)
3725 Lisp_Object root = f->root_window;
3726 struct window *r = XWINDOW (root);
3727 Lisp_Object mini = f->minibuffer_window;
3728 struct window *m;
3729 /* new_size is the new size of the frame's root window. */
3730 int new_size = (horflag
3731 ? size
3732 : (size
3733 - FRAME_TOP_MARGIN (f)
3734 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3735 ? 1 : 0)));
3737 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
3738 if (NILP (r->vchild) && NILP (r->hchild))
3739 /* For a leaf root window just set the size. */
3740 if (horflag)
3741 wset_total_cols (r, make_number (new_size));
3742 else
3743 wset_total_lines (r, make_number (new_size));
3744 else
3746 /* old_size is the old size of the frame's root window. */
3747 int old_size = XFASTINT (horflag ? r->total_cols
3748 : r->total_lines);
3749 Lisp_Object delta;
3751 XSETINT (delta, new_size - old_size);
3752 /* Try a "normal" resize first. */
3753 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3754 if (window_resize_check (r, horflag)
3755 && new_size == XINT (r->new_total))
3756 window_resize_apply (r, horflag);
3757 else
3759 /* Try with "reasonable" minimum sizes next. */
3760 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3761 if (window_resize_check (r, horflag)
3762 && new_size == XINT (r->new_total))
3763 window_resize_apply (r, horflag);
3764 else
3766 /* Finally, try with "safe" minimum sizes. */
3767 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3768 if (window_resize_check (r, horflag)
3769 && new_size == XINT (r->new_total))
3770 window_resize_apply (r, horflag);
3771 else
3773 /* We lost. Delete all windows but the frame's
3774 selected one. */
3775 root = f->selected_window;
3776 Fdelete_other_windows_internal (root, Qnil);
3777 if (horflag)
3778 wset_total_cols (XWINDOW (root), make_number (new_size));
3779 else
3780 wset_total_lines (XWINDOW (root), make_number (new_size));
3786 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3788 m = XWINDOW (mini);
3789 if (horflag)
3790 wset_total_cols (m, make_number (size));
3791 else
3793 /* Are we sure we always want 1 line here? */
3794 wset_total_lines (m, make_number (1));
3795 wset_top_line
3796 (m, make_number (XINT (r->top_line) + XINT (r->total_lines)));
3800 windows_or_buffers_changed++;
3804 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3805 doc: /* Split window OLD.
3806 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3807 new window. In any case TOTAL-SIZE must be a positive integer.
3809 Third argument SIDE nil (or `below') specifies that the new window shall
3810 be located below WINDOW. SIDE `above' means the new window shall be
3811 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3812 lines of the new window including space reserved for the mode and/or
3813 header line.
3815 SIDE t (or `right') specifies that the new window shall be located on
3816 the right side of WINDOW. SIDE `left' means the new window shall be
3817 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3818 number of columns of the new window including space reserved for fringes
3819 and the scrollbar or a divider column.
3821 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3822 according to the SIDE argument.
3824 The new total and normal sizes of all involved windows must have been
3825 set correctly. See the code of `split-window' for how this is done. */)
3826 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3828 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3829 parent window or an internal window we have to install as OLD's new
3830 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3831 provided OLD is a leaf window, or to the frame's selected window.
3832 NEW (*n) is the new window created with some parameters taken from
3833 REFERENCE (*r). */
3834 register Lisp_Object new, frame, reference;
3835 register struct window *o, *p, *n, *r;
3836 struct frame *f;
3837 int horflag
3838 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3839 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3840 int combination_limit = 0;
3842 CHECK_WINDOW (old);
3843 o = XWINDOW (old);
3844 frame = WINDOW_FRAME (o);
3845 f = XFRAME (frame);
3847 CHECK_NUMBER (total_size);
3849 /* Set combination_limit to 1 if we have to make a new parent window.
3850 We do that if either `window-combination-limit' is t, or OLD has no
3851 parent, or OLD is ortho-combined. */
3852 combination_limit =
3853 EQ (Vwindow_combination_limit, Qt)
3854 || NILP (o->parent)
3855 || NILP (horflag
3856 ? (XWINDOW (o->parent)->hchild)
3857 : (XWINDOW (o->parent)->vchild));
3859 /* We need a live reference window to initialize some parameters. */
3860 if (WINDOW_LIVE_P (old))
3861 /* OLD is live, use it as reference window. */
3862 reference = old;
3863 else
3864 /* Use the frame's selected window as reference window. */
3865 reference = FRAME_SELECTED_WINDOW (f);
3866 r = XWINDOW (reference);
3868 /* The following bugs are caught by `split-window'. */
3869 if (MINI_WINDOW_P (o))
3870 error ("Attempt to split minibuffer window");
3871 else if (XINT (total_size) < (horflag ? 2 : 1))
3872 error ("Size of new window too small (after split)");
3873 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3874 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3875 proportionally. */
3877 p = XWINDOW (o->parent);
3878 /* Temporarily pretend we split the parent window. */
3879 wset_new_total
3880 (p, make_number (XINT (horflag ? p->total_cols : p->total_lines)
3881 - XINT (total_size)));
3882 if (!window_resize_check (p, horflag))
3883 error ("Window sizes don't fit");
3884 else
3885 /* Undo the temporary pretension. */
3886 wset_new_total (p, horflag ? p->total_cols : p->total_lines);
3888 else
3890 if (!window_resize_check (o, horflag))
3891 error ("Resizing old window failed");
3892 else if (XINT (total_size) + XINT (o->new_total)
3893 != XINT (horflag ? o->total_cols : o->total_lines))
3894 error ("Sum of sizes of old and new window don't fit");
3897 /* This is our point of no return. */
3898 if (combination_limit)
3900 /* Save the old value of o->normal_cols/lines. It gets corrupted
3901 by make_parent_window and we need it below for assigning it to
3902 p->new_normal. */
3903 Lisp_Object new_normal
3904 = horflag ? o->normal_cols : o->normal_lines;
3906 make_parent_window (old, horflag);
3907 p = XWINDOW (o->parent);
3908 if (EQ (Vwindow_combination_limit, Qt))
3909 /* Store t in the new parent's combination_limit slot to avoid
3910 that its children get merged into another window. */
3911 wset_combination_limit (p, Qt);
3912 /* These get applied below. */
3913 wset_new_total (p, horflag ? o->total_cols : o->total_lines);
3914 wset_new_normal (p, new_normal);
3916 else
3917 p = XWINDOW (o->parent);
3919 windows_or_buffers_changed++;
3920 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3921 new = make_window ();
3922 n = XWINDOW (new);
3923 wset_frame (n, frame);
3924 wset_parent (n, o->parent);
3925 wset_vchild (n, Qnil);
3926 wset_hchild (n, Qnil);
3928 if (EQ (side, Qabove) || EQ (side, Qleft))
3930 wset_prev (n, o->prev);
3931 if (NILP (n->prev))
3932 if (horflag)
3933 wset_hchild (p, new);
3934 else
3935 wset_vchild (p, new);
3936 else
3937 wset_next (XWINDOW (n->prev), new);
3938 wset_next (n, old);
3939 wset_prev (o, new);
3941 else
3943 wset_next (n, o->next);
3944 if (!NILP (n->next))
3945 wset_prev (XWINDOW (n->next), new);
3946 wset_prev (n, old);
3947 wset_next (o, new);
3950 wset_window_end_valid (n, Qnil);
3951 memset (&n->last_cursor, 0, sizeof n->last_cursor);
3953 /* Get special geometry settings from reference window. */
3954 wset_left_margin_cols (n, r->left_margin_cols);
3955 wset_right_margin_cols (n, r->right_margin_cols);
3956 wset_left_fringe_width (n, r->left_fringe_width);
3957 wset_right_fringe_width (n, r->right_fringe_width);
3958 n->fringes_outside_margins = r->fringes_outside_margins;
3959 wset_scroll_bar_width (n, r->scroll_bar_width);
3960 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
3962 /* Directly assign orthogonal coordinates and sizes. */
3963 if (horflag)
3965 wset_top_line (n, o->top_line);
3966 wset_total_lines (n, o->total_lines);
3968 else
3970 wset_left_col (n, o->left_col);
3971 wset_total_cols (n, o->total_cols);
3974 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3975 get them ready here. */
3976 wset_new_total (n, total_size);
3977 wset_new_normal (n, normal_size);
3979 block_input ();
3980 window_resize_apply (p, horflag);
3981 adjust_glyphs (f);
3982 /* Set buffer of NEW to buffer of reference window. Don't run
3983 any hooks. */
3984 set_window_buffer (new, r->buffer, 0, 1);
3985 unblock_input ();
3987 /* Maybe we should run the scroll functions in Elisp (which already
3988 runs the configuration change hook). */
3989 if (! NILP (Vwindow_scroll_functions))
3990 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3991 Fmarker_position (n->start));
3992 /* Return NEW. */
3993 return new;
3997 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3998 doc: /* Remove WINDOW from its frame.
3999 WINDOW defaults to the selected window. Return nil.
4000 Signal an error when WINDOW is the only window on its frame. */)
4001 (register Lisp_Object window)
4003 register Lisp_Object parent, sibling, frame, root;
4004 struct window *w, *p, *s, *r;
4005 struct frame *f;
4006 int horflag;
4007 int before_sibling = 0;
4009 w = decode_any_window (window);
4010 XSETWINDOW (window, w);
4011 if (NILP (w->buffer)
4012 && NILP (w->hchild) && NILP (w->vchild))
4013 /* It's a no-op to delete an already deleted window. */
4014 return Qnil;
4016 parent = w->parent;
4017 if (NILP (parent))
4018 /* Never delete a minibuffer or frame root window. */
4019 error ("Attempt to delete minibuffer or sole ordinary window");
4020 else if (NILP (w->prev) && NILP (w->next))
4021 /* Rather bow out here, this case should be handled on the Elisp
4022 level. */
4023 error ("Attempt to delete sole window of parent");
4025 p = XWINDOW (parent);
4026 horflag = NILP (p->vchild);
4028 frame = WINDOW_FRAME (w);
4029 f = XFRAME (frame);
4031 root = FRAME_ROOT_WINDOW (f);
4032 r = XWINDOW (root);
4034 /* Unlink WINDOW from window tree. */
4035 if (NILP (w->prev))
4036 /* Get SIBLING below (on the right of) WINDOW. */
4038 /* before_sibling 1 means WINDOW is the first child of its
4039 parent and thus before the sibling. */
4040 before_sibling = 1;
4041 sibling = w->next;
4042 s = XWINDOW (sibling);
4043 wset_prev (s, Qnil);
4044 if (horflag)
4045 wset_hchild (p, sibling);
4046 else
4047 wset_vchild (p, sibling);
4049 else
4050 /* Get SIBLING above (on the left of) WINDOW. */
4052 sibling = w->prev;
4053 s = XWINDOW (sibling);
4054 wset_next (s, w->next);
4055 if (!NILP (s->next))
4056 wset_prev (XWINDOW (s->next), sibling);
4059 if (window_resize_check (r, horflag)
4060 && EQ (r->new_total,
4061 (horflag ? r->total_cols : r->total_lines)))
4062 /* We can delete WINDOW now. */
4065 /* Block input. */
4066 block_input ();
4067 window_resize_apply (p, horflag);
4069 /* If this window is referred to by the dpyinfo's mouse
4070 highlight, invalidate that slot to be safe (Bug#9904). */
4071 if (!FRAME_INITIAL_P (f))
4073 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4075 if (EQ (hlinfo->mouse_face_window, window))
4076 hlinfo->mouse_face_window = Qnil;
4079 windows_or_buffers_changed++;
4080 Vwindow_list = Qnil;
4081 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4083 wset_next (w, Qnil); /* Don't delete w->next too. */
4084 free_window_matrices (w);
4086 if (!NILP (w->vchild))
4088 delete_all_child_windows (w->vchild);
4089 wset_vchild (w, Qnil);
4091 else if (!NILP (w->hchild))
4093 delete_all_child_windows (w->hchild);
4094 wset_hchild (w, Qnil);
4096 else if (!NILP (w->buffer))
4098 unshow_buffer (w);
4099 unchain_marker (XMARKER (w->pointm));
4100 unchain_marker (XMARKER (w->start));
4101 wset_buffer (w, Qnil);
4104 if (NILP (s->prev) && NILP (s->next))
4105 /* A matrjoshka where SIBLING has become the only child of
4106 PARENT. */
4108 /* Put SIBLING into PARENT's place. */
4109 replace_window (parent, sibling, 0);
4110 /* Have SIBLING inherit the following three slot values from
4111 PARENT (the combination_limit slot is not inherited). */
4112 wset_normal_cols (s, p->normal_cols);
4113 wset_normal_lines (s, p->normal_lines);
4114 /* Mark PARENT as deleted. */
4115 wset_vchild (p, Qnil);
4116 wset_hchild (p, Qnil);
4117 /* Try to merge SIBLING into its new parent. */
4118 recombine_windows (sibling);
4121 adjust_glyphs (f);
4123 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4124 /* We deleted the frame's selected window. */
4126 /* Use the frame's first window as fallback ... */
4127 Lisp_Object new_selected_window = Fframe_first_window (frame);
4128 /* ... but preferably use its most recently used window. */
4129 Lisp_Object mru_window;
4131 /* `get-mru-window' might fail for some reason so play it safe
4132 - promote the first window _without recording it_ first. */
4133 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4134 Fselect_window (new_selected_window, Qt);
4135 else
4136 fset_selected_window (f, new_selected_window);
4138 unblock_input ();
4140 /* Now look whether `get-mru-window' gets us something. */
4141 mru_window = call1 (Qget_mru_window, frame);
4142 if (WINDOW_LIVE_P (mru_window)
4143 && EQ (XWINDOW (mru_window)->frame, frame))
4144 new_selected_window = mru_window;
4146 /* If all ended up well, we now promote the mru window. */
4147 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4148 Fselect_window (new_selected_window, Qnil);
4149 else
4150 fset_selected_window (f, new_selected_window);
4152 else
4153 unblock_input ();
4155 /* Must be run by the caller:
4156 run_window_configuration_change_hook (f); */
4158 else
4159 /* We failed: Relink WINDOW into window tree. */
4161 if (before_sibling)
4163 wset_prev (s, window);
4164 if (horflag)
4165 wset_hchild (p, window);
4166 else
4167 wset_vchild (p, window);
4169 else
4171 wset_next (s, window);
4172 if (!NILP (w->next))
4173 wset_prev (XWINDOW (w->next), window);
4175 error ("Deletion failed");
4178 return Qnil;
4181 /***********************************************************************
4182 Resizing Mini-Windows
4183 ***********************************************************************/
4185 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4186 can. */
4187 void
4188 grow_mini_window (struct window *w, int delta)
4190 struct frame *f = XFRAME (w->frame);
4191 struct window *r;
4192 Lisp_Object root, value;
4194 eassert (MINI_WINDOW_P (w));
4195 eassert (delta >= 0);
4197 root = FRAME_ROOT_WINDOW (f);
4198 r = XWINDOW (root);
4199 value = call2 (Qwindow_resize_root_window_vertically,
4200 root, make_number (- delta));
4201 if (INTEGERP (value) && window_resize_check (r, 0))
4203 block_input ();
4204 window_resize_apply (r, 0);
4206 /* Grow the mini-window. */
4207 wset_top_line
4208 (w, make_number (XFASTINT (r->top_line) + XFASTINT (r->total_lines)));
4209 wset_total_lines
4210 (w, make_number (XFASTINT (w->total_lines) - XINT (value)));
4211 w->last_modified = 0;
4212 w->last_overlay_modified = 0;
4214 windows_or_buffers_changed++;
4215 adjust_glyphs (f);
4216 unblock_input ();
4221 /* Shrink mini-window W. */
4222 void
4223 shrink_mini_window (struct window *w)
4225 struct frame *f = XFRAME (w->frame);
4226 struct window *r;
4227 Lisp_Object root, value;
4228 EMACS_INT size;
4230 eassert (MINI_WINDOW_P (w));
4232 size = XINT (w->total_lines);
4233 if (size > 1)
4235 root = FRAME_ROOT_WINDOW (f);
4236 r = XWINDOW (root);
4237 value = call2 (Qwindow_resize_root_window_vertically,
4238 root, make_number (size - 1));
4239 if (INTEGERP (value) && window_resize_check (r, 0))
4241 block_input ();
4242 window_resize_apply (r, 0);
4244 /* Shrink the mini-window. */
4245 wset_top_line (w, make_number (XFASTINT (r->top_line)
4246 + XFASTINT (r->total_lines)));
4247 wset_total_lines (w, make_number (1));
4249 w->last_modified = 0;
4250 w->last_overlay_modified = 0;
4252 windows_or_buffers_changed++;
4253 adjust_glyphs (f);
4254 unblock_input ();
4256 /* If the above failed for whatever strange reason we must make a
4257 one window frame here. The same routine will be needed when
4258 shrinking the frame (and probably when making the initial
4259 *scratch* window). For the moment leave things as they are. */
4263 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4264 doc: /* Resize minibuffer window WINDOW. */)
4265 (Lisp_Object window)
4267 struct window *w = XWINDOW (window);
4268 struct window *r;
4269 struct frame *f;
4270 int height;
4272 CHECK_WINDOW (window);
4273 f = XFRAME (w->frame);
4275 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4276 error ("Not a valid minibuffer window");
4277 else if (FRAME_MINIBUF_ONLY_P (f))
4278 error ("Cannot resize a minibuffer-only frame");
4280 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4281 height = XINT (r->total_lines) + XINT (w->total_lines);
4282 if (window_resize_check (r, 0)
4283 && XINT (w->new_total) > 0
4284 && height == XINT (r->new_total) + XINT (w->new_total))
4286 block_input ();
4287 window_resize_apply (r, 0);
4289 wset_total_lines (w, w->new_total);
4290 wset_top_line (w, make_number (XINT (r->top_line)
4291 + XINT (r->total_lines)));
4293 windows_or_buffers_changed++;
4294 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4295 adjust_glyphs (f);
4296 unblock_input ();
4298 run_window_configuration_change_hook (f);
4299 return Qt;
4301 else error ("Failed to resize minibuffer window");
4304 /* Mark window cursors off for all windows in the window tree rooted
4305 at W by setting their phys_cursor_on_p flag to zero. Called from
4306 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4307 the frame are cleared. */
4309 void
4310 mark_window_cursors_off (struct window *w)
4312 while (w)
4314 if (!NILP (w->hchild))
4315 mark_window_cursors_off (XWINDOW (w->hchild));
4316 else if (!NILP (w->vchild))
4317 mark_window_cursors_off (XWINDOW (w->vchild));
4318 else
4319 w->phys_cursor_on_p = 0;
4321 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4326 /* Return number of lines of text (not counting mode lines) in W. */
4329 window_internal_height (struct window *w)
4331 int ht = XFASTINT (w->total_lines);
4333 if (!MINI_WINDOW_P (w))
4335 if (!NILP (w->parent)
4336 || !NILP (w->vchild)
4337 || !NILP (w->hchild)
4338 || !NILP (w->next)
4339 || !NILP (w->prev)
4340 || WINDOW_WANTS_MODELINE_P (w))
4341 --ht;
4343 if (WINDOW_WANTS_HEADER_LINE_P (w))
4344 --ht;
4347 return ht;
4350 /************************************************************************
4351 Window Scrolling
4352 ***********************************************************************/
4354 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4355 N screen-fulls, which is defined as the height of the window minus
4356 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4357 instead. Negative values of N mean scroll down. NOERROR non-zero
4358 means don't signal an error if we try to move over BEGV or ZV,
4359 respectively. */
4361 static void
4362 window_scroll (Lisp_Object window, EMACS_INT n, int whole, int noerror)
4364 immediate_quit = 1;
4365 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4367 /* If we must, use the pixel-based version which is much slower than
4368 the line-based one but can handle varying line heights. */
4369 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4370 window_scroll_pixel_based (window, n, whole, noerror);
4371 else
4372 window_scroll_line_based (window, n, whole, noerror);
4374 immediate_quit = 0;
4378 /* Implementation of window_scroll that works based on pixel line
4379 heights. See the comment of window_scroll for parameter
4380 descriptions. */
4382 static void
4383 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4385 struct it it;
4386 struct window *w = XWINDOW (window);
4387 struct text_pos start;
4388 int this_scroll_margin;
4389 /* True if we fiddled the window vscroll field without really scrolling. */
4390 int vscrolled = 0;
4391 int x, y, rtop, rbot, rowh, vpos;
4392 void *itdata = NULL;
4394 SET_TEXT_POS_FROM_MARKER (start, w->start);
4395 /* Scrolling a minibuffer window via scroll bar when the echo area
4396 shows long text sometimes resets the minibuffer contents behind
4397 our backs. */
4398 if (CHARPOS (start) > ZV)
4399 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4401 /* If PT is not visible in WINDOW, move back one half of
4402 the screen. Allow PT to be partially visible, otherwise
4403 something like (scroll-down 1) with PT in the line before
4404 the partially visible one would recenter. */
4406 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4408 itdata = bidi_shelve_cache ();
4409 /* Move backward half the height of the window. Performance note:
4410 vmotion used here is about 10% faster, but would give wrong
4411 results for variable height lines. */
4412 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4413 it.current_y = it.last_visible_y;
4414 move_it_vertically_backward (&it, window_box_height (w) / 2);
4416 /* The function move_iterator_vertically may move over more than
4417 the specified y-distance. If it->w is small, e.g. a
4418 mini-buffer window, we may end up in front of the window's
4419 display area. This is the case when Start displaying at the
4420 start of the line containing PT in this case. */
4421 if (it.current_y <= 0)
4423 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4424 move_it_vertically_backward (&it, 0);
4425 it.current_y = 0;
4428 start = it.current.pos;
4429 bidi_unshelve_cache (itdata, 0);
4431 else if (auto_window_vscroll_p)
4433 if (rtop || rbot) /* partially visible */
4435 int px;
4436 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4437 if (whole)
4438 dy = max ((window_box_height (w)
4439 - next_screen_context_lines * dy),
4440 dy);
4441 dy *= n;
4443 if (n < 0)
4445 /* Only vscroll backwards if already vscrolled forwards. */
4446 if (w->vscroll < 0 && rtop > 0)
4448 px = max (0, -w->vscroll - min (rtop, -dy));
4449 Fset_window_vscroll (window, make_number (px), Qt);
4450 return;
4453 if (n > 0)
4455 /* Do vscroll if already vscrolled or only display line. */
4456 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4458 px = max (0, -w->vscroll + min (rbot, dy));
4459 Fset_window_vscroll (window, make_number (px), Qt);
4460 return;
4463 /* Maybe modify window start instead of scrolling. */
4464 if (rbot > 0 || w->vscroll < 0)
4466 ptrdiff_t spos;
4468 Fset_window_vscroll (window, make_number (0), Qt);
4469 /* If there are other text lines above the current row,
4470 move window start to current row. Else to next row. */
4471 if (rbot > 0)
4472 spos = XINT (Fline_beginning_position (Qnil));
4473 else
4474 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4475 set_marker_restricted (w->start, make_number (spos),
4476 w->buffer);
4477 w->start_at_line_beg = 1;
4478 w->update_mode_line = 1;
4479 w->last_modified = 0;
4480 w->last_overlay_modified = 0;
4481 /* Set force_start so that redisplay_window will run the
4482 window-scroll-functions. */
4483 w->force_start = 1;
4484 return;
4488 /* Cancel previous vscroll. */
4489 Fset_window_vscroll (window, make_number (0), Qt);
4492 itdata = bidi_shelve_cache ();
4493 /* If scroll_preserve_screen_position is non-nil, we try to set
4494 point in the same window line as it is now, so get that line. */
4495 if (!NILP (Vscroll_preserve_screen_position))
4497 /* We preserve the goal pixel coordinate across consecutive
4498 calls to scroll-up, scroll-down and other commands that
4499 have the `scroll-command' property. This avoids the
4500 possibility of point becoming "stuck" on a tall line when
4501 scrolling by one line. */
4502 if (window_scroll_pixel_based_preserve_y < 0
4503 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4504 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4506 start_display (&it, w, start);
4507 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4508 window_scroll_pixel_based_preserve_y = it.current_y;
4509 window_scroll_pixel_based_preserve_x = it.current_x;
4512 else
4513 window_scroll_pixel_based_preserve_y
4514 = window_scroll_pixel_based_preserve_x = -1;
4516 /* Move iterator it from start the specified distance forward or
4517 backward. The result is the new window start. */
4518 start_display (&it, w, start);
4519 if (whole)
4521 ptrdiff_t start_pos = IT_CHARPOS (it);
4522 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4523 dy = max ((window_box_height (w)
4524 - next_screen_context_lines * dy),
4525 dy) * n;
4527 /* Note that move_it_vertically always moves the iterator to the
4528 start of a line. So, if the last line doesn't have a newline,
4529 we would end up at the start of the line ending at ZV. */
4530 if (dy <= 0)
4532 move_it_vertically_backward (&it, -dy);
4533 /* Ensure we actually do move, e.g. in case we are currently
4534 looking at an image that is taller that the window height. */
4535 while (start_pos == IT_CHARPOS (it)
4536 && start_pos > BEGV)
4537 move_it_by_lines (&it, -1);
4539 else if (dy > 0)
4541 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4542 MOVE_TO_POS | MOVE_TO_Y);
4543 /* Ensure we actually do move, e.g. in case we are currently
4544 looking at an image that is taller that the window height. */
4545 while (start_pos == IT_CHARPOS (it)
4546 && start_pos < ZV)
4547 move_it_by_lines (&it, 1);
4550 else
4551 move_it_by_lines (&it, n);
4553 /* We failed if we find ZV is already on the screen (scrolling up,
4554 means there's nothing past the end), or if we can't start any
4555 earlier (scrolling down, means there's nothing past the top). */
4556 if ((n > 0 && IT_CHARPOS (it) == ZV)
4557 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4559 if (IT_CHARPOS (it) == ZV)
4561 if (it.current_y < it.last_visible_y
4562 && (it.current_y + it.max_ascent + it.max_descent
4563 > it.last_visible_y))
4565 /* The last line was only partially visible, make it fully
4566 visible. */
4567 w->vscroll = (it.last_visible_y
4568 - it.current_y + it.max_ascent + it.max_descent);
4569 adjust_glyphs (it.f);
4571 else
4573 bidi_unshelve_cache (itdata, 0);
4574 if (noerror)
4575 return;
4576 else if (n < 0) /* could happen with empty buffers */
4577 xsignal0 (Qbeginning_of_buffer);
4578 else
4579 xsignal0 (Qend_of_buffer);
4582 else
4584 if (w->vscroll != 0)
4585 /* The first line was only partially visible, make it fully
4586 visible. */
4587 w->vscroll = 0;
4588 else
4590 bidi_unshelve_cache (itdata, 0);
4591 if (noerror)
4592 return;
4593 else
4594 xsignal0 (Qbeginning_of_buffer);
4598 /* If control gets here, then we vscrolled. */
4600 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4602 /* Don't try to change the window start below. */
4603 vscrolled = 1;
4606 if (! vscrolled)
4608 ptrdiff_t pos = IT_CHARPOS (it);
4609 ptrdiff_t bytepos;
4611 /* If in the middle of a multi-glyph character move forward to
4612 the next character. */
4613 if (in_display_vector_p (&it))
4615 ++pos;
4616 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4619 /* Set the window start, and set up the window for redisplay. */
4620 set_marker_restricted (w->start, make_number (pos),
4621 w->buffer);
4622 bytepos = marker_byte_position (w->start);
4623 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4624 w->update_mode_line = 1;
4625 w->last_modified = 0;
4626 w->last_overlay_modified = 0;
4627 /* Set force_start so that redisplay_window will run the
4628 window-scroll-functions. */
4629 w->force_start = 1;
4632 /* The rest of this function uses current_y in a nonstandard way,
4633 not including the height of the header line if any. */
4634 it.current_y = it.vpos = 0;
4636 /* Move PT out of scroll margins.
4637 This code wants current_y to be zero at the window start position
4638 even if there is a header line. */
4639 this_scroll_margin = max (0, scroll_margin);
4640 this_scroll_margin
4641 = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4642 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4644 if (n > 0)
4646 /* We moved the window start towards ZV, so PT may be now
4647 in the scroll margin at the top. */
4648 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4649 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4650 && (NILP (Vscroll_preserve_screen_position)
4651 || EQ (Vscroll_preserve_screen_position, Qt)))
4652 /* We found PT at a legitimate height. Leave it alone. */
4654 else if (window_scroll_pixel_based_preserve_y >= 0)
4656 /* If we have a header line, take account of it.
4657 This is necessary because we set it.current_y to 0, above. */
4658 move_it_to (&it, -1,
4659 window_scroll_pixel_based_preserve_x,
4660 window_scroll_pixel_based_preserve_y
4661 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4662 -1, MOVE_TO_Y | MOVE_TO_X);
4663 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4665 else
4667 while (it.current_y < this_scroll_margin)
4669 int prev = it.current_y;
4670 move_it_by_lines (&it, 1);
4671 if (prev == it.current_y)
4672 break;
4674 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4677 else if (n < 0)
4679 ptrdiff_t charpos, bytepos;
4680 int partial_p;
4682 /* Save our position, for the
4683 window_scroll_pixel_based_preserve_y case. */
4684 charpos = IT_CHARPOS (it);
4685 bytepos = IT_BYTEPOS (it);
4687 /* We moved the window start towards BEGV, so PT may be now
4688 in the scroll margin at the bottom. */
4689 move_it_to (&it, PT, -1,
4690 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4691 - this_scroll_margin - 1),
4693 MOVE_TO_POS | MOVE_TO_Y);
4695 /* Save our position, in case it's correct. */
4696 charpos = IT_CHARPOS (it);
4697 bytepos = IT_BYTEPOS (it);
4699 /* See if point is on a partially visible line at the end. */
4700 if (it.what == IT_EOB)
4701 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4702 else
4704 move_it_by_lines (&it, 1);
4705 partial_p = it.current_y > it.last_visible_y;
4708 if (charpos == PT && !partial_p
4709 && (NILP (Vscroll_preserve_screen_position)
4710 || EQ (Vscroll_preserve_screen_position, Qt)))
4711 /* We found PT before we found the display margin, so PT is ok. */
4713 else if (window_scroll_pixel_based_preserve_y >= 0)
4715 SET_TEXT_POS_FROM_MARKER (start, w->start);
4716 start_display (&it, w, start);
4717 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4718 here because we called start_display again and did not
4719 alter it.current_y this time. */
4720 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4721 window_scroll_pixel_based_preserve_y, -1,
4722 MOVE_TO_Y | MOVE_TO_X);
4723 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4725 else
4727 if (partial_p)
4728 /* The last line was only partially visible, so back up two
4729 lines to make sure we're on a fully visible line. */
4731 move_it_by_lines (&it, -2);
4732 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4734 else
4735 /* No, the position we saved is OK, so use it. */
4736 SET_PT_BOTH (charpos, bytepos);
4739 bidi_unshelve_cache (itdata, 0);
4743 /* Implementation of window_scroll that works based on screen lines.
4744 See the comment of window_scroll for parameter descriptions. */
4746 static void
4747 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
4749 register struct window *w = XWINDOW (window);
4750 /* Fvertical_motion enters redisplay, which can trigger
4751 fontification, which in turn can modify buffer text (e.g., if the
4752 fontification functions replace escape sequences with faces, as
4753 in `grep-mode-font-lock-keywords'). So we use a marker to record
4754 the old point position, to prevent crashes in SET_PT_BOTH. */
4755 Lisp_Object opoint_marker = Fpoint_marker ();
4756 register ptrdiff_t pos, pos_byte;
4757 register int ht = window_internal_height (w);
4758 register Lisp_Object tem;
4759 int lose;
4760 Lisp_Object bolp;
4761 ptrdiff_t startpos;
4762 Lisp_Object original_pos = Qnil;
4764 /* If scrolling screen-fulls, compute the number of lines to
4765 scroll from the window's height. */
4766 if (whole)
4767 n *= max (1, ht - next_screen_context_lines);
4769 startpos = marker_position (w->start);
4771 if (!NILP (Vscroll_preserve_screen_position))
4773 if (window_scroll_preserve_vpos <= 0
4774 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4775 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4777 struct position posit
4778 = *compute_motion (startpos, 0, 0, 0,
4779 PT, ht, 0,
4780 -1, w->hscroll,
4781 0, w);
4782 window_scroll_preserve_vpos = posit.vpos;
4783 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
4786 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4787 make_number (window_scroll_preserve_vpos));
4790 XSETFASTINT (tem, PT);
4791 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4793 if (NILP (tem))
4795 Fvertical_motion (make_number (- (ht / 2)), window);
4796 startpos = PT;
4799 SET_PT (startpos);
4800 lose = n < 0 && PT == BEGV;
4801 Fvertical_motion (make_number (n), window);
4802 pos = PT;
4803 pos_byte = PT_BYTE;
4804 bolp = Fbolp ();
4805 SET_PT_BOTH (marker_position (opoint_marker),
4806 marker_byte_position (opoint_marker));
4808 if (lose)
4810 if (noerror)
4811 return;
4812 else
4813 xsignal0 (Qbeginning_of_buffer);
4816 if (pos < ZV)
4818 /* Don't use a scroll margin that is negative or too large. */
4819 int this_scroll_margin =
4820 max (0, min (scroll_margin, XINT (w->total_lines) / 4));
4822 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4823 w->start_at_line_beg = !NILP (bolp);
4824 w->update_mode_line = 1;
4825 w->last_modified = 0;
4826 w->last_overlay_modified = 0;
4827 /* Set force_start so that redisplay_window will run
4828 the window-scroll-functions. */
4829 w->force_start = 1;
4831 if (!NILP (Vscroll_preserve_screen_position)
4832 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4834 SET_PT_BOTH (pos, pos_byte);
4835 Fvertical_motion (original_pos, window);
4837 /* If we scrolled forward, put point enough lines down
4838 that it is outside the scroll margin. */
4839 else if (n > 0)
4841 int top_margin;
4843 if (this_scroll_margin > 0)
4845 SET_PT_BOTH (pos, pos_byte);
4846 Fvertical_motion (make_number (this_scroll_margin), window);
4847 top_margin = PT;
4849 else
4850 top_margin = pos;
4852 if (top_margin <= marker_position (opoint_marker))
4853 SET_PT_BOTH (marker_position (opoint_marker),
4854 marker_byte_position (opoint_marker));
4855 else if (!NILP (Vscroll_preserve_screen_position))
4857 SET_PT_BOTH (pos, pos_byte);
4858 Fvertical_motion (original_pos, window);
4860 else
4861 SET_PT (top_margin);
4863 else if (n < 0)
4865 int bottom_margin;
4867 /* If we scrolled backward, put point near the end of the window
4868 but not within the scroll margin. */
4869 SET_PT_BOTH (pos, pos_byte);
4870 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4871 if (XFASTINT (tem) == ht - this_scroll_margin)
4872 bottom_margin = PT;
4873 else
4874 bottom_margin = PT + 1;
4876 if (bottom_margin > marker_position (opoint_marker))
4877 SET_PT_BOTH (marker_position (opoint_marker),
4878 marker_byte_position (opoint_marker));
4879 else
4881 if (!NILP (Vscroll_preserve_screen_position))
4883 SET_PT_BOTH (pos, pos_byte);
4884 Fvertical_motion (original_pos, window);
4886 else
4887 Fvertical_motion (make_number (-1), window);
4891 else
4893 if (noerror)
4894 return;
4895 else
4896 xsignal0 (Qend_of_buffer);
4901 /* Scroll selected_window up or down. If N is nil, scroll a
4902 screen-full which is defined as the height of the window minus
4903 next_screen_context_lines. If N is the symbol `-', scroll.
4904 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4905 up. This is the guts of Fscroll_up and Fscroll_down. */
4907 static void
4908 scroll_command (Lisp_Object n, int direction)
4910 ptrdiff_t count = SPECPDL_INDEX ();
4912 eassert (eabs (direction) == 1);
4914 /* If selected window's buffer isn't current, make it current for
4915 the moment. But don't screw up if window_scroll gets an error. */
4916 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4918 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4919 Fset_buffer (XWINDOW (selected_window)->buffer);
4921 /* Make redisplay consider other windows than just selected_window. */
4922 ++windows_or_buffers_changed;
4925 if (NILP (n))
4926 window_scroll (selected_window, direction, 1, 0);
4927 else if (EQ (n, Qminus))
4928 window_scroll (selected_window, -direction, 1, 0);
4929 else
4931 n = Fprefix_numeric_value (n);
4932 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4935 unbind_to (count, Qnil);
4938 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4939 doc: /* Scroll text of selected window upward ARG lines.
4940 If ARG is omitted or nil, scroll upward by a near full screen.
4941 A near full screen is `next-screen-context-lines' less than a full screen.
4942 Negative ARG means scroll downward.
4943 If ARG is the atom `-', scroll downward by nearly full screen.
4944 When calling from a program, supply as argument a number, nil, or `-'. */)
4945 (Lisp_Object arg)
4947 scroll_command (arg, 1);
4948 return Qnil;
4951 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4952 doc: /* Scroll text of selected window down ARG lines.
4953 If ARG is omitted or nil, scroll down by a near full screen.
4954 A near full screen is `next-screen-context-lines' less than a full screen.
4955 Negative ARG means scroll upward.
4956 If ARG is the atom `-', scroll upward by nearly full screen.
4957 When calling from a program, supply as argument a number, nil, or `-'. */)
4958 (Lisp_Object arg)
4960 scroll_command (arg, -1);
4961 return Qnil;
4964 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4965 doc: /* Return the other window for \"other window scroll\" commands.
4966 If `other-window-scroll-buffer' is non-nil, a window
4967 showing that buffer is used.
4968 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4969 specifies the window. This takes precedence over
4970 `other-window-scroll-buffer'. */)
4971 (void)
4973 Lisp_Object window;
4975 if (MINI_WINDOW_P (XWINDOW (selected_window))
4976 && !NILP (Vminibuf_scroll_window))
4977 window = Vminibuf_scroll_window;
4978 /* If buffer is specified, scroll that buffer. */
4979 else if (!NILP (Vother_window_scroll_buffer))
4981 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4982 if (NILP (window))
4983 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4985 else
4987 /* Nothing specified; look for a neighboring window on the same
4988 frame. */
4989 window = Fnext_window (selected_window, Qnil, Qnil);
4991 if (EQ (window, selected_window))
4992 /* That didn't get us anywhere; look for a window on another
4993 visible frame. */
4995 window = Fnext_window (window, Qnil, Qt);
4996 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4997 && ! EQ (window, selected_window));
5000 CHECK_LIVE_WINDOW (window);
5002 if (EQ (window, selected_window))
5003 error ("There is no other window");
5005 return window;
5008 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5009 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5010 A near full screen is `next-screen-context-lines' less than a full screen.
5011 The next window is the one below the current one; or the one at the top
5012 if the current one is at the bottom. Negative ARG means scroll downward.
5013 If ARG is the atom `-', scroll downward by nearly full screen.
5014 When calling from a program, supply as argument a number, nil, or `-'.
5016 If `other-window-scroll-buffer' is non-nil, scroll the window
5017 showing that buffer, popping the buffer up if necessary.
5018 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5019 specifies the window to scroll. This takes precedence over
5020 `other-window-scroll-buffer'. */)
5021 (Lisp_Object arg)
5023 Lisp_Object window;
5024 struct window *w;
5025 ptrdiff_t count = SPECPDL_INDEX ();
5027 window = Fother_window_for_scrolling ();
5028 w = XWINDOW (window);
5030 /* Don't screw up if window_scroll gets an error. */
5031 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5032 ++windows_or_buffers_changed;
5034 Fset_buffer (w->buffer);
5035 SET_PT (marker_position (w->pointm));
5037 if (NILP (arg))
5038 window_scroll (window, 1, 1, 1);
5039 else if (EQ (arg, Qminus))
5040 window_scroll (window, -1, 1, 1);
5041 else
5043 if (CONSP (arg))
5044 arg = XCAR (arg);
5045 CHECK_NUMBER (arg);
5046 window_scroll (window, XINT (arg), 0, 1);
5049 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5050 unbind_to (count, Qnil);
5052 return Qnil;
5055 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5056 doc: /* Scroll selected window display ARG columns left.
5057 Default for ARG is window width minus 2.
5058 Value is the total amount of leftward horizontal scrolling in
5059 effect after the change.
5060 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5061 lower bound for automatic scrolling, i.e. automatic scrolling
5062 will not scroll a window to a column less than the value returned
5063 by this function. This happens in an interactive call. */)
5064 (register Lisp_Object arg, Lisp_Object set_minimum)
5066 struct window *w = XWINDOW (selected_window);
5067 EMACS_INT requested_arg = (NILP (arg)
5068 ? window_body_cols (w) - 2
5069 : XINT (Fprefix_numeric_value (arg)));
5070 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
5072 if (!NILP (set_minimum))
5073 w->min_hscroll = w->hscroll;
5075 return result;
5078 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5079 doc: /* Scroll selected window display ARG columns right.
5080 Default for ARG is window width minus 2.
5081 Value is the total amount of leftward horizontal scrolling in
5082 effect after the change.
5083 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5084 lower bound for automatic scrolling, i.e. automatic scrolling
5085 will not scroll a window to a column less than the value returned
5086 by this function. This happens in an interactive call. */)
5087 (register Lisp_Object arg, Lisp_Object set_minimum)
5089 struct window *w = XWINDOW (selected_window);
5090 EMACS_INT requested_arg = (NILP (arg)
5091 ? window_body_cols (w) - 2
5092 : XINT (Fprefix_numeric_value (arg)));
5093 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5095 if (!NILP (set_minimum))
5096 w->min_hscroll = w->hscroll;
5098 return result;
5101 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5102 doc: /* Return the window which was selected when entering the minibuffer.
5103 Returns nil, if selected window is not a minibuffer window. */)
5104 (void)
5106 if (minibuf_level > 0
5107 && MINI_WINDOW_P (XWINDOW (selected_window))
5108 && WINDOW_LIVE_P (minibuf_selected_window))
5109 return minibuf_selected_window;
5111 return Qnil;
5114 /* Value is the number of lines actually displayed in window W,
5115 as opposed to its height. */
5117 static int
5118 displayed_window_lines (struct window *w)
5120 struct it it;
5121 struct text_pos start;
5122 ptrdiff_t charpos = marker_position (w->start);
5123 int height = window_box_height (w);
5124 struct buffer *old_buffer;
5125 int bottom_y;
5126 void *itdata = NULL;
5128 if (XBUFFER (w->buffer) != current_buffer)
5130 old_buffer = current_buffer;
5131 set_buffer_internal (XBUFFER (w->buffer));
5133 else
5134 old_buffer = NULL;
5136 /* In case W->start is out of the accessible range, do something
5137 reasonable. This happens in Info mode when Info-scroll-down
5138 calls (recenter -1) while W->start is 1. */
5139 if (charpos < BEGV)
5140 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5141 else if (charpos > ZV)
5142 SET_TEXT_POS (start, ZV, ZV_BYTE);
5143 else
5144 SET_TEXT_POS_FROM_MARKER (start, w->start);
5146 itdata = bidi_shelve_cache ();
5147 start_display (&it, w, start);
5148 move_it_vertically (&it, height);
5149 bottom_y = line_bottom_y (&it);
5150 bidi_unshelve_cache (itdata, 0);
5152 /* rms: On a non-window display,
5153 the value of it.vpos at the bottom of the screen
5154 seems to be 1 larger than window_box_height (w).
5155 This kludge fixes a bug whereby (move-to-window-line -1)
5156 when ZV is on the last screen line
5157 moves to the previous screen line instead of the last one. */
5158 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5159 height++;
5161 /* Add in empty lines at the bottom of the window. */
5162 if (bottom_y < height)
5164 int uy = FRAME_LINE_HEIGHT (it.f);
5165 it.vpos += (height - bottom_y + uy - 1) / uy;
5168 if (old_buffer)
5169 set_buffer_internal (old_buffer);
5171 return it.vpos;
5175 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5176 doc: /* Center point in selected window and maybe redisplay frame.
5177 With prefix argument ARG, recenter putting point on screen line ARG
5178 relative to the selected window. If ARG is negative, it counts up from the
5179 bottom of the window. (ARG should be less than the height of the window.)
5181 If ARG is omitted or nil, then recenter with point on the middle line of
5182 the selected window; if the variable `recenter-redisplay' is non-nil,
5183 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5184 is set to `grow-only', this resets the tool-bar's height to the minimum
5185 height needed); if `recenter-redisplay' has the special value `tty',
5186 then only tty frames are redrawn.
5188 Just C-u as prefix means put point in the center of the window
5189 and redisplay normally--don't erase and redraw the frame. */)
5190 (register Lisp_Object arg)
5192 struct window *w = XWINDOW (selected_window);
5193 struct buffer *buf = XBUFFER (w->buffer);
5194 struct buffer *obuf = current_buffer;
5195 int center_p = 0;
5196 ptrdiff_t charpos, bytepos;
5197 EMACS_INT iarg IF_LINT (= 0);
5198 int this_scroll_margin;
5200 /* If redisplay is suppressed due to an error, try again. */
5201 obuf->display_error_modiff = 0;
5203 if (NILP (arg))
5205 if (!NILP (Vrecenter_redisplay)
5206 && (!EQ (Vrecenter_redisplay, Qtty)
5207 || !NILP (Ftty_type (selected_frame))))
5209 ptrdiff_t i;
5211 /* Invalidate pixel data calculated for all compositions. */
5212 for (i = 0; i < n_compositions; i++)
5213 composition_table[i]->font = NULL;
5215 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5217 Fredraw_frame (WINDOW_FRAME (w));
5218 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5221 center_p = 1;
5223 else if (CONSP (arg)) /* Just C-u. */
5224 center_p = 1;
5225 else
5227 arg = Fprefix_numeric_value (arg);
5228 CHECK_NUMBER (arg);
5229 iarg = XINT (arg);
5232 set_buffer_internal (buf);
5234 /* Do this after making BUF current
5235 in case scroll_margin is buffer-local. */
5236 this_scroll_margin =
5237 max (0, min (scroll_margin, XFASTINT (w->total_lines) / 4));
5239 /* Handle centering on a graphical frame specially. Such frames can
5240 have variable-height lines and centering point on the basis of
5241 line counts would lead to strange effects. */
5242 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5244 if (center_p)
5246 struct it it;
5247 struct text_pos pt;
5248 void *itdata = bidi_shelve_cache ();
5250 SET_TEXT_POS (pt, PT, PT_BYTE);
5251 start_display (&it, w, pt);
5252 move_it_vertically_backward (&it, window_box_height (w) / 2);
5253 charpos = IT_CHARPOS (it);
5254 bytepos = IT_BYTEPOS (it);
5255 bidi_unshelve_cache (itdata, 0);
5257 else if (iarg < 0)
5259 struct it it;
5260 struct text_pos pt;
5261 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5262 int extra_line_spacing;
5263 int h = window_box_height (w);
5264 void *itdata = bidi_shelve_cache ();
5266 iarg = - max (-iarg, this_scroll_margin);
5268 SET_TEXT_POS (pt, PT, PT_BYTE);
5269 start_display (&it, w, pt);
5271 /* Be sure we have the exact height of the full line containing PT. */
5272 move_it_by_lines (&it, 0);
5274 /* The amount of pixels we have to move back is the window
5275 height minus what's displayed in the line containing PT,
5276 and the lines below. */
5277 it.current_y = 0;
5278 it.vpos = 0;
5279 move_it_by_lines (&it, nlines);
5281 if (it.vpos == nlines)
5282 h -= it.current_y;
5283 else
5285 /* Last line has no newline */
5286 h -= line_bottom_y (&it);
5287 it.vpos++;
5290 /* Don't reserve space for extra line spacing of last line. */
5291 extra_line_spacing = it.max_extra_line_spacing;
5293 /* If we can't move down NLINES lines because we hit
5294 the end of the buffer, count in some empty lines. */
5295 if (it.vpos < nlines)
5297 nlines -= it.vpos;
5298 extra_line_spacing = it.extra_line_spacing;
5299 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5301 if (h <= 0)
5303 bidi_unshelve_cache (itdata, 0);
5304 return Qnil;
5307 /* Now find the new top line (starting position) of the window. */
5308 start_display (&it, w, pt);
5309 it.current_y = 0;
5310 move_it_vertically_backward (&it, h);
5312 /* If extra line spacing is present, we may move too far
5313 back. This causes the last line to be only partially
5314 visible (which triggers redisplay to recenter that line
5315 in the middle), so move forward.
5316 But ignore extra line spacing on last line, as it is not
5317 considered to be part of the visible height of the line.
5319 h += extra_line_spacing;
5320 while (-it.current_y > h)
5321 move_it_by_lines (&it, 1);
5323 charpos = IT_CHARPOS (it);
5324 bytepos = IT_BYTEPOS (it);
5326 bidi_unshelve_cache (itdata, 0);
5328 else
5330 struct position pos;
5332 iarg = max (iarg, this_scroll_margin);
5334 pos = *vmotion (PT, -iarg, w);
5335 charpos = pos.bufpos;
5336 bytepos = pos.bytepos;
5339 else
5341 struct position pos;
5342 int ht = window_internal_height (w);
5344 if (center_p)
5345 iarg = ht / 2;
5346 else if (iarg < 0)
5347 iarg += ht;
5349 /* Don't let it get into the margin at either top or bottom. */
5350 iarg = clip_to_bounds (this_scroll_margin, iarg,
5351 ht - this_scroll_margin - 1);
5353 pos = *vmotion (PT, - iarg, w);
5354 charpos = pos.bufpos;
5355 bytepos = pos.bytepos;
5358 /* Set the new window start. */
5359 set_marker_both (w->start, w->buffer, charpos, bytepos);
5360 wset_window_end_valid (w, Qnil);
5362 w->optional_new_start = 1;
5364 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5365 FETCH_BYTE (bytepos - 1) == '\n');
5367 set_buffer_internal (obuf);
5368 return Qnil;
5371 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5372 0, 1, 0,
5373 doc: /* Return the height in lines of the text display area of WINDOW.
5374 WINDOW must be a live window and defaults to the selected one.
5376 The returned height does not include the mode line, any header line,
5377 nor any partial-height lines at the bottom of the text area. */)
5378 (Lisp_Object window)
5380 struct window *w = decode_live_window (window);
5381 int pixel_height = window_box_height (w);
5382 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5383 return make_number (line_height);
5388 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5389 1, 1, "P",
5390 doc: /* Position point relative to window.
5391 ARG nil means position point at center of window.
5392 Else, ARG specifies vertical position within the window;
5393 zero means top of window, negative means relative to bottom of window. */)
5394 (Lisp_Object arg)
5396 struct window *w = XWINDOW (selected_window);
5397 int lines, start;
5398 Lisp_Object window;
5399 #if 0
5400 int this_scroll_margin;
5401 #endif
5403 if (!(BUFFERP (w->buffer)
5404 && XBUFFER (w->buffer) == current_buffer))
5405 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5406 when passed below to set_marker_both. */
5407 error ("move-to-window-line called from unrelated buffer");
5409 window = selected_window;
5410 start = marker_position (w->start);
5411 if (start < BEGV || start > ZV)
5413 int height = window_internal_height (w);
5414 Fvertical_motion (make_number (- (height / 2)), window);
5415 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5416 w->start_at_line_beg = !NILP (Fbolp ());
5417 w->force_start = 1;
5419 else
5420 Fgoto_char (w->start);
5422 lines = displayed_window_lines (w);
5424 #if 0
5425 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5426 #endif
5428 if (NILP (arg))
5429 XSETFASTINT (arg, lines / 2);
5430 else
5432 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5434 if (iarg < 0)
5435 iarg = iarg + lines;
5437 #if 0 /* This code would prevent move-to-window-line from moving point
5438 to a place inside the scroll margins (which would cause the
5439 next redisplay to scroll). I wrote this code, but then concluded
5440 it is probably better not to install it. However, it is here
5441 inside #if 0 so as not to lose it. -- rms. */
5443 /* Don't let it get into the margin at either top or bottom. */
5444 iarg = max (iarg, this_scroll_margin);
5445 iarg = min (iarg, lines - this_scroll_margin - 1);
5446 #endif
5448 arg = make_number (iarg);
5451 /* Skip past a partially visible first line. */
5452 if (w->vscroll)
5453 XSETINT (arg, XINT (arg) + 1);
5455 return Fvertical_motion (arg, window);
5460 /***********************************************************************
5461 Window Configuration
5462 ***********************************************************************/
5464 struct save_window_data
5466 struct vectorlike_header header;
5467 Lisp_Object selected_frame;
5468 Lisp_Object current_window;
5469 Lisp_Object current_buffer;
5470 Lisp_Object minibuf_scroll_window;
5471 Lisp_Object minibuf_selected_window;
5472 Lisp_Object root_window;
5473 Lisp_Object focus_frame;
5474 /* A vector, each of whose elements is a struct saved_window
5475 for one window. */
5476 Lisp_Object saved_windows;
5478 /* All fields above are traced by the GC.
5479 From `fame-cols' down, the fields are ignored by the GC. */
5481 int frame_cols, frame_lines, frame_menu_bar_lines;
5482 int frame_tool_bar_lines;
5485 /* This is saved as a Lisp_Vector */
5486 struct saved_window
5488 struct vectorlike_header header;
5490 Lisp_Object window, buffer, start, pointm, mark;
5491 Lisp_Object left_col, top_line, total_cols, total_lines;
5492 Lisp_Object normal_cols, normal_lines;
5493 Lisp_Object hscroll, min_hscroll;
5494 Lisp_Object parent, prev;
5495 Lisp_Object start_at_line_beg;
5496 Lisp_Object display_table;
5497 Lisp_Object left_margin_cols, right_margin_cols;
5498 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5499 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5500 Lisp_Object combination_limit, window_parameters;
5503 #define SAVED_WINDOW_N(swv,n) \
5504 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5506 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5507 doc: /* Return t if OBJECT is a window-configuration object. */)
5508 (Lisp_Object object)
5510 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5513 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5514 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5515 (Lisp_Object config)
5517 register struct save_window_data *data;
5518 struct Lisp_Vector *saved_windows;
5520 CHECK_WINDOW_CONFIGURATION (config);
5522 data = (struct save_window_data *) XVECTOR (config);
5523 saved_windows = XVECTOR (data->saved_windows);
5524 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5527 DEFUN ("set-window-configuration", Fset_window_configuration,
5528 Sset_window_configuration, 1, 1, 0,
5529 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5530 CONFIGURATION must be a value previously returned
5531 by `current-window-configuration' (which see).
5532 If CONFIGURATION was made from a frame that is now deleted,
5533 only frame-independent values can be restored. In this case,
5534 the return value is nil. Otherwise the value is t. */)
5535 (Lisp_Object configuration)
5537 register struct save_window_data *data;
5538 struct Lisp_Vector *saved_windows;
5539 Lisp_Object new_current_buffer;
5540 Lisp_Object frame;
5541 FRAME_PTR f;
5542 ptrdiff_t old_point = -1;
5544 CHECK_WINDOW_CONFIGURATION (configuration);
5546 data = (struct save_window_data *) XVECTOR (configuration);
5547 saved_windows = XVECTOR (data->saved_windows);
5549 new_current_buffer = data->current_buffer;
5550 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5551 new_current_buffer = Qnil;
5552 else
5554 if (XBUFFER (new_current_buffer) == current_buffer)
5555 /* The code further down "preserves point" by saving here PT in
5556 old_point and then setting it later back into PT. When the
5557 current-selected-window and the final-selected-window both show
5558 the current buffer, this suffers from the problem that the
5559 current PT is the window-point of the current-selected-window,
5560 while the final PT is the point of the final-selected-window, so
5561 this copy from one PT to the other would end up moving the
5562 window-point of the final-selected-window to the window-point of
5563 the current-selected-window. So we have to be careful which
5564 point of the current-buffer we copy into old_point. */
5565 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5566 && WINDOWP (selected_window)
5567 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5568 && !EQ (selected_window, data->current_window))
5569 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5570 else
5571 old_point = PT;
5572 else
5573 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5574 point in new_current_buffer as of the last time this buffer was
5575 used. This can be non-deterministic since it can be changed by
5576 things like jit-lock by mere temporary selection of some random
5577 window that happens to show this buffer.
5578 So if possible we want this arbitrary choice of "which point" to
5579 be the one from the to-be-selected-window so as to prevent this
5580 window's cursor from being copied from another window. */
5581 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5582 /* If current_window = selected_window, its point is in BUF_PT. */
5583 && !EQ (selected_window, data->current_window))
5584 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5585 else
5586 old_point = BUF_PT (XBUFFER (new_current_buffer));
5589 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5590 f = XFRAME (frame);
5592 /* If f is a dead frame, don't bother rebuilding its window tree.
5593 However, there is other stuff we should still try to do below. */
5594 if (FRAME_LIVE_P (f))
5596 Lisp_Object window;
5597 Lisp_Object dead_windows = Qnil;
5598 register Lisp_Object tem, par, pers;
5599 register struct window *w;
5600 register struct saved_window *p;
5601 struct window *root_window;
5602 struct window **leaf_windows;
5603 int n_leaf_windows;
5604 ptrdiff_t k;
5605 int i, n;
5607 /* If the frame has been resized since this window configuration was
5608 made, we change the frame to the size specified in the
5609 configuration, restore the configuration, and then resize it
5610 back. We keep track of the prevailing height in these variables. */
5611 int previous_frame_lines = FRAME_LINES (f);
5612 int previous_frame_cols = FRAME_COLS (f);
5613 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5614 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5616 /* Don't do this within the main loop below: This may call Lisp
5617 code and is thus potentially unsafe while input is blocked. */
5618 for (k = 0; k < saved_windows->header.size; k++)
5620 p = SAVED_WINDOW_N (saved_windows, k);
5621 window = p->window;
5622 w = XWINDOW (window);
5623 if (!NILP (w->buffer)
5624 && !EQ (w->buffer, p->buffer)
5625 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5626 /* If a window we restore gets another buffer, record the
5627 window's old buffer. */
5628 call1 (Qrecord_window_buffer, window);
5631 /* The mouse highlighting code could get screwed up
5632 if it runs during this. */
5633 block_input ();
5635 if (data->frame_lines != previous_frame_lines
5636 || data->frame_cols != previous_frame_cols)
5637 change_frame_size (f, data->frame_lines,
5638 data->frame_cols, 0, 0, 0);
5639 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5640 if (data->frame_menu_bar_lines
5641 != previous_frame_menu_bar_lines)
5642 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5643 make_number (0));
5644 #ifdef HAVE_WINDOW_SYSTEM
5645 if (data->frame_tool_bar_lines
5646 != previous_frame_tool_bar_lines)
5647 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5648 make_number (0));
5649 #endif
5650 #endif
5652 /* "Swap out" point from the selected window's buffer
5653 into the window itself. (Normally the pointm of the selected
5654 window holds garbage.) We do this now, before
5655 restoring the window contents, and prevent it from
5656 being done later on when we select a new window. */
5657 if (! NILP (XWINDOW (selected_window)->buffer))
5659 w = XWINDOW (selected_window);
5660 set_marker_both (w->pointm,
5661 w->buffer,
5662 BUF_PT (XBUFFER (w->buffer)),
5663 BUF_PT_BYTE (XBUFFER (w->buffer)));
5666 windows_or_buffers_changed++;
5667 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5669 /* Problem: Freeing all matrices and later allocating them again
5670 is a serious redisplay flickering problem. What we would
5671 really like to do is to free only those matrices not reused
5672 below. */
5673 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5674 leaf_windows = alloca (count_windows (root_window)
5675 * sizeof *leaf_windows);
5676 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5678 /* Kludge Alert!
5679 Mark all windows now on frame as "deleted".
5680 Restoring the new configuration "undeletes" any that are in it.
5682 Save their current buffers in their height fields, since we may
5683 need it later, if a buffer saved in the configuration is now
5684 dead. */
5685 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5687 for (k = 0; k < saved_windows->header.size; k++)
5689 p = SAVED_WINDOW_N (saved_windows, k);
5690 window = p->window;
5691 w = XWINDOW (window);
5692 wset_next (w, Qnil);
5694 if (!NILP (p->parent))
5695 wset_parent
5696 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
5697 else
5698 wset_parent (w, Qnil);
5700 if (!NILP (p->prev))
5702 wset_prev
5703 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
5704 wset_next (XWINDOW (w->prev), p->window);
5706 else
5708 wset_prev (w, Qnil);
5709 if (!NILP (w->parent))
5711 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5713 wset_vchild (XWINDOW (w->parent), p->window);
5714 wset_hchild (XWINDOW (w->parent), Qnil);
5716 else
5718 wset_hchild (XWINDOW (w->parent), p->window);
5719 wset_vchild (XWINDOW (w->parent), Qnil);
5724 /* If we squirreled away the buffer in the window's height,
5725 restore it now. */
5726 if (BUFFERP (w->total_lines))
5727 wset_buffer (w, w->total_lines);
5728 wset_left_col (w, p->left_col);
5729 wset_top_line (w, p->top_line);
5730 wset_total_cols (w, p->total_cols);
5731 wset_total_lines (w, p->total_lines);
5732 wset_normal_cols (w, p->normal_cols);
5733 wset_normal_lines (w, p->normal_lines);
5734 w->hscroll = XFASTINT (p->hscroll);
5735 w->min_hscroll = XFASTINT (p->min_hscroll);
5736 wset_display_table (w, p->display_table);
5737 wset_left_margin_cols (w, p->left_margin_cols);
5738 wset_right_margin_cols (w, p->right_margin_cols);
5739 wset_left_fringe_width (w, p->left_fringe_width);
5740 wset_right_fringe_width (w, p->right_fringe_width);
5741 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
5742 wset_scroll_bar_width (w, p->scroll_bar_width);
5743 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
5744 wset_dedicated (w, p->dedicated);
5745 wset_combination_limit (w, p->combination_limit);
5746 /* Restore any window parameters that have been saved.
5747 Parameters that have not been saved are left alone. */
5748 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5750 pers = XCAR (tem);
5751 if (CONSP (pers))
5753 if (NILP (XCDR (pers)))
5755 par = Fassq (XCAR (pers), w->window_parameters);
5756 if (CONSP (par) && !NILP (XCDR (par)))
5757 /* Reset a parameter to nil if and only if it
5758 has a non-nil association. Don't make new
5759 associations. */
5760 Fsetcdr (par, Qnil);
5762 else
5763 /* Always restore a non-nil value. */
5764 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5768 w->last_modified = 0;
5769 w->last_overlay_modified = 0;
5771 /* Reinstall the saved buffer and pointers into it. */
5772 if (NILP (p->buffer))
5773 /* An internal window. */
5774 wset_buffer (w, p->buffer);
5775 else if (BUFFER_LIVE_P (XBUFFER (p->buffer)))
5776 /* If saved buffer is alive, install it. */
5778 wset_buffer (w, p->buffer);
5779 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5780 set_marker_restricted (w->start, p->start, w->buffer);
5781 set_marker_restricted (w->pointm, p->pointm,
5782 w->buffer);
5783 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
5784 p->mark, w->buffer);
5786 /* As documented in Fcurrent_window_configuration, don't
5787 restore the location of point in the buffer which was
5788 current when the window configuration was recorded. */
5789 if (!EQ (p->buffer, new_current_buffer)
5790 && XBUFFER (p->buffer) == current_buffer)
5791 Fgoto_char (w->pointm);
5793 else if (!NILP (w->buffer)
5794 && BUFFER_LIVE_P (XBUFFER (w->buffer)))
5795 /* Keep window's old buffer; make sure the markers are
5796 real. */
5798 /* Set window markers at start of visible range. */
5799 if (XMARKER (w->start)->buffer == 0)
5800 set_marker_restricted (w->start, make_number (0),
5801 w->buffer);
5802 if (XMARKER (w->pointm)->buffer == 0)
5803 set_marker_restricted_both
5804 (w->pointm, w->buffer,
5805 BUF_PT (XBUFFER (w->buffer)),
5806 BUF_PT_BYTE (XBUFFER (w->buffer)));
5807 w->start_at_line_beg = 1;
5809 else
5810 /* Window has no live buffer, get one. */
5812 /* Get the buffer via other_buffer_safely in order to
5813 avoid showing an unimportant buffer and, if necessary, to
5814 recreate *scratch* in the course (part of Juanma's bs-show
5815 scenario from March 2011). */
5816 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
5817 /* This will set the markers to beginning of visible
5818 range. */
5819 set_marker_restricted (w->start,
5820 make_number (0), w->buffer);
5821 set_marker_restricted (w->pointm,
5822 make_number (0), w->buffer);
5823 w->start_at_line_beg = 1;
5824 if (!NILP (w->dedicated))
5825 /* Record this window as dead. */
5826 dead_windows = Fcons (window, dead_windows);
5827 /* Make sure window is no more dedicated. */
5828 wset_dedicated (w, Qnil);
5832 fset_root_window (f, data->root_window);
5833 /* Arrange *not* to restore point in the buffer that was
5834 current when the window configuration was saved. */
5835 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5836 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5837 make_number (old_point),
5838 XWINDOW (data->current_window)->buffer);
5840 /* In the following call to `select-window', prevent "swapping out
5841 point" in the old selected window using the buffer that has
5842 been restored into it. We already swapped out that point from
5843 that window's old buffer. */
5844 select_window (data->current_window, Qnil, 1);
5845 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
5846 = selected_window;
5848 if (NILP (data->focus_frame)
5849 || (FRAMEP (data->focus_frame)
5850 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5851 Fredirect_frame_focus (frame, data->focus_frame);
5853 /* Set the screen height to the value it had before this function. */
5854 if (previous_frame_lines != FRAME_LINES (f)
5855 || previous_frame_cols != FRAME_COLS (f))
5856 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5857 0, 0, 0);
5858 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5859 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5860 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5861 make_number (0));
5862 #ifdef HAVE_WINDOW_SYSTEM
5863 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5864 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5865 make_number (0));
5866 #endif
5867 #endif
5869 /* Now, free glyph matrices in windows that were not reused. */
5870 for (i = n = 0; i < n_leaf_windows; ++i)
5872 if (NILP (leaf_windows[i]->buffer))
5874 /* Assert it's not reused as a combination. */
5875 eassert (NILP (leaf_windows[i]->hchild)
5876 && NILP (leaf_windows[i]->vchild));
5877 free_window_matrices (leaf_windows[i]);
5879 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5880 ++n;
5883 adjust_glyphs (f);
5884 unblock_input ();
5886 /* Scan dead buffer windows. */
5887 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5889 window = XCAR (dead_windows);
5890 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5891 delete_deletable_window (window);
5894 /* Fselect_window will have made f the selected frame, so we
5895 reselect the proper frame here. Fhandle_switch_frame will change the
5896 selected window too, but that doesn't make the call to
5897 Fselect_window above totally superfluous; it still sets f's
5898 selected window. */
5899 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5900 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5902 run_window_configuration_change_hook (f);
5905 if (!NILP (new_current_buffer))
5907 Fset_buffer (new_current_buffer);
5908 /* If the new current buffer doesn't appear in the selected
5909 window, go to its old point (see bug#12208). */
5910 if (!EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5911 Fgoto_char (make_number (old_point));
5914 Vminibuf_scroll_window = data->minibuf_scroll_window;
5915 minibuf_selected_window = data->minibuf_selected_window;
5917 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5921 /* Recursively delete all child windows reachable via the next, vchild,
5922 and hchild slots of WINDOW. */
5923 void
5924 delete_all_child_windows (Lisp_Object window)
5926 register struct window *w;
5928 w = XWINDOW (window);
5930 if (!NILP (w->next))
5931 /* Delete WINDOW's siblings (we traverse postorderly). */
5932 delete_all_child_windows (w->next);
5934 /* See Fset_window_configuration for excuse. */
5935 wset_total_lines (w, w->buffer);
5937 if (!NILP (w->vchild))
5939 delete_all_child_windows (w->vchild);
5940 wset_vchild (w, Qnil);
5942 else if (!NILP (w->hchild))
5944 delete_all_child_windows (w->hchild);
5945 wset_hchild (w, Qnil);
5947 else if (!NILP (w->buffer))
5949 unshow_buffer (w);
5950 unchain_marker (XMARKER (w->pointm));
5951 unchain_marker (XMARKER (w->start));
5952 wset_buffer (w, Qnil);
5955 Vwindow_list = Qnil;
5958 static int
5959 count_windows (register struct window *window)
5961 register int count = 1;
5962 if (!NILP (window->next))
5963 count += count_windows (XWINDOW (window->next));
5964 if (!NILP (window->vchild))
5965 count += count_windows (XWINDOW (window->vchild));
5966 if (!NILP (window->hchild))
5967 count += count_windows (XWINDOW (window->hchild));
5968 return count;
5972 /* Fill vector FLAT with leaf windows under W, starting at index I.
5973 Value is last index + 1. */
5974 static int
5975 get_leaf_windows (struct window *w, struct window **flat, int i)
5977 while (w)
5979 if (!NILP (w->hchild))
5980 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5981 else if (!NILP (w->vchild))
5982 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5983 else
5984 flat[i++] = w;
5986 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5989 return i;
5993 /* Return a pointer to the glyph W's physical cursor is on. Value is
5994 null if W's current matrix is invalid, so that no meaningful glyph
5995 can be returned. */
5996 struct glyph *
5997 get_phys_cursor_glyph (struct window *w)
5999 struct glyph_row *row;
6000 struct glyph *glyph;
6001 int hpos = w->phys_cursor.hpos;
6003 if (!(w->phys_cursor.vpos >= 0
6004 && w->phys_cursor.vpos < w->current_matrix->nrows))
6005 return NULL;
6007 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
6008 if (!row->enabled_p)
6009 return NULL;
6011 if (w->hscroll)
6013 /* When the window is hscrolled, cursor hpos can legitimately be
6014 out of bounds, but we draw the cursor at the corresponding
6015 window margin in that case. */
6016 if (!row->reversed_p && hpos < 0)
6017 hpos = 0;
6018 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
6019 hpos = row->used[TEXT_AREA] - 1;
6022 if (row->used[TEXT_AREA] > hpos
6023 && 0 <= hpos)
6024 glyph = row->glyphs[TEXT_AREA] + hpos;
6025 else
6026 glyph = NULL;
6028 return glyph;
6032 static int
6033 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6035 register struct saved_window *p;
6036 register struct window *w;
6037 register Lisp_Object tem, pers, par;
6039 for (;!NILP (window); window = w->next)
6041 p = SAVED_WINDOW_N (vector, i);
6042 w = XWINDOW (window);
6044 wset_temslot (w, make_number (i)); i++;
6045 p->window = window;
6046 p->buffer = w->buffer;
6047 p->left_col = w->left_col;
6048 p->top_line = w->top_line;
6049 p->total_cols = w->total_cols;
6050 p->total_lines = w->total_lines;
6051 p->normal_cols = w->normal_cols;
6052 p->normal_lines = w->normal_lines;
6053 XSETFASTINT (p->hscroll, w->hscroll);
6054 XSETFASTINT (p->min_hscroll, w->min_hscroll);
6055 p->display_table = w->display_table;
6056 p->left_margin_cols = w->left_margin_cols;
6057 p->right_margin_cols = w->right_margin_cols;
6058 p->left_fringe_width = w->left_fringe_width;
6059 p->right_fringe_width = w->right_fringe_width;
6060 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
6061 p->scroll_bar_width = w->scroll_bar_width;
6062 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6063 p->dedicated = w->dedicated;
6064 p->combination_limit = w->combination_limit;
6065 p->window_parameters = Qnil;
6067 if (!NILP (Vwindow_persistent_parameters))
6069 /* Run cycle detection on Vwindow_persistent_parameters. */
6070 Lisp_Object tortoise, hare;
6072 hare = tortoise = Vwindow_persistent_parameters;
6073 while (CONSP (hare))
6075 hare = XCDR (hare);
6076 if (!CONSP (hare))
6077 break;
6079 hare = XCDR (hare);
6080 tortoise = XCDR (tortoise);
6082 if (EQ (hare, tortoise))
6083 /* Reset Vwindow_persistent_parameters to Qnil. */
6085 Vwindow_persistent_parameters = Qnil;
6086 break;
6090 for (tem = Vwindow_persistent_parameters; CONSP (tem);
6091 tem = XCDR (tem))
6093 pers = XCAR (tem);
6094 /* Save values for persistent window parameters. */
6095 if (CONSP (pers) && !NILP (XCDR (pers)))
6097 par = Fassq (XCAR (pers), w->window_parameters);
6098 if (NILP (par))
6099 /* If the window has no value for the parameter,
6100 make one. */
6101 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
6102 p->window_parameters);
6103 else
6104 /* If the window has a value for the parameter,
6105 save it. */
6106 p->window_parameters = Fcons (Fcons (XCAR (par),
6107 XCDR (par)),
6108 p->window_parameters);
6113 if (!NILP (w->buffer))
6115 /* Save w's value of point in the window configuration. If w
6116 is the selected window, then get the value of point from
6117 the buffer; pointm is garbage in the selected window. */
6118 if (EQ (window, selected_window))
6119 p->pointm = build_marker (XBUFFER (w->buffer),
6120 BUF_PT (XBUFFER (w->buffer)),
6121 BUF_PT_BYTE (XBUFFER (w->buffer)));
6122 else
6123 p->pointm = Fcopy_marker (w->pointm, Qnil);
6124 XMARKER (p->pointm)->insertion_type
6125 = !NILP (Vwindow_point_insertion_type);
6127 p->start = Fcopy_marker (w->start, Qnil);
6128 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6130 tem = BVAR (XBUFFER (w->buffer), mark);
6131 p->mark = Fcopy_marker (tem, Qnil);
6133 else
6135 p->pointm = Qnil;
6136 p->start = Qnil;
6137 p->mark = Qnil;
6138 p->start_at_line_beg = Qnil;
6141 if (NILP (w->parent))
6142 p->parent = Qnil;
6143 else
6144 p->parent = XWINDOW (w->parent)->temslot;
6146 if (NILP (w->prev))
6147 p->prev = Qnil;
6148 else
6149 p->prev = XWINDOW (w->prev)->temslot;
6151 if (!NILP (w->vchild))
6152 i = save_window_save (w->vchild, vector, i);
6153 if (!NILP (w->hchild))
6154 i = save_window_save (w->hchild, vector, i);
6157 return i;
6160 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6161 Scurrent_window_configuration, 0, 1, 0,
6162 doc: /* Return an object representing the current window configuration of FRAME.
6163 If FRAME is nil or omitted, use the selected frame.
6164 This describes the number of windows, their sizes and current buffers,
6165 and for each displayed buffer, where display starts, and the positions of
6166 point and mark. An exception is made for point in the current buffer:
6167 its value is -not- saved.
6168 This also records the currently selected frame, and FRAME's focus
6169 redirection (see `redirect-frame-focus'). The variable
6170 `window-persistent-parameters' specifies which window parameters are
6171 saved by this function. */)
6172 (Lisp_Object frame)
6174 register Lisp_Object tem;
6175 register int n_windows;
6176 register struct save_window_data *data;
6177 register int i;
6178 struct frame *f = decode_live_frame (frame);
6180 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6181 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6182 PVEC_WINDOW_CONFIGURATION);
6184 data->frame_cols = FRAME_COLS (f);
6185 data->frame_lines = FRAME_LINES (f);
6186 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6187 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6188 data->selected_frame = selected_frame;
6189 data->current_window = FRAME_SELECTED_WINDOW (f);
6190 XSETBUFFER (data->current_buffer, current_buffer);
6191 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6192 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6193 data->root_window = FRAME_ROOT_WINDOW (f);
6194 data->focus_frame = FRAME_FOCUS_FRAME (f);
6195 tem = Fmake_vector (make_number (n_windows), Qnil);
6196 data->saved_windows = tem;
6197 for (i = 0; i < n_windows; i++)
6198 ASET (tem, i,
6199 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6200 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6201 XSETWINDOW_CONFIGURATION (tem, data);
6202 return (tem);
6205 /***********************************************************************
6206 Marginal Areas
6207 ***********************************************************************/
6209 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6210 2, 3, 0,
6211 doc: /* Set width of marginal areas of window WINDOW.
6212 WINDOW must be a live window and defaults to the selected one.
6214 Second arg LEFT-WIDTH specifies the number of character cells to
6215 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6216 does the same for the right marginal area. A nil width parameter
6217 means no margin. */)
6218 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6220 struct window *w = decode_live_window (window);
6222 /* Translate negative or zero widths to nil.
6223 Margins that are too wide have to be checked elsewhere. */
6225 if (!NILP (left_width))
6227 CHECK_NUMBER (left_width);
6228 if (XINT (left_width) <= 0)
6229 left_width = Qnil;
6232 if (!NILP (right_width))
6234 CHECK_NUMBER (right_width);
6235 if (XINT (right_width) <= 0)
6236 right_width = Qnil;
6239 if (!EQ (w->left_margin_cols, left_width)
6240 || !EQ (w->right_margin_cols, right_width))
6242 wset_left_margin_cols (w, left_width);
6243 wset_right_margin_cols (w, right_width);
6245 adjust_window_margins (w);
6247 ++windows_or_buffers_changed;
6248 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6251 return Qnil;
6255 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6256 0, 1, 0,
6257 doc: /* Get width of marginal areas of window WINDOW.
6258 WINDOW must be a live window and defaults to the selected one.
6260 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6261 If a marginal area does not exist, its width will be returned
6262 as nil. */)
6263 (Lisp_Object window)
6265 struct window *w = decode_live_window (window);
6266 return Fcons (w->left_margin_cols, w->right_margin_cols);
6271 /***********************************************************************
6272 Fringes
6273 ***********************************************************************/
6275 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6276 2, 4, 0,
6277 doc: /* Set the fringe widths of window WINDOW.
6278 WINDOW must be a live window and defaults to the selected one.
6280 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6281 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6282 fringe width. If a fringe width arg is nil, that means to use the
6283 frame's default fringe width. Default fringe widths can be set with
6284 the command `set-fringe-style'.
6285 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6286 outside of the display margins. By default, fringes are drawn between
6287 display marginal areas and the text area. */)
6288 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6290 struct window *w = decode_live_window (window);
6291 int outside = !NILP (outside_margins);
6293 if (!NILP (left_width))
6294 CHECK_NATNUM (left_width);
6295 if (!NILP (right_width))
6296 CHECK_NATNUM (right_width);
6298 /* Do nothing on a tty. */
6299 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6300 && (!EQ (w->left_fringe_width, left_width)
6301 || !EQ (w->right_fringe_width, right_width)
6302 || w->fringes_outside_margins != outside))
6304 wset_left_fringe_width (w, left_width);
6305 wset_right_fringe_width (w, right_width);
6306 w->fringes_outside_margins = outside;
6308 adjust_window_margins (w);
6310 clear_glyph_matrix (w->current_matrix);
6311 wset_window_end_valid (w, Qnil);
6313 ++windows_or_buffers_changed;
6314 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6317 return Qnil;
6321 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6322 0, 1, 0,
6323 doc: /* Get width of fringes of window WINDOW.
6324 WINDOW must be a live window and defaults to the selected one.
6326 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6327 (Lisp_Object window)
6329 struct window *w = decode_live_window (window);
6331 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6332 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6333 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6338 /***********************************************************************
6339 Scroll bars
6340 ***********************************************************************/
6342 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6343 Sset_window_scroll_bars, 2, 4, 0,
6344 doc: /* Set width and type of scroll bars of window WINDOW.
6345 WINDOW must be a live window and defaults to the selected one.
6347 Second parameter WIDTH specifies the pixel width for the scroll bar;
6348 this is automatically adjusted to a multiple of the frame column width.
6349 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6350 bar: left, right, or nil.
6351 If WIDTH is nil, use the frame's scroll-bar width.
6352 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6353 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6354 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6356 struct window *w = decode_live_window (window);
6358 if (!NILP (width))
6360 CHECK_RANGED_INTEGER (width, 0, INT_MAX);
6362 if (XINT (width) == 0)
6363 vertical_type = Qnil;
6366 if (!(NILP (vertical_type)
6367 || EQ (vertical_type, Qleft)
6368 || EQ (vertical_type, Qright)
6369 || EQ (vertical_type, Qt)))
6370 error ("Invalid type of vertical scroll bar");
6372 if (!EQ (w->scroll_bar_width, width)
6373 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6375 wset_scroll_bar_width (w, width);
6376 wset_vertical_scroll_bar_type (w, vertical_type);
6378 adjust_window_margins (w);
6380 clear_glyph_matrix (w->current_matrix);
6381 wset_window_end_valid (w, Qnil);
6383 ++windows_or_buffers_changed;
6384 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6387 return Qnil;
6391 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6392 0, 1, 0,
6393 doc: /* Get width and type of scroll bars of window WINDOW.
6394 WINDOW must be a live window and defaults to the selected one.
6396 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6397 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6398 value. */)
6399 (Lisp_Object window)
6401 struct window *w = decode_live_window (window);
6403 return list4 (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6404 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6405 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6406 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6407 w->vertical_scroll_bar_type, Qnil);
6412 /***********************************************************************
6413 Smooth scrolling
6414 ***********************************************************************/
6416 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6417 doc: /* Return the amount by which WINDOW is scrolled vertically.
6418 If WINDOW is omitted or nil, it defaults to the selected window.
6419 Normally, value is a multiple of the canonical character height of WINDOW;
6420 optional second arg PIXELS-P means value is measured in pixels. */)
6421 (Lisp_Object window, Lisp_Object pixels_p)
6423 Lisp_Object result;
6424 struct window *w = decode_live_window (window);
6425 struct frame *f = XFRAME (w->frame);
6427 if (FRAME_WINDOW_P (f))
6428 result = (NILP (pixels_p)
6429 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6430 : make_number (-w->vscroll));
6431 else
6432 result = make_number (0);
6433 return result;
6437 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6438 2, 3, 0,
6439 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6440 WINDOW nil means use the selected window. Normally, VSCROLL is a
6441 non-negative multiple of the canonical character height of WINDOW;
6442 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6443 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6444 corresponds to an integral number of pixels. The return value is the
6445 result of this rounding.
6446 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6447 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6449 struct window *w = decode_live_window (window);
6450 struct frame *f = XFRAME (w->frame);
6452 CHECK_NUMBER_OR_FLOAT (vscroll);
6454 if (FRAME_WINDOW_P (f))
6456 int old_dy = w->vscroll;
6458 w->vscroll = - (NILP (pixels_p)
6459 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6460 : XFLOATINT (vscroll));
6461 w->vscroll = min (w->vscroll, 0);
6463 if (w->vscroll != old_dy)
6465 /* Adjust glyph matrix of the frame if the virtual display
6466 area becomes larger than before. */
6467 if (w->vscroll < 0 && w->vscroll < old_dy)
6468 adjust_glyphs (f);
6470 /* Prevent redisplay shortcuts. */
6471 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6475 return Fwindow_vscroll (window, pixels_p);
6479 /* Call FN for all leaf windows on frame F. FN is called with the
6480 first argument being a pointer to the leaf window, and with
6481 additional argument USER_DATA. Stops when FN returns 0. */
6483 static void
6484 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6485 void *user_data)
6487 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6488 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6489 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6493 /* Helper function for foreach_window. Call FN for all leaf windows
6494 reachable from W. FN is called with the first argument being a
6495 pointer to the leaf window, and with additional argument USER_DATA.
6496 Stop when FN returns 0. Value is 0 if stopped by FN. */
6498 static int
6499 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6501 int cont;
6503 for (cont = 1; w && cont;)
6505 if (!NILP (w->hchild))
6506 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6507 else if (!NILP (w->vchild))
6508 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6509 else
6510 cont = fn (w, user_data);
6512 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6515 return cont;
6519 /* Freeze or unfreeze the window start of W unless it is a
6520 mini-window or the selected window. FREEZE_P non-null means freeze
6521 the window start. */
6523 static int
6524 freeze_window_start (struct window *w, void *freeze_p)
6526 if (MINI_WINDOW_P (w)
6527 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6528 && (w == XWINDOW (selected_window)
6529 || (MINI_WINDOW_P (XWINDOW (selected_window))
6530 && ! NILP (Vminibuf_scroll_window)
6531 && w == XWINDOW (Vminibuf_scroll_window)))))
6532 freeze_p = NULL;
6534 w->frozen_window_start_p = freeze_p != NULL;
6535 return 1;
6539 /* Freeze or unfreeze the window starts of all leaf windows on frame
6540 F, except the selected window and a mini-window. FREEZE_P non-zero
6541 means freeze the window start. */
6543 void
6544 freeze_window_starts (struct frame *f, int freeze_p)
6546 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6550 /***********************************************************************
6551 Initialization
6552 ***********************************************************************/
6554 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6555 describe the same state of affairs. This is used by Fequal.
6557 IGNORE_POSITIONS means ignore non-matching scroll positions
6558 and the like.
6560 This ignores a couple of things like the dedication status of
6561 window, combination_limit and the like. This might have to be
6562 fixed. */
6564 bool
6565 compare_window_configurations (Lisp_Object configuration1,
6566 Lisp_Object configuration2,
6567 bool ignore_positions)
6569 register struct save_window_data *d1, *d2;
6570 struct Lisp_Vector *sws1, *sws2;
6571 ptrdiff_t i;
6573 CHECK_WINDOW_CONFIGURATION (configuration1);
6574 CHECK_WINDOW_CONFIGURATION (configuration2);
6576 d1 = (struct save_window_data *) XVECTOR (configuration1);
6577 d2 = (struct save_window_data *) XVECTOR (configuration2);
6578 sws1 = XVECTOR (d1->saved_windows);
6579 sws2 = XVECTOR (d2->saved_windows);
6581 /* Frame settings must match. */
6582 if (d1->frame_cols != d2->frame_cols
6583 || d1->frame_lines != d2->frame_lines
6584 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6585 || !EQ (d1->selected_frame, d2->selected_frame)
6586 || !EQ (d1->current_buffer, d2->current_buffer)
6587 || (!ignore_positions
6588 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6589 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6590 || !EQ (d1->focus_frame, d2->focus_frame)
6591 /* Verify that the two configurations have the same number of windows. */
6592 || sws1->header.size != sws2->header.size)
6593 return 0;
6595 for (i = 0; i < sws1->header.size; i++)
6597 struct saved_window *sw1, *sw2;
6599 sw1 = SAVED_WINDOW_N (sws1, i);
6600 sw2 = SAVED_WINDOW_N (sws2, i);
6602 if (
6603 /* The "current" windows in the two configurations must
6604 correspond to each other. */
6605 EQ (d1->current_window, sw1->window)
6606 != EQ (d2->current_window, sw2->window)
6607 /* Windows' buffers must match. */
6608 || !EQ (sw1->buffer, sw2->buffer)
6609 || !EQ (sw1->left_col, sw2->left_col)
6610 || !EQ (sw1->top_line, sw2->top_line)
6611 || !EQ (sw1->total_cols, sw2->total_cols)
6612 || !EQ (sw1->total_lines, sw2->total_lines)
6613 || !EQ (sw1->display_table, sw2->display_table)
6614 /* The next two disjuncts check the window structure for
6615 equality. */
6616 || !EQ (sw1->parent, sw2->parent)
6617 || !EQ (sw1->prev, sw2->prev)
6618 || (!ignore_positions
6619 && (!EQ (sw1->hscroll, sw2->hscroll)
6620 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6621 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6622 || NILP (Fequal (sw1->start, sw2->start))
6623 || NILP (Fequal (sw1->pointm, sw2->pointm))
6624 || NILP (Fequal (sw1->mark, sw2->mark))))
6625 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6626 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6627 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6628 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6629 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6630 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6631 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6632 return 0;
6635 return 1;
6638 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6639 Scompare_window_configurations, 2, 2, 0,
6640 doc: /* Compare two window configurations as regards the structure of windows.
6641 This function ignores details such as the values of point and mark
6642 and scrolling positions. */)
6643 (Lisp_Object x, Lisp_Object y)
6645 if (compare_window_configurations (x, y, 1))
6646 return Qt;
6647 return Qnil;
6650 void
6651 init_window_once (void)
6653 struct frame *f = make_initial_frame ();
6654 XSETFRAME (selected_frame, f);
6655 Vterminal_frame = selected_frame;
6656 minibuf_window = f->minibuffer_window;
6657 selected_window = f->selected_window;
6658 last_nonminibuf_frame = f;
6660 window_initialized = 1;
6663 void
6664 init_window (void)
6666 Vwindow_list = Qnil;
6669 void
6670 syms_of_window (void)
6672 DEFSYM (Qscroll_up, "scroll-up");
6673 DEFSYM (Qscroll_down, "scroll-down");
6674 DEFSYM (Qscroll_command, "scroll-command");
6676 Fput (Qscroll_up, Qscroll_command, Qt);
6677 Fput (Qscroll_down, Qscroll_command, Qt);
6679 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6680 DEFSYM (Qwindowp, "windowp");
6681 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6682 DEFSYM (Qwindow_live_p, "window-live-p");
6683 DEFSYM (Qwindow_valid_p, "window-valid-p");
6684 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6685 DEFSYM (Qdelete_window, "delete-window");
6686 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6687 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6688 DEFSYM (Qsafe, "safe");
6689 DEFSYM (Qdisplay_buffer, "display-buffer");
6690 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6691 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6692 DEFSYM (Qget_mru_window, "get-mru-window");
6693 DEFSYM (Qwindow_size, "window-size");
6694 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6695 DEFSYM (Qabove, "above");
6696 DEFSYM (Qbelow, "below");
6697 DEFSYM (Qclone_of, "clone-of");
6699 staticpro (&Vwindow_list);
6701 minibuf_selected_window = Qnil;
6702 staticpro (&minibuf_selected_window);
6704 window_scroll_pixel_based_preserve_x = -1;
6705 window_scroll_pixel_based_preserve_y = -1;
6706 window_scroll_preserve_hpos = -1;
6707 window_scroll_preserve_vpos = -1;
6709 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6710 doc: /* Non-nil means call as function to display a help buffer.
6711 The function is called with one argument, the buffer to be displayed.
6712 Used by `with-output-to-temp-buffer'.
6713 If this function is used, then it must do the entire job of showing
6714 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6715 Vtemp_buffer_show_function = Qnil;
6717 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6718 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6719 Vminibuf_scroll_window = Qnil;
6721 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6722 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6723 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6724 is displayed in the `mode-line' face. */);
6725 mode_line_in_non_selected_windows = 1;
6727 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6728 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6729 Vother_window_scroll_buffer = Qnil;
6731 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6732 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6733 auto_window_vscroll_p = 1;
6735 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6736 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6737 next_screen_context_lines = 2;
6739 DEFVAR_LISP ("scroll-preserve-screen-position",
6740 Vscroll_preserve_screen_position,
6741 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6742 A value of nil means point does not keep its screen position except
6743 at the scroll margin or window boundary respectively.
6744 A value of t means point keeps its screen position if the scroll
6745 command moved it vertically out of the window, e.g. when scrolling
6746 by full screens.
6747 Any other value means point always keeps its screen position.
6748 Scroll commands should have the `scroll-command' property
6749 on their symbols to be controlled by this variable. */);
6750 Vscroll_preserve_screen_position = Qnil;
6752 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6753 doc: /* Type of marker to use for `window-point'. */);
6754 Vwindow_point_insertion_type = Qnil;
6756 DEFVAR_LISP ("window-configuration-change-hook",
6757 Vwindow_configuration_change_hook,
6758 doc: /* Functions to call when window configuration changes.
6759 The buffer-local part is run once per window, with the relevant window
6760 selected; while the global part is run only once for the modified frame,
6761 with the relevant frame selected. */);
6762 Vwindow_configuration_change_hook = Qnil;
6764 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6765 doc: /* Non-nil means `recenter' redraws entire frame.
6766 If this option is non-nil, then the `recenter' command with a nil
6767 argument will redraw the entire frame; the special value `tty' causes
6768 the frame to be redrawn only if it is a tty frame. */);
6769 Vrecenter_redisplay = Qtty;
6771 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6772 doc: /* If t, resize window combinations proportionally.
6773 If this variable is nil, splitting a window gets the entire screen space
6774 for displaying the new window from the window to split. Deleting and
6775 resizing a window preferably resizes one adjacent window only.
6777 If this variable is t, splitting a window tries to get the space
6778 proportionally from all windows in the same combination. This also
6779 allows to split a window that is otherwise too small or of fixed size.
6780 Resizing and deleting a window proportionally resize all windows in the
6781 same combination.
6783 Other values are reserved for future use.
6785 This variable takes no effect if `window-combination-limit' is non-nil. */);
6786 Vwindow_combination_resize = Qnil;
6788 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6789 doc: /* If non-nil, splitting a window makes a new parent window.
6790 The following values are recognized:
6792 nil means splitting a window will create a new parent window only if the
6793 window has no parent window or the window shall become part of a
6794 combination orthogonal to the one it is part of.
6796 `window-size' means that splitting a window for displaying a buffer
6797 makes a new parent window provided `display-buffer' is supposed to
6798 explicitly set the window's size due to the presence of a
6799 `window-height' or `window-width' entry in the alist used by
6800 `display-buffer'. Otherwise, this value is handled like nil.
6802 `temp-buffer' means that splitting a window for displaying a temporary
6803 buffer always makes a new parent window. Otherwise, this value is
6804 handled like nil.
6806 `display-buffer' means that splitting a window for displaying a buffer
6807 always makes a new parent window. Since temporary buffers are
6808 displayed by the function `display-buffer', this value is stronger
6809 than `temp-buffer'. Splitting a window for other purpose makes a
6810 new parent window only if needed.
6812 t means that splitting a window always creates a new parent window. If
6813 all splits behave this way, each frame's window tree is a binary
6814 tree and every window but the frame's root window has exactly one
6815 sibling.
6817 Other values are reserved for future use. */);
6818 Vwindow_combination_limit = Qwindow_size;
6820 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6821 doc: /* Alist of persistent window parameters.
6822 This alist specifies which window parameters shall get saved by
6823 `current-window-configuration' and `window-state-get' and subsequently
6824 restored to their previous values by `set-window-configuration' and
6825 `window-state-put'.
6827 The car of each entry of this alist is the symbol specifying the
6828 parameter. The cdr is one of the following:
6830 nil means the parameter is neither saved by `window-state-get' nor by
6831 `current-window-configuration'.
6833 t means the parameter is saved by `current-window-configuration' and,
6834 provided its WRITABLE argument is nil, by `window-state-get'.
6836 The symbol `writable' means the parameter is saved unconditionally by
6837 both `current-window-configuration' and `window-state-get'. Do not use
6838 this value for parameters without read syntax (like windows or frames).
6840 Parameters not saved by `current-window-configuration' or
6841 `window-state-get' are left alone by `set-window-configuration'
6842 respectively are not installed by `window-state-put'. */);
6843 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6845 defsubr (&Sselected_window);
6846 defsubr (&Sminibuffer_window);
6847 defsubr (&Swindow_minibuffer_p);
6848 defsubr (&Swindowp);
6849 defsubr (&Swindow_valid_p);
6850 defsubr (&Swindow_live_p);
6851 defsubr (&Swindow_frame);
6852 defsubr (&Sframe_root_window);
6853 defsubr (&Sframe_first_window);
6854 defsubr (&Sframe_selected_window);
6855 defsubr (&Sset_frame_selected_window);
6856 defsubr (&Spos_visible_in_window_p);
6857 defsubr (&Swindow_line_height);
6858 defsubr (&Swindow_buffer);
6859 defsubr (&Swindow_parent);
6860 defsubr (&Swindow_top_child);
6861 defsubr (&Swindow_left_child);
6862 defsubr (&Swindow_next_sibling);
6863 defsubr (&Swindow_prev_sibling);
6864 defsubr (&Swindow_combination_limit);
6865 defsubr (&Sset_window_combination_limit);
6866 defsubr (&Swindow_use_time);
6867 defsubr (&Swindow_top_line);
6868 defsubr (&Swindow_left_column);
6869 defsubr (&Swindow_total_height);
6870 defsubr (&Swindow_total_width);
6871 defsubr (&Swindow_normal_size);
6872 defsubr (&Swindow_new_total);
6873 defsubr (&Swindow_new_normal);
6874 defsubr (&Sset_window_new_total);
6875 defsubr (&Sset_window_new_normal);
6876 defsubr (&Swindow_resize_apply);
6877 defsubr (&Swindow_body_height);
6878 defsubr (&Swindow_body_width);
6879 defsubr (&Swindow_hscroll);
6880 defsubr (&Sset_window_hscroll);
6881 defsubr (&Swindow_redisplay_end_trigger);
6882 defsubr (&Sset_window_redisplay_end_trigger);
6883 defsubr (&Swindow_edges);
6884 defsubr (&Swindow_pixel_edges);
6885 defsubr (&Swindow_absolute_pixel_edges);
6886 defsubr (&Swindow_inside_edges);
6887 defsubr (&Swindow_inside_pixel_edges);
6888 defsubr (&Swindow_inside_absolute_pixel_edges);
6889 defsubr (&Scoordinates_in_window_p);
6890 defsubr (&Swindow_at);
6891 defsubr (&Swindow_point);
6892 defsubr (&Swindow_start);
6893 defsubr (&Swindow_end);
6894 defsubr (&Sset_window_point);
6895 defsubr (&Sset_window_start);
6896 defsubr (&Swindow_dedicated_p);
6897 defsubr (&Sset_window_dedicated_p);
6898 defsubr (&Swindow_display_table);
6899 defsubr (&Sset_window_display_table);
6900 defsubr (&Snext_window);
6901 defsubr (&Sprevious_window);
6902 defsubr (&Sget_buffer_window);
6903 defsubr (&Sdelete_other_windows_internal);
6904 defsubr (&Sdelete_window_internal);
6905 defsubr (&Sresize_mini_window_internal);
6906 defsubr (&Sset_window_buffer);
6907 defsubr (&Srun_window_configuration_change_hook);
6908 defsubr (&Sselect_window);
6909 defsubr (&Sforce_window_update);
6910 defsubr (&Ssplit_window_internal);
6911 defsubr (&Sscroll_up);
6912 defsubr (&Sscroll_down);
6913 defsubr (&Sscroll_left);
6914 defsubr (&Sscroll_right);
6915 defsubr (&Sother_window_for_scrolling);
6916 defsubr (&Sscroll_other_window);
6917 defsubr (&Sminibuffer_selected_window);
6918 defsubr (&Srecenter);
6919 defsubr (&Swindow_text_height);
6920 defsubr (&Smove_to_window_line);
6921 defsubr (&Swindow_configuration_p);
6922 defsubr (&Swindow_configuration_frame);
6923 defsubr (&Sset_window_configuration);
6924 defsubr (&Scurrent_window_configuration);
6925 defsubr (&Sset_window_margins);
6926 defsubr (&Swindow_margins);
6927 defsubr (&Sset_window_fringes);
6928 defsubr (&Swindow_fringes);
6929 defsubr (&Sset_window_scroll_bars);
6930 defsubr (&Swindow_scroll_bars);
6931 defsubr (&Swindow_vscroll);
6932 defsubr (&Sset_window_vscroll);
6933 defsubr (&Scompare_window_configurations);
6934 defsubr (&Swindow_list);
6935 defsubr (&Swindow_list_1);
6936 defsubr (&Swindow_prev_buffers);
6937 defsubr (&Sset_window_prev_buffers);
6938 defsubr (&Swindow_next_buffers);
6939 defsubr (&Sset_window_next_buffers);
6940 defsubr (&Swindow_parameters);
6941 defsubr (&Swindow_parameter);
6942 defsubr (&Sset_window_parameter);
6945 void
6946 keys_of_window (void)
6948 initial_define_key (control_x_map, '<', "scroll-left");
6949 initial_define_key (control_x_map, '>', "scroll-right");
6951 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6952 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6953 initial_define_key (meta_map, 'v', "scroll-down-command");