Merged from emacs@sv.gnu.org
[emacs.git] / src / window.c
blob61b85e5ad22ee3e272a4661701c466950d9b5e78
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
4 2001, 2002, 2003, 2004, 2005, 2006 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 2, or (at your option)
11 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; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include <config.h>
24 #include <stdio.h>
26 #include "lisp.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "indent.h"
34 #include "termchar.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "blockinput.h"
38 #include "intervals.h"
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 MAC_OS
50 #include "macterm.h"
51 #endif
54 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
55 Lisp_Object Qscroll_up, Qscroll_down;
56 Lisp_Object Qwindow_size_fixed;
57 extern Lisp_Object Qleft_margin, Qright_margin;
59 static int displayed_window_lines P_ ((struct window *));
60 static struct window *decode_window P_ ((Lisp_Object));
61 static int count_windows P_ ((struct window *));
62 static int get_leaf_windows P_ ((struct window *, struct window **, int));
63 static void window_scroll P_ ((Lisp_Object, int, int, int));
64 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
65 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
66 static int window_min_size_1 P_ ((struct window *, int));
67 static int window_min_size P_ ((struct window *, int, int, int *));
68 static void size_window P_ ((Lisp_Object, int, int, int));
69 static int freeze_window_start P_ ((struct window *, void *));
70 static int window_fixed_size_p P_ ((struct window *, int, int));
71 static void enlarge_window P_ ((Lisp_Object, int, int));
72 static Lisp_Object window_list P_ ((void));
73 static int add_window_to_list P_ ((struct window *, void *));
74 static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
75 Lisp_Object));
76 static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
77 Lisp_Object, int));
78 static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
79 Lisp_Object *));
80 static int foreach_window_1 P_ ((struct window *,
81 int (* fn) (struct window *, void *),
82 void *));
83 static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
85 /* This is the window in which the terminal's cursor should
86 be left when nothing is being done with it. This must
87 always be a leaf window, and its buffer is selected by
88 the top level editing loop at the end of each command.
90 This value is always the same as
91 FRAME_SELECTED_WINDOW (selected_frame). */
93 Lisp_Object selected_window;
95 /* A list of all windows for use by next_window and Fwindow_list.
96 Functions creating or deleting windows should invalidate this cache
97 by setting it to nil. */
99 Lisp_Object Vwindow_list;
101 /* The mini-buffer window of the selected frame.
102 Note that you cannot test for mini-bufferness of an arbitrary window
103 by comparing against this; but you can test for mini-bufferness of
104 the selected window. */
106 Lisp_Object minibuf_window;
108 /* Non-nil means it is the window whose mode line should be
109 shown as the selected window when the minibuffer is selected. */
111 Lisp_Object minibuf_selected_window;
113 /* Non-nil means it is the window for C-M-v to scroll
114 when the mini-buffer is selected. */
116 Lisp_Object Vminibuf_scroll_window;
118 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
120 Lisp_Object Vother_window_scroll_buffer;
122 /* Non-nil means it's function to call to display temp buffers. */
124 Lisp_Object Vtemp_buffer_show_function;
126 /* Non-zero means line and page scrolling on tall lines (with images)
127 does partial scrolling by modifying window-vscroll. */
129 int auto_window_vscroll_p;
131 /* Non-zero means to use mode-line-inactive face in all windows but the
132 selected-window and the minibuffer-scroll-window when the
133 minibuffer is active. */
134 int mode_line_in_non_selected_windows;
136 /* If a window gets smaller than either of these, it is removed. */
138 EMACS_INT window_min_height;
139 EMACS_INT window_min_width;
141 /* Nonzero implies Fdisplay_buffer should create windows. */
143 int pop_up_windows;
145 /* Nonzero implies make new frames for Fdisplay_buffer. */
147 int pop_up_frames;
149 /* Nonzero means reuse existing frames for displaying buffers. */
151 int display_buffer_reuse_frames;
153 /* Non-nil means use this function instead of default */
155 Lisp_Object Vpop_up_frame_function;
157 /* Function to call to handle Fdisplay_buffer. */
159 Lisp_Object Vdisplay_buffer_function;
161 /* Non-nil means that Fdisplay_buffer should even the heights of windows. */
163 Lisp_Object Veven_window_heights;
165 /* List of buffer *names* for buffers that should have their own frames. */
167 Lisp_Object Vspecial_display_buffer_names;
169 /* List of regexps for buffer names that should have their own frames. */
171 Lisp_Object Vspecial_display_regexps;
173 /* Function to pop up a special frame. */
175 Lisp_Object Vspecial_display_function;
177 /* List of buffer *names* for buffers to appear in selected window. */
179 Lisp_Object Vsame_window_buffer_names;
181 /* List of regexps for buffer names to appear in selected window. */
183 Lisp_Object Vsame_window_regexps;
185 /* Hook run at end of temp_output_buffer_show. */
187 Lisp_Object Qtemp_buffer_show_hook;
189 /* Fdisplay_buffer always splits the largest window
190 if that window is more than this high. */
192 EMACS_INT split_height_threshold;
194 /* Number of lines of continuity in scrolling by screenfuls. */
196 EMACS_INT next_screen_context_lines;
198 /* Incremented for each window created. */
200 static int sequence_number;
202 /* Nonzero after init_window_once has finished. */
204 static int window_initialized;
206 /* Hook to run when window config changes. */
208 Lisp_Object Qwindow_configuration_change_hook;
209 Lisp_Object Vwindow_configuration_change_hook;
211 /* Non-nil means scroll commands try to put point
212 at the same screen height as previously. */
214 Lisp_Object Vscroll_preserve_screen_position;
216 /* Incremented by 1 whenever a window is deleted. */
218 int window_deletion_count;
220 /* Used by the function window_scroll_pixel_based */
222 static int window_scroll_pixel_based_preserve_y;
224 #if 0 /* This isn't used anywhere. */
225 /* Nonzero means we can split a frame even if it is "unsplittable". */
226 static int inhibit_frame_unsplittable;
227 #endif /* 0 */
229 extern EMACS_INT scroll_margin;
231 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
233 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
234 doc: /* Returns t if OBJECT is a window. */)
235 (object)
236 Lisp_Object object;
238 return WINDOWP (object) ? Qt : Qnil;
241 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
242 doc: /* Returns t if OBJECT is a window which is currently visible. */)
243 (object)
244 Lisp_Object object;
246 return WINDOW_LIVE_P (object) ? Qt : Qnil;
249 Lisp_Object
250 make_window ()
252 Lisp_Object val;
253 register struct window *p;
255 p = allocate_window ();
256 ++sequence_number;
257 XSETFASTINT (p->sequence_number, sequence_number);
258 XSETFASTINT (p->left_col, 0);
259 XSETFASTINT (p->top_line, 0);
260 XSETFASTINT (p->total_lines, 0);
261 XSETFASTINT (p->total_cols, 0);
262 XSETFASTINT (p->hscroll, 0);
263 XSETFASTINT (p->min_hscroll, 0);
264 p->orig_top_line = p->orig_total_lines = Qnil;
265 p->start = Fmake_marker ();
266 p->pointm = Fmake_marker ();
267 XSETFASTINT (p->use_time, 0);
268 p->frame = Qnil;
269 p->display_table = Qnil;
270 p->dedicated = Qnil;
271 p->pseudo_window_p = 0;
272 bzero (&p->cursor, sizeof (p->cursor));
273 bzero (&p->last_cursor, sizeof (p->last_cursor));
274 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
275 p->desired_matrix = p->current_matrix = 0;
276 p->nrows_scale_factor = p->ncols_scale_factor = 1;
277 p->phys_cursor_type = -1;
278 p->phys_cursor_width = -1;
279 p->must_be_updated_p = 0;
280 XSETFASTINT (p->window_end_vpos, 0);
281 XSETFASTINT (p->window_end_pos, 0);
282 p->window_end_valid = Qnil;
283 p->vscroll = 0;
284 XSETWINDOW (val, p);
285 XSETFASTINT (p->last_point, 0);
286 p->frozen_window_start_p = 0;
287 p->last_cursor_off_p = p->cursor_off_p = 0;
288 p->left_margin_cols = Qnil;
289 p->right_margin_cols = Qnil;
290 p->left_fringe_width = Qnil;
291 p->right_fringe_width = Qnil;
292 p->fringes_outside_margins = Qnil;
293 p->scroll_bar_width = Qnil;
294 p->vertical_scroll_bar_type = Qt;
296 Vwindow_list = Qnil;
297 return val;
300 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
301 doc: /* Return the window that the cursor now appears in and commands apply to. */)
304 return selected_window;
307 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
308 doc: /* Return the window used now for minibuffers.
309 If the optional argument FRAME is specified, return the minibuffer window
310 used by that frame. */)
311 (frame)
312 Lisp_Object frame;
314 if (NILP (frame))
315 frame = selected_frame;
316 CHECK_LIVE_FRAME (frame);
317 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
320 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
321 doc: /* Returns non-nil if WINDOW is a minibuffer window.
322 WINDOW defaults to the selected window. */)
323 (window)
324 Lisp_Object window;
326 struct window *w = decode_window (window);
327 return MINI_WINDOW_P (w) ? Qt : Qnil;
331 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
332 Spos_visible_in_window_p, 0, 3, 0,
333 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
334 Return nil if that position is scrolled vertically out of view.
335 If a character is only partially visible, nil is returned, unless the
336 optional argument PARTIALLY is non-nil.
337 If POS is only out of view because of horizontal scrolling, return non-nil.
338 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
340 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
341 return value is a list (X Y PARTIAL) where X and Y are the pixel coordinates
342 relative to the top left corner of the window. PARTIAL is nil if the character
343 after POS is fully visible; otherwise it is a cons (RTOP . RBOT) where RTOP
344 and RBOT are the number of pixels invisible at the top and bottom of the row. */)
345 (pos, window, partially)
346 Lisp_Object pos, window, partially;
348 register struct window *w;
349 register int posint;
350 register struct buffer *buf;
351 struct text_pos top;
352 Lisp_Object in_window = Qnil;
353 int rtop, rbot, fully_p = 1;
354 int x, y;
356 w = decode_window (window);
357 buf = XBUFFER (w->buffer);
358 SET_TEXT_POS_FROM_MARKER (top, w->start);
360 if (!NILP (pos))
362 CHECK_NUMBER_COERCE_MARKER (pos);
363 posint = XINT (pos);
365 else if (w == XWINDOW (selected_window))
366 posint = PT;
367 else
368 posint = XMARKER (w->pointm)->charpos;
370 /* If position is above window start or outside buffer boundaries,
371 or if window start is out of range, position is not visible. */
372 if (posint >= CHARPOS (top)
373 && posint <= BUF_ZV (buf)
374 && CHARPOS (top) >= BUF_BEGV (buf)
375 && CHARPOS (top) <= BUF_ZV (buf)
376 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, NILP (partially))
377 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
378 in_window = Qt;
380 if (!NILP (in_window) && !NILP (partially))
381 in_window = Fcons (make_number (x),
382 Fcons (make_number (y),
383 Fcons ((fully_p ? Qnil
384 : Fcons (make_number (rtop),
385 make_number (rbot))),
386 Qnil)));
387 return in_window;
391 static struct window *
392 decode_window (window)
393 register Lisp_Object window;
395 if (NILP (window))
396 return XWINDOW (selected_window);
398 CHECK_LIVE_WINDOW (window);
399 return XWINDOW (window);
402 static struct window *
403 decode_any_window (window)
404 register Lisp_Object window;
406 if (NILP (window))
407 return XWINDOW (selected_window);
409 CHECK_WINDOW (window);
410 return XWINDOW (window);
413 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
414 doc: /* Return the buffer that WINDOW is displaying.
415 WINDOW defaults to the selected window. */)
416 (window)
417 Lisp_Object window;
419 return decode_window (window)->buffer;
422 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
423 doc: /* Return the number of lines in WINDOW (including its mode line). */)
424 (window)
425 Lisp_Object window;
427 return decode_any_window (window)->total_lines;
430 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
431 doc: /* Return the number of display columns in WINDOW.
432 This is the width that is usable columns available for text in WINDOW.
433 If you want to find out how many columns WINDOW takes up,
434 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
435 (window)
436 Lisp_Object window;
438 return make_number (window_box_text_cols (decode_any_window (window)));
441 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
442 doc: /* Return the number of columns by which WINDOW is scrolled from left margin. */)
443 (window)
444 Lisp_Object window;
446 return decode_window (window)->hscroll;
449 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
450 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
451 Return NCOL. NCOL should be zero or positive.
453 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
454 window so that the location of point becomes invisible. */)
455 (window, ncol)
456 Lisp_Object window, ncol;
458 struct window *w = decode_window (window);
459 int hscroll;
461 CHECK_NUMBER (ncol);
462 hscroll = max (0, XINT (ncol));
464 /* Prevent redisplay shortcuts when changing the hscroll. */
465 if (XINT (w->hscroll) != hscroll)
466 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
468 w->hscroll = make_number (hscroll);
469 return ncol;
472 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
473 Swindow_redisplay_end_trigger, 0, 1, 0,
474 doc: /* Return WINDOW's redisplay end trigger value.
475 See `set-window-redisplay-end-trigger' for more information. */)
476 (window)
477 Lisp_Object window;
479 return decode_window (window)->redisplay_end_trigger;
482 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
483 Sset_window_redisplay_end_trigger, 2, 2, 0,
484 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
485 VALUE should be a buffer position (typically a marker) or nil.
486 If it is a buffer position, then if redisplay in WINDOW reaches a position
487 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
488 with two arguments: WINDOW, and the end trigger value.
489 Afterwards the end-trigger value is reset to nil. */)
490 (window, value)
491 register Lisp_Object window, value;
493 register struct window *w;
495 w = decode_window (window);
496 w->redisplay_end_trigger = value;
497 return value;
500 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
501 doc: /* Return a list of the edge coordinates of WINDOW.
502 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
503 RIGHT is one more than the rightmost column occupied by WINDOW,
504 and BOTTOM is one more than the bottommost row occupied by WINDOW.
505 The edges include the space used by the window's scroll bar,
506 display margins, fringes, header line, and mode line, if it has them.
507 To get the edges of the actual text area, use `window-inside-edges'. */)
508 (window)
509 Lisp_Object window;
511 register struct window *w = decode_any_window (window);
513 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
514 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
515 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
516 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
517 Qnil))));
520 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
521 doc: /* Return a list of the edge pixel coordinates of WINDOW.
522 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
523 RIGHT is one more than the rightmost x position occupied by WINDOW,
524 and BOTTOM is one more than the bottommost y position occupied by WINDOW.
525 The pixel edges include the space used by the window's scroll bar,
526 display margins, fringes, header line, and mode line, if it has them.
527 To get the edges of the actual text area, use `window-inside-pixel-edges'. */)
528 (window)
529 Lisp_Object window;
531 register struct window *w = decode_any_window (window);
533 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
534 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
535 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
536 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
537 Qnil))));
540 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
541 doc: /* Return a list of the edge coordinates of WINDOW.
542 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
543 RIGHT is one more than the rightmost column used by text in WINDOW,
544 and BOTTOM is one more than the bottommost row used by text in WINDOW.
545 The inside edges do not include the space used by the window's scroll bar,
546 display margins, fringes, header line, and/or mode line. */)
547 (window)
548 Lisp_Object window;
550 register struct window *w = decode_any_window (window);
552 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
553 + WINDOW_LEFT_MARGIN_COLS (w)
554 + WINDOW_LEFT_FRINGE_COLS (w)),
555 make_number (WINDOW_TOP_EDGE_LINE (w)
556 + WINDOW_HEADER_LINE_LINES (w)),
557 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
558 - WINDOW_RIGHT_MARGIN_COLS (w)
559 - WINDOW_RIGHT_FRINGE_COLS (w)),
560 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
561 - WINDOW_MODE_LINE_LINES (w)));
564 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
565 doc: /* Return a list of the edge pixel coordinates of WINDOW.
566 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
567 RIGHT is one more than the rightmost x position used by text in WINDOW,
568 and BOTTOM is one more than the bottommost y position used by text in WINDOW.
569 The inside edges do not include the space used by the window's scroll bar,
570 display margins, fringes, header line, and/or mode line. */)
571 (window)
572 Lisp_Object window;
574 register struct window *w = decode_any_window (window);
576 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
577 + WINDOW_LEFT_MARGIN_WIDTH (w)
578 + WINDOW_LEFT_FRINGE_WIDTH (w)),
579 make_number (WINDOW_TOP_EDGE_Y (w)
580 + WINDOW_HEADER_LINE_HEIGHT (w)),
581 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
582 - WINDOW_RIGHT_MARGIN_WIDTH (w)
583 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
584 make_number (WINDOW_BOTTOM_EDGE_Y (w)
585 - WINDOW_MODE_LINE_HEIGHT (w)));
588 /* Test if the character at column *X, row *Y is within window W.
589 If it is not, return ON_NOTHING;
590 if it is in the window's text area,
591 set *x and *y to its location relative to the upper left corner
592 of the window, and
593 return ON_TEXT;
594 if it is on the window's modeline, return ON_MODE_LINE;
595 if it is on the border between the window and its right sibling,
596 return ON_VERTICAL_BORDER.
597 if it is on a scroll bar,
598 return ON_SCROLL_BAR.
599 if it is on the window's top line, return ON_HEADER_LINE;
600 if it is in left or right fringe of the window,
601 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
602 to window-relative coordinates;
603 if it is in the marginal area to the left/right of the window,
604 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
605 to window-relative coordinates.
607 X and Y are frame relative pixel coordinates. */
609 static enum window_part
610 coordinates_in_window (w, x, y)
611 register struct window *w;
612 register int *x, *y;
614 struct frame *f = XFRAME (WINDOW_FRAME (w));
615 int left_x, right_x, top_y, bottom_y;
616 enum window_part part;
617 int ux = FRAME_COLUMN_WIDTH (f);
618 int x0 = WINDOW_LEFT_EDGE_X (w);
619 int x1 = WINDOW_RIGHT_EDGE_X (w);
620 /* The width of the area where the vertical line can be dragged.
621 (Between mode lines for instance. */
622 int grabbable_width = ux;
623 int lmargin_width, rmargin_width, text_left, text_right;
625 /* In what's below, we subtract 1 when computing right_x because we
626 want the rightmost pixel, which is given by left_pixel+width-1. */
627 if (w->pseudo_window_p)
629 left_x = 0;
630 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
631 top_y = WINDOW_TOP_EDGE_Y (w);
632 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
634 else
636 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
637 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
638 top_y = WINDOW_TOP_EDGE_Y (w);
639 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
642 /* Outside any interesting row? */
643 if (*y < top_y || *y >= bottom_y)
644 return ON_NOTHING;
646 /* On the mode line or header line? If it's near the start of
647 the mode or header line of window that's has a horizontal
648 sibling, say it's on the vertical line. That's to be able
649 to resize windows horizontally in case we're using toolkit
650 scroll bars. */
652 if (WINDOW_WANTS_MODELINE_P (w)
653 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
655 part = ON_MODE_LINE;
657 header_vertical_border_check:
658 /* We're somewhere on the mode line. We consider the place
659 between mode lines of horizontally adjacent mode lines
660 as the vertical border. If scroll bars on the left,
661 return the right window. */
662 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
663 || WINDOW_RIGHTMOST_P (w))
665 if (!WINDOW_LEFTMOST_P (w) && abs (*x - x0) < grabbable_width)
667 /* Convert X and Y to window relative coordinates.
668 Vertical border is at the left edge of window. */
669 *x = max (0, *x - x0);
670 *y -= top_y;
671 return ON_VERTICAL_BORDER;
674 else
676 if (abs (*x - x1) < grabbable_width)
678 /* Convert X and Y to window relative coordinates.
679 Vertical border is at the right edge of window. */
680 *x = min (x1, *x) - x0;
681 *y -= top_y;
682 return ON_VERTICAL_BORDER;
686 if (*x < x0 || *x >= x1)
687 return ON_NOTHING;
689 /* Convert X and Y to window relative coordinates.
690 Mode line starts at left edge of window. */
691 *x -= x0;
692 *y -= top_y;
693 return part;
696 if (WINDOW_WANTS_HEADER_LINE_P (w)
697 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
699 part = ON_HEADER_LINE;
700 goto header_vertical_border_check;
703 if (*x < x0 || *x >= x1)
704 return ON_NOTHING;
706 /* Outside any interesting column? */
707 if (*x < left_x || *x > right_x)
709 *y -= top_y;
710 return ON_SCROLL_BAR;
713 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
714 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
716 text_left = window_box_left (w, TEXT_AREA);
717 text_right = text_left + window_box_width (w, TEXT_AREA);
719 if (FRAME_WINDOW_P (f))
721 if (!w->pseudo_window_p
722 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
723 && !WINDOW_RIGHTMOST_P (w)
724 && (abs (*x - right_x) < grabbable_width))
726 /* Convert X and Y to window relative coordinates.
727 Vertical border is at the right edge of window. */
728 *x = min (right_x, *x) - left_x;
729 *y -= top_y;
730 return ON_VERTICAL_BORDER;
733 else
735 /* Need to say "*x > right_x" rather than >=, since on character
736 terminals, the vertical line's x coordinate is right_x. */
737 if (!w->pseudo_window_p
738 && !WINDOW_RIGHTMOST_P (w)
739 && *x > right_x - ux)
741 /* On the border on the right side of the window? Assume that
742 this area begins at RIGHT_X minus a canonical char width. */
743 *x = min (right_x, *x) - left_x;
744 *y -= top_y;
745 return ON_VERTICAL_BORDER;
749 if (*x < text_left)
751 if (lmargin_width > 0
752 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
753 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
754 : (*x < left_x + lmargin_width)))
756 *x -= left_x;
757 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
758 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
759 *y -= top_y;
760 return ON_LEFT_MARGIN;
763 /* Convert X and Y to window-relative pixel coordinates. */
764 *x -= left_x;
765 *y -= top_y;
766 return ON_LEFT_FRINGE;
769 if (*x >= text_right)
771 if (rmargin_width > 0
772 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
773 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
774 : (*x >= right_x - rmargin_width)))
776 *x -= right_x - rmargin_width;
777 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
778 *x += WINDOW_RIGHT_FRINGE_WIDTH (w);
779 *y -= top_y;
780 return ON_RIGHT_MARGIN;
783 /* Convert X and Y to window-relative pixel coordinates. */
784 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
785 *y -= top_y;
786 return ON_RIGHT_FRINGE;
789 /* Everything special ruled out - must be on text area */
790 *x -= text_left;
791 *y -= top_y;
792 return ON_TEXT;
796 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
797 Scoordinates_in_window_p, 2, 2, 0,
798 doc: /* Return non-nil if COORDINATES are in WINDOW.
799 COORDINATES is a cons of the form (X . Y), X and Y being distances
800 measured in characters from the upper-left corner of the frame.
801 \(0 . 0) denotes the character in the upper left corner of the
802 frame.
803 If COORDINATES are in the text portion of WINDOW,
804 the coordinates relative to the window are returned.
805 If they are in the mode line of WINDOW, `mode-line' is returned.
806 If they are in the top mode line of WINDOW, `header-line' is returned.
807 If they are in the left fringe of WINDOW, `left-fringe' is returned.
808 If they are in the right fringe of WINDOW, `right-fringe' is returned.
809 If they are on the border between WINDOW and its right sibling,
810 `vertical-line' is returned.
811 If they are in the windows's left or right marginal areas, `left-margin'\n\
812 or `right-margin' is returned. */)
813 (coordinates, window)
814 register Lisp_Object coordinates, window;
816 struct window *w;
817 struct frame *f;
818 int x, y;
819 Lisp_Object lx, ly;
821 CHECK_WINDOW (window);
822 w = XWINDOW (window);
823 f = XFRAME (w->frame);
824 CHECK_CONS (coordinates);
825 lx = Fcar (coordinates);
826 ly = Fcdr (coordinates);
827 CHECK_NUMBER_OR_FLOAT (lx);
828 CHECK_NUMBER_OR_FLOAT (ly);
829 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
830 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
832 switch (coordinates_in_window (w, &x, &y))
834 case ON_NOTHING:
835 return Qnil;
837 case ON_TEXT:
838 /* X and Y are now window relative pixel coordinates. Convert
839 them to canonical char units before returning them. */
840 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
841 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
843 case ON_MODE_LINE:
844 return Qmode_line;
846 case ON_VERTICAL_BORDER:
847 return Qvertical_line;
849 case ON_HEADER_LINE:
850 return Qheader_line;
852 case ON_LEFT_FRINGE:
853 return Qleft_fringe;
855 case ON_RIGHT_FRINGE:
856 return Qright_fringe;
858 case ON_LEFT_MARGIN:
859 return Qleft_margin;
861 case ON_RIGHT_MARGIN:
862 return Qright_margin;
864 case ON_SCROLL_BAR:
865 /* Historically we are supposed to return nil in this case. */
866 return Qnil;
868 default:
869 abort ();
874 /* Callback for foreach_window, used in window_from_coordinates.
875 Check if window W contains coordinates specified by USER_DATA which
876 is actually a pointer to a struct check_window_data CW.
878 Check if window W contains coordinates *CW->x and *CW->y. If it
879 does, return W in *CW->window, as Lisp_Object, and return in
880 *CW->part the part of the window under coordinates *X,*Y. Return
881 zero from this function to stop iterating over windows. */
883 struct check_window_data
885 Lisp_Object *window;
886 int *x, *y;
887 enum window_part *part;
890 static int
891 check_window_containing (w, user_data)
892 struct window *w;
893 void *user_data;
895 struct check_window_data *cw = (struct check_window_data *) user_data;
896 enum window_part found;
897 int continue_p = 1;
899 found = coordinates_in_window (w, cw->x, cw->y);
900 if (found != ON_NOTHING)
902 *cw->part = found;
903 XSETWINDOW (*cw->window, w);
904 continue_p = 0;
907 return continue_p;
911 /* Find the window containing frame-relative pixel position X/Y and
912 return it as a Lisp_Object.
914 If X, Y is on one of the window's special `window_part' elements,
915 set *PART to the id of that element, and return X and Y converted
916 to window relative coordinates in WX and WY.
918 If there is no window under X, Y return nil and leave *PART
919 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
921 This function was previously implemented with a loop cycling over
922 windows with Fnext_window, and starting with the frame's selected
923 window. It turned out that this doesn't work with an
924 implementation of next_window using Vwindow_list, because
925 FRAME_SELECTED_WINDOW (F) is not always contained in the window
926 tree of F when this function is called asynchronously from
927 note_mouse_highlight. The original loop didn't terminate in this
928 case. */
930 Lisp_Object
931 window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
932 struct frame *f;
933 int x, y;
934 enum window_part *part;
935 int *wx, *wy;
936 int tool_bar_p;
938 Lisp_Object window;
939 struct check_window_data cw;
940 enum window_part dummy;
942 if (part == 0)
943 part = &dummy;
945 window = Qnil;
946 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
947 foreach_window (f, check_window_containing, &cw);
949 /* If not found above, see if it's in the tool bar window, if a tool
950 bar exists. */
951 if (NILP (window)
952 && tool_bar_p
953 && WINDOWP (f->tool_bar_window)
954 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
955 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
956 != ON_NOTHING))
958 *part = ON_TEXT;
959 window = f->tool_bar_window;
962 if (wx) *wx = x;
963 if (wy) *wy = y;
965 return window;
968 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
969 doc: /* Return window containing coordinates X and Y on FRAME.
970 If omitted, FRAME defaults to the currently selected frame.
971 The top left corner of the frame is considered to be row 0,
972 column 0. */)
973 (x, y, frame)
974 Lisp_Object x, y, frame;
976 struct frame *f;
978 if (NILP (frame))
979 frame = selected_frame;
980 CHECK_LIVE_FRAME (frame);
981 f = XFRAME (frame);
983 /* Check that arguments are integers or floats. */
984 CHECK_NUMBER_OR_FLOAT (x);
985 CHECK_NUMBER_OR_FLOAT (y);
987 return window_from_coordinates (f,
988 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
989 + FRAME_INTERNAL_BORDER_WIDTH (f)),
990 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
991 + FRAME_INTERNAL_BORDER_WIDTH (f)),
992 0, 0, 0, 0);
995 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
996 doc: /* Return current value of point in WINDOW.
997 For a nonselected window, this is the value point would have
998 if that window were selected.
1000 Note that, when WINDOW is the selected window and its buffer
1001 is also currently selected, the value returned is the same as (point).
1002 It would be more strictly correct to return the `top-level' value
1003 of point, outside of any save-excursion forms.
1004 But that is hard to define. */)
1005 (window)
1006 Lisp_Object window;
1008 register struct window *w = decode_window (window);
1010 if (w == XWINDOW (selected_window)
1011 && current_buffer == XBUFFER (w->buffer))
1012 return Fpoint ();
1013 return Fmarker_position (w->pointm);
1016 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1017 doc: /* Return position at which display currently starts in WINDOW.
1018 This is updated by redisplay or by calling `set-window-start'. */)
1019 (window)
1020 Lisp_Object window;
1022 return Fmarker_position (decode_window (window)->start);
1025 /* This is text temporarily removed from the doc string below.
1027 This function returns nil if the position is not currently known.
1028 That happens when redisplay is preempted and doesn't finish.
1029 If in that case you want to compute where the end of the window would
1030 have been if redisplay had finished, do this:
1031 (save-excursion
1032 (goto-char (window-start window))
1033 (vertical-motion (1- (window-height window)) window)
1034 (point))") */
1036 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1037 doc: /* Return position at which display currently ends in WINDOW.
1038 This is updated by redisplay, when it runs to completion.
1039 Simply changing the buffer text or setting `window-start'
1040 does not update this value.
1041 Return nil if there is no recorded value. \(This can happen if the
1042 last redisplay of WINDOW was preempted, and did not finish.)
1043 If UPDATE is non-nil, compute the up-to-date position
1044 if it isn't already recorded. */)
1045 (window, update)
1046 Lisp_Object window, update;
1048 Lisp_Object value;
1049 struct window *w = decode_window (window);
1050 Lisp_Object buf;
1052 buf = w->buffer;
1053 CHECK_BUFFER (buf);
1055 #if 0 /* This change broke some things. We should make it later. */
1056 /* If we don't know the end position, return nil.
1057 The user can compute it with vertical-motion if he wants to.
1058 It would be nicer to do it automatically,
1059 but that's so slow that it would probably bother people. */
1060 if (NILP (w->window_end_valid))
1061 return Qnil;
1062 #endif
1064 if (! NILP (update)
1065 && ! (! NILP (w->window_end_valid)
1066 && XFASTINT (w->last_modified) >= MODIFF)
1067 && !noninteractive)
1069 struct text_pos startp;
1070 struct it it;
1071 struct buffer *old_buffer = NULL, *b = XBUFFER (buf);
1073 /* In case W->start is out of the range, use something
1074 reasonable. This situation occurred when loading a file with
1075 `-l' containing a call to `rmail' with subsequent other
1076 commands. At the end, W->start happened to be BEG, while
1077 rmail had already narrowed the buffer. */
1078 if (XMARKER (w->start)->charpos < BEGV)
1079 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1080 else if (XMARKER (w->start)->charpos > ZV)
1081 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1082 else
1083 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1085 /* Cannot use Fvertical_motion because that function doesn't
1086 cope with variable-height lines. */
1087 if (b != current_buffer)
1089 old_buffer = current_buffer;
1090 set_buffer_internal (b);
1093 start_display (&it, w, startp);
1094 move_it_vertically (&it, window_box_height (w));
1095 if (it.current_y < it.last_visible_y)
1096 move_it_past_eol (&it);
1097 value = make_number (IT_CHARPOS (it));
1099 if (old_buffer)
1100 set_buffer_internal (old_buffer);
1102 else
1103 XSETINT (value, BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
1105 return value;
1108 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1109 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1110 Return POS. */)
1111 (window, pos)
1112 Lisp_Object window, pos;
1114 register struct window *w = decode_window (window);
1116 CHECK_NUMBER_COERCE_MARKER (pos);
1117 if (w == XWINDOW (selected_window)
1118 && XBUFFER (w->buffer) == current_buffer)
1119 Fgoto_char (pos);
1120 else
1121 set_marker_restricted (w->pointm, pos, w->buffer);
1123 /* We have to make sure that redisplay updates the window to show
1124 the new value of point. */
1125 if (!EQ (window, selected_window))
1126 ++windows_or_buffers_changed;
1128 return pos;
1131 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1132 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1133 Return POS.
1134 Optional third arg NOFORCE non-nil inhibits next redisplay
1135 from overriding motion of point in order to display at this exact start. */)
1136 (window, pos, noforce)
1137 Lisp_Object window, pos, noforce;
1139 register struct window *w = decode_window (window);
1141 CHECK_NUMBER_COERCE_MARKER (pos);
1142 set_marker_restricted (w->start, pos, w->buffer);
1143 /* this is not right, but much easier than doing what is right. */
1144 w->start_at_line_beg = Qnil;
1145 if (NILP (noforce))
1146 w->force_start = Qt;
1147 w->update_mode_line = Qt;
1148 XSETFASTINT (w->last_modified, 0);
1149 XSETFASTINT (w->last_overlay_modified, 0);
1150 if (!EQ (window, selected_window))
1151 windows_or_buffers_changed++;
1153 return pos;
1156 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1157 1, 1, 0,
1158 doc: /* Return WINDOW's dedicated object, usually t or nil.
1159 See also `set-window-dedicated-p'. */)
1160 (window)
1161 Lisp_Object window;
1163 return decode_window (window)->dedicated;
1166 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1167 Sset_window_dedicated_p, 2, 2, 0,
1168 doc: /* Control whether WINDOW is dedicated to the buffer it displays.
1169 If it is dedicated, Emacs will not automatically change
1170 which buffer appears in it.
1171 The second argument is the new value for the dedication flag;
1172 non-nil means yes. */)
1173 (window, arg)
1174 Lisp_Object window, arg;
1176 register struct window *w = decode_window (window);
1178 w->dedicated = arg;
1180 return w->dedicated;
1183 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1184 0, 1, 0,
1185 doc: /* Return the display-table that WINDOW is using. */)
1186 (window)
1187 Lisp_Object window;
1189 return decode_window (window)->display_table;
1192 /* Get the display table for use on window W. This is either W's
1193 display table or W's buffer's display table. Ignore the specified
1194 tables if they are not valid; if no valid table is specified,
1195 return 0. */
1197 struct Lisp_Char_Table *
1198 window_display_table (w)
1199 struct window *w;
1201 struct Lisp_Char_Table *dp = NULL;
1203 if (DISP_TABLE_P (w->display_table))
1204 dp = XCHAR_TABLE (w->display_table);
1205 else if (BUFFERP (w->buffer))
1207 struct buffer *b = XBUFFER (w->buffer);
1209 if (DISP_TABLE_P (b->display_table))
1210 dp = XCHAR_TABLE (b->display_table);
1211 else if (DISP_TABLE_P (Vstandard_display_table))
1212 dp = XCHAR_TABLE (Vstandard_display_table);
1215 return dp;
1218 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1219 doc: /* Set WINDOW's display-table to TABLE. */)
1220 (window, table)
1221 register Lisp_Object window, table;
1223 register struct window *w;
1225 w = decode_window (window);
1226 w->display_table = table;
1227 return table;
1230 /* Record info on buffer window w is displaying
1231 when it is about to cease to display that buffer. */
1232 static void
1233 unshow_buffer (w)
1234 register struct window *w;
1236 Lisp_Object buf;
1237 struct buffer *b;
1239 buf = w->buffer;
1240 b = XBUFFER (buf);
1241 if (b != XMARKER (w->pointm)->buffer)
1242 abort ();
1244 #if 0
1245 if (w == XWINDOW (selected_window)
1246 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1247 /* Do this except when the selected window's buffer
1248 is being removed from some other window. */
1249 #endif
1250 /* last_window_start records the start position that this buffer
1251 had in the last window to be disconnected from it.
1252 Now that this statement is unconditional,
1253 it is possible for the buffer to be displayed in the
1254 selected window, while last_window_start reflects another
1255 window which was recently showing the same buffer.
1256 Some people might say that might be a good thing. Let's see. */
1257 b->last_window_start = marker_position (w->start);
1259 /* Point in the selected window's buffer
1260 is actually stored in that buffer, and the window's pointm isn't used.
1261 So don't clobber point in that buffer. */
1262 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1263 /* This line helps to fix Horsley's testbug.el bug. */
1264 && !(WINDOWP (b->last_selected_window)
1265 && w != XWINDOW (b->last_selected_window)
1266 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1267 temp_set_point_both (b,
1268 clip_to_bounds (BUF_BEGV (b),
1269 XMARKER (w->pointm)->charpos,
1270 BUF_ZV (b)),
1271 clip_to_bounds (BUF_BEGV_BYTE (b),
1272 marker_byte_position (w->pointm),
1273 BUF_ZV_BYTE (b)));
1275 if (WINDOWP (b->last_selected_window)
1276 && w == XWINDOW (b->last_selected_window))
1277 b->last_selected_window = Qnil;
1280 /* Put replacement into the window structure in place of old. */
1281 static void
1282 replace_window (old, replacement)
1283 Lisp_Object old, replacement;
1285 register Lisp_Object tem;
1286 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1288 /* If OLD is its frame's root_window, then replacement is the new
1289 root_window for that frame. */
1291 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1292 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1294 p->left_col = o->left_col;
1295 p->top_line = o->top_line;
1296 p->total_cols = o->total_cols;
1297 p->total_lines = o->total_lines;
1298 p->desired_matrix = p->current_matrix = 0;
1299 p->vscroll = 0;
1300 bzero (&p->cursor, sizeof (p->cursor));
1301 bzero (&p->last_cursor, sizeof (p->last_cursor));
1302 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1303 p->phys_cursor_type = -1;
1304 p->phys_cursor_width = -1;
1305 p->must_be_updated_p = 0;
1306 p->pseudo_window_p = 0;
1307 XSETFASTINT (p->window_end_vpos, 0);
1308 XSETFASTINT (p->window_end_pos, 0);
1309 p->window_end_valid = Qnil;
1310 p->frozen_window_start_p = 0;
1311 p->orig_top_line = p->orig_total_lines = Qnil;
1313 p->next = tem = o->next;
1314 if (!NILP (tem))
1315 XWINDOW (tem)->prev = replacement;
1317 p->prev = tem = o->prev;
1318 if (!NILP (tem))
1319 XWINDOW (tem)->next = replacement;
1321 p->parent = tem = o->parent;
1322 if (!NILP (tem))
1324 if (EQ (XWINDOW (tem)->vchild, old))
1325 XWINDOW (tem)->vchild = replacement;
1326 if (EQ (XWINDOW (tem)->hchild, old))
1327 XWINDOW (tem)->hchild = replacement;
1330 /*** Here, if replacement is a vertical combination
1331 and so is its new parent, we should make replacement's
1332 children be children of that parent instead. ***/
1335 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1336 doc: /* Remove WINDOW from the display. Default is selected window. */)
1337 (window)
1338 register Lisp_Object window;
1340 delete_window (window);
1342 if (! NILP (Vwindow_configuration_change_hook)
1343 && ! NILP (Vrun_hooks))
1344 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1346 return Qnil;
1349 void
1350 delete_window (window)
1351 register Lisp_Object window;
1353 register Lisp_Object tem, parent, sib;
1354 register struct window *p;
1355 register struct window *par;
1356 struct frame *f;
1358 /* Because this function is called by other C code on non-leaf
1359 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1360 so we can't decode_window here. */
1361 if (NILP (window))
1362 window = selected_window;
1363 else
1364 CHECK_WINDOW (window);
1365 p = XWINDOW (window);
1367 /* It's a no-op to delete an already-deleted window. */
1368 if (NILP (p->buffer)
1369 && NILP (p->hchild)
1370 && NILP (p->vchild))
1371 return;
1373 parent = p->parent;
1374 if (NILP (parent))
1375 error ("Attempt to delete minibuffer or sole ordinary window");
1376 par = XWINDOW (parent);
1378 windows_or_buffers_changed++;
1379 Vwindow_list = Qnil;
1380 f = XFRAME (WINDOW_FRAME (p));
1381 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1383 /* Are we trying to delete any frame's selected window? */
1385 Lisp_Object swindow, pwindow;
1387 /* See if the frame's selected window is either WINDOW
1388 or any subwindow of it, by finding all that window's parents
1389 and comparing each one with WINDOW. */
1390 swindow = FRAME_SELECTED_WINDOW (f);
1392 while (1)
1394 pwindow = swindow;
1395 while (!NILP (pwindow))
1397 if (EQ (window, pwindow))
1398 break;
1399 pwindow = XWINDOW (pwindow)->parent;
1402 /* If the window being deleted is not a parent of SWINDOW,
1403 then SWINDOW is ok as the new selected window. */
1404 if (!EQ (window, pwindow))
1405 break;
1406 /* Otherwise, try another window for SWINDOW. */
1407 swindow = Fnext_window (swindow, Qlambda, Qnil);;
1409 /* If we get back to the frame's selected window,
1410 it means there was no acceptable alternative,
1411 so we cannot delete. */
1412 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1413 error ("Cannot delete window");
1416 /* If we need to change SWINDOW, do it. */
1417 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1419 /* If we're about to delete the selected window on the
1420 selected frame, then we should use Fselect_window to select
1421 the new window. On the other hand, if we're about to
1422 delete the selected window on any other frame, we shouldn't do
1423 anything but set the frame's selected_window slot. */
1424 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1425 Fselect_window (swindow, Qnil);
1426 else
1427 FRAME_SELECTED_WINDOW (f) = swindow;
1431 /* Now we know we can delete this one. */
1432 window_deletion_count++;
1434 tem = p->buffer;
1435 /* tem is null for dummy parent windows
1436 (which have inferiors but not any contents themselves) */
1437 if (!NILP (tem))
1439 unshow_buffer (p);
1440 unchain_marker (XMARKER (p->pointm));
1441 unchain_marker (XMARKER (p->start));
1444 /* Free window glyph matrices. It is sure that they are allocated
1445 again when ADJUST_GLYPHS is called. Block input so that expose
1446 events and other events that access glyph matrices are not
1447 processed while we are changing them. */
1448 BLOCK_INPUT;
1449 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1451 tem = p->next;
1452 if (!NILP (tem))
1453 XWINDOW (tem)->prev = p->prev;
1455 tem = p->prev;
1456 if (!NILP (tem))
1457 XWINDOW (tem)->next = p->next;
1459 if (EQ (window, par->hchild))
1460 par->hchild = p->next;
1461 if (EQ (window, par->vchild))
1462 par->vchild = p->next;
1464 /* Find one of our siblings to give our space to. */
1465 sib = p->prev;
1466 if (NILP (sib))
1468 /* If p gives its space to its next sibling, that sibling needs
1469 to have its top/left side pulled back to where p's is.
1470 set_window_{height,width} will re-position the sibling's
1471 children. */
1472 sib = p->next;
1473 XWINDOW (sib)->top_line = p->top_line;
1474 XWINDOW (sib)->left_col = p->left_col;
1477 /* Stretch that sibling. */
1478 if (!NILP (par->vchild))
1479 set_window_height (sib,
1480 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1482 if (!NILP (par->hchild))
1483 set_window_width (sib,
1484 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1487 /* If parent now has only one child,
1488 put the child into the parent's place. */
1489 tem = par->hchild;
1490 if (NILP (tem))
1491 tem = par->vchild;
1492 if (NILP (XWINDOW (tem)->next)) {
1493 replace_window (parent, tem);
1494 par = XWINDOW (tem);
1497 /* Since we may be deleting combination windows, we must make sure that
1498 not only p but all its children have been marked as deleted. */
1499 if (! NILP (p->hchild))
1500 delete_all_subwindows (XWINDOW (p->hchild));
1501 else if (! NILP (p->vchild))
1502 delete_all_subwindows (XWINDOW (p->vchild));
1504 /* Mark this window as deleted. */
1505 p->buffer = p->hchild = p->vchild = Qnil;
1507 if (! NILP (par->parent))
1508 par = XWINDOW (par->parent);
1510 /* Check if we have a v/hchild with a v/hchild. In that case remove
1511 one of them. */
1513 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1515 p = XWINDOW (par->vchild);
1516 par->vchild = p->vchild;
1517 tem = p->vchild;
1519 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1521 p = XWINDOW (par->hchild);
1522 par->hchild = p->hchild;
1523 tem = p->hchild;
1525 else
1526 p = 0;
1528 if (p)
1530 while (! NILP (tem)) {
1531 XWINDOW (tem)->parent = p->parent;
1532 if (NILP (XWINDOW (tem)->next))
1533 break;
1534 tem = XWINDOW (tem)->next;
1536 if (! NILP (tem)) {
1537 /* The next of the v/hchild we are removing is now the next of the
1538 last child for the v/hchild:
1539 Before v/hchild -> v/hchild -> next1 -> next2
1541 -> next3
1542 After: v/hchild -> next1 -> next2 -> next3
1544 XWINDOW (tem)->next = p->next;
1545 if (! NILP (p->next))
1546 XWINDOW (p->next)->prev = tem;
1548 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1552 /* Adjust glyph matrices. */
1553 adjust_glyphs (f);
1554 UNBLOCK_INPUT;
1559 /***********************************************************************
1560 Window List
1561 ***********************************************************************/
1563 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1564 pointer. This is a callback function for foreach_window, used in
1565 function window_list. */
1567 static int
1568 add_window_to_list (w, user_data)
1569 struct window *w;
1570 void *user_data;
1572 Lisp_Object *list = (Lisp_Object *) user_data;
1573 Lisp_Object window;
1574 XSETWINDOW (window, w);
1575 *list = Fcons (window, *list);
1576 return 1;
1580 /* Return a list of all windows, for use by next_window. If
1581 Vwindow_list is a list, return that list. Otherwise, build a new
1582 list, cache it in Vwindow_list, and return that. */
1584 static Lisp_Object
1585 window_list ()
1587 if (!CONSP (Vwindow_list))
1589 Lisp_Object tail;
1591 Vwindow_list = Qnil;
1592 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1594 Lisp_Object args[2];
1596 /* We are visiting windows in canonical order, and add
1597 new windows at the front of args[1], which means we
1598 have to reverse this list at the end. */
1599 args[1] = Qnil;
1600 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1601 args[0] = Vwindow_list;
1602 args[1] = Fnreverse (args[1]);
1603 Vwindow_list = Fnconc (2, args);
1607 return Vwindow_list;
1611 /* Value is non-zero if WINDOW satisfies the constraints given by
1612 OWINDOW, MINIBUF and ALL_FRAMES.
1614 MINIBUF t means WINDOW may be minibuffer windows.
1615 `lambda' means WINDOW may not be a minibuffer window.
1616 a window means a specific minibuffer window
1618 ALL_FRAMES t means search all frames,
1619 nil means search just current frame,
1620 `visible' means search just visible frames,
1621 0 means search visible and iconified frames,
1622 a window means search the frame that window belongs to,
1623 a frame means consider windows on that frame, only. */
1625 static int
1626 candidate_window_p (window, owindow, minibuf, all_frames)
1627 Lisp_Object window, owindow, minibuf, all_frames;
1629 struct window *w = XWINDOW (window);
1630 struct frame *f = XFRAME (w->frame);
1631 int candidate_p = 1;
1633 if (!BUFFERP (w->buffer))
1634 candidate_p = 0;
1635 else if (MINI_WINDOW_P (w)
1636 && (EQ (minibuf, Qlambda)
1637 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1639 /* If MINIBUF is `lambda' don't consider any mini-windows.
1640 If it is a window, consider only that one. */
1641 candidate_p = 0;
1643 else if (EQ (all_frames, Qt))
1644 candidate_p = 1;
1645 else if (NILP (all_frames))
1647 xassert (WINDOWP (owindow));
1648 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1650 else if (EQ (all_frames, Qvisible))
1652 FRAME_SAMPLE_VISIBILITY (f);
1653 candidate_p = FRAME_VISIBLE_P (f);
1655 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1657 FRAME_SAMPLE_VISIBILITY (f);
1658 candidate_p = FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1660 else if (WINDOWP (all_frames))
1661 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1662 || EQ (XWINDOW (all_frames)->frame, w->frame)
1663 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1664 else if (FRAMEP (all_frames))
1665 candidate_p = EQ (all_frames, w->frame);
1667 return candidate_p;
1671 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1672 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1673 ALL_FRAMES. */
1675 static void
1676 decode_next_window_args (window, minibuf, all_frames)
1677 Lisp_Object *window, *minibuf, *all_frames;
1679 if (NILP (*window))
1680 *window = selected_window;
1681 else
1682 CHECK_LIVE_WINDOW (*window);
1684 /* MINIBUF nil may or may not include minibuffers. Decide if it
1685 does. */
1686 if (NILP (*minibuf))
1687 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1688 else if (!EQ (*minibuf, Qt))
1689 *minibuf = Qlambda;
1691 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1692 => count none of them, or a specific minibuffer window (the
1693 active one) to count. */
1695 /* ALL_FRAMES nil doesn't specify which frames to include. */
1696 if (NILP (*all_frames))
1697 *all_frames = (!EQ (*minibuf, Qlambda)
1698 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1699 : Qnil);
1700 else if (EQ (*all_frames, Qvisible))
1702 else if (EQ (*all_frames, make_number (0)))
1704 else if (FRAMEP (*all_frames))
1706 else if (!EQ (*all_frames, Qt))
1707 *all_frames = Qnil;
1709 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1710 search just current frame, `visible' meaning search just visible
1711 frames, 0 meaning search visible and iconified frames, or a
1712 window, meaning search the frame that window belongs to, or a
1713 frame, meaning consider windows on that frame, only. */
1717 /* Return the next or previous window of WINDOW in canonical ordering
1718 of windows. NEXT_P non-zero means return the next window. See the
1719 documentation string of next-window for the meaning of MINIBUF and
1720 ALL_FRAMES. */
1722 static Lisp_Object
1723 next_window (window, minibuf, all_frames, next_p)
1724 Lisp_Object window, minibuf, all_frames;
1725 int next_p;
1727 decode_next_window_args (&window, &minibuf, &all_frames);
1729 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1730 return the first window on the frame. */
1731 if (FRAMEP (all_frames)
1732 && !EQ (all_frames, XWINDOW (window)->frame))
1733 return Fframe_first_window (all_frames);
1735 if (next_p)
1737 Lisp_Object list;
1739 /* Find WINDOW in the list of all windows. */
1740 list = Fmemq (window, window_list ());
1742 /* Scan forward from WINDOW to the end of the window list. */
1743 if (CONSP (list))
1744 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1745 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1746 break;
1748 /* Scan from the start of the window list up to WINDOW. */
1749 if (!CONSP (list))
1750 for (list = Vwindow_list;
1751 CONSP (list) && !EQ (XCAR (list), window);
1752 list = XCDR (list))
1753 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1754 break;
1756 if (CONSP (list))
1757 window = XCAR (list);
1759 else
1761 Lisp_Object candidate, list;
1763 /* Scan through the list of windows for candidates. If there are
1764 candidate windows in front of WINDOW, the last one of these
1765 is the one we want. If there are candidates following WINDOW
1766 in the list, again the last one of these is the one we want. */
1767 candidate = Qnil;
1768 for (list = window_list (); CONSP (list); list = XCDR (list))
1770 if (EQ (XCAR (list), window))
1772 if (WINDOWP (candidate))
1773 break;
1775 else if (candidate_window_p (XCAR (list), window, minibuf,
1776 all_frames))
1777 candidate = XCAR (list);
1780 if (WINDOWP (candidate))
1781 window = candidate;
1784 return window;
1788 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1789 doc: /* Return next window after WINDOW in canonical ordering of windows.
1790 If omitted, WINDOW defaults to the selected window.
1792 Optional second arg MINIBUF t means count the minibuffer window even
1793 if not active. MINIBUF nil or omitted means count the minibuffer iff
1794 it is active. MINIBUF neither t nor nil means not to count the
1795 minibuffer even if it is active.
1797 Several frames may share a single minibuffer; if the minibuffer
1798 counts, all windows on all frames that share that minibuffer count
1799 too. Therefore, `next-window' can be used to iterate through the
1800 set of windows even when the minibuffer is on another frame. If the
1801 minibuffer does not count, only windows from WINDOW's frame count.
1803 Optional third arg ALL-FRAMES t means include windows on all frames.
1804 ALL-FRAMES nil or omitted means cycle within the frames as specified
1805 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1806 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1807 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1808 Anything else means restrict to WINDOW's frame.
1810 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1811 `next-window' to iterate through the entire cycle of acceptable
1812 windows, eventually ending up back at the window you started with.
1813 `previous-window' traverses the same cycle, in the reverse order. */)
1814 (window, minibuf, all_frames)
1815 Lisp_Object window, minibuf, all_frames;
1817 return next_window (window, minibuf, all_frames, 1);
1821 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1822 doc: /* Return the window preceding WINDOW in canonical ordering of windows.
1823 If omitted, WINDOW defaults to the selected window.
1825 Optional second arg MINIBUF t means count the minibuffer window even
1826 if not active. MINIBUF nil or omitted means count the minibuffer iff
1827 it is active. MINIBUF neither t nor nil means not to count the
1828 minibuffer even if it is active.
1830 Several frames may share a single minibuffer; if the minibuffer
1831 counts, all windows on all frames that share that minibuffer count
1832 too. Therefore, `previous-window' can be used to iterate through
1833 the set of windows even when the minibuffer is on another frame. If
1834 the minibuffer does not count, only windows from WINDOW's frame count
1836 Optional third arg ALL-FRAMES t means include windows on all frames.
1837 ALL-FRAMES nil or omitted means cycle within the frames as specified
1838 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1839 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1840 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1841 Anything else means restrict to WINDOW's frame.
1843 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1844 `previous-window' to iterate through the entire cycle of acceptable
1845 windows, eventually ending up back at the window you started with.
1846 `next-window' traverses the same cycle, in the reverse order. */)
1847 (window, minibuf, all_frames)
1848 Lisp_Object window, minibuf, all_frames;
1850 return next_window (window, minibuf, all_frames, 0);
1854 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1855 doc: /* Select the ARG'th different window on this frame.
1856 All windows on current frame are arranged in a cyclic order.
1857 This command selects the window ARG steps away in that order.
1858 A negative ARG moves in the opposite order. The optional second
1859 argument ALL-FRAMES has the same meaning as in `next-window', which see. */)
1860 (arg, all_frames)
1861 Lisp_Object arg, all_frames;
1863 Lisp_Object window;
1864 int i;
1866 CHECK_NUMBER (arg);
1867 window = selected_window;
1869 for (i = XINT (arg); i > 0; --i)
1870 window = Fnext_window (window, Qnil, all_frames);
1871 for (; i < 0; ++i)
1872 window = Fprevious_window (window, Qnil, all_frames);
1874 Fselect_window (window, Qnil);
1875 return Qnil;
1879 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
1880 doc: /* Return a list of windows on FRAME, starting with WINDOW.
1881 FRAME nil or omitted means use the selected frame.
1882 WINDOW nil or omitted means use the selected window.
1883 MINIBUF t means include the minibuffer window, even if it isn't active.
1884 MINIBUF nil or omitted means include the minibuffer window only
1885 if it's active.
1886 MINIBUF neither nil nor t means never include the minibuffer window. */)
1887 (frame, minibuf, window)
1888 Lisp_Object frame, minibuf, window;
1890 if (NILP (window))
1891 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
1892 CHECK_WINDOW (window);
1893 if (NILP (frame))
1894 frame = selected_frame;
1896 if (!EQ (frame, XWINDOW (window)->frame))
1897 error ("Window is on a different frame");
1899 return window_list_1 (window, minibuf, frame);
1903 /* Return a list of windows in canonical ordering. Arguments are like
1904 for `next-window'. */
1906 static Lisp_Object
1907 window_list_1 (window, minibuf, all_frames)
1908 Lisp_Object window, minibuf, all_frames;
1910 Lisp_Object tail, list, rest;
1912 decode_next_window_args (&window, &minibuf, &all_frames);
1913 list = Qnil;
1915 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
1916 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
1917 list = Fcons (XCAR (tail), list);
1919 /* Rotate the list to start with WINDOW. */
1920 list = Fnreverse (list);
1921 rest = Fmemq (window, list);
1922 if (!NILP (rest) && !EQ (rest, list))
1924 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
1926 XSETCDR (tail, Qnil);
1927 list = nconc2 (rest, list);
1929 return list;
1934 /* Look at all windows, performing an operation specified by TYPE
1935 with argument OBJ.
1936 If FRAMES is Qt, look at all frames;
1937 Qnil, look at just the selected frame;
1938 Qvisible, look at visible frames;
1939 a frame, just look at windows on that frame.
1940 If MINI is non-zero, perform the operation on minibuffer windows too. */
1942 enum window_loop
1944 WINDOW_LOOP_UNUSED,
1945 GET_BUFFER_WINDOW, /* Arg is buffer */
1946 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1947 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1948 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1949 GET_LARGEST_WINDOW,
1950 UNSHOW_BUFFER, /* Arg is buffer */
1951 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
1952 CHECK_ALL_WINDOWS
1955 static Lisp_Object
1956 window_loop (type, obj, mini, frames)
1957 enum window_loop type;
1958 Lisp_Object obj, frames;
1959 int mini;
1961 Lisp_Object window, windows, best_window, frame_arg;
1962 struct frame *f;
1963 struct gcpro gcpro1;
1965 /* If we're only looping through windows on a particular frame,
1966 frame points to that frame. If we're looping through windows
1967 on all frames, frame is 0. */
1968 if (FRAMEP (frames))
1969 f = XFRAME (frames);
1970 else if (NILP (frames))
1971 f = SELECTED_FRAME ();
1972 else
1973 f = NULL;
1975 if (f)
1976 frame_arg = Qlambda;
1977 else if (EQ (frames, make_number (0)))
1978 frame_arg = frames;
1979 else if (EQ (frames, Qvisible))
1980 frame_arg = frames;
1981 else
1982 frame_arg = Qt;
1984 /* frame_arg is Qlambda to stick to one frame,
1985 Qvisible to consider all visible frames,
1986 or Qt otherwise. */
1988 /* Pick a window to start with. */
1989 if (WINDOWP (obj))
1990 window = obj;
1991 else if (f)
1992 window = FRAME_SELECTED_WINDOW (f);
1993 else
1994 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
1996 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
1997 GCPRO1 (windows);
1998 best_window = Qnil;
2000 for (; CONSP (windows); windows = XCDR (windows))
2002 struct window *w;
2004 window = XCAR (windows);
2005 w = XWINDOW (window);
2007 /* Note that we do not pay attention here to whether the frame
2008 is visible, since Fwindow_list skips non-visible frames if
2009 that is desired, under the control of frame_arg. */
2010 if (!MINI_WINDOW_P (w)
2011 /* For UNSHOW_BUFFER, we must always consider all windows. */
2012 || type == UNSHOW_BUFFER
2013 || (mini && minibuf_level > 0))
2014 switch (type)
2016 case GET_BUFFER_WINDOW:
2017 if (EQ (w->buffer, obj)
2018 /* Don't find any minibuffer window
2019 except the one that is currently in use. */
2020 && (MINI_WINDOW_P (w)
2021 ? EQ (window, minibuf_window)
2022 : 1))
2024 if (NILP (best_window))
2025 best_window = window;
2026 else if (EQ (window, selected_window))
2027 /* For compatibility with 20.x, prefer to return
2028 selected-window. */
2029 best_window = window;
2031 break;
2033 case GET_LRU_WINDOW:
2034 /* `obj' is an integer encoding a bitvector.
2035 `obj & 1' means consider only full-width windows.
2036 `obj & 2' means consider also dedicated windows. */
2037 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2038 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2039 /* Minibuffer windows are always ignored. */
2040 || MINI_WINDOW_P (w))
2041 break;
2042 if (NILP (best_window)
2043 || (XFASTINT (XWINDOW (best_window)->use_time)
2044 > XFASTINT (w->use_time)))
2045 best_window = window;
2046 break;
2048 case DELETE_OTHER_WINDOWS:
2049 if (!EQ (window, obj))
2050 Fdelete_window (window);
2051 break;
2053 case DELETE_BUFFER_WINDOWS:
2054 if (EQ (w->buffer, obj))
2056 struct frame *f = XFRAME (WINDOW_FRAME (w));
2058 /* If this window is dedicated, and in a frame of its own,
2059 kill the frame. */
2060 if (EQ (window, FRAME_ROOT_WINDOW (f))
2061 && !NILP (w->dedicated)
2062 && other_visible_frames (f))
2064 /* Skip the other windows on this frame.
2065 There might be one, the minibuffer! */
2066 while (CONSP (XCDR (windows))
2067 && EQ (XWINDOW (XCAR (windows))->frame,
2068 XWINDOW (XCAR (XCDR (windows)))->frame))
2069 windows = XCDR (windows);
2071 /* Now we can safely delete the frame. */
2072 Fdelete_frame (w->frame, Qnil);
2074 else if (NILP (w->parent))
2076 /* If we're deleting the buffer displayed in the
2077 only window on the frame, find a new buffer to
2078 display there. */
2079 Lisp_Object buffer;
2080 buffer = Fother_buffer (obj, Qnil, w->frame);
2081 Fset_window_buffer (window, buffer, Qnil);
2082 if (EQ (window, selected_window))
2083 Fset_buffer (w->buffer);
2085 else
2086 Fdelete_window (window);
2088 break;
2090 case GET_LARGEST_WINDOW:
2091 { /* nil `obj' means to ignore dedicated windows. */
2092 /* Ignore dedicated windows and minibuffers. */
2093 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2094 break;
2096 if (NILP (best_window))
2097 best_window = window;
2098 else
2100 struct window *b = XWINDOW (best_window);
2101 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2102 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2103 best_window = window;
2106 break;
2108 case UNSHOW_BUFFER:
2109 if (EQ (w->buffer, obj))
2111 Lisp_Object buffer;
2112 struct frame *f = XFRAME (w->frame);
2114 /* Find another buffer to show in this window. */
2115 buffer = Fother_buffer (obj, Qnil, w->frame);
2117 /* If this window is dedicated, and in a frame of its own,
2118 kill the frame. */
2119 if (EQ (window, FRAME_ROOT_WINDOW (f))
2120 && !NILP (w->dedicated)
2121 && other_visible_frames (f))
2123 /* Skip the other windows on this frame.
2124 There might be one, the minibuffer! */
2125 while (CONSP (XCDR (windows))
2126 && EQ (XWINDOW (XCAR (windows))->frame,
2127 XWINDOW (XCAR (XCDR (windows)))->frame))
2128 windows = XCDR (windows);
2130 /* Now we can safely delete the frame. */
2131 Fdelete_frame (w->frame, Qnil);
2133 else if (!NILP (w->dedicated) && !NILP (w->parent))
2135 Lisp_Object window;
2136 XSETWINDOW (window, w);
2137 /* If this window is dedicated and not the only window
2138 in its frame, then kill it. */
2139 Fdelete_window (window);
2141 else
2143 /* Otherwise show a different buffer in the window. */
2144 w->dedicated = Qnil;
2145 Fset_window_buffer (window, buffer, Qnil);
2146 if (EQ (window, selected_window))
2147 Fset_buffer (w->buffer);
2150 break;
2152 case REDISPLAY_BUFFER_WINDOWS:
2153 if (EQ (w->buffer, obj))
2155 mark_window_display_accurate (window, 0);
2156 w->update_mode_line = Qt;
2157 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2158 ++update_mode_lines;
2159 best_window = window;
2161 break;
2163 /* Check for a window that has a killed buffer. */
2164 case CHECK_ALL_WINDOWS:
2165 if (! NILP (w->buffer)
2166 && NILP (XBUFFER (w->buffer)->name))
2167 abort ();
2168 break;
2170 case WINDOW_LOOP_UNUSED:
2171 break;
2175 UNGCPRO;
2176 return best_window;
2179 /* Used for debugging. Abort if any window has a dead buffer. */
2181 void
2182 check_all_windows ()
2184 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2187 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2188 doc: /* Return the window least recently selected or used for display.
2189 Return a full-width window if possible.
2190 A minibuffer window is never a candidate.
2191 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2192 so if all windows are dedicated, the value is nil.
2193 If optional argument FRAME is `visible', search all visible frames.
2194 If FRAME is 0, search all visible and iconified frames.
2195 If FRAME is t, search all frames.
2196 If FRAME is nil, search only the selected frame.
2197 If FRAME is a frame, search only that frame. */)
2198 (frame, dedicated)
2199 Lisp_Object frame, dedicated;
2201 register Lisp_Object w;
2202 /* First try for a window that is full-width */
2203 w = window_loop (GET_LRU_WINDOW,
2204 NILP (dedicated) ? make_number (1) : make_number (3),
2205 0, frame);
2206 if (!NILP (w) && !EQ (w, selected_window))
2207 return w;
2208 /* If none of them, try the rest */
2209 return window_loop (GET_LRU_WINDOW,
2210 NILP (dedicated) ? make_number (0) : make_number (2),
2211 0, frame);
2214 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2215 doc: /* Return the largest window in area.
2216 A minibuffer window is never a candidate.
2217 A dedicated window is never a candidate unless DEDICATED is non-nil,
2218 so if all windows are dedicated, the value is nil.
2219 If optional argument FRAME is `visible', search all visible frames.
2220 If FRAME is 0, search all visible and iconified frames.
2221 If FRAME is t, search all frames.
2222 If FRAME is nil, search only the selected frame.
2223 If FRAME is a frame, search only that frame. */)
2224 (frame, dedicated)
2225 Lisp_Object frame, dedicated;
2227 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2228 frame);
2231 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
2232 doc: /* Return a window currently displaying BUFFER, or nil if none.
2233 BUFFER can be a buffer or a buffer name.
2234 If optional argument FRAME is `visible', search all visible frames.
2235 If optional argument FRAME is 0, search all visible and iconified frames.
2236 If FRAME is t, search all frames.
2237 If FRAME is nil, search only the selected frame.
2238 If FRAME is a frame, search only that frame. */)
2239 (buffer, frame)
2240 Lisp_Object buffer, frame;
2242 buffer = Fget_buffer (buffer);
2243 if (BUFFERP (buffer))
2244 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2245 else
2246 return Qnil;
2249 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2250 0, 1, "",
2251 doc: /* Make WINDOW (or the selected window) fill its frame.
2252 Only the frame WINDOW is on is affected.
2253 This function tries to reduce display jumps
2254 by keeping the text previously visible in WINDOW
2255 in the same place on the frame. Doing this depends on
2256 the value of (window-start WINDOW), so if calling this function
2257 in a program gives strange scrolling, make sure the window-start
2258 value is reasonable when this function is called. */)
2259 (window)
2260 Lisp_Object window;
2262 struct window *w;
2263 int startpos;
2264 int top, new_top;
2266 if (NILP (window))
2267 window = selected_window;
2268 else
2269 CHECK_LIVE_WINDOW (window);
2270 w = XWINDOW (window);
2272 startpos = marker_position (w->start);
2273 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2275 if (MINI_WINDOW_P (w) && top > 0)
2276 error ("Can't expand minibuffer to full frame");
2278 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2280 /* Try to minimize scrolling, by setting the window start to the point
2281 will cause the text at the old window start to be at the same place
2282 on the frame. But don't try to do this if the window start is
2283 outside the visible portion (as might happen when the display is
2284 not current, due to typeahead). */
2285 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2286 if (new_top != top
2287 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2288 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2290 struct position pos;
2291 struct buffer *obuf = current_buffer;
2293 Fset_buffer (w->buffer);
2294 /* This computation used to temporarily move point, but that can
2295 have unwanted side effects due to text properties. */
2296 pos = *vmotion (startpos, -top, w);
2298 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2299 w->window_end_valid = Qnil;
2300 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2301 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2302 : Qnil);
2303 /* We need to do this, so that the window-scroll-functions
2304 get called. */
2305 w->optional_new_start = Qt;
2307 set_buffer_internal (obuf);
2310 return Qnil;
2313 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2314 1, 2, "bDelete windows on (buffer): ",
2315 doc: /* Delete all windows showing BUFFER.
2316 BUFFER must be a buffer or the name of an existing buffer.
2317 Optional second argument FRAME controls which frames are affected.
2318 If optional argument FRAME is `visible', search all visible frames.
2319 If FRAME is 0, search all visible and iconified frames.
2320 If FRAME is nil, search all frames.
2321 If FRAME is t, search only the selected frame.
2322 If FRAME is a frame, search only that frame. */)
2323 (buffer, frame)
2324 Lisp_Object buffer, frame;
2326 /* FRAME uses t and nil to mean the opposite of what window_loop
2327 expects. */
2328 if (NILP (frame))
2329 frame = Qt;
2330 else if (EQ (frame, Qt))
2331 frame = Qnil;
2333 if (!NILP (buffer))
2335 buffer = Fget_buffer (buffer);
2336 CHECK_BUFFER (buffer);
2337 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2340 return Qnil;
2343 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2344 Sreplace_buffer_in_windows,
2345 1, 1, "bReplace buffer in windows: ",
2346 doc: /* Replace BUFFER with some other buffer in all windows showing it.
2347 BUFFER may be a buffer or the name of an existing buffer. */)
2348 (buffer)
2349 Lisp_Object buffer;
2351 if (!NILP (buffer))
2353 buffer = Fget_buffer (buffer);
2354 CHECK_BUFFER (buffer);
2355 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2357 return Qnil;
2360 /* Replace BUFFER with some other buffer in all windows
2361 of all frames, even those on other keyboards. */
2363 void
2364 replace_buffer_in_all_windows (buffer)
2365 Lisp_Object buffer;
2367 #ifdef MULTI_KBOARD
2368 Lisp_Object tail, frame;
2370 /* A single call to window_loop won't do the job
2371 because it only considers frames on the current keyboard.
2372 So loop manually over frames, and handle each one. */
2373 FOR_EACH_FRAME (tail, frame)
2374 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2375 #else
2376 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
2377 #endif
2380 /* Set the height of WINDOW and all its inferiors. */
2382 /* The smallest acceptable dimensions for a window. Anything smaller
2383 might crash Emacs. */
2385 #define MIN_SAFE_WINDOW_WIDTH (2)
2386 #define MIN_SAFE_WINDOW_HEIGHT (1)
2388 /* Make sure that window_min_height and window_min_width are
2389 not too small; if they are, set them to safe minima. */
2391 static void
2392 check_min_window_sizes ()
2394 /* Smaller values might permit a crash. */
2395 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
2396 window_min_width = MIN_SAFE_WINDOW_WIDTH;
2397 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
2398 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
2401 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2402 minimum allowable size. */
2404 void
2405 check_frame_size (frame, rows, cols)
2406 FRAME_PTR frame;
2407 int *rows, *cols;
2409 /* For height, we have to see:
2410 how many windows the frame has at minimum (one or two),
2411 and whether it has a menu bar or other special stuff at the top. */
2412 int min_height
2413 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2414 ? MIN_SAFE_WINDOW_HEIGHT
2415 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2417 if (FRAME_TOP_MARGIN (frame) > 0)
2418 min_height += FRAME_TOP_MARGIN (frame);
2420 if (*rows < min_height)
2421 *rows = min_height;
2422 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2423 *cols = MIN_SAFE_WINDOW_WIDTH;
2427 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2428 check if W's width can be changed, otherwise check W's height.
2429 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2430 siblings, too. If none of the siblings is resizable, WINDOW isn't
2431 either. */
2433 static int
2434 window_fixed_size_p (w, width_p, check_siblings_p)
2435 struct window *w;
2436 int width_p, check_siblings_p;
2438 int fixed_p;
2439 struct window *c;
2441 if (!NILP (w->hchild))
2443 c = XWINDOW (w->hchild);
2445 if (width_p)
2447 /* A horiz. combination is fixed-width if all of if its
2448 children are. */
2449 while (c && window_fixed_size_p (c, width_p, 0))
2450 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2451 fixed_p = c == NULL;
2453 else
2455 /* A horiz. combination is fixed-height if one of if its
2456 children is. */
2457 while (c && !window_fixed_size_p (c, width_p, 0))
2458 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2459 fixed_p = c != NULL;
2462 else if (!NILP (w->vchild))
2464 c = XWINDOW (w->vchild);
2466 if (width_p)
2468 /* A vert. combination is fixed-width if one of if its
2469 children is. */
2470 while (c && !window_fixed_size_p (c, width_p, 0))
2471 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2472 fixed_p = c != NULL;
2474 else
2476 /* A vert. combination is fixed-height if all of if its
2477 children are. */
2478 while (c && window_fixed_size_p (c, width_p, 0))
2479 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2480 fixed_p = c == NULL;
2483 else if (BUFFERP (w->buffer))
2485 struct buffer *old = current_buffer;
2486 Lisp_Object val;
2488 current_buffer = XBUFFER (w->buffer);
2489 val = find_symbol_value (Qwindow_size_fixed);
2490 current_buffer = old;
2492 fixed_p = 0;
2493 if (!EQ (val, Qunbound))
2495 fixed_p = !NILP (val);
2497 if (fixed_p
2498 && ((EQ (val, Qheight) && width_p)
2499 || (EQ (val, Qwidth) && !width_p)))
2500 fixed_p = 0;
2503 /* Can't tell if this one is resizable without looking at
2504 siblings. If all siblings are fixed-size this one is too. */
2505 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2507 Lisp_Object child;
2509 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2510 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2511 break;
2513 if (NILP (child))
2514 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2515 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2516 break;
2518 if (NILP (child))
2519 fixed_p = 1;
2522 else
2523 fixed_p = 1;
2525 return fixed_p;
2529 /* Return the minimum size of window W, not taking fixed-width windows
2530 into account. WIDTH_P non-zero means return the minimum width,
2531 otherwise return the minimum height. If W is a combination window,
2532 compute the minimum size from the minimum sizes of W's children. */
2534 static int
2535 window_min_size_1 (w, width_p)
2536 struct window *w;
2537 int width_p;
2539 struct window *c;
2540 int size;
2542 if (!NILP (w->hchild))
2544 c = XWINDOW (w->hchild);
2545 size = 0;
2547 if (width_p)
2549 /* The min width of a horizontal combination is
2550 the sum of the min widths of its children. */
2551 while (c)
2553 size += window_min_size_1 (c, width_p);
2554 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2557 else
2559 /* The min height a horizontal combination equals
2560 the maximum of all min height of its children. */
2561 while (c)
2563 int min_size = window_min_size_1 (c, width_p);
2564 size = max (min_size, size);
2565 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2569 else if (!NILP (w->vchild))
2571 c = XWINDOW (w->vchild);
2572 size = 0;
2574 if (width_p)
2576 /* The min width of a vertical combination is
2577 the maximum of the min widths of its children. */
2578 while (c)
2580 int min_size = window_min_size_1 (c, width_p);
2581 size = max (min_size, size);
2582 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2585 else
2587 /* The min height of a vertical combination equals
2588 the sum of the min height of its children. */
2589 while (c)
2591 size += window_min_size_1 (c, width_p);
2592 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2596 else
2598 if (width_p)
2599 size = max (window_min_width,
2600 (MIN_SAFE_WINDOW_WIDTH
2601 + WINDOW_FRINGE_COLS (w)
2602 + WINDOW_SCROLL_BAR_COLS (w)));
2603 else
2605 if (MINI_WINDOW_P (w)
2606 || (!WINDOW_WANTS_MODELINE_P (w)
2607 && !WINDOW_WANTS_HEADER_LINE_P (w)))
2608 size = 1;
2609 else
2610 size = window_min_height;
2614 return size;
2618 /* Return the minimum size of window W, taking fixed-size windows into
2619 account. WIDTH_P non-zero means return the minimum width,
2620 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2621 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2622 unless FIXED is null. */
2624 static int
2625 window_min_size (w, width_p, ignore_fixed_p, fixed)
2626 struct window *w;
2627 int width_p, ignore_fixed_p, *fixed;
2629 int size, fixed_p;
2631 if (ignore_fixed_p)
2632 fixed_p = 0;
2633 else
2634 fixed_p = window_fixed_size_p (w, width_p, 1);
2636 if (fixed)
2637 *fixed = fixed_p;
2639 if (fixed_p)
2640 size = width_p ? XFASTINT (w->total_cols) : XFASTINT (w->total_lines);
2641 else
2642 size = window_min_size_1 (w, width_p);
2644 return size;
2648 /* Adjust the margins of window W if text area is too small.
2649 Return 1 if window width is ok after adjustment; 0 if window
2650 is still too narrow. */
2652 static int
2653 adjust_window_margins (w)
2654 struct window *w;
2656 int box_cols = (WINDOW_TOTAL_COLS (w)
2657 - WINDOW_FRINGE_COLS (w)
2658 - WINDOW_SCROLL_BAR_COLS (w));
2659 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2660 + WINDOW_RIGHT_MARGIN_COLS (w));
2662 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2663 return 1;
2665 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2666 return 0;
2668 /* Window's text area is too narrow, but reducing the window
2669 margins will fix that. */
2670 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2671 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2673 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2674 w->left_margin_cols = w->right_margin_cols
2675 = make_number (margin_cols/2);
2676 else
2677 w->right_margin_cols = make_number (margin_cols);
2679 else
2680 w->left_margin_cols = make_number (margin_cols);
2681 return 1;
2684 /* Calculate new sizes for windows in the list FORWARD when the window size
2685 goes from TOTAL to SIZE. TOTAL must be greater than SIZE.
2686 The number of windows in FORWARD is NCHILDREN, and the number that
2687 can shrink is SHRINKABLE.
2688 The minimum size a window can have is MIN_SIZE.
2689 If we are shrinking fixed windows, RESIZE_FIXED_P is non-zero.
2690 If we are shrinking columns, WIDTH_P is non-zero, otherwise we are
2691 shrinking rows.
2693 This function returns an allocated array of new sizes that the caller
2694 must free. The size -1 means the window is fixed and RESIZE_FIXED_P
2695 is zero. Array index 0 refers to the first window in FORWARD, 1 to
2696 the second, and so on.
2698 This function tries to keep windows at least at the minimum size
2699 and resize other windows before it resizes any window to zero (i.e.
2700 delete that window).
2702 Windows are resized proportional to their size, so bigger windows
2703 shrink more than smaller windows. */
2704 static int *
2705 shrink_windows (total, size, nchildren, shrinkable,
2706 min_size, resize_fixed_p, forward, width_p)
2707 int total, size, nchildren, shrinkable, min_size;
2708 int resize_fixed_p, width_p;
2709 Lisp_Object forward;
2711 int available_resize = 0;
2712 int *new_sizes;
2713 struct window *c;
2714 Lisp_Object child;
2715 int smallest = total;
2716 int total_removed = 0;
2717 int total_shrink = total - size;
2718 int i;
2720 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2722 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2724 int child_size;
2726 c = XWINDOW (child);
2727 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2729 if (! resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2730 new_sizes[i] = -1;
2731 else
2733 new_sizes[i] = child_size;
2734 if (child_size > min_size)
2735 available_resize += child_size - min_size;
2738 /* We might need to shrink some windows to zero. Find the smallest
2739 windows and set them to 0 until we can fulfil the new size. */
2741 while (shrinkable > 1 && size + available_resize < total)
2743 for (i = 0; i < nchildren; ++i)
2744 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2745 smallest = new_sizes[i];
2747 for (i = 0; i < nchildren; ++i)
2748 if (new_sizes[i] == smallest)
2750 /* Resize this window down to zero. */
2751 new_sizes[i] = 0;
2752 if (smallest > min_size)
2753 available_resize -= smallest - min_size;
2754 available_resize += smallest;
2755 --shrinkable;
2756 total_removed += smallest;
2758 /* We don't know what the smallest is now. */
2759 smallest = total;
2761 /* Out of for, just remove one window at the time and
2762 check again if we have enough space. */
2763 break;
2767 /* Now, calculate the new sizes. Try to shrink each window
2768 proportional to its size. */
2769 for (i = 0; i < nchildren; ++i)
2771 if (new_sizes[i] > min_size)
2773 int to_shrink = total_shrink*new_sizes[i]/total;
2774 if (new_sizes[i] - to_shrink < min_size)
2775 to_shrink = new_sizes[i] - min_size;
2776 new_sizes[i] -= to_shrink;
2777 total_removed += to_shrink;
2781 /* Any reminder due to rounding, we just subtract from windows
2782 that are left and still can be shrunk. */
2783 while (total_shrink > total_removed)
2785 int nonzero_sizes = 0;
2786 int nonzero_idx = -1;
2788 for (i = 0; i < nchildren; ++i)
2789 if (new_sizes[i] > 0)
2791 ++nonzero_sizes;
2792 nonzero_idx = i;
2795 for (i = 0; i < nchildren; ++i)
2796 if (new_sizes[i] > min_size)
2798 --new_sizes[i];
2799 ++total_removed;
2801 /* Out of for, just shrink one window at the time and
2802 check again if we have enough space. */
2803 break;
2807 /* Special case, only one window left. */
2808 if (nonzero_sizes == 1)
2809 break;
2812 /* Any surplus due to rounding, we add to windows that are left. */
2813 while (total_shrink < total_removed)
2815 for (i = 0; i < nchildren; ++i)
2817 if (new_sizes[i] != 0 && total_shrink < total_removed)
2819 ++new_sizes[i];
2820 --total_removed;
2821 break;
2826 return new_sizes;
2829 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2830 WINDOW's width. Resize WINDOW's children, if any, so that they
2831 keep their proportionate size relative to WINDOW. Propagate
2832 WINDOW's top or left edge position to children. Delete windows
2833 that become too small unless NODELETE_P is non-zero.
2835 If NODELETE_P is 2, that means we do delete windows that are
2836 too small, even if they were too small before! */
2838 static void
2839 size_window (window, size, width_p, nodelete_p)
2840 Lisp_Object window;
2841 int size, width_p, nodelete_p;
2843 struct window *w = XWINDOW (window);
2844 struct window *c;
2845 Lisp_Object child, *forward, *sideward;
2846 int old_size, min_size, safe_min_size;
2848 /* We test nodelete_p != 2 and nodelete_p != 1 below, so it
2849 seems like it's too soon to do this here. ++KFS. */
2850 if (nodelete_p == 2)
2851 nodelete_p = 0;
2853 check_min_window_sizes ();
2854 size = max (0, size);
2856 /* If the window has been "too small" at one point,
2857 don't delete it for being "too small" in the future.
2858 Preserve it as long as that is at all possible. */
2859 if (width_p)
2861 old_size = WINDOW_TOTAL_COLS (w);
2862 min_size = window_min_width;
2863 /* Ensure that there is room for the scroll bar and fringes!
2864 We may reduce display margins though. */
2865 safe_min_size = (MIN_SAFE_WINDOW_WIDTH
2866 + WINDOW_FRINGE_COLS (w)
2867 + WINDOW_SCROLL_BAR_COLS (w));
2869 else
2871 old_size = XINT (w->total_lines);
2872 min_size = window_min_height;
2873 safe_min_size = MIN_SAFE_WINDOW_HEIGHT;
2876 if (old_size < min_size && nodelete_p != 2)
2877 w->too_small_ok = Qt;
2879 /* Maybe delete WINDOW if it's too small. */
2880 if (nodelete_p != 1 && !NILP (w->parent))
2882 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
2883 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
2884 if (min_size < safe_min_size)
2885 min_size = safe_min_size;
2886 if (size < min_size)
2888 delete_window (window);
2889 return;
2893 /* Set redisplay hints. */
2894 w->last_modified = make_number (0);
2895 w->last_overlay_modified = make_number (0);
2896 windows_or_buffers_changed++;
2897 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
2899 if (width_p)
2901 sideward = &w->vchild;
2902 forward = &w->hchild;
2903 w->total_cols = make_number (size);
2904 adjust_window_margins (w);
2906 else
2908 sideward = &w->hchild;
2909 forward = &w->vchild;
2910 w->total_lines = make_number (size);
2911 w->orig_total_lines = Qnil;
2914 if (!NILP (*sideward))
2916 for (child = *sideward; !NILP (child); child = c->next)
2918 c = XWINDOW (child);
2919 if (width_p)
2920 c->left_col = w->left_col;
2921 else
2922 c->top_line = w->top_line;
2923 size_window (child, size, width_p, nodelete_p);
2926 else if (!NILP (*forward))
2928 int fixed_size, each, extra, n;
2929 int resize_fixed_p, nfixed;
2930 int last_pos, first_pos, nchildren, total;
2931 int *new_sizes = NULL;
2933 /* Determine the fixed-size portion of the this window, and the
2934 number of child windows. */
2935 fixed_size = nchildren = nfixed = total = 0;
2936 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
2938 int child_size;
2940 c = XWINDOW (child);
2941 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2942 total += child_size;
2944 if (window_fixed_size_p (c, width_p, 0))
2946 fixed_size += child_size;
2947 ++nfixed;
2951 /* If the new size is smaller than fixed_size, or if there
2952 aren't any resizable windows, allow resizing fixed-size
2953 windows. */
2954 resize_fixed_p = nfixed == nchildren || size < fixed_size;
2956 /* Compute how many lines/columns to add/remove to each child. The
2957 value of extra takes care of rounding errors. */
2958 n = resize_fixed_p ? nchildren : nchildren - nfixed;
2959 if (size < total && n > 1)
2960 new_sizes = shrink_windows (total, size, nchildren, n, min_size,
2961 resize_fixed_p, *forward, width_p);
2962 else
2964 each = (size - total) / n;
2965 extra = (size - total) - n * each;
2968 /* Compute new children heights and edge positions. */
2969 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
2970 last_pos = first_pos;
2971 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
2973 int new_size, old_size;
2975 c = XWINDOW (child);
2976 old_size = width_p ? XFASTINT (c->total_cols) : XFASTINT (c->total_lines);
2977 new_size = old_size;
2979 /* The top or left edge position of this child equals the
2980 bottom or right edge of its predecessor. */
2981 if (width_p)
2982 c->left_col = make_number (last_pos);
2983 else
2984 c->top_line = make_number (last_pos);
2986 /* If this child can be resized, do it. */
2987 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
2989 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
2990 extra = 0;
2993 /* Set new height. Note that size_window also propagates
2994 edge positions to children, so it's not a no-op if we
2995 didn't change the child's size. */
2996 size_window (child, new_size, width_p, 1);
2998 /* Remember the bottom/right edge position of this child; it
2999 will be used to set the top/left edge of the next child. */
3000 last_pos += new_size;
3003 if (new_sizes) xfree (new_sizes);
3005 /* We should have covered the parent exactly with child windows. */
3006 xassert (size == last_pos - first_pos);
3008 /* Now delete any children that became too small. */
3009 if (!nodelete_p)
3010 for (child = *forward; !NILP (child); child = c->next)
3012 int child_size;
3013 c = XWINDOW (child);
3014 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
3015 size_window (child, child_size, width_p, 2);
3020 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3021 WINDOW's children. NODELETE non-zero means don't delete windows
3022 that become too small in the process. (The caller should check
3023 later and do so if appropriate.) */
3025 void
3026 set_window_height (window, height, nodelete)
3027 Lisp_Object window;
3028 int height;
3029 int nodelete;
3031 size_window (window, height, 0, nodelete);
3035 /* Set WINDOW's width to WIDTH, and recursively change the width of
3036 WINDOW's children. NODELETE non-zero means don't delete windows
3037 that become too small in the process. (The caller should check
3038 later and do so if appropriate.) */
3040 void
3041 set_window_width (window, width, nodelete)
3042 Lisp_Object window;
3043 int width;
3044 int nodelete;
3046 size_window (window, width, 1, nodelete);
3049 /* Change window heights in windows rooted in WINDOW by N lines. */
3051 void
3052 change_window_heights (window, n)
3053 Lisp_Object window;
3054 int n;
3056 struct window *w = XWINDOW (window);
3058 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3059 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3061 if (INTEGERP (w->orig_top_line))
3062 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3063 if (INTEGERP (w->orig_total_lines))
3064 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3066 /* Handle just the top child in a vertical split. */
3067 if (!NILP (w->vchild))
3068 change_window_heights (w->vchild, n);
3070 /* Adjust all children in a horizontal split. */
3071 for (window = w->hchild; !NILP (window); window = w->next)
3073 w = XWINDOW (window);
3074 change_window_heights (window, n);
3079 int window_select_count;
3081 Lisp_Object
3082 Fset_window_buffer_unwind (obuf)
3083 Lisp_Object obuf;
3085 Fset_buffer (obuf);
3086 return Qnil;
3089 EXFUN (Fset_window_fringes, 4);
3090 EXFUN (Fset_window_scroll_bars, 4);
3092 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3093 means it's allowed to run hooks. See make_frame for a case where
3094 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3095 margins, fringes, and scroll-bar settings of the window are not
3096 reset from the buffer's local settings. */
3098 void
3099 set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
3100 Lisp_Object window, buffer;
3101 int run_hooks_p, keep_margins_p;
3103 struct window *w = XWINDOW (window);
3104 struct buffer *b = XBUFFER (buffer);
3105 int count = SPECPDL_INDEX ();
3107 w->buffer = buffer;
3109 if (EQ (window, selected_window))
3110 b->last_selected_window = window;
3112 /* Let redisplay errors through. */
3113 b->display_error_modiff = 0;
3115 /* Update time stamps of buffer display. */
3116 if (INTEGERP (b->display_count))
3117 XSETINT (b->display_count, XINT (b->display_count) + 1);
3118 b->display_time = Fcurrent_time ();
3120 XSETFASTINT (w->window_end_pos, 0);
3121 XSETFASTINT (w->window_end_vpos, 0);
3122 bzero (&w->last_cursor, sizeof w->last_cursor);
3123 w->window_end_valid = Qnil;
3124 w->hscroll = w->min_hscroll = make_number (0);
3125 w->vscroll = 0;
3126 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3127 set_marker_restricted (w->start,
3128 make_number (b->last_window_start),
3129 buffer);
3130 w->start_at_line_beg = Qnil;
3131 w->force_start = Qnil;
3132 XSETFASTINT (w->last_modified, 0);
3133 XSETFASTINT (w->last_overlay_modified, 0);
3134 windows_or_buffers_changed++;
3136 /* We must select BUFFER for running the window-scroll-functions.
3137 If WINDOW is selected, switch permanently.
3138 Otherwise, switch but go back to the ambient buffer afterward. */
3139 if (EQ (window, selected_window))
3140 Fset_buffer (buffer);
3141 /* We can't check ! NILP (Vwindow_scroll_functions) here
3142 because that might itself be a local variable. */
3143 else if (window_initialized)
3145 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
3146 Fset_buffer (buffer);
3149 if (!keep_margins_p)
3151 /* Set left and right marginal area width etc. from buffer. */
3153 /* This may call adjust_window_margins three times, so
3154 temporarily disable window margins. */
3155 Lisp_Object save_left = w->left_margin_cols;
3156 Lisp_Object save_right = w->right_margin_cols;
3158 w->left_margin_cols = w->right_margin_cols = Qnil;
3160 Fset_window_fringes (window,
3161 b->left_fringe_width, b->right_fringe_width,
3162 b->fringes_outside_margins);
3164 Fset_window_scroll_bars (window,
3165 b->scroll_bar_width,
3166 b->vertical_scroll_bar_type, Qnil);
3168 w->left_margin_cols = save_left;
3169 w->right_margin_cols = save_right;
3171 Fset_window_margins (window,
3172 b->left_margin_cols, b->right_margin_cols);
3175 if (run_hooks_p)
3177 if (! NILP (Vwindow_scroll_functions))
3178 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3179 Fmarker_position (w->start));
3181 if (! NILP (Vwindow_configuration_change_hook)
3182 && ! NILP (Vrun_hooks))
3183 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3186 unbind_to (count, Qnil);
3190 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3191 doc: /* Make WINDOW display BUFFER as its contents.
3192 BUFFER can be a buffer or the name of an existing buffer.
3193 Optional third arg KEEP-MARGINS non-nil means that WINDOW's current
3194 display margins, fringe widths, and scroll bar settings are maintained;
3195 the default is to reset these from BUFFER's local settings or the frame
3196 defaults.
3198 This function runs the hook `window-scroll-functions'. */)
3199 (window, buffer, keep_margins)
3200 register Lisp_Object window, buffer, keep_margins;
3202 register Lisp_Object tem;
3203 register struct window *w = decode_window (window);
3205 XSETWINDOW (window, w);
3206 buffer = Fget_buffer (buffer);
3207 CHECK_BUFFER (buffer);
3209 if (NILP (XBUFFER (buffer)->name))
3210 error ("Attempt to display deleted buffer");
3212 tem = w->buffer;
3213 if (NILP (tem))
3214 error ("Window is deleted");
3215 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
3216 is first being set up. */
3218 if (!NILP (w->dedicated) && !EQ (tem, buffer))
3219 error ("Window is dedicated to `%s'",
3220 SDATA (XBUFFER (tem)->name));
3222 unshow_buffer (w);
3225 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3226 return Qnil;
3229 /* Note that selected_window can be nil
3230 when this is called from Fset_window_configuration. */
3232 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3233 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3234 If WINDOW is not already selected, make WINDOW's buffer current
3235 and make WINDOW the frame's selected window. Return WINDOW.
3236 Optional second arg NORECORD non-nil means
3237 do not put this buffer at the front of the list of recently selected ones.
3239 Note that the main editor command loop
3240 selects the buffer of the selected window before each command. */)
3241 (window, norecord)
3242 register Lisp_Object window, norecord;
3244 register struct window *w;
3245 register struct window *ow;
3246 struct frame *sf;
3248 CHECK_LIVE_WINDOW (window);
3250 w = XWINDOW (window);
3251 w->frozen_window_start_p = 0;
3253 ++window_select_count;
3254 XSETFASTINT (w->use_time, window_select_count);
3255 if (EQ (window, selected_window))
3256 return window;
3258 /* Store the current buffer's actual point into the
3259 old selected window. It belongs to that window,
3260 and when the window is not selected, must be in the window. */
3261 if (!NILP (selected_window))
3263 ow = XWINDOW (selected_window);
3264 if (! NILP (ow->buffer))
3265 set_marker_both (ow->pointm, ow->buffer,
3266 BUF_PT (XBUFFER (ow->buffer)),
3267 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3270 selected_window = window;
3271 sf = SELECTED_FRAME ();
3272 if (XFRAME (WINDOW_FRAME (w)) != sf)
3274 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3275 /* Use this rather than Fhandle_switch_frame
3276 so that FRAME_FOCUS_FRAME is moved appropriately as we
3277 move around in the state where a minibuffer in a separate
3278 frame is active. */
3279 Fselect_frame (WINDOW_FRAME (w));
3281 else
3282 sf->selected_window = window;
3284 if (NILP (norecord))
3285 record_buffer (w->buffer);
3286 Fset_buffer (w->buffer);
3288 XBUFFER (w->buffer)->last_selected_window = window;
3290 /* Go to the point recorded in the window.
3291 This is important when the buffer is in more
3292 than one window. It also matters when
3293 redisplay_window has altered point after scrolling,
3294 because it makes the change only in the window. */
3296 register int new_point = marker_position (w->pointm);
3297 if (new_point < BEGV)
3298 SET_PT (BEGV);
3299 else if (new_point > ZV)
3300 SET_PT (ZV);
3301 else
3302 SET_PT (new_point);
3305 windows_or_buffers_changed++;
3306 return window;
3309 static Lisp_Object
3310 select_window_norecord (window)
3311 Lisp_Object window;
3313 return Fselect_window (window, Qt);
3316 /* Deiconify the frame containing the window WINDOW,
3317 unless it is the selected frame;
3318 then return WINDOW.
3320 The reason for the exception for the selected frame
3321 is that it seems better not to change the selected frames visibility
3322 merely because of displaying a different buffer in it.
3323 The deiconification is useful when a buffer gets shown in
3324 another frame that you were not using lately. */
3326 static Lisp_Object
3327 display_buffer_1 (window)
3328 Lisp_Object window;
3330 Lisp_Object frame = XWINDOW (window)->frame;
3331 FRAME_PTR f = XFRAME (frame);
3333 FRAME_SAMPLE_VISIBILITY (f);
3335 if (EQ (frame, selected_frame))
3336 ; /* Assume the selected frame is already visible enough. */
3337 else if (minibuf_level > 0
3338 && MINI_WINDOW_P (XWINDOW (selected_window))
3339 && WINDOW_LIVE_P (minibuf_selected_window)
3340 && EQ (frame, WINDOW_FRAME (XWINDOW (minibuf_selected_window))))
3341 ; /* Assume the frame from which we invoked the minibuffer is visible. */
3342 else
3344 if (FRAME_ICONIFIED_P (f))
3345 Fmake_frame_visible (frame);
3346 else if (FRAME_VISIBLE_P (f))
3347 Fraise_frame (frame);
3350 return window;
3353 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
3354 doc: /* Returns non-nil if a buffer named BUFFER-NAME gets a special frame.
3355 If the value is t, `display-buffer' or `pop-to-buffer' would create a
3356 special frame for that buffer using the default frame parameters.
3358 If the value is a list, it is a list of frame parameters that would be used
3359 to make a frame for that buffer.
3360 The variables `special-display-buffer-names'
3361 and `special-display-regexps' control this. */)
3362 (buffer_name)
3363 Lisp_Object buffer_name;
3365 Lisp_Object tem;
3367 CHECK_STRING (buffer_name);
3369 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
3370 if (!NILP (tem))
3371 return Qt;
3373 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
3374 if (!NILP (tem))
3375 return XCDR (tem);
3377 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
3379 Lisp_Object car = XCAR (tem);
3380 if (STRINGP (car)
3381 && fast_string_match (car, buffer_name) >= 0)
3382 return Qt;
3383 else if (CONSP (car)
3384 && STRINGP (XCAR (car))
3385 && fast_string_match (XCAR (car), buffer_name) >= 0)
3386 return XCDR (car);
3388 return Qnil;
3391 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
3392 doc: /* Returns non-nil if a buffer named BUFFER-NAME would use the same window.
3393 More precisely, if `display-buffer' or `pop-to-buffer' would display
3394 that buffer in the selected window rather than (as usual) in some other window.
3395 See `same-window-buffer-names' and `same-window-regexps'. */)
3396 (buffer_name)
3397 Lisp_Object buffer_name;
3399 Lisp_Object tem;
3401 CHECK_STRING (buffer_name);
3403 tem = Fmember (buffer_name, Vsame_window_buffer_names);
3404 if (!NILP (tem))
3405 return Qt;
3407 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
3408 if (!NILP (tem))
3409 return Qt;
3411 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
3413 Lisp_Object car = XCAR (tem);
3414 if (STRINGP (car)
3415 && fast_string_match (car, buffer_name) >= 0)
3416 return Qt;
3417 else if (CONSP (car)
3418 && STRINGP (XCAR (car))
3419 && fast_string_match (XCAR (car), buffer_name) >= 0)
3420 return Qt;
3422 return Qnil;
3425 /* Use B so the default is (other-buffer). */
3426 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
3427 "BDisplay buffer: \nP",
3428 doc: /* Make BUFFER appear in some window but don't select it.
3429 BUFFER must be the name of an existing buffer, or, when called from Lisp,
3430 a buffer.
3431 If BUFFER is shown already in some window, just use that one,
3432 unless the window is the selected window and the optional second
3433 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).
3434 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
3435 Returns the window displaying BUFFER.
3436 If `display-buffer-reuse-frames' is non-nil, and another frame is currently
3437 displaying BUFFER, then simply raise that frame.
3439 The variables `special-display-buffer-names',
3440 `special-display-regexps', `same-window-buffer-names', and
3441 `same-window-regexps' customize how certain buffer names are handled.
3442 The latter two take effect only if NOT-THIS-WINDOW is t.
3444 If optional argument FRAME is `visible', search all visible frames.
3445 If FRAME is 0, search all visible and iconified frames.
3446 If FRAME is t, search all frames.
3447 If FRAME is a frame, search only that frame.
3448 If FRAME is nil, search only the selected frame
3449 (actually the last nonminibuffer frame),
3450 unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,
3451 which means search visible and iconified frames.
3453 If a full-width window on a splittable frame is available to display
3454 the buffer, it may be split, subject to the value of the variable
3455 `split-height-threshold'.
3457 If `even-window-heights' is non-nil, window heights will be evened out
3458 if displaying the buffer causes two vertically adjacent windows to be
3459 displayed. */)
3460 (buffer, not_this_window, frame)
3461 register Lisp_Object buffer, not_this_window, frame;
3463 register Lisp_Object window, tem, swp;
3464 struct frame *f;
3466 swp = Qnil;
3467 buffer = Fget_buffer (buffer);
3468 CHECK_BUFFER (buffer);
3470 if (!NILP (Vdisplay_buffer_function))
3471 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
3473 if (NILP (not_this_window)
3474 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
3475 return display_buffer_1 (selected_window);
3477 /* See if the user has specified this buffer should appear
3478 in the selected window. */
3479 if (NILP (not_this_window))
3481 swp = Fsame_window_p (XBUFFER (buffer)->name);
3482 if (!NILP (swp) && !no_switch_window (selected_window))
3484 Fswitch_to_buffer (buffer, Qnil);
3485 return display_buffer_1 (selected_window);
3489 /* If the user wants pop-up-frames or display-buffer-reuse-frames,
3490 look for a window showing BUFFER on any visible or iconified frame.
3491 Otherwise search only the current frame. */
3492 if (! NILP (frame))
3493 tem = frame;
3494 else if (pop_up_frames
3495 || display_buffer_reuse_frames
3496 || last_nonminibuf_frame == 0)
3497 XSETFASTINT (tem, 0);
3498 else
3499 XSETFRAME (tem, last_nonminibuf_frame);
3501 window = Fget_buffer_window (buffer, tem);
3502 if (!NILP (window)
3503 && (NILP (not_this_window) || !EQ (window, selected_window)))
3504 return display_buffer_1 (window);
3506 /* Certain buffer names get special handling. */
3507 if (!NILP (Vspecial_display_function) && NILP (swp))
3509 tem = Fspecial_display_p (XBUFFER (buffer)->name);
3510 if (EQ (tem, Qt))
3511 return call1 (Vspecial_display_function, buffer);
3512 if (CONSP (tem))
3513 return call2 (Vspecial_display_function, buffer, tem);
3516 /* If there are no frames open that have more than a minibuffer,
3517 we need to create a new frame. */
3518 if (pop_up_frames || last_nonminibuf_frame == 0)
3520 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3521 Fset_window_buffer (window, buffer, Qnil);
3522 return display_buffer_1 (window);
3525 f = SELECTED_FRAME ();
3526 if (pop_up_windows
3527 || FRAME_MINIBUF_ONLY_P (f)
3528 /* If the current frame is a special display frame,
3529 don't try to reuse its windows. */
3530 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
3532 Lisp_Object frames;
3534 frames = Qnil;
3535 if (FRAME_MINIBUF_ONLY_P (f))
3536 XSETFRAME (frames, last_nonminibuf_frame);
3537 /* Don't try to create a window if we would get an error. */
3538 if (split_height_threshold < window_min_height << 1)
3539 split_height_threshold = window_min_height << 1;
3541 /* Note that both Fget_largest_window and Fget_lru_window
3542 ignore minibuffers and dedicated windows.
3543 This means they can return nil. */
3545 /* If the frame we would try to split cannot be split,
3546 try other frames. */
3547 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
3549 /* Try visible frames first. */
3550 window = Fget_largest_window (Qvisible, Qt);
3551 /* If that didn't work, try iconified frames. */
3552 if (NILP (window))
3553 window = Fget_largest_window (make_number (0), Qt);
3554 #if 0 /* Don't try windows on other displays. */
3555 if (NILP (window))
3556 window = Fget_largest_window (Qt, Qt);
3557 #endif
3559 else
3560 window = Fget_largest_window (frames, Qt);
3562 /* If we got a tall enough full-width window that can be split,
3563 split it. */
3564 if (!NILP (window)
3565 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3566 && window_height (window) >= split_height_threshold
3567 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
3568 window = Fsplit_window (window, Qnil, Qnil);
3569 else
3571 Lisp_Object upper, lower, other;
3573 window = Fget_lru_window (frames, Qt);
3574 /* If the LRU window is selected, and big enough,
3575 and can be split, split it. */
3576 if (!NILP (window)
3577 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3578 && (EQ (window, selected_window)
3579 || EQ (XWINDOW (window)->parent, Qnil))
3580 && window_height (window) >= window_min_height << 1)
3581 window = Fsplit_window (window, Qnil, Qnil);
3582 else
3583 window = Fget_lru_window (frames, Qnil);
3584 /* If Fget_lru_window returned nil, try other approaches. */
3586 /* Try visible frames first. */
3587 if (NILP (window))
3588 window = Fget_buffer_window (buffer, Qvisible);
3589 if (NILP (window))
3590 window = Fget_largest_window (Qvisible, Qnil);
3591 /* If that didn't work, try iconified frames. */
3592 if (NILP (window))
3593 window = Fget_buffer_window (buffer, make_number (0));
3594 if (NILP (window))
3595 window = Fget_largest_window (make_number (0), Qnil);
3597 #if 0 /* Don't try frames on other displays. */
3598 if (NILP (window))
3599 window = Fget_buffer_window (buffer, Qt);
3600 if (NILP (window))
3601 window = Fget_largest_window (Qt, Qnil);
3602 #endif
3603 /* As a last resort, make a new frame. */
3604 if (NILP (window))
3605 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3606 /* If window appears above or below another,
3607 even out their heights. */
3608 other = upper = lower = Qnil;
3609 if (!NILP (XWINDOW (window)->prev))
3610 other = upper = XWINDOW (window)->prev, lower = window;
3611 if (!NILP (XWINDOW (window)->next))
3612 other = lower = XWINDOW (window)->next, upper = window;
3613 if (!NILP (other)
3614 && !NILP (Veven_window_heights)
3615 /* Check that OTHER and WINDOW are vertically arrayed. */
3616 && !EQ (XWINDOW (other)->top_line, XWINDOW (window)->top_line)
3617 && (XFASTINT (XWINDOW (other)->total_lines)
3618 > XFASTINT (XWINDOW (window)->total_lines)))
3620 int total = (XFASTINT (XWINDOW (other)->total_lines)
3621 + XFASTINT (XWINDOW (window)->total_lines));
3622 enlarge_window (upper,
3623 total / 2 - XFASTINT (XWINDOW (upper)->total_lines),
3628 else
3629 window = Fget_lru_window (Qnil, Qnil);
3631 Fset_window_buffer (window, buffer, Qnil);
3632 return display_buffer_1 (window);
3636 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3637 0, 1, 0,
3638 doc: /* Force redisplay of all windows.
3639 If optional arg OBJECT is a window, force redisplay of that window only.
3640 If OBJECT is a buffer or buffer name, force redisplay of all windows
3641 displaying that buffer. */)
3642 (object)
3643 Lisp_Object object;
3645 if (NILP (object))
3647 windows_or_buffers_changed++;
3648 update_mode_lines++;
3649 return Qt;
3652 if (WINDOWP (object))
3654 struct window *w = XWINDOW (object);
3655 mark_window_display_accurate (object, 0);
3656 w->update_mode_line = Qt;
3657 if (BUFFERP (w->buffer))
3658 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3659 ++update_mode_lines;
3660 return Qt;
3663 if (STRINGP (object))
3664 object = Fget_buffer (object);
3665 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3667 /* Walk all windows looking for buffer, and force update
3668 of each of those windows. */
3670 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3671 return NILP (object) ? Qnil : Qt;
3674 /* If nothing suitable was found, just return.
3675 We could signal an error, but this feature will typically be used
3676 asynchronously in timers or process sentinels, so we don't. */
3677 return Qnil;
3681 void
3682 temp_output_buffer_show (buf)
3683 register Lisp_Object buf;
3685 register struct buffer *old = current_buffer;
3686 register Lisp_Object window;
3687 register struct window *w;
3689 XBUFFER (buf)->directory = current_buffer->directory;
3691 Fset_buffer (buf);
3692 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3693 BEGV = BEG;
3694 ZV = Z;
3695 SET_PT (BEG);
3696 #if 0 /* rms: there should be no reason for this. */
3697 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3698 #endif
3699 set_buffer_internal (old);
3701 if (!NILP (Vtemp_buffer_show_function))
3702 call1 (Vtemp_buffer_show_function, buf);
3703 else
3705 window = Fdisplay_buffer (buf, Qnil, Qnil);
3707 if (!EQ (XWINDOW (window)->frame, selected_frame))
3708 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3709 Vminibuf_scroll_window = window;
3710 w = XWINDOW (window);
3711 XSETFASTINT (w->hscroll, 0);
3712 XSETFASTINT (w->min_hscroll, 0);
3713 set_marker_restricted_both (w->start, buf, BEG, BEG);
3714 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3716 /* Run temp-buffer-show-hook, with the chosen window selected
3717 and its buffer current. */
3719 if (!NILP (Vrun_hooks)
3720 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3721 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3723 int count = SPECPDL_INDEX ();
3724 Lisp_Object prev_window, prev_buffer;
3725 prev_window = selected_window;
3726 XSETBUFFER (prev_buffer, old);
3728 /* Select the window that was chosen, for running the hook.
3729 Note: Both Fselect_window and select_window_norecord may
3730 set-buffer to the buffer displayed in the window,
3731 so we need to save the current buffer. --stef */
3732 record_unwind_protect (Fset_buffer, prev_buffer);
3733 record_unwind_protect (select_window_norecord, prev_window);
3734 Fselect_window (window, Qt);
3735 Fset_buffer (w->buffer);
3736 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3737 unbind_to (count, Qnil);
3742 static void
3743 make_dummy_parent (window)
3744 Lisp_Object window;
3746 Lisp_Object new;
3747 register struct window *o, *p;
3748 int i;
3750 o = XWINDOW (window);
3751 p = allocate_window ();
3752 for (i = 0; i < VECSIZE (struct window); ++i)
3753 ((struct Lisp_Vector *) p)->contents[i]
3754 = ((struct Lisp_Vector *)o)->contents[i];
3755 XSETWINDOW (new, p);
3757 ++sequence_number;
3758 XSETFASTINT (p->sequence_number, sequence_number);
3760 /* Put new into window structure in place of window */
3761 replace_window (window, new);
3763 o->next = Qnil;
3764 o->prev = Qnil;
3765 o->vchild = Qnil;
3766 o->hchild = Qnil;
3767 o->parent = new;
3769 p->start = Qnil;
3770 p->pointm = Qnil;
3771 p->buffer = Qnil;
3774 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3775 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3776 WINDOW defaults to selected one and SIZE to half its size.
3777 If optional third arg HORFLAG is non-nil, split side by side
3778 and put SIZE columns in the first of the pair. In that case,
3779 SIZE includes that window's scroll bar, or the divider column to its right.
3780 Interactively, all arguments are nil.
3782 Returns the newly created window (which is the lower or rightmost one).
3783 The upper or leftmost window is the original one and remains selected.
3784 See Info node `(elisp)Splitting Windows' for more details and examples.*/)
3785 (window, size, horflag)
3786 Lisp_Object window, size, horflag;
3788 register Lisp_Object new;
3789 register struct window *o, *p;
3790 FRAME_PTR fo;
3791 register int size_int;
3793 if (NILP (window))
3794 window = selected_window;
3795 else
3796 CHECK_LIVE_WINDOW (window);
3798 o = XWINDOW (window);
3799 fo = XFRAME (WINDOW_FRAME (o));
3801 if (NILP (size))
3803 if (!NILP (horflag))
3804 /* Calculate the size of the left-hand window, by dividing
3805 the usable space in columns by two.
3806 We round up, since the left-hand window may include
3807 a dividing line, while the right-hand may not. */
3808 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3809 else
3810 size_int = XFASTINT (o->total_lines) >> 1;
3812 else
3814 CHECK_NUMBER (size);
3815 size_int = XINT (size);
3818 if (MINI_WINDOW_P (o))
3819 error ("Attempt to split minibuffer window");
3820 else if (window_fixed_size_p (o, !NILP (horflag), 0))
3821 error ("Attempt to split fixed-size window");
3823 check_min_window_sizes ();
3825 if (NILP (horflag))
3827 if (size_int < window_min_height)
3828 error ("Window height %d too small (after splitting)", size_int);
3829 if (size_int + window_min_height > XFASTINT (o->total_lines))
3830 error ("Window height %d too small (after splitting)",
3831 XFASTINT (o->total_lines) - size_int);
3832 if (NILP (o->parent)
3833 || NILP (XWINDOW (o->parent)->vchild))
3835 make_dummy_parent (window);
3836 new = o->parent;
3837 XWINDOW (new)->vchild = window;
3840 else
3842 if (size_int < window_min_width)
3843 error ("Window width %d too small (after splitting)", size_int);
3845 if (size_int + window_min_width > XFASTINT (o->total_cols))
3846 error ("Window width %d too small (after splitting)",
3847 XFASTINT (o->total_cols) - size_int);
3848 if (NILP (o->parent)
3849 || NILP (XWINDOW (o->parent)->hchild))
3851 make_dummy_parent (window);
3852 new = o->parent;
3853 XWINDOW (new)->hchild = window;
3857 /* Now we know that window's parent is a vertical combination
3858 if we are dividing vertically, or a horizontal combination
3859 if we are making side-by-side windows */
3861 windows_or_buffers_changed++;
3862 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3863 new = make_window ();
3864 p = XWINDOW (new);
3866 p->frame = o->frame;
3867 p->next = o->next;
3868 if (!NILP (p->next))
3869 XWINDOW (p->next)->prev = new;
3870 p->prev = window;
3871 o->next = new;
3872 p->parent = o->parent;
3873 p->buffer = Qt;
3874 p->window_end_valid = Qnil;
3875 bzero (&p->last_cursor, sizeof p->last_cursor);
3877 /* Duplicate special geometry settings. */
3879 p->left_margin_cols = o->left_margin_cols;
3880 p->right_margin_cols = o->right_margin_cols;
3881 p->left_fringe_width = o->left_fringe_width;
3882 p->right_fringe_width = o->right_fringe_width;
3883 p->fringes_outside_margins = o->fringes_outside_margins;
3884 p->scroll_bar_width = o->scroll_bar_width;
3885 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3887 /* Apportion the available frame space among the two new windows */
3889 if (!NILP (horflag))
3891 p->total_lines = o->total_lines;
3892 p->top_line = o->top_line;
3893 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3894 XSETFASTINT (o->total_cols, size_int);
3895 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3896 adjust_window_margins (p);
3897 adjust_window_margins (o);
3899 else
3901 p->left_col = o->left_col;
3902 p->total_cols = o->total_cols;
3903 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3904 XSETFASTINT (o->total_lines, size_int);
3905 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3908 /* Adjust glyph matrices. */
3909 adjust_glyphs (fo);
3911 Fset_window_buffer (new, o->buffer, Qt);
3912 return new;
3915 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3916 doc: /* Make current window ARG lines bigger.
3917 From program, optional second arg non-nil means grow sideways ARG columns.
3918 Interactively, if an argument is not given, make the window one line bigger.
3919 If HORIZONTAL is non-nil, enlarge horizontally instead of vertically.
3920 This function can delete windows, even the second window, if they get
3921 too small. */)
3922 (arg, horizontal)
3923 Lisp_Object arg, horizontal;
3925 CHECK_NUMBER (arg);
3926 enlarge_window (selected_window, XINT (arg), !NILP (horizontal));
3928 if (! NILP (Vwindow_configuration_change_hook))
3929 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3931 return Qnil;
3934 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3935 doc: /* Make current window ARG lines smaller.
3936 From program, optional second arg non-nil means shrink sideways arg columns.
3937 Interactively, if an argument is not given, make the window one line smaller. Only
3938 siblings to the right or below are changed. */)
3939 (arg, side)
3940 Lisp_Object arg, side;
3942 CHECK_NUMBER (arg);
3943 enlarge_window (selected_window, -XINT (arg), !NILP (side));
3945 if (! NILP (Vwindow_configuration_change_hook))
3946 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3948 return Qnil;
3952 window_height (window)
3953 Lisp_Object window;
3955 register struct window *p = XWINDOW (window);
3956 return WINDOW_TOTAL_LINES (p);
3960 window_width (window)
3961 Lisp_Object window;
3963 register struct window *p = XWINDOW (window);
3964 return WINDOW_TOTAL_COLS (p);
3968 #define CURBEG(w) \
3969 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3971 #define CURSIZE(w) \
3972 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3975 /* Enlarge WINDOW by DELTA.
3976 HORIZ_FLAG nonzero means enlarge it horizontally;
3977 zero means do it vertically.
3979 Siblings of the selected window are resized to fulfill the size
3980 request. If they become too small in the process, they will be
3981 deleted. */
3983 static void
3984 enlarge_window (window, delta, horiz_flag)
3985 Lisp_Object window;
3986 int delta, horiz_flag;
3988 Lisp_Object parent, next, prev;
3989 struct window *p;
3990 Lisp_Object *sizep;
3991 int maximum;
3992 int (*sizefun) P_ ((Lisp_Object))
3993 = horiz_flag ? window_width : window_height;
3994 void (*setsizefun) P_ ((Lisp_Object, int, int))
3995 = (horiz_flag ? set_window_width : set_window_height);
3997 /* Check values of window_min_width and window_min_height for
3998 validity. */
3999 check_min_window_sizes ();
4001 /* Give up if this window cannot be resized. */
4002 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4003 error ("Window is not resizable");
4005 /* Find the parent of the selected window. */
4006 while (1)
4008 p = XWINDOW (window);
4009 parent = p->parent;
4011 if (NILP (parent))
4013 if (horiz_flag)
4014 error ("No other window to side of this one");
4015 break;
4018 if (horiz_flag
4019 ? !NILP (XWINDOW (parent)->hchild)
4020 : !NILP (XWINDOW (parent)->vchild))
4021 break;
4023 window = parent;
4026 sizep = &CURSIZE (window);
4029 register int maxdelta;
4031 /* Compute the maximum size increment this window can have. */
4033 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4034 /* This is a main window followed by a minibuffer. */
4035 : !NILP (p->next) ? ((*sizefun) (p->next)
4036 - window_min_size (XWINDOW (p->next),
4037 horiz_flag, 0, 0))
4038 /* This is a minibuffer following a main window. */
4039 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4040 - window_min_size (XWINDOW (p->prev),
4041 horiz_flag, 0, 0))
4042 /* This is a frame with only one window, a minibuffer-only
4043 or a minibufferless frame. */
4044 : (delta = 0));
4046 if (delta > maxdelta)
4047 /* This case traps trying to make the minibuffer
4048 the full frame, or make the only window aside from the
4049 minibuffer the full frame. */
4050 delta = maxdelta;
4053 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window), horiz_flag, 0, 0))
4055 delete_window (window);
4056 return;
4059 if (delta == 0)
4060 return;
4062 /* Find the total we can get from other siblings without deleting them. */
4063 maximum = 0;
4064 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
4065 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4066 horiz_flag, 0, 0);
4067 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
4068 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4069 horiz_flag, 0, 0);
4071 /* If we can get it all from them without deleting them, do so. */
4072 if (delta <= maximum)
4074 Lisp_Object first_unaffected;
4075 Lisp_Object first_affected;
4076 int fixed_p;
4078 next = p->next;
4079 prev = p->prev;
4080 first_affected = window;
4081 /* Look at one sibling at a time,
4082 moving away from this window in both directions alternately,
4083 and take as much as we can get without deleting that sibling. */
4084 while (delta != 0
4085 && (!NILP (next) || !NILP (prev)))
4087 if (! NILP (next))
4089 int this_one = ((*sizefun) (next)
4090 - window_min_size (XWINDOW (next),
4091 horiz_flag, 0, &fixed_p));
4092 if (!fixed_p)
4094 if (this_one > delta)
4095 this_one = delta;
4097 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4098 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4100 delta -= this_one;
4103 next = XWINDOW (next)->next;
4106 if (delta == 0)
4107 break;
4109 if (! NILP (prev))
4111 int this_one = ((*sizefun) (prev)
4112 - window_min_size (XWINDOW (prev),
4113 horiz_flag, 0, &fixed_p));
4114 if (!fixed_p)
4116 if (this_one > delta)
4117 this_one = delta;
4119 first_affected = prev;
4121 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4122 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4124 delta -= this_one;
4127 prev = XWINDOW (prev)->prev;
4131 xassert (delta == 0);
4133 /* Now recalculate the edge positions of all the windows affected,
4134 based on the new sizes. */
4135 first_unaffected = next;
4136 prev = first_affected;
4137 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4138 prev = next, next = XWINDOW (next)->next)
4140 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4141 /* This does not change size of NEXT,
4142 but it propagates the new top edge to its children */
4143 (*setsizefun) (next, (*sizefun) (next), 0);
4146 else
4148 register int delta1;
4149 register int opht = (*sizefun) (parent);
4151 if (opht <= XINT (*sizep) + delta)
4153 /* If trying to grow this window to or beyond size of the parent,
4154 just delete all the sibling windows. */
4155 Lisp_Object start, tem, next;
4157 start = XWINDOW (parent)->vchild;
4158 if (NILP (start))
4159 start = XWINDOW (parent)->hchild;
4161 /* Delete any siblings that come after WINDOW. */
4162 tem = XWINDOW (window)->next;
4163 while (! NILP (tem))
4165 next = XWINDOW (tem)->next;
4166 delete_window (tem);
4167 tem = next;
4170 /* Delete any siblings that come after WINDOW.
4171 Note that if START is not WINDOW, then WINDOW still
4172 Fhas siblings, so WINDOW has not yet replaced its parent. */
4173 tem = start;
4174 while (! EQ (tem, window))
4176 next = XWINDOW (tem)->next;
4177 delete_window (tem);
4178 tem = next;
4181 else
4183 /* Otherwise, make delta1 just right so that if we add
4184 delta1 lines to this window and to the parent, and then
4185 shrink the parent back to its original size, the new
4186 proportional size of this window will increase by delta.
4188 The function size_window will compute the new height h'
4189 of the window from delta1 as:
4191 e = delta1/n
4192 x = delta1 - delta1/n * n for the 1st resizable child
4193 h' = h + e + x
4195 where n is the number of children that can be resized.
4196 We can ignore x by choosing a delta1 that is a multiple of
4197 n. We want the height of this window to come out as
4199 h' = h + delta
4201 So, delta1 must be
4203 h + e = h + delta
4204 delta1/n = delta
4205 delta1 = n * delta.
4207 The number of children n equals the number of resizable
4208 children of this window + 1 because we know window itself
4209 is resizable (otherwise we would have signalled an error). */
4211 struct window *w = XWINDOW (window);
4212 Lisp_Object s;
4213 int n = 1;
4215 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
4216 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4217 ++n;
4218 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
4219 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4220 ++n;
4222 delta1 = n * delta;
4224 /* Add delta1 lines or columns to this window, and to the parent,
4225 keeping things consistent while not affecting siblings. */
4226 XSETINT (CURSIZE (parent), opht + delta1);
4227 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4229 /* Squeeze out delta1 lines or columns from our parent,
4230 shriking this window and siblings proportionately.
4231 This brings parent back to correct size.
4232 Delta1 was calculated so this makes this window the desired size,
4233 taking it all out of the siblings. */
4234 (*setsizefun) (parent, opht, 0);
4239 XSETFASTINT (p->last_modified, 0);
4240 XSETFASTINT (p->last_overlay_modified, 0);
4242 /* Adjust glyph matrices. */
4243 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4247 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4248 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4249 zero means adjust the height, moving the bottom edge.
4251 Following siblings of the selected window are resized to fulfill
4252 the size request. If they become too small in the process, they
4253 are not deleted; instead, we signal an error. */
4255 static void
4256 adjust_window_trailing_edge (window, delta, horiz_flag)
4257 Lisp_Object window;
4258 int delta, horiz_flag;
4260 Lisp_Object parent, child;
4261 struct window *p;
4262 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4263 int delcount = window_deletion_count;
4265 /* Check values of window_min_width and window_min_height for
4266 validity. */
4267 check_min_window_sizes ();
4269 if (NILP (window))
4270 window = Fselected_window ();
4272 CHECK_WINDOW (window);
4274 /* Give up if this window cannot be resized. */
4275 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4276 error ("Window is not resizable");
4278 while (1)
4280 Lisp_Object first_parallel = Qnil;
4282 p = XWINDOW (window);
4283 parent = p->parent;
4285 if (NILP (XWINDOW (window)->next))
4287 Fset_window_configuration (old_config);
4288 error ("No other window following this one");
4291 /* See if this level has windows in parallel in the specified
4292 direction. If so, set FIRST_PARALLEL to the first one. */
4293 if (horiz_flag)
4295 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4296 first_parallel = XWINDOW (parent)->vchild;
4298 else
4300 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4301 first_parallel = XWINDOW (parent)->hchild;
4304 /* Don't make this window too small. */
4305 if (XINT (CURSIZE (window)) + delta
4306 < (horiz_flag ? window_min_width : window_min_height))
4308 Fset_window_configuration (old_config);
4309 error ("Cannot adjust window size as specified");
4312 /* Clear out some redisplay caches. */
4313 XSETFASTINT (p->last_modified, 0);
4314 XSETFASTINT (p->last_overlay_modified, 0);
4316 /* Adjust this window's edge. */
4317 XSETINT (CURSIZE (window),
4318 XINT (CURSIZE (window)) + delta);
4320 /* If this window has following siblings in the desired dimension,
4321 make them smaller, and exit the loop.
4323 (If we reach the top of the tree and can never do this,
4324 we will fail and report an error, above.) */
4325 if (NILP (first_parallel))
4327 if (!NILP (XWINDOW (window)->next))
4329 /* This may happen for the minibuffer. In that case
4330 the window_deletion_count check below does not work. */
4331 if (XINT (CURSIZE (p->next)) - delta <= 0)
4333 Fset_window_configuration (old_config);
4334 error ("Cannot adjust window size as specified");
4337 XSETINT (CURBEG (p->next),
4338 XINT (CURBEG (p->next)) + delta);
4339 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4340 horiz_flag, 0);
4341 break;
4344 else
4345 /* Here we have a chain of parallel siblings, in the other dimension.
4346 Change the size of the other siblings. */
4347 for (child = first_parallel;
4348 ! NILP (child);
4349 child = XWINDOW (child)->next)
4350 if (! EQ (child, window))
4351 size_window (child, XINT (CURSIZE (child)) + delta,
4352 horiz_flag, 0);
4354 window = parent;
4357 /* If we made a window so small it got deleted,
4358 we failed. Report failure. */
4359 if (delcount != window_deletion_count)
4361 Fset_window_configuration (old_config);
4362 error ("Cannot adjust window size as specified");
4365 /* Adjust glyph matrices. */
4366 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4369 #undef CURBEG
4370 #undef CURSIZE
4372 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4373 Sadjust_window_trailing_edge, 3, 3, 0,
4374 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4375 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4376 Otherwise, adjust the height, moving the bottom edge.
4378 Following siblings of the selected window are resized to fulfill
4379 the size request. If they become too small in the process, they
4380 are not deleted; instead, we signal an error. */)
4381 (window, delta, horizontal)
4382 Lisp_Object window, delta, horizontal;
4384 CHECK_NUMBER (delta);
4385 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4387 if (! NILP (Vwindow_configuration_change_hook))
4388 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
4390 return Qnil;
4395 /***********************************************************************
4396 Resizing Mini-Windows
4397 ***********************************************************************/
4399 static void shrink_window_lowest_first P_ ((struct window *, int));
4401 enum save_restore_action
4403 CHECK_ORIG_SIZES,
4404 SAVE_ORIG_SIZES,
4405 RESTORE_ORIG_SIZES
4408 static int save_restore_orig_size P_ ((struct window *,
4409 enum save_restore_action));
4411 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4412 from lowest windows first. */
4414 static void
4415 shrink_window_lowest_first (w, height)
4416 struct window *w;
4417 int height;
4419 struct window *c;
4420 Lisp_Object child;
4421 int old_height;
4423 xassert (!MINI_WINDOW_P (w));
4425 /* Set redisplay hints. */
4426 XSETFASTINT (w->last_modified, 0);
4427 XSETFASTINT (w->last_overlay_modified, 0);
4428 windows_or_buffers_changed++;
4429 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4431 old_height = XFASTINT (w->total_lines);
4432 XSETFASTINT (w->total_lines, height);
4434 if (!NILP (w->hchild))
4436 for (child = w->hchild; !NILP (child); child = c->next)
4438 c = XWINDOW (child);
4439 c->top_line = w->top_line;
4440 shrink_window_lowest_first (c, height);
4443 else if (!NILP (w->vchild))
4445 Lisp_Object last_child;
4446 int delta = old_height - height;
4447 int last_top;
4449 last_child = Qnil;
4451 /* Find the last child. We are taking space from lowest windows
4452 first, so we iterate over children from the last child
4453 backwards. */
4454 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
4455 last_child = child;
4457 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
4458 for (child = last_child; delta && !NILP (child); child = c->prev)
4460 int this_one;
4462 c = XWINDOW (child);
4463 this_one = XFASTINT (c->total_lines) - MIN_SAFE_WINDOW_HEIGHT;
4465 if (this_one > delta)
4466 this_one = delta;
4468 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4469 delta -= this_one;
4472 /* Compute new positions. */
4473 last_top = XINT (w->top_line);
4474 for (child = w->vchild; !NILP (child); child = c->next)
4476 c = XWINDOW (child);
4477 c->top_line = make_number (last_top);
4478 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4479 last_top += XFASTINT (c->total_lines);
4485 /* Save, restore, or check positions and sizes in the window tree
4486 rooted at W. ACTION says what to do.
4488 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4489 orig_total_lines members are valid for all windows in the window
4490 tree. Value is non-zero if they are valid.
4492 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4493 orig_top_line and orig_total_lines for all windows in the tree.
4495 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4496 stored in orig_top_line and orig_total_lines for all windows. */
4498 static int
4499 save_restore_orig_size (w, action)
4500 struct window *w;
4501 enum save_restore_action action;
4503 int success_p = 1;
4505 while (w)
4507 if (!NILP (w->hchild))
4509 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4510 success_p = 0;
4512 else if (!NILP (w->vchild))
4514 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4515 success_p = 0;
4518 switch (action)
4520 case CHECK_ORIG_SIZES:
4521 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4522 return 0;
4523 break;
4525 case SAVE_ORIG_SIZES:
4526 w->orig_top_line = w->top_line;
4527 w->orig_total_lines = w->total_lines;
4528 XSETFASTINT (w->last_modified, 0);
4529 XSETFASTINT (w->last_overlay_modified, 0);
4530 break;
4532 case RESTORE_ORIG_SIZES:
4533 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4534 w->top_line = w->orig_top_line;
4535 w->total_lines = w->orig_total_lines;
4536 w->orig_total_lines = w->orig_top_line = Qnil;
4537 XSETFASTINT (w->last_modified, 0);
4538 XSETFASTINT (w->last_overlay_modified, 0);
4539 break;
4541 default:
4542 abort ();
4545 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4548 return success_p;
4552 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4553 without deleting other windows. */
4555 void
4556 grow_mini_window (w, delta)
4557 struct window *w;
4558 int delta;
4560 struct frame *f = XFRAME (w->frame);
4561 struct window *root;
4563 xassert (MINI_WINDOW_P (w));
4564 xassert (delta >= 0);
4566 /* Check values of window_min_width and window_min_height for
4567 validity. */
4568 check_min_window_sizes ();
4570 /* Compute how much we can enlarge the mini-window without deleting
4571 other windows. */
4572 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4573 if (delta)
4575 int min_height = window_min_size (root, 0, 0, 0);
4576 if (XFASTINT (root->total_lines) - delta < min_height)
4577 /* Note that the root window may already be smaller than
4578 min_height. */
4579 delta = max (0, XFASTINT (root->total_lines) - min_height);
4582 if (delta)
4584 /* Save original window sizes and positions, if not already done. */
4585 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4586 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4588 /* Shrink other windows. */
4589 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4591 /* Grow the mini-window. */
4592 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4593 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4594 XSETFASTINT (w->last_modified, 0);
4595 XSETFASTINT (w->last_overlay_modified, 0);
4597 adjust_glyphs (f);
4602 /* Shrink mini-window W. If there is recorded info about window sizes
4603 before a call to grow_mini_window, restore recorded window sizes.
4604 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4605 line. */
4607 void
4608 shrink_mini_window (w)
4609 struct window *w;
4611 struct frame *f = XFRAME (w->frame);
4612 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4614 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4616 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4617 adjust_glyphs (f);
4618 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4619 windows_or_buffers_changed = 1;
4621 else if (XFASTINT (w->total_lines) > 1)
4623 /* Distribute the additional lines of the mini-window
4624 among the other windows. */
4625 Lisp_Object window;
4626 XSETWINDOW (window, w);
4627 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4633 /* Mark window cursors off for all windows in the window tree rooted
4634 at W by setting their phys_cursor_on_p flag to zero. Called from
4635 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4636 the frame are cleared. */
4638 void
4639 mark_window_cursors_off (w)
4640 struct window *w;
4642 while (w)
4644 if (!NILP (w->hchild))
4645 mark_window_cursors_off (XWINDOW (w->hchild));
4646 else if (!NILP (w->vchild))
4647 mark_window_cursors_off (XWINDOW (w->vchild));
4648 else
4649 w->phys_cursor_on_p = 0;
4651 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4656 /* Return number of lines of text (not counting mode lines) in W. */
4659 window_internal_height (w)
4660 struct window *w;
4662 int ht = XFASTINT (w->total_lines);
4664 if (!MINI_WINDOW_P (w))
4666 if (!NILP (w->parent)
4667 || !NILP (w->vchild)
4668 || !NILP (w->hchild)
4669 || !NILP (w->next)
4670 || !NILP (w->prev)
4671 || WINDOW_WANTS_MODELINE_P (w))
4672 --ht;
4674 if (WINDOW_WANTS_HEADER_LINE_P (w))
4675 --ht;
4678 return ht;
4682 /* Return the number of columns in W.
4683 Don't count columns occupied by scroll bars or the vertical bar
4684 separating W from the sibling to its right. */
4687 window_box_text_cols (w)
4688 struct window *w;
4690 struct frame *f = XFRAME (WINDOW_FRAME (w));
4691 int width = XINT (w->total_cols);
4693 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4694 /* Scroll bars occupy a few columns. */
4695 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4696 else if (!FRAME_WINDOW_P (f)
4697 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4698 /* The column of `|' characters separating side-by-side windows
4699 occupies one column only. */
4700 width -= 1;
4702 if (FRAME_WINDOW_P (f))
4703 /* On window-systems, fringes and display margins cannot be
4704 used for normal text. */
4705 width -= (WINDOW_FRINGE_COLS (w)
4706 + WINDOW_LEFT_MARGIN_COLS (w)
4707 + WINDOW_RIGHT_MARGIN_COLS (w));
4709 return width;
4713 /************************************************************************
4714 Window Scrolling
4715 ***********************************************************************/
4717 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4718 N screen-fulls, which is defined as the height of the window minus
4719 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4720 instead. Negative values of N mean scroll down. NOERROR non-zero
4721 means don't signal an error if we try to move over BEGV or ZV,
4722 respectively. */
4724 static void
4725 window_scroll (window, n, whole, noerror)
4726 Lisp_Object window;
4727 int n;
4728 int whole;
4729 int noerror;
4731 immediate_quit = 1;
4733 /* If we must, use the pixel-based version which is much slower than
4734 the line-based one but can handle varying line heights. */
4735 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4736 window_scroll_pixel_based (window, n, whole, noerror);
4737 else
4738 window_scroll_line_based (window, n, whole, noerror);
4740 immediate_quit = 0;
4744 /* Implementation of window_scroll that works based on pixel line
4745 heights. See the comment of window_scroll for parameter
4746 descriptions. */
4748 static void
4749 window_scroll_pixel_based (window, n, whole, noerror)
4750 Lisp_Object window;
4751 int n;
4752 int whole;
4753 int noerror;
4755 struct it it;
4756 struct window *w = XWINDOW (window);
4757 struct text_pos start;
4758 Lisp_Object tem;
4759 int this_scroll_margin;
4760 /* True if we fiddled the window vscroll field without really scrolling. */
4761 int vscrolled = 0;
4763 SET_TEXT_POS_FROM_MARKER (start, w->start);
4765 /* If PT is not visible in WINDOW, move back one half of
4766 the screen. Allow PT to be partially visible, otherwise
4767 something like (scroll-down 1) with PT in the line before
4768 the partially visible one would recenter. */
4769 tem = Fpos_visible_in_window_p (make_number (PT), window, Qt);
4770 if (NILP (tem))
4772 /* Move backward half the height of the window. Performance note:
4773 vmotion used here is about 10% faster, but would give wrong
4774 results for variable height lines. */
4775 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4776 it.current_y = it.last_visible_y;
4777 move_it_vertically_backward (&it, window_box_height (w) / 2);
4779 /* The function move_iterator_vertically may move over more than
4780 the specified y-distance. If it->w is small, e.g. a
4781 mini-buffer window, we may end up in front of the window's
4782 display area. This is the case when Start displaying at the
4783 start of the line containing PT in this case. */
4784 if (it.current_y <= 0)
4786 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4787 move_it_vertically_backward (&it, 0);
4788 it.current_y = 0;
4791 start = it.current.pos;
4793 else if (auto_window_vscroll_p)
4795 if (tem = XCAR (XCDR (XCDR (tem))), CONSP (tem))
4797 int px;
4798 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4799 if (whole)
4800 dy = max ((window_box_height (w)
4801 - next_screen_context_lines * dy),
4802 dy);
4803 dy *= n;
4805 if (n < 0 && (px = XINT (XCAR (tem))) > 0)
4807 px = max (0, -w->vscroll - min (px, -dy));
4808 Fset_window_vscroll (window, make_number (px), Qt);
4809 return;
4811 if (n > 0 && (px = XINT (XCDR (tem))) > 0)
4813 px = max (0, -w->vscroll + min (px, dy));
4814 Fset_window_vscroll (window, make_number (px), Qt);
4815 return;
4818 Fset_window_vscroll (window, make_number (0), Qt);
4821 /* If scroll_preserve_screen_position is non-nil, we try to set
4822 point in the same window line as it is now, so get that line. */
4823 if (!NILP (Vscroll_preserve_screen_position))
4825 /* We preserve the goal pixel coordinate across consecutive
4826 calls to scroll-up or scroll-down. This avoids the
4827 possibility of point becoming "stuck" on a tall line when
4828 scrolling by one line. */
4829 if (window_scroll_pixel_based_preserve_y < 0
4830 || (!EQ (current_kboard->Vlast_command, Qscroll_up)
4831 && !EQ (current_kboard->Vlast_command, Qscroll_down)))
4833 start_display (&it, w, start);
4834 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4835 window_scroll_pixel_based_preserve_y = it.current_y;
4838 else
4839 window_scroll_pixel_based_preserve_y = -1;
4841 /* Move iterator it from start the specified distance forward or
4842 backward. The result is the new window start. */
4843 start_display (&it, w, start);
4844 if (whole)
4846 int start_pos = IT_CHARPOS (it);
4847 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4848 dy = max ((window_box_height (w)
4849 - next_screen_context_lines * dy),
4850 dy) * n;
4852 /* Note that move_it_vertically always moves the iterator to the
4853 start of a line. So, if the last line doesn't have a newline,
4854 we would end up at the start of the line ending at ZV. */
4855 if (dy <= 0)
4857 move_it_vertically_backward (&it, -dy);
4858 /* Ensure we actually does move, e.g. in case we are currently
4859 looking at an image that is taller that the window height. */
4860 while (start_pos == IT_CHARPOS (it)
4861 && start_pos > BEGV)
4862 move_it_by_lines (&it, -1, 1);
4864 else if (dy > 0)
4866 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4867 MOVE_TO_POS | MOVE_TO_Y);
4868 /* Ensure we actually does move, e.g. in case we are currently
4869 looking at an image that is taller that the window height. */
4870 while (start_pos == IT_CHARPOS (it)
4871 && start_pos < ZV)
4872 move_it_by_lines (&it, 1, 1);
4875 else
4876 move_it_by_lines (&it, n, 1);
4878 /* We failed if we find ZV is already on the screen (scrolling up,
4879 means there's nothing past the end), or if we can't start any
4880 earlier (scrolling down, means there's nothing past the top). */
4881 if ((n > 0 && IT_CHARPOS (it) == ZV)
4882 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4884 if (IT_CHARPOS (it) == ZV)
4886 if (it.current_y < it.last_visible_y
4887 && (it.current_y + it.max_ascent + it.max_descent
4888 > it.last_visible_y))
4890 /* The last line was only partially visible, make it fully
4891 visible. */
4892 w->vscroll = (it.last_visible_y
4893 - it.current_y + it.max_ascent + it.max_descent);
4894 adjust_glyphs (it.f);
4896 else if (noerror)
4897 return;
4898 else
4899 Fsignal (Qend_of_buffer, Qnil);
4901 else
4903 if (w->vscroll != 0)
4904 /* The first line was only partially visible, make it fully
4905 visible. */
4906 w->vscroll = 0;
4907 else if (noerror)
4908 return;
4909 else
4910 Fsignal (Qbeginning_of_buffer, Qnil);
4913 /* If control gets here, then we vscrolled. */
4915 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4917 /* Don't try to change the window start below. */
4918 vscrolled = 1;
4921 if (! vscrolled)
4923 int pos = IT_CHARPOS (it);
4924 int bytepos;
4926 /* If in the middle of a multi-glyph character move forward to
4927 the next character. */
4928 if (in_display_vector_p (&it))
4930 ++pos;
4931 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4934 /* Set the window start, and set up the window for redisplay. */
4935 set_marker_restricted (w->start, make_number (pos),
4936 w->buffer);
4937 bytepos = XMARKER (w->start)->bytepos;
4938 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4939 ? Qt : Qnil);
4940 w->update_mode_line = Qt;
4941 XSETFASTINT (w->last_modified, 0);
4942 XSETFASTINT (w->last_overlay_modified, 0);
4943 /* Set force_start so that redisplay_window will run the
4944 window-scroll-functions. */
4945 w->force_start = Qt;
4948 /* The rest of this function uses current_y in a nonstandard way,
4949 not including the height of the header line if any. */
4950 it.current_y = it.vpos = 0;
4952 /* Move PT out of scroll margins.
4953 This code wants current_y to be zero at the window start position
4954 even if there is a header line. */
4955 this_scroll_margin = max (0, scroll_margin);
4956 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4957 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4959 if (n > 0)
4961 /* We moved the window start towards ZV, so PT may be now
4962 in the scroll margin at the top. */
4963 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4964 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4965 && (NILP (Vscroll_preserve_screen_position)
4966 || EQ (Vscroll_preserve_screen_position, Qt)))
4967 /* We found PT at a legitimate height. Leave it alone. */
4969 else if (window_scroll_pixel_based_preserve_y >= 0)
4971 /* If we have a header line, take account of it.
4972 This is necessary because we set it.current_y to 0, above. */
4973 move_it_to (&it, -1, -1,
4974 window_scroll_pixel_based_preserve_y
4975 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4976 -1, MOVE_TO_Y);
4977 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4979 else
4981 while (it.current_y < this_scroll_margin)
4983 int prev = it.current_y;
4984 move_it_by_lines (&it, 1, 1);
4985 if (prev == it.current_y)
4986 break;
4988 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4991 else if (n < 0)
4993 int charpos, bytepos;
4994 int partial_p;
4996 /* Save our position, for the
4997 window_scroll_pixel_based_preserve_y case. */
4998 charpos = IT_CHARPOS (it);
4999 bytepos = IT_BYTEPOS (it);
5001 /* We moved the window start towards BEGV, so PT may be now
5002 in the scroll margin at the bottom. */
5003 move_it_to (&it, PT, -1,
5004 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5005 - this_scroll_margin - 1),
5007 MOVE_TO_POS | MOVE_TO_Y);
5009 /* Save our position, in case it's correct. */
5010 charpos = IT_CHARPOS (it);
5011 bytepos = IT_BYTEPOS (it);
5013 /* See if point is on a partially visible line at the end. */
5014 if (it.what == IT_EOB)
5015 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5016 else
5018 move_it_by_lines (&it, 1, 1);
5019 partial_p = it.current_y > it.last_visible_y;
5022 if (charpos == PT && !partial_p
5023 && (NILP (Vscroll_preserve_screen_position)
5024 || EQ (Vscroll_preserve_screen_position, Qt)))
5025 /* We found PT before we found the display margin, so PT is ok. */
5027 else if (window_scroll_pixel_based_preserve_y >= 0)
5029 SET_TEXT_POS_FROM_MARKER (start, w->start);
5030 start_display (&it, w, start);
5031 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5032 here because we called start_display again and did not
5033 alter it.current_y this time. */
5034 move_it_to (&it, -1, -1, window_scroll_pixel_based_preserve_y, -1,
5035 MOVE_TO_Y);
5036 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5038 else
5040 if (partial_p)
5041 /* The last line was only partially visible, so back up two
5042 lines to make sure we're on a fully visible line. */
5044 move_it_by_lines (&it, -2, 0);
5045 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5047 else
5048 /* No, the position we saved is OK, so use it. */
5049 SET_PT_BOTH (charpos, bytepos);
5055 /* Implementation of window_scroll that works based on screen lines.
5056 See the comment of window_scroll for parameter descriptions. */
5058 static void
5059 window_scroll_line_based (window, n, whole, noerror)
5060 Lisp_Object window;
5061 int n;
5062 int whole;
5063 int noerror;
5065 register struct window *w = XWINDOW (window);
5066 register int opoint = PT, opoint_byte = PT_BYTE;
5067 register int pos, pos_byte;
5068 register int ht = window_internal_height (w);
5069 register Lisp_Object tem;
5070 int lose;
5071 Lisp_Object bolp;
5072 int startpos;
5073 struct position posit;
5074 int original_vpos;
5076 /* If scrolling screen-fulls, compute the number of lines to
5077 scroll from the window's height. */
5078 if (whole)
5079 n *= max (1, ht - next_screen_context_lines);
5081 startpos = marker_position (w->start);
5083 posit = *compute_motion (startpos, 0, 0, 0,
5084 PT, ht, 0,
5085 -1, XINT (w->hscroll),
5086 0, w);
5087 original_vpos = posit.vpos;
5089 XSETFASTINT (tem, PT);
5090 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5092 if (NILP (tem))
5094 Fvertical_motion (make_number (- (ht / 2)), window);
5095 startpos = PT;
5098 SET_PT (startpos);
5099 lose = n < 0 && PT == BEGV;
5100 Fvertical_motion (make_number (n), window);
5101 pos = PT;
5102 pos_byte = PT_BYTE;
5103 bolp = Fbolp ();
5104 SET_PT_BOTH (opoint, opoint_byte);
5106 if (lose)
5108 if (noerror)
5109 return;
5110 else
5111 Fsignal (Qbeginning_of_buffer, Qnil);
5114 if (pos < ZV)
5116 int this_scroll_margin = scroll_margin;
5118 /* Don't use a scroll margin that is negative or too large. */
5119 if (this_scroll_margin < 0)
5120 this_scroll_margin = 0;
5122 if (XINT (w->total_lines) < 4 * scroll_margin)
5123 this_scroll_margin = XINT (w->total_lines) / 4;
5125 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5126 w->start_at_line_beg = bolp;
5127 w->update_mode_line = Qt;
5128 XSETFASTINT (w->last_modified, 0);
5129 XSETFASTINT (w->last_overlay_modified, 0);
5130 /* Set force_start so that redisplay_window will run
5131 the window-scroll-functions. */
5132 w->force_start = Qt;
5134 if (!NILP (Vscroll_preserve_screen_position)
5135 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5137 SET_PT_BOTH (pos, pos_byte);
5138 Fvertical_motion (make_number (original_vpos), window);
5140 /* If we scrolled forward, put point enough lines down
5141 that it is outside the scroll margin. */
5142 else if (n > 0)
5144 int top_margin;
5146 if (this_scroll_margin > 0)
5148 SET_PT_BOTH (pos, pos_byte);
5149 Fvertical_motion (make_number (this_scroll_margin), window);
5150 top_margin = PT;
5152 else
5153 top_margin = pos;
5155 if (top_margin <= opoint)
5156 SET_PT_BOTH (opoint, opoint_byte);
5157 else if (!NILP (Vscroll_preserve_screen_position))
5159 SET_PT_BOTH (pos, pos_byte);
5160 Fvertical_motion (make_number (original_vpos), window);
5162 else
5163 SET_PT (top_margin);
5165 else if (n < 0)
5167 int bottom_margin;
5169 /* If we scrolled backward, put point near the end of the window
5170 but not within the scroll margin. */
5171 SET_PT_BOTH (pos, pos_byte);
5172 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5173 if (XFASTINT (tem) == ht - this_scroll_margin)
5174 bottom_margin = PT;
5175 else
5176 bottom_margin = PT + 1;
5178 if (bottom_margin > opoint)
5179 SET_PT_BOTH (opoint, opoint_byte);
5180 else
5182 if (!NILP (Vscroll_preserve_screen_position))
5184 SET_PT_BOTH (pos, pos_byte);
5185 Fvertical_motion (make_number (original_vpos), window);
5187 else
5188 Fvertical_motion (make_number (-1), window);
5192 else
5194 if (noerror)
5195 return;
5196 else
5197 Fsignal (Qend_of_buffer, Qnil);
5202 /* Scroll selected_window up or down. If N is nil, scroll a
5203 screen-full which is defined as the height of the window minus
5204 next_screen_context_lines. If N is the symbol `-', scroll.
5205 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5206 up. This is the guts of Fscroll_up and Fscroll_down. */
5208 static void
5209 scroll_command (n, direction)
5210 Lisp_Object n;
5211 int direction;
5213 int count = SPECPDL_INDEX ();
5215 xassert (abs (direction) == 1);
5217 /* If selected window's buffer isn't current, make it current for
5218 the moment. But don't screw up if window_scroll gets an error. */
5219 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5221 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5222 Fset_buffer (XWINDOW (selected_window)->buffer);
5224 /* Make redisplay consider other windows than just selected_window. */
5225 ++windows_or_buffers_changed;
5228 if (NILP (n))
5229 window_scroll (selected_window, direction, 1, 0);
5230 else if (EQ (n, Qminus))
5231 window_scroll (selected_window, -direction, 1, 0);
5232 else
5234 n = Fprefix_numeric_value (n);
5235 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5238 unbind_to (count, Qnil);
5241 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
5242 doc: /* Scroll text of current window upward ARG lines.
5243 If ARG is omitted or nil, scroll upward by a near full screen.
5244 A near full screen is `next-screen-context-lines' less than a full screen.
5245 Negative ARG means scroll downward.
5246 If ARG is the atom `-', scroll downward by nearly full screen.
5247 When calling from a program, supply as argument a number, nil, or `-'. */)
5248 (arg)
5249 Lisp_Object arg;
5251 scroll_command (arg, 1);
5252 return Qnil;
5255 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
5256 doc: /* Scroll text of current window down ARG lines.
5257 If ARG is omitted or nil, scroll down by a near full screen.
5258 A near full screen is `next-screen-context-lines' less than a full screen.
5259 Negative ARG means scroll upward.
5260 If ARG is the atom `-', scroll upward by nearly full screen.
5261 When calling from a program, supply as argument a number, nil, or `-'. */)
5262 (arg)
5263 Lisp_Object arg;
5265 scroll_command (arg, -1);
5266 return Qnil;
5269 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5270 doc: /* Return the other window for \"other window scroll\" commands.
5271 If `other-window-scroll-buffer' is non-nil, a window
5272 showing that buffer is used.
5273 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5274 specifies the window. This takes precedence over
5275 `other-window-scroll-buffer'. */)
5278 Lisp_Object window;
5280 if (MINI_WINDOW_P (XWINDOW (selected_window))
5281 && !NILP (Vminibuf_scroll_window))
5282 window = Vminibuf_scroll_window;
5283 /* If buffer is specified, scroll that buffer. */
5284 else if (!NILP (Vother_window_scroll_buffer))
5286 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5287 if (NILP (window))
5288 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5290 else
5292 /* Nothing specified; look for a neighboring window on the same
5293 frame. */
5294 window = Fnext_window (selected_window, Qnil, Qnil);
5296 if (EQ (window, selected_window))
5297 /* That didn't get us anywhere; look for a window on another
5298 visible frame. */
5300 window = Fnext_window (window, Qnil, Qt);
5301 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5302 && ! EQ (window, selected_window));
5305 CHECK_LIVE_WINDOW (window);
5307 if (EQ (window, selected_window))
5308 error ("There is no other window");
5310 return window;
5313 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5314 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5315 A near full screen is `next-screen-context-lines' less than a full screen.
5316 The next window is the one below the current one; or the one at the top
5317 if the current one is at the bottom. Negative ARG means scroll downward.
5318 If ARG is the atom `-', scroll downward by nearly full screen.
5319 When calling from a program, supply as argument a number, nil, or `-'.
5321 If `other-window-scroll-buffer' is non-nil, scroll the window
5322 showing that buffer, popping the buffer up if necessary.
5323 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5324 specifies the window to scroll. This takes precedence over
5325 `other-window-scroll-buffer'. */)
5326 (arg)
5327 Lisp_Object arg;
5329 Lisp_Object window;
5330 struct window *w;
5331 int count = SPECPDL_INDEX ();
5333 window = Fother_window_for_scrolling ();
5334 w = XWINDOW (window);
5336 /* Don't screw up if window_scroll gets an error. */
5337 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5338 ++windows_or_buffers_changed;
5340 Fset_buffer (w->buffer);
5341 SET_PT (marker_position (w->pointm));
5343 if (NILP (arg))
5344 window_scroll (window, 1, 1, 1);
5345 else if (EQ (arg, Qminus))
5346 window_scroll (window, -1, 1, 1);
5347 else
5349 if (CONSP (arg))
5350 arg = Fcar (arg);
5351 CHECK_NUMBER (arg);
5352 window_scroll (window, XINT (arg), 0, 1);
5355 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5356 unbind_to (count, Qnil);
5358 return Qnil;
5361 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "P\np",
5362 doc: /* Scroll selected window display ARG columns left.
5363 Default for ARG is window width minus 2.
5364 Value is the total amount of leftward horizontal scrolling in
5365 effect after the change.
5366 If SET_MINIMUM is non-nil, the new scroll amount becomes the
5367 lower bound for automatic scrolling, i.e. automatic scrolling
5368 will not scroll a window to a column less than the value returned
5369 by this function. This happens in an interactive call. */)
5370 (arg, set_minimum)
5371 register Lisp_Object arg, set_minimum;
5373 Lisp_Object result;
5374 int hscroll;
5375 struct window *w = XWINDOW (selected_window);
5377 if (NILP (arg))
5378 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5379 else
5380 arg = Fprefix_numeric_value (arg);
5382 hscroll = XINT (w->hscroll) + XINT (arg);
5383 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5385 if (!NILP (set_minimum))
5386 w->min_hscroll = w->hscroll;
5388 return result;
5391 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "P\np",
5392 doc: /* Scroll selected window display ARG columns right.
5393 Default for ARG is window width minus 2.
5394 Value is the total amount of leftward horizontal scrolling in
5395 effect after the change.
5396 If SET_MINIMUM is non-nil, the new scroll amount becomes the
5397 lower bound for automatic scrolling, i.e. automatic scrolling
5398 will not scroll a window to a column less than the value returned
5399 by this function. This happens in an interactive call. */)
5400 (arg, set_minimum)
5401 register Lisp_Object arg, set_minimum;
5403 Lisp_Object result;
5404 int hscroll;
5405 struct window *w = XWINDOW (selected_window);
5407 if (NILP (arg))
5408 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5409 else
5410 arg = Fprefix_numeric_value (arg);
5412 hscroll = XINT (w->hscroll) - XINT (arg);
5413 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5415 if (!NILP (set_minimum))
5416 w->min_hscroll = w->hscroll;
5418 return result;
5421 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5422 doc: /* Return the window which was selected when entering the minibuffer.
5423 Returns nil, if current window is not a minibuffer window. */)
5426 if (minibuf_level > 0
5427 && MINI_WINDOW_P (XWINDOW (selected_window))
5428 && WINDOW_LIVE_P (minibuf_selected_window))
5429 return minibuf_selected_window;
5431 return Qnil;
5434 /* Value is the number of lines actually displayed in window W,
5435 as opposed to its height. */
5437 static int
5438 displayed_window_lines (w)
5439 struct window *w;
5441 struct it it;
5442 struct text_pos start;
5443 int height = window_box_height (w);
5444 struct buffer *old_buffer;
5445 int bottom_y;
5447 if (XBUFFER (w->buffer) != current_buffer)
5449 old_buffer = current_buffer;
5450 set_buffer_internal (XBUFFER (w->buffer));
5452 else
5453 old_buffer = NULL;
5455 /* In case W->start is out of the accessible range, do something
5456 reasonable. This happens in Info mode when Info-scroll-down
5457 calls (recenter -1) while W->start is 1. */
5458 if (XMARKER (w->start)->charpos < BEGV)
5459 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5460 else if (XMARKER (w->start)->charpos > ZV)
5461 SET_TEXT_POS (start, ZV, ZV_BYTE);
5462 else
5463 SET_TEXT_POS_FROM_MARKER (start, w->start);
5465 start_display (&it, w, start);
5466 move_it_vertically (&it, height);
5467 bottom_y = line_bottom_y (&it);
5469 /* rms: On a non-window display,
5470 the value of it.vpos at the bottom of the screen
5471 seems to be 1 larger than window_box_height (w).
5472 This kludge fixes a bug whereby (move-to-window-line -1)
5473 when ZV is on the last screen line
5474 moves to the previous screen line instead of the last one. */
5475 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5476 height++;
5478 /* Add in empty lines at the bottom of the window. */
5479 if (bottom_y < height)
5481 int uy = FRAME_LINE_HEIGHT (it.f);
5482 it.vpos += (height - bottom_y + uy - 1) / uy;
5485 if (old_buffer)
5486 set_buffer_internal (old_buffer);
5488 return it.vpos;
5492 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5493 doc: /* Center point in window and redisplay frame.
5494 With prefix argument ARG, recenter putting point on screen line ARG
5495 relative to the current window. If ARG is negative, it counts up from the
5496 bottom of the window. (ARG should be less than the height of the window.)
5498 If ARG is omitted or nil, erase the entire frame and then
5499 redraw with point in the center of the current window.
5500 Just C-u as prefix means put point in the center of the window
5501 and redisplay normally--don't erase and redraw the frame. */)
5502 (arg)
5503 register Lisp_Object arg;
5505 struct window *w = XWINDOW (selected_window);
5506 struct buffer *buf = XBUFFER (w->buffer);
5507 struct buffer *obuf = current_buffer;
5508 int center_p = 0;
5509 int charpos, bytepos;
5510 int iarg;
5511 int this_scroll_margin;
5513 /* If redisplay is suppressed due to an error, try again. */
5514 obuf->display_error_modiff = 0;
5516 if (NILP (arg))
5518 int i;
5520 /* Invalidate pixel data calculated for all compositions. */
5521 for (i = 0; i < n_compositions; i++)
5522 composition_table[i]->font = NULL;
5524 Fredraw_frame (w->frame);
5525 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
5526 center_p = 1;
5528 else if (CONSP (arg)) /* Just C-u. */
5529 center_p = 1;
5530 else
5532 arg = Fprefix_numeric_value (arg);
5533 CHECK_NUMBER (arg);
5534 iarg = XINT (arg);
5537 set_buffer_internal (buf);
5539 /* Do this after making BUF current
5540 in case scroll_margin is buffer-local. */
5541 this_scroll_margin = max (0, scroll_margin);
5542 this_scroll_margin = min (this_scroll_margin,
5543 XFASTINT (w->total_lines) / 4);
5545 /* Handle centering on a graphical frame specially. Such frames can
5546 have variable-height lines and centering point on the basis of
5547 line counts would lead to strange effects. */
5548 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5550 if (center_p)
5552 struct it it;
5553 struct text_pos pt;
5555 SET_TEXT_POS (pt, PT, PT_BYTE);
5556 start_display (&it, w, pt);
5557 move_it_vertically_backward (&it, window_box_height (w) / 2);
5558 charpos = IT_CHARPOS (it);
5559 bytepos = IT_BYTEPOS (it);
5561 else if (iarg < 0)
5563 struct it it;
5564 struct text_pos pt;
5565 int nlines = -iarg;
5566 int extra_line_spacing;
5567 int h = window_box_height (w);
5569 iarg = - max (-iarg, this_scroll_margin);
5571 SET_TEXT_POS (pt, PT, PT_BYTE);
5572 start_display (&it, w, pt);
5574 /* Be sure we have the exact height of the full line containing PT. */
5575 move_it_by_lines (&it, 0, 1);
5577 /* The amount of pixels we have to move back is the window
5578 height minus what's displayed in the line containing PT,
5579 and the lines below. */
5580 it.current_y = 0;
5581 it.vpos = 0;
5582 move_it_by_lines (&it, nlines, 1);
5584 if (it.vpos == nlines)
5585 h -= it.current_y;
5586 else
5588 /* Last line has no newline */
5589 h -= line_bottom_y (&it);
5590 it.vpos++;
5593 /* Don't reserve space for extra line spacing of last line. */
5594 extra_line_spacing = it.max_extra_line_spacing;
5596 /* If we can't move down NLINES lines because we hit
5597 the end of the buffer, count in some empty lines. */
5598 if (it.vpos < nlines)
5600 nlines -= it.vpos;
5601 extra_line_spacing = it.extra_line_spacing;
5602 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5604 if (h <= 0)
5605 return Qnil;
5607 /* Now find the new top line (starting position) of the window. */
5608 start_display (&it, w, pt);
5609 it.current_y = 0;
5610 move_it_vertically_backward (&it, h);
5612 /* If extra line spacing is present, we may move too far
5613 back. This causes the last line to be only partially
5614 visible (which triggers redisplay to recenter that line
5615 in the middle), so move forward.
5616 But ignore extra line spacing on last line, as it is not
5617 considered to be part of the visible height of the line.
5619 h += extra_line_spacing;
5620 while (-it.current_y > h)
5621 move_it_by_lines (&it, 1, 1);
5623 charpos = IT_CHARPOS (it);
5624 bytepos = IT_BYTEPOS (it);
5626 else
5628 struct position pos;
5630 iarg = max (iarg, this_scroll_margin);
5632 pos = *vmotion (PT, -iarg, w);
5633 charpos = pos.bufpos;
5634 bytepos = pos.bytepos;
5637 else
5639 struct position pos;
5640 int ht = window_internal_height (w);
5642 if (center_p)
5643 iarg = ht / 2;
5644 else if (iarg < 0)
5645 iarg += ht;
5647 /* Don't let it get into the margin at either top or bottom. */
5648 iarg = max (iarg, this_scroll_margin);
5649 iarg = min (iarg, ht - this_scroll_margin - 1);
5651 pos = *vmotion (PT, - iarg, w);
5652 charpos = pos.bufpos;
5653 bytepos = pos.bytepos;
5656 /* Set the new window start. */
5657 set_marker_both (w->start, w->buffer, charpos, bytepos);
5658 w->window_end_valid = Qnil;
5660 w->optional_new_start = Qt;
5662 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5663 w->start_at_line_beg = Qt;
5664 else
5665 w->start_at_line_beg = Qnil;
5667 set_buffer_internal (obuf);
5668 return Qnil;
5672 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5673 0, 1, 0,
5674 doc: /* Return the height in lines of the text display area of WINDOW.
5675 This doesn't include the mode-line (or header-line if any) or any
5676 partial-height lines in the text display area. */)
5677 (window)
5678 Lisp_Object window;
5680 struct window *w = decode_window (window);
5681 int pixel_height = window_box_height (w);
5682 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5683 return make_number (line_height);
5688 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5689 1, 1, "P",
5690 doc: /* Position point relative to window.
5691 With no argument, position point at center of window.
5692 An argument specifies vertical position within the window;
5693 zero means top of window, negative means relative to bottom of window. */)
5694 (arg)
5695 Lisp_Object arg;
5697 struct window *w = XWINDOW (selected_window);
5698 int lines, start;
5699 Lisp_Object window;
5700 #if 0
5701 int this_scroll_margin;
5702 #endif
5704 window = selected_window;
5705 start = marker_position (w->start);
5706 if (start < BEGV || start > ZV)
5708 int height = window_internal_height (w);
5709 Fvertical_motion (make_number (- (height / 2)), window);
5710 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5711 w->start_at_line_beg = Fbolp ();
5712 w->force_start = Qt;
5714 else
5715 Fgoto_char (w->start);
5717 lines = displayed_window_lines (w);
5719 #if 0
5720 this_scroll_margin = max (0, scroll_margin);
5721 this_scroll_margin = min (this_scroll_margin, lines / 4);
5722 #endif
5724 if (NILP (arg))
5725 XSETFASTINT (arg, lines / 2);
5726 else
5728 int iarg = XINT (Fprefix_numeric_value (arg));
5730 if (iarg < 0)
5731 iarg = iarg + lines;
5733 #if 0 /* This code would prevent move-to-window-line from moving point
5734 to a place inside the scroll margins (which would cause the
5735 next redisplay to scroll). I wrote this code, but then concluded
5736 it is probably better not to install it. However, it is here
5737 inside #if 0 so as not to lose it. -- rms. */
5739 /* Don't let it get into the margin at either top or bottom. */
5740 iarg = max (iarg, this_scroll_margin);
5741 iarg = min (iarg, lines - this_scroll_margin - 1);
5742 #endif
5744 arg = make_number (iarg);
5747 /* Skip past a partially visible first line. */
5748 if (w->vscroll)
5749 XSETINT (arg, XINT (arg) + 1);
5751 return Fvertical_motion (arg, window);
5756 /***********************************************************************
5757 Window Configuration
5758 ***********************************************************************/
5760 struct save_window_data
5762 EMACS_INT size_from_Lisp_Vector_struct;
5763 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5764 Lisp_Object frame_cols, frame_lines, frame_menu_bar_lines;
5765 Lisp_Object frame_tool_bar_lines;
5766 Lisp_Object selected_frame;
5767 Lisp_Object current_window;
5768 Lisp_Object current_buffer;
5769 Lisp_Object minibuf_scroll_window;
5770 Lisp_Object minibuf_selected_window;
5771 Lisp_Object root_window;
5772 Lisp_Object focus_frame;
5773 /* Record the values of window-min-width and window-min-height
5774 so that window sizes remain consistent with them. */
5775 Lisp_Object min_width, min_height;
5776 /* A vector, each of whose elements is a struct saved_window
5777 for one window. */
5778 Lisp_Object saved_windows;
5781 /* This is saved as a Lisp_Vector */
5782 struct saved_window
5784 /* these first two must agree with struct Lisp_Vector in lisp.h */
5785 EMACS_INT size_from_Lisp_Vector_struct;
5786 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5788 Lisp_Object window;
5789 Lisp_Object buffer, start, pointm, mark;
5790 Lisp_Object left_col, top_line, total_cols, total_lines;
5791 Lisp_Object hscroll, min_hscroll;
5792 Lisp_Object parent, prev;
5793 Lisp_Object start_at_line_beg;
5794 Lisp_Object display_table;
5795 Lisp_Object orig_top_line, orig_total_lines;
5796 Lisp_Object left_margin_cols, right_margin_cols;
5797 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5798 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5801 #define SAVED_WINDOW_N(swv,n) \
5802 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5804 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5805 doc: /* Return t if OBJECT is a window-configuration object. */)
5806 (object)
5807 Lisp_Object object;
5809 if (WINDOW_CONFIGURATIONP (object))
5810 return Qt;
5811 return Qnil;
5814 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5815 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5816 (config)
5817 Lisp_Object config;
5819 register struct save_window_data *data;
5820 struct Lisp_Vector *saved_windows;
5822 if (! WINDOW_CONFIGURATIONP (config))
5823 wrong_type_argument (Qwindow_configuration_p, config);
5825 data = (struct save_window_data *) XVECTOR (config);
5826 saved_windows = XVECTOR (data->saved_windows);
5827 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5830 DEFUN ("set-window-configuration", Fset_window_configuration,
5831 Sset_window_configuration, 1, 1, 0,
5832 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5833 CONFIGURATION must be a value previously returned
5834 by `current-window-configuration' (which see).
5835 If CONFIGURATION was made from a frame that is now deleted,
5836 only frame-independent values can be restored. In this case,
5837 the return value is nil. Otherwise the value is t. */)
5838 (configuration)
5839 Lisp_Object configuration;
5841 register struct save_window_data *data;
5842 struct Lisp_Vector *saved_windows;
5843 Lisp_Object new_current_buffer;
5844 Lisp_Object frame;
5845 FRAME_PTR f;
5846 int old_point = -1;
5848 while (!WINDOW_CONFIGURATIONP (configuration))
5849 wrong_type_argument (Qwindow_configuration_p, configuration);
5851 data = (struct save_window_data *) XVECTOR (configuration);
5852 saved_windows = XVECTOR (data->saved_windows);
5854 new_current_buffer = data->current_buffer;
5855 if (NILP (XBUFFER (new_current_buffer)->name))
5856 new_current_buffer = Qnil;
5857 else
5859 if (XBUFFER (new_current_buffer) == current_buffer)
5860 /* The code further down "preserves point" by saving here PT in
5861 old_point and then setting it later back into PT. When the
5862 current-selected-window and the final-selected-window both show
5863 the current buffer, this suffers from the problem that the
5864 current PT is the window-point of the current-selected-window,
5865 while the final PT is the point of the final-selected-window, so
5866 this copy from one PT to the other would end up moving the
5867 window-point of the final-selected-window to the window-point of
5868 the current-selected-window. So we have to be careful which
5869 point of the current-buffer we copy into old_point. */
5870 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5871 && WINDOWP (selected_window)
5872 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5873 && !EQ (selected_window, data->current_window))
5874 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5875 else
5876 old_point = PT;
5877 else
5878 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5879 point in new_current_buffer as of the last time this buffer was
5880 used. This can be non-deterministic since it can be changed by
5881 things like jit-lock by mere temporary selection of some random
5882 window that happens to show this buffer.
5883 So if possible we want this arbitrary choice of "which point" to
5884 be the one from the to-be-selected-window so as to prevent this
5885 window's cursor from being copied from another window. */
5886 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5887 /* If current_window = selected_window, its point is in BUF_PT. */
5888 && !EQ (selected_window, data->current_window))
5889 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5890 else
5891 old_point = BUF_PT (XBUFFER (new_current_buffer));
5894 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5895 f = XFRAME (frame);
5897 /* If f is a dead frame, don't bother rebuilding its window tree.
5898 However, there is other stuff we should still try to do below. */
5899 if (FRAME_LIVE_P (f))
5901 register struct window *w;
5902 register struct saved_window *p;
5903 struct window *root_window;
5904 struct window **leaf_windows;
5905 int n_leaf_windows;
5906 int k, i, n;
5908 /* If the frame has been resized since this window configuration was
5909 made, we change the frame to the size specified in the
5910 configuration, restore the configuration, and then resize it
5911 back. We keep track of the prevailing height in these variables. */
5912 int previous_frame_lines = FRAME_LINES (f);
5913 int previous_frame_cols = FRAME_COLS (f);
5914 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5915 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5917 /* The mouse highlighting code could get screwed up
5918 if it runs during this. */
5919 BLOCK_INPUT;
5921 if (XFASTINT (data->frame_lines) != previous_frame_lines
5922 || XFASTINT (data->frame_cols) != previous_frame_cols)
5923 change_frame_size (f, XFASTINT (data->frame_lines),
5924 XFASTINT (data->frame_cols), 0, 0, 0);
5925 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5926 if (XFASTINT (data->frame_menu_bar_lines)
5927 != previous_frame_menu_bar_lines)
5928 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
5929 #ifdef HAVE_WINDOW_SYSTEM
5930 if (XFASTINT (data->frame_tool_bar_lines)
5931 != previous_frame_tool_bar_lines)
5932 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
5933 #endif
5934 #endif
5936 /* "Swap out" point from the selected window's buffer
5937 into the window itself. (Normally the pointm of the selected
5938 window holds garbage.) We do this now, before
5939 restoring the window contents, and prevent it from
5940 being done later on when we select a new window. */
5941 if (! NILP (XWINDOW (selected_window)->buffer))
5943 w = XWINDOW (selected_window);
5944 set_marker_both (w->pointm,
5945 w->buffer,
5946 BUF_PT (XBUFFER (w->buffer)),
5947 BUF_PT_BYTE (XBUFFER (w->buffer)));
5950 windows_or_buffers_changed++;
5951 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5953 /* Problem: Freeing all matrices and later allocating them again
5954 is a serious redisplay flickering problem. What we would
5955 really like to do is to free only those matrices not reused
5956 below. */
5957 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5958 leaf_windows
5959 = (struct window **) alloca (count_windows (root_window)
5960 * sizeof (struct window *));
5961 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5963 /* Temporarily avoid any problems with windows that are smaller
5964 than they are supposed to be. */
5965 window_min_height = 1;
5966 window_min_width = 1;
5968 /* Kludge Alert!
5969 Mark all windows now on frame as "deleted".
5970 Restoring the new configuration "undeletes" any that are in it.
5972 Save their current buffers in their height fields, since we may
5973 need it later, if a buffer saved in the configuration is now
5974 dead. */
5975 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5977 for (k = 0; k < saved_windows->size; k++)
5979 p = SAVED_WINDOW_N (saved_windows, k);
5980 w = XWINDOW (p->window);
5981 w->next = Qnil;
5983 if (!NILP (p->parent))
5984 w->parent = SAVED_WINDOW_N (saved_windows,
5985 XFASTINT (p->parent))->window;
5986 else
5987 w->parent = Qnil;
5989 if (!NILP (p->prev))
5991 w->prev = SAVED_WINDOW_N (saved_windows,
5992 XFASTINT (p->prev))->window;
5993 XWINDOW (w->prev)->next = p->window;
5995 else
5997 w->prev = Qnil;
5998 if (!NILP (w->parent))
6000 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6002 XWINDOW (w->parent)->vchild = p->window;
6003 XWINDOW (w->parent)->hchild = Qnil;
6005 else
6007 XWINDOW (w->parent)->hchild = p->window;
6008 XWINDOW (w->parent)->vchild = Qnil;
6013 /* If we squirreled away the buffer in the window's height,
6014 restore it now. */
6015 if (BUFFERP (w->total_lines))
6016 w->buffer = w->total_lines;
6017 w->left_col = p->left_col;
6018 w->top_line = p->top_line;
6019 w->total_cols = p->total_cols;
6020 w->total_lines = p->total_lines;
6021 w->hscroll = p->hscroll;
6022 w->min_hscroll = p->min_hscroll;
6023 w->display_table = p->display_table;
6024 w->orig_top_line = p->orig_top_line;
6025 w->orig_total_lines = p->orig_total_lines;
6026 w->left_margin_cols = p->left_margin_cols;
6027 w->right_margin_cols = p->right_margin_cols;
6028 w->left_fringe_width = p->left_fringe_width;
6029 w->right_fringe_width = p->right_fringe_width;
6030 w->fringes_outside_margins = p->fringes_outside_margins;
6031 w->scroll_bar_width = p->scroll_bar_width;
6032 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6033 XSETFASTINT (w->last_modified, 0);
6034 XSETFASTINT (w->last_overlay_modified, 0);
6036 /* Reinstall the saved buffer and pointers into it. */
6037 if (NILP (p->buffer))
6038 w->buffer = p->buffer;
6039 else
6041 if (!NILP (XBUFFER (p->buffer)->name))
6042 /* If saved buffer is alive, install it. */
6044 w->buffer = p->buffer;
6045 w->start_at_line_beg = p->start_at_line_beg;
6046 set_marker_restricted (w->start, p->start, w->buffer);
6047 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6048 Fset_marker (XBUFFER (w->buffer)->mark,
6049 p->mark, w->buffer);
6051 /* As documented in Fcurrent_window_configuration, don't
6052 restore the location of point in the buffer which was
6053 current when the window configuration was recorded. */
6054 if (!EQ (p->buffer, new_current_buffer)
6055 && XBUFFER (p->buffer) == current_buffer)
6056 Fgoto_char (w->pointm);
6058 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
6059 /* Else unless window has a live buffer, get one. */
6061 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6062 /* This will set the markers to beginning of visible
6063 range. */
6064 set_marker_restricted (w->start, make_number (0), w->buffer);
6065 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6066 w->start_at_line_beg = Qt;
6068 else
6069 /* Keeping window's old buffer; make sure the markers
6070 are real. */
6072 /* Set window markers at start of visible range. */
6073 if (XMARKER (w->start)->buffer == 0)
6074 set_marker_restricted (w->start, make_number (0),
6075 w->buffer);
6076 if (XMARKER (w->pointm)->buffer == 0)
6077 set_marker_restricted_both (w->pointm, w->buffer,
6078 BUF_PT (XBUFFER (w->buffer)),
6079 BUF_PT_BYTE (XBUFFER (w->buffer)));
6080 w->start_at_line_beg = Qt;
6085 FRAME_ROOT_WINDOW (f) = data->root_window;
6086 /* Prevent "swapping out point" in the old selected window
6087 using the buffer that has been restored into it.
6088 We already swapped out point that from that window's old buffer. */
6089 selected_window = Qnil;
6091 /* Arrange *not* to restore point in the buffer that was
6092 current when the window configuration was saved. */
6093 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6094 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6095 make_number (old_point),
6096 XWINDOW (data->current_window)->buffer);
6098 Fselect_window (data->current_window, Qnil);
6099 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
6100 = selected_window;
6102 if (NILP (data->focus_frame)
6103 || (FRAMEP (data->focus_frame)
6104 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6105 Fredirect_frame_focus (frame, data->focus_frame);
6107 #if 0 /* I don't understand why this is needed, and it causes problems
6108 when the frame's old selected window has been deleted. */
6109 if (f != selected_frame && FRAME_WINDOW_P (f))
6110 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
6111 0, 0);
6112 #endif
6114 /* Set the screen height to the value it had before this function. */
6115 if (previous_frame_lines != FRAME_LINES (f)
6116 || previous_frame_cols != FRAME_COLS (f))
6117 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6118 0, 0, 0);
6119 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6120 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6121 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6122 make_number (0));
6123 #ifdef HAVE_WINDOW_SYSTEM
6124 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6125 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6126 make_number (0));
6127 #endif
6128 #endif
6130 /* Now, free glyph matrices in windows that were not reused. */
6131 for (i = n = 0; i < n_leaf_windows; ++i)
6133 if (NILP (leaf_windows[i]->buffer))
6135 /* Assert it's not reused as a combination. */
6136 xassert (NILP (leaf_windows[i]->hchild)
6137 && NILP (leaf_windows[i]->vchild));
6138 free_window_matrices (leaf_windows[i]);
6140 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6141 ++n;
6144 adjust_glyphs (f);
6146 UNBLOCK_INPUT;
6148 /* Fselect_window will have made f the selected frame, so we
6149 reselect the proper frame here. Fhandle_switch_frame will change the
6150 selected window too, but that doesn't make the call to
6151 Fselect_window above totally superfluous; it still sets f's
6152 selected window. */
6153 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6154 do_switch_frame (data->selected_frame, 0, 0);
6156 if (! NILP (Vwindow_configuration_change_hook)
6157 && ! NILP (Vrun_hooks))
6158 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
6161 if (!NILP (new_current_buffer))
6162 Fset_buffer (new_current_buffer);
6164 /* Restore the minimum heights recorded in the configuration. */
6165 window_min_height = XINT (data->min_height);
6166 window_min_width = XINT (data->min_width);
6168 Vminibuf_scroll_window = data->minibuf_scroll_window;
6169 minibuf_selected_window = data->minibuf_selected_window;
6171 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6174 /* Mark all windows now on frame as deleted
6175 by setting their buffers to nil. */
6177 void
6178 delete_all_subwindows (w)
6179 register struct window *w;
6181 if (!NILP (w->next))
6182 delete_all_subwindows (XWINDOW (w->next));
6183 if (!NILP (w->vchild))
6184 delete_all_subwindows (XWINDOW (w->vchild));
6185 if (!NILP (w->hchild))
6186 delete_all_subwindows (XWINDOW (w->hchild));
6188 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6190 if (!NILP (w->buffer))
6191 unshow_buffer (w);
6193 /* We set all three of these fields to nil, to make sure that we can
6194 distinguish this dead window from any live window. Live leaf
6195 windows will have buffer set, and combination windows will have
6196 vchild or hchild set. */
6197 w->buffer = Qnil;
6198 w->vchild = Qnil;
6199 w->hchild = Qnil;
6201 Vwindow_list = Qnil;
6204 static int
6205 count_windows (window)
6206 register struct window *window;
6208 register int count = 1;
6209 if (!NILP (window->next))
6210 count += count_windows (XWINDOW (window->next));
6211 if (!NILP (window->vchild))
6212 count += count_windows (XWINDOW (window->vchild));
6213 if (!NILP (window->hchild))
6214 count += count_windows (XWINDOW (window->hchild));
6215 return count;
6219 /* Fill vector FLAT with leaf windows under W, starting at index I.
6220 Value is last index + 1. */
6222 static int
6223 get_leaf_windows (w, flat, i)
6224 struct window *w;
6225 struct window **flat;
6226 int i;
6228 while (w)
6230 if (!NILP (w->hchild))
6231 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6232 else if (!NILP (w->vchild))
6233 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6234 else
6235 flat[i++] = w;
6237 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6240 return i;
6244 /* Return a pointer to the glyph W's physical cursor is on. Value is
6245 null if W's current matrix is invalid, so that no meaningfull glyph
6246 can be returned. */
6248 struct glyph *
6249 get_phys_cursor_glyph (w)
6250 struct window *w;
6252 struct glyph_row *row;
6253 struct glyph *glyph;
6255 if (w->phys_cursor.vpos >= 0
6256 && w->phys_cursor.vpos < w->current_matrix->nrows
6257 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6258 row->enabled_p)
6259 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6260 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6261 else
6262 glyph = NULL;
6264 return glyph;
6268 static int
6269 save_window_save (window, vector, i)
6270 Lisp_Object window;
6271 struct Lisp_Vector *vector;
6272 int i;
6274 register struct saved_window *p;
6275 register struct window *w;
6276 register Lisp_Object tem;
6278 for (;!NILP (window); window = w->next)
6280 p = SAVED_WINDOW_N (vector, i);
6281 w = XWINDOW (window);
6283 XSETFASTINT (w->temslot, i); i++;
6284 p->window = window;
6285 p->buffer = w->buffer;
6286 p->left_col = w->left_col;
6287 p->top_line = w->top_line;
6288 p->total_cols = w->total_cols;
6289 p->total_lines = w->total_lines;
6290 p->hscroll = w->hscroll;
6291 p->min_hscroll = w->min_hscroll;
6292 p->display_table = w->display_table;
6293 p->orig_top_line = w->orig_top_line;
6294 p->orig_total_lines = w->orig_total_lines;
6295 p->left_margin_cols = w->left_margin_cols;
6296 p->right_margin_cols = w->right_margin_cols;
6297 p->left_fringe_width = w->left_fringe_width;
6298 p->right_fringe_width = w->right_fringe_width;
6299 p->fringes_outside_margins = w->fringes_outside_margins;
6300 p->scroll_bar_width = w->scroll_bar_width;
6301 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6302 if (!NILP (w->buffer))
6304 /* Save w's value of point in the window configuration.
6305 If w is the selected window, then get the value of point
6306 from the buffer; pointm is garbage in the selected window. */
6307 if (EQ (window, selected_window))
6309 p->pointm = Fmake_marker ();
6310 set_marker_both (p->pointm, w->buffer,
6311 BUF_PT (XBUFFER (w->buffer)),
6312 BUF_PT_BYTE (XBUFFER (w->buffer)));
6314 else
6315 p->pointm = Fcopy_marker (w->pointm, Qnil);
6317 p->start = Fcopy_marker (w->start, Qnil);
6318 p->start_at_line_beg = w->start_at_line_beg;
6320 tem = XBUFFER (w->buffer)->mark;
6321 p->mark = Fcopy_marker (tem, Qnil);
6323 else
6325 p->pointm = Qnil;
6326 p->start = Qnil;
6327 p->mark = Qnil;
6328 p->start_at_line_beg = Qnil;
6331 if (NILP (w->parent))
6332 p->parent = Qnil;
6333 else
6334 p->parent = XWINDOW (w->parent)->temslot;
6336 if (NILP (w->prev))
6337 p->prev = Qnil;
6338 else
6339 p->prev = XWINDOW (w->prev)->temslot;
6341 if (!NILP (w->vchild))
6342 i = save_window_save (w->vchild, vector, i);
6343 if (!NILP (w->hchild))
6344 i = save_window_save (w->hchild, vector, i);
6347 return i;
6350 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6351 Scurrent_window_configuration, 0, 1, 0,
6352 doc: /* Return an object representing the current window configuration of FRAME.
6353 If FRAME is nil or omitted, use the selected frame.
6354 This describes the number of windows, their sizes and current buffers,
6355 and for each displayed buffer, where display starts, and the positions of
6356 point and mark. An exception is made for point in the current buffer:
6357 its value is -not- saved.
6358 This also records the currently selected frame, and FRAME's focus
6359 redirection (see `redirect-frame-focus'). */)
6360 (frame)
6361 Lisp_Object frame;
6363 register Lisp_Object tem;
6364 register int n_windows;
6365 register struct save_window_data *data;
6366 register struct Lisp_Vector *vec;
6367 register int i;
6368 FRAME_PTR f;
6370 if (NILP (frame))
6371 frame = selected_frame;
6372 CHECK_LIVE_FRAME (frame);
6373 f = XFRAME (frame);
6375 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6376 vec = allocate_other_vector (VECSIZE (struct save_window_data));
6377 data = (struct save_window_data *)vec;
6379 XSETFASTINT (data->frame_cols, FRAME_COLS (f));
6380 XSETFASTINT (data->frame_lines, FRAME_LINES (f));
6381 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
6382 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
6383 data->selected_frame = selected_frame;
6384 data->current_window = FRAME_SELECTED_WINDOW (f);
6385 XSETBUFFER (data->current_buffer, current_buffer);
6386 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6387 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6388 data->root_window = FRAME_ROOT_WINDOW (f);
6389 data->focus_frame = FRAME_FOCUS_FRAME (f);
6390 XSETINT (data->min_height, window_min_height);
6391 XSETINT (data->min_width, window_min_width);
6392 tem = Fmake_vector (make_number (n_windows), Qnil);
6393 data->saved_windows = tem;
6394 for (i = 0; i < n_windows; i++)
6395 XVECTOR (tem)->contents[i]
6396 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6397 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6398 XSETWINDOW_CONFIGURATION (tem, data);
6399 return (tem);
6402 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
6403 0, UNEVALLED, 0,
6404 doc: /* Execute BODY, preserving window sizes and contents.
6405 Return the value of the last form in BODY.
6406 Restore which buffer appears in which window, where display starts,
6407 and the value of point and mark for each window.
6408 Also restore the choice of selected window.
6409 Also restore which buffer is current.
6410 Does not restore the value of point in current buffer.
6411 usage: (save-window-excursion BODY ...) */)
6412 (args)
6413 Lisp_Object args;
6415 register Lisp_Object val;
6416 register int count = SPECPDL_INDEX ();
6418 record_unwind_protect (Fset_window_configuration,
6419 Fcurrent_window_configuration (Qnil));
6420 val = Fprogn (args);
6421 return unbind_to (count, val);
6426 /***********************************************************************
6427 Window Split Tree
6428 ***********************************************************************/
6430 static Lisp_Object
6431 window_tree (w)
6432 struct window *w;
6434 Lisp_Object tail = Qnil;
6435 Lisp_Object result = Qnil;
6437 while (w)
6439 Lisp_Object wn;
6441 XSETWINDOW (wn, w);
6442 if (!NILP (w->hchild))
6443 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6444 window_tree (XWINDOW (w->hchild))));
6445 else if (!NILP (w->vchild))
6446 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6447 window_tree (XWINDOW (w->vchild))));
6449 if (NILP (result))
6451 result = tail = Fcons (wn, Qnil);
6453 else
6455 XSETCDR (tail, Fcons (wn, Qnil));
6456 tail = XCDR (tail);
6459 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6462 return result;
6467 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6468 0, 1, 0,
6469 doc: /* Return the window tree for frame FRAME.
6471 The return value is a list of the form (ROOT MINI), where ROOT
6472 represents the window tree of the frame's root window, and MINI
6473 is the frame's minibuffer window.
6475 If the root window is not split, ROOT is the root window itself.
6476 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6477 horizontal split, and t for a vertical split, EDGES gives the combined
6478 size and position of the subwindows in the split, and the rest of the
6479 elements are the subwindows in the split. Each of the subwindows may
6480 again be a window or a list representing a window split, and so on.
6481 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6483 If FRAME is nil or omitted, return information on the currently
6484 selected frame. */)
6485 (frame)
6486 Lisp_Object frame;
6488 FRAME_PTR f;
6490 if (NILP (frame))
6491 frame = selected_frame;
6493 CHECK_FRAME (frame);
6494 f = XFRAME (frame);
6496 if (!FRAME_LIVE_P (f))
6497 return Qnil;
6499 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6503 /***********************************************************************
6504 Marginal Areas
6505 ***********************************************************************/
6507 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6508 2, 3, 0,
6509 doc: /* Set width of marginal areas of window WINDOW.
6510 If WINDOW is nil, set margins of the currently selected window.
6511 Second arg LEFT-WIDTH specifies the number of character cells to
6512 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6513 does the same for the right marginal area. A nil width parameter
6514 means no margin. */)
6515 (window, left_width, right_width)
6516 Lisp_Object window, left_width, right_width;
6518 struct window *w = decode_window (window);
6520 /* Translate negative or zero widths to nil.
6521 Margins that are too wide have to be checked elsewhere. */
6523 if (!NILP (left_width))
6525 CHECK_NUMBER (left_width);
6526 if (XINT (left_width) <= 0)
6527 left_width = Qnil;
6530 if (!NILP (right_width))
6532 CHECK_NUMBER (right_width);
6533 if (XINT (right_width) <= 0)
6534 right_width = Qnil;
6537 if (!EQ (w->left_margin_cols, left_width)
6538 || !EQ (w->right_margin_cols, right_width))
6540 w->left_margin_cols = left_width;
6541 w->right_margin_cols = right_width;
6543 adjust_window_margins (w);
6545 ++windows_or_buffers_changed;
6546 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6549 return Qnil;
6553 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6554 0, 1, 0,
6555 doc: /* Get width of marginal areas of window WINDOW.
6556 If WINDOW is omitted or nil, use the currently selected window.
6557 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6558 If a marginal area does not exist, its width will be returned
6559 as nil. */)
6560 (window)
6561 Lisp_Object window;
6563 struct window *w = decode_window (window);
6564 return Fcons (w->left_margin_cols, w->right_margin_cols);
6569 /***********************************************************************
6570 Fringes
6571 ***********************************************************************/
6573 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6574 2, 4, 0,
6575 doc: /* Set the fringe widths of window WINDOW.
6576 If WINDOW is nil, set the fringe widths of the currently selected
6577 window.
6578 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6579 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6580 fringe width. If a fringe width arg is nil, that means to use the
6581 frame's default fringe width. Default fringe widths can be set with
6582 the command `set-fringe-style'.
6583 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6584 outside of the display margins. By default, fringes are drawn between
6585 display marginal areas and the text area. */)
6586 (window, left_width, right_width, outside_margins)
6587 Lisp_Object window, left_width, right_width, outside_margins;
6589 struct window *w = decode_window (window);
6591 if (!NILP (left_width))
6592 CHECK_NATNUM (left_width);
6593 if (!NILP (right_width))
6594 CHECK_NATNUM (right_width);
6596 if (!EQ (w->left_fringe_width, left_width)
6597 || !EQ (w->right_fringe_width, right_width)
6598 || !EQ (w->fringes_outside_margins, outside_margins))
6600 w->left_fringe_width = left_width;
6601 w->right_fringe_width = right_width;
6602 w->fringes_outside_margins = outside_margins;
6604 adjust_window_margins (w);
6606 clear_glyph_matrix (w->current_matrix);
6607 w->window_end_valid = Qnil;
6609 ++windows_or_buffers_changed;
6610 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6613 return Qnil;
6617 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6618 0, 1, 0,
6619 doc: /* Get width of fringes of window WINDOW.
6620 If WINDOW is omitted or nil, use the currently selected window.
6621 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6622 (window)
6623 Lisp_Object window;
6625 struct window *w = decode_window (window);
6626 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6627 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6628 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ?
6629 Qt : Qnil), Qnil)));
6634 /***********************************************************************
6635 Scroll bars
6636 ***********************************************************************/
6638 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6639 2, 4, 0,
6640 doc: /* Set width and type of scroll bars of window WINDOW.
6641 If window is nil, set scroll bars of the currently selected window.
6642 Second parameter WIDTH specifies the pixel width for the scroll bar;
6643 this is automatically adjusted to a multiple of the frame column width.
6644 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6645 bar: left, right, or nil.
6646 If WIDTH is nil, use the frame's scroll-bar width.
6647 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6648 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6649 (window, width, vertical_type, horizontal_type)
6650 Lisp_Object window, width, vertical_type, horizontal_type;
6652 struct window *w = decode_window (window);
6654 if (!NILP (width))
6656 CHECK_NATNUM (width);
6658 if (XINT (width) == 0)
6659 vertical_type = Qnil;
6662 if (!(EQ (vertical_type, Qnil)
6663 || EQ (vertical_type, Qleft)
6664 || EQ (vertical_type, Qright)
6665 || EQ (vertical_type, Qt)))
6666 error ("Invalid type of vertical scroll bar");
6668 if (!EQ (w->scroll_bar_width, width)
6669 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6671 w->scroll_bar_width = width;
6672 w->vertical_scroll_bar_type = vertical_type;
6674 adjust_window_margins (w);
6676 clear_glyph_matrix (w->current_matrix);
6677 w->window_end_valid = Qnil;
6679 ++windows_or_buffers_changed;
6680 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6683 return Qnil;
6687 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6688 0, 1, 0,
6689 doc: /* Get width and type of scroll bars of window WINDOW.
6690 If WINDOW is omitted or nil, use the currently selected window.
6691 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6692 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6693 value. */)
6694 (window)
6695 Lisp_Object window;
6697 struct window *w = decode_window (window);
6698 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6699 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6700 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6701 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6702 Fcons (w->vertical_scroll_bar_type,
6703 Fcons (Qnil, Qnil))));
6708 /***********************************************************************
6709 Smooth scrolling
6710 ***********************************************************************/
6712 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6713 doc: /* Return the amount by which WINDOW is scrolled vertically.
6714 Use the selected window if WINDOW is nil or omitted.
6715 Normally, value is a multiple of the canonical character height of WINDOW;
6716 optional second arg PIXELS-P means value is measured in pixels. */)
6717 (window, pixels_p)
6718 Lisp_Object window, pixels_p;
6720 Lisp_Object result;
6721 struct frame *f;
6722 struct window *w;
6724 if (NILP (window))
6725 window = selected_window;
6726 else
6727 CHECK_WINDOW (window);
6728 w = XWINDOW (window);
6729 f = XFRAME (w->frame);
6731 if (FRAME_WINDOW_P (f))
6732 result = (NILP (pixels_p)
6733 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6734 : make_number (-w->vscroll));
6735 else
6736 result = make_number (0);
6737 return result;
6741 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6742 2, 3, 0,
6743 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6744 WINDOW nil means use the selected window. Normally, VSCROLL is a
6745 non-negative multiple of the canonical character height of WINDOW;
6746 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6747 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6748 corresponds to an integral number of pixels. The return value is the
6749 result of this rounding.
6750 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6751 (window, vscroll, pixels_p)
6752 Lisp_Object window, vscroll, pixels_p;
6754 struct window *w;
6755 struct frame *f;
6757 if (NILP (window))
6758 window = selected_window;
6759 else
6760 CHECK_WINDOW (window);
6761 CHECK_NUMBER_OR_FLOAT (vscroll);
6763 w = XWINDOW (window);
6764 f = XFRAME (w->frame);
6766 if (FRAME_WINDOW_P (f))
6768 int old_dy = w->vscroll;
6770 w->vscroll = - (NILP (pixels_p)
6771 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6772 : XFLOATINT (vscroll));
6773 w->vscroll = min (w->vscroll, 0);
6775 if (w->vscroll != old_dy)
6777 /* Adjust glyph matrix of the frame if the virtual display
6778 area becomes larger than before. */
6779 if (w->vscroll < 0 && w->vscroll < old_dy)
6780 adjust_glyphs (f);
6782 /* Prevent redisplay shortcuts. */
6783 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6787 return Fwindow_vscroll (window, pixels_p);
6791 /* Call FN for all leaf windows on frame F. FN is called with the
6792 first argument being a pointer to the leaf window, and with
6793 additional argument USER_DATA. Stops when FN returns 0. */
6795 void
6796 foreach_window (f, fn, user_data)
6797 struct frame *f;
6798 int (* fn) P_ ((struct window *, void *));
6799 void *user_data;
6801 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6805 /* Helper function for foreach_window. Call FN for all leaf windows
6806 reachable from W. FN is called with the first argument being a
6807 pointer to the leaf window, and with additional argument USER_DATA.
6808 Stop when FN returns 0. Value is 0 if stopped by FN. */
6810 static int
6811 foreach_window_1 (w, fn, user_data)
6812 struct window *w;
6813 int (* fn) P_ ((struct window *, void *));
6814 void *user_data;
6816 int cont;
6818 for (cont = 1; w && cont;)
6820 if (!NILP (w->hchild))
6821 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6822 else if (!NILP (w->vchild))
6823 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6824 else
6825 cont = fn (w, user_data);
6827 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6830 return cont;
6834 /* Freeze or unfreeze the window start of W unless it is a
6835 mini-window or the selected window. FREEZE_P non-null means freeze
6836 the window start. */
6838 static int
6839 freeze_window_start (w, freeze_p)
6840 struct window *w;
6841 void *freeze_p;
6843 if (w == XWINDOW (selected_window)
6844 || MINI_WINDOW_P (w)
6845 || (MINI_WINDOW_P (XWINDOW (selected_window))
6846 && ! NILP (Vminibuf_scroll_window)
6847 && w == XWINDOW (Vminibuf_scroll_window)))
6848 freeze_p = NULL;
6850 w->frozen_window_start_p = freeze_p != NULL;
6851 return 1;
6855 /* Freeze or unfreeze the window starts of all leaf windows on frame
6856 F, except the selected window and a mini-window. FREEZE_P non-zero
6857 means freeze the window start. */
6859 void
6860 freeze_window_starts (f, freeze_p)
6861 struct frame *f;
6862 int freeze_p;
6864 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6868 /***********************************************************************
6869 Initialization
6870 ***********************************************************************/
6872 /* Return 1 if window configurations C1 and C2
6873 describe the same state of affairs. This is used by Fequal. */
6876 compare_window_configurations (c1, c2, ignore_positions)
6877 Lisp_Object c1, c2;
6878 int ignore_positions;
6880 register struct save_window_data *d1, *d2;
6881 struct Lisp_Vector *sw1, *sw2;
6882 int i;
6884 if (!WINDOW_CONFIGURATIONP (c1))
6885 wrong_type_argument (Qwindow_configuration_p, c1);
6886 if (!WINDOW_CONFIGURATIONP (c2))
6887 wrong_type_argument (Qwindow_configuration_p, c2);
6889 d1 = (struct save_window_data *) XVECTOR (c1);
6890 d2 = (struct save_window_data *) XVECTOR (c2);
6891 sw1 = XVECTOR (d1->saved_windows);
6892 sw2 = XVECTOR (d2->saved_windows);
6894 if (! EQ (d1->frame_cols, d2->frame_cols))
6895 return 0;
6896 if (! EQ (d1->frame_lines, d2->frame_lines))
6897 return 0;
6898 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
6899 return 0;
6900 if (! EQ (d1->selected_frame, d2->selected_frame))
6901 return 0;
6902 /* Don't compare the current_window field directly.
6903 Instead see w1_is_current and w2_is_current, below. */
6904 if (! EQ (d1->current_buffer, d2->current_buffer))
6905 return 0;
6906 if (! ignore_positions)
6908 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6909 return 0;
6910 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6911 return 0;
6913 /* Don't compare the root_window field.
6914 We don't require the two configurations
6915 to use the same window object,
6916 and the two root windows must be equivalent
6917 if everything else compares equal. */
6918 if (! EQ (d1->focus_frame, d2->focus_frame))
6919 return 0;
6920 if (! EQ (d1->min_width, d2->min_width))
6921 return 0;
6922 if (! EQ (d1->min_height, d2->min_height))
6923 return 0;
6925 /* Verify that the two confis have the same number of windows. */
6926 if (sw1->size != sw2->size)
6927 return 0;
6929 for (i = 0; i < sw1->size; i++)
6931 struct saved_window *p1, *p2;
6932 int w1_is_current, w2_is_current;
6934 p1 = SAVED_WINDOW_N (sw1, i);
6935 p2 = SAVED_WINDOW_N (sw2, i);
6937 /* Verify that the current windows in the two
6938 configurations correspond to each other. */
6939 w1_is_current = EQ (d1->current_window, p1->window);
6940 w2_is_current = EQ (d2->current_window, p2->window);
6942 if (w1_is_current != w2_is_current)
6943 return 0;
6945 /* Verify that the corresponding windows do match. */
6946 if (! EQ (p1->buffer, p2->buffer))
6947 return 0;
6948 if (! EQ (p1->left_col, p2->left_col))
6949 return 0;
6950 if (! EQ (p1->top_line, p2->top_line))
6951 return 0;
6952 if (! EQ (p1->total_cols, p2->total_cols))
6953 return 0;
6954 if (! EQ (p1->total_lines, p2->total_lines))
6955 return 0;
6956 if (! EQ (p1->display_table, p2->display_table))
6957 return 0;
6958 if (! EQ (p1->parent, p2->parent))
6959 return 0;
6960 if (! EQ (p1->prev, p2->prev))
6961 return 0;
6962 if (! ignore_positions)
6964 if (! EQ (p1->hscroll, p2->hscroll))
6965 return 0;
6966 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6967 return 0;
6968 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6969 return 0;
6970 if (NILP (Fequal (p1->start, p2->start)))
6971 return 0;
6972 if (NILP (Fequal (p1->pointm, p2->pointm)))
6973 return 0;
6974 if (NILP (Fequal (p1->mark, p2->mark)))
6975 return 0;
6977 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6978 return 0;
6979 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6980 return 0;
6981 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6982 return 0;
6983 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6984 return 0;
6985 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6986 return 0;
6987 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6988 return 0;
6989 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6990 return 0;
6993 return 1;
6996 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6997 Scompare_window_configurations, 2, 2, 0,
6998 doc: /* Compare two window configurations as regards the structure of windows.
6999 This function ignores details such as the values of point and mark
7000 and scrolling positions. */)
7001 (x, y)
7002 Lisp_Object x, y;
7004 if (compare_window_configurations (x, y, 1))
7005 return Qt;
7006 return Qnil;
7009 void
7010 init_window_once ()
7012 struct frame *f = make_initial_frame ();
7013 XSETFRAME (selected_frame, f);
7014 Vterminal_frame = selected_frame;
7015 minibuf_window = f->minibuffer_window;
7016 selected_window = f->selected_window;
7017 last_nonminibuf_frame = f;
7019 window_initialized = 1;
7022 void
7023 init_window ()
7025 Vwindow_list = Qnil;
7028 void
7029 syms_of_window ()
7031 Qscroll_up = intern ("scroll-up");
7032 staticpro (&Qscroll_up);
7034 Qscroll_down = intern ("scroll-down");
7035 staticpro (&Qscroll_down);
7037 Qwindow_size_fixed = intern ("window-size-fixed");
7038 staticpro (&Qwindow_size_fixed);
7039 Fset (Qwindow_size_fixed, Qnil);
7041 staticpro (&Qwindow_configuration_change_hook);
7042 Qwindow_configuration_change_hook
7043 = intern ("window-configuration-change-hook");
7045 Qwindowp = intern ("windowp");
7046 staticpro (&Qwindowp);
7048 Qwindow_configuration_p = intern ("window-configuration-p");
7049 staticpro (&Qwindow_configuration_p);
7051 Qwindow_live_p = intern ("window-live-p");
7052 staticpro (&Qwindow_live_p);
7054 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
7055 staticpro (&Qtemp_buffer_show_hook);
7057 staticpro (&Vwindow_list);
7059 minibuf_selected_window = Qnil;
7060 staticpro (&minibuf_selected_window);
7062 window_scroll_pixel_based_preserve_y = -1;
7064 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
7065 doc: /* Non-nil means call as function to display a help buffer.
7066 The function is called with one argument, the buffer to be displayed.
7067 Used by `with-output-to-temp-buffer'.
7068 If this function is used, then it must do the entire job of showing
7069 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7070 Vtemp_buffer_show_function = Qnil;
7072 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
7073 doc: /* If non-nil, function to call to handle `display-buffer'.
7074 It will receive two args, the buffer and a flag which if non-nil means
7075 that the currently selected window is not acceptable.
7076 It should choose or create a window, display the specified buffer in it,
7077 and return the window.
7078 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'
7079 work using this function. */);
7080 Vdisplay_buffer_function = Qnil;
7082 DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
7083 doc: /* *If non-nil, `display-buffer' should even the window heights.
7084 If nil, `display-buffer' will leave the window configuration alone. */);
7085 Veven_window_heights = Qt;
7087 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
7088 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7089 Vminibuf_scroll_window = Qnil;
7091 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
7092 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7093 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7094 is displayed in the `mode-line' face. */);
7095 mode_line_in_non_selected_windows = 1;
7097 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
7098 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7099 Vother_window_scroll_buffer = Qnil;
7101 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
7102 doc: /* *Non-nil means `display-buffer' should make a separate frame. */);
7103 pop_up_frames = 0;
7105 DEFVAR_BOOL ("auto-window-vscroll", &auto_window_vscroll_p,
7106 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7107 auto_window_vscroll_p = 1;
7109 DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
7110 doc: /* *Non-nil means `display-buffer' should reuse frames.
7111 If the buffer in question is already displayed in a frame, raise that frame. */);
7112 display_buffer_reuse_frames = 0;
7114 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
7115 doc: /* Function to call to handle automatic new frame creation.
7116 It is called with no arguments and should return a newly created frame.
7118 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
7119 where `pop-up-frame-alist' would hold the default frame parameters. */);
7120 Vpop_up_frame_function = Qnil;
7122 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
7123 doc: /* *List of buffer names that should have their own special frames.
7124 Displaying a buffer with `display-buffer' or `pop-to-buffer',
7125 if its name is in this list, makes a special frame for it
7126 using `special-display-function'. See also `special-display-regexps'.
7128 An element of the list can be a list instead of just a string.
7129 There are two ways to use a list as an element:
7130 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
7131 In the first case, the FRAME-PARAMETERS are pairs of the form
7132 \(PARAMETER . VALUE); these parameter values are used to create the frame.
7133 In the second case, FUNCTION is called with BUFFER as the first argument,
7134 followed by the OTHER-ARGS--it can display BUFFER in any way it likes.
7135 All this is done by the function found in `special-display-function'.
7137 If the specified frame parameters include (same-buffer . t), the
7138 buffer is displayed in the currently selected window. Otherwise, if
7139 they include (same-frame . t), the buffer is displayed in a new window
7140 in the currently selected frame.
7142 If this variable appears \"not to work\", because you add a name to it
7143 but that buffer still appears in the selected window, look at the
7144 values of `same-window-buffer-names' and `same-window-regexps'.
7145 Those variables take precedence over this one. */);
7146 Vspecial_display_buffer_names = Qnil;
7148 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
7149 doc: /* *List of regexps saying which buffers should have their own special frames.
7150 When displaying a buffer with `display-buffer' or `pop-to-buffer',
7151 if any regexp in this list matches the buffer name, it makes a
7152 special frame for the buffer by calling `special-display-function'.
7154 An element of the list can be a list instead of just a string.
7155 There are two ways to use a list as an element:
7156 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
7157 In the first case, the FRAME-PARAMETERS are pairs of the form
7158 \(PARAMETER . VALUE); these parameter values are used to create the frame.
7159 In the second case, FUNCTION is called with BUFFER as the first argument,
7160 followed by the OTHER-ARGS--it can display the buffer in any way it likes.
7161 All this is done by the function found in `special-display-function'.
7163 If the specified frame parameters include (same-buffer . t), the
7164 buffer is displayed in the currently selected window. Otherwise, if
7165 they include (same-frame . t), the buffer is displayed in a new window
7166 in the currently selected frame.
7168 If this variable appears \"not to work\", because you add a regexp to it
7169 but the matching buffers still appear in the selected window, look at the
7170 values of `same-window-buffer-names' and `same-window-regexps'.
7171 Those variables take precedence over this one. */);
7172 Vspecial_display_regexps = Qnil;
7174 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
7175 doc: /* Function to call to make a new frame for a special buffer.
7176 It is called with two arguments, the buffer and optional buffer specific
7177 data, and should return a window displaying that buffer.
7178 The default value normally makes a separate frame for the buffer,
7179 using `special-display-frame-alist' to specify the frame parameters.
7180 But if the buffer specific data includes (same-buffer . t) then the
7181 buffer is displayed in the current selected window.
7182 Otherwise if it includes (same-frame . t) then the buffer is displayed in
7183 a new window in the currently selected frame.
7185 A buffer is special if it is listed in `special-display-buffer-names'
7186 or matches a regexp in `special-display-regexps'. */);
7187 Vspecial_display_function = Qnil;
7189 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
7190 doc: /* *List of buffer names that should appear in the selected window.
7191 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
7192 switches to it in the selected window, rather than making it appear
7193 in some other window.
7195 An element of the list can be a cons cell instead of just a string.
7196 Then the car must be a string, which specifies the buffer name.
7197 This is for compatibility with `special-display-buffer-names';
7198 the cdr of the cons cell is ignored.
7200 See also `same-window-regexps'. */);
7201 Vsame_window_buffer_names = Qnil;
7203 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
7204 doc: /* *List of regexps saying which buffers should appear in the selected window.
7205 If a buffer name matches one of these regexps, then displaying it
7206 using `display-buffer' or `pop-to-buffer' switches to it
7207 in the selected window, rather than making it appear in some other window.
7209 An element of the list can be a cons cell instead of just a string.
7210 Then the car must be a string, which specifies the buffer name.
7211 This is for compatibility with `special-display-buffer-names';
7212 the cdr of the cons cell is ignored.
7214 See also `same-window-buffer-names'. */);
7215 Vsame_window_regexps = Qnil;
7217 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
7218 doc: /* *Non-nil means display-buffer should make new windows. */);
7219 pop_up_windows = 1;
7221 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
7222 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7223 next_screen_context_lines = 2;
7225 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
7226 doc: /* *A window must be at least this tall to be eligible for splitting by `display-buffer'.
7227 If there is only one window, it is split regardless of this value. */);
7228 split_height_threshold = 500;
7230 DEFVAR_INT ("window-min-height", &window_min_height,
7231 doc: /* *Delete any window less than this tall (including its mode line). */);
7232 window_min_height = 4;
7234 DEFVAR_INT ("window-min-width", &window_min_width,
7235 doc: /* *Delete any window less than this wide. */);
7236 window_min_width = 10;
7238 DEFVAR_LISP ("scroll-preserve-screen-position",
7239 &Vscroll_preserve_screen_position,
7240 doc: /* *Controls if scroll commands move point to keep its screen line unchanged.
7241 A value of nil means point does not keep its screen position except
7242 at the scroll margin or window boundary respectively.
7243 A value of t means point keeps its screen position if the scroll
7244 command moved it vertically out of the window, e.g. when scrolling
7245 by full screens.
7246 Any other value means point always keeps its screen position. */);
7247 Vscroll_preserve_screen_position = Qnil;
7249 DEFVAR_LISP ("window-configuration-change-hook",
7250 &Vwindow_configuration_change_hook,
7251 doc: /* Functions to call when window configuration changes.
7252 The selected frame is the one whose configuration has changed. */);
7253 Vwindow_configuration_change_hook = Qnil;
7255 defsubr (&Sselected_window);
7256 defsubr (&Sminibuffer_window);
7257 defsubr (&Swindow_minibuffer_p);
7258 defsubr (&Swindowp);
7259 defsubr (&Swindow_live_p);
7260 defsubr (&Spos_visible_in_window_p);
7261 defsubr (&Swindow_buffer);
7262 defsubr (&Swindow_height);
7263 defsubr (&Swindow_width);
7264 defsubr (&Swindow_hscroll);
7265 defsubr (&Sset_window_hscroll);
7266 defsubr (&Swindow_redisplay_end_trigger);
7267 defsubr (&Sset_window_redisplay_end_trigger);
7268 defsubr (&Swindow_edges);
7269 defsubr (&Swindow_pixel_edges);
7270 defsubr (&Swindow_inside_edges);
7271 defsubr (&Swindow_inside_pixel_edges);
7272 defsubr (&Scoordinates_in_window_p);
7273 defsubr (&Swindow_at);
7274 defsubr (&Swindow_point);
7275 defsubr (&Swindow_start);
7276 defsubr (&Swindow_end);
7277 defsubr (&Sset_window_point);
7278 defsubr (&Sset_window_start);
7279 defsubr (&Swindow_dedicated_p);
7280 defsubr (&Sset_window_dedicated_p);
7281 defsubr (&Swindow_display_table);
7282 defsubr (&Sset_window_display_table);
7283 defsubr (&Snext_window);
7284 defsubr (&Sprevious_window);
7285 defsubr (&Sother_window);
7286 defsubr (&Sget_lru_window);
7287 defsubr (&Sget_largest_window);
7288 defsubr (&Sget_buffer_window);
7289 defsubr (&Sdelete_other_windows);
7290 defsubr (&Sdelete_windows_on);
7291 defsubr (&Sreplace_buffer_in_windows);
7292 defsubr (&Sdelete_window);
7293 defsubr (&Sset_window_buffer);
7294 defsubr (&Sselect_window);
7295 defsubr (&Sspecial_display_p);
7296 defsubr (&Ssame_window_p);
7297 defsubr (&Sdisplay_buffer);
7298 defsubr (&Sforce_window_update);
7299 defsubr (&Ssplit_window);
7300 defsubr (&Senlarge_window);
7301 defsubr (&Sshrink_window);
7302 defsubr (&Sadjust_window_trailing_edge);
7303 defsubr (&Sscroll_up);
7304 defsubr (&Sscroll_down);
7305 defsubr (&Sscroll_left);
7306 defsubr (&Sscroll_right);
7307 defsubr (&Sother_window_for_scrolling);
7308 defsubr (&Sscroll_other_window);
7309 defsubr (&Sminibuffer_selected_window);
7310 defsubr (&Srecenter);
7311 defsubr (&Swindow_text_height);
7312 defsubr (&Smove_to_window_line);
7313 defsubr (&Swindow_configuration_p);
7314 defsubr (&Swindow_configuration_frame);
7315 defsubr (&Sset_window_configuration);
7316 defsubr (&Scurrent_window_configuration);
7317 defsubr (&Ssave_window_excursion);
7318 defsubr (&Swindow_tree);
7319 defsubr (&Sset_window_margins);
7320 defsubr (&Swindow_margins);
7321 defsubr (&Sset_window_fringes);
7322 defsubr (&Swindow_fringes);
7323 defsubr (&Sset_window_scroll_bars);
7324 defsubr (&Swindow_scroll_bars);
7325 defsubr (&Swindow_vscroll);
7326 defsubr (&Sset_window_vscroll);
7327 defsubr (&Scompare_window_configurations);
7328 defsubr (&Swindow_list);
7331 void
7332 keys_of_window ()
7334 initial_define_key (control_x_map, '1', "delete-other-windows");
7335 initial_define_key (control_x_map, '2', "split-window");
7336 initial_define_key (control_x_map, '0', "delete-window");
7337 initial_define_key (control_x_map, 'o', "other-window");
7338 initial_define_key (control_x_map, '^', "enlarge-window");
7339 initial_define_key (control_x_map, '<', "scroll-left");
7340 initial_define_key (control_x_map, '>', "scroll-right");
7342 initial_define_key (global_map, Ctl ('V'), "scroll-up");
7343 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7344 initial_define_key (meta_map, 'v', "scroll-down");
7346 initial_define_key (global_map, Ctl('L'), "recenter");
7347 initial_define_key (meta_map, 'r', "move-to-window-line");
7350 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
7351 (do not change this comment) */