Install some window-size related functions and window-list-1.
[emacs.git] / src / window.c
blob43635fe5a6bb06e42065c40a3f6494af0c6a64aa
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2011
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <setjmp.h>
25 #include "lisp.h"
26 #include "buffer.h"
27 #include "keyboard.h"
28 #include "keymap.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "commands.h"
32 #include "indent.h"
33 #include "termchar.h"
34 #include "disptab.h"
35 #include "dispextern.h"
36 #include "blockinput.h"
37 #include "intervals.h"
38 #include "termhooks.h" /* For FRAME_TERMINAL. */
40 #ifdef HAVE_X_WINDOWS
41 #include "xterm.h"
42 #endif /* HAVE_X_WINDOWS */
43 #ifdef WINDOWSNT
44 #include "w32term.h"
45 #endif
46 #ifdef MSDOS
47 #include "msdos.h"
48 #endif
49 #ifdef HAVE_NS
50 #include "nsterm.h"
51 #endif
53 Lisp_Object Qwindowp, Qwindow_live_p;
54 static Lisp_Object Qwindow_configuration_p;
55 static Lisp_Object Qdisplay_buffer;
56 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
57 static Lisp_Object Qwindow_size_fixed;
59 static int displayed_window_lines (struct window *);
60 static struct window *decode_window (Lisp_Object);
61 static int count_windows (struct window *);
62 static int get_leaf_windows (struct window *, struct window **, int);
63 static void window_scroll (Lisp_Object, int, int, int);
64 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
65 static void window_scroll_line_based (Lisp_Object, int, int, int);
66 static int window_min_size_1 (struct window *, int, int);
67 static int window_min_size_2 (struct window *, int, int);
68 static int window_min_size (struct window *, int, int, int, int *);
69 static void size_window (Lisp_Object, int, int, int, int, int);
70 static int freeze_window_start (struct window *, void *);
71 static int window_fixed_size_p (struct window *, int, int);
72 static void enlarge_window (Lisp_Object, int, int);
73 static Lisp_Object window_list (void);
74 static int add_window_to_list (struct window *, void *);
75 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
76 Lisp_Object);
77 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
78 Lisp_Object, int);
79 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
80 Lisp_Object *);
81 static void foreach_window (struct frame *,
82 int (* fn) (struct window *, void *),
83 void *);
84 static int foreach_window_1 (struct window *,
85 int (* fn) (struct window *, void *),
86 void *);
87 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
88 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
90 /* This is the window in which the terminal's cursor should
91 be left when nothing is being done with it. This must
92 always be a leaf window, and its buffer is selected by
93 the top level editing loop at the end of each command.
95 This value is always the same as
96 FRAME_SELECTED_WINDOW (selected_frame). */
97 Lisp_Object selected_window;
99 /* A list of all windows for use by next_window and Fwindow_list.
100 Functions creating or deleting windows should invalidate this cache
101 by setting it to nil. */
102 Lisp_Object Vwindow_list;
104 /* The mini-buffer window of the selected frame.
105 Note that you cannot test for mini-bufferness of an arbitrary window
106 by comparing against this; but you can test for mini-bufferness of
107 the selected window. */
108 Lisp_Object minibuf_window;
110 /* Non-nil means it is the window whose mode line should be
111 shown as the selected window when the minibuffer is selected. */
112 Lisp_Object minibuf_selected_window;
114 /* Hook run at end of temp_output_buffer_show. */
115 static Lisp_Object Qtemp_buffer_show_hook;
117 /* Incremented for each window created. */
118 static int sequence_number;
120 /* Nonzero after init_window_once has finished. */
121 static int window_initialized;
123 /* Hook to run when window config changes. */
124 static Lisp_Object Qwindow_configuration_change_hook;
126 /* Incremented by 1 whenever a window is deleted. */
127 static int window_deletion_count;
129 /* Used by the function window_scroll_pixel_based */
130 static int window_scroll_pixel_based_preserve_x;
131 static int window_scroll_pixel_based_preserve_y;
133 /* Same for window_scroll_line_based. */
134 static int window_scroll_preserve_hpos;
135 static int window_scroll_preserve_vpos;
137 static struct window *
138 decode_window (register Lisp_Object window)
140 if (NILP (window))
141 return XWINDOW (selected_window);
143 CHECK_LIVE_WINDOW (window);
144 return XWINDOW (window);
147 static struct window *
148 decode_any_window (register Lisp_Object window)
150 if (NILP (window))
151 return XWINDOW (selected_window);
153 CHECK_WINDOW (window);
154 return XWINDOW (window);
157 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
158 doc: /* Return t if OBJECT is a window and nil otherwise. */)
159 (Lisp_Object object)
161 return WINDOWP (object) ? Qt : Qnil;
164 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
165 doc: /* Return t if OBJECT is a live window and nil otherwise.
166 A live window is a window that displays a buffer. */)
167 (Lisp_Object object)
169 return WINDOW_LIVE_P (object) ? Qt : Qnil;
172 /* Frames and windows. */
173 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 1, 1, 0,
174 doc: /* Return the frame that window WINDOW is on.
175 WINDOW can be any window and defaults to the selected one. */)
176 (Lisp_Object window)
178 return decode_any_window (window)->frame;
181 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
182 doc: /* Return the root window of FRAME_OR_WINDOW.
183 If omitted, FRAME_OR_WINDOW defaults to the currently selected frame.
184 Else if FRAME_OR_WINDOW denotes any window, return the root window of
185 that window's frame. If FRAME_OR_WINDOW denotes a live frame, return
186 the root window of that frame. */)
187 (Lisp_Object frame_or_window)
189 Lisp_Object window;
191 if (NILP (frame_or_window))
192 window = SELECTED_FRAME ()->root_window;
193 else if (WINDOWP (frame_or_window))
194 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
195 else
197 CHECK_LIVE_FRAME (frame_or_window);
198 window = XFRAME (frame_or_window)->root_window;
201 return window;
204 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
205 doc: /* Return the window used now for minibuffers.
206 If the optional argument FRAME is specified, return the minibuffer window
207 used by that frame. */)
208 (Lisp_Object frame)
210 if (NILP (frame))
211 frame = selected_frame;
212 CHECK_LIVE_FRAME (frame);
213 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
216 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
217 Swindow_minibuffer_p, 0, 1, 0,
218 doc: /* Return non-nil if WINDOW is a minibuffer window.
219 WINDOW can be any window and defaults to the selected one. */)
220 (Lisp_Object window)
222 return MINI_WINDOW_P (decode_any_window (window)) ? Qt : Qnil;
225 /* Don't move this to window.el - this must be a safe routine. */
226 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
227 doc: /* Return the topmost, leftmost live window on FRAME_OR_WINDOW.
228 If omitted, FRAME_OR_WINDOW defaults to the currently selected frame.
229 Else if FRAME_OR_WINDOW denotes any window, return the first window of
230 that window's frame. If FRAME_OR_WINDOW denotes a live frame, return
231 the first window of that frame. */)
232 (Lisp_Object frame_or_window)
234 Lisp_Object window;
236 if (NILP (frame_or_window))
237 window = SELECTED_FRAME ()->root_window;
238 else if (WINDOWP (frame_or_window))
239 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
240 else
242 CHECK_LIVE_FRAME (frame_or_window);
243 window = XFRAME (frame_or_window)->root_window;
246 while (NILP (XWINDOW (window)->buffer))
248 if (! NILP (XWINDOW (window)->hchild))
249 window = XWINDOW (window)->hchild;
250 else if (! NILP (XWINDOW (window)->vchild))
251 window = XWINDOW (window)->vchild;
252 else
253 abort ();
256 return window;
259 DEFUN ("frame-selected-window", Fframe_selected_window,
260 Sframe_selected_window, 0, 1, 0,
261 doc: /* Return the selected window of FRAME_OR_WINDOW.
262 If omitted, FRAME_OR_WINDOW defaults to the currently selected frame.
263 Else if FRAME_OR_WINDOW denotes any window, return the selected window
264 of that window's frame. If FRAME_OR_WINDOW denotes a live frame, return
265 the selected window of that frame. */)
266 (Lisp_Object frame_or_window)
268 Lisp_Object window;
270 if (NILP (frame_or_window))
271 window = SELECTED_FRAME ()->selected_window;
272 else if (WINDOWP (frame_or_window))
273 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
274 else
276 CHECK_LIVE_FRAME (frame_or_window);
277 window = XFRAME (frame_or_window)->selected_window;
280 return window;
283 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
284 Sset_frame_selected_window, 2, 3, 0,
285 doc: /* Set selected window of FRAME to WINDOW.
286 FRAME must be a live frame and defaults to the selected one. If FRAME
287 is the selected frame, this makes WINDOW the selected window. Optional
288 argument NORECORD non-nil means to neither change the order of recently
289 selected windows nor the buffer list. WINDOW must denote a live window.
290 Return WINDOW. */)
291 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
293 if (NILP (frame))
294 frame = selected_frame;
296 CHECK_LIVE_FRAME (frame);
297 CHECK_LIVE_WINDOW (window);
299 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
300 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
302 if (EQ (frame, selected_frame))
303 return Fselect_window (window, norecord);
304 else
305 return XFRAME (frame)->selected_window = window;
308 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
309 doc: /* Return the selected window.
310 The selected window is the window in which the standard cursor for
311 selected windows appears and to which many commands apply. */)
312 (void)
314 return selected_window;
317 /* If select_window is called with inhibit_point_swap non-zero it will
318 not store point of the old selected window's buffer back into that
319 window's pointm slot. This is needed by Fset_window_configuration to
320 avoid that the display routine is called with selected_window set to
321 Qnil causing a subsequent crash. */
322 static Lisp_Object
323 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
325 register struct window *w;
326 register struct window *ow;
327 struct frame *sf;
329 CHECK_LIVE_WINDOW (window);
331 w = XWINDOW (window);
332 w->frozen_window_start_p = 0;
334 if (NILP (norecord))
336 ++window_select_count;
337 XSETFASTINT (w->use_time, window_select_count);
338 record_buffer (w->buffer);
341 if (EQ (window, selected_window) && !inhibit_point_swap)
342 return window;
344 sf = SELECTED_FRAME ();
345 if (XFRAME (WINDOW_FRAME (w)) != sf)
347 XFRAME (WINDOW_FRAME (w))->selected_window = window;
348 /* Use this rather than Fhandle_switch_frame
349 so that FRAME_FOCUS_FRAME is moved appropriately as we
350 move around in the state where a minibuffer in a separate
351 frame is active. */
352 Fselect_frame (WINDOW_FRAME (w), norecord);
353 /* Fselect_frame called us back so we've done all the work already. */
354 eassert (EQ (window, selected_window));
355 return window;
357 else
358 sf->selected_window = window;
360 /* Store the current buffer's actual point into the
361 old selected window. It belongs to that window,
362 and when the window is not selected, must be in the window. */
363 if (!inhibit_point_swap)
365 ow = XWINDOW (selected_window);
366 if (! NILP (ow->buffer))
367 set_marker_both (ow->pointm, ow->buffer,
368 BUF_PT (XBUFFER (ow->buffer)),
369 BUF_PT_BYTE (XBUFFER (ow->buffer)));
372 selected_window = window;
374 Fset_buffer (w->buffer);
376 BVAR (XBUFFER (w->buffer), last_selected_window) = window;
378 /* Go to the point recorded in the window.
379 This is important when the buffer is in more
380 than one window. It also matters when
381 redisplay_window has altered point after scrolling,
382 because it makes the change only in the window. */
384 register EMACS_INT new_point = marker_position (w->pointm);
385 if (new_point < BEGV)
386 SET_PT (BEGV);
387 else if (new_point > ZV)
388 SET_PT (ZV);
389 else
390 SET_PT (new_point);
393 windows_or_buffers_changed++;
394 return window;
397 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
398 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
399 Also make WINDOW's buffer current and make WINDOW the frame's selected
400 window. Return WINDOW.
402 Optional second arg NORECORD non-nil means do not put this buffer at the
403 front of the buffer list and do not make this window the most recently
404 selected one.
406 Note that the main editor command loop sets the current buffer to the
407 buffer of the selected window before each command. */)
408 (register Lisp_Object window, Lisp_Object norecord)
410 return select_window (window, norecord, 0);
413 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
414 doc: /* Return the buffer that WINDOW is displaying.
415 WINDOW can be any window and defaults to the selected one.
416 If WINDOW is an internal window return nil. */)
417 (Lisp_Object window)
419 return decode_any_window (window)->buffer;
422 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
423 doc: /* Return WINDOW's parent window.
424 WINDOW can be any window and defaults to the selected one.
425 Return nil if WINDOW has no parent. */)
426 (Lisp_Object window)
428 return decode_any_window (window)->parent;
431 DEFUN ("window-vchild", Fwindow_vchild, Swindow_vchild, 0, 1, 0,
432 doc: /* Return WINDOW's first vertical child window.
433 WINDOW can be any window and defaults to the selected one.
434 Return nil if WINDOW has no vertical child. */)
435 (Lisp_Object window)
437 return decode_any_window (window)->vchild;
440 DEFUN ("window-hchild", Fwindow_hchild, Swindow_hchild, 0, 1, 0,
441 doc: /* Return WINDOW's first horizontal child window.
442 WINDOW can be any window and defaults to the selected one.
443 Return nil if WINDOW has no horizontal child. */)
444 (Lisp_Object window)
446 return decode_any_window (window)->hchild;
449 DEFUN ("window-next", Fwindow_next, Swindow_next, 0, 1, 0,
450 doc: /* Return WINDOW's right sibling window.
451 WINDOW can be any window and defaults to the selected one.
452 Return nil if WINDOW has no right sibling. */)
453 (Lisp_Object window)
455 return decode_any_window (window)->next;
458 DEFUN ("window-prev", Fwindow_prev, Swindow_prev, 0, 1, 0,
459 doc: /* Return WINDOW's left sibling window.
460 WINDOW can be any window and defaults to the selected one.
461 Return nil if WINDOW has no left sibling. */)
462 (Lisp_Object window)
464 return decode_any_window (window)->prev;
467 Lisp_Object
468 make_window (void)
470 Lisp_Object val;
471 register struct window *p;
473 p = allocate_window ();
474 ++sequence_number;
475 XSETFASTINT (p->sequence_number, sequence_number);
476 XSETFASTINT (p->left_col, 0);
477 XSETFASTINT (p->top_line, 0);
478 XSETFASTINT (p->total_lines, 0);
479 XSETFASTINT (p->total_cols, 0);
480 XSETFASTINT (p->hscroll, 0);
481 XSETFASTINT (p->min_hscroll, 0);
482 p->orig_top_line = p->orig_total_lines = Qnil;
483 p->start = Fmake_marker ();
484 p->pointm = Fmake_marker ();
485 XSETFASTINT (p->use_time, 0);
486 p->frame = Qnil;
487 p->display_table = Qnil;
488 p->dedicated = Qnil;
489 p->window_parameters = Qnil;
490 p->pseudo_window_p = 0;
491 memset (&p->cursor, 0, sizeof (p->cursor));
492 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
493 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
494 p->desired_matrix = p->current_matrix = 0;
495 p->nrows_scale_factor = p->ncols_scale_factor = 1;
496 p->phys_cursor_type = -1;
497 p->phys_cursor_width = -1;
498 p->must_be_updated_p = 0;
499 XSETFASTINT (p->window_end_vpos, 0);
500 XSETFASTINT (p->window_end_pos, 0);
501 p->window_end_valid = Qnil;
502 p->vscroll = 0;
503 XSETWINDOW (val, p);
504 XSETFASTINT (p->last_point, 0);
505 p->frozen_window_start_p = 0;
506 p->last_cursor_off_p = p->cursor_off_p = 0;
507 p->left_margin_cols = Qnil;
508 p->right_margin_cols = Qnil;
509 p->left_fringe_width = Qnil;
510 p->right_fringe_width = Qnil;
511 p->fringes_outside_margins = Qnil;
512 p->scroll_bar_width = Qnil;
513 p->vertical_scroll_bar_type = Qt;
514 p->resize_proportionally = Qnil;
516 Vwindow_list = Qnil;
517 return val;
520 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
521 Spos_visible_in_window_p, 0, 3, 0,
522 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
523 Return nil if that position is scrolled vertically out of view.
524 If a character is only partially visible, nil is returned, unless the
525 optional argument PARTIALLY is non-nil.
526 If POS is only out of view because of horizontal scrolling, return non-nil.
527 If POS is t, it specifies the position of the last visible glyph in WINDOW.
528 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
530 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
531 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
532 where X and Y are the pixel coordinates relative to the top left corner
533 of the window. The remaining elements are omitted if the character after
534 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
535 off-window at the top and bottom of the row, ROWH is the height of the
536 display row, and VPOS is the row number (0-based) containing POS. */)
537 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
539 register struct window *w;
540 register EMACS_INT posint;
541 register struct buffer *buf;
542 struct text_pos top;
543 Lisp_Object in_window = Qnil;
544 int rtop, rbot, rowh, vpos, fully_p = 1;
545 int x, y;
547 w = decode_window (window);
548 buf = XBUFFER (w->buffer);
549 SET_TEXT_POS_FROM_MARKER (top, w->start);
551 if (EQ (pos, Qt))
552 posint = -1;
553 else if (!NILP (pos))
555 CHECK_NUMBER_COERCE_MARKER (pos);
556 posint = XINT (pos);
558 else if (w == XWINDOW (selected_window))
559 posint = PT;
560 else
561 posint = XMARKER (w->pointm)->charpos;
563 /* If position is above window start or outside buffer boundaries,
564 or if window start is out of range, position is not visible. */
565 if ((EQ (pos, Qt)
566 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
567 && CHARPOS (top) >= BUF_BEGV (buf)
568 && CHARPOS (top) <= BUF_ZV (buf)
569 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
570 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
571 in_window = Qt;
573 if (!NILP (in_window) && !NILP (partially))
575 Lisp_Object part = Qnil;
576 if (!fully_p)
577 part = list4 (make_number (rtop), make_number (rbot),
578 make_number (rowh), make_number (vpos));
579 in_window = Fcons (make_number (x),
580 Fcons (make_number (y), part));
583 return in_window;
586 DEFUN ("window-line-height", Fwindow_line_height,
587 Swindow_line_height, 0, 2, 0,
588 doc: /* Return height in pixels of text line LINE in window WINDOW.
589 If WINDOW is nil or omitted, use selected window.
591 Return height of current line if LINE is omitted or nil. Return height of
592 header or mode line if LINE is `header-line' and `mode-line'.
593 Otherwise, LINE is a text line number starting from 0. A negative number
594 counts from the end of the window.
596 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
597 in pixels of the visible part of the line, VPOS and YPOS are the
598 vertical position in lines and pixels of the line, relative to the top
599 of the first text line, and OFFBOT is the number of off-window pixels at
600 the bottom of the text line. If there are off-window pixels at the top
601 of the (first) text line, YPOS is negative.
603 Return nil if window display is not up-to-date. In that case, use
604 `pos-visible-in-window-p' to obtain the information. */)
605 (Lisp_Object line, Lisp_Object window)
607 register struct window *w;
608 register struct buffer *b;
609 struct glyph_row *row, *end_row;
610 int max_y, crop, i, n;
612 w = decode_window (window);
614 if (noninteractive
615 || w->pseudo_window_p)
616 return Qnil;
618 CHECK_BUFFER (w->buffer);
619 b = XBUFFER (w->buffer);
621 /* Fail if current matrix is not up-to-date. */
622 if (NILP (w->window_end_valid)
623 || current_buffer->clip_changed
624 || current_buffer->prevent_redisplay_optimizations_p
625 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
626 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
627 return Qnil;
629 if (NILP (line))
631 i = w->cursor.vpos;
632 if (i < 0 || i >= w->current_matrix->nrows
633 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
634 return Qnil;
635 max_y = window_text_bottom_y (w);
636 goto found_row;
639 if (EQ (line, Qheader_line))
641 if (!WINDOW_WANTS_HEADER_LINE_P (w))
642 return Qnil;
643 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
644 if (!row->enabled_p)
645 return Qnil;
646 return list4 (make_number (row->height),
647 make_number (0), make_number (0),
648 make_number (0));
651 if (EQ (line, Qmode_line))
653 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
654 if (!row->enabled_p)
655 return Qnil;
656 return list4 (make_number (row->height),
657 make_number (0), /* not accurate */
658 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
659 + window_text_bottom_y (w)),
660 make_number (0));
663 CHECK_NUMBER (line);
664 n = XINT (line);
666 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
667 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
668 max_y = window_text_bottom_y (w);
669 i = 0;
671 while ((n < 0 || i < n)
672 && row <= end_row && row->enabled_p
673 && row->y + row->height < max_y)
674 row++, i++;
676 if (row > end_row || !row->enabled_p)
677 return Qnil;
679 if (++n < 0)
681 if (-n > i)
682 return Qnil;
683 row += n;
684 i += n;
687 found_row:
688 crop = max (0, (row->y + row->height) - max_y);
689 return list4 (make_number (row->height + min (0, row->y) - crop),
690 make_number (i),
691 make_number (row->y),
692 make_number (crop));
695 DEFUN ("window-total-size", Fwindow_total_size, Swindow_total_size, 0, 2, 0,
696 doc: /* Return the total number of lines of WINDOW.
697 WINDOW can be any window and defaults to the selected one. The return
698 value includes WINDOW's mode line and header line, if any. If WINDOW
699 is internal, the return value is the sum of the total number of lines
700 of WINDOW's child windows if these are vertically combined and the
701 height of WINDOW's first child otherwise.
703 Optional argument HORIZONTAL non-nil means return the total number of
704 columns of WINDOW. In this case the return value includes any vertical
705 dividers or scrollbars of WINDOW. If WINDOW is internal, the return
706 value is the sum of the total number of columns of WINDOW's child
707 windows if they are horizontally combined and the width of WINDOW's
708 first child otherwise. */)
709 (Lisp_Object window, Lisp_Object horizontal)
711 if (NILP (horizontal))
712 return decode_any_window (window)->total_lines;
713 else
714 return decode_any_window (window)->total_cols;
717 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
718 doc: /* Return left column of WINDOW.
719 WINDOW can be any window and defaults to the selected one. */)
720 (Lisp_Object window)
722 return decode_any_window (window)->left_col;
725 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
726 doc: /* Return top line of WINDOW.
727 WINDOW can be any window and defaults to the selected one. */)
728 (Lisp_Object window)
730 return decode_any_window (window)->top_line;
733 /* Return the number of lines of W's body. Don't count any mode or
734 header line of W. */
737 window_body_lines (struct window *w)
739 int height = XFASTINT (w->total_lines);
741 if (!MINI_WINDOW_P (w))
743 if (WINDOW_WANTS_MODELINE_P (w))
744 --height;
745 if (WINDOW_WANTS_HEADER_LINE_P (w))
746 --height;
749 return height;
752 /* Return the number of columns of W's body. Don't count columns
753 occupied by the scroll bar or the vertical bar separating W from its
754 right sibling. On window-systems don't count fringes or display
755 margins either. */
758 window_body_cols (struct window *w)
760 struct frame *f = XFRAME (WINDOW_FRAME (w));
761 int width = XINT (w->total_cols);
763 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
764 /* Scroll bars occupy a few columns. */
765 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
766 else if (!FRAME_WINDOW_P (f)
767 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
768 /* The column of `|' characters separating side-by-side windows
769 occupies one column only. */
770 width -= 1;
772 if (FRAME_WINDOW_P (f))
773 /* On window-systems, fringes and display margins cannot be
774 used for normal text. */
775 width -= (WINDOW_FRINGE_COLS (w)
776 + WINDOW_LEFT_MARGIN_COLS (w)
777 + WINDOW_RIGHT_MARGIN_COLS (w));
779 return width;
782 DEFUN ("window-body-size", Fwindow_body_size, Swindow_body_size, 0, 2, 0,
783 doc: /* Return the number of lines of WINDOW's body.
784 WINDOW must be a live window and defaults to the selected one. The
785 return value does not include WINDOW's mode line and header line, if
786 any.
788 Optional argument HORIZONTAL non-nil means return the number of columns
789 of WINDOW's body. In this case, the return value does not include any
790 vertical dividers or scroll bars owned by WINDOW. On a window-system
791 the return value does not include the number of columns used for
792 WINDOW's fringes or display margins either. */)
793 (Lisp_Object window, Lisp_Object horizontal)
795 struct window *w = decode_any_window (window);
797 if (NILP (horizontal))
798 return make_number (window_body_lines (w));
799 else
800 return make_number (window_body_cols (w));
803 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
804 doc: /* Return the number of lines in WINDOW.
805 WINDOW defaults to the selected window.
807 The return value includes WINDOW's mode line and header line, if any.
809 Note: The function does not take into account the value of `line-spacing'
810 when calculating the number of lines in WINDOW. */)
811 (Lisp_Object window)
813 return decode_any_window (window)->total_lines;
816 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
817 doc: /* Return the number of display columns in WINDOW.
818 WINDOW defaults to the selected window.
820 Note: The return value is the number of columns available for text in
821 WINDOW. If you want to find out how many columns WINDOW takes up, use
822 (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
823 (Lisp_Object window)
825 return make_number (window_body_cols (decode_any_window (window)));
828 DEFUN ("window-full-width-p", Fwindow_full_width_p, Swindow_full_width_p, 0, 1, 0,
829 doc: /* Return t if WINDOW is as wide as its frame.
830 WINDOW defaults to the selected window. */)
831 (Lisp_Object window)
833 return WINDOW_FULL_WIDTH_P (decode_any_window (window)) ? Qt : Qnil;
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 defaults to the selected window. */)
839 (Lisp_Object window)
841 return decode_window (window)->hscroll;
844 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
845 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
846 Return NCOL. NCOL should be zero or positive.
848 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
849 window so that the location of point moves off-window. */)
850 (Lisp_Object window, Lisp_Object ncol)
852 struct window *w = decode_window (window);
853 int hscroll;
855 CHECK_NUMBER (ncol);
856 hscroll = max (0, XINT (ncol));
858 /* Prevent redisplay shortcuts when changing the hscroll. */
859 if (XINT (w->hscroll) != hscroll)
860 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
862 w->hscroll = make_number (hscroll);
863 return ncol;
866 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
867 Swindow_redisplay_end_trigger, 0, 1, 0,
868 doc: /* Return WINDOW's redisplay end trigger value.
869 WINDOW defaults to the selected window.
870 See `set-window-redisplay-end-trigger' for more information. */)
871 (Lisp_Object window)
873 return decode_window (window)->redisplay_end_trigger;
876 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
877 Sset_window_redisplay_end_trigger, 2, 2, 0,
878 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
879 VALUE should be a buffer position (typically a marker) or nil.
880 If it is a buffer position, then if redisplay in WINDOW reaches a position
881 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
882 with two arguments: WINDOW, and the end trigger value.
883 Afterwards the end-trigger value is reset to nil. */)
884 (register Lisp_Object window, Lisp_Object value)
886 register struct window *w;
888 w = decode_window (window);
889 w->redisplay_end_trigger = value;
890 return value;
893 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
894 doc: /* Return a list of the edge coordinates of WINDOW.
895 The list has the form (LEFT TOP RIGHT BOTTOM).
896 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
897 all relative to 0, 0 at top left corner of frame.
899 RIGHT is one more than the rightmost column occupied by WINDOW.
900 BOTTOM is one more than the bottommost row occupied by WINDOW.
901 The edges include the space used by WINDOW's scroll bar, display
902 margins, fringes, header line, and/or mode line. For the edges of
903 just the text area, use `window-inside-edges'. */)
904 (Lisp_Object window)
906 register struct window *w = decode_any_window (window);
908 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
909 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
910 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
911 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
912 Qnil))));
915 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
916 doc: /* Return a list of the edge pixel coordinates of WINDOW.
917 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
918 the top left corner of the frame.
920 RIGHT is one more than the rightmost x position occupied by WINDOW.
921 BOTTOM is one more than the bottommost y position occupied by WINDOW.
922 The pixel edges include the space used by WINDOW's scroll bar, display
923 margins, fringes, header line, and/or mode line. For the pixel edges
924 of just the text area, use `window-inside-pixel-edges'. */)
925 (Lisp_Object window)
927 register struct window *w = decode_any_window (window);
929 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
930 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
931 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
932 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
933 Qnil))));
936 static void
937 calc_absolute_offset(struct window *w, int *add_x, int *add_y)
939 struct frame *f = XFRAME (w->frame);
940 *add_y = f->top_pos;
941 #ifdef FRAME_MENUBAR_HEIGHT
942 *add_y += FRAME_MENUBAR_HEIGHT (f);
943 #endif
944 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
945 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
946 #elif FRAME_TOOLBAR_HEIGHT
947 *add_y += FRAME_TOOLBAR_HEIGHT (f);
948 #endif
949 #ifdef FRAME_NS_TITLEBAR_HEIGHT
950 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
951 #endif
952 *add_x = f->left_pos;
953 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
954 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
955 #endif
958 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
959 Swindow_absolute_pixel_edges, 0, 1, 0,
960 doc: /* Return a list of the edge pixel coordinates of WINDOW.
961 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
962 the top left corner of the display.
964 RIGHT is one more than the rightmost x position occupied by WINDOW.
965 BOTTOM is one more than the bottommost y position occupied by WINDOW.
966 The pixel edges include the space used by WINDOW's scroll bar, display
967 margins, fringes, header line, and/or mode line. For the pixel edges
968 of just the text area, use `window-inside-absolute-pixel-edges'. */)
969 (Lisp_Object window)
971 register struct window *w = decode_any_window (window);
972 int add_x, add_y;
973 calc_absolute_offset (w, &add_x, &add_y);
975 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
976 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
977 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
978 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
979 Qnil))));
982 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
983 doc: /* Return a list of the edge coordinates of WINDOW.
984 The list has the form (LEFT TOP RIGHT BOTTOM).
985 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
986 all relative to 0, 0 at top left corner of frame.
988 RIGHT is one more than the rightmost column of WINDOW's text area.
989 BOTTOM is one more than the bottommost row of WINDOW's text area.
990 The inside edges do not include the space used by the WINDOW's scroll
991 bar, display margins, fringes, header line, and/or mode line. */)
992 (Lisp_Object window)
994 register struct window *w = decode_any_window (window);
996 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
997 + WINDOW_LEFT_MARGIN_COLS (w)
998 + WINDOW_LEFT_FRINGE_COLS (w)),
999 make_number (WINDOW_TOP_EDGE_LINE (w)
1000 + WINDOW_HEADER_LINE_LINES (w)),
1001 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
1002 - WINDOW_RIGHT_MARGIN_COLS (w)
1003 - WINDOW_RIGHT_FRINGE_COLS (w)),
1004 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
1005 - WINDOW_MODE_LINE_LINES (w)));
1008 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
1009 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1010 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
1011 the top left corner of the frame.
1013 RIGHT is one more than the rightmost x position of WINDOW's text area.
1014 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1015 The inside edges do not include the space used by WINDOW's scroll bar,
1016 display margins, fringes, header line, and/or mode line. */)
1017 (Lisp_Object window)
1019 register struct window *w = decode_any_window (window);
1021 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
1022 + WINDOW_LEFT_MARGIN_WIDTH (w)
1023 + WINDOW_LEFT_FRINGE_WIDTH (w)),
1024 make_number (WINDOW_TOP_EDGE_Y (w)
1025 + WINDOW_HEADER_LINE_HEIGHT (w)),
1026 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
1027 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1028 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
1029 make_number (WINDOW_BOTTOM_EDGE_Y (w)
1030 - WINDOW_MODE_LINE_HEIGHT (w)));
1033 DEFUN ("window-inside-absolute-pixel-edges",
1034 Fwindow_inside_absolute_pixel_edges,
1035 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
1036 doc: /* Return a list of the edge pixel coordinates of WINDOW.
1037 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
1038 the top left corner of the display.
1040 RIGHT is one more than the rightmost x position of WINDOW's text area.
1041 BOTTOM is one more than the bottommost y position of WINDOW's text area.
1042 The inside edges do not include the space used by WINDOW's scroll bar,
1043 display margins, fringes, header line, and/or mode line. */)
1044 (Lisp_Object window)
1046 register struct window *w = decode_any_window (window);
1047 int add_x, add_y;
1048 calc_absolute_offset (w, &add_x, &add_y);
1050 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
1051 + WINDOW_LEFT_MARGIN_WIDTH (w)
1052 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
1053 make_number (WINDOW_TOP_EDGE_Y (w)
1054 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
1055 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
1056 - WINDOW_RIGHT_MARGIN_WIDTH (w)
1057 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
1058 make_number (WINDOW_BOTTOM_EDGE_Y (w)
1059 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
1062 /* Test if the character at column X, row Y is within window W.
1063 If it is not, return ON_NOTHING;
1064 if it is in the window's text area, return ON_TEXT;
1065 if it is on the window's modeline, return ON_MODE_LINE;
1066 if it is on the border between the window and its right sibling,
1067 return ON_VERTICAL_BORDER.
1068 if it is on a scroll bar, return ON_SCROLL_BAR.
1069 if it is on the window's top line, return ON_HEADER_LINE;
1070 if it is in left or right fringe of the window,
1071 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
1072 if it is in the marginal area to the left/right of the window,
1073 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
1075 X and Y are frame relative pixel coordinates. */
1077 static enum window_part
1078 coordinates_in_window (register struct window *w, int x, int y)
1080 struct frame *f = XFRAME (WINDOW_FRAME (w));
1081 int left_x, right_x;
1082 enum window_part part;
1083 int ux = FRAME_COLUMN_WIDTH (f);
1084 int x0 = WINDOW_LEFT_EDGE_X (w);
1085 int x1 = WINDOW_RIGHT_EDGE_X (w);
1086 /* The width of the area where the vertical line can be dragged.
1087 (Between mode lines for instance. */
1088 int grabbable_width = ux;
1089 int lmargin_width, rmargin_width, text_left, text_right;
1090 int top_y = WINDOW_TOP_EDGE_Y (w);
1091 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
1093 /* Outside any interesting row? */
1094 if (y < top_y || y >= bottom_y)
1095 return ON_NOTHING;
1097 /* In what's below, we subtract 1 when computing right_x because we
1098 want the rightmost pixel, which is given by left_pixel+width-1. */
1099 if (w->pseudo_window_p)
1101 left_x = 0;
1102 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
1104 else
1106 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
1107 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
1110 /* On the mode line or header line? If it's near the start of
1111 the mode or header line of window that's has a horizontal
1112 sibling, say it's on the vertical line. That's to be able
1113 to resize windows horizontally in case we're using toolkit
1114 scroll bars. */
1116 if (WINDOW_WANTS_MODELINE_P (w)
1117 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
1119 part = ON_MODE_LINE;
1121 header_vertical_border_check:
1122 /* We're somewhere on the mode line. We consider the place
1123 between mode lines of horizontally adjacent mode lines
1124 as the vertical border. If scroll bars on the left,
1125 return the right window. */
1126 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
1127 || WINDOW_RIGHTMOST_P (w))
1128 && !WINDOW_LEFTMOST_P (w)
1129 && eabs (x - x0) < grabbable_width)
1130 return ON_VERTICAL_BORDER;
1132 /* Make sure we're not at the rightmost position of a
1133 mode-/header-line and there's yet another window on the
1134 right. (Bug#1372) */
1135 else if ((WINDOW_RIGHTMOST_P (w) || x < x1)
1136 && eabs (x - x1) < grabbable_width)
1137 return ON_VERTICAL_BORDER;
1139 if (x < x0 || x >= x1)
1140 return ON_NOTHING;
1142 return part;
1145 if (WINDOW_WANTS_HEADER_LINE_P (w)
1146 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
1148 part = ON_HEADER_LINE;
1149 goto header_vertical_border_check;
1152 if (x < x0 || x >= x1) return ON_NOTHING;
1154 /* Outside any interesting column? */
1155 if (x < left_x || x > right_x)
1156 return ON_SCROLL_BAR;
1158 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
1159 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
1161 text_left = window_box_left (w, TEXT_AREA);
1162 text_right = text_left + window_box_width (w, TEXT_AREA);
1164 if (FRAME_WINDOW_P (f))
1166 if (!w->pseudo_window_p
1167 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
1168 && !WINDOW_RIGHTMOST_P (w)
1169 && (eabs (x - right_x) < grabbable_width))
1170 return ON_VERTICAL_BORDER;
1172 /* Need to say "x > right_x" rather than >=, since on character
1173 terminals, the vertical line's x coordinate is right_x. */
1174 else if (!w->pseudo_window_p
1175 && !WINDOW_RIGHTMOST_P (w)
1176 && x > right_x - ux)
1177 return ON_VERTICAL_BORDER;
1179 if (x < text_left)
1181 if (lmargin_width > 0
1182 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1183 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
1184 : (x < left_x + lmargin_width)))
1185 return ON_LEFT_MARGIN;
1187 return ON_LEFT_FRINGE;
1190 if (x >= text_right)
1192 if (rmargin_width > 0
1193 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1194 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1195 : (x >= right_x - rmargin_width)))
1196 return ON_RIGHT_MARGIN;
1198 return ON_RIGHT_FRINGE;
1201 /* Everything special ruled out - must be on text area */
1202 return ON_TEXT;
1205 /* Take X is the frame-relative pixel x-coordinate, and return the
1206 x-coordinate relative to part PART of window W. */
1208 window_relative_x_coord (struct window *w, enum window_part part, int x)
1210 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1212 switch (part)
1214 case ON_TEXT:
1215 return x - window_box_left (w, TEXT_AREA);
1217 case ON_LEFT_FRINGE:
1218 return x - left_x;
1220 case ON_RIGHT_FRINGE:
1221 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1223 case ON_LEFT_MARGIN:
1224 return (x - left_x
1225 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1226 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1228 case ON_RIGHT_MARGIN:
1229 return (x + 1
1230 - ((w->pseudo_window_p)
1231 ? WINDOW_TOTAL_WIDTH (w)
1232 : WINDOW_BOX_RIGHT_EDGE_X (w))
1233 + window_box_width (w, RIGHT_MARGIN_AREA)
1234 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1235 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1238 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1239 return 0;
1243 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1244 Scoordinates_in_window_p, 2, 2, 0,
1245 doc: /* Return non-nil if COORDINATES are in WINDOW.
1246 COORDINATES is a cons of the form (X . Y), X and Y being distances
1247 measured in characters from the upper-left corner of the frame.
1248 \(0 . 0) denotes the character in the upper left corner of the
1249 frame.
1250 If COORDINATES are in the text portion of WINDOW,
1251 the coordinates relative to the window are returned.
1252 If they are in the mode line of WINDOW, `mode-line' is returned.
1253 If they are in the top mode line of WINDOW, `header-line' is returned.
1254 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1255 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1256 If they are on the border between WINDOW and its right sibling,
1257 `vertical-line' is returned.
1258 If they are in the windows's left or right marginal areas, `left-margin'\n\
1259 or `right-margin' is returned. */)
1260 (register Lisp_Object coordinates, Lisp_Object window)
1262 struct window *w;
1263 struct frame *f;
1264 int x, y;
1265 Lisp_Object lx, ly;
1267 CHECK_WINDOW (window);
1268 w = XWINDOW (window);
1269 f = XFRAME (w->frame);
1270 CHECK_CONS (coordinates);
1271 lx = Fcar (coordinates);
1272 ly = Fcdr (coordinates);
1273 CHECK_NUMBER_OR_FLOAT (lx);
1274 CHECK_NUMBER_OR_FLOAT (ly);
1275 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1276 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1278 switch (coordinates_in_window (w, x, y))
1280 case ON_NOTHING:
1281 return Qnil;
1283 case ON_TEXT:
1284 /* Convert X and Y to window relative pixel coordinates, and
1285 return the canonical char units. */
1286 x -= window_box_left (w, TEXT_AREA);
1287 y -= WINDOW_TOP_EDGE_Y (w);
1288 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1289 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1291 case ON_MODE_LINE:
1292 return Qmode_line;
1294 case ON_VERTICAL_BORDER:
1295 return Qvertical_line;
1297 case ON_HEADER_LINE:
1298 return Qheader_line;
1300 case ON_LEFT_FRINGE:
1301 return Qleft_fringe;
1303 case ON_RIGHT_FRINGE:
1304 return Qright_fringe;
1306 case ON_LEFT_MARGIN:
1307 return Qleft_margin;
1309 case ON_RIGHT_MARGIN:
1310 return Qright_margin;
1312 case ON_SCROLL_BAR:
1313 /* Historically we are supposed to return nil in this case. */
1314 return Qnil;
1316 default:
1317 abort ();
1322 /* Callback for foreach_window, used in window_from_coordinates.
1323 Check if window W contains coordinates specified by USER_DATA which
1324 is actually a pointer to a struct check_window_data CW.
1326 Check if window W contains coordinates *CW->x and *CW->y. If it
1327 does, return W in *CW->window, as Lisp_Object, and return in
1328 *CW->part the part of the window under coordinates *X,*Y. Return
1329 zero from this function to stop iterating over windows. */
1331 struct check_window_data
1333 Lisp_Object *window;
1334 int x, y;
1335 enum window_part *part;
1338 static int
1339 check_window_containing (struct window *w, void *user_data)
1341 struct check_window_data *cw = (struct check_window_data *) user_data;
1342 enum window_part found;
1343 int continue_p = 1;
1345 found = coordinates_in_window (w, cw->x, cw->y);
1346 if (found != ON_NOTHING)
1348 *cw->part = found;
1349 XSETWINDOW (*cw->window, w);
1350 continue_p = 0;
1353 return continue_p;
1357 /* Find the window containing frame-relative pixel position X/Y and
1358 return it as a Lisp_Object.
1360 If X, Y is on one of the window's special `window_part' elements,
1361 set *PART to the id of that element.
1363 If there is no window under X, Y return nil and leave *PART
1364 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1366 This function was previously implemented with a loop cycling over
1367 windows with Fnext_window, and starting with the frame's selected
1368 window. It turned out that this doesn't work with an
1369 implementation of next_window using Vwindow_list, because
1370 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1371 tree of F when this function is called asynchronously from
1372 note_mouse_highlight. The original loop didn't terminate in this
1373 case. */
1375 Lisp_Object
1376 window_from_coordinates (struct frame *f, int x, int y,
1377 enum window_part *part, int tool_bar_p)
1379 Lisp_Object window;
1380 struct check_window_data cw;
1381 enum window_part dummy;
1383 if (part == 0)
1384 part = &dummy;
1386 window = Qnil;
1387 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1388 foreach_window (f, check_window_containing, &cw);
1390 /* If not found above, see if it's in the tool bar window, if a tool
1391 bar exists. */
1392 if (NILP (window)
1393 && tool_bar_p
1394 && WINDOWP (f->tool_bar_window)
1395 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1396 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1397 != ON_NOTHING))
1399 *part = ON_TEXT;
1400 window = f->tool_bar_window;
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 If omitted, FRAME defaults to the currently selected frame.
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;
1415 if (NILP (frame))
1416 frame = selected_frame;
1417 CHECK_LIVE_FRAME (frame);
1418 f = XFRAME (frame);
1420 /* Check that arguments are integers or floats. */
1421 CHECK_NUMBER_OR_FLOAT (x);
1422 CHECK_NUMBER_OR_FLOAT (y);
1424 return window_from_coordinates (f,
1425 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1426 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1427 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1428 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1429 0, 0);
1432 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1433 doc: /* Return current value of point in WINDOW.
1434 WINDOW defaults to the selected window.
1436 For a nonselected window, this is the value point would have
1437 if that window were selected.
1439 Note that, when WINDOW is the selected window and its buffer
1440 is also currently selected, the value returned is the same as (point).
1441 It would be more strictly correct to return the `top-level' value
1442 of point, outside of any save-excursion forms.
1443 But that is hard to define. */)
1444 (Lisp_Object window)
1446 register struct window *w = decode_window (window);
1448 if (w == XWINDOW (selected_window)
1449 && current_buffer == XBUFFER (w->buffer))
1450 return Fpoint ();
1451 return Fmarker_position (w->pointm);
1454 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1455 doc: /* Return position at which display currently starts in WINDOW.
1456 WINDOW defaults to the selected window.
1457 This is updated by redisplay or by calling `set-window-start'. */)
1458 (Lisp_Object window)
1460 return Fmarker_position (decode_window (window)->start);
1463 /* This is text temporarily removed from the doc string below.
1465 This function returns nil if the position is not currently known.
1466 That happens when redisplay is preempted and doesn't finish.
1467 If in that case you want to compute where the end of the window would
1468 have been if redisplay had finished, do this:
1469 (save-excursion
1470 (goto-char (window-start window))
1471 (vertical-motion (1- (window-height window)) window)
1472 (point))") */
1474 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1475 doc: /* Return position at which display currently ends in WINDOW.
1476 WINDOW defaults to the selected window.
1477 This is updated by redisplay, when it runs to completion.
1478 Simply changing the buffer text or setting `window-start'
1479 does not update this value.
1480 Return nil if there is no recorded value. \(This can happen if the
1481 last redisplay of WINDOW was preempted, and did not finish.)
1482 If UPDATE is non-nil, compute the up-to-date position
1483 if it isn't already recorded. */)
1484 (Lisp_Object window, Lisp_Object update)
1486 Lisp_Object value;
1487 struct window *w = decode_window (window);
1488 Lisp_Object buf;
1489 struct buffer *b;
1491 buf = w->buffer;
1492 CHECK_BUFFER (buf);
1493 b = XBUFFER (buf);
1495 #if 0 /* This change broke some things. We should make it later. */
1496 /* If we don't know the end position, return nil.
1497 The user can compute it with vertical-motion if he wants to.
1498 It would be nicer to do it automatically,
1499 but that's so slow that it would probably bother people. */
1500 if (NILP (w->window_end_valid))
1501 return Qnil;
1502 #endif
1504 if (! NILP (update)
1505 && ! (! NILP (w->window_end_valid)
1506 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1507 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1508 && !noninteractive)
1510 struct text_pos startp;
1511 struct it it;
1512 struct buffer *old_buffer = NULL;
1514 /* Cannot use Fvertical_motion because that function doesn't
1515 cope with variable-height lines. */
1516 if (b != current_buffer)
1518 old_buffer = current_buffer;
1519 set_buffer_internal (b);
1522 /* In case W->start is out of the range, use something
1523 reasonable. This situation occurred when loading a file with
1524 `-l' containing a call to `rmail' with subsequent other
1525 commands. At the end, W->start happened to be BEG, while
1526 rmail had already narrowed the buffer. */
1527 if (XMARKER (w->start)->charpos < BEGV)
1528 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1529 else if (XMARKER (w->start)->charpos > ZV)
1530 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1531 else
1532 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1534 start_display (&it, w, startp);
1535 move_it_vertically (&it, window_box_height (w));
1536 if (it.current_y < it.last_visible_y)
1537 move_it_past_eol (&it);
1538 value = make_number (IT_CHARPOS (it));
1540 if (old_buffer)
1541 set_buffer_internal (old_buffer);
1543 else
1544 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1546 return value;
1549 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1550 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1551 Return POS. */)
1552 (Lisp_Object window, Lisp_Object pos)
1554 register struct window *w = decode_window (window);
1556 CHECK_NUMBER_COERCE_MARKER (pos);
1557 if (w == XWINDOW (selected_window)
1558 && XBUFFER (w->buffer) == current_buffer)
1559 Fgoto_char (pos);
1560 else
1561 set_marker_restricted (w->pointm, pos, w->buffer);
1563 /* We have to make sure that redisplay updates the window to show
1564 the new value of point. */
1565 if (!EQ (window, selected_window))
1566 ++windows_or_buffers_changed;
1568 return pos;
1571 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1572 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1573 WINDOW defaults to the selected window. Return POS.
1574 Optional third arg NOFORCE non-nil inhibits next redisplay from
1575 overriding motion of point in order to display at this exact start. */)
1576 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1578 register struct window *w = decode_window (window);
1580 CHECK_NUMBER_COERCE_MARKER (pos);
1581 set_marker_restricted (w->start, pos, w->buffer);
1582 /* this is not right, but much easier than doing what is right. */
1583 w->start_at_line_beg = Qnil;
1584 if (NILP (noforce))
1585 w->force_start = Qt;
1586 w->update_mode_line = Qt;
1587 XSETFASTINT (w->last_modified, 0);
1588 XSETFASTINT (w->last_overlay_modified, 0);
1589 if (!EQ (window, selected_window))
1590 windows_or_buffers_changed++;
1592 return pos;
1596 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1597 0, 1, 0,
1598 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1599 More precisely, return the value assigned by the last call of
1600 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1601 never called with WINDOW as its argument, or the value set by that
1602 function was internally reset since its last call. WINDOW defaults to
1603 the selected window.
1605 When a window is dedicated to its buffer, `display-buffer' will refrain
1606 from displaying another buffer in it. `get-lru-window' and
1607 `get-largest-window' treat dedicated windows specially.
1608 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1609 `kill-buffer' can delete a dedicated window and the containing frame.
1611 Functions like `set-window-buffer' may change the buffer displayed by a
1612 window, unless that window is "strongly" dedicated to its buffer, that
1613 is the value returned by `window-dedicated-p' is t. */)
1614 (Lisp_Object window)
1616 return decode_window (window)->dedicated;
1619 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1620 Sset_window_dedicated_p, 2, 2, 0,
1621 doc: /* Mark WINDOW as dedicated according to FLAG.
1622 WINDOW defaults to the selected window. FLAG non-nil means mark WINDOW
1623 as dedicated to its buffer. FLAG nil means mark WINDOW as non-dedicated.
1624 Return FLAG.
1626 When a window is dedicated to its buffer, `display-buffer' will refrain
1627 from displaying another buffer in it. `get-lru-window' and
1628 `get-largest-window' treat dedicated windows specially.
1629 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1630 `kill-buffer' can delete a dedicated window and the containing
1631 frame.
1633 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1634 its buffer. Functions like `set-window-buffer' may change the buffer
1635 displayed by a window, unless that window is strongly dedicated to its
1636 buffer. If and when `set-window-buffer' displays another buffer in a
1637 window, it also makes sure that the window is not marked as dedicated. */)
1638 (Lisp_Object window, Lisp_Object flag)
1640 register struct window *w = decode_window (window);
1642 w->dedicated = flag;
1643 return w->dedicated;
1647 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1648 0, 1, 0,
1649 doc: /* Return the parameters of WINDOW and their values.
1650 WINDOW defaults to the selected window. The return value is a list of
1651 elements of the form (PARAMETER . VALUE). */)
1652 (Lisp_Object window)
1654 return Fcopy_alist (decode_window (window)->window_parameters);
1657 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1658 2, 2, 0,
1659 doc: /* Return WINDOW's value for PARAMETER.
1660 WINDOW defaults to the selected window. */)
1661 (Lisp_Object window, Lisp_Object parameter)
1663 Lisp_Object result;
1665 result = Fassq (parameter, decode_window (window)->window_parameters);
1666 return CDR_SAFE (result);
1669 DEFUN ("set-window-parameter", Fset_window_parameter,
1670 Sset_window_parameter, 3, 3, 0,
1671 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1672 WINDOW defaults to the selected window. Return VALUE. */)
1673 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1675 register struct window *w = decode_window (window);
1676 Lisp_Object old_alist_elt;
1678 old_alist_elt = Fassq (parameter, w->window_parameters);
1679 if (NILP (old_alist_elt))
1680 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1681 else
1682 Fsetcdr (old_alist_elt, value);
1683 return value;
1687 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1688 0, 1, 0,
1689 doc: /* Return the display-table that WINDOW is using.
1690 WINDOW defaults to the selected window. */)
1691 (Lisp_Object window)
1693 return decode_window (window)->display_table;
1696 /* Get the display table for use on window W. This is either W's
1697 display table or W's buffer's display table. Ignore the specified
1698 tables if they are not valid; if no valid table is specified,
1699 return 0. */
1701 struct Lisp_Char_Table *
1702 window_display_table (struct window *w)
1704 struct Lisp_Char_Table *dp = NULL;
1706 if (DISP_TABLE_P (w->display_table))
1707 dp = XCHAR_TABLE (w->display_table);
1708 else if (BUFFERP (w->buffer))
1710 struct buffer *b = XBUFFER (w->buffer);
1712 if (DISP_TABLE_P (BVAR (b, display_table)))
1713 dp = XCHAR_TABLE (BVAR (b, display_table));
1714 else if (DISP_TABLE_P (Vstandard_display_table))
1715 dp = XCHAR_TABLE (Vstandard_display_table);
1718 return dp;
1721 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1722 doc: /* Set WINDOW's display-table to TABLE. */)
1723 (register Lisp_Object window, Lisp_Object table)
1725 register struct window *w;
1727 w = decode_window (window);
1728 w->display_table = table;
1729 return table;
1732 static void delete_window (Lisp_Object);
1734 /* Record info on buffer window w is displaying
1735 when it is about to cease to display that buffer. */
1736 static void
1737 unshow_buffer (register struct window *w)
1739 Lisp_Object buf;
1740 struct buffer *b;
1742 buf = w->buffer;
1743 b = XBUFFER (buf);
1744 if (b != XMARKER (w->pointm)->buffer)
1745 abort ();
1747 #if 0
1748 if (w == XWINDOW (selected_window)
1749 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1750 /* Do this except when the selected window's buffer
1751 is being removed from some other window. */
1752 #endif
1753 /* last_window_start records the start position that this buffer
1754 had in the last window to be disconnected from it.
1755 Now that this statement is unconditional,
1756 it is possible for the buffer to be displayed in the
1757 selected window, while last_window_start reflects another
1758 window which was recently showing the same buffer.
1759 Some people might say that might be a good thing. Let's see. */
1760 b->last_window_start = marker_position (w->start);
1762 /* Point in the selected window's buffer
1763 is actually stored in that buffer, and the window's pointm isn't used.
1764 So don't clobber point in that buffer. */
1765 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1766 /* This line helps to fix Horsley's testbug.el bug. */
1767 && !(WINDOWP (BVAR (b, last_selected_window))
1768 && w != XWINDOW (BVAR (b, last_selected_window))
1769 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1770 temp_set_point_both (b,
1771 clip_to_bounds (BUF_BEGV (b),
1772 XMARKER (w->pointm)->charpos,
1773 BUF_ZV (b)),
1774 clip_to_bounds (BUF_BEGV_BYTE (b),
1775 marker_byte_position (w->pointm),
1776 BUF_ZV_BYTE (b)));
1778 if (WINDOWP (BVAR (b, last_selected_window))
1779 && w == XWINDOW (BVAR (b, last_selected_window)))
1780 BVAR (b, last_selected_window) = Qnil;
1783 /* Put replacement into the window structure in place of old. */
1784 static void
1785 replace_window (Lisp_Object old, Lisp_Object replacement)
1787 register Lisp_Object tem;
1788 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1790 /* If OLD is its frame's root_window, then replacement is the new
1791 root_window for that frame. */
1793 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1794 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1796 p->left_col = o->left_col;
1797 p->top_line = o->top_line;
1798 p->total_cols = o->total_cols;
1799 p->total_lines = o->total_lines;
1800 p->desired_matrix = p->current_matrix = 0;
1801 p->vscroll = 0;
1802 memset (&p->cursor, 0, sizeof (p->cursor));
1803 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
1804 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
1805 p->phys_cursor_type = -1;
1806 p->phys_cursor_width = -1;
1807 p->must_be_updated_p = 0;
1808 p->pseudo_window_p = 0;
1809 XSETFASTINT (p->window_end_vpos, 0);
1810 XSETFASTINT (p->window_end_pos, 0);
1811 p->window_end_valid = Qnil;
1812 p->frozen_window_start_p = 0;
1813 p->orig_top_line = p->orig_total_lines = Qnil;
1815 p->next = tem = o->next;
1816 if (!NILP (tem))
1817 XWINDOW (tem)->prev = replacement;
1819 p->prev = tem = o->prev;
1820 if (!NILP (tem))
1821 XWINDOW (tem)->next = replacement;
1823 p->parent = tem = o->parent;
1824 if (!NILP (tem))
1826 if (EQ (XWINDOW (tem)->vchild, old))
1827 XWINDOW (tem)->vchild = replacement;
1828 if (EQ (XWINDOW (tem)->hchild, old))
1829 XWINDOW (tem)->hchild = replacement;
1832 /*** Here, if replacement is a vertical combination
1833 and so is its new parent, we should make replacement's
1834 children be children of that parent instead. ***/
1837 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1838 doc: /* Remove WINDOW from its frame.
1839 WINDOW defaults to the selected window. Return nil.
1840 Signal an error when WINDOW is the only window on its frame. */)
1841 (register Lisp_Object window)
1843 struct frame *f;
1844 if (NILP (window))
1845 window = selected_window;
1846 else
1847 CHECK_LIVE_WINDOW (window);
1849 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1850 delete_window (window);
1852 run_window_configuration_change_hook (f);
1854 return Qnil;
1857 static void
1858 delete_window (register Lisp_Object window)
1860 register Lisp_Object tem, parent, sib;
1861 register struct window *p;
1862 register struct window *par;
1863 struct frame *f;
1865 /* Because this function is called by other C code on non-leaf
1866 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1867 so we can't decode_window here. */
1868 CHECK_WINDOW (window);
1869 p = XWINDOW (window);
1871 /* It's a no-op to delete an already-deleted window. */
1872 if (NILP (p->buffer)
1873 && NILP (p->hchild)
1874 && NILP (p->vchild))
1875 return;
1877 parent = p->parent;
1878 if (NILP (parent))
1879 error ("Attempt to delete minibuffer or sole ordinary window");
1880 par = XWINDOW (parent);
1882 windows_or_buffers_changed++;
1883 Vwindow_list = Qnil;
1884 f = XFRAME (WINDOW_FRAME (p));
1885 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1887 /* Are we trying to delete any frame's selected window? */
1889 Lisp_Object swindow, pwindow;
1891 /* See if the frame's selected window is either WINDOW
1892 or any subwindow of it, by finding all that window's parents
1893 and comparing each one with WINDOW. */
1894 swindow = FRAME_SELECTED_WINDOW (f);
1896 while (1)
1898 pwindow = swindow;
1899 while (!NILP (pwindow))
1901 if (EQ (window, pwindow))
1902 break;
1903 pwindow = XWINDOW (pwindow)->parent;
1906 /* If the window being deleted is not a parent of SWINDOW,
1907 then SWINDOW is ok as the new selected window. */
1908 if (!EQ (window, pwindow))
1909 break;
1910 /* Otherwise, try another window for SWINDOW. */
1911 swindow = Fnext_window (swindow, Qlambda, Qnil);
1913 /* If we get back to the frame's selected window,
1914 it means there was no acceptable alternative,
1915 so we cannot delete. */
1916 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1917 error ("Cannot delete window");
1920 /* If we need to change SWINDOW, do it. */
1921 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1923 /* If we're about to delete the selected window on the
1924 selected frame, then we should use Fselect_window to select
1925 the new window. On the other hand, if we're about to
1926 delete the selected window on any other frame, we shouldn't do
1927 anything but set the frame's selected_window slot. */
1928 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1929 Fselect_window (swindow, Qnil);
1930 else
1931 FRAME_SELECTED_WINDOW (f) = swindow;
1935 /* Now we know we can delete this one. */
1936 window_deletion_count++;
1938 tem = p->buffer;
1939 /* tem is null for dummy parent windows
1940 (which have inferiors but not any contents themselves) */
1941 if (!NILP (tem))
1943 unshow_buffer (p);
1944 unchain_marker (XMARKER (p->pointm));
1945 unchain_marker (XMARKER (p->start));
1948 /* Free window glyph matrices. It is sure that they are allocated
1949 again when ADJUST_GLYPHS is called. Block input so that expose
1950 events and other events that access glyph matrices are not
1951 processed while we are changing them. */
1952 BLOCK_INPUT;
1953 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1955 tem = p->next;
1956 if (!NILP (tem))
1957 XWINDOW (tem)->prev = p->prev;
1959 tem = p->prev;
1960 if (!NILP (tem))
1961 XWINDOW (tem)->next = p->next;
1963 if (EQ (window, par->hchild))
1964 par->hchild = p->next;
1965 if (EQ (window, par->vchild))
1966 par->vchild = p->next;
1968 /* Find one of our siblings to give our space to. */
1969 sib = p->prev;
1970 if (NILP (sib))
1972 /* If p gives its space to its next sibling, that sibling needs
1973 to have its top/left side pulled back to where p's is.
1974 set_window_{height,width} will re-position the sibling's
1975 children. */
1976 sib = p->next;
1977 XWINDOW (sib)->top_line = p->top_line;
1978 XWINDOW (sib)->left_col = p->left_col;
1981 /* Stretch that sibling. */
1982 if (!NILP (par->vchild))
1983 set_window_height (sib,
1984 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1986 if (!NILP (par->hchild))
1987 set_window_width (sib,
1988 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1991 /* If parent now has only one child,
1992 put the child into the parent's place. */
1993 tem = par->hchild;
1994 if (NILP (tem))
1995 tem = par->vchild;
1996 if (NILP (XWINDOW (tem)->next)) {
1997 replace_window (parent, tem);
1998 par = XWINDOW (tem);
2001 /* Since we may be deleting combination windows, we must make sure that
2002 not only p but all its children have been marked as deleted. */
2003 if (! NILP (p->hchild))
2004 delete_all_subwindows (XWINDOW (p->hchild));
2005 else if (! NILP (p->vchild))
2006 delete_all_subwindows (XWINDOW (p->vchild));
2008 /* Mark this window as deleted. */
2009 p->buffer = p->hchild = p->vchild = Qnil;
2011 if (! NILP (par->parent))
2012 par = XWINDOW (par->parent);
2014 /* Check if we have a v/hchild with a v/hchild. In that case remove
2015 one of them. */
2017 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
2019 p = XWINDOW (par->vchild);
2020 par->vchild = p->vchild;
2021 tem = p->vchild;
2023 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
2025 p = XWINDOW (par->hchild);
2026 par->hchild = p->hchild;
2027 tem = p->hchild;
2029 else
2030 p = 0;
2032 if (p)
2034 while (! NILP (tem)) {
2035 XWINDOW (tem)->parent = p->parent;
2036 if (NILP (XWINDOW (tem)->next))
2037 break;
2038 tem = XWINDOW (tem)->next;
2040 if (! NILP (tem)) {
2041 /* The next of the v/hchild we are removing is now the next of the
2042 last child for the v/hchild:
2043 Before v/hchild -> v/hchild -> next1 -> next2
2045 -> next3
2046 After: v/hchild -> next1 -> next2 -> next3
2048 XWINDOW (tem)->next = p->next;
2049 if (! NILP (p->next))
2050 XWINDOW (p->next)->prev = tem;
2052 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
2056 /* Adjust glyph matrices. */
2057 adjust_glyphs (f);
2058 UNBLOCK_INPUT;
2063 /***********************************************************************
2064 Window List
2065 ***********************************************************************/
2067 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
2068 pointer. This is a callback function for foreach_window, used in
2069 function window_list. */
2071 static int
2072 add_window_to_list (struct window *w, void *user_data)
2074 Lisp_Object *list = (Lisp_Object *) user_data;
2075 Lisp_Object window;
2076 XSETWINDOW (window, w);
2077 *list = Fcons (window, *list);
2078 return 1;
2082 /* Return a list of all windows, for use by next_window. If
2083 Vwindow_list is a list, return that list. Otherwise, build a new
2084 list, cache it in Vwindow_list, and return that. */
2086 static Lisp_Object
2087 window_list (void)
2089 if (!CONSP (Vwindow_list))
2091 Lisp_Object tail;
2093 Vwindow_list = Qnil;
2094 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
2096 Lisp_Object args[2];
2098 /* We are visiting windows in canonical order, and add
2099 new windows at the front of args[1], which means we
2100 have to reverse this list at the end. */
2101 args[1] = Qnil;
2102 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
2103 args[0] = Vwindow_list;
2104 args[1] = Fnreverse (args[1]);
2105 Vwindow_list = Fnconc (2, args);
2109 return Vwindow_list;
2113 /* Value is non-zero if WINDOW satisfies the constraints given by
2114 OWINDOW, MINIBUF and ALL_FRAMES.
2116 MINIBUF t means WINDOW may be minibuffer windows.
2117 `lambda' means WINDOW may not be a minibuffer window.
2118 a window means a specific minibuffer window
2120 ALL_FRAMES t means search all frames,
2121 nil means search just current frame,
2122 `visible' means search just visible frames on the
2123 current terminal,
2124 0 means search visible and iconified frames on the
2125 current terminal,
2126 a window means search the frame that window belongs to,
2127 a frame means consider windows on that frame, only. */
2129 static int
2130 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
2132 struct window *w = XWINDOW (window);
2133 struct frame *f = XFRAME (w->frame);
2134 int candidate_p = 1;
2136 if (!BUFFERP (w->buffer))
2137 candidate_p = 0;
2138 else if (MINI_WINDOW_P (w)
2139 && (EQ (minibuf, Qlambda)
2140 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2142 /* If MINIBUF is `lambda' don't consider any mini-windows.
2143 If it is a window, consider only that one. */
2144 candidate_p = 0;
2146 else if (EQ (all_frames, Qt))
2147 candidate_p = 1;
2148 else if (NILP (all_frames))
2150 xassert (WINDOWP (owindow));
2151 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2153 else if (EQ (all_frames, Qvisible))
2155 FRAME_SAMPLE_VISIBILITY (f);
2156 candidate_p = FRAME_VISIBLE_P (f)
2157 && (FRAME_TERMINAL (XFRAME (w->frame))
2158 == FRAME_TERMINAL (XFRAME (selected_frame)));
2161 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2163 FRAME_SAMPLE_VISIBILITY (f);
2164 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2165 #ifdef HAVE_X_WINDOWS
2166 /* Yuck!! If we've just created the frame and the
2167 window-manager requested the user to place it
2168 manually, the window may still not be considered
2169 `visible'. I'd argue it should be at least
2170 something like `iconified', but don't know how to do
2171 that yet. --Stef */
2172 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2173 && !f->output_data.x->has_been_visible)
2174 #endif
2176 && (FRAME_TERMINAL (XFRAME (w->frame))
2177 == FRAME_TERMINAL (XFRAME (selected_frame)));
2179 else if (WINDOWP (all_frames))
2180 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2181 || EQ (XWINDOW (all_frames)->frame, w->frame)
2182 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2183 else if (FRAMEP (all_frames))
2184 candidate_p = EQ (all_frames, w->frame);
2186 return candidate_p;
2190 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2191 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2192 MINIBUF, and ALL_FRAMES. */
2194 static void
2195 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2197 if (NILP (*window))
2198 *window = selected_window;
2199 else
2200 CHECK_LIVE_WINDOW (*window);
2202 /* MINIBUF nil may or may not include minibuffers. Decide if it
2203 does. */
2204 if (NILP (*minibuf))
2205 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2206 else if (!EQ (*minibuf, Qt))
2207 *minibuf = Qlambda;
2209 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2210 => count none of them, or a specific minibuffer window (the
2211 active one) to count. */
2213 /* ALL_FRAMES nil doesn't specify which frames to include. */
2214 if (NILP (*all_frames))
2215 *all_frames = (!EQ (*minibuf, Qlambda)
2216 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
2217 : Qnil);
2218 else if (EQ (*all_frames, Qvisible))
2220 else if (EQ (*all_frames, make_number (0)))
2222 else if (FRAMEP (*all_frames))
2224 else if (!EQ (*all_frames, Qt))
2225 *all_frames = Qnil;
2229 /* Return the next or previous window of WINDOW in cyclic ordering
2230 of windows. NEXT_P non-zero means return the next window. See the
2231 documentation string of next-window for the meaning of MINIBUF and
2232 ALL_FRAMES. */
2234 static Lisp_Object
2235 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2237 decode_next_window_args (&window, &minibuf, &all_frames);
2239 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2240 return the first window on the frame. */
2241 if (FRAMEP (all_frames)
2242 && !EQ (all_frames, XWINDOW (window)->frame))
2243 return Fframe_first_window (all_frames);
2245 if (next_p)
2247 Lisp_Object list;
2249 /* Find WINDOW in the list of all windows. */
2250 list = Fmemq (window, window_list ());
2252 /* Scan forward from WINDOW to the end of the window list. */
2253 if (CONSP (list))
2254 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2255 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2256 break;
2258 /* Scan from the start of the window list up to WINDOW. */
2259 if (!CONSP (list))
2260 for (list = Vwindow_list;
2261 CONSP (list) && !EQ (XCAR (list), window);
2262 list = XCDR (list))
2263 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2264 break;
2266 if (CONSP (list))
2267 window = XCAR (list);
2269 else
2271 Lisp_Object candidate, list;
2273 /* Scan through the list of windows for candidates. If there are
2274 candidate windows in front of WINDOW, the last one of these
2275 is the one we want. If there are candidates following WINDOW
2276 in the list, again the last one of these is the one we want. */
2277 candidate = Qnil;
2278 for (list = window_list (); CONSP (list); list = XCDR (list))
2280 if (EQ (XCAR (list), window))
2282 if (WINDOWP (candidate))
2283 break;
2285 else if (candidate_window_p (XCAR (list), window, minibuf,
2286 all_frames))
2287 candidate = XCAR (list);
2290 if (WINDOWP (candidate))
2291 window = candidate;
2294 return window;
2298 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2299 doc: /* Return window following WINDOW in cyclic ordering of windows.
2300 WINDOW defaults to the selected window. The optional arguments
2301 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2303 MINIBUF t means consider the minibuffer window even if the
2304 minibuffer is not active. MINIBUF nil or omitted means consider
2305 the minibuffer window only if the minibuffer is active. Any
2306 other value means do not consider the minibuffer window even if
2307 the minibuffer is active.
2309 Several frames may share a single minibuffer; if the minibuffer
2310 is active, all windows on all frames that share that minibuffer
2311 are considered too. Therefore, if you are using a separate
2312 minibuffer frame and the minibuffer is active and MINIBUF says it
2313 counts, `next-window' considers the windows in the frame from
2314 which you entered the minibuffer, as well as the minibuffer
2315 window.
2317 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
2318 frame, plus the minibuffer window if specified by the MINIBUF
2319 argument, see above. If the minibuffer counts, consider all
2320 windows on all frames that share that minibuffer too.
2321 ALL-FRAMES t means consider all windows on all existing frames.
2322 ALL-FRAMES `visible' means consider all windows on all visible
2323 frames on the current terminal.
2324 ALL-FRAMES 0 means consider all windows on all visible and
2325 iconified frames on the current terminal.
2326 ALL-FRAMES a frame means consider all windows on that frame only.
2327 Anything else means consider all windows on WINDOW's frame and no
2328 others.
2330 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2331 `next-window' to iterate through the entire cycle of acceptable
2332 windows, eventually ending up back at the window you started with.
2333 `previous-window' traverses the same cycle, in the reverse order. */)
2334 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2336 return next_window (window, minibuf, all_frames, 1);
2340 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2341 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
2342 WINDOW defaults to the selected window. The optional arguments
2343 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2344 For the precise meaning of these arguments see `next-window'.
2346 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2347 use `previous-window' to iterate through the entire cycle of
2348 acceptable windows, eventually ending up back at the window you
2349 started with. `next-window' traverses the same cycle, in the
2350 reverse order. */)
2351 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2353 return next_window (window, minibuf, all_frames, 0);
2357 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2358 doc: /* Return a list of all live windows.
2359 WINDOW specifies the first window to list and defaults to the selected
2360 window.
2362 Optional argument MINIBUF nil or omitted means consider the minibuffer
2363 window only if the minibuffer is active. MINIBUF t means consider the
2364 minibuffer window even if the minibuffer is not active. Any other value
2365 means do not consider the minibuffer window even if the minibuffer is
2366 active.
2368 Optional argument ALL-FRAMES nil or omitted means consider all windows
2369 on WINDOW's frame, plus the minibuffer window if specified by the
2370 MINIBUF argument. If the minibuffer counts, consider all windows on all
2371 frames that share that minibuffer too. The following non-nil values of
2372 ALL-FRAMES have special meanings:
2374 - t means consider all windows on all existing frames.
2376 - `visible' means consider all windows on all visible frames.
2378 - 0 (the number zero) means consider all windows on all visible and
2379 iconified frames.
2381 - A frame means consider all windows on that frame only.
2383 Anything else means consider all windows on WINDOW's frame and no
2384 others.
2386 If WINDOW is not on the list of windows returned, some other window will
2387 be listed first but no error is signalled. */)
2388 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2390 return window_list_1 (window, minibuf, all_frames);
2394 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
2395 doc: /* Select another window in cyclic ordering of windows.
2396 COUNT specifies the number of windows to skip, starting with the
2397 selected window, before making the selection. If COUNT is
2398 positive, skip COUNT windows forwards. If COUNT is negative,
2399 skip -COUNT windows backwards. COUNT zero means do not skip any
2400 window, so select the selected window. In an interactive call,
2401 COUNT is the numeric prefix argument. Return nil.
2403 This function uses `next-window' for finding the window to select.
2404 The argument ALL-FRAMES has the same meaning as in `next-window',
2405 but the MINIBUF argument of `next-window' is always effectively
2406 nil. */)
2407 (Lisp_Object count, Lisp_Object all_frames)
2409 Lisp_Object window;
2410 int i;
2412 CHECK_NUMBER (count);
2413 window = selected_window;
2415 for (i = XINT (count); i > 0; --i)
2416 window = Fnext_window (window, Qnil, all_frames);
2417 for (; i < 0; ++i)
2418 window = Fprevious_window (window, Qnil, all_frames);
2420 Fselect_window (window, Qnil);
2421 return Qnil;
2425 /* Return a list of windows in cyclic ordering. Arguments are like
2426 for `next-window'. */
2428 static Lisp_Object
2429 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2431 Lisp_Object tail, list, rest;
2433 decode_next_window_args (&window, &minibuf, &all_frames);
2434 list = Qnil;
2436 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2437 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2438 list = Fcons (XCAR (tail), list);
2440 /* Rotate the list to start with WINDOW. */
2441 list = Fnreverse (list);
2442 rest = Fmemq (window, list);
2443 if (!NILP (rest) && !EQ (rest, list))
2445 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2447 XSETCDR (tail, Qnil);
2448 list = nconc2 (rest, list);
2450 return list;
2454 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2455 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2456 FRAME nil or omitted means use the selected frame.
2457 WINDOW nil or omitted means use the selected window.
2458 MINIBUF t means include the minibuffer window, even if it isn't active.
2459 MINIBUF nil or omitted means include the minibuffer window only
2460 if it's active.
2461 MINIBUF neither nil nor t means never include the minibuffer window. */)
2462 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2464 if (NILP (window))
2465 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2466 CHECK_WINDOW (window);
2467 if (NILP (frame))
2468 frame = selected_frame;
2470 if (!EQ (frame, XWINDOW (window)->frame))
2471 error ("Window is on a different frame");
2473 return window_list_1 (window, minibuf, frame);
2478 /* Look at all windows, performing an operation specified by TYPE
2479 with argument OBJ.
2480 If FRAMES is Qt, look at all frames;
2481 Qnil, look at just the selected frame;
2482 Qvisible, look at visible frames;
2483 a frame, just look at windows on that frame.
2484 If MINI is non-zero, perform the operation on minibuffer windows too. */
2486 enum window_loop
2488 WINDOW_LOOP_UNUSED,
2489 GET_BUFFER_WINDOW, /* Arg is buffer */
2490 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2491 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2492 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2493 GET_LARGEST_WINDOW,
2494 UNSHOW_BUFFER, /* Arg is buffer */
2495 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2496 CHECK_ALL_WINDOWS
2499 static Lisp_Object
2500 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2502 Lisp_Object window, windows, best_window, frame_arg;
2503 struct frame *f;
2504 struct gcpro gcpro1;
2506 /* If we're only looping through windows on a particular frame,
2507 frame points to that frame. If we're looping through windows
2508 on all frames, frame is 0. */
2509 if (FRAMEP (frames))
2510 f = XFRAME (frames);
2511 else if (NILP (frames))
2512 f = SELECTED_FRAME ();
2513 else
2514 f = NULL;
2516 if (f)
2517 frame_arg = Qlambda;
2518 else if (EQ (frames, make_number (0)))
2519 frame_arg = frames;
2520 else if (EQ (frames, Qvisible))
2521 frame_arg = frames;
2522 else
2523 frame_arg = Qt;
2525 /* frame_arg is Qlambda to stick to one frame,
2526 Qvisible to consider all visible frames,
2527 or Qt otherwise. */
2529 /* Pick a window to start with. */
2530 if (WINDOWP (obj))
2531 window = obj;
2532 else if (f)
2533 window = FRAME_SELECTED_WINDOW (f);
2534 else
2535 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2537 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2538 GCPRO1 (windows);
2539 best_window = Qnil;
2541 for (; CONSP (windows); windows = XCDR (windows))
2543 struct window *w;
2545 window = XCAR (windows);
2546 w = XWINDOW (window);
2548 /* Note that we do not pay attention here to whether the frame
2549 is visible, since Fwindow_list skips non-visible frames if
2550 that is desired, under the control of frame_arg. */
2551 if (!MINI_WINDOW_P (w)
2552 /* For UNSHOW_BUFFER, we must always consider all windows. */
2553 || type == UNSHOW_BUFFER
2554 || (mini && minibuf_level > 0))
2555 switch (type)
2557 case GET_BUFFER_WINDOW:
2558 if (EQ (w->buffer, obj)
2559 /* Don't find any minibuffer window
2560 except the one that is currently in use. */
2561 && (MINI_WINDOW_P (w)
2562 ? EQ (window, minibuf_window)
2563 : 1))
2565 if (NILP (best_window))
2566 best_window = window;
2567 else if (EQ (window, selected_window))
2568 /* Prefer to return selected-window. */
2569 RETURN_UNGCPRO (window);
2570 else if (EQ (Fwindow_frame (window), selected_frame))
2571 /* Prefer windows on the current frame. */
2572 best_window = window;
2574 break;
2576 case GET_LRU_WINDOW:
2577 /* `obj' is an integer encoding a bitvector.
2578 `obj & 1' means consider only full-width windows.
2579 `obj & 2' means consider also dedicated windows. */
2580 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2581 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2582 /* Minibuffer windows are always ignored. */
2583 || MINI_WINDOW_P (w))
2584 break;
2585 if (NILP (best_window)
2586 || (XFASTINT (XWINDOW (best_window)->use_time)
2587 > XFASTINT (w->use_time)))
2588 best_window = window;
2589 break;
2591 case DELETE_OTHER_WINDOWS:
2592 if (!EQ (window, obj))
2593 Fdelete_window (window);
2594 break;
2596 case DELETE_BUFFER_WINDOWS:
2597 if (EQ (w->buffer, obj))
2599 struct frame *fr = XFRAME (WINDOW_FRAME (w));
2601 /* If this window is dedicated, and in a frame of its own,
2602 kill the frame. */
2603 if (EQ (window, FRAME_ROOT_WINDOW (fr))
2604 && !NILP (w->dedicated)
2605 && other_visible_frames (fr))
2607 /* Skip the other windows on this frame.
2608 There might be one, the minibuffer! */
2609 while (CONSP (XCDR (windows))
2610 && EQ (XWINDOW (XCAR (windows))->frame,
2611 XWINDOW (XCAR (XCDR (windows)))->frame))
2612 windows = XCDR (windows);
2614 /* Now we can safely delete the frame. */
2615 delete_frame (w->frame, Qnil);
2617 else if (NILP (w->parent))
2619 /* If we're deleting the buffer displayed in the
2620 only window on the frame, find a new buffer to
2621 display there. */
2622 Lisp_Object buffer;
2623 buffer = Fother_buffer (obj, Qnil, w->frame);
2624 /* Reset dedicated state of window. */
2625 w->dedicated = Qnil;
2626 Fset_window_buffer (window, buffer, Qnil);
2627 if (EQ (window, selected_window))
2628 Fset_buffer (w->buffer);
2630 else
2631 Fdelete_window (window);
2633 break;
2635 case GET_LARGEST_WINDOW:
2636 { /* nil `obj' means to ignore dedicated windows. */
2637 /* Ignore dedicated windows and minibuffers. */
2638 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2639 break;
2641 if (NILP (best_window))
2642 best_window = window;
2643 else
2645 struct window *b = XWINDOW (best_window);
2646 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2647 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2648 best_window = window;
2651 break;
2653 case UNSHOW_BUFFER:
2654 if (EQ (w->buffer, obj))
2656 Lisp_Object buffer;
2657 struct frame *fr = XFRAME (w->frame);
2659 /* Find another buffer to show in this window. */
2660 buffer = Fother_buffer (obj, Qnil, w->frame);
2662 /* If this window is dedicated, and in a frame of its own,
2663 kill the frame. */
2664 if (EQ (window, FRAME_ROOT_WINDOW (fr))
2665 && !NILP (w->dedicated)
2666 && other_visible_frames (fr))
2668 /* Skip the other windows on this frame.
2669 There might be one, the minibuffer! */
2670 while (CONSP (XCDR (windows))
2671 && EQ (XWINDOW (XCAR (windows))->frame,
2672 XWINDOW (XCAR (XCDR (windows)))->frame))
2673 windows = XCDR (windows);
2675 /* Now we can safely delete the frame. */
2676 delete_frame (w->frame, Qnil);
2678 else if (!NILP (w->dedicated) && !NILP (w->parent))
2680 Lisp_Object window_to_delete;
2681 XSETWINDOW (window_to_delete, w);
2682 /* If this window is dedicated and not the only window
2683 in its frame, then kill it. */
2684 Fdelete_window (window_to_delete);
2686 else
2688 /* Otherwise show a different buffer in the window. */
2689 w->dedicated = Qnil;
2690 Fset_window_buffer (window, buffer, Qnil);
2691 if (EQ (window, selected_window))
2692 Fset_buffer (w->buffer);
2695 break;
2697 case REDISPLAY_BUFFER_WINDOWS:
2698 if (EQ (w->buffer, obj))
2700 mark_window_display_accurate (window, 0);
2701 w->update_mode_line = Qt;
2702 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2703 ++update_mode_lines;
2704 best_window = window;
2706 break;
2708 /* Check for a window that has a killed buffer. */
2709 case CHECK_ALL_WINDOWS:
2710 if (! NILP (w->buffer)
2711 && NILP (BVAR (XBUFFER (w->buffer), name)))
2712 abort ();
2713 break;
2715 case WINDOW_LOOP_UNUSED:
2716 break;
2720 UNGCPRO;
2721 return best_window;
2724 /* Used for debugging. Abort if any window has a dead buffer. */
2726 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2727 void
2728 check_all_windows (void)
2730 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2733 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
2734 doc: /* Return WINDOW's use time.
2735 WINDOW defaults to the selected window. The window with the highest use
2736 time is the most recently selected one. The window with the lowest use
2737 time is the least recently selected one. */)
2738 (Lisp_Object window)
2740 return decode_window (window)->use_time;
2743 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2744 doc: /* Return the window least recently selected or used for display.
2745 \(LRU means Least Recently Used.)
2747 Return a full-width window if possible.
2748 A minibuffer window is never a candidate.
2749 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2750 so if all windows are dedicated, the value is nil.
2751 If optional argument FRAME is `visible', search all visible frames.
2752 If FRAME is 0, search all visible and iconified frames.
2753 If FRAME is t, search all frames.
2754 If FRAME is nil, search only the selected frame.
2755 If FRAME is a frame, search only that frame. */)
2756 (Lisp_Object frame, Lisp_Object dedicated)
2758 register Lisp_Object w;
2759 /* First try for a window that is full-width */
2760 w = window_loop (GET_LRU_WINDOW,
2761 NILP (dedicated) ? make_number (1) : make_number (3),
2762 0, frame);
2763 if (!NILP (w) && !EQ (w, selected_window))
2764 return w;
2765 /* If none of them, try the rest */
2766 return window_loop (GET_LRU_WINDOW,
2767 NILP (dedicated) ? make_number (0) : make_number (2),
2768 0, frame);
2771 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2772 doc: /* Return the largest window in area.
2773 A minibuffer window is never a candidate.
2774 A dedicated window is never a candidate unless DEDICATED is non-nil,
2775 so if all windows are dedicated, the value is nil.
2776 If optional argument FRAME is `visible', search all visible frames.
2777 If FRAME is 0, search all visible and iconified frames.
2778 If FRAME is t, search all frames.
2779 If FRAME is nil, search only the selected frame.
2780 If FRAME is a frame, search only that frame. */)
2781 (Lisp_Object frame, Lisp_Object dedicated)
2783 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2784 frame);
2787 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2788 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2789 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to the
2790 current buffer.
2791 If optional argument FRAME is `visible', search all visible frames.
2792 If optional argument FRAME is 0, search all visible and iconified frames.
2793 If FRAME is t, search all frames.
2794 If FRAME is nil, search only the selected frame.
2795 If FRAME is a frame, search only that frame. */)
2796 (Lisp_Object buffer_or_name, Lisp_Object frame)
2798 Lisp_Object buffer;
2800 if (NILP (buffer_or_name))
2801 buffer = Fcurrent_buffer ();
2802 else
2803 buffer = Fget_buffer (buffer_or_name);
2805 if (BUFFERP (buffer))
2806 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2807 else
2808 return Qnil;
2811 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2812 0, 1, "",
2813 doc: /* Make WINDOW (or the selected window) fill its frame.
2814 Only the frame WINDOW is on is affected.
2815 This function tries to reduce display jumps by keeping the text
2816 previously visible in WINDOW in the same place on the frame. Doing this
2817 depends on the value of (window-start WINDOW), so if calling this
2818 function in a program gives strange scrolling, make sure the
2819 window-start value is reasonable when this function is called. */)
2820 (Lisp_Object window)
2822 struct window *w;
2823 EMACS_INT startpos;
2824 int top, new_top;
2826 if (NILP (window))
2827 window = selected_window;
2828 else
2829 CHECK_LIVE_WINDOW (window);
2830 w = XWINDOW (window);
2832 startpos = marker_position (w->start);
2833 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2835 if (MINI_WINDOW_P (w) && top > 0)
2836 error ("Can't expand minibuffer to full frame");
2838 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2840 /* Try to minimize scrolling, by setting the window start to the point
2841 will cause the text at the old window start to be at the same place
2842 on the frame. But don't try to do this if the window start is
2843 outside the visible portion (as might happen when the display is
2844 not current, due to typeahead). */
2845 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2846 if (new_top != top
2847 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2848 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2850 struct position pos;
2851 struct buffer *obuf = current_buffer;
2853 Fset_buffer (w->buffer);
2854 /* This computation used to temporarily move point, but that can
2855 have unwanted side effects due to text properties. */
2856 pos = *vmotion (startpos, -top, w);
2858 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2859 w->window_end_valid = Qnil;
2860 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2861 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2862 : Qnil);
2863 /* We need to do this, so that the window-scroll-functions
2864 get called. */
2865 w->optional_new_start = Qt;
2867 set_buffer_internal (obuf);
2870 return Qnil;
2873 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2874 0, 2, "bDelete windows on (buffer): ",
2875 doc: /* Delete all windows showing BUFFER-OR-NAME.
2876 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2877 defaults to the current buffer.
2879 Optional second argument FRAME controls which frames are affected.
2880 If optional argument FRAME is `visible', search all visible frames.
2881 If FRAME is 0, search all visible and iconified frames.
2882 If FRAME is nil, search all frames.
2883 If FRAME is t, search only the selected frame.
2884 If FRAME is a frame, search only that frame.
2885 When a window showing BUFFER-OR-NAME is dedicated and the only window of
2886 its frame, that frame is deleted when there are other frames left. */)
2887 (Lisp_Object buffer_or_name, Lisp_Object frame)
2889 Lisp_Object buffer;
2891 /* FRAME uses t and nil to mean the opposite of what window_loop
2892 expects. */
2893 if (NILP (frame))
2894 frame = Qt;
2895 else if (EQ (frame, Qt))
2896 frame = Qnil;
2898 if (NILP (buffer_or_name))
2899 buffer = Fcurrent_buffer ();
2900 else
2902 buffer = Fget_buffer (buffer_or_name);
2903 CHECK_BUFFER (buffer);
2906 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2908 return Qnil;
2911 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2912 Sreplace_buffer_in_windows,
2913 0, 1, "bReplace buffer in windows: ",
2914 doc: /* Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2915 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2916 defaults to the current buffer.
2918 When a window showing BUFFER-OR-NAME is dedicated that window is
2919 deleted. If that window is the only window on its frame, that frame is
2920 deleted too when there are other frames left. If there are no other
2921 frames left, some other buffer is displayed in that window. */)
2922 (Lisp_Object buffer_or_name)
2924 Lisp_Object buffer;
2926 if (NILP (buffer_or_name))
2927 buffer = Fcurrent_buffer ();
2928 else
2930 buffer = Fget_buffer (buffer_or_name);
2931 CHECK_BUFFER (buffer);
2934 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2936 return Qnil;
2939 /* Replace BUFFER with some other buffer in all windows
2940 of all frames, even those on other keyboards. */
2942 void
2943 replace_buffer_in_all_windows (Lisp_Object buffer)
2945 Lisp_Object tail, frame;
2947 /* A single call to window_loop won't do the job
2948 because it only considers frames on the current keyboard.
2949 So loop manually over frames, and handle each one. */
2950 FOR_EACH_FRAME (tail, frame)
2951 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2954 /* Set the height of WINDOW and all its inferiors. */
2956 /* The smallest acceptable dimensions for a window. Anything smaller
2957 might crash Emacs. */
2959 #define MIN_SAFE_WINDOW_WIDTH (2)
2960 #define MIN_SAFE_WINDOW_HEIGHT (1)
2962 /* For wp non-zero the total number of columns of window w. Otherwise
2963 the total number of lines of w. */
2965 #define WINDOW_TOTAL_SIZE(w, wp) \
2966 (wp ? WINDOW_TOTAL_COLS (w) : WINDOW_TOTAL_LINES (w))
2968 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2969 minimum allowable size. */
2971 void
2972 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
2974 /* For height, we have to see:
2975 how many windows the frame has at minimum (one or two),
2976 and whether it has a menu bar or other special stuff at the top. */
2977 int min_height
2978 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2979 ? MIN_SAFE_WINDOW_HEIGHT
2980 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2982 if (FRAME_TOP_MARGIN (frame) > 0)
2983 min_height += FRAME_TOP_MARGIN (frame);
2985 if (*rows < min_height)
2986 *rows = min_height;
2987 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2988 *cols = MIN_SAFE_WINDOW_WIDTH;
2991 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2992 check if W's width can be changed, otherwise check W's height.
2993 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2994 siblings, too. If none of the siblings is resizable, WINDOW isn't
2995 either. */
2997 static int
2998 window_fixed_size_p (struct window *w, int width_p, int check_siblings_p)
3000 int fixed_p;
3001 struct window *c;
3003 if (!NILP (w->hchild))
3005 c = XWINDOW (w->hchild);
3007 if (width_p)
3009 /* A horizontal combination is fixed-width if all of if its
3010 children are. */
3011 while (c && window_fixed_size_p (c, width_p, 0))
3012 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
3013 fixed_p = c == NULL;
3015 else
3017 /* A horizontal combination is fixed-height if one of if its
3018 children is. */
3019 while (c && !window_fixed_size_p (c, width_p, 0))
3020 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
3021 fixed_p = c != NULL;
3024 else if (!NILP (w->vchild))
3026 c = XWINDOW (w->vchild);
3028 if (width_p)
3030 /* A vertical combination is fixed-width if one of if its
3031 children is. */
3032 while (c && !window_fixed_size_p (c, width_p, 0))
3033 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
3034 fixed_p = c != NULL;
3036 else
3038 /* A vertical combination is fixed-height if all of if its
3039 children are. */
3040 while (c && window_fixed_size_p (c, width_p, 0))
3041 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
3042 fixed_p = c == NULL;
3045 else if (BUFFERP (w->buffer))
3047 struct buffer *old = current_buffer;
3048 Lisp_Object val;
3050 current_buffer = XBUFFER (w->buffer);
3051 val = find_symbol_value (Qwindow_size_fixed);
3052 current_buffer = old;
3054 fixed_p = 0;
3055 if (!EQ (val, Qunbound))
3057 fixed_p = !NILP (val);
3059 if (fixed_p
3060 && ((EQ (val, Qheight) && width_p)
3061 || (EQ (val, Qwidth) && !width_p)))
3062 fixed_p = 0;
3065 /* Can't tell if this one is resizable without looking at
3066 siblings. If all siblings are fixed-size this one is too. */
3067 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
3069 Lisp_Object child;
3071 for (child = w->prev; WINDOWP (child); child = XWINDOW (child)->prev)
3072 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
3073 break;
3075 if (NILP (child))
3076 for (child = w->next; WINDOWP (child); child = XWINDOW (child)->next)
3077 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
3078 break;
3080 if (NILP (child))
3081 fixed_p = 1;
3084 else
3085 fixed_p = 1;
3087 return fixed_p;
3090 /* Return minimum size of leaf window W. WIDTH_P non-zero means return
3091 the minimum width of W, WIDTH_P zero means return the minimum height
3092 of W. SAFE_P non-zero means ignore window-min-height|width but just
3093 return values that won't crash Emacs and don't hide components like
3094 fringes, scrollbars, or modelines. If WIDTH_P is zero and W is the
3095 minibuffer window, always return 1. */
3097 static int
3098 window_min_size_2 (struct window *w, int width_p, int safe_p)
3100 /* We should consider buffer-local values of window_min_height and
3101 window_min_width here. */
3102 if (width_p)
3104 int safe_size = (MIN_SAFE_WINDOW_WIDTH
3105 + WINDOW_FRINGE_COLS (w)
3106 + WINDOW_SCROLL_BAR_COLS (w));
3108 return safe_p ? safe_size : max (window_min_width, safe_size);
3110 else if (MINI_WINDOW_P (w))
3111 return 1;
3112 else
3114 int safe_size = (MIN_SAFE_WINDOW_HEIGHT
3115 + ((BUFFERP (w->buffer)
3116 && !NILP (BVAR (XBUFFER (w->buffer), mode_line_format)))
3117 ? 1 : 0));
3119 return safe_p ? safe_size : max (window_min_height, safe_size);
3123 /* Return minimum size of window W, not taking fixed-width windows into
3124 account. WIDTH_P non-zero means return the minimum width, otherwise
3125 return the minimum height. SAFE_P non-zero means ignore
3126 window-min-height|width but just return values that won't crash Emacs
3127 and don't hide components like fringes, scrollbars, or modelines. If
3128 W is a combination window, compute the minimum size from the minimum
3129 sizes of W's children. */
3131 static int
3132 window_min_size_1 (struct window *w, int width_p, int safe_p)
3134 struct window *c;
3135 int size;
3137 if (!NILP (w->hchild))
3139 /* W is a horizontal combination. */
3140 c = XWINDOW (w->hchild);
3141 size = 0;
3143 if (width_p)
3145 /* The minimum width of a horizontal combination is the sum of
3146 the minimum widths of its children. */
3147 while (c)
3149 size += window_min_size_1 (c, 1, safe_p);
3150 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
3153 else
3155 /* The minimum height of a horizontal combination is the
3156 maximum of the minimum heights of its children. */
3157 while (c)
3159 size = max (window_min_size_1 (c, 0, safe_p), size);
3160 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
3164 else if (!NILP (w->vchild))
3166 /* W is a vertical combination. */
3167 c = XWINDOW (w->vchild);
3168 size = 0;
3170 if (width_p)
3172 /* The minimum width of a vertical combination is the maximum
3173 of the minimum widths of its children. */
3174 while (c)
3176 size = max (window_min_size_1 (c, 1, safe_p), size);
3177 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
3180 else
3182 /* The minimum height of a vertical combination is the sum of
3183 the minimum height of its children. */
3184 while (c)
3186 size += window_min_size_1 (c, 0, safe_p);
3187 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
3191 else
3192 /* W is a leaf window. */
3193 size = window_min_size_2 (w, width_p, safe_p);
3195 return size;
3198 /* Return the minimum size of window W, taking fixed-size windows into
3199 account. WIDTH_P non-zero means return the minimum width, otherwise
3200 return the minimum height. SAFE_P non-zero means ignore
3201 window-min-height|width but just return values that won't crash Emacs
3202 and don't hide components like fringes, scrollbars, or modelines.
3203 IGNORE_FIXED_P non-zero means ignore if W is fixed-size. Set *FIXED
3204 to 1 if W is fixed-size unless FIXED is null. */
3206 static int
3207 window_min_size (struct window *w, int width_p, int safe_p, int ignore_fixed_p, int *fixed)
3209 int size, fixed_p;
3211 if (ignore_fixed_p)
3212 fixed_p = 0;
3213 else
3214 fixed_p = window_fixed_size_p (w, width_p, 1);
3216 if (fixed)
3217 *fixed = fixed_p;
3219 if (fixed_p)
3220 size = WINDOW_TOTAL_SIZE (w, width_p);
3221 else
3222 size = window_min_size_1 (w, width_p, safe_p);
3224 return size;
3228 /* Adjust the margins of window W if text area is too small.
3229 Return 1 if window width is ok after adjustment; 0 if window
3230 is still too narrow. */
3232 static int
3233 adjust_window_margins (struct window *w)
3235 int box_cols = (WINDOW_TOTAL_COLS (w)
3236 - WINDOW_FRINGE_COLS (w)
3237 - WINDOW_SCROLL_BAR_COLS (w));
3238 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
3239 + WINDOW_RIGHT_MARGIN_COLS (w));
3241 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
3242 return 1;
3244 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
3245 return 0;
3247 /* Window's text area is too narrow, but reducing the window
3248 margins will fix that. */
3249 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
3250 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
3252 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
3253 w->left_margin_cols = w->right_margin_cols
3254 = make_number (margin_cols/2);
3255 else
3256 w->right_margin_cols = make_number (margin_cols);
3258 else
3259 w->left_margin_cols = make_number (margin_cols);
3260 return 1;
3263 /* Calculate new sizes for windows in the list FORWARD when their
3264 compound size goes from TOTAL to SIZE. TOTAL must be greater than
3265 SIZE. The number of windows in FORWARD is NCHILDREN, and the number
3266 that can shrink is SHRINKABLE. Fixed-size windows may be shrunk if
3267 and only if RESIZE_FIXED_P is non-zero. WIDTH_P non-zero means
3268 shrink columns, otherwise shrink lines.
3270 SAFE_P zero means windows may be sized down to window-min-height
3271 lines (window-min-window columns for WIDTH_P non-zero). SAFE_P
3272 non-zero means windows may be sized down to their minimum safe sizes
3273 taking into account the space needed to display modelines, fringes,
3274 and scrollbars.
3276 This function returns an allocated array of new sizes that the caller
3277 must free. A size -1 means the window is fixed and RESIZE_FIXED_P is
3278 zero. A size zero means the window shall be deleted. Array index 0
3279 refers to the first window in FORWARD, 1 to the second, and so on.
3281 This function resizes windows proportionally to their size. It also
3282 tries to preserve smaller windows by resizing larger windows before
3283 resizing any window to zero. If resize_proportionally is non-nil for
3284 a specific window, it will attempt to strictly resize that window
3285 proportionally, even at the expense of deleting smaller windows. */
3286 static int *
3287 shrink_windows (int total, int size, int nchildren, int shrinkable,
3288 int resize_fixed_p, Lisp_Object forward, int width_p, int safe_p)
3290 int available_resize = 0;
3291 int *new_sizes, *min_sizes;
3292 struct window *c;
3293 Lisp_Object child;
3294 int smallest = total;
3295 int total_removed = 0;
3296 int total_shrink = total - size;
3297 int i;
3299 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
3300 min_sizes = xmalloc (sizeof (*min_sizes) * nchildren);
3302 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
3304 int child_size;
3306 c = XWINDOW (child);
3307 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3309 if (!resize_fixed_p && window_fixed_size_p (c, width_p, 0))
3310 new_sizes[i] = -1;
3311 else
3313 new_sizes[i] = child_size;
3314 min_sizes[i] = window_min_size_1 (c, width_p, safe_p);
3315 if (child_size > min_sizes[i]
3316 && NILP (c->resize_proportionally))
3317 available_resize += child_size - min_sizes[i];
3320 /* We might need to shrink some windows to zero. Find the smallest
3321 windows and set them to 0 until we can fulfil the new size. */
3323 while (shrinkable > 1 && size + available_resize < total)
3325 for (i = 0; i < nchildren; ++i)
3326 if (new_sizes[i] > 0 && smallest > new_sizes[i])
3327 smallest = new_sizes[i];
3329 for (i = 0; i < nchildren; ++i)
3330 if (new_sizes[i] == smallest)
3332 /* Resize this window down to zero. */
3333 new_sizes[i] = 0;
3334 if (smallest > min_sizes[i])
3335 available_resize -= smallest - min_sizes[i];
3336 available_resize += smallest;
3337 --shrinkable;
3338 total_removed += smallest;
3340 /* We don't know what the smallest is now. */
3341 smallest = total;
3343 /* Out of for, just remove one window at the time and
3344 check again if we have enough space. */
3345 break;
3349 /* Now, calculate the new sizes. Try to shrink each window
3350 proportional to its size. */
3351 for (i = 0; i < nchildren; ++i)
3353 if (new_sizes[i] > min_sizes[i])
3355 int to_shrink = total_shrink * new_sizes[i] / total;
3357 if (new_sizes[i] - to_shrink < min_sizes[i])
3358 to_shrink = new_sizes[i] - min_sizes[i];
3359 new_sizes[i] -= to_shrink;
3360 total_removed += to_shrink;
3364 /* Any reminder due to rounding, we just subtract from windows
3365 that are left and still can be shrunk. */
3366 while (total_shrink > total_removed)
3368 int nonzero_sizes = 0;
3370 for (i = 0; i < nchildren; ++i)
3371 if (new_sizes[i] > 0)
3372 ++nonzero_sizes;
3374 for (i = 0; i < nchildren; ++i)
3375 if (new_sizes[i] > min_sizes[i])
3377 --new_sizes[i];
3378 ++total_removed;
3380 /* Out of for, just shrink one window at the time and
3381 check again if we have enough space. */
3382 break;
3385 /* Special case, only one window left. */
3386 if (nonzero_sizes == 1)
3387 break;
3390 /* Any surplus due to rounding, we add to windows that are left. */
3391 while (total_shrink < total_removed)
3393 for (i = 0; i < nchildren; ++i)
3395 if (new_sizes[i] != 0 && total_shrink < total_removed)
3397 ++new_sizes[i];
3398 --total_removed;
3399 break;
3404 xfree (min_sizes);
3406 return new_sizes;
3409 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
3410 WINDOW's width. Resize WINDOW's children, if any, so that they keep
3411 their proportionate size relative to WINDOW.
3413 If FIRST_ONLY is 1, change only the first of WINDOW's children when
3414 they are in series. If LAST_ONLY is 1, change only the last of
3415 WINDOW's children when they are in series.
3417 Propagate WINDOW's top or left edge position to children. Delete
3418 windows that become too small unless NODELETE_P is 1. When
3419 NODELETE_P equals 2 do not honor settings for window-min-height and
3420 window-min-width when resizing windows but use safe defaults instead.
3421 This should give better behavior when resizing frames. */
3423 static void
3424 size_window (Lisp_Object window, int size, int width_p, int nodelete_p, int first_only, int last_only)
3426 struct window *w = XWINDOW (window);
3427 struct window *c;
3428 Lisp_Object child, *forward, *sideward;
3429 int old_size = WINDOW_TOTAL_SIZE (w, width_p);
3431 size = max (0, size);
3433 /* Delete WINDOW if it's too small. */
3434 if (nodelete_p != 1 && !NILP (w->parent)
3435 && size < window_min_size_1 (w, width_p, nodelete_p == 2))
3437 delete_window (window);
3438 return;
3441 /* Set redisplay hints. */
3442 w->last_modified = make_number (0);
3443 w->last_overlay_modified = make_number (0);
3444 windows_or_buffers_changed++;
3445 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
3447 if (width_p)
3449 sideward = &w->vchild;
3450 forward = &w->hchild;
3451 w->total_cols = make_number (size);
3452 adjust_window_margins (w);
3454 else
3456 sideward = &w->hchild;
3457 forward = &w->vchild;
3458 w->total_lines = make_number (size);
3459 w->orig_total_lines = Qnil;
3462 if (!NILP (*sideward))
3464 /* We have a chain of parallel siblings whose size should all change. */
3465 for (child = *sideward; !NILP (child); child = c->next)
3467 c = XWINDOW (child);
3468 if (width_p)
3469 c->left_col = w->left_col;
3470 else
3471 c->top_line = w->top_line;
3472 size_window (child, size, width_p, nodelete_p,
3473 first_only, last_only);
3476 else if (!NILP (*forward) && last_only)
3478 /* Change the last in a series of siblings. */
3479 Lisp_Object last_child;
3480 int child_size;
3482 child = *forward;
3485 c = XWINDOW (child);
3486 last_child = child;
3487 child = c->next;
3489 while (!NILP (child));
3491 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3492 size_window (last_child, size - old_size + child_size,
3493 width_p, nodelete_p, first_only, last_only);
3495 else if (!NILP (*forward) && first_only)
3497 /* Change the first in a series of siblings. */
3498 int child_size;
3500 child = *forward;
3501 c = XWINDOW (child);
3503 if (width_p)
3504 c->left_col = w->left_col;
3505 else
3506 c->top_line = w->top_line;
3508 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3509 size_window (child, size - old_size + child_size,
3510 width_p, nodelete_p, first_only, last_only);
3512 else if (!NILP (*forward))
3514 int fixed_size, each IF_LINT (= 0), extra IF_LINT (= 0), n;
3515 int resize_fixed_p, nfixed;
3516 int last_pos, first_pos, nchildren, total;
3517 int *new_sizes = NULL;
3519 /* Determine the fixed-size portion of this window, and the
3520 number of child windows. */
3521 fixed_size = nchildren = nfixed = total = 0;
3522 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
3524 int child_size;
3526 c = XWINDOW (child);
3527 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3528 total += child_size;
3530 if (window_fixed_size_p (c, width_p, 0))
3532 fixed_size += child_size;
3533 ++nfixed;
3537 /* If the new size is smaller than fixed_size, or if there
3538 aren't any resizable windows, allow resizing fixed-size
3539 windows. */
3540 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3542 /* Compute how many lines/columns to add/remove to each child. The
3543 value of extra takes care of rounding errors. */
3544 n = resize_fixed_p ? nchildren : nchildren - nfixed;
3545 if (size < total && n > 1)
3546 new_sizes = shrink_windows (total, size, nchildren, n,
3547 resize_fixed_p, *forward, width_p,
3548 nodelete_p == 2);
3549 else
3551 each = (size - total) / n;
3552 extra = (size - total) - n * each;
3555 /* Compute new children heights and edge positions. */
3556 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
3557 last_pos = first_pos;
3558 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
3560 int new_child_size, old_child_size;
3562 c = XWINDOW (child);
3563 old_child_size = WINDOW_TOTAL_SIZE (c, width_p);
3564 new_child_size = old_child_size;
3566 /* The top or left edge position of this child equals the
3567 bottom or right edge of its predecessor. */
3568 if (width_p)
3569 c->left_col = make_number (last_pos);
3570 else
3571 c->top_line = make_number (last_pos);
3573 /* If this child can be resized, do it. */
3574 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3576 new_child_size =
3577 new_sizes ? new_sizes[n] : old_child_size + each + extra;
3578 extra = 0;
3581 /* Set new size. Note that size_window also propagates
3582 edge positions to children, so it's not a no-op if we
3583 didn't change the child's size. */
3584 size_window (child, new_child_size, width_p, 1,
3585 first_only, last_only);
3587 /* Remember the bottom/right edge position of this child; it
3588 will be used to set the top/left edge of the next child. */
3589 last_pos += new_child_size;
3592 xfree (new_sizes);
3594 /* We should have covered the parent exactly with child windows. */
3595 xassert (size == last_pos - first_pos);
3597 /* Now delete any children that became too small. */
3598 if (nodelete_p != 1)
3599 for (child = *forward; !NILP (child); child = c->next)
3601 int child_size;
3603 c = XWINDOW (child);
3604 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3605 size_window (child, child_size, width_p, nodelete_p,
3606 first_only, last_only);
3611 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3612 WINDOW's children. NODELETE zero means windows that have become
3613 smaller than window-min-height in the process may be deleted.
3614 NODELETE 1 means never delete windows that become too small in the
3615 process. (The caller should check later and do so if appropriate.)
3616 NODELETE 2 means delete only windows that have become too small to be
3617 displayed correctly. */
3619 void
3620 set_window_height (Lisp_Object window, int height, int nodelete)
3622 size_window (window, height, 0, nodelete, 0, 0);
3625 /* Set WINDOW's width to WIDTH, and recursively change the width of
3626 WINDOW's children. NODELETE zero means windows that have become
3627 smaller than window-min-width in the process may be deleted.
3628 NODELETE 1 means never delete windows that become too small in the
3629 process. (The caller should check later and do so if appropriate.)
3630 NODELETE 2 means delete only windows that have become too small to be
3631 displayed correctly. */
3633 void
3634 set_window_width (Lisp_Object window, int width, int nodelete)
3636 size_window (window, width, 1, nodelete, 0, 0);
3639 /* Change window heights in windows rooted in WINDOW by N lines. */
3641 void
3642 change_window_heights (Lisp_Object window, int n)
3644 struct window *w = XWINDOW (window);
3646 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3647 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3649 if (INTEGERP (w->orig_top_line))
3650 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3651 if (INTEGERP (w->orig_total_lines))
3652 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3654 /* Handle just the top child in a vertical split. */
3655 if (!NILP (w->vchild))
3656 change_window_heights (w->vchild, n);
3658 /* Adjust all children in a horizontal split. */
3659 for (window = w->hchild; !NILP (window); window = w->next)
3661 w = XWINDOW (window);
3662 change_window_heights (window, n);
3667 int window_select_count;
3669 static Lisp_Object Fset_window_margins (Lisp_Object, Lisp_Object, Lisp_Object);
3670 static Lisp_Object Fset_window_fringes (Lisp_Object, Lisp_Object, Lisp_Object,
3671 Lisp_Object);
3672 static Lisp_Object Fset_window_scroll_bars (Lisp_Object, Lisp_Object,
3673 Lisp_Object, Lisp_Object);
3674 static Lisp_Object Fset_window_vscroll (Lisp_Object, Lisp_Object, Lisp_Object);
3676 static void
3677 run_funs (Lisp_Object funs)
3679 for (; CONSP (funs); funs = XCDR (funs))
3680 if (!EQ (XCAR (funs), Qt))
3681 call0 (XCAR (funs));
3684 static Lisp_Object select_window_norecord (Lisp_Object window);
3685 static Lisp_Object select_frame_norecord (Lisp_Object frame);
3687 void
3688 run_window_configuration_change_hook (struct frame *f)
3690 int count = SPECPDL_INDEX ();
3691 Lisp_Object frame, global_wcch
3692 = Fdefault_value (Qwindow_configuration_change_hook);
3693 XSETFRAME (frame, f);
3695 if (NILP (Vrun_hooks))
3696 return;
3698 if (SELECTED_FRAME () != f)
3700 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3701 Fselect_frame (frame, Qt);
3704 /* Use the right buffer. Matters when running the local hooks. */
3705 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3707 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3708 Fset_buffer (Fwindow_buffer (Qnil));
3711 /* Look for buffer-local values. */
3713 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3714 for (; CONSP (windows); windows = XCDR (windows))
3716 Lisp_Object window = XCAR (windows);
3717 Lisp_Object buffer = Fwindow_buffer (window);
3718 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3719 buffer)))
3721 int count1 = SPECPDL_INDEX ();
3722 record_unwind_protect (select_window_norecord, Fselected_window ());
3723 select_window_norecord (window);
3724 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3725 buffer));
3726 unbind_to (count1, Qnil);
3731 run_funs (global_wcch);
3732 unbind_to (count, Qnil);
3735 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3736 means it's allowed to run hooks. See make_frame for a case where
3737 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3738 margins, fringes, and scroll-bar settings of the window are not
3739 reset from the buffer's local settings. */
3741 void
3742 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
3744 struct window *w = XWINDOW (window);
3745 struct buffer *b = XBUFFER (buffer);
3746 int count = SPECPDL_INDEX ();
3747 int samebuf = EQ (buffer, w->buffer);
3749 w->buffer = buffer;
3751 if (EQ (window, selected_window))
3752 BVAR (b, last_selected_window) = window;
3754 /* Let redisplay errors through. */
3755 b->display_error_modiff = 0;
3757 /* Update time stamps of buffer display. */
3758 if (INTEGERP (BVAR (b, display_count)))
3759 XSETINT (BVAR (b, display_count), XINT (BVAR (b, display_count)) + 1);
3760 BVAR (b, display_time) = Fcurrent_time ();
3762 XSETFASTINT (w->window_end_pos, 0);
3763 XSETFASTINT (w->window_end_vpos, 0);
3764 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3765 w->window_end_valid = Qnil;
3766 if (!(keep_margins_p && samebuf))
3767 { /* If we're not actually changing the buffer, don't reset hscroll and
3768 vscroll. This case happens for example when called from
3769 change_frame_size_1, where we use a dummy call to
3770 Fset_window_buffer on the frame's selected window (and no other)
3771 just in order to run window-configuration-change-hook.
3772 Resetting hscroll and vscroll here is problematic for things like
3773 image-mode and doc-view-mode since it resets the image's position
3774 whenever we resize the frame. */
3775 w->hscroll = w->min_hscroll = make_number (0);
3776 w->vscroll = 0;
3777 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3778 set_marker_restricted (w->start,
3779 make_number (b->last_window_start),
3780 buffer);
3781 w->start_at_line_beg = Qnil;
3782 w->force_start = Qnil;
3783 XSETFASTINT (w->last_modified, 0);
3784 XSETFASTINT (w->last_overlay_modified, 0);
3786 /* Maybe we could move this into the `if' but it's not obviously safe and
3787 I doubt it's worth the trouble. */
3788 windows_or_buffers_changed++;
3790 /* We must select BUFFER for running the window-scroll-functions. */
3791 /* We can't check ! NILP (Vwindow_scroll_functions) here
3792 because that might itself be a local variable. */
3793 if (window_initialized)
3795 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3796 Fset_buffer (buffer);
3799 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3801 if (!keep_margins_p)
3803 /* Set left and right marginal area width etc. from buffer. */
3805 /* This may call adjust_window_margins three times, so
3806 temporarily disable window margins. */
3807 Lisp_Object save_left = w->left_margin_cols;
3808 Lisp_Object save_right = w->right_margin_cols;
3810 w->left_margin_cols = w->right_margin_cols = Qnil;
3812 Fset_window_fringes (window,
3813 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3814 BVAR (b, fringes_outside_margins));
3816 Fset_window_scroll_bars (window,
3817 BVAR (b, scroll_bar_width),
3818 BVAR (b, vertical_scroll_bar_type), Qnil);
3820 w->left_margin_cols = save_left;
3821 w->right_margin_cols = save_right;
3823 Fset_window_margins (window,
3824 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3827 if (run_hooks_p)
3829 if (! NILP (Vwindow_scroll_functions))
3830 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3831 Fmarker_position (w->start));
3832 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3835 unbind_to (count, Qnil);
3839 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3840 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3841 WINDOW defaults to the selected window. BUFFER-OR-NAME must be a buffer
3842 or the name of an existing buffer. Optional third argument KEEP-MARGINS
3843 non-nil means that WINDOW's current display margins, fringe widths, and
3844 scroll bar settings are preserved; the default is to reset these from
3845 the local settings for BUFFER-OR-NAME or the frame defaults. Return nil.
3847 This function throws an error when WINDOW is strongly dedicated to its
3848 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3849 already display BUFFER-OR-NAME.
3851 This function runs `window-scroll-functions' before running
3852 `window-configuration-change-hook'. */)
3853 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3855 register Lisp_Object tem, buffer;
3856 register struct window *w = decode_window (window);
3858 XSETWINDOW (window, w);
3859 buffer = Fget_buffer (buffer_or_name);
3860 CHECK_BUFFER (buffer);
3861 if (NILP (BVAR (XBUFFER (buffer), name)))
3862 error ("Attempt to display deleted buffer");
3864 tem = w->buffer;
3865 if (NILP (tem))
3866 error ("Window is deleted");
3867 else if (!EQ (tem, Qt))
3868 /* w->buffer is t when the window is first being set up. */
3870 if (EQ (tem, buffer))
3871 return Qnil;
3872 else if (EQ (w->dedicated, Qt))
3873 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3874 else
3875 w->dedicated = Qnil;
3877 unshow_buffer (w);
3880 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3881 return Qnil;
3884 static Lisp_Object
3885 select_window_norecord (Lisp_Object window)
3887 return WINDOW_LIVE_P (window)
3888 ? Fselect_window (window, Qt) : selected_window;
3891 static Lisp_Object
3892 select_frame_norecord (Lisp_Object frame)
3894 return FRAME_LIVE_P (XFRAME (frame))
3895 ? Fselect_frame (frame, Qt) : selected_frame;
3898 static Lisp_Object
3899 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3901 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3904 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3905 0, 1, 0,
3906 doc: /* Force all windows to be updated on next redisplay.
3907 If optional arg OBJECT is a window, force redisplay of that window only.
3908 If OBJECT is a buffer or buffer name, force redisplay of all windows
3909 displaying that buffer. */)
3910 (Lisp_Object object)
3912 if (NILP (object))
3914 windows_or_buffers_changed++;
3915 update_mode_lines++;
3916 return Qt;
3919 if (WINDOWP (object))
3921 struct window *w = XWINDOW (object);
3922 mark_window_display_accurate (object, 0);
3923 w->update_mode_line = Qt;
3924 if (BUFFERP (w->buffer))
3925 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3926 ++update_mode_lines;
3927 return Qt;
3930 if (STRINGP (object))
3931 object = Fget_buffer (object);
3932 if (BUFFERP (object) && !NILP (BVAR (XBUFFER (object), name)))
3934 /* Walk all windows looking for buffer, and force update
3935 of each of those windows. */
3937 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3938 return NILP (object) ? Qnil : Qt;
3941 /* If nothing suitable was found, just return.
3942 We could signal an error, but this feature will typically be used
3943 asynchronously in timers or process sentinels, so we don't. */
3944 return Qnil;
3948 void
3949 temp_output_buffer_show (register Lisp_Object buf)
3951 register struct buffer *old = current_buffer;
3952 register Lisp_Object window;
3953 register struct window *w;
3955 BVAR (XBUFFER (buf), directory) = BVAR (current_buffer, directory);
3957 Fset_buffer (buf);
3958 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3959 BEGV = BEG;
3960 ZV = Z;
3961 SET_PT (BEG);
3962 set_buffer_internal (old);
3964 if (!NILP (Vtemp_buffer_show_function))
3965 call1 (Vtemp_buffer_show_function, buf);
3966 else
3968 window = display_buffer (buf, Qnil, Qnil);
3970 if (!EQ (XWINDOW (window)->frame, selected_frame))
3971 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3972 Vminibuf_scroll_window = window;
3973 w = XWINDOW (window);
3974 XSETFASTINT (w->hscroll, 0);
3975 XSETFASTINT (w->min_hscroll, 0);
3976 set_marker_restricted_both (w->start, buf, BEG, BEG);
3977 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3979 /* Run temp-buffer-show-hook, with the chosen window selected
3980 and its buffer current. */
3982 int count = SPECPDL_INDEX ();
3983 Lisp_Object prev_window, prev_buffer;
3984 prev_window = selected_window;
3985 XSETBUFFER (prev_buffer, old);
3987 /* Select the window that was chosen, for running the hook.
3988 Note: Both Fselect_window and select_window_norecord may
3989 set-buffer to the buffer displayed in the window,
3990 so we need to save the current buffer. --stef */
3991 record_unwind_protect (Fset_buffer, prev_buffer);
3992 record_unwind_protect (select_window_norecord, prev_window);
3993 Fselect_window (window, Qt);
3994 Fset_buffer (w->buffer);
3995 Frun_hooks (1, &Qtemp_buffer_show_hook);
3996 unbind_to (count, Qnil);
4001 DEFUN ("internal-temp-output-buffer-show",
4002 Ftemp_output_buffer_show, Stemp_output_buffer_show,
4003 1, 1, 0,
4004 doc: /* Internal function for `with-output-to-temp-buffer''. */)
4005 (Lisp_Object buf)
4007 temp_output_buffer_show (buf);
4008 return Qnil;
4011 static void
4012 make_dummy_parent (Lisp_Object window)
4014 Lisp_Object new;
4015 register struct window *o, *p;
4016 int i;
4018 o = XWINDOW (window);
4019 p = allocate_window ();
4020 for (i = 0; i < VECSIZE (struct window); ++i)
4021 ((struct Lisp_Vector *) p)->contents[i]
4022 = ((struct Lisp_Vector *)o)->contents[i];
4023 XSETWINDOW (new, p);
4025 ++sequence_number;
4026 XSETFASTINT (p->sequence_number, sequence_number);
4028 /* Put new into window structure in place of window */
4029 replace_window (window, new);
4031 o->next = Qnil;
4032 o->prev = Qnil;
4033 o->vchild = Qnil;
4034 o->hchild = Qnil;
4035 o->parent = new;
4037 p->start = Qnil;
4038 p->pointm = Qnil;
4039 p->buffer = Qnil;
4042 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
4043 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
4044 WINDOW defaults to selected one and SIZE to half its size.
4045 If optional third arg HORIZONTAL is non-nil, split side by side and put
4046 SIZE columns in the first of the pair. In that case, SIZE includes that
4047 window's scroll bar, or the divider column to its right.
4048 Interactively, all arguments are nil.
4049 Returns the newly created window (which is the lower or rightmost one).
4050 The upper or leftmost window is the original one, and remains selected
4051 if it was selected before.
4053 See Info node `(elisp)Splitting Windows' for more details and examples. */)
4054 (Lisp_Object window, Lisp_Object size, Lisp_Object horizontal)
4056 register Lisp_Object new;
4057 register struct window *o, *p;
4058 FRAME_PTR fo;
4059 register int size_int;
4061 if (NILP (window))
4062 window = selected_window;
4063 else
4064 CHECK_LIVE_WINDOW (window);
4066 o = XWINDOW (window);
4067 fo = XFRAME (WINDOW_FRAME (o));
4069 if (NILP (size))
4071 if (!NILP (horizontal))
4072 /* Calculate the size of the left-hand window, by dividing
4073 the usable space in columns by two.
4074 We round up, since the left-hand window may include
4075 a dividing line, while the right-hand may not. */
4076 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
4077 else
4078 size_int = XFASTINT (o->total_lines) >> 1;
4080 else
4082 CHECK_NUMBER (size);
4083 size_int = XINT (size);
4086 if (MINI_WINDOW_P (o))
4087 error ("Attempt to split minibuffer window");
4088 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
4089 error ("Attempt to split fixed-size window");
4091 if (NILP (horizontal))
4093 int window_safe_height = window_min_size_2 (o, 0, 0);
4095 if (size_int < window_safe_height)
4096 error ("Window height %d too small (after splitting)", size_int);
4097 if (size_int + window_safe_height > XFASTINT (o->total_lines))
4098 error ("Window height %d too small (after splitting)",
4099 (int) (XFASTINT (o->total_lines) - size_int));
4100 if (NILP (o->parent)
4101 || NILP (XWINDOW (o->parent)->vchild))
4103 make_dummy_parent (window);
4104 new = o->parent;
4105 XWINDOW (new)->vchild = window;
4108 else
4110 int window_safe_width = window_min_size_2 (o, 1, 0);
4112 if (size_int < window_safe_width)
4113 error ("Window width %d too small (after splitting)", size_int);
4114 if (size_int + window_safe_width > XFASTINT (o->total_cols))
4115 error ("Window width %d too small (after splitting)",
4116 (int) (XFASTINT (o->total_cols) - size_int));
4117 if (NILP (o->parent)
4118 || NILP (XWINDOW (o->parent)->hchild))
4120 make_dummy_parent (window);
4121 new = o->parent;
4122 XWINDOW (new)->hchild = window;
4126 /* Now we know that window's parent is a vertical combination
4127 if we are dividing vertically, or a horizontal combination
4128 if we are making side-by-side windows */
4130 windows_or_buffers_changed++;
4131 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
4132 new = make_window ();
4133 p = XWINDOW (new);
4135 p->frame = o->frame;
4136 p->next = o->next;
4137 if (!NILP (p->next))
4138 XWINDOW (p->next)->prev = new;
4139 p->prev = window;
4140 o->next = new;
4141 p->parent = o->parent;
4142 p->buffer = Qt;
4143 p->window_end_valid = Qnil;
4144 memset (&p->last_cursor, 0, sizeof p->last_cursor);
4146 /* Duplicate special geometry settings. */
4148 p->left_margin_cols = o->left_margin_cols;
4149 p->right_margin_cols = o->right_margin_cols;
4150 p->left_fringe_width = o->left_fringe_width;
4151 p->right_fringe_width = o->right_fringe_width;
4152 p->fringes_outside_margins = o->fringes_outside_margins;
4153 p->scroll_bar_width = o->scroll_bar_width;
4154 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
4156 /* Apportion the available frame space among the two new windows */
4158 if (!NILP (horizontal))
4160 p->total_lines = o->total_lines;
4161 p->top_line = o->top_line;
4162 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
4163 XSETFASTINT (o->total_cols, size_int);
4164 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
4165 adjust_window_margins (p);
4166 adjust_window_margins (o);
4168 else
4170 p->left_col = o->left_col;
4171 p->total_cols = o->total_cols;
4172 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
4173 XSETFASTINT (o->total_lines, size_int);
4174 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
4177 /* Adjust glyph matrices. */
4178 adjust_glyphs (fo);
4180 Fset_window_buffer (new, o->buffer, Qt);
4181 return new;
4184 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
4185 doc: /* Make selected window SIZE lines taller.
4186 Interactively, if no argument is given, make the selected window one
4187 line taller. If optional argument HORIZONTAL is non-nil, make selected
4188 window wider by SIZE columns. If SIZE is negative, shrink the window by
4189 -SIZE lines or columns. Return nil.
4191 This function can delete windows if they get too small. The size of
4192 fixed size windows is not altered by this function. */)
4193 (Lisp_Object size, Lisp_Object horizontal)
4195 CHECK_NUMBER (size);
4196 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
4198 run_window_configuration_change_hook (SELECTED_FRAME ());
4200 return Qnil;
4203 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
4204 doc: /* Make selected window SIZE lines smaller.
4205 Interactively, if no argument is given, make the selected window one
4206 line smaller. If optional argument HORIZONTAL is non-nil, make the
4207 window narrower by SIZE columns. If SIZE is negative, enlarge selected
4208 window by -SIZE lines or columns. Return nil.
4210 This function can delete windows if they get too small. The size of
4211 fixed size windows is not altered by this function. */)
4212 (Lisp_Object size, Lisp_Object horizontal)
4214 CHECK_NUMBER (size);
4215 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
4217 run_window_configuration_change_hook (SELECTED_FRAME ());
4219 return Qnil;
4222 static int
4223 window_height (Lisp_Object window)
4225 register struct window *p = XWINDOW (window);
4226 return WINDOW_TOTAL_LINES (p);
4229 static int
4230 window_width (Lisp_Object window)
4232 register struct window *p = XWINDOW (window);
4233 return WINDOW_TOTAL_COLS (p);
4237 #define CURBEG(w) \
4238 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
4240 #define CURSIZE(w) \
4241 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
4244 /* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
4245 horizontally; zero means do it vertically.
4247 Siblings of the selected window are resized to fulfill the size
4248 request. If they become too small in the process, they may be
4249 deleted. */
4251 static void
4252 enlarge_window (Lisp_Object window, int delta, int horiz_flag)
4254 Lisp_Object parent, next, prev;
4255 struct window *p;
4256 Lisp_Object *sizep;
4257 int maximum;
4258 int (*sizefun) (Lisp_Object)
4259 = horiz_flag ? window_width : window_height;
4260 void (*setsizefun) (Lisp_Object, int, int)
4261 = (horiz_flag ? set_window_width : set_window_height);
4263 /* Give up if this window cannot be resized. */
4264 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4265 error ("Window is not resizable");
4267 /* Find the parent of the selected window. */
4268 while (1)
4270 p = XWINDOW (window);
4271 parent = p->parent;
4273 if (NILP (parent))
4275 if (horiz_flag)
4276 error ("No other window to side of this one");
4277 break;
4280 if (horiz_flag
4281 ? !NILP (XWINDOW (parent)->hchild)
4282 : !NILP (XWINDOW (parent)->vchild))
4283 break;
4285 window = parent;
4288 sizep = &CURSIZE (window);
4291 register int maxdelta;
4293 /* Compute the maximum size increment this window can have. */
4295 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4296 /* This is a main window followed by a minibuffer. */
4297 : !NILP (p->next) ? ((*sizefun) (p->next)
4298 - window_min_size (XWINDOW (p->next),
4299 horiz_flag, 0, 0, 0))
4300 /* This is a minibuffer following a main window. */
4301 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4302 - window_min_size (XWINDOW (p->prev),
4303 horiz_flag, 0, 0, 0))
4304 /* This is a frame with only one window, a minibuffer-only
4305 or a minibufferless frame. */
4306 : (delta = 0));
4308 if (delta > maxdelta)
4309 /* This case traps trying to make the minibuffer
4310 the full frame, or make the only window aside from the
4311 minibuffer the full frame. */
4312 delta = maxdelta;
4315 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4316 horiz_flag, 0, 0, 0))
4318 delete_window (window);
4319 return;
4322 if (delta == 0)
4323 return;
4325 /* Find the total we can get from other siblings without deleting them. */
4326 maximum = 0;
4327 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
4328 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4329 horiz_flag, 0, 0, 0);
4330 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
4331 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4332 horiz_flag, 0, 0, 0);
4334 /* If we can get it all from them without deleting them, do so. */
4335 if (delta <= maximum)
4337 Lisp_Object first_unaffected;
4338 Lisp_Object first_affected;
4339 int fixed_p;
4341 next = p->next;
4342 prev = p->prev;
4343 first_affected = window;
4344 /* Look at one sibling at a time,
4345 moving away from this window in both directions alternately,
4346 and take as much as we can get without deleting that sibling. */
4347 while (delta != 0
4348 && (!NILP (next) || !NILP (prev)))
4350 if (! NILP (next))
4352 int this_one = ((*sizefun) (next)
4353 - window_min_size (XWINDOW (next), horiz_flag,
4354 0, 0, &fixed_p));
4355 if (!fixed_p)
4357 if (this_one > delta)
4358 this_one = delta;
4360 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4361 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4363 delta -= this_one;
4366 next = XWINDOW (next)->next;
4369 if (delta == 0)
4370 break;
4372 if (! NILP (prev))
4374 int this_one = ((*sizefun) (prev)
4375 - window_min_size (XWINDOW (prev), horiz_flag,
4376 0, 0, &fixed_p));
4377 if (!fixed_p)
4379 if (this_one > delta)
4380 this_one = delta;
4382 first_affected = prev;
4384 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4385 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4387 delta -= this_one;
4390 prev = XWINDOW (prev)->prev;
4394 xassert (delta == 0);
4396 /* Now recalculate the edge positions of all the windows affected,
4397 based on the new sizes. */
4398 first_unaffected = next;
4399 prev = first_affected;
4400 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4401 prev = next, next = XWINDOW (next)->next)
4403 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4404 /* This does not change size of NEXT,
4405 but it propagates the new top edge to its children */
4406 (*setsizefun) (next, (*sizefun) (next), 0);
4409 else
4411 register int delta1;
4412 register int opht = (*sizefun) (parent);
4414 if (opht <= XINT (*sizep) + delta)
4416 /* If trying to grow this window to or beyond size of the parent,
4417 just delete all the sibling windows. */
4418 Lisp_Object start, tem;
4420 start = XWINDOW (parent)->vchild;
4421 if (NILP (start))
4422 start = XWINDOW (parent)->hchild;
4424 /* Delete any siblings that come after WINDOW. */
4425 tem = XWINDOW (window)->next;
4426 while (! NILP (tem))
4428 Lisp_Object next1 = XWINDOW (tem)->next;
4429 delete_window (tem);
4430 tem = next1;
4433 /* Delete any siblings that come after WINDOW.
4434 Note that if START is not WINDOW, then WINDOW still
4435 has siblings, so WINDOW has not yet replaced its parent. */
4436 tem = start;
4437 while (! EQ (tem, window))
4439 Lisp_Object next1 = XWINDOW (tem)->next;
4440 delete_window (tem);
4441 tem = next1;
4444 else
4446 /* Otherwise, make delta1 just right so that if we add
4447 delta1 lines to this window and to the parent, and then
4448 shrink the parent back to its original size, the new
4449 proportional size of this window will increase by delta.
4451 The function size_window will compute the new height h'
4452 of the window from delta1 as:
4454 e = delta1/n
4455 x = delta1 - delta1/n * n for the 1st resizable child
4456 h' = h + e + x
4458 where n is the number of children that can be resized.
4459 We can ignore x by choosing a delta1 that is a multiple of
4460 n. We want the height of this window to come out as
4462 h' = h + delta
4464 So, delta1 must be
4466 h + e = h + delta
4467 delta1/n = delta
4468 delta1 = n * delta.
4470 The number of children n equals the number of resizable
4471 children of this window + 1 because we know window itself
4472 is resizable (otherwise we would have signaled an error).
4474 This reasoning is not correct when other windows become too
4475 small and shrink_windows refuses to delete them. Below we
4476 use resize_proportionally to work around this problem. */
4478 struct window *w = XWINDOW (window);
4479 Lisp_Object s;
4480 int n = 1;
4482 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
4483 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4484 ++n;
4485 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
4486 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4487 ++n;
4489 delta1 = n * delta;
4491 /* Add delta1 lines or columns to this window, and to the parent,
4492 keeping things consistent while not affecting siblings. */
4493 XSETINT (CURSIZE (parent), opht + delta1);
4494 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4496 /* Squeeze out delta1 lines or columns from our parent,
4497 shrinking this window and siblings proportionately. This
4498 brings parent back to correct size. Delta1 was calculated
4499 so this makes this window the desired size, taking it all
4500 out of the siblings.
4502 Temporarily set resize_proportionally to Qt to assure that,
4503 if necessary, shrink_windows deletes smaller windows rather
4504 than shrink this window. */
4505 w->resize_proportionally = Qt;
4506 (*setsizefun) (parent, opht, 0);
4507 w->resize_proportionally = Qnil;
4511 XSETFASTINT (p->last_modified, 0);
4512 XSETFASTINT (p->last_overlay_modified, 0);
4514 /* Adjust glyph matrices. */
4515 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4519 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4520 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4521 zero means adjust the height, moving the bottom edge.
4523 Following siblings of the selected window are resized to fulfill
4524 the size request. If they become too small in the process, they
4525 are not deleted; instead, we signal an error. */
4527 static void
4528 adjust_window_trailing_edge (Lisp_Object window, int delta, int horiz_flag)
4530 Lisp_Object parent, child;
4531 struct window *p;
4532 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4533 int delcount = window_deletion_count;
4535 CHECK_WINDOW (window);
4537 /* Give up if this window cannot be resized. */
4538 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4539 error ("Window is not resizable");
4541 while (1)
4543 Lisp_Object first_parallel = Qnil;
4545 if (NILP (window))
4547 /* This happens if WINDOW on the previous iteration was
4548 at top level of the window tree. */
4549 Fset_window_configuration (old_config);
4550 error ("Specified window edge is fixed");
4553 p = XWINDOW (window);
4554 parent = p->parent;
4556 /* See if this level has windows in parallel in the specified
4557 direction. If so, set FIRST_PARALLEL to the first one. */
4558 if (horiz_flag)
4560 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4561 first_parallel = XWINDOW (parent)->vchild;
4562 else if (NILP (parent) && !NILP (p->next))
4564 /* Handle the vertical chain of main window and minibuffer
4565 which has no parent. */
4566 first_parallel = window;
4567 while (! NILP (XWINDOW (first_parallel)->prev))
4568 first_parallel = XWINDOW (first_parallel)->prev;
4571 else
4573 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4574 first_parallel = XWINDOW (parent)->hchild;
4577 /* If this level's succession is in the desired dimension,
4578 and this window is the last one, and there is no higher level,
4579 its trailing edge is fixed. */
4580 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4581 && NILP (parent))
4583 Fset_window_configuration (old_config);
4584 error ("Specified window edge is fixed");
4587 /* Don't make this window too small. */
4588 if (XINT (CURSIZE (window)) + delta
4589 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
4591 Fset_window_configuration (old_config);
4592 error ("Cannot adjust window size as specified");
4595 /* Clear out some redisplay caches. */
4596 XSETFASTINT (p->last_modified, 0);
4597 XSETFASTINT (p->last_overlay_modified, 0);
4599 /* Adjust this window's edge. */
4600 XSETINT (CURSIZE (window),
4601 XINT (CURSIZE (window)) + delta);
4603 /* If this window has following siblings in the desired dimension,
4604 make them smaller, and exit the loop.
4606 (If we reach the top of the tree and can never do this,
4607 we will fail and report an error, above.) */
4608 if (NILP (first_parallel))
4610 if (!NILP (p->next))
4612 /* This may happen for the minibuffer. In that case
4613 the window_deletion_count check below does not work. */
4614 if (XINT (CURSIZE (p->next)) - delta <= 0)
4616 Fset_window_configuration (old_config);
4617 error ("Cannot adjust window size as specified");
4620 XSETINT (CURBEG (p->next),
4621 XINT (CURBEG (p->next)) + delta);
4622 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4623 horiz_flag, 0, 1, 0);
4624 break;
4627 else
4628 /* Here we have a chain of parallel siblings, in the other dimension.
4629 Change the size of the other siblings. */
4630 for (child = first_parallel;
4631 ! NILP (child);
4632 child = XWINDOW (child)->next)
4633 if (! EQ (child, window))
4634 size_window (child, XINT (CURSIZE (child)) + delta,
4635 horiz_flag, 0, 0, 1);
4637 window = parent;
4640 /* If we made a window so small it got deleted,
4641 we failed. Report failure. */
4642 if (delcount != window_deletion_count)
4644 Fset_window_configuration (old_config);
4645 error ("Cannot adjust window size as specified");
4648 /* Adjust glyph matrices. */
4649 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4652 #undef CURBEG
4653 #undef CURSIZE
4655 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4656 Sadjust_window_trailing_edge, 3, 3, 0,
4657 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4658 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4659 Otherwise, adjust the height, moving the bottom edge.
4661 Following siblings of the selected window are resized to fulfill
4662 the size request. If they become too small in the process, they
4663 are not deleted; instead, we signal an error. */)
4664 (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal)
4666 CHECK_NUMBER (delta);
4667 if (NILP (window))
4668 window = selected_window;
4669 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4671 run_window_configuration_change_hook
4672 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4674 return Qnil;
4679 /***********************************************************************
4680 Resizing Mini-Windows
4681 ***********************************************************************/
4683 static void shrink_window_lowest_first (struct window *, int);
4685 enum save_restore_action
4687 CHECK_ORIG_SIZES,
4688 SAVE_ORIG_SIZES,
4689 RESTORE_ORIG_SIZES
4692 static int save_restore_orig_size (struct window *,
4693 enum save_restore_action);
4695 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4696 from lowest windows first. */
4698 static void
4699 shrink_window_lowest_first (struct window *w, int height)
4701 struct window *c;
4702 Lisp_Object child;
4703 int old_height;
4705 xassert (!MINI_WINDOW_P (w));
4707 /* Set redisplay hints. */
4708 XSETFASTINT (w->last_modified, 0);
4709 XSETFASTINT (w->last_overlay_modified, 0);
4710 windows_or_buffers_changed++;
4711 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4713 old_height = XFASTINT (w->total_lines);
4714 XSETFASTINT (w->total_lines, height);
4716 if (!NILP (w->hchild))
4718 for (child = w->hchild; !NILP (child); child = c->next)
4720 c = XWINDOW (child);
4721 c->top_line = w->top_line;
4722 shrink_window_lowest_first (c, height);
4725 else if (!NILP (w->vchild))
4727 Lisp_Object last_child;
4728 int delta = old_height - height;
4729 int last_top;
4731 last_child = Qnil;
4733 /* Find the last child. We are taking space from lowest windows
4734 first, so we iterate over children from the last child
4735 backwards. */
4736 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
4737 last_child = child;
4739 /* Size children down to their safe heights. */
4740 for (child = last_child; delta && !NILP (child); child = c->prev)
4742 int this_one;
4744 c = XWINDOW (child);
4745 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
4747 if (this_one > delta)
4748 this_one = delta;
4750 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4751 delta -= this_one;
4754 /* Compute new positions. */
4755 last_top = XINT (w->top_line);
4756 for (child = w->vchild; !NILP (child); child = c->next)
4758 c = XWINDOW (child);
4759 c->top_line = make_number (last_top);
4760 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4761 last_top += XFASTINT (c->total_lines);
4767 /* Save, restore, or check positions and sizes in the window tree
4768 rooted at W. ACTION says what to do.
4770 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4771 orig_total_lines members are valid for all windows in the window
4772 tree. Value is non-zero if they are valid.
4774 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4775 orig_top_line and orig_total_lines for all windows in the tree.
4777 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4778 stored in orig_top_line and orig_total_lines for all windows. */
4780 static int
4781 save_restore_orig_size (struct window *w, enum save_restore_action action)
4783 int success_p = 1;
4785 while (w)
4787 if (!NILP (w->hchild))
4789 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4790 success_p = 0;
4792 else if (!NILP (w->vchild))
4794 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4795 success_p = 0;
4798 switch (action)
4800 case CHECK_ORIG_SIZES:
4801 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4802 return 0;
4803 break;
4805 case SAVE_ORIG_SIZES:
4806 w->orig_top_line = w->top_line;
4807 w->orig_total_lines = w->total_lines;
4808 XSETFASTINT (w->last_modified, 0);
4809 XSETFASTINT (w->last_overlay_modified, 0);
4810 break;
4812 case RESTORE_ORIG_SIZES:
4813 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4814 w->top_line = w->orig_top_line;
4815 w->total_lines = w->orig_total_lines;
4816 w->orig_total_lines = w->orig_top_line = Qnil;
4817 XSETFASTINT (w->last_modified, 0);
4818 XSETFASTINT (w->last_overlay_modified, 0);
4819 break;
4821 default:
4822 abort ();
4825 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4828 return success_p;
4832 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4833 without deleting other windows. */
4835 void
4836 grow_mini_window (struct window *w, int delta)
4838 struct frame *f = XFRAME (w->frame);
4839 struct window *root;
4841 xassert (MINI_WINDOW_P (w));
4842 /* Commenting out the following assertion goes against the stated interface
4843 of the function, but it currently does not seem to do anything useful.
4844 See discussion of this issue in the thread for bug#4534.
4845 xassert (delta >= 0); */
4847 /* Compute how much we can enlarge the mini-window without deleting
4848 other windows. */
4849 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4850 if (delta > 0)
4852 int min_height = window_min_size (root, 0, 0, 0, 0);
4853 if (XFASTINT (root->total_lines) - delta < min_height)
4854 /* Note that the root window may already be smaller than
4855 min_height. */
4856 delta = max (0, XFASTINT (root->total_lines) - min_height);
4859 if (delta)
4861 /* Save original window sizes and positions, if not already done. */
4862 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4863 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4865 /* Shrink other windows. */
4866 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4868 /* Grow the mini-window. */
4869 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4870 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4871 XSETFASTINT (w->last_modified, 0);
4872 XSETFASTINT (w->last_overlay_modified, 0);
4874 adjust_glyphs (f);
4879 /* Shrink mini-window W. If there is recorded info about window sizes
4880 before a call to grow_mini_window, restore recorded window sizes.
4881 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4882 line. */
4884 void
4885 shrink_mini_window (struct window *w)
4887 struct frame *f = XFRAME (w->frame);
4888 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4890 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4892 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4893 adjust_glyphs (f);
4894 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4895 windows_or_buffers_changed = 1;
4897 else if (XFASTINT (w->total_lines) > 1)
4899 /* Distribute the additional lines of the mini-window
4900 among the other windows. */
4901 Lisp_Object window;
4902 XSETWINDOW (window, w);
4903 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4909 /* Mark window cursors off for all windows in the window tree rooted
4910 at W by setting their phys_cursor_on_p flag to zero. Called from
4911 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4912 the frame are cleared. */
4914 void
4915 mark_window_cursors_off (struct window *w)
4917 while (w)
4919 if (!NILP (w->hchild))
4920 mark_window_cursors_off (XWINDOW (w->hchild));
4921 else if (!NILP (w->vchild))
4922 mark_window_cursors_off (XWINDOW (w->vchild));
4923 else
4924 w->phys_cursor_on_p = 0;
4926 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4931 /* Return number of lines of text (not counting mode lines) in W. */
4934 window_internal_height (struct window *w)
4936 int ht = XFASTINT (w->total_lines);
4938 if (!MINI_WINDOW_P (w))
4940 if (!NILP (w->parent)
4941 || !NILP (w->vchild)
4942 || !NILP (w->hchild)
4943 || !NILP (w->next)
4944 || !NILP (w->prev)
4945 || WINDOW_WANTS_MODELINE_P (w))
4946 --ht;
4948 if (WINDOW_WANTS_HEADER_LINE_P (w))
4949 --ht;
4952 return ht;
4955 /************************************************************************
4956 Window Scrolling
4957 ***********************************************************************/
4959 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4960 N screen-fulls, which is defined as the height of the window minus
4961 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4962 instead. Negative values of N mean scroll down. NOERROR non-zero
4963 means don't signal an error if we try to move over BEGV or ZV,
4964 respectively. */
4966 static void
4967 window_scroll (Lisp_Object window, int n, int whole, int noerror)
4969 immediate_quit = 1;
4971 /* If we must, use the pixel-based version which is much slower than
4972 the line-based one but can handle varying line heights. */
4973 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4974 window_scroll_pixel_based (window, n, whole, noerror);
4975 else
4976 window_scroll_line_based (window, n, whole, noerror);
4978 immediate_quit = 0;
4982 /* Implementation of window_scroll that works based on pixel line
4983 heights. See the comment of window_scroll for parameter
4984 descriptions. */
4986 static void
4987 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4989 struct it it;
4990 struct window *w = XWINDOW (window);
4991 struct text_pos start;
4992 int this_scroll_margin;
4993 /* True if we fiddled the window vscroll field without really scrolling. */
4994 int vscrolled = 0;
4995 int x, y, rtop, rbot, rowh, vpos;
4997 SET_TEXT_POS_FROM_MARKER (start, w->start);
4999 /* If PT is not visible in WINDOW, move back one half of
5000 the screen. Allow PT to be partially visible, otherwise
5001 something like (scroll-down 1) with PT in the line before
5002 the partially visible one would recenter. */
5004 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
5006 /* Move backward half the height of the window. Performance note:
5007 vmotion used here is about 10% faster, but would give wrong
5008 results for variable height lines. */
5009 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5010 it.current_y = it.last_visible_y;
5011 move_it_vertically_backward (&it, window_box_height (w) / 2);
5013 /* The function move_iterator_vertically may move over more than
5014 the specified y-distance. If it->w is small, e.g. a
5015 mini-buffer window, we may end up in front of the window's
5016 display area. This is the case when Start displaying at the
5017 start of the line containing PT in this case. */
5018 if (it.current_y <= 0)
5020 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
5021 move_it_vertically_backward (&it, 0);
5022 it.current_y = 0;
5025 start = it.current.pos;
5027 else if (auto_window_vscroll_p)
5029 if (rtop || rbot) /* partially visible */
5031 int px;
5032 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
5033 if (whole)
5034 dy = max ((window_box_height (w)
5035 - next_screen_context_lines * dy),
5036 dy);
5037 dy *= n;
5039 if (n < 0)
5041 /* Only vscroll backwards if already vscrolled forwards. */
5042 if (w->vscroll < 0 && rtop > 0)
5044 px = max (0, -w->vscroll - min (rtop, -dy));
5045 Fset_window_vscroll (window, make_number (px), Qt);
5046 return;
5049 if (n > 0)
5051 /* Do vscroll if already vscrolled or only display line. */
5052 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
5054 px = max (0, -w->vscroll + min (rbot, dy));
5055 Fset_window_vscroll (window, make_number (px), Qt);
5056 return;
5059 /* Maybe modify window start instead of scrolling. */
5060 if (rbot > 0 || w->vscroll < 0)
5062 EMACS_INT spos;
5064 Fset_window_vscroll (window, make_number (0), Qt);
5065 /* If there are other text lines above the current row,
5066 move window start to current row. Else to next row. */
5067 if (rbot > 0)
5068 spos = XINT (Fline_beginning_position (Qnil));
5069 else
5070 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
5071 set_marker_restricted (w->start, make_number (spos),
5072 w->buffer);
5073 w->start_at_line_beg = Qt;
5074 w->update_mode_line = Qt;
5075 XSETFASTINT (w->last_modified, 0);
5076 XSETFASTINT (w->last_overlay_modified, 0);
5077 /* Set force_start so that redisplay_window will run the
5078 window-scroll-functions. */
5079 w->force_start = Qt;
5080 return;
5084 /* Cancel previous vscroll. */
5085 Fset_window_vscroll (window, make_number (0), Qt);
5088 /* If scroll_preserve_screen_position is non-nil, we try to set
5089 point in the same window line as it is now, so get that line. */
5090 if (!NILP (Vscroll_preserve_screen_position))
5092 /* We preserve the goal pixel coordinate across consecutive
5093 calls to scroll-up, scroll-down and other commands that
5094 have the `scroll-command' property. This avoids the
5095 possibility of point becoming "stuck" on a tall line when
5096 scrolling by one line. */
5097 if (window_scroll_pixel_based_preserve_y < 0
5098 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5099 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5101 start_display (&it, w, start);
5102 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5103 window_scroll_pixel_based_preserve_y = it.current_y;
5104 window_scroll_pixel_based_preserve_x = it.current_x;
5107 else
5108 window_scroll_pixel_based_preserve_y
5109 = window_scroll_pixel_based_preserve_x = -1;
5111 /* Move iterator it from start the specified distance forward or
5112 backward. The result is the new window start. */
5113 start_display (&it, w, start);
5114 if (whole)
5116 EMACS_INT start_pos = IT_CHARPOS (it);
5117 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
5118 dy = max ((window_box_height (w)
5119 - next_screen_context_lines * dy),
5120 dy) * n;
5122 /* Note that move_it_vertically always moves the iterator to the
5123 start of a line. So, if the last line doesn't have a newline,
5124 we would end up at the start of the line ending at ZV. */
5125 if (dy <= 0)
5127 move_it_vertically_backward (&it, -dy);
5128 /* Ensure we actually do move, e.g. in case we are currently
5129 looking at an image that is taller that the window height. */
5130 while (start_pos == IT_CHARPOS (it)
5131 && start_pos > BEGV)
5132 move_it_by_lines (&it, -1);
5134 else if (dy > 0)
5136 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
5137 MOVE_TO_POS | MOVE_TO_Y);
5138 /* Ensure we actually do move, e.g. in case we are currently
5139 looking at an image that is taller that the window height. */
5140 while (start_pos == IT_CHARPOS (it)
5141 && start_pos < ZV)
5142 move_it_by_lines (&it, 1);
5145 else
5146 move_it_by_lines (&it, n);
5148 /* We failed if we find ZV is already on the screen (scrolling up,
5149 means there's nothing past the end), or if we can't start any
5150 earlier (scrolling down, means there's nothing past the top). */
5151 if ((n > 0 && IT_CHARPOS (it) == ZV)
5152 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5154 if (IT_CHARPOS (it) == ZV)
5156 if (it.current_y < it.last_visible_y
5157 && (it.current_y + it.max_ascent + it.max_descent
5158 > it.last_visible_y))
5160 /* The last line was only partially visible, make it fully
5161 visible. */
5162 w->vscroll = (it.last_visible_y
5163 - it.current_y + it.max_ascent + it.max_descent);
5164 adjust_glyphs (it.f);
5166 else if (noerror)
5167 return;
5168 else if (n < 0) /* could happen with empty buffers */
5169 xsignal0 (Qbeginning_of_buffer);
5170 else
5171 xsignal0 (Qend_of_buffer);
5173 else
5175 if (w->vscroll != 0)
5176 /* The first line was only partially visible, make it fully
5177 visible. */
5178 w->vscroll = 0;
5179 else if (noerror)
5180 return;
5181 else
5182 xsignal0 (Qbeginning_of_buffer);
5185 /* If control gets here, then we vscrolled. */
5187 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
5189 /* Don't try to change the window start below. */
5190 vscrolled = 1;
5193 if (! vscrolled)
5195 EMACS_INT pos = IT_CHARPOS (it);
5196 EMACS_INT bytepos;
5198 /* If in the middle of a multi-glyph character move forward to
5199 the next character. */
5200 if (in_display_vector_p (&it))
5202 ++pos;
5203 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5206 /* Set the window start, and set up the window for redisplay. */
5207 set_marker_restricted (w->start, make_number (pos),
5208 w->buffer);
5209 bytepos = XMARKER (w->start)->bytepos;
5210 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
5211 ? Qt : Qnil);
5212 w->update_mode_line = Qt;
5213 XSETFASTINT (w->last_modified, 0);
5214 XSETFASTINT (w->last_overlay_modified, 0);
5215 /* Set force_start so that redisplay_window will run the
5216 window-scroll-functions. */
5217 w->force_start = Qt;
5220 /* The rest of this function uses current_y in a nonstandard way,
5221 not including the height of the header line if any. */
5222 it.current_y = it.vpos = 0;
5224 /* Move PT out of scroll margins.
5225 This code wants current_y to be zero at the window start position
5226 even if there is a header line. */
5227 this_scroll_margin = max (0, scroll_margin);
5228 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
5229 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5231 if (n > 0)
5233 /* We moved the window start towards ZV, so PT may be now
5234 in the scroll margin at the top. */
5235 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5236 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5237 && (NILP (Vscroll_preserve_screen_position)
5238 || EQ (Vscroll_preserve_screen_position, Qt)))
5239 /* We found PT at a legitimate height. Leave it alone. */
5241 else if (window_scroll_pixel_based_preserve_y >= 0)
5243 /* If we have a header line, take account of it.
5244 This is necessary because we set it.current_y to 0, above. */
5245 move_it_to (&it, -1,
5246 window_scroll_pixel_based_preserve_x,
5247 window_scroll_pixel_based_preserve_y
5248 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5249 -1, MOVE_TO_Y | MOVE_TO_X);
5250 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5252 else
5254 while (it.current_y < this_scroll_margin)
5256 int prev = it.current_y;
5257 move_it_by_lines (&it, 1);
5258 if (prev == it.current_y)
5259 break;
5261 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5264 else if (n < 0)
5266 EMACS_INT charpos, bytepos;
5267 int partial_p;
5269 /* Save our position, for the
5270 window_scroll_pixel_based_preserve_y case. */
5271 charpos = IT_CHARPOS (it);
5272 bytepos = IT_BYTEPOS (it);
5274 /* We moved the window start towards BEGV, so PT may be now
5275 in the scroll margin at the bottom. */
5276 move_it_to (&it, PT, -1,
5277 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5278 - this_scroll_margin - 1),
5280 MOVE_TO_POS | MOVE_TO_Y);
5282 /* Save our position, in case it's correct. */
5283 charpos = IT_CHARPOS (it);
5284 bytepos = IT_BYTEPOS (it);
5286 /* See if point is on a partially visible line at the end. */
5287 if (it.what == IT_EOB)
5288 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5289 else
5291 move_it_by_lines (&it, 1);
5292 partial_p = it.current_y > it.last_visible_y;
5295 if (charpos == PT && !partial_p
5296 && (NILP (Vscroll_preserve_screen_position)
5297 || EQ (Vscroll_preserve_screen_position, Qt)))
5298 /* We found PT before we found the display margin, so PT is ok. */
5300 else if (window_scroll_pixel_based_preserve_y >= 0)
5302 SET_TEXT_POS_FROM_MARKER (start, w->start);
5303 start_display (&it, w, start);
5304 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5305 here because we called start_display again and did not
5306 alter it.current_y this time. */
5307 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5308 window_scroll_pixel_based_preserve_y, -1,
5309 MOVE_TO_Y | MOVE_TO_X);
5310 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5312 else
5314 if (partial_p)
5315 /* The last line was only partially visible, so back up two
5316 lines to make sure we're on a fully visible line. */
5318 move_it_by_lines (&it, -2);
5319 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5321 else
5322 /* No, the position we saved is OK, so use it. */
5323 SET_PT_BOTH (charpos, bytepos);
5329 /* Implementation of window_scroll that works based on screen lines.
5330 See the comment of window_scroll for parameter descriptions. */
5332 static void
5333 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
5335 register struct window *w = XWINDOW (window);
5336 /* Fvertical_motion enters redisplay, which can trigger
5337 fontification, which in turn can modify buffer text (e.g., if the
5338 fontification functions replace escape sequences with faces, as
5339 in `grep-mode-font-lock-keywords'). So we use a marker to record
5340 the old point position, to prevent crashes in SET_PT_BOTH. */
5341 Lisp_Object opoint_marker = Fpoint_marker ();
5342 register EMACS_INT pos, pos_byte;
5343 register int ht = window_internal_height (w);
5344 register Lisp_Object tem;
5345 int lose;
5346 Lisp_Object bolp;
5347 EMACS_INT startpos;
5348 Lisp_Object original_pos = Qnil;
5350 /* If scrolling screen-fulls, compute the number of lines to
5351 scroll from the window's height. */
5352 if (whole)
5353 n *= max (1, ht - next_screen_context_lines);
5355 startpos = marker_position (w->start);
5357 if (!NILP (Vscroll_preserve_screen_position))
5359 if (window_scroll_preserve_vpos <= 0
5360 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5361 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5363 struct position posit
5364 = *compute_motion (startpos, 0, 0, 0,
5365 PT, ht, 0,
5366 -1, XINT (w->hscroll),
5367 0, w);
5368 window_scroll_preserve_vpos = posit.vpos;
5369 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
5372 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5373 make_number (window_scroll_preserve_vpos));
5376 XSETFASTINT (tem, PT);
5377 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5379 if (NILP (tem))
5381 Fvertical_motion (make_number (- (ht / 2)), window);
5382 startpos = PT;
5385 SET_PT (startpos);
5386 lose = n < 0 && PT == BEGV;
5387 Fvertical_motion (make_number (n), window);
5388 pos = PT;
5389 pos_byte = PT_BYTE;
5390 bolp = Fbolp ();
5391 SET_PT_BOTH (marker_position (opoint_marker),
5392 marker_byte_position (opoint_marker));
5394 if (lose)
5396 if (noerror)
5397 return;
5398 else
5399 xsignal0 (Qbeginning_of_buffer);
5402 if (pos < ZV)
5404 int this_scroll_margin = scroll_margin;
5406 /* Don't use a scroll margin that is negative or too large. */
5407 if (this_scroll_margin < 0)
5408 this_scroll_margin = 0;
5410 if (XINT (w->total_lines) < 4 * scroll_margin)
5411 this_scroll_margin = XINT (w->total_lines) / 4;
5413 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5414 w->start_at_line_beg = bolp;
5415 w->update_mode_line = Qt;
5416 XSETFASTINT (w->last_modified, 0);
5417 XSETFASTINT (w->last_overlay_modified, 0);
5418 /* Set force_start so that redisplay_window will run
5419 the window-scroll-functions. */
5420 w->force_start = Qt;
5422 if (!NILP (Vscroll_preserve_screen_position)
5423 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5425 SET_PT_BOTH (pos, pos_byte);
5426 Fvertical_motion (original_pos, window);
5428 /* If we scrolled forward, put point enough lines down
5429 that it is outside the scroll margin. */
5430 else if (n > 0)
5432 int top_margin;
5434 if (this_scroll_margin > 0)
5436 SET_PT_BOTH (pos, pos_byte);
5437 Fvertical_motion (make_number (this_scroll_margin), window);
5438 top_margin = PT;
5440 else
5441 top_margin = pos;
5443 if (top_margin <= marker_position (opoint_marker))
5444 SET_PT_BOTH (marker_position (opoint_marker),
5445 marker_byte_position (opoint_marker));
5446 else if (!NILP (Vscroll_preserve_screen_position))
5448 SET_PT_BOTH (pos, pos_byte);
5449 Fvertical_motion (original_pos, window);
5451 else
5452 SET_PT (top_margin);
5454 else if (n < 0)
5456 int bottom_margin;
5458 /* If we scrolled backward, put point near the end of the window
5459 but not within the scroll margin. */
5460 SET_PT_BOTH (pos, pos_byte);
5461 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5462 if (XFASTINT (tem) == ht - this_scroll_margin)
5463 bottom_margin = PT;
5464 else
5465 bottom_margin = PT + 1;
5467 if (bottom_margin > marker_position (opoint_marker))
5468 SET_PT_BOTH (marker_position (opoint_marker),
5469 marker_byte_position (opoint_marker));
5470 else
5472 if (!NILP (Vscroll_preserve_screen_position))
5474 SET_PT_BOTH (pos, pos_byte);
5475 Fvertical_motion (original_pos, window);
5477 else
5478 Fvertical_motion (make_number (-1), window);
5482 else
5484 if (noerror)
5485 return;
5486 else
5487 xsignal0 (Qend_of_buffer);
5492 /* Scroll selected_window up or down. If N is nil, scroll a
5493 screen-full which is defined as the height of the window minus
5494 next_screen_context_lines. If N is the symbol `-', scroll.
5495 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5496 up. This is the guts of Fscroll_up and Fscroll_down. */
5498 static void
5499 scroll_command (Lisp_Object n, int direction)
5501 int count = SPECPDL_INDEX ();
5503 xassert (eabs (direction) == 1);
5505 /* If selected window's buffer isn't current, make it current for
5506 the moment. But don't screw up if window_scroll gets an error. */
5507 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5509 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5510 Fset_buffer (XWINDOW (selected_window)->buffer);
5512 /* Make redisplay consider other windows than just selected_window. */
5513 ++windows_or_buffers_changed;
5516 if (NILP (n))
5517 window_scroll (selected_window, direction, 1, 0);
5518 else if (EQ (n, Qminus))
5519 window_scroll (selected_window, -direction, 1, 0);
5520 else
5522 n = Fprefix_numeric_value (n);
5523 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5526 unbind_to (count, Qnil);
5529 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5530 doc: /* Scroll text of selected window upward ARG lines.
5531 If ARG is omitted or nil, scroll upward by a near full screen.
5532 A near full screen is `next-screen-context-lines' less than a full screen.
5533 Negative ARG means scroll downward.
5534 If ARG is the atom `-', scroll downward by nearly full screen.
5535 When calling from a program, supply as argument a number, nil, or `-'. */)
5536 (Lisp_Object arg)
5538 scroll_command (arg, 1);
5539 return Qnil;
5542 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5543 doc: /* Scroll text of selected window down ARG lines.
5544 If ARG is omitted or nil, scroll down by a near full screen.
5545 A near full screen is `next-screen-context-lines' less than a full screen.
5546 Negative ARG means scroll upward.
5547 If ARG is the atom `-', scroll upward by nearly full screen.
5548 When calling from a program, supply as argument a number, nil, or `-'. */)
5549 (Lisp_Object arg)
5551 scroll_command (arg, -1);
5552 return Qnil;
5555 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5556 doc: /* Return the other window for \"other window scroll\" commands.
5557 If `other-window-scroll-buffer' is non-nil, a window
5558 showing that buffer is used.
5559 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5560 specifies the window. This takes precedence over
5561 `other-window-scroll-buffer'. */)
5562 (void)
5564 Lisp_Object window;
5566 if (MINI_WINDOW_P (XWINDOW (selected_window))
5567 && !NILP (Vminibuf_scroll_window))
5568 window = Vminibuf_scroll_window;
5569 /* If buffer is specified, scroll that buffer. */
5570 else if (!NILP (Vother_window_scroll_buffer))
5572 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5573 if (NILP (window))
5574 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5576 else
5578 /* Nothing specified; look for a neighboring window on the same
5579 frame. */
5580 window = Fnext_window (selected_window, Qnil, Qnil);
5582 if (EQ (window, selected_window))
5583 /* That didn't get us anywhere; look for a window on another
5584 visible frame. */
5586 window = Fnext_window (window, Qnil, Qt);
5587 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5588 && ! EQ (window, selected_window));
5591 CHECK_LIVE_WINDOW (window);
5593 if (EQ (window, selected_window))
5594 error ("There is no other window");
5596 return window;
5599 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5600 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5601 A near full screen is `next-screen-context-lines' less than a full screen.
5602 The next window is the one below the current one; or the one at the top
5603 if the current one is at the bottom. Negative ARG means scroll downward.
5604 If ARG is the atom `-', scroll downward by nearly full screen.
5605 When calling from a program, supply as argument a number, nil, or `-'.
5607 If `other-window-scroll-buffer' is non-nil, scroll the window
5608 showing that buffer, popping the buffer up if necessary.
5609 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5610 specifies the window to scroll. This takes precedence over
5611 `other-window-scroll-buffer'. */)
5612 (Lisp_Object arg)
5614 Lisp_Object window;
5615 struct window *w;
5616 int count = SPECPDL_INDEX ();
5618 window = Fother_window_for_scrolling ();
5619 w = XWINDOW (window);
5621 /* Don't screw up if window_scroll gets an error. */
5622 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5623 ++windows_or_buffers_changed;
5625 Fset_buffer (w->buffer);
5626 SET_PT (marker_position (w->pointm));
5628 if (NILP (arg))
5629 window_scroll (window, 1, 1, 1);
5630 else if (EQ (arg, Qminus))
5631 window_scroll (window, -1, 1, 1);
5632 else
5634 if (CONSP (arg))
5635 arg = Fcar (arg);
5636 CHECK_NUMBER (arg);
5637 window_scroll (window, XINT (arg), 0, 1);
5640 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5641 unbind_to (count, Qnil);
5643 return Qnil;
5646 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5647 doc: /* Scroll selected window display ARG columns left.
5648 Default for ARG is window width minus 2.
5649 Value is the total amount of leftward horizontal scrolling in
5650 effect after the change.
5651 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5652 lower bound for automatic scrolling, i.e. automatic scrolling
5653 will not scroll a window to a column less than the value returned
5654 by this function. This happens in an interactive call. */)
5655 (register Lisp_Object arg, Lisp_Object set_minimum)
5657 Lisp_Object result;
5658 int hscroll;
5659 struct window *w = XWINDOW (selected_window);
5661 if (NILP (arg))
5662 XSETFASTINT (arg, window_body_cols (w) - 2);
5663 else
5664 arg = Fprefix_numeric_value (arg);
5666 hscroll = XINT (w->hscroll) + XINT (arg);
5667 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5669 if (!NILP (set_minimum))
5670 w->min_hscroll = w->hscroll;
5672 return result;
5675 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5676 doc: /* Scroll selected window display ARG columns right.
5677 Default for ARG is window width minus 2.
5678 Value is the total amount of leftward horizontal scrolling in
5679 effect after the change.
5680 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5681 lower bound for automatic scrolling, i.e. automatic scrolling
5682 will not scroll a window to a column less than the value returned
5683 by this function. This happens in an interactive call. */)
5684 (register Lisp_Object arg, Lisp_Object set_minimum)
5686 Lisp_Object result;
5687 int hscroll;
5688 struct window *w = XWINDOW (selected_window);
5690 if (NILP (arg))
5691 XSETFASTINT (arg, window_body_cols (w) - 2);
5692 else
5693 arg = Fprefix_numeric_value (arg);
5695 hscroll = XINT (w->hscroll) - XINT (arg);
5696 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5698 if (!NILP (set_minimum))
5699 w->min_hscroll = w->hscroll;
5701 return result;
5704 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5705 doc: /* Return the window which was selected when entering the minibuffer.
5706 Returns nil, if selected window is not a minibuffer window. */)
5707 (void)
5709 if (minibuf_level > 0
5710 && MINI_WINDOW_P (XWINDOW (selected_window))
5711 && WINDOW_LIVE_P (minibuf_selected_window))
5712 return minibuf_selected_window;
5714 return Qnil;
5717 /* Value is the number of lines actually displayed in window W,
5718 as opposed to its height. */
5720 static int
5721 displayed_window_lines (struct window *w)
5723 struct it it;
5724 struct text_pos start;
5725 int height = window_box_height (w);
5726 struct buffer *old_buffer;
5727 int bottom_y;
5729 if (XBUFFER (w->buffer) != current_buffer)
5731 old_buffer = current_buffer;
5732 set_buffer_internal (XBUFFER (w->buffer));
5734 else
5735 old_buffer = NULL;
5737 /* In case W->start is out of the accessible range, do something
5738 reasonable. This happens in Info mode when Info-scroll-down
5739 calls (recenter -1) while W->start is 1. */
5740 if (XMARKER (w->start)->charpos < BEGV)
5741 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5742 else if (XMARKER (w->start)->charpos > ZV)
5743 SET_TEXT_POS (start, ZV, ZV_BYTE);
5744 else
5745 SET_TEXT_POS_FROM_MARKER (start, w->start);
5747 start_display (&it, w, start);
5748 move_it_vertically (&it, height);
5749 bottom_y = line_bottom_y (&it);
5751 /* rms: On a non-window display,
5752 the value of it.vpos at the bottom of the screen
5753 seems to be 1 larger than window_box_height (w).
5754 This kludge fixes a bug whereby (move-to-window-line -1)
5755 when ZV is on the last screen line
5756 moves to the previous screen line instead of the last one. */
5757 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5758 height++;
5760 /* Add in empty lines at the bottom of the window. */
5761 if (bottom_y < height)
5763 int uy = FRAME_LINE_HEIGHT (it.f);
5764 it.vpos += (height - bottom_y + uy - 1) / uy;
5767 if (old_buffer)
5768 set_buffer_internal (old_buffer);
5770 return it.vpos;
5774 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5775 doc: /* Center point in selected window and maybe redisplay frame.
5776 With prefix argument ARG, recenter putting point on screen line ARG
5777 relative to the selected window. If ARG is negative, it counts up from the
5778 bottom of the window. (ARG should be less than the height of the window.)
5780 If ARG is omitted or nil, then recenter with point on the middle line of
5781 the selected window; if the variable `recenter-redisplay' is non-nil,
5782 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5783 is set to `grow-only', this resets the tool-bar's height to the minimum
5784 height needed); if `recenter-redisplay' has the special value `tty',
5785 then only tty frame are redrawn.
5787 Just C-u as prefix means put point in the center of the window
5788 and redisplay normally--don't erase and redraw the frame. */)
5789 (register Lisp_Object arg)
5791 struct window *w = XWINDOW (selected_window);
5792 struct buffer *buf = XBUFFER (w->buffer);
5793 struct buffer *obuf = current_buffer;
5794 int center_p = 0;
5795 EMACS_INT charpos, bytepos;
5796 int iarg IF_LINT (= 0);
5797 int this_scroll_margin;
5799 /* If redisplay is suppressed due to an error, try again. */
5800 obuf->display_error_modiff = 0;
5802 if (NILP (arg))
5804 if (!NILP (Vrecenter_redisplay)
5805 && (!EQ (Vrecenter_redisplay, Qtty)
5806 || !NILP (Ftty_type (selected_frame))))
5808 int i;
5810 /* Invalidate pixel data calculated for all compositions. */
5811 for (i = 0; i < n_compositions; i++)
5812 composition_table[i]->font = NULL;
5814 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5816 Fredraw_frame (WINDOW_FRAME (w));
5817 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5820 center_p = 1;
5822 else if (CONSP (arg)) /* Just C-u. */
5823 center_p = 1;
5824 else
5826 arg = Fprefix_numeric_value (arg);
5827 CHECK_NUMBER (arg);
5828 iarg = XINT (arg);
5831 set_buffer_internal (buf);
5833 /* Do this after making BUF current
5834 in case scroll_margin is buffer-local. */
5835 this_scroll_margin = max (0, scroll_margin);
5836 this_scroll_margin = min (this_scroll_margin,
5837 XFASTINT (w->total_lines) / 4);
5839 /* Handle centering on a graphical frame specially. Such frames can
5840 have variable-height lines and centering point on the basis of
5841 line counts would lead to strange effects. */
5842 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5844 if (center_p)
5846 struct it it;
5847 struct text_pos pt;
5849 SET_TEXT_POS (pt, PT, PT_BYTE);
5850 start_display (&it, w, pt);
5851 move_it_vertically_backward (&it, window_box_height (w) / 2);
5852 charpos = IT_CHARPOS (it);
5853 bytepos = IT_BYTEPOS (it);
5855 else if (iarg < 0)
5857 struct it it;
5858 struct text_pos pt;
5859 int nlines = -iarg;
5860 int extra_line_spacing;
5861 int h = window_box_height (w);
5863 iarg = - max (-iarg, this_scroll_margin);
5865 SET_TEXT_POS (pt, PT, PT_BYTE);
5866 start_display (&it, w, pt);
5868 /* Be sure we have the exact height of the full line containing PT. */
5869 move_it_by_lines (&it, 0);
5871 /* The amount of pixels we have to move back is the window
5872 height minus what's displayed in the line containing PT,
5873 and the lines below. */
5874 it.current_y = 0;
5875 it.vpos = 0;
5876 move_it_by_lines (&it, nlines);
5878 if (it.vpos == nlines)
5879 h -= it.current_y;
5880 else
5882 /* Last line has no newline */
5883 h -= line_bottom_y (&it);
5884 it.vpos++;
5887 /* Don't reserve space for extra line spacing of last line. */
5888 extra_line_spacing = it.max_extra_line_spacing;
5890 /* If we can't move down NLINES lines because we hit
5891 the end of the buffer, count in some empty lines. */
5892 if (it.vpos < nlines)
5894 nlines -= it.vpos;
5895 extra_line_spacing = it.extra_line_spacing;
5896 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5898 if (h <= 0)
5899 return Qnil;
5901 /* Now find the new top line (starting position) of the window. */
5902 start_display (&it, w, pt);
5903 it.current_y = 0;
5904 move_it_vertically_backward (&it, h);
5906 /* If extra line spacing is present, we may move too far
5907 back. This causes the last line to be only partially
5908 visible (which triggers redisplay to recenter that line
5909 in the middle), so move forward.
5910 But ignore extra line spacing on last line, as it is not
5911 considered to be part of the visible height of the line.
5913 h += extra_line_spacing;
5914 while (-it.current_y > h)
5915 move_it_by_lines (&it, 1);
5917 charpos = IT_CHARPOS (it);
5918 bytepos = IT_BYTEPOS (it);
5920 else
5922 struct position pos;
5924 iarg = max (iarg, this_scroll_margin);
5926 pos = *vmotion (PT, -iarg, w);
5927 charpos = pos.bufpos;
5928 bytepos = pos.bytepos;
5931 else
5933 struct position pos;
5934 int ht = window_internal_height (w);
5936 if (center_p)
5937 iarg = ht / 2;
5938 else if (iarg < 0)
5939 iarg += ht;
5941 /* Don't let it get into the margin at either top or bottom. */
5942 iarg = max (iarg, this_scroll_margin);
5943 iarg = min (iarg, ht - this_scroll_margin - 1);
5945 pos = *vmotion (PT, - iarg, w);
5946 charpos = pos.bufpos;
5947 bytepos = pos.bytepos;
5950 /* Set the new window start. */
5951 set_marker_both (w->start, w->buffer, charpos, bytepos);
5952 w->window_end_valid = Qnil;
5954 w->optional_new_start = Qt;
5956 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5957 w->start_at_line_beg = Qt;
5958 else
5959 w->start_at_line_beg = Qnil;
5961 set_buffer_internal (obuf);
5962 return Qnil;
5966 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5967 0, 1, 0,
5968 doc: /* Return the height in lines of the text display area of WINDOW.
5969 WINDOW defaults to the selected window.
5971 The return value does not include the mode line, any header line, nor
5972 any partial-height lines in the text display area. */)
5973 (Lisp_Object window)
5975 struct window *w = decode_window (window);
5976 int pixel_height = window_box_height (w);
5977 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5978 return make_number (line_height);
5983 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5984 1, 1, "P",
5985 doc: /* Position point relative to window.
5986 With no argument, position point at center of window.
5987 An argument specifies vertical position within the window;
5988 zero means top of window, negative means relative to bottom of window. */)
5989 (Lisp_Object arg)
5991 struct window *w = XWINDOW (selected_window);
5992 int lines, start;
5993 Lisp_Object window;
5994 #if 0
5995 int this_scroll_margin;
5996 #endif
5998 if (!(BUFFERP (w->buffer)
5999 && XBUFFER (w->buffer) == current_buffer))
6000 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
6001 when passed below to set_marker_both. */
6002 error ("move-to-window-line called from unrelated buffer");
6004 window = selected_window;
6005 start = marker_position (w->start);
6006 if (start < BEGV || start > ZV)
6008 int height = window_internal_height (w);
6009 Fvertical_motion (make_number (- (height / 2)), window);
6010 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
6011 w->start_at_line_beg = Fbolp ();
6012 w->force_start = Qt;
6014 else
6015 Fgoto_char (w->start);
6017 lines = displayed_window_lines (w);
6019 #if 0
6020 this_scroll_margin = max (0, scroll_margin);
6021 this_scroll_margin = min (this_scroll_margin, lines / 4);
6022 #endif
6024 if (NILP (arg))
6025 XSETFASTINT (arg, lines / 2);
6026 else
6028 int iarg = XINT (Fprefix_numeric_value (arg));
6030 if (iarg < 0)
6031 iarg = iarg + lines;
6033 #if 0 /* This code would prevent move-to-window-line from moving point
6034 to a place inside the scroll margins (which would cause the
6035 next redisplay to scroll). I wrote this code, but then concluded
6036 it is probably better not to install it. However, it is here
6037 inside #if 0 so as not to lose it. -- rms. */
6039 /* Don't let it get into the margin at either top or bottom. */
6040 iarg = max (iarg, this_scroll_margin);
6041 iarg = min (iarg, lines - this_scroll_margin - 1);
6042 #endif
6044 arg = make_number (iarg);
6047 /* Skip past a partially visible first line. */
6048 if (w->vscroll)
6049 XSETINT (arg, XINT (arg) + 1);
6051 return Fvertical_motion (arg, window);
6056 /***********************************************************************
6057 Window Configuration
6058 ***********************************************************************/
6060 struct save_window_data
6062 struct vectorlike_header header;
6063 Lisp_Object selected_frame;
6064 Lisp_Object current_window;
6065 Lisp_Object current_buffer;
6066 Lisp_Object minibuf_scroll_window;
6067 Lisp_Object minibuf_selected_window;
6068 Lisp_Object root_window;
6069 Lisp_Object focus_frame;
6070 /* A vector, each of whose elements is a struct saved_window
6071 for one window. */
6072 Lisp_Object saved_windows;
6074 /* All fields above are traced by the GC.
6075 From `fame-cols' down, the fields are ignored by the GC. */
6077 int frame_cols, frame_lines, frame_menu_bar_lines;
6078 int frame_tool_bar_lines;
6081 /* This is saved as a Lisp_Vector */
6082 struct saved_window
6084 struct vectorlike_header header;
6085 Lisp_Object window;
6086 Lisp_Object buffer, start, pointm, mark;
6087 Lisp_Object left_col, top_line, total_cols, total_lines;
6088 Lisp_Object hscroll, min_hscroll;
6089 Lisp_Object parent, prev;
6090 Lisp_Object start_at_line_beg;
6091 Lisp_Object display_table;
6092 Lisp_Object orig_top_line, orig_total_lines;
6093 Lisp_Object left_margin_cols, right_margin_cols;
6094 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
6095 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
6096 Lisp_Object dedicated, resize_proportionally;
6099 #define SAVED_WINDOW_N(swv,n) \
6100 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
6102 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
6103 doc: /* Return t if OBJECT is a window-configuration object. */)
6104 (Lisp_Object object)
6106 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
6109 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
6110 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
6111 (Lisp_Object config)
6113 register struct save_window_data *data;
6114 struct Lisp_Vector *saved_windows;
6116 CHECK_WINDOW_CONFIGURATION (config);
6118 data = (struct save_window_data *) XVECTOR (config);
6119 saved_windows = XVECTOR (data->saved_windows);
6120 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6123 DEFUN ("set-window-configuration", Fset_window_configuration,
6124 Sset_window_configuration, 1, 1, 0,
6125 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
6126 CONFIGURATION must be a value previously returned
6127 by `current-window-configuration' (which see).
6128 If CONFIGURATION was made from a frame that is now deleted,
6129 only frame-independent values can be restored. In this case,
6130 the return value is nil. Otherwise the value is t. */)
6131 (Lisp_Object configuration)
6133 register struct save_window_data *data;
6134 struct Lisp_Vector *saved_windows;
6135 Lisp_Object new_current_buffer;
6136 Lisp_Object frame;
6137 FRAME_PTR f;
6138 EMACS_INT old_point = -1;
6140 CHECK_WINDOW_CONFIGURATION (configuration);
6142 data = (struct save_window_data *) XVECTOR (configuration);
6143 saved_windows = XVECTOR (data->saved_windows);
6145 new_current_buffer = data->current_buffer;
6146 if (NILP (BVAR (XBUFFER (new_current_buffer), name)))
6147 new_current_buffer = Qnil;
6148 else
6150 if (XBUFFER (new_current_buffer) == current_buffer)
6151 /* The code further down "preserves point" by saving here PT in
6152 old_point and then setting it later back into PT. When the
6153 current-selected-window and the final-selected-window both show
6154 the current buffer, this suffers from the problem that the
6155 current PT is the window-point of the current-selected-window,
6156 while the final PT is the point of the final-selected-window, so
6157 this copy from one PT to the other would end up moving the
6158 window-point of the final-selected-window to the window-point of
6159 the current-selected-window. So we have to be careful which
6160 point of the current-buffer we copy into old_point. */
6161 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6162 && WINDOWP (selected_window)
6163 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
6164 && !EQ (selected_window, data->current_window))
6165 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6166 else
6167 old_point = PT;
6168 else
6169 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6170 point in new_current_buffer as of the last time this buffer was
6171 used. This can be non-deterministic since it can be changed by
6172 things like jit-lock by mere temporary selection of some random
6173 window that happens to show this buffer.
6174 So if possible we want this arbitrary choice of "which point" to
6175 be the one from the to-be-selected-window so as to prevent this
6176 window's cursor from being copied from another window. */
6177 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6178 /* If current_window = selected_window, its point is in BUF_PT. */
6179 && !EQ (selected_window, data->current_window))
6180 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6181 else
6182 old_point = BUF_PT (XBUFFER (new_current_buffer));
6185 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6186 f = XFRAME (frame);
6188 /* If f is a dead frame, don't bother rebuilding its window tree.
6189 However, there is other stuff we should still try to do below. */
6190 if (FRAME_LIVE_P (f))
6192 register struct window *w;
6193 register struct saved_window *p;
6194 struct window *root_window;
6195 struct window **leaf_windows;
6196 int n_leaf_windows;
6197 int k, i, n;
6199 /* If the frame has been resized since this window configuration was
6200 made, we change the frame to the size specified in the
6201 configuration, restore the configuration, and then resize it
6202 back. We keep track of the prevailing height in these variables. */
6203 int previous_frame_lines = FRAME_LINES (f);
6204 int previous_frame_cols = FRAME_COLS (f);
6205 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6206 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6208 /* The mouse highlighting code could get screwed up
6209 if it runs during this. */
6210 BLOCK_INPUT;
6212 if (data->frame_lines != previous_frame_lines
6213 || data->frame_cols != previous_frame_cols)
6214 change_frame_size (f, data->frame_lines,
6215 data->frame_cols, 0, 0, 0);
6216 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6217 if (data->frame_menu_bar_lines
6218 != previous_frame_menu_bar_lines)
6219 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6220 make_number (0));
6221 #ifdef HAVE_WINDOW_SYSTEM
6222 if (data->frame_tool_bar_lines
6223 != previous_frame_tool_bar_lines)
6224 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6225 make_number (0));
6226 #endif
6227 #endif
6229 /* "Swap out" point from the selected window's buffer
6230 into the window itself. (Normally the pointm of the selected
6231 window holds garbage.) We do this now, before
6232 restoring the window contents, and prevent it from
6233 being done later on when we select a new window. */
6234 if (! NILP (XWINDOW (selected_window)->buffer))
6236 w = XWINDOW (selected_window);
6237 set_marker_both (w->pointm,
6238 w->buffer,
6239 BUF_PT (XBUFFER (w->buffer)),
6240 BUF_PT_BYTE (XBUFFER (w->buffer)));
6243 windows_or_buffers_changed++;
6244 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6246 /* Problem: Freeing all matrices and later allocating them again
6247 is a serious redisplay flickering problem. What we would
6248 really like to do is to free only those matrices not reused
6249 below. */
6250 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6251 leaf_windows
6252 = (struct window **) alloca (count_windows (root_window)
6253 * sizeof (struct window *));
6254 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6256 /* Kludge Alert!
6257 Mark all windows now on frame as "deleted".
6258 Restoring the new configuration "undeletes" any that are in it.
6260 Save their current buffers in their height fields, since we may
6261 need it later, if a buffer saved in the configuration is now
6262 dead. */
6263 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6265 for (k = 0; k < saved_windows->header.size; k++)
6267 p = SAVED_WINDOW_N (saved_windows, k);
6268 w = XWINDOW (p->window);
6269 w->next = Qnil;
6271 if (!NILP (p->parent))
6272 w->parent = SAVED_WINDOW_N (saved_windows,
6273 XFASTINT (p->parent))->window;
6274 else
6275 w->parent = Qnil;
6277 if (!NILP (p->prev))
6279 w->prev = SAVED_WINDOW_N (saved_windows,
6280 XFASTINT (p->prev))->window;
6281 XWINDOW (w->prev)->next = p->window;
6283 else
6285 w->prev = Qnil;
6286 if (!NILP (w->parent))
6288 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6290 XWINDOW (w->parent)->vchild = p->window;
6291 XWINDOW (w->parent)->hchild = Qnil;
6293 else
6295 XWINDOW (w->parent)->hchild = p->window;
6296 XWINDOW (w->parent)->vchild = Qnil;
6301 /* If we squirreled away the buffer in the window's height,
6302 restore it now. */
6303 if (BUFFERP (w->total_lines))
6304 w->buffer = w->total_lines;
6305 w->left_col = p->left_col;
6306 w->top_line = p->top_line;
6307 w->total_cols = p->total_cols;
6308 w->total_lines = p->total_lines;
6309 w->hscroll = p->hscroll;
6310 w->min_hscroll = p->min_hscroll;
6311 w->display_table = p->display_table;
6312 w->orig_top_line = p->orig_top_line;
6313 w->orig_total_lines = p->orig_total_lines;
6314 w->left_margin_cols = p->left_margin_cols;
6315 w->right_margin_cols = p->right_margin_cols;
6316 w->left_fringe_width = p->left_fringe_width;
6317 w->right_fringe_width = p->right_fringe_width;
6318 w->fringes_outside_margins = p->fringes_outside_margins;
6319 w->scroll_bar_width = p->scroll_bar_width;
6320 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6321 w->dedicated = p->dedicated;
6322 w->resize_proportionally = p->resize_proportionally;
6323 XSETFASTINT (w->last_modified, 0);
6324 XSETFASTINT (w->last_overlay_modified, 0);
6326 /* Reinstall the saved buffer and pointers into it. */
6327 if (NILP (p->buffer))
6328 w->buffer = p->buffer;
6329 else
6331 if (!NILP (BVAR (XBUFFER (p->buffer), name)))
6332 /* If saved buffer is alive, install it. */
6334 w->buffer = p->buffer;
6335 w->start_at_line_beg = p->start_at_line_beg;
6336 set_marker_restricted (w->start, p->start, w->buffer);
6337 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6338 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
6339 p->mark, w->buffer);
6341 /* As documented in Fcurrent_window_configuration, don't
6342 restore the location of point in the buffer which was
6343 current when the window configuration was recorded. */
6344 if (!EQ (p->buffer, new_current_buffer)
6345 && XBUFFER (p->buffer) == current_buffer)
6346 Fgoto_char (w->pointm);
6348 else if (NILP (w->buffer) || NILP (BVAR (XBUFFER (w->buffer), name)))
6349 /* Else unless window has a live buffer, get one. */
6351 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6352 /* This will set the markers to beginning of visible
6353 range. */
6354 set_marker_restricted (w->start, make_number (0), w->buffer);
6355 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6356 w->start_at_line_beg = Qt;
6358 else
6359 /* Keeping window's old buffer; make sure the markers
6360 are real. */
6362 /* Set window markers at start of visible range. */
6363 if (XMARKER (w->start)->buffer == 0)
6364 set_marker_restricted (w->start, make_number (0),
6365 w->buffer);
6366 if (XMARKER (w->pointm)->buffer == 0)
6367 set_marker_restricted_both (w->pointm, w->buffer,
6368 BUF_PT (XBUFFER (w->buffer)),
6369 BUF_PT_BYTE (XBUFFER (w->buffer)));
6370 w->start_at_line_beg = Qt;
6375 FRAME_ROOT_WINDOW (f) = data->root_window;
6377 /* Arrange *not* to restore point in the buffer that was
6378 current when the window configuration was saved. */
6379 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6380 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6381 make_number (old_point),
6382 XWINDOW (data->current_window)->buffer);
6384 /* In the following call to `select-window, prevent "swapping
6385 out point" in the old selected window using the buffer that
6386 has been restored into it. We already swapped out that point
6387 from that window's old buffer. */
6388 select_window (data->current_window, Qnil, 1);
6389 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
6390 = selected_window;
6392 if (NILP (data->focus_frame)
6393 || (FRAMEP (data->focus_frame)
6394 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6395 Fredirect_frame_focus (frame, data->focus_frame);
6397 /* Set the screen height to the value it had before this function. */
6398 if (previous_frame_lines != FRAME_LINES (f)
6399 || previous_frame_cols != FRAME_COLS (f))
6400 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6401 0, 0, 0);
6402 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6403 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6404 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6405 make_number (0));
6406 #ifdef HAVE_WINDOW_SYSTEM
6407 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6408 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6409 make_number (0));
6410 #endif
6411 #endif
6413 /* Now, free glyph matrices in windows that were not reused. */
6414 for (i = n = 0; i < n_leaf_windows; ++i)
6416 if (NILP (leaf_windows[i]->buffer))
6418 /* Assert it's not reused as a combination. */
6419 xassert (NILP (leaf_windows[i]->hchild)
6420 && NILP (leaf_windows[i]->vchild));
6421 free_window_matrices (leaf_windows[i]);
6423 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6424 ++n;
6427 adjust_glyphs (f);
6429 UNBLOCK_INPUT;
6431 /* Fselect_window will have made f the selected frame, so we
6432 reselect the proper frame here. Fhandle_switch_frame will change the
6433 selected window too, but that doesn't make the call to
6434 Fselect_window above totally superfluous; it still sets f's
6435 selected window. */
6436 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6437 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6439 run_window_configuration_change_hook (f);
6442 if (!NILP (new_current_buffer))
6443 Fset_buffer (new_current_buffer);
6445 Vminibuf_scroll_window = data->minibuf_scroll_window;
6446 minibuf_selected_window = data->minibuf_selected_window;
6448 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6451 /* Mark all windows now on frame as deleted
6452 by setting their buffers to nil. */
6454 void
6455 delete_all_subwindows (register struct window *w)
6457 if (!NILP (w->next))
6458 delete_all_subwindows (XWINDOW (w->next));
6459 if (!NILP (w->vchild))
6460 delete_all_subwindows (XWINDOW (w->vchild));
6461 if (!NILP (w->hchild))
6462 delete_all_subwindows (XWINDOW (w->hchild));
6464 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6466 if (!NILP (w->buffer))
6467 unshow_buffer (w);
6469 /* We set all three of these fields to nil, to make sure that we can
6470 distinguish this dead window from any live window. Live leaf
6471 windows will have buffer set, and combination windows will have
6472 vchild or hchild set. */
6473 w->buffer = Qnil;
6474 w->vchild = Qnil;
6475 w->hchild = Qnil;
6477 Vwindow_list = Qnil;
6480 static int
6481 count_windows (register struct window *window)
6483 register int count = 1;
6484 if (!NILP (window->next))
6485 count += count_windows (XWINDOW (window->next));
6486 if (!NILP (window->vchild))
6487 count += count_windows (XWINDOW (window->vchild));
6488 if (!NILP (window->hchild))
6489 count += count_windows (XWINDOW (window->hchild));
6490 return count;
6494 /* Fill vector FLAT with leaf windows under W, starting at index I.
6495 Value is last index + 1. */
6497 static int
6498 get_leaf_windows (struct window *w, struct window **flat, int i)
6500 while (w)
6502 if (!NILP (w->hchild))
6503 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6504 else if (!NILP (w->vchild))
6505 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6506 else
6507 flat[i++] = w;
6509 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6512 return i;
6516 /* Return a pointer to the glyph W's physical cursor is on. Value is
6517 null if W's current matrix is invalid, so that no meaningfull glyph
6518 can be returned. */
6520 struct glyph *
6521 get_phys_cursor_glyph (struct window *w)
6523 struct glyph_row *row;
6524 struct glyph *glyph;
6526 if (w->phys_cursor.vpos >= 0
6527 && w->phys_cursor.vpos < w->current_matrix->nrows
6528 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6529 row->enabled_p)
6530 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6531 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6532 else
6533 glyph = NULL;
6535 return glyph;
6539 static int
6540 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6542 register struct saved_window *p;
6543 register struct window *w;
6544 register Lisp_Object tem;
6546 for (;!NILP (window); window = w->next)
6548 p = SAVED_WINDOW_N (vector, i);
6549 w = XWINDOW (window);
6551 XSETFASTINT (w->temslot, i); i++;
6552 p->window = window;
6553 p->buffer = w->buffer;
6554 p->left_col = w->left_col;
6555 p->top_line = w->top_line;
6556 p->total_cols = w->total_cols;
6557 p->total_lines = w->total_lines;
6558 p->hscroll = w->hscroll;
6559 p->min_hscroll = w->min_hscroll;
6560 p->display_table = w->display_table;
6561 p->orig_top_line = w->orig_top_line;
6562 p->orig_total_lines = w->orig_total_lines;
6563 p->left_margin_cols = w->left_margin_cols;
6564 p->right_margin_cols = w->right_margin_cols;
6565 p->left_fringe_width = w->left_fringe_width;
6566 p->right_fringe_width = w->right_fringe_width;
6567 p->fringes_outside_margins = w->fringes_outside_margins;
6568 p->scroll_bar_width = w->scroll_bar_width;
6569 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6570 p->dedicated = w->dedicated;
6571 p->resize_proportionally = w->resize_proportionally;
6572 if (!NILP (w->buffer))
6574 /* Save w's value of point in the window configuration.
6575 If w is the selected window, then get the value of point
6576 from the buffer; pointm is garbage in the selected window. */
6577 if (EQ (window, selected_window))
6579 p->pointm = Fmake_marker ();
6580 set_marker_both (p->pointm, w->buffer,
6581 BUF_PT (XBUFFER (w->buffer)),
6582 BUF_PT_BYTE (XBUFFER (w->buffer)));
6584 else
6585 p->pointm = Fcopy_marker (w->pointm, Qnil);
6587 p->start = Fcopy_marker (w->start, Qnil);
6588 p->start_at_line_beg = w->start_at_line_beg;
6590 tem = BVAR (XBUFFER (w->buffer), mark);
6591 p->mark = Fcopy_marker (tem, Qnil);
6593 else
6595 p->pointm = Qnil;
6596 p->start = Qnil;
6597 p->mark = Qnil;
6598 p->start_at_line_beg = Qnil;
6601 if (NILP (w->parent))
6602 p->parent = Qnil;
6603 else
6604 p->parent = XWINDOW (w->parent)->temslot;
6606 if (NILP (w->prev))
6607 p->prev = Qnil;
6608 else
6609 p->prev = XWINDOW (w->prev)->temslot;
6611 if (!NILP (w->vchild))
6612 i = save_window_save (w->vchild, vector, i);
6613 if (!NILP (w->hchild))
6614 i = save_window_save (w->hchild, vector, i);
6617 return i;
6620 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6621 Scurrent_window_configuration, 0, 1, 0,
6622 doc: /* Return an object representing the current window configuration of FRAME.
6623 If FRAME is nil or omitted, use the selected frame.
6624 This describes the number of windows, their sizes and current buffers,
6625 and for each displayed buffer, where display starts, and the positions of
6626 point and mark. An exception is made for point in the current buffer:
6627 its value is -not- saved.
6628 This also records the currently selected frame, and FRAME's focus
6629 redirection (see `redirect-frame-focus'). */)
6630 (Lisp_Object frame)
6632 register Lisp_Object tem;
6633 register int n_windows;
6634 register struct save_window_data *data;
6635 register int i;
6636 FRAME_PTR f;
6638 if (NILP (frame))
6639 frame = selected_frame;
6640 CHECK_LIVE_FRAME (frame);
6641 f = XFRAME (frame);
6643 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6644 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6645 PVEC_WINDOW_CONFIGURATION);
6647 data->frame_cols = FRAME_COLS (f);
6648 data->frame_lines = FRAME_LINES (f);
6649 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6650 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6651 data->selected_frame = selected_frame;
6652 data->current_window = FRAME_SELECTED_WINDOW (f);
6653 XSETBUFFER (data->current_buffer, current_buffer);
6654 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6655 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6656 data->root_window = FRAME_ROOT_WINDOW (f);
6657 data->focus_frame = FRAME_FOCUS_FRAME (f);
6658 tem = Fmake_vector (make_number (n_windows), Qnil);
6659 data->saved_windows = tem;
6660 for (i = 0; i < n_windows; i++)
6661 XVECTOR (tem)->contents[i]
6662 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6663 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6664 XSETWINDOW_CONFIGURATION (tem, data);
6665 return (tem);
6669 /***********************************************************************
6670 Window Split Tree
6671 ***********************************************************************/
6673 static Lisp_Object
6674 window_tree (struct window *w)
6676 Lisp_Object tail = Qnil;
6677 Lisp_Object result = Qnil;
6679 while (w)
6681 Lisp_Object wn;
6683 XSETWINDOW (wn, w);
6684 if (!NILP (w->hchild))
6685 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6686 window_tree (XWINDOW (w->hchild))));
6687 else if (!NILP (w->vchild))
6688 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6689 window_tree (XWINDOW (w->vchild))));
6691 if (NILP (result))
6693 result = tail = Fcons (wn, Qnil);
6695 else
6697 XSETCDR (tail, Fcons (wn, Qnil));
6698 tail = XCDR (tail);
6701 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6704 return result;
6709 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6710 0, 1, 0,
6711 doc: /* Return the window tree for frame FRAME.
6713 The return value is a list of the form (ROOT MINI), where ROOT
6714 represents the window tree of the frame's root window, and MINI
6715 is the frame's minibuffer window.
6717 If the root window is not split, ROOT is the root window itself.
6718 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6719 horizontal split, and t for a vertical split, EDGES gives the combined
6720 size and position of the subwindows in the split, and the rest of the
6721 elements are the subwindows in the split. Each of the subwindows may
6722 again be a window or a list representing a window split, and so on.
6723 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6725 If FRAME is nil or omitted, return information on the currently
6726 selected frame. */)
6727 (Lisp_Object frame)
6729 FRAME_PTR f;
6731 if (NILP (frame))
6732 frame = selected_frame;
6734 CHECK_FRAME (frame);
6735 f = XFRAME (frame);
6737 if (!FRAME_LIVE_P (f))
6738 return Qnil;
6740 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6744 /***********************************************************************
6745 Marginal Areas
6746 ***********************************************************************/
6748 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6749 2, 3, 0,
6750 doc: /* Set width of marginal areas of window WINDOW.
6751 If WINDOW is nil, set margins of the currently selected window.
6752 Second arg LEFT-WIDTH specifies the number of character cells to
6753 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6754 does the same for the right marginal area. A nil width parameter
6755 means no margin. */)
6756 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6758 struct window *w = decode_window (window);
6760 /* Translate negative or zero widths to nil.
6761 Margins that are too wide have to be checked elsewhere. */
6763 if (!NILP (left_width))
6765 CHECK_NUMBER (left_width);
6766 if (XINT (left_width) <= 0)
6767 left_width = Qnil;
6770 if (!NILP (right_width))
6772 CHECK_NUMBER (right_width);
6773 if (XINT (right_width) <= 0)
6774 right_width = Qnil;
6777 if (!EQ (w->left_margin_cols, left_width)
6778 || !EQ (w->right_margin_cols, right_width))
6780 w->left_margin_cols = left_width;
6781 w->right_margin_cols = right_width;
6783 adjust_window_margins (w);
6785 ++windows_or_buffers_changed;
6786 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6789 return Qnil;
6793 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6794 0, 1, 0,
6795 doc: /* Get width of marginal areas of window WINDOW.
6796 If WINDOW is omitted or nil, use the currently selected window.
6797 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6798 If a marginal area does not exist, its width will be returned
6799 as nil. */)
6800 (Lisp_Object window)
6802 struct window *w = decode_window (window);
6803 return Fcons (w->left_margin_cols, w->right_margin_cols);
6808 /***********************************************************************
6809 Fringes
6810 ***********************************************************************/
6812 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6813 2, 4, 0,
6814 doc: /* Set the fringe widths of window WINDOW.
6815 If WINDOW is nil, set the fringe widths of the currently selected
6816 window.
6817 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6818 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6819 fringe width. If a fringe width arg is nil, that means to use the
6820 frame's default fringe width. Default fringe widths can be set with
6821 the command `set-fringe-style'.
6822 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6823 outside of the display margins. By default, fringes are drawn between
6824 display marginal areas and the text area. */)
6825 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6827 struct window *w = decode_window (window);
6829 if (!NILP (left_width))
6830 CHECK_NATNUM (left_width);
6831 if (!NILP (right_width))
6832 CHECK_NATNUM (right_width);
6834 /* Do nothing on a tty. */
6835 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6836 && (!EQ (w->left_fringe_width, left_width)
6837 || !EQ (w->right_fringe_width, right_width)
6838 || !EQ (w->fringes_outside_margins, outside_margins)))
6840 w->left_fringe_width = left_width;
6841 w->right_fringe_width = right_width;
6842 w->fringes_outside_margins = outside_margins;
6844 adjust_window_margins (w);
6846 clear_glyph_matrix (w->current_matrix);
6847 w->window_end_valid = Qnil;
6849 ++windows_or_buffers_changed;
6850 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6853 return Qnil;
6857 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6858 0, 1, 0,
6859 doc: /* Get width of fringes of window WINDOW.
6860 If WINDOW is omitted or nil, use the currently selected window.
6861 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6862 (Lisp_Object window)
6864 struct window *w = decode_window (window);
6866 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6867 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6868 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6869 ? Qt : Qnil), Qnil)));
6874 /***********************************************************************
6875 Scroll bars
6876 ***********************************************************************/
6878 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6879 Sset_window_scroll_bars, 2, 4, 0,
6880 doc: /* Set width and type of scroll bars of window WINDOW.
6881 If window is nil, set scroll bars of the currently selected window.
6882 Second parameter WIDTH specifies the pixel width for the scroll bar;
6883 this is automatically adjusted to a multiple of the frame column width.
6884 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6885 bar: left, right, or nil.
6886 If WIDTH is nil, use the frame's scroll-bar width.
6887 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6888 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6889 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6891 struct window *w = decode_window (window);
6893 if (!NILP (width))
6895 CHECK_NATNUM (width);
6897 if (XINT (width) == 0)
6898 vertical_type = Qnil;
6901 if (!(NILP (vertical_type)
6902 || EQ (vertical_type, Qleft)
6903 || EQ (vertical_type, Qright)
6904 || EQ (vertical_type, Qt)))
6905 error ("Invalid type of vertical scroll bar");
6907 if (!EQ (w->scroll_bar_width, width)
6908 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6910 w->scroll_bar_width = width;
6911 w->vertical_scroll_bar_type = vertical_type;
6913 adjust_window_margins (w);
6915 clear_glyph_matrix (w->current_matrix);
6916 w->window_end_valid = Qnil;
6918 ++windows_or_buffers_changed;
6919 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6922 return Qnil;
6926 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6927 0, 1, 0,
6928 doc: /* Get width and type of scroll bars of window WINDOW.
6929 If WINDOW is omitted or nil, use the currently selected window.
6930 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6931 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6932 value. */)
6933 (Lisp_Object window)
6935 struct window *w = decode_window (window);
6936 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6937 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6938 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6939 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6940 Fcons (w->vertical_scroll_bar_type,
6941 Fcons (Qnil, Qnil))));
6946 /***********************************************************************
6947 Smooth scrolling
6948 ***********************************************************************/
6950 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6951 doc: /* Return the amount by which WINDOW is scrolled vertically.
6952 Use the selected window if WINDOW is nil or omitted.
6953 Normally, value is a multiple of the canonical character height of WINDOW;
6954 optional second arg PIXELS-P means value is measured in pixels. */)
6955 (Lisp_Object window, Lisp_Object pixels_p)
6957 Lisp_Object result;
6958 struct frame *f;
6959 struct window *w;
6961 if (NILP (window))
6962 window = selected_window;
6963 else
6964 CHECK_WINDOW (window);
6965 w = XWINDOW (window);
6966 f = XFRAME (w->frame);
6968 if (FRAME_WINDOW_P (f))
6969 result = (NILP (pixels_p)
6970 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6971 : make_number (-w->vscroll));
6972 else
6973 result = make_number (0);
6974 return result;
6978 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6979 2, 3, 0,
6980 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6981 WINDOW nil means use the selected window. Normally, VSCROLL is a
6982 non-negative multiple of the canonical character height of WINDOW;
6983 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6984 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6985 corresponds to an integral number of pixels. The return value is the
6986 result of this rounding.
6987 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6988 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6990 struct window *w;
6991 struct frame *f;
6993 if (NILP (window))
6994 window = selected_window;
6995 else
6996 CHECK_WINDOW (window);
6997 CHECK_NUMBER_OR_FLOAT (vscroll);
6999 w = XWINDOW (window);
7000 f = XFRAME (w->frame);
7002 if (FRAME_WINDOW_P (f))
7004 int old_dy = w->vscroll;
7006 w->vscroll = - (NILP (pixels_p)
7007 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
7008 : XFLOATINT (vscroll));
7009 w->vscroll = min (w->vscroll, 0);
7011 if (w->vscroll != old_dy)
7013 /* Adjust glyph matrix of the frame if the virtual display
7014 area becomes larger than before. */
7015 if (w->vscroll < 0 && w->vscroll < old_dy)
7016 adjust_glyphs (f);
7018 /* Prevent redisplay shortcuts. */
7019 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
7023 return Fwindow_vscroll (window, pixels_p);
7027 /* Call FN for all leaf windows on frame F. FN is called with the
7028 first argument being a pointer to the leaf window, and with
7029 additional argument USER_DATA. Stops when FN returns 0. */
7031 static void
7032 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
7033 void *user_data)
7035 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
7036 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
7037 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
7041 /* Helper function for foreach_window. Call FN for all leaf windows
7042 reachable from W. FN is called with the first argument being a
7043 pointer to the leaf window, and with additional argument USER_DATA.
7044 Stop when FN returns 0. Value is 0 if stopped by FN. */
7046 static int
7047 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
7049 int cont;
7051 for (cont = 1; w && cont;)
7053 if (!NILP (w->hchild))
7054 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
7055 else if (!NILP (w->vchild))
7056 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
7057 else
7058 cont = fn (w, user_data);
7060 w = NILP (w->next) ? 0 : XWINDOW (w->next);
7063 return cont;
7067 /* Freeze or unfreeze the window start of W unless it is a
7068 mini-window or the selected window. FREEZE_P non-null means freeze
7069 the window start. */
7071 static int
7072 freeze_window_start (struct window *w, void *freeze_p)
7074 if (MINI_WINDOW_P (w)
7075 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
7076 && (w == XWINDOW (selected_window)
7077 || (MINI_WINDOW_P (XWINDOW (selected_window))
7078 && ! NILP (Vminibuf_scroll_window)
7079 && w == XWINDOW (Vminibuf_scroll_window)))))
7080 freeze_p = NULL;
7082 w->frozen_window_start_p = freeze_p != NULL;
7083 return 1;
7087 /* Freeze or unfreeze the window starts of all leaf windows on frame
7088 F, except the selected window and a mini-window. FREEZE_P non-zero
7089 means freeze the window start. */
7091 void
7092 freeze_window_starts (struct frame *f, int freeze_p)
7094 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
7098 /***********************************************************************
7099 Initialization
7100 ***********************************************************************/
7102 /* Return 1 if window configurations C1 and C2
7103 describe the same state of affairs. This is used by Fequal. */
7106 compare_window_configurations (Lisp_Object c1, Lisp_Object c2, int ignore_positions)
7108 register struct save_window_data *d1, *d2;
7109 struct Lisp_Vector *sw1, *sw2;
7110 int i;
7112 CHECK_WINDOW_CONFIGURATION (c1);
7113 CHECK_WINDOW_CONFIGURATION (c2);
7115 d1 = (struct save_window_data *) XVECTOR (c1);
7116 d2 = (struct save_window_data *) XVECTOR (c2);
7117 sw1 = XVECTOR (d1->saved_windows);
7118 sw2 = XVECTOR (d2->saved_windows);
7120 if (d1->frame_cols != d2->frame_cols)
7121 return 0;
7122 if (d1->frame_lines != d2->frame_lines)
7123 return 0;
7124 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
7125 return 0;
7126 if (! EQ (d1->selected_frame, d2->selected_frame))
7127 return 0;
7128 /* Don't compare the current_window field directly.
7129 Instead see w1_is_current and w2_is_current, below. */
7130 if (! EQ (d1->current_buffer, d2->current_buffer))
7131 return 0;
7132 if (! ignore_positions)
7134 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
7135 return 0;
7136 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
7137 return 0;
7139 /* Don't compare the root_window field.
7140 We don't require the two configurations
7141 to use the same window object,
7142 and the two root windows must be equivalent
7143 if everything else compares equal. */
7144 if (! EQ (d1->focus_frame, d2->focus_frame))
7145 return 0;
7147 /* Verify that the two confis have the same number of windows. */
7148 if (sw1->header.size != sw2->header.size)
7149 return 0;
7151 for (i = 0; i < sw1->header.size; i++)
7153 struct saved_window *p1, *p2;
7154 int w1_is_current, w2_is_current;
7156 p1 = SAVED_WINDOW_N (sw1, i);
7157 p2 = SAVED_WINDOW_N (sw2, i);
7159 /* Verify that the current windows in the two
7160 configurations correspond to each other. */
7161 w1_is_current = EQ (d1->current_window, p1->window);
7162 w2_is_current = EQ (d2->current_window, p2->window);
7164 if (w1_is_current != w2_is_current)
7165 return 0;
7167 /* Verify that the corresponding windows do match. */
7168 if (! EQ (p1->buffer, p2->buffer))
7169 return 0;
7170 if (! EQ (p1->left_col, p2->left_col))
7171 return 0;
7172 if (! EQ (p1->top_line, p2->top_line))
7173 return 0;
7174 if (! EQ (p1->total_cols, p2->total_cols))
7175 return 0;
7176 if (! EQ (p1->total_lines, p2->total_lines))
7177 return 0;
7178 if (! EQ (p1->display_table, p2->display_table))
7179 return 0;
7180 if (! EQ (p1->parent, p2->parent))
7181 return 0;
7182 if (! EQ (p1->prev, p2->prev))
7183 return 0;
7184 if (! ignore_positions)
7186 if (! EQ (p1->hscroll, p2->hscroll))
7187 return 0;
7188 if (!EQ (p1->min_hscroll, p2->min_hscroll))
7189 return 0;
7190 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
7191 return 0;
7192 if (NILP (Fequal (p1->start, p2->start)))
7193 return 0;
7194 if (NILP (Fequal (p1->pointm, p2->pointm)))
7195 return 0;
7196 if (NILP (Fequal (p1->mark, p2->mark)))
7197 return 0;
7199 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
7200 return 0;
7201 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
7202 return 0;
7203 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
7204 return 0;
7205 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
7206 return 0;
7207 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
7208 return 0;
7209 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
7210 return 0;
7211 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
7212 return 0;
7215 return 1;
7218 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7219 Scompare_window_configurations, 2, 2, 0,
7220 doc: /* Compare two window configurations as regards the structure of windows.
7221 This function ignores details such as the values of point and mark
7222 and scrolling positions. */)
7223 (Lisp_Object x, Lisp_Object y)
7225 if (compare_window_configurations (x, y, 1))
7226 return Qt;
7227 return Qnil;
7230 void
7231 init_window_once (void)
7233 struct frame *f = make_initial_frame ();
7234 XSETFRAME (selected_frame, f);
7235 Vterminal_frame = selected_frame;
7236 minibuf_window = f->minibuffer_window;
7237 selected_window = f->selected_window;
7238 last_nonminibuf_frame = f;
7240 window_initialized = 1;
7243 void
7244 init_window (void)
7246 Vwindow_list = Qnil;
7249 void
7250 syms_of_window (void)
7252 Qscroll_up = intern_c_string ("scroll-up");
7253 staticpro (&Qscroll_up);
7255 Qscroll_down = intern_c_string ("scroll-down");
7256 staticpro (&Qscroll_down);
7258 Qscroll_command = intern_c_string ("scroll-command");
7259 staticpro (&Qscroll_command);
7261 Fput (Qscroll_up, Qscroll_command, Qt);
7262 Fput (Qscroll_down, Qscroll_command, Qt);
7264 Qwindow_size_fixed = intern_c_string ("window-size-fixed");
7265 staticpro (&Qwindow_size_fixed);
7266 Fset (Qwindow_size_fixed, Qnil);
7268 staticpro (&Qwindow_configuration_change_hook);
7269 Qwindow_configuration_change_hook
7270 = intern_c_string ("window-configuration-change-hook");
7272 Qwindowp = intern_c_string ("windowp");
7273 staticpro (&Qwindowp);
7275 Qwindow_configuration_p = intern_c_string ("window-configuration-p");
7276 staticpro (&Qwindow_configuration_p);
7278 Qwindow_live_p = intern_c_string ("window-live-p");
7279 staticpro (&Qwindow_live_p);
7281 Qdisplay_buffer = intern_c_string ("display-buffer");
7282 staticpro (&Qdisplay_buffer);
7284 Qtemp_buffer_show_hook = intern_c_string ("temp-buffer-show-hook");
7285 staticpro (&Qtemp_buffer_show_hook);
7287 staticpro (&Vwindow_list);
7289 minibuf_selected_window = Qnil;
7290 staticpro (&minibuf_selected_window);
7292 window_scroll_pixel_based_preserve_x = -1;
7293 window_scroll_pixel_based_preserve_y = -1;
7294 window_scroll_preserve_hpos = -1;
7295 window_scroll_preserve_vpos = -1;
7297 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7298 doc: /* Non-nil means call as function to display a help buffer.
7299 The function is called with one argument, the buffer to be displayed.
7300 Used by `with-output-to-temp-buffer'.
7301 If this function is used, then it must do the entire job of showing
7302 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7303 Vtemp_buffer_show_function = Qnil;
7305 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7306 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7307 Vminibuf_scroll_window = Qnil;
7309 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7310 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7311 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7312 is displayed in the `mode-line' face. */);
7313 mode_line_in_non_selected_windows = 1;
7315 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7316 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7317 Vother_window_scroll_buffer = Qnil;
7319 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7320 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7321 auto_window_vscroll_p = 1;
7323 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7324 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7325 next_screen_context_lines = 2;
7327 DEFVAR_INT ("window-min-height", window_min_height,
7328 doc: /* Allow deleting windows less than this tall.
7329 The value is measured in line units. If a window wants a modeline it
7330 is counted as one line.
7332 Emacs honors settings of this variable when enlarging or shrinking
7333 windows vertically. A value less than 1 is invalid. */);
7334 window_min_height = 4;
7336 DEFVAR_INT ("window-min-width", window_min_width,
7337 doc: /* Allow deleting windows less than this wide.
7338 The value is measured in characters and includes any fringes or
7339 the scrollbar.
7341 Emacs honors settings of this variable when enlarging or shrinking
7342 windows horizontally. A value less than 2 is invalid. */);
7343 window_min_width = 10;
7345 DEFVAR_LISP ("scroll-preserve-screen-position",
7346 Vscroll_preserve_screen_position,
7347 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
7348 A value of nil means point does not keep its screen position except
7349 at the scroll margin or window boundary respectively.
7350 A value of t means point keeps its screen position if the scroll
7351 command moved it vertically out of the window, e.g. when scrolling
7352 by full screens.
7353 Any other value means point always keeps its screen position.
7354 Scroll commands should have the `scroll-command' property
7355 on their symbols to be controlled by this variable. */);
7356 Vscroll_preserve_screen_position = Qnil;
7358 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7359 doc: /* Type of marker to use for `window-point'. */);
7360 Vwindow_point_insertion_type = Qnil;
7362 DEFVAR_LISP ("window-configuration-change-hook",
7363 Vwindow_configuration_change_hook,
7364 doc: /* Functions to call when window configuration changes.
7365 The buffer-local part is run once per window, with the relevant window
7366 selected; while the global part is run only once for the modified frame,
7367 with the relevant frame selected. */);
7368 Vwindow_configuration_change_hook = Qnil;
7370 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7371 doc: /* If non-nil, then the `recenter' command with a nil argument
7372 will redraw the entire frame; the special value `tty' causes the
7373 frame to be redrawn only if it is a tty frame. */);
7374 Vrecenter_redisplay = Qtty;
7377 defsubr (&Sselected_window);
7378 defsubr (&Sminibuffer_window);
7379 defsubr (&Swindow_minibuffer_p);
7380 defsubr (&Swindowp);
7381 defsubr (&Swindow_live_p);
7382 defsubr (&Swindow_frame);
7383 defsubr (&Sframe_root_window);
7384 defsubr (&Sframe_first_window);
7385 defsubr (&Sframe_selected_window);
7386 defsubr (&Sset_frame_selected_window);
7387 defsubr (&Spos_visible_in_window_p);
7388 defsubr (&Swindow_line_height);
7389 defsubr (&Swindow_buffer);
7390 defsubr (&Swindow_parent);
7391 defsubr (&Swindow_vchild);
7392 defsubr (&Swindow_hchild);
7393 defsubr (&Swindow_next);
7394 defsubr (&Swindow_prev);
7395 defsubr (&Swindow_use_time);
7396 defsubr (&Swindow_top_line);
7397 defsubr (&Swindow_left_column);
7398 defsubr (&Swindow_total_size);
7399 defsubr (&Swindow_body_size);
7400 defsubr (&Swindow_height);
7401 defsubr (&Swindow_width);
7402 defsubr (&Swindow_full_width_p);
7403 defsubr (&Swindow_hscroll);
7404 defsubr (&Sset_window_hscroll);
7405 defsubr (&Swindow_redisplay_end_trigger);
7406 defsubr (&Sset_window_redisplay_end_trigger);
7407 defsubr (&Swindow_edges);
7408 defsubr (&Swindow_pixel_edges);
7409 defsubr (&Swindow_absolute_pixel_edges);
7410 defsubr (&Swindow_inside_edges);
7411 defsubr (&Swindow_inside_pixel_edges);
7412 defsubr (&Swindow_inside_absolute_pixel_edges);
7413 defsubr (&Scoordinates_in_window_p);
7414 defsubr (&Swindow_at);
7415 defsubr (&Swindow_point);
7416 defsubr (&Swindow_start);
7417 defsubr (&Swindow_end);
7418 defsubr (&Sset_window_point);
7419 defsubr (&Sset_window_start);
7420 defsubr (&Swindow_dedicated_p);
7421 defsubr (&Sset_window_dedicated_p);
7422 defsubr (&Swindow_display_table);
7423 defsubr (&Sset_window_display_table);
7424 defsubr (&Snext_window);
7425 defsubr (&Sprevious_window);
7426 defsubr (&Sother_window);
7427 defsubr (&Sget_lru_window);
7428 defsubr (&Sget_largest_window);
7429 defsubr (&Sget_buffer_window);
7430 defsubr (&Sdelete_other_windows);
7431 defsubr (&Sdelete_windows_on);
7432 defsubr (&Sreplace_buffer_in_windows);
7433 defsubr (&Sdelete_window);
7434 defsubr (&Sset_window_buffer);
7435 defsubr (&Sselect_window);
7436 defsubr (&Sforce_window_update);
7437 defsubr (&Stemp_output_buffer_show);
7438 defsubr (&Ssplit_window);
7439 defsubr (&Senlarge_window);
7440 defsubr (&Sshrink_window);
7441 defsubr (&Sadjust_window_trailing_edge);
7442 defsubr (&Sscroll_up);
7443 defsubr (&Sscroll_down);
7444 defsubr (&Sscroll_left);
7445 defsubr (&Sscroll_right);
7446 defsubr (&Sother_window_for_scrolling);
7447 defsubr (&Sscroll_other_window);
7448 defsubr (&Sminibuffer_selected_window);
7449 defsubr (&Srecenter);
7450 defsubr (&Swindow_text_height);
7451 defsubr (&Smove_to_window_line);
7452 defsubr (&Swindow_configuration_p);
7453 defsubr (&Swindow_configuration_frame);
7454 defsubr (&Sset_window_configuration);
7455 defsubr (&Scurrent_window_configuration);
7456 defsubr (&Swindow_tree);
7457 defsubr (&Sset_window_margins);
7458 defsubr (&Swindow_margins);
7459 defsubr (&Sset_window_fringes);
7460 defsubr (&Swindow_fringes);
7461 defsubr (&Sset_window_scroll_bars);
7462 defsubr (&Swindow_scroll_bars);
7463 defsubr (&Swindow_vscroll);
7464 defsubr (&Sset_window_vscroll);
7465 defsubr (&Scompare_window_configurations);
7466 defsubr (&Swindow_list);
7467 defsubr (&Swindow_list_1);
7468 defsubr (&Swindow_parameters);
7469 defsubr (&Swindow_parameter);
7470 defsubr (&Sset_window_parameter);
7473 void
7474 keys_of_window (void)
7476 initial_define_key (control_x_map, '1', "delete-other-windows");
7477 initial_define_key (control_x_map, '2', "split-window");
7478 initial_define_key (control_x_map, '0', "delete-window");
7479 initial_define_key (control_x_map, 'o', "other-window");
7480 initial_define_key (control_x_map, '^', "enlarge-window");
7481 initial_define_key (control_x_map, '<', "scroll-left");
7482 initial_define_key (control_x_map, '>', "scroll-right");
7484 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7485 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7486 initial_define_key (meta_map, 'v', "scroll-down-command");