merge from trunk
[emacs.git] / src / window.c
blob74b763414461f439ce291501d1b2bbf296ac20bd
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2013 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #define WINDOW_INLINE EXTERN_INLINE
25 #include <stdio.h>
27 #include "lisp.h"
28 #include "character.h"
29 #include "buffer.h"
30 #include "keyboard.h"
31 #include "keymap.h"
32 #include "frame.h"
33 #include "window.h"
34 #include "commands.h"
35 #include "indent.h"
36 #include "termchar.h"
37 #include "disptab.h"
38 #include "dispextern.h"
39 #include "blockinput.h"
40 #include "intervals.h"
41 #include "termhooks.h" /* For FRAME_TERMINAL. */
42 #ifdef HAVE_WINDOW_SYSTEM
43 #include TERM_HEADER
44 #endif /* HAVE_WINDOW_SYSTEM */
45 #ifdef MSDOS
46 #include "msdos.h"
47 #endif
48 #ifdef HAVE_NS
49 #include "nsterm.h"
50 #endif
51 #ifdef HAVE_XWIDGETS
52 #include "xwidget.h"
53 #endif
55 Lisp_Object Qwindowp, Qwindow_live_p;
56 static Lisp_Object Qwindow_valid_p;
57 static Lisp_Object Qwindow_configuration_p;
58 static Lisp_Object 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, bool, int);
69 static void window_scroll_pixel_based (Lisp_Object, int, bool, int);
70 static void window_scroll_line_based (Lisp_Object, int, bool, int);
71 static Lisp_Object window_list (void);
72 static int add_window_to_list (struct window *, void *);
73 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
74 Lisp_Object, int);
75 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
76 Lisp_Object *);
77 static void foreach_window (struct frame *,
78 int (* fn) (struct window *, void *),
79 void *);
80 static int foreach_window_1 (struct window *,
81 int (* fn) (struct window *, void *),
82 void *);
83 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
84 static int window_resize_check (struct window *, bool);
85 static void window_resize_apply (struct window *, bool);
86 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
87 static void select_window_1 (Lisp_Object, bool);
89 static struct window *set_window_fringes (struct window *, Lisp_Object,
90 Lisp_Object, Lisp_Object);
91 static struct window *set_window_margins (struct window *, Lisp_Object,
92 Lisp_Object);
93 static struct window *set_window_scroll_bars (struct window *, Lisp_Object,
94 Lisp_Object, Lisp_Object);
95 static void apply_window_adjustment (struct window *);
97 /* This is the window in which the terminal's cursor should
98 be left when nothing is being done with it. This must
99 always be a leaf window, and its buffer is selected by
100 the top level editing loop at the end of each command.
102 This value is always the same as
103 FRAME_SELECTED_WINDOW (selected_frame). */
104 Lisp_Object selected_window;
106 /* A list of all windows for use by next_window and Fwindow_list.
107 Functions creating or deleting windows should invalidate this cache
108 by setting it to nil. */
109 Lisp_Object Vwindow_list;
111 /* The mini-buffer window of the selected frame.
112 Note that you cannot test for mini-bufferness of an arbitrary window
113 by comparing against this; but you can test for mini-bufferness of
114 the selected window. */
115 Lisp_Object minibuf_window;
117 /* Non-nil means it is the window whose mode line should be
118 shown as the selected window when the minibuffer is selected. */
119 Lisp_Object minibuf_selected_window;
121 /* Hook run at end of temp_output_buffer_show. */
122 static Lisp_Object Qtemp_buffer_show_hook;
124 /* Nonzero after init_window_once has finished. */
125 static int window_initialized;
127 /* Hook to run when window config changes. */
128 static Lisp_Object Qwindow_configuration_change_hook;
130 /* Used by the function window_scroll_pixel_based */
131 static int window_scroll_pixel_based_preserve_x;
132 static int window_scroll_pixel_based_preserve_y;
134 /* Same for window_scroll_line_based. */
135 static EMACS_INT window_scroll_preserve_hpos;
136 static EMACS_INT window_scroll_preserve_vpos;
138 static void
139 CHECK_WINDOW_CONFIGURATION (Lisp_Object x)
141 CHECK_TYPE (WINDOW_CONFIGURATIONP (x), Qwindow_configuration_p, x);
144 /* These setters are used only in this file, so they can be private. */
145 static void
146 wset_combination_limit (struct window *w, Lisp_Object val)
148 w->combination_limit = val;
150 static void
151 wset_dedicated (struct window *w, Lisp_Object val)
153 w->dedicated = val;
155 static void
156 wset_display_table (struct window *w, Lisp_Object val)
158 w->display_table = val;
160 static void
161 wset_new_normal (struct window *w, Lisp_Object val)
163 w->new_normal = val;
165 static void
166 wset_new_total (struct window *w, Lisp_Object val)
168 w->new_total = val;
170 static void
171 wset_normal_cols (struct window *w, Lisp_Object val)
173 w->normal_cols = val;
175 static void
176 wset_normal_lines (struct window *w, Lisp_Object val)
178 w->normal_lines = val;
180 static void
181 wset_parent (struct window *w, Lisp_Object val)
183 w->parent = val;
185 static void
186 wset_pointm (struct window *w, Lisp_Object val)
188 w->pointm = val;
190 static void
191 wset_start (struct window *w, Lisp_Object val)
193 w->start = val;
195 static void
196 wset_temslot (struct window *w, Lisp_Object val)
198 w->temslot = val;
200 static void
201 wset_vertical_scroll_bar_type (struct window *w, Lisp_Object val)
203 w->vertical_scroll_bar_type = val;
205 static void
206 wset_window_parameters (struct window *w, Lisp_Object val)
208 w->window_parameters = val;
210 static void
211 wset_combination (struct window *w, bool horflag, Lisp_Object val)
213 /* Since leaf windows never becomes non-leaf, there should
214 be no buffer and markers in start and pointm fields of W. */
215 eassert (!BUFFERP (w->contents) && NILP (w->start) && NILP (w->pointm));
216 w->contents = val;
217 /* When an internal window is deleted and VAL is nil, HORFLAG
218 is meaningless. */
219 if (!NILP (val))
220 w->horizontal = horflag;
223 /* Nonzero if leaf window W doesn't reflect the actual state
224 of displayed buffer due to its text or overlays change. */
226 bool
227 window_outdated (struct window *w)
229 struct buffer *b = XBUFFER (w->contents);
230 return (w->last_modified < BUF_MODIFF (b)
231 || w->last_overlay_modified < BUF_OVERLAY_MODIFF (b));
234 struct window *
235 decode_live_window (register Lisp_Object window)
237 if (NILP (window))
238 return XWINDOW (selected_window);
240 CHECK_LIVE_WINDOW (window);
241 return XWINDOW (window);
244 struct window *
245 decode_any_window (register Lisp_Object window)
247 struct window *w;
249 if (NILP (window))
250 return XWINDOW (selected_window);
252 CHECK_WINDOW (window);
253 w = XWINDOW (window);
254 return w;
257 static struct window *
258 decode_valid_window (register Lisp_Object window)
260 struct window *w;
262 if (NILP (window))
263 return XWINDOW (selected_window);
265 CHECK_VALID_WINDOW (window);
266 w = XWINDOW (window);
267 return w;
270 /* Called when W's buffer slot is changed. ARG -1 means that W is about to
271 cease its buffer, and 1 means that W is about to set up the new one. */
273 static void
274 adjust_window_count (struct window *w, int arg)
276 eassert (eabs (arg) == 1);
277 if (BUFFERP (w->contents))
279 struct buffer *b = XBUFFER (w->contents);
281 if (b->base_buffer)
282 b = b->base_buffer;
283 b->window_count += arg;
284 eassert (b->window_count >= 0);
285 /* These should be recalculated by redisplay code. */
286 w->window_end_valid = 0;
287 w->base_line_pos = 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 if (BUFFERP (val))
299 /* Make sure that we do not assign the buffer
300 to an internal window. */
301 eassert (MARKERP (w->start) && MARKERP (w->pointm));
302 w->contents = val;
303 adjust_window_count (w, 1);
306 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
307 doc: /* Return t if OBJECT is a window and nil otherwise. */)
308 (Lisp_Object object)
310 return WINDOWP (object) ? Qt : Qnil;
313 DEFUN ("window-valid-p", Fwindow_valid_p, Swindow_valid_p, 1, 1, 0,
314 doc: /* Return t if OBJECT is a valid window and nil otherwise.
315 A valid window is either a window that displays a buffer or an internal
316 window. Deleted windows are not live. */)
317 (Lisp_Object object)
319 return WINDOW_VALID_P (object) ? Qt : Qnil;
322 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
323 doc: /* Return t if OBJECT is a live window and nil otherwise.
324 A live window is a window that displays a buffer.
325 Internal windows and deleted windows are not live. */)
326 (Lisp_Object object)
328 return WINDOW_LIVE_P (object) ? Qt : Qnil;
331 /* Frames and windows. */
332 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 0, 1, 0,
333 doc: /* Return the frame that window WINDOW is on.
334 WINDOW must be a valid window and defaults to the selected one. */)
335 (Lisp_Object window)
337 return decode_valid_window (window)->frame;
340 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
341 doc: /* Return the root window of FRAME-OR-WINDOW.
342 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
343 With a frame argument, return that frame's root window.
344 With a window argument, return the root window of that window's frame. */)
345 (Lisp_Object frame_or_window)
347 Lisp_Object window;
349 if (NILP (frame_or_window))
350 window = SELECTED_FRAME ()->root_window;
351 else if (WINDOW_VALID_P (frame_or_window))
352 window = XFRAME (XWINDOW (frame_or_window)->frame)->root_window;
353 else
355 CHECK_LIVE_FRAME (frame_or_window);
356 window = XFRAME (frame_or_window)->root_window;
359 return window;
362 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
363 doc: /* Return the minibuffer window for frame FRAME.
364 If FRAME is omitted or nil, it defaults to the selected frame. */)
365 (Lisp_Object frame)
367 return FRAME_MINIBUF_WINDOW (decode_live_frame (frame));
370 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
371 Swindow_minibuffer_p, 0, 1, 0,
372 doc: /* Return non-nil if WINDOW is a minibuffer window.
373 WINDOW must be a valid window and defaults to the selected one. */)
374 (Lisp_Object window)
376 return MINI_WINDOW_P (decode_valid_window (window)) ? Qt : Qnil;
379 /* Don't move this to window.el - this must be a safe routine. */
380 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
381 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
382 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
383 Else if FRAME-OR-WINDOW denotes a valid window, return the first window
384 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
385 the first window of that frame. */)
386 (Lisp_Object frame_or_window)
388 Lisp_Object window;
390 if (NILP (frame_or_window))
391 window = SELECTED_FRAME ()->root_window;
392 else if (WINDOW_VALID_P (frame_or_window))
393 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
394 else
396 CHECK_LIVE_FRAME (frame_or_window);
397 window = XFRAME (frame_or_window)->root_window;
400 while (WINDOWP (XWINDOW (window)->contents))
401 window = XWINDOW (window)->contents;
403 return window;
406 DEFUN ("frame-selected-window", Fframe_selected_window,
407 Sframe_selected_window, 0, 1, 0,
408 doc: /* Return the selected window of FRAME-OR-WINDOW.
409 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
410 Else if FRAME-OR-WINDOW denotes a valid window, return the selected
411 window of that window's frame. If FRAME-OR-WINDOW denotes a live frame,
412 return the selected window of that frame. */)
413 (Lisp_Object frame_or_window)
415 Lisp_Object window;
417 if (NILP (frame_or_window))
418 window = SELECTED_FRAME ()->selected_window;
419 else if (WINDOW_VALID_P (frame_or_window))
420 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
421 else
423 CHECK_LIVE_FRAME (frame_or_window);
424 window = XFRAME (frame_or_window)->selected_window;
427 return window;
430 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
431 Sset_frame_selected_window, 2, 3, 0,
432 doc: /* Set selected window of FRAME to WINDOW.
433 FRAME must be a live frame and defaults to the selected one. If FRAME
434 is the selected frame, this makes WINDOW the selected window. Optional
435 argument NORECORD non-nil means to neither change the order of recently
436 selected windows nor the buffer list. WINDOW must denote a live window.
437 Return WINDOW. */)
438 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
440 if (NILP (frame))
441 frame = selected_frame;
443 CHECK_LIVE_FRAME (frame);
444 CHECK_LIVE_WINDOW (window);
446 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
447 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
449 if (EQ (frame, selected_frame))
450 return Fselect_window (window, norecord);
451 else
453 fset_selected_window (XFRAME (frame), window);
454 return window;
458 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
459 doc: /* Return the selected window.
460 The selected window is the window in which the standard cursor for
461 selected windows appears and to which many commands apply. */)
462 (void)
464 return selected_window;
467 int window_select_count;
469 /* If select_window is called with inhibit_point_swap non-zero it will
470 not store point of the old selected window's buffer back into that
471 window's pointm slot. This is needed by Fset_window_configuration to
472 avoid that the display routine is called with selected_window set to
473 Qnil causing a subsequent crash. */
474 static Lisp_Object
475 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
477 register struct window *w;
478 struct frame *sf;
480 CHECK_LIVE_WINDOW (window);
482 w = XWINDOW (window);
484 /* Make the selected window's buffer current. */
485 Fset_buffer (w->contents);
487 if (EQ (window, selected_window) && !inhibit_point_swap)
488 /* `switch-to-buffer' uses (select-window (selected-window)) as a "clever"
489 way to call record_buffer from Elisp, so it's important that we call
490 record_buffer before returning here. */
491 goto record_and_return;
493 sf = SELECTED_FRAME ();
494 if (XFRAME (WINDOW_FRAME (w)) != sf)
496 fset_selected_window (XFRAME (WINDOW_FRAME (w)), window);
497 /* Use this rather than Fhandle_switch_frame
498 so that FRAME_FOCUS_FRAME is moved appropriately as we
499 move around in the state where a minibuffer in a separate
500 frame is active. */
501 Fselect_frame (WINDOW_FRAME (w), norecord);
502 /* Fselect_frame called us back so we've done all the work already. */
503 eassert (EQ (window, selected_window));
504 return window;
506 else
507 fset_selected_window (sf, window);
509 select_window_1 (window, inhibit_point_swap);
510 bset_last_selected_window (XBUFFER (w->contents), window);
511 windows_or_buffers_changed++;
513 record_and_return:
514 /* record_buffer can run QUIT, so make sure it is run only after we have
515 re-established the invariant between selected_window and selected_frame,
516 otherwise the temporary broken invariant might "escape" (bug#14161). */
517 if (NILP (norecord))
519 w->use_time = ++window_select_count;
520 record_buffer (w->contents);
523 return window;
526 /* Select window with a minimum of fuss, i.e. don't record the change anywhere
527 (not even for redisplay's benefit), and assume that the window's frame is
528 already selected. */
529 static void
530 select_window_1 (Lisp_Object window, bool inhibit_point_swap)
532 /* Store the old selected window's buffer's point in pointm of the old
533 selected window. It belongs to that window, and when the window is
534 not selected, must be in the window. */
535 if (!inhibit_point_swap)
537 struct window *ow = XWINDOW (selected_window);
538 if (BUFFERP (ow->contents))
539 set_marker_both (ow->pointm, ow->contents,
540 BUF_PT (XBUFFER (ow->contents)),
541 BUF_PT_BYTE (XBUFFER (ow->contents)));
544 selected_window = window;
546 /* Go to the point recorded in the window.
547 This is important when the buffer is in more
548 than one window. It also matters when
549 redisplay_window has altered point after scrolling,
550 because it makes the change only in the window. */
551 set_point_from_marker (XWINDOW (window)->pointm);
554 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
555 doc: /* Select WINDOW which must be a live window.
556 Also make WINDOW's frame the selected frame and WINDOW that frame's
557 selected window. In addition, make WINDOW's buffer current and set that
558 buffer's value of `point' to the value of WINDOW's `window-point'.
559 Return WINDOW.
561 Optional second arg NORECORD non-nil means do not put this buffer at the
562 front of the buffer list and do not make this window the most recently
563 selected one.
565 Note that the main editor command loop sets the current buffer to the
566 buffer of the selected window before each command. */)
567 (register Lisp_Object window, Lisp_Object norecord)
569 return select_window (window, norecord, 0);
572 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
573 doc: /* Return the buffer displayed in window WINDOW.
574 If WINDOW is omitted or nil, it defaults to the selected window.
575 Return nil for an internal window or a deleted window. */)
576 (Lisp_Object window)
578 struct window *w = decode_any_window (window);
579 return WINDOW_LEAF_P (w) ? w->contents : Qnil;
582 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
583 doc: /* Return the parent window of window WINDOW.
584 WINDOW must be a valid window and defaults to the selected one.
585 Return nil for a window with no parent (e.g. a root window). */)
586 (Lisp_Object window)
588 return decode_valid_window (window)->parent;
591 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 0, 1, 0,
592 doc: /* Return the topmost child window of window WINDOW.
593 WINDOW must be a valid window and defaults to the selected one.
594 Return nil if WINDOW is a live window (live windows have no children).
595 Return nil if WINDOW is an internal window whose children form a
596 horizontal combination. */)
597 (Lisp_Object window)
599 struct window *w = decode_valid_window (window);
600 return WINDOW_VERTICAL_COMBINATION_P (w) ? w->contents : Qnil;
603 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 0, 1, 0,
604 doc: /* Return the leftmost child window of window WINDOW.
605 WINDOW must be a valid window and defaults to the selected one.
606 Return nil if WINDOW is a live window (live windows have no children).
607 Return nil if WINDOW is an internal window whose children form a
608 vertical combination. */)
609 (Lisp_Object window)
611 struct window *w = decode_valid_window (window);
612 return WINDOW_HORIZONTAL_COMBINATION_P (w) ? w->contents : Qnil;
615 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
616 doc: /* Return the next sibling window of window WINDOW.
617 WINDOW must be a valid window and defaults to the selected one.
618 Return nil if WINDOW has no next sibling. */)
619 (Lisp_Object window)
621 return decode_valid_window (window)->next;
624 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
625 doc: /* Return the previous sibling window of window WINDOW.
626 WINDOW must be a valid window and defaults to the selected one.
627 Return nil if WINDOW has no previous sibling. */)
628 (Lisp_Object window)
630 return decode_valid_window (window)->prev;
633 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
634 doc: /* Return combination limit of window WINDOW.
635 WINDOW must be a valid window used in horizontal or vertical combination.
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. */)
639 (Lisp_Object window)
641 struct window *w;
643 CHECK_VALID_WINDOW (window);
644 w = XWINDOW (window);
645 if (WINDOW_LEAF_P (w))
646 error ("Combination limit is meaningful for internal windows only");
647 return w->combination_limit;
650 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
651 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
652 WINDOW must be a valid window used in horizontal or vertical combination.
653 If LIMIT is nil, child windows of WINDOW can be recombined with WINDOW's
654 siblings. LIMIT t means that child windows of WINDOW are never
655 \(re-)combined with WINDOW's siblings. Other values are reserved for
656 future use. */)
657 (Lisp_Object window, Lisp_Object limit)
659 struct window *w;
661 CHECK_VALID_WINDOW (window);
662 w = XWINDOW (window);
663 if (WINDOW_LEAF_P (w))
664 error ("Combination limit is meaningful for internal windows only");
665 wset_combination_limit (w, limit);
666 return limit;
669 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
670 doc: /* Return the use time of window WINDOW.
671 WINDOW must be a live window and defaults to the selected one.
672 The window with the highest use time is the most recently selected
673 one. The window with the lowest use time is the least recently
674 selected one. */)
675 (Lisp_Object window)
677 return make_number (decode_live_window (window)->use_time);
680 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
681 doc: /* Return the total height, in lines, of window WINDOW.
682 WINDOW must be a valid window and defaults to the selected one.
684 The return value includes the mode line and header line, if any.
685 If WINDOW is an internal window, the total height is the height
686 of the screen areas spanned by its children.
688 On a graphical display, this total height is reported as an
689 integer multiple of the default character height. */)
690 (Lisp_Object window)
692 return make_number (decode_valid_window (window)->total_lines);
695 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
696 doc: /* Return the total width, in columns, of window WINDOW.
697 WINDOW must be a valid window and defaults to the selected one.
699 The return value includes any vertical dividers or scroll bars
700 belonging to WINDOW. If WINDOW is an internal window, the total width
701 is the width of the screen areas spanned by its children.
703 On a graphical display, this total width is reported as an
704 integer multiple of the default character width. */)
705 (Lisp_Object window)
707 return make_number (decode_valid_window (window)->total_cols);
710 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
711 doc: /* Return the new total size of window WINDOW.
712 WINDOW must be a valid window and defaults to the selected one. */)
713 (Lisp_Object window)
715 return decode_valid_window (window)->new_total;
718 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
719 doc: /* Return the normal height of window WINDOW.
720 WINDOW must be a valid window and defaults to the selected one.
721 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
722 (Lisp_Object window, Lisp_Object horizontal)
724 struct window *w = decode_valid_window (window);
726 return NILP (horizontal) ? w->normal_lines : w->normal_cols;
729 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
730 doc: /* Return new normal size of window WINDOW.
731 WINDOW must be a valid window and defaults to the selected one. */)
732 (Lisp_Object window)
734 return decode_valid_window (window)->new_normal;
737 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
738 doc: /* Return left column of window WINDOW.
739 This is the distance, in columns, between the left edge of WINDOW and
740 the left edge of the frame's window area. For instance, the return
741 value is 0 if there is no window to the left of WINDOW.
743 WINDOW must be a valid window and defaults to the selected one. */)
744 (Lisp_Object window)
746 return make_number (decode_valid_window (window)->left_col);
749 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
750 doc: /* Return top line of window WINDOW.
751 This is the distance, in lines, between the top of WINDOW and the top
752 of the frame's window area. For instance, the return value is 0 if
753 there is no window above WINDOW.
755 WINDOW must be a valid window and defaults to the selected one. */)
756 (Lisp_Object window)
758 return make_number (decode_valid_window (window)->top_line);
761 /* Return the number of lines of W's body. Don't count any mode or
762 header line of W. */
764 static int
765 window_body_lines (struct window *w)
767 int height = w->total_lines;
769 if (!MINI_WINDOW_P (w))
771 if (WINDOW_WANTS_MODELINE_P (w))
772 --height;
773 if (WINDOW_WANTS_HEADER_LINE_P (w))
774 --height;
777 return height;
780 /* Return the number of columns of W's body. Don't count columns
781 occupied by the scroll bar or the vertical bar separating W from its
782 right sibling. On window-systems don't count fringes or display
783 margins either. */
786 window_body_cols (struct window *w)
788 struct frame *f = XFRAME (WINDOW_FRAME (w));
789 int width = w->total_cols;
791 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
792 /* Scroll bars occupy a few columns. */
793 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
794 else if (!FRAME_WINDOW_P (f)
795 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
796 /* The column of `|' characters separating side-by-side windows
797 occupies one column only. */
798 width -= 1;
800 /* Display margins cannot be used for normal text. */
801 width -= WINDOW_LEFT_MARGIN_COLS (w) + WINDOW_RIGHT_MARGIN_COLS (w);
803 if (FRAME_WINDOW_P (f))
804 /* On window-systems, fringes cannot be used for normal text. */
805 width -= WINDOW_FRINGE_COLS (w);
807 return width;
810 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 1, 0,
811 doc: /* Return the height, in lines, of WINDOW's text area.
812 WINDOW must be a live window and defaults to the selected one.
814 The returned height does not include the mode line or header line.
815 On a graphical display, the height is expressed as an integer multiple
816 of the default character height. If a line at the bottom of the text
817 area is only partially visible, that counts as a whole line; to
818 exclude partially-visible lines, use `window-text-height'. */)
819 (Lisp_Object window)
821 return make_number (window_body_lines (decode_live_window (window)));
824 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 1, 0,
825 doc: /* Return the width, in columns, of WINDOW's text area.
826 WINDOW must be a live window and defaults to the selected one.
828 The return value does not include any vertical dividers, fringe or
829 marginal areas, or scroll bars. On a graphical display, the width is
830 expressed as an integer multiple of the default character width. */)
831 (Lisp_Object window)
833 return make_number (window_body_cols (decode_live_window (window)));
836 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
837 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
838 WINDOW must be a live window and defaults to the selected one. */)
839 (Lisp_Object window)
841 return make_number (decode_live_window (window)->hscroll);
844 /* Set W's horizontal scroll amount to HSCROLL clipped to a reasonable
845 range, returning the new amount as a fixnum. */
846 static Lisp_Object
847 set_window_hscroll (struct window *w, EMACS_INT hscroll)
849 /* Horizontal scrolling has problems with large scroll amounts.
850 It's too slow with long lines, and even with small lines the
851 display can be messed up. For now, though, impose only the limits
852 required by the internal representation: horizontal scrolling must
853 fit in fixnum (since it's visible to Elisp) and into ptrdiff_t
854 (since it's stored in a ptrdiff_t). */
855 ptrdiff_t hscroll_max = min (MOST_POSITIVE_FIXNUM, PTRDIFF_MAX);
856 ptrdiff_t new_hscroll = clip_to_bounds (0, hscroll, hscroll_max);
858 /* Prevent redisplay shortcuts when changing the hscroll. */
859 if (w->hscroll != new_hscroll)
860 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
862 w->hscroll = new_hscroll;
863 return make_number (new_hscroll);
866 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
867 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
868 WINDOW must be a live window and defaults to the selected one.
869 Clip the number to a reasonable value if out of range.
870 Return the new number. NCOL should be zero or positive.
872 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
873 window so that the location of point moves off-window. */)
874 (Lisp_Object window, Lisp_Object ncol)
876 CHECK_NUMBER (ncol);
877 return set_window_hscroll (decode_live_window (window), XINT (ncol));
880 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
881 Swindow_redisplay_end_trigger, 0, 1, 0,
882 doc: /* Return WINDOW's redisplay end trigger value.
883 WINDOW must be a live window and defaults to the selected one.
884 See `set-window-redisplay-end-trigger' for more information. */)
885 (Lisp_Object window)
887 return decode_live_window (window)->redisplay_end_trigger;
890 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
891 Sset_window_redisplay_end_trigger, 2, 2, 0,
892 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
893 WINDOW must be a live window and defaults to the selected one. VALUE
894 should be a buffer position (typically a marker) or nil. If it is a
895 buffer position, then if redisplay in WINDOW reaches a position beyond
896 VALUE, the functions in `redisplay-end-trigger-functions' are called
897 with two arguments: WINDOW, and the end trigger value. Afterwards the
898 end-trigger value is reset to nil. */)
899 (register Lisp_Object window, Lisp_Object value)
901 wset_redisplay_end_trigger (decode_live_window (window), value);
902 return value;
905 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
906 doc: /* Return a list of the edge coordinates of WINDOW.
907 WINDOW must be a valid window and defaults to the selected one.
909 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
910 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
911 0 at top left corner of frame.
913 RIGHT is one more than the rightmost column occupied by WINDOW. BOTTOM
914 is one more than the bottommost row occupied by WINDOW. The edges
915 include the space used by WINDOW's scroll bar, display margins, fringes,
916 header line, and/or mode line. For the edges of just the text area, use
917 `window-inside-edges'. */)
918 (Lisp_Object window)
920 register struct window *w = decode_valid_window (window);
922 return list4i (WINDOW_LEFT_EDGE_COL (w), WINDOW_TOP_EDGE_LINE (w),
923 WINDOW_RIGHT_EDGE_COL (w), WINDOW_BOTTOM_EDGE_LINE (w));
926 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
927 doc: /* Return a list of the edge pixel coordinates of WINDOW.
928 WINDOW must be a valid window and defaults to the selected one.
930 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
931 0, 0 at the top left corner of the frame.
933 RIGHT is one more than the rightmost x position occupied by WINDOW.
934 BOTTOM is one more than the bottommost y position occupied by WINDOW.
935 The pixel edges include the space used by WINDOW's scroll bar, display
936 margins, fringes, header line, and/or mode line. For the pixel edges
937 of just the text area, use `window-inside-pixel-edges'. */)
938 (Lisp_Object window)
940 register struct window *w = decode_valid_window (window);
942 return list4i (WINDOW_LEFT_EDGE_X (w), WINDOW_TOP_EDGE_Y (w),
943 WINDOW_RIGHT_EDGE_X (w), WINDOW_BOTTOM_EDGE_Y (w));
946 static void
947 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
949 struct frame *f = XFRAME (w->frame);
950 *add_y = f->top_pos;
951 #ifdef FRAME_MENUBAR_HEIGHT
952 *add_y += FRAME_MENUBAR_HEIGHT (f);
953 #endif
954 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
955 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
956 #elif FRAME_TOOLBAR_HEIGHT
957 *add_y += FRAME_TOOLBAR_HEIGHT (f);
958 #endif
959 #ifdef FRAME_NS_TITLEBAR_HEIGHT
960 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
961 #endif
962 *add_x = f->left_pos;
963 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
964 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
965 #endif
968 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
969 Swindow_absolute_pixel_edges, 0, 1, 0,
970 doc: /* Return a list of the edge pixel coordinates of WINDOW.
971 WINDOW must be a valid window and defaults to the selected one.
973 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
974 0, 0 at the top left corner of the display.
976 RIGHT is one more than the rightmost x position occupied by WINDOW.
977 BOTTOM is one more than the bottommost y position occupied by WINDOW.
978 The pixel edges include the space used by WINDOW's scroll bar, display
979 margins, fringes, header line, and/or mode line. For the pixel edges
980 of just the text area, use `window-inside-absolute-pixel-edges'. */)
981 (Lisp_Object window)
983 register struct window *w = decode_valid_window (window);
984 int add_x, add_y;
986 calc_absolute_offset (w, &add_x, &add_y);
988 return list4i (WINDOW_LEFT_EDGE_X (w) + add_x,
989 WINDOW_TOP_EDGE_Y (w) + add_y,
990 WINDOW_RIGHT_EDGE_X (w) + add_x,
991 WINDOW_BOTTOM_EDGE_Y (w) + add_y);
994 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
995 doc: /* Return a list of the edge coordinates of WINDOW.
996 WINDOW must be a live window and defaults to the selected one.
998 The returned list has the form (LEFT TOP RIGHT BOTTOM). TOP and BOTTOM
999 count by lines, and LEFT and RIGHT count by columns, all relative to 0,
1000 0 at top left corner of frame.
1002 RIGHT is one more than the rightmost column of WINDOW's text area.
1003 BOTTOM is one more than the bottommost row of WINDOW's text area. The
1004 inside edges do not include the space used by the WINDOW's scroll bar,
1005 display margins, fringes, header line, and/or mode line. */)
1006 (Lisp_Object window)
1008 register struct window *w = decode_live_window (window);
1010 return list4i ((WINDOW_BOX_LEFT_EDGE_COL (w)
1011 + WINDOW_LEFT_MARGIN_COLS (w)
1012 + WINDOW_LEFT_FRINGE_COLS (w)),
1013 (WINDOW_TOP_EDGE_LINE (w)
1014 + WINDOW_HEADER_LINE_LINES (w)),
1015 (WINDOW_BOX_RIGHT_EDGE_COL (w)
1016 - WINDOW_RIGHT_MARGIN_COLS (w)
1017 - WINDOW_RIGHT_FRINGE_COLS (w)),
1018 (WINDOW_BOTTOM_EDGE_LINE (w)
1019 - WINDOW_MODE_LINE_LINES (w)));
1022 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1023 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1024 WINDOW must be a live window and defaults to the selected one.
1026 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1027 (0,0) at the top left corner of the frame's window area.
1029 RIGHT is one more than the rightmost x position of WINDOW's text area.
1030 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1031 The inside edges do not include the space used by WINDOW's scroll bar,
1032 display margins, fringes, header line, and/or mode line. */)
1033 (Lisp_Object window)
1035 register struct window *w = decode_live_window (window);
1037 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1038 + WINDOW_LEFT_MARGIN_WIDTH (w)
1039 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1040 (WINDOW_TOP_EDGE_Y (w)
1041 + WINDOW_HEADER_LINE_HEIGHT (w)),
1042 (WINDOW_BOX_RIGHT_EDGE_X (w)
1043 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1044 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1045 (WINDOW_BOTTOM_EDGE_Y (w)
1046 - WINDOW_MODE_LINE_HEIGHT (w)));
1049 DEFUN ("window-inside-absolute-pixel-edges",
1050 Fwindow_inside_absolute_pixel_edges,
1051 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1052 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
1053 WINDOW must be a live window and defaults to the selected one.
1055 The returned list has the form (LEFT TOP RIGHT BOTTOM), all relative to
1056 (0,0) at the top left corner of the frame's window area.
1058 RIGHT is one more than the rightmost x position of WINDOW's text area.
1059 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1060 The inside edges do not include the space used by WINDOW's scroll bar,
1061 display margins, fringes, header line, and/or mode line. */)
1062 (Lisp_Object window)
1064 register struct window *w = decode_live_window (window);
1065 int add_x, add_y;
1067 calc_absolute_offset (w, &add_x, &add_y);
1069 return list4i ((WINDOW_BOX_LEFT_EDGE_X (w)
1070 + WINDOW_LEFT_MARGIN_WIDTH (w)
1071 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1072 (WINDOW_TOP_EDGE_Y (w)
1073 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1074 (WINDOW_BOX_RIGHT_EDGE_X (w)
1075 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1076 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1077 (WINDOW_BOTTOM_EDGE_Y (w)
1078 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1081 /* Test if the character at column X, row Y is within window W.
1082 If it is not, return ON_NOTHING;
1083 if it is in the window's text area, return ON_TEXT;
1084 if it is on the window's modeline, return ON_MODE_LINE;
1085 if it is on the border between the window and its right sibling,
1086 return ON_VERTICAL_BORDER.
1087 if it is on a scroll bar, return ON_SCROLL_BAR.
1088 if it is on the window's top line, return ON_HEADER_LINE;
1089 if it is in left or right fringe of the window,
1090 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1091 if it is in the marginal area to the left/right of the window,
1092 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1094 X and Y are frame relative pixel coordinates. */
1096 static enum window_part
1097 coordinates_in_window (register struct window *w, int x, int y)
1099 struct frame *f = XFRAME (WINDOW_FRAME (w));
1100 enum window_part part;
1101 int ux = FRAME_COLUMN_WIDTH (f);
1102 int left_x = WINDOW_LEFT_EDGE_X (w);
1103 int right_x = WINDOW_RIGHT_EDGE_X (w);
1104 int top_y = WINDOW_TOP_EDGE_Y (w);
1105 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1106 /* The width of the area where the vertical line can be dragged.
1107 (Between mode lines for instance. */
1108 int grabbable_width = ux;
1109 int lmargin_width, rmargin_width, text_left, text_right;
1111 /* Outside any interesting row or column? */
1112 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
1113 return ON_NOTHING;
1115 /* On the mode line or header line? */
1116 if ((WINDOW_WANTS_MODELINE_P (w)
1117 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
1118 && (part = ON_MODE_LINE))
1119 || (WINDOW_WANTS_HEADER_LINE_P (w)
1120 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
1121 && (part = ON_HEADER_LINE)))
1123 /* If it's under/over the scroll bar portion of the mode/header
1124 line, say it's on the vertical line. That's to be able to
1125 resize windows horizontally in case we're using toolkit scroll
1126 bars. Note: If scrollbars are on the left, the window that
1127 must be eventually resized is that on the left of WINDOW. */
1128 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1129 && !WINDOW_LEFTMOST_P (w)
1130 && eabs (x - left_x) < grabbable_width)
1131 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1132 && !WINDOW_RIGHTMOST_P (w)
1133 && eabs (x - right_x) < grabbable_width))
1134 return ON_VERTICAL_BORDER;
1135 else
1136 return part;
1139 /* In what's below, we subtract 1 when computing right_x because we
1140 want the rightmost pixel, which is given by left_pixel+width-1. */
1141 if (w->pseudo_window_p)
1143 left_x = 0;
1144 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
1146 else
1148 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1149 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1152 /* Outside any interesting column? */
1153 if (x < left_x || x > right_x)
1154 return ON_SCROLL_BAR;
1156 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1157 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1159 text_left = window_box_left (w, TEXT_AREA);
1160 text_right = text_left + window_box_width (w, TEXT_AREA);
1162 if (FRAME_WINDOW_P (f))
1164 if (!w->pseudo_window_p
1165 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1166 && !WINDOW_RIGHTMOST_P (w)
1167 && (eabs (x - right_x) < grabbable_width))
1168 return ON_VERTICAL_BORDER;
1170 /* Need to say "x > right_x" rather than >=, since on character
1171 terminals, the vertical line's x coordinate is right_x. */
1172 else if (!w->pseudo_window_p
1173 && !WINDOW_RIGHTMOST_P (w)
1174 && x > right_x - ux)
1175 return ON_VERTICAL_BORDER;
1177 if (x < text_left)
1179 if (lmargin_width > 0
1180 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1181 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1182 : (x < left_x + lmargin_width)))
1183 return ON_LEFT_MARGIN;
1185 return ON_LEFT_FRINGE;
1188 if (x >= text_right)
1190 if (rmargin_width > 0
1191 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1192 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1193 : (x >= right_x - rmargin_width)))
1194 return ON_RIGHT_MARGIN;
1196 return ON_RIGHT_FRINGE;
1199 /* Everything special ruled out - must be on text area */
1200 return ON_TEXT;
1203 /* Take X is the frame-relative pixel x-coordinate, and return the
1204 x-coordinate relative to part PART of window W. */
1206 window_relative_x_coord (struct window *w, enum window_part part, int x)
1208 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1210 switch (part)
1212 case ON_TEXT:
1213 return x - window_box_left (w, TEXT_AREA);
1215 case ON_LEFT_FRINGE:
1216 return x - left_x;
1218 case ON_RIGHT_FRINGE:
1219 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1221 case ON_LEFT_MARGIN:
1222 return (x - left_x
1223 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1224 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1226 case ON_RIGHT_MARGIN:
1227 return (x + 1
1228 - ((w->pseudo_window_p)
1229 ? WINDOW_TOTAL_WIDTH (w)
1230 : WINDOW_BOX_RIGHT_EDGE_X (w))
1231 + window_box_width (w, RIGHT_MARGIN_AREA)
1232 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1233 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1236 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1237 return 0;
1241 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1242 Scoordinates_in_window_p, 2, 2, 0,
1243 doc: /* Return non-nil if COORDINATES are in WINDOW.
1244 WINDOW must be a live window and defaults to the selected one.
1245 COORDINATES is a cons of the form (X . Y), X and Y being distances
1246 measured in characters from the upper-left corner of the frame.
1247 \(0 . 0) denotes the character in the upper left corner of the
1248 frame.
1249 If COORDINATES are in the text portion of WINDOW,
1250 the coordinates relative to the window are returned.
1251 If they are in the mode line of WINDOW, `mode-line' is returned.
1252 If they are in the top mode line of WINDOW, `header-line' is returned.
1253 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1254 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1255 If they are on the border between WINDOW and its right sibling,
1256 `vertical-line' is returned.
1257 If they are in the windows's left or right marginal areas, `left-margin'\n\
1258 or `right-margin' is returned. */)
1259 (register Lisp_Object coordinates, Lisp_Object window)
1261 struct window *w;
1262 struct frame *f;
1263 int x, y;
1264 Lisp_Object lx, ly;
1266 w = decode_live_window (window);
1267 f = XFRAME (w->frame);
1268 CHECK_CONS (coordinates);
1269 lx = Fcar (coordinates);
1270 ly = Fcdr (coordinates);
1271 CHECK_NUMBER_OR_FLOAT (lx);
1272 CHECK_NUMBER_OR_FLOAT (ly);
1273 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1274 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1276 switch (coordinates_in_window (w, x, y))
1278 case ON_NOTHING:
1279 return Qnil;
1281 case ON_TEXT:
1282 /* Convert X and Y to window relative pixel coordinates, and
1283 return the canonical char units. */
1284 x -= window_box_left (w, TEXT_AREA);
1285 y -= WINDOW_TOP_EDGE_Y (w);
1286 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1287 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1289 case ON_MODE_LINE:
1290 return Qmode_line;
1292 case ON_VERTICAL_BORDER:
1293 return Qvertical_line;
1295 case ON_HEADER_LINE:
1296 return Qheader_line;
1298 case ON_LEFT_FRINGE:
1299 return Qleft_fringe;
1301 case ON_RIGHT_FRINGE:
1302 return Qright_fringe;
1304 case ON_LEFT_MARGIN:
1305 return Qleft_margin;
1307 case ON_RIGHT_MARGIN:
1308 return Qright_margin;
1310 case ON_SCROLL_BAR:
1311 /* Historically we are supposed to return nil in this case. */
1312 return Qnil;
1314 default:
1315 emacs_abort ();
1320 /* Callback for foreach_window, used in window_from_coordinates.
1321 Check if window W contains coordinates specified by USER_DATA which
1322 is actually a pointer to a struct check_window_data CW.
1324 Check if window W contains coordinates *CW->x and *CW->y. If it
1325 does, return W in *CW->window, as Lisp_Object, and return in
1326 *CW->part the part of the window under coordinates *X,*Y. Return
1327 zero from this function to stop iterating over windows. */
1329 struct check_window_data
1331 Lisp_Object *window;
1332 int x, y;
1333 enum window_part *part;
1336 static int
1337 check_window_containing (struct window *w, void *user_data)
1339 struct check_window_data *cw = user_data;
1340 enum window_part found;
1341 int continue_p = 1;
1343 found = coordinates_in_window (w, cw->x, cw->y);
1344 if (found != ON_NOTHING)
1346 *cw->part = found;
1347 XSETWINDOW (*cw->window, w);
1348 continue_p = 0;
1351 return continue_p;
1355 /* Find the window containing frame-relative pixel position X/Y and
1356 return it as a Lisp_Object.
1358 If X, Y is on one of the window's special `window_part' elements,
1359 set *PART to the id of that element.
1361 If there is no window under X, Y return nil and leave *PART
1362 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1364 This function was previously implemented with a loop cycling over
1365 windows with Fnext_window, and starting with the frame's selected
1366 window. It turned out that this doesn't work with an
1367 implementation of next_window using Vwindow_list, because
1368 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1369 tree of F when this function is called asynchronously from
1370 note_mouse_highlight. The original loop didn't terminate in this
1371 case. */
1373 Lisp_Object
1374 window_from_coordinates (struct frame *f, int x, int y,
1375 enum window_part *part, bool tool_bar_p)
1377 Lisp_Object window;
1378 struct check_window_data cw;
1379 enum window_part dummy;
1381 if (part == 0)
1382 part = &dummy;
1384 window = Qnil;
1385 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1386 foreach_window (f, check_window_containing, &cw);
1388 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
1389 /* If not found above, see if it's in the tool bar window, if a tool
1390 bar exists. */
1391 if (NILP (window)
1392 && tool_bar_p
1393 && WINDOWP (f->tool_bar_window)
1394 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1395 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1396 != ON_NOTHING))
1398 *part = ON_TEXT;
1399 window = f->tool_bar_window;
1401 #endif
1403 return window;
1406 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1407 doc: /* Return window containing coordinates X and Y on FRAME.
1408 FRAME must be a live frame and defaults to the selected one.
1409 The top left corner of the frame is considered to be row 0,
1410 column 0. */)
1411 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1413 struct frame *f = decode_live_frame (frame);
1415 /* Check that arguments are integers or floats. */
1416 CHECK_NUMBER_OR_FLOAT (x);
1417 CHECK_NUMBER_OR_FLOAT (y);
1419 return window_from_coordinates (f,
1420 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1421 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1422 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1423 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1424 0, 0);
1427 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1428 doc: /* Return current value of point in WINDOW.
1429 WINDOW must be a live window and defaults to the selected one.
1431 For a nonselected window, this is the value point would have if that
1432 window were selected.
1434 Note that, when WINDOW is selected, the value returned is the same as
1435 that returned by `point' for WINDOW's buffer. It would be more strictly
1436 correct to return the top-level value of `point', outside of any
1437 `save-excursion' forms. But that is hard to define. */)
1438 (Lisp_Object window)
1440 register struct window *w = decode_live_window (window);
1442 if (w == XWINDOW (selected_window))
1443 return make_number (BUF_PT (XBUFFER (w->contents)));
1444 else
1445 return Fmarker_position (w->pointm);
1448 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1449 doc: /* Return position at which display currently starts in WINDOW.
1450 WINDOW must be a live window and defaults to the selected one.
1451 This is updated by redisplay or by calling `set-window-start'. */)
1452 (Lisp_Object window)
1454 return Fmarker_position (decode_live_window (window)->start);
1457 /* This is text temporarily removed from the doc string below.
1459 This function returns nil if the position is not currently known.
1460 That happens when redisplay is preempted and doesn't finish.
1461 If in that case you want to compute where the end of the window would
1462 have been if redisplay had finished, do this:
1463 (save-excursion
1464 (goto-char (window-start window))
1465 (vertical-motion (1- (window-height window)) window)
1466 (point))") */
1468 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1469 doc: /* Return position at which display currently ends in WINDOW.
1470 WINDOW must be a live window and defaults to the selected one.
1471 This is updated by redisplay, when it runs to completion.
1472 Simply changing the buffer text or setting `window-start'
1473 does not update this value.
1474 Return nil if there is no recorded value. (This can happen if the
1475 last redisplay of WINDOW was preempted, and did not finish.)
1476 If UPDATE is non-nil, compute the up-to-date position
1477 if it isn't already recorded. */)
1478 (Lisp_Object window, Lisp_Object update)
1480 Lisp_Object value;
1481 struct window *w = decode_live_window (window);
1482 Lisp_Object buf;
1483 struct buffer *b;
1485 buf = w->contents;
1486 CHECK_BUFFER (buf);
1487 b = XBUFFER (buf);
1489 if (! NILP (update)
1490 && (windows_or_buffers_changed
1491 || !w->window_end_valid
1492 || b->clip_changed
1493 || b->prevent_redisplay_optimizations_p
1494 || window_outdated (w))
1495 && !noninteractive)
1497 struct text_pos startp;
1498 struct it it;
1499 struct buffer *old_buffer = NULL;
1500 void *itdata = NULL;
1502 /* Cannot use Fvertical_motion because that function doesn't
1503 cope with variable-height lines. */
1504 if (b != current_buffer)
1506 old_buffer = current_buffer;
1507 set_buffer_internal (b);
1510 /* In case W->start is out of the range, use something
1511 reasonable. This situation occurred when loading a file with
1512 `-l' containing a call to `rmail' with subsequent other
1513 commands. At the end, W->start happened to be BEG, while
1514 rmail had already narrowed the buffer. */
1515 CLIP_TEXT_POS_FROM_MARKER (startp, w->start);
1517 itdata = bidi_shelve_cache ();
1518 start_display (&it, w, startp);
1519 move_it_vertically (&it, window_box_height (w));
1520 if (it.current_y < it.last_visible_y)
1521 move_it_past_eol (&it);
1522 value = make_number (IT_CHARPOS (it));
1523 bidi_unshelve_cache (itdata, 0);
1525 if (old_buffer)
1526 set_buffer_internal (old_buffer);
1528 else
1529 XSETINT (value, BUF_Z (b) - w->window_end_pos);
1531 return value;
1534 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1535 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1536 WINDOW must be a live window and defaults to the selected one.
1537 Return POS. */)
1538 (Lisp_Object window, Lisp_Object pos)
1540 register struct window *w = decode_live_window (window);
1542 /* Type of POS is checked by Fgoto_char or set_marker_restricted ... */
1544 if (w == XWINDOW (selected_window))
1546 if (XBUFFER (w->contents) == current_buffer)
1547 Fgoto_char (pos);
1548 else
1550 struct buffer *old_buffer = current_buffer;
1552 /* ... but here we want to catch type error before buffer change. */
1553 CHECK_NUMBER_COERCE_MARKER (pos);
1554 set_buffer_internal (XBUFFER (w->contents));
1555 Fgoto_char (pos);
1556 set_buffer_internal (old_buffer);
1559 else
1561 set_marker_restricted (w->pointm, pos, w->contents);
1562 /* We have to make sure that redisplay updates the window to show
1563 the new value of point. */
1564 ++windows_or_buffers_changed;
1567 return pos;
1570 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1571 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1572 WINDOW must be a live window and defaults to the selected one. Return
1573 POS. Optional third arg NOFORCE non-nil inhibits next redisplay from
1574 overriding motion of point in order to display at this exact start. */)
1575 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1577 register struct window *w = decode_live_window (window);
1579 set_marker_restricted (w->start, pos, w->contents);
1580 /* This is not right, but much easier than doing what is right. */
1581 w->start_at_line_beg = 0;
1582 if (NILP (noforce))
1583 w->force_start = 1;
1584 w->update_mode_line = 1;
1585 if (w != XWINDOW (selected_window))
1586 /* Enforce full redisplay. FIXME: make it more selective. */
1587 windows_or_buffers_changed++;
1589 return pos;
1592 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1593 Spos_visible_in_window_p, 0, 3, 0,
1594 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1595 WINDOW must be a live window and defaults to the selected one.
1597 Return nil if that position is scrolled vertically out of view. If a
1598 character is only partially visible, nil is returned, unless the
1599 optional argument PARTIALLY is non-nil. If POS is only out of view
1600 because of horizontal scrolling, return non-nil. If POS is t, it
1601 specifies the position of the last visible glyph in WINDOW. POS
1602 defaults to point in WINDOW; WINDOW defaults to the selected window.
1604 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1605 the return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1606 where X and Y are the pixel coordinates relative to the top left corner
1607 of the window. The remaining elements are omitted if the character after
1608 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1609 off-window at the top and bottom of the screen line ("row") containing
1610 POS, ROWH is the visible height of that row, and VPOS is the row number
1611 \(zero-based). */)
1612 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1614 register struct window *w;
1615 register EMACS_INT posint;
1616 register struct buffer *buf;
1617 struct text_pos top;
1618 Lisp_Object in_window = Qnil;
1619 int rtop, rbot, rowh, vpos, fully_p = 1;
1620 int x, y;
1622 w = decode_live_window (window);
1623 buf = XBUFFER (w->contents);
1624 SET_TEXT_POS_FROM_MARKER (top, w->start);
1626 if (EQ (pos, Qt))
1627 posint = -1;
1628 else if (!NILP (pos))
1630 CHECK_NUMBER_COERCE_MARKER (pos);
1631 posint = XINT (pos);
1633 else if (w == XWINDOW (selected_window))
1634 posint = PT;
1635 else
1636 posint = marker_position (w->pointm);
1638 /* If position is above window start or outside buffer boundaries,
1639 or if window start is out of range, position is not visible. */
1640 if ((EQ (pos, Qt)
1641 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1642 && CHARPOS (top) >= BUF_BEGV (buf)
1643 && CHARPOS (top) <= BUF_ZV (buf)
1644 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1645 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1646 in_window = Qt;
1648 if (!NILP (in_window) && !NILP (partially))
1650 Lisp_Object part = Qnil;
1651 if (!fully_p)
1652 part = list4i (rtop, rbot, rowh, vpos);
1653 in_window = Fcons (make_number (x),
1654 Fcons (make_number (y), part));
1657 return in_window;
1660 DEFUN ("window-line-height", Fwindow_line_height,
1661 Swindow_line_height, 0, 2, 0,
1662 doc: /* Return height in pixels of text line LINE in window WINDOW.
1663 WINDOW must be a live window and defaults to the selected one.
1665 Return height of current line if LINE is omitted or nil. Return height of
1666 header or mode line if LINE is `header-line' or `mode-line'.
1667 Otherwise, LINE is a text line number starting from 0. A negative number
1668 counts from the end of the window.
1670 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1671 in pixels of the visible part of the line, VPOS and YPOS are the
1672 vertical position in lines and pixels of the line, relative to the top
1673 of the first text line, and OFFBOT is the number of off-window pixels at
1674 the bottom of the text line. If there are off-window pixels at the top
1675 of the (first) text line, YPOS is negative.
1677 Return nil if window display is not up-to-date. In that case, use
1678 `pos-visible-in-window-p' to obtain the information. */)
1679 (Lisp_Object line, Lisp_Object window)
1681 register struct window *w;
1682 register struct buffer *b;
1683 struct glyph_row *row, *end_row;
1684 int max_y, crop, i;
1685 EMACS_INT n;
1687 w = decode_live_window (window);
1689 if (noninteractive || w->pseudo_window_p)
1690 return Qnil;
1692 CHECK_BUFFER (w->contents);
1693 b = XBUFFER (w->contents);
1695 /* Fail if current matrix is not up-to-date. */
1696 if (!w->window_end_valid
1697 || windows_or_buffers_changed
1698 || b->clip_changed
1699 || b->prevent_redisplay_optimizations_p
1700 || window_outdated (w))
1701 return Qnil;
1703 if (NILP (line))
1705 i = w->cursor.vpos;
1706 if (i < 0 || i >= w->current_matrix->nrows
1707 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1708 return Qnil;
1709 max_y = window_text_bottom_y (w);
1710 goto found_row;
1713 if (EQ (line, Qheader_line))
1715 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1716 return Qnil;
1717 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1718 return row->enabled_p ? list4i (row->height, 0, 0, 0) : Qnil;
1721 if (EQ (line, Qmode_line))
1723 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1724 return (row->enabled_p ?
1725 list4i (row->height,
1726 0, /* not accurate */
1727 (WINDOW_HEADER_LINE_HEIGHT (w)
1728 + window_text_bottom_y (w)),
1730 : Qnil);
1733 CHECK_NUMBER (line);
1734 n = XINT (line);
1736 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1737 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1738 max_y = window_text_bottom_y (w);
1739 i = 0;
1741 while ((n < 0 || i < n)
1742 && row <= end_row && row->enabled_p
1743 && row->y + row->height < max_y)
1744 row++, i++;
1746 if (row > end_row || !row->enabled_p)
1747 return Qnil;
1749 if (++n < 0)
1751 if (-n > i)
1752 return Qnil;
1753 row += n;
1754 i += n;
1757 found_row:
1758 crop = max (0, (row->y + row->height) - max_y);
1759 return list4i (row->height + min (0, row->y) - crop, i, row->y, crop);
1762 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1763 0, 1, 0,
1764 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1765 More precisely, return the value assigned by the last call of
1766 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1767 never called with WINDOW as its argument, or the value set by that
1768 function was internally reset since its last call. WINDOW must be a
1769 live window and defaults to the selected one.
1771 When a window is dedicated to its buffer, `display-buffer' will refrain
1772 from displaying another buffer in it. `get-lru-window' and
1773 `get-largest-window' treat dedicated windows specially.
1774 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1775 `kill-buffer' can delete a dedicated window and the containing frame.
1777 Functions like `set-window-buffer' may change the buffer displayed by a
1778 window, unless that window is "strongly" dedicated to its buffer, that
1779 is the value returned by `window-dedicated-p' is t. */)
1780 (Lisp_Object window)
1782 return decode_live_window (window)->dedicated;
1785 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1786 Sset_window_dedicated_p, 2, 2, 0,
1787 doc: /* Mark WINDOW as dedicated according to FLAG.
1788 WINDOW must be a live window and defaults to the selected one. FLAG
1789 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1790 mark WINDOW as non-dedicated. Return FLAG.
1792 When a window is dedicated to its buffer, `display-buffer' will refrain
1793 from displaying another buffer in it. `get-lru-window' and
1794 `get-largest-window' treat dedicated windows specially.
1795 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1796 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1797 and the containing frame.
1799 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1800 its buffer. Functions like `set-window-buffer' may change the buffer
1801 displayed by a window, unless that window is strongly dedicated to its
1802 buffer. If and when `set-window-buffer' displays another buffer in a
1803 window, it also makes sure that the window is no more dedicated. */)
1804 (Lisp_Object window, Lisp_Object flag)
1806 wset_dedicated (decode_live_window (window), flag);
1807 return flag;
1810 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1811 0, 1, 0,
1812 doc: /* Return buffers previously shown in WINDOW.
1813 WINDOW must be a live window and defaults to the selected one.
1815 The return value is a list of elements (BUFFER WINDOW-START POS),
1816 where BUFFER is a buffer, WINDOW-START is the start position of the
1817 window for that buffer, and POS is a window-specific point value. */)
1818 (Lisp_Object window)
1820 return decode_live_window (window)->prev_buffers;
1823 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1824 Sset_window_prev_buffers, 2, 2, 0,
1825 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1826 WINDOW must be a live window and defaults to the selected one.
1828 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1829 where BUFFER is a buffer, WINDOW-START is the start position of the
1830 window for that buffer, and POS is a window-specific point value. */)
1831 (Lisp_Object window, Lisp_Object prev_buffers)
1833 wset_prev_buffers (decode_live_window (window), prev_buffers);
1834 return prev_buffers;
1837 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1838 0, 1, 0,
1839 doc: /* Return list of buffers recently re-shown in WINDOW.
1840 WINDOW must be a live window and defaults to the selected one. */)
1841 (Lisp_Object window)
1843 return decode_live_window (window)->next_buffers;
1846 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1847 Sset_window_next_buffers, 2, 2, 0,
1848 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1849 WINDOW must be a live window and defaults to the selected one.
1850 NEXT-BUFFERS should be a list of buffers. */)
1851 (Lisp_Object window, Lisp_Object next_buffers)
1853 wset_next_buffers (decode_live_window (window), next_buffers);
1854 return next_buffers;
1857 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1858 0, 1, 0,
1859 doc: /* Return the parameters of WINDOW and their values.
1860 WINDOW must be a valid window and defaults to the selected one. The
1861 return value is a list of elements of the form (PARAMETER . VALUE). */)
1862 (Lisp_Object window)
1864 return Fcopy_alist (decode_valid_window (window)->window_parameters);
1867 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1868 2, 2, 0,
1869 doc: /* Return WINDOW's value for PARAMETER.
1870 WINDOW can be any window and defaults to the selected one. */)
1871 (Lisp_Object window, Lisp_Object parameter)
1873 Lisp_Object result;
1875 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1876 return CDR_SAFE (result);
1879 DEFUN ("set-window-parameter", Fset_window_parameter,
1880 Sset_window_parameter, 3, 3, 0,
1881 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1882 WINDOW can be any window and defaults to the selected one.
1883 Return VALUE. */)
1884 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1886 register struct window *w = decode_any_window (window);
1887 Lisp_Object old_alist_elt;
1889 old_alist_elt = Fassq (parameter, w->window_parameters);
1890 if (NILP (old_alist_elt))
1891 wset_window_parameters
1892 (w, Fcons (Fcons (parameter, value), w->window_parameters));
1893 else
1894 Fsetcdr (old_alist_elt, value);
1895 return value;
1898 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1899 0, 1, 0,
1900 doc: /* Return the display-table that WINDOW is using.
1901 WINDOW must be a live window and defaults to the selected one. */)
1902 (Lisp_Object window)
1904 return decode_live_window (window)->display_table;
1907 /* Get the display table for use on window W. This is either W's
1908 display table or W's buffer's display table. Ignore the specified
1909 tables if they are not valid; if no valid table is specified,
1910 return 0. */
1912 struct Lisp_Char_Table *
1913 window_display_table (struct window *w)
1915 struct Lisp_Char_Table *dp = NULL;
1917 if (DISP_TABLE_P (w->display_table))
1918 dp = XCHAR_TABLE (w->display_table);
1919 else if (BUFFERP (w->contents))
1921 struct buffer *b = XBUFFER (w->contents);
1923 if (DISP_TABLE_P (BVAR (b, display_table)))
1924 dp = XCHAR_TABLE (BVAR (b, display_table));
1925 else if (DISP_TABLE_P (Vstandard_display_table))
1926 dp = XCHAR_TABLE (Vstandard_display_table);
1929 return dp;
1932 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1933 doc: /* Set WINDOW's display-table to TABLE.
1934 WINDOW must be a live window and defaults to the selected one. */)
1935 (register Lisp_Object window, Lisp_Object table)
1937 wset_display_table (decode_live_window (window), table);
1938 return table;
1941 /* Record info on buffer window W is displaying
1942 when it is about to cease to display that buffer. */
1943 static void
1944 unshow_buffer (register struct window *w)
1946 Lisp_Object buf = w->contents;
1947 struct buffer *b = XBUFFER (buf);
1949 eassert (b == XMARKER (w->pointm)->buffer);
1951 #if 0
1952 if (w == XWINDOW (selected_window)
1953 || ! EQ (buf, XWINDOW (selected_window)->contents))
1954 /* Do this except when the selected window's buffer
1955 is being removed from some other window. */
1956 #endif
1957 /* last_window_start records the start position that this buffer
1958 had in the last window to be disconnected from it.
1959 Now that this statement is unconditional,
1960 it is possible for the buffer to be displayed in the
1961 selected window, while last_window_start reflects another
1962 window which was recently showing the same buffer.
1963 Some people might say that might be a good thing. Let's see. */
1964 b->last_window_start = marker_position (w->start);
1966 /* Point in the selected window's buffer
1967 is actually stored in that buffer, and the window's pointm isn't used.
1968 So don't clobber point in that buffer. */
1969 if (! EQ (buf, XWINDOW (selected_window)->contents)
1970 /* Don't clobber point in current buffer either (this could be
1971 useful in connection with bug#12208).
1972 && XBUFFER (buf) != current_buffer */
1973 /* This line helps to fix Horsley's testbug.el bug. */
1974 && !(WINDOWP (BVAR (b, last_selected_window))
1975 && w != XWINDOW (BVAR (b, last_selected_window))
1976 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->contents)))
1977 temp_set_point_both (b,
1978 clip_to_bounds (BUF_BEGV (b),
1979 marker_position (w->pointm),
1980 BUF_ZV (b)),
1981 clip_to_bounds (BUF_BEGV_BYTE (b),
1982 marker_byte_position (w->pointm),
1983 BUF_ZV_BYTE (b)));
1985 if (WINDOWP (BVAR (b, last_selected_window))
1986 && w == XWINDOW (BVAR (b, last_selected_window)))
1987 bset_last_selected_window (b, Qnil);
1990 /* Put NEW into the window structure in place of OLD. SETFLAG zero
1991 means change window structure only. Otherwise store geometry and
1992 other settings as well. */
1993 static void
1994 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
1996 register Lisp_Object tem;
1997 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
1999 /* If OLD is its frame's root window, then NEW is the new
2000 root window for that frame. */
2001 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
2002 fset_root_window (XFRAME (o->frame), new);
2004 if (setflag)
2006 n->left_col = o->left_col;
2007 n->top_line = o->top_line;
2008 n->total_cols = o->total_cols;
2009 n->total_lines = o->total_lines;
2010 wset_normal_cols (n, o->normal_cols);
2011 wset_normal_cols (o, make_float (1.0));
2012 wset_normal_lines (n, o->normal_lines);
2013 wset_normal_lines (o, make_float (1.0));
2014 n->desired_matrix = n->current_matrix = 0;
2015 n->vscroll = 0;
2016 memset (&n->cursor, 0, sizeof (n->cursor));
2017 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
2018 n->last_cursor_vpos = 0;
2019 n->phys_cursor_type = -1;
2020 n->phys_cursor_width = -1;
2021 n->must_be_updated_p = 0;
2022 n->pseudo_window_p = 0;
2023 n->window_end_vpos = 0;
2024 n->window_end_pos = 0;
2025 n->window_end_valid = 0;
2028 tem = o->next;
2029 wset_next (n, tem);
2030 if (!NILP (tem))
2031 wset_prev (XWINDOW (tem), new);
2033 tem = o->prev;
2034 wset_prev (n, tem);
2035 if (!NILP (tem))
2036 wset_next (XWINDOW (tem), new);
2038 tem = o->parent;
2039 wset_parent (n, tem);
2040 if (!NILP (tem) && EQ (XWINDOW (tem)->contents, old))
2041 wset_combination (XWINDOW (tem), XWINDOW (tem)->horizontal, new);
2044 /* If window WINDOW and its parent window are iso-combined, merge
2045 WINDOW's children into those of its parent window and mark WINDOW as
2046 deleted. */
2048 static void
2049 recombine_windows (Lisp_Object window)
2051 struct window *w, *p, *c;
2052 Lisp_Object parent, child;
2053 bool horflag;
2055 w = XWINDOW (window);
2056 parent = w->parent;
2057 if (!NILP (parent) && NILP (w->combination_limit))
2059 p = XWINDOW (parent);
2060 if (WINDOWP (p->contents) && WINDOWP (w->contents)
2061 && p->horizontal == w->horizontal)
2062 /* WINDOW and PARENT are both either a vertical or a horizontal
2063 combination. */
2065 horflag = WINDOW_HORIZONTAL_COMBINATION_P (w);
2066 child = w->contents;
2067 c = XWINDOW (child);
2069 /* Splice WINDOW's children into its parent's children and
2070 assign new normal sizes. */
2071 if (NILP (w->prev))
2072 wset_combination (p, horflag, child);
2073 else
2075 wset_prev (c, w->prev);
2076 wset_next (XWINDOW (w->prev), child);
2079 while (c)
2081 wset_parent (c, parent);
2083 if (horflag)
2084 wset_normal_cols (c,
2085 make_float ((double) c->total_cols
2086 / (double) p->total_cols));
2087 else
2088 wset_normal_lines (c,
2089 make_float ((double) c->total_lines
2090 / (double) p->total_lines));
2092 if (NILP (c->next))
2094 if (!NILP (w->next))
2096 wset_next (c, w->next);
2097 wset_prev (XWINDOW (c->next), child);
2100 c = 0;
2102 else
2104 child = c->next;
2105 c = XWINDOW (child);
2109 /* WINDOW can be deleted now. */
2110 wset_combination (w, 0, Qnil);
2115 /* If WINDOW can be deleted, delete it. */
2116 static void
2117 delete_deletable_window (Lisp_Object window)
2119 if (!NILP (call1 (Qwindow_deletable_p, window)))
2120 call1 (Qdelete_window, window);
2123 /***********************************************************************
2124 Window List
2125 ***********************************************************************/
2127 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2128 pointer. This is a callback function for foreach_window, used in
2129 the window_list function. */
2131 static int
2132 add_window_to_list (struct window *w, void *user_data)
2134 Lisp_Object *list = user_data;
2135 Lisp_Object window;
2136 XSETWINDOW (window, w);
2137 *list = Fcons (window, *list);
2138 return 1;
2142 /* Return a list of all windows, for use by next_window. If
2143 Vwindow_list is a list, return that list. Otherwise, build a new
2144 list, cache it in Vwindow_list, and return that. */
2146 static Lisp_Object
2147 window_list (void)
2149 if (!CONSP (Vwindow_list))
2151 Lisp_Object tail, frame;
2153 Vwindow_list = Qnil;
2154 FOR_EACH_FRAME (tail, frame)
2156 Lisp_Object args[2];
2158 /* We are visiting windows in canonical order, and add
2159 new windows at the front of args[1], which means we
2160 have to reverse this list at the end. */
2161 args[1] = Qnil;
2162 foreach_window (XFRAME (frame), add_window_to_list, &args[1]);
2163 args[0] = Vwindow_list;
2164 args[1] = Fnreverse (args[1]);
2165 Vwindow_list = Fnconc (2, args);
2169 return Vwindow_list;
2173 /* Value is non-zero if WINDOW satisfies the constraints given by
2174 OWINDOW, MINIBUF and ALL_FRAMES.
2176 MINIBUF t means WINDOW may be minibuffer windows.
2177 `lambda' means WINDOW may not be a minibuffer window.
2178 a window means a specific minibuffer window
2180 ALL_FRAMES t means search all frames,
2181 nil means search just current frame,
2182 `visible' means search just visible frames on the
2183 current terminal,
2184 0 means search visible and iconified frames on the
2185 current terminal,
2186 a window means search the frame that window belongs to,
2187 a frame means consider windows on that frame, only. */
2189 static bool
2190 candidate_window_p (Lisp_Object window, Lisp_Object owindow,
2191 Lisp_Object minibuf, Lisp_Object all_frames)
2193 struct window *w = XWINDOW (window);
2194 struct frame *f = XFRAME (w->frame);
2195 bool candidate_p = 1;
2197 if (!BUFFERP (w->contents))
2198 candidate_p = 0;
2199 else if (MINI_WINDOW_P (w)
2200 && (EQ (minibuf, Qlambda)
2201 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2203 /* If MINIBUF is `lambda' don't consider any mini-windows.
2204 If it is a window, consider only that one. */
2205 candidate_p = 0;
2207 else if (EQ (all_frames, Qt))
2208 candidate_p = 1;
2209 else if (NILP (all_frames))
2211 eassert (WINDOWP (owindow));
2212 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2214 else if (EQ (all_frames, Qvisible))
2216 candidate_p = FRAME_VISIBLE_P (f)
2217 && (FRAME_TERMINAL (XFRAME (w->frame))
2218 == FRAME_TERMINAL (XFRAME (selected_frame)));
2221 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2223 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2224 #ifdef HAVE_X_WINDOWS
2225 /* Yuck!! If we've just created the frame and the
2226 window-manager requested the user to place it
2227 manually, the window may still not be considered
2228 `visible'. I'd argue it should be at least
2229 something like `iconified', but don't know how to do
2230 that yet. --Stef */
2231 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2232 && !f->output_data.x->has_been_visible)
2233 #endif
2235 && (FRAME_TERMINAL (XFRAME (w->frame))
2236 == FRAME_TERMINAL (XFRAME (selected_frame)));
2238 else if (WINDOWP (all_frames))
2239 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2240 || EQ (XWINDOW (all_frames)->frame, w->frame)
2241 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2242 else if (FRAMEP (all_frames))
2243 candidate_p = EQ (all_frames, w->frame);
2245 return candidate_p;
2249 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2250 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2251 MINIBUF, and ALL_FRAMES. */
2253 static void
2254 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2256 struct window *w = decode_live_window (*window);
2258 XSETWINDOW (*window, w);
2259 /* MINIBUF nil may or may not include minibuffers. Decide if it
2260 does. */
2261 if (NILP (*minibuf))
2262 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2263 else if (!EQ (*minibuf, Qt))
2264 *minibuf = Qlambda;
2266 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2267 => count none of them, or a specific minibuffer window (the
2268 active one) to count. */
2270 /* ALL_FRAMES nil doesn't specify which frames to include. */
2271 if (NILP (*all_frames))
2272 *all_frames
2273 = (!EQ (*minibuf, Qlambda)
2274 ? FRAME_MINIBUF_WINDOW (XFRAME (w->frame))
2275 : Qnil);
2276 else if (EQ (*all_frames, Qvisible))
2278 else if (EQ (*all_frames, make_number (0)))
2280 else if (FRAMEP (*all_frames))
2282 else if (!EQ (*all_frames, Qt))
2283 *all_frames = Qnil;
2287 /* Return the next or previous window of WINDOW in cyclic ordering
2288 of windows. NEXT_P non-zero means return the next window. See the
2289 documentation string of next-window for the meaning of MINIBUF and
2290 ALL_FRAMES. */
2292 static Lisp_Object
2293 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2295 decode_next_window_args (&window, &minibuf, &all_frames);
2297 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2298 return the first window on the frame. */
2299 if (FRAMEP (all_frames)
2300 && !EQ (all_frames, XWINDOW (window)->frame))
2301 return Fframe_first_window (all_frames);
2303 if (next_p)
2305 Lisp_Object list;
2307 /* Find WINDOW in the list of all windows. */
2308 list = Fmemq (window, window_list ());
2310 /* Scan forward from WINDOW to the end of the window list. */
2311 if (CONSP (list))
2312 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2313 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2314 break;
2316 /* Scan from the start of the window list up to WINDOW. */
2317 if (!CONSP (list))
2318 for (list = Vwindow_list;
2319 CONSP (list) && !EQ (XCAR (list), window);
2320 list = XCDR (list))
2321 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2322 break;
2324 if (CONSP (list))
2325 window = XCAR (list);
2327 else
2329 Lisp_Object candidate, list;
2331 /* Scan through the list of windows for candidates. If there are
2332 candidate windows in front of WINDOW, the last one of these
2333 is the one we want. If there are candidates following WINDOW
2334 in the list, again the last one of these is the one we want. */
2335 candidate = Qnil;
2336 for (list = window_list (); CONSP (list); list = XCDR (list))
2338 if (EQ (XCAR (list), window))
2340 if (WINDOWP (candidate))
2341 break;
2343 else if (candidate_window_p (XCAR (list), window, minibuf,
2344 all_frames))
2345 candidate = XCAR (list);
2348 if (WINDOWP (candidate))
2349 window = candidate;
2352 return window;
2356 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2357 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2358 WINDOW must be a live window and defaults to the selected one. The
2359 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2360 consider.
2362 MINIBUF nil or omitted means consider the minibuffer window only if the
2363 minibuffer is active. MINIBUF t means consider the minibuffer window
2364 even if the minibuffer is not active. Any other value means do not
2365 consider the minibuffer window even if the minibuffer is active.
2367 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2368 plus the minibuffer window if specified by the MINIBUF argument. If the
2369 minibuffer counts, consider all windows on all frames that share that
2370 minibuffer too. The following non-nil values of ALL-FRAMES have special
2371 meanings:
2373 - t means consider all windows on all existing frames.
2375 - `visible' means consider all windows on all visible frames.
2377 - 0 (the number zero) means consider all windows on all visible and
2378 iconified frames.
2380 - A frame means consider all windows on that frame only.
2382 Anything else means consider all windows on WINDOW's frame and no
2383 others.
2385 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2386 `next-window' to iterate through the entire cycle of acceptable
2387 windows, eventually ending up back at the window you started with.
2388 `previous-window' traverses the same cycle, in the reverse order. */)
2389 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2391 return next_window (window, minibuf, all_frames, 1);
2395 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2396 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2397 WINDOW must be a live window and defaults to the selected one. The
2398 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2399 consider.
2401 MINIBUF nil or omitted means consider the minibuffer window only if the
2402 minibuffer is active. MINIBUF t means consider the minibuffer window
2403 even if the minibuffer is not active. Any other value means do not
2404 consider the minibuffer window even if the minibuffer is active.
2406 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2407 plus the minibuffer window if specified by the MINIBUF argument. If the
2408 minibuffer counts, consider all windows on all frames that share that
2409 minibuffer too. The following non-nil values of ALL-FRAMES have special
2410 meanings:
2412 - t means consider all windows on all existing frames.
2414 - `visible' means consider all windows on all visible frames.
2416 - 0 (the number zero) means consider all windows on all visible and
2417 iconified frames.
2419 - A frame means consider all windows on that frame only.
2421 Anything else means consider all windows on WINDOW's frame and no
2422 others.
2424 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2425 use `previous-window' to iterate through the entire cycle of
2426 acceptable windows, eventually ending up back at the window you
2427 started with. `next-window' traverses the same cycle, in the
2428 reverse order. */)
2429 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2431 return next_window (window, minibuf, all_frames, 0);
2435 /* Return a list of windows in cyclic ordering. Arguments are like
2436 for `next-window'. */
2438 static Lisp_Object
2439 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2441 Lisp_Object tail, list, rest;
2443 decode_next_window_args (&window, &minibuf, &all_frames);
2444 list = Qnil;
2446 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2447 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2448 list = Fcons (XCAR (tail), list);
2450 /* Rotate the list to start with WINDOW. */
2451 list = Fnreverse (list);
2452 rest = Fmemq (window, list);
2453 if (!NILP (rest) && !EQ (rest, list))
2455 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2457 XSETCDR (tail, Qnil);
2458 list = nconc2 (rest, list);
2460 return list;
2464 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2465 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2466 FRAME nil or omitted means use the selected frame.
2467 WINDOW nil or omitted means use the window selected within FRAME.
2468 MINIBUF t means include the minibuffer window, even if it isn't active.
2469 MINIBUF nil or omitted means include the minibuffer window only
2470 if it's active.
2471 MINIBUF neither nil nor t means never include the minibuffer window. */)
2472 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2474 if (NILP (window))
2475 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2476 CHECK_WINDOW (window);
2477 if (NILP (frame))
2478 frame = selected_frame;
2480 if (!EQ (frame, XWINDOW (window)->frame))
2481 error ("Window is on a different frame");
2483 return window_list_1 (window, minibuf, frame);
2487 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2488 doc: /* Return a list of all live windows.
2489 WINDOW specifies the first window to list and defaults to the selected
2490 window.
2492 Optional argument MINIBUF nil or omitted means consider the minibuffer
2493 window only if the minibuffer is active. MINIBUF t means consider the
2494 minibuffer window even if the minibuffer is not active. Any other value
2495 means do not consider the minibuffer window even if the minibuffer is
2496 active.
2498 Optional argument ALL-FRAMES nil or omitted means consider all windows
2499 on WINDOW's frame, plus the minibuffer window if specified by the
2500 MINIBUF argument. If the minibuffer counts, consider all windows on all
2501 frames that share that minibuffer too. The following non-nil values of
2502 ALL-FRAMES have special meanings:
2504 - t means consider all windows on all existing frames.
2506 - `visible' means consider all windows on all visible frames.
2508 - 0 (the number zero) means consider all windows on all visible and
2509 iconified frames.
2511 - A frame means consider all windows on that frame only.
2513 Anything else means consider all windows on WINDOW's frame and no
2514 others.
2516 If WINDOW is not on the list of windows returned, some other window will
2517 be listed first but no error is signaled. */)
2518 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2520 return window_list_1 (window, minibuf, all_frames);
2523 /* Look at all windows, performing an operation specified by TYPE
2524 with argument OBJ.
2525 If FRAMES is Qt, look at all frames;
2526 Qnil, look at just the selected frame;
2527 Qvisible, look at visible frames;
2528 a frame, just look at windows on that frame.
2529 If MINI is non-zero, perform the operation on minibuffer windows too. */
2531 enum window_loop
2533 WINDOW_LOOP_UNUSED,
2534 GET_BUFFER_WINDOW, /* Arg is buffer */
2535 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2536 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2537 CHECK_ALL_WINDOWS /* Arg is ignored */
2540 static Lisp_Object
2541 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2543 Lisp_Object window, windows, best_window, frame_arg;
2544 int frame_best_window_flag = 0;
2545 struct frame *f;
2546 struct gcpro gcpro1;
2548 /* If we're only looping through windows on a particular frame,
2549 frame points to that frame. If we're looping through windows
2550 on all frames, frame is 0. */
2551 if (FRAMEP (frames))
2552 f = XFRAME (frames);
2553 else if (NILP (frames))
2554 f = SELECTED_FRAME ();
2555 else
2556 f = NULL;
2558 if (f)
2559 frame_arg = Qlambda;
2560 else if (EQ (frames, make_number (0)))
2561 frame_arg = frames;
2562 else if (EQ (frames, Qvisible))
2563 frame_arg = frames;
2564 else
2565 frame_arg = Qt;
2567 /* frame_arg is Qlambda to stick to one frame,
2568 Qvisible to consider all visible frames,
2569 or Qt otherwise. */
2571 /* Pick a window to start with. */
2572 if (WINDOWP (obj))
2573 window = obj;
2574 else if (f)
2575 window = FRAME_SELECTED_WINDOW (f);
2576 else
2577 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2579 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2580 GCPRO1 (windows);
2581 best_window = Qnil;
2583 for (; CONSP (windows); windows = XCDR (windows))
2585 struct window *w;
2587 window = XCAR (windows);
2588 w = XWINDOW (window);
2590 /* Note that we do not pay attention here to whether the frame
2591 is visible, since Fwindow_list skips non-visible frames if
2592 that is desired, under the control of frame_arg. */
2593 if (!MINI_WINDOW_P (w)
2594 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2595 consider all windows. */
2596 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2597 || (mini && minibuf_level > 0))
2598 switch (type)
2600 case GET_BUFFER_WINDOW:
2601 if (EQ (w->contents, obj)
2602 /* Don't find any minibuffer window except the one that
2603 is currently in use. */
2604 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2606 if (EQ (window, selected_window))
2607 /* Preferably return the selected window. */
2608 RETURN_UNGCPRO (window);
2609 else if (EQ (XWINDOW (window)->frame, selected_frame)
2610 && !frame_best_window_flag)
2611 /* Prefer windows on the current frame (but don't
2612 choose another one if we have one already). */
2614 best_window = window;
2615 frame_best_window_flag = 1;
2617 else if (NILP (best_window))
2618 best_window = window;
2620 break;
2622 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2623 /* We could simply check whether the buffer shown by window
2624 is live, and show another buffer in case it isn't. */
2625 if (EQ (w->contents, obj))
2627 /* Undedicate WINDOW. */
2628 wset_dedicated (w, Qnil);
2629 /* Make WINDOW show the buffer returned by
2630 other_buffer_safely, don't run any hooks. */
2631 set_window_buffer
2632 (window, other_buffer_safely (w->contents), 0, 0);
2633 /* If WINDOW is the selected window, make its buffer
2634 current. But do so only if the window shows the
2635 current buffer (Bug#6454). */
2636 if (EQ (window, selected_window)
2637 && XBUFFER (w->contents) == current_buffer)
2638 Fset_buffer (w->contents);
2640 break;
2642 case REDISPLAY_BUFFER_WINDOWS:
2643 if (EQ (w->contents, obj))
2645 mark_window_display_accurate (window, 0);
2646 w->update_mode_line = 1;
2647 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2648 ++update_mode_lines;
2649 best_window = window;
2651 break;
2653 /* Check for a leaf window that has a killed buffer
2654 or broken markers. */
2655 case CHECK_ALL_WINDOWS:
2656 if (BUFFERP (w->contents))
2658 struct buffer *b = XBUFFER (w->contents);
2660 if (!BUFFER_LIVE_P (b))
2661 emacs_abort ();
2662 if (!MARKERP (w->start) || XMARKER (w->start)->buffer != b)
2663 emacs_abort ();
2664 if (!MARKERP (w->pointm) || XMARKER (w->pointm)->buffer != b)
2665 emacs_abort ();
2667 break;
2669 case WINDOW_LOOP_UNUSED:
2670 break;
2674 UNGCPRO;
2675 return best_window;
2678 /* Used for debugging. Abort if any window has a dead buffer. */
2680 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2681 void
2682 check_all_windows (void)
2684 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2687 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2688 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2689 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2690 the current buffer.
2692 The optional argument ALL-FRAMES specifies the frames to consider:
2694 - t means consider all windows on all existing frames.
2696 - `visible' means consider all windows on all visible frames.
2698 - 0 (the number zero) means consider all windows on all visible
2699 and iconified frames.
2701 - A frame means consider all windows on that frame only.
2703 Any other value of ALL-FRAMES means consider all windows on the
2704 selected frame and no others. */)
2705 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2707 Lisp_Object buffer;
2709 if (NILP (buffer_or_name))
2710 buffer = Fcurrent_buffer ();
2711 else
2712 buffer = Fget_buffer (buffer_or_name);
2714 if (BUFFERP (buffer))
2715 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2716 else
2717 return Qnil;
2720 static Lisp_Object
2721 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2723 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2727 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2728 Sdelete_other_windows_internal, 0, 2, "",
2729 doc: /* Make WINDOW fill its frame.
2730 Only the frame WINDOW is on is affected. WINDOW must be a valid window
2731 and defaults to the selected one.
2733 Optional argument ROOT, if non-nil, must specify an internal window such
2734 that WINDOW is in its window subtree. If this is the case, replace ROOT
2735 by WINDOW and leave alone any windows not part of ROOT's subtree.
2737 When WINDOW is live try to reduce display jumps by keeping the text
2738 previously visible in WINDOW in the same place on the frame. Doing this
2739 depends on the value of (window-start WINDOW), so if calling this
2740 function in a program gives strange scrolling, make sure the
2741 window-start value is reasonable when this function is called. */)
2742 (Lisp_Object window, Lisp_Object root)
2744 struct window *w, *r, *s;
2745 struct frame *f;
2746 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2747 ptrdiff_t startpos IF_LINT (= 0), startbyte IF_LINT (= 0);
2748 int top IF_LINT (= 0), new_top, resize_failed;
2750 w = decode_valid_window (window);
2751 XSETWINDOW (window, w);
2752 f = XFRAME (w->frame);
2754 if (NILP (root))
2755 /* ROOT is the frame's root window. */
2757 root = FRAME_ROOT_WINDOW (f);
2758 r = XWINDOW (root);
2760 else
2761 /* ROOT must be an ancestor of WINDOW. */
2763 r = decode_valid_window (root);
2764 pwindow = XWINDOW (window)->parent;
2765 while (!NILP (pwindow))
2766 if (EQ (pwindow, root))
2767 break;
2768 else
2769 pwindow = XWINDOW (pwindow)->parent;
2770 if (!EQ (pwindow, root))
2771 error ("Specified root is not an ancestor of specified window");
2774 if (EQ (window, root))
2775 /* A noop. */
2776 return Qnil;
2777 /* I don't understand the "top > 0" part below. If we deal with a
2778 standalone minibuffer it would have been caught by the preceding
2779 test. */
2780 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2781 error ("Can't expand minibuffer to full frame");
2783 if (BUFFERP (w->contents))
2785 startpos = marker_position (w->start);
2786 startbyte = marker_byte_position (w->start);
2787 top = WINDOW_TOP_EDGE_LINE (w)
2788 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2789 /* Make sure WINDOW is the frame's selected window. */
2790 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2792 if (EQ (selected_frame, w->frame))
2793 Fselect_window (window, Qnil);
2794 else
2795 fset_selected_window (f, window);
2798 else
2800 /* See if the frame's selected window is a part of the window
2801 subtree rooted at WINDOW, by finding all the selected window's
2802 parents and comparing each one with WINDOW. If it isn't we
2803 need a new selected window for this frame. */
2804 swindow = FRAME_SELECTED_WINDOW (f);
2805 while (1)
2807 pwindow = swindow;
2808 while (!NILP (pwindow) && !EQ (window, pwindow))
2809 pwindow = XWINDOW (pwindow)->parent;
2811 if (EQ (window, pwindow))
2812 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2813 as the new selected window. */
2814 break;
2815 else
2816 /* Else try the previous window of SWINDOW. */
2817 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2820 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2822 if (EQ (selected_frame, w->frame))
2823 Fselect_window (swindow, Qnil);
2824 else
2825 fset_selected_window (f, swindow);
2829 block_input ();
2830 if (!FRAME_INITIAL_P (f))
2832 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2834 /* We are going to free the glyph matrices of WINDOW, and with
2835 that we might lose any information about glyph rows that have
2836 some of their glyphs highlighted in mouse face. (These rows
2837 are marked with a non-zero mouse_face_p flag.) If WINDOW
2838 indeed has some glyphs highlighted in mouse face, signal to
2839 frame's up-to-date hook that mouse highlight was overwritten,
2840 so that it will arrange for redisplaying the highlight. */
2841 if (EQ (hlinfo->mouse_face_window, window))
2842 reset_mouse_highlight (hlinfo);
2844 free_window_matrices (r);
2846 windows_or_buffers_changed++;
2847 Vwindow_list = Qnil;
2848 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2849 resize_failed = 0;
2851 if (!WINDOW_LEAF_P (w))
2853 /* Resize child windows vertically. */
2854 XSETINT (delta, r->total_lines - w->total_lines);
2855 w->top_line = r->top_line;
2856 resize_root_window (window, delta, Qnil, Qnil);
2857 if (window_resize_check (w, 0))
2858 window_resize_apply (w, 0);
2859 else
2861 resize_root_window (window, delta, Qnil, Qt);
2862 if (window_resize_check (w, 0))
2863 window_resize_apply (w, 0);
2864 else
2865 resize_failed = 1;
2868 /* Resize child windows horizontally. */
2869 if (!resize_failed)
2871 w->left_col = r->left_col;
2872 XSETINT (delta, r->total_cols - w->total_cols);
2873 resize_root_window (window, delta, Qt, Qnil);
2874 if (window_resize_check (w, 1))
2875 window_resize_apply (w, 1);
2876 else
2878 resize_root_window (window, delta, Qt, Qt);
2879 if (window_resize_check (w, 1))
2880 window_resize_apply (w, 1);
2881 else
2882 resize_failed = 1;
2886 if (resize_failed)
2887 /* Play safe, if we still can ... */
2889 window = swindow;
2890 w = XWINDOW (window);
2894 /* Cleanly unlink WINDOW from window-tree. */
2895 if (!NILP (w->prev))
2896 /* Get SIBLING above (on the left of) WINDOW. */
2898 sibling = w->prev;
2899 s = XWINDOW (sibling);
2900 wset_next (s, w->next);
2901 if (!NILP (s->next))
2902 wset_prev (XWINDOW (s->next), sibling);
2904 else
2905 /* Get SIBLING below (on the right of) WINDOW. */
2907 sibling = w->next;
2908 s = XWINDOW (sibling);
2909 wset_prev (s, Qnil);
2910 wset_combination (XWINDOW (w->parent),
2911 XWINDOW (w->parent)->horizontal, sibling);
2914 /* Delete ROOT and all child windows of ROOT. */
2915 if (WINDOWP (r->contents))
2917 delete_all_child_windows (r->contents);
2918 wset_combination (r, 0, Qnil);
2921 replace_window (root, window, 1);
2923 /* This must become SWINDOW anyway ....... */
2924 if (BUFFERP (w->contents) && !resize_failed)
2926 /* Try to minimize scrolling, by setting the window start to the
2927 point will cause the text at the old window start to be at the
2928 same place on the frame. But don't try to do this if the
2929 window start is outside the visible portion (as might happen
2930 when the display is not current, due to typeahead). */
2931 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2932 if (new_top != top
2933 && startpos >= BUF_BEGV (XBUFFER (w->contents))
2934 && startpos <= BUF_ZV (XBUFFER (w->contents)))
2936 struct position pos;
2937 struct buffer *obuf = current_buffer;
2939 Fset_buffer (w->contents);
2940 /* This computation used to temporarily move point, but that
2941 can have unwanted side effects due to text properties. */
2942 pos = *vmotion (startpos, startbyte, -top, w);
2944 set_marker_both (w->start, w->contents, pos.bufpos, pos.bytepos);
2945 w->window_end_valid = 0;
2946 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
2947 || FETCH_BYTE (pos.bytepos - 1) == '\n');
2948 /* We need to do this, so that the window-scroll-functions
2949 get called. */
2950 w->optional_new_start = 1;
2952 set_buffer_internal (obuf);
2956 adjust_frame_glyphs (f);
2957 unblock_input ();
2959 run_window_configuration_change_hook (f);
2961 return Qnil;
2965 void
2966 replace_buffer_in_windows (Lisp_Object buffer)
2968 call1 (Qreplace_buffer_in_windows, buffer);
2971 /* If BUFFER is shown in a window, safely replace it with some other
2972 buffer in all windows of all frames, even those on other keyboards. */
2974 void
2975 replace_buffer_in_windows_safely (Lisp_Object buffer)
2977 if (buffer_window_count (XBUFFER (buffer)))
2979 Lisp_Object tail, frame;
2981 /* A single call to window_loop won't do the job because it only
2982 considers frames on the current keyboard. So loop manually over
2983 frames, and handle each one. */
2984 FOR_EACH_FRAME (tail, frame)
2985 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
2989 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2990 minimum allowable size. */
2992 void
2993 check_frame_size (struct frame *frame, int *rows, int *cols)
2995 /* For height, we have to see:
2996 how many windows the frame has at minimum (one or two),
2997 and whether it has a menu bar or other special stuff at the top. */
2998 int min_height
2999 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
3000 ? MIN_SAFE_WINDOW_HEIGHT
3001 : 2 * MIN_SAFE_WINDOW_HEIGHT);
3003 if (FRAME_TOP_MARGIN (frame) > 0)
3004 min_height += FRAME_TOP_MARGIN (frame);
3006 if (*rows < min_height)
3007 *rows = min_height;
3008 if (*cols < MIN_SAFE_WINDOW_WIDTH)
3009 *cols = MIN_SAFE_WINDOW_WIDTH;
3012 /* Adjust the margins of window W if text area is too small.
3013 Return 1 if window width is ok after adjustment; 0 if window
3014 is still too narrow. */
3016 static int
3017 adjust_window_margins (struct window *w)
3019 int box_cols = (WINDOW_TOTAL_COLS (w)
3020 - WINDOW_FRINGE_COLS (w)
3021 - WINDOW_SCROLL_BAR_COLS (w));
3022 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
3023 + WINDOW_RIGHT_MARGIN_COLS (w));
3025 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
3026 return 1;
3028 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
3029 return 0;
3031 /* Window's text area is too narrow, but reducing the window
3032 margins will fix that. */
3033 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
3034 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
3036 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
3037 w->left_margin_cols = w->right_margin_cols = margin_cols / 2;
3038 else
3039 w->right_margin_cols = margin_cols;
3041 else
3042 w->left_margin_cols = margin_cols;
3043 return 1;
3046 /* The following three routines are needed for running a window's
3047 configuration change hook. */
3048 static void
3049 run_funs (Lisp_Object funs)
3051 for (; CONSP (funs); funs = XCDR (funs))
3052 if (!EQ (XCAR (funs), Qt))
3053 call0 (XCAR (funs));
3056 static void
3057 select_window_norecord (Lisp_Object window)
3059 if (WINDOW_LIVE_P (window))
3060 Fselect_window (window, Qt);
3063 static void
3064 select_frame_norecord (Lisp_Object frame)
3066 if (FRAME_LIVE_P (XFRAME (frame)))
3067 Fselect_frame (frame, Qt);
3070 void
3071 run_window_configuration_change_hook (struct frame *f)
3073 ptrdiff_t count = SPECPDL_INDEX ();
3074 Lisp_Object frame, global_wcch
3075 = Fdefault_value (Qwindow_configuration_change_hook);
3076 XSETFRAME (frame, f);
3078 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
3079 return;
3081 /* Use the right buffer. Matters when running the local hooks. */
3082 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3084 record_unwind_current_buffer ();
3085 Fset_buffer (Fwindow_buffer (Qnil));
3088 if (SELECTED_FRAME () != f)
3090 record_unwind_protect (select_frame_norecord, selected_frame);
3091 select_frame_norecord (frame);
3094 /* Look for buffer-local values. */
3096 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3097 for (; CONSP (windows); windows = XCDR (windows))
3099 Lisp_Object window = XCAR (windows);
3100 Lisp_Object buffer = Fwindow_buffer (window);
3101 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3102 buffer)))
3104 ptrdiff_t inner_count = SPECPDL_INDEX ();
3105 record_unwind_protect (select_window_norecord, selected_window);
3106 select_window_norecord (window);
3107 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3108 buffer));
3109 unbind_to (inner_count, Qnil);
3114 run_funs (global_wcch);
3115 unbind_to (count, Qnil);
3118 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
3119 Srun_window_configuration_change_hook, 0, 1, 0,
3120 doc: /* Run `window-configuration-change-hook' for FRAME.
3121 If FRAME is omitted or nil, it defaults to the selected frame. */)
3122 (Lisp_Object frame)
3124 run_window_configuration_change_hook (decode_live_frame (frame));
3125 return Qnil;
3128 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3129 means it's allowed to run hooks. See make_frame for a case where
3130 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3131 margins, fringes, and scroll-bar settings of the window are not
3132 reset from the buffer's local settings. */
3134 void
3135 set_window_buffer (Lisp_Object window, Lisp_Object buffer,
3136 bool run_hooks_p, bool keep_margins_p)
3138 struct window *w = XWINDOW (window);
3139 struct buffer *b = XBUFFER (buffer);
3140 ptrdiff_t count = SPECPDL_INDEX ();
3141 int samebuf = EQ (buffer, w->contents);
3143 wset_buffer (w, buffer);
3145 if (EQ (window, selected_window))
3146 bset_last_selected_window (b, window);
3148 /* Let redisplay errors through. */
3149 b->display_error_modiff = 0;
3151 /* Update time stamps of buffer display. */
3152 if (INTEGERP (BVAR (b, display_count)))
3153 bset_display_count (b, make_number (XINT (BVAR (b, display_count)) + 1));
3154 bset_display_time (b, Fcurrent_time ());
3156 w->window_end_pos = 0;
3157 w->window_end_vpos = 0;
3158 w->last_cursor_vpos = 0;
3160 if (!(keep_margins_p && samebuf))
3161 { /* If we're not actually changing the buffer, don't reset hscroll and
3162 vscroll. This case happens for example when called from
3163 change_frame_size_1, where we use a dummy call to
3164 Fset_window_buffer on the frame's selected window (and no other)
3165 just in order to run window-configuration-change-hook.
3166 Resetting hscroll and vscroll here is problematic for things like
3167 image-mode and doc-view-mode since it resets the image's position
3168 whenever we resize the frame. */
3169 w->hscroll = w->min_hscroll = 0;
3170 w->vscroll = 0;
3171 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3172 set_marker_restricted (w->start,
3173 make_number (b->last_window_start),
3174 buffer);
3175 w->start_at_line_beg = 0;
3176 w->force_start = 0;
3178 /* Maybe we could move this into the `if' but it's not obviously safe and
3179 I doubt it's worth the trouble. */
3180 windows_or_buffers_changed++;
3182 /* We must select BUFFER for running the window-scroll-functions. */
3183 /* We can't check ! NILP (Vwindow_scroll_functions) here
3184 because that might itself be a local variable. */
3185 if (window_initialized)
3187 record_unwind_current_buffer ();
3188 Fset_buffer (buffer);
3191 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3193 if (!keep_margins_p)
3195 /* Set left and right marginal area width etc. from buffer. */
3196 set_window_fringes (w, BVAR (b, left_fringe_width),
3197 BVAR (b, right_fringe_width),
3198 BVAR (b, fringes_outside_margins));
3199 set_window_scroll_bars (w, BVAR (b, scroll_bar_width),
3200 BVAR (b, vertical_scroll_bar_type), Qnil);
3201 set_window_margins (w, BVAR (b, left_margin_cols),
3202 BVAR (b, right_margin_cols));
3203 apply_window_adjustment (w);
3206 if (run_hooks_p)
3208 if (! NILP (Vwindow_scroll_functions))
3209 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3210 Fmarker_position (w->start));
3211 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3214 unbind_to (count, Qnil);
3217 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3218 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3219 WINDOW must be a live window and defaults to the selected one.
3220 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3222 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3223 display margins, fringe widths, and scroll bar settings are preserved;
3224 the default is to reset these from the local settings for BUFFER-OR-NAME
3225 or the frame defaults. Return nil.
3227 This function throws an error when WINDOW is strongly dedicated to its
3228 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3229 already display BUFFER-OR-NAME.
3231 This function runs `window-scroll-functions' before running
3232 `window-configuration-change-hook'. */)
3233 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3235 register Lisp_Object tem, buffer;
3236 register struct window *w = decode_live_window (window);
3238 XSETWINDOW (window, w);
3239 buffer = Fget_buffer (buffer_or_name);
3240 CHECK_BUFFER (buffer);
3241 if (!BUFFER_LIVE_P (XBUFFER (buffer)))
3242 error ("Attempt to display deleted buffer");
3244 tem = w->contents;
3245 if (NILP (tem))
3246 error ("Window is deleted");
3247 else
3249 if (!EQ (tem, buffer))
3251 if (EQ (w->dedicated, Qt))
3252 /* WINDOW is strongly dedicated to its buffer, signal an
3253 error. */
3254 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3255 else
3256 /* WINDOW is weakly dedicated to its buffer, reset
3257 dedication. */
3258 wset_dedicated (w, Qnil);
3260 call1 (Qrecord_window_buffer, window);
3263 unshow_buffer (w);
3266 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3268 return Qnil;
3271 static Lisp_Object
3272 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3274 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3277 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3278 0, 1, 0,
3279 doc: /* Force all windows to be updated on next redisplay.
3280 If optional arg OBJECT is a window, force redisplay of that window only.
3281 If OBJECT is a buffer or buffer name, force redisplay of all windows
3282 displaying that buffer. */)
3283 (Lisp_Object object)
3285 if (NILP (object))
3287 windows_or_buffers_changed++;
3288 update_mode_lines++;
3289 return Qt;
3292 if (WINDOWP (object))
3294 struct window *w = XWINDOW (object);
3295 mark_window_display_accurate (object, 0);
3296 w->update_mode_line = 1;
3297 if (BUFFERP (w->contents))
3298 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
3299 ++update_mode_lines;
3300 return Qt;
3303 if (STRINGP (object))
3304 object = Fget_buffer (object);
3305 if (BUFFERP (object) && BUFFER_LIVE_P (XBUFFER (object))
3306 && buffer_window_count (XBUFFER (object)))
3308 /* If buffer is live and shown in at least one window, find
3309 all windows showing this buffer and force update of them. */
3310 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3311 return NILP (object) ? Qnil : Qt;
3314 /* If nothing suitable was found, just return.
3315 We could signal an error, but this feature will typically be used
3316 asynchronously in timers or process sentinels, so we don't. */
3317 return Qnil;
3320 /* Obsolete since 24.3. */
3321 void
3322 temp_output_buffer_show (register Lisp_Object buf)
3324 register struct buffer *old = current_buffer;
3325 register Lisp_Object window;
3326 register struct window *w;
3328 bset_directory (XBUFFER (buf), BVAR (current_buffer, directory));
3330 Fset_buffer (buf);
3331 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3332 BEGV = BEG;
3333 ZV = Z;
3334 SET_PT (BEG);
3335 set_buffer_internal (old);
3337 if (!NILP (Vtemp_buffer_show_function))
3338 call1 (Vtemp_buffer_show_function, buf);
3339 else if (WINDOW_LIVE_P (window = display_buffer (buf, Qnil, Qnil)))
3341 if (!EQ (XWINDOW (window)->frame, selected_frame))
3342 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3343 Vminibuf_scroll_window = window;
3344 w = XWINDOW (window);
3345 w->hscroll = 0;
3346 w->min_hscroll = 0;
3347 set_marker_restricted_both (w->start, buf, BEG, BEG);
3348 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3350 /* Run temp-buffer-show-hook, with the chosen window selected
3351 and its buffer current. */
3353 ptrdiff_t count = SPECPDL_INDEX ();
3354 Lisp_Object prev_window, prev_buffer;
3355 prev_window = selected_window;
3356 XSETBUFFER (prev_buffer, old);
3358 /* Select the window that was chosen, for running the hook.
3359 Note: Both Fselect_window and select_window_norecord may
3360 set-buffer to the buffer displayed in the window,
3361 so we need to save the current buffer. --stef */
3362 record_unwind_protect (restore_buffer, prev_buffer);
3363 record_unwind_protect (select_window_norecord, prev_window);
3364 Fselect_window (window, Qt);
3365 Fset_buffer (w->contents);
3366 Frun_hooks (1, &Qtemp_buffer_show_hook);
3367 unbind_to (count, Qnil);
3372 /* Make new window, have it replace WINDOW in window-tree, and make
3373 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3374 horizontal child). */
3375 static void
3376 make_parent_window (Lisp_Object window, bool horflag)
3378 Lisp_Object parent;
3379 register struct window *o, *p;
3381 o = XWINDOW (window);
3382 p = allocate_window ();
3383 memcpy ((char *) p + sizeof (struct vectorlike_header),
3384 (char *) o + sizeof (struct vectorlike_header),
3385 word_size * VECSIZE (struct window));
3386 /* P's buffer slot may change from nil to a buffer... */
3387 adjust_window_count (p, 1);
3388 XSETWINDOW (parent, p);
3390 replace_window (window, parent, 1);
3392 wset_next (o, Qnil);
3393 wset_prev (o, Qnil);
3394 wset_parent (o, parent);
3395 /* ...but now P becomes an internal window. */
3396 wset_start (p, Qnil);
3397 wset_pointm (p, Qnil);
3398 wset_buffer (p, Qnil);
3399 wset_combination (p, horflag, window);
3400 wset_combination_limit (p, Qnil);
3401 wset_window_parameters (p, Qnil);
3404 /* Make new window from scratch. */
3405 Lisp_Object
3406 make_window (void)
3408 Lisp_Object window;
3409 register struct window *w;
3411 w = allocate_window ();
3412 /* Initialize Lisp data. Note that allocate_window initializes all
3413 Lisp data to nil, so do it only for slots which should not be nil. */
3414 wset_normal_lines (w, make_float (1.0));
3415 wset_normal_cols (w, make_float (1.0));
3416 wset_new_total (w, make_number (0));
3417 wset_new_normal (w, make_number (0));
3418 wset_start (w, Fmake_marker ());
3419 wset_pointm (w, Fmake_marker ());
3420 wset_vertical_scroll_bar_type (w, Qt);
3421 /* These Lisp fields are marked specially so they're not set to nil by
3422 allocate_window. */
3423 wset_prev_buffers (w, Qnil);
3424 wset_next_buffers (w, Qnil);
3426 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3427 non-Lisp data, so do it only for slots which should not be zero. */
3428 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3429 w->left_fringe_width = w->right_fringe_width = -1;
3430 w->mode_line_height = w->header_line_height = -1;
3431 w->phys_cursor_type = -1;
3432 w->phys_cursor_width = -1;
3433 w->scroll_bar_width = -1;
3434 w->column_number_displayed = -1;
3436 /* Reset window_list. */
3437 Vwindow_list = Qnil;
3438 /* Return window. */
3439 XSETWINDOW (window, w);
3440 return window;
3443 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3444 doc: /* Set new total size of WINDOW to SIZE.
3445 WINDOW must be a valid window and defaults to the selected one.
3446 Return SIZE.
3448 Optional argument ADD non-nil means add SIZE to the new total size of
3449 WINDOW and return the sum.
3451 Note: This function does not operate on any child windows of WINDOW. */)
3452 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3454 struct window *w = decode_valid_window (window);
3456 CHECK_NUMBER (size);
3457 if (NILP (add))
3458 wset_new_total (w, size);
3459 else
3460 wset_new_total (w, make_number (XINT (w->new_total) + XINT (size)));
3462 return w->new_total;
3465 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3466 doc: /* Set new normal size of WINDOW to SIZE.
3467 WINDOW must be a valid window and defaults to the selected one.
3468 Return SIZE.
3470 Note: This function does not operate on any child windows of WINDOW. */)
3471 (Lisp_Object window, Lisp_Object size)
3473 wset_new_normal (decode_valid_window (window), size);
3474 return size;
3477 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3478 non-zero) to w->new_total would result in correct heights (widths)
3479 for window W and recursively all child windows of W.
3481 Note: This function does not check any of `window-fixed-size-p',
3482 `window-min-height' or `window-min-width'. It does check that window
3483 sizes do not drop below one line (two columns). */
3484 static int
3485 window_resize_check (struct window *w, bool horflag)
3487 struct window *c;
3489 if (WINDOW_VERTICAL_COMBINATION_P (w))
3490 /* W is a vertical combination. */
3492 c = XWINDOW (w->contents);
3493 if (horflag)
3494 /* All child windows of W must have the same width as W. */
3496 while (c)
3498 if ((XINT (c->new_total) != XINT (w->new_total))
3499 || !window_resize_check (c, horflag))
3500 return 0;
3501 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3503 return 1;
3505 else
3506 /* The sum of the heights of the child windows of W must equal
3507 W's height. */
3509 int sum_of_sizes = 0;
3510 while (c)
3512 if (!window_resize_check (c, horflag))
3513 return 0;
3514 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3515 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3517 return (sum_of_sizes == XINT (w->new_total));
3520 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3521 /* W is a horizontal combination. */
3523 c = XWINDOW (w->contents);
3524 if (horflag)
3525 /* The sum of the widths of the child windows of W must equal W's
3526 width. */
3528 int sum_of_sizes = 0;
3529 while (c)
3531 if (!window_resize_check (c, horflag))
3532 return 0;
3533 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3534 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3536 return (sum_of_sizes == XINT (w->new_total));
3538 else
3539 /* All child windows of W must have the same height as W. */
3541 while (c)
3543 if ((XINT (c->new_total) != XINT (w->new_total))
3544 || !window_resize_check (c, horflag))
3545 return 0;
3546 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3548 return 1;
3551 else
3552 /* A leaf window. Make sure it's not too small. The following
3553 hardcodes the values of `window-safe-min-width' (2) and
3554 `window-safe-min-height' (1) which are defined in window.el. */
3555 return XINT (w->new_total) >= (horflag ? 2 : 1);
3558 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3559 w->new_total for window W and recursively all child windows of W.
3560 Also calculate and assign the new vertical (horizontal) start
3561 positions of each of these windows.
3563 This function does not perform any error checks. Make sure you have
3564 run window_resize_check on W before applying this function. */
3565 static void
3566 window_resize_apply (struct window *w, bool horflag)
3568 struct window *c;
3569 int pos;
3571 /* Note: Assigning new_normal requires that the new total size of the
3572 parent window has been set *before*. */
3573 if (horflag)
3575 w->total_cols = XFASTINT (w->new_total);
3576 if (NUMBERP (w->new_normal))
3577 wset_normal_cols (w, w->new_normal);
3579 pos = w->left_col;
3581 else
3583 w->total_lines = XFASTINT (w->new_total);
3584 if (NUMBERP (w->new_normal))
3585 wset_normal_lines (w, w->new_normal);
3587 pos = w->top_line;
3590 if (WINDOW_VERTICAL_COMBINATION_P (w))
3591 /* W is a vertical combination. */
3593 c = XWINDOW (w->contents);
3594 while (c)
3596 if (horflag)
3597 c->left_col = pos;
3598 else
3599 c->top_line = pos;
3600 window_resize_apply (c, horflag);
3601 if (!horflag)
3602 pos = pos + c->total_lines;
3603 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3606 else if (WINDOW_HORIZONTAL_COMBINATION_P (w))
3607 /* W is a horizontal combination. */
3609 c = XWINDOW (w->contents);
3610 while (c)
3612 if (horflag)
3613 c->left_col = pos;
3614 else
3615 c->top_line = pos;
3616 window_resize_apply (c, horflag);
3617 if (horflag)
3618 pos = pos + c->total_cols;
3619 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3625 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 0, 2, 0,
3626 doc: /* Apply requested size values for window-tree of FRAME.
3627 If FRAME is omitted or nil, it defaults to the selected frame.
3629 Optional argument HORIZONTAL omitted or nil means apply requested
3630 height values. HORIZONTAL non-nil means apply requested width values.
3632 This function checks whether the requested values sum up to a valid
3633 window layout, recursively assigns the new sizes of all child windows
3634 and calculates and assigns the new start positions of these windows.
3636 Note: This function does not check any of `window-fixed-size-p',
3637 `window-min-height' or `window-min-width'. All these checks have to
3638 be applied on the Elisp level. */)
3639 (Lisp_Object frame, Lisp_Object horizontal)
3641 struct frame *f = decode_live_frame (frame);
3642 struct window *r = XWINDOW (FRAME_ROOT_WINDOW (f));
3643 bool horflag = !NILP (horizontal);
3645 if (!window_resize_check (r, horflag)
3646 || (XINT (r->new_total)
3647 != (horflag ? r->total_cols : r->total_lines)))
3648 return Qnil;
3650 block_input ();
3651 window_resize_apply (r, horflag);
3653 windows_or_buffers_changed++;
3654 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3656 adjust_frame_glyphs (f);
3657 unblock_input ();
3659 run_window_configuration_change_hook (f);
3661 return Qt;
3665 /* Resize frame F's windows when number of lines of F is set to SIZE.
3666 HORFLAG 1 means resize windows when number of columns of F is set to
3667 SIZE.
3669 This function can delete all windows but the selected one in order to
3670 satisfy the request. The result will be meaningful if and only if
3671 F's windows have meaningful sizes when you call this. */
3672 void
3673 resize_frame_windows (struct frame *f, int size, bool horflag)
3675 Lisp_Object root = f->root_window;
3676 struct window *r = XWINDOW (root);
3677 Lisp_Object mini = f->minibuffer_window;
3678 struct window *m;
3679 /* new_size is the new size of the frame's root window. */
3680 int new_size = (horflag
3681 ? size
3682 : (size
3683 - FRAME_TOP_MARGIN (f)
3684 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3685 ? 1 : 0)));
3687 r->top_line = FRAME_TOP_MARGIN (f);
3688 if (WINDOW_LEAF_P (r))
3689 /* For a leaf root window just set the size. */
3690 if (horflag)
3691 r->total_cols = new_size;
3692 else
3693 r->total_lines = new_size;
3694 else
3696 /* old_size is the old size of the frame's root window. */
3697 int old_size = horflag ? r->total_cols : r->total_lines;
3698 Lisp_Object delta;
3700 XSETINT (delta, new_size - old_size);
3701 /* Try a "normal" resize first. */
3702 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3703 if (window_resize_check (r, horflag)
3704 && new_size == XINT (r->new_total))
3705 window_resize_apply (r, horflag);
3706 else
3708 /* Try with "reasonable" minimum sizes next. */
3709 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3710 if (window_resize_check (r, horflag)
3711 && new_size == XINT (r->new_total))
3712 window_resize_apply (r, horflag);
3713 else
3715 /* Finally, try with "safe" minimum sizes. */
3716 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3717 if (window_resize_check (r, horflag)
3718 && new_size == XINT (r->new_total))
3719 window_resize_apply (r, horflag);
3720 else
3722 /* We lost. Delete all windows but the frame's
3723 selected one. */
3724 root = f->selected_window;
3725 Fdelete_other_windows_internal (root, Qnil);
3726 if (horflag)
3727 XWINDOW (root)->total_cols = new_size;
3728 else
3729 XWINDOW (root)->total_lines = new_size;
3735 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3737 m = XWINDOW (mini);
3738 if (horflag)
3739 m->total_cols = size;
3740 else
3742 /* Are we sure we always want 1 line here? */
3743 m->total_lines = 1;
3744 m->top_line = r->top_line + r->total_lines;
3748 windows_or_buffers_changed++;
3752 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3753 doc: /* Split window OLD.
3754 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3755 new window. In any case TOTAL-SIZE must be a positive integer.
3757 Third argument SIDE nil (or `below') specifies that the new window shall
3758 be located below WINDOW. SIDE `above' means the new window shall be
3759 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3760 lines of the new window including space reserved for the mode and/or
3761 header line.
3763 SIDE t (or `right') specifies that the new window shall be located on
3764 the right side of WINDOW. SIDE `left' means the new window shall be
3765 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3766 number of columns of the new window including space reserved for fringes
3767 and the scrollbar or a divider column.
3769 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3770 according to the SIDE argument.
3772 The new total and normal sizes of all involved windows must have been
3773 set correctly. See the code of `split-window' for how this is done. */)
3774 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3776 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3777 parent window or an internal window we have to install as OLD's new
3778 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3779 provided OLD is a leaf window, or to the frame's selected window.
3780 NEW (*n) is the new window created with some parameters taken from
3781 REFERENCE (*r). */
3782 register Lisp_Object new, frame, reference;
3783 register struct window *o, *p, *n, *r;
3784 struct frame *f;
3785 bool horflag
3786 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3787 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3788 int combination_limit = 0;
3790 CHECK_WINDOW (old);
3791 o = XWINDOW (old);
3792 frame = WINDOW_FRAME (o);
3793 f = XFRAME (frame);
3795 CHECK_NUMBER (total_size);
3797 /* Set combination_limit to 1 if we have to make a new parent window.
3798 We do that if either `window-combination-limit' is t, or OLD has no
3799 parent, or OLD is ortho-combined. */
3800 combination_limit =
3801 EQ (Vwindow_combination_limit, Qt)
3802 || NILP (o->parent)
3803 || (horflag
3804 ? WINDOW_VERTICAL_COMBINATION_P (XWINDOW (o->parent))
3805 : WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (o->parent)));
3807 /* We need a live reference window to initialize some parameters. */
3808 if (WINDOW_LIVE_P (old))
3809 /* OLD is live, use it as reference window. */
3810 reference = old;
3811 else
3812 /* Use the frame's selected window as reference window. */
3813 reference = FRAME_SELECTED_WINDOW (f);
3814 r = XWINDOW (reference);
3816 /* The following bugs are caught by `split-window'. */
3817 if (MINI_WINDOW_P (o))
3818 error ("Attempt to split minibuffer window");
3819 else if (XINT (total_size) < (horflag ? 2 : 1))
3820 error ("Size of new window too small (after split)");
3821 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3822 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3823 proportionally. */
3825 p = XWINDOW (o->parent);
3826 /* Temporarily pretend we split the parent window. */
3827 wset_new_total
3828 (p, make_number ((horflag ? p->total_cols : p->total_lines)
3829 - XINT (total_size)));
3830 if (!window_resize_check (p, horflag))
3831 error ("Window sizes don't fit");
3832 else
3833 /* Undo the temporary pretension. */
3834 wset_new_total (p, make_number
3835 (horflag ? p->total_cols : p->total_lines));
3837 else
3839 if (!window_resize_check (o, horflag))
3840 error ("Resizing old window failed");
3841 else if (XINT (total_size) + XINT (o->new_total)
3842 != (horflag ? o->total_cols : o->total_lines))
3843 error ("Sum of sizes of old and new window don't fit");
3846 /* This is our point of no return. */
3847 if (combination_limit)
3849 /* Save the old value of o->normal_cols/lines. It gets corrupted
3850 by make_parent_window and we need it below for assigning it to
3851 p->new_normal. */
3852 Lisp_Object new_normal
3853 = horflag ? o->normal_cols : o->normal_lines;
3855 make_parent_window (old, horflag);
3856 p = XWINDOW (o->parent);
3857 if (EQ (Vwindow_combination_limit, Qt))
3858 /* Store t in the new parent's combination_limit slot to avoid
3859 that its children get merged into another window. */
3860 wset_combination_limit (p, Qt);
3861 /* These get applied below. */
3862 wset_new_total (p, make_number
3863 (horflag ? o->total_cols : o->total_lines));
3864 wset_new_normal (p, new_normal);
3866 else
3867 p = XWINDOW (o->parent);
3869 windows_or_buffers_changed++;
3870 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3871 new = make_window ();
3872 n = XWINDOW (new);
3873 wset_frame (n, frame);
3874 wset_parent (n, o->parent);
3876 if (EQ (side, Qabove) || EQ (side, Qleft))
3878 wset_prev (n, o->prev);
3879 if (NILP (n->prev))
3880 wset_combination (p, horflag, new);
3881 else
3882 wset_next (XWINDOW (n->prev), new);
3883 wset_next (n, old);
3884 wset_prev (o, new);
3886 else
3888 wset_next (n, o->next);
3889 if (!NILP (n->next))
3890 wset_prev (XWINDOW (n->next), new);
3891 wset_prev (n, old);
3892 wset_next (o, new);
3895 n->window_end_valid = 0;
3896 n->last_cursor_vpos = 0;
3898 /* Get special geometry settings from reference window. */
3899 n->left_margin_cols = r->left_margin_cols;
3900 n->right_margin_cols = r->right_margin_cols;
3901 n->left_fringe_width = r->left_fringe_width;
3902 n->right_fringe_width = r->right_fringe_width;
3903 n->fringes_outside_margins = r->fringes_outside_margins;
3904 n->scroll_bar_width = r->scroll_bar_width;
3905 wset_vertical_scroll_bar_type (n, r->vertical_scroll_bar_type);
3907 /* Directly assign orthogonal coordinates and sizes. */
3908 if (horflag)
3910 n->top_line = o->top_line;
3911 n->total_lines = o->total_lines;
3913 else
3915 n->left_col = o->left_col;
3916 n->total_cols = o->total_cols;
3919 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3920 get them ready here. */
3921 wset_new_total (n, total_size);
3922 wset_new_normal (n, normal_size);
3924 block_input ();
3925 window_resize_apply (p, horflag);
3926 adjust_frame_glyphs (f);
3927 /* Set buffer of NEW to buffer of reference window. Don't run
3928 any hooks. */
3929 set_window_buffer (new, r->contents, 0, 1);
3930 unblock_input ();
3932 /* Maybe we should run the scroll functions in Elisp (which already
3933 runs the configuration change hook). */
3934 if (! NILP (Vwindow_scroll_functions))
3935 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3936 Fmarker_position (n->start));
3937 /* Return NEW. */
3938 return new;
3942 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3943 doc: /* Remove WINDOW from its frame.
3944 WINDOW defaults to the selected window. Return nil.
3945 Signal an error when WINDOW is the only window on its frame. */)
3946 (register Lisp_Object window)
3948 register Lisp_Object parent, sibling, frame, root;
3949 struct window *w, *p, *s, *r;
3950 struct frame *f;
3951 bool horflag, before_sibling = 0;
3953 w = decode_any_window (window);
3954 XSETWINDOW (window, w);
3955 if (NILP (w->contents))
3956 /* It's a no-op to delete an already deleted window. */
3957 return Qnil;
3959 parent = w->parent;
3960 if (NILP (parent))
3961 /* Never delete a minibuffer or frame root window. */
3962 error ("Attempt to delete minibuffer or sole ordinary window");
3963 else if (NILP (w->prev) && NILP (w->next))
3964 /* Rather bow out here, this case should be handled on the Elisp
3965 level. */
3966 error ("Attempt to delete sole window of parent");
3968 p = XWINDOW (parent);
3969 horflag = WINDOW_HORIZONTAL_COMBINATION_P (p);
3971 frame = WINDOW_FRAME (w);
3972 f = XFRAME (frame);
3974 root = FRAME_ROOT_WINDOW (f);
3975 r = XWINDOW (root);
3977 /* Unlink WINDOW from window tree. */
3978 if (NILP (w->prev))
3979 /* Get SIBLING below (on the right of) WINDOW. */
3981 /* before_sibling 1 means WINDOW is the first child of its
3982 parent and thus before the sibling. */
3983 before_sibling = 1;
3984 sibling = w->next;
3985 s = XWINDOW (sibling);
3986 wset_prev (s, Qnil);
3987 wset_combination (p, horflag, sibling);
3989 else
3990 /* Get SIBLING above (on the left of) WINDOW. */
3992 sibling = w->prev;
3993 s = XWINDOW (sibling);
3994 wset_next (s, w->next);
3995 if (!NILP (s->next))
3996 wset_prev (XWINDOW (s->next), sibling);
3999 if (window_resize_check (r, horflag)
4000 && (XINT (r->new_total)
4001 == (horflag ? r->total_cols : r->total_lines)))
4002 /* We can delete WINDOW now. */
4005 /* Block input. */
4006 block_input ();
4007 #ifdef HAVE_XWIDGETS
4008 xwidget_view_delete_all_in_window(w);
4009 #endif
4010 window_resize_apply (p, horflag);
4012 /* If this window is referred to by the dpyinfo's mouse
4013 highlight, invalidate that slot to be safe (Bug#9904). */
4014 if (!FRAME_INITIAL_P (f))
4016 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
4018 if (EQ (hlinfo->mouse_face_window, window))
4019 hlinfo->mouse_face_window = Qnil;
4022 windows_or_buffers_changed++;
4023 Vwindow_list = Qnil;
4024 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4026 wset_next (w, Qnil); /* Don't delete w->next too. */
4027 free_window_matrices (w);
4029 if (WINDOWP (w->contents))
4031 delete_all_child_windows (w->contents);
4032 wset_combination (w, 0, Qnil);
4034 else
4036 unshow_buffer (w);
4037 unchain_marker (XMARKER (w->pointm));
4038 unchain_marker (XMARKER (w->start));
4039 wset_buffer (w, Qnil);
4042 if (NILP (s->prev) && NILP (s->next))
4043 /* A matrjoshka where SIBLING has become the only child of
4044 PARENT. */
4046 /* Put SIBLING into PARENT's place. */
4047 replace_window (parent, sibling, 0);
4048 /* Have SIBLING inherit the following three slot values from
4049 PARENT (the combination_limit slot is not inherited). */
4050 wset_normal_cols (s, p->normal_cols);
4051 wset_normal_lines (s, p->normal_lines);
4052 /* Mark PARENT as deleted. */
4053 wset_combination (p, 0, Qnil);
4054 /* Try to merge SIBLING into its new parent. */
4055 recombine_windows (sibling);
4058 adjust_frame_glyphs (f);
4060 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
4061 /* We deleted the frame's selected window. */
4063 /* Use the frame's first window as fallback ... */
4064 Lisp_Object new_selected_window = Fframe_first_window (frame);
4065 /* ... but preferably use its most recently used window. */
4066 Lisp_Object mru_window;
4068 /* `get-mru-window' might fail for some reason so play it safe
4069 - promote the first window _without recording it_ first. */
4070 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4071 Fselect_window (new_selected_window, Qt);
4072 else
4073 fset_selected_window (f, new_selected_window);
4075 unblock_input ();
4077 /* Now look whether `get-mru-window' gets us something. */
4078 mru_window = call1 (Qget_mru_window, frame);
4079 if (WINDOW_LIVE_P (mru_window)
4080 && EQ (XWINDOW (mru_window)->frame, frame))
4081 new_selected_window = mru_window;
4083 /* If all ended up well, we now promote the mru window. */
4084 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
4085 Fselect_window (new_selected_window, Qnil);
4086 else
4087 fset_selected_window (f, new_selected_window);
4089 else
4090 unblock_input ();
4092 /* Must be run by the caller:
4093 run_window_configuration_change_hook (f); */
4095 else
4096 /* We failed: Relink WINDOW into window tree. */
4098 if (before_sibling)
4100 wset_prev (s, window);
4101 wset_combination (p, horflag, window);
4103 else
4105 wset_next (s, window);
4106 if (!NILP (w->next))
4107 wset_prev (XWINDOW (w->next), window);
4109 error ("Deletion failed");
4112 return Qnil;
4115 /***********************************************************************
4116 Resizing Mini-Windows
4117 ***********************************************************************/
4119 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4120 can. */
4121 void
4122 grow_mini_window (struct window *w, int delta)
4124 struct frame *f = XFRAME (w->frame);
4125 struct window *r;
4126 Lisp_Object root, value;
4128 eassert (MINI_WINDOW_P (w));
4129 eassert (delta >= 0);
4131 root = FRAME_ROOT_WINDOW (f);
4132 r = XWINDOW (root);
4133 value = call2 (Qwindow_resize_root_window_vertically,
4134 root, make_number (- delta));
4135 if (INTEGERP (value) && window_resize_check (r, 0))
4137 block_input ();
4138 window_resize_apply (r, 0);
4140 /* Grow the mini-window. */
4141 w->top_line = r->top_line + r->total_lines;
4142 w->total_lines -= XINT (value);
4143 /* Enforce full redisplay. FIXME: make it more selective. */
4144 windows_or_buffers_changed++;
4145 adjust_frame_glyphs (f);
4146 unblock_input ();
4151 /* Shrink mini-window W. */
4152 void
4153 shrink_mini_window (struct window *w)
4155 struct frame *f = XFRAME (w->frame);
4156 struct window *r;
4157 Lisp_Object root, value;
4158 EMACS_INT size;
4160 eassert (MINI_WINDOW_P (w));
4162 size = w->total_lines;
4163 if (size > 1)
4165 root = FRAME_ROOT_WINDOW (f);
4166 r = XWINDOW (root);
4167 value = call2 (Qwindow_resize_root_window_vertically,
4168 root, make_number (size - 1));
4169 if (INTEGERP (value) && window_resize_check (r, 0))
4171 block_input ();
4172 window_resize_apply (r, 0);
4174 /* Shrink the mini-window. */
4175 w->top_line = r->top_line + r->total_lines;
4176 w->total_lines = 1;
4177 /* Enforce full redisplay. FIXME: make it more selective. */
4178 windows_or_buffers_changed++;
4179 adjust_frame_glyphs (f);
4180 unblock_input ();
4182 /* If the above failed for whatever strange reason we must make a
4183 one window frame here. The same routine will be needed when
4184 shrinking the frame (and probably when making the initial
4185 *scratch* window). For the moment leave things as they are. */
4189 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4190 doc: /* Resize minibuffer window WINDOW. */)
4191 (Lisp_Object window)
4193 struct window *w = XWINDOW (window);
4194 struct window *r;
4195 struct frame *f;
4196 int height;
4198 CHECK_WINDOW (window);
4199 f = XFRAME (w->frame);
4201 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4202 error ("Not a valid minibuffer window");
4203 else if (FRAME_MINIBUF_ONLY_P (f))
4204 error ("Cannot resize a minibuffer-only frame");
4206 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4207 height = r->total_lines + w->total_lines;
4208 if (window_resize_check (r, 0)
4209 && XINT (w->new_total) > 0
4210 && height == XINT (r->new_total) + XINT (w->new_total))
4212 block_input ();
4213 window_resize_apply (r, 0);
4215 w->total_lines = XFASTINT (w->new_total);
4216 w->top_line = r->top_line + r->total_lines;
4218 windows_or_buffers_changed++;
4219 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4220 adjust_frame_glyphs (f);
4221 unblock_input ();
4223 run_window_configuration_change_hook (f);
4224 return Qt;
4226 else error ("Failed to resize minibuffer window");
4229 /* Mark window cursors off for all windows in the window tree rooted
4230 at W by setting their phys_cursor_on_p flag to zero. Called from
4231 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4232 the frame are cleared. */
4234 void
4235 mark_window_cursors_off (struct window *w)
4237 while (w)
4239 if (WINDOWP (w->contents))
4240 mark_window_cursors_off (XWINDOW (w->contents));
4241 else
4242 w->phys_cursor_on_p = 0;
4244 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4249 /* Return number of lines of text (not counting mode lines) in W. */
4252 window_internal_height (struct window *w)
4254 int ht = w->total_lines;
4256 if (!MINI_WINDOW_P (w))
4258 if (!NILP (w->parent)
4259 || WINDOWP (w->contents)
4260 || !NILP (w->next)
4261 || !NILP (w->prev)
4262 || WINDOW_WANTS_MODELINE_P (w))
4263 --ht;
4265 if (WINDOW_WANTS_HEADER_LINE_P (w))
4266 --ht;
4269 return ht;
4272 /************************************************************************
4273 Window Scrolling
4274 ***********************************************************************/
4276 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4277 N screen-fulls, which is defined as the height of the window minus
4278 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4279 instead. Negative values of N mean scroll down. NOERROR non-zero
4280 means don't signal an error if we try to move over BEGV or ZV,
4281 respectively. */
4283 static void
4284 window_scroll (Lisp_Object window, EMACS_INT n, bool whole, int noerror)
4286 immediate_quit = 1;
4287 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4289 /* If we must, use the pixel-based version which is much slower than
4290 the line-based one but can handle varying line heights. */
4291 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4292 window_scroll_pixel_based (window, n, whole, noerror);
4293 else
4294 window_scroll_line_based (window, n, whole, noerror);
4296 immediate_quit = 0;
4300 /* Implementation of window_scroll that works based on pixel line
4301 heights. See the comment of window_scroll for parameter
4302 descriptions. */
4304 static void
4305 window_scroll_pixel_based (Lisp_Object window, int n, bool whole, int noerror)
4307 struct it it;
4308 struct window *w = XWINDOW (window);
4309 struct text_pos start;
4310 int this_scroll_margin;
4311 /* True if we fiddled the window vscroll field without really scrolling. */
4312 int vscrolled = 0;
4313 int x, y, rtop, rbot, rowh, vpos;
4314 void *itdata = NULL;
4315 int window_total_lines;
4316 int frame_line_height = default_line_pixel_height (w);
4318 SET_TEXT_POS_FROM_MARKER (start, w->start);
4319 /* Scrolling a minibuffer window via scroll bar when the echo area
4320 shows long text sometimes resets the minibuffer contents behind
4321 our backs. */
4322 if (CHARPOS (start) > ZV)
4323 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4325 /* If PT is not visible in WINDOW, move back one half of
4326 the screen. Allow PT to be partially visible, otherwise
4327 something like (scroll-down 1) with PT in the line before
4328 the partially visible one would recenter. */
4330 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4332 itdata = bidi_shelve_cache ();
4333 /* Move backward half the height of the window. Performance note:
4334 vmotion used here is about 10% faster, but would give wrong
4335 results for variable height lines. */
4336 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4337 it.current_y = it.last_visible_y;
4338 move_it_vertically_backward (&it, window_box_height (w) / 2);
4340 /* The function move_iterator_vertically may move over more than
4341 the specified y-distance. If it->w is small, e.g. a
4342 mini-buffer window, we may end up in front of the window's
4343 display area. This is the case when Start displaying at the
4344 start of the line containing PT in this case. */
4345 if (it.current_y <= 0)
4347 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4348 move_it_vertically_backward (&it, 0);
4349 it.current_y = 0;
4352 start = it.current.pos;
4353 bidi_unshelve_cache (itdata, 0);
4355 else if (auto_window_vscroll_p)
4357 if (rtop || rbot) /* partially visible */
4359 int px;
4360 int dy = frame_line_height;
4361 if (whole)
4362 dy = max ((window_box_height (w)
4363 - next_screen_context_lines * dy),
4364 dy);
4365 dy *= n;
4367 if (n < 0)
4369 /* Only vscroll backwards if already vscrolled forwards. */
4370 if (w->vscroll < 0 && rtop > 0)
4372 px = max (0, -w->vscroll - min (rtop, -dy));
4373 Fset_window_vscroll (window, make_number (px), Qt);
4374 return;
4377 if (n > 0)
4379 /* Do vscroll if already vscrolled or only display line. */
4380 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4382 px = max (0, -w->vscroll + min (rbot, dy));
4383 Fset_window_vscroll (window, make_number (px), Qt);
4384 return;
4387 /* Maybe modify window start instead of scrolling. */
4388 if (rbot > 0 || w->vscroll < 0)
4390 ptrdiff_t spos;
4392 Fset_window_vscroll (window, make_number (0), Qt);
4393 /* If there are other text lines above the current row,
4394 move window start to current row. Else to next row. */
4395 if (rbot > 0)
4396 spos = XINT (Fline_beginning_position (Qnil));
4397 else
4398 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4399 set_marker_restricted (w->start, make_number (spos),
4400 w->contents);
4401 w->start_at_line_beg = 1;
4402 w->update_mode_line = 1;
4403 /* Set force_start so that redisplay_window will run the
4404 window-scroll-functions. */
4405 w->force_start = 1;
4406 return;
4410 /* Cancel previous vscroll. */
4411 Fset_window_vscroll (window, make_number (0), Qt);
4414 itdata = bidi_shelve_cache ();
4415 /* If scroll_preserve_screen_position is non-nil, we try to set
4416 point in the same window line as it is now, so get that line. */
4417 if (!NILP (Vscroll_preserve_screen_position))
4419 /* We preserve the goal pixel coordinate across consecutive
4420 calls to scroll-up, scroll-down and other commands that
4421 have the `scroll-command' property. This avoids the
4422 possibility of point becoming "stuck" on a tall line when
4423 scrolling by one line. */
4424 if (window_scroll_pixel_based_preserve_y < 0
4425 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4426 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4428 start_display (&it, w, start);
4429 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4430 window_scroll_pixel_based_preserve_y = it.current_y;
4431 window_scroll_pixel_based_preserve_x = it.current_x;
4434 else
4435 window_scroll_pixel_based_preserve_y
4436 = window_scroll_pixel_based_preserve_x = -1;
4438 /* Move iterator it from start the specified distance forward or
4439 backward. The result is the new window start. */
4440 start_display (&it, w, start);
4441 if (whole)
4443 ptrdiff_t start_pos = IT_CHARPOS (it);
4444 int dy = frame_line_height;
4445 dy = max ((window_box_height (w)
4446 - next_screen_context_lines * dy),
4447 dy) * n;
4449 /* Note that move_it_vertically always moves the iterator to the
4450 start of a line. So, if the last line doesn't have a newline,
4451 we would end up at the start of the line ending at ZV. */
4452 if (dy <= 0)
4454 move_it_vertically_backward (&it, -dy);
4455 /* Ensure we actually do move, e.g. in case we are currently
4456 looking at an image that is taller that the window height. */
4457 while (start_pos == IT_CHARPOS (it)
4458 && start_pos > BEGV)
4459 move_it_by_lines (&it, -1);
4461 else if (dy > 0)
4463 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4464 MOVE_TO_POS | MOVE_TO_Y);
4465 /* Ensure we actually do move, e.g. in case we are currently
4466 looking at an image that is taller that the window height. */
4467 while (start_pos == IT_CHARPOS (it)
4468 && start_pos < ZV)
4469 move_it_by_lines (&it, 1);
4472 else
4473 move_it_by_lines (&it, n);
4475 /* We failed if we find ZV is already on the screen (scrolling up,
4476 means there's nothing past the end), or if we can't start any
4477 earlier (scrolling down, means there's nothing past the top). */
4478 if ((n > 0 && IT_CHARPOS (it) == ZV)
4479 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4481 if (IT_CHARPOS (it) == ZV)
4483 if (it.current_y < it.last_visible_y
4484 && (it.current_y + it.max_ascent + it.max_descent
4485 > it.last_visible_y))
4487 /* The last line was only partially visible, make it fully
4488 visible. */
4489 w->vscroll = (it.last_visible_y
4490 - it.current_y + it.max_ascent + it.max_descent);
4491 adjust_frame_glyphs (it.f);
4493 else
4495 bidi_unshelve_cache (itdata, 0);
4496 if (noerror)
4497 return;
4498 else if (n < 0) /* could happen with empty buffers */
4499 xsignal0 (Qbeginning_of_buffer);
4500 else
4501 xsignal0 (Qend_of_buffer);
4504 else
4506 if (w->vscroll != 0)
4507 /* The first line was only partially visible, make it fully
4508 visible. */
4509 w->vscroll = 0;
4510 else
4512 bidi_unshelve_cache (itdata, 0);
4513 if (noerror)
4514 return;
4515 else
4516 xsignal0 (Qbeginning_of_buffer);
4520 /* If control gets here, then we vscrolled. */
4522 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
4524 /* Don't try to change the window start below. */
4525 vscrolled = 1;
4528 if (! vscrolled)
4530 ptrdiff_t pos = IT_CHARPOS (it);
4531 ptrdiff_t bytepos;
4533 /* If in the middle of a multi-glyph character move forward to
4534 the next character. */
4535 if (in_display_vector_p (&it))
4537 ++pos;
4538 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4541 /* Set the window start, and set up the window for redisplay. */
4542 set_marker_restricted_both (w->start, w->contents, IT_CHARPOS (it),
4543 IT_BYTEPOS (it));
4544 bytepos = marker_byte_position (w->start);
4545 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4546 w->update_mode_line = 1;
4547 /* Set force_start so that redisplay_window will run the
4548 window-scroll-functions. */
4549 w->force_start = 1;
4552 /* The rest of this function uses current_y in a nonstandard way,
4553 not including the height of the header line if any. */
4554 it.current_y = it.vpos = 0;
4556 /* Move PT out of scroll margins.
4557 This code wants current_y to be zero at the window start position
4558 even if there is a header line. */
4559 window_total_lines
4560 = w->total_lines * WINDOW_FRAME_LINE_HEIGHT (w) / frame_line_height;
4561 this_scroll_margin = max (0, scroll_margin);
4562 this_scroll_margin
4563 = min (this_scroll_margin, window_total_lines / 4);
4564 this_scroll_margin *= frame_line_height;
4566 if (n > 0)
4568 /* We moved the window start towards ZV, so PT may be now
4569 in the scroll margin at the top. */
4570 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4571 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4572 && (NILP (Vscroll_preserve_screen_position)
4573 || EQ (Vscroll_preserve_screen_position, Qt)))
4574 /* We found PT at a legitimate height. Leave it alone. */
4576 else if (window_scroll_pixel_based_preserve_y >= 0)
4578 /* If we have a header line, take account of it.
4579 This is necessary because we set it.current_y to 0, above. */
4580 move_it_to (&it, -1,
4581 window_scroll_pixel_based_preserve_x,
4582 window_scroll_pixel_based_preserve_y
4583 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4584 -1, MOVE_TO_Y | MOVE_TO_X);
4585 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4587 else
4589 while (it.current_y < this_scroll_margin)
4591 int prev = it.current_y;
4592 move_it_by_lines (&it, 1);
4593 if (prev == it.current_y)
4594 break;
4596 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4599 else if (n < 0)
4601 ptrdiff_t charpos, bytepos;
4602 int partial_p;
4604 /* Save our position, for the
4605 window_scroll_pixel_based_preserve_y case. */
4606 charpos = IT_CHARPOS (it);
4607 bytepos = IT_BYTEPOS (it);
4609 /* We moved the window start towards BEGV, so PT may be now
4610 in the scroll margin at the bottom. */
4611 move_it_to (&it, PT, -1,
4612 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4613 - this_scroll_margin - 1),
4615 MOVE_TO_POS | MOVE_TO_Y);
4617 /* Save our position, in case it's correct. */
4618 charpos = IT_CHARPOS (it);
4619 bytepos = IT_BYTEPOS (it);
4621 /* See if point is on a partially visible line at the end. */
4622 if (it.what == IT_EOB)
4623 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4624 else
4626 move_it_by_lines (&it, 1);
4627 partial_p = it.current_y > it.last_visible_y;
4630 if (charpos == PT && !partial_p
4631 && (NILP (Vscroll_preserve_screen_position)
4632 || EQ (Vscroll_preserve_screen_position, Qt)))
4633 /* We found PT before we found the display margin, so PT is ok. */
4635 else if (window_scroll_pixel_based_preserve_y >= 0)
4637 SET_TEXT_POS_FROM_MARKER (start, w->start);
4638 start_display (&it, w, start);
4639 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4640 here because we called start_display again and did not
4641 alter it.current_y this time. */
4642 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4643 window_scroll_pixel_based_preserve_y, -1,
4644 MOVE_TO_Y | MOVE_TO_X);
4645 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4647 else
4649 if (partial_p)
4650 /* The last line was only partially visible, so back up two
4651 lines to make sure we're on a fully visible line. */
4653 move_it_by_lines (&it, -2);
4654 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4656 else
4657 /* No, the position we saved is OK, so use it. */
4658 SET_PT_BOTH (charpos, bytepos);
4661 bidi_unshelve_cache (itdata, 0);
4665 /* Implementation of window_scroll that works based on screen lines.
4666 See the comment of window_scroll for parameter descriptions. */
4668 static void
4669 window_scroll_line_based (Lisp_Object window, int n, bool whole, int noerror)
4671 register struct window *w = XWINDOW (window);
4672 /* Fvertical_motion enters redisplay, which can trigger
4673 fontification, which in turn can modify buffer text (e.g., if the
4674 fontification functions replace escape sequences with faces, as
4675 in `grep-mode-font-lock-keywords'). So we use a marker to record
4676 the old point position, to prevent crashes in SET_PT_BOTH. */
4677 Lisp_Object opoint_marker = Fpoint_marker ();
4678 register ptrdiff_t pos, pos_byte;
4679 register int ht = window_internal_height (w);
4680 register Lisp_Object tem;
4681 bool lose;
4682 Lisp_Object bolp;
4683 ptrdiff_t startpos = marker_position (w->start);
4684 ptrdiff_t startbyte = marker_byte_position (w->start);
4685 Lisp_Object original_pos = Qnil;
4687 /* If scrolling screen-fulls, compute the number of lines to
4688 scroll from the window's height. */
4689 if (whole)
4690 n *= max (1, ht - next_screen_context_lines);
4692 if (!NILP (Vscroll_preserve_screen_position))
4694 if (window_scroll_preserve_vpos <= 0
4695 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4696 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4698 struct position posit
4699 = *compute_motion (startpos, startbyte, 0, 0, 0,
4700 PT, ht, 0, -1, w->hscroll, 0, w);
4701 window_scroll_preserve_vpos = posit.vpos;
4702 window_scroll_preserve_hpos = posit.hpos + w->hscroll;
4705 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4706 make_number (window_scroll_preserve_vpos));
4709 XSETFASTINT (tem, PT);
4710 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4712 if (NILP (tem))
4714 Fvertical_motion (make_number (- (ht / 2)), window);
4715 startpos = PT;
4716 startbyte = PT_BYTE;
4719 SET_PT_BOTH (startpos, startbyte);
4720 lose = n < 0 && PT == BEGV;
4721 Fvertical_motion (make_number (n), window);
4722 pos = PT;
4723 pos_byte = PT_BYTE;
4724 bolp = Fbolp ();
4725 SET_PT_BOTH (marker_position (opoint_marker),
4726 marker_byte_position (opoint_marker));
4728 if (lose)
4730 if (noerror)
4731 return;
4732 else
4733 xsignal0 (Qbeginning_of_buffer);
4736 if (pos < ZV)
4738 /* Don't use a scroll margin that is negative or too large. */
4739 int this_scroll_margin =
4740 max (0, min (scroll_margin, w->total_lines / 4));
4742 set_marker_restricted_both (w->start, w->contents, pos, pos_byte);
4743 w->start_at_line_beg = !NILP (bolp);
4744 w->update_mode_line = 1;
4745 /* Set force_start so that redisplay_window will run
4746 the window-scroll-functions. */
4747 w->force_start = 1;
4749 if (!NILP (Vscroll_preserve_screen_position)
4750 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4752 SET_PT_BOTH (pos, pos_byte);
4753 Fvertical_motion (original_pos, window);
4755 /* If we scrolled forward, put point enough lines down
4756 that it is outside the scroll margin. */
4757 else if (n > 0)
4759 int top_margin;
4761 if (this_scroll_margin > 0)
4763 SET_PT_BOTH (pos, pos_byte);
4764 Fvertical_motion (make_number (this_scroll_margin), window);
4765 top_margin = PT;
4767 else
4768 top_margin = pos;
4770 if (top_margin <= marker_position (opoint_marker))
4771 SET_PT_BOTH (marker_position (opoint_marker),
4772 marker_byte_position (opoint_marker));
4773 else if (!NILP (Vscroll_preserve_screen_position))
4775 SET_PT_BOTH (pos, pos_byte);
4776 Fvertical_motion (original_pos, window);
4778 else
4779 SET_PT (top_margin);
4781 else if (n < 0)
4783 int bottom_margin;
4785 /* If we scrolled backward, put point near the end of the window
4786 but not within the scroll margin. */
4787 SET_PT_BOTH (pos, pos_byte);
4788 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4789 if (XFASTINT (tem) == ht - this_scroll_margin)
4790 bottom_margin = PT;
4791 else
4792 bottom_margin = PT + 1;
4794 if (bottom_margin > marker_position (opoint_marker))
4795 SET_PT_BOTH (marker_position (opoint_marker),
4796 marker_byte_position (opoint_marker));
4797 else
4799 if (!NILP (Vscroll_preserve_screen_position))
4801 SET_PT_BOTH (pos, pos_byte);
4802 Fvertical_motion (original_pos, window);
4804 else
4805 Fvertical_motion (make_number (-1), window);
4809 else
4811 if (noerror)
4812 return;
4813 else
4814 xsignal0 (Qend_of_buffer);
4819 /* Scroll selected_window up or down. If N is nil, scroll a
4820 screen-full which is defined as the height of the window minus
4821 next_screen_context_lines. If N is the symbol `-', scroll.
4822 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4823 up. This is the guts of Fscroll_up and Fscroll_down. */
4825 static void
4826 scroll_command (Lisp_Object n, int direction)
4828 ptrdiff_t count = SPECPDL_INDEX ();
4830 eassert (eabs (direction) == 1);
4832 /* If selected window's buffer isn't current, make it current for
4833 the moment. But don't screw up if window_scroll gets an error. */
4834 if (XBUFFER (XWINDOW (selected_window)->contents) != current_buffer)
4836 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4837 Fset_buffer (XWINDOW (selected_window)->contents);
4839 /* Make redisplay consider other windows than just selected_window. */
4840 ++windows_or_buffers_changed;
4843 if (NILP (n))
4844 window_scroll (selected_window, direction, 1, 0);
4845 else if (EQ (n, Qminus))
4846 window_scroll (selected_window, -direction, 1, 0);
4847 else
4849 n = Fprefix_numeric_value (n);
4850 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4853 unbind_to (count, Qnil);
4856 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4857 doc: /* Scroll text of selected window upward ARG lines.
4858 If ARG is omitted or nil, scroll upward by a near full screen.
4859 A near full screen is `next-screen-context-lines' less than a full screen.
4860 Negative ARG means scroll downward.
4861 If ARG is the atom `-', scroll downward by nearly full screen.
4862 When calling from a program, supply as argument a number, nil, or `-'. */)
4863 (Lisp_Object arg)
4865 scroll_command (arg, 1);
4866 return Qnil;
4869 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4870 doc: /* Scroll text of selected window down ARG lines.
4871 If ARG is omitted or nil, scroll down by a near full screen.
4872 A near full screen is `next-screen-context-lines' less than a full screen.
4873 Negative ARG means scroll upward.
4874 If ARG is the atom `-', scroll upward by nearly full screen.
4875 When calling from a program, supply as argument a number, nil, or `-'. */)
4876 (Lisp_Object arg)
4878 scroll_command (arg, -1);
4879 return Qnil;
4882 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4883 doc: /* Return the other window for \"other window scroll\" commands.
4884 If `other-window-scroll-buffer' is non-nil, a window
4885 showing that buffer is used.
4886 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4887 specifies the window. This takes precedence over
4888 `other-window-scroll-buffer'. */)
4889 (void)
4891 Lisp_Object window;
4893 if (MINI_WINDOW_P (XWINDOW (selected_window))
4894 && !NILP (Vminibuf_scroll_window))
4895 window = Vminibuf_scroll_window;
4896 /* If buffer is specified, scroll that buffer. */
4897 else if (!NILP (Vother_window_scroll_buffer))
4899 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4900 if (NILP (window))
4901 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4903 else
4905 /* Nothing specified; look for a neighboring window on the same
4906 frame. */
4907 window = Fnext_window (selected_window, Qnil, Qnil);
4909 if (EQ (window, selected_window))
4910 /* That didn't get us anywhere; look for a window on another
4911 visible frame. */
4913 window = Fnext_window (window, Qnil, Qt);
4914 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4915 && ! EQ (window, selected_window));
4918 CHECK_LIVE_WINDOW (window);
4920 if (EQ (window, selected_window))
4921 error ("There is no other window");
4923 return window;
4926 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4927 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4928 A near full screen is `next-screen-context-lines' less than a full screen.
4929 The next window is the one below the current one; or the one at the top
4930 if the current one is at the bottom. Negative ARG means scroll downward.
4931 If ARG is the atom `-', scroll downward by nearly full screen.
4932 When calling from a program, supply as argument a number, nil, or `-'.
4934 If `other-window-scroll-buffer' is non-nil, scroll the window
4935 showing that buffer, popping the buffer up if necessary.
4936 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4937 specifies the window to scroll. This takes precedence over
4938 `other-window-scroll-buffer'. */)
4939 (Lisp_Object arg)
4941 Lisp_Object window;
4942 struct window *w;
4943 ptrdiff_t count = SPECPDL_INDEX ();
4945 window = Fother_window_for_scrolling ();
4946 w = XWINDOW (window);
4948 /* Don't screw up if window_scroll gets an error. */
4949 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4950 ++windows_or_buffers_changed;
4952 Fset_buffer (w->contents);
4953 SET_PT_BOTH (marker_position (w->pointm), marker_byte_position (w->pointm));
4955 if (NILP (arg))
4956 window_scroll (window, 1, 1, 1);
4957 else if (EQ (arg, Qminus))
4958 window_scroll (window, -1, 1, 1);
4959 else
4961 if (CONSP (arg))
4962 arg = XCAR (arg);
4963 CHECK_NUMBER (arg);
4964 window_scroll (window, XINT (arg), 0, 1);
4967 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4968 unbind_to (count, Qnil);
4970 return Qnil;
4973 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
4974 doc: /* Scroll selected window display ARG columns left.
4975 Default for ARG is window width minus 2.
4976 Value is the total amount of leftward horizontal scrolling in
4977 effect after the change.
4978 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4979 lower bound for automatic scrolling, i.e. automatic scrolling
4980 will not scroll a window to a column less than the value returned
4981 by this function. This happens in an interactive call. */)
4982 (register Lisp_Object arg, Lisp_Object set_minimum)
4984 struct window *w = XWINDOW (selected_window);
4985 EMACS_INT requested_arg = (NILP (arg)
4986 ? window_body_cols (w) - 2
4987 : XINT (Fprefix_numeric_value (arg)));
4988 Lisp_Object result = set_window_hscroll (w, w->hscroll + requested_arg);
4990 if (!NILP (set_minimum))
4991 w->min_hscroll = w->hscroll;
4993 return result;
4996 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
4997 doc: /* Scroll selected window display ARG columns right.
4998 Default for ARG is window width minus 2.
4999 Value is the total amount of leftward horizontal scrolling in
5000 effect after the change.
5001 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5002 lower bound for automatic scrolling, i.e. automatic scrolling
5003 will not scroll a window to a column less than the value returned
5004 by this function. This happens in an interactive call. */)
5005 (register Lisp_Object arg, Lisp_Object set_minimum)
5007 struct window *w = XWINDOW (selected_window);
5008 EMACS_INT requested_arg = (NILP (arg)
5009 ? window_body_cols (w) - 2
5010 : XINT (Fprefix_numeric_value (arg)));
5011 Lisp_Object result = set_window_hscroll (w, w->hscroll - requested_arg);
5013 if (!NILP (set_minimum))
5014 w->min_hscroll = w->hscroll;
5016 return result;
5019 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5020 doc: /* Return the window which was selected when entering the minibuffer.
5021 Returns nil, if selected window is not a minibuffer window. */)
5022 (void)
5024 if (minibuf_level > 0
5025 && MINI_WINDOW_P (XWINDOW (selected_window))
5026 && WINDOW_LIVE_P (minibuf_selected_window))
5027 return minibuf_selected_window;
5029 return Qnil;
5032 /* Value is the number of lines actually displayed in window W,
5033 as opposed to its height. */
5035 static int
5036 displayed_window_lines (struct window *w)
5038 struct it it;
5039 struct text_pos start;
5040 int height = window_box_height (w);
5041 struct buffer *old_buffer;
5042 int bottom_y;
5043 void *itdata = NULL;
5045 if (XBUFFER (w->contents) != current_buffer)
5047 old_buffer = current_buffer;
5048 set_buffer_internal (XBUFFER (w->contents));
5050 else
5051 old_buffer = NULL;
5053 /* In case W->start is out of the accessible range, do something
5054 reasonable. This happens in Info mode when Info-scroll-down
5055 calls (recenter -1) while W->start is 1. */
5056 CLIP_TEXT_POS_FROM_MARKER (start, w->start);
5058 itdata = bidi_shelve_cache ();
5059 start_display (&it, w, start);
5060 move_it_vertically (&it, height);
5061 bottom_y = line_bottom_y (&it);
5062 bidi_unshelve_cache (itdata, 0);
5064 /* rms: On a non-window display,
5065 the value of it.vpos at the bottom of the screen
5066 seems to be 1 larger than window_box_height (w).
5067 This kludge fixes a bug whereby (move-to-window-line -1)
5068 when ZV is on the last screen line
5069 moves to the previous screen line instead of the last one. */
5070 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5071 height++;
5073 /* Add in empty lines at the bottom of the window. */
5074 if (bottom_y < height)
5076 int uy = FRAME_LINE_HEIGHT (it.f);
5077 it.vpos += (height - bottom_y + uy - 1) / uy;
5080 if (old_buffer)
5081 set_buffer_internal (old_buffer);
5083 return it.vpos;
5087 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5088 doc: /* Center point in selected window and maybe redisplay frame.
5089 With a numeric prefix argument ARG, recenter putting point on screen line ARG
5090 relative to the selected window. If ARG is negative, it counts up from the
5091 bottom of the window. (ARG should be less than the height of the window.)
5093 If ARG is omitted or nil, then recenter with point on the middle line of
5094 the selected window; if the variable `recenter-redisplay' is non-nil,
5095 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5096 is set to `grow-only', this resets the tool-bar's height to the minimum
5097 height needed); if `recenter-redisplay' has the special value `tty',
5098 then only tty frames are redrawn.
5100 Just C-u as prefix means put point in the center of the window
5101 and redisplay normally--don't erase and redraw the frame. */)
5102 (register Lisp_Object arg)
5104 struct window *w = XWINDOW (selected_window);
5105 struct buffer *buf = XBUFFER (w->contents);
5106 struct buffer *obuf = current_buffer;
5107 bool center_p = 0;
5108 ptrdiff_t charpos, bytepos;
5109 EMACS_INT iarg IF_LINT (= 0);
5110 int this_scroll_margin;
5112 /* If redisplay is suppressed due to an error, try again. */
5113 obuf->display_error_modiff = 0;
5115 if (NILP (arg))
5117 if (!NILP (Vrecenter_redisplay)
5118 && (!EQ (Vrecenter_redisplay, Qtty)
5119 || !NILP (Ftty_type (selected_frame))))
5121 ptrdiff_t i;
5123 /* Invalidate pixel data calculated for all compositions. */
5124 for (i = 0; i < n_compositions; i++)
5125 composition_table[i]->font = NULL;
5126 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
5127 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5128 #endif
5129 Fredraw_frame (WINDOW_FRAME (w));
5130 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5133 center_p = 1;
5135 else if (CONSP (arg)) /* Just C-u. */
5136 center_p = 1;
5137 else
5139 arg = Fprefix_numeric_value (arg);
5140 CHECK_NUMBER (arg);
5141 iarg = XINT (arg);
5144 set_buffer_internal (buf);
5146 /* Do this after making BUF current
5147 in case scroll_margin is buffer-local. */
5148 this_scroll_margin =
5149 max (0, min (scroll_margin, w->total_lines / 4));
5151 /* Handle centering on a graphical frame specially. Such frames can
5152 have variable-height lines and centering point on the basis of
5153 line counts would lead to strange effects. */
5154 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5156 if (center_p)
5158 struct it it;
5159 struct text_pos pt;
5160 void *itdata = bidi_shelve_cache ();
5162 SET_TEXT_POS (pt, PT, PT_BYTE);
5163 start_display (&it, w, pt);
5164 move_it_vertically_backward (&it, window_box_height (w) / 2);
5165 charpos = IT_CHARPOS (it);
5166 bytepos = IT_BYTEPOS (it);
5167 bidi_unshelve_cache (itdata, 0);
5169 else if (iarg < 0)
5171 struct it it;
5172 struct text_pos pt;
5173 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5174 int extra_line_spacing;
5175 int h = window_box_height (w);
5176 void *itdata = bidi_shelve_cache ();
5178 iarg = - max (-iarg, this_scroll_margin);
5180 SET_TEXT_POS (pt, PT, PT_BYTE);
5181 start_display (&it, w, pt);
5183 /* Be sure we have the exact height of the full line containing PT. */
5184 move_it_by_lines (&it, 0);
5186 /* The amount of pixels we have to move back is the window
5187 height minus what's displayed in the line containing PT,
5188 and the lines below. */
5189 it.current_y = 0;
5190 it.vpos = 0;
5191 move_it_by_lines (&it, nlines);
5193 if (it.vpos == nlines)
5194 h -= it.current_y;
5195 else
5197 /* Last line has no newline */
5198 h -= line_bottom_y (&it);
5199 it.vpos++;
5202 /* Don't reserve space for extra line spacing of last line. */
5203 extra_line_spacing = it.max_extra_line_spacing;
5205 /* If we can't move down NLINES lines because we hit
5206 the end of the buffer, count in some empty lines. */
5207 if (it.vpos < nlines)
5209 nlines -= it.vpos;
5210 extra_line_spacing = it.extra_line_spacing;
5211 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5213 if (h <= 0)
5215 bidi_unshelve_cache (itdata, 0);
5216 return Qnil;
5219 /* Now find the new top line (starting position) of the window. */
5220 start_display (&it, w, pt);
5221 it.current_y = 0;
5222 move_it_vertically_backward (&it, h);
5224 /* If extra line spacing is present, we may move too far
5225 back. This causes the last line to be only partially
5226 visible (which triggers redisplay to recenter that line
5227 in the middle), so move forward.
5228 But ignore extra line spacing on last line, as it is not
5229 considered to be part of the visible height of the line.
5231 h += extra_line_spacing;
5232 while (-it.current_y > h)
5233 move_it_by_lines (&it, 1);
5235 charpos = IT_CHARPOS (it);
5236 bytepos = IT_BYTEPOS (it);
5238 bidi_unshelve_cache (itdata, 0);
5240 else
5242 struct position pos;
5244 iarg = max (iarg, this_scroll_margin);
5246 pos = *vmotion (PT, PT_BYTE, -iarg, w);
5247 charpos = pos.bufpos;
5248 bytepos = pos.bytepos;
5251 else
5253 struct position pos;
5254 int ht = window_internal_height (w);
5256 if (center_p)
5257 iarg = ht / 2;
5258 else if (iarg < 0)
5259 iarg += ht;
5261 /* Don't let it get into the margin at either top or bottom. */
5262 iarg = clip_to_bounds (this_scroll_margin, iarg,
5263 ht - this_scroll_margin - 1);
5265 pos = *vmotion (PT, PT_BYTE, - iarg, w);
5266 charpos = pos.bufpos;
5267 bytepos = pos.bytepos;
5270 /* Set the new window start. */
5271 set_marker_both (w->start, w->contents, charpos, bytepos);
5272 w->window_end_valid = 0;
5274 w->optional_new_start = 1;
5276 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5277 FETCH_BYTE (bytepos - 1) == '\n');
5279 set_buffer_internal (obuf);
5280 return Qnil;
5283 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5284 0, 1, 0,
5285 doc: /* Return the height in lines of the text display area of WINDOW.
5286 WINDOW must be a live window and defaults to the selected one.
5288 The returned height does not include the mode line, any header line,
5289 nor any partial-height lines at the bottom of the text area. */)
5290 (Lisp_Object window)
5292 struct window *w = decode_live_window (window);
5293 int pixel_height = window_box_height (w);
5294 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5295 return make_number (line_height);
5300 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5301 1, 1, "P",
5302 doc: /* Position point relative to window.
5303 ARG nil means position point at center of window.
5304 Else, ARG specifies vertical position within the window;
5305 zero means top of window, negative means relative to bottom of window. */)
5306 (Lisp_Object arg)
5308 struct window *w = XWINDOW (selected_window);
5309 int lines, start;
5310 Lisp_Object window;
5311 #if 0
5312 int this_scroll_margin;
5313 #endif
5315 if (!(BUFFERP (w->contents) && XBUFFER (w->contents) == current_buffer))
5316 /* This test is needed to make sure PT/PT_BYTE make sense in w->contents
5317 when passed below to set_marker_both. */
5318 error ("move-to-window-line called from unrelated buffer");
5320 window = selected_window;
5321 start = marker_position (w->start);
5322 if (start < BEGV || start > ZV)
5324 int height = window_internal_height (w);
5325 Fvertical_motion (make_number (- (height / 2)), window);
5326 set_marker_both (w->start, w->contents, PT, PT_BYTE);
5327 w->start_at_line_beg = !NILP (Fbolp ());
5328 w->force_start = 1;
5330 else
5331 Fgoto_char (w->start);
5333 lines = displayed_window_lines (w);
5335 #if 0
5336 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5337 #endif
5339 if (NILP (arg))
5340 XSETFASTINT (arg, lines / 2);
5341 else
5343 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5345 if (iarg < 0)
5346 iarg = iarg + lines;
5348 #if 0 /* This code would prevent move-to-window-line from moving point
5349 to a place inside the scroll margins (which would cause the
5350 next redisplay to scroll). I wrote this code, but then concluded
5351 it is probably better not to install it. However, it is here
5352 inside #if 0 so as not to lose it. -- rms. */
5354 /* Don't let it get into the margin at either top or bottom. */
5355 iarg = max (iarg, this_scroll_margin);
5356 iarg = min (iarg, lines - this_scroll_margin - 1);
5357 #endif
5359 arg = make_number (iarg);
5362 /* Skip past a partially visible first line. */
5363 if (w->vscroll)
5364 XSETINT (arg, XINT (arg) + 1);
5366 return Fvertical_motion (arg, window);
5371 /***********************************************************************
5372 Window Configuration
5373 ***********************************************************************/
5375 struct save_window_data
5377 struct vectorlike_header header;
5378 Lisp_Object selected_frame;
5379 Lisp_Object current_window;
5380 Lisp_Object current_buffer;
5381 Lisp_Object minibuf_scroll_window;
5382 Lisp_Object minibuf_selected_window;
5383 Lisp_Object root_window;
5384 Lisp_Object focus_frame;
5385 /* A vector, each of whose elements is a struct saved_window
5386 for one window. */
5387 Lisp_Object saved_windows;
5389 /* All fields above are traced by the GC.
5390 From `frame-cols' down, the fields are ignored by the GC. */
5392 int frame_cols, frame_lines, frame_menu_bar_lines;
5393 int frame_tool_bar_lines;
5396 /* This is saved as a Lisp_Vector */
5397 struct saved_window
5399 struct vectorlike_header header;
5401 Lisp_Object window, buffer, start, pointm, mark;
5402 Lisp_Object left_col, top_line, total_cols, total_lines;
5403 Lisp_Object normal_cols, normal_lines;
5404 Lisp_Object hscroll, min_hscroll;
5405 Lisp_Object parent, prev;
5406 Lisp_Object start_at_line_beg;
5407 Lisp_Object display_table;
5408 Lisp_Object left_margin_cols, right_margin_cols;
5409 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5410 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5411 Lisp_Object combination_limit, window_parameters;
5414 #define SAVED_WINDOW_N(swv,n) \
5415 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5417 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5418 doc: /* Return t if OBJECT is a window-configuration object. */)
5419 (Lisp_Object object)
5421 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5424 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5425 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5426 (Lisp_Object config)
5428 register struct save_window_data *data;
5429 struct Lisp_Vector *saved_windows;
5431 CHECK_WINDOW_CONFIGURATION (config);
5433 data = (struct save_window_data *) XVECTOR (config);
5434 saved_windows = XVECTOR (data->saved_windows);
5435 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5438 DEFUN ("set-window-configuration", Fset_window_configuration,
5439 Sset_window_configuration, 1, 1, 0,
5440 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5441 CONFIGURATION must be a value previously returned
5442 by `current-window-configuration' (which see).
5443 If CONFIGURATION was made from a frame that is now deleted,
5444 only frame-independent values can be restored. In this case,
5445 the return value is nil. Otherwise the value is t. */)
5446 (Lisp_Object configuration)
5448 register struct save_window_data *data;
5449 struct Lisp_Vector *saved_windows;
5450 Lisp_Object new_current_buffer;
5451 Lisp_Object frame;
5452 struct frame *f;
5453 ptrdiff_t old_point = -1;
5455 CHECK_WINDOW_CONFIGURATION (configuration);
5457 data = (struct save_window_data *) XVECTOR (configuration);
5458 saved_windows = XVECTOR (data->saved_windows);
5460 new_current_buffer = data->current_buffer;
5461 if (!BUFFER_LIVE_P (XBUFFER (new_current_buffer)))
5462 new_current_buffer = Qnil;
5463 else
5465 if (XBUFFER (new_current_buffer) == current_buffer)
5466 /* The code further down "preserves point" by saving here PT in
5467 old_point and then setting it later back into PT. When the
5468 current-selected-window and the final-selected-window both show
5469 the current buffer, this suffers from the problem that the
5470 current PT is the window-point of the current-selected-window,
5471 while the final PT is the point of the final-selected-window, so
5472 this copy from one PT to the other would end up moving the
5473 window-point of the final-selected-window to the window-point of
5474 the current-selected-window. So we have to be careful which
5475 point of the current-buffer we copy into old_point. */
5476 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5477 && WINDOWP (selected_window)
5478 && EQ (XWINDOW (selected_window)->contents, new_current_buffer)
5479 && !EQ (selected_window, data->current_window))
5480 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5481 else
5482 old_point = PT;
5483 else
5484 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5485 point in new_current_buffer as of the last time this buffer was
5486 used. This can be non-deterministic since it can be changed by
5487 things like jit-lock by mere temporary selection of some random
5488 window that happens to show this buffer.
5489 So if possible we want this arbitrary choice of "which point" to
5490 be the one from the to-be-selected-window so as to prevent this
5491 window's cursor from being copied from another window. */
5492 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer)
5493 /* If current_window = selected_window, its point is in BUF_PT. */
5494 && !EQ (selected_window, data->current_window))
5495 old_point = marker_position (XWINDOW (data->current_window)->pointm);
5496 else
5497 old_point = BUF_PT (XBUFFER (new_current_buffer));
5500 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5501 f = XFRAME (frame);
5503 /* If f is a dead frame, don't bother rebuilding its window tree.
5504 However, there is other stuff we should still try to do below. */
5505 if (FRAME_LIVE_P (f))
5507 Lisp_Object window;
5508 Lisp_Object dead_windows = Qnil;
5509 register Lisp_Object tem, par, pers;
5510 register struct window *w;
5511 register struct saved_window *p;
5512 struct window *root_window;
5513 struct window **leaf_windows;
5514 int n_leaf_windows;
5515 ptrdiff_t k;
5516 int i, n;
5518 /* If the frame has been resized since this window configuration was
5519 made, we change the frame to the size specified in the
5520 configuration, restore the configuration, and then resize it
5521 back. We keep track of the prevailing height in these variables. */
5522 int previous_frame_lines = FRAME_LINES (f);
5523 int previous_frame_cols = FRAME_COLS (f);
5524 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5525 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5527 /* Don't do this within the main loop below: This may call Lisp
5528 code and is thus potentially unsafe while input is blocked. */
5529 for (k = 0; k < saved_windows->header.size; k++)
5531 p = SAVED_WINDOW_N (saved_windows, k);
5532 window = p->window;
5533 w = XWINDOW (window);
5534 if (BUFFERP (w->contents)
5535 && !EQ (w->contents, p->buffer)
5536 && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5537 /* If a window we restore gets another buffer, record the
5538 window's old buffer. */
5539 call1 (Qrecord_window_buffer, window);
5542 /* The mouse highlighting code could get screwed up
5543 if it runs during this. */
5544 block_input ();
5546 if (data->frame_lines != previous_frame_lines
5547 || data->frame_cols != previous_frame_cols)
5548 change_frame_size (f, data->frame_lines,
5549 data->frame_cols, 0, 0, 0);
5550 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5551 if (data->frame_menu_bar_lines
5552 != previous_frame_menu_bar_lines)
5553 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5554 make_number (0));
5555 #ifdef HAVE_WINDOW_SYSTEM
5556 if (data->frame_tool_bar_lines
5557 != previous_frame_tool_bar_lines)
5558 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5559 make_number (0));
5560 #endif
5561 #endif
5563 /* "Swap out" point from the selected window's buffer
5564 into the window itself. (Normally the pointm of the selected
5565 window holds garbage.) We do this now, before
5566 restoring the window contents, and prevent it from
5567 being done later on when we select a new window. */
5568 if (! NILP (XWINDOW (selected_window)->contents))
5570 w = XWINDOW (selected_window);
5571 set_marker_both (w->pointm,
5572 w->contents,
5573 BUF_PT (XBUFFER (w->contents)),
5574 BUF_PT_BYTE (XBUFFER (w->contents)));
5577 windows_or_buffers_changed++;
5578 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5580 /* Problem: Freeing all matrices and later allocating them again
5581 is a serious redisplay flickering problem. What we would
5582 really like to do is to free only those matrices not reused
5583 below. */
5584 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5585 leaf_windows = alloca (count_windows (root_window)
5586 * sizeof *leaf_windows);
5587 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5589 /* Kludge Alert!
5590 Mark all windows now on frame as "deleted".
5591 Restoring the new configuration "undeletes" any that are in it.
5593 Save their current buffers in their height fields, since we may
5594 need it later, if a buffer saved in the configuration is now
5595 dead. */
5596 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5598 for (k = 0; k < saved_windows->header.size; k++)
5600 p = SAVED_WINDOW_N (saved_windows, k);
5601 window = p->window;
5602 w = XWINDOW (window);
5603 wset_next (w, Qnil);
5605 if (!NILP (p->parent))
5606 wset_parent
5607 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->parent))->window);
5608 else
5609 wset_parent (w, Qnil);
5611 if (!NILP (p->prev))
5613 wset_prev
5614 (w, SAVED_WINDOW_N (saved_windows, XFASTINT (p->prev))->window);
5615 wset_next (XWINDOW (w->prev), p->window);
5617 else
5619 wset_prev (w, Qnil);
5620 if (!NILP (w->parent))
5621 wset_combination (XWINDOW (w->parent),
5622 (XINT (p->total_cols)
5623 != XWINDOW (w->parent)->total_cols),
5624 p->window);
5627 /* If we squirreled away the buffer, restore it now. */
5628 if (BUFFERP (w->combination_limit))
5629 wset_buffer (w, w->combination_limit);
5630 w->left_col = XFASTINT (p->left_col);
5631 w->top_line = XFASTINT (p->top_line);
5632 w->total_cols = XFASTINT (p->total_cols);
5633 w->total_lines = XFASTINT (p->total_lines);
5634 wset_normal_cols (w, p->normal_cols);
5635 wset_normal_lines (w, p->normal_lines);
5636 w->hscroll = XFASTINT (p->hscroll);
5637 w->min_hscroll = XFASTINT (p->min_hscroll);
5638 wset_display_table (w, p->display_table);
5639 w->left_margin_cols = XINT (p->left_margin_cols);
5640 w->right_margin_cols = XINT (p->right_margin_cols);
5641 w->left_fringe_width = XINT (p->left_fringe_width);
5642 w->right_fringe_width = XINT (p->right_fringe_width);
5643 w->fringes_outside_margins = !NILP (p->fringes_outside_margins);
5644 w->scroll_bar_width = XINT (p->scroll_bar_width);
5645 wset_vertical_scroll_bar_type (w, p->vertical_scroll_bar_type);
5646 wset_dedicated (w, p->dedicated);
5647 wset_combination_limit (w, p->combination_limit);
5648 /* Restore any window parameters that have been saved.
5649 Parameters that have not been saved are left alone. */
5650 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5652 pers = XCAR (tem);
5653 if (CONSP (pers))
5655 if (NILP (XCDR (pers)))
5657 par = Fassq (XCAR (pers), w->window_parameters);
5658 if (CONSP (par) && !NILP (XCDR (par)))
5659 /* Reset a parameter to nil if and only if it
5660 has a non-nil association. Don't make new
5661 associations. */
5662 Fsetcdr (par, Qnil);
5664 else
5665 /* Always restore a non-nil value. */
5666 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5670 if (BUFFERP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
5671 /* If saved buffer is alive, install it. */
5673 wset_buffer (w, p->buffer);
5674 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5675 set_marker_restricted (w->start, p->start, w->contents);
5676 set_marker_restricted (w->pointm, p->pointm,
5677 w->contents);
5678 Fset_marker (BVAR (XBUFFER (w->contents), mark),
5679 p->mark, w->contents);
5681 /* As documented in Fcurrent_window_configuration, don't
5682 restore the location of point in the buffer which was
5683 current when the window configuration was recorded. */
5684 if (!EQ (p->buffer, new_current_buffer)
5685 && XBUFFER (p->buffer) == current_buffer)
5686 Fgoto_char (w->pointm);
5688 else if (BUFFERP (w->contents) && BUFFER_LIVE_P (XBUFFER (w->contents)))
5689 /* Keep window's old buffer; make sure the markers are real. */
5691 /* Set window markers at start of visible range. */
5692 if (XMARKER (w->start)->buffer == 0)
5693 set_marker_restricted_both (w->start, w->contents, 0, 0);
5694 if (XMARKER (w->pointm)->buffer == 0)
5695 set_marker_restricted_both
5696 (w->pointm, w->contents,
5697 BUF_PT (XBUFFER (w->contents)),
5698 BUF_PT_BYTE (XBUFFER (w->contents)));
5699 w->start_at_line_beg = 1;
5701 else if (!NILP (w->start))
5702 /* Leaf window has no live buffer, get one. */
5704 /* Get the buffer via other_buffer_safely in order to
5705 avoid showing an unimportant buffer and, if necessary, to
5706 recreate *scratch* in the course (part of Juanma's bs-show
5707 scenario from March 2011). */
5708 wset_buffer (w, other_buffer_safely (Fcurrent_buffer ()));
5709 /* This will set the markers to beginning of visible
5710 range. */
5711 set_marker_restricted_both (w->start, w->contents, 0, 0);
5712 set_marker_restricted_both (w->pointm, w->contents, 0, 0);
5713 w->start_at_line_beg = 1;
5714 if (!NILP (w->dedicated))
5715 /* Record this window as dead. */
5716 dead_windows = Fcons (window, dead_windows);
5717 /* Make sure window is no more dedicated. */
5718 wset_dedicated (w, Qnil);
5722 fset_root_window (f, data->root_window);
5723 /* Arrange *not* to restore point in the buffer that was
5724 current when the window configuration was saved. */
5725 if (EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
5726 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5727 make_number (old_point),
5728 XWINDOW (data->current_window)->contents);
5730 /* In the following call to `select-window', prevent "swapping out
5731 point" in the old selected window using the buffer that has
5732 been restored into it. We already swapped out that point from
5733 that window's old buffer. */
5734 select_window (data->current_window, Qnil, 1);
5735 BVAR (XBUFFER (XWINDOW (selected_window)->contents), last_selected_window)
5736 = selected_window;
5738 if (NILP (data->focus_frame)
5739 || (FRAMEP (data->focus_frame)
5740 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5741 Fredirect_frame_focus (frame, data->focus_frame);
5743 /* Set the screen height to the value it had before this function. */
5744 if (previous_frame_lines != FRAME_LINES (f)
5745 || previous_frame_cols != FRAME_COLS (f))
5746 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5747 0, 0, 0);
5748 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5749 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5750 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5751 make_number (0));
5752 #ifdef HAVE_WINDOW_SYSTEM
5753 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5754 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5755 make_number (0));
5756 #endif
5757 #endif
5759 /* Now, free glyph matrices in windows that were not reused. */
5760 for (i = n = 0; i < n_leaf_windows; ++i)
5762 if (NILP (leaf_windows[i]->contents))
5763 free_window_matrices (leaf_windows[i]);
5764 else if (EQ (leaf_windows[i]->contents, new_current_buffer))
5765 ++n;
5768 adjust_frame_glyphs (f);
5769 unblock_input ();
5771 /* Scan dead buffer windows. */
5772 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5774 window = XCAR (dead_windows);
5775 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5776 delete_deletable_window (window);
5779 /* Fselect_window will have made f the selected frame, so we
5780 reselect the proper frame here. Fhandle_switch_frame will change the
5781 selected window too, but that doesn't make the call to
5782 Fselect_window above totally superfluous; it still sets f's
5783 selected window. */
5784 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5785 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5787 run_window_configuration_change_hook (f);
5790 if (!NILP (new_current_buffer))
5792 Fset_buffer (new_current_buffer);
5793 /* If the new current buffer doesn't appear in the selected
5794 window, go to its old point (see bug#12208). */
5795 if (!EQ (XWINDOW (data->current_window)->contents, new_current_buffer))
5796 Fgoto_char (make_number (old_point));
5799 Vminibuf_scroll_window = data->minibuf_scroll_window;
5800 minibuf_selected_window = data->minibuf_selected_window;
5802 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5805 void
5806 restore_window_configuration (Lisp_Object configuration)
5808 Fset_window_configuration (configuration);
5812 /* If WINDOW is an internal window, recursively delete all child windows
5813 reachable via the next and contents slots of WINDOW. Otherwise setup
5814 WINDOW to not show any buffer. */
5816 void
5817 delete_all_child_windows (Lisp_Object window)
5819 register struct window *w;
5821 w = XWINDOW (window);
5823 if (!NILP (w->next))
5824 /* Delete WINDOW's siblings (we traverse postorderly). */
5825 delete_all_child_windows (w->next);
5827 if (WINDOWP (w->contents))
5829 delete_all_child_windows (w->contents);
5830 wset_combination (w, 0, Qnil);
5832 else if (BUFFERP (w->contents))
5834 unshow_buffer (w);
5835 unchain_marker (XMARKER (w->pointm));
5836 unchain_marker (XMARKER (w->start));
5837 /* Since combination limit makes sense for an internal windows
5838 only, we use this slot to save the buffer for the sake of
5839 possible resurrection in Fset_window_configuration. */
5840 wset_combination_limit (w, w->contents);
5841 wset_buffer (w, Qnil);
5844 Vwindow_list = Qnil;
5847 static int
5848 count_windows (register struct window *window)
5850 register int count = 1;
5851 if (!NILP (window->next))
5852 count += count_windows (XWINDOW (window->next));
5853 if (WINDOWP (window->contents))
5854 count += count_windows (XWINDOW (window->contents));
5855 return count;
5859 /* Fill vector FLAT with leaf windows under W, starting at index I.
5860 Value is last index + 1. */
5861 static int
5862 get_leaf_windows (struct window *w, struct window **flat, int i)
5864 while (w)
5866 if (WINDOWP (w->contents))
5867 i = get_leaf_windows (XWINDOW (w->contents), flat, i);
5868 else
5869 flat[i++] = w;
5871 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5874 return i;
5878 /* Return a pointer to the glyph W's physical cursor is on. Value is
5879 null if W's current matrix is invalid, so that no meaningful glyph
5880 can be returned. */
5881 struct glyph *
5882 get_phys_cursor_glyph (struct window *w)
5884 struct glyph_row *row;
5885 struct glyph *glyph;
5886 int hpos = w->phys_cursor.hpos;
5888 if (!(w->phys_cursor.vpos >= 0
5889 && w->phys_cursor.vpos < w->current_matrix->nrows))
5890 return NULL;
5892 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5893 if (!row->enabled_p)
5894 return NULL;
5896 if (w->hscroll)
5898 /* When the window is hscrolled, cursor hpos can legitimately be
5899 out of bounds, but we draw the cursor at the corresponding
5900 window margin in that case. */
5901 if (!row->reversed_p && hpos < 0)
5902 hpos = 0;
5903 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
5904 hpos = row->used[TEXT_AREA] - 1;
5907 if (0 <= hpos && hpos < row->used[TEXT_AREA])
5908 glyph = row->glyphs[TEXT_AREA] + hpos;
5909 else
5910 glyph = NULL;
5912 return glyph;
5916 static int
5917 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
5919 register struct saved_window *p;
5920 register struct window *w;
5921 register Lisp_Object tem, pers, par;
5923 for (; !NILP (window); window = w->next)
5925 p = SAVED_WINDOW_N (vector, i);
5926 w = XWINDOW (window);
5928 wset_temslot (w, make_number (i)); i++;
5929 p->window = window;
5930 p->buffer = (WINDOW_LEAF_P (w) ? w->contents : Qnil);
5931 p->left_col = make_number (w->left_col);
5932 p->top_line = make_number (w->top_line);
5933 p->total_cols = make_number (w->total_cols);
5934 p->total_lines = make_number (w->total_lines);
5935 p->normal_cols = w->normal_cols;
5936 p->normal_lines = w->normal_lines;
5937 XSETFASTINT (p->hscroll, w->hscroll);
5938 XSETFASTINT (p->min_hscroll, w->min_hscroll);
5939 p->display_table = w->display_table;
5940 p->left_margin_cols = make_number (w->left_margin_cols);
5941 p->right_margin_cols = make_number (w->right_margin_cols);
5942 p->left_fringe_width = make_number (w->left_fringe_width);
5943 p->right_fringe_width = make_number (w->right_fringe_width);
5944 p->fringes_outside_margins = w->fringes_outside_margins ? Qt : Qnil;
5945 p->scroll_bar_width = make_number (w->scroll_bar_width);
5946 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5947 p->dedicated = w->dedicated;
5948 p->combination_limit = w->combination_limit;
5949 p->window_parameters = Qnil;
5951 if (!NILP (Vwindow_persistent_parameters))
5953 /* Run cycle detection on Vwindow_persistent_parameters. */
5954 Lisp_Object tortoise, hare;
5956 hare = tortoise = Vwindow_persistent_parameters;
5957 while (CONSP (hare))
5959 hare = XCDR (hare);
5960 if (!CONSP (hare))
5961 break;
5963 hare = XCDR (hare);
5964 tortoise = XCDR (tortoise);
5966 if (EQ (hare, tortoise))
5967 /* Reset Vwindow_persistent_parameters to Qnil. */
5969 Vwindow_persistent_parameters = Qnil;
5970 break;
5974 for (tem = Vwindow_persistent_parameters; CONSP (tem);
5975 tem = XCDR (tem))
5977 pers = XCAR (tem);
5978 /* Save values for persistent window parameters. */
5979 if (CONSP (pers) && !NILP (XCDR (pers)))
5981 par = Fassq (XCAR (pers), w->window_parameters);
5982 if (NILP (par))
5983 /* If the window has no value for the parameter,
5984 make one. */
5985 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
5986 p->window_parameters);
5987 else
5988 /* If the window has a value for the parameter,
5989 save it. */
5990 p->window_parameters = Fcons (Fcons (XCAR (par),
5991 XCDR (par)),
5992 p->window_parameters);
5997 if (BUFFERP (w->contents))
5999 /* Save w's value of point in the window configuration. If w
6000 is the selected window, then get the value of point from
6001 the buffer; pointm is garbage in the selected window. */
6002 if (EQ (window, selected_window))
6003 p->pointm = build_marker (XBUFFER (w->contents),
6004 BUF_PT (XBUFFER (w->contents)),
6005 BUF_PT_BYTE (XBUFFER (w->contents)));
6006 else
6007 p->pointm = Fcopy_marker (w->pointm, Qnil);
6008 XMARKER (p->pointm)->insertion_type
6009 = !NILP (Vwindow_point_insertion_type);
6011 p->start = Fcopy_marker (w->start, Qnil);
6012 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
6014 tem = BVAR (XBUFFER (w->contents), mark);
6015 p->mark = Fcopy_marker (tem, Qnil);
6017 else
6019 p->pointm = Qnil;
6020 p->start = Qnil;
6021 p->mark = Qnil;
6022 p->start_at_line_beg = Qnil;
6025 if (NILP (w->parent))
6026 p->parent = Qnil;
6027 else
6028 p->parent = XWINDOW (w->parent)->temslot;
6030 if (NILP (w->prev))
6031 p->prev = Qnil;
6032 else
6033 p->prev = XWINDOW (w->prev)->temslot;
6035 if (WINDOWP (w->contents))
6036 i = save_window_save (w->contents, vector, i);
6039 return i;
6042 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6043 Scurrent_window_configuration, 0, 1, 0,
6044 doc: /* Return an object representing the current window configuration of FRAME.
6045 If FRAME is nil or omitted, use the selected frame.
6046 This describes the number of windows, their sizes and current buffers,
6047 and for each displayed buffer, where display starts, and the positions of
6048 point and mark. An exception is made for point in the current buffer:
6049 its value is -not- saved.
6050 This also records the currently selected frame, and FRAME's focus
6051 redirection (see `redirect-frame-focus'). The variable
6052 `window-persistent-parameters' specifies which window parameters are
6053 saved by this function. */)
6054 (Lisp_Object frame)
6056 register Lisp_Object tem;
6057 register int n_windows;
6058 register struct save_window_data *data;
6059 register int i;
6060 struct frame *f = decode_live_frame (frame);
6062 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6063 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6064 PVEC_WINDOW_CONFIGURATION);
6066 data->frame_cols = FRAME_COLS (f);
6067 data->frame_lines = FRAME_LINES (f);
6068 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6069 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6070 data->selected_frame = selected_frame;
6071 data->current_window = FRAME_SELECTED_WINDOW (f);
6072 XSETBUFFER (data->current_buffer, current_buffer);
6073 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6074 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6075 data->root_window = FRAME_ROOT_WINDOW (f);
6076 data->focus_frame = FRAME_FOCUS_FRAME (f);
6077 tem = make_uninit_vector (n_windows);
6078 data->saved_windows = tem;
6079 for (i = 0; i < n_windows; i++)
6080 ASET (tem, i,
6081 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6082 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6083 XSETWINDOW_CONFIGURATION (tem, data);
6084 return (tem);
6087 /* Called after W's margins, fringes or scroll bars was adjusted. */
6089 static void
6090 apply_window_adjustment (struct window *w)
6092 eassert (w);
6093 adjust_window_margins (w);
6094 clear_glyph_matrix (w->current_matrix);
6095 w->window_end_valid = 0;
6096 windows_or_buffers_changed++;
6097 adjust_frame_glyphs (XFRAME (WINDOW_FRAME (w)));
6101 /***********************************************************************
6102 Marginal Areas
6103 ***********************************************************************/
6105 static struct window *
6106 set_window_margins (struct window *w, Lisp_Object left_width,
6107 Lisp_Object right_width)
6109 int left, right;
6111 /* FIXME: what about margins that are too wide? */
6112 left = (NILP (left_width) ? 0
6113 : (CHECK_NATNUM (left_width), XINT (left_width)));
6114 right = (NILP (right_width) ? 0
6115 : (CHECK_NATNUM (right_width), XINT (right_width)));
6117 if (w->left_margin_cols != left || w->right_margin_cols != right)
6119 w->left_margin_cols = left;
6120 w->right_margin_cols = right;
6121 return w;
6123 return NULL;
6126 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6127 2, 3, 0,
6128 doc: /* Set width of marginal areas of window WINDOW.
6129 WINDOW must be a live window and defaults to the selected one.
6131 Second arg LEFT-WIDTH specifies the number of character cells to
6132 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6133 does the same for the right marginal area. A nil width parameter
6134 means no margin.
6136 Return t if any margin was actually changed and nil otherwise. */)
6137 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6139 struct window *w = set_window_margins (decode_live_window (window),
6140 left_width, right_width);
6141 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6145 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6146 0, 1, 0,
6147 doc: /* Get width of marginal areas of window WINDOW.
6148 WINDOW must be a live window and defaults to the selected one.
6150 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6151 If a marginal area does not exist, its width will be returned
6152 as nil. */)
6153 (Lisp_Object window)
6155 struct window *w = decode_live_window (window);
6156 return Fcons (w->left_margin_cols ? make_number (w->left_margin_cols) : Qnil,
6157 w->right_margin_cols ? make_number (w->right_margin_cols) : Qnil);
6162 /***********************************************************************
6163 Fringes
6164 ***********************************************************************/
6166 static struct window *
6167 set_window_fringes (struct window *w, Lisp_Object left_width,
6168 Lisp_Object right_width, Lisp_Object outside_margins)
6170 int left, right, outside = !NILP (outside_margins);
6172 left = (NILP (left_width) ? -1
6173 : (CHECK_NATNUM (left_width), XINT (left_width)));
6174 right = (NILP (right_width) ? -1
6175 : (CHECK_NATNUM (right_width), XINT (right_width)));
6177 /* Do nothing on a tty or if nothing to actually change. */
6178 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6179 && (w->left_fringe_width != left
6180 || w->right_fringe_width != right
6181 || w->fringes_outside_margins != outside))
6183 w->left_fringe_width = left;
6184 w->right_fringe_width = right;
6185 w->fringes_outside_margins = outside;
6186 return w;
6188 return NULL;
6191 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6192 2, 4, 0,
6193 doc: /* Set the fringe widths of window WINDOW.
6194 WINDOW must be a live window and defaults to the selected one.
6196 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6197 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6198 fringe width. If a fringe width arg is nil, that means to use the
6199 frame's default fringe width. Default fringe widths can be set with
6200 the command `set-fringe-style'.
6201 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6202 outside of the display margins. By default, fringes are drawn between
6203 display marginal areas and the text area.
6205 Return t if any fringe was actually changed and nil otherwise. */)
6206 (Lisp_Object window, Lisp_Object left_width,
6207 Lisp_Object right_width, Lisp_Object outside_margins)
6209 struct window *w
6210 = set_window_fringes (decode_live_window (window),
6211 left_width, right_width, outside_margins);
6212 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6216 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6217 0, 1, 0,
6218 doc: /* Get width of fringes of window WINDOW.
6219 WINDOW must be a live window and defaults to the selected one.
6221 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6222 (Lisp_Object window)
6224 struct window *w = decode_live_window (window);
6226 return list3 (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6227 make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6228 WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ? Qt : Qnil);
6233 /***********************************************************************
6234 Scroll bars
6235 ***********************************************************************/
6237 static struct window *
6238 set_window_scroll_bars (struct window *w, Lisp_Object width,
6239 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6241 int iwidth = (NILP (width) ? -1 : (CHECK_NATNUM (width), XINT (width)));
6243 if (iwidth == 0)
6244 vertical_type = Qnil;
6246 if (!(NILP (vertical_type)
6247 || EQ (vertical_type, Qleft)
6248 || EQ (vertical_type, Qright)
6249 || EQ (vertical_type, Qt)))
6250 error ("Invalid type of vertical scroll bar");
6252 if (w->scroll_bar_width != iwidth
6253 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6255 w->scroll_bar_width = iwidth;
6256 wset_vertical_scroll_bar_type (w, vertical_type);
6257 return w;
6259 return NULL;
6262 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6263 Sset_window_scroll_bars, 2, 4, 0,
6264 doc: /* Set width and type of scroll bars of window WINDOW.
6265 WINDOW must be a live window and defaults to the selected one.
6267 Second parameter WIDTH specifies the pixel width for the scroll bar;
6268 this is automatically adjusted to a multiple of the frame column width.
6269 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6270 bar: left, right, or nil.
6271 If WIDTH is nil, use the frame's scroll-bar width.
6272 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6273 Fourth parameter HORIZONTAL-TYPE is currently unused.
6275 Return t if scroll bars was actually changed and nil otherwise. */)
6276 (Lisp_Object window, Lisp_Object width,
6277 Lisp_Object vertical_type, Lisp_Object horizontal_type)
6279 struct window *w
6280 = set_window_scroll_bars (decode_live_window (window),
6281 width, vertical_type, horizontal_type);
6282 return w ? (apply_window_adjustment (w), Qt) : Qnil;
6286 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6287 0, 1, 0,
6288 doc: /* Get width and type of scroll bars of window WINDOW.
6289 WINDOW must be a live window and defaults to the selected one.
6291 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6292 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6293 value. */)
6294 (Lisp_Object window)
6296 struct window *w = decode_live_window (window);
6298 return list4 (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6299 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6300 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6301 make_number (WINDOW_SCROLL_BAR_COLS (w)),
6302 w->vertical_scroll_bar_type, Qnil);
6307 /***********************************************************************
6308 Smooth scrolling
6309 ***********************************************************************/
6311 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6312 doc: /* Return the amount by which WINDOW is scrolled vertically.
6313 If WINDOW is omitted or nil, it defaults to the selected window.
6314 Normally, value is a multiple of the canonical character height of WINDOW;
6315 optional second arg PIXELS-P means value is measured in pixels. */)
6316 (Lisp_Object window, Lisp_Object pixels_p)
6318 Lisp_Object result;
6319 struct window *w = decode_live_window (window);
6320 struct frame *f = XFRAME (w->frame);
6322 if (FRAME_WINDOW_P (f))
6323 result = (NILP (pixels_p)
6324 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6325 : make_number (-w->vscroll));
6326 else
6327 result = make_number (0);
6328 return result;
6332 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6333 2, 3, 0,
6334 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6335 WINDOW nil means use the selected window. Normally, VSCROLL is a
6336 non-negative multiple of the canonical character height of WINDOW;
6337 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6338 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6339 corresponds to an integral number of pixels. The return value is the
6340 result of this rounding.
6341 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6342 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6344 struct window *w = decode_live_window (window);
6345 struct frame *f = XFRAME (w->frame);
6347 CHECK_NUMBER_OR_FLOAT (vscroll);
6349 if (FRAME_WINDOW_P (f))
6351 int old_dy = w->vscroll;
6353 w->vscroll = - (NILP (pixels_p)
6354 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6355 : XFLOATINT (vscroll));
6356 w->vscroll = min (w->vscroll, 0);
6358 if (w->vscroll != old_dy)
6360 /* Adjust glyph matrix of the frame if the virtual display
6361 area becomes larger than before. */
6362 if (w->vscroll < 0 && w->vscroll < old_dy)
6363 adjust_frame_glyphs (f);
6365 /* Prevent redisplay shortcuts. */
6366 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
6370 return Fwindow_vscroll (window, pixels_p);
6374 /* Call FN for all leaf windows on frame F. FN is called with the
6375 first argument being a pointer to the leaf window, and with
6376 additional argument USER_DATA. Stops when FN returns 0. */
6378 static void
6379 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6380 void *user_data)
6382 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6383 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6384 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6388 /* Helper function for foreach_window. Call FN for all leaf windows
6389 reachable from W. FN is called with the first argument being a
6390 pointer to the leaf window, and with additional argument USER_DATA.
6391 Stop when FN returns 0. Value is 0 if stopped by FN. */
6393 static int
6394 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6396 int cont;
6398 for (cont = 1; w && cont;)
6400 if (WINDOWP (w->contents))
6401 cont = foreach_window_1 (XWINDOW (w->contents), fn, user_data);
6402 else
6403 cont = fn (w, user_data);
6405 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6408 return cont;
6411 /***********************************************************************
6412 Initialization
6413 ***********************************************************************/
6415 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6416 describe the same state of affairs. This is used by Fequal.
6418 IGNORE_POSITIONS means ignore non-matching scroll positions
6419 and the like.
6421 This ignores a couple of things like the dedication status of
6422 window, combination_limit and the like. This might have to be
6423 fixed. */
6425 bool
6426 compare_window_configurations (Lisp_Object configuration1,
6427 Lisp_Object configuration2,
6428 bool ignore_positions)
6430 register struct save_window_data *d1, *d2;
6431 struct Lisp_Vector *sws1, *sws2;
6432 ptrdiff_t i;
6434 CHECK_WINDOW_CONFIGURATION (configuration1);
6435 CHECK_WINDOW_CONFIGURATION (configuration2);
6437 d1 = (struct save_window_data *) XVECTOR (configuration1);
6438 d2 = (struct save_window_data *) XVECTOR (configuration2);
6439 sws1 = XVECTOR (d1->saved_windows);
6440 sws2 = XVECTOR (d2->saved_windows);
6442 /* Frame settings must match. */
6443 if (d1->frame_cols != d2->frame_cols
6444 || d1->frame_lines != d2->frame_lines
6445 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6446 || !EQ (d1->selected_frame, d2->selected_frame)
6447 || !EQ (d1->current_buffer, d2->current_buffer)
6448 || (!ignore_positions
6449 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6450 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6451 || !EQ (d1->focus_frame, d2->focus_frame)
6452 /* Verify that the two configurations have the same number of windows. */
6453 || sws1->header.size != sws2->header.size)
6454 return 0;
6456 for (i = 0; i < sws1->header.size; i++)
6458 struct saved_window *sw1, *sw2;
6460 sw1 = SAVED_WINDOW_N (sws1, i);
6461 sw2 = SAVED_WINDOW_N (sws2, i);
6463 if (
6464 /* The "current" windows in the two configurations must
6465 correspond to each other. */
6466 EQ (d1->current_window, sw1->window)
6467 != EQ (d2->current_window, sw2->window)
6468 /* Windows' buffers must match. */
6469 || !EQ (sw1->buffer, sw2->buffer)
6470 || !EQ (sw1->left_col, sw2->left_col)
6471 || !EQ (sw1->top_line, sw2->top_line)
6472 || !EQ (sw1->total_cols, sw2->total_cols)
6473 || !EQ (sw1->total_lines, sw2->total_lines)
6474 || !EQ (sw1->display_table, sw2->display_table)
6475 /* The next two disjuncts check the window structure for
6476 equality. */
6477 || !EQ (sw1->parent, sw2->parent)
6478 || !EQ (sw1->prev, sw2->prev)
6479 || (!ignore_positions
6480 && (!EQ (sw1->hscroll, sw2->hscroll)
6481 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6482 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6483 || NILP (Fequal (sw1->start, sw2->start))
6484 || NILP (Fequal (sw1->pointm, sw2->pointm))
6485 || NILP (Fequal (sw1->mark, sw2->mark))))
6486 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6487 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6488 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6489 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6490 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6491 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6492 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6493 return 0;
6496 return 1;
6499 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6500 Scompare_window_configurations, 2, 2, 0,
6501 doc: /* Compare two window configurations as regards the structure of windows.
6502 This function ignores details such as the values of point and mark
6503 and scrolling positions. */)
6504 (Lisp_Object x, Lisp_Object y)
6506 if (compare_window_configurations (x, y, 1))
6507 return Qt;
6508 return Qnil;
6511 void
6512 init_window_once (void)
6514 struct frame *f = make_initial_frame ();
6515 XSETFRAME (selected_frame, f);
6516 Vterminal_frame = selected_frame;
6517 minibuf_window = f->minibuffer_window;
6518 selected_window = f->selected_window;
6519 last_nonminibuf_frame = f;
6521 window_initialized = 1;
6524 void
6525 init_window (void)
6527 Vwindow_list = Qnil;
6530 void
6531 syms_of_window (void)
6533 DEFSYM (Qscroll_up, "scroll-up");
6534 DEFSYM (Qscroll_down, "scroll-down");
6535 DEFSYM (Qscroll_command, "scroll-command");
6537 Fput (Qscroll_up, Qscroll_command, Qt);
6538 Fput (Qscroll_down, Qscroll_command, Qt);
6540 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6541 DEFSYM (Qwindowp, "windowp");
6542 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6543 DEFSYM (Qwindow_live_p, "window-live-p");
6544 DEFSYM (Qwindow_valid_p, "window-valid-p");
6545 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6546 DEFSYM (Qdelete_window, "delete-window");
6547 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6548 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6549 DEFSYM (Qsafe, "safe");
6550 DEFSYM (Qdisplay_buffer, "display-buffer");
6551 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6552 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6553 DEFSYM (Qget_mru_window, "get-mru-window");
6554 DEFSYM (Qwindow_size, "window-size");
6555 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6556 DEFSYM (Qabove, "above");
6557 DEFSYM (Qbelow, "below");
6558 DEFSYM (Qclone_of, "clone-of");
6560 staticpro (&Vwindow_list);
6562 minibuf_selected_window = Qnil;
6563 staticpro (&minibuf_selected_window);
6565 window_scroll_pixel_based_preserve_x = -1;
6566 window_scroll_pixel_based_preserve_y = -1;
6567 window_scroll_preserve_hpos = -1;
6568 window_scroll_preserve_vpos = -1;
6570 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6571 doc: /* Non-nil means call as function to display a help buffer.
6572 The function is called with one argument, the buffer to be displayed.
6573 Used by `with-output-to-temp-buffer'.
6574 If this function is used, then it must do the entire job of showing
6575 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6576 Vtemp_buffer_show_function = Qnil;
6578 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6579 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6580 Vminibuf_scroll_window = Qnil;
6582 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6583 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6584 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6585 is displayed in the `mode-line' face. */);
6586 mode_line_in_non_selected_windows = 1;
6588 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6589 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6590 Vother_window_scroll_buffer = Qnil;
6592 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6593 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6594 auto_window_vscroll_p = 1;
6596 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6597 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6598 next_screen_context_lines = 2;
6600 DEFVAR_LISP ("scroll-preserve-screen-position",
6601 Vscroll_preserve_screen_position,
6602 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6603 A value of nil means point does not keep its screen position except
6604 at the scroll margin or window boundary respectively.
6605 A value of t means point keeps its screen position if the scroll
6606 command moved it vertically out of the window, e.g. when scrolling
6607 by full screens.
6608 Any other value means point always keeps its screen position.
6609 Scroll commands should have the `scroll-command' property
6610 on their symbols to be controlled by this variable. */);
6611 Vscroll_preserve_screen_position = Qnil;
6613 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6614 doc: /* Type of marker to use for `window-point'. */);
6615 Vwindow_point_insertion_type = Qnil;
6617 DEFVAR_LISP ("window-configuration-change-hook",
6618 Vwindow_configuration_change_hook,
6619 doc: /* Functions to call when window configuration changes.
6620 The buffer-local part is run once per window, with the relevant window
6621 selected; while the global part is run only once for the modified frame,
6622 with the relevant frame selected. */);
6623 Vwindow_configuration_change_hook = Qnil;
6625 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6626 doc: /* Non-nil means `recenter' redraws entire frame.
6627 If this option is non-nil, then the `recenter' command with a nil
6628 argument will redraw the entire frame; the special value `tty' causes
6629 the frame to be redrawn only if it is a tty frame. */);
6630 Vrecenter_redisplay = Qtty;
6632 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6633 doc: /* If t, resize window combinations proportionally.
6634 If this variable is nil, splitting a window gets the entire screen space
6635 for displaying the new window from the window to split. Deleting and
6636 resizing a window preferably resizes one adjacent window only.
6638 If this variable is t, splitting a window tries to get the space
6639 proportionally from all windows in the same combination. This also
6640 allows to split a window that is otherwise too small or of fixed size.
6641 Resizing and deleting a window proportionally resize all windows in the
6642 same combination.
6644 Other values are reserved for future use.
6646 This variable takes no effect if the variable `window-combination-limit' is
6647 non-nil. */);
6648 Vwindow_combination_resize = Qnil;
6650 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6651 doc: /* If non-nil, splitting a window makes a new parent window.
6652 The following values are recognized:
6654 nil means splitting a window will create a new parent window only if the
6655 window has no parent window or the window shall become part of a
6656 combination orthogonal to the one it is part of.
6658 `window-size' means that splitting a window for displaying a buffer
6659 makes a new parent window provided `display-buffer' is supposed to
6660 explicitly set the window's size due to the presence of a
6661 `window-height' or `window-width' entry in the alist used by
6662 `display-buffer'. Otherwise, this value is handled like nil.
6664 `temp-buffer' means that splitting a window for displaying a temporary
6665 buffer always makes a new parent window. Otherwise, this value is
6666 handled like nil.
6668 `display-buffer' means that splitting a window for displaying a buffer
6669 always makes a new parent window. Since temporary buffers are
6670 displayed by the function `display-buffer', this value is stronger
6671 than `temp-buffer'. Splitting a window for other purpose makes a
6672 new parent window only if needed.
6674 t means that splitting a window always creates a new parent window. If
6675 all splits behave this way, each frame's window tree is a binary
6676 tree and every window but the frame's root window has exactly one
6677 sibling.
6679 Other values are reserved for future use. */);
6680 Vwindow_combination_limit = Qwindow_size;
6682 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6683 doc: /* Alist of persistent window parameters.
6684 This alist specifies which window parameters shall get saved by
6685 `current-window-configuration' and `window-state-get' and subsequently
6686 restored to their previous values by `set-window-configuration' and
6687 `window-state-put'.
6689 The car of each entry of this alist is the symbol specifying the
6690 parameter. The cdr is one of the following:
6692 nil means the parameter is neither saved by `window-state-get' nor by
6693 `current-window-configuration'.
6695 t means the parameter is saved by `current-window-configuration' and,
6696 provided its WRITABLE argument is nil, by `window-state-get'.
6698 The symbol `writable' means the parameter is saved unconditionally by
6699 both `current-window-configuration' and `window-state-get'. Do not use
6700 this value for parameters without read syntax (like windows or frames).
6702 Parameters not saved by `current-window-configuration' or
6703 `window-state-get' are left alone by `set-window-configuration'
6704 respectively are not installed by `window-state-put'. */);
6705 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6707 defsubr (&Sselected_window);
6708 defsubr (&Sminibuffer_window);
6709 defsubr (&Swindow_minibuffer_p);
6710 defsubr (&Swindowp);
6711 defsubr (&Swindow_valid_p);
6712 defsubr (&Swindow_live_p);
6713 defsubr (&Swindow_frame);
6714 defsubr (&Sframe_root_window);
6715 defsubr (&Sframe_first_window);
6716 defsubr (&Sframe_selected_window);
6717 defsubr (&Sset_frame_selected_window);
6718 defsubr (&Spos_visible_in_window_p);
6719 defsubr (&Swindow_line_height);
6720 defsubr (&Swindow_buffer);
6721 defsubr (&Swindow_parent);
6722 defsubr (&Swindow_top_child);
6723 defsubr (&Swindow_left_child);
6724 defsubr (&Swindow_next_sibling);
6725 defsubr (&Swindow_prev_sibling);
6726 defsubr (&Swindow_combination_limit);
6727 defsubr (&Sset_window_combination_limit);
6728 defsubr (&Swindow_use_time);
6729 defsubr (&Swindow_top_line);
6730 defsubr (&Swindow_left_column);
6731 defsubr (&Swindow_total_height);
6732 defsubr (&Swindow_total_width);
6733 defsubr (&Swindow_normal_size);
6734 defsubr (&Swindow_new_total);
6735 defsubr (&Swindow_new_normal);
6736 defsubr (&Sset_window_new_total);
6737 defsubr (&Sset_window_new_normal);
6738 defsubr (&Swindow_resize_apply);
6739 defsubr (&Swindow_body_height);
6740 defsubr (&Swindow_body_width);
6741 defsubr (&Swindow_hscroll);
6742 defsubr (&Sset_window_hscroll);
6743 defsubr (&Swindow_redisplay_end_trigger);
6744 defsubr (&Sset_window_redisplay_end_trigger);
6745 defsubr (&Swindow_edges);
6746 defsubr (&Swindow_pixel_edges);
6747 defsubr (&Swindow_absolute_pixel_edges);
6748 defsubr (&Swindow_inside_edges);
6749 defsubr (&Swindow_inside_pixel_edges);
6750 defsubr (&Swindow_inside_absolute_pixel_edges);
6751 defsubr (&Scoordinates_in_window_p);
6752 defsubr (&Swindow_at);
6753 defsubr (&Swindow_point);
6754 defsubr (&Swindow_start);
6755 defsubr (&Swindow_end);
6756 defsubr (&Sset_window_point);
6757 defsubr (&Sset_window_start);
6758 defsubr (&Swindow_dedicated_p);
6759 defsubr (&Sset_window_dedicated_p);
6760 defsubr (&Swindow_display_table);
6761 defsubr (&Sset_window_display_table);
6762 defsubr (&Snext_window);
6763 defsubr (&Sprevious_window);
6764 defsubr (&Sget_buffer_window);
6765 defsubr (&Sdelete_other_windows_internal);
6766 defsubr (&Sdelete_window_internal);
6767 defsubr (&Sresize_mini_window_internal);
6768 defsubr (&Sset_window_buffer);
6769 defsubr (&Srun_window_configuration_change_hook);
6770 defsubr (&Sselect_window);
6771 defsubr (&Sforce_window_update);
6772 defsubr (&Ssplit_window_internal);
6773 defsubr (&Sscroll_up);
6774 defsubr (&Sscroll_down);
6775 defsubr (&Sscroll_left);
6776 defsubr (&Sscroll_right);
6777 defsubr (&Sother_window_for_scrolling);
6778 defsubr (&Sscroll_other_window);
6779 defsubr (&Sminibuffer_selected_window);
6780 defsubr (&Srecenter);
6781 defsubr (&Swindow_text_height);
6782 defsubr (&Smove_to_window_line);
6783 defsubr (&Swindow_configuration_p);
6784 defsubr (&Swindow_configuration_frame);
6785 defsubr (&Sset_window_configuration);
6786 defsubr (&Scurrent_window_configuration);
6787 defsubr (&Sset_window_margins);
6788 defsubr (&Swindow_margins);
6789 defsubr (&Sset_window_fringes);
6790 defsubr (&Swindow_fringes);
6791 defsubr (&Sset_window_scroll_bars);
6792 defsubr (&Swindow_scroll_bars);
6793 defsubr (&Swindow_vscroll);
6794 defsubr (&Sset_window_vscroll);
6795 defsubr (&Scompare_window_configurations);
6796 defsubr (&Swindow_list);
6797 defsubr (&Swindow_list_1);
6798 defsubr (&Swindow_prev_buffers);
6799 defsubr (&Sset_window_prev_buffers);
6800 defsubr (&Swindow_next_buffers);
6801 defsubr (&Sset_window_next_buffers);
6802 defsubr (&Swindow_parameters);
6803 defsubr (&Swindow_parameter);
6804 defsubr (&Sset_window_parameter);
6807 void
6808 keys_of_window (void)
6810 initial_define_key (control_x_map, '<', "scroll-left");
6811 initial_define_key (control_x_map, '>', "scroll-right");
6813 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6814 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6815 initial_define_key (meta_map, 'v', "scroll-down-command");