Fix C-g during `make-network-process'. (Reported by Mark Plaksin.)
[emacs.git] / src / window.c
blobdd80f629e1ed5765373c31bdca8cc08c3b84b531
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 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 Qwindow_size_fixed;
56 extern Lisp_Object Qleft_margin, Qright_margin;
58 static int displayed_window_lines P_ ((struct window *));
59 static struct window *decode_window P_ ((Lisp_Object));
60 static int count_windows P_ ((struct window *));
61 static int get_leaf_windows P_ ((struct window *, struct window **, int));
62 static void window_scroll P_ ((Lisp_Object, int, int, int));
63 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
64 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
65 static int window_min_size_1 P_ ((struct window *, int));
66 static int window_min_size P_ ((struct window *, int, int, int *));
67 static void size_window P_ ((Lisp_Object, int, int, int));
68 static int freeze_window_start P_ ((struct window *, void *));
69 static int window_fixed_size_p P_ ((struct window *, int, int));
70 static void enlarge_window P_ ((Lisp_Object, int, int));
71 static Lisp_Object window_list P_ ((void));
72 static int add_window_to_list P_ ((struct window *, void *));
73 static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
74 Lisp_Object));
75 static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
76 Lisp_Object, int));
77 static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
78 Lisp_Object *));
79 static int foreach_window_1 P_ ((struct window *,
80 int (* fn) (struct window *, void *),
81 void *));
82 static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
84 /* This is the window in which the terminal's cursor should
85 be left when nothing is being done with it. This must
86 always be a leaf window, and its buffer is selected by
87 the top level editing loop at the end of each command.
89 This value is always the same as
90 FRAME_SELECTED_WINDOW (selected_frame). */
92 Lisp_Object selected_window;
94 /* A list of all windows for use by next_window and Fwindow_list.
95 Functions creating or deleting windows should invalidate this cache
96 by setting it to nil. */
98 Lisp_Object Vwindow_list;
100 /* The mini-buffer window of the selected frame.
101 Note that you cannot test for mini-bufferness of an arbitrary window
102 by comparing against this; but you can test for mini-bufferness of
103 the selected window. */
105 Lisp_Object minibuf_window;
107 /* Non-nil means it is the window whose mode line should be
108 shown as the selected window when the minibuffer is selected. */
110 Lisp_Object minibuf_selected_window;
112 /* Non-nil means it is the window for C-M-v to scroll
113 when the mini-buffer is selected. */
115 Lisp_Object Vminibuf_scroll_window;
117 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
119 Lisp_Object Vother_window_scroll_buffer;
121 /* Non-nil means it's function to call to display temp buffers. */
123 Lisp_Object Vtemp_buffer_show_function;
125 /* Non-zero means line and page scrolling on tall lines (with images)
126 does partial scrolling by modifying window-vscroll. */
128 int auto_window_vscroll_p;
130 /* Non-zero means to use mode-line-inactive face in all windows but the
131 selected-window and the minibuffer-scroll-window when the
132 minibuffer is active. */
133 int mode_line_in_non_selected_windows;
135 /* If a window gets smaller than either of these, it is removed. */
137 EMACS_INT window_min_height;
138 EMACS_INT window_min_width;
140 /* Nonzero implies Fdisplay_buffer should create windows. */
142 int pop_up_windows;
144 /* Nonzero implies make new frames for Fdisplay_buffer. */
146 int pop_up_frames;
148 /* Nonzero means reuse existing frames for displaying buffers. */
150 int display_buffer_reuse_frames;
152 /* Non-nil means use this function instead of default */
154 Lisp_Object Vpop_up_frame_function;
156 /* Function to call to handle Fdisplay_buffer. */
158 Lisp_Object Vdisplay_buffer_function;
160 /* Non-nil means that Fdisplay_buffer should even the heights of windows. */
162 Lisp_Object Veven_window_heights;
164 /* List of buffer *names* for buffers that should have their own frames. */
166 Lisp_Object Vspecial_display_buffer_names;
168 /* List of regexps for buffer names that should have their own frames. */
170 Lisp_Object Vspecial_display_regexps;
172 /* Function to pop up a special frame. */
174 Lisp_Object Vspecial_display_function;
176 /* List of buffer *names* for buffers to appear in selected window. */
178 Lisp_Object Vsame_window_buffer_names;
180 /* List of regexps for buffer names to appear in selected window. */
182 Lisp_Object Vsame_window_regexps;
184 /* Hook run at end of temp_output_buffer_show. */
186 Lisp_Object Qtemp_buffer_show_hook;
188 /* Fdisplay_buffer always splits the largest window
189 if that window is more than this high. */
191 EMACS_INT split_height_threshold;
193 /* Number of lines of continuity in scrolling by screenfuls. */
195 EMACS_INT next_screen_context_lines;
197 /* Incremented for each window created. */
199 static int sequence_number;
201 /* Nonzero after init_window_once has finished. */
203 static int window_initialized;
205 /* Hook to run when window config changes. */
207 Lisp_Object Qwindow_configuration_change_hook;
208 Lisp_Object Vwindow_configuration_change_hook;
210 /* Non-nil means scroll commands try to put point
211 at the same screen height as previously. */
213 Lisp_Object Vscroll_preserve_screen_position;
215 /* Incremented by 1 whenever a window is deleted. */
217 int window_deletion_count;
219 #if 0 /* This isn't used anywhere. */
220 /* Nonzero means we can split a frame even if it is "unsplittable". */
221 static int inhibit_frame_unsplittable;
222 #endif /* 0 */
224 extern EMACS_INT scroll_margin;
226 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
228 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
229 doc: /* Returns t if OBJECT is a window. */)
230 (object)
231 Lisp_Object object;
233 return WINDOWP (object) ? Qt : Qnil;
236 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
237 doc: /* Returns t if OBJECT is a window which is currently visible. */)
238 (object)
239 Lisp_Object object;
241 return WINDOW_LIVE_P (object) ? Qt : Qnil;
244 Lisp_Object
245 make_window ()
247 Lisp_Object val;
248 register struct window *p;
250 p = allocate_window ();
251 ++sequence_number;
252 XSETFASTINT (p->sequence_number, sequence_number);
253 XSETFASTINT (p->left_col, 0);
254 XSETFASTINT (p->top_line, 0);
255 XSETFASTINT (p->total_lines, 0);
256 XSETFASTINT (p->total_cols, 0);
257 XSETFASTINT (p->hscroll, 0);
258 XSETFASTINT (p->min_hscroll, 0);
259 p->orig_top_line = p->orig_total_lines = Qnil;
260 p->start = Fmake_marker ();
261 p->pointm = Fmake_marker ();
262 XSETFASTINT (p->use_time, 0);
263 p->frame = Qnil;
264 p->display_table = Qnil;
265 p->dedicated = Qnil;
266 p->pseudo_window_p = 0;
267 bzero (&p->cursor, sizeof (p->cursor));
268 bzero (&p->last_cursor, sizeof (p->last_cursor));
269 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
270 p->desired_matrix = p->current_matrix = 0;
271 p->nrows_scale_factor = p->ncols_scale_factor = 1;
272 p->phys_cursor_type = -1;
273 p->phys_cursor_width = -1;
274 p->must_be_updated_p = 0;
275 XSETFASTINT (p->window_end_vpos, 0);
276 XSETFASTINT (p->window_end_pos, 0);
277 p->window_end_valid = Qnil;
278 p->vscroll = 0;
279 XSETWINDOW (val, p);
280 XSETFASTINT (p->last_point, 0);
281 p->frozen_window_start_p = 0;
282 p->last_cursor_off_p = p->cursor_off_p = 0;
283 p->left_margin_cols = Qnil;
284 p->right_margin_cols = Qnil;
285 p->left_fringe_width = Qnil;
286 p->right_fringe_width = Qnil;
287 p->fringes_outside_margins = Qnil;
288 p->scroll_bar_width = Qnil;
289 p->vertical_scroll_bar_type = Qt;
291 Vwindow_list = Qnil;
292 return val;
295 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
296 doc: /* Return the window that the cursor now appears in and commands apply to. */)
299 return selected_window;
302 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
303 doc: /* Return the window used now for minibuffers.
304 If the optional argument FRAME is specified, return the minibuffer window
305 used by that frame. */)
306 (frame)
307 Lisp_Object frame;
309 if (NILP (frame))
310 frame = selected_frame;
311 CHECK_LIVE_FRAME (frame);
312 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
315 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
316 doc: /* Returns non-nil if WINDOW is a minibuffer window.
317 WINDOW defaults to the selected window. */)
318 (window)
319 Lisp_Object window;
321 struct window *w = decode_window (window);
322 return MINI_WINDOW_P (w) ? Qt : Qnil;
326 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
327 Spos_visible_in_window_p, 0, 3, 0,
328 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
329 Return nil if that position is scrolled vertically out of view.
330 If a character is only partially visible, nil is returned, unless the
331 optional argument PARTIALLY is non-nil.
332 If POS is only out of view because of horizontal scrolling, return non-nil.
333 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
335 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
336 return value is a list (X Y PARTIAL) where X and Y are the pixel coordinates
337 relative to the top left corner of the window. PARTIAL is nil if the character
338 after POS is fully visible; otherwise it is a cons (RTOP . RBOT) where RTOP
339 and RBOT are the number of pixels invisible at the top and bottom of the row. */)
340 (pos, window, partially)
341 Lisp_Object pos, window, partially;
343 register struct window *w;
344 register int posint;
345 register struct buffer *buf;
346 struct text_pos top;
347 Lisp_Object in_window = Qnil;
348 int rtop, rbot, fully_p = 1;
349 int x, y;
351 w = decode_window (window);
352 buf = XBUFFER (w->buffer);
353 SET_TEXT_POS_FROM_MARKER (top, w->start);
355 if (!NILP (pos))
357 CHECK_NUMBER_COERCE_MARKER (pos);
358 posint = XINT (pos);
360 else if (w == XWINDOW (selected_window))
361 posint = PT;
362 else
363 posint = XMARKER (w->pointm)->charpos;
365 /* If position is above window start or outside buffer boundaries,
366 or if window start is out of range, position is not visible. */
367 if (posint >= CHARPOS (top)
368 && posint <= BUF_ZV (buf)
369 && CHARPOS (top) >= BUF_BEGV (buf)
370 && CHARPOS (top) <= BUF_ZV (buf)
371 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, NILP (partially))
372 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
373 in_window = Qt;
375 if (!NILP (in_window) && !NILP (partially))
376 in_window = Fcons (make_number (x),
377 Fcons (make_number (y),
378 Fcons ((fully_p ? Qnil
379 : Fcons (make_number (rtop),
380 make_number (rbot))),
381 Qnil)));
382 return in_window;
386 static struct window *
387 decode_window (window)
388 register Lisp_Object window;
390 if (NILP (window))
391 return XWINDOW (selected_window);
393 CHECK_LIVE_WINDOW (window);
394 return XWINDOW (window);
397 static struct window *
398 decode_any_window (window)
399 register Lisp_Object window;
401 if (NILP (window))
402 return XWINDOW (selected_window);
404 CHECK_WINDOW (window);
405 return XWINDOW (window);
408 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
409 doc: /* Return the buffer that WINDOW is displaying.
410 WINDOW defaults to the selected window. */)
411 (window)
412 Lisp_Object window;
414 return decode_window (window)->buffer;
417 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
418 doc: /* Return the number of lines in WINDOW (including its mode line). */)
419 (window)
420 Lisp_Object window;
422 return decode_any_window (window)->total_lines;
425 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
426 doc: /* Return the number of display columns in WINDOW.
427 This is the width that is usable columns available for text in WINDOW.
428 If you want to find out how many columns WINDOW takes up,
429 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
430 (window)
431 Lisp_Object window;
433 return make_number (window_box_text_cols (decode_any_window (window)));
436 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
437 doc: /* Return the number of columns by which WINDOW is scrolled from left margin. */)
438 (window)
439 Lisp_Object window;
441 return decode_window (window)->hscroll;
444 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
445 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
446 Return NCOL. NCOL should be zero or positive.
448 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
449 window so that the location of point becomes invisible. */)
450 (window, ncol)
451 Lisp_Object window, ncol;
453 struct window *w = decode_window (window);
454 int hscroll;
456 CHECK_NUMBER (ncol);
457 hscroll = max (0, XINT (ncol));
459 /* Prevent redisplay shortcuts when changing the hscroll. */
460 if (XINT (w->hscroll) != hscroll)
461 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
463 w->hscroll = make_number (hscroll);
464 return ncol;
467 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
468 Swindow_redisplay_end_trigger, 0, 1, 0,
469 doc: /* Return WINDOW's redisplay end trigger value.
470 See `set-window-redisplay-end-trigger' for more information. */)
471 (window)
472 Lisp_Object window;
474 return decode_window (window)->redisplay_end_trigger;
477 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
478 Sset_window_redisplay_end_trigger, 2, 2, 0,
479 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
480 VALUE should be a buffer position (typically a marker) or nil.
481 If it is a buffer position, then if redisplay in WINDOW reaches a position
482 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
483 with two arguments: WINDOW, and the end trigger value.
484 Afterwards the end-trigger value is reset to nil. */)
485 (window, value)
486 register Lisp_Object window, value;
488 register struct window *w;
490 w = decode_window (window);
491 w->redisplay_end_trigger = value;
492 return value;
495 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
496 doc: /* Return a list of the edge coordinates of WINDOW.
497 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
498 RIGHT is one more than the rightmost column occupied by WINDOW,
499 and BOTTOM is one more than the bottommost row occupied by WINDOW.
500 The edges include the space used by the window's scroll bar,
501 display margins, fringes, header line, and mode line, if it has them.
502 To get the edges of the actual text area, use `window-inside-edges'. */)
503 (window)
504 Lisp_Object window;
506 register struct window *w = decode_any_window (window);
508 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
509 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
510 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
511 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
512 Qnil))));
515 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
516 doc: /* Return a list of the edge pixel coordinates of WINDOW.
517 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
518 RIGHT is one more than the rightmost x position occupied by WINDOW,
519 and BOTTOM is one more than the bottommost y position occupied by WINDOW.
520 The pixel edges include the space used by the window's scroll bar,
521 display margins, fringes, header line, and mode line, if it has them.
522 To get the edges of the actual text area, use `window-inside-pixel-edges'. */)
523 (window)
524 Lisp_Object window;
526 register struct window *w = decode_any_window (window);
528 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
529 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
530 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
531 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
532 Qnil))));
535 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
536 doc: /* Return a list of the edge coordinates of WINDOW.
537 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
538 RIGHT is one more than the rightmost column used by text in WINDOW,
539 and BOTTOM is one more than the bottommost row used by text in WINDOW.
540 The inside edges do not include the space used by the window's scroll bar,
541 display margins, fringes, header line, and/or mode line. */)
542 (window)
543 Lisp_Object window;
545 register struct window *w = decode_any_window (window);
547 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
548 + WINDOW_LEFT_MARGIN_COLS (w)
549 + WINDOW_LEFT_FRINGE_COLS (w)),
550 make_number (WINDOW_TOP_EDGE_LINE (w)
551 + WINDOW_HEADER_LINE_LINES (w)),
552 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
553 - WINDOW_RIGHT_MARGIN_COLS (w)
554 - WINDOW_RIGHT_FRINGE_COLS (w)),
555 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
556 - WINDOW_MODE_LINE_LINES (w)));
559 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
560 doc: /* Return a list of the edge pixel coordinates of WINDOW.
561 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
562 RIGHT is one more than the rightmost x position used by text in WINDOW,
563 and BOTTOM is one more than the bottommost y position used by text in WINDOW.
564 The inside edges do not include the space used by the window's scroll bar,
565 display margins, fringes, header line, and/or mode line. */)
566 (window)
567 Lisp_Object window;
569 register struct window *w = decode_any_window (window);
571 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
572 + WINDOW_LEFT_MARGIN_WIDTH (w)
573 + WINDOW_LEFT_FRINGE_WIDTH (w)),
574 make_number (WINDOW_TOP_EDGE_Y (w)
575 + WINDOW_HEADER_LINE_HEIGHT (w)),
576 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
577 - WINDOW_RIGHT_MARGIN_WIDTH (w)
578 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
579 make_number (WINDOW_BOTTOM_EDGE_Y (w)
580 - WINDOW_MODE_LINE_HEIGHT (w)));
583 /* Test if the character at column *X, row *Y is within window W.
584 If it is not, return ON_NOTHING;
585 if it is in the window's text area,
586 set *x and *y to its location relative to the upper left corner
587 of the window, and
588 return ON_TEXT;
589 if it is on the window's modeline, return ON_MODE_LINE;
590 if it is on the border between the window and its right sibling,
591 return ON_VERTICAL_BORDER.
592 if it is on a scroll bar,
593 return ON_SCROLL_BAR.
594 if it is on the window's top line, return ON_HEADER_LINE;
595 if it is in left or right fringe of the window,
596 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
597 to window-relative coordinates;
598 if it is in the marginal area to the left/right of the window,
599 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
600 to window-relative coordinates.
602 X and Y are frame relative pixel coordinates. */
604 static enum window_part
605 coordinates_in_window (w, x, y)
606 register struct window *w;
607 register int *x, *y;
609 struct frame *f = XFRAME (WINDOW_FRAME (w));
610 int left_x, right_x, top_y, bottom_y;
611 enum window_part part;
612 int ux = FRAME_COLUMN_WIDTH (f);
613 int x0 = WINDOW_LEFT_EDGE_X (w);
614 int x1 = WINDOW_RIGHT_EDGE_X (w);
615 /* The width of the area where the vertical line can be dragged.
616 (Between mode lines for instance. */
617 int grabbable_width = ux;
618 int lmargin_width, rmargin_width, text_left, text_right;
620 /* In what's below, we subtract 1 when computing right_x because we
621 want the rightmost pixel, which is given by left_pixel+width-1. */
622 if (w->pseudo_window_p)
624 left_x = 0;
625 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
626 top_y = WINDOW_TOP_EDGE_Y (w);
627 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
629 else
631 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
632 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
633 top_y = WINDOW_TOP_EDGE_Y (w);
634 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
637 /* Outside any interesting row? */
638 if (*y < top_y || *y >= bottom_y)
639 return ON_NOTHING;
641 /* On the mode line or header line? If it's near the start of
642 the mode or header line of window that's has a horizontal
643 sibling, say it's on the vertical line. That's to be able
644 to resize windows horizontally in case we're using toolkit
645 scroll bars. */
647 if (WINDOW_WANTS_MODELINE_P (w)
648 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
650 part = ON_MODE_LINE;
652 header_vertical_border_check:
653 /* We're somewhere on the mode line. We consider the place
654 between mode lines of horizontally adjacent mode lines
655 as the vertical border. If scroll bars on the left,
656 return the right window. */
657 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
658 || WINDOW_RIGHTMOST_P (w))
660 if (!WINDOW_LEFTMOST_P (w) && abs (*x - x0) < grabbable_width)
661 return ON_VERTICAL_BORDER;
663 else
665 if (abs (*x - x1) < grabbable_width)
666 return ON_VERTICAL_BORDER;
669 if (*x < x0 || *x >= x1)
670 return ON_NOTHING;
672 /* Convert X and Y to window relative coordinates.
673 Mode line starts at left edge of window. */
674 *x -= x0;
675 *y -= top_y;
676 return part;
679 if (WINDOW_WANTS_HEADER_LINE_P (w)
680 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
682 part = ON_HEADER_LINE;
683 goto header_vertical_border_check;
686 if (*x < x0 || *x >= x1)
687 return ON_NOTHING;
689 /* Outside any interesting column? */
690 if (*x < left_x || *x > right_x)
692 *y -= top_y;
693 return ON_SCROLL_BAR;
696 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
697 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
699 text_left = window_box_left (w, TEXT_AREA);
700 text_right = text_left + window_box_width (w, TEXT_AREA);
702 if (FRAME_WINDOW_P (f))
704 if (!w->pseudo_window_p
705 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
706 && !WINDOW_RIGHTMOST_P (w)
707 && (abs (*x - right_x) < grabbable_width))
708 return ON_VERTICAL_BORDER;
710 else
712 /* Need to say "*x > right_x" rather than >=, since on character
713 terminals, the vertical line's x coordinate is right_x. */
714 if (!w->pseudo_window_p
715 && !WINDOW_RIGHTMOST_P (w)
716 && *x > right_x - ux)
718 /* On the border on the right side of the window? Assume that
719 this area begins at RIGHT_X minus a canonical char width. */
720 return ON_VERTICAL_BORDER;
724 if (*x < text_left)
726 if (lmargin_width > 0
727 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
728 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
729 : (*x < left_x + lmargin_width)))
731 *x -= left_x;
732 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
733 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
734 *y -= top_y;
735 return ON_LEFT_MARGIN;
738 /* Convert X and Y to window-relative pixel coordinates. */
739 *x -= left_x;
740 *y -= top_y;
741 return ON_LEFT_FRINGE;
744 if (*x >= text_right)
746 if (rmargin_width > 0
747 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
748 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
749 : (*x >= right_x - rmargin_width)))
751 *x -= right_x - rmargin_width;
752 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
753 *x += WINDOW_RIGHT_FRINGE_WIDTH (w);
754 *y -= top_y;
755 return ON_RIGHT_MARGIN;
758 /* Convert X and Y to window-relative pixel coordinates. */
759 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
760 *y -= top_y;
761 return ON_RIGHT_FRINGE;
764 /* Everything special ruled out - must be on text area */
765 *x -= text_left;
766 *y -= top_y;
767 return ON_TEXT;
771 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
772 Scoordinates_in_window_p, 2, 2, 0,
773 doc: /* Return non-nil if COORDINATES are in WINDOW.
774 COORDINATES is a cons of the form (X . Y), X and Y being distances
775 measured in characters from the upper-left corner of the frame.
776 \(0 . 0) denotes the character in the upper left corner of the
777 frame.
778 If COORDINATES are in the text portion of WINDOW,
779 the coordinates relative to the window are returned.
780 If they are in the mode line of WINDOW, `mode-line' is returned.
781 If they are in the top mode line of WINDOW, `header-line' is returned.
782 If they are in the left fringe of WINDOW, `left-fringe' is returned.
783 If they are in the right fringe of WINDOW, `right-fringe' is returned.
784 If they are on the border between WINDOW and its right sibling,
785 `vertical-line' is returned.
786 If they are in the windows's left or right marginal areas, `left-margin'\n\
787 or `right-margin' is returned. */)
788 (coordinates, window)
789 register Lisp_Object coordinates, window;
791 struct window *w;
792 struct frame *f;
793 int x, y;
794 Lisp_Object lx, ly;
796 CHECK_WINDOW (window);
797 w = XWINDOW (window);
798 f = XFRAME (w->frame);
799 CHECK_CONS (coordinates);
800 lx = Fcar (coordinates);
801 ly = Fcdr (coordinates);
802 CHECK_NUMBER_OR_FLOAT (lx);
803 CHECK_NUMBER_OR_FLOAT (ly);
804 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
805 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
807 switch (coordinates_in_window (w, &x, &y))
809 case ON_NOTHING:
810 return Qnil;
812 case ON_TEXT:
813 /* X and Y are now window relative pixel coordinates. Convert
814 them to canonical char units before returning them. */
815 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
816 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
818 case ON_MODE_LINE:
819 return Qmode_line;
821 case ON_VERTICAL_BORDER:
822 return Qvertical_line;
824 case ON_HEADER_LINE:
825 return Qheader_line;
827 case ON_LEFT_FRINGE:
828 return Qleft_fringe;
830 case ON_RIGHT_FRINGE:
831 return Qright_fringe;
833 case ON_LEFT_MARGIN:
834 return Qleft_margin;
836 case ON_RIGHT_MARGIN:
837 return Qright_margin;
839 case ON_SCROLL_BAR:
840 /* Historically we are supposed to return nil in this case. */
841 return Qnil;
843 default:
844 abort ();
849 /* Callback for foreach_window, used in window_from_coordinates.
850 Check if window W contains coordinates specified by USER_DATA which
851 is actually a pointer to a struct check_window_data CW.
853 Check if window W contains coordinates *CW->x and *CW->y. If it
854 does, return W in *CW->window, as Lisp_Object, and return in
855 *CW->part the part of the window under coordinates *X,*Y. Return
856 zero from this function to stop iterating over windows. */
858 struct check_window_data
860 Lisp_Object *window;
861 int *x, *y;
862 enum window_part *part;
865 static int
866 check_window_containing (w, user_data)
867 struct window *w;
868 void *user_data;
870 struct check_window_data *cw = (struct check_window_data *) user_data;
871 enum window_part found;
872 int continue_p = 1;
874 found = coordinates_in_window (w, cw->x, cw->y);
875 if (found != ON_NOTHING)
877 *cw->part = found;
878 XSETWINDOW (*cw->window, w);
879 continue_p = 0;
882 return continue_p;
886 /* Find the window containing frame-relative pixel position X/Y and
887 return it as a Lisp_Object.
889 If X, Y is on one of the window's special `window_part' elements,
890 set *PART to the id of that element, and return X and Y converted
891 to window relative coordinates in WX and WY.
893 If there is no window under X, Y return nil and leave *PART
894 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
896 This function was previously implemented with a loop cycling over
897 windows with Fnext_window, and starting with the frame's selected
898 window. It turned out that this doesn't work with an
899 implementation of next_window using Vwindow_list, because
900 FRAME_SELECTED_WINDOW (F) is not always contained in the window
901 tree of F when this function is called asynchronously from
902 note_mouse_highlight. The original loop didn't terminate in this
903 case. */
905 Lisp_Object
906 window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
907 struct frame *f;
908 int x, y;
909 enum window_part *part;
910 int *wx, *wy;
911 int tool_bar_p;
913 Lisp_Object window;
914 struct check_window_data cw;
915 enum window_part dummy;
917 if (part == 0)
918 part = &dummy;
920 window = Qnil;
921 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
922 foreach_window (f, check_window_containing, &cw);
924 /* If not found above, see if it's in the tool bar window, if a tool
925 bar exists. */
926 if (NILP (window)
927 && tool_bar_p
928 && WINDOWP (f->tool_bar_window)
929 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
930 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
931 != ON_NOTHING))
933 *part = ON_TEXT;
934 window = f->tool_bar_window;
937 if (wx) *wx = x;
938 if (wy) *wy = y;
940 return window;
943 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
944 doc: /* Return window containing coordinates X and Y on FRAME.
945 If omitted, FRAME defaults to the currently selected frame.
946 The top left corner of the frame is considered to be row 0,
947 column 0. */)
948 (x, y, frame)
949 Lisp_Object x, y, frame;
951 struct frame *f;
953 if (NILP (frame))
954 frame = selected_frame;
955 CHECK_LIVE_FRAME (frame);
956 f = XFRAME (frame);
958 /* Check that arguments are integers or floats. */
959 CHECK_NUMBER_OR_FLOAT (x);
960 CHECK_NUMBER_OR_FLOAT (y);
962 return window_from_coordinates (f,
963 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
964 + FRAME_INTERNAL_BORDER_WIDTH (f)),
965 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
966 + FRAME_INTERNAL_BORDER_WIDTH (f)),
967 0, 0, 0, 0);
970 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
971 doc: /* Return current value of point in WINDOW.
972 For a nonselected window, this is the value point would have
973 if that window were selected.
975 Note that, when WINDOW is the selected window and its buffer
976 is also currently selected, the value returned is the same as (point).
977 It would be more strictly correct to return the `top-level' value
978 of point, outside of any save-excursion forms.
979 But that is hard to define. */)
980 (window)
981 Lisp_Object window;
983 register struct window *w = decode_window (window);
985 if (w == XWINDOW (selected_window)
986 && current_buffer == XBUFFER (w->buffer))
987 return Fpoint ();
988 return Fmarker_position (w->pointm);
991 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
992 doc: /* Return position at which display currently starts in WINDOW.
993 This is updated by redisplay or by calling `set-window-start'. */)
994 (window)
995 Lisp_Object window;
997 return Fmarker_position (decode_window (window)->start);
1000 /* This is text temporarily removed from the doc string below.
1002 This function returns nil if the position is not currently known.
1003 That happens when redisplay is preempted and doesn't finish.
1004 If in that case you want to compute where the end of the window would
1005 have been if redisplay had finished, do this:
1006 (save-excursion
1007 (goto-char (window-start window))
1008 (vertical-motion (1- (window-height window)) window)
1009 (point))") */
1011 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1012 doc: /* Return position at which display currently ends in WINDOW.
1013 This is updated by redisplay, when it runs to completion.
1014 Simply changing the buffer text or setting `window-start'
1015 does not update this value.
1016 Return nil if there is no recorded value. \(This can happen if the
1017 last redisplay of WINDOW was preempted, and did not finish.)
1018 If UPDATE is non-nil, compute the up-to-date position
1019 if it isn't already recorded. */)
1020 (window, update)
1021 Lisp_Object window, update;
1023 Lisp_Object value;
1024 struct window *w = decode_window (window);
1025 Lisp_Object buf;
1027 buf = w->buffer;
1028 CHECK_BUFFER (buf);
1030 #if 0 /* This change broke some things. We should make it later. */
1031 /* If we don't know the end position, return nil.
1032 The user can compute it with vertical-motion if he wants to.
1033 It would be nicer to do it automatically,
1034 but that's so slow that it would probably bother people. */
1035 if (NILP (w->window_end_valid))
1036 return Qnil;
1037 #endif
1039 if (! NILP (update)
1040 && ! (! NILP (w->window_end_valid)
1041 && XFASTINT (w->last_modified) >= MODIFF)
1042 && !noninteractive)
1044 struct text_pos startp;
1045 struct it it;
1046 struct buffer *old_buffer = NULL, *b = XBUFFER (buf);
1048 /* In case W->start is out of the range, use something
1049 reasonable. This situation occurred when loading a file with
1050 `-l' containing a call to `rmail' with subsequent other
1051 commands. At the end, W->start happened to be BEG, while
1052 rmail had already narrowed the buffer. */
1053 if (XMARKER (w->start)->charpos < BEGV)
1054 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1055 else if (XMARKER (w->start)->charpos > ZV)
1056 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1057 else
1058 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1060 /* Cannot use Fvertical_motion because that function doesn't
1061 cope with variable-height lines. */
1062 if (b != current_buffer)
1064 old_buffer = current_buffer;
1065 set_buffer_internal (b);
1068 start_display (&it, w, startp);
1069 move_it_vertically (&it, window_box_height (w));
1070 if (it.current_y < it.last_visible_y)
1071 move_it_past_eol (&it);
1072 value = make_number (IT_CHARPOS (it));
1074 if (old_buffer)
1075 set_buffer_internal (old_buffer);
1077 else
1078 XSETINT (value, BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
1080 return value;
1083 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1084 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1085 Return POS. */)
1086 (window, pos)
1087 Lisp_Object window, pos;
1089 register struct window *w = decode_window (window);
1091 CHECK_NUMBER_COERCE_MARKER (pos);
1092 if (w == XWINDOW (selected_window)
1093 && XBUFFER (w->buffer) == current_buffer)
1094 Fgoto_char (pos);
1095 else
1096 set_marker_restricted (w->pointm, pos, w->buffer);
1098 /* We have to make sure that redisplay updates the window to show
1099 the new value of point. */
1100 if (!EQ (window, selected_window))
1101 ++windows_or_buffers_changed;
1103 return pos;
1106 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1107 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1108 Return POS.
1109 Optional third arg NOFORCE non-nil inhibits next redisplay
1110 from overriding motion of point in order to display at this exact start. */)
1111 (window, pos, noforce)
1112 Lisp_Object window, pos, noforce;
1114 register struct window *w = decode_window (window);
1116 CHECK_NUMBER_COERCE_MARKER (pos);
1117 set_marker_restricted (w->start, pos, w->buffer);
1118 /* this is not right, but much easier than doing what is right. */
1119 w->start_at_line_beg = Qnil;
1120 if (NILP (noforce))
1121 w->force_start = Qt;
1122 w->update_mode_line = Qt;
1123 XSETFASTINT (w->last_modified, 0);
1124 XSETFASTINT (w->last_overlay_modified, 0);
1125 if (!EQ (window, selected_window))
1126 windows_or_buffers_changed++;
1128 return pos;
1131 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1132 1, 1, 0,
1133 doc: /* Return WINDOW's dedicated object, usually t or nil.
1134 See also `set-window-dedicated-p'. */)
1135 (window)
1136 Lisp_Object window;
1138 return decode_window (window)->dedicated;
1141 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1142 Sset_window_dedicated_p, 2, 2, 0,
1143 doc: /* Control whether WINDOW is dedicated to the buffer it displays.
1144 If it is dedicated, Emacs will not automatically change
1145 which buffer appears in it.
1146 The second argument is the new value for the dedication flag;
1147 non-nil means yes. */)
1148 (window, arg)
1149 Lisp_Object window, arg;
1151 register struct window *w = decode_window (window);
1153 w->dedicated = arg;
1155 return w->dedicated;
1158 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1159 0, 1, 0,
1160 doc: /* Return the display-table that WINDOW is using. */)
1161 (window)
1162 Lisp_Object window;
1164 return decode_window (window)->display_table;
1167 /* Get the display table for use on window W. This is either W's
1168 display table or W's buffer's display table. Ignore the specified
1169 tables if they are not valid; if no valid table is specified,
1170 return 0. */
1172 struct Lisp_Char_Table *
1173 window_display_table (w)
1174 struct window *w;
1176 struct Lisp_Char_Table *dp = NULL;
1178 if (DISP_TABLE_P (w->display_table))
1179 dp = XCHAR_TABLE (w->display_table);
1180 else if (BUFFERP (w->buffer))
1182 struct buffer *b = XBUFFER (w->buffer);
1184 if (DISP_TABLE_P (b->display_table))
1185 dp = XCHAR_TABLE (b->display_table);
1186 else if (DISP_TABLE_P (Vstandard_display_table))
1187 dp = XCHAR_TABLE (Vstandard_display_table);
1190 return dp;
1193 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1194 doc: /* Set WINDOW's display-table to TABLE. */)
1195 (window, table)
1196 register Lisp_Object window, table;
1198 register struct window *w;
1200 w = decode_window (window);
1201 w->display_table = table;
1202 return table;
1205 /* Record info on buffer window w is displaying
1206 when it is about to cease to display that buffer. */
1207 static void
1208 unshow_buffer (w)
1209 register struct window *w;
1211 Lisp_Object buf;
1212 struct buffer *b;
1214 buf = w->buffer;
1215 b = XBUFFER (buf);
1216 if (b != XMARKER (w->pointm)->buffer)
1217 abort ();
1219 #if 0
1220 if (w == XWINDOW (selected_window)
1221 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1222 /* Do this except when the selected window's buffer
1223 is being removed from some other window. */
1224 #endif
1225 /* last_window_start records the start position that this buffer
1226 had in the last window to be disconnected from it.
1227 Now that this statement is unconditional,
1228 it is possible for the buffer to be displayed in the
1229 selected window, while last_window_start reflects another
1230 window which was recently showing the same buffer.
1231 Some people might say that might be a good thing. Let's see. */
1232 b->last_window_start = marker_position (w->start);
1234 /* Point in the selected window's buffer
1235 is actually stored in that buffer, and the window's pointm isn't used.
1236 So don't clobber point in that buffer. */
1237 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1238 /* This line helps to fix Horsley's testbug.el bug. */
1239 && !(WINDOWP (b->last_selected_window)
1240 && w != XWINDOW (b->last_selected_window)
1241 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1242 temp_set_point_both (b,
1243 clip_to_bounds (BUF_BEGV (b),
1244 XMARKER (w->pointm)->charpos,
1245 BUF_ZV (b)),
1246 clip_to_bounds (BUF_BEGV_BYTE (b),
1247 marker_byte_position (w->pointm),
1248 BUF_ZV_BYTE (b)));
1250 if (WINDOWP (b->last_selected_window)
1251 && w == XWINDOW (b->last_selected_window))
1252 b->last_selected_window = Qnil;
1255 /* Put replacement into the window structure in place of old. */
1256 static void
1257 replace_window (old, replacement)
1258 Lisp_Object old, replacement;
1260 register Lisp_Object tem;
1261 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1263 /* If OLD is its frame's root_window, then replacement is the new
1264 root_window for that frame. */
1266 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1267 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1269 p->left_col = o->left_col;
1270 p->top_line = o->top_line;
1271 p->total_cols = o->total_cols;
1272 p->total_lines = o->total_lines;
1273 p->desired_matrix = p->current_matrix = 0;
1274 p->vscroll = 0;
1275 bzero (&p->cursor, sizeof (p->cursor));
1276 bzero (&p->last_cursor, sizeof (p->last_cursor));
1277 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1278 p->phys_cursor_type = -1;
1279 p->phys_cursor_width = -1;
1280 p->must_be_updated_p = 0;
1281 p->pseudo_window_p = 0;
1282 XSETFASTINT (p->window_end_vpos, 0);
1283 XSETFASTINT (p->window_end_pos, 0);
1284 p->window_end_valid = Qnil;
1285 p->frozen_window_start_p = 0;
1286 p->orig_top_line = p->orig_total_lines = Qnil;
1288 p->next = tem = o->next;
1289 if (!NILP (tem))
1290 XWINDOW (tem)->prev = replacement;
1292 p->prev = tem = o->prev;
1293 if (!NILP (tem))
1294 XWINDOW (tem)->next = replacement;
1296 p->parent = tem = o->parent;
1297 if (!NILP (tem))
1299 if (EQ (XWINDOW (tem)->vchild, old))
1300 XWINDOW (tem)->vchild = replacement;
1301 if (EQ (XWINDOW (tem)->hchild, old))
1302 XWINDOW (tem)->hchild = replacement;
1305 /*** Here, if replacement is a vertical combination
1306 and so is its new parent, we should make replacement's
1307 children be children of that parent instead. ***/
1310 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1311 doc: /* Remove WINDOW from the display. Default is selected window. */)
1312 (window)
1313 register Lisp_Object window;
1315 delete_window (window);
1317 if (! NILP (Vwindow_configuration_change_hook)
1318 && ! NILP (Vrun_hooks))
1319 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1321 return Qnil;
1324 void
1325 delete_window (window)
1326 register Lisp_Object window;
1328 register Lisp_Object tem, parent, sib;
1329 register struct window *p;
1330 register struct window *par;
1331 struct frame *f;
1333 /* Because this function is called by other C code on non-leaf
1334 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1335 so we can't decode_window here. */
1336 if (NILP (window))
1337 window = selected_window;
1338 else
1339 CHECK_WINDOW (window);
1340 p = XWINDOW (window);
1342 /* It's a no-op to delete an already-deleted window. */
1343 if (NILP (p->buffer)
1344 && NILP (p->hchild)
1345 && NILP (p->vchild))
1346 return;
1348 parent = p->parent;
1349 if (NILP (parent))
1350 error ("Attempt to delete minibuffer or sole ordinary window");
1351 par = XWINDOW (parent);
1353 windows_or_buffers_changed++;
1354 Vwindow_list = Qnil;
1355 f = XFRAME (WINDOW_FRAME (p));
1356 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1358 /* Are we trying to delete any frame's selected window? */
1360 Lisp_Object swindow, pwindow;
1362 /* See if the frame's selected window is either WINDOW
1363 or any subwindow of it, by finding all that window's parents
1364 and comparing each one with WINDOW. */
1365 swindow = FRAME_SELECTED_WINDOW (f);
1367 while (1)
1369 pwindow = swindow;
1370 while (!NILP (pwindow))
1372 if (EQ (window, pwindow))
1373 break;
1374 pwindow = XWINDOW (pwindow)->parent;
1377 /* If the window being deleted is not a parent of SWINDOW,
1378 then SWINDOW is ok as the new selected window. */
1379 if (!EQ (window, pwindow))
1380 break;
1381 /* Otherwise, try another window for SWINDOW. */
1382 swindow = Fnext_window (swindow, Qlambda, Qnil);;
1384 /* If we get back to the frame's selected window,
1385 it means there was no acceptable alternative,
1386 so we cannot delete. */
1387 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1388 error ("Cannot delete window");
1391 /* If we need to change SWINDOW, do it. */
1392 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1394 /* If we're about to delete the selected window on the
1395 selected frame, then we should use Fselect_window to select
1396 the new window. On the other hand, if we're about to
1397 delete the selected window on any other frame, we shouldn't do
1398 anything but set the frame's selected_window slot. */
1399 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1400 Fselect_window (swindow, Qnil);
1401 else
1402 FRAME_SELECTED_WINDOW (f) = swindow;
1406 /* Now we know we can delete this one. */
1407 window_deletion_count++;
1409 tem = p->buffer;
1410 /* tem is null for dummy parent windows
1411 (which have inferiors but not any contents themselves) */
1412 if (!NILP (tem))
1414 unshow_buffer (p);
1415 unchain_marker (XMARKER (p->pointm));
1416 unchain_marker (XMARKER (p->start));
1419 /* Free window glyph matrices. It is sure that they are allocated
1420 again when ADJUST_GLYPHS is called. Block input so that expose
1421 events and other events that access glyph matrices are not
1422 processed while we are changing them. */
1423 BLOCK_INPUT;
1424 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1426 tem = p->next;
1427 if (!NILP (tem))
1428 XWINDOW (tem)->prev = p->prev;
1430 tem = p->prev;
1431 if (!NILP (tem))
1432 XWINDOW (tem)->next = p->next;
1434 if (EQ (window, par->hchild))
1435 par->hchild = p->next;
1436 if (EQ (window, par->vchild))
1437 par->vchild = p->next;
1439 /* Find one of our siblings to give our space to. */
1440 sib = p->prev;
1441 if (NILP (sib))
1443 /* If p gives its space to its next sibling, that sibling needs
1444 to have its top/left side pulled back to where p's is.
1445 set_window_{height,width} will re-position the sibling's
1446 children. */
1447 sib = p->next;
1448 XWINDOW (sib)->top_line = p->top_line;
1449 XWINDOW (sib)->left_col = p->left_col;
1452 /* Stretch that sibling. */
1453 if (!NILP (par->vchild))
1454 set_window_height (sib,
1455 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1457 if (!NILP (par->hchild))
1458 set_window_width (sib,
1459 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1462 /* If parent now has only one child,
1463 put the child into the parent's place. */
1464 tem = par->hchild;
1465 if (NILP (tem))
1466 tem = par->vchild;
1467 if (NILP (XWINDOW (tem)->next)) {
1468 replace_window (parent, tem);
1469 par = XWINDOW (tem);
1472 /* Since we may be deleting combination windows, we must make sure that
1473 not only p but all its children have been marked as deleted. */
1474 if (! NILP (p->hchild))
1475 delete_all_subwindows (XWINDOW (p->hchild));
1476 else if (! NILP (p->vchild))
1477 delete_all_subwindows (XWINDOW (p->vchild));
1479 /* Mark this window as deleted. */
1480 p->buffer = p->hchild = p->vchild = Qnil;
1482 if (! NILP (par->parent))
1483 par = XWINDOW (par->parent);
1485 /* Check if we have a v/hchild with a v/hchild. In that case remove
1486 one of them. */
1488 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1490 p = XWINDOW (par->vchild);
1491 par->vchild = p->vchild;
1492 tem = p->vchild;
1494 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1496 p = XWINDOW (par->hchild);
1497 par->hchild = p->hchild;
1498 tem = p->hchild;
1500 else
1501 p = 0;
1503 if (p)
1505 while (! NILP (tem)) {
1506 XWINDOW (tem)->parent = p->parent;
1507 if (NILP (XWINDOW (tem)->next))
1508 break;
1509 tem = XWINDOW (tem)->next;
1511 if (! NILP (tem)) {
1512 /* The next of the v/hchild we are removing is now the next of the
1513 last child for the v/hchild:
1514 Before v/hchild -> v/hchild -> next1 -> next2
1516 -> next3
1517 After: v/hchild -> next1 -> next2 -> next3
1519 XWINDOW (tem)->next = p->next;
1520 if (! NILP (p->next))
1521 XWINDOW (p->next)->prev = tem;
1523 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1527 /* Adjust glyph matrices. */
1528 adjust_glyphs (f);
1529 UNBLOCK_INPUT;
1534 /***********************************************************************
1535 Window List
1536 ***********************************************************************/
1538 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1539 pointer. This is a callback function for foreach_window, used in
1540 function window_list. */
1542 static int
1543 add_window_to_list (w, user_data)
1544 struct window *w;
1545 void *user_data;
1547 Lisp_Object *list = (Lisp_Object *) user_data;
1548 Lisp_Object window;
1549 XSETWINDOW (window, w);
1550 *list = Fcons (window, *list);
1551 return 1;
1555 /* Return a list of all windows, for use by next_window. If
1556 Vwindow_list is a list, return that list. Otherwise, build a new
1557 list, cache it in Vwindow_list, and return that. */
1559 static Lisp_Object
1560 window_list ()
1562 if (!CONSP (Vwindow_list))
1564 Lisp_Object tail;
1566 Vwindow_list = Qnil;
1567 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1569 Lisp_Object args[2];
1571 /* We are visiting windows in canonical order, and add
1572 new windows at the front of args[1], which means we
1573 have to reverse this list at the end. */
1574 args[1] = Qnil;
1575 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1576 args[0] = Vwindow_list;
1577 args[1] = Fnreverse (args[1]);
1578 Vwindow_list = Fnconc (2, args);
1582 return Vwindow_list;
1586 /* Value is non-zero if WINDOW satisfies the constraints given by
1587 OWINDOW, MINIBUF and ALL_FRAMES.
1589 MINIBUF t means WINDOW may be minibuffer windows.
1590 `lambda' means WINDOW may not be a minibuffer window.
1591 a window means a specific minibuffer window
1593 ALL_FRAMES t means search all frames,
1594 nil means search just current frame,
1595 `visible' means search just visible frames,
1596 0 means search visible and iconified frames,
1597 a window means search the frame that window belongs to,
1598 a frame means consider windows on that frame, only. */
1600 static int
1601 candidate_window_p (window, owindow, minibuf, all_frames)
1602 Lisp_Object window, owindow, minibuf, all_frames;
1604 struct window *w = XWINDOW (window);
1605 struct frame *f = XFRAME (w->frame);
1606 int candidate_p = 1;
1608 if (!BUFFERP (w->buffer))
1609 candidate_p = 0;
1610 else if (MINI_WINDOW_P (w)
1611 && (EQ (minibuf, Qlambda)
1612 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1614 /* If MINIBUF is `lambda' don't consider any mini-windows.
1615 If it is a window, consider only that one. */
1616 candidate_p = 0;
1618 else if (EQ (all_frames, Qt))
1619 candidate_p = 1;
1620 else if (NILP (all_frames))
1622 xassert (WINDOWP (owindow));
1623 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1625 else if (EQ (all_frames, Qvisible))
1627 FRAME_SAMPLE_VISIBILITY (f);
1628 candidate_p = FRAME_VISIBLE_P (f);
1630 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1632 FRAME_SAMPLE_VISIBILITY (f);
1633 candidate_p = FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1635 else if (WINDOWP (all_frames))
1636 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1637 || EQ (XWINDOW (all_frames)->frame, w->frame)
1638 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1639 else if (FRAMEP (all_frames))
1640 candidate_p = EQ (all_frames, w->frame);
1642 return candidate_p;
1646 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1647 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1648 ALL_FRAMES. */
1650 static void
1651 decode_next_window_args (window, minibuf, all_frames)
1652 Lisp_Object *window, *minibuf, *all_frames;
1654 if (NILP (*window))
1655 *window = selected_window;
1656 else
1657 CHECK_LIVE_WINDOW (*window);
1659 /* MINIBUF nil may or may not include minibuffers. Decide if it
1660 does. */
1661 if (NILP (*minibuf))
1662 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1663 else if (!EQ (*minibuf, Qt))
1664 *minibuf = Qlambda;
1666 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1667 => count none of them, or a specific minibuffer window (the
1668 active one) to count. */
1670 /* ALL_FRAMES nil doesn't specify which frames to include. */
1671 if (NILP (*all_frames))
1672 *all_frames = (!EQ (*minibuf, Qlambda)
1673 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1674 : Qnil);
1675 else if (EQ (*all_frames, Qvisible))
1677 else if (EQ (*all_frames, make_number (0)))
1679 else if (FRAMEP (*all_frames))
1681 else if (!EQ (*all_frames, Qt))
1682 *all_frames = Qnil;
1684 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1685 search just current frame, `visible' meaning search just visible
1686 frames, 0 meaning search visible and iconified frames, or a
1687 window, meaning search the frame that window belongs to, or a
1688 frame, meaning consider windows on that frame, only. */
1692 /* Return the next or previous window of WINDOW in canonical ordering
1693 of windows. NEXT_P non-zero means return the next window. See the
1694 documentation string of next-window for the meaning of MINIBUF and
1695 ALL_FRAMES. */
1697 static Lisp_Object
1698 next_window (window, minibuf, all_frames, next_p)
1699 Lisp_Object window, minibuf, all_frames;
1700 int next_p;
1702 decode_next_window_args (&window, &minibuf, &all_frames);
1704 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1705 return the first window on the frame. */
1706 if (FRAMEP (all_frames)
1707 && !EQ (all_frames, XWINDOW (window)->frame))
1708 return Fframe_first_window (all_frames);
1710 if (next_p)
1712 Lisp_Object list;
1714 /* Find WINDOW in the list of all windows. */
1715 list = Fmemq (window, window_list ());
1717 /* Scan forward from WINDOW to the end of the window list. */
1718 if (CONSP (list))
1719 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1720 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1721 break;
1723 /* Scan from the start of the window list up to WINDOW. */
1724 if (!CONSP (list))
1725 for (list = Vwindow_list;
1726 CONSP (list) && !EQ (XCAR (list), window);
1727 list = XCDR (list))
1728 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1729 break;
1731 if (CONSP (list))
1732 window = XCAR (list);
1734 else
1736 Lisp_Object candidate, list;
1738 /* Scan through the list of windows for candidates. If there are
1739 candidate windows in front of WINDOW, the last one of these
1740 is the one we want. If there are candidates following WINDOW
1741 in the list, again the last one of these is the one we want. */
1742 candidate = Qnil;
1743 for (list = window_list (); CONSP (list); list = XCDR (list))
1745 if (EQ (XCAR (list), window))
1747 if (WINDOWP (candidate))
1748 break;
1750 else if (candidate_window_p (XCAR (list), window, minibuf,
1751 all_frames))
1752 candidate = XCAR (list);
1755 if (WINDOWP (candidate))
1756 window = candidate;
1759 return window;
1763 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1764 doc: /* Return next window after WINDOW in canonical ordering of windows.
1765 If omitted, WINDOW defaults to the selected window.
1767 Optional second arg MINIBUF t means count the minibuffer window even
1768 if not active. MINIBUF nil or omitted means count the minibuffer iff
1769 it is active. MINIBUF neither t nor nil means not to count the
1770 minibuffer even if it is active.
1772 Several frames may share a single minibuffer; if the minibuffer
1773 counts, all windows on all frames that share that minibuffer count
1774 too. Therefore, `next-window' can be used to iterate through the
1775 set of windows even when the minibuffer is on another frame. If the
1776 minibuffer does not count, only windows from WINDOW's frame count.
1778 Optional third arg ALL-FRAMES t means include windows on all frames.
1779 ALL-FRAMES nil or omitted means cycle within the frames as specified
1780 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1781 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1782 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1783 Anything else means restrict to WINDOW's frame.
1785 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1786 `next-window' to iterate through the entire cycle of acceptable
1787 windows, eventually ending up back at the window you started with.
1788 `previous-window' traverses the same cycle, in the reverse order. */)
1789 (window, minibuf, all_frames)
1790 Lisp_Object window, minibuf, all_frames;
1792 return next_window (window, minibuf, all_frames, 1);
1796 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1797 doc: /* Return the window preceding WINDOW in canonical ordering of windows.
1798 If omitted, WINDOW defaults to the selected window.
1800 Optional second arg MINIBUF t means count the minibuffer window even
1801 if not active. MINIBUF nil or omitted means count the minibuffer iff
1802 it is active. MINIBUF neither t nor nil means not to count the
1803 minibuffer even if it is active.
1805 Several frames may share a single minibuffer; if the minibuffer
1806 counts, all windows on all frames that share that minibuffer count
1807 too. Therefore, `previous-window' can be used to iterate through
1808 the set of windows even when the minibuffer is on another frame. If
1809 the minibuffer does not count, only windows from WINDOW's frame count
1811 Optional third arg ALL-FRAMES t means include windows on all frames.
1812 ALL-FRAMES nil or omitted means cycle within the frames as specified
1813 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1814 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1815 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1816 Anything else means restrict to WINDOW's frame.
1818 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1819 `previous-window' to iterate through the entire cycle of acceptable
1820 windows, eventually ending up back at the window you started with.
1821 `next-window' traverses the same cycle, in the reverse order. */)
1822 (window, minibuf, all_frames)
1823 Lisp_Object window, minibuf, all_frames;
1825 return next_window (window, minibuf, all_frames, 0);
1829 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1830 doc: /* Select the ARG'th different window on this frame.
1831 All windows on current frame are arranged in a cyclic order.
1832 This command selects the window ARG steps away in that order.
1833 A negative ARG moves in the opposite order. The optional second
1834 argument ALL_FRAMES has the same meaning as in `next-window', which see. */)
1835 (arg, all_frames)
1836 Lisp_Object arg, all_frames;
1838 Lisp_Object window;
1839 int i;
1841 CHECK_NUMBER (arg);
1842 window = selected_window;
1844 for (i = XINT (arg); i > 0; --i)
1845 window = Fnext_window (window, Qnil, all_frames);
1846 for (; i < 0; ++i)
1847 window = Fprevious_window (window, Qnil, all_frames);
1849 Fselect_window (window, Qnil);
1850 return Qnil;
1854 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
1855 doc: /* Return a list of windows on FRAME, starting with WINDOW.
1856 FRAME nil or omitted means use the selected frame.
1857 WINDOW nil or omitted means use the selected window.
1858 MINIBUF t means include the minibuffer window, even if it isn't active.
1859 MINIBUF nil or omitted means include the minibuffer window only
1860 if it's active.
1861 MINIBUF neither nil nor t means never include the minibuffer window. */)
1862 (frame, minibuf, window)
1863 Lisp_Object frame, minibuf, window;
1865 if (NILP (window))
1866 window = selected_window;
1867 if (NILP (frame))
1868 frame = selected_frame;
1870 if (!EQ (frame, XWINDOW (window)->frame))
1871 error ("Window is on a different frame");
1873 return window_list_1 (window, minibuf, frame);
1877 /* Return a list of windows in canonical ordering. Arguments are like
1878 for `next-window'. */
1880 static Lisp_Object
1881 window_list_1 (window, minibuf, all_frames)
1882 Lisp_Object window, minibuf, all_frames;
1884 Lisp_Object tail, list, rest;
1886 decode_next_window_args (&window, &minibuf, &all_frames);
1887 list = Qnil;
1889 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
1890 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
1891 list = Fcons (XCAR (tail), list);
1893 /* Rotate the list to start with WINDOW. */
1894 list = Fnreverse (list);
1895 rest = Fmemq (window, list);
1896 if (!NILP (rest) && !EQ (rest, list))
1898 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
1900 XSETCDR (tail, Qnil);
1901 list = nconc2 (rest, list);
1903 return list;
1908 /* Look at all windows, performing an operation specified by TYPE
1909 with argument OBJ.
1910 If FRAMES is Qt, look at all frames;
1911 Qnil, look at just the selected frame;
1912 Qvisible, look at visible frames;
1913 a frame, just look at windows on that frame.
1914 If MINI is non-zero, perform the operation on minibuffer windows too. */
1916 enum window_loop
1918 WINDOW_LOOP_UNUSED,
1919 GET_BUFFER_WINDOW, /* Arg is buffer */
1920 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1921 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1922 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1923 GET_LARGEST_WINDOW,
1924 UNSHOW_BUFFER, /* Arg is buffer */
1925 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
1926 CHECK_ALL_WINDOWS
1929 static Lisp_Object
1930 window_loop (type, obj, mini, frames)
1931 enum window_loop type;
1932 Lisp_Object obj, frames;
1933 int mini;
1935 Lisp_Object window, windows, best_window, frame_arg;
1936 struct frame *f;
1937 struct gcpro gcpro1;
1939 /* If we're only looping through windows on a particular frame,
1940 frame points to that frame. If we're looping through windows
1941 on all frames, frame is 0. */
1942 if (FRAMEP (frames))
1943 f = XFRAME (frames);
1944 else if (NILP (frames))
1945 f = SELECTED_FRAME ();
1946 else
1947 f = NULL;
1949 if (f)
1950 frame_arg = Qlambda;
1951 else if (EQ (frames, make_number (0)))
1952 frame_arg = frames;
1953 else if (EQ (frames, Qvisible))
1954 frame_arg = frames;
1955 else
1956 frame_arg = Qt;
1958 /* frame_arg is Qlambda to stick to one frame,
1959 Qvisible to consider all visible frames,
1960 or Qt otherwise. */
1962 /* Pick a window to start with. */
1963 if (WINDOWP (obj))
1964 window = obj;
1965 else if (f)
1966 window = FRAME_SELECTED_WINDOW (f);
1967 else
1968 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
1970 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
1971 GCPRO1 (windows);
1972 best_window = Qnil;
1974 for (; CONSP (windows); windows = XCDR (windows))
1976 struct window *w;
1978 window = XCAR (windows);
1979 w = XWINDOW (window);
1981 /* Note that we do not pay attention here to whether the frame
1982 is visible, since Fwindow_list skips non-visible frames if
1983 that is desired, under the control of frame_arg. */
1984 if (!MINI_WINDOW_P (w)
1985 /* For UNSHOW_BUFFER, we must always consider all windows. */
1986 || type == UNSHOW_BUFFER
1987 || (mini && minibuf_level > 0))
1988 switch (type)
1990 case GET_BUFFER_WINDOW:
1991 if (EQ (w->buffer, obj)
1992 /* Don't find any minibuffer window
1993 except the one that is currently in use. */
1994 && (MINI_WINDOW_P (w)
1995 ? EQ (window, minibuf_window)
1996 : 1))
1998 if (NILP (best_window))
1999 best_window = window;
2000 else if (EQ (window, selected_window))
2001 /* For compatibility with 20.x, prefer to return
2002 selected-window. */
2003 best_window = window;
2005 break;
2007 case GET_LRU_WINDOW:
2008 /* `obj' is an integer encoding a bitvector.
2009 `obj & 1' means consider only full-width windows.
2010 `obj & 2' means consider also dedicated windows. */
2011 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2012 || (!(XINT (obj) & 2) && EQ (w->dedicated, Qt))
2013 /* Minibuffer windows are always ignored. */
2014 || MINI_WINDOW_P (w))
2015 break;
2016 if (NILP (best_window)
2017 || (XFASTINT (XWINDOW (best_window)->use_time)
2018 > XFASTINT (w->use_time)))
2019 best_window = window;
2020 break;
2022 case DELETE_OTHER_WINDOWS:
2023 if (!EQ (window, obj))
2024 Fdelete_window (window);
2025 break;
2027 case DELETE_BUFFER_WINDOWS:
2028 if (EQ (w->buffer, obj))
2030 struct frame *f = XFRAME (WINDOW_FRAME (w));
2032 /* If this window is dedicated, and in a frame of its own,
2033 kill the frame. */
2034 if (EQ (window, FRAME_ROOT_WINDOW (f))
2035 && !NILP (w->dedicated)
2036 && other_visible_frames (f))
2038 /* Skip the other windows on this frame.
2039 There might be one, the minibuffer! */
2040 while (CONSP (XCDR (windows))
2041 && EQ (XWINDOW (XCAR (windows))->frame,
2042 XWINDOW (XCAR (XCDR (windows)))->frame))
2043 windows = XCDR (windows);
2045 /* Now we can safely delete the frame. */
2046 Fdelete_frame (w->frame, Qnil);
2048 else if (NILP (w->parent))
2050 /* If we're deleting the buffer displayed in the
2051 only window on the frame, find a new buffer to
2052 display there. */
2053 Lisp_Object buffer;
2054 buffer = Fother_buffer (obj, Qnil, w->frame);
2055 Fset_window_buffer (window, buffer, Qnil);
2056 if (EQ (window, selected_window))
2057 Fset_buffer (w->buffer);
2059 else
2060 Fdelete_window (window);
2062 break;
2064 case GET_LARGEST_WINDOW:
2065 { /* nil `obj' means to ignore dedicated windows. */
2066 /* Ignore dedicated windows and minibuffers. */
2067 if (MINI_WINDOW_P (w) || (NILP (obj) && EQ (w->dedicated, Qt)))
2068 break;
2070 if (NILP (best_window))
2071 best_window = window;
2072 else
2074 struct window *b = XWINDOW (best_window);
2075 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2076 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2077 best_window = window;
2080 break;
2082 case UNSHOW_BUFFER:
2083 if (EQ (w->buffer, obj))
2085 Lisp_Object buffer;
2086 struct frame *f = XFRAME (w->frame);
2088 /* Find another buffer to show in this window. */
2089 buffer = Fother_buffer (obj, Qnil, w->frame);
2091 /* If this window is dedicated, and in a frame of its own,
2092 kill the frame. */
2093 if (EQ (window, FRAME_ROOT_WINDOW (f))
2094 && !NILP (w->dedicated)
2095 && other_visible_frames (f))
2097 /* Skip the other windows on this frame.
2098 There might be one, the minibuffer! */
2099 while (CONSP (XCDR (windows))
2100 && EQ (XWINDOW (XCAR (windows))->frame,
2101 XWINDOW (XCAR (XCDR (windows)))->frame))
2102 windows = XCDR (windows);
2104 /* Now we can safely delete the frame. */
2105 Fdelete_frame (w->frame, Qnil);
2107 else if (!NILP (w->dedicated) && !NILP (w->parent))
2109 Lisp_Object window;
2110 XSETWINDOW (window, w);
2111 /* If this window is dedicated and not the only window
2112 in its frame, then kill it. */
2113 Fdelete_window (window);
2115 else
2117 /* Otherwise show a different buffer in the window. */
2118 w->dedicated = Qnil;
2119 Fset_window_buffer (window, buffer, Qnil);
2120 if (EQ (window, selected_window))
2121 Fset_buffer (w->buffer);
2124 break;
2126 case REDISPLAY_BUFFER_WINDOWS:
2127 if (EQ (w->buffer, obj))
2129 mark_window_display_accurate (window, 0);
2130 w->update_mode_line = Qt;
2131 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2132 ++update_mode_lines;
2133 best_window = window;
2135 break;
2137 /* Check for a window that has a killed buffer. */
2138 case CHECK_ALL_WINDOWS:
2139 if (! NILP (w->buffer)
2140 && NILP (XBUFFER (w->buffer)->name))
2141 abort ();
2142 break;
2144 case WINDOW_LOOP_UNUSED:
2145 break;
2149 UNGCPRO;
2150 return best_window;
2153 /* Used for debugging. Abort if any window has a dead buffer. */
2155 void
2156 check_all_windows ()
2158 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2161 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2162 doc: /* Return the window least recently selected or used for display.
2163 Return a full-width window if possible.
2164 A minibuffer window is never a candidate.
2165 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2166 so if all windows are dedicated, the value is nil.
2167 If optional argument FRAME is `visible', search all visible frames.
2168 If FRAME is 0, search all visible and iconified frames.
2169 If FRAME is t, search all frames.
2170 If FRAME is nil, search only the selected frame.
2171 If FRAME is a frame, search only that frame. */)
2172 (frame, dedicated)
2173 Lisp_Object frame, dedicated;
2175 register Lisp_Object w;
2176 /* First try for a window that is full-width */
2177 w = window_loop (GET_LRU_WINDOW,
2178 NILP (dedicated) ? make_number (1) : make_number (3),
2179 0, frame);
2180 if (!NILP (w) && !EQ (w, selected_window))
2181 return w;
2182 /* If none of them, try the rest */
2183 return window_loop (GET_LRU_WINDOW,
2184 NILP (dedicated) ? make_number (0) : make_number (2),
2185 0, frame);
2188 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2189 doc: /* Return the largest window in area.
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 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2202 frame);
2205 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
2206 doc: /* Return a window currently displaying BUFFER, or nil if none.
2207 BUFFER can be a buffer or a buffer name.
2208 If optional argument FRAME is `visible', search all visible frames.
2209 If optional argument FRAME is 0, search all visible and iconified frames.
2210 If FRAME is t, search all frames.
2211 If FRAME is nil, search only the selected frame.
2212 If FRAME is a frame, search only that frame. */)
2213 (buffer, frame)
2214 Lisp_Object buffer, frame;
2216 buffer = Fget_buffer (buffer);
2217 if (BUFFERP (buffer))
2218 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2219 else
2220 return Qnil;
2223 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2224 0, 1, "",
2225 doc: /* Make WINDOW (or the selected window) fill its frame.
2226 Only the frame WINDOW is on is affected.
2227 This function tries to reduce display jumps
2228 by keeping the text previously visible in WINDOW
2229 in the same place on the frame. Doing this depends on
2230 the value of (window-start WINDOW), so if calling this function
2231 in a program gives strange scrolling, make sure the window-start
2232 value is reasonable when this function is called. */)
2233 (window)
2234 Lisp_Object window;
2236 struct window *w;
2237 int startpos;
2238 int top, new_top;
2240 if (NILP (window))
2241 window = selected_window;
2242 else
2243 CHECK_LIVE_WINDOW (window);
2244 w = XWINDOW (window);
2246 startpos = marker_position (w->start);
2247 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2249 if (MINI_WINDOW_P (w) && top > 0)
2250 error ("Can't expand minibuffer to full frame");
2252 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2254 /* Try to minimize scrolling, by setting the window start to the point
2255 will cause the text at the old window start to be at the same place
2256 on the frame. But don't try to do this if the window start is
2257 outside the visible portion (as might happen when the display is
2258 not current, due to typeahead). */
2259 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2260 if (new_top != top
2261 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2262 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2264 struct position pos;
2265 struct buffer *obuf = current_buffer;
2267 Fset_buffer (w->buffer);
2268 /* This computation used to temporarily move point, but that can
2269 have unwanted side effects due to text properties. */
2270 pos = *vmotion (startpos, -top, w);
2272 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2273 w->window_end_valid = Qnil;
2274 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2275 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2276 : Qnil);
2277 /* We need to do this, so that the window-scroll-functions
2278 get called. */
2279 w->optional_new_start = Qt;
2281 set_buffer_internal (obuf);
2284 return Qnil;
2287 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2288 1, 2, "bDelete windows on (buffer): ",
2289 doc: /* Delete all windows showing BUFFER.
2290 BUFFER must be a buffer or the name of an existing buffer.
2291 Optional second argument FRAME controls which frames are affected.
2292 If optional argument FRAME is `visible', search all visible frames.
2293 If FRAME is 0, search all visible and iconified frames.
2294 If FRAME is nil, search all frames.
2295 If FRAME is t, search only the selected frame.
2296 If FRAME is a frame, search only that frame. */)
2297 (buffer, frame)
2298 Lisp_Object buffer, frame;
2300 /* FRAME uses t and nil to mean the opposite of what window_loop
2301 expects. */
2302 if (NILP (frame))
2303 frame = Qt;
2304 else if (EQ (frame, Qt))
2305 frame = Qnil;
2307 if (!NILP (buffer))
2309 buffer = Fget_buffer (buffer);
2310 CHECK_BUFFER (buffer);
2311 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2314 return Qnil;
2317 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2318 Sreplace_buffer_in_windows,
2319 1, 1, "bReplace buffer in windows: ",
2320 doc: /* Replace BUFFER with some other buffer in all windows showing it.
2321 BUFFER may be a buffer or the name of an existing buffer. */)
2322 (buffer)
2323 Lisp_Object buffer;
2325 if (!NILP (buffer))
2327 buffer = Fget_buffer (buffer);
2328 CHECK_BUFFER (buffer);
2329 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2331 return Qnil;
2334 /* Replace BUFFER with some other buffer in all windows
2335 of all frames, even those on other keyboards. */
2337 void
2338 replace_buffer_in_all_windows (buffer)
2339 Lisp_Object buffer;
2341 #ifdef MULTI_KBOARD
2342 Lisp_Object tail, frame;
2344 /* A single call to window_loop won't do the job
2345 because it only considers frames on the current keyboard.
2346 So loop manually over frames, and handle each one. */
2347 FOR_EACH_FRAME (tail, frame)
2348 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2349 #else
2350 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
2351 #endif
2354 /* Set the height of WINDOW and all its inferiors. */
2356 /* The smallest acceptable dimensions for a window. Anything smaller
2357 might crash Emacs. */
2359 #define MIN_SAFE_WINDOW_WIDTH (2)
2360 #define MIN_SAFE_WINDOW_HEIGHT (1)
2362 /* Make sure that window_min_height and window_min_width are
2363 not too small; if they are, set them to safe minima. */
2365 static void
2366 check_min_window_sizes ()
2368 /* Smaller values might permit a crash. */
2369 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
2370 window_min_width = MIN_SAFE_WINDOW_WIDTH;
2371 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
2372 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
2375 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2376 minimum allowable size. */
2378 void
2379 check_frame_size (frame, rows, cols)
2380 FRAME_PTR frame;
2381 int *rows, *cols;
2383 /* For height, we have to see:
2384 how many windows the frame has at minimum (one or two),
2385 and whether it has a menu bar or other special stuff at the top. */
2386 int min_height
2387 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2388 ? MIN_SAFE_WINDOW_HEIGHT
2389 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2391 if (FRAME_TOP_MARGIN (frame) > 0)
2392 min_height += FRAME_TOP_MARGIN (frame);
2394 if (*rows < min_height)
2395 *rows = min_height;
2396 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2397 *cols = MIN_SAFE_WINDOW_WIDTH;
2401 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2402 check if W's width can be changed, otherwise check W's height.
2403 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2404 siblings, too. If none of the siblings is resizable, WINDOW isn't
2405 either. */
2407 static int
2408 window_fixed_size_p (w, width_p, check_siblings_p)
2409 struct window *w;
2410 int width_p, check_siblings_p;
2412 int fixed_p;
2413 struct window *c;
2415 if (!NILP (w->hchild))
2417 c = XWINDOW (w->hchild);
2419 if (width_p)
2421 /* A horiz. combination is fixed-width if all of if its
2422 children are. */
2423 while (c && window_fixed_size_p (c, width_p, 0))
2424 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2425 fixed_p = c == NULL;
2427 else
2429 /* A horiz. combination is fixed-height if one of if its
2430 children is. */
2431 while (c && !window_fixed_size_p (c, width_p, 0))
2432 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2433 fixed_p = c != NULL;
2436 else if (!NILP (w->vchild))
2438 c = XWINDOW (w->vchild);
2440 if (width_p)
2442 /* A vert. combination is fixed-width if one of if its
2443 children is. */
2444 while (c && !window_fixed_size_p (c, width_p, 0))
2445 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2446 fixed_p = c != NULL;
2448 else
2450 /* A vert. combination is fixed-height if all of if its
2451 children are. */
2452 while (c && window_fixed_size_p (c, width_p, 0))
2453 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2454 fixed_p = c == NULL;
2457 else if (BUFFERP (w->buffer))
2459 struct buffer *old = current_buffer;
2460 Lisp_Object val;
2462 current_buffer = XBUFFER (w->buffer);
2463 val = find_symbol_value (Qwindow_size_fixed);
2464 current_buffer = old;
2466 fixed_p = 0;
2467 if (!EQ (val, Qunbound))
2469 fixed_p = !NILP (val);
2471 if (fixed_p
2472 && ((EQ (val, Qheight) && width_p)
2473 || (EQ (val, Qwidth) && !width_p)))
2474 fixed_p = 0;
2477 /* Can't tell if this one is resizable without looking at
2478 siblings. If all siblings are fixed-size this one is too. */
2479 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2481 Lisp_Object child;
2483 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2484 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2485 break;
2487 if (NILP (child))
2488 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2489 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2490 break;
2492 if (NILP (child))
2493 fixed_p = 1;
2496 else
2497 fixed_p = 1;
2499 return fixed_p;
2503 /* Return the minimum size of window W, not taking fixed-width windows
2504 into account. WIDTH_P non-zero means return the minimum width,
2505 otherwise return the minimum height. If W is a combination window,
2506 compute the minimum size from the minimum sizes of W's children. */
2508 static int
2509 window_min_size_1 (w, width_p)
2510 struct window *w;
2511 int width_p;
2513 struct window *c;
2514 int size;
2516 if (!NILP (w->hchild))
2518 c = XWINDOW (w->hchild);
2519 size = 0;
2521 if (width_p)
2523 /* The min width of a horizontal combination is
2524 the sum of the min widths of its children. */
2525 while (c)
2527 size += window_min_size_1 (c, width_p);
2528 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2531 else
2533 /* The min height a horizontal combination equals
2534 the maximum of all min height of its children. */
2535 while (c)
2537 int min_size = window_min_size_1 (c, width_p);
2538 size = max (min_size, size);
2539 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2543 else if (!NILP (w->vchild))
2545 c = XWINDOW (w->vchild);
2546 size = 0;
2548 if (width_p)
2550 /* The min width of a vertical combination is
2551 the maximum of the min widths of its children. */
2552 while (c)
2554 int min_size = window_min_size_1 (c, width_p);
2555 size = max (min_size, size);
2556 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2559 else
2561 /* The min height of a vertical combination equals
2562 the sum of the min height of its children. */
2563 while (c)
2565 size += window_min_size_1 (c, width_p);
2566 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2570 else
2572 if (width_p)
2573 size = window_min_width;
2574 else
2576 if (MINI_WINDOW_P (w)
2577 || (!WINDOW_WANTS_MODELINE_P (w)
2578 && !WINDOW_WANTS_HEADER_LINE_P (w)))
2579 size = 1;
2580 else
2581 size = window_min_height;
2585 return size;
2589 /* Return the minimum size of window W, taking fixed-size windows into
2590 account. WIDTH_P non-zero means return the minimum width,
2591 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2592 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2593 unless FIXED is null. */
2595 static int
2596 window_min_size (w, width_p, ignore_fixed_p, fixed)
2597 struct window *w;
2598 int width_p, ignore_fixed_p, *fixed;
2600 int size, fixed_p;
2602 if (ignore_fixed_p)
2603 fixed_p = 0;
2604 else
2605 fixed_p = window_fixed_size_p (w, width_p, 1);
2607 if (fixed)
2608 *fixed = fixed_p;
2610 if (fixed_p)
2611 size = width_p ? XFASTINT (w->total_cols) : XFASTINT (w->total_lines);
2612 else
2613 size = window_min_size_1 (w, width_p);
2615 return size;
2619 /* Adjust the margins of window W if text area is too small.
2620 Return 1 if window width is ok after adjustment; 0 if window
2621 is still too narrow. */
2623 static int
2624 adjust_window_margins (w)
2625 struct window *w;
2627 int box_cols = (WINDOW_TOTAL_COLS (w)
2628 - WINDOW_FRINGE_COLS (w)
2629 - WINDOW_SCROLL_BAR_COLS (w));
2630 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2631 + WINDOW_RIGHT_MARGIN_COLS (w));
2633 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2634 return 1;
2636 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2637 return 0;
2639 /* Window's text area is too narrow, but reducing the window
2640 margins will fix that. */
2641 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2642 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2644 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2645 w->left_margin_cols = w->right_margin_cols
2646 = make_number (margin_cols/2);
2647 else
2648 w->right_margin_cols = make_number (margin_cols);
2650 else
2651 w->left_margin_cols = make_number (margin_cols);
2652 return 1;
2655 /* Calculate new sizes for windows in the list FORWARD when the window size
2656 goes from TOTAL to SIZE. TOTAL must be greater than SIZE.
2657 The number of windows in FORWARD is NCHILDREN, and the number that
2658 can shrink is SHRINKABLE.
2659 The minimum size a window can have is MIN_SIZE.
2660 If we are shrinking fixed windows, RESIZE_FIXED_P is non-zero.
2661 If we are shrinking columns, WIDTH_P is non-zero, otherwise we are
2662 shrinking rows.
2664 This function returns an allocated array of new sizes that the caller
2665 must free. The size -1 means the window is fixed and RESIZE_FIXED_P
2666 is zero. Array index 0 refers to the first window in FORWARD, 1 to
2667 the second, and so on.
2669 This function tries to keep windows at least at the minimum size
2670 and resize other windows before it resizes any window to zero (i.e.
2671 delete that window).
2673 Windows are resized proportional to their size, so bigger windows
2674 shrink more than smaller windows. */
2675 static int *
2676 shrink_windows (total, size, nchildren, shrinkable,
2677 min_size, resize_fixed_p, forward, width_p)
2678 int total, size, nchildren, shrinkable, min_size;
2679 int resize_fixed_p, width_p;
2680 Lisp_Object forward;
2682 int available_resize = 0;
2683 int *new_sizes;
2684 struct window *c;
2685 Lisp_Object child;
2686 int smallest = total;
2687 int total_removed = 0;
2688 int total_shrink = total - size;
2689 int i;
2691 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2693 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2695 int child_size;
2697 c = XWINDOW (child);
2698 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2700 if (! resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2701 new_sizes[i] = -1;
2702 else
2704 new_sizes[i] = child_size;
2705 if (child_size > min_size)
2706 available_resize += child_size - min_size;
2709 /* We might need to shrink some windows to zero. Find the smallest
2710 windows and set them to 0 until we can fulfil the new size. */
2712 while (shrinkable > 1 && size + available_resize < total)
2714 for (i = 0; i < nchildren; ++i)
2715 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2716 smallest = new_sizes[i];
2718 for (i = 0; i < nchildren; ++i)
2719 if (new_sizes[i] == smallest)
2721 /* Resize this window down to zero. */
2722 new_sizes[i] = 0;
2723 if (smallest > min_size)
2724 available_resize -= smallest - min_size;
2725 available_resize += smallest;
2726 --shrinkable;
2727 total_removed += smallest;
2729 /* We don't know what the smallest is now. */
2730 smallest = total;
2732 /* Out of for, just remove one window at the time and
2733 check again if we have enough space. */
2734 break;
2738 /* Now, calculate the new sizes. Try to shrink each window
2739 proportional to its size. */
2740 for (i = 0; i < nchildren; ++i)
2742 if (new_sizes[i] > min_size)
2744 int to_shrink = total_shrink*new_sizes[i]/total;
2745 if (new_sizes[i] - to_shrink < min_size)
2746 to_shrink = new_sizes[i] - min_size;
2747 new_sizes[i] -= to_shrink;
2748 total_removed += to_shrink;
2752 /* Any reminder due to rounding, we just subtract from windows
2753 that are left and still can be shrunk. */
2754 while (total_shrink > total_removed)
2756 int nonzero_sizes = 0;
2757 int nonzero_idx = -1;
2759 for (i = 0; i < nchildren; ++i)
2760 if (new_sizes[i] > 0)
2762 ++nonzero_sizes;
2763 nonzero_idx = i;
2766 for (i = 0; i < nchildren; ++i)
2767 if (new_sizes[i] > min_size)
2769 --new_sizes[i];
2770 ++total_removed;
2772 /* Out of for, just shrink one window at the time and
2773 check again if we have enough space. */
2774 break;
2778 /* Special case, only one window left. */
2779 if (nonzero_sizes == 1)
2780 break;
2783 /* Any surplus due to rounding, we add to windows that are left. */
2784 while (total_shrink < total_removed)
2786 for (i = 0; i < nchildren; ++i)
2788 if (new_sizes[i] != 0 && total_shrink < total_removed)
2790 ++new_sizes[i];
2791 --total_removed;
2792 break;
2797 return new_sizes;
2800 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2801 WINDOW's width. Resize WINDOW's children, if any, so that they
2802 keep their proportionate size relative to WINDOW. Propagate
2803 WINDOW's top or left edge position to children. Delete windows
2804 that become too small unless NODELETE_P is non-zero.
2806 If NODELETE_P is 2, that means we do delete windows that are
2807 too small, even if they were too small before! */
2809 static void
2810 size_window (window, size, width_p, nodelete_p)
2811 Lisp_Object window;
2812 int size, width_p, nodelete_p;
2814 struct window *w = XWINDOW (window);
2815 struct window *c;
2816 Lisp_Object child, *forward, *sideward;
2817 int old_size, min_size, safe_min_size;
2819 /* We test nodelete_p != 2 and nodelete_p != 1 below, so it
2820 seems like it's too soon to do this here. ++KFS. */
2821 if (nodelete_p == 2)
2822 nodelete_p = 0;
2824 check_min_window_sizes ();
2825 size = max (0, size);
2827 /* If the window has been "too small" at one point,
2828 don't delete it for being "too small" in the future.
2829 Preserve it as long as that is at all possible. */
2830 if (width_p)
2832 old_size = WINDOW_TOTAL_COLS (w);
2833 min_size = window_min_width;
2834 /* Ensure that there is room for the scroll bar and fringes!
2835 We may reduce display margins though. */
2836 safe_min_size = (MIN_SAFE_WINDOW_WIDTH
2837 + WINDOW_FRINGE_COLS (w)
2838 + WINDOW_SCROLL_BAR_COLS (w));
2840 else
2842 old_size = XINT (w->total_lines);
2843 min_size = window_min_height;
2844 safe_min_size = MIN_SAFE_WINDOW_HEIGHT;
2847 if (old_size < min_size && nodelete_p != 2)
2848 w->too_small_ok = Qt;
2850 /* Maybe delete WINDOW if it's too small. */
2851 if (nodelete_p != 1 && !NILP (w->parent))
2853 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
2854 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
2855 if (min_size < safe_min_size)
2856 min_size = safe_min_size;
2857 if (size < min_size)
2859 delete_window (window);
2860 return;
2864 /* Set redisplay hints. */
2865 w->last_modified = make_number (0);
2866 w->last_overlay_modified = make_number (0);
2867 windows_or_buffers_changed++;
2868 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
2870 if (width_p)
2872 sideward = &w->vchild;
2873 forward = &w->hchild;
2874 w->total_cols = make_number (size);
2875 adjust_window_margins (w);
2877 else
2879 sideward = &w->hchild;
2880 forward = &w->vchild;
2881 w->total_lines = make_number (size);
2882 w->orig_total_lines = Qnil;
2885 if (!NILP (*sideward))
2887 for (child = *sideward; !NILP (child); child = c->next)
2889 c = XWINDOW (child);
2890 if (width_p)
2891 c->left_col = w->left_col;
2892 else
2893 c->top_line = w->top_line;
2894 size_window (child, size, width_p, nodelete_p);
2897 else if (!NILP (*forward))
2899 int fixed_size, each, extra, n;
2900 int resize_fixed_p, nfixed;
2901 int last_pos, first_pos, nchildren, total;
2902 int *new_sizes = NULL;
2904 /* Determine the fixed-size portion of the this window, and the
2905 number of child windows. */
2906 fixed_size = nchildren = nfixed = total = 0;
2907 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
2909 int child_size;
2911 c = XWINDOW (child);
2912 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2913 total += child_size;
2915 if (window_fixed_size_p (c, width_p, 0))
2917 fixed_size += child_size;
2918 ++nfixed;
2922 /* If the new size is smaller than fixed_size, or if there
2923 aren't any resizable windows, allow resizing fixed-size
2924 windows. */
2925 resize_fixed_p = nfixed == nchildren || size < fixed_size;
2927 /* Compute how many lines/columns to add/remove to each child. The
2928 value of extra takes care of rounding errors. */
2929 n = resize_fixed_p ? nchildren : nchildren - nfixed;
2930 if (size < total && n > 1)
2931 new_sizes = shrink_windows (total, size, nchildren, n, min_size,
2932 resize_fixed_p, *forward, width_p);
2933 else
2935 each = (size - total) / n;
2936 extra = (size - total) - n * each;
2939 /* Compute new children heights and edge positions. */
2940 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
2941 last_pos = first_pos;
2942 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
2944 int new_size, old_size;
2946 c = XWINDOW (child);
2947 old_size = width_p ? XFASTINT (c->total_cols) : XFASTINT (c->total_lines);
2948 new_size = old_size;
2950 /* The top or left edge position of this child equals the
2951 bottom or right edge of its predecessor. */
2952 if (width_p)
2953 c->left_col = make_number (last_pos);
2954 else
2955 c->top_line = make_number (last_pos);
2957 /* If this child can be resized, do it. */
2958 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
2960 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
2961 extra = 0;
2964 /* Set new height. Note that size_window also propagates
2965 edge positions to children, so it's not a no-op if we
2966 didn't change the child's size. */
2967 size_window (child, new_size, width_p, 1);
2969 /* Remember the bottom/right edge position of this child; it
2970 will be used to set the top/left edge of the next child. */
2971 last_pos += new_size;
2974 if (new_sizes) xfree (new_sizes);
2976 /* We should have covered the parent exactly with child windows. */
2977 xassert (size == last_pos - first_pos);
2979 /* Now delete any children that became too small. */
2980 if (!nodelete_p)
2981 for (child = *forward; !NILP (child); child = c->next)
2983 int child_size;
2984 c = XWINDOW (child);
2985 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2986 size_window (child, child_size, width_p, 2);
2991 /* Set WINDOW's height to HEIGHT, and recursively change the height of
2992 WINDOW's children. NODELETE non-zero means don't delete windows
2993 that become too small in the process. (The caller should check
2994 later and do so if appropriate.) */
2996 void
2997 set_window_height (window, height, nodelete)
2998 Lisp_Object window;
2999 int height;
3000 int nodelete;
3002 size_window (window, height, 0, nodelete);
3006 /* Set WINDOW's width to WIDTH, and recursively change the width of
3007 WINDOW's children. NODELETE non-zero means don't delete windows
3008 that become too small in the process. (The caller should check
3009 later and do so if appropriate.) */
3011 void
3012 set_window_width (window, width, nodelete)
3013 Lisp_Object window;
3014 int width;
3015 int nodelete;
3017 size_window (window, width, 1, nodelete);
3020 /* Change window heights in windows rooted in WINDOW by N lines. */
3022 void
3023 change_window_heights (window, n)
3024 Lisp_Object window;
3025 int n;
3027 struct window *w = XWINDOW (window);
3029 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3030 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3032 if (INTEGERP (w->orig_top_line))
3033 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3034 if (INTEGERP (w->orig_total_lines))
3035 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3037 /* Handle just the top child in a vertical split. */
3038 if (!NILP (w->vchild))
3039 change_window_heights (w->vchild, n);
3041 /* Adjust all children in a horizontal split. */
3042 for (window = w->hchild; !NILP (window); window = w->next)
3044 w = XWINDOW (window);
3045 change_window_heights (window, n);
3050 int window_select_count;
3052 Lisp_Object
3053 Fset_window_buffer_unwind (obuf)
3054 Lisp_Object obuf;
3056 Fset_buffer (obuf);
3057 return Qnil;
3060 EXFUN (Fset_window_fringes, 4);
3061 EXFUN (Fset_window_scroll_bars, 4);
3063 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3064 means it's allowed to run hooks. See make_frame for a case where
3065 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3066 margins, fringes, and scroll-bar settings of the window are not
3067 reset from the buffer's local settings. */
3069 void
3070 set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
3071 Lisp_Object window, buffer;
3072 int run_hooks_p, keep_margins_p;
3074 struct window *w = XWINDOW (window);
3075 struct buffer *b = XBUFFER (buffer);
3076 int count = SPECPDL_INDEX ();
3078 w->buffer = buffer;
3080 if (EQ (window, selected_window))
3081 b->last_selected_window = window;
3083 /* Let redisplay errors through. */
3084 b->display_error_modiff = 0;
3086 /* Update time stamps of buffer display. */
3087 if (INTEGERP (b->display_count))
3088 XSETINT (b->display_count, XINT (b->display_count) + 1);
3089 b->display_time = Fcurrent_time ();
3091 XSETFASTINT (w->window_end_pos, 0);
3092 XSETFASTINT (w->window_end_vpos, 0);
3093 bzero (&w->last_cursor, sizeof w->last_cursor);
3094 w->window_end_valid = Qnil;
3095 w->hscroll = w->min_hscroll = make_number (0);
3096 w->vscroll = 0;
3097 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3098 set_marker_restricted (w->start,
3099 make_number (b->last_window_start),
3100 buffer);
3101 w->start_at_line_beg = Qnil;
3102 w->force_start = Qnil;
3103 XSETFASTINT (w->last_modified, 0);
3104 XSETFASTINT (w->last_overlay_modified, 0);
3105 windows_or_buffers_changed++;
3107 /* We must select BUFFER for running the window-scroll-functions.
3108 If WINDOW is selected, switch permanently.
3109 Otherwise, switch but go back to the ambient buffer afterward. */
3110 if (EQ (window, selected_window))
3111 Fset_buffer (buffer);
3112 /* We can't check ! NILP (Vwindow_scroll_functions) here
3113 because that might itself be a local variable. */
3114 else if (window_initialized)
3116 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
3117 Fset_buffer (buffer);
3120 if (!keep_margins_p)
3122 /* Set left and right marginal area width etc. from buffer. */
3124 /* This may call adjust_window_margins three times, so
3125 temporarily disable window margins. */
3126 Lisp_Object save_left = w->left_margin_cols;
3127 Lisp_Object save_right = w->right_margin_cols;
3129 w->left_margin_cols = w->right_margin_cols = Qnil;
3131 Fset_window_fringes (window,
3132 b->left_fringe_width, b->right_fringe_width,
3133 b->fringes_outside_margins);
3135 Fset_window_scroll_bars (window,
3136 b->scroll_bar_width,
3137 b->vertical_scroll_bar_type, Qnil);
3139 w->left_margin_cols = save_left;
3140 w->right_margin_cols = save_right;
3142 Fset_window_margins (window,
3143 b->left_margin_cols, b->right_margin_cols);
3146 if (run_hooks_p)
3148 if (! NILP (Vwindow_scroll_functions))
3149 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3150 Fmarker_position (w->start));
3152 if (! NILP (Vwindow_configuration_change_hook)
3153 && ! NILP (Vrun_hooks))
3154 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3157 unbind_to (count, Qnil);
3161 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3162 doc: /* Make WINDOW display BUFFER as its contents.
3163 BUFFER can be a buffer or the name of an existing buffer.
3164 Optional third arg KEEP-MARGINS non-nil means that WINDOW's current
3165 display margins, fringe widths, and scroll bar settings are maintained;
3166 the default is to reset these from BUFFER's local settings or the frame
3167 defaults.
3169 This function runs the hook `window-scroll-functions'. */)
3170 (window, buffer, keep_margins)
3171 register Lisp_Object window, buffer, keep_margins;
3173 register Lisp_Object tem;
3174 register struct window *w = decode_window (window);
3176 XSETWINDOW (window, w);
3177 buffer = Fget_buffer (buffer);
3178 CHECK_BUFFER (buffer);
3180 if (NILP (XBUFFER (buffer)->name))
3181 error ("Attempt to display deleted buffer");
3183 tem = w->buffer;
3184 if (NILP (tem))
3185 error ("Window is deleted");
3186 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
3187 is first being set up. */
3189 if (!NILP (w->dedicated) && !EQ (tem, buffer))
3190 error ("Window is dedicated to `%s'",
3191 SDATA (XBUFFER (tem)->name));
3193 unshow_buffer (w);
3196 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3197 return Qnil;
3200 /* Note that selected_window can be nil
3201 when this is called from Fset_window_configuration. */
3203 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3204 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3205 If WINDOW is not already selected, make WINDOW's buffer current
3206 and make WINDOW the frame's selected window. Return WINDOW.
3207 Optional second arg NORECORD non-nil means
3208 do not put this buffer at the front of the list of recently selected ones.
3210 Note that the main editor command loop
3211 selects the buffer of the selected window before each command. */)
3212 (window, norecord)
3213 register Lisp_Object window, norecord;
3215 register struct window *w;
3216 register struct window *ow;
3217 struct frame *sf;
3219 CHECK_LIVE_WINDOW (window);
3221 w = XWINDOW (window);
3222 w->frozen_window_start_p = 0;
3224 ++window_select_count;
3225 XSETFASTINT (w->use_time, window_select_count);
3226 if (EQ (window, selected_window))
3227 return window;
3229 /* Store the current buffer's actual point into the
3230 old selected window. It belongs to that window,
3231 and when the window is not selected, must be in the window. */
3232 if (!NILP (selected_window))
3234 ow = XWINDOW (selected_window);
3235 if (! NILP (ow->buffer))
3236 set_marker_both (ow->pointm, ow->buffer,
3237 BUF_PT (XBUFFER (ow->buffer)),
3238 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3241 selected_window = window;
3242 sf = SELECTED_FRAME ();
3243 if (XFRAME (WINDOW_FRAME (w)) != sf)
3245 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3246 /* Use this rather than Fhandle_switch_frame
3247 so that FRAME_FOCUS_FRAME is moved appropriately as we
3248 move around in the state where a minibuffer in a separate
3249 frame is active. */
3250 Fselect_frame (WINDOW_FRAME (w));
3252 else
3253 sf->selected_window = window;
3255 if (NILP (norecord))
3256 record_buffer (w->buffer);
3257 Fset_buffer (w->buffer);
3259 XBUFFER (w->buffer)->last_selected_window = window;
3261 /* Go to the point recorded in the window.
3262 This is important when the buffer is in more
3263 than one window. It also matters when
3264 redisplay_window has altered point after scrolling,
3265 because it makes the change only in the window. */
3267 register int new_point = marker_position (w->pointm);
3268 if (new_point < BEGV)
3269 SET_PT (BEGV);
3270 else if (new_point > ZV)
3271 SET_PT (ZV);
3272 else
3273 SET_PT (new_point);
3276 windows_or_buffers_changed++;
3277 return window;
3280 static Lisp_Object
3281 select_window_norecord (window)
3282 Lisp_Object window;
3284 return Fselect_window (window, Qt);
3287 /* Deiconify the frame containing the window WINDOW,
3288 unless it is the selected frame;
3289 then return WINDOW.
3291 The reason for the exception for the selected frame
3292 is that it seems better not to change the selected frames visibility
3293 merely because of displaying a different buffer in it.
3294 The deiconification is useful when a buffer gets shown in
3295 another frame that you were not using lately. */
3297 static Lisp_Object
3298 display_buffer_1 (window)
3299 Lisp_Object window;
3301 Lisp_Object frame = XWINDOW (window)->frame;
3302 FRAME_PTR f = XFRAME (frame);
3304 FRAME_SAMPLE_VISIBILITY (f);
3306 if (EQ (frame, selected_frame))
3307 ; /* Assume the selected frame is already visible enough. */
3308 else if (minibuf_level > 0
3309 && MINI_WINDOW_P (XWINDOW (selected_window))
3310 && WINDOW_LIVE_P (minibuf_selected_window)
3311 && EQ (frame, WINDOW_FRAME (XWINDOW (minibuf_selected_window))))
3312 ; /* Assume the frame from which we invoked the minibuffer is visible. */
3313 else
3315 if (FRAME_ICONIFIED_P (f))
3316 Fmake_frame_visible (frame);
3317 else if (FRAME_VISIBLE_P (f))
3318 Fraise_frame (frame);
3321 return window;
3324 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
3325 doc: /* Returns non-nil if a buffer named BUFFER-NAME gets a special frame.
3326 If the value is t, `display-buffer' or `pop-to-buffer' would create a
3327 special frame for that buffer using the default frame parameters.
3329 If the value is a list, it is a list of frame parameters that would be used
3330 to make a frame for that buffer.
3331 The variables `special-display-buffer-names'
3332 and `special-display-regexps' control this. */)
3333 (buffer_name)
3334 Lisp_Object buffer_name;
3336 Lisp_Object tem;
3338 CHECK_STRING (buffer_name);
3340 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
3341 if (!NILP (tem))
3342 return Qt;
3344 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
3345 if (!NILP (tem))
3346 return XCDR (tem);
3348 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
3350 Lisp_Object car = XCAR (tem);
3351 if (STRINGP (car)
3352 && fast_string_match (car, buffer_name) >= 0)
3353 return Qt;
3354 else if (CONSP (car)
3355 && STRINGP (XCAR (car))
3356 && fast_string_match (XCAR (car), buffer_name) >= 0)
3357 return XCDR (car);
3359 return Qnil;
3362 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
3363 doc: /* Returns non-nil if a buffer named BUFFER-NAME would use the same window.
3364 More precisely, if `display-buffer' or `pop-to-buffer' would display
3365 that buffer in the selected window rather than (as usual) in some other window.
3366 See `same-window-buffer-names' and `same-window-regexps'. */)
3367 (buffer_name)
3368 Lisp_Object buffer_name;
3370 Lisp_Object tem;
3372 CHECK_STRING (buffer_name);
3374 tem = Fmember (buffer_name, Vsame_window_buffer_names);
3375 if (!NILP (tem))
3376 return Qt;
3378 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
3379 if (!NILP (tem))
3380 return Qt;
3382 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
3384 Lisp_Object car = XCAR (tem);
3385 if (STRINGP (car)
3386 && fast_string_match (car, buffer_name) >= 0)
3387 return Qt;
3388 else if (CONSP (car)
3389 && STRINGP (XCAR (car))
3390 && fast_string_match (XCAR (car), buffer_name) >= 0)
3391 return Qt;
3393 return Qnil;
3396 /* Use B so the default is (other-buffer). */
3397 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
3398 "BDisplay buffer: \nP",
3399 doc: /* Make BUFFER appear in some window but don't select it.
3400 BUFFER must be the name of an existing buffer, or, when called from Lisp,
3401 a buffer.
3402 If BUFFER is shown already in some window, just use that one,
3403 unless the window is the selected window and the optional second
3404 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).
3405 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
3406 Returns the window displaying BUFFER.
3407 If `display-buffer-reuse-frames' is non-nil, and another frame is currently
3408 displaying BUFFER, then simply raise that frame.
3410 The variables `special-display-buffer-names',
3411 `special-display-regexps', `same-window-buffer-names', and
3412 `same-window-regexps' customize how certain buffer names are handled.
3413 The latter two take effect only if NOT-THIS-WINDOW is t.
3415 If optional argument FRAME is `visible', search all visible frames.
3416 If FRAME is 0, search all visible and iconified frames.
3417 If FRAME is t, search all frames.
3418 If FRAME is a frame, search only that frame.
3419 If FRAME is nil, search only the selected frame
3420 (actually the last nonminibuffer frame),
3421 unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,
3422 which means search visible and iconified frames.
3424 If a full-width window on a splittable frame is available to display
3425 the buffer, it may be split, subject to the value of the variable
3426 `split-height-threshold'.
3428 If `even-window-heights' is non-nil, window heights will be evened out
3429 if displaying the buffer causes two vertically adjacent windows to be
3430 displayed. */)
3431 (buffer, not_this_window, frame)
3432 register Lisp_Object buffer, not_this_window, frame;
3434 register Lisp_Object window, tem, swp;
3435 struct frame *f;
3437 swp = Qnil;
3438 buffer = Fget_buffer (buffer);
3439 CHECK_BUFFER (buffer);
3441 if (!NILP (Vdisplay_buffer_function))
3442 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
3444 if (NILP (not_this_window)
3445 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
3446 return display_buffer_1 (selected_window);
3448 /* See if the user has specified this buffer should appear
3449 in the selected window. */
3450 if (NILP (not_this_window))
3452 swp = Fsame_window_p (XBUFFER (buffer)->name);
3453 if (!NILP (swp) && !no_switch_window (selected_window))
3455 Fswitch_to_buffer (buffer, Qnil);
3456 return display_buffer_1 (selected_window);
3460 /* If the user wants pop-up-frames or display-buffer-reuse-frames,
3461 look for a window showing BUFFER on any visible or iconified frame.
3462 Otherwise search only the current frame. */
3463 if (! NILP (frame))
3464 tem = frame;
3465 else if (pop_up_frames
3466 || display_buffer_reuse_frames
3467 || last_nonminibuf_frame == 0)
3468 XSETFASTINT (tem, 0);
3469 else
3470 XSETFRAME (tem, last_nonminibuf_frame);
3472 window = Fget_buffer_window (buffer, tem);
3473 if (!NILP (window)
3474 && (NILP (not_this_window) || !EQ (window, selected_window)))
3475 return display_buffer_1 (window);
3477 /* Certain buffer names get special handling. */
3478 if (!NILP (Vspecial_display_function) && NILP (swp))
3480 tem = Fspecial_display_p (XBUFFER (buffer)->name);
3481 if (EQ (tem, Qt))
3482 return call1 (Vspecial_display_function, buffer);
3483 if (CONSP (tem))
3484 return call2 (Vspecial_display_function, buffer, tem);
3487 /* If there are no frames open that have more than a minibuffer,
3488 we need to create a new frame. */
3489 if (pop_up_frames || last_nonminibuf_frame == 0)
3491 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3492 Fset_window_buffer (window, buffer, Qnil);
3493 return display_buffer_1 (window);
3496 f = SELECTED_FRAME ();
3497 if (pop_up_windows
3498 || FRAME_MINIBUF_ONLY_P (f)
3499 /* If the current frame is a special display frame,
3500 don't try to reuse its windows. */
3501 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
3503 Lisp_Object frames;
3505 frames = Qnil;
3506 if (FRAME_MINIBUF_ONLY_P (f))
3507 XSETFRAME (frames, last_nonminibuf_frame);
3508 /* Don't try to create a window if we would get an error. */
3509 if (split_height_threshold < window_min_height << 1)
3510 split_height_threshold = window_min_height << 1;
3512 /* Note that both Fget_largest_window and Fget_lru_window
3513 ignore minibuffers and dedicated windows.
3514 This means they can return nil. */
3516 /* If the frame we would try to split cannot be split,
3517 try other frames. */
3518 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
3520 /* Try visible frames first. */
3521 window = Fget_largest_window (Qvisible, Qt);
3522 /* If that didn't work, try iconified frames. */
3523 if (NILP (window))
3524 window = Fget_largest_window (make_number (0), Qt);
3525 #if 0 /* Don't try windows on other displays. */
3526 if (NILP (window))
3527 window = Fget_largest_window (Qt, Qt);
3528 #endif
3530 else
3531 window = Fget_largest_window (frames, Qt);
3533 /* If we got a tall enough full-width window that can be split,
3534 split it. */
3535 if (!NILP (window)
3536 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3537 && window_height (window) >= split_height_threshold
3538 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
3539 window = Fsplit_window (window, Qnil, Qnil);
3540 else
3542 Lisp_Object upper, lower, other;
3544 window = Fget_lru_window (frames, Qt);
3545 /* If the LRU window is selected, and big enough,
3546 and can be split, split it. */
3547 if (!NILP (window)
3548 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3549 && (EQ (window, selected_window)
3550 || EQ (XWINDOW (window)->parent, Qnil))
3551 && window_height (window) >= window_min_height << 1)
3552 window = Fsplit_window (window, Qnil, Qnil);
3553 else
3554 window = Fget_lru_window (frames, Qnil);
3555 /* If Fget_lru_window returned nil, try other approaches. */
3557 /* Try visible frames first. */
3558 if (NILP (window))
3559 window = Fget_buffer_window (buffer, Qvisible);
3560 if (NILP (window))
3561 window = Fget_largest_window (Qvisible, Qnil);
3562 /* If that didn't work, try iconified frames. */
3563 if (NILP (window))
3564 window = Fget_buffer_window (buffer, make_number (0));
3565 if (NILP (window))
3566 window = Fget_largest_window (make_number (0), Qnil);
3568 #if 0 /* Don't try frames on other displays. */
3569 if (NILP (window))
3570 window = Fget_buffer_window (buffer, Qt);
3571 if (NILP (window))
3572 window = Fget_largest_window (Qt, Qnil);
3573 #endif
3574 /* As a last resort, make a new frame. */
3575 if (NILP (window))
3576 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3577 /* If window appears above or below another,
3578 even out their heights. */
3579 other = upper = lower = Qnil;
3580 if (!NILP (XWINDOW (window)->prev))
3581 other = upper = XWINDOW (window)->prev, lower = window;
3582 if (!NILP (XWINDOW (window)->next))
3583 other = lower = XWINDOW (window)->next, upper = window;
3584 if (!NILP (other)
3585 && !NILP (Veven_window_heights)
3586 /* Check that OTHER and WINDOW are vertically arrayed. */
3587 && !EQ (XWINDOW (other)->top_line, XWINDOW (window)->top_line)
3588 && (XFASTINT (XWINDOW (other)->total_lines)
3589 > XFASTINT (XWINDOW (window)->total_lines)))
3591 int total = (XFASTINT (XWINDOW (other)->total_lines)
3592 + XFASTINT (XWINDOW (window)->total_lines));
3593 enlarge_window (upper,
3594 total / 2 - XFASTINT (XWINDOW (upper)->total_lines),
3599 else
3600 window = Fget_lru_window (Qnil, Qnil);
3602 Fset_window_buffer (window, buffer, Qnil);
3603 return display_buffer_1 (window);
3607 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3608 0, 1, 0,
3609 doc: /* Force redisplay of all windows.
3610 If optional arg OBJECT is a window, force redisplay of that window only.
3611 If OBJECT is a buffer or buffer name, force redisplay of all windows
3612 displaying that buffer. */)
3613 (object)
3614 Lisp_Object object;
3616 if (NILP (object))
3618 windows_or_buffers_changed++;
3619 update_mode_lines++;
3620 return Qt;
3623 if (WINDOWP (object))
3625 struct window *w = XWINDOW (object);
3626 mark_window_display_accurate (object, 0);
3627 w->update_mode_line = Qt;
3628 if (BUFFERP (w->buffer))
3629 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3630 ++update_mode_lines;
3631 return Qt;
3634 if (STRINGP (object))
3635 object = Fget_buffer (object);
3636 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3638 /* Walk all windows looking for buffer, and force update
3639 of each of those windows. */
3641 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3642 return NILP (object) ? Qnil : Qt;
3645 /* If nothing suitable was found, just return.
3646 We could signal an error, but this feature will typically be used
3647 asynchronously in timers or process sentinels, so we don't. */
3648 return Qnil;
3652 void
3653 temp_output_buffer_show (buf)
3654 register Lisp_Object buf;
3656 register struct buffer *old = current_buffer;
3657 register Lisp_Object window;
3658 register struct window *w;
3660 XBUFFER (buf)->directory = current_buffer->directory;
3662 Fset_buffer (buf);
3663 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3664 BEGV = BEG;
3665 ZV = Z;
3666 SET_PT (BEG);
3667 #if 0 /* rms: there should be no reason for this. */
3668 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3669 #endif
3670 set_buffer_internal (old);
3672 if (!NILP (Vtemp_buffer_show_function))
3673 call1 (Vtemp_buffer_show_function, buf);
3674 else
3676 window = Fdisplay_buffer (buf, Qnil, Qnil);
3678 if (!EQ (XWINDOW (window)->frame, selected_frame))
3679 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3680 Vminibuf_scroll_window = window;
3681 w = XWINDOW (window);
3682 XSETFASTINT (w->hscroll, 0);
3683 XSETFASTINT (w->min_hscroll, 0);
3684 set_marker_restricted_both (w->start, buf, BEG, BEG);
3685 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3687 /* Run temp-buffer-show-hook, with the chosen window selected
3688 and its buffer current. */
3690 if (!NILP (Vrun_hooks)
3691 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3692 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3694 int count = SPECPDL_INDEX ();
3695 Lisp_Object prev_window, prev_buffer;
3696 prev_window = selected_window;
3697 XSETBUFFER (prev_buffer, old);
3699 /* Select the window that was chosen, for running the hook.
3700 Note: Both Fselect_window and select_window_norecord may
3701 set-buffer to the buffer displayed in the window,
3702 so we need to save the current buffer. --stef */
3703 record_unwind_protect (Fset_buffer, prev_buffer);
3704 record_unwind_protect (select_window_norecord, prev_window);
3705 Fselect_window (window, Qt);
3706 Fset_buffer (w->buffer);
3707 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3708 unbind_to (count, Qnil);
3713 static void
3714 make_dummy_parent (window)
3715 Lisp_Object window;
3717 Lisp_Object new;
3718 register struct window *o, *p;
3719 int i;
3721 o = XWINDOW (window);
3722 p = allocate_window ();
3723 for (i = 0; i < VECSIZE (struct window); ++i)
3724 ((struct Lisp_Vector *) p)->contents[i]
3725 = ((struct Lisp_Vector *)o)->contents[i];
3726 XSETWINDOW (new, p);
3728 ++sequence_number;
3729 XSETFASTINT (p->sequence_number, sequence_number);
3731 /* Put new into window structure in place of window */
3732 replace_window (window, new);
3734 o->next = Qnil;
3735 o->prev = Qnil;
3736 o->vchild = Qnil;
3737 o->hchild = Qnil;
3738 o->parent = new;
3740 p->start = Qnil;
3741 p->pointm = Qnil;
3742 p->buffer = Qnil;
3745 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3746 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3747 WINDOW defaults to selected one and SIZE to half its size.
3748 If optional third arg HORFLAG is non-nil, split side by side
3749 and put SIZE columns in the first of the pair. In that case,
3750 SIZE includes that window's scroll bar, or the divider column to its right.
3751 Interactively, all arguments are nil.
3753 Returns the newly created window (which is the lower or rightmost one).
3754 The upper or leftmost window is the original one and remains selected.
3755 See Info node `(elisp)Splitting Windows' for more details and examples.*/)
3756 (window, size, horflag)
3757 Lisp_Object window, size, horflag;
3759 register Lisp_Object new;
3760 register struct window *o, *p;
3761 FRAME_PTR fo;
3762 register int size_int;
3764 if (NILP (window))
3765 window = selected_window;
3766 else
3767 CHECK_LIVE_WINDOW (window);
3769 o = XWINDOW (window);
3770 fo = XFRAME (WINDOW_FRAME (o));
3772 if (NILP (size))
3774 if (!NILP (horflag))
3775 /* Calculate the size of the left-hand window, by dividing
3776 the usable space in columns by two.
3777 We round up, since the left-hand window may include
3778 a dividing line, while the right-hand may not. */
3779 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3780 else
3781 size_int = XFASTINT (o->total_lines) >> 1;
3783 else
3785 CHECK_NUMBER (size);
3786 size_int = XINT (size);
3789 if (MINI_WINDOW_P (o))
3790 error ("Attempt to split minibuffer window");
3791 else if (window_fixed_size_p (o, !NILP (horflag), 0))
3792 error ("Attempt to split fixed-size window");
3794 check_min_window_sizes ();
3796 if (NILP (horflag))
3798 if (size_int < window_min_height)
3799 error ("Window height %d too small (after splitting)", size_int);
3800 if (size_int + window_min_height > XFASTINT (o->total_lines))
3801 error ("Window height %d too small (after splitting)",
3802 XFASTINT (o->total_lines) - size_int);
3803 if (NILP (o->parent)
3804 || NILP (XWINDOW (o->parent)->vchild))
3806 make_dummy_parent (window);
3807 new = o->parent;
3808 XWINDOW (new)->vchild = window;
3811 else
3813 if (size_int < window_min_width)
3814 error ("Window width %d too small (after splitting)", size_int);
3816 if (size_int + window_min_width > XFASTINT (o->total_cols))
3817 error ("Window width %d too small (after splitting)",
3818 XFASTINT (o->total_cols) - size_int);
3819 if (NILP (o->parent)
3820 || NILP (XWINDOW (o->parent)->hchild))
3822 make_dummy_parent (window);
3823 new = o->parent;
3824 XWINDOW (new)->hchild = window;
3828 /* Now we know that window's parent is a vertical combination
3829 if we are dividing vertically, or a horizontal combination
3830 if we are making side-by-side windows */
3832 windows_or_buffers_changed++;
3833 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3834 new = make_window ();
3835 p = XWINDOW (new);
3837 p->frame = o->frame;
3838 p->next = o->next;
3839 if (!NILP (p->next))
3840 XWINDOW (p->next)->prev = new;
3841 p->prev = window;
3842 o->next = new;
3843 p->parent = o->parent;
3844 p->buffer = Qt;
3845 p->window_end_valid = Qnil;
3846 bzero (&p->last_cursor, sizeof p->last_cursor);
3848 /* Duplicate special geometry settings. */
3850 p->left_margin_cols = o->left_margin_cols;
3851 p->right_margin_cols = o->right_margin_cols;
3852 p->left_fringe_width = o->left_fringe_width;
3853 p->right_fringe_width = o->right_fringe_width;
3854 p->fringes_outside_margins = o->fringes_outside_margins;
3855 p->scroll_bar_width = o->scroll_bar_width;
3856 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3858 /* Apportion the available frame space among the two new windows */
3860 if (!NILP (horflag))
3862 p->total_lines = o->total_lines;
3863 p->top_line = o->top_line;
3864 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3865 XSETFASTINT (o->total_cols, size_int);
3866 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3867 adjust_window_margins (p);
3868 adjust_window_margins (o);
3870 else
3872 p->left_col = o->left_col;
3873 p->total_cols = o->total_cols;
3874 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3875 XSETFASTINT (o->total_lines, size_int);
3876 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3879 /* Adjust glyph matrices. */
3880 adjust_glyphs (fo);
3882 Fset_window_buffer (new, o->buffer, Qt);
3883 return new;
3886 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3887 doc: /* Make current window ARG lines bigger.
3888 From program, optional second arg non-nil means grow sideways ARG columns.
3889 Interactively, if an argument is not given, make the window one line bigger.
3890 If HORIZONTAL is non-nil, enlarge horizontally instead of vertically.
3891 This function can delete windows, even the second window, if they get
3892 too small. */)
3893 (arg, horizontal)
3894 Lisp_Object arg, horizontal;
3896 CHECK_NUMBER (arg);
3897 enlarge_window (selected_window, XINT (arg), !NILP (horizontal));
3899 if (! NILP (Vwindow_configuration_change_hook))
3900 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3902 return Qnil;
3905 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3906 doc: /* Make current window ARG lines smaller.
3907 From program, optional second arg non-nil means shrink sideways arg columns.
3908 Interactively, if an argument is not given, make the window one line smaller. Only
3909 siblings to the right or below are changed. */)
3910 (arg, side)
3911 Lisp_Object arg, side;
3913 CHECK_NUMBER (arg);
3914 enlarge_window (selected_window, -XINT (arg), !NILP (side));
3916 if (! NILP (Vwindow_configuration_change_hook))
3917 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3919 return Qnil;
3923 window_height (window)
3924 Lisp_Object window;
3926 register struct window *p = XWINDOW (window);
3927 return WINDOW_TOTAL_LINES (p);
3931 window_width (window)
3932 Lisp_Object window;
3934 register struct window *p = XWINDOW (window);
3935 return WINDOW_TOTAL_COLS (p);
3939 #define CURBEG(w) \
3940 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3942 #define CURSIZE(w) \
3943 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3946 /* Enlarge WINDOW by DELTA.
3947 HORIZ_FLAG nonzero means enlarge it horizontally;
3948 zero means do it vertically.
3950 Siblings of the selected window are resized to fulfill the size
3951 request. If they become too small in the process, they will be
3952 deleted. */
3954 static void
3955 enlarge_window (window, delta, horiz_flag)
3956 Lisp_Object window;
3957 int delta, horiz_flag;
3959 Lisp_Object parent, next, prev;
3960 struct window *p;
3961 Lisp_Object *sizep;
3962 int maximum;
3963 int (*sizefun) P_ ((Lisp_Object))
3964 = horiz_flag ? window_width : window_height;
3965 void (*setsizefun) P_ ((Lisp_Object, int, int))
3966 = (horiz_flag ? set_window_width : set_window_height);
3968 /* Check values of window_min_width and window_min_height for
3969 validity. */
3970 check_min_window_sizes ();
3972 /* Give up if this window cannot be resized. */
3973 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
3974 error ("Window is not resizable");
3976 /* Find the parent of the selected window. */
3977 while (1)
3979 p = XWINDOW (window);
3980 parent = p->parent;
3982 if (NILP (parent))
3984 if (horiz_flag)
3985 error ("No other window to side of this one");
3986 break;
3989 if (horiz_flag
3990 ? !NILP (XWINDOW (parent)->hchild)
3991 : !NILP (XWINDOW (parent)->vchild))
3992 break;
3994 window = parent;
3997 sizep = &CURSIZE (window);
4000 register int maxdelta;
4002 /* Compute the maximum size increment this window can have. */
4004 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4005 /* This is a main window followed by a minibuffer. */
4006 : !NILP (p->next) ? ((*sizefun) (p->next)
4007 - window_min_size (XWINDOW (p->next),
4008 horiz_flag, 0, 0))
4009 /* This is a minibuffer following a main window. */
4010 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4011 - window_min_size (XWINDOW (p->prev),
4012 horiz_flag, 0, 0))
4013 /* This is a frame with only one window, a minibuffer-only
4014 or a minibufferless frame. */
4015 : (delta = 0));
4017 if (delta > maxdelta)
4018 /* This case traps trying to make the minibuffer
4019 the full frame, or make the only window aside from the
4020 minibuffer the full frame. */
4021 delta = maxdelta;
4024 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window), horiz_flag, 0, 0))
4026 delete_window (window);
4027 return;
4030 if (delta == 0)
4031 return;
4033 /* Find the total we can get from other siblings without deleting them. */
4034 maximum = 0;
4035 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
4036 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4037 horiz_flag, 0, 0);
4038 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
4039 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4040 horiz_flag, 0, 0);
4042 /* If we can get it all from them without deleting them, do so. */
4043 if (delta <= maximum)
4045 Lisp_Object first_unaffected;
4046 Lisp_Object first_affected;
4047 int fixed_p;
4049 next = p->next;
4050 prev = p->prev;
4051 first_affected = window;
4052 /* Look at one sibling at a time,
4053 moving away from this window in both directions alternately,
4054 and take as much as we can get without deleting that sibling. */
4055 while (delta != 0
4056 && (!NILP (next) || !NILP (prev)))
4058 if (! NILP (next))
4060 int this_one = ((*sizefun) (next)
4061 - window_min_size (XWINDOW (next),
4062 horiz_flag, 0, &fixed_p));
4063 if (!fixed_p)
4065 if (this_one > delta)
4066 this_one = delta;
4068 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4069 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4071 delta -= this_one;
4074 next = XWINDOW (next)->next;
4077 if (delta == 0)
4078 break;
4080 if (! NILP (prev))
4082 int this_one = ((*sizefun) (prev)
4083 - window_min_size (XWINDOW (prev),
4084 horiz_flag, 0, &fixed_p));
4085 if (!fixed_p)
4087 if (this_one > delta)
4088 this_one = delta;
4090 first_affected = prev;
4092 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4093 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4095 delta -= this_one;
4098 prev = XWINDOW (prev)->prev;
4102 xassert (delta == 0);
4104 /* Now recalculate the edge positions of all the windows affected,
4105 based on the new sizes. */
4106 first_unaffected = next;
4107 prev = first_affected;
4108 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4109 prev = next, next = XWINDOW (next)->next)
4111 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4112 /* This does not change size of NEXT,
4113 but it propagates the new top edge to its children */
4114 (*setsizefun) (next, (*sizefun) (next), 0);
4117 else
4119 register int delta1;
4120 register int opht = (*sizefun) (parent);
4122 if (opht <= XINT (*sizep) + delta)
4124 /* If trying to grow this window to or beyond size of the parent,
4125 just delete all the sibling windows. */
4126 Lisp_Object start, tem, next;
4128 start = XWINDOW (parent)->vchild;
4129 if (NILP (start))
4130 start = XWINDOW (parent)->hchild;
4132 /* Delete any siblings that come after WINDOW. */
4133 tem = XWINDOW (window)->next;
4134 while (! NILP (tem))
4136 next = XWINDOW (tem)->next;
4137 delete_window (tem);
4138 tem = next;
4141 /* Delete any siblings that come after WINDOW.
4142 Note that if START is not WINDOW, then WINDOW still
4143 Fhas siblings, so WINDOW has not yet replaced its parent. */
4144 tem = start;
4145 while (! EQ (tem, window))
4147 next = XWINDOW (tem)->next;
4148 delete_window (tem);
4149 tem = next;
4152 else
4154 /* Otherwise, make delta1 just right so that if we add
4155 delta1 lines to this window and to the parent, and then
4156 shrink the parent back to its original size, the new
4157 proportional size of this window will increase by delta.
4159 The function size_window will compute the new height h'
4160 of the window from delta1 as:
4162 e = delta1/n
4163 x = delta1 - delta1/n * n for the 1st resizable child
4164 h' = h + e + x
4166 where n is the number of children that can be resized.
4167 We can ignore x by choosing a delta1 that is a multiple of
4168 n. We want the height of this window to come out as
4170 h' = h + delta
4172 So, delta1 must be
4174 h + e = h + delta
4175 delta1/n = delta
4176 delta1 = n * delta.
4178 The number of children n equals the number of resizable
4179 children of this window + 1 because we know window itself
4180 is resizable (otherwise we would have signalled an error). */
4182 struct window *w = XWINDOW (window);
4183 Lisp_Object s;
4184 int n = 1;
4186 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
4187 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4188 ++n;
4189 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
4190 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4191 ++n;
4193 delta1 = n * delta;
4195 /* Add delta1 lines or columns to this window, and to the parent,
4196 keeping things consistent while not affecting siblings. */
4197 XSETINT (CURSIZE (parent), opht + delta1);
4198 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4200 /* Squeeze out delta1 lines or columns from our parent,
4201 shriking this window and siblings proportionately.
4202 This brings parent back to correct size.
4203 Delta1 was calculated so this makes this window the desired size,
4204 taking it all out of the siblings. */
4205 (*setsizefun) (parent, opht, 0);
4210 XSETFASTINT (p->last_modified, 0);
4211 XSETFASTINT (p->last_overlay_modified, 0);
4213 /* Adjust glyph matrices. */
4214 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4218 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4219 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4220 zero means adjust the height, moving the bottom edge.
4222 Following siblings of the selected window are resized to fulfill
4223 the size request. If they become too small in the process, they
4224 are not deleted; instead, we signal an error. */
4226 static void
4227 adjust_window_trailing_edge (window, delta, horiz_flag)
4228 Lisp_Object window;
4229 int delta, horiz_flag;
4231 Lisp_Object parent, child;
4232 struct window *p;
4233 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4234 int delcount = window_deletion_count;
4236 /* Check values of window_min_width and window_min_height for
4237 validity. */
4238 check_min_window_sizes ();
4240 if (NILP (window))
4241 window = Fselected_window ();
4243 CHECK_WINDOW (window);
4245 /* Give up if this window cannot be resized. */
4246 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4247 error ("Window is not resizable");
4249 while (1)
4251 p = XWINDOW (window);
4252 parent = p->parent;
4254 /* Make sure there is a following window. */
4255 if (NILP (parent)
4256 && (horiz_flag ? 1
4257 : NILP (XWINDOW (window)->next)))
4259 Fset_window_configuration (old_config);
4260 error ("No other window following this one");
4263 /* Don't make this window too small. */
4264 if (XINT (CURSIZE (window)) + delta
4265 < (horiz_flag ? window_min_width : window_min_height))
4267 Fset_window_configuration (old_config);
4268 error ("Cannot adjust window size as specified");
4271 /* Clear out some redisplay caches. */
4272 XSETFASTINT (p->last_modified, 0);
4273 XSETFASTINT (p->last_overlay_modified, 0);
4275 /* Adjust this window's edge. */
4276 XSETINT (CURSIZE (window),
4277 XINT (CURSIZE (window)) + delta);
4279 /* If this window has following siblings in the desired dimension,
4280 make them smaller.
4281 (If we reach the top of the tree and can never do this,
4282 we will fail and report an error, above.) */
4283 if (horiz_flag
4284 ? !NILP (XWINDOW (parent)->hchild)
4285 : !NILP (XWINDOW (parent)->vchild))
4287 if (!NILP (XWINDOW (window)->next))
4289 XSETINT (CURBEG (p->next),
4290 XINT (CURBEG (p->next)) + delta);
4291 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4292 horiz_flag, 0);
4293 break;
4296 else
4297 /* Here we have a chain of parallel siblings, in the other dimension.
4298 Change the size of the other siblings. */
4299 for (child = (horiz_flag
4300 ? XWINDOW (parent)->vchild
4301 : XWINDOW (parent)->hchild);
4302 ! NILP (child);
4303 child = XWINDOW (child)->next)
4304 if (! EQ (child, window))
4305 size_window (child, XINT (CURSIZE (child)) + delta,
4306 horiz_flag, 0);
4308 window = parent;
4311 /* If we made a window so small it got deleted,
4312 we failed. Report failure. */
4313 if (delcount != window_deletion_count)
4315 Fset_window_configuration (old_config);
4316 error ("Cannot adjust window size as specified");
4319 /* Adjust glyph matrices. */
4320 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4323 #undef CURBEG
4324 #undef CURSIZE
4326 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4327 Sadjust_window_trailing_edge, 3, 3, 0,
4328 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4329 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4330 Otherwise, adjust the height, moving the bottom edge.
4332 Following siblings of the selected window are resized to fulfill
4333 the size request. If they become too small in the process, they
4334 are not deleted; instead, we signal an error. */)
4335 (window, delta, horizontal)
4336 Lisp_Object window, delta, horizontal;
4338 CHECK_NUMBER (delta);
4339 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4341 if (! NILP (Vwindow_configuration_change_hook))
4342 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
4344 return Qnil;
4349 /***********************************************************************
4350 Resizing Mini-Windows
4351 ***********************************************************************/
4353 static void shrink_window_lowest_first P_ ((struct window *, int));
4355 enum save_restore_action
4357 CHECK_ORIG_SIZES,
4358 SAVE_ORIG_SIZES,
4359 RESTORE_ORIG_SIZES
4362 static int save_restore_orig_size P_ ((struct window *,
4363 enum save_restore_action));
4365 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4366 from lowest windows first. */
4368 static void
4369 shrink_window_lowest_first (w, height)
4370 struct window *w;
4371 int height;
4373 struct window *c;
4374 Lisp_Object child;
4375 int old_height;
4377 xassert (!MINI_WINDOW_P (w));
4379 /* Set redisplay hints. */
4380 XSETFASTINT (w->last_modified, 0);
4381 XSETFASTINT (w->last_overlay_modified, 0);
4382 windows_or_buffers_changed++;
4383 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4385 old_height = XFASTINT (w->total_lines);
4386 XSETFASTINT (w->total_lines, height);
4388 if (!NILP (w->hchild))
4390 for (child = w->hchild; !NILP (child); child = c->next)
4392 c = XWINDOW (child);
4393 c->top_line = w->top_line;
4394 shrink_window_lowest_first (c, height);
4397 else if (!NILP (w->vchild))
4399 Lisp_Object last_child;
4400 int delta = old_height - height;
4401 int last_top;
4403 last_child = Qnil;
4405 /* Find the last child. We are taking space from lowest windows
4406 first, so we iterate over children from the last child
4407 backwards. */
4408 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
4409 last_child = child;
4411 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
4412 for (child = last_child; delta && !NILP (child); child = c->prev)
4414 int this_one;
4416 c = XWINDOW (child);
4417 this_one = XFASTINT (c->total_lines) - MIN_SAFE_WINDOW_HEIGHT;
4419 if (this_one > delta)
4420 this_one = delta;
4422 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4423 delta -= this_one;
4426 /* Compute new positions. */
4427 last_top = XINT (w->top_line);
4428 for (child = w->vchild; !NILP (child); child = c->next)
4430 c = XWINDOW (child);
4431 c->top_line = make_number (last_top);
4432 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4433 last_top += XFASTINT (c->total_lines);
4439 /* Save, restore, or check positions and sizes in the window tree
4440 rooted at W. ACTION says what to do.
4442 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4443 orig_total_lines members are valid for all windows in the window
4444 tree. Value is non-zero if they are valid.
4446 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4447 orig_top_line and orig_total_lines for all windows in the tree.
4449 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4450 stored in orig_top_line and orig_total_lines for all windows. */
4452 static int
4453 save_restore_orig_size (w, action)
4454 struct window *w;
4455 enum save_restore_action action;
4457 int success_p = 1;
4459 while (w)
4461 if (!NILP (w->hchild))
4463 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4464 success_p = 0;
4466 else if (!NILP (w->vchild))
4468 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4469 success_p = 0;
4472 switch (action)
4474 case CHECK_ORIG_SIZES:
4475 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4476 return 0;
4477 break;
4479 case SAVE_ORIG_SIZES:
4480 w->orig_top_line = w->top_line;
4481 w->orig_total_lines = w->total_lines;
4482 XSETFASTINT (w->last_modified, 0);
4483 XSETFASTINT (w->last_overlay_modified, 0);
4484 break;
4486 case RESTORE_ORIG_SIZES:
4487 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4488 w->top_line = w->orig_top_line;
4489 w->total_lines = w->orig_total_lines;
4490 w->orig_total_lines = w->orig_top_line = Qnil;
4491 XSETFASTINT (w->last_modified, 0);
4492 XSETFASTINT (w->last_overlay_modified, 0);
4493 break;
4495 default:
4496 abort ();
4499 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4502 return success_p;
4506 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4507 without deleting other windows. */
4509 void
4510 grow_mini_window (w, delta)
4511 struct window *w;
4512 int delta;
4514 struct frame *f = XFRAME (w->frame);
4515 struct window *root;
4517 xassert (MINI_WINDOW_P (w));
4518 xassert (delta >= 0);
4520 /* Check values of window_min_width and window_min_height for
4521 validity. */
4522 check_min_window_sizes ();
4524 /* Compute how much we can enlarge the mini-window without deleting
4525 other windows. */
4526 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4527 if (delta)
4529 int min_height = window_min_size (root, 0, 0, 0);
4530 if (XFASTINT (root->total_lines) - delta < min_height)
4531 /* Note that the root window may already be smaller than
4532 min_height. */
4533 delta = max (0, XFASTINT (root->total_lines) - min_height);
4536 if (delta)
4538 /* Save original window sizes and positions, if not already done. */
4539 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4540 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4542 /* Shrink other windows. */
4543 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4545 /* Grow the mini-window. */
4546 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4547 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4548 XSETFASTINT (w->last_modified, 0);
4549 XSETFASTINT (w->last_overlay_modified, 0);
4551 adjust_glyphs (f);
4556 /* Shrink mini-window W. If there is recorded info about window sizes
4557 before a call to grow_mini_window, restore recorded window sizes.
4558 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4559 line. */
4561 void
4562 shrink_mini_window (w)
4563 struct window *w;
4565 struct frame *f = XFRAME (w->frame);
4566 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4568 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4570 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4571 adjust_glyphs (f);
4572 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4573 windows_or_buffers_changed = 1;
4575 else if (XFASTINT (w->total_lines) > 1)
4577 /* Distribute the additional lines of the mini-window
4578 among the other windows. */
4579 Lisp_Object window;
4580 XSETWINDOW (window, w);
4581 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4587 /* Mark window cursors off for all windows in the window tree rooted
4588 at W by setting their phys_cursor_on_p flag to zero. Called from
4589 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4590 the frame are cleared. */
4592 void
4593 mark_window_cursors_off (w)
4594 struct window *w;
4596 while (w)
4598 if (!NILP (w->hchild))
4599 mark_window_cursors_off (XWINDOW (w->hchild));
4600 else if (!NILP (w->vchild))
4601 mark_window_cursors_off (XWINDOW (w->vchild));
4602 else
4603 w->phys_cursor_on_p = 0;
4605 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4610 /* Return number of lines of text (not counting mode lines) in W. */
4613 window_internal_height (w)
4614 struct window *w;
4616 int ht = XFASTINT (w->total_lines);
4618 if (!MINI_WINDOW_P (w))
4620 if (!NILP (w->parent)
4621 || !NILP (w->vchild)
4622 || !NILP (w->hchild)
4623 || !NILP (w->next)
4624 || !NILP (w->prev)
4625 || WINDOW_WANTS_MODELINE_P (w))
4626 --ht;
4628 if (WINDOW_WANTS_HEADER_LINE_P (w))
4629 --ht;
4632 return ht;
4636 /* Return the number of columns in W.
4637 Don't count columns occupied by scroll bars or the vertical bar
4638 separating W from the sibling to its right. */
4641 window_box_text_cols (w)
4642 struct window *w;
4644 struct frame *f = XFRAME (WINDOW_FRAME (w));
4645 int width = XINT (w->total_cols);
4647 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4648 /* Scroll bars occupy a few columns. */
4649 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4650 else if (!FRAME_WINDOW_P (f)
4651 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4652 /* The column of `|' characters separating side-by-side windows
4653 occupies one column only. */
4654 width -= 1;
4656 if (FRAME_WINDOW_P (f))
4657 /* On window-systems, fringes and display margins cannot be
4658 used for normal text. */
4659 width -= (WINDOW_FRINGE_COLS (w)
4660 + WINDOW_LEFT_MARGIN_COLS (w)
4661 + WINDOW_RIGHT_MARGIN_COLS (w));
4663 return width;
4667 /************************************************************************
4668 Window Scrolling
4669 ***********************************************************************/
4671 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4672 N screen-fulls, which is defined as the height of the window minus
4673 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4674 instead. Negative values of N mean scroll down. NOERROR non-zero
4675 means don't signal an error if we try to move over BEGV or ZV,
4676 respectively. */
4678 static void
4679 window_scroll (window, n, whole, noerror)
4680 Lisp_Object window;
4681 int n;
4682 int whole;
4683 int noerror;
4685 immediate_quit = 1;
4687 /* If we must, use the pixel-based version which is much slower than
4688 the line-based one but can handle varying line heights. */
4689 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4690 window_scroll_pixel_based (window, n, whole, noerror);
4691 else
4692 window_scroll_line_based (window, n, whole, noerror);
4694 immediate_quit = 0;
4698 /* Implementation of window_scroll that works based on pixel line
4699 heights. See the comment of window_scroll for parameter
4700 descriptions. */
4702 static void
4703 window_scroll_pixel_based (window, n, whole, noerror)
4704 Lisp_Object window;
4705 int n;
4706 int whole;
4707 int noerror;
4709 struct it it;
4710 struct window *w = XWINDOW (window);
4711 struct text_pos start;
4712 Lisp_Object tem;
4713 int this_scroll_margin;
4714 int preserve_y;
4715 /* True if we fiddled the window vscroll field without really scrolling. */
4716 int vscrolled = 0;
4718 SET_TEXT_POS_FROM_MARKER (start, w->start);
4720 /* If PT is not visible in WINDOW, move back one half of
4721 the screen. Allow PT to be partially visible, otherwise
4722 something like (scroll-down 1) with PT in the line before
4723 the partially visible one would recenter. */
4724 tem = Fpos_visible_in_window_p (make_number (PT), window, Qt);
4725 if (NILP (tem))
4727 /* Move backward half the height of the window. Performance note:
4728 vmotion used here is about 10% faster, but would give wrong
4729 results for variable height lines. */
4730 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4731 it.current_y = it.last_visible_y;
4732 move_it_vertically_backward (&it, window_box_height (w) / 2);
4734 /* The function move_iterator_vertically may move over more than
4735 the specified y-distance. If it->w is small, e.g. a
4736 mini-buffer window, we may end up in front of the window's
4737 display area. This is the case when Start displaying at the
4738 start of the line containing PT in this case. */
4739 if (it.current_y <= 0)
4741 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4742 move_it_vertically_backward (&it, 0);
4743 it.current_y = 0;
4746 start = it.current.pos;
4748 else if (auto_window_vscroll_p)
4750 if (tem = XCAR (XCDR (XCDR (tem))), CONSP (tem))
4752 int px;
4753 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4754 if (whole)
4755 dy = max ((window_box_height (w)
4756 - next_screen_context_lines * dy),
4757 dy);
4758 dy *= n;
4760 if (n < 0 && (px = XINT (XCAR (tem))) > 0)
4762 px = max (0, -w->vscroll - min (px, -dy));
4763 Fset_window_vscroll (window, make_number (px), Qt);
4764 return;
4766 if (n > 0 && (px = XINT (XCDR (tem))) > 0)
4768 px = max (0, -w->vscroll + min (px, dy));
4769 Fset_window_vscroll (window, make_number (px), Qt);
4770 return;
4773 Fset_window_vscroll (window, make_number (0), Qt);
4776 /* If scroll_preserve_screen_position is non-nil, we try to set
4777 point in the same window line as it is now, so get that line. */
4778 if (!NILP (Vscroll_preserve_screen_position))
4780 start_display (&it, w, start);
4781 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4782 preserve_y = it.current_y;
4784 else
4785 preserve_y = -1;
4787 /* Move iterator it from start the specified distance forward or
4788 backward. The result is the new window start. */
4789 start_display (&it, w, start);
4790 if (whole)
4792 int start_pos = IT_CHARPOS (it);
4793 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4794 dy = max ((window_box_height (w)
4795 - next_screen_context_lines * dy),
4796 dy) * n;
4798 /* Note that move_it_vertically always moves the iterator to the
4799 start of a line. So, if the last line doesn't have a newline,
4800 we would end up at the start of the line ending at ZV. */
4801 if (dy <= 0)
4803 move_it_vertically_backward (&it, -dy);
4804 /* Ensure we actually does move, e.g. in case we are currently
4805 looking at an image that is taller that the window height. */
4806 while (start_pos == IT_CHARPOS (it)
4807 && start_pos > BEGV)
4808 move_it_by_lines (&it, -1, 1);
4810 else if (dy > 0)
4812 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4813 MOVE_TO_POS | MOVE_TO_Y);
4814 /* Ensure we actually does move, e.g. in case we are currently
4815 looking at an image that is taller that the window height. */
4816 while (start_pos == IT_CHARPOS (it)
4817 && start_pos < ZV)
4818 move_it_by_lines (&it, 1, 1);
4821 else
4822 move_it_by_lines (&it, n, 1);
4824 /* We failed if we find ZV is already on the screen (scrolling up,
4825 means there's nothing past the end), or if we can't start any
4826 earlier (scrolling down, means there's nothing past the top). */
4827 if ((n > 0 && IT_CHARPOS (it) == ZV)
4828 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4830 if (IT_CHARPOS (it) == ZV)
4832 if (it.current_y < it.last_visible_y
4833 && (it.current_y + it.max_ascent + it.max_descent
4834 >= it.last_visible_y))
4836 /* The last line was only partially visible, make it fully
4837 visible. */
4838 w->vscroll = (it.last_visible_y
4839 - it.current_y + it.max_ascent + it.max_descent);
4840 adjust_glyphs (it.f);
4842 else if (noerror)
4843 return;
4844 else
4845 Fsignal (Qend_of_buffer, Qnil);
4847 else
4849 if (w->vscroll != 0)
4850 /* The first line was only partially visible, make it fully
4851 visible. */
4852 w->vscroll = 0;
4853 else if (noerror)
4854 return;
4855 else
4856 Fsignal (Qbeginning_of_buffer, Qnil);
4859 /* If control gets here, then we vscrolled. */
4861 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4863 /* Don't try to change the window start below. */
4864 vscrolled = 1;
4867 if (! vscrolled)
4869 int pos = IT_CHARPOS (it);
4870 int bytepos;
4872 /* If in the middle of a multi-glyph character move forward to
4873 the next character. */
4874 if (in_display_vector_p (&it))
4876 ++pos;
4877 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4880 /* Set the window start, and set up the window for redisplay. */
4881 set_marker_restricted (w->start, make_number (pos),
4882 w->buffer);
4883 bytepos = XMARKER (w->start)->bytepos;
4884 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4885 ? Qt : Qnil);
4886 w->update_mode_line = Qt;
4887 XSETFASTINT (w->last_modified, 0);
4888 XSETFASTINT (w->last_overlay_modified, 0);
4889 /* Set force_start so that redisplay_window will run the
4890 window-scroll-functions. */
4891 w->force_start = Qt;
4894 /* The rest of this function uses current_y in a nonstandard way,
4895 not including the height of the header line if any. */
4896 it.current_y = it.vpos = 0;
4898 /* Move PT out of scroll margins.
4899 This code wants current_y to be zero at the window start position
4900 even if there is a header line. */
4901 this_scroll_margin = max (0, scroll_margin);
4902 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4903 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4905 if (n > 0)
4907 /* We moved the window start towards ZV, so PT may be now
4908 in the scroll margin at the top. */
4909 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4910 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4911 && (NILP (Vscroll_preserve_screen_position)
4912 || EQ (Vscroll_preserve_screen_position, Qt)))
4913 /* We found PT at a legitimate height. Leave it alone. */
4915 else if (preserve_y >= 0)
4917 /* If we have a header line, take account of it.
4918 This is necessary because we set it.current_y to 0, above. */
4919 if (WINDOW_WANTS_HEADER_LINE_P (w))
4920 preserve_y -= CURRENT_HEADER_LINE_HEIGHT (w);
4922 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
4923 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4925 else
4927 while (it.current_y < this_scroll_margin)
4929 int prev = it.current_y;
4930 move_it_by_lines (&it, 1, 1);
4931 if (prev == it.current_y)
4932 break;
4934 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4937 else if (n < 0)
4939 int charpos, bytepos;
4940 int partial_p;
4942 /* Save our position, for the preserve_y case. */
4943 charpos = IT_CHARPOS (it);
4944 bytepos = IT_BYTEPOS (it);
4946 /* We moved the window start towards BEGV, so PT may be now
4947 in the scroll margin at the bottom. */
4948 move_it_to (&it, PT, -1,
4949 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4950 - this_scroll_margin - 1),
4952 MOVE_TO_POS | MOVE_TO_Y);
4954 /* Save our position, in case it's correct. */
4955 charpos = IT_CHARPOS (it);
4956 bytepos = IT_BYTEPOS (it);
4958 /* See if point is on a partially visible line at the end. */
4959 if (it.what == IT_EOB)
4960 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4961 else
4963 move_it_by_lines (&it, 1, 1);
4964 partial_p = it.current_y > it.last_visible_y;
4967 if (charpos == PT && !partial_p
4968 && (NILP (Vscroll_preserve_screen_position)
4969 || EQ (Vscroll_preserve_screen_position, Qt)))
4970 /* We found PT before we found the display margin, so PT is ok. */
4972 else if (preserve_y >= 0)
4974 SET_TEXT_POS_FROM_MARKER (start, w->start);
4975 start_display (&it, w, start);
4976 #if 0 /* It's wrong to subtract this here
4977 because we called start_display again
4978 and did not alter it.current_y this time. */
4980 /* If we have a header line, take account of it. */
4981 if (WINDOW_WANTS_HEADER_LINE_P (w))
4982 preserve_y -= CURRENT_HEADER_LINE_HEIGHT (w);
4983 #endif
4985 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
4986 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4988 else
4990 if (partial_p)
4991 /* The last line was only partially visible, so back up two
4992 lines to make sure we're on a fully visible line. */
4994 move_it_by_lines (&it, -2, 0);
4995 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4997 else
4998 /* No, the position we saved is OK, so use it. */
4999 SET_PT_BOTH (charpos, bytepos);
5005 /* Implementation of window_scroll that works based on screen lines.
5006 See the comment of window_scroll for parameter descriptions. */
5008 static void
5009 window_scroll_line_based (window, n, whole, noerror)
5010 Lisp_Object window;
5011 int n;
5012 int whole;
5013 int noerror;
5015 register struct window *w = XWINDOW (window);
5016 register int opoint = PT, opoint_byte = PT_BYTE;
5017 register int pos, pos_byte;
5018 register int ht = window_internal_height (w);
5019 register Lisp_Object tem;
5020 int lose;
5021 Lisp_Object bolp;
5022 int startpos;
5023 struct position posit;
5024 int original_vpos;
5026 /* If scrolling screen-fulls, compute the number of lines to
5027 scroll from the window's height. */
5028 if (whole)
5029 n *= max (1, ht - next_screen_context_lines);
5031 startpos = marker_position (w->start);
5033 posit = *compute_motion (startpos, 0, 0, 0,
5034 PT, ht, 0,
5035 -1, XINT (w->hscroll),
5036 0, w);
5037 original_vpos = posit.vpos;
5039 XSETFASTINT (tem, PT);
5040 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5042 if (NILP (tem))
5044 Fvertical_motion (make_number (- (ht / 2)), window);
5045 startpos = PT;
5048 SET_PT (startpos);
5049 lose = n < 0 && PT == BEGV;
5050 Fvertical_motion (make_number (n), window);
5051 pos = PT;
5052 pos_byte = PT_BYTE;
5053 bolp = Fbolp ();
5054 SET_PT_BOTH (opoint, opoint_byte);
5056 if (lose)
5058 if (noerror)
5059 return;
5060 else
5061 Fsignal (Qbeginning_of_buffer, Qnil);
5064 if (pos < ZV)
5066 int this_scroll_margin = scroll_margin;
5068 /* Don't use a scroll margin that is negative or too large. */
5069 if (this_scroll_margin < 0)
5070 this_scroll_margin = 0;
5072 if (XINT (w->total_lines) < 4 * scroll_margin)
5073 this_scroll_margin = XINT (w->total_lines) / 4;
5075 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5076 w->start_at_line_beg = bolp;
5077 w->update_mode_line = Qt;
5078 XSETFASTINT (w->last_modified, 0);
5079 XSETFASTINT (w->last_overlay_modified, 0);
5080 /* Set force_start so that redisplay_window will run
5081 the window-scroll-functions. */
5082 w->force_start = Qt;
5084 if (!NILP (Vscroll_preserve_screen_position)
5085 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5087 SET_PT_BOTH (pos, pos_byte);
5088 Fvertical_motion (make_number (original_vpos), window);
5090 /* If we scrolled forward, put point enough lines down
5091 that it is outside the scroll margin. */
5092 else if (n > 0)
5094 int top_margin;
5096 if (this_scroll_margin > 0)
5098 SET_PT_BOTH (pos, pos_byte);
5099 Fvertical_motion (make_number (this_scroll_margin), window);
5100 top_margin = PT;
5102 else
5103 top_margin = pos;
5105 if (top_margin <= opoint)
5106 SET_PT_BOTH (opoint, opoint_byte);
5107 else if (!NILP (Vscroll_preserve_screen_position))
5109 SET_PT_BOTH (pos, pos_byte);
5110 Fvertical_motion (make_number (original_vpos), window);
5112 else
5113 SET_PT (top_margin);
5115 else if (n < 0)
5117 int bottom_margin;
5119 /* If we scrolled backward, put point near the end of the window
5120 but not within the scroll margin. */
5121 SET_PT_BOTH (pos, pos_byte);
5122 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5123 if (XFASTINT (tem) == ht - this_scroll_margin)
5124 bottom_margin = PT;
5125 else
5126 bottom_margin = PT + 1;
5128 if (bottom_margin > opoint)
5129 SET_PT_BOTH (opoint, opoint_byte);
5130 else
5132 if (!NILP (Vscroll_preserve_screen_position))
5134 SET_PT_BOTH (pos, pos_byte);
5135 Fvertical_motion (make_number (original_vpos), window);
5137 else
5138 Fvertical_motion (make_number (-1), window);
5142 else
5144 if (noerror)
5145 return;
5146 else
5147 Fsignal (Qend_of_buffer, Qnil);
5152 /* Scroll selected_window up or down. If N is nil, scroll a
5153 screen-full which is defined as the height of the window minus
5154 next_screen_context_lines. If N is the symbol `-', scroll.
5155 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5156 up. This is the guts of Fscroll_up and Fscroll_down. */
5158 static void
5159 scroll_command (n, direction)
5160 Lisp_Object n;
5161 int direction;
5163 int count = SPECPDL_INDEX ();
5165 xassert (abs (direction) == 1);
5167 /* If selected window's buffer isn't current, make it current for
5168 the moment. But don't screw up if window_scroll gets an error. */
5169 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5171 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5172 Fset_buffer (XWINDOW (selected_window)->buffer);
5174 /* Make redisplay consider other windows than just selected_window. */
5175 ++windows_or_buffers_changed;
5178 if (NILP (n))
5179 window_scroll (selected_window, direction, 1, 0);
5180 else if (EQ (n, Qminus))
5181 window_scroll (selected_window, -direction, 1, 0);
5182 else
5184 n = Fprefix_numeric_value (n);
5185 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5188 unbind_to (count, Qnil);
5191 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
5192 doc: /* Scroll text of current window upward ARG lines.
5193 If ARG is omitted or nil, scroll upward by a near full screen.
5194 A near full screen is `next-screen-context-lines' less than a full screen.
5195 Negative ARG means scroll downward.
5196 If ARG is the atom `-', scroll downward by nearly full screen.
5197 When calling from a program, supply as argument a number, nil, or `-'. */)
5198 (arg)
5199 Lisp_Object arg;
5201 scroll_command (arg, 1);
5202 return Qnil;
5205 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
5206 doc: /* Scroll text of current window down ARG lines.
5207 If ARG is omitted or nil, scroll down by a near full screen.
5208 A near full screen is `next-screen-context-lines' less than a full screen.
5209 Negative ARG means scroll upward.
5210 If ARG is the atom `-', scroll upward by nearly full screen.
5211 When calling from a program, supply as argument a number, nil, or `-'. */)
5212 (arg)
5213 Lisp_Object arg;
5215 scroll_command (arg, -1);
5216 return Qnil;
5219 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5220 doc: /* Return the other window for \"other window scroll\" commands.
5221 If `other-window-scroll-buffer' is non-nil, a window
5222 showing that buffer is used.
5223 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5224 specifies the window. This takes precedence over
5225 `other-window-scroll-buffer'. */)
5228 Lisp_Object window;
5230 if (MINI_WINDOW_P (XWINDOW (selected_window))
5231 && !NILP (Vminibuf_scroll_window))
5232 window = Vminibuf_scroll_window;
5233 /* If buffer is specified, scroll that buffer. */
5234 else if (!NILP (Vother_window_scroll_buffer))
5236 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5237 if (NILP (window))
5238 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5240 else
5242 /* Nothing specified; look for a neighboring window on the same
5243 frame. */
5244 window = Fnext_window (selected_window, Qnil, Qnil);
5246 if (EQ (window, selected_window))
5247 /* That didn't get us anywhere; look for a window on another
5248 visible frame. */
5250 window = Fnext_window (window, Qnil, Qt);
5251 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5252 && ! EQ (window, selected_window));
5255 CHECK_LIVE_WINDOW (window);
5257 if (EQ (window, selected_window))
5258 error ("There is no other window");
5260 return window;
5263 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5264 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5265 A near full screen is `next-screen-context-lines' less than a full screen.
5266 The next window is the one below the current one; or the one at the top
5267 if the current one is at the bottom. Negative ARG means scroll downward.
5268 If ARG is the atom `-', scroll downward by nearly full screen.
5269 When calling from a program, supply as argument a number, nil, or `-'.
5271 If `other-window-scroll-buffer' is non-nil, scroll the window
5272 showing that buffer, popping the buffer up if necessary.
5273 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5274 specifies the window to scroll. This takes precedence over
5275 `other-window-scroll-buffer'. */)
5276 (arg)
5277 Lisp_Object arg;
5279 Lisp_Object window;
5280 struct window *w;
5281 int count = SPECPDL_INDEX ();
5283 window = Fother_window_for_scrolling ();
5284 w = XWINDOW (window);
5286 /* Don't screw up if window_scroll gets an error. */
5287 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5288 ++windows_or_buffers_changed;
5290 Fset_buffer (w->buffer);
5291 SET_PT (marker_position (w->pointm));
5293 if (NILP (arg))
5294 window_scroll (window, 1, 1, 1);
5295 else if (EQ (arg, Qminus))
5296 window_scroll (window, -1, 1, 1);
5297 else
5299 if (CONSP (arg))
5300 arg = Fcar (arg);
5301 CHECK_NUMBER (arg);
5302 window_scroll (window, XINT (arg), 0, 1);
5305 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5306 unbind_to (count, Qnil);
5308 return Qnil;
5311 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "P\np",
5312 doc: /* Scroll selected window display ARG columns left.
5313 Default for ARG is window width minus 2.
5314 Value is the total amount of leftward horizontal scrolling in
5315 effect after the change.
5316 If SET_MINIMUM is non-nil, the new scroll amount becomes the
5317 lower bound for automatic scrolling, i.e. automatic scrolling
5318 will not scroll a window to a column less than the value returned
5319 by this function. This happens in an interactive call. */)
5320 (arg, set_minimum)
5321 register Lisp_Object arg, set_minimum;
5323 Lisp_Object result;
5324 int hscroll;
5325 struct window *w = XWINDOW (selected_window);
5327 if (NILP (arg))
5328 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5329 else
5330 arg = Fprefix_numeric_value (arg);
5332 hscroll = XINT (w->hscroll) + XINT (arg);
5333 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5335 if (!NILP (set_minimum))
5336 w->min_hscroll = w->hscroll;
5338 return result;
5341 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "P\np",
5342 doc: /* Scroll selected window display ARG columns right.
5343 Default for ARG is window width minus 2.
5344 Value is the total amount of leftward horizontal scrolling in
5345 effect after the change.
5346 If SET_MINIMUM is non-nil, the new scroll amount becomes the
5347 lower bound for automatic scrolling, i.e. automatic scrolling
5348 will not scroll a window to a column less than the value returned
5349 by this function. This happens in an interactive call. */)
5350 (arg, set_minimum)
5351 register Lisp_Object arg, set_minimum;
5353 Lisp_Object result;
5354 int hscroll;
5355 struct window *w = XWINDOW (selected_window);
5357 if (NILP (arg))
5358 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5359 else
5360 arg = Fprefix_numeric_value (arg);
5362 hscroll = XINT (w->hscroll) - XINT (arg);
5363 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5365 if (!NILP (set_minimum))
5366 w->min_hscroll = w->hscroll;
5368 return result;
5371 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5372 doc: /* Return the window which was selected when entering the minibuffer.
5373 Returns nil, if current window is not a minibuffer window. */)
5376 if (minibuf_level > 0
5377 && MINI_WINDOW_P (XWINDOW (selected_window))
5378 && WINDOW_LIVE_P (minibuf_selected_window))
5379 return minibuf_selected_window;
5381 return Qnil;
5384 /* Value is the number of lines actually displayed in window W,
5385 as opposed to its height. */
5387 static int
5388 displayed_window_lines (w)
5389 struct window *w;
5391 struct it it;
5392 struct text_pos start;
5393 int height = window_box_height (w);
5394 struct buffer *old_buffer;
5395 int bottom_y;
5397 if (XBUFFER (w->buffer) != current_buffer)
5399 old_buffer = current_buffer;
5400 set_buffer_internal (XBUFFER (w->buffer));
5402 else
5403 old_buffer = NULL;
5405 /* In case W->start is out of the accessible range, do something
5406 reasonable. This happens in Info mode when Info-scroll-down
5407 calls (recenter -1) while W->start is 1. */
5408 if (XMARKER (w->start)->charpos < BEGV)
5409 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5410 else if (XMARKER (w->start)->charpos > ZV)
5411 SET_TEXT_POS (start, ZV, ZV_BYTE);
5412 else
5413 SET_TEXT_POS_FROM_MARKER (start, w->start);
5415 start_display (&it, w, start);
5416 move_it_vertically (&it, height);
5417 bottom_y = line_bottom_y (&it);
5419 /* rms: On a non-window display,
5420 the value of it.vpos at the bottom of the screen
5421 seems to be 1 larger than window_box_height (w).
5422 This kludge fixes a bug whereby (move-to-window-line -1)
5423 when ZV is on the last screen line
5424 moves to the previous screen line instead of the last one. */
5425 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5426 height++;
5428 /* Add in empty lines at the bottom of the window. */
5429 if (bottom_y < height)
5431 int uy = FRAME_LINE_HEIGHT (it.f);
5432 it.vpos += (height - bottom_y + uy - 1) / uy;
5435 if (old_buffer)
5436 set_buffer_internal (old_buffer);
5438 return it.vpos;
5442 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5443 doc: /* Center point in window and redisplay frame.
5444 With prefix argument ARG, recenter putting point on screen line ARG
5445 relative to the current window. If ARG is negative, it counts up from the
5446 bottom of the window. (ARG should be less than the height of the window.)
5448 If ARG is omitted or nil, erase the entire frame and then
5449 redraw with point in the center of the current window.
5450 Just C-u as prefix means put point in the center of the window
5451 and redisplay normally--don't erase and redraw the frame. */)
5452 (arg)
5453 register Lisp_Object arg;
5455 struct window *w = XWINDOW (selected_window);
5456 struct buffer *buf = XBUFFER (w->buffer);
5457 struct buffer *obuf = current_buffer;
5458 int center_p = 0;
5459 int charpos, bytepos;
5460 int iarg;
5461 int this_scroll_margin;
5463 /* If redisplay is suppressed due to an error, try again. */
5464 obuf->display_error_modiff = 0;
5466 if (NILP (arg))
5468 int i;
5470 /* Invalidate pixel data calculated for all compositions. */
5471 for (i = 0; i < n_compositions; i++)
5472 composition_table[i]->font = NULL;
5474 Fredraw_frame (w->frame);
5475 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
5476 center_p = 1;
5478 else if (CONSP (arg)) /* Just C-u. */
5479 center_p = 1;
5480 else
5482 arg = Fprefix_numeric_value (arg);
5483 CHECK_NUMBER (arg);
5484 iarg = XINT (arg);
5487 set_buffer_internal (buf);
5489 /* Do this after making BUF current
5490 in case scroll_margin is buffer-local. */
5491 this_scroll_margin = max (0, scroll_margin);
5492 this_scroll_margin = min (this_scroll_margin,
5493 XFASTINT (w->total_lines) / 4);
5495 /* Handle centering on a graphical frame specially. Such frames can
5496 have variable-height lines and centering point on the basis of
5497 line counts would lead to strange effects. */
5498 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5500 if (center_p)
5502 struct it it;
5503 struct text_pos pt;
5505 SET_TEXT_POS (pt, PT, PT_BYTE);
5506 start_display (&it, w, pt);
5507 move_it_vertically_backward (&it, window_box_height (w) / 2);
5508 charpos = IT_CHARPOS (it);
5509 bytepos = IT_BYTEPOS (it);
5511 else if (iarg < 0)
5513 struct it it;
5514 struct text_pos pt;
5515 int nlines = -iarg;
5516 int extra_line_spacing;
5517 int h = window_box_height (w);
5519 iarg = - max (-iarg, this_scroll_margin);
5521 SET_TEXT_POS (pt, PT, PT_BYTE);
5522 start_display (&it, w, pt);
5524 /* Be sure we have the exact height of the full line containing PT. */
5525 move_it_by_lines (&it, 0, 1);
5527 /* The amount of pixels we have to move back is the window
5528 height minus what's displayed in the line containing PT,
5529 and the lines below. */
5530 it.current_y = 0;
5531 it.vpos = 0;
5532 move_it_by_lines (&it, nlines, 1);
5534 if (it.vpos == nlines)
5535 h -= it.current_y;
5536 else
5538 /* Last line has no newline */
5539 h -= line_bottom_y (&it);
5540 it.vpos++;
5543 /* Don't reserve space for extra line spacing of last line. */
5544 extra_line_spacing = it.max_extra_line_spacing;
5546 /* If we can't move down NLINES lines because we hit
5547 the end of the buffer, count in some empty lines. */
5548 if (it.vpos < nlines)
5550 nlines -= it.vpos;
5551 extra_line_spacing = it.extra_line_spacing;
5552 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5554 if (h <= 0)
5555 return Qnil;
5557 /* Now find the new top line (starting position) of the window. */
5558 start_display (&it, w, pt);
5559 it.current_y = 0;
5560 move_it_vertically_backward (&it, h);
5562 /* If extra line spacing is present, we may move too far
5563 back. This causes the last line to be only partially
5564 visible (which triggers redisplay to recenter that line
5565 in the middle), so move forward.
5566 But ignore extra line spacing on last line, as it is not
5567 considered to be part of the visible height of the line.
5569 h += extra_line_spacing;
5570 while (-it.current_y > h)
5571 move_it_by_lines (&it, 1, 1);
5573 charpos = IT_CHARPOS (it);
5574 bytepos = IT_BYTEPOS (it);
5576 else
5578 struct position pos;
5580 iarg = max (iarg, this_scroll_margin);
5582 pos = *vmotion (PT, -iarg, w);
5583 charpos = pos.bufpos;
5584 bytepos = pos.bytepos;
5587 else
5589 struct position pos;
5590 int ht = window_internal_height (w);
5592 if (center_p)
5593 iarg = ht / 2;
5594 else if (iarg < 0)
5595 iarg += ht;
5597 /* Don't let it get into the margin at either top or bottom. */
5598 iarg = max (iarg, this_scroll_margin);
5599 iarg = min (iarg, ht - this_scroll_margin - 1);
5601 pos = *vmotion (PT, - iarg, w);
5602 charpos = pos.bufpos;
5603 bytepos = pos.bytepos;
5606 /* Set the new window start. */
5607 set_marker_both (w->start, w->buffer, charpos, bytepos);
5608 w->window_end_valid = Qnil;
5610 w->optional_new_start = Qt;
5612 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5613 w->start_at_line_beg = Qt;
5614 else
5615 w->start_at_line_beg = Qnil;
5617 set_buffer_internal (obuf);
5618 return Qnil;
5622 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5623 0, 1, 0,
5624 doc: /* Return the height in lines of the text display area of WINDOW.
5625 This doesn't include the mode-line (or header-line if any) or any
5626 partial-height lines in the text display area. */)
5627 (window)
5628 Lisp_Object window;
5630 struct window *w = decode_window (window);
5631 int pixel_height = window_box_height (w);
5632 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5633 return make_number (line_height);
5638 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5639 1, 1, "P",
5640 doc: /* Position point relative to window.
5641 With no argument, position point at center of window.
5642 An argument specifies vertical position within the window;
5643 zero means top of window, negative means relative to bottom of window. */)
5644 (arg)
5645 Lisp_Object arg;
5647 struct window *w = XWINDOW (selected_window);
5648 int lines, start;
5649 Lisp_Object window;
5650 #if 0
5651 int this_scroll_margin;
5652 #endif
5654 window = selected_window;
5655 start = marker_position (w->start);
5656 if (start < BEGV || start > ZV)
5658 int height = window_internal_height (w);
5659 Fvertical_motion (make_number (- (height / 2)), window);
5660 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5661 w->start_at_line_beg = Fbolp ();
5662 w->force_start = Qt;
5664 else
5665 Fgoto_char (w->start);
5667 lines = displayed_window_lines (w);
5669 #if 0
5670 this_scroll_margin = max (0, scroll_margin);
5671 this_scroll_margin = min (this_scroll_margin, lines / 4);
5672 #endif
5674 if (NILP (arg))
5675 XSETFASTINT (arg, lines / 2);
5676 else
5678 int iarg = XINT (Fprefix_numeric_value (arg));
5680 if (iarg < 0)
5681 iarg = iarg + lines;
5683 #if 0 /* This code would prevent move-to-window-line from moving point
5684 to a place inside the scroll margins (which would cause the
5685 next redisplay to scroll). I wrote this code, but then concluded
5686 it is probably better not to install it. However, it is here
5687 inside #if 0 so as not to lose it. -- rms. */
5689 /* Don't let it get into the margin at either top or bottom. */
5690 iarg = max (iarg, this_scroll_margin);
5691 iarg = min (iarg, lines - this_scroll_margin - 1);
5692 #endif
5694 arg = make_number (iarg);
5697 /* Skip past a partially visible first line. */
5698 if (w->vscroll)
5699 XSETINT (arg, XINT (arg) + 1);
5701 return Fvertical_motion (arg, window);
5706 /***********************************************************************
5707 Window Configuration
5708 ***********************************************************************/
5710 struct save_window_data
5712 EMACS_INT size_from_Lisp_Vector_struct;
5713 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5714 Lisp_Object frame_cols, frame_lines, frame_menu_bar_lines;
5715 Lisp_Object frame_tool_bar_lines;
5716 Lisp_Object selected_frame;
5717 Lisp_Object current_window;
5718 Lisp_Object current_buffer;
5719 Lisp_Object minibuf_scroll_window;
5720 Lisp_Object minibuf_selected_window;
5721 Lisp_Object root_window;
5722 Lisp_Object focus_frame;
5723 /* Record the values of window-min-width and window-min-height
5724 so that window sizes remain consistent with them. */
5725 Lisp_Object min_width, min_height;
5726 /* A vector, each of whose elements is a struct saved_window
5727 for one window. */
5728 Lisp_Object saved_windows;
5731 /* This is saved as a Lisp_Vector */
5732 struct saved_window
5734 /* these first two must agree with struct Lisp_Vector in lisp.h */
5735 EMACS_INT size_from_Lisp_Vector_struct;
5736 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5738 Lisp_Object window;
5739 Lisp_Object buffer, start, pointm, mark;
5740 Lisp_Object left_col, top_line, total_cols, total_lines;
5741 Lisp_Object hscroll, min_hscroll;
5742 Lisp_Object parent, prev;
5743 Lisp_Object start_at_line_beg;
5744 Lisp_Object display_table;
5745 Lisp_Object orig_top_line, orig_total_lines;
5746 Lisp_Object left_margin_cols, right_margin_cols;
5747 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5748 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5751 #define SAVED_WINDOW_N(swv,n) \
5752 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5754 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5755 doc: /* Return t if OBJECT is a window-configuration object. */)
5756 (object)
5757 Lisp_Object object;
5759 if (WINDOW_CONFIGURATIONP (object))
5760 return Qt;
5761 return Qnil;
5764 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5765 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5766 (config)
5767 Lisp_Object config;
5769 register struct save_window_data *data;
5770 struct Lisp_Vector *saved_windows;
5772 if (! WINDOW_CONFIGURATIONP (config))
5773 wrong_type_argument (Qwindow_configuration_p, config);
5775 data = (struct save_window_data *) XVECTOR (config);
5776 saved_windows = XVECTOR (data->saved_windows);
5777 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5780 DEFUN ("set-window-configuration", Fset_window_configuration,
5781 Sset_window_configuration, 1, 1, 0,
5782 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5783 CONFIGURATION must be a value previously returned
5784 by `current-window-configuration' (which see).
5785 If CONFIGURATION was made from a frame that is now deleted,
5786 only frame-independent values can be restored. In this case,
5787 the return value is nil. Otherwise the value is t. */)
5788 (configuration)
5789 Lisp_Object configuration;
5791 register struct save_window_data *data;
5792 struct Lisp_Vector *saved_windows;
5793 Lisp_Object new_current_buffer;
5794 Lisp_Object frame;
5795 FRAME_PTR f;
5796 int old_point = -1;
5798 while (!WINDOW_CONFIGURATIONP (configuration))
5799 wrong_type_argument (Qwindow_configuration_p, configuration);
5801 data = (struct save_window_data *) XVECTOR (configuration);
5802 saved_windows = XVECTOR (data->saved_windows);
5804 new_current_buffer = data->current_buffer;
5805 if (NILP (XBUFFER (new_current_buffer)->name))
5806 new_current_buffer = Qnil;
5807 else
5809 if (XBUFFER (new_current_buffer) == current_buffer)
5810 /* The code further down "preserves point" by saving here PT in
5811 old_point and then setting it later back into PT. When the
5812 current-selected-window and the final-selected-window both show
5813 the current buffer, this suffers from the problem that the
5814 current PT is the window-point of the current-selected-window,
5815 while the final PT is the point of the final-selected-window, so
5816 this copy from one PT to the other would end up moving the
5817 window-point of the final-selected-window to the window-point of
5818 the current-selected-window. So we have to be careful which
5819 point of the current-buffer we copy into old_point. */
5820 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5821 && WINDOWP (selected_window)
5822 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5823 && !EQ (selected_window, data->current_window))
5824 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5825 else
5826 old_point = PT;
5827 else
5828 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5829 point in new_current_buffer as of the last time this buffer was
5830 used. This can be non-deterministic since it can be changed by
5831 things like jit-lock by mere temporary selection of some random
5832 window that happens to show this buffer.
5833 So if possible we want this arbitrary choice of "which point" to
5834 be the one from the to-be-selected-window so as to prevent this
5835 window's cursor from being copied from another window. */
5836 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5837 /* If current_window = selected_window, its point is in BUF_PT. */
5838 && !EQ (selected_window, data->current_window))
5839 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5840 else
5841 old_point = BUF_PT (XBUFFER (new_current_buffer));
5844 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5845 f = XFRAME (frame);
5847 /* If f is a dead frame, don't bother rebuilding its window tree.
5848 However, there is other stuff we should still try to do below. */
5849 if (FRAME_LIVE_P (f))
5851 register struct window *w;
5852 register struct saved_window *p;
5853 struct window *root_window;
5854 struct window **leaf_windows;
5855 int n_leaf_windows;
5856 int k, i, n;
5858 /* If the frame has been resized since this window configuration was
5859 made, we change the frame to the size specified in the
5860 configuration, restore the configuration, and then resize it
5861 back. We keep track of the prevailing height in these variables. */
5862 int previous_frame_lines = FRAME_LINES (f);
5863 int previous_frame_cols = FRAME_COLS (f);
5864 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5865 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5867 /* The mouse highlighting code could get screwed up
5868 if it runs during this. */
5869 BLOCK_INPUT;
5871 if (XFASTINT (data->frame_lines) != previous_frame_lines
5872 || XFASTINT (data->frame_cols) != previous_frame_cols)
5873 change_frame_size (f, XFASTINT (data->frame_lines),
5874 XFASTINT (data->frame_cols), 0, 0, 0);
5875 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5876 if (XFASTINT (data->frame_menu_bar_lines)
5877 != previous_frame_menu_bar_lines)
5878 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
5879 #ifdef HAVE_WINDOW_SYSTEM
5880 if (XFASTINT (data->frame_tool_bar_lines)
5881 != previous_frame_tool_bar_lines)
5882 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
5883 #endif
5884 #endif
5886 /* "Swap out" point from the selected window's buffer
5887 into the window itself. (Normally the pointm of the selected
5888 window holds garbage.) We do this now, before
5889 restoring the window contents, and prevent it from
5890 being done later on when we select a new window. */
5891 if (! NILP (XWINDOW (selected_window)->buffer))
5893 w = XWINDOW (selected_window);
5894 set_marker_both (w->pointm,
5895 w->buffer,
5896 BUF_PT (XBUFFER (w->buffer)),
5897 BUF_PT_BYTE (XBUFFER (w->buffer)));
5900 windows_or_buffers_changed++;
5901 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5903 /* Problem: Freeing all matrices and later allocating them again
5904 is a serious redisplay flickering problem. What we would
5905 really like to do is to free only those matrices not reused
5906 below. */
5907 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5908 leaf_windows
5909 = (struct window **) alloca (count_windows (root_window)
5910 * sizeof (struct window *));
5911 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5913 /* Temporarily avoid any problems with windows that are smaller
5914 than they are supposed to be. */
5915 window_min_height = 1;
5916 window_min_width = 1;
5918 /* Kludge Alert!
5919 Mark all windows now on frame as "deleted".
5920 Restoring the new configuration "undeletes" any that are in it.
5922 Save their current buffers in their height fields, since we may
5923 need it later, if a buffer saved in the configuration is now
5924 dead. */
5925 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5927 for (k = 0; k < saved_windows->size; k++)
5929 p = SAVED_WINDOW_N (saved_windows, k);
5930 w = XWINDOW (p->window);
5931 w->next = Qnil;
5933 if (!NILP (p->parent))
5934 w->parent = SAVED_WINDOW_N (saved_windows,
5935 XFASTINT (p->parent))->window;
5936 else
5937 w->parent = Qnil;
5939 if (!NILP (p->prev))
5941 w->prev = SAVED_WINDOW_N (saved_windows,
5942 XFASTINT (p->prev))->window;
5943 XWINDOW (w->prev)->next = p->window;
5945 else
5947 w->prev = Qnil;
5948 if (!NILP (w->parent))
5950 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5952 XWINDOW (w->parent)->vchild = p->window;
5953 XWINDOW (w->parent)->hchild = Qnil;
5955 else
5957 XWINDOW (w->parent)->hchild = p->window;
5958 XWINDOW (w->parent)->vchild = Qnil;
5963 /* If we squirreled away the buffer in the window's height,
5964 restore it now. */
5965 if (BUFFERP (w->total_lines))
5966 w->buffer = w->total_lines;
5967 w->left_col = p->left_col;
5968 w->top_line = p->top_line;
5969 w->total_cols = p->total_cols;
5970 w->total_lines = p->total_lines;
5971 w->hscroll = p->hscroll;
5972 w->min_hscroll = p->min_hscroll;
5973 w->display_table = p->display_table;
5974 w->orig_top_line = p->orig_top_line;
5975 w->orig_total_lines = p->orig_total_lines;
5976 w->left_margin_cols = p->left_margin_cols;
5977 w->right_margin_cols = p->right_margin_cols;
5978 w->left_fringe_width = p->left_fringe_width;
5979 w->right_fringe_width = p->right_fringe_width;
5980 w->fringes_outside_margins = p->fringes_outside_margins;
5981 w->scroll_bar_width = p->scroll_bar_width;
5982 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
5983 XSETFASTINT (w->last_modified, 0);
5984 XSETFASTINT (w->last_overlay_modified, 0);
5986 /* Reinstall the saved buffer and pointers into it. */
5987 if (NILP (p->buffer))
5988 w->buffer = p->buffer;
5989 else
5991 if (!NILP (XBUFFER (p->buffer)->name))
5992 /* If saved buffer is alive, install it. */
5994 w->buffer = p->buffer;
5995 w->start_at_line_beg = p->start_at_line_beg;
5996 set_marker_restricted (w->start, p->start, w->buffer);
5997 set_marker_restricted (w->pointm, p->pointm, w->buffer);
5998 Fset_marker (XBUFFER (w->buffer)->mark,
5999 p->mark, w->buffer);
6001 /* As documented in Fcurrent_window_configuration, don't
6002 restore the location of point in the buffer which was
6003 current when the window configuration was recorded. */
6004 if (!EQ (p->buffer, new_current_buffer)
6005 && XBUFFER (p->buffer) == current_buffer)
6006 Fgoto_char (w->pointm);
6008 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
6009 /* Else unless window has a live buffer, get one. */
6011 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6012 /* This will set the markers to beginning of visible
6013 range. */
6014 set_marker_restricted (w->start, make_number (0), w->buffer);
6015 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6016 w->start_at_line_beg = Qt;
6018 else
6019 /* Keeping window's old buffer; make sure the markers
6020 are real. */
6022 /* Set window markers at start of visible range. */
6023 if (XMARKER (w->start)->buffer == 0)
6024 set_marker_restricted (w->start, make_number (0),
6025 w->buffer);
6026 if (XMARKER (w->pointm)->buffer == 0)
6027 set_marker_restricted_both (w->pointm, w->buffer,
6028 BUF_PT (XBUFFER (w->buffer)),
6029 BUF_PT_BYTE (XBUFFER (w->buffer)));
6030 w->start_at_line_beg = Qt;
6035 FRAME_ROOT_WINDOW (f) = data->root_window;
6036 /* Prevent "swapping out point" in the old selected window
6037 using the buffer that has been restored into it.
6038 We already swapped out point that from that window's old buffer. */
6039 selected_window = Qnil;
6041 /* Arrange *not* to restore point in the buffer that was
6042 current when the window configuration was saved. */
6043 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6044 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6045 make_number (old_point),
6046 XWINDOW (data->current_window)->buffer);
6048 Fselect_window (data->current_window, Qnil);
6049 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
6050 = selected_window;
6052 if (NILP (data->focus_frame)
6053 || (FRAMEP (data->focus_frame)
6054 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6055 Fredirect_frame_focus (frame, data->focus_frame);
6057 #if 0 /* I don't understand why this is needed, and it causes problems
6058 when the frame's old selected window has been deleted. */
6059 if (f != selected_frame && FRAME_WINDOW_P (f))
6060 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
6061 0, 0);
6062 #endif
6064 /* Set the screen height to the value it had before this function. */
6065 if (previous_frame_lines != FRAME_LINES (f)
6066 || previous_frame_cols != FRAME_COLS (f))
6067 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6068 0, 0, 0);
6069 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6070 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6071 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6072 make_number (0));
6073 #ifdef HAVE_WINDOW_SYSTEM
6074 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6075 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6076 make_number (0));
6077 #endif
6078 #endif
6080 /* Now, free glyph matrices in windows that were not reused. */
6081 for (i = n = 0; i < n_leaf_windows; ++i)
6083 if (NILP (leaf_windows[i]->buffer))
6085 /* Assert it's not reused as a combination. */
6086 xassert (NILP (leaf_windows[i]->hchild)
6087 && NILP (leaf_windows[i]->vchild));
6088 free_window_matrices (leaf_windows[i]);
6090 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6091 ++n;
6094 adjust_glyphs (f);
6096 UNBLOCK_INPUT;
6098 /* Fselect_window will have made f the selected frame, so we
6099 reselect the proper frame here. Fhandle_switch_frame will change the
6100 selected window too, but that doesn't make the call to
6101 Fselect_window above totally superfluous; it still sets f's
6102 selected window. */
6103 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6104 do_switch_frame (data->selected_frame, 0, 0);
6106 if (! NILP (Vwindow_configuration_change_hook)
6107 && ! NILP (Vrun_hooks))
6108 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
6111 if (!NILP (new_current_buffer))
6112 Fset_buffer (new_current_buffer);
6114 /* Restore the minimum heights recorded in the configuration. */
6115 window_min_height = XINT (data->min_height);
6116 window_min_width = XINT (data->min_width);
6118 Vminibuf_scroll_window = data->minibuf_scroll_window;
6119 minibuf_selected_window = data->minibuf_selected_window;
6121 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6124 /* Mark all windows now on frame as deleted
6125 by setting their buffers to nil. */
6127 void
6128 delete_all_subwindows (w)
6129 register struct window *w;
6131 if (!NILP (w->next))
6132 delete_all_subwindows (XWINDOW (w->next));
6133 if (!NILP (w->vchild))
6134 delete_all_subwindows (XWINDOW (w->vchild));
6135 if (!NILP (w->hchild))
6136 delete_all_subwindows (XWINDOW (w->hchild));
6138 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6140 if (!NILP (w->buffer))
6141 unshow_buffer (w);
6143 /* We set all three of these fields to nil, to make sure that we can
6144 distinguish this dead window from any live window. Live leaf
6145 windows will have buffer set, and combination windows will have
6146 vchild or hchild set. */
6147 w->buffer = Qnil;
6148 w->vchild = Qnil;
6149 w->hchild = Qnil;
6151 Vwindow_list = Qnil;
6154 static int
6155 count_windows (window)
6156 register struct window *window;
6158 register int count = 1;
6159 if (!NILP (window->next))
6160 count += count_windows (XWINDOW (window->next));
6161 if (!NILP (window->vchild))
6162 count += count_windows (XWINDOW (window->vchild));
6163 if (!NILP (window->hchild))
6164 count += count_windows (XWINDOW (window->hchild));
6165 return count;
6169 /* Fill vector FLAT with leaf windows under W, starting at index I.
6170 Value is last index + 1. */
6172 static int
6173 get_leaf_windows (w, flat, i)
6174 struct window *w;
6175 struct window **flat;
6176 int i;
6178 while (w)
6180 if (!NILP (w->hchild))
6181 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6182 else if (!NILP (w->vchild))
6183 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6184 else
6185 flat[i++] = w;
6187 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6190 return i;
6194 /* Return a pointer to the glyph W's physical cursor is on. Value is
6195 null if W's current matrix is invalid, so that no meaningfull glyph
6196 can be returned. */
6198 struct glyph *
6199 get_phys_cursor_glyph (w)
6200 struct window *w;
6202 struct glyph_row *row;
6203 struct glyph *glyph;
6205 if (w->phys_cursor.vpos >= 0
6206 && w->phys_cursor.vpos < w->current_matrix->nrows
6207 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6208 row->enabled_p)
6209 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6210 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6211 else
6212 glyph = NULL;
6214 return glyph;
6218 static int
6219 save_window_save (window, vector, i)
6220 Lisp_Object window;
6221 struct Lisp_Vector *vector;
6222 int i;
6224 register struct saved_window *p;
6225 register struct window *w;
6226 register Lisp_Object tem;
6228 for (;!NILP (window); window = w->next)
6230 p = SAVED_WINDOW_N (vector, i);
6231 w = XWINDOW (window);
6233 XSETFASTINT (w->temslot, i); i++;
6234 p->window = window;
6235 p->buffer = w->buffer;
6236 p->left_col = w->left_col;
6237 p->top_line = w->top_line;
6238 p->total_cols = w->total_cols;
6239 p->total_lines = w->total_lines;
6240 p->hscroll = w->hscroll;
6241 p->min_hscroll = w->min_hscroll;
6242 p->display_table = w->display_table;
6243 p->orig_top_line = w->orig_top_line;
6244 p->orig_total_lines = w->orig_total_lines;
6245 p->left_margin_cols = w->left_margin_cols;
6246 p->right_margin_cols = w->right_margin_cols;
6247 p->left_fringe_width = w->left_fringe_width;
6248 p->right_fringe_width = w->right_fringe_width;
6249 p->fringes_outside_margins = w->fringes_outside_margins;
6250 p->scroll_bar_width = w->scroll_bar_width;
6251 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6252 if (!NILP (w->buffer))
6254 /* Save w's value of point in the window configuration.
6255 If w is the selected window, then get the value of point
6256 from the buffer; pointm is garbage in the selected window. */
6257 if (EQ (window, selected_window))
6259 p->pointm = Fmake_marker ();
6260 set_marker_both (p->pointm, w->buffer,
6261 BUF_PT (XBUFFER (w->buffer)),
6262 BUF_PT_BYTE (XBUFFER (w->buffer)));
6264 else
6265 p->pointm = Fcopy_marker (w->pointm, Qnil);
6267 p->start = Fcopy_marker (w->start, Qnil);
6268 p->start_at_line_beg = w->start_at_line_beg;
6270 tem = XBUFFER (w->buffer)->mark;
6271 p->mark = Fcopy_marker (tem, Qnil);
6273 else
6275 p->pointm = Qnil;
6276 p->start = Qnil;
6277 p->mark = Qnil;
6278 p->start_at_line_beg = Qnil;
6281 if (NILP (w->parent))
6282 p->parent = Qnil;
6283 else
6284 p->parent = XWINDOW (w->parent)->temslot;
6286 if (NILP (w->prev))
6287 p->prev = Qnil;
6288 else
6289 p->prev = XWINDOW (w->prev)->temslot;
6291 if (!NILP (w->vchild))
6292 i = save_window_save (w->vchild, vector, i);
6293 if (!NILP (w->hchild))
6294 i = save_window_save (w->hchild, vector, i);
6297 return i;
6300 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6301 Scurrent_window_configuration, 0, 1, 0,
6302 doc: /* Return an object representing the current window configuration of FRAME.
6303 If FRAME is nil or omitted, use the selected frame.
6304 This describes the number of windows, their sizes and current buffers,
6305 and for each displayed buffer, where display starts, and the positions of
6306 point and mark. An exception is made for point in the current buffer:
6307 its value is -not- saved.
6308 This also records the currently selected frame, and FRAME's focus
6309 redirection (see `redirect-frame-focus'). */)
6310 (frame)
6311 Lisp_Object frame;
6313 register Lisp_Object tem;
6314 register int n_windows;
6315 register struct save_window_data *data;
6316 register struct Lisp_Vector *vec;
6317 register int i;
6318 FRAME_PTR f;
6320 if (NILP (frame))
6321 frame = selected_frame;
6322 CHECK_LIVE_FRAME (frame);
6323 f = XFRAME (frame);
6325 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6326 vec = allocate_other_vector (VECSIZE (struct save_window_data));
6327 data = (struct save_window_data *)vec;
6329 XSETFASTINT (data->frame_cols, FRAME_COLS (f));
6330 XSETFASTINT (data->frame_lines, FRAME_LINES (f));
6331 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
6332 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
6333 data->selected_frame = selected_frame;
6334 data->current_window = FRAME_SELECTED_WINDOW (f);
6335 XSETBUFFER (data->current_buffer, current_buffer);
6336 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6337 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6338 data->root_window = FRAME_ROOT_WINDOW (f);
6339 data->focus_frame = FRAME_FOCUS_FRAME (f);
6340 XSETINT (data->min_height, window_min_height);
6341 XSETINT (data->min_width, window_min_width);
6342 tem = Fmake_vector (make_number (n_windows), Qnil);
6343 data->saved_windows = tem;
6344 for (i = 0; i < n_windows; i++)
6345 XVECTOR (tem)->contents[i]
6346 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6347 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6348 XSETWINDOW_CONFIGURATION (tem, data);
6349 return (tem);
6352 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
6353 0, UNEVALLED, 0,
6354 doc: /* Execute BODY, preserving window sizes and contents.
6355 Return the value of the last form in BODY.
6356 Restore which buffer appears in which window, where display starts,
6357 and the value of point and mark for each window.
6358 Also restore the choice of selected window.
6359 Also restore which buffer is current.
6360 Does not restore the value of point in current buffer.
6361 usage: (save-window-excursion BODY ...) */)
6362 (args)
6363 Lisp_Object args;
6365 register Lisp_Object val;
6366 register int count = SPECPDL_INDEX ();
6368 record_unwind_protect (Fset_window_configuration,
6369 Fcurrent_window_configuration (Qnil));
6370 val = Fprogn (args);
6371 return unbind_to (count, val);
6376 /***********************************************************************
6377 Window Split Tree
6378 ***********************************************************************/
6380 static Lisp_Object
6381 window_tree (w)
6382 struct window *w;
6384 Lisp_Object tail = Qnil;
6385 Lisp_Object result = Qnil;
6387 while (w)
6389 Lisp_Object wn;
6391 XSETWINDOW (wn, w);
6392 if (!NILP (w->hchild))
6393 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6394 window_tree (XWINDOW (w->hchild))));
6395 else if (!NILP (w->vchild))
6396 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6397 window_tree (XWINDOW (w->vchild))));
6399 if (NILP (result))
6401 result = tail = Fcons (wn, Qnil);
6403 else
6405 XSETCDR (tail, Fcons (wn, Qnil));
6406 tail = XCDR (tail);
6409 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6412 return result;
6417 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6418 0, 1, 0,
6419 doc: /* Return the window tree for frame FRAME.
6421 The return value is a list of the form (ROOT MINI), where ROOT
6422 represents the window tree of the frame's root window, and MINI
6423 is the frame's minibuffer window.
6425 If the root window is not split, ROOT is the root window itself.
6426 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6427 horizontal split, and t for a vertical split, EDGES gives the combined
6428 size and position of the subwindows in the split, and the rest of the
6429 elements are the subwindows in the split. Each of the subwindows may
6430 again be a window or a list representing a window split, and so on.
6431 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6433 If FRAME is nil or omitted, return information on the currently
6434 selected frame. */)
6435 (frame)
6436 Lisp_Object frame;
6438 FRAME_PTR f;
6440 if (NILP (frame))
6441 frame = selected_frame;
6443 CHECK_FRAME (frame);
6444 f = XFRAME (frame);
6446 if (!FRAME_LIVE_P (f))
6447 return Qnil;
6449 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6453 /***********************************************************************
6454 Marginal Areas
6455 ***********************************************************************/
6457 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6458 2, 3, 0,
6459 doc: /* Set width of marginal areas of window WINDOW.
6460 If WINDOW is nil, set margins of the currently selected window.
6461 Second arg LEFT-WIDTH specifies the number of character cells to
6462 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6463 does the same for the right marginal area. A nil width parameter
6464 means no margin. */)
6465 (window, left_width, right_width)
6466 Lisp_Object window, left_width, right_width;
6468 struct window *w = decode_window (window);
6470 /* Translate negative or zero widths to nil.
6471 Margins that are too wide have to be checked elsewhere. */
6473 if (!NILP (left_width))
6475 CHECK_NUMBER (left_width);
6476 if (XINT (left_width) <= 0)
6477 left_width = Qnil;
6480 if (!NILP (right_width))
6482 CHECK_NUMBER (right_width);
6483 if (XINT (right_width) <= 0)
6484 right_width = Qnil;
6487 if (!EQ (w->left_margin_cols, left_width)
6488 || !EQ (w->right_margin_cols, right_width))
6490 w->left_margin_cols = left_width;
6491 w->right_margin_cols = right_width;
6493 adjust_window_margins (w);
6495 ++windows_or_buffers_changed;
6496 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6499 return Qnil;
6503 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6504 0, 1, 0,
6505 doc: /* Get width of marginal areas of window WINDOW.
6506 If WINDOW is omitted or nil, use the currently selected window.
6507 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6508 If a marginal area does not exist, its width will be returned
6509 as nil. */)
6510 (window)
6511 Lisp_Object window;
6513 struct window *w = decode_window (window);
6514 return Fcons (w->left_margin_cols, w->right_margin_cols);
6519 /***********************************************************************
6520 Fringes
6521 ***********************************************************************/
6523 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6524 2, 4, 0,
6525 doc: /* Set the fringe widths of window WINDOW.
6526 If WINDOW is nil, set the fringe widths of the currently selected
6527 window.
6528 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6529 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6530 fringe width. If a fringe width arg is nil, that means to use the
6531 frame's default fringe width. Default fringe widths can be set with
6532 the command `set-fringe-style'.
6533 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6534 outside of the display margins. By default, fringes are drawn between
6535 display marginal areas and the text area. */)
6536 (window, left_width, right_width, outside_margins)
6537 Lisp_Object window, left_width, right_width, outside_margins;
6539 struct window *w = decode_window (window);
6541 if (!NILP (left_width))
6542 CHECK_NATNUM (left_width);
6543 if (!NILP (right_width))
6544 CHECK_NATNUM (right_width);
6546 if (!EQ (w->left_fringe_width, left_width)
6547 || !EQ (w->right_fringe_width, right_width)
6548 || !EQ (w->fringes_outside_margins, outside_margins))
6550 w->left_fringe_width = left_width;
6551 w->right_fringe_width = right_width;
6552 w->fringes_outside_margins = outside_margins;
6554 adjust_window_margins (w);
6556 clear_glyph_matrix (w->current_matrix);
6557 w->window_end_valid = Qnil;
6559 ++windows_or_buffers_changed;
6560 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6563 return Qnil;
6567 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6568 0, 1, 0,
6569 doc: /* Get width of fringes of window WINDOW.
6570 If WINDOW is omitted or nil, use the currently selected window.
6571 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6572 (window)
6573 Lisp_Object window;
6575 struct window *w = decode_window (window);
6576 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6577 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6578 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ?
6579 Qt : Qnil), Qnil)));
6584 /***********************************************************************
6585 Scroll bars
6586 ***********************************************************************/
6588 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6589 2, 4, 0,
6590 doc: /* Set width and type of scroll bars of window WINDOW.
6591 If window is nil, set scroll bars of the currently selected window.
6592 Second parameter WIDTH specifies the pixel width for the scroll bar;
6593 this is automatically adjusted to a multiple of the frame column width.
6594 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6595 bar: left, right, or nil.
6596 If WIDTH is nil, use the frame's scroll-bar width.
6597 If TYPE is t, use the frame's scroll-bar type. */)
6598 (window, width, vertical_type, horizontal_type)
6599 Lisp_Object window, width, vertical_type, horizontal_type;
6601 struct window *w = decode_window (window);
6603 if (!NILP (width))
6605 CHECK_NATNUM (width);
6607 if (XINT (width) == 0)
6608 vertical_type = Qnil;
6611 if (!(EQ (vertical_type, Qnil)
6612 || EQ (vertical_type, Qleft)
6613 || EQ (vertical_type, Qright)
6614 || EQ (vertical_type, Qt)))
6615 error ("Invalid type of vertical scroll bar");
6617 if (!EQ (w->scroll_bar_width, width)
6618 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6620 w->scroll_bar_width = width;
6621 w->vertical_scroll_bar_type = vertical_type;
6623 adjust_window_margins (w);
6625 clear_glyph_matrix (w->current_matrix);
6626 w->window_end_valid = Qnil;
6628 ++windows_or_buffers_changed;
6629 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6632 return Qnil;
6636 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6637 0, 1, 0,
6638 doc: /* Get width and type of scroll bars of window WINDOW.
6639 If WINDOW is omitted or nil, use the currently selected window.
6640 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6641 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6642 value. */)
6643 (window)
6644 Lisp_Object window;
6646 struct window *w = decode_window (window);
6647 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6648 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6649 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6650 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6651 Fcons (w->vertical_scroll_bar_type,
6652 Fcons (Qnil, Qnil))));
6657 /***********************************************************************
6658 Smooth scrolling
6659 ***********************************************************************/
6661 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6662 doc: /* Return the amount by which WINDOW is scrolled vertically.
6663 Use the selected window if WINDOW is nil or omitted.
6664 Normally, value is a multiple of the canonical character height of WINDOW;
6665 optional second arg PIXELS_P means value is measured in pixels. */)
6666 (window, pixels_p)
6667 Lisp_Object window, pixels_p;
6669 Lisp_Object result;
6670 struct frame *f;
6671 struct window *w;
6673 if (NILP (window))
6674 window = selected_window;
6675 else
6676 CHECK_WINDOW (window);
6677 w = XWINDOW (window);
6678 f = XFRAME (w->frame);
6680 if (FRAME_WINDOW_P (f))
6681 result = (NILP (pixels_p)
6682 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6683 : make_number (-w->vscroll));
6684 else
6685 result = make_number (0);
6686 return result;
6690 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6691 2, 3, 0,
6692 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6693 WINDOW nil means use the selected window. Normally, VSCROLL is a
6694 non-negative multiple of the canonical character height of WINDOW;
6695 optional third arg PIXELS_P non-nil means that VSCROLL is in pixels.
6696 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6697 corresponds to an integral number of pixels. The return value is the
6698 result of this rounding.
6699 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6700 (window, vscroll, pixels_p)
6701 Lisp_Object window, vscroll, pixels_p;
6703 struct window *w;
6704 struct frame *f;
6706 if (NILP (window))
6707 window = selected_window;
6708 else
6709 CHECK_WINDOW (window);
6710 CHECK_NUMBER_OR_FLOAT (vscroll);
6712 w = XWINDOW (window);
6713 f = XFRAME (w->frame);
6715 if (FRAME_WINDOW_P (f))
6717 int old_dy = w->vscroll;
6719 w->vscroll = - (NILP (pixels_p)
6720 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6721 : XFLOATINT (vscroll));
6722 w->vscroll = min (w->vscroll, 0);
6724 if (w->vscroll != old_dy)
6726 /* Adjust glyph matrix of the frame if the virtual display
6727 area becomes larger than before. */
6728 if (w->vscroll < 0 && w->vscroll < old_dy)
6729 adjust_glyphs (f);
6731 /* Prevent redisplay shortcuts. */
6732 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6736 return Fwindow_vscroll (window, pixels_p);
6740 /* Call FN for all leaf windows on frame F. FN is called with the
6741 first argument being a pointer to the leaf window, and with
6742 additional argument USER_DATA. Stops when FN returns 0. */
6744 void
6745 foreach_window (f, fn, user_data)
6746 struct frame *f;
6747 int (* fn) P_ ((struct window *, void *));
6748 void *user_data;
6750 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6754 /* Helper function for foreach_window. Call FN for all leaf windows
6755 reachable from W. FN is called with the first argument being a
6756 pointer to the leaf window, and with additional argument USER_DATA.
6757 Stop when FN returns 0. Value is 0 if stopped by FN. */
6759 static int
6760 foreach_window_1 (w, fn, user_data)
6761 struct window *w;
6762 int (* fn) P_ ((struct window *, void *));
6763 void *user_data;
6765 int cont;
6767 for (cont = 1; w && cont;)
6769 if (!NILP (w->hchild))
6770 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6771 else if (!NILP (w->vchild))
6772 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6773 else
6774 cont = fn (w, user_data);
6776 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6779 return cont;
6783 /* Freeze or unfreeze the window start of W unless it is a
6784 mini-window or the selected window. FREEZE_P non-null means freeze
6785 the window start. */
6787 static int
6788 freeze_window_start (w, freeze_p)
6789 struct window *w;
6790 void *freeze_p;
6792 if (w == XWINDOW (selected_window)
6793 || MINI_WINDOW_P (w)
6794 || (MINI_WINDOW_P (XWINDOW (selected_window))
6795 && ! NILP (Vminibuf_scroll_window)
6796 && w == XWINDOW (Vminibuf_scroll_window)))
6797 freeze_p = NULL;
6799 w->frozen_window_start_p = freeze_p != NULL;
6800 return 1;
6804 /* Freeze or unfreeze the window starts of all leaf windows on frame
6805 F, except the selected window and a mini-window. FREEZE_P non-zero
6806 means freeze the window start. */
6808 void
6809 freeze_window_starts (f, freeze_p)
6810 struct frame *f;
6811 int freeze_p;
6813 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6817 /***********************************************************************
6818 Initialization
6819 ***********************************************************************/
6821 /* Return 1 if window configurations C1 and C2
6822 describe the same state of affairs. This is used by Fequal. */
6825 compare_window_configurations (c1, c2, ignore_positions)
6826 Lisp_Object c1, c2;
6827 int ignore_positions;
6829 register struct save_window_data *d1, *d2;
6830 struct Lisp_Vector *sw1, *sw2;
6831 int i;
6833 if (!WINDOW_CONFIGURATIONP (c1))
6834 wrong_type_argument (Qwindow_configuration_p, c1);
6835 if (!WINDOW_CONFIGURATIONP (c2))
6836 wrong_type_argument (Qwindow_configuration_p, c2);
6838 d1 = (struct save_window_data *) XVECTOR (c1);
6839 d2 = (struct save_window_data *) XVECTOR (c2);
6840 sw1 = XVECTOR (d1->saved_windows);
6841 sw2 = XVECTOR (d2->saved_windows);
6843 if (! EQ (d1->frame_cols, d2->frame_cols))
6844 return 0;
6845 if (! EQ (d1->frame_lines, d2->frame_lines))
6846 return 0;
6847 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
6848 return 0;
6849 if (! EQ (d1->selected_frame, d2->selected_frame))
6850 return 0;
6851 /* Don't compare the current_window field directly.
6852 Instead see w1_is_current and w2_is_current, below. */
6853 if (! EQ (d1->current_buffer, d2->current_buffer))
6854 return 0;
6855 if (! ignore_positions)
6857 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6858 return 0;
6859 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6860 return 0;
6862 /* Don't compare the root_window field.
6863 We don't require the two configurations
6864 to use the same window object,
6865 and the two root windows must be equivalent
6866 if everything else compares equal. */
6867 if (! EQ (d1->focus_frame, d2->focus_frame))
6868 return 0;
6869 if (! EQ (d1->min_width, d2->min_width))
6870 return 0;
6871 if (! EQ (d1->min_height, d2->min_height))
6872 return 0;
6874 /* Verify that the two confis have the same number of windows. */
6875 if (sw1->size != sw2->size)
6876 return 0;
6878 for (i = 0; i < sw1->size; i++)
6880 struct saved_window *p1, *p2;
6881 int w1_is_current, w2_is_current;
6883 p1 = SAVED_WINDOW_N (sw1, i);
6884 p2 = SAVED_WINDOW_N (sw2, i);
6886 /* Verify that the current windows in the two
6887 configurations correspond to each other. */
6888 w1_is_current = EQ (d1->current_window, p1->window);
6889 w2_is_current = EQ (d2->current_window, p2->window);
6891 if (w1_is_current != w2_is_current)
6892 return 0;
6894 /* Verify that the corresponding windows do match. */
6895 if (! EQ (p1->buffer, p2->buffer))
6896 return 0;
6897 if (! EQ (p1->left_col, p2->left_col))
6898 return 0;
6899 if (! EQ (p1->top_line, p2->top_line))
6900 return 0;
6901 if (! EQ (p1->total_cols, p2->total_cols))
6902 return 0;
6903 if (! EQ (p1->total_lines, p2->total_lines))
6904 return 0;
6905 if (! EQ (p1->display_table, p2->display_table))
6906 return 0;
6907 if (! EQ (p1->parent, p2->parent))
6908 return 0;
6909 if (! EQ (p1->prev, p2->prev))
6910 return 0;
6911 if (! ignore_positions)
6913 if (! EQ (p1->hscroll, p2->hscroll))
6914 return 0;
6915 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6916 return 0;
6917 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6918 return 0;
6919 if (NILP (Fequal (p1->start, p2->start)))
6920 return 0;
6921 if (NILP (Fequal (p1->pointm, p2->pointm)))
6922 return 0;
6923 if (NILP (Fequal (p1->mark, p2->mark)))
6924 return 0;
6926 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6927 return 0;
6928 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6929 return 0;
6930 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6931 return 0;
6932 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6933 return 0;
6934 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6935 return 0;
6936 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6937 return 0;
6938 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6939 return 0;
6942 return 1;
6945 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6946 Scompare_window_configurations, 2, 2, 0,
6947 doc: /* Compare two window configurations as regards the structure of windows.
6948 This function ignores details such as the values of point and mark
6949 and scrolling positions. */)
6950 (x, y)
6951 Lisp_Object x, y;
6953 if (compare_window_configurations (x, y, 1))
6954 return Qt;
6955 return Qnil;
6958 void
6959 init_window_once ()
6961 struct frame *f = make_initial_frame ();
6962 XSETFRAME (selected_frame, f);
6963 Vterminal_frame = selected_frame;
6964 minibuf_window = f->minibuffer_window;
6965 selected_window = f->selected_window;
6966 last_nonminibuf_frame = f;
6968 window_initialized = 1;
6971 void
6972 init_window ()
6974 Vwindow_list = Qnil;
6977 void
6978 syms_of_window ()
6980 Qwindow_size_fixed = intern ("window-size-fixed");
6981 staticpro (&Qwindow_size_fixed);
6982 Fset (Qwindow_size_fixed, Qnil);
6984 staticpro (&Qwindow_configuration_change_hook);
6985 Qwindow_configuration_change_hook
6986 = intern ("window-configuration-change-hook");
6988 Qwindowp = intern ("windowp");
6989 staticpro (&Qwindowp);
6991 Qwindow_configuration_p = intern ("window-configuration-p");
6992 staticpro (&Qwindow_configuration_p);
6994 Qwindow_live_p = intern ("window-live-p");
6995 staticpro (&Qwindow_live_p);
6997 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
6998 staticpro (&Qtemp_buffer_show_hook);
7000 staticpro (&Vwindow_list);
7002 minibuf_selected_window = Qnil;
7003 staticpro (&minibuf_selected_window);
7005 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
7006 doc: /* Non-nil means call as function to display a help buffer.
7007 The function is called with one argument, the buffer to be displayed.
7008 Used by `with-output-to-temp-buffer'.
7009 If this function is used, then it must do the entire job of showing
7010 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7011 Vtemp_buffer_show_function = Qnil;
7013 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
7014 doc: /* If non-nil, function to call to handle `display-buffer'.
7015 It will receive two args, the buffer and a flag which if non-nil means
7016 that the currently selected window is not acceptable.
7017 It should choose or create a window, display the specified buffer in it,
7018 and return the window.
7019 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'
7020 work using this function. */);
7021 Vdisplay_buffer_function = Qnil;
7023 DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
7024 doc: /* *If non-nil, `display-buffer' should even the window heights.
7025 If nil, `display-buffer' will leave the window configuration alone. */);
7026 Veven_window_heights = Qt;
7028 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
7029 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7030 Vminibuf_scroll_window = Qnil;
7032 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
7033 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7034 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7035 is displayed in the `mode-line' face. */);
7036 mode_line_in_non_selected_windows = 1;
7038 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
7039 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7040 Vother_window_scroll_buffer = Qnil;
7042 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
7043 doc: /* *Non-nil means `display-buffer' should make a separate frame. */);
7044 pop_up_frames = 0;
7046 DEFVAR_BOOL ("auto-window-vscroll", &auto_window_vscroll_p,
7047 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7048 auto_window_vscroll_p = 1;
7050 DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
7051 doc: /* *Non-nil means `display-buffer' should reuse frames.
7052 If the buffer in question is already displayed in a frame, raise that frame. */);
7053 display_buffer_reuse_frames = 0;
7055 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
7056 doc: /* Function to call to handle automatic new frame creation.
7057 It is called with no arguments and should return a newly created frame.
7059 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
7060 where `pop-up-frame-alist' would hold the default frame parameters. */);
7061 Vpop_up_frame_function = Qnil;
7063 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
7064 doc: /* *List of buffer names that should have their own special frames.
7065 Displaying a buffer with `display-buffer' or `pop-to-buffer',
7066 if its name is in this list, makes a special frame for it
7067 using `special-display-function'. See also `special-display-regexps'.
7069 An element of the list can be a list instead of just a string.
7070 There are two ways to use a list as an element:
7071 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
7072 In the first case, the FRAME-PARAMETERS are pairs of the form
7073 \(PARAMETER . VALUE); these parameter values are used to create the frame.
7074 In the second case, FUNCTION is called with BUFFER as the first argument,
7075 followed by the OTHER-ARGS--it can display BUFFER in any way it likes.
7076 All this is done by the function found in `special-display-function'.
7078 If the specified frame parameters include (same-buffer . t), the
7079 buffer is displayed in the currently selected window. Otherwise, if
7080 they include (same-frame . t), the buffer is displayed in a new window
7081 in the currently selected frame.
7083 If this variable appears \"not to work\", because you add a name to it
7084 but that buffer still appears in the selected window, look at the
7085 values of `same-window-buffer-names' and `same-window-regexps'.
7086 Those variables take precedence over this one. */);
7087 Vspecial_display_buffer_names = Qnil;
7089 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
7090 doc: /* *List of regexps saying which buffers should have their own special frames.
7091 When displaying a buffer with `display-buffer' or `pop-to-buffer',
7092 if any regexp in this list matches the buffer name, it makes a
7093 special frame for the buffer by calling `special-display-function'.
7095 An element of the list can be a list instead of just a string.
7096 There are two ways to use a list as an element:
7097 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
7098 In the first case, the FRAME-PARAMETERS are pairs of the form
7099 \(PARAMETER . VALUE); these parameter values are used to create the frame.
7100 In the second case, FUNCTION is called with BUFFER as the first argument,
7101 followed by the OTHER-ARGS--it can display the buffer in any way it likes.
7102 All this is done by the function found in `special-display-function'.
7104 If the specified frame parameters include (same-buffer . t), the
7105 buffer is displayed in the currently selected window. Otherwise, if
7106 they include (same-frame . t), the buffer is displayed in a new window
7107 in the currently selected frame.
7109 If this variable appears \"not to work\", because you add a regexp to it
7110 but the matching buffers still appear in the selected window, look at the
7111 values of `same-window-buffer-names' and `same-window-regexps'.
7112 Those variables take precedence over this one. */);
7113 Vspecial_display_regexps = Qnil;
7115 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
7116 doc: /* Function to call to make a new frame for a special buffer.
7117 It is called with two arguments, the buffer and optional buffer specific
7118 data, and should return a window displaying that buffer.
7119 The default value normally makes a separate frame for the buffer,
7120 using `special-display-frame-alist' to specify the frame parameters.
7121 But if the buffer specific data includes (same-buffer . t) then the
7122 buffer is displayed in the current selected window.
7123 Otherwise if it includes (same-frame . t) then the buffer is displayed in
7124 a new window in the currently selected frame.
7126 A buffer is special if it is listed in `special-display-buffer-names'
7127 or matches a regexp in `special-display-regexps'. */);
7128 Vspecial_display_function = Qnil;
7130 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
7131 doc: /* *List of buffer names that should appear in the selected window.
7132 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
7133 switches to it in the selected window, rather than making it appear
7134 in some other window.
7136 An element of the list can be a cons cell instead of just a string.
7137 Then the car must be a string, which specifies the buffer name.
7138 This is for compatibility with `special-display-buffer-names';
7139 the cdr of the cons cell is ignored.
7141 See also `same-window-regexps'. */);
7142 Vsame_window_buffer_names = Qnil;
7144 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
7145 doc: /* *List of regexps saying which buffers should appear in the selected window.
7146 If a buffer name matches one of these regexps, then displaying it
7147 using `display-buffer' or `pop-to-buffer' switches to it
7148 in the selected window, rather than making it appear in some other window.
7150 An element of the list can be a cons cell instead of just a string.
7151 Then the car must be a string, which specifies the buffer name.
7152 This is for compatibility with `special-display-buffer-names';
7153 the cdr of the cons cell is ignored.
7155 See also `same-window-buffer-names'. */);
7156 Vsame_window_regexps = Qnil;
7158 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
7159 doc: /* *Non-nil means display-buffer should make new windows. */);
7160 pop_up_windows = 1;
7162 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
7163 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7164 next_screen_context_lines = 2;
7166 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
7167 doc: /* *A window must be at least this tall to be eligible for splitting by `display-buffer'.
7168 If there is only one window, it is split regardless of this value. */);
7169 split_height_threshold = 500;
7171 DEFVAR_INT ("window-min-height", &window_min_height,
7172 doc: /* *Delete any window less than this tall (including its mode line). */);
7173 window_min_height = 4;
7175 DEFVAR_INT ("window-min-width", &window_min_width,
7176 doc: /* *Delete any window less than this wide. */);
7177 window_min_width = 10;
7179 DEFVAR_LISP ("scroll-preserve-screen-position",
7180 &Vscroll_preserve_screen_position,
7181 doc: /* *Controls if scroll commands move point to keep its screen line unchanged.
7182 A value of nil means point does not keep its screen position except
7183 at the scroll margin or window boundary respectively.
7184 A value of t means point keeps its screen position if the scroll
7185 command moved it vertically out of the window, e.g. when scrolling
7186 by full screens.
7187 Any other value means point always keeps its screen position. */);
7188 Vscroll_preserve_screen_position = Qnil;
7190 DEFVAR_LISP ("window-configuration-change-hook",
7191 &Vwindow_configuration_change_hook,
7192 doc: /* Functions to call when window configuration changes.
7193 The selected frame is the one whose configuration has changed. */);
7194 Vwindow_configuration_change_hook = Qnil;
7196 defsubr (&Sselected_window);
7197 defsubr (&Sminibuffer_window);
7198 defsubr (&Swindow_minibuffer_p);
7199 defsubr (&Swindowp);
7200 defsubr (&Swindow_live_p);
7201 defsubr (&Spos_visible_in_window_p);
7202 defsubr (&Swindow_buffer);
7203 defsubr (&Swindow_height);
7204 defsubr (&Swindow_width);
7205 defsubr (&Swindow_hscroll);
7206 defsubr (&Sset_window_hscroll);
7207 defsubr (&Swindow_redisplay_end_trigger);
7208 defsubr (&Sset_window_redisplay_end_trigger);
7209 defsubr (&Swindow_edges);
7210 defsubr (&Swindow_pixel_edges);
7211 defsubr (&Swindow_inside_edges);
7212 defsubr (&Swindow_inside_pixel_edges);
7213 defsubr (&Scoordinates_in_window_p);
7214 defsubr (&Swindow_at);
7215 defsubr (&Swindow_point);
7216 defsubr (&Swindow_start);
7217 defsubr (&Swindow_end);
7218 defsubr (&Sset_window_point);
7219 defsubr (&Sset_window_start);
7220 defsubr (&Swindow_dedicated_p);
7221 defsubr (&Sset_window_dedicated_p);
7222 defsubr (&Swindow_display_table);
7223 defsubr (&Sset_window_display_table);
7224 defsubr (&Snext_window);
7225 defsubr (&Sprevious_window);
7226 defsubr (&Sother_window);
7227 defsubr (&Sget_lru_window);
7228 defsubr (&Sget_largest_window);
7229 defsubr (&Sget_buffer_window);
7230 defsubr (&Sdelete_other_windows);
7231 defsubr (&Sdelete_windows_on);
7232 defsubr (&Sreplace_buffer_in_windows);
7233 defsubr (&Sdelete_window);
7234 defsubr (&Sset_window_buffer);
7235 defsubr (&Sselect_window);
7236 defsubr (&Sspecial_display_p);
7237 defsubr (&Ssame_window_p);
7238 defsubr (&Sdisplay_buffer);
7239 defsubr (&Sforce_window_update);
7240 defsubr (&Ssplit_window);
7241 defsubr (&Senlarge_window);
7242 defsubr (&Sshrink_window);
7243 defsubr (&Sadjust_window_trailing_edge);
7244 defsubr (&Sscroll_up);
7245 defsubr (&Sscroll_down);
7246 defsubr (&Sscroll_left);
7247 defsubr (&Sscroll_right);
7248 defsubr (&Sother_window_for_scrolling);
7249 defsubr (&Sscroll_other_window);
7250 defsubr (&Sminibuffer_selected_window);
7251 defsubr (&Srecenter);
7252 defsubr (&Swindow_text_height);
7253 defsubr (&Smove_to_window_line);
7254 defsubr (&Swindow_configuration_p);
7255 defsubr (&Swindow_configuration_frame);
7256 defsubr (&Sset_window_configuration);
7257 defsubr (&Scurrent_window_configuration);
7258 defsubr (&Ssave_window_excursion);
7259 defsubr (&Swindow_tree);
7260 defsubr (&Sset_window_margins);
7261 defsubr (&Swindow_margins);
7262 defsubr (&Sset_window_fringes);
7263 defsubr (&Swindow_fringes);
7264 defsubr (&Sset_window_scroll_bars);
7265 defsubr (&Swindow_scroll_bars);
7266 defsubr (&Swindow_vscroll);
7267 defsubr (&Sset_window_vscroll);
7268 defsubr (&Scompare_window_configurations);
7269 defsubr (&Swindow_list);
7272 void
7273 keys_of_window ()
7275 initial_define_key (control_x_map, '1', "delete-other-windows");
7276 initial_define_key (control_x_map, '2', "split-window");
7277 initial_define_key (control_x_map, '0', "delete-window");
7278 initial_define_key (control_x_map, 'o', "other-window");
7279 initial_define_key (control_x_map, '^', "enlarge-window");
7280 initial_define_key (control_x_map, '<', "scroll-left");
7281 initial_define_key (control_x_map, '>', "scroll-right");
7283 initial_define_key (global_map, Ctl ('V'), "scroll-up");
7284 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7285 initial_define_key (meta_map, 'v', "scroll-down");
7287 initial_define_key (global_map, Ctl('L'), "recenter");
7288 initial_define_key (meta_map, 'r', "move-to-window-line");
7291 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
7292 (do not change this comment) */