Update copyright.
[emacs.git] / src / window.c
blob8d56b330d93e744f87d6b4c5537aa324ca2744fb
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985,86,87,93,94,95,96,97,1998,2000, 2001, 2002, 2003
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;
283 Vwindow_list = Qnil;
284 return val;
287 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
288 doc: /* Return the window that the cursor now appears in and commands apply to. */)
291 return selected_window;
294 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
295 doc: /* Return the window used now for minibuffers.
296 If the optional argument FRAME is specified, return the minibuffer window
297 used by that frame. */)
298 (frame)
299 Lisp_Object frame;
301 if (NILP (frame))
302 frame = selected_frame;
303 CHECK_LIVE_FRAME (frame);
304 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
307 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
308 doc: /* Returns non-nil if WINDOW is a minibuffer window. */)
309 (window)
310 Lisp_Object window;
312 struct window *w = decode_window (window);
313 return MINI_WINDOW_P (w) ? Qt : Qnil;
317 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
318 Spos_visible_in_window_p, 0, 3, 0,
319 doc: /* Return t if position POS is currently on the frame in WINDOW.
320 Return nil if that position is scrolled vertically out of view.
321 If a character is only partially visible, nil is returned, unless the
322 optional argument PARTIALLY is non-nil.
323 POS defaults to point in WINDOW; WINDOW defaults to the selected window. */)
324 (pos, window, partially)
325 Lisp_Object pos, window, partially;
327 register struct window *w;
328 register int posint;
329 register struct buffer *buf;
330 struct text_pos top;
331 Lisp_Object in_window;
332 int fully_p;
334 w = decode_window (window);
335 buf = XBUFFER (w->buffer);
336 SET_TEXT_POS_FROM_MARKER (top, w->start);
338 if (!NILP (pos))
340 CHECK_NUMBER_COERCE_MARKER (pos);
341 posint = XINT (pos);
343 else if (w == XWINDOW (selected_window))
344 posint = PT;
345 else
346 posint = XMARKER (w->pointm)->charpos;
348 /* If position is above window start, it's not visible. */
349 if (posint < CHARPOS (top))
350 in_window = Qnil;
351 else if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
352 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf)
353 && posint < BUF_Z (buf) - XFASTINT (w->window_end_pos))
355 /* If frame is up-to-date, and POSINT is < window end pos, use
356 that info. This doesn't work for POSINT == end pos, because
357 the window end pos is actually the position _after_ the last
358 char in the window. */
359 if (NILP (partially))
361 pos_visible_p (w, posint, &fully_p, NILP (partially));
362 in_window = fully_p ? Qt : Qnil;
364 else
365 in_window = Qt;
367 else if (posint > BUF_ZV (buf))
368 in_window = Qnil;
369 else if (CHARPOS (top) < BUF_BEGV (buf) || CHARPOS (top) > BUF_ZV (buf))
370 /* If window start is out of range, do something reasonable. */
371 in_window = Qnil;
372 else
374 if (pos_visible_p (w, posint, &fully_p, NILP (partially)))
375 in_window = !NILP (partially) || fully_p ? Qt : Qnil;
376 else
377 in_window = Qnil;
380 return in_window;
384 static struct window *
385 decode_window (window)
386 register Lisp_Object window;
388 if (NILP (window))
389 return XWINDOW (selected_window);
391 CHECK_LIVE_WINDOW (window);
392 return XWINDOW (window);
395 static struct window *
396 decode_any_window (window)
397 register Lisp_Object window;
399 if (NILP (window))
400 return XWINDOW (selected_window);
402 CHECK_WINDOW (window);
403 return XWINDOW (window);
406 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
407 doc: /* Return the buffer that WINDOW is displaying. */)
408 (window)
409 Lisp_Object window;
411 return decode_window (window)->buffer;
414 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
415 doc: /* Return the number of lines in WINDOW (including its mode line). */)
416 (window)
417 Lisp_Object window;
419 return decode_any_window (window)->total_lines;
422 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
423 doc: /* Return the number of display columns in WINDOW.
424 This is the width that is usable columns available for text in WINDOW.
425 If you want to find out how many columns WINDOW takes up,
426 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
427 (window)
428 Lisp_Object window;
430 return make_number (window_box_text_cols (decode_any_window (window)));
433 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
434 doc: /* Return the number of columns by which WINDOW is scrolled from left margin. */)
435 (window)
436 Lisp_Object window;
438 return decode_window (window)->hscroll;
441 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
442 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
443 NCOL should be zero or positive.
445 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
446 window so that the location of point becomes invisible. */)
447 (window, ncol)
448 Lisp_Object window, ncol;
450 struct window *w = decode_window (window);
451 int hscroll;
453 CHECK_NUMBER (ncol);
454 hscroll = max (0, XINT (ncol));
456 /* Prevent redisplay shortcuts when changing the hscroll. */
457 if (XINT (w->hscroll) != hscroll)
458 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
460 w->hscroll = make_number (hscroll);
461 return ncol;
464 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
465 Swindow_redisplay_end_trigger, 0, 1, 0,
466 doc: /* Return WINDOW's redisplay end trigger value.
467 See `set-window-redisplay-end-trigger' for more information. */)
468 (window)
469 Lisp_Object window;
471 return decode_window (window)->redisplay_end_trigger;
474 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
475 Sset_window_redisplay_end_trigger, 2, 2, 0,
476 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
477 VALUE should be a buffer position (typically a marker) or nil.
478 If it is a buffer position, then if redisplay in WINDOW reaches a position
479 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
480 with two arguments: WINDOW, and the end trigger value.
481 Afterwards the end-trigger value is reset to nil. */)
482 (window, value)
483 register Lisp_Object window, value;
485 register struct window *w;
487 w = decode_window (window);
488 w->redisplay_end_trigger = value;
489 return value;
492 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
493 doc: /* Return a list of the edge coordinates of WINDOW.
494 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
495 RIGHT is one more than the rightmost column occupied by WINDOW,
496 and BOTTOM is one more than the bottommost row occupied by WINDOW.
497 The edges include the space used by the window's scroll bar,
498 display margins, fringes, header line, and mode line, if it has them.
499 To get the edges of the actual text area, use `window-inside-edges'. */)
500 (window)
501 Lisp_Object window;
503 register struct window *w = decode_any_window (window);
505 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
506 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
507 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
508 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
509 Qnil))));
512 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
513 doc: /* Return a list of the edge pixel coordinates of WINDOW.
514 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
515 RIGHT is one more than the rightmost x position occupied by WINDOW,
516 and BOTTOM is one more than the bottommost y position occupied by WINDOW.
517 The pixel edges include the space used by the window's scroll bar,
518 display margins, fringes, header line, and mode line, if it has them.
519 To get the edges of the actual text area, use `window-inside-pixel-edges'. */)
520 (window)
521 Lisp_Object window;
523 register struct window *w = decode_any_window (window);
525 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
526 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
527 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
528 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
529 Qnil))));
532 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
533 doc: /* Return a list of the edge coordinates of WINDOW.
534 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
535 RIGHT is one more than the rightmost column used by text in WINDOW,
536 and BOTTOM is one more than the bottommost row used by text in WINDOW.
537 The inside edges do not include the space used by the window's scroll bar,
538 display margins, fringes, header line, and/or mode line. */)
539 (window)
540 Lisp_Object window;
542 register struct window *w = decode_any_window (window);
544 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
545 + WINDOW_LEFT_MARGIN_COLS (w)
546 + WINDOW_LEFT_FRINGE_COLS (w)),
547 make_number (WINDOW_TOP_EDGE_LINE (w)
548 + WINDOW_HEADER_LINE_LINES (w)),
549 make_number (WINDOW_RIGHT_EDGE_COL (w)
550 - WINDOW_RIGHT_MARGIN_COLS (w)
551 - WINDOW_RIGHT_FRINGE_COLS (w)),
552 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
553 - WINDOW_MODE_LINE_LINES (w)));
556 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
557 doc: /* Return a list of the edge coordinates of WINDOW.
558 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
559 RIGHT is one more than the rightmost x position used by text in WINDOW,
560 and BOTTOM is one more than the bottommost y position used by text in WINDOW.
561 The inside edges do not include the space used by the window's scroll bar,
562 display margins, fringes, header line, and/or mode line. */)
563 (window)
564 Lisp_Object window;
566 register struct window *w = decode_any_window (window);
568 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
569 + WINDOW_LEFT_MARGIN_WIDTH (w)
570 + WINDOW_LEFT_FRINGE_WIDTH (w)),
571 make_number (WINDOW_TOP_EDGE_Y (w)
572 + WINDOW_HEADER_LINE_HEIGHT (w)),
573 make_number (WINDOW_RIGHT_EDGE_X (w)
574 - WINDOW_RIGHT_MARGIN_WIDTH (w)
575 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
576 make_number (WINDOW_BOTTOM_EDGE_Y (w)
577 - WINDOW_MODE_LINE_HEIGHT (w)));
580 /* Test if the character at column *X, row *Y is within window W.
581 If it is not, return ON_NOTHING;
582 if it is in the window's text area,
583 set *x and *y to its location relative to the upper left corner
584 of the window, and
585 return ON_TEXT;
586 if it is on the window's modeline, return ON_MODE_LINE;
587 if it is on the border between the window and its right sibling,
588 return ON_VERTICAL_BORDER.
589 if it is on the window's top line, return ON_HEADER_LINE;
590 if it is in left or right fringe of the window,
591 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
592 to window-relative coordinates;
593 if it is in the marginal area to the left/right of the window,
594 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
595 to window-relative coordinates.
597 X and Y are frame relative pixel coordinates. */
599 static enum window_part
600 coordinates_in_window (w, x, y)
601 register struct window *w;
602 register int *x, *y;
604 struct frame *f = XFRAME (WINDOW_FRAME (w));
605 int left_x, right_x, top_y, bottom_y;
606 enum window_part part;
607 int ux = FRAME_COLUMN_WIDTH (f);
608 int x0 = WINDOW_LEFT_EDGE_X (w);
609 int x1 = WINDOW_RIGHT_EDGE_X (w);
610 /* The width of the area where the vertical line can be dragged.
611 (Between mode lines for instance. */
612 int grabbable_width = ux;
613 int lmargin_width, rmargin_width, text_left, text_right;
615 if (*x < x0 || *x >= x1)
616 return ON_NOTHING;
618 /* In what's below, we subtract 1 when computing right_x because we
619 want the rightmost pixel, which is given by left_pixel+width-1. */
620 if (w->pseudo_window_p)
622 left_x = 0;
623 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
624 top_y = WINDOW_TOP_EDGE_Y (w);
625 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
627 else
629 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
630 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
631 top_y = WINDOW_TOP_EDGE_Y (w);
632 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
635 /* Outside any interesting row? */
636 if (*y < top_y || *y >= bottom_y)
637 return ON_NOTHING;
639 /* On the mode line or header line? If it's near the start of
640 the mode or header line of window that's has a horizontal
641 sibling, say it's on the vertical line. That's to be able
642 to resize windows horizontally in case we're using toolkit
643 scroll bars. */
645 if (WINDOW_WANTS_MODELINE_P (w)
646 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
648 part = ON_MODE_LINE;
650 header_vertical_border_check:
651 /* We're somewhere on the mode line. We consider the place
652 between mode lines of horizontally adjacent mode lines
653 as the vertical border. If scroll bars on the left,
654 return the right window. */
655 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
656 || WINDOW_RIGHTMOST_P (w))
658 if (!WINDOW_LEFTMOST_P (w) && abs (*x - x0) < grabbable_width)
659 return ON_VERTICAL_BORDER;
661 else
663 if (abs (*x - x1) < grabbable_width)
664 return ON_VERTICAL_BORDER;
667 /* Convert X and Y to window relative coordinates.
668 Mode line starts at left edge of window. */
669 *x -= x0;
670 *y -= top_y;
671 return part;
674 if (WINDOW_WANTS_HEADER_LINE_P (w)
675 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
677 part = ON_HEADER_LINE;
678 goto header_vertical_border_check;
681 /* Outside any interesting column? */
682 if (*x < left_x || *x > right_x)
683 return ON_NOTHING;
685 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
686 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
688 text_left = window_box_left (w, TEXT_AREA);
689 text_right = text_left + window_box_width (w, TEXT_AREA);
691 if (FRAME_WINDOW_P (f))
693 if (!w->pseudo_window_p
694 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
695 && !WINDOW_RIGHTMOST_P (w)
696 && (abs (*x - right_x) < grabbable_width))
697 return ON_VERTICAL_BORDER;
699 else
701 /* Need to say "*x > right_x" rather than >=, since on character
702 terminals, the vertical line's x coordinate is right_x. */
703 if (!w->pseudo_window_p
704 && !WINDOW_RIGHTMOST_P (w)
705 && *x > right_x - ux)
707 /* On the border on the right side of the window? Assume that
708 this area begins at RIGHT_X minus a canonical char width. */
709 return ON_VERTICAL_BORDER;
713 if (*x < text_left)
715 if (lmargin_width > 0
716 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
717 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
718 : (*x < left_x + lmargin_width)))
720 *x -= left_x;
721 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
722 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
723 *y -= top_y;
724 return ON_LEFT_MARGIN;
727 /* Convert X and Y to window-relative pixel coordinates. */
728 *x -= left_x;
729 *y -= top_y;
730 return ON_LEFT_FRINGE;
733 if (*x >= text_right)
735 if (rmargin_width > 0
736 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
737 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
738 : (*x >= right_x - rmargin_width)))
740 *x -= right_x;
741 if (!WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
742 *x -= WINDOW_RIGHT_FRINGE_WIDTH (w);
743 *y -= top_y;
744 return ON_RIGHT_MARGIN;
747 /* Convert X and Y to window-relative pixel coordinates. */
748 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
749 *y -= top_y;
750 return ON_RIGHT_FRINGE;
753 /* Everything special ruled out - must be on text area */
754 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
755 *y -= top_y;
756 return ON_TEXT;
760 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
761 Scoordinates_in_window_p, 2, 2, 0,
762 doc: /* Return non-nil if COORDINATES are in WINDOW.
763 COORDINATES is a cons of the form (X . Y), X and Y being distances
764 measured in characters from the upper-left corner of the frame.
765 \(0 . 0) denotes the character in the upper left corner of the
766 frame.
767 If COORDINATES are in the text portion of WINDOW,
768 the coordinates relative to the window are returned.
769 If they are in the mode line of WINDOW, `mode-line' is returned.
770 If they are in the top mode line of WINDOW, `header-line' is returned.
771 If they are in the left fringe of WINDOW, `left-fringe' is returned.
772 If they are in the right fringe of WINDOW, `right-fringe' is returned.
773 If they are on the border between WINDOW and its right sibling,
774 `vertical-line' is returned.
775 If they are in the windows's left or right marginal areas, `left-margin'\n\
776 or `right-margin' is returned. */)
777 (coordinates, window)
778 register Lisp_Object coordinates, window;
780 struct window *w;
781 struct frame *f;
782 int x, y;
783 Lisp_Object lx, ly;
785 CHECK_WINDOW (window);
786 w = XWINDOW (window);
787 f = XFRAME (w->frame);
788 CHECK_CONS (coordinates);
789 lx = Fcar (coordinates);
790 ly = Fcdr (coordinates);
791 CHECK_NUMBER_OR_FLOAT (lx);
792 CHECK_NUMBER_OR_FLOAT (ly);
793 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx);
794 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly);
796 switch (coordinates_in_window (w, &x, &y))
798 case ON_NOTHING:
799 return Qnil;
801 case ON_TEXT:
802 /* X and Y are now window relative pixel coordinates. Convert
803 them to canonical char units before returning them. */
804 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
805 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
807 case ON_MODE_LINE:
808 return Qmode_line;
810 case ON_VERTICAL_BORDER:
811 return Qvertical_line;
813 case ON_HEADER_LINE:
814 return Qheader_line;
816 case ON_LEFT_FRINGE:
817 return Qleft_fringe;
819 case ON_RIGHT_FRINGE:
820 return Qright_fringe;
822 case ON_LEFT_MARGIN:
823 return Qleft_margin;
825 case ON_RIGHT_MARGIN:
826 return Qright_margin;
828 default:
829 abort ();
834 /* Callback for foreach_window, used in window_from_coordinates.
835 Check if window W contains coordinates specified by USER_DATA which
836 is actually a pointer to a struct check_window_data CW.
838 Check if window W contains coordinates *CW->x and *CW->y. If it
839 does, return W in *CW->window, as Lisp_Object, and return in
840 *CW->part the part of the window under coordinates *X,*Y. Return
841 zero from this function to stop iterating over windows. */
843 struct check_window_data
845 Lisp_Object *window;
846 int *x, *y;
847 enum window_part *part;
850 static int
851 check_window_containing (w, user_data)
852 struct window *w;
853 void *user_data;
855 struct check_window_data *cw = (struct check_window_data *) user_data;
856 enum window_part found;
857 int continue_p = 1;
859 found = coordinates_in_window (w, cw->x, cw->y);
860 if (found != ON_NOTHING)
862 *cw->part = found;
863 XSETWINDOW (*cw->window, w);
864 continue_p = 0;
867 return continue_p;
871 /* Find the window containing frame-relative pixel position X/Y and
872 return it as a Lisp_Object.
874 If X, Y is on one of the window's special `window_part' elements,
875 set *PART to the id of that element, and return X and Y converted
876 to window relative coordinates in WX and WY.
878 If there is no window under X, Y return nil and leave *PART
879 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
881 This function was previously implemented with a loop cycling over
882 windows with Fnext_window, and starting with the frame's selected
883 window. It turned out that this doesn't work with an
884 implementation of next_window using Vwindow_list, because
885 FRAME_SELECTED_WINDOW (F) is not always contained in the window
886 tree of F when this function is called asynchronously from
887 note_mouse_highlight. The original loop didn't terminate in this
888 case. */
890 Lisp_Object
891 window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
892 struct frame *f;
893 int x, y;
894 enum window_part *part;
895 int *wx, *wy;
896 int tool_bar_p;
898 Lisp_Object window;
899 struct check_window_data cw;
900 enum window_part dummy;
902 if (part == 0)
903 part = &dummy;
905 window = Qnil;
906 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
907 foreach_window (f, check_window_containing, &cw);
909 /* If not found above, see if it's in the tool bar window, if a tool
910 bar exists. */
911 if (NILP (window)
912 && tool_bar_p
913 && WINDOWP (f->tool_bar_window)
914 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
915 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
916 != ON_NOTHING))
918 *part = ON_TEXT;
919 window = f->tool_bar_window;
922 if (wx) *wx = x;
923 if (wy) *wy = y;
925 return window;
928 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
929 doc: /* Return window containing coordinates X and Y on FRAME.
930 If omitted, FRAME defaults to the currently selected frame.
931 The top left corner of the frame is considered to be row 0,
932 column 0. */)
933 (x, y, frame)
934 Lisp_Object x, y, frame;
936 struct frame *f;
938 if (NILP (frame))
939 frame = selected_frame;
940 CHECK_LIVE_FRAME (frame);
941 f = XFRAME (frame);
943 /* Check that arguments are integers or floats. */
944 CHECK_NUMBER_OR_FLOAT (x);
945 CHECK_NUMBER_OR_FLOAT (y);
947 return window_from_coordinates (f,
948 FRAME_PIXEL_X_FROM_CANON_X (f, x),
949 FRAME_PIXEL_Y_FROM_CANON_Y (f, y),
950 0, 0, 0, 0);
953 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
954 doc: /* Return current value of point in WINDOW.
955 For a nonselected window, this is the value point would have
956 if that window were selected.
958 Note that, when WINDOW is the selected window and its buffer
959 is also currently selected, the value returned is the same as (point).
960 It would be more strictly correct to return the `top-level' value
961 of point, outside of any save-excursion forms.
962 But that is hard to define. */)
963 (window)
964 Lisp_Object window;
966 register struct window *w = decode_window (window);
968 if (w == XWINDOW (selected_window)
969 && current_buffer == XBUFFER (w->buffer))
970 return Fpoint ();
971 return Fmarker_position (w->pointm);
974 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
975 doc: /* Return position at which display currently starts in WINDOW.
976 This is updated by redisplay or by calling `set-window-start'. */)
977 (window)
978 Lisp_Object window;
980 return Fmarker_position (decode_window (window)->start);
983 /* This is text temporarily removed from the doc string below.
985 This function returns nil if the position is not currently known.
986 That happens when redisplay is preempted and doesn't finish.
987 If in that case you want to compute where the end of the window would
988 have been if redisplay had finished, do this:
989 (save-excursion
990 (goto-char (window-start window))
991 (vertical-motion (1- (window-height window)) window)
992 (point))") */
994 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
995 doc: /* Return position at which display currently ends in WINDOW.
996 This is updated by redisplay, when it runs to completion.
997 Simply changing the buffer text or setting `window-start'
998 does not update this value.
999 If UPDATE is non-nil, compute the up-to-date position
1000 if it isn't already recorded. */)
1001 (window, update)
1002 Lisp_Object window, update;
1004 Lisp_Object value;
1005 struct window *w = decode_window (window);
1006 Lisp_Object buf;
1008 buf = w->buffer;
1009 CHECK_BUFFER (buf);
1011 #if 0 /* This change broke some things. We should make it later. */
1012 /* If we don't know the end position, return nil.
1013 The user can compute it with vertical-motion if he wants to.
1014 It would be nicer to do it automatically,
1015 but that's so slow that it would probably bother people. */
1016 if (NILP (w->window_end_valid))
1017 return Qnil;
1018 #endif
1020 if (! NILP (update)
1021 && ! (! NILP (w->window_end_valid)
1022 && XFASTINT (w->last_modified) >= MODIFF))
1024 struct text_pos startp;
1025 struct it it;
1026 struct buffer *old_buffer = NULL, *b = XBUFFER (buf);
1028 /* In case W->start is out of the range, use something
1029 reasonable. This situation occurred when loading a file with
1030 `-l' containing a call to `rmail' with subsequent other
1031 commands. At the end, W->start happened to be BEG, while
1032 rmail had already narrowed the buffer. */
1033 if (XMARKER (w->start)->charpos < BEGV)
1034 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1035 else if (XMARKER (w->start)->charpos > ZV)
1036 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1037 else
1038 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1040 /* Cannot use Fvertical_motion because that function doesn't
1041 cope with variable-height lines. */
1042 if (b != current_buffer)
1044 old_buffer = current_buffer;
1045 set_buffer_internal (b);
1048 start_display (&it, w, startp);
1049 move_it_vertically (&it, window_box_height (w));
1050 if (it.current_y < it.last_visible_y)
1051 move_it_past_eol (&it);
1052 value = make_number (IT_CHARPOS (it));
1054 if (old_buffer)
1055 set_buffer_internal (old_buffer);
1057 else
1058 XSETINT (value, BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
1060 return value;
1063 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1064 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer. */)
1065 (window, pos)
1066 Lisp_Object window, pos;
1068 register struct window *w = decode_window (window);
1070 CHECK_NUMBER_COERCE_MARKER (pos);
1071 if (w == XWINDOW (selected_window)
1072 && XBUFFER (w->buffer) == current_buffer)
1073 Fgoto_char (pos);
1074 else
1075 set_marker_restricted (w->pointm, pos, w->buffer);
1077 /* We have to make sure that redisplay updates the window to show
1078 the new value of point. */
1079 if (!EQ (window, selected_window))
1080 ++windows_or_buffers_changed;
1082 return pos;
1085 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1086 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1087 Optional third arg NOFORCE non-nil inhibits next redisplay
1088 from overriding motion of point in order to display at this exact start. */)
1089 (window, pos, noforce)
1090 Lisp_Object window, pos, noforce;
1092 register struct window *w = decode_window (window);
1094 CHECK_NUMBER_COERCE_MARKER (pos);
1095 set_marker_restricted (w->start, pos, w->buffer);
1096 /* this is not right, but much easier than doing what is right. */
1097 w->start_at_line_beg = Qnil;
1098 if (NILP (noforce))
1099 w->force_start = Qt;
1100 w->update_mode_line = Qt;
1101 XSETFASTINT (w->last_modified, 0);
1102 XSETFASTINT (w->last_overlay_modified, 0);
1103 if (!EQ (window, selected_window))
1104 windows_or_buffers_changed++;
1106 return pos;
1109 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1110 1, 1, 0,
1111 doc: /* Return WINDOW's dedicated object, usually t or nil.
1112 See also `set-window-dedicated-p'. */)
1113 (window)
1114 Lisp_Object window;
1116 return decode_window (window)->dedicated;
1119 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1120 Sset_window_dedicated_p, 2, 2, 0,
1121 doc: /* Control whether WINDOW is dedicated to the buffer it displays.
1122 If it is dedicated, Emacs will not automatically change
1123 which buffer appears in it.
1124 The second argument is the new value for the dedication flag;
1125 non-nil means yes. */)
1126 (window, arg)
1127 Lisp_Object window, arg;
1129 register struct window *w = decode_window (window);
1131 w->dedicated = arg;
1133 return w->dedicated;
1136 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1137 0, 1, 0,
1138 doc: /* Return the display-table that WINDOW is using. */)
1139 (window)
1140 Lisp_Object window;
1142 return decode_window (window)->display_table;
1145 /* Get the display table for use on window W. This is either W's
1146 display table or W's buffer's display table. Ignore the specified
1147 tables if they are not valid; if no valid table is specified,
1148 return 0. */
1150 struct Lisp_Char_Table *
1151 window_display_table (w)
1152 struct window *w;
1154 struct Lisp_Char_Table *dp = NULL;
1156 if (DISP_TABLE_P (w->display_table))
1157 dp = XCHAR_TABLE (w->display_table);
1158 else if (BUFFERP (w->buffer))
1160 struct buffer *b = XBUFFER (w->buffer);
1162 if (DISP_TABLE_P (b->display_table))
1163 dp = XCHAR_TABLE (b->display_table);
1164 else if (DISP_TABLE_P (Vstandard_display_table))
1165 dp = XCHAR_TABLE (Vstandard_display_table);
1168 return dp;
1171 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1172 doc: /* Set WINDOW's display-table to TABLE. */)
1173 (window, table)
1174 register Lisp_Object window, table;
1176 register struct window *w;
1178 w = decode_window (window);
1179 w->display_table = table;
1180 return table;
1183 /* Record info on buffer window w is displaying
1184 when it is about to cease to display that buffer. */
1185 static void
1186 unshow_buffer (w)
1187 register struct window *w;
1189 Lisp_Object buf;
1190 struct buffer *b;
1192 buf = w->buffer;
1193 b = XBUFFER (buf);
1194 if (b != XMARKER (w->pointm)->buffer)
1195 abort ();
1197 #if 0
1198 if (w == XWINDOW (selected_window)
1199 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1200 /* Do this except when the selected window's buffer
1201 is being removed from some other window. */
1202 #endif
1203 /* last_window_start records the start position that this buffer
1204 had in the last window to be disconnected from it.
1205 Now that this statement is unconditional,
1206 it is possible for the buffer to be displayed in the
1207 selected window, while last_window_start reflects another
1208 window which was recently showing the same buffer.
1209 Some people might say that might be a good thing. Let's see. */
1210 b->last_window_start = marker_position (w->start);
1212 /* Point in the selected window's buffer
1213 is actually stored in that buffer, and the window's pointm isn't used.
1214 So don't clobber point in that buffer. */
1215 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1216 /* This line helps to fix Horsley's testbug.el bug. */
1217 && !(WINDOWP (b->last_selected_window)
1218 && w != XWINDOW (b->last_selected_window)
1219 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1220 temp_set_point_both (b,
1221 clip_to_bounds (BUF_BEGV (b),
1222 XMARKER (w->pointm)->charpos,
1223 BUF_ZV (b)),
1224 clip_to_bounds (BUF_BEGV_BYTE (b),
1225 marker_byte_position (w->pointm),
1226 BUF_ZV_BYTE (b)));
1228 if (WINDOWP (b->last_selected_window)
1229 && w == XWINDOW (b->last_selected_window))
1230 b->last_selected_window = Qnil;
1233 /* Put replacement into the window structure in place of old. */
1234 static void
1235 replace_window (old, replacement)
1236 Lisp_Object old, replacement;
1238 register Lisp_Object tem;
1239 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1241 /* If OLD is its frame's root_window, then replacement is the new
1242 root_window for that frame. */
1244 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1245 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1247 p->left_col = o->left_col;
1248 p->top_line = o->top_line;
1249 p->total_cols = o->total_cols;
1250 p->total_lines = o->total_lines;
1251 p->desired_matrix = p->current_matrix = 0;
1252 p->vscroll = 0;
1253 bzero (&p->cursor, sizeof (p->cursor));
1254 bzero (&p->last_cursor, sizeof (p->last_cursor));
1255 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1256 p->phys_cursor_type = -1;
1257 p->phys_cursor_width = -1;
1258 p->must_be_updated_p = 0;
1259 p->pseudo_window_p = 0;
1260 XSETFASTINT (p->window_end_vpos, 0);
1261 XSETFASTINT (p->window_end_pos, 0);
1262 p->window_end_valid = Qnil;
1263 p->frozen_window_start_p = 0;
1264 p->orig_top_line = p->orig_total_lines = Qnil;
1266 p->next = tem = o->next;
1267 if (!NILP (tem))
1268 XWINDOW (tem)->prev = replacement;
1270 p->prev = tem = o->prev;
1271 if (!NILP (tem))
1272 XWINDOW (tem)->next = replacement;
1274 p->parent = tem = o->parent;
1275 if (!NILP (tem))
1277 if (EQ (XWINDOW (tem)->vchild, old))
1278 XWINDOW (tem)->vchild = replacement;
1279 if (EQ (XWINDOW (tem)->hchild, old))
1280 XWINDOW (tem)->hchild = replacement;
1283 /*** Here, if replacement is a vertical combination
1284 and so is its new parent, we should make replacement's
1285 children be children of that parent instead. ***/
1288 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1289 doc: /* Remove WINDOW from the display. Default is selected window. */)
1290 (window)
1291 register Lisp_Object window;
1293 delete_window (window);
1295 if (! NILP (Vwindow_configuration_change_hook)
1296 && ! NILP (Vrun_hooks))
1297 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1299 return Qnil;
1302 void
1303 delete_window (window)
1304 register Lisp_Object window;
1306 register Lisp_Object tem, parent, sib;
1307 register struct window *p;
1308 register struct window *par;
1309 struct frame *f;
1311 /* Because this function is called by other C code on non-leaf
1312 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1313 so we can't decode_window here. */
1314 if (NILP (window))
1315 window = selected_window;
1316 else
1317 CHECK_WINDOW (window);
1318 p = XWINDOW (window);
1320 /* It's okay to delete an already-deleted window. */
1321 if (NILP (p->buffer)
1322 && NILP (p->hchild)
1323 && NILP (p->vchild))
1324 return;
1326 parent = p->parent;
1327 if (NILP (parent))
1328 error ("Attempt to delete minibuffer or sole ordinary window");
1329 par = XWINDOW (parent);
1331 windows_or_buffers_changed++;
1332 Vwindow_list = Qnil;
1333 f = XFRAME (WINDOW_FRAME (p));
1334 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1336 /* Are we trying to delete any frame's selected window? */
1338 Lisp_Object swindow, pwindow;
1340 /* See if the frame's selected window is either WINDOW
1341 or any subwindow of it, by finding all that window's parents
1342 and comparing each one with WINDOW. */
1343 swindow = FRAME_SELECTED_WINDOW (f);
1345 while (1)
1347 pwindow = swindow;
1348 while (!NILP (pwindow))
1350 if (EQ (window, pwindow))
1351 break;
1352 pwindow = XWINDOW (pwindow)->parent;
1355 /* If the window being deleted is not a parent of SWINDOW,
1356 then SWINDOW is ok as the new selected window. */
1357 if (!EQ (window, pwindow))
1358 break;
1359 /* Otherwise, try another window for SWINDOW. */
1360 swindow = Fnext_window (swindow, Qlambda, Qnil);;
1362 /* If we get back to the frame's selected window,
1363 it means there was no acceptable alternative,
1364 so we cannot delete. */
1365 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1366 error ("Cannot delete window");
1369 /* If we need to change SWINDOW, do it. */
1370 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1372 /* If we're about to delete the selected window on the
1373 selected frame, then we should use Fselect_window to select
1374 the new window. On the other hand, if we're about to
1375 delete the selected window on any other frame, we shouldn't do
1376 anything but set the frame's selected_window slot. */
1377 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1378 Fselect_window (swindow, Qnil);
1379 else
1380 FRAME_SELECTED_WINDOW (f) = swindow;
1384 tem = p->buffer;
1385 /* tem is null for dummy parent windows
1386 (which have inferiors but not any contents themselves) */
1387 if (!NILP (tem))
1389 unshow_buffer (p);
1390 unchain_marker (XMARKER (p->pointm));
1391 unchain_marker (XMARKER (p->start));
1394 /* Free window glyph matrices. It is sure that they are allocated
1395 again when ADJUST_GLYPHS is called. Block input so that expose
1396 events and other events that access glyph matrices are not
1397 processed while we are changing them. */
1398 BLOCK_INPUT;
1399 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1401 tem = p->next;
1402 if (!NILP (tem))
1403 XWINDOW (tem)->prev = p->prev;
1405 tem = p->prev;
1406 if (!NILP (tem))
1407 XWINDOW (tem)->next = p->next;
1409 if (EQ (window, par->hchild))
1410 par->hchild = p->next;
1411 if (EQ (window, par->vchild))
1412 par->vchild = p->next;
1414 /* Find one of our siblings to give our space to. */
1415 sib = p->prev;
1416 if (NILP (sib))
1418 /* If p gives its space to its next sibling, that sibling needs
1419 to have its top/left side pulled back to where p's is.
1420 set_window_{height,width} will re-position the sibling's
1421 children. */
1422 sib = p->next;
1423 XWINDOW (sib)->top_line = p->top_line;
1424 XWINDOW (sib)->left_col = p->left_col;
1427 /* Stretch that sibling. */
1428 if (!NILP (par->vchild))
1429 set_window_height (sib,
1430 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1432 if (!NILP (par->hchild))
1433 set_window_width (sib,
1434 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1437 /* If parent now has only one child,
1438 put the child into the parent's place. */
1439 tem = par->hchild;
1440 if (NILP (tem))
1441 tem = par->vchild;
1442 if (NILP (XWINDOW (tem)->next))
1443 replace_window (parent, tem);
1445 /* Since we may be deleting combination windows, we must make sure that
1446 not only p but all its children have been marked as deleted. */
1447 if (! NILP (p->hchild))
1448 delete_all_subwindows (XWINDOW (p->hchild));
1449 else if (! NILP (p->vchild))
1450 delete_all_subwindows (XWINDOW (p->vchild));
1452 /* Mark this window as deleted. */
1453 p->buffer = p->hchild = p->vchild = Qnil;
1455 /* Adjust glyph matrices. */
1456 adjust_glyphs (f);
1457 UNBLOCK_INPUT;
1462 /***********************************************************************
1463 Window List
1464 ***********************************************************************/
1466 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1467 pointer. This is a callback function for foreach_window, used in
1468 function window_list. */
1470 static int
1471 add_window_to_list (w, user_data)
1472 struct window *w;
1473 void *user_data;
1475 Lisp_Object *list = (Lisp_Object *) user_data;
1476 Lisp_Object window;
1477 XSETWINDOW (window, w);
1478 *list = Fcons (window, *list);
1479 return 1;
1483 /* Return a list of all windows, for use by next_window. If
1484 Vwindow_list is a list, return that list. Otherwise, build a new
1485 list, cache it in Vwindow_list, and return that. */
1487 static Lisp_Object
1488 window_list ()
1490 if (!CONSP (Vwindow_list))
1492 Lisp_Object tail;
1494 Vwindow_list = Qnil;
1495 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1497 Lisp_Object args[2];
1499 /* We are visiting windows in canonical order, and add
1500 new windows at the front of args[1], which means we
1501 have to reverse this list at the end. */
1502 args[1] = Qnil;
1503 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1504 args[0] = Vwindow_list;
1505 args[1] = Fnreverse (args[1]);
1506 Vwindow_list = Fnconc (2, args);
1510 return Vwindow_list;
1514 /* Value is non-zero if WINDOW satisfies the constraints given by
1515 OWINDOW, MINIBUF and ALL_FRAMES.
1517 MINIBUF t means WINDOW may be minibuffer windows.
1518 `lambda' means WINDOW may not be a minibuffer window.
1519 a window means a specific minibuffer window
1521 ALL_FRAMES t means search all frames,
1522 nil means search just current frame,
1523 `visible' means search just visible frames,
1524 0 means search visible and iconified frames,
1525 a window means search the frame that window belongs to,
1526 a frame means consider windows on that frame, only. */
1528 static int
1529 candidate_window_p (window, owindow, minibuf, all_frames)
1530 Lisp_Object window, owindow, minibuf, all_frames;
1532 struct window *w = XWINDOW (window);
1533 struct frame *f = XFRAME (w->frame);
1534 int candidate_p = 1;
1536 if (!BUFFERP (w->buffer))
1537 candidate_p = 0;
1538 else if (MINI_WINDOW_P (w)
1539 && (EQ (minibuf, Qlambda)
1540 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1542 /* If MINIBUF is `lambda' don't consider any mini-windows.
1543 If it is a window, consider only that one. */
1544 candidate_p = 0;
1546 else if (EQ (all_frames, Qt))
1547 candidate_p = 1;
1548 else if (NILP (all_frames))
1550 xassert (WINDOWP (owindow));
1551 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1553 else if (EQ (all_frames, Qvisible))
1555 FRAME_SAMPLE_VISIBILITY (f);
1556 candidate_p = FRAME_VISIBLE_P (f);
1558 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1560 FRAME_SAMPLE_VISIBILITY (f);
1561 candidate_p = FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1563 else if (WINDOWP (all_frames))
1564 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1565 || EQ (XWINDOW (all_frames)->frame, w->frame)
1566 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1567 else if (FRAMEP (all_frames))
1568 candidate_p = EQ (all_frames, w->frame);
1570 return candidate_p;
1574 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1575 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1576 ALL_FRAMES. */
1578 static void
1579 decode_next_window_args (window, minibuf, all_frames)
1580 Lisp_Object *window, *minibuf, *all_frames;
1582 if (NILP (*window))
1583 *window = selected_window;
1584 else
1585 CHECK_LIVE_WINDOW (*window);
1587 /* MINIBUF nil may or may not include minibuffers. Decide if it
1588 does. */
1589 if (NILP (*minibuf))
1590 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1591 else if (!EQ (*minibuf, Qt))
1592 *minibuf = Qlambda;
1594 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1595 => count none of them, or a specific minibuffer window (the
1596 active one) to count. */
1598 /* ALL_FRAMES nil doesn't specify which frames to include. */
1599 if (NILP (*all_frames))
1600 *all_frames = (!EQ (*minibuf, Qlambda)
1601 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1602 : Qnil);
1603 else if (EQ (*all_frames, Qvisible))
1605 else if (XFASTINT (*all_frames) == 0)
1607 else if (FRAMEP (*all_frames))
1609 else if (!EQ (*all_frames, Qt))
1610 *all_frames = Qnil;
1612 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1613 search just current frame, `visible' meaning search just visible
1614 frames, 0 meaning search visible and iconified frames, or a
1615 window, meaning search the frame that window belongs to, or a
1616 frame, meaning consider windows on that frame, only. */
1620 /* Return the next or previous window of WINDOW in canonical ordering
1621 of windows. NEXT_P non-zero means return the next window. See the
1622 documentation string of next-window for the meaning of MINIBUF and
1623 ALL_FRAMES. */
1625 static Lisp_Object
1626 next_window (window, minibuf, all_frames, next_p)
1627 Lisp_Object window, minibuf, all_frames;
1628 int next_p;
1630 decode_next_window_args (&window, &minibuf, &all_frames);
1632 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1633 return the first window on the frame. */
1634 if (FRAMEP (all_frames)
1635 && !EQ (all_frames, XWINDOW (window)->frame))
1636 return Fframe_first_window (all_frames);
1638 if (next_p)
1640 Lisp_Object list;
1642 /* Find WINDOW in the list of all windows. */
1643 list = Fmemq (window, window_list ());
1645 /* Scan forward from WINDOW to the end of the window list. */
1646 if (CONSP (list))
1647 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1648 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1649 break;
1651 /* Scan from the start of the window list up to WINDOW. */
1652 if (!CONSP (list))
1653 for (list = Vwindow_list;
1654 CONSP (list) && !EQ (XCAR (list), window);
1655 list = XCDR (list))
1656 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1657 break;
1659 if (CONSP (list))
1660 window = XCAR (list);
1662 else
1664 Lisp_Object candidate, list;
1666 /* Scan through the list of windows for candidates. If there are
1667 candidate windows in front of WINDOW, the last one of these
1668 is the one we want. If there are candidates following WINDOW
1669 in the list, again the last one of these is the one we want. */
1670 candidate = Qnil;
1671 for (list = window_list (); CONSP (list); list = XCDR (list))
1673 if (EQ (XCAR (list), window))
1675 if (WINDOWP (candidate))
1676 break;
1678 else if (candidate_window_p (XCAR (list), window, minibuf,
1679 all_frames))
1680 candidate = XCAR (list);
1683 if (WINDOWP (candidate))
1684 window = candidate;
1687 return window;
1691 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1692 doc: /* Return next window after WINDOW in canonical ordering of windows.
1693 If omitted, WINDOW defaults to the selected window.
1695 Optional second arg MINIBUF t means count the minibuffer window even
1696 if not active. MINIBUF nil or omitted means count the minibuffer iff
1697 it is active. MINIBUF neither t nor nil means not to count the
1698 minibuffer even if it is active.
1700 Several frames may share a single minibuffer; if the minibuffer
1701 counts, all windows on all frames that share that minibuffer count
1702 too. Therefore, `next-window' can be used to iterate through the
1703 set of windows even when the minibuffer is on another frame. If the
1704 minibuffer does not count, only windows from WINDOW's frame count.
1706 Optional third arg ALL-FRAMES t means include windows on all frames.
1707 ALL-FRAMES nil or omitted means cycle within the frames as specified
1708 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1709 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1710 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1711 Anything else means restrict to WINDOW's frame.
1713 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1714 `next-window' to iterate through the entire cycle of acceptable
1715 windows, eventually ending up back at the window you started with.
1716 `previous-window' traverses the same cycle, in the reverse order. */)
1717 (window, minibuf, all_frames)
1718 Lisp_Object window, minibuf, all_frames;
1720 return next_window (window, minibuf, all_frames, 1);
1724 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1725 doc: /* Return the window preceding WINDOW in canonical ordering of windows.
1726 If omitted, WINDOW defaults to the selected window.
1728 Optional second arg MINIBUF t means count the minibuffer window even
1729 if not active. MINIBUF nil or omitted means count the minibuffer iff
1730 it is active. MINIBUF neither t nor nil means not to count the
1731 minibuffer even if it is active.
1733 Several frames may share a single minibuffer; if the minibuffer
1734 counts, all windows on all frames that share that minibuffer count
1735 too. Therefore, `previous-window' can be used to iterate through
1736 the set of windows even when the minibuffer is on another frame. If
1737 the minibuffer does not count, only windows from WINDOW's frame count
1739 Optional third arg ALL-FRAMES t means include windows on all frames.
1740 ALL-FRAMES nil or omitted means cycle within the frames as specified
1741 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1742 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1743 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1744 Anything else means restrict to WINDOW's frame.
1746 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1747 `previous-window' to iterate through the entire cycle of acceptable
1748 windows, eventually ending up back at the window you started with.
1749 `next-window' traverses the same cycle, in the reverse order. */)
1750 (window, minibuf, all_frames)
1751 Lisp_Object window, minibuf, all_frames;
1753 return next_window (window, minibuf, all_frames, 0);
1757 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1758 doc: /* Select the ARG'th different window on this frame.
1759 All windows on current frame are arranged in a cyclic order.
1760 This command selects the window ARG steps away in that order.
1761 A negative ARG moves in the opposite order. If the optional second
1762 argument ALL_FRAMES is non-nil, cycle through all frames. */)
1763 (arg, all_frames)
1764 Lisp_Object arg, all_frames;
1766 Lisp_Object window;
1767 int i;
1769 CHECK_NUMBER (arg);
1770 window = selected_window;
1772 for (i = XINT (arg); i > 0; --i)
1773 window = Fnext_window (window, Qnil, all_frames);
1774 for (; i < 0; ++i)
1775 window = Fprevious_window (window, Qnil, all_frames);
1777 Fselect_window (window, Qnil);
1778 return Qnil;
1782 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
1783 doc: /* Return a list of windows on FRAME, starting with WINDOW.
1784 FRAME nil or omitted means use the selected frame.
1785 WINDOW nil or omitted means use the selected window.
1786 MINIBUF t means include the minibuffer window, even if it isn't active.
1787 MINIBUF nil or omitted means include the minibuffer window only
1788 if it's active.
1789 MINIBUF neither nil nor t means never include the minibuffer window. */)
1790 (frame, minibuf, window)
1791 Lisp_Object frame, minibuf, window;
1793 if (NILP (window))
1794 window = selected_window;
1795 if (NILP (frame))
1796 frame = selected_frame;
1798 if (!EQ (frame, XWINDOW (window)->frame))
1799 error ("Window is on a different frame");
1801 return window_list_1 (window, minibuf, frame);
1805 /* Return a list of windows in canonical ordering. Arguments are like
1806 for `next-window'. */
1808 static Lisp_Object
1809 window_list_1 (window, minibuf, all_frames)
1810 Lisp_Object window, minibuf, all_frames;
1812 Lisp_Object tail, list;
1814 decode_next_window_args (&window, &minibuf, &all_frames);
1815 list = Qnil;
1817 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
1818 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
1819 list = Fcons (XCAR (tail), list);
1821 return Fnreverse (list);
1826 /* Look at all windows, performing an operation specified by TYPE
1827 with argument OBJ.
1828 If FRAMES is Qt, look at all frames;
1829 Qnil, look at just the selected frame;
1830 Qvisible, look at visible frames;
1831 a frame, just look at windows on that frame.
1832 If MINI is non-zero, perform the operation on minibuffer windows too. */
1834 enum window_loop
1836 WINDOW_LOOP_UNUSED,
1837 GET_BUFFER_WINDOW, /* Arg is buffer */
1838 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1839 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1840 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1841 GET_LARGEST_WINDOW,
1842 UNSHOW_BUFFER, /* Arg is buffer */
1843 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
1844 CHECK_ALL_WINDOWS
1847 static Lisp_Object
1848 window_loop (type, obj, mini, frames)
1849 enum window_loop type;
1850 Lisp_Object obj, frames;
1851 int mini;
1853 Lisp_Object window, windows, best_window, frame_arg;
1854 struct frame *f;
1855 struct gcpro gcpro1;
1857 /* If we're only looping through windows on a particular frame,
1858 frame points to that frame. If we're looping through windows
1859 on all frames, frame is 0. */
1860 if (FRAMEP (frames))
1861 f = XFRAME (frames);
1862 else if (NILP (frames))
1863 f = SELECTED_FRAME ();
1864 else
1865 f = NULL;
1867 if (f)
1868 frame_arg = Qlambda;
1869 else if (XFASTINT (frames) == 0)
1870 frame_arg = frames;
1871 else if (EQ (frames, Qvisible))
1872 frame_arg = frames;
1873 else
1874 frame_arg = Qt;
1876 /* frame_arg is Qlambda to stick to one frame,
1877 Qvisible to consider all visible frames,
1878 or Qt otherwise. */
1880 /* Pick a window to start with. */
1881 if (WINDOWP (obj))
1882 window = obj;
1883 else if (f)
1884 window = FRAME_SELECTED_WINDOW (f);
1885 else
1886 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
1888 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
1889 GCPRO1 (windows);
1890 best_window = Qnil;
1892 for (; CONSP (windows); windows = CDR (windows))
1894 struct window *w;
1896 window = XCAR (windows);
1897 w = XWINDOW (window);
1899 /* Note that we do not pay attention here to whether the frame
1900 is visible, since Fwindow_list skips non-visible frames if
1901 that is desired, under the control of frame_arg. */
1902 if (!MINI_WINDOW_P (w)
1903 /* For UNSHOW_BUFFER, we must always consider all windows. */
1904 || type == UNSHOW_BUFFER
1905 || (mini && minibuf_level > 0))
1906 switch (type)
1908 case GET_BUFFER_WINDOW:
1909 if (EQ (w->buffer, obj)
1910 /* Don't find any minibuffer window
1911 except the one that is currently in use. */
1912 && (MINI_WINDOW_P (w)
1913 ? EQ (window, minibuf_window)
1914 : 1))
1916 if (NILP (best_window))
1917 best_window = window;
1918 else if (EQ (window, selected_window))
1919 /* For compatibility with 20.x, prefer to return
1920 selected-window. */
1921 best_window = window;
1923 break;
1925 case GET_LRU_WINDOW:
1926 /* t as arg means consider only full-width windows */
1927 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (w))
1928 break;
1929 /* Ignore dedicated windows and minibuffers. */
1930 if (MINI_WINDOW_P (w) || EQ (w->dedicated, Qt))
1931 break;
1932 if (NILP (best_window)
1933 || (XFASTINT (XWINDOW (best_window)->use_time)
1934 > XFASTINT (w->use_time)))
1935 best_window = window;
1936 break;
1938 case DELETE_OTHER_WINDOWS:
1939 if (!EQ (window, obj))
1940 Fdelete_window (window);
1941 break;
1943 case DELETE_BUFFER_WINDOWS:
1944 if (EQ (w->buffer, obj))
1946 struct frame *f = XFRAME (WINDOW_FRAME (w));
1948 /* If this window is dedicated, and in a frame of its own,
1949 kill the frame. */
1950 if (EQ (window, FRAME_ROOT_WINDOW (f))
1951 && !NILP (w->dedicated)
1952 && other_visible_frames (f))
1954 /* Skip the other windows on this frame.
1955 There might be one, the minibuffer! */
1956 while (CONSP (XCDR (windows))
1957 && EQ (XWINDOW (XCAR (windows))->frame,
1958 XWINDOW (XCAR (XCDR (windows)))->frame))
1959 windows = XCDR (windows);
1961 /* Now we can safely delete the frame. */
1962 Fdelete_frame (w->frame, Qnil);
1964 else if (NILP (w->parent))
1966 /* If we're deleting the buffer displayed in the
1967 only window on the frame, find a new buffer to
1968 display there. */
1969 Lisp_Object buffer;
1970 buffer = Fother_buffer (obj, Qnil, w->frame);
1971 Fset_window_buffer (window, buffer, Qnil);
1972 if (EQ (window, selected_window))
1973 Fset_buffer (w->buffer);
1975 else
1976 Fdelete_window (window);
1978 break;
1980 case GET_LARGEST_WINDOW:
1982 /* Ignore dedicated windows and minibuffers. */
1983 if (MINI_WINDOW_P (w) || EQ (w->dedicated, Qt))
1984 break;
1986 if (NILP (best_window))
1987 best_window = window;
1988 else
1990 struct window *b = XWINDOW (best_window);
1991 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
1992 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
1993 best_window = window;
1996 break;
1998 case UNSHOW_BUFFER:
1999 if (EQ (w->buffer, obj))
2001 Lisp_Object buffer;
2002 struct frame *f = XFRAME (w->frame);
2004 /* Find another buffer to show in this window. */
2005 buffer = Fother_buffer (obj, Qnil, w->frame);
2007 /* If this window is dedicated, and in a frame of its own,
2008 kill the frame. */
2009 if (EQ (window, FRAME_ROOT_WINDOW (f))
2010 && !NILP (w->dedicated)
2011 && other_visible_frames (f))
2013 /* Skip the other windows on this frame.
2014 There might be one, the minibuffer! */
2015 while (CONSP (XCDR (windows))
2016 && EQ (XWINDOW (XCAR (windows))->frame,
2017 XWINDOW (XCAR (XCDR (windows)))->frame))
2018 windows = XCDR (windows);
2020 /* Now we can safely delete the frame. */
2021 Fdelete_frame (w->frame, Qnil);
2023 else if (!NILP (w->dedicated) && !NILP (w->parent))
2025 Lisp_Object window;
2026 XSETWINDOW (window, w);
2027 /* If this window is dedicated and not the only window
2028 in its frame, then kill it. */
2029 Fdelete_window (window);
2031 else
2033 /* Otherwise show a different buffer in the window. */
2034 w->dedicated = Qnil;
2035 Fset_window_buffer (window, buffer, Qnil);
2036 if (EQ (window, selected_window))
2037 Fset_buffer (w->buffer);
2040 break;
2042 case REDISPLAY_BUFFER_WINDOWS:
2043 if (EQ (w->buffer, obj))
2045 mark_window_display_accurate (window, 0);
2046 best_window = window;
2048 break;
2050 /* Check for a window that has a killed buffer. */
2051 case CHECK_ALL_WINDOWS:
2052 if (! NILP (w->buffer)
2053 && NILP (XBUFFER (w->buffer)->name))
2054 abort ();
2055 break;
2057 case WINDOW_LOOP_UNUSED:
2058 break;
2062 UNGCPRO;
2063 return best_window;
2066 /* Used for debugging. Abort if any window has a dead buffer. */
2068 void
2069 check_all_windows ()
2071 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2074 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
2075 doc: /* Return the window least recently selected or used for display.
2076 If optional argument FRAME is `visible', search all visible frames.
2077 If FRAME is 0, search all visible and iconified frames.
2078 If FRAME is t, search all frames.
2079 If FRAME is nil, search only the selected frame.
2080 If FRAME is a frame, search only that frame. */)
2081 (frame)
2082 Lisp_Object frame;
2084 register Lisp_Object w;
2085 /* First try for a window that is full-width */
2086 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
2087 if (!NILP (w) && !EQ (w, selected_window))
2088 return w;
2089 /* If none of them, try the rest */
2090 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
2093 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
2094 doc: /* Return the largest window in area.
2095 If optional argument FRAME is `visible', search all visible frames.
2096 If FRAME is 0, search all visible and iconified frames.
2097 If FRAME is t, search all frames.
2098 If FRAME is nil, search only the selected frame.
2099 If FRAME is a frame, search only that frame. */)
2100 (frame)
2101 Lisp_Object frame;
2103 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
2104 frame);
2107 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
2108 doc: /* Return a window currently displaying BUFFER, or nil if none.
2109 If optional argument FRAME is `visible', search all visible frames.
2110 If optional argument FRAME is 0, search all visible and iconified frames.
2111 If FRAME is t, search all frames.
2112 If FRAME is nil, search only the selected frame.
2113 If FRAME is a frame, search only that frame. */)
2114 (buffer, frame)
2115 Lisp_Object buffer, frame;
2117 buffer = Fget_buffer (buffer);
2118 if (BUFFERP (buffer))
2119 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2120 else
2121 return Qnil;
2124 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2125 0, 1, "",
2126 doc: /* Make WINDOW (or the selected window) fill its frame.
2127 Only the frame WINDOW is on is affected.
2128 This function tries to reduce display jumps
2129 by keeping the text previously visible in WINDOW
2130 in the same place on the frame. Doing this depends on
2131 the value of (window-start WINDOW), so if calling this function
2132 in a program gives strange scrolling, make sure the window-start
2133 value is reasonable when this function is called. */)
2134 (window)
2135 Lisp_Object window;
2137 struct window *w;
2138 int startpos;
2139 int top, new_top;
2141 if (NILP (window))
2142 window = selected_window;
2143 else
2144 CHECK_LIVE_WINDOW (window);
2145 w = XWINDOW (window);
2147 startpos = marker_position (w->start);
2148 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2150 if (MINI_WINDOW_P (w) && top > 0)
2151 error ("Can't expand minibuffer to full frame");
2153 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2155 /* Try to minimize scrolling, by setting the window start to the point
2156 will cause the text at the old window start to be at the same place
2157 on the frame. But don't try to do this if the window start is
2158 outside the visible portion (as might happen when the display is
2159 not current, due to typeahead). */
2160 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2161 if (new_top != top
2162 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2163 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2165 struct position pos;
2166 struct buffer *obuf = current_buffer;
2168 Fset_buffer (w->buffer);
2169 /* This computation used to temporarily move point, but that can
2170 have unwanted side effects due to text properties. */
2171 pos = *vmotion (startpos, -top, w);
2173 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2174 w->window_end_valid = Qnil;
2175 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2176 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2177 : Qnil);
2178 /* We need to do this, so that the window-scroll-functions
2179 get called. */
2180 w->optional_new_start = Qt;
2182 set_buffer_internal (obuf);
2185 return Qnil;
2188 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2189 1, 2, "bDelete windows on (buffer): ",
2190 doc: /* Delete all windows showing BUFFER.
2191 Optional second argument FRAME controls which frames are affected.
2192 If optional argument FRAME is `visible', search all visible frames.
2193 If FRAME is 0, search all visible and iconified frames.
2194 If FRAME is nil, search all frames.
2195 If FRAME is t, search only the selected frame.
2196 If FRAME is a frame, search only that frame. */)
2197 (buffer, frame)
2198 Lisp_Object buffer, frame;
2200 /* FRAME uses t and nil to mean the opposite of what window_loop
2201 expects. */
2202 if (NILP (frame))
2203 frame = Qt;
2204 else if (EQ (frame, Qt))
2205 frame = Qnil;
2207 if (!NILP (buffer))
2209 buffer = Fget_buffer (buffer);
2210 CHECK_BUFFER (buffer);
2211 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2214 return Qnil;
2217 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2218 Sreplace_buffer_in_windows,
2219 1, 1, "bReplace buffer in windows: ",
2220 doc: /* Replace BUFFER with some other buffer in all windows showing it. */)
2221 (buffer)
2222 Lisp_Object buffer;
2224 if (!NILP (buffer))
2226 buffer = Fget_buffer (buffer);
2227 CHECK_BUFFER (buffer);
2228 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2230 return Qnil;
2233 /* Replace BUFFER with some other buffer in all windows
2234 of all frames, even those on other keyboards. */
2236 void
2237 replace_buffer_in_all_windows (buffer)
2238 Lisp_Object buffer;
2240 #ifdef MULTI_KBOARD
2241 Lisp_Object tail, frame;
2243 /* A single call to window_loop won't do the job
2244 because it only considers frames on the current keyboard.
2245 So loop manually over frames, and handle each one. */
2246 FOR_EACH_FRAME (tail, frame)
2247 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2248 #else
2249 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
2250 #endif
2253 /* Set the height of WINDOW and all its inferiors. */
2255 /* The smallest acceptable dimensions for a window. Anything smaller
2256 might crash Emacs. */
2258 #define MIN_SAFE_WINDOW_WIDTH (2)
2259 #define MIN_SAFE_WINDOW_HEIGHT (1)
2261 /* Make sure that window_min_height and window_min_width are
2262 not too small; if they are, set them to safe minima. */
2264 static void
2265 check_min_window_sizes ()
2267 /* Smaller values might permit a crash. */
2268 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
2269 window_min_width = MIN_SAFE_WINDOW_WIDTH;
2270 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
2271 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
2274 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2275 minimum allowable size. */
2277 void
2278 check_frame_size (frame, rows, cols)
2279 FRAME_PTR frame;
2280 int *rows, *cols;
2282 /* For height, we have to see:
2283 how many windows the frame has at minimum (one or two),
2284 and whether it has a menu bar or other special stuff at the top. */
2285 int min_height
2286 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2287 ? MIN_SAFE_WINDOW_HEIGHT
2288 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2290 if (FRAME_TOP_MARGIN (frame) > 0)
2291 min_height += FRAME_TOP_MARGIN (frame);
2293 if (*rows < min_height)
2294 *rows = min_height;
2295 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2296 *cols = MIN_SAFE_WINDOW_WIDTH;
2300 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2301 check if W's width can be changed, otherwise check W's height.
2302 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2303 siblings, too. If none of the siblings is resizable, WINDOW isn't
2304 either. */
2306 static int
2307 window_fixed_size_p (w, width_p, check_siblings_p)
2308 struct window *w;
2309 int width_p, check_siblings_p;
2311 int fixed_p;
2312 struct window *c;
2314 if (!NILP (w->hchild))
2316 c = XWINDOW (w->hchild);
2318 if (width_p)
2320 /* A horiz. combination is fixed-width if all of if its
2321 children are. */
2322 while (c && window_fixed_size_p (c, width_p, 0))
2323 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2324 fixed_p = c == NULL;
2326 else
2328 /* A horiz. combination is fixed-height if one of if its
2329 children is. */
2330 while (c && !window_fixed_size_p (c, width_p, 0))
2331 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2332 fixed_p = c != NULL;
2335 else if (!NILP (w->vchild))
2337 c = XWINDOW (w->vchild);
2339 if (width_p)
2341 /* A vert. combination is fixed-width if one of if its
2342 children is. */
2343 while (c && !window_fixed_size_p (c, width_p, 0))
2344 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2345 fixed_p = c != NULL;
2347 else
2349 /* A vert. combination is fixed-height if all of if its
2350 children are. */
2351 while (c && window_fixed_size_p (c, width_p, 0))
2352 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2353 fixed_p = c == NULL;
2356 else if (BUFFERP (w->buffer))
2358 if (w->height_fixed_p && !width_p)
2359 fixed_p = 1;
2360 else
2362 struct buffer *old = current_buffer;
2363 Lisp_Object val;
2365 current_buffer = XBUFFER (w->buffer);
2366 val = find_symbol_value (Qwindow_size_fixed);
2367 current_buffer = old;
2369 fixed_p = 0;
2370 if (!EQ (val, Qunbound))
2372 fixed_p = !NILP (val);
2374 if (fixed_p
2375 && ((EQ (val, Qheight) && width_p)
2376 || (EQ (val, Qwidth) && !width_p)))
2377 fixed_p = 0;
2381 /* Can't tell if this one is resizable without looking at
2382 siblings. If all siblings are fixed-size this one is too. */
2383 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2385 Lisp_Object child;
2387 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2388 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2389 break;
2391 if (NILP (child))
2392 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2393 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2394 break;
2396 if (NILP (child))
2397 fixed_p = 1;
2400 else
2401 fixed_p = 1;
2403 return fixed_p;
2407 /* Return the minimum size of window W, not taking fixed-width windows
2408 into account. WIDTH_P non-zero means return the minimum width,
2409 otherwise return the minimum height. If W is a combination window,
2410 compute the minimum size from the minimum sizes of W's children. */
2412 static int
2413 window_min_size_1 (w, width_p)
2414 struct window *w;
2415 int width_p;
2417 struct window *c;
2418 int size;
2420 if (!NILP (w->hchild))
2422 c = XWINDOW (w->hchild);
2423 size = 0;
2425 if (width_p)
2427 /* The min width of a horizontal combination is
2428 the sum of the min widths of its children. */
2429 while (c)
2431 size += window_min_size_1 (c, width_p);
2432 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2435 else
2437 /* The min height a horizontal combination equals
2438 the maximum of all min height of its children. */
2439 while (c)
2441 int min_size = window_min_size_1 (c, width_p);
2442 size = max (min_size, size);
2443 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2447 else if (!NILP (w->vchild))
2449 c = XWINDOW (w->vchild);
2450 size = 0;
2452 if (width_p)
2454 /* The min width of a vertical combination is
2455 the maximum of the min widths of its children. */
2456 while (c)
2458 int min_size = window_min_size_1 (c, width_p);
2459 size = max (min_size, size);
2460 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2463 else
2465 /* The min height of a vertical combination equals
2466 the sum of the min height of its children. */
2467 while (c)
2469 size += window_min_size_1 (c, width_p);
2470 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2474 else
2476 if (width_p)
2477 size = window_min_width;
2478 else
2480 if (MINI_WINDOW_P (w)
2481 || (!WINDOW_WANTS_MODELINE_P (w)
2482 && !WINDOW_WANTS_HEADER_LINE_P (w)))
2483 size = 1;
2484 else
2485 size = window_min_height;
2489 return size;
2493 /* Return the minimum size of window W, taking fixed-size windows into
2494 account. WIDTH_P non-zero means return the minimum width,
2495 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2496 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2497 unless FIXED is null. */
2499 static int
2500 window_min_size (w, width_p, ignore_fixed_p, fixed)
2501 struct window *w;
2502 int width_p, ignore_fixed_p, *fixed;
2504 int size, fixed_p;
2506 if (ignore_fixed_p)
2507 fixed_p = 0;
2508 else
2509 fixed_p = window_fixed_size_p (w, width_p, 1);
2511 if (fixed)
2512 *fixed = fixed_p;
2514 if (fixed_p)
2515 size = width_p ? XFASTINT (w->total_cols) : XFASTINT (w->total_lines);
2516 else
2517 size = window_min_size_1 (w, width_p);
2519 return size;
2523 /* Adjust the margins of window W if text area is too small.
2524 Return 1 if window width is ok after adjustment; 0 if window
2525 is still too narrow. */
2527 static int
2528 adjust_window_margins (w)
2529 struct window *w;
2531 int box_cols = (WINDOW_TOTAL_COLS (w)
2532 - WINDOW_FRINGE_COLS (w)
2533 - WINDOW_SCROLL_BAR_COLS (w));
2534 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2535 + WINDOW_RIGHT_MARGIN_COLS (w));
2537 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2538 return 1;
2540 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2541 return 0;
2543 /* Window's text area is too narrow, but reducing the window
2544 margins will fix that. */
2545 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2546 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2548 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2549 w->left_margin_cols = w->right_margin_cols
2550 = make_number (margin_cols/2);
2551 else
2552 w->right_margin_cols = make_number (margin_cols);
2554 else
2555 w->left_margin_cols = make_number (margin_cols);
2556 return 1;
2559 /* Calculate new sizes for windows in the list FORWARD when the window size
2560 goes from TOTAL to SIZE. TOTAL must be greater than SIZE.
2561 The number of windows in FORWARD is NCHILDREN, and the number that
2562 can shrink is SHRINKABLE.
2563 The minimum size a window can have is MIN_SIZE.
2564 If we are shrinking fixed windows, RESIZE_FIXED_P is non-zero.
2565 If we are shrinking columns, WIDTH_P is non-zero, otherwise we are
2566 shrinking rows.
2568 This function returns an allocated array of new sizes that the caller
2569 must free. The size -1 means the window is fixed and RESIZE_FIXED_P
2570 is zero. Array index 0 refers to the first window in FORWARD, 1 to
2571 the second, and so on.
2573 This function tries to keep windows at least at the minimum size
2574 and resize other windows before it resizes any window to zero (i.e.
2575 delete that window).
2577 Windows are resized proportional to their size, so bigger windows
2578 shrink more than smaller windows. */
2579 static int *
2580 shrink_windows (total, size, nchildren, shrinkable,
2581 min_size, resize_fixed_p, forward, width_p)
2582 int total, size, nchildren, shrinkable, min_size;
2583 int resize_fixed_p, width_p;
2584 Lisp_Object forward;
2586 int available_resize = 0;
2587 int *new_sizes;
2588 struct window *c;
2589 Lisp_Object child;
2590 int smallest = total;
2591 int total_removed = 0;
2592 int total_shrink = total - size;
2593 int i;
2595 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2597 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2599 int child_size;
2601 c = XWINDOW (child);
2602 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2604 if (! resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2605 new_sizes[i] = -1;
2606 else
2608 new_sizes[i] = child_size;
2609 if (child_size > min_size)
2610 available_resize += child_size - min_size;
2613 /* We might need to shrink some windows to zero. Find the smallest
2614 windows and set them to 0 until we can fulfil the new size. */
2616 while (shrinkable > 1 && size + available_resize < total)
2618 for (i = 0; i < nchildren; ++i)
2619 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2620 smallest = new_sizes[i];
2622 for (i = 0; i < nchildren; ++i)
2623 if (new_sizes[i] == smallest)
2625 /* Resize this window down to zero. */
2626 new_sizes[i] = 0;
2627 if (smallest > min_size)
2628 available_resize -= smallest - min_size;
2629 available_resize += smallest;
2630 --shrinkable;
2631 total_removed += smallest;
2633 /* Out of for, just remove one window at the time and
2634 check again if we have enough space. */
2635 break;
2639 /* Now, calculate the new sizes. Try to shrink each window
2640 proportional to its size. */
2641 for (i = 0; i < nchildren; ++i)
2643 if (new_sizes[i] > min_size)
2645 int to_shrink = total_shrink*new_sizes[i]/total;
2646 if (new_sizes[i] - to_shrink < min_size)
2647 to_shrink = new_sizes[i] - min_size;
2648 new_sizes[i] -= to_shrink;
2649 total_removed += to_shrink;
2653 /* Any reminder due to rounding, we just subtract from windows
2654 that are left and still can be shrunk. */
2655 while (total_shrink > total_removed)
2657 for (i = 0; i < nchildren; ++i)
2658 if (new_sizes[i] > min_size)
2660 --new_sizes[i];
2661 ++total_removed;
2663 /* Out of for, just shrink one window at the time and
2664 check again if we have enough space. */
2665 break;
2669 return new_sizes;
2672 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2673 WINDOW's width. Resize WINDOW's children, if any, so that they
2674 keep their proportionate size relative to WINDOW. Propagate
2675 WINDOW's top or left edge position to children. Delete windows
2676 that become too small unless NODELETE_P is non-zero.
2678 If NODELETE_P is 2, that means we do delete windows that are
2679 too small, even if they were too small before! */
2681 static void
2682 size_window (window, size, width_p, nodelete_p)
2683 Lisp_Object window;
2684 int size, width_p, nodelete_p;
2686 struct window *w = XWINDOW (window);
2687 struct window *c;
2688 Lisp_Object child, *forward, *sideward;
2689 int old_size, min_size, safe_min_size;
2691 /* We test nodelete_p != 2 and nodelete_p != 1 below, so it
2692 seems like it's too soon to do this here. ++KFS. */
2693 if (nodelete_p == 2)
2694 nodelete_p = 0;
2696 check_min_window_sizes ();
2697 size = max (0, size);
2699 /* If the window has been "too small" at one point,
2700 don't delete it for being "too small" in the future.
2701 Preserve it as long as that is at all possible. */
2702 if (width_p)
2704 old_size = WINDOW_TOTAL_COLS (w);
2705 min_size = window_min_width;
2706 /* Ensure that there is room for the scroll bar and fringes!
2707 We may reduce display margins though. */
2708 safe_min_size = (MIN_SAFE_WINDOW_WIDTH
2709 + WINDOW_FRINGE_COLS (w)
2710 + WINDOW_SCROLL_BAR_COLS (w));
2712 else
2714 old_size = XINT (w->total_lines);
2715 min_size = window_min_height;
2716 safe_min_size = MIN_SAFE_WINDOW_HEIGHT;
2719 if (old_size < min_size && nodelete_p != 2)
2720 w->too_small_ok = Qt;
2722 /* Maybe delete WINDOW if it's too small. */
2723 if (nodelete_p != 1 && !NILP (w->parent))
2725 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
2726 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
2727 if (min_size < safe_min_size)
2728 min_size = safe_min_size;
2729 if (size < min_size)
2731 delete_window (window);
2732 return;
2736 /* Set redisplay hints. */
2737 w->last_modified = make_number (0);
2738 w->last_overlay_modified = make_number (0);
2739 windows_or_buffers_changed++;
2740 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
2742 if (width_p)
2744 sideward = &w->vchild;
2745 forward = &w->hchild;
2746 w->total_cols = make_number (size);
2747 adjust_window_margins (w);
2749 else
2751 sideward = &w->hchild;
2752 forward = &w->vchild;
2753 w->total_lines = make_number (size);
2754 w->orig_total_lines = Qnil;
2757 if (!NILP (*sideward))
2759 for (child = *sideward; !NILP (child); child = c->next)
2761 c = XWINDOW (child);
2762 if (width_p)
2763 c->left_col = w->left_col;
2764 else
2765 c->top_line = w->top_line;
2766 size_window (child, size, width_p, nodelete_p);
2769 else if (!NILP (*forward))
2771 int fixed_size, each, extra, n;
2772 int resize_fixed_p, nfixed;
2773 int last_pos, first_pos, nchildren, total;
2774 int *new_sizes = NULL;
2776 /* Determine the fixed-size portion of the this window, and the
2777 number of child windows. */
2778 fixed_size = nchildren = nfixed = total = 0;
2779 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
2781 int child_size;
2783 c = XWINDOW (child);
2784 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2785 total += child_size;
2787 if (window_fixed_size_p (c, width_p, 0))
2789 fixed_size += child_size;
2790 ++nfixed;
2794 /* If the new size is smaller than fixed_size, or if there
2795 aren't any resizable windows, allow resizing fixed-size
2796 windows. */
2797 resize_fixed_p = nfixed == nchildren || size < fixed_size;
2799 /* Compute how many lines/columns to add/remove to each child. The
2800 value of extra takes care of rounding errors. */
2801 n = resize_fixed_p ? nchildren : nchildren - nfixed;
2802 if (size < total && n > 1)
2803 new_sizes = shrink_windows (total, size, nchildren, n, min_size,
2804 resize_fixed_p, *forward, width_p);
2805 else
2807 each = (size - total) / n;
2808 extra = (size - total) - n * each;
2811 /* Compute new children heights and edge positions. */
2812 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
2813 last_pos = first_pos;
2814 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
2816 int new_size, old_size;
2818 c = XWINDOW (child);
2819 old_size = width_p ? XFASTINT (c->total_cols) : XFASTINT (c->total_lines);
2820 new_size = old_size;
2822 /* The top or left edge position of this child equals the
2823 bottom or right edge of its predecessor. */
2824 if (width_p)
2825 c->left_col = make_number (last_pos);
2826 else
2827 c->top_line = make_number (last_pos);
2829 /* If this child can be resized, do it. */
2830 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
2832 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
2833 extra = 0;
2836 /* Set new height. Note that size_window also propagates
2837 edge positions to children, so it's not a no-op if we
2838 didn't change the child's size. */
2839 size_window (child, new_size, width_p, 1);
2841 /* Remember the bottom/right edge position of this child; it
2842 will be used to set the top/left edge of the next child. */
2843 last_pos += new_size;
2846 if (new_sizes) xfree (new_sizes);
2848 /* We should have covered the parent exactly with child windows. */
2849 xassert (size == last_pos - first_pos);
2851 /* Now delete any children that became too small. */
2852 if (!nodelete_p)
2853 for (child = *forward; !NILP (child); child = c->next)
2855 int child_size;
2856 c = XWINDOW (child);
2857 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2858 size_window (child, child_size, width_p, 2);
2863 /* Set WINDOW's height to HEIGHT, and recursively change the height of
2864 WINDOW's children. NODELETE non-zero means don't delete windows
2865 that become too small in the process. (The caller should check
2866 later and do so if appropriate.) */
2868 void
2869 set_window_height (window, height, nodelete)
2870 Lisp_Object window;
2871 int height;
2872 int nodelete;
2874 size_window (window, height, 0, nodelete);
2878 /* Set WINDOW's width to WIDTH, and recursively change the width of
2879 WINDOW's children. NODELETE non-zero means don't delete windows
2880 that become too small in the process. (The caller should check
2881 later and do so if appropriate.) */
2883 void
2884 set_window_width (window, width, nodelete)
2885 Lisp_Object window;
2886 int width;
2887 int nodelete;
2889 size_window (window, width, 1, nodelete);
2892 /* Change window heights in windows rooted in WINDOW by N lines. */
2894 void
2895 change_window_heights (window, n)
2896 Lisp_Object window;
2897 int n;
2899 struct window *w = XWINDOW (window);
2901 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
2902 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
2904 if (INTEGERP (w->orig_top_line))
2905 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
2906 if (INTEGERP (w->orig_total_lines))
2907 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
2909 /* Handle just the top child in a vertical split. */
2910 if (!NILP (w->vchild))
2911 change_window_heights (w->vchild, n);
2913 /* Adjust all children in a horizontal split. */
2914 for (window = w->hchild; !NILP (window); window = w->next)
2916 w = XWINDOW (window);
2917 change_window_heights (window, n);
2922 int window_select_count;
2924 Lisp_Object
2925 Fset_window_buffer_unwind (obuf)
2926 Lisp_Object obuf;
2928 Fset_buffer (obuf);
2929 return Qnil;
2932 EXFUN (Fset_window_fringes, 4);
2933 EXFUN (Fset_window_scroll_bars, 4);
2935 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2936 means it's allowed to run hooks. See make_frame for a case where
2937 it's not allowed. KEEP_MARGINS_P non-zero means that the current
2938 margins, fringes, and scroll-bar settings of the window are not
2939 reset from the buffer's local settings. */
2941 void
2942 set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
2943 Lisp_Object window, buffer;
2944 int run_hooks_p, keep_margins_p;
2946 struct window *w = XWINDOW (window);
2947 struct buffer *b = XBUFFER (buffer);
2948 int count = SPECPDL_INDEX ();
2950 w->buffer = buffer;
2952 if (EQ (window, selected_window))
2953 b->last_selected_window = window;
2955 /* Update time stamps of buffer display. */
2956 if (INTEGERP (b->display_count))
2957 XSETINT (b->display_count, XINT (b->display_count) + 1);
2958 b->display_time = Fcurrent_time ();
2960 XSETFASTINT (w->window_end_pos, 0);
2961 XSETFASTINT (w->window_end_vpos, 0);
2962 bzero (&w->last_cursor, sizeof w->last_cursor);
2963 w->window_end_valid = Qnil;
2964 w->hscroll = w->min_hscroll = make_number (0);
2965 w->vscroll = 0;
2966 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
2967 set_marker_restricted (w->start,
2968 make_number (b->last_window_start),
2969 buffer);
2970 w->start_at_line_beg = Qnil;
2971 w->force_start = Qnil;
2972 XSETFASTINT (w->last_modified, 0);
2973 XSETFASTINT (w->last_overlay_modified, 0);
2974 windows_or_buffers_changed++;
2976 /* We must select BUFFER for running the window-scroll-functions.
2977 If WINDOW is selected, switch permanently.
2978 Otherwise, switch but go back to the ambient buffer afterward. */
2979 if (EQ (window, selected_window))
2980 Fset_buffer (buffer);
2981 /* We can't check ! NILP (Vwindow_scroll_functions) here
2982 because that might itself be a local variable. */
2983 else if (window_initialized)
2985 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
2986 Fset_buffer (buffer);
2989 if (!keep_margins_p)
2991 /* Set left and right marginal area width etc. from buffer. */
2993 /* This may call adjust_window_margins three times, so
2994 temporarily disable window margins. */
2995 Lisp_Object save_left = w->left_margin_cols;
2996 Lisp_Object save_right = w->right_margin_cols;
2998 w->left_margin_cols = w->right_margin_cols = Qnil;
3000 Fset_window_fringes (window,
3001 b->left_fringe_width, b->right_fringe_width,
3002 b->fringes_outside_margins);
3004 Fset_window_scroll_bars (window,
3005 b->scroll_bar_width,
3006 b->vertical_scroll_bar_type, Qnil);
3008 w->left_margin_cols = save_left;
3009 w->right_margin_cols = save_right;
3011 Fset_window_margins (window,
3012 b->left_margin_cols, b->right_margin_cols);
3015 if (run_hooks_p)
3017 if (! NILP (Vwindow_scroll_functions))
3018 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3019 Fmarker_position (w->start));
3021 if (! NILP (Vwindow_configuration_change_hook)
3022 && ! NILP (Vrun_hooks))
3023 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3026 unbind_to (count, Qnil);
3030 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3031 doc: /* Make WINDOW display BUFFER as its contents.
3032 BUFFER can be a buffer or buffer name.
3033 Optional third arg KEEP_MARGINS non-nil means that WINDOW's current
3034 display margins, fringe widths, and scroll bar settings are maintained;
3035 the default is to reset these from BUFFER's local settings or the frame
3036 defaults. */)
3037 (window, buffer, keep_margins)
3038 register Lisp_Object window, buffer, keep_margins;
3040 register Lisp_Object tem;
3041 register struct window *w = decode_window (window);
3043 XSETWINDOW (window, w);
3044 buffer = Fget_buffer (buffer);
3045 CHECK_BUFFER (buffer);
3047 if (NILP (XBUFFER (buffer)->name))
3048 error ("Attempt to display deleted buffer");
3050 tem = w->buffer;
3051 if (NILP (tem))
3052 error ("Window is deleted");
3053 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
3054 is first being set up. */
3056 if (!NILP (w->dedicated) && !EQ (tem, buffer))
3057 error ("Window is dedicated to `%s'",
3058 SDATA (XBUFFER (tem)->name));
3060 unshow_buffer (w);
3063 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3064 return Qnil;
3067 /* Note that selected_window can be nil
3068 when this is called from Fset_window_configuration. */
3070 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3071 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3072 If WINDOW is not already selected, also make WINDOW's buffer current.
3073 Also make WINDOW the frame's selected window.
3074 Optional second arg NORECORD non-nil means
3075 do not put this buffer at the front of the list of recently selected ones.
3077 Note that the main editor command loop
3078 selects the buffer of the selected window before each command. */)
3079 (window, norecord)
3080 register Lisp_Object window, norecord;
3082 register struct window *w;
3083 register struct window *ow;
3084 struct frame *sf;
3086 CHECK_LIVE_WINDOW (window);
3088 w = XWINDOW (window);
3089 w->frozen_window_start_p = 0;
3091 XSETFASTINT (w->use_time, ++window_select_count);
3092 if (EQ (window, selected_window))
3093 return window;
3095 if (!NILP (selected_window))
3097 ow = XWINDOW (selected_window);
3098 if (! NILP (ow->buffer))
3099 set_marker_both (ow->pointm, ow->buffer,
3100 BUF_PT (XBUFFER (ow->buffer)),
3101 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3104 selected_window = window;
3105 sf = SELECTED_FRAME ();
3106 if (XFRAME (WINDOW_FRAME (w)) != sf)
3108 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3109 /* Use this rather than Fhandle_switch_frame
3110 so that FRAME_FOCUS_FRAME is moved appropriately as we
3111 move around in the state where a minibuffer in a separate
3112 frame is active. */
3113 Fselect_frame (WINDOW_FRAME (w), Qnil);
3115 else
3116 sf->selected_window = window;
3118 if (NILP (norecord))
3119 record_buffer (w->buffer);
3120 Fset_buffer (w->buffer);
3122 XBUFFER (w->buffer)->last_selected_window = window;
3124 /* Go to the point recorded in the window.
3125 This is important when the buffer is in more
3126 than one window. It also matters when
3127 redisplay_window has altered point after scrolling,
3128 because it makes the change only in the window. */
3130 register int new_point = marker_position (w->pointm);
3131 if (new_point < BEGV)
3132 SET_PT (BEGV);
3133 else if (new_point > ZV)
3134 SET_PT (ZV);
3135 else
3136 SET_PT (new_point);
3139 windows_or_buffers_changed++;
3140 return window;
3143 static Lisp_Object
3144 select_window_norecord (window)
3145 Lisp_Object window;
3147 return Fselect_window (window, Qt);
3150 /* Deiconify the frame containing the window WINDOW,
3151 unless it is the selected frame;
3152 then return WINDOW.
3154 The reason for the exception for the selected frame
3155 is that it seems better not to change the selected frames visibility
3156 merely because of displaying a different buffer in it.
3157 The deiconification is useful when a buffer gets shown in
3158 another frame that you were not using lately. */
3160 static Lisp_Object
3161 display_buffer_1 (window)
3162 Lisp_Object window;
3164 Lisp_Object frame = XWINDOW (window)->frame;
3165 FRAME_PTR f = XFRAME (frame);
3167 FRAME_SAMPLE_VISIBILITY (f);
3169 if (EQ (frame, selected_frame))
3170 ; /* Assume the selected frame is already visible enough. */
3171 else if (minibuf_level > 0
3172 && MINI_WINDOW_P (XWINDOW (selected_window))
3173 && WINDOW_LIVE_P (minibuf_selected_window)
3174 && EQ (frame, WINDOW_FRAME (XWINDOW (minibuf_selected_window))))
3175 ; /* Assume the frame from which we invoked the minibuffer is visible. */
3176 else
3178 if (FRAME_ICONIFIED_P (f))
3179 Fmake_frame_visible (frame);
3180 else if (FRAME_VISIBLE_P (f))
3181 Fraise_frame (frame);
3184 return window;
3187 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
3188 doc: /* Returns non-nil if a buffer named BUFFER-NAME would be created specially.
3189 The value is actually t if the frame should be called with default frame
3190 parameters, and a list of frame parameters if they were specified.
3191 See `special-display-buffer-names', and `special-display-regexps'. */)
3192 (buffer_name)
3193 Lisp_Object buffer_name;
3195 Lisp_Object tem;
3197 CHECK_STRING (buffer_name);
3199 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
3200 if (!NILP (tem))
3201 return Qt;
3203 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
3204 if (!NILP (tem))
3205 return XCDR (tem);
3207 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
3209 Lisp_Object car = XCAR (tem);
3210 if (STRINGP (car)
3211 && fast_string_match (car, buffer_name) >= 0)
3212 return Qt;
3213 else if (CONSP (car)
3214 && STRINGP (XCAR (car))
3215 && fast_string_match (XCAR (car), buffer_name) >= 0)
3216 return XCDR (car);
3218 return Qnil;
3221 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
3222 doc: /* Returns non-nil if a new buffer named BUFFER-NAME would use the same window.
3223 See `same-window-buffer-names' and `same-window-regexps'. */)
3224 (buffer_name)
3225 Lisp_Object buffer_name;
3227 Lisp_Object tem;
3229 CHECK_STRING (buffer_name);
3231 tem = Fmember (buffer_name, Vsame_window_buffer_names);
3232 if (!NILP (tem))
3233 return Qt;
3235 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
3236 if (!NILP (tem))
3237 return Qt;
3239 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
3241 Lisp_Object car = XCAR (tem);
3242 if (STRINGP (car)
3243 && fast_string_match (car, buffer_name) >= 0)
3244 return Qt;
3245 else if (CONSP (car)
3246 && STRINGP (XCAR (car))
3247 && fast_string_match (XCAR (car), buffer_name) >= 0)
3248 return Qt;
3250 return Qnil;
3253 /* Use B so the default is (other-buffer). */
3254 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
3255 "BDisplay buffer: \nP",
3256 doc: /* Make BUFFER appear in some window but don't select it.
3257 BUFFER can be a buffer or a buffer name.
3258 If BUFFER is shown already in some window, just use that one,
3259 unless the window is the selected window and the optional second
3260 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).
3261 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
3262 Returns the window displaying BUFFER.
3263 If `display-buffer-reuse-frames' is non-nil, and another frame is currently
3264 displaying BUFFER, then simply raise that frame.
3266 The variables `special-display-buffer-names', `special-display-regexps',
3267 `same-window-buffer-names', and `same-window-regexps' customize how certain
3268 buffer names are handled.
3270 If optional argument FRAME is `visible', search all visible frames.
3271 If FRAME is 0, search all visible and iconified frames.
3272 If FRAME is t, search all frames.
3273 If FRAME is a frame, search only that frame.
3274 If FRAME is nil, search only the selected frame
3275 (actually the last nonminibuffer frame),
3276 unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,
3277 which means search visible and iconified frames.
3279 If `even-window-heights' is non-nil, window heights will be evened out
3280 if displaying the buffer causes two vertically adjacent windows to be
3281 displayed. */)
3282 (buffer, not_this_window, frame)
3283 register Lisp_Object buffer, not_this_window, frame;
3285 register Lisp_Object window, tem, swp;
3286 struct frame *f;
3288 swp = Qnil;
3289 buffer = Fget_buffer (buffer);
3290 CHECK_BUFFER (buffer);
3292 if (!NILP (Vdisplay_buffer_function))
3293 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
3295 if (NILP (not_this_window)
3296 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
3297 return display_buffer_1 (selected_window);
3299 /* See if the user has specified this buffer should appear
3300 in the selected window. */
3301 if (NILP (not_this_window))
3303 swp = Fsame_window_p (XBUFFER (buffer)->name);
3304 if (!NILP (swp) && !no_switch_window (selected_window))
3306 Fswitch_to_buffer (buffer, Qnil);
3307 return display_buffer_1 (selected_window);
3311 /* If the user wants pop-up-frames or display-buffer-reuse-frames,
3312 look for a window showing BUFFER on any visible or iconified frame.
3313 Otherwise search only the current frame. */
3314 if (! NILP (frame))
3315 tem = frame;
3316 else if (pop_up_frames
3317 || display_buffer_reuse_frames
3318 || last_nonminibuf_frame == 0)
3319 XSETFASTINT (tem, 0);
3320 else
3321 XSETFRAME (tem, last_nonminibuf_frame);
3323 window = Fget_buffer_window (buffer, tem);
3324 if (!NILP (window)
3325 && (NILP (not_this_window) || !EQ (window, selected_window)))
3326 return display_buffer_1 (window);
3328 /* Certain buffer names get special handling. */
3329 if (!NILP (Vspecial_display_function) && NILP (swp))
3331 tem = Fspecial_display_p (XBUFFER (buffer)->name);
3332 if (EQ (tem, Qt))
3333 return call1 (Vspecial_display_function, buffer);
3334 if (CONSP (tem))
3335 return call2 (Vspecial_display_function, buffer, tem);
3338 /* If there are no frames open that have more than a minibuffer,
3339 we need to create a new frame. */
3340 if (pop_up_frames || last_nonminibuf_frame == 0)
3342 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3343 Fset_window_buffer (window, buffer, Qnil);
3344 return display_buffer_1 (window);
3347 f = SELECTED_FRAME ();
3348 if (pop_up_windows
3349 || FRAME_MINIBUF_ONLY_P (f)
3350 /* If the current frame is a special display frame,
3351 don't try to reuse its windows. */
3352 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
3354 Lisp_Object frames;
3356 frames = Qnil;
3357 if (FRAME_MINIBUF_ONLY_P (f))
3358 XSETFRAME (frames, last_nonminibuf_frame);
3359 /* Don't try to create a window if we would get an error. */
3360 if (split_height_threshold < window_min_height << 1)
3361 split_height_threshold = window_min_height << 1;
3363 /* Note that both Fget_largest_window and Fget_lru_window
3364 ignore minibuffers and dedicated windows.
3365 This means they can return nil. */
3367 /* If the frame we would try to split cannot be split,
3368 try other frames. */
3369 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
3371 /* Try visible frames first. */
3372 window = Fget_largest_window (Qvisible);
3373 /* If that didn't work, try iconified frames. */
3374 if (NILP (window))
3375 window = Fget_largest_window (make_number (0));
3376 if (NILP (window))
3377 window = Fget_largest_window (Qt);
3379 else
3380 window = Fget_largest_window (frames);
3382 /* If we got a tall enough full-width window that can be split,
3383 split it. */
3384 if (!NILP (window)
3385 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3386 && window_height (window) >= split_height_threshold
3387 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
3388 window = Fsplit_window (window, Qnil, Qnil);
3389 else
3391 Lisp_Object upper, lower, other;
3393 window = Fget_lru_window (frames);
3394 /* If the LRU window is selected, and big enough,
3395 and can be split, split it. */
3396 if (!NILP (window)
3397 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3398 && (EQ (window, selected_window)
3399 || EQ (XWINDOW (window)->parent, Qnil))
3400 && window_height (window) >= window_min_height << 1)
3401 window = Fsplit_window (window, Qnil, Qnil);
3402 /* If Fget_lru_window returned nil, try other approaches. */
3404 /* Try visible frames first. */
3405 if (NILP (window))
3406 window = Fget_buffer_window (buffer, Qvisible);
3407 if (NILP (window))
3408 window = Fget_largest_window (Qvisible);
3409 /* If that didn't work, try iconified frames. */
3410 if (NILP (window))
3411 window = Fget_buffer_window (buffer, make_number (0));
3412 if (NILP (window))
3413 window = Fget_largest_window (make_number (0));
3414 /* Try invisible frames. */
3415 if (NILP (window))
3416 window = Fget_buffer_window (buffer, Qt);
3417 if (NILP (window))
3418 window = Fget_largest_window (Qt);
3419 /* As a last resort, make a new frame. */
3420 if (NILP (window))
3421 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3422 /* If window appears above or below another,
3423 even out their heights. */
3424 other = upper = lower = Qnil;
3425 if (!NILP (XWINDOW (window)->prev))
3426 other = upper = XWINDOW (window)->prev, lower = window;
3427 if (!NILP (XWINDOW (window)->next))
3428 other = lower = XWINDOW (window)->next, upper = window;
3429 if (!NILP (other)
3430 && !NILP (Veven_window_heights)
3431 /* Check that OTHER and WINDOW are vertically arrayed. */
3432 && !EQ (XWINDOW (other)->top_line, XWINDOW (window)->top_line)
3433 && (XFASTINT (XWINDOW (other)->total_lines)
3434 > XFASTINT (XWINDOW (window)->total_lines)))
3436 int total = (XFASTINT (XWINDOW (other)->total_lines)
3437 + XFASTINT (XWINDOW (window)->total_lines));
3438 enlarge_window (upper,
3439 total / 2 - XFASTINT (XWINDOW (upper)->total_lines),
3440 0, 0);
3444 else
3445 window = Fget_lru_window (Qnil);
3447 Fset_window_buffer (window, buffer, Qnil);
3448 return display_buffer_1 (window);
3452 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3453 0, 1, 0,
3454 doc: /* Force redisplay of all windows.
3455 If optional arg OBJECT is a window, force redisplay of that window only.
3456 If OBJECT is a buffer or buffer name, force redisplay of all windows
3457 displaying that buffer. */)
3458 (object)
3459 Lisp_Object object;
3461 if (NILP (object))
3463 windows_or_buffers_changed++;
3464 return Qt;
3467 if (WINDOWP (object))
3469 mark_window_display_accurate (object, 0);
3470 return Qt;
3473 if (STRINGP (object))
3474 object = Fget_buffer (object);
3475 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3477 /* Walk all windows looking for buffer, and force update
3478 of each of those windows. */
3480 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3481 return NILP (object) ? Qnil : Qt;
3484 /* If nothing suitable was found, just return.
3485 We could signal an error, but this feature will typically be used
3486 asynchronously in timers or process sentinels, so we don't. */
3487 return Qnil;
3491 void
3492 temp_output_buffer_show (buf)
3493 register Lisp_Object buf;
3495 register struct buffer *old = current_buffer;
3496 register Lisp_Object window;
3497 register struct window *w;
3499 XBUFFER (buf)->directory = current_buffer->directory;
3501 Fset_buffer (buf);
3502 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3503 BEGV = BEG;
3504 ZV = Z;
3505 SET_PT (BEG);
3506 #if 0 /* rms: there should be no reason for this. */
3507 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3508 #endif
3509 set_buffer_internal (old);
3511 if (!EQ (Vtemp_buffer_show_function, Qnil))
3512 call1 (Vtemp_buffer_show_function, buf);
3513 else
3515 window = Fdisplay_buffer (buf, Qnil, Qnil);
3517 if (!EQ (XWINDOW (window)->frame, selected_frame))
3518 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3519 Vminibuf_scroll_window = window;
3520 w = XWINDOW (window);
3521 XSETFASTINT (w->hscroll, 0);
3522 XSETFASTINT (w->min_hscroll, 0);
3523 set_marker_restricted_both (w->start, buf, BEG, BEG);
3524 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3526 /* Run temp-buffer-show-hook, with the chosen window selected
3527 and its buffer current. */
3529 if (!NILP (Vrun_hooks)
3530 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3531 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3533 int count = SPECPDL_INDEX ();
3534 Lisp_Object prev_window, prev_buffer;
3535 prev_window = selected_window;
3536 XSETBUFFER (prev_buffer, old);
3538 /* Select the window that was chosen, for running the hook.
3539 Note: Both Fselect_window and select_window_norecord may
3540 set-buffer to the buffer displayed in the window,
3541 so we need to save the current buffer. --stef */
3542 record_unwind_protect (Fset_buffer, prev_buffer);
3543 record_unwind_protect (select_window_norecord, prev_window);
3544 Fselect_window (window, Qt);
3545 Fset_buffer (w->buffer);
3546 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3547 unbind_to (count, Qnil);
3552 static void
3553 make_dummy_parent (window)
3554 Lisp_Object window;
3556 Lisp_Object new;
3557 register struct window *o, *p;
3558 int i;
3560 o = XWINDOW (window);
3561 p = allocate_window ();
3562 for (i = 0; i < VECSIZE (struct window); ++i)
3563 ((struct Lisp_Vector *) p)->contents[i]
3564 = ((struct Lisp_Vector *)o)->contents[i];
3565 XSETWINDOW (new, p);
3567 XSETFASTINT (p->sequence_number, ++sequence_number);
3569 /* Put new into window structure in place of window */
3570 replace_window (window, new);
3572 o->next = Qnil;
3573 o->prev = Qnil;
3574 o->vchild = Qnil;
3575 o->hchild = Qnil;
3576 o->parent = new;
3578 p->start = Qnil;
3579 p->pointm = Qnil;
3580 p->buffer = Qnil;
3583 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3584 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3585 WINDOW defaults to selected one and SIZE to half its size.
3586 If optional third arg HORFLAG is non-nil, split side by side
3587 and put SIZE columns in the first of the pair. In that case,
3588 SIZE includes that window's scroll bar, or the divider column to its right. */)
3589 (window, size, horflag)
3590 Lisp_Object window, size, horflag;
3592 register Lisp_Object new;
3593 register struct window *o, *p;
3594 FRAME_PTR fo;
3595 register int size_int;
3597 if (NILP (window))
3598 window = selected_window;
3599 else
3600 CHECK_LIVE_WINDOW (window);
3602 o = XWINDOW (window);
3603 fo = XFRAME (WINDOW_FRAME (o));
3605 if (NILP (size))
3607 if (!NILP (horflag))
3608 /* Calculate the size of the left-hand window, by dividing
3609 the usable space in columns by two.
3610 We round up, since the left-hand window may include
3611 a dividing line, while the right-hand may not. */
3612 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3613 else
3614 size_int = XFASTINT (o->total_lines) >> 1;
3616 else
3618 CHECK_NUMBER (size);
3619 size_int = XINT (size);
3622 if (MINI_WINDOW_P (o))
3623 error ("Attempt to split minibuffer window");
3624 else if (window_fixed_size_p (o, !NILP (horflag), 0))
3625 error ("Attempt to split fixed-size window");
3627 check_min_window_sizes ();
3629 if (NILP (horflag))
3631 if (size_int < window_min_height)
3632 error ("Window height %d too small (after splitting)", size_int);
3633 if (size_int + window_min_height > XFASTINT (o->total_lines))
3634 error ("Window height %d too small (after splitting)",
3635 XFASTINT (o->total_lines) - size_int);
3636 if (NILP (o->parent)
3637 || NILP (XWINDOW (o->parent)->vchild))
3639 make_dummy_parent (window);
3640 new = o->parent;
3641 XWINDOW (new)->vchild = window;
3644 else
3646 if (size_int < window_min_width)
3647 error ("Window width %d too small (after splitting)", size_int);
3649 if (size_int + window_min_width > XFASTINT (o->total_cols))
3650 error ("Window width %d too small (after splitting)",
3651 XFASTINT (o->total_cols) - size_int);
3652 if (NILP (o->parent)
3653 || NILP (XWINDOW (o->parent)->hchild))
3655 make_dummy_parent (window);
3656 new = o->parent;
3657 XWINDOW (new)->hchild = window;
3661 /* Now we know that window's parent is a vertical combination
3662 if we are dividing vertically, or a horizontal combination
3663 if we are making side-by-side windows */
3665 windows_or_buffers_changed++;
3666 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3667 new = make_window ();
3668 p = XWINDOW (new);
3670 p->frame = o->frame;
3671 p->next = o->next;
3672 if (!NILP (p->next))
3673 XWINDOW (p->next)->prev = new;
3674 p->prev = window;
3675 o->next = new;
3676 p->parent = o->parent;
3677 p->buffer = Qt;
3678 p->window_end_valid = Qnil;
3679 bzero (&p->last_cursor, sizeof p->last_cursor);
3681 /* Duplicate special geometry settings. */
3683 p->left_margin_cols = o->left_margin_cols;
3684 p->right_margin_cols = o->right_margin_cols;
3685 p->left_fringe_width = o->left_fringe_width;
3686 p->right_fringe_width = o->right_fringe_width;
3687 p->fringes_outside_margins = o->fringes_outside_margins;
3688 p->scroll_bar_width = o->scroll_bar_width;
3689 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3691 /* Apportion the available frame space among the two new windows */
3693 if (!NILP (horflag))
3695 p->total_lines = o->total_lines;
3696 p->top_line = o->top_line;
3697 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3698 XSETFASTINT (o->total_cols, size_int);
3699 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3700 adjust_window_margins (p);
3701 adjust_window_margins (o);
3703 else
3705 p->left_col = o->left_col;
3706 p->total_cols = o->total_cols;
3707 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3708 XSETFASTINT (o->total_lines, size_int);
3709 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3712 /* Adjust glyph matrices. */
3713 adjust_glyphs (fo);
3715 Fset_window_buffer (new, o->buffer, Qt);
3716 return new;
3719 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 3, "p",
3720 doc: /* Make current window ARG lines bigger.
3721 From program, optional second arg non-nil means grow sideways ARG columns.
3722 Interactively, if an argument is not given, make the window one line bigger.
3724 Optional third arg PRESERVE-BEFORE, if non-nil, means do not change the size
3725 of the siblings above or to the left of the selected window. Only
3726 siblings to the right or below are changed. */)
3727 (arg, side, preserve_before)
3728 register Lisp_Object arg, side, preserve_before;
3730 CHECK_NUMBER (arg);
3731 enlarge_window (selected_window, XINT (arg), !NILP (side),
3732 !NILP (preserve_before));
3734 if (! NILP (Vwindow_configuration_change_hook))
3735 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3737 return Qnil;
3740 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 3, "p",
3741 doc: /* Make current window ARG lines smaller.
3742 From program, optional second arg non-nil means shrink sideways arg columns.
3743 Interactively, if an argument is not given, make the window one line smaller.
3745 Optional third arg PRESERVE-BEFORE, if non-nil, means do not change the size
3746 of the siblings above or to the left of the selected window. Only
3747 siblings to the right or below are changed. */)
3748 (arg, side, preserve_before)
3749 register Lisp_Object arg, side, preserve_before;
3751 CHECK_NUMBER (arg);
3752 enlarge_window (selected_window, -XINT (arg), !NILP (side),
3753 !NILP (preserve_before));
3755 if (! NILP (Vwindow_configuration_change_hook))
3756 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3758 return Qnil;
3762 window_height (window)
3763 Lisp_Object window;
3765 register struct window *p = XWINDOW (window);
3766 return WINDOW_TOTAL_LINES (p);
3770 window_width (window)
3771 Lisp_Object window;
3773 register struct window *p = XWINDOW (window);
3774 return WINDOW_TOTAL_COLS (p);
3778 #define CURBEG(w) \
3779 *(widthflag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3781 #define CURSIZE(w) \
3782 *(widthflag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3785 /* Enlarge WINDOW by DELTA. WIDTHFLAG non-zero means
3786 increase its width. Siblings of the selected window are resized to
3787 fulfill the size request. If they become too small in the process,
3788 they will be deleted.
3790 If PRESERVE_BEFORE is nonzero, that means don't alter
3791 the siblings to the left or above WINDOW. */
3793 static void
3794 enlarge_window (window, delta, widthflag, preserve_before)
3795 Lisp_Object window;
3796 int delta, widthflag, preserve_before;
3798 Lisp_Object parent, next, prev;
3799 struct window *p;
3800 Lisp_Object *sizep;
3801 int maximum;
3802 int (*sizefun) P_ ((Lisp_Object))
3803 = widthflag ? window_width : window_height;
3804 void (*setsizefun) P_ ((Lisp_Object, int, int))
3805 = (widthflag ? set_window_width : set_window_height);
3807 /* Check values of window_min_width and window_min_height for
3808 validity. */
3809 check_min_window_sizes ();
3811 /* Give up if this window cannot be resized. */
3812 if (window_fixed_size_p (XWINDOW (window), widthflag, 1))
3813 error ("Window is not resizable");
3815 /* Find the parent of the selected window. */
3816 while (1)
3818 p = XWINDOW (window);
3819 parent = p->parent;
3821 if (NILP (parent))
3823 if (widthflag)
3824 error ("No other window to side of this one");
3825 break;
3828 if (widthflag
3829 ? !NILP (XWINDOW (parent)->hchild)
3830 : !NILP (XWINDOW (parent)->vchild))
3831 break;
3833 window = parent;
3836 sizep = &CURSIZE (window);
3839 register int maxdelta;
3841 /* Compute the maximum size increment this window can have. */
3843 if (preserve_before)
3845 if (!NILP (parent))
3847 maxdelta = (*sizefun) (parent) - XINT (*sizep);
3848 /* Subtract size of siblings before, since we can't take that. */
3849 maxdelta -= XINT (CURBEG (window)) - XINT (CURBEG (parent));
3851 else
3852 maxdelta = (!NILP (p->next) ? ((*sizefun) (p->next)
3853 - window_min_size (XWINDOW (p->next),
3854 widthflag, 0, 0))
3855 : (delta = 0));
3857 else
3858 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
3859 /* This is a main window followed by a minibuffer. */
3860 : !NILP (p->next) ? ((*sizefun) (p->next)
3861 - window_min_size (XWINDOW (p->next),
3862 widthflag, 0, 0))
3863 /* This is a minibuffer following a main window. */
3864 : !NILP (p->prev) ? ((*sizefun) (p->prev)
3865 - window_min_size (XWINDOW (p->prev),
3866 widthflag, 0, 0))
3867 /* This is a frame with only one window, a minibuffer-only
3868 or a minibufferless frame. */
3869 : (delta = 0));
3871 if (delta > maxdelta)
3872 /* This case traps trying to make the minibuffer
3873 the full frame, or make the only window aside from the
3874 minibuffer the full frame. */
3875 delta = maxdelta;
3878 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window), widthflag, 0, 0))
3880 delete_window (window);
3881 return;
3884 if (delta == 0)
3885 return;
3887 /* Find the total we can get from other siblings without deleting them. */
3888 maximum = 0;
3889 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
3890 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
3891 widthflag, 0, 0);
3892 if (! preserve_before)
3893 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
3894 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
3895 widthflag, 0, 0);
3897 /* If we can get it all from them without deleting them, do so. */
3898 if (delta <= maximum)
3900 Lisp_Object first_unaffected;
3901 Lisp_Object first_affected;
3902 int fixed_p;
3904 next = p->next;
3905 prev = p->prev;
3906 first_affected = window;
3907 /* Look at one sibling at a time,
3908 moving away from this window in both directions alternately,
3909 and take as much as we can get without deleting that sibling. */
3910 while (delta != 0
3911 && (!NILP (next) || (!preserve_before && !NILP (prev))))
3913 if (! NILP (next))
3915 int this_one = ((*sizefun) (next)
3916 - window_min_size (XWINDOW (next),
3917 widthflag, 0, &fixed_p));
3918 if (!fixed_p)
3920 if (this_one > delta)
3921 this_one = delta;
3923 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
3924 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
3926 delta -= this_one;
3929 next = XWINDOW (next)->next;
3932 if (delta == 0)
3933 break;
3935 if (!preserve_before && ! NILP (prev))
3937 int this_one = ((*sizefun) (prev)
3938 - window_min_size (XWINDOW (prev),
3939 widthflag, 0, &fixed_p));
3940 if (!fixed_p)
3942 if (this_one > delta)
3943 this_one = delta;
3945 first_affected = prev;
3947 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
3948 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
3950 delta -= this_one;
3953 prev = XWINDOW (prev)->prev;
3957 xassert (delta == 0);
3959 /* Now recalculate the edge positions of all the windows affected,
3960 based on the new sizes. */
3961 first_unaffected = next;
3962 prev = first_affected;
3963 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
3964 prev = next, next = XWINDOW (next)->next)
3966 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
3967 /* This does not change size of NEXT,
3968 but it propagates the new top edge to its children */
3969 (*setsizefun) (next, (*sizefun) (next), 0);
3972 else
3974 register int delta1;
3975 register int opht = (*sizefun) (parent);
3977 if (opht <= XINT (*sizep) + delta)
3979 /* If trying to grow this window to or beyond size of the parent,
3980 just delete all the sibling windows. */
3981 Lisp_Object start, tem, next;
3983 start = XWINDOW (parent)->vchild;
3984 if (NILP (start))
3985 start = XWINDOW (parent)->hchild;
3987 /* Delete any siblings that come after WINDOW. */
3988 tem = XWINDOW (window)->next;
3989 while (! NILP (tem))
3991 next = XWINDOW (tem)->next;
3992 delete_window (tem);
3993 tem = next;
3996 /* Delete any siblings that come after WINDOW.
3997 Note that if START is not WINDOW, then WINDOW still
3998 Fhas siblings, so WINDOW has not yet replaced its parent. */
3999 tem = start;
4000 while (! EQ (tem, window))
4002 next = XWINDOW (tem)->next;
4003 delete_window (tem);
4004 tem = next;
4007 else
4009 /* Otherwise, make delta1 just right so that if we add
4010 delta1 lines to this window and to the parent, and then
4011 shrink the parent back to its original size, the new
4012 proportional size of this window will increase by delta.
4014 The function size_window will compute the new height h'
4015 of the window from delta1 as:
4017 e = delta1/n
4018 x = delta1 - delta1/n * n for the 1st resizable child
4019 h' = h + e + x
4021 where n is the number of children that can be resized.
4022 We can ignore x by choosing a delta1 that is a multiple of
4023 n. We want the height of this window to come out as
4025 h' = h + delta
4027 So, delta1 must be
4029 h + e = h + delta
4030 delta1/n = delta
4031 delta1 = n * delta.
4033 The number of children n equals the number of resizable
4034 children of this window + 1 because we know window itself
4035 is resizable (otherwise we would have signalled an error. */
4037 struct window *w = XWINDOW (window);
4038 Lisp_Object s;
4039 int n = 1;
4041 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
4042 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
4043 ++n;
4044 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
4045 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
4046 ++n;
4048 delta1 = n * delta;
4050 /* Add delta1 lines or columns to this window, and to the parent,
4051 keeping things consistent while not affecting siblings. */
4052 XSETINT (CURSIZE (parent), opht + delta1);
4053 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4055 /* Squeeze out delta1 lines or columns from our parent,
4056 shriking this window and siblings proportionately.
4057 This brings parent back to correct size.
4058 Delta1 was calculated so this makes this window the desired size,
4059 taking it all out of the siblings. */
4060 (*setsizefun) (parent, opht, 0);
4065 XSETFASTINT (p->last_modified, 0);
4066 XSETFASTINT (p->last_overlay_modified, 0);
4068 /* Adjust glyph matrices. */
4069 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4072 #undef CURBEG
4073 #undef CURSIZE
4077 /***********************************************************************
4078 Resizing Mini-Windows
4079 ***********************************************************************/
4081 static void shrink_window_lowest_first P_ ((struct window *, int));
4083 enum save_restore_action
4085 CHECK_ORIG_SIZES,
4086 SAVE_ORIG_SIZES,
4087 RESTORE_ORIG_SIZES
4090 static int save_restore_orig_size P_ ((struct window *,
4091 enum save_restore_action));
4093 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4094 from lowest windows first. */
4096 static void
4097 shrink_window_lowest_first (w, height)
4098 struct window *w;
4099 int height;
4101 struct window *c;
4102 Lisp_Object child;
4103 int old_height;
4105 xassert (!MINI_WINDOW_P (w));
4107 /* Set redisplay hints. */
4108 XSETFASTINT (w->last_modified, 0);
4109 XSETFASTINT (w->last_overlay_modified, 0);
4110 windows_or_buffers_changed++;
4111 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4113 old_height = XFASTINT (w->total_lines);
4114 XSETFASTINT (w->total_lines, height);
4116 if (!NILP (w->hchild))
4118 for (child = w->hchild; !NILP (child); child = c->next)
4120 c = XWINDOW (child);
4121 c->top_line = w->top_line;
4122 shrink_window_lowest_first (c, height);
4125 else if (!NILP (w->vchild))
4127 Lisp_Object last_child;
4128 int delta = old_height - height;
4129 int last_top;
4131 last_child = Qnil;
4133 /* Find the last child. We are taking space from lowest windows
4134 first, so we iterate over children from the last child
4135 backwards. */
4136 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
4137 last_child = child;
4139 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
4140 for (child = last_child; delta && !NILP (child); child = c->prev)
4142 int this_one;
4144 c = XWINDOW (child);
4145 this_one = XFASTINT (c->total_lines) - MIN_SAFE_WINDOW_HEIGHT;
4147 if (this_one > delta)
4148 this_one = delta;
4150 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4151 delta -= this_one;
4154 /* Compute new positions. */
4155 last_top = XINT (w->top_line);
4156 for (child = w->vchild; !NILP (child); child = c->next)
4158 c = XWINDOW (child);
4159 c->top_line = make_number (last_top);
4160 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4161 last_top += XFASTINT (c->total_lines);
4167 /* Save, restore, or check positions and sizes in the window tree
4168 rooted at W. ACTION says what to do.
4170 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4171 orig_total_lines members are valid for all windows in the window
4172 tree. Value is non-zero if they are valid.
4174 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4175 orig_top_line and orig_total_lines for all windows in the tree.
4177 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4178 stored in orig_top_line and orig_total_lines for all windows. */
4180 static int
4181 save_restore_orig_size (w, action)
4182 struct window *w;
4183 enum save_restore_action action;
4185 int success_p = 1;
4187 while (w)
4189 if (!NILP (w->hchild))
4191 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4192 success_p = 0;
4194 else if (!NILP (w->vchild))
4196 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4197 success_p = 0;
4200 switch (action)
4202 case CHECK_ORIG_SIZES:
4203 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4204 return 0;
4205 break;
4207 case SAVE_ORIG_SIZES:
4208 w->orig_top_line = w->top_line;
4209 w->orig_total_lines = w->total_lines;
4210 XSETFASTINT (w->last_modified, 0);
4211 XSETFASTINT (w->last_overlay_modified, 0);
4212 break;
4214 case RESTORE_ORIG_SIZES:
4215 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4216 w->top_line = w->orig_top_line;
4217 w->total_lines = w->orig_total_lines;
4218 w->orig_total_lines = w->orig_top_line = Qnil;
4219 XSETFASTINT (w->last_modified, 0);
4220 XSETFASTINT (w->last_overlay_modified, 0);
4221 break;
4223 default:
4224 abort ();
4227 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4230 return success_p;
4234 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4235 without deleting other windows. */
4237 void
4238 grow_mini_window (w, delta)
4239 struct window *w;
4240 int delta;
4242 struct frame *f = XFRAME (w->frame);
4243 struct window *root;
4245 xassert (MINI_WINDOW_P (w));
4246 xassert (delta >= 0);
4248 /* Check values of window_min_width and window_min_height for
4249 validity. */
4250 check_min_window_sizes ();
4252 /* Compute how much we can enlarge the mini-window without deleting
4253 other windows. */
4254 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4255 if (delta)
4257 int min_height = window_min_size (root, 0, 0, 0);
4258 if (XFASTINT (root->total_lines) - delta < min_height)
4259 /* Note that the root window may already be smaller than
4260 min_height. */
4261 delta = max (0, XFASTINT (root->total_lines) - min_height);
4264 if (delta)
4266 /* Save original window sizes and positions, if not already done. */
4267 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4268 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4270 /* Shrink other windows. */
4271 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4273 /* Grow the mini-window. */
4274 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4275 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4276 XSETFASTINT (w->last_modified, 0);
4277 XSETFASTINT (w->last_overlay_modified, 0);
4279 adjust_glyphs (f);
4284 /* Shrink mini-window W. If there is recorded info about window sizes
4285 before a call to grow_mini_window, restore recorded window sizes.
4286 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4287 line. */
4289 void
4290 shrink_mini_window (w)
4291 struct window *w;
4293 struct frame *f = XFRAME (w->frame);
4294 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4296 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4298 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4299 adjust_glyphs (f);
4300 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4301 windows_or_buffers_changed = 1;
4303 else if (XFASTINT (w->total_lines) > 1)
4305 /* Distribute the additional lines of the mini-window
4306 among the other windows. */
4307 Lisp_Object window;
4308 XSETWINDOW (window, w);
4309 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0, 0);
4315 /* Mark window cursors off for all windows in the window tree rooted
4316 at W by setting their phys_cursor_on_p flag to zero. Called from
4317 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4318 the frame are cleared. */
4320 void
4321 mark_window_cursors_off (w)
4322 struct window *w;
4324 while (w)
4326 if (!NILP (w->hchild))
4327 mark_window_cursors_off (XWINDOW (w->hchild));
4328 else if (!NILP (w->vchild))
4329 mark_window_cursors_off (XWINDOW (w->vchild));
4330 else
4331 w->phys_cursor_on_p = 0;
4333 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4338 /* Return number of lines of text (not counting mode lines) in W. */
4341 window_internal_height (w)
4342 struct window *w;
4344 int ht = XFASTINT (w->total_lines);
4346 if (!MINI_WINDOW_P (w))
4348 if (!NILP (w->parent)
4349 || !NILP (w->vchild)
4350 || !NILP (w->hchild)
4351 || !NILP (w->next)
4352 || !NILP (w->prev)
4353 || WINDOW_WANTS_MODELINE_P (w))
4354 --ht;
4356 if (WINDOW_WANTS_HEADER_LINE_P (w))
4357 --ht;
4360 return ht;
4364 /* Return the number of columns in W.
4365 Don't count columns occupied by scroll bars or the vertical bar
4366 separating W from the sibling to its right. */
4369 window_box_text_cols (w)
4370 struct window *w;
4372 struct frame *f = XFRAME (WINDOW_FRAME (w));
4373 int width = XINT (w->total_cols);
4375 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4376 /* Scroll bars occupy a few columns. */
4377 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4378 else if (!FRAME_WINDOW_P (f)
4379 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4380 /* The column of `|' characters separating side-by-side windows
4381 occupies one column only. */
4382 width -= 1;
4384 if (FRAME_WINDOW_P (f))
4385 /* On window-systems, fringes and display margins cannot be
4386 used for normal text. */
4387 width -= (WINDOW_FRINGE_COLS (w)
4388 + WINDOW_LEFT_MARGIN_COLS (w)
4389 + WINDOW_RIGHT_MARGIN_COLS (w));
4391 return width;
4395 /************************************************************************
4396 Window Scrolling
4397 ***********************************************************************/
4399 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4400 N screen-fulls, which is defined as the height of the window minus
4401 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4402 instead. Negative values of N mean scroll down. NOERROR non-zero
4403 means don't signal an error if we try to move over BEGV or ZV,
4404 respectively. */
4406 static void
4407 window_scroll (window, n, whole, noerror)
4408 Lisp_Object window;
4409 int n;
4410 int whole;
4411 int noerror;
4413 immediate_quit = 1;
4415 /* If we must, use the pixel-based version which is much slower than
4416 the line-based one but can handle varying line heights. */
4417 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4418 window_scroll_pixel_based (window, n, whole, noerror);
4419 else
4420 window_scroll_line_based (window, n, whole, noerror);
4422 immediate_quit = 0;
4426 /* Implementation of window_scroll that works based on pixel line
4427 heights. See the comment of window_scroll for parameter
4428 descriptions. */
4430 static void
4431 window_scroll_pixel_based (window, n, whole, noerror)
4432 Lisp_Object window;
4433 int n;
4434 int whole;
4435 int noerror;
4437 struct it it;
4438 struct window *w = XWINDOW (window);
4439 struct text_pos start;
4440 Lisp_Object tem;
4441 int this_scroll_margin;
4442 int preserve_y;
4443 /* True if we fiddled the window vscroll field without really scrolling. */
4444 int vscrolled = 0;
4446 SET_TEXT_POS_FROM_MARKER (start, w->start);
4448 /* If PT is not visible in WINDOW, move back one half of
4449 the screen. Allow PT to be partially visible, otherwise
4450 something like (scroll-down 1) with PT in the line before
4451 the partially visible one would recenter. */
4452 tem = Fpos_visible_in_window_p (make_number (PT), window, Qt);
4453 if (NILP (tem))
4455 /* Move backward half the height of the window. Performance note:
4456 vmotion used here is about 10% faster, but would give wrong
4457 results for variable height lines. */
4458 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4459 it.current_y = it.last_visible_y;
4460 move_it_vertically (&it, - window_box_height (w) / 2);
4462 /* The function move_iterator_vertically may move over more than
4463 the specified y-distance. If it->w is small, e.g. a
4464 mini-buffer window, we may end up in front of the window's
4465 display area. This is the case when Start displaying at the
4466 start of the line containing PT in this case. */
4467 if (it.current_y <= 0)
4469 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4470 move_it_vertically (&it, 0);
4471 it.current_y = 0;
4474 start = it.current.pos;
4477 /* If scroll_preserve_screen_position is non-zero, we try to set
4478 point in the same window line as it is now, so get that line. */
4479 if (!NILP (Vscroll_preserve_screen_position))
4481 start_display (&it, w, start);
4482 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4483 preserve_y = it.current_y;
4485 else
4486 preserve_y = -1;
4488 /* Move iterator it from start the specified distance forward or
4489 backward. The result is the new window start. */
4490 start_display (&it, w, start);
4491 if (whole)
4493 int screen_full = (window_box_height (w)
4494 - next_screen_context_lines * FRAME_LINE_HEIGHT (it.f));
4495 int dy = n * screen_full;
4497 /* Note that move_it_vertically always moves the iterator to the
4498 start of a line. So, if the last line doesn't have a newline,
4499 we would end up at the start of the line ending at ZV. */
4500 if (dy <= 0)
4501 move_it_vertically_backward (&it, -dy);
4502 else if (dy > 0)
4503 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4504 MOVE_TO_POS | MOVE_TO_Y);
4506 else
4507 move_it_by_lines (&it, n, 1);
4509 /* We failed if we find ZV is already on the screen (scrolling up,
4510 means there's nothing past the end), or if we can't start any
4511 earlier (scrolling down, means there's nothing past the top). */
4512 if ((n > 0 && IT_CHARPOS (it) == ZV)
4513 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4515 if (IT_CHARPOS (it) == ZV)
4517 if (it.current_y < it.last_visible_y
4518 && (it.current_y + it.max_ascent + it.max_descent
4519 >= it.last_visible_y))
4521 /* The last line was only partially visible, make it fully
4522 visible. */
4523 w->vscroll = (it.last_visible_y
4524 - it.current_y + it.max_ascent + it.max_descent);
4525 adjust_glyphs (it.f);
4527 else if (noerror)
4528 return;
4529 else
4530 Fsignal (Qend_of_buffer, Qnil);
4532 else
4534 if (w->vscroll != 0)
4535 /* The first line was only partially visible, make it fully
4536 visible. */
4537 w->vscroll = 0;
4538 else if (noerror)
4539 return;
4540 else
4541 Fsignal (Qbeginning_of_buffer, Qnil);
4544 /* If control gets here, then we vscrolled. */
4546 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4548 /* Don't try to change the window start below. */
4549 vscrolled = 1;
4552 if (! vscrolled)
4554 int pos = IT_CHARPOS (it);
4555 int bytepos;
4557 /* If in the middle of a multi-glyph character move forward to
4558 the next character. */
4559 if (in_display_vector_p (&it))
4561 ++pos;
4562 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4565 /* Set the window start, and set up the window for redisplay. */
4566 set_marker_restricted (w->start, make_number (pos),
4567 w->buffer);
4568 bytepos = XMARKER (w->start)->bytepos;
4569 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4570 ? Qt : Qnil);
4571 w->update_mode_line = Qt;
4572 XSETFASTINT (w->last_modified, 0);
4573 XSETFASTINT (w->last_overlay_modified, 0);
4574 /* Set force_start so that redisplay_window will run the
4575 window-scroll-functions. */
4576 w->force_start = Qt;
4579 it.current_y = it.vpos = 0;
4581 /* Preserve the screen position if we must. */
4582 if (preserve_y >= 0)
4584 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
4585 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4587 else
4589 /* Move PT out of scroll margins. */
4590 this_scroll_margin = max (0, scroll_margin);
4591 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4592 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4594 if (n > 0)
4596 /* We moved the window start towards ZV, so PT may be now
4597 in the scroll margin at the top. */
4598 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4599 while (it.current_y < this_scroll_margin)
4601 int prev = it.current_y;
4602 move_it_by_lines (&it, 1, 1);
4603 if (prev == it.current_y)
4604 break;
4606 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4608 else if (n < 0)
4610 int charpos, bytepos;
4612 /* We moved the window start towards BEGV, so PT may be now
4613 in the scroll margin at the bottom. */
4614 move_it_to (&it, PT, -1,
4615 it.last_visible_y - this_scroll_margin - 1, -1,
4616 MOVE_TO_POS | MOVE_TO_Y);
4618 /* Save our position, in case it's correct. */
4619 charpos = IT_CHARPOS (it);
4620 bytepos = IT_BYTEPOS (it);
4622 /* See if point is on a partially visible line at the end. */
4623 move_it_by_lines (&it, 1, 1);
4624 if (it.current_y > it.last_visible_y)
4625 /* The last line was only partially visible, so back up two
4626 lines to make sure we're on a fully visible line. */
4628 move_it_by_lines (&it, -2, 0);
4629 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4631 else
4632 /* No, the position we saved is OK, so use it. */
4633 SET_PT_BOTH (charpos, bytepos);
4639 /* Implementation of window_scroll that works based on screen lines.
4640 See the comment of window_scroll for parameter descriptions. */
4642 static void
4643 window_scroll_line_based (window, n, whole, noerror)
4644 Lisp_Object window;
4645 int n;
4646 int whole;
4647 int noerror;
4649 register struct window *w = XWINDOW (window);
4650 register int opoint = PT, opoint_byte = PT_BYTE;
4651 register int pos, pos_byte;
4652 register int ht = window_internal_height (w);
4653 register Lisp_Object tem;
4654 int lose;
4655 Lisp_Object bolp;
4656 int startpos;
4657 struct position posit;
4658 int original_vpos;
4660 /* If scrolling screen-fulls, compute the number of lines to
4661 scroll from the window's height. */
4662 if (whole)
4663 n *= max (1, ht - next_screen_context_lines);
4665 startpos = marker_position (w->start);
4667 posit = *compute_motion (startpos, 0, 0, 0,
4668 PT, ht, 0,
4669 window_box_text_cols (w), XINT (w->hscroll),
4670 0, w);
4671 original_vpos = posit.vpos;
4673 XSETFASTINT (tem, PT);
4674 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4676 if (NILP (tem))
4678 Fvertical_motion (make_number (- (ht / 2)), window);
4679 startpos = PT;
4682 SET_PT (startpos);
4683 lose = n < 0 && PT == BEGV;
4684 Fvertical_motion (make_number (n), window);
4685 pos = PT;
4686 pos_byte = PT_BYTE;
4687 bolp = Fbolp ();
4688 SET_PT_BOTH (opoint, opoint_byte);
4690 if (lose)
4692 if (noerror)
4693 return;
4694 else
4695 Fsignal (Qbeginning_of_buffer, Qnil);
4698 if (pos < ZV)
4700 int this_scroll_margin = scroll_margin;
4702 /* Don't use a scroll margin that is negative or too large. */
4703 if (this_scroll_margin < 0)
4704 this_scroll_margin = 0;
4706 if (XINT (w->total_lines) < 4 * scroll_margin)
4707 this_scroll_margin = XINT (w->total_lines) / 4;
4709 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4710 w->start_at_line_beg = bolp;
4711 w->update_mode_line = Qt;
4712 XSETFASTINT (w->last_modified, 0);
4713 XSETFASTINT (w->last_overlay_modified, 0);
4714 /* Set force_start so that redisplay_window will run
4715 the window-scroll-functions. */
4716 w->force_start = Qt;
4718 if (whole && !NILP (Vscroll_preserve_screen_position))
4720 SET_PT_BOTH (pos, pos_byte);
4721 Fvertical_motion (make_number (original_vpos), window);
4723 /* If we scrolled forward, put point enough lines down
4724 that it is outside the scroll margin. */
4725 else if (n > 0)
4727 int top_margin;
4729 if (this_scroll_margin > 0)
4731 SET_PT_BOTH (pos, pos_byte);
4732 Fvertical_motion (make_number (this_scroll_margin), window);
4733 top_margin = PT;
4735 else
4736 top_margin = pos;
4738 if (top_margin <= opoint)
4739 SET_PT_BOTH (opoint, opoint_byte);
4740 else if (!NILP (Vscroll_preserve_screen_position))
4742 SET_PT_BOTH (pos, pos_byte);
4743 Fvertical_motion (make_number (original_vpos), window);
4745 else
4746 SET_PT (top_margin);
4748 else if (n < 0)
4750 int bottom_margin;
4752 /* If we scrolled backward, put point near the end of the window
4753 but not within the scroll margin. */
4754 SET_PT_BOTH (pos, pos_byte);
4755 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4756 if (XFASTINT (tem) == ht - this_scroll_margin)
4757 bottom_margin = PT;
4758 else
4759 bottom_margin = PT + 1;
4761 if (bottom_margin > opoint)
4762 SET_PT_BOTH (opoint, opoint_byte);
4763 else
4765 if (!NILP (Vscroll_preserve_screen_position))
4767 SET_PT_BOTH (pos, pos_byte);
4768 Fvertical_motion (make_number (original_vpos), window);
4770 else
4771 Fvertical_motion (make_number (-1), window);
4775 else
4777 if (noerror)
4778 return;
4779 else
4780 Fsignal (Qend_of_buffer, Qnil);
4785 /* Scroll selected_window up or down. If N is nil, scroll a
4786 screen-full which is defined as the height of the window minus
4787 next_screen_context_lines. If N is the symbol `-', scroll.
4788 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4789 up. This is the guts of Fscroll_up and Fscroll_down. */
4791 static void
4792 scroll_command (n, direction)
4793 Lisp_Object n;
4794 int direction;
4796 int count = SPECPDL_INDEX ();
4798 xassert (abs (direction) == 1);
4800 /* If selected window's buffer isn't current, make it current for
4801 the moment. But don't screw up if window_scroll gets an error. */
4802 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4804 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4805 Fset_buffer (XWINDOW (selected_window)->buffer);
4807 /* Make redisplay consider other windows than just selected_window. */
4808 ++windows_or_buffers_changed;
4811 if (NILP (n))
4812 window_scroll (selected_window, direction, 1, 0);
4813 else if (EQ (n, Qminus))
4814 window_scroll (selected_window, -direction, 1, 0);
4815 else
4817 n = Fprefix_numeric_value (n);
4818 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4821 unbind_to (count, Qnil);
4824 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
4825 doc: /* Scroll text of current window upward ARG lines; or near full screen if no ARG.
4826 A near full screen is `next-screen-context-lines' less than a full screen.
4827 Negative ARG means scroll downward.
4828 If ARG is the atom `-', scroll downward by nearly full screen.
4829 When calling from a program, supply as argument a number, nil, or `-'. */)
4830 (arg)
4831 Lisp_Object arg;
4833 scroll_command (arg, 1);
4834 return Qnil;
4837 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
4838 doc: /* Scroll text of current window down ARG lines; or near full screen if no ARG.
4839 A near full screen is `next-screen-context-lines' less than a full screen.
4840 Negative ARG means scroll upward.
4841 If ARG is the atom `-', scroll upward by nearly full screen.
4842 When calling from a program, supply as argument a number, nil, or `-'. */)
4843 (arg)
4844 Lisp_Object arg;
4846 scroll_command (arg, -1);
4847 return Qnil;
4850 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4851 doc: /* Return the other window for \"other window scroll\" commands.
4852 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4853 specifies the window.
4854 If `other-window-scroll-buffer' is non-nil, a window
4855 showing that buffer is used. */)
4858 Lisp_Object window;
4860 if (MINI_WINDOW_P (XWINDOW (selected_window))
4861 && !NILP (Vminibuf_scroll_window))
4862 window = Vminibuf_scroll_window;
4863 /* If buffer is specified, scroll that buffer. */
4864 else if (!NILP (Vother_window_scroll_buffer))
4866 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4867 if (NILP (window))
4868 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4870 else
4872 /* Nothing specified; look for a neighboring window on the same
4873 frame. */
4874 window = Fnext_window (selected_window, Qnil, Qnil);
4876 if (EQ (window, selected_window))
4877 /* That didn't get us anywhere; look for a window on another
4878 visible frame. */
4880 window = Fnext_window (window, Qnil, Qt);
4881 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4882 && ! EQ (window, selected_window));
4885 CHECK_LIVE_WINDOW (window);
4887 if (EQ (window, selected_window))
4888 error ("There is no other window");
4890 return window;
4893 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4894 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4895 A near full screen is `next-screen-context-lines' less than a full screen.
4896 The next window is the one below the current one; or the one at the top
4897 if the current one is at the bottom. Negative ARG means scroll downward.
4898 If ARG is the atom `-', scroll downward by nearly full screen.
4899 When calling from a program, supply as argument a number, nil, or `-'.
4901 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4902 specifies the window to scroll.
4903 If `other-window-scroll-buffer' is non-nil, scroll the window
4904 showing that buffer, popping the buffer up if necessary. */)
4905 (arg)
4906 Lisp_Object arg;
4908 Lisp_Object window;
4909 struct window *w;
4910 int count = SPECPDL_INDEX ();
4912 window = Fother_window_for_scrolling ();
4913 w = XWINDOW (window);
4915 /* Don't screw up if window_scroll gets an error. */
4916 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4917 ++windows_or_buffers_changed;
4919 Fset_buffer (w->buffer);
4920 SET_PT (marker_position (w->pointm));
4922 if (NILP (arg))
4923 window_scroll (window, 1, 1, 1);
4924 else if (EQ (arg, Qminus))
4925 window_scroll (window, -1, 1, 1);
4926 else
4928 if (CONSP (arg))
4929 arg = Fcar (arg);
4930 CHECK_NUMBER (arg);
4931 window_scroll (window, XINT (arg), 0, 1);
4934 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4935 unbind_to (count, Qnil);
4937 return Qnil;
4940 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
4941 doc: /* Scroll selected window display ARG columns left.
4942 Default for ARG is window width minus 2.
4943 Value is the total amount of leftward horizontal scrolling in
4944 effect after the change.
4945 If `automatic-hscrolling' is non-nil, the argument ARG modifies
4946 a lower bound for automatic scrolling, i.e. automatic scrolling
4947 will not scroll a window to a column less than the value returned
4948 by this function. */)
4949 (arg)
4950 register Lisp_Object arg;
4952 Lisp_Object result;
4953 int hscroll;
4954 struct window *w = XWINDOW (selected_window);
4956 if (NILP (arg))
4957 XSETFASTINT (arg, window_box_text_cols (w) - 2);
4958 else
4959 arg = Fprefix_numeric_value (arg);
4961 hscroll = XINT (w->hscroll) + XINT (arg);
4962 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4964 if (interactive_p (0))
4965 w->min_hscroll = w->hscroll;
4967 return result;
4970 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
4971 doc: /* Scroll selected window display ARG columns right.
4972 Default for ARG is window width minus 2.
4973 Value is the total amount of leftward horizontal scrolling in
4974 effect after the change.
4975 If `automatic-hscrolling' is non-nil, the argument ARG modifies
4976 a lower bound for automatic scrolling, i.e. automatic scrolling
4977 will not scroll a window to a column less than the value returned
4978 by this function. */)
4979 (arg)
4980 register Lisp_Object arg;
4982 Lisp_Object result;
4983 int hscroll;
4984 struct window *w = XWINDOW (selected_window);
4986 if (NILP (arg))
4987 XSETFASTINT (arg, window_box_text_cols (w) - 2);
4988 else
4989 arg = Fprefix_numeric_value (arg);
4991 hscroll = XINT (w->hscroll) - XINT (arg);
4992 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4994 if (interactive_p (0))
4995 w->min_hscroll = w->hscroll;
4997 return result;
5000 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5001 doc: /* Return the window which was selected when entering the minibuffer.
5002 Returns nil, if current window is not a minibuffer window. */)
5005 if (minibuf_level > 0
5006 && MINI_WINDOW_P (XWINDOW (selected_window))
5007 && WINDOW_LIVE_P (minibuf_selected_window))
5008 return minibuf_selected_window;
5010 return Qnil;
5013 /* Value is the number of lines actually displayed in window W,
5014 as opposed to its height. */
5016 static int
5017 displayed_window_lines (w)
5018 struct window *w;
5020 struct it it;
5021 struct text_pos start;
5022 int height = window_box_height (w);
5023 struct buffer *old_buffer;
5024 int bottom_y;
5026 if (XBUFFER (w->buffer) != current_buffer)
5028 old_buffer = current_buffer;
5029 set_buffer_internal (XBUFFER (w->buffer));
5031 else
5032 old_buffer = NULL;
5034 /* In case W->start is out of the accessible range, do something
5035 reasonable. This happens in Info mode when Info-scroll-down
5036 calls (recenter -1) while W->start is 1. */
5037 if (XMARKER (w->start)->charpos < BEGV)
5038 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5039 else if (XMARKER (w->start)->charpos > ZV)
5040 SET_TEXT_POS (start, ZV, ZV_BYTE);
5041 else
5042 SET_TEXT_POS_FROM_MARKER (start, w->start);
5044 start_display (&it, w, start);
5045 move_it_vertically (&it, height);
5046 bottom_y = line_bottom_y (&it);
5048 /* rms: On a non-window display,
5049 the value of it.vpos at the bottom of the screen
5050 seems to be 1 larger than window_box_height (w).
5051 This kludge fixes a bug whereby (move-to-window-line -1)
5052 when ZV is on the last screen line
5053 moves to the previous screen line instead of the last one. */
5054 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5055 height++;
5057 /* Add in empty lines at the bottom of the window. */
5058 if (bottom_y < height)
5060 int uy = FRAME_LINE_HEIGHT (it.f);
5061 it.vpos += (height - bottom_y + uy - 1) / uy;
5064 if (old_buffer)
5065 set_buffer_internal (old_buffer);
5067 return it.vpos;
5071 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5072 doc: /* Center point in window and redisplay frame.
5073 With prefix argument ARG, recenter putting point on screen line ARG
5074 relative to the current window. If ARG is negative, it counts up from the
5075 bottom of the window. (ARG should be less than the height of the window.)
5077 If ARG is omitted or nil, erase the entire frame and then
5078 redraw with point in the center of the current window.
5079 Just C-u as prefix means put point in the center of the window
5080 and redisplay normally--don't erase and redraw the frame. */)
5081 (arg)
5082 register Lisp_Object arg;
5084 struct window *w = XWINDOW (selected_window);
5085 struct buffer *buf = XBUFFER (w->buffer);
5086 struct buffer *obuf = current_buffer;
5087 int center_p = 0;
5088 int charpos, bytepos;
5090 /* If redisplay is suppressed due to an error, try again. */
5091 obuf->display_error_modiff = 0;
5093 if (NILP (arg))
5095 int i;
5097 /* Invalidate pixel data calculated for all compositions. */
5098 for (i = 0; i < n_compositions; i++)
5099 composition_table[i]->font = NULL;
5101 Fredraw_frame (w->frame);
5102 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
5103 center_p = 1;
5105 else if (CONSP (arg)) /* Just C-u. */
5106 center_p = 1;
5107 else
5109 arg = Fprefix_numeric_value (arg);
5110 CHECK_NUMBER (arg);
5113 set_buffer_internal (buf);
5115 /* Handle centering on a graphical frame specially. Such frames can
5116 have variable-height lines and centering point on the basis of
5117 line counts would lead to strange effects. */
5118 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5120 if (center_p)
5122 struct it it;
5123 struct text_pos pt;
5125 SET_TEXT_POS (pt, PT, PT_BYTE);
5126 start_display (&it, w, pt);
5127 move_it_vertically (&it, - window_box_height (w) / 2);
5128 charpos = IT_CHARPOS (it);
5129 bytepos = IT_BYTEPOS (it);
5131 else if (XINT (arg) < 0)
5133 struct it it;
5134 struct text_pos pt;
5135 int y0, y1, h, nlines;
5137 SET_TEXT_POS (pt, PT, PT_BYTE);
5138 start_display (&it, w, pt);
5139 y0 = it.current_y;
5141 /* The amount of pixels we have to move back is the window
5142 height minus what's displayed in the line containing PT,
5143 and the lines below. */
5144 nlines = - XINT (arg) - 1;
5145 move_it_by_lines (&it, nlines, 1);
5147 y1 = line_bottom_y (&it);
5149 /* If we can't move down NLINES lines because we hit
5150 the end of the buffer, count in some empty lines. */
5151 if (it.vpos < nlines)
5152 y1 += (nlines - it.vpos) * FRAME_LINE_HEIGHT (it.f);
5154 h = window_box_height (w) - (y1 - y0);
5156 start_display (&it, w, pt);
5157 move_it_vertically (&it, - h);
5158 charpos = IT_CHARPOS (it);
5159 bytepos = IT_BYTEPOS (it);
5161 else
5163 struct position pos;
5164 pos = *vmotion (PT, - XINT (arg), w);
5165 charpos = pos.bufpos;
5166 bytepos = pos.bytepos;
5169 else
5171 struct position pos;
5172 int ht = window_internal_height (w);
5174 if (center_p)
5175 arg = make_number (ht / 2);
5176 else if (XINT (arg) < 0)
5177 arg = make_number (XINT (arg) + ht);
5179 pos = *vmotion (PT, - XINT (arg), w);
5180 charpos = pos.bufpos;
5181 bytepos = pos.bytepos;
5184 /* Set the new window start. */
5185 set_marker_both (w->start, w->buffer, charpos, bytepos);
5186 w->window_end_valid = Qnil;
5188 w->optional_new_start = Qt;
5190 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5191 w->start_at_line_beg = Qt;
5192 else
5193 w->start_at_line_beg = Qnil;
5195 set_buffer_internal (obuf);
5196 return Qnil;
5200 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5201 0, 1, 0,
5202 doc: /* Return the height in lines of the text display area of WINDOW.
5203 This doesn't include the mode-line (or header-line if any) or any
5204 partial-height lines in the text display area. */)
5205 (window)
5206 Lisp_Object window;
5208 struct window *w = decode_window (window);
5209 int pixel_height = window_box_height (w);
5210 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5211 return make_number (line_height);
5216 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5217 1, 1, "P",
5218 doc: /* Position point relative to window.
5219 With no argument, position point at center of window.
5220 An argument specifies vertical position within the window;
5221 zero means top of window, negative means relative to bottom of window. */)
5222 (arg)
5223 Lisp_Object arg;
5225 struct window *w = XWINDOW (selected_window);
5226 int lines, start;
5227 Lisp_Object window;
5229 window = selected_window;
5230 start = marker_position (w->start);
5231 if (start < BEGV || start > ZV)
5233 int height = window_internal_height (w);
5234 Fvertical_motion (make_number (- (height / 2)), window);
5235 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5236 w->start_at_line_beg = Fbolp ();
5237 w->force_start = Qt;
5239 else
5240 Fgoto_char (w->start);
5242 lines = displayed_window_lines (w);
5243 if (NILP (arg))
5244 XSETFASTINT (arg, lines / 2);
5245 else
5247 arg = Fprefix_numeric_value (arg);
5248 if (XINT (arg) < 0)
5249 XSETINT (arg, XINT (arg) + lines);
5252 /* Skip past a partially visible first line. */
5253 if (w->vscroll)
5254 XSETINT (arg, XINT (arg) + 1);
5256 return Fvertical_motion (arg, window);
5261 /***********************************************************************
5262 Window Configuration
5263 ***********************************************************************/
5265 struct save_window_data
5267 EMACS_INT size_from_Lisp_Vector_struct;
5268 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5269 Lisp_Object frame_cols, frame_lines, frame_menu_bar_lines;
5270 Lisp_Object frame_tool_bar_lines;
5271 Lisp_Object selected_frame;
5272 Lisp_Object current_window;
5273 Lisp_Object current_buffer;
5274 Lisp_Object minibuf_scroll_window;
5275 Lisp_Object minibuf_selected_window;
5276 Lisp_Object root_window;
5277 Lisp_Object focus_frame;
5278 /* Record the values of window-min-width and window-min-height
5279 so that window sizes remain consistent with them. */
5280 Lisp_Object min_width, min_height;
5281 /* A vector, each of whose elements is a struct saved_window
5282 for one window. */
5283 Lisp_Object saved_windows;
5286 /* This is saved as a Lisp_Vector */
5287 struct saved_window
5289 /* these first two must agree with struct Lisp_Vector in lisp.h */
5290 EMACS_INT size_from_Lisp_Vector_struct;
5291 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5293 Lisp_Object window;
5294 Lisp_Object buffer, start, pointm, mark;
5295 Lisp_Object left_col, top_line, total_cols, total_lines;
5296 Lisp_Object hscroll, min_hscroll;
5297 Lisp_Object parent, prev;
5298 Lisp_Object start_at_line_beg;
5299 Lisp_Object display_table;
5300 Lisp_Object orig_top_line, orig_total_lines;
5301 Lisp_Object left_margin_cols, right_margin_cols;
5302 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5303 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5306 #define SAVED_WINDOW_VECTOR_SIZE 24 /* Arg to Fmake_vector */
5308 #define SAVED_WINDOW_N(swv,n) \
5309 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5311 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5312 doc: /* Return t if OBJECT is a window-configuration object. */)
5313 (object)
5314 Lisp_Object object;
5316 if (WINDOW_CONFIGURATIONP (object))
5317 return Qt;
5318 return Qnil;
5321 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5322 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5323 (config)
5324 Lisp_Object config;
5326 register struct save_window_data *data;
5327 struct Lisp_Vector *saved_windows;
5329 if (! WINDOW_CONFIGURATIONP (config))
5330 wrong_type_argument (Qwindow_configuration_p, config);
5332 data = (struct save_window_data *) XVECTOR (config);
5333 saved_windows = XVECTOR (data->saved_windows);
5334 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5337 DEFUN ("set-window-configuration", Fset_window_configuration,
5338 Sset_window_configuration, 1, 1, 0,
5339 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5340 CONFIGURATION must be a value previously returned
5341 by `current-window-configuration' (which see).
5342 If CONFIGURATION was made from a frame that is now deleted,
5343 only frame-independent values can be restored. In this case,
5344 the return value is nil. Otherwise the value is t. */)
5345 (configuration)
5346 Lisp_Object configuration;
5348 register struct save_window_data *data;
5349 struct Lisp_Vector *saved_windows;
5350 Lisp_Object new_current_buffer;
5351 Lisp_Object frame;
5352 FRAME_PTR f;
5353 int old_point = -1;
5355 while (!WINDOW_CONFIGURATIONP (configuration))
5356 wrong_type_argument (Qwindow_configuration_p, configuration);
5358 data = (struct save_window_data *) XVECTOR (configuration);
5359 saved_windows = XVECTOR (data->saved_windows);
5361 new_current_buffer = data->current_buffer;
5362 if (NILP (XBUFFER (new_current_buffer)->name))
5363 new_current_buffer = Qnil;
5364 else
5366 if (XBUFFER (new_current_buffer) == current_buffer)
5367 old_point = PT;
5368 else
5369 old_point = BUF_PT (XBUFFER (new_current_buffer));
5372 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5373 f = XFRAME (frame);
5375 /* If f is a dead frame, don't bother rebuilding its window tree.
5376 However, there is other stuff we should still try to do below. */
5377 if (FRAME_LIVE_P (f))
5379 register struct window *w;
5380 register struct saved_window *p;
5381 struct window *root_window;
5382 struct window **leaf_windows;
5383 int n_leaf_windows;
5384 int k, i, n;
5386 /* If the frame has been resized since this window configuration was
5387 made, we change the frame to the size specified in the
5388 configuration, restore the configuration, and then resize it
5389 back. We keep track of the prevailing height in these variables. */
5390 int previous_frame_lines = FRAME_LINES (f);
5391 int previous_frame_cols = FRAME_COLS (f);
5392 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5393 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5395 /* The mouse highlighting code could get screwed up
5396 if it runs during this. */
5397 BLOCK_INPUT;
5399 if (XFASTINT (data->frame_lines) != previous_frame_lines
5400 || XFASTINT (data->frame_cols) != previous_frame_cols)
5401 change_frame_size (f, XFASTINT (data->frame_lines),
5402 XFASTINT (data->frame_cols), 0, 0, 0);
5403 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5404 if (XFASTINT (data->frame_menu_bar_lines)
5405 != previous_frame_menu_bar_lines)
5406 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
5407 #ifdef HAVE_WINDOW_SYSTEM
5408 if (XFASTINT (data->frame_tool_bar_lines)
5409 != previous_frame_tool_bar_lines)
5410 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
5411 #endif
5412 #endif
5414 /* "Swap out" point from the selected window
5415 into its buffer. We do this now, before
5416 restoring the window contents, and prevent it from
5417 being done later on when we select a new window. */
5418 if (! NILP (XWINDOW (selected_window)->buffer))
5420 w = XWINDOW (selected_window);
5421 set_marker_both (w->pointm,
5422 w->buffer,
5423 BUF_PT (XBUFFER (w->buffer)),
5424 BUF_PT_BYTE (XBUFFER (w->buffer)));
5427 windows_or_buffers_changed++;
5428 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5430 /* Problem: Freeing all matrices and later allocating them again
5431 is a serious redisplay flickering problem. What we would
5432 really like to do is to free only those matrices not reused
5433 below. */
5434 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5435 leaf_windows
5436 = (struct window **) alloca (count_windows (root_window)
5437 * sizeof (struct window *));
5438 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5440 /* Temporarily avoid any problems with windows that are smaller
5441 than they are supposed to be. */
5442 window_min_height = 1;
5443 window_min_width = 1;
5445 /* Kludge Alert!
5446 Mark all windows now on frame as "deleted".
5447 Restoring the new configuration "undeletes" any that are in it.
5449 Save their current buffers in their height fields, since we may
5450 need it later, if a buffer saved in the configuration is now
5451 dead. */
5452 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5454 for (k = 0; k < saved_windows->size; k++)
5456 p = SAVED_WINDOW_N (saved_windows, k);
5457 w = XWINDOW (p->window);
5458 w->next = Qnil;
5460 if (!NILP (p->parent))
5461 w->parent = SAVED_WINDOW_N (saved_windows,
5462 XFASTINT (p->parent))->window;
5463 else
5464 w->parent = Qnil;
5466 if (!NILP (p->prev))
5468 w->prev = SAVED_WINDOW_N (saved_windows,
5469 XFASTINT (p->prev))->window;
5470 XWINDOW (w->prev)->next = p->window;
5472 else
5474 w->prev = Qnil;
5475 if (!NILP (w->parent))
5477 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5479 XWINDOW (w->parent)->vchild = p->window;
5480 XWINDOW (w->parent)->hchild = Qnil;
5482 else
5484 XWINDOW (w->parent)->hchild = p->window;
5485 XWINDOW (w->parent)->vchild = Qnil;
5490 /* If we squirreled away the buffer in the window's height,
5491 restore it now. */
5492 if (BUFFERP (w->total_lines))
5493 w->buffer = w->total_lines;
5494 w->left_col = p->left_col;
5495 w->top_line = p->top_line;
5496 w->total_cols = p->total_cols;
5497 w->total_lines = p->total_lines;
5498 w->hscroll = p->hscroll;
5499 w->min_hscroll = p->min_hscroll;
5500 w->display_table = p->display_table;
5501 w->orig_top_line = p->orig_top_line;
5502 w->orig_total_lines = p->orig_total_lines;
5503 w->left_margin_cols = p->left_margin_cols;
5504 w->right_margin_cols = p->right_margin_cols;
5505 w->left_fringe_width = p->left_fringe_width;
5506 w->right_fringe_width = p->right_fringe_width;
5507 w->fringes_outside_margins = p->fringes_outside_margins;
5508 w->scroll_bar_width = p->scroll_bar_width;
5509 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
5510 XSETFASTINT (w->last_modified, 0);
5511 XSETFASTINT (w->last_overlay_modified, 0);
5513 /* Reinstall the saved buffer and pointers into it. */
5514 if (NILP (p->buffer))
5515 w->buffer = p->buffer;
5516 else
5518 if (!NILP (XBUFFER (p->buffer)->name))
5519 /* If saved buffer is alive, install it. */
5521 w->buffer = p->buffer;
5522 w->start_at_line_beg = p->start_at_line_beg;
5523 set_marker_restricted (w->start, p->start, w->buffer);
5524 set_marker_restricted (w->pointm, p->pointm, w->buffer);
5525 Fset_marker (XBUFFER (w->buffer)->mark,
5526 p->mark, w->buffer);
5528 /* As documented in Fcurrent_window_configuration, don't
5529 restore the location of point in the buffer which was
5530 current when the window configuration was recorded. */
5531 if (!EQ (p->buffer, new_current_buffer)
5532 && XBUFFER (p->buffer) == current_buffer)
5533 Fgoto_char (w->pointm);
5535 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
5536 /* Else unless window has a live buffer, get one. */
5538 w->buffer = Fcdr (Fcar (Vbuffer_alist));
5539 /* This will set the markers to beginning of visible
5540 range. */
5541 set_marker_restricted (w->start, make_number (0), w->buffer);
5542 set_marker_restricted (w->pointm, make_number (0),w->buffer);
5543 w->start_at_line_beg = Qt;
5545 else
5546 /* Keeping window's old buffer; make sure the markers
5547 are real. */
5549 /* Set window markers at start of visible range. */
5550 if (XMARKER (w->start)->buffer == 0)
5551 set_marker_restricted (w->start, make_number (0),
5552 w->buffer);
5553 if (XMARKER (w->pointm)->buffer == 0)
5554 set_marker_restricted_both (w->pointm, w->buffer,
5555 BUF_PT (XBUFFER (w->buffer)),
5556 BUF_PT_BYTE (XBUFFER (w->buffer)));
5557 w->start_at_line_beg = Qt;
5562 FRAME_ROOT_WINDOW (f) = data->root_window;
5563 /* Prevent "swapping out point" in the old selected window
5564 using the buffer that has been restored into it.
5565 Use the point value from the beginning of this function
5566 since unshow_buffer (called from delete_all_subwindows)
5567 could have altered it. */
5568 selected_window = Qnil;
5569 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5570 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5571 make_number (old_point),
5572 XWINDOW (data->current_window)->buffer);
5574 Fselect_window (data->current_window, Qnil);
5575 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
5576 = selected_window;
5578 if (NILP (data->focus_frame)
5579 || (FRAMEP (data->focus_frame)
5580 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5581 Fredirect_frame_focus (frame, data->focus_frame);
5583 #if 0 /* I don't understand why this is needed, and it causes problems
5584 when the frame's old selected window has been deleted. */
5585 if (f != selected_frame && FRAME_WINDOW_P (f))
5586 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
5587 0, 0);
5588 #endif
5590 /* Set the screen height to the value it had before this function. */
5591 if (previous_frame_lines != FRAME_LINES (f)
5592 || previous_frame_cols != FRAME_COLS (f))
5593 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5594 0, 0, 0);
5595 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5596 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5597 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5598 make_number (0));
5599 #ifdef HAVE_WINDOW_SYSTEM
5600 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5601 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5602 make_number (0));
5603 #endif
5604 #endif
5606 /* Now, free glyph matrices in windows that were not reused. */
5607 for (i = n = 0; i < n_leaf_windows; ++i)
5609 if (NILP (leaf_windows[i]->buffer))
5611 /* Assert it's not reused as a combination. */
5612 xassert (NILP (leaf_windows[i]->hchild)
5613 && NILP (leaf_windows[i]->vchild));
5614 free_window_matrices (leaf_windows[i]);
5616 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5617 ++n;
5620 adjust_glyphs (f);
5622 UNBLOCK_INPUT;
5624 /* Fselect_window will have made f the selected frame, so we
5625 reselect the proper frame here. Fhandle_switch_frame will change the
5626 selected window too, but that doesn't make the call to
5627 Fselect_window above totally superfluous; it still sets f's
5628 selected window. */
5629 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5630 do_switch_frame (data->selected_frame, 0, 0);
5632 if (! NILP (Vwindow_configuration_change_hook)
5633 && ! NILP (Vrun_hooks))
5634 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
5637 if (!NILP (new_current_buffer))
5638 Fset_buffer (new_current_buffer);
5640 /* Restore the minimum heights recorded in the configuration. */
5641 window_min_height = XINT (data->min_height);
5642 window_min_width = XINT (data->min_width);
5644 Vminibuf_scroll_window = data->minibuf_scroll_window;
5645 minibuf_selected_window = data->minibuf_selected_window;
5647 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5650 /* Mark all windows now on frame as deleted
5651 by setting their buffers to nil. */
5653 void
5654 delete_all_subwindows (w)
5655 register struct window *w;
5657 if (!NILP (w->next))
5658 delete_all_subwindows (XWINDOW (w->next));
5659 if (!NILP (w->vchild))
5660 delete_all_subwindows (XWINDOW (w->vchild));
5661 if (!NILP (w->hchild))
5662 delete_all_subwindows (XWINDOW (w->hchild));
5664 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
5666 if (!NILP (w->buffer))
5667 unshow_buffer (w);
5669 /* We set all three of these fields to nil, to make sure that we can
5670 distinguish this dead window from any live window. Live leaf
5671 windows will have buffer set, and combination windows will have
5672 vchild or hchild set. */
5673 w->buffer = Qnil;
5674 w->vchild = Qnil;
5675 w->hchild = Qnil;
5677 Vwindow_list = Qnil;
5680 static int
5681 count_windows (window)
5682 register struct window *window;
5684 register int count = 1;
5685 if (!NILP (window->next))
5686 count += count_windows (XWINDOW (window->next));
5687 if (!NILP (window->vchild))
5688 count += count_windows (XWINDOW (window->vchild));
5689 if (!NILP (window->hchild))
5690 count += count_windows (XWINDOW (window->hchild));
5691 return count;
5695 /* Fill vector FLAT with leaf windows under W, starting at index I.
5696 Value is last index + 1. */
5698 static int
5699 get_leaf_windows (w, flat, i)
5700 struct window *w;
5701 struct window **flat;
5702 int i;
5704 while (w)
5706 if (!NILP (w->hchild))
5707 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5708 else if (!NILP (w->vchild))
5709 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5710 else
5711 flat[i++] = w;
5713 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5716 return i;
5720 /* Return a pointer to the glyph W's physical cursor is on. Value is
5721 null if W's current matrix is invalid, so that no meaningfull glyph
5722 can be returned. */
5724 struct glyph *
5725 get_phys_cursor_glyph (w)
5726 struct window *w;
5728 struct glyph_row *row;
5729 struct glyph *glyph;
5731 if (w->phys_cursor.vpos >= 0
5732 && w->phys_cursor.vpos < w->current_matrix->nrows
5733 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
5734 row->enabled_p)
5735 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
5736 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
5737 else
5738 glyph = NULL;
5740 return glyph;
5744 static int
5745 save_window_save (window, vector, i)
5746 Lisp_Object window;
5747 struct Lisp_Vector *vector;
5748 int i;
5750 register struct saved_window *p;
5751 register struct window *w;
5752 register Lisp_Object tem;
5754 for (;!NILP (window); window = w->next)
5756 p = SAVED_WINDOW_N (vector, i);
5757 w = XWINDOW (window);
5759 XSETFASTINT (w->temslot, i++);
5760 p->window = window;
5761 p->buffer = w->buffer;
5762 p->left_col = w->left_col;
5763 p->top_line = w->top_line;
5764 p->total_cols = w->total_cols;
5765 p->total_lines = w->total_lines;
5766 p->hscroll = w->hscroll;
5767 p->min_hscroll = w->min_hscroll;
5768 p->display_table = w->display_table;
5769 p->orig_top_line = w->orig_top_line;
5770 p->orig_total_lines = w->orig_total_lines;
5771 p->left_margin_cols = w->left_margin_cols;
5772 p->right_margin_cols = w->right_margin_cols;
5773 p->left_fringe_width = w->left_fringe_width;
5774 p->right_fringe_width = w->right_fringe_width;
5775 p->fringes_outside_margins = w->fringes_outside_margins;
5776 p->scroll_bar_width = w->scroll_bar_width;
5777 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5778 if (!NILP (w->buffer))
5780 /* Save w's value of point in the window configuration.
5781 If w is the selected window, then get the value of point
5782 from the buffer; pointm is garbage in the selected window. */
5783 if (EQ (window, selected_window))
5785 p->pointm = Fmake_marker ();
5786 set_marker_both (p->pointm, w->buffer,
5787 BUF_PT (XBUFFER (w->buffer)),
5788 BUF_PT_BYTE (XBUFFER (w->buffer)));
5790 else
5791 p->pointm = Fcopy_marker (w->pointm, Qnil);
5793 p->start = Fcopy_marker (w->start, Qnil);
5794 p->start_at_line_beg = w->start_at_line_beg;
5796 tem = XBUFFER (w->buffer)->mark;
5797 p->mark = Fcopy_marker (tem, Qnil);
5799 else
5801 p->pointm = Qnil;
5802 p->start = Qnil;
5803 p->mark = Qnil;
5804 p->start_at_line_beg = Qnil;
5807 if (NILP (w->parent))
5808 p->parent = Qnil;
5809 else
5810 p->parent = XWINDOW (w->parent)->temslot;
5812 if (NILP (w->prev))
5813 p->prev = Qnil;
5814 else
5815 p->prev = XWINDOW (w->prev)->temslot;
5817 if (!NILP (w->vchild))
5818 i = save_window_save (w->vchild, vector, i);
5819 if (!NILP (w->hchild))
5820 i = save_window_save (w->hchild, vector, i);
5823 return i;
5826 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
5827 Scurrent_window_configuration, 0, 1, 0,
5828 doc: /* Return an object representing the current window configuration of FRAME.
5829 If FRAME is nil or omitted, use the selected frame.
5830 This describes the number of windows, their sizes and current buffers,
5831 and for each displayed buffer, where display starts, and the positions of
5832 point and mark. An exception is made for point in the current buffer:
5833 its value is -not- saved.
5834 This also records the currently selected frame, and FRAME's focus
5835 redirection (see `redirect-frame-focus'). */)
5836 (frame)
5837 Lisp_Object frame;
5839 register Lisp_Object tem;
5840 register int n_windows;
5841 register struct save_window_data *data;
5842 register struct Lisp_Vector *vec;
5843 register int i;
5844 FRAME_PTR f;
5846 if (NILP (frame))
5847 frame = selected_frame;
5848 CHECK_LIVE_FRAME (frame);
5849 f = XFRAME (frame);
5851 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5852 vec = allocate_other_vector (VECSIZE (struct save_window_data));
5853 data = (struct save_window_data *)vec;
5855 XSETFASTINT (data->frame_cols, FRAME_COLS (f));
5856 XSETFASTINT (data->frame_lines, FRAME_LINES (f));
5857 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
5858 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
5859 data->selected_frame = selected_frame;
5860 data->current_window = FRAME_SELECTED_WINDOW (f);
5861 XSETBUFFER (data->current_buffer, current_buffer);
5862 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
5863 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
5864 data->root_window = FRAME_ROOT_WINDOW (f);
5865 data->focus_frame = FRAME_FOCUS_FRAME (f);
5866 XSETINT (data->min_height, window_min_height);
5867 XSETINT (data->min_width, window_min_width);
5868 tem = Fmake_vector (make_number (n_windows), Qnil);
5869 data->saved_windows = tem;
5870 for (i = 0; i < n_windows; i++)
5871 XVECTOR (tem)->contents[i]
5872 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
5873 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
5874 XSETWINDOW_CONFIGURATION (tem, data);
5875 return (tem);
5878 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
5879 0, UNEVALLED, 0,
5880 doc: /* Execute body, preserving window sizes and contents.
5881 Restore which buffer appears in which window, where display starts,
5882 and the value of point and mark for each window.
5883 Also restore the choice of selected window.
5884 Also restore which buffer is current.
5885 Does not restore the value of point in current buffer.
5886 usage: (save-window-excursion BODY ...) */)
5887 (args)
5888 Lisp_Object args;
5890 register Lisp_Object val;
5891 register int count = SPECPDL_INDEX ();
5893 record_unwind_protect (Fset_window_configuration,
5894 Fcurrent_window_configuration (Qnil));
5895 val = Fprogn (args);
5896 return unbind_to (count, val);
5900 /***********************************************************************
5901 Marginal Areas
5902 ***********************************************************************/
5904 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
5905 2, 3, 0,
5906 doc: /* Set width of marginal areas of window WINDOW.
5907 If WINDOW is nil, set margins of the currently selected window.
5908 Second arg LEFT-WIDTH specifies the number of character cells to
5909 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
5910 does the same for the right marginal area. A nil width parameter
5911 means no margin. */)
5912 (window, left, right)
5913 Lisp_Object window, left, right;
5915 struct window *w = decode_window (window);
5917 /* Translate negative or zero widths to nil.
5918 Margins that are too wide have to be checked elsewhere. */
5920 if (!NILP (left))
5922 CHECK_NUMBER (left);
5923 if (XINT (left) <= 0)
5924 left = Qnil;
5927 if (!NILP (right))
5929 CHECK_NUMBER (right);
5930 if (XINT (right) <= 0)
5931 right = Qnil;
5934 if (!EQ (w->left_margin_cols, left)
5935 || !EQ (w->right_margin_cols, right))
5937 w->left_margin_cols = left;
5938 w->right_margin_cols = right;
5940 adjust_window_margins (w);
5942 ++windows_or_buffers_changed;
5943 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
5946 return Qnil;
5950 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
5951 0, 1, 0,
5952 doc: /* Get width of marginal areas of window WINDOW.
5953 If WINDOW is omitted or nil, use the currently selected window.
5954 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
5955 If a marginal area does not exist, its width will be returned
5956 as nil. */)
5957 (window)
5958 Lisp_Object window;
5960 struct window *w = decode_window (window);
5961 return Fcons (w->left_margin_cols, w->right_margin_cols);
5966 /***********************************************************************
5967 Fringes
5968 ***********************************************************************/
5970 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
5971 2, 4, 0,
5972 doc: /* Set the fringe widths of window WINDOW.
5973 If WINDOW is nil, set the fringe widths of the currently selected
5974 window.
5975 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
5976 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
5977 fringe width. If a fringe width arg is nil, that means to use the
5978 frame's default fringe width. Default fringe widths can be set with
5979 the command `set-fringe-style'.
5980 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
5981 outside of the display margins. By default, fringes are drawn between
5982 display marginal areas and the text area. */)
5983 (window, left, right, outside_margins)
5984 Lisp_Object window, left, right, outside_margins;
5986 struct window *w = decode_window (window);
5988 if (!NILP (left))
5989 CHECK_NUMBER (left);
5990 if (!NILP (right))
5991 CHECK_NUMBER (right);
5993 if (!EQ (w->left_fringe_width, left)
5994 || !EQ (w->right_fringe_width, right)
5995 || !EQ (w->fringes_outside_margins, outside_margins))
5997 w->left_fringe_width = left;
5998 w->right_fringe_width = right;
5999 w->fringes_outside_margins = outside_margins;
6001 adjust_window_margins (w);
6003 clear_glyph_matrix (w->current_matrix);
6004 w->window_end_valid = Qnil;
6006 ++windows_or_buffers_changed;
6007 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6010 return Qnil;
6014 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6015 0, 1, 0,
6016 doc: /* Get width of fringes of window WINDOW.
6017 If WINDOW is omitted or nil, use the currently selected window.
6018 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS).
6019 If a window specific fringe width is not set, its width will be returned
6020 as nil. */)
6021 (window)
6022 Lisp_Object window;
6024 struct window *w = decode_window (window);
6025 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6026 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6027 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ?
6028 Qt : Qnil), Qnil)));
6033 /***********************************************************************
6034 Scroll bars
6035 ***********************************************************************/
6037 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6038 2, 4, 0,
6039 doc: /* Set width and type of scroll bars of window WINDOW.
6040 If window is nil, set scroll bars of the currently selected window.
6041 Second parameter WIDTH specifies the pixel width for the scroll bar;
6042 this is automatically adjusted to a multiple of the frame column width.
6043 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6044 bar: left, right, or nil.
6045 If WIDTH is nil, use the frame's scroll-bar width.
6046 If TYPE is t, use the frame's scroll-bar type. */)
6047 (window, width, vertical_type, horizontal_type)
6048 Lisp_Object window, width, vertical_type, horizontal_type;
6050 struct window *w = decode_window (window);
6052 if (!NILP (width))
6053 CHECK_NUMBER (width);
6055 if (XINT (width) == 0)
6056 vertical_type = Qnil;
6058 if (!(EQ (vertical_type, Qnil)
6059 || EQ (vertical_type, Qleft)
6060 || EQ (vertical_type, Qright)
6061 || EQ (vertical_type, Qt)))
6062 error ("Invalid type of vertical scroll bar");
6064 if (!EQ (w->scroll_bar_width, width)
6065 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6067 w->scroll_bar_width = width;
6068 w->vertical_scroll_bar_type = vertical_type;
6070 adjust_window_margins (w);
6072 clear_glyph_matrix (w->current_matrix);
6073 w->window_end_valid = Qnil;
6075 ++windows_or_buffers_changed;
6076 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6079 return Qnil;
6083 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6084 0, 1, 0,
6085 doc: /* Get width and type of scroll bars of window WINDOW.
6086 If WINDOW is omitted or nil, use the currently selected window.
6087 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6088 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6089 value. */)
6090 (window)
6091 Lisp_Object window;
6093 struct window *w = decode_window (window);
6094 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6095 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6096 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6097 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6098 Fcons (w->vertical_scroll_bar_type,
6099 Fcons (Qnil, Qnil))));
6104 /***********************************************************************
6105 Smooth scrolling
6106 ***********************************************************************/
6108 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 1, 0,
6109 doc: /* Return the amount by which WINDOW is scrolled vertically.
6110 Use the selected window if WINDOW is nil or omitted.
6111 Value is a multiple of the canonical character height of WINDOW. */)
6112 (window)
6113 Lisp_Object window;
6115 Lisp_Object result;
6116 struct frame *f;
6117 struct window *w;
6119 if (NILP (window))
6120 window = selected_window;
6121 else
6122 CHECK_WINDOW (window);
6123 w = XWINDOW (window);
6124 f = XFRAME (w->frame);
6126 if (FRAME_WINDOW_P (f))
6127 result = FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll);
6128 else
6129 result = make_number (0);
6130 return result;
6134 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6135 2, 2, 0,
6136 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6137 WINDOW nil means use the selected window. VSCROLL is a non-negative
6138 multiple of the canonical character height of WINDOW. */)
6139 (window, vscroll)
6140 Lisp_Object window, vscroll;
6142 struct window *w;
6143 struct frame *f;
6145 if (NILP (window))
6146 window = selected_window;
6147 else
6148 CHECK_WINDOW (window);
6149 CHECK_NUMBER_OR_FLOAT (vscroll);
6151 w = XWINDOW (window);
6152 f = XFRAME (w->frame);
6154 if (FRAME_WINDOW_P (f))
6156 int old_dy = w->vscroll;
6158 w->vscroll = - FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll);
6159 w->vscroll = min (w->vscroll, 0);
6161 /* Adjust glyph matrix of the frame if the virtual display
6162 area becomes larger than before. */
6163 if (w->vscroll < 0 && w->vscroll < old_dy)
6164 adjust_glyphs (f);
6166 /* Prevent redisplay shortcuts. */
6167 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6170 return Fwindow_vscroll (window);
6174 /* Call FN for all leaf windows on frame F. FN is called with the
6175 first argument being a pointer to the leaf window, and with
6176 additional argument USER_DATA. Stops when FN returns 0. */
6178 void
6179 foreach_window (f, fn, user_data)
6180 struct frame *f;
6181 int (* fn) P_ ((struct window *, void *));
6182 void *user_data;
6184 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6188 /* Helper function for foreach_window. Call FN for all leaf windows
6189 reachable from W. FN is called with the first argument being a
6190 pointer to the leaf window, and with additional argument USER_DATA.
6191 Stop when FN returns 0. Value is 0 if stopped by FN. */
6193 static int
6194 foreach_window_1 (w, fn, user_data)
6195 struct window *w;
6196 int (* fn) P_ ((struct window *, void *));
6197 void *user_data;
6199 int cont;
6201 for (cont = 1; w && cont;)
6203 if (!NILP (w->hchild))
6204 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6205 else if (!NILP (w->vchild))
6206 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6207 else
6208 cont = fn (w, user_data);
6210 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6213 return cont;
6217 /* Freeze or unfreeze the window start of W unless it is a
6218 mini-window or the selected window. FREEZE_P non-null means freeze
6219 the window start. */
6221 static int
6222 freeze_window_start (w, freeze_p)
6223 struct window *w;
6224 void *freeze_p;
6226 if (w == XWINDOW (selected_window)
6227 || MINI_WINDOW_P (w)
6228 || (MINI_WINDOW_P (XWINDOW (selected_window))
6229 && ! NILP (Vminibuf_scroll_window)
6230 && w == XWINDOW (Vminibuf_scroll_window)))
6231 freeze_p = NULL;
6233 w->frozen_window_start_p = freeze_p != NULL;
6234 return 1;
6238 /* Freeze or unfreeze the window starts of all leaf windows on frame
6239 F, except the selected window and a mini-window. FREEZE_P non-zero
6240 means freeze the window start. */
6242 void
6243 freeze_window_starts (f, freeze_p)
6244 struct frame *f;
6245 int freeze_p;
6247 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6251 /***********************************************************************
6252 Initialization
6253 ***********************************************************************/
6255 /* Return 1 if window configurations C1 and C2
6256 describe the same state of affairs. This is used by Fequal. */
6259 compare_window_configurations (c1, c2, ignore_positions)
6260 Lisp_Object c1, c2;
6261 int ignore_positions;
6263 register struct save_window_data *d1, *d2;
6264 struct Lisp_Vector *sw1, *sw2;
6265 int i;
6267 if (!WINDOW_CONFIGURATIONP (c1))
6268 wrong_type_argument (Qwindow_configuration_p, c1);
6269 if (!WINDOW_CONFIGURATIONP (c2))
6270 wrong_type_argument (Qwindow_configuration_p, c2);
6272 d1 = (struct save_window_data *) XVECTOR (c1);
6273 d2 = (struct save_window_data *) XVECTOR (c2);
6274 sw1 = XVECTOR (d1->saved_windows);
6275 sw2 = XVECTOR (d2->saved_windows);
6277 if (! EQ (d1->frame_cols, d2->frame_cols))
6278 return 0;
6279 if (! EQ (d1->frame_lines, d2->frame_lines))
6280 return 0;
6281 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
6282 return 0;
6283 if (! EQ (d1->selected_frame, d2->selected_frame))
6284 return 0;
6285 /* Don't compare the current_window field directly.
6286 Instead see w1_is_current and w2_is_current, below. */
6287 if (! EQ (d1->current_buffer, d2->current_buffer))
6288 return 0;
6289 if (! ignore_positions)
6291 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6292 return 0;
6293 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6294 return 0;
6296 /* Don't compare the root_window field.
6297 We don't require the two configurations
6298 to use the same window object,
6299 and the two root windows must be equivalent
6300 if everything else compares equal. */
6301 if (! EQ (d1->focus_frame, d2->focus_frame))
6302 return 0;
6303 if (! EQ (d1->min_width, d2->min_width))
6304 return 0;
6305 if (! EQ (d1->min_height, d2->min_height))
6306 return 0;
6308 /* Verify that the two confis have the same number of windows. */
6309 if (sw1->size != sw2->size)
6310 return 0;
6312 for (i = 0; i < sw1->size; i++)
6314 struct saved_window *p1, *p2;
6315 int w1_is_current, w2_is_current;
6317 p1 = SAVED_WINDOW_N (sw1, i);
6318 p2 = SAVED_WINDOW_N (sw2, i);
6320 /* Verify that the current windows in the two
6321 configurations correspond to each other. */
6322 w1_is_current = EQ (d1->current_window, p1->window);
6323 w2_is_current = EQ (d2->current_window, p2->window);
6325 if (w1_is_current != w2_is_current)
6326 return 0;
6328 /* Verify that the corresponding windows do match. */
6329 if (! EQ (p1->buffer, p2->buffer))
6330 return 0;
6331 if (! EQ (p1->left_col, p2->left_col))
6332 return 0;
6333 if (! EQ (p1->top_line, p2->top_line))
6334 return 0;
6335 if (! EQ (p1->total_cols, p2->total_cols))
6336 return 0;
6337 if (! EQ (p1->total_lines, p2->total_lines))
6338 return 0;
6339 if (! EQ (p1->display_table, p2->display_table))
6340 return 0;
6341 if (! EQ (p1->parent, p2->parent))
6342 return 0;
6343 if (! EQ (p1->prev, p2->prev))
6344 return 0;
6345 if (! ignore_positions)
6347 if (! EQ (p1->hscroll, p2->hscroll))
6348 return 0;
6349 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6350 return 0;
6351 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6352 return 0;
6353 if (NILP (Fequal (p1->start, p2->start)))
6354 return 0;
6355 if (NILP (Fequal (p1->pointm, p2->pointm)))
6356 return 0;
6357 if (NILP (Fequal (p1->mark, p2->mark)))
6358 return 0;
6360 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6361 return 0;
6362 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6363 return 0;
6364 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6365 return 0;
6366 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6367 return 0;
6368 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6369 return 0;
6370 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6371 return 0;
6372 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6373 return 0;
6376 return 1;
6379 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6380 Scompare_window_configurations, 2, 2, 0,
6381 doc: /* Compare two window configurations as regards the structure of windows.
6382 This function ignores details such as the values of point and mark
6383 and scrolling positions. */)
6384 (x, y)
6385 Lisp_Object x, y;
6387 if (compare_window_configurations (x, y, 1))
6388 return Qt;
6389 return Qnil;
6392 void
6393 init_window_once ()
6395 struct frame *f = make_terminal_frame ();
6396 XSETFRAME (selected_frame, f);
6397 Vterminal_frame = selected_frame;
6398 minibuf_window = f->minibuffer_window;
6399 selected_window = f->selected_window;
6400 last_nonminibuf_frame = f;
6402 window_initialized = 1;
6405 void
6406 init_window ()
6408 Vwindow_list = Qnil;
6411 void
6412 syms_of_window ()
6414 Qwindow_size_fixed = intern ("window-size-fixed");
6415 staticpro (&Qwindow_size_fixed);
6417 staticpro (&Qwindow_configuration_change_hook);
6418 Qwindow_configuration_change_hook
6419 = intern ("window-configuration-change-hook");
6421 Qwindowp = intern ("windowp");
6422 staticpro (&Qwindowp);
6424 Qwindow_configuration_p = intern ("window-configuration-p");
6425 staticpro (&Qwindow_configuration_p);
6427 Qwindow_live_p = intern ("window-live-p");
6428 staticpro (&Qwindow_live_p);
6430 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
6431 staticpro (&Qtemp_buffer_show_hook);
6433 staticpro (&Vwindow_list);
6435 minibuf_selected_window = Qnil;
6436 staticpro (&minibuf_selected_window);
6438 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
6439 doc: /* Non-nil means call as function to display a help buffer.
6440 The function is called with one argument, the buffer to be displayed.
6441 Used by `with-output-to-temp-buffer'.
6442 If this function is used, then it must do the entire job of showing
6443 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6444 Vtemp_buffer_show_function = Qnil;
6446 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
6447 doc: /* If non-nil, function to call to handle `display-buffer'.
6448 It will receive two args, the buffer and a flag which if non-nil means
6449 that the currently selected window is not acceptable.
6450 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'
6451 work using this function. */);
6452 Vdisplay_buffer_function = Qnil;
6454 DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
6455 doc: /* *If non-nil, `display-buffer' should even the window heights.
6456 If nil, `display-buffer' will leave the window configuration alone. */);
6457 Veven_window_heights = Qt;
6459 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
6460 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6461 Vminibuf_scroll_window = Qnil;
6463 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
6464 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6465 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6466 is displayed in the `mode-line' face. */);
6467 mode_line_in_non_selected_windows = 1;
6469 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
6470 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6471 Vother_window_scroll_buffer = Qnil;
6473 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
6474 doc: /* *Non-nil means `display-buffer' should make a separate frame. */);
6475 pop_up_frames = 0;
6477 DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
6478 doc: /* *Non-nil means `display-buffer' should reuse frames.
6479 If the buffer in question is already displayed in a frame, raise that frame. */);
6480 display_buffer_reuse_frames = 0;
6482 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
6483 doc: /* Function to call to handle automatic new frame creation.
6484 It is called with no arguments and should return a newly created frame.
6486 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
6487 where `pop-up-frame-alist' would hold the default frame parameters. */);
6488 Vpop_up_frame_function = Qnil;
6490 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
6491 doc: /* *List of buffer names that should have their own special frames.
6492 Displaying a buffer whose name is in this list makes a special frame for it
6493 using `special-display-function'. See also `special-display-regexps'.
6495 An element of the list can be a list instead of just a string.
6496 There are two ways to use a list as an element:
6497 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
6498 In the first case, FRAME-PARAMETERS are used to create the frame.
6499 In the latter case, FUNCTION is called with BUFFER as the first argument,
6500 followed by OTHER-ARGS--it can display BUFFER in any way it likes.
6501 All this is done by the function found in `special-display-function'.
6503 If the specified frame parameters include (same-buffer . t), the
6504 buffer is displayed in the currently selected window. Otherwise, if
6505 they include (same-frame . t), the buffer is displayed in a new window
6506 in the currently selected frame.
6508 If this variable appears \"not to work\", because you add a name to it
6509 but that buffer still appears in the selected window, look at the
6510 values of `same-window-buffer-names' and `same-window-regexps'.
6511 Those variables take precedence over this one. */);
6512 Vspecial_display_buffer_names = Qnil;
6514 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
6515 doc: /* *List of regexps saying which buffers should have their own special frames.
6516 If a buffer name matches one of these regexps, it gets its own frame.
6517 Displaying a buffer whose name is in this list makes a special frame for it
6518 using `special-display-function'.
6520 An element of the list can be a list instead of just a string.
6521 There are two ways to use a list as an element:
6522 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
6523 In the first case, FRAME-PARAMETERS are used to create the frame.
6524 In the latter case, FUNCTION is called with the buffer as first argument,
6525 followed by OTHER-ARGS--it can display the buffer in any way it likes.
6526 All this is done by the function found in `special-display-function'.
6528 If the specified frame parameters include (same-buffer . t), the
6529 buffer is displayed in the currently selected window. Otherwise, if
6530 they include (same-frame . t), the buffer is displayed in a new window
6531 in the currently selected frame.
6533 If this variable appears \"not to work\", because you add a regexp to it
6534 but the matching buffers still appear in the selected window, look at the
6535 values of `same-window-buffer-names' and `same-window-regexps'.
6536 Those variables take precedence over this one. */);
6537 Vspecial_display_regexps = Qnil;
6539 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
6540 doc: /* Function to call to make a new frame for a special buffer.
6541 It is called with two arguments, the buffer and optional buffer specific
6542 data, and should return a window displaying that buffer.
6543 The default value normally makes a separate frame for the buffer,
6544 using `special-display-frame-alist' to specify the frame parameters.
6545 But if the buffer specific data includes (same-buffer . t) then the
6546 buffer is displayed in the current selected window.
6547 Otherwise if it includes (same-frame . t) then the buffer is displayed in
6548 a new window in the currently selected frame.
6550 A buffer is special if it is listed in `special-display-buffer-names'
6551 or matches a regexp in `special-display-regexps'. */);
6552 Vspecial_display_function = Qnil;
6554 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
6555 doc: /* *List of buffer names that should appear in the selected window.
6556 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
6557 switches to it in the selected window, rather than making it appear
6558 in some other window.
6560 An element of the list can be a cons cell instead of just a string.
6561 Then the car must be a string, which specifies the buffer name.
6562 This is for compatibility with `special-display-buffer-names';
6563 the cdr of the cons cell is ignored.
6565 See also `same-window-regexps'. */);
6566 Vsame_window_buffer_names = Qnil;
6568 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
6569 doc: /* *List of regexps saying which buffers should appear in the selected window.
6570 If a buffer name matches one of these regexps, then displaying it
6571 using `display-buffer' or `pop-to-buffer' switches to it
6572 in the selected window, rather than making it appear in some other window.
6574 An element of the list can be a cons cell instead of just a string.
6575 Then the car must be a string, which specifies the buffer name.
6576 This is for compatibility with `special-display-buffer-names';
6577 the cdr of the cons cell is ignored.
6579 See also `same-window-buffer-names'. */);
6580 Vsame_window_regexps = Qnil;
6582 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
6583 doc: /* *Non-nil means display-buffer should make new windows. */);
6584 pop_up_windows = 1;
6586 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
6587 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
6588 next_screen_context_lines = 2;
6590 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
6591 doc: /* *display-buffer would prefer to split the largest window if this large.
6592 If there is only one window, it is split regardless of this value. */);
6593 split_height_threshold = 500;
6595 DEFVAR_INT ("window-min-height", &window_min_height,
6596 doc: /* *Delete any window less than this tall (including its mode line). */);
6597 window_min_height = 4;
6599 DEFVAR_INT ("window-min-width", &window_min_width,
6600 doc: /* *Delete any window less than this wide. */);
6601 window_min_width = 10;
6603 DEFVAR_LISP ("scroll-preserve-screen-position",
6604 &Vscroll_preserve_screen_position,
6605 doc: /* *Non-nil means scroll commands move point to keep its screen line unchanged. */);
6606 Vscroll_preserve_screen_position = Qnil;
6608 DEFVAR_LISP ("window-configuration-change-hook",
6609 &Vwindow_configuration_change_hook,
6610 doc: /* Functions to call when window configuration changes.
6611 The selected frame is the one whose configuration has changed. */);
6612 Vwindow_configuration_change_hook = Qnil;
6614 DEFVAR_BOOL ("window-size-fixed", &window_size_fixed,
6615 doc: /* Non-nil in a buffer means windows displaying the buffer are fixed-size.
6616 Emacs won't change the size of any window displaying that buffer,
6617 unless you explicitly change the size, or Emacs has no other choice.
6618 This variable automatically becomes buffer-local when set. */);
6619 Fmake_variable_buffer_local (Qwindow_size_fixed);
6620 window_size_fixed = 0;
6622 defsubr (&Sselected_window);
6623 defsubr (&Sminibuffer_window);
6624 defsubr (&Swindow_minibuffer_p);
6625 defsubr (&Swindowp);
6626 defsubr (&Swindow_live_p);
6627 defsubr (&Spos_visible_in_window_p);
6628 defsubr (&Swindow_buffer);
6629 defsubr (&Swindow_height);
6630 defsubr (&Swindow_width);
6631 defsubr (&Swindow_hscroll);
6632 defsubr (&Sset_window_hscroll);
6633 defsubr (&Swindow_redisplay_end_trigger);
6634 defsubr (&Sset_window_redisplay_end_trigger);
6635 defsubr (&Swindow_edges);
6636 defsubr (&Swindow_pixel_edges);
6637 defsubr (&Swindow_inside_edges);
6638 defsubr (&Swindow_inside_pixel_edges);
6639 defsubr (&Scoordinates_in_window_p);
6640 defsubr (&Swindow_at);
6641 defsubr (&Swindow_point);
6642 defsubr (&Swindow_start);
6643 defsubr (&Swindow_end);
6644 defsubr (&Sset_window_point);
6645 defsubr (&Sset_window_start);
6646 defsubr (&Swindow_dedicated_p);
6647 defsubr (&Sset_window_dedicated_p);
6648 defsubr (&Swindow_display_table);
6649 defsubr (&Sset_window_display_table);
6650 defsubr (&Snext_window);
6651 defsubr (&Sprevious_window);
6652 defsubr (&Sother_window);
6653 defsubr (&Sget_lru_window);
6654 defsubr (&Sget_largest_window);
6655 defsubr (&Sget_buffer_window);
6656 defsubr (&Sdelete_other_windows);
6657 defsubr (&Sdelete_windows_on);
6658 defsubr (&Sreplace_buffer_in_windows);
6659 defsubr (&Sdelete_window);
6660 defsubr (&Sset_window_buffer);
6661 defsubr (&Sselect_window);
6662 defsubr (&Sspecial_display_p);
6663 defsubr (&Ssame_window_p);
6664 defsubr (&Sdisplay_buffer);
6665 defsubr (&Sforce_window_update);
6666 defsubr (&Ssplit_window);
6667 defsubr (&Senlarge_window);
6668 defsubr (&Sshrink_window);
6669 defsubr (&Sscroll_up);
6670 defsubr (&Sscroll_down);
6671 defsubr (&Sscroll_left);
6672 defsubr (&Sscroll_right);
6673 defsubr (&Sother_window_for_scrolling);
6674 defsubr (&Sscroll_other_window);
6675 defsubr (&Sminibuffer_selected_window);
6676 defsubr (&Srecenter);
6677 defsubr (&Swindow_text_height);
6678 defsubr (&Smove_to_window_line);
6679 defsubr (&Swindow_configuration_p);
6680 defsubr (&Swindow_configuration_frame);
6681 defsubr (&Sset_window_configuration);
6682 defsubr (&Scurrent_window_configuration);
6683 defsubr (&Ssave_window_excursion);
6684 defsubr (&Sset_window_margins);
6685 defsubr (&Swindow_margins);
6686 defsubr (&Sset_window_fringes);
6687 defsubr (&Swindow_fringes);
6688 defsubr (&Sset_window_scroll_bars);
6689 defsubr (&Swindow_scroll_bars);
6690 defsubr (&Swindow_vscroll);
6691 defsubr (&Sset_window_vscroll);
6692 defsubr (&Scompare_window_configurations);
6693 defsubr (&Swindow_list);
6696 void
6697 keys_of_window ()
6699 initial_define_key (control_x_map, '1', "delete-other-windows");
6700 initial_define_key (control_x_map, '2', "split-window");
6701 initial_define_key (control_x_map, '0', "delete-window");
6702 initial_define_key (control_x_map, 'o', "other-window");
6703 initial_define_key (control_x_map, '^', "enlarge-window");
6704 initial_define_key (control_x_map, '<', "scroll-left");
6705 initial_define_key (control_x_map, '>', "scroll-right");
6707 initial_define_key (global_map, Ctl ('V'), "scroll-up");
6708 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6709 initial_define_key (meta_map, 'v', "scroll-down");
6711 initial_define_key (global_map, Ctl('L'), "recenter");
6712 initial_define_key (meta_map, 'r', "move-to-window-line");
6715 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
6716 (do not change this comment) */