*** empty log message ***
[emacs.git] / src / window.c
blobab15a7d1b217a452984f7ac030b76a9d39c16655
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985,86,87, 1993,94,95,96,97,98, 2000,01,02,03,04
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 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., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "buffer.h"
26 #include "keyboard.h"
27 #include "keymap.h"
28 #include "frame.h"
29 #include "window.h"
30 #include "commands.h"
31 #include "indent.h"
32 #include "termchar.h"
33 #include "disptab.h"
34 #include "dispextern.h"
35 #include "blockinput.h"
36 #include "intervals.h"
38 #ifdef HAVE_X_WINDOWS
39 #include "xterm.h"
40 #endif /* HAVE_X_WINDOWS */
41 #ifdef WINDOWSNT
42 #include "w32term.h"
43 #endif
44 #ifdef MSDOS
45 #include "msdos.h"
46 #endif
47 #ifdef MAC_OS
48 #include "macterm.h"
49 #endif
52 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
53 Lisp_Object Qwindow_size_fixed;
54 extern Lisp_Object Qleft_margin, Qright_margin;
56 static int displayed_window_lines P_ ((struct window *));
57 static struct window *decode_window P_ ((Lisp_Object));
58 static int count_windows P_ ((struct window *));
59 static int get_leaf_windows P_ ((struct window *, struct window **, int));
60 static void window_scroll P_ ((Lisp_Object, int, int, int));
61 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
62 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
63 static int window_min_size_1 P_ ((struct window *, int));
64 static int window_min_size P_ ((struct window *, int, int, int *));
65 static void size_window P_ ((Lisp_Object, int, int, int));
66 static int freeze_window_start P_ ((struct window *, void *));
67 static int window_fixed_size_p P_ ((struct window *, int, int));
68 static void enlarge_window P_ ((Lisp_Object, int, int, int));
69 static Lisp_Object window_list P_ ((void));
70 static int add_window_to_list P_ ((struct window *, void *));
71 static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
72 Lisp_Object));
73 static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
74 Lisp_Object, int));
75 static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
76 Lisp_Object *));
77 static int foreach_window_1 P_ ((struct window *,
78 int (* fn) (struct window *, void *),
79 void *));
80 static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
82 /* The value of `window-size-fixed'. */
84 int window_size_fixed;
86 /* This is the window in which the terminal's cursor should
87 be left when nothing is being done with it. This must
88 always be a leaf window, and its buffer is selected by
89 the top level editing loop at the end of each command.
91 This value is always the same as
92 FRAME_SELECTED_WINDOW (selected_frame). */
94 Lisp_Object selected_window;
96 /* A list of all windows for use by next_window and Fwindow_list.
97 Functions creating or deleting windows should invalidate this cache
98 by setting it to nil. */
100 Lisp_Object Vwindow_list;
102 /* The mini-buffer window of the selected frame.
103 Note that you cannot test for mini-bufferness of an arbitrary window
104 by comparing against this; but you can test for mini-bufferness of
105 the selected window. */
107 Lisp_Object minibuf_window;
109 /* Non-nil means it is the window whose mode line should be
110 shown as the selected window when the minibuffer is selected. */
112 Lisp_Object minibuf_selected_window;
114 /* Non-nil means it is the window for C-M-v to scroll
115 when the mini-buffer is selected. */
117 Lisp_Object Vminibuf_scroll_window;
119 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
121 Lisp_Object Vother_window_scroll_buffer;
123 /* Non-nil means it's function to call to display temp buffers. */
125 Lisp_Object Vtemp_buffer_show_function;
127 /* Non-zero means to use mode-line-inactive face in all windows but the
128 selected-window and the minibuffer-scroll-window when the
129 minibuffer is active. */
130 int mode_line_in_non_selected_windows;
132 /* If a window gets smaller than either of these, it is removed. */
134 EMACS_INT window_min_height;
135 EMACS_INT window_min_width;
137 /* Nonzero implies Fdisplay_buffer should create windows. */
139 int pop_up_windows;
141 /* Nonzero implies make new frames for Fdisplay_buffer. */
143 int pop_up_frames;
145 /* Nonzero means reuse existing frames for displaying buffers. */
147 int display_buffer_reuse_frames;
149 /* Non-nil means use this function instead of default */
151 Lisp_Object Vpop_up_frame_function;
153 /* Function to call to handle Fdisplay_buffer. */
155 Lisp_Object Vdisplay_buffer_function;
157 /* Non-nil means that Fdisplay_buffer should even the heights of windows. */
159 Lisp_Object Veven_window_heights;
161 /* List of buffer *names* for buffers that should have their own frames. */
163 Lisp_Object Vspecial_display_buffer_names;
165 /* List of regexps for buffer names that should have their own frames. */
167 Lisp_Object Vspecial_display_regexps;
169 /* Function to pop up a special frame. */
171 Lisp_Object Vspecial_display_function;
173 /* List of buffer *names* for buffers to appear in selected window. */
175 Lisp_Object Vsame_window_buffer_names;
177 /* List of regexps for buffer names to appear in selected window. */
179 Lisp_Object Vsame_window_regexps;
181 /* Hook run at end of temp_output_buffer_show. */
183 Lisp_Object Qtemp_buffer_show_hook;
185 /* Fdisplay_buffer always splits the largest window
186 if that window is more than this high. */
188 EMACS_INT split_height_threshold;
190 /* Number of lines of continuity in scrolling by screenfuls. */
192 EMACS_INT next_screen_context_lines;
194 /* Incremented for each window created. */
196 static int sequence_number;
198 /* Nonzero after init_window_once has finished. */
200 static int window_initialized;
202 /* Hook to run when window config changes. */
204 Lisp_Object Qwindow_configuration_change_hook;
205 Lisp_Object Vwindow_configuration_change_hook;
207 /* Nonzero means scroll commands try to put point
208 at the same screen height as previously. */
210 Lisp_Object Vscroll_preserve_screen_position;
212 #if 0 /* This isn't used anywhere. */
213 /* Nonzero means we can split a frame even if it is "unsplittable". */
214 static int inhibit_frame_unsplittable;
215 #endif /* 0 */
217 extern EMACS_INT scroll_margin;
219 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
222 doc: /* Returns t if OBJECT is a window. */)
223 (object)
224 Lisp_Object object;
226 return WINDOWP (object) ? Qt : Qnil;
229 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
230 doc: /* Returns t if OBJECT is a window which is currently visible. */)
231 (object)
232 Lisp_Object object;
234 return WINDOW_LIVE_P (object) ? Qt : Qnil;
237 Lisp_Object
238 make_window ()
240 Lisp_Object val;
241 register struct window *p;
243 p = allocate_window ();
244 XSETFASTINT (p->sequence_number, ++sequence_number);
245 XSETFASTINT (p->left_col, 0);
246 XSETFASTINT (p->top_line, 0);
247 XSETFASTINT (p->total_lines, 0);
248 XSETFASTINT (p->total_cols, 0);
249 XSETFASTINT (p->hscroll, 0);
250 XSETFASTINT (p->min_hscroll, 0);
251 p->orig_top_line = p->orig_total_lines = Qnil;
252 p->start = Fmake_marker ();
253 p->pointm = Fmake_marker ();
254 XSETFASTINT (p->use_time, 0);
255 p->frame = Qnil;
256 p->display_table = Qnil;
257 p->dedicated = Qnil;
258 p->pseudo_window_p = 0;
259 bzero (&p->cursor, sizeof (p->cursor));
260 bzero (&p->last_cursor, sizeof (p->last_cursor));
261 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
262 p->desired_matrix = p->current_matrix = 0;
263 p->phys_cursor_type = -1;
264 p->phys_cursor_width = -1;
265 p->must_be_updated_p = 0;
266 XSETFASTINT (p->window_end_vpos, 0);
267 XSETFASTINT (p->window_end_pos, 0);
268 p->window_end_valid = Qnil;
269 p->vscroll = 0;
270 XSETWINDOW (val, p);
271 XSETFASTINT (p->last_point, 0);
272 p->frozen_window_start_p = 0;
273 p->height_fixed_p = 0;
274 p->last_cursor_off_p = p->cursor_off_p = 0;
275 p->left_margin_cols = Qnil;
276 p->right_margin_cols = Qnil;
277 p->left_fringe_width = Qnil;
278 p->right_fringe_width = Qnil;
279 p->fringes_outside_margins = Qnil;
280 p->scroll_bar_width = Qnil;
281 p->vertical_scroll_bar_type = Qt;
282 p->overlay_arrow_bitmap = 0;
284 Vwindow_list = Qnil;
285 return val;
288 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
289 doc: /* Return the window that the cursor now appears in and commands apply to. */)
292 return selected_window;
295 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
296 doc: /* Return the window used now for minibuffers.
297 If the optional argument FRAME is specified, return the minibuffer window
298 used by that frame. */)
299 (frame)
300 Lisp_Object frame;
302 if (NILP (frame))
303 frame = selected_frame;
304 CHECK_LIVE_FRAME (frame);
305 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
308 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
309 doc: /* Returns non-nil if WINDOW is a minibuffer window.
310 WINDOW defaults to the selected window. */)
311 (window)
312 Lisp_Object window;
314 struct window *w = decode_window (window);
315 return MINI_WINDOW_P (w) ? Qt : Qnil;
319 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
320 Spos_visible_in_window_p, 0, 3, 0,
321 doc: /* Return t if position POS is currently on the frame in WINDOW.
322 Return nil if that position is scrolled vertically out of view.
323 If a character is only partially visible, nil is returned, unless the
324 optional argument PARTIALLY is non-nil.
325 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
327 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
328 return value is a list (X Y PARTIAL) where X and Y are the pixel relative
329 coordinate */)
330 (pos, window, partially)
331 Lisp_Object pos, window, partially;
333 register struct window *w;
334 register int posint;
335 register struct buffer *buf;
336 struct text_pos top;
337 Lisp_Object in_window = Qnil;
338 int fully_p = 1;
339 int x, y;
341 w = decode_window (window);
342 buf = XBUFFER (w->buffer);
343 SET_TEXT_POS_FROM_MARKER (top, w->start);
345 if (!NILP (pos))
347 CHECK_NUMBER_COERCE_MARKER (pos);
348 posint = XINT (pos);
350 else if (w == XWINDOW (selected_window))
351 posint = PT;
352 else
353 posint = XMARKER (w->pointm)->charpos;
355 /* If position is above window start or outside buffer boundaries,
356 or if window start is out of range, position is not visible. */
357 if (posint >= CHARPOS (top)
358 && posint <= BUF_ZV (buf)
359 && CHARPOS (top) >= BUF_BEGV (buf)
360 && CHARPOS (top) <= BUF_ZV (buf)
361 && pos_visible_p (w, posint, &fully_p, &x, &y, NILP (partially))
362 && (!NILP (partially) || fully_p))
363 in_window = Qt;
365 if (!NILP (in_window) && !NILP (partially))
366 in_window = Fcons (make_number (x),
367 Fcons (make_number (y),
368 Fcons (fully_p ? Qt : Qnil, Qnil)));
369 return in_window;
373 static struct window *
374 decode_window (window)
375 register Lisp_Object window;
377 if (NILP (window))
378 return XWINDOW (selected_window);
380 CHECK_LIVE_WINDOW (window);
381 return XWINDOW (window);
384 static struct window *
385 decode_any_window (window)
386 register Lisp_Object window;
388 if (NILP (window))
389 return XWINDOW (selected_window);
391 CHECK_WINDOW (window);
392 return XWINDOW (window);
395 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
396 doc: /* Return the buffer that WINDOW is displaying. */)
397 (window)
398 Lisp_Object window;
400 return decode_window (window)->buffer;
403 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
404 doc: /* Return the number of lines in WINDOW (including its mode line). */)
405 (window)
406 Lisp_Object window;
408 return decode_any_window (window)->total_lines;
411 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
412 doc: /* Return the number of display columns in WINDOW.
413 This is the width that is usable columns available for text in WINDOW.
414 If you want to find out how many columns WINDOW takes up,
415 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
416 (window)
417 Lisp_Object window;
419 return make_number (window_box_text_cols (decode_any_window (window)));
422 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
423 doc: /* Return the number of columns by which WINDOW is scrolled from left margin. */)
424 (window)
425 Lisp_Object window;
427 return decode_window (window)->hscroll;
430 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
431 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
432 NCOL should be zero or positive.
434 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
435 window so that the location of point becomes invisible. */)
436 (window, ncol)
437 Lisp_Object window, ncol;
439 struct window *w = decode_window (window);
440 int hscroll;
442 CHECK_NUMBER (ncol);
443 hscroll = max (0, XINT (ncol));
445 /* Prevent redisplay shortcuts when changing the hscroll. */
446 if (XINT (w->hscroll) != hscroll)
447 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
449 w->hscroll = make_number (hscroll);
450 return ncol;
453 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
454 Swindow_redisplay_end_trigger, 0, 1, 0,
455 doc: /* Return WINDOW's redisplay end trigger value.
456 See `set-window-redisplay-end-trigger' for more information. */)
457 (window)
458 Lisp_Object window;
460 return decode_window (window)->redisplay_end_trigger;
463 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
464 Sset_window_redisplay_end_trigger, 2, 2, 0,
465 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
466 VALUE should be a buffer position (typically a marker) or nil.
467 If it is a buffer position, then if redisplay in WINDOW reaches a position
468 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
469 with two arguments: WINDOW, and the end trigger value.
470 Afterwards the end-trigger value is reset to nil. */)
471 (window, value)
472 register Lisp_Object window, value;
474 register struct window *w;
476 w = decode_window (window);
477 w->redisplay_end_trigger = value;
478 return value;
481 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
482 doc: /* Return a list of the edge coordinates of WINDOW.
483 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
484 RIGHT is one more than the rightmost column occupied by WINDOW,
485 and BOTTOM is one more than the bottommost row occupied by WINDOW.
486 The edges include the space used by the window's scroll bar,
487 display margins, fringes, header line, and mode line, if it has them.
488 To get the edges of the actual text area, use `window-inside-edges'. */)
489 (window)
490 Lisp_Object window;
492 register struct window *w = decode_any_window (window);
494 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
495 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
496 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
497 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
498 Qnil))));
501 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
502 doc: /* Return a list of the edge pixel coordinates of WINDOW.
503 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
504 RIGHT is one more than the rightmost x position occupied by WINDOW,
505 and BOTTOM is one more than the bottommost y position occupied by WINDOW.
506 The pixel edges include the space used by the window's scroll bar,
507 display margins, fringes, header line, and mode line, if it has them.
508 To get the edges of the actual text area, use `window-inside-pixel-edges'. */)
509 (window)
510 Lisp_Object window;
512 register struct window *w = decode_any_window (window);
514 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
515 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
516 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
517 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
518 Qnil))));
521 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
522 doc: /* Return a list of the edge coordinates of WINDOW.
523 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
524 RIGHT is one more than the rightmost column used by text in WINDOW,
525 and BOTTOM is one more than the bottommost row used by text in WINDOW.
526 The inside edges do not include the space used by the window's scroll bar,
527 display margins, fringes, header line, and/or mode line. */)
528 (window)
529 Lisp_Object window;
531 register struct window *w = decode_any_window (window);
533 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
534 + WINDOW_LEFT_MARGIN_COLS (w)
535 + WINDOW_LEFT_FRINGE_COLS (w)),
536 make_number (WINDOW_TOP_EDGE_LINE (w)
537 + WINDOW_HEADER_LINE_LINES (w)),
538 make_number (WINDOW_RIGHT_EDGE_COL (w)
539 - WINDOW_RIGHT_MARGIN_COLS (w)
540 - WINDOW_RIGHT_FRINGE_COLS (w)),
541 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
542 - WINDOW_MODE_LINE_LINES (w)));
545 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
546 doc: /* Return a list of the edge coordinates of WINDOW.
547 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
548 RIGHT is one more than the rightmost x position used by text in WINDOW,
549 and BOTTOM is one more than the bottommost y position used by text in WINDOW.
550 The inside edges do not include the space used by the window's scroll bar,
551 display margins, fringes, header line, and/or mode line. */)
552 (window)
553 Lisp_Object window;
555 register struct window *w = decode_any_window (window);
557 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
558 + WINDOW_LEFT_MARGIN_WIDTH (w)
559 + WINDOW_LEFT_FRINGE_WIDTH (w)),
560 make_number (WINDOW_TOP_EDGE_Y (w)
561 + WINDOW_HEADER_LINE_HEIGHT (w)),
562 make_number (WINDOW_RIGHT_EDGE_X (w)
563 - WINDOW_RIGHT_MARGIN_WIDTH (w)
564 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
565 make_number (WINDOW_BOTTOM_EDGE_Y (w)
566 - WINDOW_MODE_LINE_HEIGHT (w)));
569 /* Test if the character at column *X, row *Y is within window W.
570 If it is not, return ON_NOTHING;
571 if it is in the window's text area,
572 set *x and *y to its location relative to the upper left corner
573 of the window, and
574 return ON_TEXT;
575 if it is on the window's modeline, return ON_MODE_LINE;
576 if it is on the border between the window and its right sibling,
577 return ON_VERTICAL_BORDER.
578 if it is on the window's top line, return ON_HEADER_LINE;
579 if it is in left or right fringe of the window,
580 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
581 to window-relative coordinates;
582 if it is in the marginal area to the left/right of the window,
583 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
584 to window-relative coordinates.
586 X and Y are frame relative pixel coordinates. */
588 static enum window_part
589 coordinates_in_window (w, x, y)
590 register struct window *w;
591 register int *x, *y;
593 struct frame *f = XFRAME (WINDOW_FRAME (w));
594 int left_x, right_x, top_y, bottom_y;
595 enum window_part part;
596 int ux = FRAME_COLUMN_WIDTH (f);
597 int x0 = WINDOW_LEFT_EDGE_X (w);
598 int x1 = WINDOW_RIGHT_EDGE_X (w);
599 /* The width of the area where the vertical line can be dragged.
600 (Between mode lines for instance. */
601 int grabbable_width = ux;
602 int lmargin_width, rmargin_width, text_left, text_right;
604 if (*x < x0 || *x >= x1)
605 return ON_NOTHING;
607 /* In what's below, we subtract 1 when computing right_x because we
608 want the rightmost pixel, which is given by left_pixel+width-1. */
609 if (w->pseudo_window_p)
611 left_x = 0;
612 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
613 top_y = WINDOW_TOP_EDGE_Y (w);
614 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
616 else
618 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
619 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
620 top_y = WINDOW_TOP_EDGE_Y (w);
621 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
624 /* Outside any interesting row? */
625 if (*y < top_y || *y >= bottom_y)
626 return ON_NOTHING;
628 /* On the mode line or header line? If it's near the start of
629 the mode or header line of window that's has a horizontal
630 sibling, say it's on the vertical line. That's to be able
631 to resize windows horizontally in case we're using toolkit
632 scroll bars. */
634 if (WINDOW_WANTS_MODELINE_P (w)
635 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
637 part = ON_MODE_LINE;
639 header_vertical_border_check:
640 /* We're somewhere on the mode line. We consider the place
641 between mode lines of horizontally adjacent mode lines
642 as the vertical border. If scroll bars on the left,
643 return the right window. */
644 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
645 || WINDOW_RIGHTMOST_P (w))
647 if (!WINDOW_LEFTMOST_P (w) && abs (*x - x0) < grabbable_width)
648 return ON_VERTICAL_BORDER;
650 else
652 if (abs (*x - x1) < grabbable_width)
653 return ON_VERTICAL_BORDER;
656 /* Convert X and Y to window relative coordinates.
657 Mode line starts at left edge of window. */
658 *x -= x0;
659 *y -= top_y;
660 return part;
663 if (WINDOW_WANTS_HEADER_LINE_P (w)
664 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
666 part = ON_HEADER_LINE;
667 goto header_vertical_border_check;
670 /* Outside any interesting column? */
671 if (*x < left_x || *x > right_x)
672 return ON_NOTHING;
674 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
675 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
677 text_left = window_box_left (w, TEXT_AREA);
678 text_right = text_left + window_box_width (w, TEXT_AREA);
680 if (FRAME_WINDOW_P (f))
682 if (!w->pseudo_window_p
683 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
684 && !WINDOW_RIGHTMOST_P (w)
685 && (abs (*x - right_x) < grabbable_width))
686 return ON_VERTICAL_BORDER;
688 else
690 /* Need to say "*x > right_x" rather than >=, since on character
691 terminals, the vertical line's x coordinate is right_x. */
692 if (!w->pseudo_window_p
693 && !WINDOW_RIGHTMOST_P (w)
694 && *x > right_x - ux)
696 /* On the border on the right side of the window? Assume that
697 this area begins at RIGHT_X minus a canonical char width. */
698 return ON_VERTICAL_BORDER;
702 if (*x < text_left)
704 if (lmargin_width > 0
705 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
706 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
707 : (*x < left_x + lmargin_width)))
709 *x -= left_x;
710 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
711 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
712 *y -= top_y;
713 return ON_LEFT_MARGIN;
716 /* Convert X and Y to window-relative pixel coordinates. */
717 *x -= left_x;
718 *y -= top_y;
719 return ON_LEFT_FRINGE;
722 if (*x >= text_right)
724 if (rmargin_width > 0
725 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
726 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
727 : (*x >= right_x - rmargin_width)))
729 *x -= right_x;
730 if (!WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
731 *x -= WINDOW_RIGHT_FRINGE_WIDTH (w);
732 *y -= top_y;
733 return ON_RIGHT_MARGIN;
736 /* Convert X and Y to window-relative pixel coordinates. */
737 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
738 *y -= top_y;
739 return ON_RIGHT_FRINGE;
742 /* Everything special ruled out - must be on text area */
743 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
744 *y -= top_y;
745 return ON_TEXT;
749 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
750 Scoordinates_in_window_p, 2, 2, 0,
751 doc: /* Return non-nil if COORDINATES are in WINDOW.
752 COORDINATES is a cons of the form (X . Y), X and Y being distances
753 measured in characters from the upper-left corner of the frame.
754 \(0 . 0) denotes the character in the upper left corner of the
755 frame.
756 If COORDINATES are in the text portion of WINDOW,
757 the coordinates relative to the window are returned.
758 If they are in the mode line of WINDOW, `mode-line' is returned.
759 If they are in the top mode line of WINDOW, `header-line' is returned.
760 If they are in the left fringe of WINDOW, `left-fringe' is returned.
761 If they are in the right fringe of WINDOW, `right-fringe' is returned.
762 If they are on the border between WINDOW and its right sibling,
763 `vertical-line' is returned.
764 If they are in the windows's left or right marginal areas, `left-margin'\n\
765 or `right-margin' is returned. */)
766 (coordinates, window)
767 register Lisp_Object coordinates, window;
769 struct window *w;
770 struct frame *f;
771 int x, y;
772 Lisp_Object lx, ly;
774 CHECK_WINDOW (window);
775 w = XWINDOW (window);
776 f = XFRAME (w->frame);
777 CHECK_CONS (coordinates);
778 lx = Fcar (coordinates);
779 ly = Fcdr (coordinates);
780 CHECK_NUMBER_OR_FLOAT (lx);
781 CHECK_NUMBER_OR_FLOAT (ly);
782 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx);
783 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly);
785 switch (coordinates_in_window (w, &x, &y))
787 case ON_NOTHING:
788 return Qnil;
790 case ON_TEXT:
791 /* X and Y are now window relative pixel coordinates. Convert
792 them to canonical char units before returning them. */
793 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
794 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
796 case ON_MODE_LINE:
797 return Qmode_line;
799 case ON_VERTICAL_BORDER:
800 return Qvertical_line;
802 case ON_HEADER_LINE:
803 return Qheader_line;
805 case ON_LEFT_FRINGE:
806 return Qleft_fringe;
808 case ON_RIGHT_FRINGE:
809 return Qright_fringe;
811 case ON_LEFT_MARGIN:
812 return Qleft_margin;
814 case ON_RIGHT_MARGIN:
815 return Qright_margin;
817 default:
818 abort ();
823 /* Callback for foreach_window, used in window_from_coordinates.
824 Check if window W contains coordinates specified by USER_DATA which
825 is actually a pointer to a struct check_window_data CW.
827 Check if window W contains coordinates *CW->x and *CW->y. If it
828 does, return W in *CW->window, as Lisp_Object, and return in
829 *CW->part the part of the window under coordinates *X,*Y. Return
830 zero from this function to stop iterating over windows. */
832 struct check_window_data
834 Lisp_Object *window;
835 int *x, *y;
836 enum window_part *part;
839 static int
840 check_window_containing (w, user_data)
841 struct window *w;
842 void *user_data;
844 struct check_window_data *cw = (struct check_window_data *) user_data;
845 enum window_part found;
846 int continue_p = 1;
848 found = coordinates_in_window (w, cw->x, cw->y);
849 if (found != ON_NOTHING)
851 *cw->part = found;
852 XSETWINDOW (*cw->window, w);
853 continue_p = 0;
856 return continue_p;
860 /* Find the window containing frame-relative pixel position X/Y and
861 return it as a Lisp_Object.
863 If X, Y is on one of the window's special `window_part' elements,
864 set *PART to the id of that element, and return X and Y converted
865 to window relative coordinates in WX and WY.
867 If there is no window under X, Y return nil and leave *PART
868 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
870 This function was previously implemented with a loop cycling over
871 windows with Fnext_window, and starting with the frame's selected
872 window. It turned out that this doesn't work with an
873 implementation of next_window using Vwindow_list, because
874 FRAME_SELECTED_WINDOW (F) is not always contained in the window
875 tree of F when this function is called asynchronously from
876 note_mouse_highlight. The original loop didn't terminate in this
877 case. */
879 Lisp_Object
880 window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
881 struct frame *f;
882 int x, y;
883 enum window_part *part;
884 int *wx, *wy;
885 int tool_bar_p;
887 Lisp_Object window;
888 struct check_window_data cw;
889 enum window_part dummy;
891 if (part == 0)
892 part = &dummy;
894 window = Qnil;
895 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
896 foreach_window (f, check_window_containing, &cw);
898 /* If not found above, see if it's in the tool bar window, if a tool
899 bar exists. */
900 if (NILP (window)
901 && tool_bar_p
902 && WINDOWP (f->tool_bar_window)
903 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
904 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
905 != ON_NOTHING))
907 *part = ON_TEXT;
908 window = f->tool_bar_window;
911 if (wx) *wx = x;
912 if (wy) *wy = y;
914 return window;
917 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
918 doc: /* Return window containing coordinates X and Y on FRAME.
919 If omitted, FRAME defaults to the currently selected frame.
920 The top left corner of the frame is considered to be row 0,
921 column 0. */)
922 (x, y, frame)
923 Lisp_Object x, y, frame;
925 struct frame *f;
927 if (NILP (frame))
928 frame = selected_frame;
929 CHECK_LIVE_FRAME (frame);
930 f = XFRAME (frame);
932 /* Check that arguments are integers or floats. */
933 CHECK_NUMBER_OR_FLOAT (x);
934 CHECK_NUMBER_OR_FLOAT (y);
936 return window_from_coordinates (f,
937 FRAME_PIXEL_X_FROM_CANON_X (f, x),
938 FRAME_PIXEL_Y_FROM_CANON_Y (f, y),
939 0, 0, 0, 0);
942 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
943 doc: /* Return current value of point in WINDOW.
944 For a nonselected window, this is the value point would have
945 if that window were selected.
947 Note that, when WINDOW is the selected window and its buffer
948 is also currently selected, the value returned is the same as (point).
949 It would be more strictly correct to return the `top-level' value
950 of point, outside of any save-excursion forms.
951 But that is hard to define. */)
952 (window)
953 Lisp_Object window;
955 register struct window *w = decode_window (window);
957 if (w == XWINDOW (selected_window)
958 && current_buffer == XBUFFER (w->buffer))
959 return Fpoint ();
960 return Fmarker_position (w->pointm);
963 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
964 doc: /* Return position at which display currently starts in WINDOW.
965 This is updated by redisplay or by calling `set-window-start'. */)
966 (window)
967 Lisp_Object window;
969 return Fmarker_position (decode_window (window)->start);
972 /* This is text temporarily removed from the doc string below.
974 This function returns nil if the position is not currently known.
975 That happens when redisplay is preempted and doesn't finish.
976 If in that case you want to compute where the end of the window would
977 have been if redisplay had finished, do this:
978 (save-excursion
979 (goto-char (window-start window))
980 (vertical-motion (1- (window-height window)) window)
981 (point))") */
983 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
984 doc: /* Return position at which display currently ends in WINDOW.
985 This is updated by redisplay, when it runs to completion.
986 Simply changing the buffer text or setting `window-start'
987 does not update this value.
988 If UPDATE is non-nil, compute the up-to-date position
989 if it isn't already recorded. */)
990 (window, update)
991 Lisp_Object window, update;
993 Lisp_Object value;
994 struct window *w = decode_window (window);
995 Lisp_Object buf;
997 buf = w->buffer;
998 CHECK_BUFFER (buf);
1000 #if 0 /* This change broke some things. We should make it later. */
1001 /* If we don't know the end position, return nil.
1002 The user can compute it with vertical-motion if he wants to.
1003 It would be nicer to do it automatically,
1004 but that's so slow that it would probably bother people. */
1005 if (NILP (w->window_end_valid))
1006 return Qnil;
1007 #endif
1009 if (! NILP (update)
1010 && ! (! NILP (w->window_end_valid)
1011 && XFASTINT (w->last_modified) >= MODIFF))
1013 struct text_pos startp;
1014 struct it it;
1015 struct buffer *old_buffer = NULL, *b = XBUFFER (buf);
1017 /* In case W->start is out of the range, use something
1018 reasonable. This situation occurred when loading a file with
1019 `-l' containing a call to `rmail' with subsequent other
1020 commands. At the end, W->start happened to be BEG, while
1021 rmail had already narrowed the buffer. */
1022 if (XMARKER (w->start)->charpos < BEGV)
1023 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1024 else if (XMARKER (w->start)->charpos > ZV)
1025 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1026 else
1027 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1029 /* Cannot use Fvertical_motion because that function doesn't
1030 cope with variable-height lines. */
1031 if (b != current_buffer)
1033 old_buffer = current_buffer;
1034 set_buffer_internal (b);
1037 start_display (&it, w, startp);
1038 move_it_vertically (&it, window_box_height (w));
1039 if (it.current_y < it.last_visible_y)
1040 move_it_past_eol (&it);
1041 value = make_number (IT_CHARPOS (it));
1043 if (old_buffer)
1044 set_buffer_internal (old_buffer);
1046 else
1047 XSETINT (value, BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
1049 return value;
1052 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1053 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer. */)
1054 (window, pos)
1055 Lisp_Object window, pos;
1057 register struct window *w = decode_window (window);
1059 CHECK_NUMBER_COERCE_MARKER (pos);
1060 if (w == XWINDOW (selected_window)
1061 && XBUFFER (w->buffer) == current_buffer)
1062 Fgoto_char (pos);
1063 else
1064 set_marker_restricted (w->pointm, pos, w->buffer);
1066 /* We have to make sure that redisplay updates the window to show
1067 the new value of point. */
1068 if (!EQ (window, selected_window))
1069 ++windows_or_buffers_changed;
1071 return pos;
1074 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1075 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1076 Optional third arg NOFORCE non-nil inhibits next redisplay
1077 from overriding motion of point in order to display at this exact start. */)
1078 (window, pos, noforce)
1079 Lisp_Object window, pos, noforce;
1081 register struct window *w = decode_window (window);
1083 CHECK_NUMBER_COERCE_MARKER (pos);
1084 set_marker_restricted (w->start, pos, w->buffer);
1085 /* this is not right, but much easier than doing what is right. */
1086 w->start_at_line_beg = Qnil;
1087 if (NILP (noforce))
1088 w->force_start = Qt;
1089 w->update_mode_line = Qt;
1090 XSETFASTINT (w->last_modified, 0);
1091 XSETFASTINT (w->last_overlay_modified, 0);
1092 if (!EQ (window, selected_window))
1093 windows_or_buffers_changed++;
1095 return pos;
1098 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1099 1, 1, 0,
1100 doc: /* Return WINDOW's dedicated object, usually t or nil.
1101 See also `set-window-dedicated-p'. */)
1102 (window)
1103 Lisp_Object window;
1105 return decode_window (window)->dedicated;
1108 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1109 Sset_window_dedicated_p, 2, 2, 0,
1110 doc: /* Control whether WINDOW is dedicated to the buffer it displays.
1111 If it is dedicated, Emacs will not automatically change
1112 which buffer appears in it.
1113 The second argument is the new value for the dedication flag;
1114 non-nil means yes. */)
1115 (window, arg)
1116 Lisp_Object window, arg;
1118 register struct window *w = decode_window (window);
1120 w->dedicated = arg;
1122 return w->dedicated;
1125 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1126 0, 1, 0,
1127 doc: /* Return the display-table that WINDOW is using. */)
1128 (window)
1129 Lisp_Object window;
1131 return decode_window (window)->display_table;
1134 /* Get the display table for use on window W. This is either W's
1135 display table or W's buffer's display table. Ignore the specified
1136 tables if they are not valid; if no valid table is specified,
1137 return 0. */
1139 struct Lisp_Char_Table *
1140 window_display_table (w)
1141 struct window *w;
1143 struct Lisp_Char_Table *dp = NULL;
1145 if (DISP_TABLE_P (w->display_table))
1146 dp = XCHAR_TABLE (w->display_table);
1147 else if (BUFFERP (w->buffer))
1149 struct buffer *b = XBUFFER (w->buffer);
1151 if (DISP_TABLE_P (b->display_table))
1152 dp = XCHAR_TABLE (b->display_table);
1153 else if (DISP_TABLE_P (Vstandard_display_table))
1154 dp = XCHAR_TABLE (Vstandard_display_table);
1157 return dp;
1160 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1161 doc: /* Set WINDOW's display-table to TABLE. */)
1162 (window, table)
1163 register Lisp_Object window, table;
1165 register struct window *w;
1167 w = decode_window (window);
1168 w->display_table = table;
1169 return table;
1172 /* Record info on buffer window w is displaying
1173 when it is about to cease to display that buffer. */
1174 static void
1175 unshow_buffer (w)
1176 register struct window *w;
1178 Lisp_Object buf;
1179 struct buffer *b;
1181 buf = w->buffer;
1182 b = XBUFFER (buf);
1183 if (b != XMARKER (w->pointm)->buffer)
1184 abort ();
1186 #if 0
1187 if (w == XWINDOW (selected_window)
1188 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1189 /* Do this except when the selected window's buffer
1190 is being removed from some other window. */
1191 #endif
1192 /* last_window_start records the start position that this buffer
1193 had in the last window to be disconnected from it.
1194 Now that this statement is unconditional,
1195 it is possible for the buffer to be displayed in the
1196 selected window, while last_window_start reflects another
1197 window which was recently showing the same buffer.
1198 Some people might say that might be a good thing. Let's see. */
1199 b->last_window_start = marker_position (w->start);
1201 /* Point in the selected window's buffer
1202 is actually stored in that buffer, and the window's pointm isn't used.
1203 So don't clobber point in that buffer. */
1204 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1205 /* This line helps to fix Horsley's testbug.el bug. */
1206 && !(WINDOWP (b->last_selected_window)
1207 && w != XWINDOW (b->last_selected_window)
1208 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1209 temp_set_point_both (b,
1210 clip_to_bounds (BUF_BEGV (b),
1211 XMARKER (w->pointm)->charpos,
1212 BUF_ZV (b)),
1213 clip_to_bounds (BUF_BEGV_BYTE (b),
1214 marker_byte_position (w->pointm),
1215 BUF_ZV_BYTE (b)));
1217 if (WINDOWP (b->last_selected_window)
1218 && w == XWINDOW (b->last_selected_window))
1219 b->last_selected_window = Qnil;
1222 /* Put replacement into the window structure in place of old. */
1223 static void
1224 replace_window (old, replacement)
1225 Lisp_Object old, replacement;
1227 register Lisp_Object tem;
1228 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1230 /* If OLD is its frame's root_window, then replacement is the new
1231 root_window for that frame. */
1233 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1234 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1236 p->left_col = o->left_col;
1237 p->top_line = o->top_line;
1238 p->total_cols = o->total_cols;
1239 p->total_lines = o->total_lines;
1240 p->desired_matrix = p->current_matrix = 0;
1241 p->vscroll = 0;
1242 bzero (&p->cursor, sizeof (p->cursor));
1243 bzero (&p->last_cursor, sizeof (p->last_cursor));
1244 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1245 p->phys_cursor_type = -1;
1246 p->phys_cursor_width = -1;
1247 p->must_be_updated_p = 0;
1248 p->pseudo_window_p = 0;
1249 XSETFASTINT (p->window_end_vpos, 0);
1250 XSETFASTINT (p->window_end_pos, 0);
1251 p->window_end_valid = Qnil;
1252 p->frozen_window_start_p = 0;
1253 p->orig_top_line = p->orig_total_lines = Qnil;
1255 p->next = tem = o->next;
1256 if (!NILP (tem))
1257 XWINDOW (tem)->prev = replacement;
1259 p->prev = tem = o->prev;
1260 if (!NILP (tem))
1261 XWINDOW (tem)->next = replacement;
1263 p->parent = tem = o->parent;
1264 if (!NILP (tem))
1266 if (EQ (XWINDOW (tem)->vchild, old))
1267 XWINDOW (tem)->vchild = replacement;
1268 if (EQ (XWINDOW (tem)->hchild, old))
1269 XWINDOW (tem)->hchild = replacement;
1272 /*** Here, if replacement is a vertical combination
1273 and so is its new parent, we should make replacement's
1274 children be children of that parent instead. ***/
1277 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1278 doc: /* Remove WINDOW from the display. Default is selected window. */)
1279 (window)
1280 register Lisp_Object window;
1282 delete_window (window);
1284 if (! NILP (Vwindow_configuration_change_hook)
1285 && ! NILP (Vrun_hooks))
1286 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1288 return Qnil;
1291 void
1292 delete_window (window)
1293 register Lisp_Object window;
1295 register Lisp_Object tem, parent, sib;
1296 register struct window *p;
1297 register struct window *par;
1298 struct frame *f;
1300 /* Because this function is called by other C code on non-leaf
1301 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1302 so we can't decode_window here. */
1303 if (NILP (window))
1304 window = selected_window;
1305 else
1306 CHECK_WINDOW (window);
1307 p = XWINDOW (window);
1309 /* It's okay to delete an already-deleted window. */
1310 if (NILP (p->buffer)
1311 && NILP (p->hchild)
1312 && NILP (p->vchild))
1313 return;
1315 parent = p->parent;
1316 if (NILP (parent))
1317 error ("Attempt to delete minibuffer or sole ordinary window");
1318 par = XWINDOW (parent);
1320 windows_or_buffers_changed++;
1321 Vwindow_list = Qnil;
1322 f = XFRAME (WINDOW_FRAME (p));
1323 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1325 /* Are we trying to delete any frame's selected window? */
1327 Lisp_Object swindow, pwindow;
1329 /* See if the frame's selected window is either WINDOW
1330 or any subwindow of it, by finding all that window's parents
1331 and comparing each one with WINDOW. */
1332 swindow = FRAME_SELECTED_WINDOW (f);
1334 while (1)
1336 pwindow = swindow;
1337 while (!NILP (pwindow))
1339 if (EQ (window, pwindow))
1340 break;
1341 pwindow = XWINDOW (pwindow)->parent;
1344 /* If the window being deleted is not a parent of SWINDOW,
1345 then SWINDOW is ok as the new selected window. */
1346 if (!EQ (window, pwindow))
1347 break;
1348 /* Otherwise, try another window for SWINDOW. */
1349 swindow = Fnext_window (swindow, Qlambda, Qnil);;
1351 /* If we get back to the frame's selected window,
1352 it means there was no acceptable alternative,
1353 so we cannot delete. */
1354 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1355 error ("Cannot delete window");
1358 /* If we need to change SWINDOW, do it. */
1359 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1361 /* If we're about to delete the selected window on the
1362 selected frame, then we should use Fselect_window to select
1363 the new window. On the other hand, if we're about to
1364 delete the selected window on any other frame, we shouldn't do
1365 anything but set the frame's selected_window slot. */
1366 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1367 Fselect_window (swindow, Qnil);
1368 else
1369 FRAME_SELECTED_WINDOW (f) = swindow;
1373 tem = p->buffer;
1374 /* tem is null for dummy parent windows
1375 (which have inferiors but not any contents themselves) */
1376 if (!NILP (tem))
1378 unshow_buffer (p);
1379 unchain_marker (XMARKER (p->pointm));
1380 unchain_marker (XMARKER (p->start));
1383 /* Free window glyph matrices. It is sure that they are allocated
1384 again when ADJUST_GLYPHS is called. Block input so that expose
1385 events and other events that access glyph matrices are not
1386 processed while we are changing them. */
1387 BLOCK_INPUT;
1388 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1390 tem = p->next;
1391 if (!NILP (tem))
1392 XWINDOW (tem)->prev = p->prev;
1394 tem = p->prev;
1395 if (!NILP (tem))
1396 XWINDOW (tem)->next = p->next;
1398 if (EQ (window, par->hchild))
1399 par->hchild = p->next;
1400 if (EQ (window, par->vchild))
1401 par->vchild = p->next;
1403 /* Find one of our siblings to give our space to. */
1404 sib = p->prev;
1405 if (NILP (sib))
1407 /* If p gives its space to its next sibling, that sibling needs
1408 to have its top/left side pulled back to where p's is.
1409 set_window_{height,width} will re-position the sibling's
1410 children. */
1411 sib = p->next;
1412 XWINDOW (sib)->top_line = p->top_line;
1413 XWINDOW (sib)->left_col = p->left_col;
1416 /* Stretch that sibling. */
1417 if (!NILP (par->vchild))
1418 set_window_height (sib,
1419 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1421 if (!NILP (par->hchild))
1422 set_window_width (sib,
1423 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1426 /* If parent now has only one child,
1427 put the child into the parent's place. */
1428 tem = par->hchild;
1429 if (NILP (tem))
1430 tem = par->vchild;
1431 if (NILP (XWINDOW (tem)->next))
1432 replace_window (parent, tem);
1434 /* Since we may be deleting combination windows, we must make sure that
1435 not only p but all its children have been marked as deleted. */
1436 if (! NILP (p->hchild))
1437 delete_all_subwindows (XWINDOW (p->hchild));
1438 else if (! NILP (p->vchild))
1439 delete_all_subwindows (XWINDOW (p->vchild));
1441 /* Mark this window as deleted. */
1442 p->buffer = p->hchild = p->vchild = Qnil;
1444 /* Adjust glyph matrices. */
1445 adjust_glyphs (f);
1446 UNBLOCK_INPUT;
1451 /***********************************************************************
1452 Window List
1453 ***********************************************************************/
1455 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1456 pointer. This is a callback function for foreach_window, used in
1457 function window_list. */
1459 static int
1460 add_window_to_list (w, user_data)
1461 struct window *w;
1462 void *user_data;
1464 Lisp_Object *list = (Lisp_Object *) user_data;
1465 Lisp_Object window;
1466 XSETWINDOW (window, w);
1467 *list = Fcons (window, *list);
1468 return 1;
1472 /* Return a list of all windows, for use by next_window. If
1473 Vwindow_list is a list, return that list. Otherwise, build a new
1474 list, cache it in Vwindow_list, and return that. */
1476 static Lisp_Object
1477 window_list ()
1479 if (!CONSP (Vwindow_list))
1481 Lisp_Object tail;
1483 Vwindow_list = Qnil;
1484 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1486 Lisp_Object args[2];
1488 /* We are visiting windows in canonical order, and add
1489 new windows at the front of args[1], which means we
1490 have to reverse this list at the end. */
1491 args[1] = Qnil;
1492 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1493 args[0] = Vwindow_list;
1494 args[1] = Fnreverse (args[1]);
1495 Vwindow_list = Fnconc (2, args);
1499 return Vwindow_list;
1503 /* Value is non-zero if WINDOW satisfies the constraints given by
1504 OWINDOW, MINIBUF and ALL_FRAMES.
1506 MINIBUF t means WINDOW may be minibuffer windows.
1507 `lambda' means WINDOW may not be a minibuffer window.
1508 a window means a specific minibuffer window
1510 ALL_FRAMES t means search all frames,
1511 nil means search just current frame,
1512 `visible' means search just visible frames,
1513 0 means search visible and iconified frames,
1514 a window means search the frame that window belongs to,
1515 a frame means consider windows on that frame, only. */
1517 static int
1518 candidate_window_p (window, owindow, minibuf, all_frames)
1519 Lisp_Object window, owindow, minibuf, all_frames;
1521 struct window *w = XWINDOW (window);
1522 struct frame *f = XFRAME (w->frame);
1523 int candidate_p = 1;
1525 if (!BUFFERP (w->buffer))
1526 candidate_p = 0;
1527 else if (MINI_WINDOW_P (w)
1528 && (EQ (minibuf, Qlambda)
1529 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1531 /* If MINIBUF is `lambda' don't consider any mini-windows.
1532 If it is a window, consider only that one. */
1533 candidate_p = 0;
1535 else if (EQ (all_frames, Qt))
1536 candidate_p = 1;
1537 else if (NILP (all_frames))
1539 xassert (WINDOWP (owindow));
1540 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1542 else if (EQ (all_frames, Qvisible))
1544 FRAME_SAMPLE_VISIBILITY (f);
1545 candidate_p = FRAME_VISIBLE_P (f);
1547 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1549 FRAME_SAMPLE_VISIBILITY (f);
1550 candidate_p = FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1552 else if (WINDOWP (all_frames))
1553 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1554 || EQ (XWINDOW (all_frames)->frame, w->frame)
1555 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1556 else if (FRAMEP (all_frames))
1557 candidate_p = EQ (all_frames, w->frame);
1559 return candidate_p;
1563 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1564 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1565 ALL_FRAMES. */
1567 static void
1568 decode_next_window_args (window, minibuf, all_frames)
1569 Lisp_Object *window, *minibuf, *all_frames;
1571 if (NILP (*window))
1572 *window = selected_window;
1573 else
1574 CHECK_LIVE_WINDOW (*window);
1576 /* MINIBUF nil may or may not include minibuffers. Decide if it
1577 does. */
1578 if (NILP (*minibuf))
1579 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1580 else if (!EQ (*minibuf, Qt))
1581 *minibuf = Qlambda;
1583 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1584 => count none of them, or a specific minibuffer window (the
1585 active one) to count. */
1587 /* ALL_FRAMES nil doesn't specify which frames to include. */
1588 if (NILP (*all_frames))
1589 *all_frames = (!EQ (*minibuf, Qlambda)
1590 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1591 : Qnil);
1592 else if (EQ (*all_frames, Qvisible))
1594 else if (XFASTINT (*all_frames) == 0)
1596 else if (FRAMEP (*all_frames))
1598 else if (!EQ (*all_frames, Qt))
1599 *all_frames = Qnil;
1601 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1602 search just current frame, `visible' meaning search just visible
1603 frames, 0 meaning search visible and iconified frames, or a
1604 window, meaning search the frame that window belongs to, or a
1605 frame, meaning consider windows on that frame, only. */
1609 /* Return the next or previous window of WINDOW in canonical ordering
1610 of windows. NEXT_P non-zero means return the next window. See the
1611 documentation string of next-window for the meaning of MINIBUF and
1612 ALL_FRAMES. */
1614 static Lisp_Object
1615 next_window (window, minibuf, all_frames, next_p)
1616 Lisp_Object window, minibuf, all_frames;
1617 int next_p;
1619 decode_next_window_args (&window, &minibuf, &all_frames);
1621 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1622 return the first window on the frame. */
1623 if (FRAMEP (all_frames)
1624 && !EQ (all_frames, XWINDOW (window)->frame))
1625 return Fframe_first_window (all_frames);
1627 if (next_p)
1629 Lisp_Object list;
1631 /* Find WINDOW in the list of all windows. */
1632 list = Fmemq (window, window_list ());
1634 /* Scan forward from WINDOW to the end of the window list. */
1635 if (CONSP (list))
1636 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1637 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1638 break;
1640 /* Scan from the start of the window list up to WINDOW. */
1641 if (!CONSP (list))
1642 for (list = Vwindow_list;
1643 CONSP (list) && !EQ (XCAR (list), window);
1644 list = XCDR (list))
1645 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1646 break;
1648 if (CONSP (list))
1649 window = XCAR (list);
1651 else
1653 Lisp_Object candidate, list;
1655 /* Scan through the list of windows for candidates. If there are
1656 candidate windows in front of WINDOW, the last one of these
1657 is the one we want. If there are candidates following WINDOW
1658 in the list, again the last one of these is the one we want. */
1659 candidate = Qnil;
1660 for (list = window_list (); CONSP (list); list = XCDR (list))
1662 if (EQ (XCAR (list), window))
1664 if (WINDOWP (candidate))
1665 break;
1667 else if (candidate_window_p (XCAR (list), window, minibuf,
1668 all_frames))
1669 candidate = XCAR (list);
1672 if (WINDOWP (candidate))
1673 window = candidate;
1676 return window;
1680 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1681 doc: /* Return next window after WINDOW in canonical ordering of windows.
1682 If omitted, WINDOW defaults to the selected window.
1684 Optional second arg MINIBUF t means count the minibuffer window even
1685 if not active. MINIBUF nil or omitted means count the minibuffer iff
1686 it is active. MINIBUF neither t nor nil means not to count the
1687 minibuffer even if it is active.
1689 Several frames may share a single minibuffer; if the minibuffer
1690 counts, all windows on all frames that share that minibuffer count
1691 too. Therefore, `next-window' can be used to iterate through the
1692 set of windows even when the minibuffer is on another frame. If the
1693 minibuffer does not count, only windows from WINDOW's frame count.
1695 Optional third arg ALL-FRAMES t means include windows on all frames.
1696 ALL-FRAMES nil or omitted means cycle within the frames as specified
1697 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1698 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1699 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1700 Anything else means restrict to WINDOW's frame.
1702 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1703 `next-window' to iterate through the entire cycle of acceptable
1704 windows, eventually ending up back at the window you started with.
1705 `previous-window' traverses the same cycle, in the reverse order. */)
1706 (window, minibuf, all_frames)
1707 Lisp_Object window, minibuf, all_frames;
1709 return next_window (window, minibuf, all_frames, 1);
1713 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1714 doc: /* Return the window preceding WINDOW in canonical ordering of windows.
1715 If omitted, WINDOW defaults to the selected window.
1717 Optional second arg MINIBUF t means count the minibuffer window even
1718 if not active. MINIBUF nil or omitted means count the minibuffer iff
1719 it is active. MINIBUF neither t nor nil means not to count the
1720 minibuffer even if it is active.
1722 Several frames may share a single minibuffer; if the minibuffer
1723 counts, all windows on all frames that share that minibuffer count
1724 too. Therefore, `previous-window' can be used to iterate through
1725 the set of windows even when the minibuffer is on another frame. If
1726 the minibuffer does not count, only windows from WINDOW's frame count
1728 Optional third arg ALL-FRAMES t means include windows on all frames.
1729 ALL-FRAMES nil or omitted means cycle within the frames as specified
1730 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1731 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1732 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1733 Anything else means restrict to WINDOW's frame.
1735 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1736 `previous-window' to iterate through the entire cycle of acceptable
1737 windows, eventually ending up back at the window you started with.
1738 `next-window' traverses the same cycle, in the reverse order. */)
1739 (window, minibuf, all_frames)
1740 Lisp_Object window, minibuf, all_frames;
1742 return next_window (window, minibuf, all_frames, 0);
1746 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1747 doc: /* Select the ARG'th different window on this frame.
1748 All windows on current frame are arranged in a cyclic order.
1749 This command selects the window ARG steps away in that order.
1750 A negative ARG moves in the opposite order. If the optional second
1751 argument ALL_FRAMES is non-nil, cycle through all frames. */)
1752 (arg, all_frames)
1753 Lisp_Object arg, all_frames;
1755 Lisp_Object window;
1756 int i;
1758 CHECK_NUMBER (arg);
1759 window = selected_window;
1761 for (i = XINT (arg); i > 0; --i)
1762 window = Fnext_window (window, Qnil, all_frames);
1763 for (; i < 0; ++i)
1764 window = Fprevious_window (window, Qnil, all_frames);
1766 Fselect_window (window, Qnil);
1767 return Qnil;
1771 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
1772 doc: /* Return a list of windows on FRAME, starting with WINDOW.
1773 FRAME nil or omitted means use the selected frame.
1774 WINDOW nil or omitted means use the selected window.
1775 MINIBUF t means include the minibuffer window, even if it isn't active.
1776 MINIBUF nil or omitted means include the minibuffer window only
1777 if it's active.
1778 MINIBUF neither nil nor t means never include the minibuffer window. */)
1779 (frame, minibuf, window)
1780 Lisp_Object frame, minibuf, window;
1782 if (NILP (window))
1783 window = selected_window;
1784 if (NILP (frame))
1785 frame = selected_frame;
1787 if (!EQ (frame, XWINDOW (window)->frame))
1788 error ("Window is on a different frame");
1790 return window_list_1 (window, minibuf, frame);
1794 /* Return a list of windows in canonical ordering. Arguments are like
1795 for `next-window'. */
1797 static Lisp_Object
1798 window_list_1 (window, minibuf, all_frames)
1799 Lisp_Object window, minibuf, all_frames;
1801 Lisp_Object tail, list;
1803 decode_next_window_args (&window, &minibuf, &all_frames);
1804 list = Qnil;
1806 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
1807 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
1808 list = Fcons (XCAR (tail), list);
1810 return Fnreverse (list);
1815 /* Look at all windows, performing an operation specified by TYPE
1816 with argument OBJ.
1817 If FRAMES is Qt, look at all frames;
1818 Qnil, look at just the selected frame;
1819 Qvisible, look at visible frames;
1820 a frame, just look at windows on that frame.
1821 If MINI is non-zero, perform the operation on minibuffer windows too. */
1823 enum window_loop
1825 WINDOW_LOOP_UNUSED,
1826 GET_BUFFER_WINDOW, /* Arg is buffer */
1827 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1828 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1829 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1830 GET_LARGEST_WINDOW,
1831 UNSHOW_BUFFER, /* Arg is buffer */
1832 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
1833 CHECK_ALL_WINDOWS
1836 static Lisp_Object
1837 window_loop (type, obj, mini, frames)
1838 enum window_loop type;
1839 Lisp_Object obj, frames;
1840 int mini;
1842 Lisp_Object window, windows, best_window, frame_arg;
1843 struct frame *f;
1844 struct gcpro gcpro1;
1846 /* If we're only looping through windows on a particular frame,
1847 frame points to that frame. If we're looping through windows
1848 on all frames, frame is 0. */
1849 if (FRAMEP (frames))
1850 f = XFRAME (frames);
1851 else if (NILP (frames))
1852 f = SELECTED_FRAME ();
1853 else
1854 f = NULL;
1856 if (f)
1857 frame_arg = Qlambda;
1858 else if (XFASTINT (frames) == 0)
1859 frame_arg = frames;
1860 else if (EQ (frames, Qvisible))
1861 frame_arg = frames;
1862 else
1863 frame_arg = Qt;
1865 /* frame_arg is Qlambda to stick to one frame,
1866 Qvisible to consider all visible frames,
1867 or Qt otherwise. */
1869 /* Pick a window to start with. */
1870 if (WINDOWP (obj))
1871 window = obj;
1872 else if (f)
1873 window = FRAME_SELECTED_WINDOW (f);
1874 else
1875 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
1877 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
1878 GCPRO1 (windows);
1879 best_window = Qnil;
1881 for (; CONSP (windows); windows = CDR (windows))
1883 struct window *w;
1885 window = XCAR (windows);
1886 w = XWINDOW (window);
1888 /* Note that we do not pay attention here to whether the frame
1889 is visible, since Fwindow_list skips non-visible frames if
1890 that is desired, under the control of frame_arg. */
1891 if (!MINI_WINDOW_P (w)
1892 /* For UNSHOW_BUFFER, we must always consider all windows. */
1893 || type == UNSHOW_BUFFER
1894 || (mini && minibuf_level > 0))
1895 switch (type)
1897 case GET_BUFFER_WINDOW:
1898 if (EQ (w->buffer, obj)
1899 /* Don't find any minibuffer window
1900 except the one that is currently in use. */
1901 && (MINI_WINDOW_P (w)
1902 ? EQ (window, minibuf_window)
1903 : 1))
1905 if (NILP (best_window))
1906 best_window = window;
1907 else if (EQ (window, selected_window))
1908 /* For compatibility with 20.x, prefer to return
1909 selected-window. */
1910 best_window = window;
1912 break;
1914 case GET_LRU_WINDOW:
1915 /* t as arg means consider only full-width windows */
1916 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (w))
1917 break;
1918 /* Ignore dedicated windows and minibuffers. */
1919 if (MINI_WINDOW_P (w) || EQ (w->dedicated, Qt))
1920 break;
1921 if (NILP (best_window)
1922 || (XFASTINT (XWINDOW (best_window)->use_time)
1923 > XFASTINT (w->use_time)))
1924 best_window = window;
1925 break;
1927 case DELETE_OTHER_WINDOWS:
1928 if (!EQ (window, obj))
1929 Fdelete_window (window);
1930 break;
1932 case DELETE_BUFFER_WINDOWS:
1933 if (EQ (w->buffer, obj))
1935 struct frame *f = XFRAME (WINDOW_FRAME (w));
1937 /* If this window is dedicated, and in a frame of its own,
1938 kill the frame. */
1939 if (EQ (window, FRAME_ROOT_WINDOW (f))
1940 && !NILP (w->dedicated)
1941 && other_visible_frames (f))
1943 /* Skip the other windows on this frame.
1944 There might be one, the minibuffer! */
1945 while (CONSP (XCDR (windows))
1946 && EQ (XWINDOW (XCAR (windows))->frame,
1947 XWINDOW (XCAR (XCDR (windows)))->frame))
1948 windows = XCDR (windows);
1950 /* Now we can safely delete the frame. */
1951 Fdelete_frame (w->frame, Qnil);
1953 else if (NILP (w->parent))
1955 /* If we're deleting the buffer displayed in the
1956 only window on the frame, find a new buffer to
1957 display there. */
1958 Lisp_Object buffer;
1959 buffer = Fother_buffer (obj, Qnil, w->frame);
1960 Fset_window_buffer (window, buffer, Qnil);
1961 if (EQ (window, selected_window))
1962 Fset_buffer (w->buffer);
1964 else
1965 Fdelete_window (window);
1967 break;
1969 case GET_LARGEST_WINDOW:
1971 /* Ignore dedicated windows and minibuffers. */
1972 if (MINI_WINDOW_P (w) || EQ (w->dedicated, Qt))
1973 break;
1975 if (NILP (best_window))
1976 best_window = window;
1977 else
1979 struct window *b = XWINDOW (best_window);
1980 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
1981 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
1982 best_window = window;
1985 break;
1987 case UNSHOW_BUFFER:
1988 if (EQ (w->buffer, obj))
1990 Lisp_Object buffer;
1991 struct frame *f = XFRAME (w->frame);
1993 /* Find another buffer to show in this window. */
1994 buffer = Fother_buffer (obj, Qnil, w->frame);
1996 /* If this window is dedicated, and in a frame of its own,
1997 kill the frame. */
1998 if (EQ (window, FRAME_ROOT_WINDOW (f))
1999 && !NILP (w->dedicated)
2000 && other_visible_frames (f))
2002 /* Skip the other windows on this frame.
2003 There might be one, the minibuffer! */
2004 while (CONSP (XCDR (windows))
2005 && EQ (XWINDOW (XCAR (windows))->frame,
2006 XWINDOW (XCAR (XCDR (windows)))->frame))
2007 windows = XCDR (windows);
2009 /* Now we can safely delete the frame. */
2010 Fdelete_frame (w->frame, Qnil);
2012 else if (!NILP (w->dedicated) && !NILP (w->parent))
2014 Lisp_Object window;
2015 XSETWINDOW (window, w);
2016 /* If this window is dedicated and not the only window
2017 in its frame, then kill it. */
2018 Fdelete_window (window);
2020 else
2022 /* Otherwise show a different buffer in the window. */
2023 w->dedicated = Qnil;
2024 Fset_window_buffer (window, buffer, Qnil);
2025 if (EQ (window, selected_window))
2026 Fset_buffer (w->buffer);
2029 break;
2031 case REDISPLAY_BUFFER_WINDOWS:
2032 if (EQ (w->buffer, obj))
2034 mark_window_display_accurate (window, 0);
2035 w->update_mode_line = Qt;
2036 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2037 ++update_mode_lines;
2038 best_window = window;
2040 break;
2042 /* Check for a window that has a killed buffer. */
2043 case CHECK_ALL_WINDOWS:
2044 if (! NILP (w->buffer)
2045 && NILP (XBUFFER (w->buffer)->name))
2046 abort ();
2047 break;
2049 case WINDOW_LOOP_UNUSED:
2050 break;
2054 UNGCPRO;
2055 return best_window;
2058 /* Used for debugging. Abort if any window has a dead buffer. */
2060 void
2061 check_all_windows ()
2063 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2066 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
2067 doc: /* Return the window least recently selected or used for display.
2068 Return a full-width window if possible.
2069 If optional argument FRAME is `visible', search all visible frames.
2070 If FRAME is 0, search all visible and iconified frames.
2071 If FRAME is t, search all frames.
2072 If FRAME is nil, search only the selected frame.
2073 If FRAME is a frame, search only that frame. */)
2074 (frame)
2075 Lisp_Object frame;
2077 register Lisp_Object w;
2078 /* First try for a window that is full-width */
2079 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
2080 if (!NILP (w) && !EQ (w, selected_window))
2081 return w;
2082 /* If none of them, try the rest */
2083 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
2086 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
2087 doc: /* Return the largest window in area.
2088 If optional argument FRAME is `visible', search all visible frames.
2089 If FRAME is 0, search all visible and iconified frames.
2090 If FRAME is t, search all frames.
2091 If FRAME is nil, search only the selected frame.
2092 If FRAME is a frame, search only that frame. */)
2093 (frame)
2094 Lisp_Object frame;
2096 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
2097 frame);
2100 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
2101 doc: /* Return a window currently displaying BUFFER, or nil if none.
2102 If optional argument FRAME is `visible', search all visible frames.
2103 If optional argument FRAME is 0, search all visible and iconified frames.
2104 If FRAME is t, search all frames.
2105 If FRAME is nil, search only the selected frame.
2106 If FRAME is a frame, search only that frame. */)
2107 (buffer, frame)
2108 Lisp_Object buffer, frame;
2110 buffer = Fget_buffer (buffer);
2111 if (BUFFERP (buffer))
2112 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2113 else
2114 return Qnil;
2117 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2118 0, 1, "",
2119 doc: /* Make WINDOW (or the selected window) fill its frame.
2120 Only the frame WINDOW is on is affected.
2121 This function tries to reduce display jumps
2122 by keeping the text previously visible in WINDOW
2123 in the same place on the frame. Doing this depends on
2124 the value of (window-start WINDOW), so if calling this function
2125 in a program gives strange scrolling, make sure the window-start
2126 value is reasonable when this function is called. */)
2127 (window)
2128 Lisp_Object window;
2130 struct window *w;
2131 int startpos;
2132 int top, new_top;
2134 if (NILP (window))
2135 window = selected_window;
2136 else
2137 CHECK_LIVE_WINDOW (window);
2138 w = XWINDOW (window);
2140 startpos = marker_position (w->start);
2141 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2143 if (MINI_WINDOW_P (w) && top > 0)
2144 error ("Can't expand minibuffer to full frame");
2146 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2148 /* Try to minimize scrolling, by setting the window start to the point
2149 will cause the text at the old window start to be at the same place
2150 on the frame. But don't try to do this if the window start is
2151 outside the visible portion (as might happen when the display is
2152 not current, due to typeahead). */
2153 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2154 if (new_top != top
2155 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2156 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2158 struct position pos;
2159 struct buffer *obuf = current_buffer;
2161 Fset_buffer (w->buffer);
2162 /* This computation used to temporarily move point, but that can
2163 have unwanted side effects due to text properties. */
2164 pos = *vmotion (startpos, -top, w);
2166 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2167 w->window_end_valid = Qnil;
2168 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2169 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2170 : Qnil);
2171 /* We need to do this, so that the window-scroll-functions
2172 get called. */
2173 w->optional_new_start = Qt;
2175 set_buffer_internal (obuf);
2178 return Qnil;
2181 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2182 1, 2, "bDelete windows on (buffer): ",
2183 doc: /* Delete all windows showing BUFFER.
2184 Optional second argument FRAME controls which frames are affected.
2185 If optional argument FRAME is `visible', search all visible frames.
2186 If FRAME is 0, search all visible and iconified frames.
2187 If FRAME is nil, search all frames.
2188 If FRAME is t, search only the selected frame.
2189 If FRAME is a frame, search only that frame. */)
2190 (buffer, frame)
2191 Lisp_Object buffer, frame;
2193 /* FRAME uses t and nil to mean the opposite of what window_loop
2194 expects. */
2195 if (NILP (frame))
2196 frame = Qt;
2197 else if (EQ (frame, Qt))
2198 frame = Qnil;
2200 if (!NILP (buffer))
2202 buffer = Fget_buffer (buffer);
2203 CHECK_BUFFER (buffer);
2204 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2207 return Qnil;
2210 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2211 Sreplace_buffer_in_windows,
2212 1, 1, "bReplace buffer in windows: ",
2213 doc: /* Replace BUFFER with some other buffer in all windows showing it. */)
2214 (buffer)
2215 Lisp_Object buffer;
2217 if (!NILP (buffer))
2219 buffer = Fget_buffer (buffer);
2220 CHECK_BUFFER (buffer);
2221 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2223 return Qnil;
2226 /* Replace BUFFER with some other buffer in all windows
2227 of all frames, even those on other keyboards. */
2229 void
2230 replace_buffer_in_all_windows (buffer)
2231 Lisp_Object buffer;
2233 #ifdef MULTI_KBOARD
2234 Lisp_Object tail, frame;
2236 /* A single call to window_loop won't do the job
2237 because it only considers frames on the current keyboard.
2238 So loop manually over frames, and handle each one. */
2239 FOR_EACH_FRAME (tail, frame)
2240 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2241 #else
2242 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
2243 #endif
2246 /* Set the height of WINDOW and all its inferiors. */
2248 /* The smallest acceptable dimensions for a window. Anything smaller
2249 might crash Emacs. */
2251 #define MIN_SAFE_WINDOW_WIDTH (2)
2252 #define MIN_SAFE_WINDOW_HEIGHT (1)
2254 /* Make sure that window_min_height and window_min_width are
2255 not too small; if they are, set them to safe minima. */
2257 static void
2258 check_min_window_sizes ()
2260 /* Smaller values might permit a crash. */
2261 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
2262 window_min_width = MIN_SAFE_WINDOW_WIDTH;
2263 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
2264 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
2267 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2268 minimum allowable size. */
2270 void
2271 check_frame_size (frame, rows, cols)
2272 FRAME_PTR frame;
2273 int *rows, *cols;
2275 /* For height, we have to see:
2276 how many windows the frame has at minimum (one or two),
2277 and whether it has a menu bar or other special stuff at the top. */
2278 int min_height
2279 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2280 ? MIN_SAFE_WINDOW_HEIGHT
2281 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2283 if (FRAME_TOP_MARGIN (frame) > 0)
2284 min_height += FRAME_TOP_MARGIN (frame);
2286 if (*rows < min_height)
2287 *rows = min_height;
2288 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2289 *cols = MIN_SAFE_WINDOW_WIDTH;
2293 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2294 check if W's width can be changed, otherwise check W's height.
2295 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2296 siblings, too. If none of the siblings is resizable, WINDOW isn't
2297 either. */
2299 static int
2300 window_fixed_size_p (w, width_p, check_siblings_p)
2301 struct window *w;
2302 int width_p, check_siblings_p;
2304 int fixed_p;
2305 struct window *c;
2307 if (!NILP (w->hchild))
2309 c = XWINDOW (w->hchild);
2311 if (width_p)
2313 /* A horiz. combination is fixed-width if all of if its
2314 children are. */
2315 while (c && window_fixed_size_p (c, width_p, 0))
2316 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2317 fixed_p = c == NULL;
2319 else
2321 /* A horiz. combination is fixed-height if one of if its
2322 children is. */
2323 while (c && !window_fixed_size_p (c, width_p, 0))
2324 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2325 fixed_p = c != NULL;
2328 else if (!NILP (w->vchild))
2330 c = XWINDOW (w->vchild);
2332 if (width_p)
2334 /* A vert. combination is fixed-width if one of if its
2335 children is. */
2336 while (c && !window_fixed_size_p (c, width_p, 0))
2337 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2338 fixed_p = c != NULL;
2340 else
2342 /* A vert. combination is fixed-height if all of if its
2343 children are. */
2344 while (c && window_fixed_size_p (c, width_p, 0))
2345 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2346 fixed_p = c == NULL;
2349 else if (BUFFERP (w->buffer))
2351 if (w->height_fixed_p && !width_p)
2352 fixed_p = 1;
2353 else
2355 struct buffer *old = current_buffer;
2356 Lisp_Object val;
2358 current_buffer = XBUFFER (w->buffer);
2359 val = find_symbol_value (Qwindow_size_fixed);
2360 current_buffer = old;
2362 fixed_p = 0;
2363 if (!EQ (val, Qunbound))
2365 fixed_p = !NILP (val);
2367 if (fixed_p
2368 && ((EQ (val, Qheight) && width_p)
2369 || (EQ (val, Qwidth) && !width_p)))
2370 fixed_p = 0;
2374 /* Can't tell if this one is resizable without looking at
2375 siblings. If all siblings are fixed-size this one is too. */
2376 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2378 Lisp_Object child;
2380 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2381 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2382 break;
2384 if (NILP (child))
2385 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2386 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2387 break;
2389 if (NILP (child))
2390 fixed_p = 1;
2393 else
2394 fixed_p = 1;
2396 return fixed_p;
2400 /* Return the minimum size of window W, not taking fixed-width windows
2401 into account. WIDTH_P non-zero means return the minimum width,
2402 otherwise return the minimum height. If W is a combination window,
2403 compute the minimum size from the minimum sizes of W's children. */
2405 static int
2406 window_min_size_1 (w, width_p)
2407 struct window *w;
2408 int width_p;
2410 struct window *c;
2411 int size;
2413 if (!NILP (w->hchild))
2415 c = XWINDOW (w->hchild);
2416 size = 0;
2418 if (width_p)
2420 /* The min width of a horizontal combination is
2421 the sum of the min widths of its children. */
2422 while (c)
2424 size += window_min_size_1 (c, width_p);
2425 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2428 else
2430 /* The min height a horizontal combination equals
2431 the maximum of all min height of its children. */
2432 while (c)
2434 int min_size = window_min_size_1 (c, width_p);
2435 size = max (min_size, size);
2436 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2440 else if (!NILP (w->vchild))
2442 c = XWINDOW (w->vchild);
2443 size = 0;
2445 if (width_p)
2447 /* The min width of a vertical combination is
2448 the maximum of the min widths of its children. */
2449 while (c)
2451 int min_size = window_min_size_1 (c, width_p);
2452 size = max (min_size, size);
2453 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2456 else
2458 /* The min height of a vertical combination equals
2459 the sum of the min height of its children. */
2460 while (c)
2462 size += window_min_size_1 (c, width_p);
2463 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2467 else
2469 if (width_p)
2470 size = window_min_width;
2471 else
2473 if (MINI_WINDOW_P (w)
2474 || (!WINDOW_WANTS_MODELINE_P (w)
2475 && !WINDOW_WANTS_HEADER_LINE_P (w)))
2476 size = 1;
2477 else
2478 size = window_min_height;
2482 return size;
2486 /* Return the minimum size of window W, taking fixed-size windows into
2487 account. WIDTH_P non-zero means return the minimum width,
2488 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2489 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2490 unless FIXED is null. */
2492 static int
2493 window_min_size (w, width_p, ignore_fixed_p, fixed)
2494 struct window *w;
2495 int width_p, ignore_fixed_p, *fixed;
2497 int size, fixed_p;
2499 if (ignore_fixed_p)
2500 fixed_p = 0;
2501 else
2502 fixed_p = window_fixed_size_p (w, width_p, 1);
2504 if (fixed)
2505 *fixed = fixed_p;
2507 if (fixed_p)
2508 size = width_p ? XFASTINT (w->total_cols) : XFASTINT (w->total_lines);
2509 else
2510 size = window_min_size_1 (w, width_p);
2512 return size;
2516 /* Adjust the margins of window W if text area is too small.
2517 Return 1 if window width is ok after adjustment; 0 if window
2518 is still too narrow. */
2520 static int
2521 adjust_window_margins (w)
2522 struct window *w;
2524 int box_cols = (WINDOW_TOTAL_COLS (w)
2525 - WINDOW_FRINGE_COLS (w)
2526 - WINDOW_SCROLL_BAR_COLS (w));
2527 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2528 + WINDOW_RIGHT_MARGIN_COLS (w));
2530 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2531 return 1;
2533 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2534 return 0;
2536 /* Window's text area is too narrow, but reducing the window
2537 margins will fix that. */
2538 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2539 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2541 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2542 w->left_margin_cols = w->right_margin_cols
2543 = make_number (margin_cols/2);
2544 else
2545 w->right_margin_cols = make_number (margin_cols);
2547 else
2548 w->left_margin_cols = make_number (margin_cols);
2549 return 1;
2552 /* Calculate new sizes for windows in the list FORWARD when the window size
2553 goes from TOTAL to SIZE. TOTAL must be greater than SIZE.
2554 The number of windows in FORWARD is NCHILDREN, and the number that
2555 can shrink is SHRINKABLE.
2556 The minimum size a window can have is MIN_SIZE.
2557 If we are shrinking fixed windows, RESIZE_FIXED_P is non-zero.
2558 If we are shrinking columns, WIDTH_P is non-zero, otherwise we are
2559 shrinking rows.
2561 This function returns an allocated array of new sizes that the caller
2562 must free. The size -1 means the window is fixed and RESIZE_FIXED_P
2563 is zero. Array index 0 refers to the first window in FORWARD, 1 to
2564 the second, and so on.
2566 This function tries to keep windows at least at the minimum size
2567 and resize other windows before it resizes any window to zero (i.e.
2568 delete that window).
2570 Windows are resized proportional to their size, so bigger windows
2571 shrink more than smaller windows. */
2572 static int *
2573 shrink_windows (total, size, nchildren, shrinkable,
2574 min_size, resize_fixed_p, forward, width_p)
2575 int total, size, nchildren, shrinkable, min_size;
2576 int resize_fixed_p, width_p;
2577 Lisp_Object forward;
2579 int available_resize = 0;
2580 int *new_sizes;
2581 struct window *c;
2582 Lisp_Object child;
2583 int smallest = total;
2584 int total_removed = 0;
2585 int total_shrink = total - size;
2586 int i;
2588 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2590 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2592 int child_size;
2594 c = XWINDOW (child);
2595 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2597 if (! resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2598 new_sizes[i] = -1;
2599 else
2601 new_sizes[i] = child_size;
2602 if (child_size > min_size)
2603 available_resize += child_size - min_size;
2606 /* We might need to shrink some windows to zero. Find the smallest
2607 windows and set them to 0 until we can fulfil the new size. */
2609 while (shrinkable > 1 && size + available_resize < total)
2611 for (i = 0; i < nchildren; ++i)
2612 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2613 smallest = new_sizes[i];
2615 for (i = 0; i < nchildren; ++i)
2616 if (new_sizes[i] == smallest)
2618 /* Resize this window down to zero. */
2619 new_sizes[i] = 0;
2620 if (smallest > min_size)
2621 available_resize -= smallest - min_size;
2622 available_resize += smallest;
2623 --shrinkable;
2624 total_removed += smallest;
2626 /* Out of for, just remove one window at the time and
2627 check again if we have enough space. */
2628 break;
2632 /* Now, calculate the new sizes. Try to shrink each window
2633 proportional to its size. */
2634 for (i = 0; i < nchildren; ++i)
2636 if (new_sizes[i] > min_size)
2638 int to_shrink = total_shrink*new_sizes[i]/total;
2639 if (new_sizes[i] - to_shrink < min_size)
2640 to_shrink = new_sizes[i] - min_size;
2641 new_sizes[i] -= to_shrink;
2642 total_removed += to_shrink;
2646 /* Any reminder due to rounding, we just subtract from windows
2647 that are left and still can be shrunk. */
2648 while (total_shrink > total_removed)
2650 for (i = 0; i < nchildren; ++i)
2651 if (new_sizes[i] > min_size)
2653 --new_sizes[i];
2654 ++total_removed;
2656 /* Out of for, just shrink one window at the time and
2657 check again if we have enough space. */
2658 break;
2662 return new_sizes;
2665 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2666 WINDOW's width. Resize WINDOW's children, if any, so that they
2667 keep their proportionate size relative to WINDOW. Propagate
2668 WINDOW's top or left edge position to children. Delete windows
2669 that become too small unless NODELETE_P is non-zero.
2671 If NODELETE_P is 2, that means we do delete windows that are
2672 too small, even if they were too small before! */
2674 static void
2675 size_window (window, size, width_p, nodelete_p)
2676 Lisp_Object window;
2677 int size, width_p, nodelete_p;
2679 struct window *w = XWINDOW (window);
2680 struct window *c;
2681 Lisp_Object child, *forward, *sideward;
2682 int old_size, min_size, safe_min_size;
2684 /* We test nodelete_p != 2 and nodelete_p != 1 below, so it
2685 seems like it's too soon to do this here. ++KFS. */
2686 if (nodelete_p == 2)
2687 nodelete_p = 0;
2689 check_min_window_sizes ();
2690 size = max (0, size);
2692 /* If the window has been "too small" at one point,
2693 don't delete it for being "too small" in the future.
2694 Preserve it as long as that is at all possible. */
2695 if (width_p)
2697 old_size = WINDOW_TOTAL_COLS (w);
2698 min_size = window_min_width;
2699 /* Ensure that there is room for the scroll bar and fringes!
2700 We may reduce display margins though. */
2701 safe_min_size = (MIN_SAFE_WINDOW_WIDTH
2702 + WINDOW_FRINGE_COLS (w)
2703 + WINDOW_SCROLL_BAR_COLS (w));
2705 else
2707 old_size = XINT (w->total_lines);
2708 min_size = window_min_height;
2709 safe_min_size = MIN_SAFE_WINDOW_HEIGHT;
2712 if (old_size < min_size && nodelete_p != 2)
2713 w->too_small_ok = Qt;
2715 /* Maybe delete WINDOW if it's too small. */
2716 if (nodelete_p != 1 && !NILP (w->parent))
2718 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
2719 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
2720 if (min_size < safe_min_size)
2721 min_size = safe_min_size;
2722 if (size < min_size)
2724 delete_window (window);
2725 return;
2729 /* Set redisplay hints. */
2730 w->last_modified = make_number (0);
2731 w->last_overlay_modified = make_number (0);
2732 windows_or_buffers_changed++;
2733 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
2735 if (width_p)
2737 sideward = &w->vchild;
2738 forward = &w->hchild;
2739 w->total_cols = make_number (size);
2740 adjust_window_margins (w);
2742 else
2744 sideward = &w->hchild;
2745 forward = &w->vchild;
2746 w->total_lines = make_number (size);
2747 w->orig_total_lines = Qnil;
2750 if (!NILP (*sideward))
2752 for (child = *sideward; !NILP (child); child = c->next)
2754 c = XWINDOW (child);
2755 if (width_p)
2756 c->left_col = w->left_col;
2757 else
2758 c->top_line = w->top_line;
2759 size_window (child, size, width_p, nodelete_p);
2762 else if (!NILP (*forward))
2764 int fixed_size, each, extra, n;
2765 int resize_fixed_p, nfixed;
2766 int last_pos, first_pos, nchildren, total;
2767 int *new_sizes = NULL;
2769 /* Determine the fixed-size portion of the this window, and the
2770 number of child windows. */
2771 fixed_size = nchildren = nfixed = total = 0;
2772 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
2774 int child_size;
2776 c = XWINDOW (child);
2777 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2778 total += child_size;
2780 if (window_fixed_size_p (c, width_p, 0))
2782 fixed_size += child_size;
2783 ++nfixed;
2787 /* If the new size is smaller than fixed_size, or if there
2788 aren't any resizable windows, allow resizing fixed-size
2789 windows. */
2790 resize_fixed_p = nfixed == nchildren || size < fixed_size;
2792 /* Compute how many lines/columns to add/remove to each child. The
2793 value of extra takes care of rounding errors. */
2794 n = resize_fixed_p ? nchildren : nchildren - nfixed;
2795 if (size < total && n > 1)
2796 new_sizes = shrink_windows (total, size, nchildren, n, min_size,
2797 resize_fixed_p, *forward, width_p);
2798 else
2800 each = (size - total) / n;
2801 extra = (size - total) - n * each;
2804 /* Compute new children heights and edge positions. */
2805 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
2806 last_pos = first_pos;
2807 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
2809 int new_size, old_size;
2811 c = XWINDOW (child);
2812 old_size = width_p ? XFASTINT (c->total_cols) : XFASTINT (c->total_lines);
2813 new_size = old_size;
2815 /* The top or left edge position of this child equals the
2816 bottom or right edge of its predecessor. */
2817 if (width_p)
2818 c->left_col = make_number (last_pos);
2819 else
2820 c->top_line = make_number (last_pos);
2822 /* If this child can be resized, do it. */
2823 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
2825 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
2826 extra = 0;
2829 /* Set new height. Note that size_window also propagates
2830 edge positions to children, so it's not a no-op if we
2831 didn't change the child's size. */
2832 size_window (child, new_size, width_p, 1);
2834 /* Remember the bottom/right edge position of this child; it
2835 will be used to set the top/left edge of the next child. */
2836 last_pos += new_size;
2839 if (new_sizes) xfree (new_sizes);
2841 /* We should have covered the parent exactly with child windows. */
2842 xassert (size == last_pos - first_pos);
2844 /* Now delete any children that became too small. */
2845 if (!nodelete_p)
2846 for (child = *forward; !NILP (child); child = c->next)
2848 int child_size;
2849 c = XWINDOW (child);
2850 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2851 size_window (child, child_size, width_p, 2);
2856 /* Set WINDOW's height to HEIGHT, and recursively change the height of
2857 WINDOW's children. NODELETE non-zero means don't delete windows
2858 that become too small in the process. (The caller should check
2859 later and do so if appropriate.) */
2861 void
2862 set_window_height (window, height, nodelete)
2863 Lisp_Object window;
2864 int height;
2865 int nodelete;
2867 size_window (window, height, 0, nodelete);
2871 /* Set WINDOW's width to WIDTH, and recursively change the width of
2872 WINDOW's children. NODELETE non-zero means don't delete windows
2873 that become too small in the process. (The caller should check
2874 later and do so if appropriate.) */
2876 void
2877 set_window_width (window, width, nodelete)
2878 Lisp_Object window;
2879 int width;
2880 int nodelete;
2882 size_window (window, width, 1, nodelete);
2885 /* Change window heights in windows rooted in WINDOW by N lines. */
2887 void
2888 change_window_heights (window, n)
2889 Lisp_Object window;
2890 int n;
2892 struct window *w = XWINDOW (window);
2894 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
2895 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
2897 if (INTEGERP (w->orig_top_line))
2898 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
2899 if (INTEGERP (w->orig_total_lines))
2900 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
2902 /* Handle just the top child in a vertical split. */
2903 if (!NILP (w->vchild))
2904 change_window_heights (w->vchild, n);
2906 /* Adjust all children in a horizontal split. */
2907 for (window = w->hchild; !NILP (window); window = w->next)
2909 w = XWINDOW (window);
2910 change_window_heights (window, n);
2915 int window_select_count;
2917 Lisp_Object
2918 Fset_window_buffer_unwind (obuf)
2919 Lisp_Object obuf;
2921 Fset_buffer (obuf);
2922 return Qnil;
2925 EXFUN (Fset_window_fringes, 4);
2926 EXFUN (Fset_window_scroll_bars, 4);
2928 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2929 means it's allowed to run hooks. See make_frame for a case where
2930 it's not allowed. KEEP_MARGINS_P non-zero means that the current
2931 margins, fringes, and scroll-bar settings of the window are not
2932 reset from the buffer's local settings. */
2934 void
2935 set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
2936 Lisp_Object window, buffer;
2937 int run_hooks_p, keep_margins_p;
2939 struct window *w = XWINDOW (window);
2940 struct buffer *b = XBUFFER (buffer);
2941 int count = SPECPDL_INDEX ();
2943 w->buffer = buffer;
2945 if (EQ (window, selected_window))
2946 b->last_selected_window = window;
2948 /* Update time stamps of buffer display. */
2949 if (INTEGERP (b->display_count))
2950 XSETINT (b->display_count, XINT (b->display_count) + 1);
2951 b->display_time = Fcurrent_time ();
2953 XSETFASTINT (w->window_end_pos, 0);
2954 XSETFASTINT (w->window_end_vpos, 0);
2955 bzero (&w->last_cursor, sizeof w->last_cursor);
2956 w->window_end_valid = Qnil;
2957 w->hscroll = w->min_hscroll = make_number (0);
2958 w->vscroll = 0;
2959 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
2960 set_marker_restricted (w->start,
2961 make_number (b->last_window_start),
2962 buffer);
2963 w->start_at_line_beg = Qnil;
2964 w->force_start = Qnil;
2965 XSETFASTINT (w->last_modified, 0);
2966 XSETFASTINT (w->last_overlay_modified, 0);
2967 windows_or_buffers_changed++;
2969 /* We must select BUFFER for running the window-scroll-functions.
2970 If WINDOW is selected, switch permanently.
2971 Otherwise, switch but go back to the ambient buffer afterward. */
2972 if (EQ (window, selected_window))
2973 Fset_buffer (buffer);
2974 /* We can't check ! NILP (Vwindow_scroll_functions) here
2975 because that might itself be a local variable. */
2976 else if (window_initialized)
2978 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
2979 Fset_buffer (buffer);
2982 if (!keep_margins_p)
2984 /* Set left and right marginal area width etc. from buffer. */
2986 /* This may call adjust_window_margins three times, so
2987 temporarily disable window margins. */
2988 Lisp_Object save_left = w->left_margin_cols;
2989 Lisp_Object save_right = w->right_margin_cols;
2991 w->left_margin_cols = w->right_margin_cols = Qnil;
2993 Fset_window_fringes (window,
2994 b->left_fringe_width, b->right_fringe_width,
2995 b->fringes_outside_margins);
2997 Fset_window_scroll_bars (window,
2998 b->scroll_bar_width,
2999 b->vertical_scroll_bar_type, Qnil);
3001 w->left_margin_cols = save_left;
3002 w->right_margin_cols = save_right;
3004 Fset_window_margins (window,
3005 b->left_margin_cols, b->right_margin_cols);
3008 if (run_hooks_p)
3010 if (! NILP (Vwindow_scroll_functions))
3011 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3012 Fmarker_position (w->start));
3014 if (! NILP (Vwindow_configuration_change_hook)
3015 && ! NILP (Vrun_hooks))
3016 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3019 unbind_to (count, Qnil);
3023 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3024 doc: /* Make WINDOW display BUFFER as its contents.
3025 BUFFER can be a buffer or buffer name.
3026 Optional third arg KEEP-MARGINS non-nil means that WINDOW's current
3027 display margins, fringe widths, and scroll bar settings are maintained;
3028 the default is to reset these from BUFFER's local settings or the frame
3029 defaults. */)
3030 (window, buffer, keep_margins)
3031 register Lisp_Object window, buffer, keep_margins;
3033 register Lisp_Object tem;
3034 register struct window *w = decode_window (window);
3036 XSETWINDOW (window, w);
3037 buffer = Fget_buffer (buffer);
3038 CHECK_BUFFER (buffer);
3040 if (NILP (XBUFFER (buffer)->name))
3041 error ("Attempt to display deleted buffer");
3043 tem = w->buffer;
3044 if (NILP (tem))
3045 error ("Window is deleted");
3046 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
3047 is first being set up. */
3049 if (!NILP (w->dedicated) && !EQ (tem, buffer))
3050 error ("Window is dedicated to `%s'",
3051 SDATA (XBUFFER (tem)->name));
3053 unshow_buffer (w);
3056 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3057 return Qnil;
3060 /* Note that selected_window can be nil
3061 when this is called from Fset_window_configuration. */
3063 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3064 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3065 If WINDOW is not already selected, make WINDOW's buffer current
3066 and make WINDOW the frame's selected window.
3067 Optional second arg NORECORD non-nil means
3068 do not put this buffer at the front of the list of recently selected ones.
3070 Note that the main editor command loop
3071 selects the buffer of the selected window before each command. */)
3072 (window, norecord)
3073 register Lisp_Object window, norecord;
3075 register struct window *w;
3076 register struct window *ow;
3077 struct frame *sf;
3079 CHECK_LIVE_WINDOW (window);
3081 w = XWINDOW (window);
3082 w->frozen_window_start_p = 0;
3084 XSETFASTINT (w->use_time, ++window_select_count);
3085 if (EQ (window, selected_window))
3086 return window;
3088 if (!NILP (selected_window))
3090 ow = XWINDOW (selected_window);
3091 if (! NILP (ow->buffer))
3092 set_marker_both (ow->pointm, ow->buffer,
3093 BUF_PT (XBUFFER (ow->buffer)),
3094 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3097 selected_window = window;
3098 sf = SELECTED_FRAME ();
3099 if (XFRAME (WINDOW_FRAME (w)) != sf)
3101 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3102 /* Use this rather than Fhandle_switch_frame
3103 so that FRAME_FOCUS_FRAME is moved appropriately as we
3104 move around in the state where a minibuffer in a separate
3105 frame is active. */
3106 Fselect_frame (WINDOW_FRAME (w), Qnil);
3108 else
3109 sf->selected_window = window;
3111 if (NILP (norecord))
3112 record_buffer (w->buffer);
3113 Fset_buffer (w->buffer);
3115 XBUFFER (w->buffer)->last_selected_window = window;
3117 /* Go to the point recorded in the window.
3118 This is important when the buffer is in more
3119 than one window. It also matters when
3120 redisplay_window has altered point after scrolling,
3121 because it makes the change only in the window. */
3123 register int new_point = marker_position (w->pointm);
3124 if (new_point < BEGV)
3125 SET_PT (BEGV);
3126 else if (new_point > ZV)
3127 SET_PT (ZV);
3128 else
3129 SET_PT (new_point);
3132 windows_or_buffers_changed++;
3133 return window;
3136 static Lisp_Object
3137 select_window_norecord (window)
3138 Lisp_Object window;
3140 return Fselect_window (window, Qt);
3143 /* Deiconify the frame containing the window WINDOW,
3144 unless it is the selected frame;
3145 then return WINDOW.
3147 The reason for the exception for the selected frame
3148 is that it seems better not to change the selected frames visibility
3149 merely because of displaying a different buffer in it.
3150 The deiconification is useful when a buffer gets shown in
3151 another frame that you were not using lately. */
3153 static Lisp_Object
3154 display_buffer_1 (window)
3155 Lisp_Object window;
3157 Lisp_Object frame = XWINDOW (window)->frame;
3158 FRAME_PTR f = XFRAME (frame);
3160 FRAME_SAMPLE_VISIBILITY (f);
3162 if (EQ (frame, selected_frame))
3163 ; /* Assume the selected frame is already visible enough. */
3164 else if (minibuf_level > 0
3165 && MINI_WINDOW_P (XWINDOW (selected_window))
3166 && WINDOW_LIVE_P (minibuf_selected_window)
3167 && EQ (frame, WINDOW_FRAME (XWINDOW (minibuf_selected_window))))
3168 ; /* Assume the frame from which we invoked the minibuffer is visible. */
3169 else
3171 if (FRAME_ICONIFIED_P (f))
3172 Fmake_frame_visible (frame);
3173 else if (FRAME_VISIBLE_P (f))
3174 Fraise_frame (frame);
3177 return window;
3180 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
3181 doc: /* Returns non-nil if a buffer named BUFFER-NAME would be created specially.
3182 The value is actually t if the frame should be called with default frame
3183 parameters, and a list of frame parameters if they were specified.
3184 See `special-display-buffer-names', and `special-display-regexps'. */)
3185 (buffer_name)
3186 Lisp_Object buffer_name;
3188 Lisp_Object tem;
3190 CHECK_STRING (buffer_name);
3192 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
3193 if (!NILP (tem))
3194 return Qt;
3196 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
3197 if (!NILP (tem))
3198 return XCDR (tem);
3200 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
3202 Lisp_Object car = XCAR (tem);
3203 if (STRINGP (car)
3204 && fast_string_match (car, buffer_name) >= 0)
3205 return Qt;
3206 else if (CONSP (car)
3207 && STRINGP (XCAR (car))
3208 && fast_string_match (XCAR (car), buffer_name) >= 0)
3209 return XCDR (car);
3211 return Qnil;
3214 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
3215 doc: /* Returns non-nil if a new buffer named BUFFER-NAME would use the same window.
3216 See `same-window-buffer-names' and `same-window-regexps'. */)
3217 (buffer_name)
3218 Lisp_Object buffer_name;
3220 Lisp_Object tem;
3222 CHECK_STRING (buffer_name);
3224 tem = Fmember (buffer_name, Vsame_window_buffer_names);
3225 if (!NILP (tem))
3226 return Qt;
3228 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
3229 if (!NILP (tem))
3230 return Qt;
3232 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
3234 Lisp_Object car = XCAR (tem);
3235 if (STRINGP (car)
3236 && fast_string_match (car, buffer_name) >= 0)
3237 return Qt;
3238 else if (CONSP (car)
3239 && STRINGP (XCAR (car))
3240 && fast_string_match (XCAR (car), buffer_name) >= 0)
3241 return Qt;
3243 return Qnil;
3246 /* Use B so the default is (other-buffer). */
3247 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
3248 "BDisplay buffer: \nP",
3249 doc: /* Make BUFFER appear in some window but don't select it.
3250 BUFFER can be a buffer or a buffer name.
3251 If BUFFER is shown already in some window, just use that one,
3252 unless the window is the selected window and the optional second
3253 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).
3254 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
3255 Returns the window displaying BUFFER.
3256 If `display-buffer-reuse-frames' is non-nil, and another frame is currently
3257 displaying BUFFER, then simply raise that frame.
3259 The variables `special-display-buffer-names',
3260 `special-display-regexps', `same-window-buffer-names', and
3261 `same-window-regexps' customize how certain buffer names are handled.
3262 The latter two take effect only if NOT-THIS-WINDOW is t.
3264 If optional argument FRAME is `visible', search all visible frames.
3265 If FRAME is 0, search all visible and iconified frames.
3266 If FRAME is t, search all frames.
3267 If FRAME is a frame, search only that frame.
3268 If FRAME is nil, search only the selected frame
3269 (actually the last nonminibuffer frame),
3270 unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,
3271 which means search visible and iconified frames.
3273 If a full-width window on a splittable frame is available to display
3274 the buffer, it may be split, subject to the value of the variable
3275 `split-height-threshold'.
3277 If `even-window-heights' is non-nil, window heights will be evened out
3278 if displaying the buffer causes two vertically adjacent windows to be
3279 displayed. */)
3280 (buffer, not_this_window, frame)
3281 register Lisp_Object buffer, not_this_window, frame;
3283 register Lisp_Object window, tem, swp;
3284 struct frame *f;
3286 swp = Qnil;
3287 buffer = Fget_buffer (buffer);
3288 CHECK_BUFFER (buffer);
3290 if (!NILP (Vdisplay_buffer_function))
3291 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
3293 if (NILP (not_this_window)
3294 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
3295 return display_buffer_1 (selected_window);
3297 /* See if the user has specified this buffer should appear
3298 in the selected window. */
3299 if (NILP (not_this_window))
3301 swp = Fsame_window_p (XBUFFER (buffer)->name);
3302 if (!NILP (swp) && !no_switch_window (selected_window))
3304 Fswitch_to_buffer (buffer, Qnil);
3305 return display_buffer_1 (selected_window);
3309 /* If the user wants pop-up-frames or display-buffer-reuse-frames,
3310 look for a window showing BUFFER on any visible or iconified frame.
3311 Otherwise search only the current frame. */
3312 if (! NILP (frame))
3313 tem = frame;
3314 else if (pop_up_frames
3315 || display_buffer_reuse_frames
3316 || last_nonminibuf_frame == 0)
3317 XSETFASTINT (tem, 0);
3318 else
3319 XSETFRAME (tem, last_nonminibuf_frame);
3321 window = Fget_buffer_window (buffer, tem);
3322 if (!NILP (window)
3323 && (NILP (not_this_window) || !EQ (window, selected_window)))
3324 return display_buffer_1 (window);
3326 /* Certain buffer names get special handling. */
3327 if (!NILP (Vspecial_display_function) && NILP (swp))
3329 tem = Fspecial_display_p (XBUFFER (buffer)->name);
3330 if (EQ (tem, Qt))
3331 return call1 (Vspecial_display_function, buffer);
3332 if (CONSP (tem))
3333 return call2 (Vspecial_display_function, buffer, tem);
3336 /* If there are no frames open that have more than a minibuffer,
3337 we need to create a new frame. */
3338 if (pop_up_frames || last_nonminibuf_frame == 0)
3340 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3341 Fset_window_buffer (window, buffer, Qnil);
3342 return display_buffer_1 (window);
3345 f = SELECTED_FRAME ();
3346 if (pop_up_windows
3347 || FRAME_MINIBUF_ONLY_P (f)
3348 /* If the current frame is a special display frame,
3349 don't try to reuse its windows. */
3350 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
3352 Lisp_Object frames;
3354 frames = Qnil;
3355 if (FRAME_MINIBUF_ONLY_P (f))
3356 XSETFRAME (frames, last_nonminibuf_frame);
3357 /* Don't try to create a window if we would get an error. */
3358 if (split_height_threshold < window_min_height << 1)
3359 split_height_threshold = window_min_height << 1;
3361 /* Note that both Fget_largest_window and Fget_lru_window
3362 ignore minibuffers and dedicated windows.
3363 This means they can return nil. */
3365 /* If the frame we would try to split cannot be split,
3366 try other frames. */
3367 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
3369 /* Try visible frames first. */
3370 window = Fget_largest_window (Qvisible);
3371 /* If that didn't work, try iconified frames. */
3372 if (NILP (window))
3373 window = Fget_largest_window (make_number (0));
3374 if (NILP (window))
3375 window = Fget_largest_window (Qt);
3377 else
3378 window = Fget_largest_window (frames);
3380 /* If we got a tall enough full-width window that can be split,
3381 split it. */
3382 if (!NILP (window)
3383 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3384 && window_height (window) >= split_height_threshold
3385 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
3386 window = Fsplit_window (window, Qnil, Qnil);
3387 else
3389 Lisp_Object upper, lower, other;
3391 window = Fget_lru_window (frames);
3392 /* If the LRU window is selected, and big enough,
3393 and can be split, split it. */
3394 if (!NILP (window)
3395 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3396 && (EQ (window, selected_window)
3397 || EQ (XWINDOW (window)->parent, Qnil))
3398 && window_height (window) >= window_min_height << 1)
3399 window = Fsplit_window (window, Qnil, Qnil);
3400 /* If Fget_lru_window returned nil, try other approaches. */
3402 /* Try visible frames first. */
3403 if (NILP (window))
3404 window = Fget_buffer_window (buffer, Qvisible);
3405 if (NILP (window))
3406 window = Fget_largest_window (Qvisible);
3407 /* If that didn't work, try iconified frames. */
3408 if (NILP (window))
3409 window = Fget_buffer_window (buffer, make_number (0));
3410 if (NILP (window))
3411 window = Fget_largest_window (make_number (0));
3412 /* Try invisible frames. */
3413 if (NILP (window))
3414 window = Fget_buffer_window (buffer, Qt);
3415 if (NILP (window))
3416 window = Fget_largest_window (Qt);
3417 /* As a last resort, make a new frame. */
3418 if (NILP (window))
3419 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3420 /* If window appears above or below another,
3421 even out their heights. */
3422 other = upper = lower = Qnil;
3423 if (!NILP (XWINDOW (window)->prev))
3424 other = upper = XWINDOW (window)->prev, lower = window;
3425 if (!NILP (XWINDOW (window)->next))
3426 other = lower = XWINDOW (window)->next, upper = window;
3427 if (!NILP (other)
3428 && !NILP (Veven_window_heights)
3429 /* Check that OTHER and WINDOW are vertically arrayed. */
3430 && !EQ (XWINDOW (other)->top_line, XWINDOW (window)->top_line)
3431 && (XFASTINT (XWINDOW (other)->total_lines)
3432 > XFASTINT (XWINDOW (window)->total_lines)))
3434 int total = (XFASTINT (XWINDOW (other)->total_lines)
3435 + XFASTINT (XWINDOW (window)->total_lines));
3436 enlarge_window (upper,
3437 total / 2 - XFASTINT (XWINDOW (upper)->total_lines),
3438 0, 0);
3442 else
3443 window = Fget_lru_window (Qnil);
3445 Fset_window_buffer (window, buffer, Qnil);
3446 return display_buffer_1 (window);
3450 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3451 0, 1, 0,
3452 doc: /* Force redisplay of all windows.
3453 If optional arg OBJECT is a window, force redisplay of that window only.
3454 If OBJECT is a buffer or buffer name, force redisplay of all windows
3455 displaying that buffer. */)
3456 (object)
3457 Lisp_Object object;
3459 if (NILP (object))
3461 windows_or_buffers_changed++;
3462 update_mode_lines++;
3463 return Qt;
3466 if (WINDOWP (object))
3468 struct window *w = XWINDOW (object);
3469 mark_window_display_accurate (object, 0);
3470 w->update_mode_line = Qt;
3471 if (BUFFERP (w->buffer))
3472 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3473 ++update_mode_lines;
3474 return Qt;
3477 if (STRINGP (object))
3478 object = Fget_buffer (object);
3479 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3481 /* Walk all windows looking for buffer, and force update
3482 of each of those windows. */
3484 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3485 return NILP (object) ? Qnil : Qt;
3488 /* If nothing suitable was found, just return.
3489 We could signal an error, but this feature will typically be used
3490 asynchronously in timers or process sentinels, so we don't. */
3491 return Qnil;
3495 void
3496 temp_output_buffer_show (buf)
3497 register Lisp_Object buf;
3499 register struct buffer *old = current_buffer;
3500 register Lisp_Object window;
3501 register struct window *w;
3503 XBUFFER (buf)->directory = current_buffer->directory;
3505 Fset_buffer (buf);
3506 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3507 BEGV = BEG;
3508 ZV = Z;
3509 SET_PT (BEG);
3510 #if 0 /* rms: there should be no reason for this. */
3511 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3512 #endif
3513 set_buffer_internal (old);
3515 if (!EQ (Vtemp_buffer_show_function, Qnil))
3516 call1 (Vtemp_buffer_show_function, buf);
3517 else
3519 window = Fdisplay_buffer (buf, Qnil, Qnil);
3521 if (!EQ (XWINDOW (window)->frame, selected_frame))
3522 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3523 Vminibuf_scroll_window = window;
3524 w = XWINDOW (window);
3525 XSETFASTINT (w->hscroll, 0);
3526 XSETFASTINT (w->min_hscroll, 0);
3527 set_marker_restricted_both (w->start, buf, BEG, BEG);
3528 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3530 /* Run temp-buffer-show-hook, with the chosen window selected
3531 and its buffer current. */
3533 if (!NILP (Vrun_hooks)
3534 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3535 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3537 int count = SPECPDL_INDEX ();
3538 Lisp_Object prev_window, prev_buffer;
3539 prev_window = selected_window;
3540 XSETBUFFER (prev_buffer, old);
3542 /* Select the window that was chosen, for running the hook.
3543 Note: Both Fselect_window and select_window_norecord may
3544 set-buffer to the buffer displayed in the window,
3545 so we need to save the current buffer. --stef */
3546 record_unwind_protect (Fset_buffer, prev_buffer);
3547 record_unwind_protect (select_window_norecord, prev_window);
3548 Fselect_window (window, Qt);
3549 Fset_buffer (w->buffer);
3550 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3551 unbind_to (count, Qnil);
3556 static void
3557 make_dummy_parent (window)
3558 Lisp_Object window;
3560 Lisp_Object new;
3561 register struct window *o, *p;
3562 int i;
3564 o = XWINDOW (window);
3565 p = allocate_window ();
3566 for (i = 0; i < VECSIZE (struct window); ++i)
3567 ((struct Lisp_Vector *) p)->contents[i]
3568 = ((struct Lisp_Vector *)o)->contents[i];
3569 XSETWINDOW (new, p);
3571 XSETFASTINT (p->sequence_number, ++sequence_number);
3573 /* Put new into window structure in place of window */
3574 replace_window (window, new);
3576 o->next = Qnil;
3577 o->prev = Qnil;
3578 o->vchild = Qnil;
3579 o->hchild = Qnil;
3580 o->parent = new;
3582 p->start = Qnil;
3583 p->pointm = Qnil;
3584 p->buffer = Qnil;
3587 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3588 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3589 WINDOW defaults to selected one and SIZE to half its size.
3590 If optional third arg HORFLAG is non-nil, split side by side
3591 and put SIZE columns in the first of the pair. In that case,
3592 SIZE includes that window's scroll bar, or the divider column to its right.
3593 Returns the newly-created window. */)
3594 (window, size, horflag)
3595 Lisp_Object window, size, horflag;
3597 register Lisp_Object new;
3598 register struct window *o, *p;
3599 FRAME_PTR fo;
3600 register int size_int;
3602 if (NILP (window))
3603 window = selected_window;
3604 else
3605 CHECK_LIVE_WINDOW (window);
3607 o = XWINDOW (window);
3608 fo = XFRAME (WINDOW_FRAME (o));
3610 if (NILP (size))
3612 if (!NILP (horflag))
3613 /* Calculate the size of the left-hand window, by dividing
3614 the usable space in columns by two.
3615 We round up, since the left-hand window may include
3616 a dividing line, while the right-hand may not. */
3617 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3618 else
3619 size_int = XFASTINT (o->total_lines) >> 1;
3621 else
3623 CHECK_NUMBER (size);
3624 size_int = XINT (size);
3627 if (MINI_WINDOW_P (o))
3628 error ("Attempt to split minibuffer window");
3629 else if (window_fixed_size_p (o, !NILP (horflag), 0))
3630 error ("Attempt to split fixed-size window");
3632 check_min_window_sizes ();
3634 if (NILP (horflag))
3636 if (size_int < window_min_height)
3637 error ("Window height %d too small (after splitting)", size_int);
3638 if (size_int + window_min_height > XFASTINT (o->total_lines))
3639 error ("Window height %d too small (after splitting)",
3640 XFASTINT (o->total_lines) - size_int);
3641 if (NILP (o->parent)
3642 || NILP (XWINDOW (o->parent)->vchild))
3644 make_dummy_parent (window);
3645 new = o->parent;
3646 XWINDOW (new)->vchild = window;
3649 else
3651 if (size_int < window_min_width)
3652 error ("Window width %d too small (after splitting)", size_int);
3654 if (size_int + window_min_width > XFASTINT (o->total_cols))
3655 error ("Window width %d too small (after splitting)",
3656 XFASTINT (o->total_cols) - size_int);
3657 if (NILP (o->parent)
3658 || NILP (XWINDOW (o->parent)->hchild))
3660 make_dummy_parent (window);
3661 new = o->parent;
3662 XWINDOW (new)->hchild = window;
3666 /* Now we know that window's parent is a vertical combination
3667 if we are dividing vertically, or a horizontal combination
3668 if we are making side-by-side windows */
3670 windows_or_buffers_changed++;
3671 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3672 new = make_window ();
3673 p = XWINDOW (new);
3675 p->frame = o->frame;
3676 p->next = o->next;
3677 if (!NILP (p->next))
3678 XWINDOW (p->next)->prev = new;
3679 p->prev = window;
3680 o->next = new;
3681 p->parent = o->parent;
3682 p->buffer = Qt;
3683 p->window_end_valid = Qnil;
3684 bzero (&p->last_cursor, sizeof p->last_cursor);
3686 /* Duplicate special geometry settings. */
3688 p->left_margin_cols = o->left_margin_cols;
3689 p->right_margin_cols = o->right_margin_cols;
3690 p->left_fringe_width = o->left_fringe_width;
3691 p->right_fringe_width = o->right_fringe_width;
3692 p->fringes_outside_margins = o->fringes_outside_margins;
3693 p->scroll_bar_width = o->scroll_bar_width;
3694 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3696 /* Apportion the available frame space among the two new windows */
3698 if (!NILP (horflag))
3700 p->total_lines = o->total_lines;
3701 p->top_line = o->top_line;
3702 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3703 XSETFASTINT (o->total_cols, size_int);
3704 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3705 adjust_window_margins (p);
3706 adjust_window_margins (o);
3708 else
3710 p->left_col = o->left_col;
3711 p->total_cols = o->total_cols;
3712 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3713 XSETFASTINT (o->total_lines, size_int);
3714 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3717 /* Adjust glyph matrices. */
3718 adjust_glyphs (fo);
3720 Fset_window_buffer (new, o->buffer, Qt);
3721 return new;
3724 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 3, "p",
3725 doc: /* Make current window ARG lines bigger.
3726 From program, optional second arg non-nil means grow sideways ARG columns.
3727 Interactively, if an argument is not given, make the window one line bigger.
3729 Optional third arg PRESERVE-BEFORE, if non-nil, means do not change the size
3730 of the siblings above or to the left of the selected window. Only
3731 siblings to the right or below are changed. */)
3732 (arg, side, preserve_before)
3733 register Lisp_Object arg, side, preserve_before;
3735 CHECK_NUMBER (arg);
3736 enlarge_window (selected_window, XINT (arg), !NILP (side),
3737 !NILP (preserve_before));
3739 if (! NILP (Vwindow_configuration_change_hook))
3740 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3742 return Qnil;
3745 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 3, "p",
3746 doc: /* Make current window ARG lines smaller.
3747 From program, optional second arg non-nil means shrink sideways arg columns.
3748 Interactively, if an argument is not given, make the window one line smaller.
3750 Optional third arg PRESERVE-BEFORE, if non-nil, means do not change the size
3751 of the siblings above or to the left of the selected window. Only
3752 siblings to the right or below are changed. */)
3753 (arg, side, preserve_before)
3754 register Lisp_Object arg, side, preserve_before;
3756 CHECK_NUMBER (arg);
3757 enlarge_window (selected_window, -XINT (arg), !NILP (side),
3758 !NILP (preserve_before));
3760 if (! NILP (Vwindow_configuration_change_hook))
3761 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3763 return Qnil;
3767 window_height (window)
3768 Lisp_Object window;
3770 register struct window *p = XWINDOW (window);
3771 return WINDOW_TOTAL_LINES (p);
3775 window_width (window)
3776 Lisp_Object window;
3778 register struct window *p = XWINDOW (window);
3779 return WINDOW_TOTAL_COLS (p);
3783 #define CURBEG(w) \
3784 *(widthflag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3786 #define CURSIZE(w) \
3787 *(widthflag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3790 /* Enlarge WINDOW by DELTA. WIDTHFLAG non-zero means
3791 increase its width. Siblings of the selected window are resized to
3792 fulfill the size request. If they become too small in the process,
3793 they will be deleted.
3795 If PRESERVE_BEFORE is nonzero, that means don't alter
3796 the siblings to the left or above WINDOW. */
3798 static void
3799 enlarge_window (window, delta, widthflag, preserve_before)
3800 Lisp_Object window;
3801 int delta, widthflag, preserve_before;
3803 Lisp_Object parent, next, prev;
3804 struct window *p;
3805 Lisp_Object *sizep;
3806 int maximum;
3807 int (*sizefun) P_ ((Lisp_Object))
3808 = widthflag ? window_width : window_height;
3809 void (*setsizefun) P_ ((Lisp_Object, int, int))
3810 = (widthflag ? set_window_width : set_window_height);
3812 /* Check values of window_min_width and window_min_height for
3813 validity. */
3814 check_min_window_sizes ();
3816 /* Give up if this window cannot be resized. */
3817 if (window_fixed_size_p (XWINDOW (window), widthflag, 1))
3818 error ("Window is not resizable");
3820 /* Find the parent of the selected window. */
3821 while (1)
3823 p = XWINDOW (window);
3824 parent = p->parent;
3826 if (NILP (parent))
3828 if (widthflag)
3829 error ("No other window to side of this one");
3830 break;
3833 if (widthflag
3834 ? !NILP (XWINDOW (parent)->hchild)
3835 : !NILP (XWINDOW (parent)->vchild))
3836 break;
3838 window = parent;
3841 sizep = &CURSIZE (window);
3844 register int maxdelta;
3846 /* Compute the maximum size increment this window can have. */
3848 if (preserve_before)
3850 if (!NILP (parent))
3852 maxdelta = (*sizefun) (parent) - XINT (*sizep);
3853 /* Subtract size of siblings before, since we can't take that. */
3854 maxdelta -= XINT (CURBEG (window)) - XINT (CURBEG (parent));
3856 else
3857 maxdelta = (!NILP (p->next) ? ((*sizefun) (p->next)
3858 - window_min_size (XWINDOW (p->next),
3859 widthflag, 0, 0))
3860 : (delta = 0));
3862 else
3863 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
3864 /* This is a main window followed by a minibuffer. */
3865 : !NILP (p->next) ? ((*sizefun) (p->next)
3866 - window_min_size (XWINDOW (p->next),
3867 widthflag, 0, 0))
3868 /* This is a minibuffer following a main window. */
3869 : !NILP (p->prev) ? ((*sizefun) (p->prev)
3870 - window_min_size (XWINDOW (p->prev),
3871 widthflag, 0, 0))
3872 /* This is a frame with only one window, a minibuffer-only
3873 or a minibufferless frame. */
3874 : (delta = 0));
3876 if (delta > maxdelta)
3877 /* This case traps trying to make the minibuffer
3878 the full frame, or make the only window aside from the
3879 minibuffer the full frame. */
3880 delta = maxdelta;
3883 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window), widthflag, 0, 0))
3885 delete_window (window);
3886 return;
3889 if (delta == 0)
3890 return;
3892 /* Find the total we can get from other siblings without deleting them. */
3893 maximum = 0;
3894 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
3895 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
3896 widthflag, 0, 0);
3897 if (! preserve_before)
3898 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
3899 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
3900 widthflag, 0, 0);
3902 /* If we can get it all from them without deleting them, do so. */
3903 if (delta <= maximum)
3905 Lisp_Object first_unaffected;
3906 Lisp_Object first_affected;
3907 int fixed_p;
3909 next = p->next;
3910 prev = p->prev;
3911 first_affected = window;
3912 /* Look at one sibling at a time,
3913 moving away from this window in both directions alternately,
3914 and take as much as we can get without deleting that sibling. */
3915 while (delta != 0
3916 && (!NILP (next) || (!preserve_before && !NILP (prev))))
3918 if (! NILP (next))
3920 int this_one = ((*sizefun) (next)
3921 - window_min_size (XWINDOW (next),
3922 widthflag, 0, &fixed_p));
3923 if (!fixed_p)
3925 if (this_one > delta)
3926 this_one = delta;
3928 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
3929 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
3931 delta -= this_one;
3934 next = XWINDOW (next)->next;
3937 if (delta == 0)
3938 break;
3940 if (!preserve_before && ! NILP (prev))
3942 int this_one = ((*sizefun) (prev)
3943 - window_min_size (XWINDOW (prev),
3944 widthflag, 0, &fixed_p));
3945 if (!fixed_p)
3947 if (this_one > delta)
3948 this_one = delta;
3950 first_affected = prev;
3952 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
3953 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
3955 delta -= this_one;
3958 prev = XWINDOW (prev)->prev;
3962 xassert (delta == 0);
3964 /* Now recalculate the edge positions of all the windows affected,
3965 based on the new sizes. */
3966 first_unaffected = next;
3967 prev = first_affected;
3968 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
3969 prev = next, next = XWINDOW (next)->next)
3971 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
3972 /* This does not change size of NEXT,
3973 but it propagates the new top edge to its children */
3974 (*setsizefun) (next, (*sizefun) (next), 0);
3977 else
3979 register int delta1;
3980 register int opht = (*sizefun) (parent);
3982 if (opht <= XINT (*sizep) + delta)
3984 /* If trying to grow this window to or beyond size of the parent,
3985 just delete all the sibling windows. */
3986 Lisp_Object start, tem, next;
3988 start = XWINDOW (parent)->vchild;
3989 if (NILP (start))
3990 start = XWINDOW (parent)->hchild;
3992 /* Delete any siblings that come after WINDOW. */
3993 tem = XWINDOW (window)->next;
3994 while (! NILP (tem))
3996 next = XWINDOW (tem)->next;
3997 delete_window (tem);
3998 tem = next;
4001 /* Delete any siblings that come after WINDOW.
4002 Note that if START is not WINDOW, then WINDOW still
4003 Fhas siblings, so WINDOW has not yet replaced its parent. */
4004 tem = start;
4005 while (! EQ (tem, window))
4007 next = XWINDOW (tem)->next;
4008 delete_window (tem);
4009 tem = next;
4012 else
4014 /* Otherwise, make delta1 just right so that if we add
4015 delta1 lines to this window and to the parent, and then
4016 shrink the parent back to its original size, the new
4017 proportional size of this window will increase by delta.
4019 The function size_window will compute the new height h'
4020 of the window from delta1 as:
4022 e = delta1/n
4023 x = delta1 - delta1/n * n for the 1st resizable child
4024 h' = h + e + x
4026 where n is the number of children that can be resized.
4027 We can ignore x by choosing a delta1 that is a multiple of
4028 n. We want the height of this window to come out as
4030 h' = h + delta
4032 So, delta1 must be
4034 h + e = h + delta
4035 delta1/n = delta
4036 delta1 = n * delta.
4038 The number of children n equals the number of resizable
4039 children of this window + 1 because we know window itself
4040 is resizable (otherwise we would have signalled an error. */
4042 struct window *w = XWINDOW (window);
4043 Lisp_Object s;
4044 int n = 1;
4046 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
4047 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
4048 ++n;
4049 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
4050 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
4051 ++n;
4053 delta1 = n * delta;
4055 /* Add delta1 lines or columns to this window, and to the parent,
4056 keeping things consistent while not affecting siblings. */
4057 XSETINT (CURSIZE (parent), opht + delta1);
4058 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4060 /* Squeeze out delta1 lines or columns from our parent,
4061 shriking this window and siblings proportionately.
4062 This brings parent back to correct size.
4063 Delta1 was calculated so this makes this window the desired size,
4064 taking it all out of the siblings. */
4065 (*setsizefun) (parent, opht, 0);
4070 XSETFASTINT (p->last_modified, 0);
4071 XSETFASTINT (p->last_overlay_modified, 0);
4073 /* Adjust glyph matrices. */
4074 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4077 #undef CURBEG
4078 #undef CURSIZE
4082 /***********************************************************************
4083 Resizing Mini-Windows
4084 ***********************************************************************/
4086 static void shrink_window_lowest_first P_ ((struct window *, int));
4088 enum save_restore_action
4090 CHECK_ORIG_SIZES,
4091 SAVE_ORIG_SIZES,
4092 RESTORE_ORIG_SIZES
4095 static int save_restore_orig_size P_ ((struct window *,
4096 enum save_restore_action));
4098 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4099 from lowest windows first. */
4101 static void
4102 shrink_window_lowest_first (w, height)
4103 struct window *w;
4104 int height;
4106 struct window *c;
4107 Lisp_Object child;
4108 int old_height;
4110 xassert (!MINI_WINDOW_P (w));
4112 /* Set redisplay hints. */
4113 XSETFASTINT (w->last_modified, 0);
4114 XSETFASTINT (w->last_overlay_modified, 0);
4115 windows_or_buffers_changed++;
4116 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4118 old_height = XFASTINT (w->total_lines);
4119 XSETFASTINT (w->total_lines, height);
4121 if (!NILP (w->hchild))
4123 for (child = w->hchild; !NILP (child); child = c->next)
4125 c = XWINDOW (child);
4126 c->top_line = w->top_line;
4127 shrink_window_lowest_first (c, height);
4130 else if (!NILP (w->vchild))
4132 Lisp_Object last_child;
4133 int delta = old_height - height;
4134 int last_top;
4136 last_child = Qnil;
4138 /* Find the last child. We are taking space from lowest windows
4139 first, so we iterate over children from the last child
4140 backwards. */
4141 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
4142 last_child = child;
4144 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
4145 for (child = last_child; delta && !NILP (child); child = c->prev)
4147 int this_one;
4149 c = XWINDOW (child);
4150 this_one = XFASTINT (c->total_lines) - MIN_SAFE_WINDOW_HEIGHT;
4152 if (this_one > delta)
4153 this_one = delta;
4155 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4156 delta -= this_one;
4159 /* Compute new positions. */
4160 last_top = XINT (w->top_line);
4161 for (child = w->vchild; !NILP (child); child = c->next)
4163 c = XWINDOW (child);
4164 c->top_line = make_number (last_top);
4165 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4166 last_top += XFASTINT (c->total_lines);
4172 /* Save, restore, or check positions and sizes in the window tree
4173 rooted at W. ACTION says what to do.
4175 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4176 orig_total_lines members are valid for all windows in the window
4177 tree. Value is non-zero if they are valid.
4179 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4180 orig_top_line and orig_total_lines for all windows in the tree.
4182 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4183 stored in orig_top_line and orig_total_lines for all windows. */
4185 static int
4186 save_restore_orig_size (w, action)
4187 struct window *w;
4188 enum save_restore_action action;
4190 int success_p = 1;
4192 while (w)
4194 if (!NILP (w->hchild))
4196 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4197 success_p = 0;
4199 else if (!NILP (w->vchild))
4201 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4202 success_p = 0;
4205 switch (action)
4207 case CHECK_ORIG_SIZES:
4208 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4209 return 0;
4210 break;
4212 case SAVE_ORIG_SIZES:
4213 w->orig_top_line = w->top_line;
4214 w->orig_total_lines = w->total_lines;
4215 XSETFASTINT (w->last_modified, 0);
4216 XSETFASTINT (w->last_overlay_modified, 0);
4217 break;
4219 case RESTORE_ORIG_SIZES:
4220 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4221 w->top_line = w->orig_top_line;
4222 w->total_lines = w->orig_total_lines;
4223 w->orig_total_lines = w->orig_top_line = Qnil;
4224 XSETFASTINT (w->last_modified, 0);
4225 XSETFASTINT (w->last_overlay_modified, 0);
4226 break;
4228 default:
4229 abort ();
4232 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4235 return success_p;
4239 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4240 without deleting other windows. */
4242 void
4243 grow_mini_window (w, delta)
4244 struct window *w;
4245 int delta;
4247 struct frame *f = XFRAME (w->frame);
4248 struct window *root;
4250 xassert (MINI_WINDOW_P (w));
4251 xassert (delta >= 0);
4253 /* Check values of window_min_width and window_min_height for
4254 validity. */
4255 check_min_window_sizes ();
4257 /* Compute how much we can enlarge the mini-window without deleting
4258 other windows. */
4259 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4260 if (delta)
4262 int min_height = window_min_size (root, 0, 0, 0);
4263 if (XFASTINT (root->total_lines) - delta < min_height)
4264 /* Note that the root window may already be smaller than
4265 min_height. */
4266 delta = max (0, XFASTINT (root->total_lines) - min_height);
4269 if (delta)
4271 /* Save original window sizes and positions, if not already done. */
4272 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4273 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4275 /* Shrink other windows. */
4276 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4278 /* Grow the mini-window. */
4279 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4280 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4281 XSETFASTINT (w->last_modified, 0);
4282 XSETFASTINT (w->last_overlay_modified, 0);
4284 adjust_glyphs (f);
4289 /* Shrink mini-window W. If there is recorded info about window sizes
4290 before a call to grow_mini_window, restore recorded window sizes.
4291 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4292 line. */
4294 void
4295 shrink_mini_window (w)
4296 struct window *w;
4298 struct frame *f = XFRAME (w->frame);
4299 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4301 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4303 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4304 adjust_glyphs (f);
4305 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4306 windows_or_buffers_changed = 1;
4308 else if (XFASTINT (w->total_lines) > 1)
4310 /* Distribute the additional lines of the mini-window
4311 among the other windows. */
4312 Lisp_Object window;
4313 XSETWINDOW (window, w);
4314 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0, 0);
4320 /* Mark window cursors off for all windows in the window tree rooted
4321 at W by setting their phys_cursor_on_p flag to zero. Called from
4322 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4323 the frame are cleared. */
4325 void
4326 mark_window_cursors_off (w)
4327 struct window *w;
4329 while (w)
4331 if (!NILP (w->hchild))
4332 mark_window_cursors_off (XWINDOW (w->hchild));
4333 else if (!NILP (w->vchild))
4334 mark_window_cursors_off (XWINDOW (w->vchild));
4335 else
4336 w->phys_cursor_on_p = 0;
4338 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4343 /* Return number of lines of text (not counting mode lines) in W. */
4346 window_internal_height (w)
4347 struct window *w;
4349 int ht = XFASTINT (w->total_lines);
4351 if (!MINI_WINDOW_P (w))
4353 if (!NILP (w->parent)
4354 || !NILP (w->vchild)
4355 || !NILP (w->hchild)
4356 || !NILP (w->next)
4357 || !NILP (w->prev)
4358 || WINDOW_WANTS_MODELINE_P (w))
4359 --ht;
4361 if (WINDOW_WANTS_HEADER_LINE_P (w))
4362 --ht;
4365 return ht;
4369 /* Return the number of columns in W.
4370 Don't count columns occupied by scroll bars or the vertical bar
4371 separating W from the sibling to its right. */
4374 window_box_text_cols (w)
4375 struct window *w;
4377 struct frame *f = XFRAME (WINDOW_FRAME (w));
4378 int width = XINT (w->total_cols);
4380 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4381 /* Scroll bars occupy a few columns. */
4382 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4383 else if (!FRAME_WINDOW_P (f)
4384 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4385 /* The column of `|' characters separating side-by-side windows
4386 occupies one column only. */
4387 width -= 1;
4389 if (FRAME_WINDOW_P (f))
4390 /* On window-systems, fringes and display margins cannot be
4391 used for normal text. */
4392 width -= (WINDOW_FRINGE_COLS (w)
4393 + WINDOW_LEFT_MARGIN_COLS (w)
4394 + WINDOW_RIGHT_MARGIN_COLS (w));
4396 return width;
4400 /************************************************************************
4401 Window Scrolling
4402 ***********************************************************************/
4404 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4405 N screen-fulls, which is defined as the height of the window minus
4406 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4407 instead. Negative values of N mean scroll down. NOERROR non-zero
4408 means don't signal an error if we try to move over BEGV or ZV,
4409 respectively. */
4411 static void
4412 window_scroll (window, n, whole, noerror)
4413 Lisp_Object window;
4414 int n;
4415 int whole;
4416 int noerror;
4418 immediate_quit = 1;
4420 /* If we must, use the pixel-based version which is much slower than
4421 the line-based one but can handle varying line heights. */
4422 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4423 window_scroll_pixel_based (window, n, whole, noerror);
4424 else
4425 window_scroll_line_based (window, n, whole, noerror);
4427 immediate_quit = 0;
4431 /* Implementation of window_scroll that works based on pixel line
4432 heights. See the comment of window_scroll for parameter
4433 descriptions. */
4435 static void
4436 window_scroll_pixel_based (window, n, whole, noerror)
4437 Lisp_Object window;
4438 int n;
4439 int whole;
4440 int noerror;
4442 struct it it;
4443 struct window *w = XWINDOW (window);
4444 struct text_pos start;
4445 Lisp_Object tem;
4446 int this_scroll_margin;
4447 int preserve_y;
4448 /* True if we fiddled the window vscroll field without really scrolling. */
4449 int vscrolled = 0;
4451 SET_TEXT_POS_FROM_MARKER (start, w->start);
4453 /* If PT is not visible in WINDOW, move back one half of
4454 the screen. Allow PT to be partially visible, otherwise
4455 something like (scroll-down 1) with PT in the line before
4456 the partially visible one would recenter. */
4457 tem = Fpos_visible_in_window_p (make_number (PT), window, Qt);
4458 if (NILP (tem))
4460 /* Move backward half the height of the window. Performance note:
4461 vmotion used here is about 10% faster, but would give wrong
4462 results for variable height lines. */
4463 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4464 it.current_y = it.last_visible_y;
4465 move_it_vertically (&it, - window_box_height (w) / 2);
4467 /* The function move_iterator_vertically may move over more than
4468 the specified y-distance. If it->w is small, e.g. a
4469 mini-buffer window, we may end up in front of the window's
4470 display area. This is the case when Start displaying at the
4471 start of the line containing PT in this case. */
4472 if (it.current_y <= 0)
4474 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4475 move_it_vertically (&it, 0);
4476 it.current_y = 0;
4479 start = it.current.pos;
4482 /* If scroll_preserve_screen_position is non-zero, we try to set
4483 point in the same window line as it is now, so get that line. */
4484 if (!NILP (Vscroll_preserve_screen_position))
4486 start_display (&it, w, start);
4487 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4488 preserve_y = it.current_y;
4490 else
4491 preserve_y = -1;
4493 /* Move iterator it from start the specified distance forward or
4494 backward. The result is the new window start. */
4495 start_display (&it, w, start);
4496 if (whole)
4498 int screen_full = (window_box_height (w)
4499 - next_screen_context_lines * FRAME_LINE_HEIGHT (it.f));
4500 int dy = n * screen_full;
4502 /* Note that move_it_vertically always moves the iterator to the
4503 start of a line. So, if the last line doesn't have a newline,
4504 we would end up at the start of the line ending at ZV. */
4505 if (dy <= 0)
4506 move_it_vertically_backward (&it, -dy);
4507 else if (dy > 0)
4508 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4509 MOVE_TO_POS | MOVE_TO_Y);
4511 else
4512 move_it_by_lines (&it, n, 1);
4514 /* We failed if we find ZV is already on the screen (scrolling up,
4515 means there's nothing past the end), or if we can't start any
4516 earlier (scrolling down, means there's nothing past the top). */
4517 if ((n > 0 && IT_CHARPOS (it) == ZV)
4518 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4520 if (IT_CHARPOS (it) == ZV)
4522 if (it.current_y < it.last_visible_y
4523 && (it.current_y + it.max_ascent + it.max_descent
4524 >= it.last_visible_y))
4526 /* The last line was only partially visible, make it fully
4527 visible. */
4528 w->vscroll = (it.last_visible_y
4529 - it.current_y + it.max_ascent + it.max_descent);
4530 adjust_glyphs (it.f);
4532 else if (noerror)
4533 return;
4534 else
4535 Fsignal (Qend_of_buffer, Qnil);
4537 else
4539 if (w->vscroll != 0)
4540 /* The first line was only partially visible, make it fully
4541 visible. */
4542 w->vscroll = 0;
4543 else if (noerror)
4544 return;
4545 else
4546 Fsignal (Qbeginning_of_buffer, Qnil);
4549 /* If control gets here, then we vscrolled. */
4551 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4553 /* Don't try to change the window start below. */
4554 vscrolled = 1;
4557 if (! vscrolled)
4559 int pos = IT_CHARPOS (it);
4560 int bytepos;
4562 /* If in the middle of a multi-glyph character move forward to
4563 the next character. */
4564 if (in_display_vector_p (&it))
4566 ++pos;
4567 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4570 /* Set the window start, and set up the window for redisplay. */
4571 set_marker_restricted (w->start, make_number (pos),
4572 w->buffer);
4573 bytepos = XMARKER (w->start)->bytepos;
4574 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4575 ? Qt : Qnil);
4576 w->update_mode_line = Qt;
4577 XSETFASTINT (w->last_modified, 0);
4578 XSETFASTINT (w->last_overlay_modified, 0);
4579 /* Set force_start so that redisplay_window will run the
4580 window-scroll-functions. */
4581 w->force_start = Qt;
4584 it.current_y = it.vpos = 0;
4586 /* Preserve the screen position if we must. */
4587 if (preserve_y >= 0)
4589 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
4590 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4592 else
4594 /* Move PT out of scroll margins. */
4595 this_scroll_margin = max (0, scroll_margin);
4596 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4597 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4599 if (n > 0)
4601 /* We moved the window start towards ZV, so PT may be now
4602 in the scroll margin at the top. */
4603 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4604 while (it.current_y < this_scroll_margin)
4606 int prev = it.current_y;
4607 move_it_by_lines (&it, 1, 1);
4608 if (prev == it.current_y)
4609 break;
4611 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4613 else if (n < 0)
4615 int charpos, bytepos;
4617 /* We moved the window start towards BEGV, so PT may be now
4618 in the scroll margin at the bottom. */
4619 move_it_to (&it, PT, -1,
4620 it.last_visible_y - this_scroll_margin - 1, -1,
4621 MOVE_TO_POS | MOVE_TO_Y);
4623 /* Save our position, in case it's correct. */
4624 charpos = IT_CHARPOS (it);
4625 bytepos = IT_BYTEPOS (it);
4627 /* See if point is on a partially visible line at the end. */
4628 move_it_by_lines (&it, 1, 1);
4629 if (it.current_y > it.last_visible_y)
4630 /* The last line was only partially visible, so back up two
4631 lines to make sure we're on a fully visible line. */
4633 move_it_by_lines (&it, -2, 0);
4634 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4636 else
4637 /* No, the position we saved is OK, so use it. */
4638 SET_PT_BOTH (charpos, bytepos);
4644 /* Implementation of window_scroll that works based on screen lines.
4645 See the comment of window_scroll for parameter descriptions. */
4647 static void
4648 window_scroll_line_based (window, n, whole, noerror)
4649 Lisp_Object window;
4650 int n;
4651 int whole;
4652 int noerror;
4654 register struct window *w = XWINDOW (window);
4655 register int opoint = PT, opoint_byte = PT_BYTE;
4656 register int pos, pos_byte;
4657 register int ht = window_internal_height (w);
4658 register Lisp_Object tem;
4659 int lose;
4660 Lisp_Object bolp;
4661 int startpos;
4662 struct position posit;
4663 int original_vpos;
4665 /* If scrolling screen-fulls, compute the number of lines to
4666 scroll from the window's height. */
4667 if (whole)
4668 n *= max (1, ht - next_screen_context_lines);
4670 startpos = marker_position (w->start);
4672 posit = *compute_motion (startpos, 0, 0, 0,
4673 PT, ht, 0,
4674 window_box_text_cols (w), XINT (w->hscroll),
4675 0, w);
4676 original_vpos = posit.vpos;
4678 XSETFASTINT (tem, PT);
4679 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4681 if (NILP (tem))
4683 Fvertical_motion (make_number (- (ht / 2)), window);
4684 startpos = PT;
4687 SET_PT (startpos);
4688 lose = n < 0 && PT == BEGV;
4689 Fvertical_motion (make_number (n), window);
4690 pos = PT;
4691 pos_byte = PT_BYTE;
4692 bolp = Fbolp ();
4693 SET_PT_BOTH (opoint, opoint_byte);
4695 if (lose)
4697 if (noerror)
4698 return;
4699 else
4700 Fsignal (Qbeginning_of_buffer, Qnil);
4703 if (pos < ZV)
4705 int this_scroll_margin = scroll_margin;
4707 /* Don't use a scroll margin that is negative or too large. */
4708 if (this_scroll_margin < 0)
4709 this_scroll_margin = 0;
4711 if (XINT (w->total_lines) < 4 * scroll_margin)
4712 this_scroll_margin = XINT (w->total_lines) / 4;
4714 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4715 w->start_at_line_beg = bolp;
4716 w->update_mode_line = Qt;
4717 XSETFASTINT (w->last_modified, 0);
4718 XSETFASTINT (w->last_overlay_modified, 0);
4719 /* Set force_start so that redisplay_window will run
4720 the window-scroll-functions. */
4721 w->force_start = Qt;
4723 if (whole && !NILP (Vscroll_preserve_screen_position))
4725 SET_PT_BOTH (pos, pos_byte);
4726 Fvertical_motion (make_number (original_vpos), window);
4728 /* If we scrolled forward, put point enough lines down
4729 that it is outside the scroll margin. */
4730 else if (n > 0)
4732 int top_margin;
4734 if (this_scroll_margin > 0)
4736 SET_PT_BOTH (pos, pos_byte);
4737 Fvertical_motion (make_number (this_scroll_margin), window);
4738 top_margin = PT;
4740 else
4741 top_margin = pos;
4743 if (top_margin <= opoint)
4744 SET_PT_BOTH (opoint, opoint_byte);
4745 else if (!NILP (Vscroll_preserve_screen_position))
4747 SET_PT_BOTH (pos, pos_byte);
4748 Fvertical_motion (make_number (original_vpos), window);
4750 else
4751 SET_PT (top_margin);
4753 else if (n < 0)
4755 int bottom_margin;
4757 /* If we scrolled backward, put point near the end of the window
4758 but not within the scroll margin. */
4759 SET_PT_BOTH (pos, pos_byte);
4760 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4761 if (XFASTINT (tem) == ht - this_scroll_margin)
4762 bottom_margin = PT;
4763 else
4764 bottom_margin = PT + 1;
4766 if (bottom_margin > opoint)
4767 SET_PT_BOTH (opoint, opoint_byte);
4768 else
4770 if (!NILP (Vscroll_preserve_screen_position))
4772 SET_PT_BOTH (pos, pos_byte);
4773 Fvertical_motion (make_number (original_vpos), window);
4775 else
4776 Fvertical_motion (make_number (-1), window);
4780 else
4782 if (noerror)
4783 return;
4784 else
4785 Fsignal (Qend_of_buffer, Qnil);
4790 /* Scroll selected_window up or down. If N is nil, scroll a
4791 screen-full which is defined as the height of the window minus
4792 next_screen_context_lines. If N is the symbol `-', scroll.
4793 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4794 up. This is the guts of Fscroll_up and Fscroll_down. */
4796 static void
4797 scroll_command (n, direction)
4798 Lisp_Object n;
4799 int direction;
4801 int count = SPECPDL_INDEX ();
4803 xassert (abs (direction) == 1);
4805 /* If selected window's buffer isn't current, make it current for
4806 the moment. But don't screw up if window_scroll gets an error. */
4807 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4809 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4810 Fset_buffer (XWINDOW (selected_window)->buffer);
4812 /* Make redisplay consider other windows than just selected_window. */
4813 ++windows_or_buffers_changed;
4816 if (NILP (n))
4817 window_scroll (selected_window, direction, 1, 0);
4818 else if (EQ (n, Qminus))
4819 window_scroll (selected_window, -direction, 1, 0);
4820 else
4822 n = Fprefix_numeric_value (n);
4823 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4826 unbind_to (count, Qnil);
4829 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
4830 doc: /* Scroll text of current window upward ARG lines; or near full screen if no ARG.
4831 A near full screen is `next-screen-context-lines' less than a full screen.
4832 Negative ARG means scroll downward.
4833 If ARG is the atom `-', scroll downward by nearly full screen.
4834 When calling from a program, supply as argument a number, nil, or `-'. */)
4835 (arg)
4836 Lisp_Object arg;
4838 scroll_command (arg, 1);
4839 return Qnil;
4842 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
4843 doc: /* Scroll text of current window down ARG lines; or near full screen if no ARG.
4844 A near full screen is `next-screen-context-lines' less than a full screen.
4845 Negative ARG means scroll upward.
4846 If ARG is the atom `-', scroll upward by nearly full screen.
4847 When calling from a program, supply as argument a number, nil, or `-'. */)
4848 (arg)
4849 Lisp_Object arg;
4851 scroll_command (arg, -1);
4852 return Qnil;
4855 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4856 doc: /* Return the other window for \"other window scroll\" commands.
4857 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4858 specifies the window.
4859 If `other-window-scroll-buffer' is non-nil, a window
4860 showing that buffer is used. */)
4863 Lisp_Object window;
4865 if (MINI_WINDOW_P (XWINDOW (selected_window))
4866 && !NILP (Vminibuf_scroll_window))
4867 window = Vminibuf_scroll_window;
4868 /* If buffer is specified, scroll that buffer. */
4869 else if (!NILP (Vother_window_scroll_buffer))
4871 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4872 if (NILP (window))
4873 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4875 else
4877 /* Nothing specified; look for a neighboring window on the same
4878 frame. */
4879 window = Fnext_window (selected_window, Qnil, Qnil);
4881 if (EQ (window, selected_window))
4882 /* That didn't get us anywhere; look for a window on another
4883 visible frame. */
4885 window = Fnext_window (window, Qnil, Qt);
4886 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4887 && ! EQ (window, selected_window));
4890 CHECK_LIVE_WINDOW (window);
4892 if (EQ (window, selected_window))
4893 error ("There is no other window");
4895 return window;
4898 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4899 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4900 A near full screen is `next-screen-context-lines' less than a full screen.
4901 The next window is the one below the current one; or the one at the top
4902 if the current one is at the bottom. Negative ARG means scroll downward.
4903 If ARG is the atom `-', scroll downward by nearly full screen.
4904 When calling from a program, supply as argument a number, nil, or `-'.
4906 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4907 specifies the window to scroll.
4908 If `other-window-scroll-buffer' is non-nil, scroll the window
4909 showing that buffer, popping the buffer up if necessary. */)
4910 (arg)
4911 Lisp_Object arg;
4913 Lisp_Object window;
4914 struct window *w;
4915 int count = SPECPDL_INDEX ();
4917 window = Fother_window_for_scrolling ();
4918 w = XWINDOW (window);
4920 /* Don't screw up if window_scroll gets an error. */
4921 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4922 ++windows_or_buffers_changed;
4924 Fset_buffer (w->buffer);
4925 SET_PT (marker_position (w->pointm));
4927 if (NILP (arg))
4928 window_scroll (window, 1, 1, 1);
4929 else if (EQ (arg, Qminus))
4930 window_scroll (window, -1, 1, 1);
4931 else
4933 if (CONSP (arg))
4934 arg = Fcar (arg);
4935 CHECK_NUMBER (arg);
4936 window_scroll (window, XINT (arg), 0, 1);
4939 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4940 unbind_to (count, Qnil);
4942 return Qnil;
4945 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
4946 doc: /* Scroll selected window display ARG columns left.
4947 Default for ARG is window width minus 2.
4948 Value is the total amount of leftward horizontal scrolling in
4949 effect after the change.
4950 If `automatic-hscrolling' is non-nil, the argument ARG modifies
4951 a lower bound for automatic scrolling, i.e. automatic scrolling
4952 will not scroll a window to a column less than the value returned
4953 by this function. */)
4954 (arg)
4955 register Lisp_Object arg;
4957 Lisp_Object result;
4958 int hscroll;
4959 struct window *w = XWINDOW (selected_window);
4961 if (NILP (arg))
4962 XSETFASTINT (arg, window_box_text_cols (w) - 2);
4963 else
4964 arg = Fprefix_numeric_value (arg);
4966 hscroll = XINT (w->hscroll) + XINT (arg);
4967 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4969 if (interactive_p (0))
4970 w->min_hscroll = w->hscroll;
4972 return result;
4975 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
4976 doc: /* Scroll selected window display ARG columns right.
4977 Default for ARG is window width minus 2.
4978 Value is the total amount of leftward horizontal scrolling in
4979 effect after the change.
4980 If `automatic-hscrolling' is non-nil, the argument ARG modifies
4981 a lower bound for automatic scrolling, i.e. automatic scrolling
4982 will not scroll a window to a column less than the value returned
4983 by this function. */)
4984 (arg)
4985 register Lisp_Object arg;
4987 Lisp_Object result;
4988 int hscroll;
4989 struct window *w = XWINDOW (selected_window);
4991 if (NILP (arg))
4992 XSETFASTINT (arg, window_box_text_cols (w) - 2);
4993 else
4994 arg = Fprefix_numeric_value (arg);
4996 hscroll = XINT (w->hscroll) - XINT (arg);
4997 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4999 if (interactive_p (0))
5000 w->min_hscroll = w->hscroll;
5002 return result;
5005 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5006 doc: /* Return the window which was selected when entering the minibuffer.
5007 Returns nil, if current window is not a minibuffer window. */)
5010 if (minibuf_level > 0
5011 && MINI_WINDOW_P (XWINDOW (selected_window))
5012 && WINDOW_LIVE_P (minibuf_selected_window))
5013 return minibuf_selected_window;
5015 return Qnil;
5018 /* Value is the number of lines actually displayed in window W,
5019 as opposed to its height. */
5021 static int
5022 displayed_window_lines (w)
5023 struct window *w;
5025 struct it it;
5026 struct text_pos start;
5027 int height = window_box_height (w);
5028 struct buffer *old_buffer;
5029 int bottom_y;
5031 if (XBUFFER (w->buffer) != current_buffer)
5033 old_buffer = current_buffer;
5034 set_buffer_internal (XBUFFER (w->buffer));
5036 else
5037 old_buffer = NULL;
5039 /* In case W->start is out of the accessible range, do something
5040 reasonable. This happens in Info mode when Info-scroll-down
5041 calls (recenter -1) while W->start is 1. */
5042 if (XMARKER (w->start)->charpos < BEGV)
5043 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5044 else if (XMARKER (w->start)->charpos > ZV)
5045 SET_TEXT_POS (start, ZV, ZV_BYTE);
5046 else
5047 SET_TEXT_POS_FROM_MARKER (start, w->start);
5049 start_display (&it, w, start);
5050 move_it_vertically (&it, height);
5051 bottom_y = line_bottom_y (&it);
5053 /* rms: On a non-window display,
5054 the value of it.vpos at the bottom of the screen
5055 seems to be 1 larger than window_box_height (w).
5056 This kludge fixes a bug whereby (move-to-window-line -1)
5057 when ZV is on the last screen line
5058 moves to the previous screen line instead of the last one. */
5059 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5060 height++;
5062 /* Add in empty lines at the bottom of the window. */
5063 if (bottom_y < height)
5065 int uy = FRAME_LINE_HEIGHT (it.f);
5066 it.vpos += (height - bottom_y + uy - 1) / uy;
5069 if (old_buffer)
5070 set_buffer_internal (old_buffer);
5072 return it.vpos;
5076 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5077 doc: /* Center point in window and redisplay frame.
5078 With prefix argument ARG, recenter putting point on screen line ARG
5079 relative to the current window. If ARG is negative, it counts up from the
5080 bottom of the window. (ARG should be less than the height of the window.)
5082 If ARG is omitted or nil, erase the entire frame and then
5083 redraw with point in the center of the current window.
5084 Just C-u as prefix means put point in the center of the window
5085 and redisplay normally--don't erase and redraw the frame. */)
5086 (arg)
5087 register Lisp_Object arg;
5089 struct window *w = XWINDOW (selected_window);
5090 struct buffer *buf = XBUFFER (w->buffer);
5091 struct buffer *obuf = current_buffer;
5092 int center_p = 0;
5093 int charpos, bytepos;
5095 /* If redisplay is suppressed due to an error, try again. */
5096 obuf->display_error_modiff = 0;
5098 if (NILP (arg))
5100 int i;
5102 /* Invalidate pixel data calculated for all compositions. */
5103 for (i = 0; i < n_compositions; i++)
5104 composition_table[i]->font = NULL;
5106 Fredraw_frame (w->frame);
5107 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
5108 center_p = 1;
5110 else if (CONSP (arg)) /* Just C-u. */
5111 center_p = 1;
5112 else
5114 arg = Fprefix_numeric_value (arg);
5115 CHECK_NUMBER (arg);
5118 set_buffer_internal (buf);
5120 /* Handle centering on a graphical frame specially. Such frames can
5121 have variable-height lines and centering point on the basis of
5122 line counts would lead to strange effects. */
5123 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5125 if (center_p)
5127 struct it it;
5128 struct text_pos pt;
5130 SET_TEXT_POS (pt, PT, PT_BYTE);
5131 start_display (&it, w, pt);
5132 move_it_vertically (&it, - window_box_height (w) / 2);
5133 charpos = IT_CHARPOS (it);
5134 bytepos = IT_BYTEPOS (it);
5136 else if (XINT (arg) < 0)
5138 struct it it;
5139 struct text_pos pt;
5140 int y0, y1, h, nlines;
5142 SET_TEXT_POS (pt, PT, PT_BYTE);
5143 start_display (&it, w, pt);
5144 y0 = it.current_y;
5146 /* The amount of pixels we have to move back is the window
5147 height minus what's displayed in the line containing PT,
5148 and the lines below. */
5149 nlines = - XINT (arg) - 1;
5150 move_it_by_lines (&it, nlines, 1);
5152 y1 = line_bottom_y (&it);
5154 /* If we can't move down NLINES lines because we hit
5155 the end of the buffer, count in some empty lines. */
5156 if (it.vpos < nlines)
5157 y1 += (nlines - it.vpos) * FRAME_LINE_HEIGHT (it.f);
5159 h = window_box_height (w) - (y1 - y0);
5161 start_display (&it, w, pt);
5162 move_it_vertically (&it, - h);
5163 charpos = IT_CHARPOS (it);
5164 bytepos = IT_BYTEPOS (it);
5166 else
5168 struct position pos;
5169 pos = *vmotion (PT, - XINT (arg), w);
5170 charpos = pos.bufpos;
5171 bytepos = pos.bytepos;
5174 else
5176 struct position pos;
5177 int ht = window_internal_height (w);
5179 if (center_p)
5180 arg = make_number (ht / 2);
5181 else if (XINT (arg) < 0)
5182 arg = make_number (XINT (arg) + ht);
5184 pos = *vmotion (PT, - XINT (arg), w);
5185 charpos = pos.bufpos;
5186 bytepos = pos.bytepos;
5189 /* Set the new window start. */
5190 set_marker_both (w->start, w->buffer, charpos, bytepos);
5191 w->window_end_valid = Qnil;
5193 w->optional_new_start = Qt;
5195 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5196 w->start_at_line_beg = Qt;
5197 else
5198 w->start_at_line_beg = Qnil;
5200 set_buffer_internal (obuf);
5201 return Qnil;
5205 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5206 0, 1, 0,
5207 doc: /* Return the height in lines of the text display area of WINDOW.
5208 This doesn't include the mode-line (or header-line if any) or any
5209 partial-height lines in the text display area. */)
5210 (window)
5211 Lisp_Object window;
5213 struct window *w = decode_window (window);
5214 int pixel_height = window_box_height (w);
5215 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5216 return make_number (line_height);
5221 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5222 1, 1, "P",
5223 doc: /* Position point relative to window.
5224 With no argument, position point at center of window.
5225 An argument specifies vertical position within the window;
5226 zero means top of window, negative means relative to bottom of window. */)
5227 (arg)
5228 Lisp_Object arg;
5230 struct window *w = XWINDOW (selected_window);
5231 int lines, start;
5232 Lisp_Object window;
5234 window = selected_window;
5235 start = marker_position (w->start);
5236 if (start < BEGV || start > ZV)
5238 int height = window_internal_height (w);
5239 Fvertical_motion (make_number (- (height / 2)), window);
5240 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5241 w->start_at_line_beg = Fbolp ();
5242 w->force_start = Qt;
5244 else
5245 Fgoto_char (w->start);
5247 lines = displayed_window_lines (w);
5248 if (NILP (arg))
5249 XSETFASTINT (arg, lines / 2);
5250 else
5252 arg = Fprefix_numeric_value (arg);
5253 if (XINT (arg) < 0)
5254 XSETINT (arg, XINT (arg) + lines);
5257 /* Skip past a partially visible first line. */
5258 if (w->vscroll)
5259 XSETINT (arg, XINT (arg) + 1);
5261 return Fvertical_motion (arg, window);
5266 /***********************************************************************
5267 Window Configuration
5268 ***********************************************************************/
5270 struct save_window_data
5272 EMACS_INT size_from_Lisp_Vector_struct;
5273 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5274 Lisp_Object frame_cols, frame_lines, frame_menu_bar_lines;
5275 Lisp_Object frame_tool_bar_lines;
5276 Lisp_Object selected_frame;
5277 Lisp_Object current_window;
5278 Lisp_Object current_buffer;
5279 Lisp_Object minibuf_scroll_window;
5280 Lisp_Object minibuf_selected_window;
5281 Lisp_Object root_window;
5282 Lisp_Object focus_frame;
5283 /* Record the values of window-min-width and window-min-height
5284 so that window sizes remain consistent with them. */
5285 Lisp_Object min_width, min_height;
5286 /* A vector, each of whose elements is a struct saved_window
5287 for one window. */
5288 Lisp_Object saved_windows;
5291 /* This is saved as a Lisp_Vector */
5292 struct saved_window
5294 /* these first two must agree with struct Lisp_Vector in lisp.h */
5295 EMACS_INT size_from_Lisp_Vector_struct;
5296 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5298 Lisp_Object window;
5299 Lisp_Object buffer, start, pointm, mark;
5300 Lisp_Object left_col, top_line, total_cols, total_lines;
5301 Lisp_Object hscroll, min_hscroll;
5302 Lisp_Object parent, prev;
5303 Lisp_Object start_at_line_beg;
5304 Lisp_Object display_table;
5305 Lisp_Object orig_top_line, orig_total_lines;
5306 Lisp_Object left_margin_cols, right_margin_cols;
5307 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5308 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5311 #define SAVED_WINDOW_VECTOR_SIZE 24 /* Arg to Fmake_vector */
5313 #define SAVED_WINDOW_N(swv,n) \
5314 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5316 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5317 doc: /* Return t if OBJECT is a window-configuration object. */)
5318 (object)
5319 Lisp_Object object;
5321 if (WINDOW_CONFIGURATIONP (object))
5322 return Qt;
5323 return Qnil;
5326 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5327 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5328 (config)
5329 Lisp_Object config;
5331 register struct save_window_data *data;
5332 struct Lisp_Vector *saved_windows;
5334 if (! WINDOW_CONFIGURATIONP (config))
5335 wrong_type_argument (Qwindow_configuration_p, config);
5337 data = (struct save_window_data *) XVECTOR (config);
5338 saved_windows = XVECTOR (data->saved_windows);
5339 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5342 DEFUN ("set-window-configuration", Fset_window_configuration,
5343 Sset_window_configuration, 1, 1, 0,
5344 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5345 CONFIGURATION must be a value previously returned
5346 by `current-window-configuration' (which see).
5347 If CONFIGURATION was made from a frame that is now deleted,
5348 only frame-independent values can be restored. In this case,
5349 the return value is nil. Otherwise the value is t. */)
5350 (configuration)
5351 Lisp_Object configuration;
5353 register struct save_window_data *data;
5354 struct Lisp_Vector *saved_windows;
5355 Lisp_Object new_current_buffer;
5356 Lisp_Object frame;
5357 FRAME_PTR f;
5358 int old_point = -1;
5360 while (!WINDOW_CONFIGURATIONP (configuration))
5361 wrong_type_argument (Qwindow_configuration_p, configuration);
5363 data = (struct save_window_data *) XVECTOR (configuration);
5364 saved_windows = XVECTOR (data->saved_windows);
5366 new_current_buffer = data->current_buffer;
5367 if (NILP (XBUFFER (new_current_buffer)->name))
5368 new_current_buffer = Qnil;
5369 else
5371 if (XBUFFER (new_current_buffer) == current_buffer)
5372 old_point = PT;
5373 else
5374 old_point = BUF_PT (XBUFFER (new_current_buffer));
5377 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5378 f = XFRAME (frame);
5380 /* If f is a dead frame, don't bother rebuilding its window tree.
5381 However, there is other stuff we should still try to do below. */
5382 if (FRAME_LIVE_P (f))
5384 register struct window *w;
5385 register struct saved_window *p;
5386 struct window *root_window;
5387 struct window **leaf_windows;
5388 int n_leaf_windows;
5389 int k, i, n;
5391 /* If the frame has been resized since this window configuration was
5392 made, we change the frame to the size specified in the
5393 configuration, restore the configuration, and then resize it
5394 back. We keep track of the prevailing height in these variables. */
5395 int previous_frame_lines = FRAME_LINES (f);
5396 int previous_frame_cols = FRAME_COLS (f);
5397 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5398 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5400 /* The mouse highlighting code could get screwed up
5401 if it runs during this. */
5402 BLOCK_INPUT;
5404 if (XFASTINT (data->frame_lines) != previous_frame_lines
5405 || XFASTINT (data->frame_cols) != previous_frame_cols)
5406 change_frame_size (f, XFASTINT (data->frame_lines),
5407 XFASTINT (data->frame_cols), 0, 0, 0);
5408 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5409 if (XFASTINT (data->frame_menu_bar_lines)
5410 != previous_frame_menu_bar_lines)
5411 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
5412 #ifdef HAVE_WINDOW_SYSTEM
5413 if (XFASTINT (data->frame_tool_bar_lines)
5414 != previous_frame_tool_bar_lines)
5415 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
5416 #endif
5417 #endif
5419 /* "Swap out" point from the selected window
5420 into its buffer. We do this now, before
5421 restoring the window contents, and prevent it from
5422 being done later on when we select a new window. */
5423 if (! NILP (XWINDOW (selected_window)->buffer))
5425 w = XWINDOW (selected_window);
5426 set_marker_both (w->pointm,
5427 w->buffer,
5428 BUF_PT (XBUFFER (w->buffer)),
5429 BUF_PT_BYTE (XBUFFER (w->buffer)));
5432 windows_or_buffers_changed++;
5433 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5435 /* Problem: Freeing all matrices and later allocating them again
5436 is a serious redisplay flickering problem. What we would
5437 really like to do is to free only those matrices not reused
5438 below. */
5439 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5440 leaf_windows
5441 = (struct window **) alloca (count_windows (root_window)
5442 * sizeof (struct window *));
5443 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5445 /* Temporarily avoid any problems with windows that are smaller
5446 than they are supposed to be. */
5447 window_min_height = 1;
5448 window_min_width = 1;
5450 /* Kludge Alert!
5451 Mark all windows now on frame as "deleted".
5452 Restoring the new configuration "undeletes" any that are in it.
5454 Save their current buffers in their height fields, since we may
5455 need it later, if a buffer saved in the configuration is now
5456 dead. */
5457 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5459 for (k = 0; k < saved_windows->size; k++)
5461 p = SAVED_WINDOW_N (saved_windows, k);
5462 w = XWINDOW (p->window);
5463 w->next = Qnil;
5465 if (!NILP (p->parent))
5466 w->parent = SAVED_WINDOW_N (saved_windows,
5467 XFASTINT (p->parent))->window;
5468 else
5469 w->parent = Qnil;
5471 if (!NILP (p->prev))
5473 w->prev = SAVED_WINDOW_N (saved_windows,
5474 XFASTINT (p->prev))->window;
5475 XWINDOW (w->prev)->next = p->window;
5477 else
5479 w->prev = Qnil;
5480 if (!NILP (w->parent))
5482 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5484 XWINDOW (w->parent)->vchild = p->window;
5485 XWINDOW (w->parent)->hchild = Qnil;
5487 else
5489 XWINDOW (w->parent)->hchild = p->window;
5490 XWINDOW (w->parent)->vchild = Qnil;
5495 /* If we squirreled away the buffer in the window's height,
5496 restore it now. */
5497 if (BUFFERP (w->total_lines))
5498 w->buffer = w->total_lines;
5499 w->left_col = p->left_col;
5500 w->top_line = p->top_line;
5501 w->total_cols = p->total_cols;
5502 w->total_lines = p->total_lines;
5503 w->hscroll = p->hscroll;
5504 w->min_hscroll = p->min_hscroll;
5505 w->display_table = p->display_table;
5506 w->orig_top_line = p->orig_top_line;
5507 w->orig_total_lines = p->orig_total_lines;
5508 w->left_margin_cols = p->left_margin_cols;
5509 w->right_margin_cols = p->right_margin_cols;
5510 w->left_fringe_width = p->left_fringe_width;
5511 w->right_fringe_width = p->right_fringe_width;
5512 w->fringes_outside_margins = p->fringes_outside_margins;
5513 w->scroll_bar_width = p->scroll_bar_width;
5514 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
5515 XSETFASTINT (w->last_modified, 0);
5516 XSETFASTINT (w->last_overlay_modified, 0);
5518 /* Reinstall the saved buffer and pointers into it. */
5519 if (NILP (p->buffer))
5520 w->buffer = p->buffer;
5521 else
5523 if (!NILP (XBUFFER (p->buffer)->name))
5524 /* If saved buffer is alive, install it. */
5526 w->buffer = p->buffer;
5527 w->start_at_line_beg = p->start_at_line_beg;
5528 set_marker_restricted (w->start, p->start, w->buffer);
5529 set_marker_restricted (w->pointm, p->pointm, w->buffer);
5530 Fset_marker (XBUFFER (w->buffer)->mark,
5531 p->mark, w->buffer);
5533 /* As documented in Fcurrent_window_configuration, don't
5534 restore the location of point in the buffer which was
5535 current when the window configuration was recorded. */
5536 if (!EQ (p->buffer, new_current_buffer)
5537 && XBUFFER (p->buffer) == current_buffer)
5538 Fgoto_char (w->pointm);
5540 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
5541 /* Else unless window has a live buffer, get one. */
5543 w->buffer = Fcdr (Fcar (Vbuffer_alist));
5544 /* This will set the markers to beginning of visible
5545 range. */
5546 set_marker_restricted (w->start, make_number (0), w->buffer);
5547 set_marker_restricted (w->pointm, make_number (0),w->buffer);
5548 w->start_at_line_beg = Qt;
5550 else
5551 /* Keeping window's old buffer; make sure the markers
5552 are real. */
5554 /* Set window markers at start of visible range. */
5555 if (XMARKER (w->start)->buffer == 0)
5556 set_marker_restricted (w->start, make_number (0),
5557 w->buffer);
5558 if (XMARKER (w->pointm)->buffer == 0)
5559 set_marker_restricted_both (w->pointm, w->buffer,
5560 BUF_PT (XBUFFER (w->buffer)),
5561 BUF_PT_BYTE (XBUFFER (w->buffer)));
5562 w->start_at_line_beg = Qt;
5567 FRAME_ROOT_WINDOW (f) = data->root_window;
5568 /* Prevent "swapping out point" in the old selected window
5569 using the buffer that has been restored into it.
5570 Use the point value from the beginning of this function
5571 since unshow_buffer (called from delete_all_subwindows)
5572 could have altered it. */
5573 selected_window = Qnil;
5574 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5575 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5576 make_number (old_point),
5577 XWINDOW (data->current_window)->buffer);
5579 Fselect_window (data->current_window, Qnil);
5580 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
5581 = selected_window;
5583 if (NILP (data->focus_frame)
5584 || (FRAMEP (data->focus_frame)
5585 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5586 Fredirect_frame_focus (frame, data->focus_frame);
5588 #if 0 /* I don't understand why this is needed, and it causes problems
5589 when the frame's old selected window has been deleted. */
5590 if (f != selected_frame && FRAME_WINDOW_P (f))
5591 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
5592 0, 0);
5593 #endif
5595 /* Set the screen height to the value it had before this function. */
5596 if (previous_frame_lines != FRAME_LINES (f)
5597 || previous_frame_cols != FRAME_COLS (f))
5598 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5599 0, 0, 0);
5600 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5601 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5602 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5603 make_number (0));
5604 #ifdef HAVE_WINDOW_SYSTEM
5605 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5606 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5607 make_number (0));
5608 #endif
5609 #endif
5611 /* Now, free glyph matrices in windows that were not reused. */
5612 for (i = n = 0; i < n_leaf_windows; ++i)
5614 if (NILP (leaf_windows[i]->buffer))
5616 /* Assert it's not reused as a combination. */
5617 xassert (NILP (leaf_windows[i]->hchild)
5618 && NILP (leaf_windows[i]->vchild));
5619 free_window_matrices (leaf_windows[i]);
5621 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5622 ++n;
5625 adjust_glyphs (f);
5627 UNBLOCK_INPUT;
5629 /* Fselect_window will have made f the selected frame, so we
5630 reselect the proper frame here. Fhandle_switch_frame will change the
5631 selected window too, but that doesn't make the call to
5632 Fselect_window above totally superfluous; it still sets f's
5633 selected window. */
5634 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5635 do_switch_frame (data->selected_frame, 0, 0);
5637 if (! NILP (Vwindow_configuration_change_hook)
5638 && ! NILP (Vrun_hooks))
5639 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
5642 if (!NILP (new_current_buffer))
5643 Fset_buffer (new_current_buffer);
5645 /* Restore the minimum heights recorded in the configuration. */
5646 window_min_height = XINT (data->min_height);
5647 window_min_width = XINT (data->min_width);
5649 Vminibuf_scroll_window = data->minibuf_scroll_window;
5650 minibuf_selected_window = data->minibuf_selected_window;
5652 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5655 /* Mark all windows now on frame as deleted
5656 by setting their buffers to nil. */
5658 void
5659 delete_all_subwindows (w)
5660 register struct window *w;
5662 if (!NILP (w->next))
5663 delete_all_subwindows (XWINDOW (w->next));
5664 if (!NILP (w->vchild))
5665 delete_all_subwindows (XWINDOW (w->vchild));
5666 if (!NILP (w->hchild))
5667 delete_all_subwindows (XWINDOW (w->hchild));
5669 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
5671 if (!NILP (w->buffer))
5672 unshow_buffer (w);
5674 /* We set all three of these fields to nil, to make sure that we can
5675 distinguish this dead window from any live window. Live leaf
5676 windows will have buffer set, and combination windows will have
5677 vchild or hchild set. */
5678 w->buffer = Qnil;
5679 w->vchild = Qnil;
5680 w->hchild = Qnil;
5682 Vwindow_list = Qnil;
5685 static int
5686 count_windows (window)
5687 register struct window *window;
5689 register int count = 1;
5690 if (!NILP (window->next))
5691 count += count_windows (XWINDOW (window->next));
5692 if (!NILP (window->vchild))
5693 count += count_windows (XWINDOW (window->vchild));
5694 if (!NILP (window->hchild))
5695 count += count_windows (XWINDOW (window->hchild));
5696 return count;
5700 /* Fill vector FLAT with leaf windows under W, starting at index I.
5701 Value is last index + 1. */
5703 static int
5704 get_leaf_windows (w, flat, i)
5705 struct window *w;
5706 struct window **flat;
5707 int i;
5709 while (w)
5711 if (!NILP (w->hchild))
5712 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5713 else if (!NILP (w->vchild))
5714 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5715 else
5716 flat[i++] = w;
5718 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5721 return i;
5725 /* Return a pointer to the glyph W's physical cursor is on. Value is
5726 null if W's current matrix is invalid, so that no meaningfull glyph
5727 can be returned. */
5729 struct glyph *
5730 get_phys_cursor_glyph (w)
5731 struct window *w;
5733 struct glyph_row *row;
5734 struct glyph *glyph;
5736 if (w->phys_cursor.vpos >= 0
5737 && w->phys_cursor.vpos < w->current_matrix->nrows
5738 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
5739 row->enabled_p)
5740 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
5741 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
5742 else
5743 glyph = NULL;
5745 return glyph;
5749 static int
5750 save_window_save (window, vector, i)
5751 Lisp_Object window;
5752 struct Lisp_Vector *vector;
5753 int i;
5755 register struct saved_window *p;
5756 register struct window *w;
5757 register Lisp_Object tem;
5759 for (;!NILP (window); window = w->next)
5761 p = SAVED_WINDOW_N (vector, i);
5762 w = XWINDOW (window);
5764 XSETFASTINT (w->temslot, i++);
5765 p->window = window;
5766 p->buffer = w->buffer;
5767 p->left_col = w->left_col;
5768 p->top_line = w->top_line;
5769 p->total_cols = w->total_cols;
5770 p->total_lines = w->total_lines;
5771 p->hscroll = w->hscroll;
5772 p->min_hscroll = w->min_hscroll;
5773 p->display_table = w->display_table;
5774 p->orig_top_line = w->orig_top_line;
5775 p->orig_total_lines = w->orig_total_lines;
5776 p->left_margin_cols = w->left_margin_cols;
5777 p->right_margin_cols = w->right_margin_cols;
5778 p->left_fringe_width = w->left_fringe_width;
5779 p->right_fringe_width = w->right_fringe_width;
5780 p->fringes_outside_margins = w->fringes_outside_margins;
5781 p->scroll_bar_width = w->scroll_bar_width;
5782 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5783 if (!NILP (w->buffer))
5785 /* Save w's value of point in the window configuration.
5786 If w is the selected window, then get the value of point
5787 from the buffer; pointm is garbage in the selected window. */
5788 if (EQ (window, selected_window))
5790 p->pointm = Fmake_marker ();
5791 set_marker_both (p->pointm, w->buffer,
5792 BUF_PT (XBUFFER (w->buffer)),
5793 BUF_PT_BYTE (XBUFFER (w->buffer)));
5795 else
5796 p->pointm = Fcopy_marker (w->pointm, Qnil);
5798 p->start = Fcopy_marker (w->start, Qnil);
5799 p->start_at_line_beg = w->start_at_line_beg;
5801 tem = XBUFFER (w->buffer)->mark;
5802 p->mark = Fcopy_marker (tem, Qnil);
5804 else
5806 p->pointm = Qnil;
5807 p->start = Qnil;
5808 p->mark = Qnil;
5809 p->start_at_line_beg = Qnil;
5812 if (NILP (w->parent))
5813 p->parent = Qnil;
5814 else
5815 p->parent = XWINDOW (w->parent)->temslot;
5817 if (NILP (w->prev))
5818 p->prev = Qnil;
5819 else
5820 p->prev = XWINDOW (w->prev)->temslot;
5822 if (!NILP (w->vchild))
5823 i = save_window_save (w->vchild, vector, i);
5824 if (!NILP (w->hchild))
5825 i = save_window_save (w->hchild, vector, i);
5828 return i;
5831 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
5832 Scurrent_window_configuration, 0, 1, 0,
5833 doc: /* Return an object representing the current window configuration of FRAME.
5834 If FRAME is nil or omitted, use the selected frame.
5835 This describes the number of windows, their sizes and current buffers,
5836 and for each displayed buffer, where display starts, and the positions of
5837 point and mark. An exception is made for point in the current buffer:
5838 its value is -not- saved.
5839 This also records the currently selected frame, and FRAME's focus
5840 redirection (see `redirect-frame-focus'). */)
5841 (frame)
5842 Lisp_Object frame;
5844 register Lisp_Object tem;
5845 register int n_windows;
5846 register struct save_window_data *data;
5847 register struct Lisp_Vector *vec;
5848 register int i;
5849 FRAME_PTR f;
5851 if (NILP (frame))
5852 frame = selected_frame;
5853 CHECK_LIVE_FRAME (frame);
5854 f = XFRAME (frame);
5856 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5857 vec = allocate_other_vector (VECSIZE (struct save_window_data));
5858 data = (struct save_window_data *)vec;
5860 XSETFASTINT (data->frame_cols, FRAME_COLS (f));
5861 XSETFASTINT (data->frame_lines, FRAME_LINES (f));
5862 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
5863 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
5864 data->selected_frame = selected_frame;
5865 data->current_window = FRAME_SELECTED_WINDOW (f);
5866 XSETBUFFER (data->current_buffer, current_buffer);
5867 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
5868 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
5869 data->root_window = FRAME_ROOT_WINDOW (f);
5870 data->focus_frame = FRAME_FOCUS_FRAME (f);
5871 XSETINT (data->min_height, window_min_height);
5872 XSETINT (data->min_width, window_min_width);
5873 tem = Fmake_vector (make_number (n_windows), Qnil);
5874 data->saved_windows = tem;
5875 for (i = 0; i < n_windows; i++)
5876 XVECTOR (tem)->contents[i]
5877 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
5878 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
5879 XSETWINDOW_CONFIGURATION (tem, data);
5880 return (tem);
5883 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
5884 0, UNEVALLED, 0,
5885 doc: /* Execute body, preserving window sizes and contents.
5886 Restore which buffer appears in which window, where display starts,
5887 and the value of point and mark for each window.
5888 Also restore the choice of selected window.
5889 Also restore which buffer is current.
5890 Does not restore the value of point in current buffer.
5891 usage: (save-window-excursion BODY ...) */)
5892 (args)
5893 Lisp_Object args;
5895 register Lisp_Object val;
5896 register int count = SPECPDL_INDEX ();
5898 record_unwind_protect (Fset_window_configuration,
5899 Fcurrent_window_configuration (Qnil));
5900 val = Fprogn (args);
5901 return unbind_to (count, val);
5905 /***********************************************************************
5906 Marginal Areas
5907 ***********************************************************************/
5909 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
5910 2, 3, 0,
5911 doc: /* Set width of marginal areas of window WINDOW.
5912 If WINDOW is nil, set margins of the currently selected window.
5913 Second arg LEFT-WIDTH specifies the number of character cells to
5914 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
5915 does the same for the right marginal area. A nil width parameter
5916 means no margin. */)
5917 (window, left, right)
5918 Lisp_Object window, left, right;
5920 struct window *w = decode_window (window);
5922 /* Translate negative or zero widths to nil.
5923 Margins that are too wide have to be checked elsewhere. */
5925 if (!NILP (left))
5927 CHECK_NUMBER (left);
5928 if (XINT (left) <= 0)
5929 left = Qnil;
5932 if (!NILP (right))
5934 CHECK_NUMBER (right);
5935 if (XINT (right) <= 0)
5936 right = Qnil;
5939 if (!EQ (w->left_margin_cols, left)
5940 || !EQ (w->right_margin_cols, right))
5942 w->left_margin_cols = left;
5943 w->right_margin_cols = right;
5945 adjust_window_margins (w);
5947 ++windows_or_buffers_changed;
5948 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
5951 return Qnil;
5955 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
5956 0, 1, 0,
5957 doc: /* Get width of marginal areas of window WINDOW.
5958 If WINDOW is omitted or nil, use the currently selected window.
5959 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
5960 If a marginal area does not exist, its width will be returned
5961 as nil. */)
5962 (window)
5963 Lisp_Object window;
5965 struct window *w = decode_window (window);
5966 return Fcons (w->left_margin_cols, w->right_margin_cols);
5971 /***********************************************************************
5972 Fringes
5973 ***********************************************************************/
5975 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
5976 2, 4, 0,
5977 doc: /* Set the fringe widths of window WINDOW.
5978 If WINDOW is nil, set the fringe widths of the currently selected
5979 window.
5980 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
5981 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
5982 fringe width. If a fringe width arg is nil, that means to use the
5983 frame's default fringe width. Default fringe widths can be set with
5984 the command `set-fringe-style'.
5985 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
5986 outside of the display margins. By default, fringes are drawn between
5987 display marginal areas and the text area. */)
5988 (window, left, right, outside_margins)
5989 Lisp_Object window, left, right, outside_margins;
5991 struct window *w = decode_window (window);
5993 if (!NILP (left))
5994 CHECK_NUMBER (left);
5995 if (!NILP (right))
5996 CHECK_NUMBER (right);
5998 if (!EQ (w->left_fringe_width, left)
5999 || !EQ (w->right_fringe_width, right)
6000 || !EQ (w->fringes_outside_margins, outside_margins))
6002 w->left_fringe_width = left;
6003 w->right_fringe_width = right;
6004 w->fringes_outside_margins = outside_margins;
6006 adjust_window_margins (w);
6008 clear_glyph_matrix (w->current_matrix);
6009 w->window_end_valid = Qnil;
6011 ++windows_or_buffers_changed;
6012 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6015 return Qnil;
6019 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6020 0, 1, 0,
6021 doc: /* Get width of fringes of window WINDOW.
6022 If WINDOW is omitted or nil, use the currently selected window.
6023 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6024 (window)
6025 Lisp_Object window;
6027 struct window *w = decode_window (window);
6028 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6029 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6030 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ?
6031 Qt : Qnil), Qnil)));
6036 /***********************************************************************
6037 Scroll bars
6038 ***********************************************************************/
6040 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6041 2, 4, 0,
6042 doc: /* Set width and type of scroll bars of window WINDOW.
6043 If window is nil, set scroll bars of the currently selected window.
6044 Second parameter WIDTH specifies the pixel width for the scroll bar;
6045 this is automatically adjusted to a multiple of the frame column width.
6046 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6047 bar: left, right, or nil.
6048 If WIDTH is nil, use the frame's scroll-bar width.
6049 If TYPE is t, use the frame's scroll-bar type. */)
6050 (window, width, vertical_type, horizontal_type)
6051 Lisp_Object window, width, vertical_type, horizontal_type;
6053 struct window *w = decode_window (window);
6055 if (!NILP (width))
6056 CHECK_NUMBER (width);
6058 if (XINT (width) == 0)
6059 vertical_type = Qnil;
6061 if (!(EQ (vertical_type, Qnil)
6062 || EQ (vertical_type, Qleft)
6063 || EQ (vertical_type, Qright)
6064 || EQ (vertical_type, Qt)))
6065 error ("Invalid type of vertical scroll bar");
6067 if (!EQ (w->scroll_bar_width, width)
6068 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6070 w->scroll_bar_width = width;
6071 w->vertical_scroll_bar_type = vertical_type;
6073 adjust_window_margins (w);
6075 clear_glyph_matrix (w->current_matrix);
6076 w->window_end_valid = Qnil;
6078 ++windows_or_buffers_changed;
6079 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6082 return Qnil;
6086 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6087 0, 1, 0,
6088 doc: /* Get width and type of scroll bars of window WINDOW.
6089 If WINDOW is omitted or nil, use the currently selected window.
6090 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6091 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6092 value. */)
6093 (window)
6094 Lisp_Object window;
6096 struct window *w = decode_window (window);
6097 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6098 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6099 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6100 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6101 Fcons (w->vertical_scroll_bar_type,
6102 Fcons (Qnil, Qnil))));
6107 /***********************************************************************
6108 Smooth scrolling
6109 ***********************************************************************/
6111 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6112 doc: /* Return the amount by which WINDOW is scrolled vertically.
6113 Use the selected window if WINDOW is nil or omitted.
6114 Normally, value is a multiple of the canonical character height of WINDOW;
6115 optional second arg PIXELS_P means value is measured in pixels. */)
6116 (window, pixels_p)
6117 Lisp_Object window, pixels_p;
6119 Lisp_Object result;
6120 struct frame *f;
6121 struct window *w;
6123 if (NILP (window))
6124 window = selected_window;
6125 else
6126 CHECK_WINDOW (window);
6127 w = XWINDOW (window);
6128 f = XFRAME (w->frame);
6130 if (FRAME_WINDOW_P (f))
6131 result = (NILP (pixels_p)
6132 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6133 : make_number (-w->vscroll));
6134 else
6135 result = make_number (0);
6136 return result;
6140 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6141 2, 3, 0,
6142 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6143 WINDOW nil means use the selected window. Normally, VSCROLL is a
6144 non-negative multiple of the canonical character height of WINDOW;
6145 optional third arg PIXELS_P non-nil means that VSCROLL is in pixels. */)
6146 (window, vscroll, pixels_p)
6147 Lisp_Object window, vscroll, pixels_p;
6149 struct window *w;
6150 struct frame *f;
6152 if (NILP (window))
6153 window = selected_window;
6154 else
6155 CHECK_WINDOW (window);
6156 CHECK_NUMBER_OR_FLOAT (vscroll);
6158 w = XWINDOW (window);
6159 f = XFRAME (w->frame);
6161 if (FRAME_WINDOW_P (f))
6163 int old_dy = w->vscroll;
6165 w->vscroll = - (NILP (pixels_p)
6166 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6167 : XFLOATINT (vscroll));
6168 w->vscroll = min (w->vscroll, 0);
6170 /* Adjust glyph matrix of the frame if the virtual display
6171 area becomes larger than before. */
6172 if (w->vscroll < 0 && w->vscroll < old_dy)
6173 adjust_glyphs (f);
6175 /* Prevent redisplay shortcuts. */
6176 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6179 return Fwindow_vscroll (window, pixels_p);
6183 /* Call FN for all leaf windows on frame F. FN is called with the
6184 first argument being a pointer to the leaf window, and with
6185 additional argument USER_DATA. Stops when FN returns 0. */
6187 void
6188 foreach_window (f, fn, user_data)
6189 struct frame *f;
6190 int (* fn) P_ ((struct window *, void *));
6191 void *user_data;
6193 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6197 /* Helper function for foreach_window. Call FN for all leaf windows
6198 reachable from W. FN is called with the first argument being a
6199 pointer to the leaf window, and with additional argument USER_DATA.
6200 Stop when FN returns 0. Value is 0 if stopped by FN. */
6202 static int
6203 foreach_window_1 (w, fn, user_data)
6204 struct window *w;
6205 int (* fn) P_ ((struct window *, void *));
6206 void *user_data;
6208 int cont;
6210 for (cont = 1; w && cont;)
6212 if (!NILP (w->hchild))
6213 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6214 else if (!NILP (w->vchild))
6215 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6216 else
6217 cont = fn (w, user_data);
6219 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6222 return cont;
6226 /* Freeze or unfreeze the window start of W unless it is a
6227 mini-window or the selected window. FREEZE_P non-null means freeze
6228 the window start. */
6230 static int
6231 freeze_window_start (w, freeze_p)
6232 struct window *w;
6233 void *freeze_p;
6235 if (w == XWINDOW (selected_window)
6236 || MINI_WINDOW_P (w)
6237 || (MINI_WINDOW_P (XWINDOW (selected_window))
6238 && ! NILP (Vminibuf_scroll_window)
6239 && w == XWINDOW (Vminibuf_scroll_window)))
6240 freeze_p = NULL;
6242 w->frozen_window_start_p = freeze_p != NULL;
6243 return 1;
6247 /* Freeze or unfreeze the window starts of all leaf windows on frame
6248 F, except the selected window and a mini-window. FREEZE_P non-zero
6249 means freeze the window start. */
6251 void
6252 freeze_window_starts (f, freeze_p)
6253 struct frame *f;
6254 int freeze_p;
6256 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6260 /***********************************************************************
6261 Initialization
6262 ***********************************************************************/
6264 /* Return 1 if window configurations C1 and C2
6265 describe the same state of affairs. This is used by Fequal. */
6268 compare_window_configurations (c1, c2, ignore_positions)
6269 Lisp_Object c1, c2;
6270 int ignore_positions;
6272 register struct save_window_data *d1, *d2;
6273 struct Lisp_Vector *sw1, *sw2;
6274 int i;
6276 if (!WINDOW_CONFIGURATIONP (c1))
6277 wrong_type_argument (Qwindow_configuration_p, c1);
6278 if (!WINDOW_CONFIGURATIONP (c2))
6279 wrong_type_argument (Qwindow_configuration_p, c2);
6281 d1 = (struct save_window_data *) XVECTOR (c1);
6282 d2 = (struct save_window_data *) XVECTOR (c2);
6283 sw1 = XVECTOR (d1->saved_windows);
6284 sw2 = XVECTOR (d2->saved_windows);
6286 if (! EQ (d1->frame_cols, d2->frame_cols))
6287 return 0;
6288 if (! EQ (d1->frame_lines, d2->frame_lines))
6289 return 0;
6290 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
6291 return 0;
6292 if (! EQ (d1->selected_frame, d2->selected_frame))
6293 return 0;
6294 /* Don't compare the current_window field directly.
6295 Instead see w1_is_current and w2_is_current, below. */
6296 if (! EQ (d1->current_buffer, d2->current_buffer))
6297 return 0;
6298 if (! ignore_positions)
6300 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6301 return 0;
6302 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6303 return 0;
6305 /* Don't compare the root_window field.
6306 We don't require the two configurations
6307 to use the same window object,
6308 and the two root windows must be equivalent
6309 if everything else compares equal. */
6310 if (! EQ (d1->focus_frame, d2->focus_frame))
6311 return 0;
6312 if (! EQ (d1->min_width, d2->min_width))
6313 return 0;
6314 if (! EQ (d1->min_height, d2->min_height))
6315 return 0;
6317 /* Verify that the two confis have the same number of windows. */
6318 if (sw1->size != sw2->size)
6319 return 0;
6321 for (i = 0; i < sw1->size; i++)
6323 struct saved_window *p1, *p2;
6324 int w1_is_current, w2_is_current;
6326 p1 = SAVED_WINDOW_N (sw1, i);
6327 p2 = SAVED_WINDOW_N (sw2, i);
6329 /* Verify that the current windows in the two
6330 configurations correspond to each other. */
6331 w1_is_current = EQ (d1->current_window, p1->window);
6332 w2_is_current = EQ (d2->current_window, p2->window);
6334 if (w1_is_current != w2_is_current)
6335 return 0;
6337 /* Verify that the corresponding windows do match. */
6338 if (! EQ (p1->buffer, p2->buffer))
6339 return 0;
6340 if (! EQ (p1->left_col, p2->left_col))
6341 return 0;
6342 if (! EQ (p1->top_line, p2->top_line))
6343 return 0;
6344 if (! EQ (p1->total_cols, p2->total_cols))
6345 return 0;
6346 if (! EQ (p1->total_lines, p2->total_lines))
6347 return 0;
6348 if (! EQ (p1->display_table, p2->display_table))
6349 return 0;
6350 if (! EQ (p1->parent, p2->parent))
6351 return 0;
6352 if (! EQ (p1->prev, p2->prev))
6353 return 0;
6354 if (! ignore_positions)
6356 if (! EQ (p1->hscroll, p2->hscroll))
6357 return 0;
6358 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6359 return 0;
6360 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6361 return 0;
6362 if (NILP (Fequal (p1->start, p2->start)))
6363 return 0;
6364 if (NILP (Fequal (p1->pointm, p2->pointm)))
6365 return 0;
6366 if (NILP (Fequal (p1->mark, p2->mark)))
6367 return 0;
6369 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6370 return 0;
6371 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6372 return 0;
6373 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6374 return 0;
6375 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6376 return 0;
6377 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6378 return 0;
6379 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6380 return 0;
6381 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6382 return 0;
6385 return 1;
6388 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6389 Scompare_window_configurations, 2, 2, 0,
6390 doc: /* Compare two window configurations as regards the structure of windows.
6391 This function ignores details such as the values of point and mark
6392 and scrolling positions. */)
6393 (x, y)
6394 Lisp_Object x, y;
6396 if (compare_window_configurations (x, y, 1))
6397 return Qt;
6398 return Qnil;
6401 void
6402 init_window_once ()
6404 struct frame *f = make_terminal_frame ();
6405 XSETFRAME (selected_frame, f);
6406 Vterminal_frame = selected_frame;
6407 minibuf_window = f->minibuffer_window;
6408 selected_window = f->selected_window;
6409 last_nonminibuf_frame = f;
6411 window_initialized = 1;
6414 void
6415 init_window ()
6417 Vwindow_list = Qnil;
6420 void
6421 syms_of_window ()
6423 Qwindow_size_fixed = intern ("window-size-fixed");
6424 staticpro (&Qwindow_size_fixed);
6426 staticpro (&Qwindow_configuration_change_hook);
6427 Qwindow_configuration_change_hook
6428 = intern ("window-configuration-change-hook");
6430 Qwindowp = intern ("windowp");
6431 staticpro (&Qwindowp);
6433 Qwindow_configuration_p = intern ("window-configuration-p");
6434 staticpro (&Qwindow_configuration_p);
6436 Qwindow_live_p = intern ("window-live-p");
6437 staticpro (&Qwindow_live_p);
6439 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
6440 staticpro (&Qtemp_buffer_show_hook);
6442 staticpro (&Vwindow_list);
6444 minibuf_selected_window = Qnil;
6445 staticpro (&minibuf_selected_window);
6447 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
6448 doc: /* Non-nil means call as function to display a help buffer.
6449 The function is called with one argument, the buffer to be displayed.
6450 Used by `with-output-to-temp-buffer'.
6451 If this function is used, then it must do the entire job of showing
6452 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6453 Vtemp_buffer_show_function = Qnil;
6455 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
6456 doc: /* If non-nil, function to call to handle `display-buffer'.
6457 It will receive two args, the buffer and a flag which if non-nil means
6458 that the currently selected window is not acceptable.
6459 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'
6460 work using this function. */);
6461 Vdisplay_buffer_function = Qnil;
6463 DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
6464 doc: /* *If non-nil, `display-buffer' should even the window heights.
6465 If nil, `display-buffer' will leave the window configuration alone. */);
6466 Veven_window_heights = Qt;
6468 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
6469 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6470 Vminibuf_scroll_window = Qnil;
6472 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
6473 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6474 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6475 is displayed in the `mode-line' face. */);
6476 mode_line_in_non_selected_windows = 1;
6478 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
6479 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6480 Vother_window_scroll_buffer = Qnil;
6482 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
6483 doc: /* *Non-nil means `display-buffer' should make a separate frame. */);
6484 pop_up_frames = 0;
6486 DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
6487 doc: /* *Non-nil means `display-buffer' should reuse frames.
6488 If the buffer in question is already displayed in a frame, raise that frame. */);
6489 display_buffer_reuse_frames = 0;
6491 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
6492 doc: /* Function to call to handle automatic new frame creation.
6493 It is called with no arguments and should return a newly created frame.
6495 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
6496 where `pop-up-frame-alist' would hold the default frame parameters. */);
6497 Vpop_up_frame_function = Qnil;
6499 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
6500 doc: /* *List of buffer names that should have their own special frames.
6501 Displaying a buffer whose name is in this list makes a special frame for it
6502 using `special-display-function'. See also `special-display-regexps'.
6504 An element of the list can be a list instead of just a string.
6505 There are two ways to use a list as an element:
6506 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
6507 In the first case, the FRAME-PARAMETERS are pairs of the form
6508 \(PARAMETER . VALUE); these parameter values are used to create the frame.
6509 In the second case, FUNCTION is called with BUFFER as the first argument,
6510 followed by the OTHER-ARGS--it can display BUFFER in any way it likes.
6511 All this is done by the function found in `special-display-function'.
6513 If the specified frame parameters include (same-buffer . t), the
6514 buffer is displayed in the currently selected window. Otherwise, if
6515 they include (same-frame . t), the buffer is displayed in a new window
6516 in the currently selected frame.
6518 If this variable appears \"not to work\", because you add a name to it
6519 but that buffer still appears in the selected window, look at the
6520 values of `same-window-buffer-names' and `same-window-regexps'.
6521 Those variables take precedence over this one. */);
6522 Vspecial_display_buffer_names = Qnil;
6524 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
6525 doc: /* *List of regexps saying which buffers should have their own special frames.
6526 If a buffer name matches one of these regexps, it gets its own frame.
6527 Displaying a buffer whose name is in this list makes a special frame for it
6528 using `special-display-function'.
6530 An element of the list can be a list instead of just a string.
6531 There are two ways to use a list as an element:
6532 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
6533 In the first case, the FRAME-PARAMETERS are pairs of the form
6534 \(PARAMETER . VALUE); these parameter values are used to create the frame.
6535 In the second case, FUNCTION is called with BUFFER as the first argument,
6536 followed by the OTHER-ARGS--it can display the buffer in any way it likes.
6537 All this is done by the function found in `special-display-function'.
6539 If the specified frame parameters include (same-buffer . t), the
6540 buffer is displayed in the currently selected window. Otherwise, if
6541 they include (same-frame . t), the buffer is displayed in a new window
6542 in the currently selected frame.
6544 If this variable appears \"not to work\", because you add a regexp to it
6545 but the matching buffers still appear in the selected window, look at the
6546 values of `same-window-buffer-names' and `same-window-regexps'.
6547 Those variables take precedence over this one. */);
6548 Vspecial_display_regexps = Qnil;
6550 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
6551 doc: /* Function to call to make a new frame for a special buffer.
6552 It is called with two arguments, the buffer and optional buffer specific
6553 data, and should return a window displaying that buffer.
6554 The default value normally makes a separate frame for the buffer,
6555 using `special-display-frame-alist' to specify the frame parameters.
6556 But if the buffer specific data includes (same-buffer . t) then the
6557 buffer is displayed in the current selected window.
6558 Otherwise if it includes (same-frame . t) then the buffer is displayed in
6559 a new window in the currently selected frame.
6561 A buffer is special if it is listed in `special-display-buffer-names'
6562 or matches a regexp in `special-display-regexps'. */);
6563 Vspecial_display_function = Qnil;
6565 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
6566 doc: /* *List of buffer names that should appear in the selected window.
6567 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
6568 switches to it in the selected window, rather than making it appear
6569 in some other window.
6571 An element of the list can be a cons cell instead of just a string.
6572 Then the car must be a string, which specifies the buffer name.
6573 This is for compatibility with `special-display-buffer-names';
6574 the cdr of the cons cell is ignored.
6576 See also `same-window-regexps'. */);
6577 Vsame_window_buffer_names = Qnil;
6579 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
6580 doc: /* *List of regexps saying which buffers should appear in the selected window.
6581 If a buffer name matches one of these regexps, then displaying it
6582 using `display-buffer' or `pop-to-buffer' switches to it
6583 in the selected window, rather than making it appear in some other window.
6585 An element of the list can be a cons cell instead of just a string.
6586 Then the car must be a string, which specifies the buffer name.
6587 This is for compatibility with `special-display-buffer-names';
6588 the cdr of the cons cell is ignored.
6590 See also `same-window-buffer-names'. */);
6591 Vsame_window_regexps = Qnil;
6593 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
6594 doc: /* *Non-nil means display-buffer should make new windows. */);
6595 pop_up_windows = 1;
6597 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
6598 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
6599 next_screen_context_lines = 2;
6601 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
6602 doc: /* *A window must be at least this tall to be eligible for splitting by `display-buffer'.
6603 If there is only one window, it is split regardless of this value. */);
6604 split_height_threshold = 500;
6606 DEFVAR_INT ("window-min-height", &window_min_height,
6607 doc: /* *Delete any window less than this tall (including its mode line). */);
6608 window_min_height = 4;
6610 DEFVAR_INT ("window-min-width", &window_min_width,
6611 doc: /* *Delete any window less than this wide. */);
6612 window_min_width = 10;
6614 DEFVAR_LISP ("scroll-preserve-screen-position",
6615 &Vscroll_preserve_screen_position,
6616 doc: /* *Non-nil means scroll commands move point to keep its screen line unchanged. */);
6617 Vscroll_preserve_screen_position = Qnil;
6619 DEFVAR_LISP ("window-configuration-change-hook",
6620 &Vwindow_configuration_change_hook,
6621 doc: /* Functions to call when window configuration changes.
6622 The selected frame is the one whose configuration has changed. */);
6623 Vwindow_configuration_change_hook = Qnil;
6625 DEFVAR_BOOL ("window-size-fixed", &window_size_fixed,
6626 doc: /* Non-nil in a buffer means windows displaying the buffer are fixed-size.
6627 Emacs won't change the size of any window displaying that buffer,
6628 unless you explicitly change the size, or Emacs has no other choice.
6629 This variable automatically becomes buffer-local when set. */);
6630 Fmake_variable_buffer_local (Qwindow_size_fixed);
6631 window_size_fixed = 0;
6633 defsubr (&Sselected_window);
6634 defsubr (&Sminibuffer_window);
6635 defsubr (&Swindow_minibuffer_p);
6636 defsubr (&Swindowp);
6637 defsubr (&Swindow_live_p);
6638 defsubr (&Spos_visible_in_window_p);
6639 defsubr (&Swindow_buffer);
6640 defsubr (&Swindow_height);
6641 defsubr (&Swindow_width);
6642 defsubr (&Swindow_hscroll);
6643 defsubr (&Sset_window_hscroll);
6644 defsubr (&Swindow_redisplay_end_trigger);
6645 defsubr (&Sset_window_redisplay_end_trigger);
6646 defsubr (&Swindow_edges);
6647 defsubr (&Swindow_pixel_edges);
6648 defsubr (&Swindow_inside_edges);
6649 defsubr (&Swindow_inside_pixel_edges);
6650 defsubr (&Scoordinates_in_window_p);
6651 defsubr (&Swindow_at);
6652 defsubr (&Swindow_point);
6653 defsubr (&Swindow_start);
6654 defsubr (&Swindow_end);
6655 defsubr (&Sset_window_point);
6656 defsubr (&Sset_window_start);
6657 defsubr (&Swindow_dedicated_p);
6658 defsubr (&Sset_window_dedicated_p);
6659 defsubr (&Swindow_display_table);
6660 defsubr (&Sset_window_display_table);
6661 defsubr (&Snext_window);
6662 defsubr (&Sprevious_window);
6663 defsubr (&Sother_window);
6664 defsubr (&Sget_lru_window);
6665 defsubr (&Sget_largest_window);
6666 defsubr (&Sget_buffer_window);
6667 defsubr (&Sdelete_other_windows);
6668 defsubr (&Sdelete_windows_on);
6669 defsubr (&Sreplace_buffer_in_windows);
6670 defsubr (&Sdelete_window);
6671 defsubr (&Sset_window_buffer);
6672 defsubr (&Sselect_window);
6673 defsubr (&Sspecial_display_p);
6674 defsubr (&Ssame_window_p);
6675 defsubr (&Sdisplay_buffer);
6676 defsubr (&Sforce_window_update);
6677 defsubr (&Ssplit_window);
6678 defsubr (&Senlarge_window);
6679 defsubr (&Sshrink_window);
6680 defsubr (&Sscroll_up);
6681 defsubr (&Sscroll_down);
6682 defsubr (&Sscroll_left);
6683 defsubr (&Sscroll_right);
6684 defsubr (&Sother_window_for_scrolling);
6685 defsubr (&Sscroll_other_window);
6686 defsubr (&Sminibuffer_selected_window);
6687 defsubr (&Srecenter);
6688 defsubr (&Swindow_text_height);
6689 defsubr (&Smove_to_window_line);
6690 defsubr (&Swindow_configuration_p);
6691 defsubr (&Swindow_configuration_frame);
6692 defsubr (&Sset_window_configuration);
6693 defsubr (&Scurrent_window_configuration);
6694 defsubr (&Ssave_window_excursion);
6695 defsubr (&Sset_window_margins);
6696 defsubr (&Swindow_margins);
6697 defsubr (&Sset_window_fringes);
6698 defsubr (&Swindow_fringes);
6699 defsubr (&Sset_window_scroll_bars);
6700 defsubr (&Swindow_scroll_bars);
6701 defsubr (&Swindow_vscroll);
6702 defsubr (&Sset_window_vscroll);
6703 defsubr (&Scompare_window_configurations);
6704 defsubr (&Swindow_list);
6707 void
6708 keys_of_window ()
6710 initial_define_key (control_x_map, '1', "delete-other-windows");
6711 initial_define_key (control_x_map, '2', "split-window");
6712 initial_define_key (control_x_map, '0', "delete-window");
6713 initial_define_key (control_x_map, 'o', "other-window");
6714 initial_define_key (control_x_map, '^', "enlarge-window");
6715 initial_define_key (control_x_map, '<', "scroll-left");
6716 initial_define_key (control_x_map, '>', "scroll-right");
6718 initial_define_key (global_map, Ctl ('V'), "scroll-up");
6719 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6720 initial_define_key (meta_map, 'v', "scroll-down");
6722 initial_define_key (global_map, Ctl('L'), "recenter");
6723 initial_define_key (meta_map, 'r', "move-to-window-line");
6726 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
6727 (do not change this comment) */