(Fpos_visible_in_window_p, Fset_window_hscroll)
[emacs.git] / src / window.c
blob98efddd446e60e3676d41f24aeb73ee994fa210e
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985,86,87, 1993,94,95,96,97,98, 2000,01,02,03,04
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "buffer.h"
26 #include "keyboard.h"
27 #include "keymap.h"
28 #include "frame.h"
29 #include "window.h"
30 #include "commands.h"
31 #include "indent.h"
32 #include "termchar.h"
33 #include "disptab.h"
34 #include "dispextern.h"
35 #include "blockinput.h"
36 #include "intervals.h"
38 #ifdef HAVE_X_WINDOWS
39 #include "xterm.h"
40 #endif /* HAVE_X_WINDOWS */
41 #ifdef WINDOWSNT
42 #include "w32term.h"
43 #endif
44 #ifdef MSDOS
45 #include "msdos.h"
46 #endif
47 #ifdef MAC_OS
48 #include "macterm.h"
49 #endif
52 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
53 Lisp_Object Qwindow_size_fixed;
54 extern Lisp_Object Qleft_margin, Qright_margin;
56 static int displayed_window_lines P_ ((struct window *));
57 static struct window *decode_window P_ ((Lisp_Object));
58 static int count_windows P_ ((struct window *));
59 static int get_leaf_windows P_ ((struct window *, struct window **, int));
60 static void window_scroll P_ ((Lisp_Object, int, int, int));
61 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
62 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
63 static int window_min_size_1 P_ ((struct window *, int));
64 static int window_min_size P_ ((struct window *, int, int, int *));
65 static void size_window P_ ((Lisp_Object, int, int, int));
66 static int freeze_window_start P_ ((struct window *, void *));
67 static int window_fixed_size_p P_ ((struct window *, int, int));
68 static void enlarge_window P_ ((Lisp_Object, int, int, int));
69 static Lisp_Object window_list P_ ((void));
70 static int add_window_to_list P_ ((struct window *, void *));
71 static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
72 Lisp_Object));
73 static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
74 Lisp_Object, int));
75 static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
76 Lisp_Object *));
77 static int foreach_window_1 P_ ((struct window *,
78 int (* fn) (struct window *, void *),
79 void *));
80 static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
82 /* The value of `window-size-fixed'. */
84 int window_size_fixed;
86 /* This is the window in which the terminal's cursor should
87 be left when nothing is being done with it. This must
88 always be a leaf window, and its buffer is selected by
89 the top level editing loop at the end of each command.
91 This value is always the same as
92 FRAME_SELECTED_WINDOW (selected_frame). */
94 Lisp_Object selected_window;
96 /* A list of all windows for use by next_window and Fwindow_list.
97 Functions creating or deleting windows should invalidate this cache
98 by setting it to nil. */
100 Lisp_Object Vwindow_list;
102 /* The mini-buffer window of the selected frame.
103 Note that you cannot test for mini-bufferness of an arbitrary window
104 by comparing against this; but you can test for mini-bufferness of
105 the selected window. */
107 Lisp_Object minibuf_window;
109 /* Non-nil means it is the window whose mode line should be
110 shown as the selected window when the minibuffer is selected. */
112 Lisp_Object minibuf_selected_window;
114 /* Non-nil means it is the window for C-M-v to scroll
115 when the mini-buffer is selected. */
117 Lisp_Object Vminibuf_scroll_window;
119 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
121 Lisp_Object Vother_window_scroll_buffer;
123 /* Non-nil means it's function to call to display temp buffers. */
125 Lisp_Object Vtemp_buffer_show_function;
127 /* Non-zero means to use mode-line-inactive face in all windows but the
128 selected-window and the minibuffer-scroll-window when the
129 minibuffer is active. */
130 int mode_line_in_non_selected_windows;
132 /* If a window gets smaller than either of these, it is removed. */
134 EMACS_INT window_min_height;
135 EMACS_INT window_min_width;
137 /* Nonzero implies Fdisplay_buffer should create windows. */
139 int pop_up_windows;
141 /* Nonzero implies make new frames for Fdisplay_buffer. */
143 int pop_up_frames;
145 /* Nonzero means reuse existing frames for displaying buffers. */
147 int display_buffer_reuse_frames;
149 /* Non-nil means use this function instead of default */
151 Lisp_Object Vpop_up_frame_function;
153 /* Function to call to handle Fdisplay_buffer. */
155 Lisp_Object Vdisplay_buffer_function;
157 /* Non-nil means that Fdisplay_buffer should even the heights of windows. */
159 Lisp_Object Veven_window_heights;
161 /* List of buffer *names* for buffers that should have their own frames. */
163 Lisp_Object Vspecial_display_buffer_names;
165 /* List of regexps for buffer names that should have their own frames. */
167 Lisp_Object Vspecial_display_regexps;
169 /* Function to pop up a special frame. */
171 Lisp_Object Vspecial_display_function;
173 /* List of buffer *names* for buffers to appear in selected window. */
175 Lisp_Object Vsame_window_buffer_names;
177 /* List of regexps for buffer names to appear in selected window. */
179 Lisp_Object Vsame_window_regexps;
181 /* Hook run at end of temp_output_buffer_show. */
183 Lisp_Object Qtemp_buffer_show_hook;
185 /* Fdisplay_buffer always splits the largest window
186 if that window is more than this high. */
188 EMACS_INT split_height_threshold;
190 /* Number of lines of continuity in scrolling by screenfuls. */
192 EMACS_INT next_screen_context_lines;
194 /* Incremented for each window created. */
196 static int sequence_number;
198 /* Nonzero after init_window_once has finished. */
200 static int window_initialized;
202 /* Hook to run when window config changes. */
204 Lisp_Object Qwindow_configuration_change_hook;
205 Lisp_Object Vwindow_configuration_change_hook;
207 /* Nonzero means scroll commands try to put point
208 at the same screen height as previously. */
210 Lisp_Object Vscroll_preserve_screen_position;
212 #if 0 /* This isn't used anywhere. */
213 /* Nonzero means we can split a frame even if it is "unsplittable". */
214 static int inhibit_frame_unsplittable;
215 #endif /* 0 */
217 extern EMACS_INT scroll_margin;
219 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
221 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
222 doc: /* Returns t if OBJECT is a window. */)
223 (object)
224 Lisp_Object object;
226 return WINDOWP (object) ? Qt : Qnil;
229 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
230 doc: /* Returns t if OBJECT is a window which is currently visible. */)
231 (object)
232 Lisp_Object object;
234 return WINDOW_LIVE_P (object) ? Qt : Qnil;
237 Lisp_Object
238 make_window ()
240 Lisp_Object val;
241 register struct window *p;
243 p = allocate_window ();
244 XSETFASTINT (p->sequence_number, ++sequence_number);
245 XSETFASTINT (p->left_col, 0);
246 XSETFASTINT (p->top_line, 0);
247 XSETFASTINT (p->total_lines, 0);
248 XSETFASTINT (p->total_cols, 0);
249 XSETFASTINT (p->hscroll, 0);
250 XSETFASTINT (p->min_hscroll, 0);
251 p->orig_top_line = p->orig_total_lines = Qnil;
252 p->start = Fmake_marker ();
253 p->pointm = Fmake_marker ();
254 XSETFASTINT (p->use_time, 0);
255 p->frame = Qnil;
256 p->display_table = Qnil;
257 p->dedicated = Qnil;
258 p->pseudo_window_p = 0;
259 bzero (&p->cursor, sizeof (p->cursor));
260 bzero (&p->last_cursor, sizeof (p->last_cursor));
261 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
262 p->desired_matrix = p->current_matrix = 0;
263 p->phys_cursor_type = -1;
264 p->phys_cursor_width = -1;
265 p->must_be_updated_p = 0;
266 XSETFASTINT (p->window_end_vpos, 0);
267 XSETFASTINT (p->window_end_pos, 0);
268 p->window_end_valid = Qnil;
269 p->vscroll = 0;
270 XSETWINDOW (val, p);
271 XSETFASTINT (p->last_point, 0);
272 p->frozen_window_start_p = 0;
273 p->height_fixed_p = 0;
274 p->last_cursor_off_p = p->cursor_off_p = 0;
275 p->left_margin_cols = Qnil;
276 p->right_margin_cols = Qnil;
277 p->left_fringe_width = Qnil;
278 p->right_fringe_width = Qnil;
279 p->fringes_outside_margins = Qnil;
280 p->scroll_bar_width = Qnil;
281 p->vertical_scroll_bar_type = Qt;
282 p->overlay_arrow_bitmap = 0;
284 Vwindow_list = Qnil;
285 return val;
288 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
289 doc: /* Return the window that the cursor now appears in and commands apply to. */)
292 return selected_window;
295 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
296 doc: /* Return the window used now for minibuffers.
297 If the optional argument FRAME is specified, return the minibuffer window
298 used by that frame. */)
299 (frame)
300 Lisp_Object frame;
302 if (NILP (frame))
303 frame = selected_frame;
304 CHECK_LIVE_FRAME (frame);
305 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
308 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
309 doc: /* Returns non-nil if WINDOW is a minibuffer window.
310 WINDOW defaults to the selected window. */)
311 (window)
312 Lisp_Object window;
314 struct window *w = decode_window (window);
315 return MINI_WINDOW_P (w) ? Qt : Qnil;
319 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
320 Spos_visible_in_window_p, 0, 3, 0,
321 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
322 Return nil if that position is scrolled vertically out of view.
323 If a character is only partially visible, nil is returned, unless the
324 optional argument PARTIALLY is non-nil.
325 If POS is only out of view because of horizontal scrolling, return non-nil.
326 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
328 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
329 return value is a list (X Y FULLY) where X and Y are the pixel coordinates
330 relative to the top left corner of the window, and FULLY is t if the
331 character after POS is fully visible and nil otherwise. */)
332 (pos, window, partially)
333 Lisp_Object pos, window, partially;
335 register struct window *w;
336 register int posint;
337 register struct buffer *buf;
338 struct text_pos top;
339 Lisp_Object in_window = Qnil;
340 int fully_p = 1;
341 int x, y;
343 w = decode_window (window);
344 buf = XBUFFER (w->buffer);
345 SET_TEXT_POS_FROM_MARKER (top, w->start);
347 if (!NILP (pos))
349 CHECK_NUMBER_COERCE_MARKER (pos);
350 posint = XINT (pos);
352 else if (w == XWINDOW (selected_window))
353 posint = PT;
354 else
355 posint = XMARKER (w->pointm)->charpos;
357 /* If position is above window start or outside buffer boundaries,
358 or if window start is out of range, position is not visible. */
359 if (posint >= CHARPOS (top)
360 && posint <= BUF_ZV (buf)
361 && CHARPOS (top) >= BUF_BEGV (buf)
362 && CHARPOS (top) <= BUF_ZV (buf)
363 && pos_visible_p (w, posint, &fully_p, &x, &y, NILP (partially))
364 && (!NILP (partially) || fully_p))
365 in_window = Qt;
367 if (!NILP (in_window) && !NILP (partially))
368 in_window = Fcons (make_number (x),
369 Fcons (make_number (y),
370 Fcons (fully_p ? Qt : Qnil, Qnil)));
371 return in_window;
375 static struct window *
376 decode_window (window)
377 register Lisp_Object window;
379 if (NILP (window))
380 return XWINDOW (selected_window);
382 CHECK_LIVE_WINDOW (window);
383 return XWINDOW (window);
386 static struct window *
387 decode_any_window (window)
388 register Lisp_Object window;
390 if (NILP (window))
391 return XWINDOW (selected_window);
393 CHECK_WINDOW (window);
394 return XWINDOW (window);
397 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
398 doc: /* Return the buffer that WINDOW is displaying.
399 WINDOW defaults to the selected window. */)
400 (window)
401 Lisp_Object window;
403 return decode_window (window)->buffer;
406 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
407 doc: /* Return the number of lines in WINDOW (including its mode line). */)
408 (window)
409 Lisp_Object window;
411 return decode_any_window (window)->total_lines;
414 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
415 doc: /* Return the number of display columns in WINDOW.
416 This is the width that is usable columns available for text in WINDOW.
417 If you want to find out how many columns WINDOW takes up,
418 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
419 (window)
420 Lisp_Object window;
422 return make_number (window_box_text_cols (decode_any_window (window)));
425 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
426 doc: /* Return the number of columns by which WINDOW is scrolled from left margin. */)
427 (window)
428 Lisp_Object window;
430 return decode_window (window)->hscroll;
433 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
434 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
435 Return NCOL. NCOL should be zero or positive.
437 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
438 window so that the location of point becomes invisible. */)
439 (window, ncol)
440 Lisp_Object window, ncol;
442 struct window *w = decode_window (window);
443 int hscroll;
445 CHECK_NUMBER (ncol);
446 hscroll = max (0, XINT (ncol));
448 /* Prevent redisplay shortcuts when changing the hscroll. */
449 if (XINT (w->hscroll) != hscroll)
450 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
452 w->hscroll = make_number (hscroll);
453 return ncol;
456 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
457 Swindow_redisplay_end_trigger, 0, 1, 0,
458 doc: /* Return WINDOW's redisplay end trigger value.
459 See `set-window-redisplay-end-trigger' for more information. */)
460 (window)
461 Lisp_Object window;
463 return decode_window (window)->redisplay_end_trigger;
466 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
467 Sset_window_redisplay_end_trigger, 2, 2, 0,
468 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
469 VALUE should be a buffer position (typically a marker) or nil.
470 If it is a buffer position, then if redisplay in WINDOW reaches a position
471 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
472 with two arguments: WINDOW, and the end trigger value.
473 Afterwards the end-trigger value is reset to nil. */)
474 (window, value)
475 register Lisp_Object window, value;
477 register struct window *w;
479 w = decode_window (window);
480 w->redisplay_end_trigger = value;
481 return value;
484 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
485 doc: /* Return a list of the edge coordinates of WINDOW.
486 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
487 RIGHT is one more than the rightmost column occupied by WINDOW,
488 and BOTTOM is one more than the bottommost row occupied by WINDOW.
489 The edges include the space used by the window's scroll bar,
490 display margins, fringes, header line, and mode line, if it has them.
491 To get the edges of the actual text area, use `window-inside-edges'. */)
492 (window)
493 Lisp_Object window;
495 register struct window *w = decode_any_window (window);
497 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
498 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
499 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
500 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
501 Qnil))));
504 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
505 doc: /* Return a list of the edge pixel coordinates of WINDOW.
506 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
507 RIGHT is one more than the rightmost x position occupied by WINDOW,
508 and BOTTOM is one more than the bottommost y position occupied by WINDOW.
509 The pixel edges include the space used by the window's scroll bar,
510 display margins, fringes, header line, and mode line, if it has them.
511 To get the edges of the actual text area, use `window-inside-pixel-edges'. */)
512 (window)
513 Lisp_Object window;
515 register struct window *w = decode_any_window (window);
517 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
518 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
519 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
520 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
521 Qnil))));
524 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
525 doc: /* Return a list of the edge coordinates of WINDOW.
526 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
527 RIGHT is one more than the rightmost column used by text in WINDOW,
528 and BOTTOM is one more than the bottommost row used by text in WINDOW.
529 The inside edges do not include the space used by the window's scroll bar,
530 display margins, fringes, header line, and/or mode line. */)
531 (window)
532 Lisp_Object window;
534 register struct window *w = decode_any_window (window);
536 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
537 + WINDOW_LEFT_MARGIN_COLS (w)
538 + WINDOW_LEFT_FRINGE_COLS (w)),
539 make_number (WINDOW_TOP_EDGE_LINE (w)
540 + WINDOW_HEADER_LINE_LINES (w)),
541 make_number (WINDOW_RIGHT_EDGE_COL (w)
542 - WINDOW_RIGHT_MARGIN_COLS (w)
543 - WINDOW_RIGHT_FRINGE_COLS (w)),
544 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
545 - WINDOW_MODE_LINE_LINES (w)));
548 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
549 doc: /* Return a list of the edge pixel coordinates of WINDOW.
550 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
551 RIGHT is one more than the rightmost x position used by text in WINDOW,
552 and BOTTOM is one more than the bottommost y position used by text in WINDOW.
553 The inside edges do not include the space used by the window's scroll bar,
554 display margins, fringes, header line, and/or mode line. */)
555 (window)
556 Lisp_Object window;
558 register struct window *w = decode_any_window (window);
560 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
561 + WINDOW_LEFT_MARGIN_WIDTH (w)
562 + WINDOW_LEFT_FRINGE_WIDTH (w)),
563 make_number (WINDOW_TOP_EDGE_Y (w)
564 + WINDOW_HEADER_LINE_HEIGHT (w)),
565 make_number (WINDOW_RIGHT_EDGE_X (w)
566 - WINDOW_RIGHT_MARGIN_WIDTH (w)
567 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
568 make_number (WINDOW_BOTTOM_EDGE_Y (w)
569 - WINDOW_MODE_LINE_HEIGHT (w)));
572 /* Test if the character at column *X, row *Y is within window W.
573 If it is not, return ON_NOTHING;
574 if it is in the window's text area,
575 set *x and *y to its location relative to the upper left corner
576 of the window, and
577 return ON_TEXT;
578 if it is on the window's modeline, return ON_MODE_LINE;
579 if it is on the border between the window and its right sibling,
580 return ON_VERTICAL_BORDER.
581 if it is on the window's top line, return ON_HEADER_LINE;
582 if it is in left or right fringe of the window,
583 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
584 to window-relative coordinates;
585 if it is in the marginal area to the left/right of the window,
586 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
587 to window-relative coordinates.
589 X and Y are frame relative pixel coordinates. */
591 static enum window_part
592 coordinates_in_window (w, x, y)
593 register struct window *w;
594 register int *x, *y;
596 struct frame *f = XFRAME (WINDOW_FRAME (w));
597 int left_x, right_x, top_y, bottom_y;
598 enum window_part part;
599 int ux = FRAME_COLUMN_WIDTH (f);
600 int x0 = WINDOW_LEFT_EDGE_X (w);
601 int x1 = WINDOW_RIGHT_EDGE_X (w);
602 /* The width of the area where the vertical line can be dragged.
603 (Between mode lines for instance. */
604 int grabbable_width = ux;
605 int lmargin_width, rmargin_width, text_left, text_right;
607 if (*x < x0 || *x >= x1)
608 return ON_NOTHING;
610 /* In what's below, we subtract 1 when computing right_x because we
611 want the rightmost pixel, which is given by left_pixel+width-1. */
612 if (w->pseudo_window_p)
614 left_x = 0;
615 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
616 top_y = WINDOW_TOP_EDGE_Y (w);
617 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
619 else
621 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
622 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
623 top_y = WINDOW_TOP_EDGE_Y (w);
624 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
627 /* Outside any interesting row? */
628 if (*y < top_y || *y >= bottom_y)
629 return ON_NOTHING;
631 /* On the mode line or header line? If it's near the start of
632 the mode or header line of window that's has a horizontal
633 sibling, say it's on the vertical line. That's to be able
634 to resize windows horizontally in case we're using toolkit
635 scroll bars. */
637 if (WINDOW_WANTS_MODELINE_P (w)
638 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
640 part = ON_MODE_LINE;
642 header_vertical_border_check:
643 /* We're somewhere on the mode line. We consider the place
644 between mode lines of horizontally adjacent mode lines
645 as the vertical border. If scroll bars on the left,
646 return the right window. */
647 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
648 || WINDOW_RIGHTMOST_P (w))
650 if (!WINDOW_LEFTMOST_P (w) && abs (*x - x0) < grabbable_width)
651 return ON_VERTICAL_BORDER;
653 else
655 if (abs (*x - x1) < grabbable_width)
656 return ON_VERTICAL_BORDER;
659 /* Convert X and Y to window relative coordinates.
660 Mode line starts at left edge of window. */
661 *x -= x0;
662 *y -= top_y;
663 return part;
666 if (WINDOW_WANTS_HEADER_LINE_P (w)
667 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
669 part = ON_HEADER_LINE;
670 goto header_vertical_border_check;
673 /* Outside any interesting column? */
674 if (*x < left_x || *x > right_x)
675 return ON_VERTICAL_BORDER;
677 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
678 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
680 text_left = window_box_left (w, TEXT_AREA);
681 text_right = text_left + window_box_width (w, TEXT_AREA);
683 if (FRAME_WINDOW_P (f))
685 if (!w->pseudo_window_p
686 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
687 && !WINDOW_RIGHTMOST_P (w)
688 && (abs (*x - right_x) < grabbable_width))
689 return ON_VERTICAL_BORDER;
691 else
693 /* Need to say "*x > right_x" rather than >=, since on character
694 terminals, the vertical line's x coordinate is right_x. */
695 if (!w->pseudo_window_p
696 && !WINDOW_RIGHTMOST_P (w)
697 && *x > right_x - ux)
699 /* On the border on the right side of the window? Assume that
700 this area begins at RIGHT_X minus a canonical char width. */
701 return ON_VERTICAL_BORDER;
705 if (*x < text_left)
707 if (lmargin_width > 0
708 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
709 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
710 : (*x < left_x + lmargin_width)))
712 *x -= left_x;
713 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
714 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
715 *y -= top_y;
716 return ON_LEFT_MARGIN;
719 /* Convert X and Y to window-relative pixel coordinates. */
720 *x -= left_x;
721 *y -= top_y;
722 return ON_LEFT_FRINGE;
725 if (*x >= text_right)
727 if (rmargin_width > 0
728 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
729 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
730 : (*x >= right_x - rmargin_width)))
732 *x -= right_x;
733 if (!WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
734 *x -= WINDOW_RIGHT_FRINGE_WIDTH (w);
735 *y -= top_y;
736 return ON_RIGHT_MARGIN;
739 /* Convert X and Y to window-relative pixel coordinates. */
740 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
741 *y -= top_y;
742 return ON_RIGHT_FRINGE;
745 /* Everything special ruled out - must be on text area */
746 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
747 *y -= top_y;
748 return ON_TEXT;
752 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
753 Scoordinates_in_window_p, 2, 2, 0,
754 doc: /* Return non-nil if COORDINATES are in WINDOW.
755 COORDINATES is a cons of the form (X . Y), X and Y being distances
756 measured in characters from the upper-left corner of the frame.
757 \(0 . 0) denotes the character in the upper left corner of the
758 frame.
759 If COORDINATES are in the text portion of WINDOW,
760 the coordinates relative to the window are returned.
761 If they are in the mode line of WINDOW, `mode-line' is returned.
762 If they are in the top mode line of WINDOW, `header-line' is returned.
763 If they are in the left fringe of WINDOW, `left-fringe' is returned.
764 If they are in the right fringe of WINDOW, `right-fringe' is returned.
765 If they are on the border between WINDOW and its right sibling,
766 `vertical-line' is returned.
767 If they are in the windows's left or right marginal areas, `left-margin'\n\
768 or `right-margin' is returned. */)
769 (coordinates, window)
770 register Lisp_Object coordinates, window;
772 struct window *w;
773 struct frame *f;
774 int x, y;
775 Lisp_Object lx, ly;
777 CHECK_WINDOW (window);
778 w = XWINDOW (window);
779 f = XFRAME (w->frame);
780 CHECK_CONS (coordinates);
781 lx = Fcar (coordinates);
782 ly = Fcdr (coordinates);
783 CHECK_NUMBER_OR_FLOAT (lx);
784 CHECK_NUMBER_OR_FLOAT (ly);
785 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx);
786 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly);
788 switch (coordinates_in_window (w, &x, &y))
790 case ON_NOTHING:
791 return Qnil;
793 case ON_TEXT:
794 /* X and Y are now window relative pixel coordinates. Convert
795 them to canonical char units before returning them. */
796 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
797 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
799 case ON_MODE_LINE:
800 return Qmode_line;
802 case ON_VERTICAL_BORDER:
803 return Qvertical_line;
805 case ON_HEADER_LINE:
806 return Qheader_line;
808 case ON_LEFT_FRINGE:
809 return Qleft_fringe;
811 case ON_RIGHT_FRINGE:
812 return Qright_fringe;
814 case ON_LEFT_MARGIN:
815 return Qleft_margin;
817 case ON_RIGHT_MARGIN:
818 return Qright_margin;
820 default:
821 abort ();
826 /* Callback for foreach_window, used in window_from_coordinates.
827 Check if window W contains coordinates specified by USER_DATA which
828 is actually a pointer to a struct check_window_data CW.
830 Check if window W contains coordinates *CW->x and *CW->y. If it
831 does, return W in *CW->window, as Lisp_Object, and return in
832 *CW->part the part of the window under coordinates *X,*Y. Return
833 zero from this function to stop iterating over windows. */
835 struct check_window_data
837 Lisp_Object *window;
838 int *x, *y;
839 enum window_part *part;
842 static int
843 check_window_containing (w, user_data)
844 struct window *w;
845 void *user_data;
847 struct check_window_data *cw = (struct check_window_data *) user_data;
848 enum window_part found;
849 int continue_p = 1;
851 found = coordinates_in_window (w, cw->x, cw->y);
852 if (found != ON_NOTHING)
854 *cw->part = found;
855 XSETWINDOW (*cw->window, w);
856 continue_p = 0;
859 return continue_p;
863 /* Find the window containing frame-relative pixel position X/Y and
864 return it as a Lisp_Object.
866 If X, Y is on one of the window's special `window_part' elements,
867 set *PART to the id of that element, and return X and Y converted
868 to window relative coordinates in WX and WY.
870 If there is no window under X, Y return nil and leave *PART
871 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
873 This function was previously implemented with a loop cycling over
874 windows with Fnext_window, and starting with the frame's selected
875 window. It turned out that this doesn't work with an
876 implementation of next_window using Vwindow_list, because
877 FRAME_SELECTED_WINDOW (F) is not always contained in the window
878 tree of F when this function is called asynchronously from
879 note_mouse_highlight. The original loop didn't terminate in this
880 case. */
882 Lisp_Object
883 window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
884 struct frame *f;
885 int x, y;
886 enum window_part *part;
887 int *wx, *wy;
888 int tool_bar_p;
890 Lisp_Object window;
891 struct check_window_data cw;
892 enum window_part dummy;
894 if (part == 0)
895 part = &dummy;
897 window = Qnil;
898 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
899 foreach_window (f, check_window_containing, &cw);
901 /* If not found above, see if it's in the tool bar window, if a tool
902 bar exists. */
903 if (NILP (window)
904 && tool_bar_p
905 && WINDOWP (f->tool_bar_window)
906 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
907 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
908 != ON_NOTHING))
910 *part = ON_TEXT;
911 window = f->tool_bar_window;
914 if (wx) *wx = x;
915 if (wy) *wy = y;
917 return window;
920 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
921 doc: /* Return window containing coordinates X and Y on FRAME.
922 If omitted, FRAME defaults to the currently selected frame.
923 The top left corner of the frame is considered to be row 0,
924 column 0. */)
925 (x, y, frame)
926 Lisp_Object x, y, frame;
928 struct frame *f;
930 if (NILP (frame))
931 frame = selected_frame;
932 CHECK_LIVE_FRAME (frame);
933 f = XFRAME (frame);
935 /* Check that arguments are integers or floats. */
936 CHECK_NUMBER_OR_FLOAT (x);
937 CHECK_NUMBER_OR_FLOAT (y);
939 return window_from_coordinates (f,
940 FRAME_PIXEL_X_FROM_CANON_X (f, x),
941 FRAME_PIXEL_Y_FROM_CANON_Y (f, y),
942 0, 0, 0, 0);
945 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
946 doc: /* Return current value of point in WINDOW.
947 For a nonselected window, this is the value point would have
948 if that window were selected.
950 Note that, when WINDOW is the selected window and its buffer
951 is also currently selected, the value returned is the same as (point).
952 It would be more strictly correct to return the `top-level' value
953 of point, outside of any save-excursion forms.
954 But that is hard to define. */)
955 (window)
956 Lisp_Object window;
958 register struct window *w = decode_window (window);
960 if (w == XWINDOW (selected_window)
961 && current_buffer == XBUFFER (w->buffer))
962 return Fpoint ();
963 return Fmarker_position (w->pointm);
966 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
967 doc: /* Return position at which display currently starts in WINDOW.
968 This is updated by redisplay or by calling `set-window-start'. */)
969 (window)
970 Lisp_Object window;
972 return Fmarker_position (decode_window (window)->start);
975 /* This is text temporarily removed from the doc string below.
977 This function returns nil if the position is not currently known.
978 That happens when redisplay is preempted and doesn't finish.
979 If in that case you want to compute where the end of the window would
980 have been if redisplay had finished, do this:
981 (save-excursion
982 (goto-char (window-start window))
983 (vertical-motion (1- (window-height window)) window)
984 (point))") */
986 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
987 doc: /* Return position at which display currently ends in WINDOW.
988 This is updated by redisplay, when it runs to completion.
989 Simply changing the buffer text or setting `window-start'
990 does not update this value.
991 Return nil if there is no recorded value. \(This can happen if the
992 last redisplay of WINDOW was preempted, and did not finish.)
993 If UPDATE is non-nil, compute the up-to-date position
994 if it isn't already recorded. */)
995 (window, update)
996 Lisp_Object window, update;
998 Lisp_Object value;
999 struct window *w = decode_window (window);
1000 Lisp_Object buf;
1002 buf = w->buffer;
1003 CHECK_BUFFER (buf);
1005 #if 0 /* This change broke some things. We should make it later. */
1006 /* If we don't know the end position, return nil.
1007 The user can compute it with vertical-motion if he wants to.
1008 It would be nicer to do it automatically,
1009 but that's so slow that it would probably bother people. */
1010 if (NILP (w->window_end_valid))
1011 return Qnil;
1012 #endif
1014 if (! NILP (update)
1015 && ! (! NILP (w->window_end_valid)
1016 && XFASTINT (w->last_modified) >= MODIFF))
1018 struct text_pos startp;
1019 struct it it;
1020 struct buffer *old_buffer = NULL, *b = XBUFFER (buf);
1022 /* In case W->start is out of the range, use something
1023 reasonable. This situation occurred when loading a file with
1024 `-l' containing a call to `rmail' with subsequent other
1025 commands. At the end, W->start happened to be BEG, while
1026 rmail had already narrowed the buffer. */
1027 if (XMARKER (w->start)->charpos < BEGV)
1028 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1029 else if (XMARKER (w->start)->charpos > ZV)
1030 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1031 else
1032 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1034 /* Cannot use Fvertical_motion because that function doesn't
1035 cope with variable-height lines. */
1036 if (b != current_buffer)
1038 old_buffer = current_buffer;
1039 set_buffer_internal (b);
1042 start_display (&it, w, startp);
1043 move_it_vertically (&it, window_box_height (w));
1044 if (it.current_y < it.last_visible_y)
1045 move_it_past_eol (&it);
1046 value = make_number (IT_CHARPOS (it));
1048 if (old_buffer)
1049 set_buffer_internal (old_buffer);
1051 else
1052 XSETINT (value, BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
1054 return value;
1057 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1058 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1059 Return POS. */)
1060 (window, pos)
1061 Lisp_Object window, pos;
1063 register struct window *w = decode_window (window);
1065 CHECK_NUMBER_COERCE_MARKER (pos);
1066 if (w == XWINDOW (selected_window)
1067 && XBUFFER (w->buffer) == current_buffer)
1068 Fgoto_char (pos);
1069 else
1070 set_marker_restricted (w->pointm, pos, w->buffer);
1072 /* We have to make sure that redisplay updates the window to show
1073 the new value of point. */
1074 if (!EQ (window, selected_window))
1075 ++windows_or_buffers_changed;
1077 return pos;
1080 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1081 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1082 Return POS.
1083 Optional third arg NOFORCE non-nil inhibits next redisplay
1084 from overriding motion of point in order to display at this exact start. */)
1085 (window, pos, noforce)
1086 Lisp_Object window, pos, noforce;
1088 register struct window *w = decode_window (window);
1090 CHECK_NUMBER_COERCE_MARKER (pos);
1091 set_marker_restricted (w->start, pos, w->buffer);
1092 /* this is not right, but much easier than doing what is right. */
1093 w->start_at_line_beg = Qnil;
1094 if (NILP (noforce))
1095 w->force_start = Qt;
1096 w->update_mode_line = Qt;
1097 XSETFASTINT (w->last_modified, 0);
1098 XSETFASTINT (w->last_overlay_modified, 0);
1099 if (!EQ (window, selected_window))
1100 windows_or_buffers_changed++;
1102 return pos;
1105 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1106 1, 1, 0,
1107 doc: /* Return WINDOW's dedicated object, usually t or nil.
1108 See also `set-window-dedicated-p'. */)
1109 (window)
1110 Lisp_Object window;
1112 return decode_window (window)->dedicated;
1115 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1116 Sset_window_dedicated_p, 2, 2, 0,
1117 doc: /* Control whether WINDOW is dedicated to the buffer it displays.
1118 If it is dedicated, Emacs will not automatically change
1119 which buffer appears in it.
1120 The second argument is the new value for the dedication flag;
1121 non-nil means yes. */)
1122 (window, arg)
1123 Lisp_Object window, arg;
1125 register struct window *w = decode_window (window);
1127 w->dedicated = arg;
1129 return w->dedicated;
1132 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1133 0, 1, 0,
1134 doc: /* Return the display-table that WINDOW is using. */)
1135 (window)
1136 Lisp_Object window;
1138 return decode_window (window)->display_table;
1141 /* Get the display table for use on window W. This is either W's
1142 display table or W's buffer's display table. Ignore the specified
1143 tables if they are not valid; if no valid table is specified,
1144 return 0. */
1146 struct Lisp_Char_Table *
1147 window_display_table (w)
1148 struct window *w;
1150 struct Lisp_Char_Table *dp = NULL;
1152 if (DISP_TABLE_P (w->display_table))
1153 dp = XCHAR_TABLE (w->display_table);
1154 else if (BUFFERP (w->buffer))
1156 struct buffer *b = XBUFFER (w->buffer);
1158 if (DISP_TABLE_P (b->display_table))
1159 dp = XCHAR_TABLE (b->display_table);
1160 else if (DISP_TABLE_P (Vstandard_display_table))
1161 dp = XCHAR_TABLE (Vstandard_display_table);
1164 return dp;
1167 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1168 doc: /* Set WINDOW's display-table to TABLE. */)
1169 (window, table)
1170 register Lisp_Object window, table;
1172 register struct window *w;
1174 w = decode_window (window);
1175 w->display_table = table;
1176 return table;
1179 /* Record info on buffer window w is displaying
1180 when it is about to cease to display that buffer. */
1181 static void
1182 unshow_buffer (w)
1183 register struct window *w;
1185 Lisp_Object buf;
1186 struct buffer *b;
1188 buf = w->buffer;
1189 b = XBUFFER (buf);
1190 if (b != XMARKER (w->pointm)->buffer)
1191 abort ();
1193 #if 0
1194 if (w == XWINDOW (selected_window)
1195 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1196 /* Do this except when the selected window's buffer
1197 is being removed from some other window. */
1198 #endif
1199 /* last_window_start records the start position that this buffer
1200 had in the last window to be disconnected from it.
1201 Now that this statement is unconditional,
1202 it is possible for the buffer to be displayed in the
1203 selected window, while last_window_start reflects another
1204 window which was recently showing the same buffer.
1205 Some people might say that might be a good thing. Let's see. */
1206 b->last_window_start = marker_position (w->start);
1208 /* Point in the selected window's buffer
1209 is actually stored in that buffer, and the window's pointm isn't used.
1210 So don't clobber point in that buffer. */
1211 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1212 /* This line helps to fix Horsley's testbug.el bug. */
1213 && !(WINDOWP (b->last_selected_window)
1214 && w != XWINDOW (b->last_selected_window)
1215 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1216 temp_set_point_both (b,
1217 clip_to_bounds (BUF_BEGV (b),
1218 XMARKER (w->pointm)->charpos,
1219 BUF_ZV (b)),
1220 clip_to_bounds (BUF_BEGV_BYTE (b),
1221 marker_byte_position (w->pointm),
1222 BUF_ZV_BYTE (b)));
1224 if (WINDOWP (b->last_selected_window)
1225 && w == XWINDOW (b->last_selected_window))
1226 b->last_selected_window = Qnil;
1229 /* Put replacement into the window structure in place of old. */
1230 static void
1231 replace_window (old, replacement)
1232 Lisp_Object old, replacement;
1234 register Lisp_Object tem;
1235 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1237 /* If OLD is its frame's root_window, then replacement is the new
1238 root_window for that frame. */
1240 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1241 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1243 p->left_col = o->left_col;
1244 p->top_line = o->top_line;
1245 p->total_cols = o->total_cols;
1246 p->total_lines = o->total_lines;
1247 p->desired_matrix = p->current_matrix = 0;
1248 p->vscroll = 0;
1249 bzero (&p->cursor, sizeof (p->cursor));
1250 bzero (&p->last_cursor, sizeof (p->last_cursor));
1251 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1252 p->phys_cursor_type = -1;
1253 p->phys_cursor_width = -1;
1254 p->must_be_updated_p = 0;
1255 p->pseudo_window_p = 0;
1256 XSETFASTINT (p->window_end_vpos, 0);
1257 XSETFASTINT (p->window_end_pos, 0);
1258 p->window_end_valid = Qnil;
1259 p->frozen_window_start_p = 0;
1260 p->orig_top_line = p->orig_total_lines = Qnil;
1262 p->next = tem = o->next;
1263 if (!NILP (tem))
1264 XWINDOW (tem)->prev = replacement;
1266 p->prev = tem = o->prev;
1267 if (!NILP (tem))
1268 XWINDOW (tem)->next = replacement;
1270 p->parent = tem = o->parent;
1271 if (!NILP (tem))
1273 if (EQ (XWINDOW (tem)->vchild, old))
1274 XWINDOW (tem)->vchild = replacement;
1275 if (EQ (XWINDOW (tem)->hchild, old))
1276 XWINDOW (tem)->hchild = replacement;
1279 /*** Here, if replacement is a vertical combination
1280 and so is its new parent, we should make replacement's
1281 children be children of that parent instead. ***/
1284 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1285 doc: /* Remove WINDOW from the display. Default is selected window. */)
1286 (window)
1287 register Lisp_Object window;
1289 delete_window (window);
1291 if (! NILP (Vwindow_configuration_change_hook)
1292 && ! NILP (Vrun_hooks))
1293 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1295 return Qnil;
1298 void
1299 delete_window (window)
1300 register Lisp_Object window;
1302 register Lisp_Object tem, parent, sib;
1303 register struct window *p;
1304 register struct window *par;
1305 struct frame *f;
1307 /* Because this function is called by other C code on non-leaf
1308 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1309 so we can't decode_window here. */
1310 if (NILP (window))
1311 window = selected_window;
1312 else
1313 CHECK_WINDOW (window);
1314 p = XWINDOW (window);
1316 /* It's okay to delete an already-deleted window. */
1317 if (NILP (p->buffer)
1318 && NILP (p->hchild)
1319 && NILP (p->vchild))
1320 return;
1322 parent = p->parent;
1323 if (NILP (parent))
1324 error ("Attempt to delete minibuffer or sole ordinary window");
1325 par = XWINDOW (parent);
1327 windows_or_buffers_changed++;
1328 Vwindow_list = Qnil;
1329 f = XFRAME (WINDOW_FRAME (p));
1330 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1332 /* Are we trying to delete any frame's selected window? */
1334 Lisp_Object swindow, pwindow;
1336 /* See if the frame's selected window is either WINDOW
1337 or any subwindow of it, by finding all that window's parents
1338 and comparing each one with WINDOW. */
1339 swindow = FRAME_SELECTED_WINDOW (f);
1341 while (1)
1343 pwindow = swindow;
1344 while (!NILP (pwindow))
1346 if (EQ (window, pwindow))
1347 break;
1348 pwindow = XWINDOW (pwindow)->parent;
1351 /* If the window being deleted is not a parent of SWINDOW,
1352 then SWINDOW is ok as the new selected window. */
1353 if (!EQ (window, pwindow))
1354 break;
1355 /* Otherwise, try another window for SWINDOW. */
1356 swindow = Fnext_window (swindow, Qlambda, Qnil);;
1358 /* If we get back to the frame's selected window,
1359 it means there was no acceptable alternative,
1360 so we cannot delete. */
1361 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1362 error ("Cannot delete window");
1365 /* If we need to change SWINDOW, do it. */
1366 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1368 /* If we're about to delete the selected window on the
1369 selected frame, then we should use Fselect_window to select
1370 the new window. On the other hand, if we're about to
1371 delete the selected window on any other frame, we shouldn't do
1372 anything but set the frame's selected_window slot. */
1373 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1374 Fselect_window (swindow, Qnil);
1375 else
1376 FRAME_SELECTED_WINDOW (f) = swindow;
1380 tem = p->buffer;
1381 /* tem is null for dummy parent windows
1382 (which have inferiors but not any contents themselves) */
1383 if (!NILP (tem))
1385 unshow_buffer (p);
1386 unchain_marker (XMARKER (p->pointm));
1387 unchain_marker (XMARKER (p->start));
1390 /* Free window glyph matrices. It is sure that they are allocated
1391 again when ADJUST_GLYPHS is called. Block input so that expose
1392 events and other events that access glyph matrices are not
1393 processed while we are changing them. */
1394 BLOCK_INPUT;
1395 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1397 tem = p->next;
1398 if (!NILP (tem))
1399 XWINDOW (tem)->prev = p->prev;
1401 tem = p->prev;
1402 if (!NILP (tem))
1403 XWINDOW (tem)->next = p->next;
1405 if (EQ (window, par->hchild))
1406 par->hchild = p->next;
1407 if (EQ (window, par->vchild))
1408 par->vchild = p->next;
1410 /* Find one of our siblings to give our space to. */
1411 sib = p->prev;
1412 if (NILP (sib))
1414 /* If p gives its space to its next sibling, that sibling needs
1415 to have its top/left side pulled back to where p's is.
1416 set_window_{height,width} will re-position the sibling's
1417 children. */
1418 sib = p->next;
1419 XWINDOW (sib)->top_line = p->top_line;
1420 XWINDOW (sib)->left_col = p->left_col;
1423 /* Stretch that sibling. */
1424 if (!NILP (par->vchild))
1425 set_window_height (sib,
1426 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1428 if (!NILP (par->hchild))
1429 set_window_width (sib,
1430 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1433 /* If parent now has only one child,
1434 put the child into the parent's place. */
1435 tem = par->hchild;
1436 if (NILP (tem))
1437 tem = par->vchild;
1438 if (NILP (XWINDOW (tem)->next))
1439 replace_window (parent, tem);
1441 /* Since we may be deleting combination windows, we must make sure that
1442 not only p but all its children have been marked as deleted. */
1443 if (! NILP (p->hchild))
1444 delete_all_subwindows (XWINDOW (p->hchild));
1445 else if (! NILP (p->vchild))
1446 delete_all_subwindows (XWINDOW (p->vchild));
1448 /* Mark this window as deleted. */
1449 p->buffer = p->hchild = p->vchild = Qnil;
1451 /* Adjust glyph matrices. */
1452 adjust_glyphs (f);
1453 UNBLOCK_INPUT;
1458 /***********************************************************************
1459 Window List
1460 ***********************************************************************/
1462 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1463 pointer. This is a callback function for foreach_window, used in
1464 function window_list. */
1466 static int
1467 add_window_to_list (w, user_data)
1468 struct window *w;
1469 void *user_data;
1471 Lisp_Object *list = (Lisp_Object *) user_data;
1472 Lisp_Object window;
1473 XSETWINDOW (window, w);
1474 *list = Fcons (window, *list);
1475 return 1;
1479 /* Return a list of all windows, for use by next_window. If
1480 Vwindow_list is a list, return that list. Otherwise, build a new
1481 list, cache it in Vwindow_list, and return that. */
1483 static Lisp_Object
1484 window_list ()
1486 if (!CONSP (Vwindow_list))
1488 Lisp_Object tail;
1490 Vwindow_list = Qnil;
1491 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1493 Lisp_Object args[2];
1495 /* We are visiting windows in canonical order, and add
1496 new windows at the front of args[1], which means we
1497 have to reverse this list at the end. */
1498 args[1] = Qnil;
1499 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1500 args[0] = Vwindow_list;
1501 args[1] = Fnreverse (args[1]);
1502 Vwindow_list = Fnconc (2, args);
1506 return Vwindow_list;
1510 /* Value is non-zero if WINDOW satisfies the constraints given by
1511 OWINDOW, MINIBUF and ALL_FRAMES.
1513 MINIBUF t means WINDOW may be minibuffer windows.
1514 `lambda' means WINDOW may not be a minibuffer window.
1515 a window means a specific minibuffer window
1517 ALL_FRAMES t means search all frames,
1518 nil means search just current frame,
1519 `visible' means search just visible frames,
1520 0 means search visible and iconified frames,
1521 a window means search the frame that window belongs to,
1522 a frame means consider windows on that frame, only. */
1524 static int
1525 candidate_window_p (window, owindow, minibuf, all_frames)
1526 Lisp_Object window, owindow, minibuf, all_frames;
1528 struct window *w = XWINDOW (window);
1529 struct frame *f = XFRAME (w->frame);
1530 int candidate_p = 1;
1532 if (!BUFFERP (w->buffer))
1533 candidate_p = 0;
1534 else if (MINI_WINDOW_P (w)
1535 && (EQ (minibuf, Qlambda)
1536 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1538 /* If MINIBUF is `lambda' don't consider any mini-windows.
1539 If it is a window, consider only that one. */
1540 candidate_p = 0;
1542 else if (EQ (all_frames, Qt))
1543 candidate_p = 1;
1544 else if (NILP (all_frames))
1546 xassert (WINDOWP (owindow));
1547 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1549 else if (EQ (all_frames, Qvisible))
1551 FRAME_SAMPLE_VISIBILITY (f);
1552 candidate_p = FRAME_VISIBLE_P (f);
1554 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1556 FRAME_SAMPLE_VISIBILITY (f);
1557 candidate_p = FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1559 else if (WINDOWP (all_frames))
1560 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1561 || EQ (XWINDOW (all_frames)->frame, w->frame)
1562 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1563 else if (FRAMEP (all_frames))
1564 candidate_p = EQ (all_frames, w->frame);
1566 return candidate_p;
1570 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1571 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1572 ALL_FRAMES. */
1574 static void
1575 decode_next_window_args (window, minibuf, all_frames)
1576 Lisp_Object *window, *minibuf, *all_frames;
1578 if (NILP (*window))
1579 *window = selected_window;
1580 else
1581 CHECK_LIVE_WINDOW (*window);
1583 /* MINIBUF nil may or may not include minibuffers. Decide if it
1584 does. */
1585 if (NILP (*minibuf))
1586 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1587 else if (!EQ (*minibuf, Qt))
1588 *minibuf = Qlambda;
1590 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1591 => count none of them, or a specific minibuffer window (the
1592 active one) to count. */
1594 /* ALL_FRAMES nil doesn't specify which frames to include. */
1595 if (NILP (*all_frames))
1596 *all_frames = (!EQ (*minibuf, Qlambda)
1597 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1598 : Qnil);
1599 else if (EQ (*all_frames, Qvisible))
1601 else if (XFASTINT (*all_frames) == 0)
1603 else if (FRAMEP (*all_frames))
1605 else if (!EQ (*all_frames, Qt))
1606 *all_frames = Qnil;
1608 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1609 search just current frame, `visible' meaning search just visible
1610 frames, 0 meaning search visible and iconified frames, or a
1611 window, meaning search the frame that window belongs to, or a
1612 frame, meaning consider windows on that frame, only. */
1616 /* Return the next or previous window of WINDOW in canonical ordering
1617 of windows. NEXT_P non-zero means return the next window. See the
1618 documentation string of next-window for the meaning of MINIBUF and
1619 ALL_FRAMES. */
1621 static Lisp_Object
1622 next_window (window, minibuf, all_frames, next_p)
1623 Lisp_Object window, minibuf, all_frames;
1624 int next_p;
1626 decode_next_window_args (&window, &minibuf, &all_frames);
1628 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1629 return the first window on the frame. */
1630 if (FRAMEP (all_frames)
1631 && !EQ (all_frames, XWINDOW (window)->frame))
1632 return Fframe_first_window (all_frames);
1634 if (next_p)
1636 Lisp_Object list;
1638 /* Find WINDOW in the list of all windows. */
1639 list = Fmemq (window, window_list ());
1641 /* Scan forward from WINDOW to the end of the window list. */
1642 if (CONSP (list))
1643 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1644 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1645 break;
1647 /* Scan from the start of the window list up to WINDOW. */
1648 if (!CONSP (list))
1649 for (list = Vwindow_list;
1650 CONSP (list) && !EQ (XCAR (list), window);
1651 list = XCDR (list))
1652 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1653 break;
1655 if (CONSP (list))
1656 window = XCAR (list);
1658 else
1660 Lisp_Object candidate, list;
1662 /* Scan through the list of windows for candidates. If there are
1663 candidate windows in front of WINDOW, the last one of these
1664 is the one we want. If there are candidates following WINDOW
1665 in the list, again the last one of these is the one we want. */
1666 candidate = Qnil;
1667 for (list = window_list (); CONSP (list); list = XCDR (list))
1669 if (EQ (XCAR (list), window))
1671 if (WINDOWP (candidate))
1672 break;
1674 else if (candidate_window_p (XCAR (list), window, minibuf,
1675 all_frames))
1676 candidate = XCAR (list);
1679 if (WINDOWP (candidate))
1680 window = candidate;
1683 return window;
1687 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1688 doc: /* Return next window after WINDOW in canonical ordering of windows.
1689 If omitted, WINDOW defaults to the selected window.
1691 Optional second arg MINIBUF t means count the minibuffer window even
1692 if not active. MINIBUF nil or omitted means count the minibuffer iff
1693 it is active. MINIBUF neither t nor nil means not to count the
1694 minibuffer even if it is active.
1696 Several frames may share a single minibuffer; if the minibuffer
1697 counts, all windows on all frames that share that minibuffer count
1698 too. Therefore, `next-window' can be used to iterate through the
1699 set of windows even when the minibuffer is on another frame. If the
1700 minibuffer does not count, only windows from WINDOW's frame count.
1702 Optional third arg ALL-FRAMES t means include windows on all frames.
1703 ALL-FRAMES nil or omitted means cycle within the frames as specified
1704 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1705 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1706 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1707 Anything else means restrict to WINDOW's frame.
1709 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1710 `next-window' to iterate through the entire cycle of acceptable
1711 windows, eventually ending up back at the window you started with.
1712 `previous-window' traverses the same cycle, in the reverse order. */)
1713 (window, minibuf, all_frames)
1714 Lisp_Object window, minibuf, all_frames;
1716 return next_window (window, minibuf, all_frames, 1);
1720 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1721 doc: /* Return the window preceding WINDOW in canonical ordering of windows.
1722 If omitted, WINDOW defaults to the selected window.
1724 Optional second arg MINIBUF t means count the minibuffer window even
1725 if not active. MINIBUF nil or omitted means count the minibuffer iff
1726 it is active. MINIBUF neither t nor nil means not to count the
1727 minibuffer even if it is active.
1729 Several frames may share a single minibuffer; if the minibuffer
1730 counts, all windows on all frames that share that minibuffer count
1731 too. Therefore, `previous-window' can be used to iterate through
1732 the set of windows even when the minibuffer is on another frame. If
1733 the minibuffer does not count, only windows from WINDOW's frame count
1735 Optional third arg ALL-FRAMES t means include windows on all frames.
1736 ALL-FRAMES nil or omitted means cycle within the frames as specified
1737 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1738 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1739 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1740 Anything else means restrict to WINDOW's frame.
1742 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1743 `previous-window' to iterate through the entire cycle of acceptable
1744 windows, eventually ending up back at the window you started with.
1745 `next-window' traverses the same cycle, in the reverse order. */)
1746 (window, minibuf, all_frames)
1747 Lisp_Object window, minibuf, all_frames;
1749 return next_window (window, minibuf, all_frames, 0);
1753 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1754 doc: /* Select the ARG'th different window on this frame.
1755 All windows on current frame are arranged in a cyclic order.
1756 This command selects the window ARG steps away in that order.
1757 A negative ARG moves in the opposite order. The optional second
1758 argument ALL_FRAMES has the same meaning as in `next-window', which see. */)
1759 (arg, all_frames)
1760 Lisp_Object arg, all_frames;
1762 Lisp_Object window;
1763 int i;
1765 CHECK_NUMBER (arg);
1766 window = selected_window;
1768 for (i = XINT (arg); i > 0; --i)
1769 window = Fnext_window (window, Qnil, all_frames);
1770 for (; i < 0; ++i)
1771 window = Fprevious_window (window, Qnil, all_frames);
1773 Fselect_window (window, Qnil);
1774 return Qnil;
1778 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
1779 doc: /* Return a list of windows on FRAME, starting with WINDOW.
1780 FRAME nil or omitted means use the selected frame.
1781 WINDOW nil or omitted means use the selected window.
1782 MINIBUF t means include the minibuffer window, even if it isn't active.
1783 MINIBUF nil or omitted means include the minibuffer window only
1784 if it's active.
1785 MINIBUF neither nil nor t means never include the minibuffer window. */)
1786 (frame, minibuf, window)
1787 Lisp_Object frame, minibuf, window;
1789 if (NILP (window))
1790 window = selected_window;
1791 if (NILP (frame))
1792 frame = selected_frame;
1794 if (!EQ (frame, XWINDOW (window)->frame))
1795 error ("Window is on a different frame");
1797 return window_list_1 (window, minibuf, frame);
1801 /* Return a list of windows in canonical ordering. Arguments are like
1802 for `next-window'. */
1804 static Lisp_Object
1805 window_list_1 (window, minibuf, all_frames)
1806 Lisp_Object window, minibuf, all_frames;
1808 Lisp_Object tail, list, rest;
1810 decode_next_window_args (&window, &minibuf, &all_frames);
1811 list = Qnil;
1813 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
1814 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
1815 list = Fcons (XCAR (tail), list);
1817 /* Rotate the list to start with WINDOW. */
1818 list = Fnreverse (list);
1819 rest = Fmemq (window, list);
1820 if (!NILP (rest) && !EQ (rest, list))
1822 for (tail = list; XCDR (tail) != rest; tail = XCDR (tail))
1824 XSETCDR (tail, Qnil);
1825 list = nconc2 (rest, list);
1827 return list;
1832 /* Look at all windows, performing an operation specified by TYPE
1833 with argument OBJ.
1834 If FRAMES is Qt, look at all frames;
1835 Qnil, look at just the selected frame;
1836 Qvisible, look at visible frames;
1837 a frame, just look at windows on that frame.
1838 If MINI is non-zero, perform the operation on minibuffer windows too. */
1840 enum window_loop
1842 WINDOW_LOOP_UNUSED,
1843 GET_BUFFER_WINDOW, /* Arg is buffer */
1844 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1845 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1846 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1847 GET_LARGEST_WINDOW,
1848 UNSHOW_BUFFER, /* Arg is buffer */
1849 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
1850 CHECK_ALL_WINDOWS
1853 static Lisp_Object
1854 window_loop (type, obj, mini, frames)
1855 enum window_loop type;
1856 Lisp_Object obj, frames;
1857 int mini;
1859 Lisp_Object window, windows, best_window, frame_arg;
1860 struct frame *f;
1861 struct gcpro gcpro1;
1863 /* If we're only looping through windows on a particular frame,
1864 frame points to that frame. If we're looping through windows
1865 on all frames, frame is 0. */
1866 if (FRAMEP (frames))
1867 f = XFRAME (frames);
1868 else if (NILP (frames))
1869 f = SELECTED_FRAME ();
1870 else
1871 f = NULL;
1873 if (f)
1874 frame_arg = Qlambda;
1875 else if (XFASTINT (frames) == 0)
1876 frame_arg = frames;
1877 else if (EQ (frames, Qvisible))
1878 frame_arg = frames;
1879 else
1880 frame_arg = Qt;
1882 /* frame_arg is Qlambda to stick to one frame,
1883 Qvisible to consider all visible frames,
1884 or Qt otherwise. */
1886 /* Pick a window to start with. */
1887 if (WINDOWP (obj))
1888 window = obj;
1889 else if (f)
1890 window = FRAME_SELECTED_WINDOW (f);
1891 else
1892 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
1894 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
1895 GCPRO1 (windows);
1896 best_window = Qnil;
1898 for (; CONSP (windows); windows = CDR (windows))
1900 struct window *w;
1902 window = XCAR (windows);
1903 w = XWINDOW (window);
1905 /* Note that we do not pay attention here to whether the frame
1906 is visible, since Fwindow_list skips non-visible frames if
1907 that is desired, under the control of frame_arg. */
1908 if (!MINI_WINDOW_P (w)
1909 /* For UNSHOW_BUFFER, we must always consider all windows. */
1910 || type == UNSHOW_BUFFER
1911 || (mini && minibuf_level > 0))
1912 switch (type)
1914 case GET_BUFFER_WINDOW:
1915 if (EQ (w->buffer, obj)
1916 /* Don't find any minibuffer window
1917 except the one that is currently in use. */
1918 && (MINI_WINDOW_P (w)
1919 ? EQ (window, minibuf_window)
1920 : 1))
1922 if (NILP (best_window))
1923 best_window = window;
1924 else if (EQ (window, selected_window))
1925 /* For compatibility with 20.x, prefer to return
1926 selected-window. */
1927 best_window = window;
1929 break;
1931 case GET_LRU_WINDOW:
1932 /* t as arg means consider only full-width windows */
1933 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (w))
1934 break;
1935 /* Ignore dedicated windows and minibuffers. */
1936 if (MINI_WINDOW_P (w) || EQ (w->dedicated, Qt))
1937 break;
1938 if (NILP (best_window)
1939 || (XFASTINT (XWINDOW (best_window)->use_time)
1940 > XFASTINT (w->use_time)))
1941 best_window = window;
1942 break;
1944 case DELETE_OTHER_WINDOWS:
1945 if (!EQ (window, obj))
1946 Fdelete_window (window);
1947 break;
1949 case DELETE_BUFFER_WINDOWS:
1950 if (EQ (w->buffer, obj))
1952 struct frame *f = XFRAME (WINDOW_FRAME (w));
1954 /* If this window is dedicated, and in a frame of its own,
1955 kill the frame. */
1956 if (EQ (window, FRAME_ROOT_WINDOW (f))
1957 && !NILP (w->dedicated)
1958 && other_visible_frames (f))
1960 /* Skip the other windows on this frame.
1961 There might be one, the minibuffer! */
1962 while (CONSP (XCDR (windows))
1963 && EQ (XWINDOW (XCAR (windows))->frame,
1964 XWINDOW (XCAR (XCDR (windows)))->frame))
1965 windows = XCDR (windows);
1967 /* Now we can safely delete the frame. */
1968 Fdelete_frame (w->frame, Qnil);
1970 else if (NILP (w->parent))
1972 /* If we're deleting the buffer displayed in the
1973 only window on the frame, find a new buffer to
1974 display there. */
1975 Lisp_Object buffer;
1976 buffer = Fother_buffer (obj, Qnil, w->frame);
1977 Fset_window_buffer (window, buffer, Qnil);
1978 if (EQ (window, selected_window))
1979 Fset_buffer (w->buffer);
1981 else
1982 Fdelete_window (window);
1984 break;
1986 case GET_LARGEST_WINDOW:
1988 /* Ignore dedicated windows and minibuffers. */
1989 if (MINI_WINDOW_P (w) || EQ (w->dedicated, Qt))
1990 break;
1992 if (NILP (best_window))
1993 best_window = window;
1994 else
1996 struct window *b = XWINDOW (best_window);
1997 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
1998 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
1999 best_window = window;
2002 break;
2004 case UNSHOW_BUFFER:
2005 if (EQ (w->buffer, obj))
2007 Lisp_Object buffer;
2008 struct frame *f = XFRAME (w->frame);
2010 /* Find another buffer to show in this window. */
2011 buffer = Fother_buffer (obj, Qnil, w->frame);
2013 /* If this window is dedicated, and in a frame of its own,
2014 kill the frame. */
2015 if (EQ (window, FRAME_ROOT_WINDOW (f))
2016 && !NILP (w->dedicated)
2017 && other_visible_frames (f))
2019 /* Skip the other windows on this frame.
2020 There might be one, the minibuffer! */
2021 while (CONSP (XCDR (windows))
2022 && EQ (XWINDOW (XCAR (windows))->frame,
2023 XWINDOW (XCAR (XCDR (windows)))->frame))
2024 windows = XCDR (windows);
2026 /* Now we can safely delete the frame. */
2027 Fdelete_frame (w->frame, Qnil);
2029 else if (!NILP (w->dedicated) && !NILP (w->parent))
2031 Lisp_Object window;
2032 XSETWINDOW (window, w);
2033 /* If this window is dedicated and not the only window
2034 in its frame, then kill it. */
2035 Fdelete_window (window);
2037 else
2039 /* Otherwise show a different buffer in the window. */
2040 w->dedicated = Qnil;
2041 Fset_window_buffer (window, buffer, Qnil);
2042 if (EQ (window, selected_window))
2043 Fset_buffer (w->buffer);
2046 break;
2048 case REDISPLAY_BUFFER_WINDOWS:
2049 if (EQ (w->buffer, obj))
2051 mark_window_display_accurate (window, 0);
2052 w->update_mode_line = Qt;
2053 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2054 ++update_mode_lines;
2055 best_window = window;
2057 break;
2059 /* Check for a window that has a killed buffer. */
2060 case CHECK_ALL_WINDOWS:
2061 if (! NILP (w->buffer)
2062 && NILP (XBUFFER (w->buffer)->name))
2063 abort ();
2064 break;
2066 case WINDOW_LOOP_UNUSED:
2067 break;
2071 UNGCPRO;
2072 return best_window;
2075 /* Used for debugging. Abort if any window has a dead buffer. */
2077 void
2078 check_all_windows ()
2080 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2083 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
2084 doc: /* Return the window least recently selected or used for display.
2085 Return a full-width window if possible.
2086 A minibuffer window is never a candidate.
2087 If optional argument FRAME is `visible', search all visible frames.
2088 If FRAME is 0, search all visible and iconified frames.
2089 If FRAME is t, search all frames.
2090 If FRAME is nil, search only the selected frame.
2091 If FRAME is a frame, search only that frame. */)
2092 (frame)
2093 Lisp_Object frame;
2095 register Lisp_Object w;
2096 /* First try for a window that is full-width */
2097 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
2098 if (!NILP (w) && !EQ (w, selected_window))
2099 return w;
2100 /* If none of them, try the rest */
2101 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
2104 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
2105 doc: /* Return the largest window in area.
2106 A minibuffer window is never a candidate.
2107 If optional argument FRAME is `visible', search all visible frames.
2108 If FRAME is 0, search all visible and iconified frames.
2109 If FRAME is t, search all frames.
2110 If FRAME is nil, search only the selected frame.
2111 If FRAME is a frame, search only that frame. */)
2112 (frame)
2113 Lisp_Object frame;
2115 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
2116 frame);
2119 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
2120 doc: /* Return a window currently displaying BUFFER, or nil if none.
2121 BUFFER can be a buffer or a buffer name.
2122 If optional argument FRAME is `visible', search all visible frames.
2123 If optional argument FRAME is 0, search all visible and iconified frames.
2124 If FRAME is t, search all frames.
2125 If FRAME is nil, search only the selected frame.
2126 If FRAME is a frame, search only that frame. */)
2127 (buffer, frame)
2128 Lisp_Object buffer, frame;
2130 buffer = Fget_buffer (buffer);
2131 if (BUFFERP (buffer))
2132 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2133 else
2134 return Qnil;
2137 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2138 0, 1, "",
2139 doc: /* Make WINDOW (or the selected window) fill its frame.
2140 Only the frame WINDOW is on is affected.
2141 This function tries to reduce display jumps
2142 by keeping the text previously visible in WINDOW
2143 in the same place on the frame. Doing this depends on
2144 the value of (window-start WINDOW), so if calling this function
2145 in a program gives strange scrolling, make sure the window-start
2146 value is reasonable when this function is called. */)
2147 (window)
2148 Lisp_Object window;
2150 struct window *w;
2151 int startpos;
2152 int top, new_top;
2154 if (NILP (window))
2155 window = selected_window;
2156 else
2157 CHECK_LIVE_WINDOW (window);
2158 w = XWINDOW (window);
2160 startpos = marker_position (w->start);
2161 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2163 if (MINI_WINDOW_P (w) && top > 0)
2164 error ("Can't expand minibuffer to full frame");
2166 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2168 /* Try to minimize scrolling, by setting the window start to the point
2169 will cause the text at the old window start to be at the same place
2170 on the frame. But don't try to do this if the window start is
2171 outside the visible portion (as might happen when the display is
2172 not current, due to typeahead). */
2173 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2174 if (new_top != top
2175 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2176 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2178 struct position pos;
2179 struct buffer *obuf = current_buffer;
2181 Fset_buffer (w->buffer);
2182 /* This computation used to temporarily move point, but that can
2183 have unwanted side effects due to text properties. */
2184 pos = *vmotion (startpos, -top, w);
2186 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2187 w->window_end_valid = Qnil;
2188 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2189 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2190 : Qnil);
2191 /* We need to do this, so that the window-scroll-functions
2192 get called. */
2193 w->optional_new_start = Qt;
2195 set_buffer_internal (obuf);
2198 return Qnil;
2201 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2202 1, 2, "bDelete windows on (buffer): ",
2203 doc: /* Delete all windows showing BUFFER.
2204 BUFFER must be a buffer or the name of an existing buffer.
2205 Optional second argument FRAME controls which frames are affected.
2206 If optional argument FRAME is `visible', search all visible frames.
2207 If FRAME is 0, search all visible and iconified frames.
2208 If FRAME is nil, search all frames.
2209 If FRAME is t, search only the selected frame.
2210 If FRAME is a frame, search only that frame. */)
2211 (buffer, frame)
2212 Lisp_Object buffer, frame;
2214 /* FRAME uses t and nil to mean the opposite of what window_loop
2215 expects. */
2216 if (NILP (frame))
2217 frame = Qt;
2218 else if (EQ (frame, Qt))
2219 frame = Qnil;
2221 if (!NILP (buffer))
2223 buffer = Fget_buffer (buffer);
2224 CHECK_BUFFER (buffer);
2225 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2228 return Qnil;
2231 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2232 Sreplace_buffer_in_windows,
2233 1, 1, "bReplace buffer in windows: ",
2234 doc: /* Replace BUFFER with some other buffer in all windows showing it.
2235 BUFFER may be a buffer or the name of an existing buffer. */)
2236 (buffer)
2237 Lisp_Object buffer;
2239 if (!NILP (buffer))
2241 buffer = Fget_buffer (buffer);
2242 CHECK_BUFFER (buffer);
2243 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2245 return Qnil;
2248 /* Replace BUFFER with some other buffer in all windows
2249 of all frames, even those on other keyboards. */
2251 void
2252 replace_buffer_in_all_windows (buffer)
2253 Lisp_Object buffer;
2255 #ifdef MULTI_KBOARD
2256 Lisp_Object tail, frame;
2258 /* A single call to window_loop won't do the job
2259 because it only considers frames on the current keyboard.
2260 So loop manually over frames, and handle each one. */
2261 FOR_EACH_FRAME (tail, frame)
2262 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2263 #else
2264 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
2265 #endif
2268 /* Set the height of WINDOW and all its inferiors. */
2270 /* The smallest acceptable dimensions for a window. Anything smaller
2271 might crash Emacs. */
2273 #define MIN_SAFE_WINDOW_WIDTH (2)
2274 #define MIN_SAFE_WINDOW_HEIGHT (1)
2276 /* Make sure that window_min_height and window_min_width are
2277 not too small; if they are, set them to safe minima. */
2279 static void
2280 check_min_window_sizes ()
2282 /* Smaller values might permit a crash. */
2283 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
2284 window_min_width = MIN_SAFE_WINDOW_WIDTH;
2285 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
2286 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
2289 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2290 minimum allowable size. */
2292 void
2293 check_frame_size (frame, rows, cols)
2294 FRAME_PTR frame;
2295 int *rows, *cols;
2297 /* For height, we have to see:
2298 how many windows the frame has at minimum (one or two),
2299 and whether it has a menu bar or other special stuff at the top. */
2300 int min_height
2301 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2302 ? MIN_SAFE_WINDOW_HEIGHT
2303 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2305 if (FRAME_TOP_MARGIN (frame) > 0)
2306 min_height += FRAME_TOP_MARGIN (frame);
2308 if (*rows < min_height)
2309 *rows = min_height;
2310 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2311 *cols = MIN_SAFE_WINDOW_WIDTH;
2315 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2316 check if W's width can be changed, otherwise check W's height.
2317 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2318 siblings, too. If none of the siblings is resizable, WINDOW isn't
2319 either. */
2321 static int
2322 window_fixed_size_p (w, width_p, check_siblings_p)
2323 struct window *w;
2324 int width_p, check_siblings_p;
2326 int fixed_p;
2327 struct window *c;
2329 if (!NILP (w->hchild))
2331 c = XWINDOW (w->hchild);
2333 if (width_p)
2335 /* A horiz. combination is fixed-width if all of if its
2336 children are. */
2337 while (c && window_fixed_size_p (c, width_p, 0))
2338 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2339 fixed_p = c == NULL;
2341 else
2343 /* A horiz. combination is fixed-height if one of if its
2344 children is. */
2345 while (c && !window_fixed_size_p (c, width_p, 0))
2346 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2347 fixed_p = c != NULL;
2350 else if (!NILP (w->vchild))
2352 c = XWINDOW (w->vchild);
2354 if (width_p)
2356 /* A vert. combination is fixed-width if one of if its
2357 children is. */
2358 while (c && !window_fixed_size_p (c, width_p, 0))
2359 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2360 fixed_p = c != NULL;
2362 else
2364 /* A vert. combination is fixed-height if all of if its
2365 children are. */
2366 while (c && window_fixed_size_p (c, width_p, 0))
2367 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2368 fixed_p = c == NULL;
2371 else if (BUFFERP (w->buffer))
2373 if (w->height_fixed_p && !width_p)
2374 fixed_p = 1;
2375 else
2377 struct buffer *old = current_buffer;
2378 Lisp_Object val;
2380 current_buffer = XBUFFER (w->buffer);
2381 val = find_symbol_value (Qwindow_size_fixed);
2382 current_buffer = old;
2384 fixed_p = 0;
2385 if (!EQ (val, Qunbound))
2387 fixed_p = !NILP (val);
2389 if (fixed_p
2390 && ((EQ (val, Qheight) && width_p)
2391 || (EQ (val, Qwidth) && !width_p)))
2392 fixed_p = 0;
2396 /* Can't tell if this one is resizable without looking at
2397 siblings. If all siblings are fixed-size this one is too. */
2398 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2400 Lisp_Object child;
2402 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2403 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2404 break;
2406 if (NILP (child))
2407 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2408 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2409 break;
2411 if (NILP (child))
2412 fixed_p = 1;
2415 else
2416 fixed_p = 1;
2418 return fixed_p;
2422 /* Return the minimum size of window W, not taking fixed-width windows
2423 into account. WIDTH_P non-zero means return the minimum width,
2424 otherwise return the minimum height. If W is a combination window,
2425 compute the minimum size from the minimum sizes of W's children. */
2427 static int
2428 window_min_size_1 (w, width_p)
2429 struct window *w;
2430 int width_p;
2432 struct window *c;
2433 int size;
2435 if (!NILP (w->hchild))
2437 c = XWINDOW (w->hchild);
2438 size = 0;
2440 if (width_p)
2442 /* The min width of a horizontal combination is
2443 the sum of the min widths of its children. */
2444 while (c)
2446 size += window_min_size_1 (c, width_p);
2447 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2450 else
2452 /* The min height a horizontal combination equals
2453 the maximum of all min height of its children. */
2454 while (c)
2456 int min_size = window_min_size_1 (c, width_p);
2457 size = max (min_size, size);
2458 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2462 else if (!NILP (w->vchild))
2464 c = XWINDOW (w->vchild);
2465 size = 0;
2467 if (width_p)
2469 /* The min width of a vertical combination is
2470 the maximum of the min widths of its children. */
2471 while (c)
2473 int min_size = window_min_size_1 (c, width_p);
2474 size = max (min_size, size);
2475 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2478 else
2480 /* The min height of a vertical combination equals
2481 the sum of the min height of its children. */
2482 while (c)
2484 size += window_min_size_1 (c, width_p);
2485 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2489 else
2491 if (width_p)
2492 size = window_min_width;
2493 else
2495 if (MINI_WINDOW_P (w)
2496 || (!WINDOW_WANTS_MODELINE_P (w)
2497 && !WINDOW_WANTS_HEADER_LINE_P (w)))
2498 size = 1;
2499 else
2500 size = window_min_height;
2504 return size;
2508 /* Return the minimum size of window W, taking fixed-size windows into
2509 account. WIDTH_P non-zero means return the minimum width,
2510 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2511 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2512 unless FIXED is null. */
2514 static int
2515 window_min_size (w, width_p, ignore_fixed_p, fixed)
2516 struct window *w;
2517 int width_p, ignore_fixed_p, *fixed;
2519 int size, fixed_p;
2521 if (ignore_fixed_p)
2522 fixed_p = 0;
2523 else
2524 fixed_p = window_fixed_size_p (w, width_p, 1);
2526 if (fixed)
2527 *fixed = fixed_p;
2529 if (fixed_p)
2530 size = width_p ? XFASTINT (w->total_cols) : XFASTINT (w->total_lines);
2531 else
2532 size = window_min_size_1 (w, width_p);
2534 return size;
2538 /* Adjust the margins of window W if text area is too small.
2539 Return 1 if window width is ok after adjustment; 0 if window
2540 is still too narrow. */
2542 static int
2543 adjust_window_margins (w)
2544 struct window *w;
2546 int box_cols = (WINDOW_TOTAL_COLS (w)
2547 - WINDOW_FRINGE_COLS (w)
2548 - WINDOW_SCROLL_BAR_COLS (w));
2549 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2550 + WINDOW_RIGHT_MARGIN_COLS (w));
2552 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2553 return 1;
2555 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2556 return 0;
2558 /* Window's text area is too narrow, but reducing the window
2559 margins will fix that. */
2560 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2561 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2563 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2564 w->left_margin_cols = w->right_margin_cols
2565 = make_number (margin_cols/2);
2566 else
2567 w->right_margin_cols = make_number (margin_cols);
2569 else
2570 w->left_margin_cols = make_number (margin_cols);
2571 return 1;
2574 /* Calculate new sizes for windows in the list FORWARD when the window size
2575 goes from TOTAL to SIZE. TOTAL must be greater than SIZE.
2576 The number of windows in FORWARD is NCHILDREN, and the number that
2577 can shrink is SHRINKABLE.
2578 The minimum size a window can have is MIN_SIZE.
2579 If we are shrinking fixed windows, RESIZE_FIXED_P is non-zero.
2580 If we are shrinking columns, WIDTH_P is non-zero, otherwise we are
2581 shrinking rows.
2583 This function returns an allocated array of new sizes that the caller
2584 must free. The size -1 means the window is fixed and RESIZE_FIXED_P
2585 is zero. Array index 0 refers to the first window in FORWARD, 1 to
2586 the second, and so on.
2588 This function tries to keep windows at least at the minimum size
2589 and resize other windows before it resizes any window to zero (i.e.
2590 delete that window).
2592 Windows are resized proportional to their size, so bigger windows
2593 shrink more than smaller windows. */
2594 static int *
2595 shrink_windows (total, size, nchildren, shrinkable,
2596 min_size, resize_fixed_p, forward, width_p)
2597 int total, size, nchildren, shrinkable, min_size;
2598 int resize_fixed_p, width_p;
2599 Lisp_Object forward;
2601 int available_resize = 0;
2602 int *new_sizes;
2603 struct window *c;
2604 Lisp_Object child;
2605 int smallest = total;
2606 int total_removed = 0;
2607 int total_shrink = total - size;
2608 int i;
2610 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2612 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2614 int child_size;
2616 c = XWINDOW (child);
2617 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2619 if (! resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2620 new_sizes[i] = -1;
2621 else
2623 new_sizes[i] = child_size;
2624 if (child_size > min_size)
2625 available_resize += child_size - min_size;
2628 /* We might need to shrink some windows to zero. Find the smallest
2629 windows and set them to 0 until we can fulfil the new size. */
2631 while (shrinkable > 1 && size + available_resize < total)
2633 for (i = 0; i < nchildren; ++i)
2634 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2635 smallest = new_sizes[i];
2637 for (i = 0; i < nchildren; ++i)
2638 if (new_sizes[i] == smallest)
2640 /* Resize this window down to zero. */
2641 new_sizes[i] = 0;
2642 if (smallest > min_size)
2643 available_resize -= smallest - min_size;
2644 available_resize += smallest;
2645 --shrinkable;
2646 total_removed += smallest;
2648 /* Out of for, just remove one window at the time and
2649 check again if we have enough space. */
2650 break;
2654 /* Now, calculate the new sizes. Try to shrink each window
2655 proportional to its size. */
2656 for (i = 0; i < nchildren; ++i)
2658 if (new_sizes[i] > min_size)
2660 int to_shrink = total_shrink*new_sizes[i]/total;
2661 if (new_sizes[i] - to_shrink < min_size)
2662 to_shrink = new_sizes[i] - min_size;
2663 new_sizes[i] -= to_shrink;
2664 total_removed += to_shrink;
2668 /* Any reminder due to rounding, we just subtract from windows
2669 that are left and still can be shrunk. */
2670 while (total_shrink > total_removed)
2672 for (i = 0; i < nchildren; ++i)
2673 if (new_sizes[i] > min_size)
2675 --new_sizes[i];
2676 ++total_removed;
2678 /* Out of for, just shrink one window at the time and
2679 check again if we have enough space. */
2680 break;
2684 return new_sizes;
2687 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2688 WINDOW's width. Resize WINDOW's children, if any, so that they
2689 keep their proportionate size relative to WINDOW. Propagate
2690 WINDOW's top or left edge position to children. Delete windows
2691 that become too small unless NODELETE_P is non-zero.
2693 If NODELETE_P is 2, that means we do delete windows that are
2694 too small, even if they were too small before! */
2696 static void
2697 size_window (window, size, width_p, nodelete_p)
2698 Lisp_Object window;
2699 int size, width_p, nodelete_p;
2701 struct window *w = XWINDOW (window);
2702 struct window *c;
2703 Lisp_Object child, *forward, *sideward;
2704 int old_size, min_size, safe_min_size;
2706 /* We test nodelete_p != 2 and nodelete_p != 1 below, so it
2707 seems like it's too soon to do this here. ++KFS. */
2708 if (nodelete_p == 2)
2709 nodelete_p = 0;
2711 check_min_window_sizes ();
2712 size = max (0, size);
2714 /* If the window has been "too small" at one point,
2715 don't delete it for being "too small" in the future.
2716 Preserve it as long as that is at all possible. */
2717 if (width_p)
2719 old_size = WINDOW_TOTAL_COLS (w);
2720 min_size = window_min_width;
2721 /* Ensure that there is room for the scroll bar and fringes!
2722 We may reduce display margins though. */
2723 safe_min_size = (MIN_SAFE_WINDOW_WIDTH
2724 + WINDOW_FRINGE_COLS (w)
2725 + WINDOW_SCROLL_BAR_COLS (w));
2727 else
2729 old_size = XINT (w->total_lines);
2730 min_size = window_min_height;
2731 safe_min_size = MIN_SAFE_WINDOW_HEIGHT;
2734 if (old_size < min_size && nodelete_p != 2)
2735 w->too_small_ok = Qt;
2737 /* Maybe delete WINDOW if it's too small. */
2738 if (nodelete_p != 1 && !NILP (w->parent))
2740 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
2741 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
2742 if (min_size < safe_min_size)
2743 min_size = safe_min_size;
2744 if (size < min_size)
2746 delete_window (window);
2747 return;
2751 /* Set redisplay hints. */
2752 w->last_modified = make_number (0);
2753 w->last_overlay_modified = make_number (0);
2754 windows_or_buffers_changed++;
2755 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
2757 if (width_p)
2759 sideward = &w->vchild;
2760 forward = &w->hchild;
2761 w->total_cols = make_number (size);
2762 adjust_window_margins (w);
2764 else
2766 sideward = &w->hchild;
2767 forward = &w->vchild;
2768 w->total_lines = make_number (size);
2769 w->orig_total_lines = Qnil;
2772 if (!NILP (*sideward))
2774 for (child = *sideward; !NILP (child); child = c->next)
2776 c = XWINDOW (child);
2777 if (width_p)
2778 c->left_col = w->left_col;
2779 else
2780 c->top_line = w->top_line;
2781 size_window (child, size, width_p, nodelete_p);
2784 else if (!NILP (*forward))
2786 int fixed_size, each, extra, n;
2787 int resize_fixed_p, nfixed;
2788 int last_pos, first_pos, nchildren, total;
2789 int *new_sizes = NULL;
2791 /* Determine the fixed-size portion of the this window, and the
2792 number of child windows. */
2793 fixed_size = nchildren = nfixed = total = 0;
2794 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
2796 int child_size;
2798 c = XWINDOW (child);
2799 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2800 total += child_size;
2802 if (window_fixed_size_p (c, width_p, 0))
2804 fixed_size += child_size;
2805 ++nfixed;
2809 /* If the new size is smaller than fixed_size, or if there
2810 aren't any resizable windows, allow resizing fixed-size
2811 windows. */
2812 resize_fixed_p = nfixed == nchildren || size < fixed_size;
2814 /* Compute how many lines/columns to add/remove to each child. The
2815 value of extra takes care of rounding errors. */
2816 n = resize_fixed_p ? nchildren : nchildren - nfixed;
2817 if (size < total && n > 1)
2818 new_sizes = shrink_windows (total, size, nchildren, n, min_size,
2819 resize_fixed_p, *forward, width_p);
2820 else
2822 each = (size - total) / n;
2823 extra = (size - total) - n * each;
2826 /* Compute new children heights and edge positions. */
2827 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
2828 last_pos = first_pos;
2829 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
2831 int new_size, old_size;
2833 c = XWINDOW (child);
2834 old_size = width_p ? XFASTINT (c->total_cols) : XFASTINT (c->total_lines);
2835 new_size = old_size;
2837 /* The top or left edge position of this child equals the
2838 bottom or right edge of its predecessor. */
2839 if (width_p)
2840 c->left_col = make_number (last_pos);
2841 else
2842 c->top_line = make_number (last_pos);
2844 /* If this child can be resized, do it. */
2845 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
2847 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
2848 extra = 0;
2851 /* Set new height. Note that size_window also propagates
2852 edge positions to children, so it's not a no-op if we
2853 didn't change the child's size. */
2854 size_window (child, new_size, width_p, 1);
2856 /* Remember the bottom/right edge position of this child; it
2857 will be used to set the top/left edge of the next child. */
2858 last_pos += new_size;
2861 if (new_sizes) xfree (new_sizes);
2863 /* We should have covered the parent exactly with child windows. */
2864 xassert (size == last_pos - first_pos);
2866 /* Now delete any children that became too small. */
2867 if (!nodelete_p)
2868 for (child = *forward; !NILP (child); child = c->next)
2870 int child_size;
2871 c = XWINDOW (child);
2872 child_size = width_p ? XINT (c->total_cols) : XINT (c->total_lines);
2873 size_window (child, child_size, width_p, 2);
2878 /* Set WINDOW's height to HEIGHT, and recursively change the height 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_height (window, height, nodelete)
2885 Lisp_Object window;
2886 int height;
2887 int nodelete;
2889 size_window (window, height, 0, nodelete);
2893 /* Set WINDOW's width to WIDTH, and recursively change the width of
2894 WINDOW's children. NODELETE non-zero means don't delete windows
2895 that become too small in the process. (The caller should check
2896 later and do so if appropriate.) */
2898 void
2899 set_window_width (window, width, nodelete)
2900 Lisp_Object window;
2901 int width;
2902 int nodelete;
2904 size_window (window, width, 1, nodelete);
2907 /* Change window heights in windows rooted in WINDOW by N lines. */
2909 void
2910 change_window_heights (window, n)
2911 Lisp_Object window;
2912 int n;
2914 struct window *w = XWINDOW (window);
2916 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
2917 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
2919 if (INTEGERP (w->orig_top_line))
2920 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
2921 if (INTEGERP (w->orig_total_lines))
2922 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
2924 /* Handle just the top child in a vertical split. */
2925 if (!NILP (w->vchild))
2926 change_window_heights (w->vchild, n);
2928 /* Adjust all children in a horizontal split. */
2929 for (window = w->hchild; !NILP (window); window = w->next)
2931 w = XWINDOW (window);
2932 change_window_heights (window, n);
2937 int window_select_count;
2939 Lisp_Object
2940 Fset_window_buffer_unwind (obuf)
2941 Lisp_Object obuf;
2943 Fset_buffer (obuf);
2944 return Qnil;
2947 EXFUN (Fset_window_fringes, 4);
2948 EXFUN (Fset_window_scroll_bars, 4);
2950 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2951 means it's allowed to run hooks. See make_frame for a case where
2952 it's not allowed. KEEP_MARGINS_P non-zero means that the current
2953 margins, fringes, and scroll-bar settings of the window are not
2954 reset from the buffer's local settings. */
2956 void
2957 set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
2958 Lisp_Object window, buffer;
2959 int run_hooks_p, keep_margins_p;
2961 struct window *w = XWINDOW (window);
2962 struct buffer *b = XBUFFER (buffer);
2963 int count = SPECPDL_INDEX ();
2965 w->buffer = buffer;
2967 if (EQ (window, selected_window))
2968 b->last_selected_window = window;
2970 /* Update time stamps of buffer display. */
2971 if (INTEGERP (b->display_count))
2972 XSETINT (b->display_count, XINT (b->display_count) + 1);
2973 b->display_time = Fcurrent_time ();
2975 XSETFASTINT (w->window_end_pos, 0);
2976 XSETFASTINT (w->window_end_vpos, 0);
2977 bzero (&w->last_cursor, sizeof w->last_cursor);
2978 w->window_end_valid = Qnil;
2979 w->hscroll = w->min_hscroll = make_number (0);
2980 w->vscroll = 0;
2981 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
2982 set_marker_restricted (w->start,
2983 make_number (b->last_window_start),
2984 buffer);
2985 w->start_at_line_beg = Qnil;
2986 w->force_start = Qnil;
2987 XSETFASTINT (w->last_modified, 0);
2988 XSETFASTINT (w->last_overlay_modified, 0);
2989 windows_or_buffers_changed++;
2991 /* We must select BUFFER for running the window-scroll-functions.
2992 If WINDOW is selected, switch permanently.
2993 Otherwise, switch but go back to the ambient buffer afterward. */
2994 if (EQ (window, selected_window))
2995 Fset_buffer (buffer);
2996 /* We can't check ! NILP (Vwindow_scroll_functions) here
2997 because that might itself be a local variable. */
2998 else if (window_initialized)
3000 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
3001 Fset_buffer (buffer);
3004 if (!keep_margins_p)
3006 /* Set left and right marginal area width etc. from buffer. */
3008 /* This may call adjust_window_margins three times, so
3009 temporarily disable window margins. */
3010 Lisp_Object save_left = w->left_margin_cols;
3011 Lisp_Object save_right = w->right_margin_cols;
3013 w->left_margin_cols = w->right_margin_cols = Qnil;
3015 Fset_window_fringes (window,
3016 b->left_fringe_width, b->right_fringe_width,
3017 b->fringes_outside_margins);
3019 Fset_window_scroll_bars (window,
3020 b->scroll_bar_width,
3021 b->vertical_scroll_bar_type, Qnil);
3023 w->left_margin_cols = save_left;
3024 w->right_margin_cols = save_right;
3026 Fset_window_margins (window,
3027 b->left_margin_cols, b->right_margin_cols);
3030 if (run_hooks_p)
3032 if (! NILP (Vwindow_scroll_functions))
3033 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3034 Fmarker_position (w->start));
3036 if (! NILP (Vwindow_configuration_change_hook)
3037 && ! NILP (Vrun_hooks))
3038 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3041 unbind_to (count, Qnil);
3045 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3046 doc: /* Make WINDOW display BUFFER as its contents.
3047 BUFFER can be a buffer or the name of an existing buffer.
3048 Optional third arg KEEP-MARGINS non-nil means that WINDOW's current
3049 display margins, fringe widths, and scroll bar settings are maintained;
3050 the default is to reset these from BUFFER's local settings or the frame
3051 defaults. */)
3052 (window, buffer, keep_margins)
3053 register Lisp_Object window, buffer, keep_margins;
3055 register Lisp_Object tem;
3056 register struct window *w = decode_window (window);
3058 XSETWINDOW (window, w);
3059 buffer = Fget_buffer (buffer);
3060 CHECK_BUFFER (buffer);
3062 if (NILP (XBUFFER (buffer)->name))
3063 error ("Attempt to display deleted buffer");
3065 tem = w->buffer;
3066 if (NILP (tem))
3067 error ("Window is deleted");
3068 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
3069 is first being set up. */
3071 if (!NILP (w->dedicated) && !EQ (tem, buffer))
3072 error ("Window is dedicated to `%s'",
3073 SDATA (XBUFFER (tem)->name));
3075 unshow_buffer (w);
3078 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3079 return Qnil;
3082 /* Note that selected_window can be nil
3083 when this is called from Fset_window_configuration. */
3085 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3086 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3087 If WINDOW is not already selected, make WINDOW's buffer current
3088 and make WINDOW the frame's selected window. Return WINDOW.
3089 Optional second arg NORECORD non-nil means
3090 do not put this buffer at the front of the list of recently selected ones.
3092 Note that the main editor command loop
3093 selects the buffer of the selected window before each command. */)
3094 (window, norecord)
3095 register Lisp_Object window, norecord;
3097 register struct window *w;
3098 register struct window *ow;
3099 struct frame *sf;
3101 CHECK_LIVE_WINDOW (window);
3103 w = XWINDOW (window);
3104 w->frozen_window_start_p = 0;
3106 XSETFASTINT (w->use_time, ++window_select_count);
3107 if (EQ (window, selected_window))
3108 return window;
3110 if (!NILP (selected_window))
3112 ow = XWINDOW (selected_window);
3113 if (! NILP (ow->buffer))
3114 set_marker_both (ow->pointm, ow->buffer,
3115 BUF_PT (XBUFFER (ow->buffer)),
3116 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3119 selected_window = window;
3120 sf = SELECTED_FRAME ();
3121 if (XFRAME (WINDOW_FRAME (w)) != sf)
3123 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3124 /* Use this rather than Fhandle_switch_frame
3125 so that FRAME_FOCUS_FRAME is moved appropriately as we
3126 move around in the state where a minibuffer in a separate
3127 frame is active. */
3128 Fselect_frame (WINDOW_FRAME (w), Qnil);
3130 else
3131 sf->selected_window = window;
3133 if (NILP (norecord))
3134 record_buffer (w->buffer);
3135 Fset_buffer (w->buffer);
3137 XBUFFER (w->buffer)->last_selected_window = window;
3139 /* Go to the point recorded in the window.
3140 This is important when the buffer is in more
3141 than one window. It also matters when
3142 redisplay_window has altered point after scrolling,
3143 because it makes the change only in the window. */
3145 register int new_point = marker_position (w->pointm);
3146 if (new_point < BEGV)
3147 SET_PT (BEGV);
3148 else if (new_point > ZV)
3149 SET_PT (ZV);
3150 else
3151 SET_PT (new_point);
3154 windows_or_buffers_changed++;
3155 return window;
3158 static Lisp_Object
3159 select_window_norecord (window)
3160 Lisp_Object window;
3162 return Fselect_window (window, Qt);
3165 /* Deiconify the frame containing the window WINDOW,
3166 unless it is the selected frame;
3167 then return WINDOW.
3169 The reason for the exception for the selected frame
3170 is that it seems better not to change the selected frames visibility
3171 merely because of displaying a different buffer in it.
3172 The deiconification is useful when a buffer gets shown in
3173 another frame that you were not using lately. */
3175 static Lisp_Object
3176 display_buffer_1 (window)
3177 Lisp_Object window;
3179 Lisp_Object frame = XWINDOW (window)->frame;
3180 FRAME_PTR f = XFRAME (frame);
3182 FRAME_SAMPLE_VISIBILITY (f);
3184 if (EQ (frame, selected_frame))
3185 ; /* Assume the selected frame is already visible enough. */
3186 else if (minibuf_level > 0
3187 && MINI_WINDOW_P (XWINDOW (selected_window))
3188 && WINDOW_LIVE_P (minibuf_selected_window)
3189 && EQ (frame, WINDOW_FRAME (XWINDOW (minibuf_selected_window))))
3190 ; /* Assume the frame from which we invoked the minibuffer is visible. */
3191 else
3193 if (FRAME_ICONIFIED_P (f))
3194 Fmake_frame_visible (frame);
3195 else if (FRAME_VISIBLE_P (f))
3196 Fraise_frame (frame);
3199 return window;
3202 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
3203 doc: /* Returns non-nil if a buffer named BUFFER-NAME would be created specially.
3204 The value is actually t if the frame should be called with default frame
3205 parameters, and a list of frame parameters if they were specified.
3206 See `special-display-buffer-names', and `special-display-regexps'. */)
3207 (buffer_name)
3208 Lisp_Object buffer_name;
3210 Lisp_Object tem;
3212 CHECK_STRING (buffer_name);
3214 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
3215 if (!NILP (tem))
3216 return Qt;
3218 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
3219 if (!NILP (tem))
3220 return XCDR (tem);
3222 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
3224 Lisp_Object car = XCAR (tem);
3225 if (STRINGP (car)
3226 && fast_string_match (car, buffer_name) >= 0)
3227 return Qt;
3228 else if (CONSP (car)
3229 && STRINGP (XCAR (car))
3230 && fast_string_match (XCAR (car), buffer_name) >= 0)
3231 return XCDR (car);
3233 return Qnil;
3236 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
3237 doc: /* Returns non-nil if a new buffer named BUFFER-NAME would use the same window.
3238 See `same-window-buffer-names' and `same-window-regexps'. */)
3239 (buffer_name)
3240 Lisp_Object buffer_name;
3242 Lisp_Object tem;
3244 CHECK_STRING (buffer_name);
3246 tem = Fmember (buffer_name, Vsame_window_buffer_names);
3247 if (!NILP (tem))
3248 return Qt;
3250 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
3251 if (!NILP (tem))
3252 return Qt;
3254 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
3256 Lisp_Object car = XCAR (tem);
3257 if (STRINGP (car)
3258 && fast_string_match (car, buffer_name) >= 0)
3259 return Qt;
3260 else if (CONSP (car)
3261 && STRINGP (XCAR (car))
3262 && fast_string_match (XCAR (car), buffer_name) >= 0)
3263 return Qt;
3265 return Qnil;
3268 /* Use B so the default is (other-buffer). */
3269 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
3270 "BDisplay buffer: \nP",
3271 doc: /* Make BUFFER appear in some window but don't select it.
3272 BUFFER must be the name of an existing buffer, or, when called from Lisp,
3273 a buffer.
3274 If BUFFER is shown already in some window, just use that one,
3275 unless the window is the selected window and the optional second
3276 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).
3277 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
3278 Returns the window displaying BUFFER.
3279 If `display-buffer-reuse-frames' is non-nil, and another frame is currently
3280 displaying BUFFER, then simply raise that frame.
3282 The variables `special-display-buffer-names',
3283 `special-display-regexps', `same-window-buffer-names', and
3284 `same-window-regexps' customize how certain buffer names are handled.
3285 The latter two take effect only if NOT-THIS-WINDOW is t.
3287 If optional argument FRAME is `visible', search all visible frames.
3288 If FRAME is 0, search all visible and iconified frames.
3289 If FRAME is t, search all frames.
3290 If FRAME is a frame, search only that frame.
3291 If FRAME is nil, search only the selected frame
3292 (actually the last nonminibuffer frame),
3293 unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,
3294 which means search visible and iconified frames.
3296 If a full-width window on a splittable frame is available to display
3297 the buffer, it may be split, subject to the value of the variable
3298 `split-height-threshold'.
3300 If `even-window-heights' is non-nil, window heights will be evened out
3301 if displaying the buffer causes two vertically adjacent windows to be
3302 displayed. */)
3303 (buffer, not_this_window, frame)
3304 register Lisp_Object buffer, not_this_window, frame;
3306 register Lisp_Object window, tem, swp;
3307 struct frame *f;
3309 swp = Qnil;
3310 buffer = Fget_buffer (buffer);
3311 CHECK_BUFFER (buffer);
3313 if (!NILP (Vdisplay_buffer_function))
3314 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
3316 if (NILP (not_this_window)
3317 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
3318 return display_buffer_1 (selected_window);
3320 /* See if the user has specified this buffer should appear
3321 in the selected window. */
3322 if (NILP (not_this_window))
3324 swp = Fsame_window_p (XBUFFER (buffer)->name);
3325 if (!NILP (swp) && !no_switch_window (selected_window))
3327 Fswitch_to_buffer (buffer, Qnil);
3328 return display_buffer_1 (selected_window);
3332 /* If the user wants pop-up-frames or display-buffer-reuse-frames,
3333 look for a window showing BUFFER on any visible or iconified frame.
3334 Otherwise search only the current frame. */
3335 if (! NILP (frame))
3336 tem = frame;
3337 else if (pop_up_frames
3338 || display_buffer_reuse_frames
3339 || last_nonminibuf_frame == 0)
3340 XSETFASTINT (tem, 0);
3341 else
3342 XSETFRAME (tem, last_nonminibuf_frame);
3344 window = Fget_buffer_window (buffer, tem);
3345 if (!NILP (window)
3346 && (NILP (not_this_window) || !EQ (window, selected_window)))
3347 return display_buffer_1 (window);
3349 /* Certain buffer names get special handling. */
3350 if (!NILP (Vspecial_display_function) && NILP (swp))
3352 tem = Fspecial_display_p (XBUFFER (buffer)->name);
3353 if (EQ (tem, Qt))
3354 return call1 (Vspecial_display_function, buffer);
3355 if (CONSP (tem))
3356 return call2 (Vspecial_display_function, buffer, tem);
3359 /* If there are no frames open that have more than a minibuffer,
3360 we need to create a new frame. */
3361 if (pop_up_frames || last_nonminibuf_frame == 0)
3363 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3364 Fset_window_buffer (window, buffer, Qnil);
3365 return display_buffer_1 (window);
3368 f = SELECTED_FRAME ();
3369 if (pop_up_windows
3370 || FRAME_MINIBUF_ONLY_P (f)
3371 /* If the current frame is a special display frame,
3372 don't try to reuse its windows. */
3373 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
3375 Lisp_Object frames;
3377 frames = Qnil;
3378 if (FRAME_MINIBUF_ONLY_P (f))
3379 XSETFRAME (frames, last_nonminibuf_frame);
3380 /* Don't try to create a window if we would get an error. */
3381 if (split_height_threshold < window_min_height << 1)
3382 split_height_threshold = window_min_height << 1;
3384 /* Note that both Fget_largest_window and Fget_lru_window
3385 ignore minibuffers and dedicated windows.
3386 This means they can return nil. */
3388 /* If the frame we would try to split cannot be split,
3389 try other frames. */
3390 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
3392 /* Try visible frames first. */
3393 window = Fget_largest_window (Qvisible);
3394 /* If that didn't work, try iconified frames. */
3395 if (NILP (window))
3396 window = Fget_largest_window (make_number (0));
3397 if (NILP (window))
3398 window = Fget_largest_window (Qt);
3400 else
3401 window = Fget_largest_window (frames);
3403 /* If we got a tall enough full-width window that can be split,
3404 split it. */
3405 if (!NILP (window)
3406 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3407 && window_height (window) >= split_height_threshold
3408 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
3409 window = Fsplit_window (window, Qnil, Qnil);
3410 else
3412 Lisp_Object upper, lower, other;
3414 window = Fget_lru_window (frames);
3415 /* If the LRU window is selected, and big enough,
3416 and can be split, split it. */
3417 if (!NILP (window)
3418 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3419 && (EQ (window, selected_window)
3420 || EQ (XWINDOW (window)->parent, Qnil))
3421 && window_height (window) >= window_min_height << 1)
3422 window = Fsplit_window (window, Qnil, Qnil);
3423 /* If Fget_lru_window returned nil, try other approaches. */
3425 /* Try visible frames first. */
3426 if (NILP (window))
3427 window = Fget_buffer_window (buffer, Qvisible);
3428 if (NILP (window))
3429 window = Fget_largest_window (Qvisible);
3430 /* If that didn't work, try iconified frames. */
3431 if (NILP (window))
3432 window = Fget_buffer_window (buffer, make_number (0));
3433 if (NILP (window))
3434 window = Fget_largest_window (make_number (0));
3435 /* Try invisible frames. */
3436 if (NILP (window))
3437 window = Fget_buffer_window (buffer, Qt);
3438 if (NILP (window))
3439 window = Fget_largest_window (Qt);
3440 /* As a last resort, make a new frame. */
3441 if (NILP (window))
3442 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3443 /* If window appears above or below another,
3444 even out their heights. */
3445 other = upper = lower = Qnil;
3446 if (!NILP (XWINDOW (window)->prev))
3447 other = upper = XWINDOW (window)->prev, lower = window;
3448 if (!NILP (XWINDOW (window)->next))
3449 other = lower = XWINDOW (window)->next, upper = window;
3450 if (!NILP (other)
3451 && !NILP (Veven_window_heights)
3452 /* Check that OTHER and WINDOW are vertically arrayed. */
3453 && !EQ (XWINDOW (other)->top_line, XWINDOW (window)->top_line)
3454 && (XFASTINT (XWINDOW (other)->total_lines)
3455 > XFASTINT (XWINDOW (window)->total_lines)))
3457 int total = (XFASTINT (XWINDOW (other)->total_lines)
3458 + XFASTINT (XWINDOW (window)->total_lines));
3459 enlarge_window (upper,
3460 total / 2 - XFASTINT (XWINDOW (upper)->total_lines),
3461 0, 0);
3465 else
3466 window = Fget_lru_window (Qnil);
3468 Fset_window_buffer (window, buffer, Qnil);
3469 return display_buffer_1 (window);
3473 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3474 0, 1, 0,
3475 doc: /* Force redisplay of all windows.
3476 If optional arg OBJECT is a window, force redisplay of that window only.
3477 If OBJECT is a buffer or buffer name, force redisplay of all windows
3478 displaying that buffer. */)
3479 (object)
3480 Lisp_Object object;
3482 if (NILP (object))
3484 windows_or_buffers_changed++;
3485 update_mode_lines++;
3486 return Qt;
3489 if (WINDOWP (object))
3491 struct window *w = XWINDOW (object);
3492 mark_window_display_accurate (object, 0);
3493 w->update_mode_line = Qt;
3494 if (BUFFERP (w->buffer))
3495 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3496 ++update_mode_lines;
3497 return Qt;
3500 if (STRINGP (object))
3501 object = Fget_buffer (object);
3502 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3504 /* Walk all windows looking for buffer, and force update
3505 of each of those windows. */
3507 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3508 return NILP (object) ? Qnil : Qt;
3511 /* If nothing suitable was found, just return.
3512 We could signal an error, but this feature will typically be used
3513 asynchronously in timers or process sentinels, so we don't. */
3514 return Qnil;
3518 void
3519 temp_output_buffer_show (buf)
3520 register Lisp_Object buf;
3522 register struct buffer *old = current_buffer;
3523 register Lisp_Object window;
3524 register struct window *w;
3526 XBUFFER (buf)->directory = current_buffer->directory;
3528 Fset_buffer (buf);
3529 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3530 BEGV = BEG;
3531 ZV = Z;
3532 SET_PT (BEG);
3533 #if 0 /* rms: there should be no reason for this. */
3534 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3535 #endif
3536 set_buffer_internal (old);
3538 if (!EQ (Vtemp_buffer_show_function, Qnil))
3539 call1 (Vtemp_buffer_show_function, buf);
3540 else
3542 window = Fdisplay_buffer (buf, Qnil, Qnil);
3544 if (!EQ (XWINDOW (window)->frame, selected_frame))
3545 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3546 Vminibuf_scroll_window = window;
3547 w = XWINDOW (window);
3548 XSETFASTINT (w->hscroll, 0);
3549 XSETFASTINT (w->min_hscroll, 0);
3550 set_marker_restricted_both (w->start, buf, BEG, BEG);
3551 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3553 /* Run temp-buffer-show-hook, with the chosen window selected
3554 and its buffer current. */
3556 if (!NILP (Vrun_hooks)
3557 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3558 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3560 int count = SPECPDL_INDEX ();
3561 Lisp_Object prev_window, prev_buffer;
3562 prev_window = selected_window;
3563 XSETBUFFER (prev_buffer, old);
3565 /* Select the window that was chosen, for running the hook.
3566 Note: Both Fselect_window and select_window_norecord may
3567 set-buffer to the buffer displayed in the window,
3568 so we need to save the current buffer. --stef */
3569 record_unwind_protect (Fset_buffer, prev_buffer);
3570 record_unwind_protect (select_window_norecord, prev_window);
3571 Fselect_window (window, Qt);
3572 Fset_buffer (w->buffer);
3573 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3574 unbind_to (count, Qnil);
3579 static void
3580 make_dummy_parent (window)
3581 Lisp_Object window;
3583 Lisp_Object new;
3584 register struct window *o, *p;
3585 int i;
3587 o = XWINDOW (window);
3588 p = allocate_window ();
3589 for (i = 0; i < VECSIZE (struct window); ++i)
3590 ((struct Lisp_Vector *) p)->contents[i]
3591 = ((struct Lisp_Vector *)o)->contents[i];
3592 XSETWINDOW (new, p);
3594 XSETFASTINT (p->sequence_number, ++sequence_number);
3596 /* Put new into window structure in place of window */
3597 replace_window (window, new);
3599 o->next = Qnil;
3600 o->prev = Qnil;
3601 o->vchild = Qnil;
3602 o->hchild = Qnil;
3603 o->parent = new;
3605 p->start = Qnil;
3606 p->pointm = Qnil;
3607 p->buffer = Qnil;
3610 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3611 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3612 WINDOW defaults to selected one and SIZE to half its size.
3613 If optional third arg HORFLAG is non-nil, split side by side
3614 and put SIZE columns in the first of the pair. In that case,
3615 SIZE includes that window's scroll bar, or the divider column to its right.
3616 Interactively, all arguments are nil.
3618 Returns the newly created window (which is the lower or rightmost one).
3619 The upper or leftmost window is the original one and remains selected.
3620 See Info node `(elisp)Splitting Windows' for more details and examples.*/)
3621 (window, size, horflag)
3622 Lisp_Object window, size, horflag;
3624 register Lisp_Object new;
3625 register struct window *o, *p;
3626 FRAME_PTR fo;
3627 register int size_int;
3629 if (NILP (window))
3630 window = selected_window;
3631 else
3632 CHECK_LIVE_WINDOW (window);
3634 o = XWINDOW (window);
3635 fo = XFRAME (WINDOW_FRAME (o));
3637 if (NILP (size))
3639 if (!NILP (horflag))
3640 /* Calculate the size of the left-hand window, by dividing
3641 the usable space in columns by two.
3642 We round up, since the left-hand window may include
3643 a dividing line, while the right-hand may not. */
3644 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3645 else
3646 size_int = XFASTINT (o->total_lines) >> 1;
3648 else
3650 CHECK_NUMBER (size);
3651 size_int = XINT (size);
3654 if (MINI_WINDOW_P (o))
3655 error ("Attempt to split minibuffer window");
3656 else if (window_fixed_size_p (o, !NILP (horflag), 0))
3657 error ("Attempt to split fixed-size window");
3659 check_min_window_sizes ();
3661 if (NILP (horflag))
3663 if (size_int < window_min_height)
3664 error ("Window height %d too small (after splitting)", size_int);
3665 if (size_int + window_min_height > XFASTINT (o->total_lines))
3666 error ("Window height %d too small (after splitting)",
3667 XFASTINT (o->total_lines) - size_int);
3668 if (NILP (o->parent)
3669 || NILP (XWINDOW (o->parent)->vchild))
3671 make_dummy_parent (window);
3672 new = o->parent;
3673 XWINDOW (new)->vchild = window;
3676 else
3678 if (size_int < window_min_width)
3679 error ("Window width %d too small (after splitting)", size_int);
3681 if (size_int + window_min_width > XFASTINT (o->total_cols))
3682 error ("Window width %d too small (after splitting)",
3683 XFASTINT (o->total_cols) - size_int);
3684 if (NILP (o->parent)
3685 || NILP (XWINDOW (o->parent)->hchild))
3687 make_dummy_parent (window);
3688 new = o->parent;
3689 XWINDOW (new)->hchild = window;
3693 /* Now we know that window's parent is a vertical combination
3694 if we are dividing vertically, or a horizontal combination
3695 if we are making side-by-side windows */
3697 windows_or_buffers_changed++;
3698 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3699 new = make_window ();
3700 p = XWINDOW (new);
3702 p->frame = o->frame;
3703 p->next = o->next;
3704 if (!NILP (p->next))
3705 XWINDOW (p->next)->prev = new;
3706 p->prev = window;
3707 o->next = new;
3708 p->parent = o->parent;
3709 p->buffer = Qt;
3710 p->window_end_valid = Qnil;
3711 bzero (&p->last_cursor, sizeof p->last_cursor);
3713 /* Duplicate special geometry settings. */
3715 p->left_margin_cols = o->left_margin_cols;
3716 p->right_margin_cols = o->right_margin_cols;
3717 p->left_fringe_width = o->left_fringe_width;
3718 p->right_fringe_width = o->right_fringe_width;
3719 p->fringes_outside_margins = o->fringes_outside_margins;
3720 p->scroll_bar_width = o->scroll_bar_width;
3721 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3723 /* Apportion the available frame space among the two new windows */
3725 if (!NILP (horflag))
3727 p->total_lines = o->total_lines;
3728 p->top_line = o->top_line;
3729 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3730 XSETFASTINT (o->total_cols, size_int);
3731 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3732 adjust_window_margins (p);
3733 adjust_window_margins (o);
3735 else
3737 p->left_col = o->left_col;
3738 p->total_cols = o->total_cols;
3739 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3740 XSETFASTINT (o->total_lines, size_int);
3741 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3744 /* Adjust glyph matrices. */
3745 adjust_glyphs (fo);
3747 Fset_window_buffer (new, o->buffer, Qt);
3748 return new;
3751 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 3, "p",
3752 doc: /* Make current window ARG lines bigger.
3753 From program, optional second arg non-nil means grow sideways ARG columns.
3754 Interactively, if an argument is not given, make the window one line bigger.
3756 Optional third arg PRESERVE-BEFORE, if non-nil, means do not change the size
3757 of the siblings above or to the left of the selected window. Only
3758 siblings to the right or below are changed. */)
3759 (arg, side, preserve_before)
3760 register Lisp_Object arg, side, preserve_before;
3762 CHECK_NUMBER (arg);
3763 enlarge_window (selected_window, XINT (arg), !NILP (side),
3764 !NILP (preserve_before));
3766 if (! NILP (Vwindow_configuration_change_hook))
3767 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3769 return Qnil;
3772 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 3, "p",
3773 doc: /* Make current window ARG lines smaller.
3774 From program, optional second arg non-nil means shrink sideways arg columns.
3775 Interactively, if an argument is not given, make the window one line smaller.
3777 Optional third arg PRESERVE-BEFORE, if non-nil, means do not change the size
3778 of the siblings above or to the left of the selected window. Only
3779 siblings to the right or below are changed. */)
3780 (arg, side, preserve_before)
3781 register Lisp_Object arg, side, preserve_before;
3783 CHECK_NUMBER (arg);
3784 enlarge_window (selected_window, -XINT (arg), !NILP (side),
3785 !NILP (preserve_before));
3787 if (! NILP (Vwindow_configuration_change_hook))
3788 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3790 return Qnil;
3794 window_height (window)
3795 Lisp_Object window;
3797 register struct window *p = XWINDOW (window);
3798 return WINDOW_TOTAL_LINES (p);
3802 window_width (window)
3803 Lisp_Object window;
3805 register struct window *p = XWINDOW (window);
3806 return WINDOW_TOTAL_COLS (p);
3810 #define CURBEG(w) \
3811 *(widthflag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3813 #define CURSIZE(w) \
3814 *(widthflag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3817 /* Enlarge WINDOW by DELTA. WIDTHFLAG non-zero means
3818 increase its width. Siblings of the selected window are resized to
3819 fulfill the size request. If they become too small in the process,
3820 they will be deleted.
3822 If PRESERVE_BEFORE is nonzero, that means don't alter
3823 the siblings to the left or above WINDOW. */
3825 static void
3826 enlarge_window (window, delta, widthflag, preserve_before)
3827 Lisp_Object window;
3828 int delta, widthflag, preserve_before;
3830 Lisp_Object parent, next, prev;
3831 struct window *p;
3832 Lisp_Object *sizep;
3833 int maximum;
3834 int (*sizefun) P_ ((Lisp_Object))
3835 = widthflag ? window_width : window_height;
3836 void (*setsizefun) P_ ((Lisp_Object, int, int))
3837 = (widthflag ? set_window_width : set_window_height);
3839 /* Check values of window_min_width and window_min_height for
3840 validity. */
3841 check_min_window_sizes ();
3843 /* Give up if this window cannot be resized. */
3844 if (window_fixed_size_p (XWINDOW (window), widthflag, 1))
3845 error ("Window is not resizable");
3847 /* Find the parent of the selected window. */
3848 while (1)
3850 p = XWINDOW (window);
3851 parent = p->parent;
3853 if (NILP (parent))
3855 if (widthflag)
3856 error ("No other window to side of this one");
3857 break;
3860 if (widthflag
3861 ? !NILP (XWINDOW (parent)->hchild)
3862 : !NILP (XWINDOW (parent)->vchild))
3863 break;
3865 window = parent;
3868 sizep = &CURSIZE (window);
3871 register int maxdelta;
3873 /* Compute the maximum size increment this window can have. */
3875 if (preserve_before)
3877 if (!NILP (parent))
3879 maxdelta = (*sizefun) (parent) - XINT (*sizep);
3880 /* Subtract size of siblings before, since we can't take that. */
3881 maxdelta -= XINT (CURBEG (window)) - XINT (CURBEG (parent));
3883 else
3884 maxdelta = (!NILP (p->next) ? ((*sizefun) (p->next)
3885 - window_min_size (XWINDOW (p->next),
3886 widthflag, 0, 0))
3887 : (delta = 0));
3889 else
3890 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
3891 /* This is a main window followed by a minibuffer. */
3892 : !NILP (p->next) ? ((*sizefun) (p->next)
3893 - window_min_size (XWINDOW (p->next),
3894 widthflag, 0, 0))
3895 /* This is a minibuffer following a main window. */
3896 : !NILP (p->prev) ? ((*sizefun) (p->prev)
3897 - window_min_size (XWINDOW (p->prev),
3898 widthflag, 0, 0))
3899 /* This is a frame with only one window, a minibuffer-only
3900 or a minibufferless frame. */
3901 : (delta = 0));
3903 if (delta > maxdelta)
3904 /* This case traps trying to make the minibuffer
3905 the full frame, or make the only window aside from the
3906 minibuffer the full frame. */
3907 delta = maxdelta;
3910 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window), widthflag, 0, 0))
3912 delete_window (window);
3913 return;
3916 if (delta == 0)
3917 return;
3919 /* Find the total we can get from other siblings without deleting them. */
3920 maximum = 0;
3921 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
3922 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
3923 widthflag, 0, 0);
3924 if (! preserve_before)
3925 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
3926 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
3927 widthflag, 0, 0);
3929 /* If we can get it all from them without deleting them, do so. */
3930 if (delta <= maximum)
3932 Lisp_Object first_unaffected;
3933 Lisp_Object first_affected;
3934 int fixed_p;
3936 next = p->next;
3937 prev = p->prev;
3938 first_affected = window;
3939 /* Look at one sibling at a time,
3940 moving away from this window in both directions alternately,
3941 and take as much as we can get without deleting that sibling. */
3942 while (delta != 0
3943 && (!NILP (next) || (!preserve_before && !NILP (prev))))
3945 if (! NILP (next))
3947 int this_one = ((*sizefun) (next)
3948 - window_min_size (XWINDOW (next),
3949 widthflag, 0, &fixed_p));
3950 if (!fixed_p)
3952 if (this_one > delta)
3953 this_one = delta;
3955 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
3956 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
3958 delta -= this_one;
3961 next = XWINDOW (next)->next;
3964 if (delta == 0)
3965 break;
3967 if (!preserve_before && ! NILP (prev))
3969 int this_one = ((*sizefun) (prev)
3970 - window_min_size (XWINDOW (prev),
3971 widthflag, 0, &fixed_p));
3972 if (!fixed_p)
3974 if (this_one > delta)
3975 this_one = delta;
3977 first_affected = prev;
3979 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
3980 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
3982 delta -= this_one;
3985 prev = XWINDOW (prev)->prev;
3989 xassert (delta == 0);
3991 /* Now recalculate the edge positions of all the windows affected,
3992 based on the new sizes. */
3993 first_unaffected = next;
3994 prev = first_affected;
3995 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
3996 prev = next, next = XWINDOW (next)->next)
3998 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
3999 /* This does not change size of NEXT,
4000 but it propagates the new top edge to its children */
4001 (*setsizefun) (next, (*sizefun) (next), 0);
4004 else
4006 register int delta1;
4007 register int opht = (*sizefun) (parent);
4009 if (opht <= XINT (*sizep) + delta)
4011 /* If trying to grow this window to or beyond size of the parent,
4012 just delete all the sibling windows. */
4013 Lisp_Object start, tem, next;
4015 start = XWINDOW (parent)->vchild;
4016 if (NILP (start))
4017 start = XWINDOW (parent)->hchild;
4019 /* Delete any siblings that come after WINDOW. */
4020 tem = XWINDOW (window)->next;
4021 while (! NILP (tem))
4023 next = XWINDOW (tem)->next;
4024 delete_window (tem);
4025 tem = next;
4028 /* Delete any siblings that come after WINDOW.
4029 Note that if START is not WINDOW, then WINDOW still
4030 Fhas siblings, so WINDOW has not yet replaced its parent. */
4031 tem = start;
4032 while (! EQ (tem, window))
4034 next = XWINDOW (tem)->next;
4035 delete_window (tem);
4036 tem = next;
4039 else
4041 /* Otherwise, make delta1 just right so that if we add
4042 delta1 lines to this window and to the parent, and then
4043 shrink the parent back to its original size, the new
4044 proportional size of this window will increase by delta.
4046 The function size_window will compute the new height h'
4047 of the window from delta1 as:
4049 e = delta1/n
4050 x = delta1 - delta1/n * n for the 1st resizable child
4051 h' = h + e + x
4053 where n is the number of children that can be resized.
4054 We can ignore x by choosing a delta1 that is a multiple of
4055 n. We want the height of this window to come out as
4057 h' = h + delta
4059 So, delta1 must be
4061 h + e = h + delta
4062 delta1/n = delta
4063 delta1 = n * delta.
4065 The number of children n equals the number of resizable
4066 children of this window + 1 because we know window itself
4067 is resizable (otherwise we would have signalled an error. */
4069 struct window *w = XWINDOW (window);
4070 Lisp_Object s;
4071 int n = 1;
4073 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
4074 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
4075 ++n;
4076 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
4077 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
4078 ++n;
4080 delta1 = n * delta;
4082 /* Add delta1 lines or columns to this window, and to the parent,
4083 keeping things consistent while not affecting siblings. */
4084 XSETINT (CURSIZE (parent), opht + delta1);
4085 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4087 /* Squeeze out delta1 lines or columns from our parent,
4088 shriking this window and siblings proportionately.
4089 This brings parent back to correct size.
4090 Delta1 was calculated so this makes this window the desired size,
4091 taking it all out of the siblings. */
4092 (*setsizefun) (parent, opht, 0);
4097 XSETFASTINT (p->last_modified, 0);
4098 XSETFASTINT (p->last_overlay_modified, 0);
4100 /* Adjust glyph matrices. */
4101 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4104 #undef CURBEG
4105 #undef CURSIZE
4109 /***********************************************************************
4110 Resizing Mini-Windows
4111 ***********************************************************************/
4113 static void shrink_window_lowest_first P_ ((struct window *, int));
4115 enum save_restore_action
4117 CHECK_ORIG_SIZES,
4118 SAVE_ORIG_SIZES,
4119 RESTORE_ORIG_SIZES
4122 static int save_restore_orig_size P_ ((struct window *,
4123 enum save_restore_action));
4125 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4126 from lowest windows first. */
4128 static void
4129 shrink_window_lowest_first (w, height)
4130 struct window *w;
4131 int height;
4133 struct window *c;
4134 Lisp_Object child;
4135 int old_height;
4137 xassert (!MINI_WINDOW_P (w));
4139 /* Set redisplay hints. */
4140 XSETFASTINT (w->last_modified, 0);
4141 XSETFASTINT (w->last_overlay_modified, 0);
4142 windows_or_buffers_changed++;
4143 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4145 old_height = XFASTINT (w->total_lines);
4146 XSETFASTINT (w->total_lines, height);
4148 if (!NILP (w->hchild))
4150 for (child = w->hchild; !NILP (child); child = c->next)
4152 c = XWINDOW (child);
4153 c->top_line = w->top_line;
4154 shrink_window_lowest_first (c, height);
4157 else if (!NILP (w->vchild))
4159 Lisp_Object last_child;
4160 int delta = old_height - height;
4161 int last_top;
4163 last_child = Qnil;
4165 /* Find the last child. We are taking space from lowest windows
4166 first, so we iterate over children from the last child
4167 backwards. */
4168 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
4169 last_child = child;
4171 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
4172 for (child = last_child; delta && !NILP (child); child = c->prev)
4174 int this_one;
4176 c = XWINDOW (child);
4177 this_one = XFASTINT (c->total_lines) - MIN_SAFE_WINDOW_HEIGHT;
4179 if (this_one > delta)
4180 this_one = delta;
4182 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4183 delta -= this_one;
4186 /* Compute new positions. */
4187 last_top = XINT (w->top_line);
4188 for (child = w->vchild; !NILP (child); child = c->next)
4190 c = XWINDOW (child);
4191 c->top_line = make_number (last_top);
4192 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4193 last_top += XFASTINT (c->total_lines);
4199 /* Save, restore, or check positions and sizes in the window tree
4200 rooted at W. ACTION says what to do.
4202 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4203 orig_total_lines members are valid for all windows in the window
4204 tree. Value is non-zero if they are valid.
4206 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4207 orig_top_line and orig_total_lines for all windows in the tree.
4209 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4210 stored in orig_top_line and orig_total_lines for all windows. */
4212 static int
4213 save_restore_orig_size (w, action)
4214 struct window *w;
4215 enum save_restore_action action;
4217 int success_p = 1;
4219 while (w)
4221 if (!NILP (w->hchild))
4223 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4224 success_p = 0;
4226 else if (!NILP (w->vchild))
4228 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4229 success_p = 0;
4232 switch (action)
4234 case CHECK_ORIG_SIZES:
4235 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4236 return 0;
4237 break;
4239 case SAVE_ORIG_SIZES:
4240 w->orig_top_line = w->top_line;
4241 w->orig_total_lines = w->total_lines;
4242 XSETFASTINT (w->last_modified, 0);
4243 XSETFASTINT (w->last_overlay_modified, 0);
4244 break;
4246 case RESTORE_ORIG_SIZES:
4247 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4248 w->top_line = w->orig_top_line;
4249 w->total_lines = w->orig_total_lines;
4250 w->orig_total_lines = w->orig_top_line = Qnil;
4251 XSETFASTINT (w->last_modified, 0);
4252 XSETFASTINT (w->last_overlay_modified, 0);
4253 break;
4255 default:
4256 abort ();
4259 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4262 return success_p;
4266 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4267 without deleting other windows. */
4269 void
4270 grow_mini_window (w, delta)
4271 struct window *w;
4272 int delta;
4274 struct frame *f = XFRAME (w->frame);
4275 struct window *root;
4277 xassert (MINI_WINDOW_P (w));
4278 xassert (delta >= 0);
4280 /* Check values of window_min_width and window_min_height for
4281 validity. */
4282 check_min_window_sizes ();
4284 /* Compute how much we can enlarge the mini-window without deleting
4285 other windows. */
4286 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4287 if (delta)
4289 int min_height = window_min_size (root, 0, 0, 0);
4290 if (XFASTINT (root->total_lines) - delta < min_height)
4291 /* Note that the root window may already be smaller than
4292 min_height. */
4293 delta = max (0, XFASTINT (root->total_lines) - min_height);
4296 if (delta)
4298 /* Save original window sizes and positions, if not already done. */
4299 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4300 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4302 /* Shrink other windows. */
4303 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4305 /* Grow the mini-window. */
4306 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4307 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4308 XSETFASTINT (w->last_modified, 0);
4309 XSETFASTINT (w->last_overlay_modified, 0);
4311 adjust_glyphs (f);
4316 /* Shrink mini-window W. If there is recorded info about window sizes
4317 before a call to grow_mini_window, restore recorded window sizes.
4318 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4319 line. */
4321 void
4322 shrink_mini_window (w)
4323 struct window *w;
4325 struct frame *f = XFRAME (w->frame);
4326 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4328 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4330 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4331 adjust_glyphs (f);
4332 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4333 windows_or_buffers_changed = 1;
4335 else if (XFASTINT (w->total_lines) > 1)
4337 /* Distribute the additional lines of the mini-window
4338 among the other windows. */
4339 Lisp_Object window;
4340 XSETWINDOW (window, w);
4341 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0, 0);
4347 /* Mark window cursors off for all windows in the window tree rooted
4348 at W by setting their phys_cursor_on_p flag to zero. Called from
4349 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4350 the frame are cleared. */
4352 void
4353 mark_window_cursors_off (w)
4354 struct window *w;
4356 while (w)
4358 if (!NILP (w->hchild))
4359 mark_window_cursors_off (XWINDOW (w->hchild));
4360 else if (!NILP (w->vchild))
4361 mark_window_cursors_off (XWINDOW (w->vchild));
4362 else
4363 w->phys_cursor_on_p = 0;
4365 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4370 /* Return number of lines of text (not counting mode lines) in W. */
4373 window_internal_height (w)
4374 struct window *w;
4376 int ht = XFASTINT (w->total_lines);
4378 if (!MINI_WINDOW_P (w))
4380 if (!NILP (w->parent)
4381 || !NILP (w->vchild)
4382 || !NILP (w->hchild)
4383 || !NILP (w->next)
4384 || !NILP (w->prev)
4385 || WINDOW_WANTS_MODELINE_P (w))
4386 --ht;
4388 if (WINDOW_WANTS_HEADER_LINE_P (w))
4389 --ht;
4392 return ht;
4396 /* Return the number of columns in W.
4397 Don't count columns occupied by scroll bars or the vertical bar
4398 separating W from the sibling to its right. */
4401 window_box_text_cols (w)
4402 struct window *w;
4404 struct frame *f = XFRAME (WINDOW_FRAME (w));
4405 int width = XINT (w->total_cols);
4407 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4408 /* Scroll bars occupy a few columns. */
4409 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4410 else if (!FRAME_WINDOW_P (f)
4411 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4412 /* The column of `|' characters separating side-by-side windows
4413 occupies one column only. */
4414 width -= 1;
4416 if (FRAME_WINDOW_P (f))
4417 /* On window-systems, fringes and display margins cannot be
4418 used for normal text. */
4419 width -= (WINDOW_FRINGE_COLS (w)
4420 + WINDOW_LEFT_MARGIN_COLS (w)
4421 + WINDOW_RIGHT_MARGIN_COLS (w));
4423 return width;
4427 /************************************************************************
4428 Window Scrolling
4429 ***********************************************************************/
4431 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4432 N screen-fulls, which is defined as the height of the window minus
4433 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4434 instead. Negative values of N mean scroll down. NOERROR non-zero
4435 means don't signal an error if we try to move over BEGV or ZV,
4436 respectively. */
4438 static void
4439 window_scroll (window, n, whole, noerror)
4440 Lisp_Object window;
4441 int n;
4442 int whole;
4443 int noerror;
4445 immediate_quit = 1;
4447 /* If we must, use the pixel-based version which is much slower than
4448 the line-based one but can handle varying line heights. */
4449 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4450 window_scroll_pixel_based (window, n, whole, noerror);
4451 else
4452 window_scroll_line_based (window, n, whole, noerror);
4454 immediate_quit = 0;
4458 /* Implementation of window_scroll that works based on pixel line
4459 heights. See the comment of window_scroll for parameter
4460 descriptions. */
4462 static void
4463 window_scroll_pixel_based (window, n, whole, noerror)
4464 Lisp_Object window;
4465 int n;
4466 int whole;
4467 int noerror;
4469 struct it it;
4470 struct window *w = XWINDOW (window);
4471 struct text_pos start;
4472 Lisp_Object tem;
4473 int this_scroll_margin;
4474 int preserve_y;
4475 /* True if we fiddled the window vscroll field without really scrolling. */
4476 int vscrolled = 0;
4478 SET_TEXT_POS_FROM_MARKER (start, w->start);
4480 /* If PT is not visible in WINDOW, move back one half of
4481 the screen. Allow PT to be partially visible, otherwise
4482 something like (scroll-down 1) with PT in the line before
4483 the partially visible one would recenter. */
4484 tem = Fpos_visible_in_window_p (make_number (PT), window, Qt);
4485 if (NILP (tem))
4487 /* Move backward half the height of the window. Performance note:
4488 vmotion used here is about 10% faster, but would give wrong
4489 results for variable height lines. */
4490 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4491 it.current_y = it.last_visible_y;
4492 move_it_vertically (&it, - window_box_height (w) / 2);
4494 /* The function move_iterator_vertically may move over more than
4495 the specified y-distance. If it->w is small, e.g. a
4496 mini-buffer window, we may end up in front of the window's
4497 display area. This is the case when Start displaying at the
4498 start of the line containing PT in this case. */
4499 if (it.current_y <= 0)
4501 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4502 move_it_vertically (&it, 0);
4503 it.current_y = 0;
4506 start = it.current.pos;
4509 /* If scroll_preserve_screen_position is non-zero, we try to set
4510 point in the same window line as it is now, so get that line. */
4511 if (!NILP (Vscroll_preserve_screen_position))
4513 start_display (&it, w, start);
4514 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4515 preserve_y = it.current_y;
4517 else
4518 preserve_y = -1;
4520 /* Move iterator it from start the specified distance forward or
4521 backward. The result is the new window start. */
4522 start_display (&it, w, start);
4523 if (whole)
4525 int screen_full = (window_box_height (w)
4526 - next_screen_context_lines * FRAME_LINE_HEIGHT (it.f));
4527 int dy = n * screen_full;
4529 /* Note that move_it_vertically always moves the iterator to the
4530 start of a line. So, if the last line doesn't have a newline,
4531 we would end up at the start of the line ending at ZV. */
4532 if (dy <= 0)
4533 move_it_vertically_backward (&it, -dy);
4534 else if (dy > 0)
4535 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4536 MOVE_TO_POS | MOVE_TO_Y);
4538 else
4539 move_it_by_lines (&it, n, 1);
4541 /* We failed if we find ZV is already on the screen (scrolling up,
4542 means there's nothing past the end), or if we can't start any
4543 earlier (scrolling down, means there's nothing past the top). */
4544 if ((n > 0 && IT_CHARPOS (it) == ZV)
4545 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4547 if (IT_CHARPOS (it) == ZV)
4549 if (it.current_y < it.last_visible_y
4550 && (it.current_y + it.max_ascent + it.max_descent
4551 >= it.last_visible_y))
4553 /* The last line was only partially visible, make it fully
4554 visible. */
4555 w->vscroll = (it.last_visible_y
4556 - it.current_y + it.max_ascent + it.max_descent);
4557 adjust_glyphs (it.f);
4559 else if (noerror)
4560 return;
4561 else
4562 Fsignal (Qend_of_buffer, Qnil);
4564 else
4566 if (w->vscroll != 0)
4567 /* The first line was only partially visible, make it fully
4568 visible. */
4569 w->vscroll = 0;
4570 else if (noerror)
4571 return;
4572 else
4573 Fsignal (Qbeginning_of_buffer, Qnil);
4576 /* If control gets here, then we vscrolled. */
4578 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4580 /* Don't try to change the window start below. */
4581 vscrolled = 1;
4584 if (! vscrolled)
4586 int pos = IT_CHARPOS (it);
4587 int bytepos;
4589 /* If in the middle of a multi-glyph character move forward to
4590 the next character. */
4591 if (in_display_vector_p (&it))
4593 ++pos;
4594 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4597 /* Set the window start, and set up the window for redisplay. */
4598 set_marker_restricted (w->start, make_number (pos),
4599 w->buffer);
4600 bytepos = XMARKER (w->start)->bytepos;
4601 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4602 ? Qt : Qnil);
4603 w->update_mode_line = Qt;
4604 XSETFASTINT (w->last_modified, 0);
4605 XSETFASTINT (w->last_overlay_modified, 0);
4606 /* Set force_start so that redisplay_window will run the
4607 window-scroll-functions. */
4608 w->force_start = Qt;
4611 it.current_y = it.vpos = 0;
4613 /* Preserve the screen position if we must. */
4614 if (preserve_y >= 0)
4616 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
4617 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4619 else
4621 /* Move PT out of scroll margins. */
4622 this_scroll_margin = max (0, scroll_margin);
4623 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4624 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4626 if (n > 0)
4628 /* We moved the window start towards ZV, so PT may be now
4629 in the scroll margin at the top. */
4630 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4631 while (it.current_y < this_scroll_margin)
4633 int prev = it.current_y;
4634 move_it_by_lines (&it, 1, 1);
4635 if (prev == it.current_y)
4636 break;
4638 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4640 else if (n < 0)
4642 int charpos, bytepos;
4644 /* We moved the window start towards BEGV, so PT may be now
4645 in the scroll margin at the bottom. */
4646 move_it_to (&it, PT, -1,
4647 it.last_visible_y - this_scroll_margin - 1, -1,
4648 MOVE_TO_POS | MOVE_TO_Y);
4650 /* Save our position, in case it's correct. */
4651 charpos = IT_CHARPOS (it);
4652 bytepos = IT_BYTEPOS (it);
4654 /* See if point is on a partially visible line at the end. */
4655 move_it_by_lines (&it, 1, 1);
4656 if (it.current_y > it.last_visible_y)
4657 /* The last line was only partially visible, so back up two
4658 lines to make sure we're on a fully visible line. */
4660 move_it_by_lines (&it, -2, 0);
4661 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4663 else
4664 /* No, the position we saved is OK, so use it. */
4665 SET_PT_BOTH (charpos, bytepos);
4671 /* Implementation of window_scroll that works based on screen lines.
4672 See the comment of window_scroll for parameter descriptions. */
4674 static void
4675 window_scroll_line_based (window, n, whole, noerror)
4676 Lisp_Object window;
4677 int n;
4678 int whole;
4679 int noerror;
4681 register struct window *w = XWINDOW (window);
4682 register int opoint = PT, opoint_byte = PT_BYTE;
4683 register int pos, pos_byte;
4684 register int ht = window_internal_height (w);
4685 register Lisp_Object tem;
4686 int lose;
4687 Lisp_Object bolp;
4688 int startpos;
4689 struct position posit;
4690 int original_vpos;
4692 /* If scrolling screen-fulls, compute the number of lines to
4693 scroll from the window's height. */
4694 if (whole)
4695 n *= max (1, ht - next_screen_context_lines);
4697 startpos = marker_position (w->start);
4699 posit = *compute_motion (startpos, 0, 0, 0,
4700 PT, ht, 0,
4701 window_box_text_cols (w), XINT (w->hscroll),
4702 0, w);
4703 original_vpos = posit.vpos;
4705 XSETFASTINT (tem, PT);
4706 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4708 if (NILP (tem))
4710 Fvertical_motion (make_number (- (ht / 2)), window);
4711 startpos = PT;
4714 SET_PT (startpos);
4715 lose = n < 0 && PT == BEGV;
4716 Fvertical_motion (make_number (n), window);
4717 pos = PT;
4718 pos_byte = PT_BYTE;
4719 bolp = Fbolp ();
4720 SET_PT_BOTH (opoint, opoint_byte);
4722 if (lose)
4724 if (noerror)
4725 return;
4726 else
4727 Fsignal (Qbeginning_of_buffer, Qnil);
4730 if (pos < ZV)
4732 int this_scroll_margin = scroll_margin;
4734 /* Don't use a scroll margin that is negative or too large. */
4735 if (this_scroll_margin < 0)
4736 this_scroll_margin = 0;
4738 if (XINT (w->total_lines) < 4 * scroll_margin)
4739 this_scroll_margin = XINT (w->total_lines) / 4;
4741 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4742 w->start_at_line_beg = bolp;
4743 w->update_mode_line = Qt;
4744 XSETFASTINT (w->last_modified, 0);
4745 XSETFASTINT (w->last_overlay_modified, 0);
4746 /* Set force_start so that redisplay_window will run
4747 the window-scroll-functions. */
4748 w->force_start = Qt;
4750 if (whole && !NILP (Vscroll_preserve_screen_position))
4752 SET_PT_BOTH (pos, pos_byte);
4753 Fvertical_motion (make_number (original_vpos), window);
4755 /* If we scrolled forward, put point enough lines down
4756 that it is outside the scroll margin. */
4757 else if (n > 0)
4759 int top_margin;
4761 if (this_scroll_margin > 0)
4763 SET_PT_BOTH (pos, pos_byte);
4764 Fvertical_motion (make_number (this_scroll_margin), window);
4765 top_margin = PT;
4767 else
4768 top_margin = pos;
4770 if (top_margin <= opoint)
4771 SET_PT_BOTH (opoint, opoint_byte);
4772 else if (!NILP (Vscroll_preserve_screen_position))
4774 SET_PT_BOTH (pos, pos_byte);
4775 Fvertical_motion (make_number (original_vpos), window);
4777 else
4778 SET_PT (top_margin);
4780 else if (n < 0)
4782 int bottom_margin;
4784 /* If we scrolled backward, put point near the end of the window
4785 but not within the scroll margin. */
4786 SET_PT_BOTH (pos, pos_byte);
4787 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4788 if (XFASTINT (tem) == ht - this_scroll_margin)
4789 bottom_margin = PT;
4790 else
4791 bottom_margin = PT + 1;
4793 if (bottom_margin > opoint)
4794 SET_PT_BOTH (opoint, opoint_byte);
4795 else
4797 if (!NILP (Vscroll_preserve_screen_position))
4799 SET_PT_BOTH (pos, pos_byte);
4800 Fvertical_motion (make_number (original_vpos), window);
4802 else
4803 Fvertical_motion (make_number (-1), window);
4807 else
4809 if (noerror)
4810 return;
4811 else
4812 Fsignal (Qend_of_buffer, Qnil);
4817 /* Scroll selected_window up or down. If N is nil, scroll a
4818 screen-full which is defined as the height of the window minus
4819 next_screen_context_lines. If N is the symbol `-', scroll.
4820 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4821 up. This is the guts of Fscroll_up and Fscroll_down. */
4823 static void
4824 scroll_command (n, direction)
4825 Lisp_Object n;
4826 int direction;
4828 int count = SPECPDL_INDEX ();
4830 xassert (abs (direction) == 1);
4832 /* If selected window's buffer isn't current, make it current for
4833 the moment. But don't screw up if window_scroll gets an error. */
4834 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4836 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4837 Fset_buffer (XWINDOW (selected_window)->buffer);
4839 /* Make redisplay consider other windows than just selected_window. */
4840 ++windows_or_buffers_changed;
4843 if (NILP (n))
4844 window_scroll (selected_window, direction, 1, 0);
4845 else if (EQ (n, Qminus))
4846 window_scroll (selected_window, -direction, 1, 0);
4847 else
4849 n = Fprefix_numeric_value (n);
4850 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4853 unbind_to (count, Qnil);
4856 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
4857 doc: /* Scroll text of current window upward ARG lines.
4858 If ARG is omitted or nil, scroll upward by a near full screen.
4859 A near full screen is `next-screen-context-lines' less than a full screen.
4860 Negative ARG means scroll downward.
4861 If ARG is the atom `-', scroll downward by nearly full screen.
4862 When calling from a program, supply as argument a number, nil, or `-'. */)
4863 (arg)
4864 Lisp_Object arg;
4866 scroll_command (arg, 1);
4867 return Qnil;
4870 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
4871 doc: /* Scroll text of current window down ARG lines.
4872 If ARG is omitted or nil, scroll down by a near full screen.
4873 A near full screen is `next-screen-context-lines' less than a full screen.
4874 Negative ARG means scroll upward.
4875 If ARG is the atom `-', scroll upward by nearly full screen.
4876 When calling from a program, supply as argument a number, nil, or `-'. */)
4877 (arg)
4878 Lisp_Object arg;
4880 scroll_command (arg, -1);
4881 return Qnil;
4884 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4885 doc: /* Return the other window for \"other window scroll\" commands.
4886 If `other-window-scroll-buffer' is non-nil, a window
4887 showing that buffer is used.
4888 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4889 specifies the window. This takes precedence over
4890 `other-window-scroll-buffer'. */)
4893 Lisp_Object window;
4895 if (MINI_WINDOW_P (XWINDOW (selected_window))
4896 && !NILP (Vminibuf_scroll_window))
4897 window = Vminibuf_scroll_window;
4898 /* If buffer is specified, scroll that buffer. */
4899 else if (!NILP (Vother_window_scroll_buffer))
4901 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4902 if (NILP (window))
4903 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4905 else
4907 /* Nothing specified; look for a neighboring window on the same
4908 frame. */
4909 window = Fnext_window (selected_window, Qnil, Qnil);
4911 if (EQ (window, selected_window))
4912 /* That didn't get us anywhere; look for a window on another
4913 visible frame. */
4915 window = Fnext_window (window, Qnil, Qt);
4916 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4917 && ! EQ (window, selected_window));
4920 CHECK_LIVE_WINDOW (window);
4922 if (EQ (window, selected_window))
4923 error ("There is no other window");
4925 return window;
4928 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4929 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4930 A near full screen is `next-screen-context-lines' less than a full screen.
4931 The next window is the one below the current one; or the one at the top
4932 if the current one is at the bottom. Negative ARG means scroll downward.
4933 If ARG is the atom `-', scroll downward by nearly full screen.
4934 When calling from a program, supply as argument a number, nil, or `-'.
4936 If `other-window-scroll-buffer' is non-nil, scroll the window
4937 showing that buffer, popping the buffer up if necessary.
4938 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4939 specifies the window to scroll. This takes precedence over
4940 `other-window-scroll-buffer'. */)
4941 (arg)
4942 Lisp_Object arg;
4944 Lisp_Object window;
4945 struct window *w;
4946 int count = SPECPDL_INDEX ();
4948 window = Fother_window_for_scrolling ();
4949 w = XWINDOW (window);
4951 /* Don't screw up if window_scroll gets an error. */
4952 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4953 ++windows_or_buffers_changed;
4955 Fset_buffer (w->buffer);
4956 SET_PT (marker_position (w->pointm));
4958 if (NILP (arg))
4959 window_scroll (window, 1, 1, 1);
4960 else if (EQ (arg, Qminus))
4961 window_scroll (window, -1, 1, 1);
4962 else
4964 if (CONSP (arg))
4965 arg = Fcar (arg);
4966 CHECK_NUMBER (arg);
4967 window_scroll (window, XINT (arg), 0, 1);
4970 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4971 unbind_to (count, Qnil);
4973 return Qnil;
4976 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
4977 doc: /* Scroll selected window display ARG columns left.
4978 Default for ARG is window width minus 2.
4979 Value is the total amount of leftward horizontal scrolling in
4980 effect after the change.
4981 If `automatic-hscrolling' is non-nil, the argument ARG modifies
4982 a lower bound for automatic scrolling, i.e. automatic scrolling
4983 will not scroll a window to a column less than the value returned
4984 by this function. */)
4985 (arg)
4986 register Lisp_Object arg;
4988 Lisp_Object result;
4989 int hscroll;
4990 struct window *w = XWINDOW (selected_window);
4992 if (NILP (arg))
4993 XSETFASTINT (arg, window_box_text_cols (w) - 2);
4994 else
4995 arg = Fprefix_numeric_value (arg);
4997 hscroll = XINT (w->hscroll) + XINT (arg);
4998 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5000 if (interactive_p (0))
5001 w->min_hscroll = w->hscroll;
5003 return result;
5006 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
5007 doc: /* Scroll selected window display ARG columns right.
5008 Default for ARG is window width minus 2.
5009 Value is the total amount of leftward horizontal scrolling in
5010 effect after the change.
5011 If `automatic-hscrolling' is non-nil, the argument ARG modifies
5012 a lower bound for automatic scrolling, i.e. automatic scrolling
5013 will not scroll a window to a column less than the value returned
5014 by this function. */)
5015 (arg)
5016 register Lisp_Object arg;
5018 Lisp_Object result;
5019 int hscroll;
5020 struct window *w = XWINDOW (selected_window);
5022 if (NILP (arg))
5023 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5024 else
5025 arg = Fprefix_numeric_value (arg);
5027 hscroll = XINT (w->hscroll) - XINT (arg);
5028 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5030 if (interactive_p (0))
5031 w->min_hscroll = w->hscroll;
5033 return result;
5036 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5037 doc: /* Return the window which was selected when entering the minibuffer.
5038 Returns nil, if current window is not a minibuffer window. */)
5041 if (minibuf_level > 0
5042 && MINI_WINDOW_P (XWINDOW (selected_window))
5043 && WINDOW_LIVE_P (minibuf_selected_window))
5044 return minibuf_selected_window;
5046 return Qnil;
5049 /* Value is the number of lines actually displayed in window W,
5050 as opposed to its height. */
5052 static int
5053 displayed_window_lines (w)
5054 struct window *w;
5056 struct it it;
5057 struct text_pos start;
5058 int height = window_box_height (w);
5059 struct buffer *old_buffer;
5060 int bottom_y;
5062 if (XBUFFER (w->buffer) != current_buffer)
5064 old_buffer = current_buffer;
5065 set_buffer_internal (XBUFFER (w->buffer));
5067 else
5068 old_buffer = NULL;
5070 /* In case W->start is out of the accessible range, do something
5071 reasonable. This happens in Info mode when Info-scroll-down
5072 calls (recenter -1) while W->start is 1. */
5073 if (XMARKER (w->start)->charpos < BEGV)
5074 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5075 else if (XMARKER (w->start)->charpos > ZV)
5076 SET_TEXT_POS (start, ZV, ZV_BYTE);
5077 else
5078 SET_TEXT_POS_FROM_MARKER (start, w->start);
5080 start_display (&it, w, start);
5081 move_it_vertically (&it, height);
5082 bottom_y = line_bottom_y (&it);
5084 /* rms: On a non-window display,
5085 the value of it.vpos at the bottom of the screen
5086 seems to be 1 larger than window_box_height (w).
5087 This kludge fixes a bug whereby (move-to-window-line -1)
5088 when ZV is on the last screen line
5089 moves to the previous screen line instead of the last one. */
5090 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5091 height++;
5093 /* Add in empty lines at the bottom of the window. */
5094 if (bottom_y < height)
5096 int uy = FRAME_LINE_HEIGHT (it.f);
5097 it.vpos += (height - bottom_y + uy - 1) / uy;
5100 if (old_buffer)
5101 set_buffer_internal (old_buffer);
5103 return it.vpos;
5107 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5108 doc: /* Center point in window and redisplay frame.
5109 With prefix argument ARG, recenter putting point on screen line ARG
5110 relative to the current window. If ARG is negative, it counts up from the
5111 bottom of the window. (ARG should be less than the height of the window.)
5113 If ARG is omitted or nil, erase the entire frame and then
5114 redraw with point in the center of the current window.
5115 Just C-u as prefix means put point in the center of the window
5116 and redisplay normally--don't erase and redraw the frame. */)
5117 (arg)
5118 register Lisp_Object arg;
5120 struct window *w = XWINDOW (selected_window);
5121 struct buffer *buf = XBUFFER (w->buffer);
5122 struct buffer *obuf = current_buffer;
5123 int center_p = 0;
5124 int charpos, bytepos;
5126 /* If redisplay is suppressed due to an error, try again. */
5127 obuf->display_error_modiff = 0;
5129 if (NILP (arg))
5131 int i;
5133 /* Invalidate pixel data calculated for all compositions. */
5134 for (i = 0; i < n_compositions; i++)
5135 composition_table[i]->font = NULL;
5137 Fredraw_frame (w->frame);
5138 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
5139 center_p = 1;
5141 else if (CONSP (arg)) /* Just C-u. */
5142 center_p = 1;
5143 else
5145 arg = Fprefix_numeric_value (arg);
5146 CHECK_NUMBER (arg);
5149 set_buffer_internal (buf);
5151 /* Handle centering on a graphical frame specially. Such frames can
5152 have variable-height lines and centering point on the basis of
5153 line counts would lead to strange effects. */
5154 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5156 if (center_p)
5158 struct it it;
5159 struct text_pos pt;
5161 SET_TEXT_POS (pt, PT, PT_BYTE);
5162 start_display (&it, w, pt);
5163 move_it_vertically (&it, - window_box_height (w) / 2);
5164 charpos = IT_CHARPOS (it);
5165 bytepos = IT_BYTEPOS (it);
5167 else if (XINT (arg) < 0)
5169 struct it it;
5170 struct text_pos pt;
5171 int y0, y1, h, nlines;
5173 SET_TEXT_POS (pt, PT, PT_BYTE);
5174 start_display (&it, w, pt);
5175 y0 = it.current_y;
5177 /* The amount of pixels we have to move back is the window
5178 height minus what's displayed in the line containing PT,
5179 and the lines below. */
5180 nlines = - XINT (arg) - 1;
5181 move_it_by_lines (&it, nlines, 1);
5183 y1 = line_bottom_y (&it);
5185 /* If we can't move down NLINES lines because we hit
5186 the end of the buffer, count in some empty lines. */
5187 if (it.vpos < nlines)
5188 y1 += (nlines - it.vpos) * FRAME_LINE_HEIGHT (it.f);
5190 h = window_box_height (w) - (y1 - y0);
5192 start_display (&it, w, pt);
5193 move_it_vertically (&it, - h);
5194 charpos = IT_CHARPOS (it);
5195 bytepos = IT_BYTEPOS (it);
5197 else
5199 struct position pos;
5200 pos = *vmotion (PT, - XINT (arg), w);
5201 charpos = pos.bufpos;
5202 bytepos = pos.bytepos;
5205 else
5207 struct position pos;
5208 int ht = window_internal_height (w);
5210 if (center_p)
5211 arg = make_number (ht / 2);
5212 else if (XINT (arg) < 0)
5213 arg = make_number (XINT (arg) + ht);
5215 pos = *vmotion (PT, - XINT (arg), w);
5216 charpos = pos.bufpos;
5217 bytepos = pos.bytepos;
5220 /* Set the new window start. */
5221 set_marker_both (w->start, w->buffer, charpos, bytepos);
5222 w->window_end_valid = Qnil;
5224 w->optional_new_start = Qt;
5226 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5227 w->start_at_line_beg = Qt;
5228 else
5229 w->start_at_line_beg = Qnil;
5231 set_buffer_internal (obuf);
5232 return Qnil;
5236 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5237 0, 1, 0,
5238 doc: /* Return the height in lines of the text display area of WINDOW.
5239 This doesn't include the mode-line (or header-line if any) or any
5240 partial-height lines in the text display area. */)
5241 (window)
5242 Lisp_Object window;
5244 struct window *w = decode_window (window);
5245 int pixel_height = window_box_height (w);
5246 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5247 return make_number (line_height);
5252 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5253 1, 1, "P",
5254 doc: /* Position point relative to window.
5255 With no argument, position point at center of window.
5256 An argument specifies vertical position within the window;
5257 zero means top of window, negative means relative to bottom of window. */)
5258 (arg)
5259 Lisp_Object arg;
5261 struct window *w = XWINDOW (selected_window);
5262 int lines, start;
5263 Lisp_Object window;
5265 window = selected_window;
5266 start = marker_position (w->start);
5267 if (start < BEGV || start > ZV)
5269 int height = window_internal_height (w);
5270 Fvertical_motion (make_number (- (height / 2)), window);
5271 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5272 w->start_at_line_beg = Fbolp ();
5273 w->force_start = Qt;
5275 else
5276 Fgoto_char (w->start);
5278 lines = displayed_window_lines (w);
5279 if (NILP (arg))
5280 XSETFASTINT (arg, lines / 2);
5281 else
5283 arg = Fprefix_numeric_value (arg);
5284 if (XINT (arg) < 0)
5285 XSETINT (arg, XINT (arg) + lines);
5288 /* Skip past a partially visible first line. */
5289 if (w->vscroll)
5290 XSETINT (arg, XINT (arg) + 1);
5292 return Fvertical_motion (arg, window);
5297 /***********************************************************************
5298 Window Configuration
5299 ***********************************************************************/
5301 struct save_window_data
5303 EMACS_INT size_from_Lisp_Vector_struct;
5304 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5305 Lisp_Object frame_cols, frame_lines, frame_menu_bar_lines;
5306 Lisp_Object frame_tool_bar_lines;
5307 Lisp_Object selected_frame;
5308 Lisp_Object current_window;
5309 Lisp_Object current_buffer;
5310 Lisp_Object minibuf_scroll_window;
5311 Lisp_Object minibuf_selected_window;
5312 Lisp_Object root_window;
5313 Lisp_Object focus_frame;
5314 /* Record the values of window-min-width and window-min-height
5315 so that window sizes remain consistent with them. */
5316 Lisp_Object min_width, min_height;
5317 /* A vector, each of whose elements is a struct saved_window
5318 for one window. */
5319 Lisp_Object saved_windows;
5322 /* This is saved as a Lisp_Vector */
5323 struct saved_window
5325 /* these first two must agree with struct Lisp_Vector in lisp.h */
5326 EMACS_INT size_from_Lisp_Vector_struct;
5327 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5329 Lisp_Object window;
5330 Lisp_Object buffer, start, pointm, mark;
5331 Lisp_Object left_col, top_line, total_cols, total_lines;
5332 Lisp_Object hscroll, min_hscroll;
5333 Lisp_Object parent, prev;
5334 Lisp_Object start_at_line_beg;
5335 Lisp_Object display_table;
5336 Lisp_Object orig_top_line, orig_total_lines;
5337 Lisp_Object left_margin_cols, right_margin_cols;
5338 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5339 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5342 #define SAVED_WINDOW_VECTOR_SIZE 24 /* Arg to Fmake_vector */
5344 #define SAVED_WINDOW_N(swv,n) \
5345 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5347 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5348 doc: /* Return t if OBJECT is a window-configuration object. */)
5349 (object)
5350 Lisp_Object object;
5352 if (WINDOW_CONFIGURATIONP (object))
5353 return Qt;
5354 return Qnil;
5357 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5358 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5359 (config)
5360 Lisp_Object config;
5362 register struct save_window_data *data;
5363 struct Lisp_Vector *saved_windows;
5365 if (! WINDOW_CONFIGURATIONP (config))
5366 wrong_type_argument (Qwindow_configuration_p, config);
5368 data = (struct save_window_data *) XVECTOR (config);
5369 saved_windows = XVECTOR (data->saved_windows);
5370 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5373 DEFUN ("set-window-configuration", Fset_window_configuration,
5374 Sset_window_configuration, 1, 1, 0,
5375 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5376 CONFIGURATION must be a value previously returned
5377 by `current-window-configuration' (which see).
5378 If CONFIGURATION was made from a frame that is now deleted,
5379 only frame-independent values can be restored. In this case,
5380 the return value is nil. Otherwise the value is t. */)
5381 (configuration)
5382 Lisp_Object configuration;
5384 register struct save_window_data *data;
5385 struct Lisp_Vector *saved_windows;
5386 Lisp_Object new_current_buffer;
5387 Lisp_Object frame;
5388 FRAME_PTR f;
5389 int old_point = -1;
5391 while (!WINDOW_CONFIGURATIONP (configuration))
5392 wrong_type_argument (Qwindow_configuration_p, configuration);
5394 data = (struct save_window_data *) XVECTOR (configuration);
5395 saved_windows = XVECTOR (data->saved_windows);
5397 new_current_buffer = data->current_buffer;
5398 if (NILP (XBUFFER (new_current_buffer)->name))
5399 new_current_buffer = Qnil;
5400 else
5402 if (XBUFFER (new_current_buffer) == current_buffer)
5403 old_point = PT;
5404 else
5405 old_point = BUF_PT (XBUFFER (new_current_buffer));
5408 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5409 f = XFRAME (frame);
5411 /* If f is a dead frame, don't bother rebuilding its window tree.
5412 However, there is other stuff we should still try to do below. */
5413 if (FRAME_LIVE_P (f))
5415 register struct window *w;
5416 register struct saved_window *p;
5417 struct window *root_window;
5418 struct window **leaf_windows;
5419 int n_leaf_windows;
5420 int k, i, n;
5422 /* If the frame has been resized since this window configuration was
5423 made, we change the frame to the size specified in the
5424 configuration, restore the configuration, and then resize it
5425 back. We keep track of the prevailing height in these variables. */
5426 int previous_frame_lines = FRAME_LINES (f);
5427 int previous_frame_cols = FRAME_COLS (f);
5428 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5429 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5431 /* The mouse highlighting code could get screwed up
5432 if it runs during this. */
5433 BLOCK_INPUT;
5435 if (XFASTINT (data->frame_lines) != previous_frame_lines
5436 || XFASTINT (data->frame_cols) != previous_frame_cols)
5437 change_frame_size (f, XFASTINT (data->frame_lines),
5438 XFASTINT (data->frame_cols), 0, 0, 0);
5439 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5440 if (XFASTINT (data->frame_menu_bar_lines)
5441 != previous_frame_menu_bar_lines)
5442 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
5443 #ifdef HAVE_WINDOW_SYSTEM
5444 if (XFASTINT (data->frame_tool_bar_lines)
5445 != previous_frame_tool_bar_lines)
5446 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
5447 #endif
5448 #endif
5450 /* "Swap out" point from the selected window
5451 into its buffer. We do this now, before
5452 restoring the window contents, and prevent it from
5453 being done later on when we select a new window. */
5454 if (! NILP (XWINDOW (selected_window)->buffer))
5456 w = XWINDOW (selected_window);
5457 set_marker_both (w->pointm,
5458 w->buffer,
5459 BUF_PT (XBUFFER (w->buffer)),
5460 BUF_PT_BYTE (XBUFFER (w->buffer)));
5463 windows_or_buffers_changed++;
5464 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5466 /* Problem: Freeing all matrices and later allocating them again
5467 is a serious redisplay flickering problem. What we would
5468 really like to do is to free only those matrices not reused
5469 below. */
5470 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5471 leaf_windows
5472 = (struct window **) alloca (count_windows (root_window)
5473 * sizeof (struct window *));
5474 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5476 /* Temporarily avoid any problems with windows that are smaller
5477 than they are supposed to be. */
5478 window_min_height = 1;
5479 window_min_width = 1;
5481 /* Kludge Alert!
5482 Mark all windows now on frame as "deleted".
5483 Restoring the new configuration "undeletes" any that are in it.
5485 Save their current buffers in their height fields, since we may
5486 need it later, if a buffer saved in the configuration is now
5487 dead. */
5488 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5490 for (k = 0; k < saved_windows->size; k++)
5492 p = SAVED_WINDOW_N (saved_windows, k);
5493 w = XWINDOW (p->window);
5494 w->next = Qnil;
5496 if (!NILP (p->parent))
5497 w->parent = SAVED_WINDOW_N (saved_windows,
5498 XFASTINT (p->parent))->window;
5499 else
5500 w->parent = Qnil;
5502 if (!NILP (p->prev))
5504 w->prev = SAVED_WINDOW_N (saved_windows,
5505 XFASTINT (p->prev))->window;
5506 XWINDOW (w->prev)->next = p->window;
5508 else
5510 w->prev = Qnil;
5511 if (!NILP (w->parent))
5513 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5515 XWINDOW (w->parent)->vchild = p->window;
5516 XWINDOW (w->parent)->hchild = Qnil;
5518 else
5520 XWINDOW (w->parent)->hchild = p->window;
5521 XWINDOW (w->parent)->vchild = Qnil;
5526 /* If we squirreled away the buffer in the window's height,
5527 restore it now. */
5528 if (BUFFERP (w->total_lines))
5529 w->buffer = w->total_lines;
5530 w->left_col = p->left_col;
5531 w->top_line = p->top_line;
5532 w->total_cols = p->total_cols;
5533 w->total_lines = p->total_lines;
5534 w->hscroll = p->hscroll;
5535 w->min_hscroll = p->min_hscroll;
5536 w->display_table = p->display_table;
5537 w->orig_top_line = p->orig_top_line;
5538 w->orig_total_lines = p->orig_total_lines;
5539 w->left_margin_cols = p->left_margin_cols;
5540 w->right_margin_cols = p->right_margin_cols;
5541 w->left_fringe_width = p->left_fringe_width;
5542 w->right_fringe_width = p->right_fringe_width;
5543 w->fringes_outside_margins = p->fringes_outside_margins;
5544 w->scroll_bar_width = p->scroll_bar_width;
5545 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
5546 XSETFASTINT (w->last_modified, 0);
5547 XSETFASTINT (w->last_overlay_modified, 0);
5549 /* Reinstall the saved buffer and pointers into it. */
5550 if (NILP (p->buffer))
5551 w->buffer = p->buffer;
5552 else
5554 if (!NILP (XBUFFER (p->buffer)->name))
5555 /* If saved buffer is alive, install it. */
5557 w->buffer = p->buffer;
5558 w->start_at_line_beg = p->start_at_line_beg;
5559 set_marker_restricted (w->start, p->start, w->buffer);
5560 set_marker_restricted (w->pointm, p->pointm, w->buffer);
5561 Fset_marker (XBUFFER (w->buffer)->mark,
5562 p->mark, w->buffer);
5564 /* As documented in Fcurrent_window_configuration, don't
5565 restore the location of point in the buffer which was
5566 current when the window configuration was recorded. */
5567 if (!EQ (p->buffer, new_current_buffer)
5568 && XBUFFER (p->buffer) == current_buffer)
5569 Fgoto_char (w->pointm);
5571 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
5572 /* Else unless window has a live buffer, get one. */
5574 w->buffer = Fcdr (Fcar (Vbuffer_alist));
5575 /* This will set the markers to beginning of visible
5576 range. */
5577 set_marker_restricted (w->start, make_number (0), w->buffer);
5578 set_marker_restricted (w->pointm, make_number (0),w->buffer);
5579 w->start_at_line_beg = Qt;
5581 else
5582 /* Keeping window's old buffer; make sure the markers
5583 are real. */
5585 /* Set window markers at start of visible range. */
5586 if (XMARKER (w->start)->buffer == 0)
5587 set_marker_restricted (w->start, make_number (0),
5588 w->buffer);
5589 if (XMARKER (w->pointm)->buffer == 0)
5590 set_marker_restricted_both (w->pointm, w->buffer,
5591 BUF_PT (XBUFFER (w->buffer)),
5592 BUF_PT_BYTE (XBUFFER (w->buffer)));
5593 w->start_at_line_beg = Qt;
5598 FRAME_ROOT_WINDOW (f) = data->root_window;
5599 /* Prevent "swapping out point" in the old selected window
5600 using the buffer that has been restored into it.
5601 Use the point value from the beginning of this function
5602 since unshow_buffer (called from delete_all_subwindows)
5603 could have altered it. */
5604 selected_window = Qnil;
5605 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5606 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5607 make_number (old_point),
5608 XWINDOW (data->current_window)->buffer);
5610 Fselect_window (data->current_window, Qnil);
5611 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
5612 = selected_window;
5614 if (NILP (data->focus_frame)
5615 || (FRAMEP (data->focus_frame)
5616 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5617 Fredirect_frame_focus (frame, data->focus_frame);
5619 #if 0 /* I don't understand why this is needed, and it causes problems
5620 when the frame's old selected window has been deleted. */
5621 if (f != selected_frame && FRAME_WINDOW_P (f))
5622 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
5623 0, 0);
5624 #endif
5626 /* Set the screen height to the value it had before this function. */
5627 if (previous_frame_lines != FRAME_LINES (f)
5628 || previous_frame_cols != FRAME_COLS (f))
5629 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5630 0, 0, 0);
5631 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5632 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5633 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5634 make_number (0));
5635 #ifdef HAVE_WINDOW_SYSTEM
5636 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5637 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5638 make_number (0));
5639 #endif
5640 #endif
5642 /* Now, free glyph matrices in windows that were not reused. */
5643 for (i = n = 0; i < n_leaf_windows; ++i)
5645 if (NILP (leaf_windows[i]->buffer))
5647 /* Assert it's not reused as a combination. */
5648 xassert (NILP (leaf_windows[i]->hchild)
5649 && NILP (leaf_windows[i]->vchild));
5650 free_window_matrices (leaf_windows[i]);
5652 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5653 ++n;
5656 adjust_glyphs (f);
5658 UNBLOCK_INPUT;
5660 /* Fselect_window will have made f the selected frame, so we
5661 reselect the proper frame here. Fhandle_switch_frame will change the
5662 selected window too, but that doesn't make the call to
5663 Fselect_window above totally superfluous; it still sets f's
5664 selected window. */
5665 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5666 do_switch_frame (data->selected_frame, 0, 0);
5668 if (! NILP (Vwindow_configuration_change_hook)
5669 && ! NILP (Vrun_hooks))
5670 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
5673 if (!NILP (new_current_buffer))
5674 Fset_buffer (new_current_buffer);
5676 /* Restore the minimum heights recorded in the configuration. */
5677 window_min_height = XINT (data->min_height);
5678 window_min_width = XINT (data->min_width);
5680 Vminibuf_scroll_window = data->minibuf_scroll_window;
5681 minibuf_selected_window = data->minibuf_selected_window;
5683 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5686 /* Mark all windows now on frame as deleted
5687 by setting their buffers to nil. */
5689 void
5690 delete_all_subwindows (w)
5691 register struct window *w;
5693 if (!NILP (w->next))
5694 delete_all_subwindows (XWINDOW (w->next));
5695 if (!NILP (w->vchild))
5696 delete_all_subwindows (XWINDOW (w->vchild));
5697 if (!NILP (w->hchild))
5698 delete_all_subwindows (XWINDOW (w->hchild));
5700 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
5702 if (!NILP (w->buffer))
5703 unshow_buffer (w);
5705 /* We set all three of these fields to nil, to make sure that we can
5706 distinguish this dead window from any live window. Live leaf
5707 windows will have buffer set, and combination windows will have
5708 vchild or hchild set. */
5709 w->buffer = Qnil;
5710 w->vchild = Qnil;
5711 w->hchild = Qnil;
5713 Vwindow_list = Qnil;
5716 static int
5717 count_windows (window)
5718 register struct window *window;
5720 register int count = 1;
5721 if (!NILP (window->next))
5722 count += count_windows (XWINDOW (window->next));
5723 if (!NILP (window->vchild))
5724 count += count_windows (XWINDOW (window->vchild));
5725 if (!NILP (window->hchild))
5726 count += count_windows (XWINDOW (window->hchild));
5727 return count;
5731 /* Fill vector FLAT with leaf windows under W, starting at index I.
5732 Value is last index + 1. */
5734 static int
5735 get_leaf_windows (w, flat, i)
5736 struct window *w;
5737 struct window **flat;
5738 int i;
5740 while (w)
5742 if (!NILP (w->hchild))
5743 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5744 else if (!NILP (w->vchild))
5745 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5746 else
5747 flat[i++] = w;
5749 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5752 return i;
5756 /* Return a pointer to the glyph W's physical cursor is on. Value is
5757 null if W's current matrix is invalid, so that no meaningfull glyph
5758 can be returned. */
5760 struct glyph *
5761 get_phys_cursor_glyph (w)
5762 struct window *w;
5764 struct glyph_row *row;
5765 struct glyph *glyph;
5767 if (w->phys_cursor.vpos >= 0
5768 && w->phys_cursor.vpos < w->current_matrix->nrows
5769 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
5770 row->enabled_p)
5771 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
5772 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
5773 else
5774 glyph = NULL;
5776 return glyph;
5780 static int
5781 save_window_save (window, vector, i)
5782 Lisp_Object window;
5783 struct Lisp_Vector *vector;
5784 int i;
5786 register struct saved_window *p;
5787 register struct window *w;
5788 register Lisp_Object tem;
5790 for (;!NILP (window); window = w->next)
5792 p = SAVED_WINDOW_N (vector, i);
5793 w = XWINDOW (window);
5795 XSETFASTINT (w->temslot, i++);
5796 p->window = window;
5797 p->buffer = w->buffer;
5798 p->left_col = w->left_col;
5799 p->top_line = w->top_line;
5800 p->total_cols = w->total_cols;
5801 p->total_lines = w->total_lines;
5802 p->hscroll = w->hscroll;
5803 p->min_hscroll = w->min_hscroll;
5804 p->display_table = w->display_table;
5805 p->orig_top_line = w->orig_top_line;
5806 p->orig_total_lines = w->orig_total_lines;
5807 p->left_margin_cols = w->left_margin_cols;
5808 p->right_margin_cols = w->right_margin_cols;
5809 p->left_fringe_width = w->left_fringe_width;
5810 p->right_fringe_width = w->right_fringe_width;
5811 p->fringes_outside_margins = w->fringes_outside_margins;
5812 p->scroll_bar_width = w->scroll_bar_width;
5813 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5814 if (!NILP (w->buffer))
5816 /* Save w's value of point in the window configuration.
5817 If w is the selected window, then get the value of point
5818 from the buffer; pointm is garbage in the selected window. */
5819 if (EQ (window, selected_window))
5821 p->pointm = Fmake_marker ();
5822 set_marker_both (p->pointm, w->buffer,
5823 BUF_PT (XBUFFER (w->buffer)),
5824 BUF_PT_BYTE (XBUFFER (w->buffer)));
5826 else
5827 p->pointm = Fcopy_marker (w->pointm, Qnil);
5829 p->start = Fcopy_marker (w->start, Qnil);
5830 p->start_at_line_beg = w->start_at_line_beg;
5832 tem = XBUFFER (w->buffer)->mark;
5833 p->mark = Fcopy_marker (tem, Qnil);
5835 else
5837 p->pointm = Qnil;
5838 p->start = Qnil;
5839 p->mark = Qnil;
5840 p->start_at_line_beg = Qnil;
5843 if (NILP (w->parent))
5844 p->parent = Qnil;
5845 else
5846 p->parent = XWINDOW (w->parent)->temslot;
5848 if (NILP (w->prev))
5849 p->prev = Qnil;
5850 else
5851 p->prev = XWINDOW (w->prev)->temslot;
5853 if (!NILP (w->vchild))
5854 i = save_window_save (w->vchild, vector, i);
5855 if (!NILP (w->hchild))
5856 i = save_window_save (w->hchild, vector, i);
5859 return i;
5862 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
5863 Scurrent_window_configuration, 0, 1, 0,
5864 doc: /* Return an object representing the current window configuration of FRAME.
5865 If FRAME is nil or omitted, use the selected frame.
5866 This describes the number of windows, their sizes and current buffers,
5867 and for each displayed buffer, where display starts, and the positions of
5868 point and mark. An exception is made for point in the current buffer:
5869 its value is -not- saved.
5870 This also records the currently selected frame, and FRAME's focus
5871 redirection (see `redirect-frame-focus'). */)
5872 (frame)
5873 Lisp_Object frame;
5875 register Lisp_Object tem;
5876 register int n_windows;
5877 register struct save_window_data *data;
5878 register struct Lisp_Vector *vec;
5879 register int i;
5880 FRAME_PTR f;
5882 if (NILP (frame))
5883 frame = selected_frame;
5884 CHECK_LIVE_FRAME (frame);
5885 f = XFRAME (frame);
5887 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5888 vec = allocate_other_vector (VECSIZE (struct save_window_data));
5889 data = (struct save_window_data *)vec;
5891 XSETFASTINT (data->frame_cols, FRAME_COLS (f));
5892 XSETFASTINT (data->frame_lines, FRAME_LINES (f));
5893 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
5894 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
5895 data->selected_frame = selected_frame;
5896 data->current_window = FRAME_SELECTED_WINDOW (f);
5897 XSETBUFFER (data->current_buffer, current_buffer);
5898 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
5899 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
5900 data->root_window = FRAME_ROOT_WINDOW (f);
5901 data->focus_frame = FRAME_FOCUS_FRAME (f);
5902 XSETINT (data->min_height, window_min_height);
5903 XSETINT (data->min_width, window_min_width);
5904 tem = Fmake_vector (make_number (n_windows), Qnil);
5905 data->saved_windows = tem;
5906 for (i = 0; i < n_windows; i++)
5907 XVECTOR (tem)->contents[i]
5908 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
5909 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
5910 XSETWINDOW_CONFIGURATION (tem, data);
5911 return (tem);
5914 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
5915 0, UNEVALLED, 0,
5916 doc: /* Execute BODY, preserving window sizes and contents.
5917 Return the value of the last form in BODY.
5918 Restore which buffer appears in which window, where display starts,
5919 and the value of point and mark for each window.
5920 Also restore the choice of selected window.
5921 Also restore which buffer is current.
5922 Does not restore the value of point in current buffer.
5923 usage: (save-window-excursion BODY ...) */)
5924 (args)
5925 Lisp_Object args;
5927 register Lisp_Object val;
5928 register int count = SPECPDL_INDEX ();
5930 record_unwind_protect (Fset_window_configuration,
5931 Fcurrent_window_configuration (Qnil));
5932 val = Fprogn (args);
5933 return unbind_to (count, val);
5937 /***********************************************************************
5938 Marginal Areas
5939 ***********************************************************************/
5941 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
5942 2, 3, 0,
5943 doc: /* Set width of marginal areas of window WINDOW.
5944 If WINDOW is nil, set margins of the currently selected window.
5945 Second arg LEFT-WIDTH specifies the number of character cells to
5946 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
5947 does the same for the right marginal area. A nil width parameter
5948 means no margin. */)
5949 (window, left, right)
5950 Lisp_Object window, left, right;
5952 struct window *w = decode_window (window);
5954 /* Translate negative or zero widths to nil.
5955 Margins that are too wide have to be checked elsewhere. */
5957 if (!NILP (left))
5959 CHECK_NUMBER (left);
5960 if (XINT (left) <= 0)
5961 left = Qnil;
5964 if (!NILP (right))
5966 CHECK_NUMBER (right);
5967 if (XINT (right) <= 0)
5968 right = Qnil;
5971 if (!EQ (w->left_margin_cols, left)
5972 || !EQ (w->right_margin_cols, right))
5974 w->left_margin_cols = left;
5975 w->right_margin_cols = right;
5977 adjust_window_margins (w);
5979 ++windows_or_buffers_changed;
5980 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
5983 return Qnil;
5987 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
5988 0, 1, 0,
5989 doc: /* Get width of marginal areas of window WINDOW.
5990 If WINDOW is omitted or nil, use the currently selected window.
5991 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
5992 If a marginal area does not exist, its width will be returned
5993 as nil. */)
5994 (window)
5995 Lisp_Object window;
5997 struct window *w = decode_window (window);
5998 return Fcons (w->left_margin_cols, w->right_margin_cols);
6003 /***********************************************************************
6004 Fringes
6005 ***********************************************************************/
6007 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6008 2, 4, 0,
6009 doc: /* Set the fringe widths of window WINDOW.
6010 If WINDOW is nil, set the fringe widths of the currently selected
6011 window.
6012 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6013 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6014 fringe width. If a fringe width arg is nil, that means to use the
6015 frame's default fringe width. Default fringe widths can be set with
6016 the command `set-fringe-style'.
6017 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6018 outside of the display margins. By default, fringes are drawn between
6019 display marginal areas and the text area. */)
6020 (window, left, right, outside_margins)
6021 Lisp_Object window, left, right, outside_margins;
6023 struct window *w = decode_window (window);
6025 if (!NILP (left))
6026 CHECK_NUMBER (left);
6027 if (!NILP (right))
6028 CHECK_NUMBER (right);
6030 if (!EQ (w->left_fringe_width, left)
6031 || !EQ (w->right_fringe_width, right)
6032 || !EQ (w->fringes_outside_margins, outside_margins))
6034 w->left_fringe_width = left;
6035 w->right_fringe_width = right;
6036 w->fringes_outside_margins = outside_margins;
6038 adjust_window_margins (w);
6040 clear_glyph_matrix (w->current_matrix);
6041 w->window_end_valid = Qnil;
6043 ++windows_or_buffers_changed;
6044 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6047 return Qnil;
6051 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6052 0, 1, 0,
6053 doc: /* Get width of fringes of window WINDOW.
6054 If WINDOW is omitted or nil, use the currently selected window.
6055 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6056 (window)
6057 Lisp_Object window;
6059 struct window *w = decode_window (window);
6060 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6061 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6062 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w) ?
6063 Qt : Qnil), Qnil)));
6068 /***********************************************************************
6069 Scroll bars
6070 ***********************************************************************/
6072 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6073 2, 4, 0,
6074 doc: /* Set width and type of scroll bars of window WINDOW.
6075 If window is nil, set scroll bars of the currently selected window.
6076 Second parameter WIDTH specifies the pixel width for the scroll bar;
6077 this is automatically adjusted to a multiple of the frame column width.
6078 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6079 bar: left, right, or nil.
6080 If WIDTH is nil, use the frame's scroll-bar width.
6081 If TYPE is t, use the frame's scroll-bar type. */)
6082 (window, width, vertical_type, horizontal_type)
6083 Lisp_Object window, width, vertical_type, horizontal_type;
6085 struct window *w = decode_window (window);
6087 if (!NILP (width))
6088 CHECK_NUMBER (width);
6090 if (XINT (width) == 0)
6091 vertical_type = Qnil;
6093 if (!(EQ (vertical_type, Qnil)
6094 || EQ (vertical_type, Qleft)
6095 || EQ (vertical_type, Qright)
6096 || EQ (vertical_type, Qt)))
6097 error ("Invalid type of vertical scroll bar");
6099 if (!EQ (w->scroll_bar_width, width)
6100 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6102 w->scroll_bar_width = width;
6103 w->vertical_scroll_bar_type = vertical_type;
6105 adjust_window_margins (w);
6107 clear_glyph_matrix (w->current_matrix);
6108 w->window_end_valid = Qnil;
6110 ++windows_or_buffers_changed;
6111 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6114 return Qnil;
6118 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6119 0, 1, 0,
6120 doc: /* Get width and type of scroll bars of window WINDOW.
6121 If WINDOW is omitted or nil, use the currently selected window.
6122 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6123 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6124 value. */)
6125 (window)
6126 Lisp_Object window;
6128 struct window *w = decode_window (window);
6129 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6130 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6131 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6132 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6133 Fcons (w->vertical_scroll_bar_type,
6134 Fcons (Qnil, Qnil))));
6139 /***********************************************************************
6140 Smooth scrolling
6141 ***********************************************************************/
6143 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6144 doc: /* Return the amount by which WINDOW is scrolled vertically.
6145 Use the selected window if WINDOW is nil or omitted.
6146 Normally, value is a multiple of the canonical character height of WINDOW;
6147 optional second arg PIXELS_P means value is measured in pixels. */)
6148 (window, pixels_p)
6149 Lisp_Object window, pixels_p;
6151 Lisp_Object result;
6152 struct frame *f;
6153 struct window *w;
6155 if (NILP (window))
6156 window = selected_window;
6157 else
6158 CHECK_WINDOW (window);
6159 w = XWINDOW (window);
6160 f = XFRAME (w->frame);
6162 if (FRAME_WINDOW_P (f))
6163 result = (NILP (pixels_p)
6164 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6165 : make_number (-w->vscroll));
6166 else
6167 result = make_number (0);
6168 return result;
6172 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6173 2, 3, 0,
6174 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6175 WINDOW nil means use the selected window. Normally, VSCROLL is a
6176 non-negative multiple of the canonical character height of WINDOW;
6177 optional third arg PIXELS_P non-nil means that VSCROLL is in pixels.
6178 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6179 corresponds to an integral number of pixels. The return value is the
6180 result of this rounding.
6181 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6182 (window, vscroll, pixels_p)
6183 Lisp_Object window, vscroll, pixels_p;
6185 struct window *w;
6186 struct frame *f;
6188 if (NILP (window))
6189 window = selected_window;
6190 else
6191 CHECK_WINDOW (window);
6192 CHECK_NUMBER_OR_FLOAT (vscroll);
6194 w = XWINDOW (window);
6195 f = XFRAME (w->frame);
6197 if (FRAME_WINDOW_P (f))
6199 int old_dy = w->vscroll;
6201 w->vscroll = - (NILP (pixels_p)
6202 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6203 : XFLOATINT (vscroll));
6204 w->vscroll = min (w->vscroll, 0);
6206 /* Adjust glyph matrix of the frame if the virtual display
6207 area becomes larger than before. */
6208 if (w->vscroll < 0 && w->vscroll < old_dy)
6209 adjust_glyphs (f);
6211 /* Prevent redisplay shortcuts. */
6212 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6215 return Fwindow_vscroll (window, pixels_p);
6219 /* Call FN for all leaf windows on frame F. FN is called with the
6220 first argument being a pointer to the leaf window, and with
6221 additional argument USER_DATA. Stops when FN returns 0. */
6223 void
6224 foreach_window (f, fn, user_data)
6225 struct frame *f;
6226 int (* fn) P_ ((struct window *, void *));
6227 void *user_data;
6229 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6233 /* Helper function for foreach_window. Call FN for all leaf windows
6234 reachable from W. FN is called with the first argument being a
6235 pointer to the leaf window, and with additional argument USER_DATA.
6236 Stop when FN returns 0. Value is 0 if stopped by FN. */
6238 static int
6239 foreach_window_1 (w, fn, user_data)
6240 struct window *w;
6241 int (* fn) P_ ((struct window *, void *));
6242 void *user_data;
6244 int cont;
6246 for (cont = 1; w && cont;)
6248 if (!NILP (w->hchild))
6249 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6250 else if (!NILP (w->vchild))
6251 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6252 else
6253 cont = fn (w, user_data);
6255 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6258 return cont;
6262 /* Freeze or unfreeze the window start of W unless it is a
6263 mini-window or the selected window. FREEZE_P non-null means freeze
6264 the window start. */
6266 static int
6267 freeze_window_start (w, freeze_p)
6268 struct window *w;
6269 void *freeze_p;
6271 if (w == XWINDOW (selected_window)
6272 || MINI_WINDOW_P (w)
6273 || (MINI_WINDOW_P (XWINDOW (selected_window))
6274 && ! NILP (Vminibuf_scroll_window)
6275 && w == XWINDOW (Vminibuf_scroll_window)))
6276 freeze_p = NULL;
6278 w->frozen_window_start_p = freeze_p != NULL;
6279 return 1;
6283 /* Freeze or unfreeze the window starts of all leaf windows on frame
6284 F, except the selected window and a mini-window. FREEZE_P non-zero
6285 means freeze the window start. */
6287 void
6288 freeze_window_starts (f, freeze_p)
6289 struct frame *f;
6290 int freeze_p;
6292 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6296 /***********************************************************************
6297 Initialization
6298 ***********************************************************************/
6300 /* Return 1 if window configurations C1 and C2
6301 describe the same state of affairs. This is used by Fequal. */
6304 compare_window_configurations (c1, c2, ignore_positions)
6305 Lisp_Object c1, c2;
6306 int ignore_positions;
6308 register struct save_window_data *d1, *d2;
6309 struct Lisp_Vector *sw1, *sw2;
6310 int i;
6312 if (!WINDOW_CONFIGURATIONP (c1))
6313 wrong_type_argument (Qwindow_configuration_p, c1);
6314 if (!WINDOW_CONFIGURATIONP (c2))
6315 wrong_type_argument (Qwindow_configuration_p, c2);
6317 d1 = (struct save_window_data *) XVECTOR (c1);
6318 d2 = (struct save_window_data *) XVECTOR (c2);
6319 sw1 = XVECTOR (d1->saved_windows);
6320 sw2 = XVECTOR (d2->saved_windows);
6322 if (! EQ (d1->frame_cols, d2->frame_cols))
6323 return 0;
6324 if (! EQ (d1->frame_lines, d2->frame_lines))
6325 return 0;
6326 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
6327 return 0;
6328 if (! EQ (d1->selected_frame, d2->selected_frame))
6329 return 0;
6330 /* Don't compare the current_window field directly.
6331 Instead see w1_is_current and w2_is_current, below. */
6332 if (! EQ (d1->current_buffer, d2->current_buffer))
6333 return 0;
6334 if (! ignore_positions)
6336 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6337 return 0;
6338 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6339 return 0;
6341 /* Don't compare the root_window field.
6342 We don't require the two configurations
6343 to use the same window object,
6344 and the two root windows must be equivalent
6345 if everything else compares equal. */
6346 if (! EQ (d1->focus_frame, d2->focus_frame))
6347 return 0;
6348 if (! EQ (d1->min_width, d2->min_width))
6349 return 0;
6350 if (! EQ (d1->min_height, d2->min_height))
6351 return 0;
6353 /* Verify that the two confis have the same number of windows. */
6354 if (sw1->size != sw2->size)
6355 return 0;
6357 for (i = 0; i < sw1->size; i++)
6359 struct saved_window *p1, *p2;
6360 int w1_is_current, w2_is_current;
6362 p1 = SAVED_WINDOW_N (sw1, i);
6363 p2 = SAVED_WINDOW_N (sw2, i);
6365 /* Verify that the current windows in the two
6366 configurations correspond to each other. */
6367 w1_is_current = EQ (d1->current_window, p1->window);
6368 w2_is_current = EQ (d2->current_window, p2->window);
6370 if (w1_is_current != w2_is_current)
6371 return 0;
6373 /* Verify that the corresponding windows do match. */
6374 if (! EQ (p1->buffer, p2->buffer))
6375 return 0;
6376 if (! EQ (p1->left_col, p2->left_col))
6377 return 0;
6378 if (! EQ (p1->top_line, p2->top_line))
6379 return 0;
6380 if (! EQ (p1->total_cols, p2->total_cols))
6381 return 0;
6382 if (! EQ (p1->total_lines, p2->total_lines))
6383 return 0;
6384 if (! EQ (p1->display_table, p2->display_table))
6385 return 0;
6386 if (! EQ (p1->parent, p2->parent))
6387 return 0;
6388 if (! EQ (p1->prev, p2->prev))
6389 return 0;
6390 if (! ignore_positions)
6392 if (! EQ (p1->hscroll, p2->hscroll))
6393 return 0;
6394 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6395 return 0;
6396 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6397 return 0;
6398 if (NILP (Fequal (p1->start, p2->start)))
6399 return 0;
6400 if (NILP (Fequal (p1->pointm, p2->pointm)))
6401 return 0;
6402 if (NILP (Fequal (p1->mark, p2->mark)))
6403 return 0;
6405 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6406 return 0;
6407 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6408 return 0;
6409 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6410 return 0;
6411 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6412 return 0;
6413 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6414 return 0;
6415 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6416 return 0;
6417 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6418 return 0;
6421 return 1;
6424 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6425 Scompare_window_configurations, 2, 2, 0,
6426 doc: /* Compare two window configurations as regards the structure of windows.
6427 This function ignores details such as the values of point and mark
6428 and scrolling positions. */)
6429 (x, y)
6430 Lisp_Object x, y;
6432 if (compare_window_configurations (x, y, 1))
6433 return Qt;
6434 return Qnil;
6437 void
6438 init_window_once ()
6440 struct frame *f = make_terminal_frame ();
6441 XSETFRAME (selected_frame, f);
6442 Vterminal_frame = selected_frame;
6443 minibuf_window = f->minibuffer_window;
6444 selected_window = f->selected_window;
6445 last_nonminibuf_frame = f;
6447 window_initialized = 1;
6450 void
6451 init_window ()
6453 Vwindow_list = Qnil;
6456 void
6457 syms_of_window ()
6459 Qwindow_size_fixed = intern ("window-size-fixed");
6460 staticpro (&Qwindow_size_fixed);
6462 staticpro (&Qwindow_configuration_change_hook);
6463 Qwindow_configuration_change_hook
6464 = intern ("window-configuration-change-hook");
6466 Qwindowp = intern ("windowp");
6467 staticpro (&Qwindowp);
6469 Qwindow_configuration_p = intern ("window-configuration-p");
6470 staticpro (&Qwindow_configuration_p);
6472 Qwindow_live_p = intern ("window-live-p");
6473 staticpro (&Qwindow_live_p);
6475 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
6476 staticpro (&Qtemp_buffer_show_hook);
6478 staticpro (&Vwindow_list);
6480 minibuf_selected_window = Qnil;
6481 staticpro (&minibuf_selected_window);
6483 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
6484 doc: /* Non-nil means call as function to display a help buffer.
6485 The function is called with one argument, the buffer to be displayed.
6486 Used by `with-output-to-temp-buffer'.
6487 If this function is used, then it must do the entire job of showing
6488 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6489 Vtemp_buffer_show_function = Qnil;
6491 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
6492 doc: /* If non-nil, function to call to handle `display-buffer'.
6493 It will receive two args, the buffer and a flag which if non-nil means
6494 that the currently selected window is not acceptable.
6495 It should choose or create a window, display the specified buffer in it,
6496 and return the window.
6497 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'
6498 work using this function. */);
6499 Vdisplay_buffer_function = Qnil;
6501 DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
6502 doc: /* *If non-nil, `display-buffer' should even the window heights.
6503 If nil, `display-buffer' will leave the window configuration alone. */);
6504 Veven_window_heights = Qt;
6506 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
6507 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6508 Vminibuf_scroll_window = Qnil;
6510 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
6511 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6512 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6513 is displayed in the `mode-line' face. */);
6514 mode_line_in_non_selected_windows = 1;
6516 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
6517 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6518 Vother_window_scroll_buffer = Qnil;
6520 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
6521 doc: /* *Non-nil means `display-buffer' should make a separate frame. */);
6522 pop_up_frames = 0;
6524 DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
6525 doc: /* *Non-nil means `display-buffer' should reuse frames.
6526 If the buffer in question is already displayed in a frame, raise that frame. */);
6527 display_buffer_reuse_frames = 0;
6529 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
6530 doc: /* Function to call to handle automatic new frame creation.
6531 It is called with no arguments and should return a newly created frame.
6533 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
6534 where `pop-up-frame-alist' would hold the default frame parameters. */);
6535 Vpop_up_frame_function = Qnil;
6537 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
6538 doc: /* *List of buffer names that should have their own special frames.
6539 Displaying a buffer whose name is in this list makes a special frame for it
6540 using `special-display-function'. See also `special-display-regexps'.
6542 An element of the list can be a list instead of just a string.
6543 There are two ways to use a list as an element:
6544 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
6545 In the first case, the FRAME-PARAMETERS are pairs of the form
6546 \(PARAMETER . VALUE); these parameter values are used to create the frame.
6547 In the second case, FUNCTION is called with BUFFER as the first argument,
6548 followed by the OTHER-ARGS--it can display BUFFER in any way it likes.
6549 All this is done by the function found in `special-display-function'.
6551 If the specified frame parameters include (same-buffer . t), the
6552 buffer is displayed in the currently selected window. Otherwise, if
6553 they include (same-frame . t), the buffer is displayed in a new window
6554 in the currently selected frame.
6556 If this variable appears \"not to work\", because you add a name to it
6557 but that buffer still appears in the selected window, look at the
6558 values of `same-window-buffer-names' and `same-window-regexps'.
6559 Those variables take precedence over this one. */);
6560 Vspecial_display_buffer_names = Qnil;
6562 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
6563 doc: /* *List of regexps saying which buffers should have their own special frames.
6564 If a buffer name matches one of these regexps, it gets its own frame.
6565 Displaying a buffer whose name is in this list makes a special frame for it
6566 using `special-display-function'.
6568 An element of the list can be a list instead of just a string.
6569 There are two ways to use a list as an element:
6570 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
6571 In the first case, the FRAME-PARAMETERS are pairs of the form
6572 \(PARAMETER . VALUE); these parameter values are used to create the frame.
6573 In the second case, FUNCTION is called with BUFFER as the first argument,
6574 followed by the OTHER-ARGS--it can display the buffer in any way it likes.
6575 All this is done by the function found in `special-display-function'.
6577 If the specified frame parameters include (same-buffer . t), the
6578 buffer is displayed in the currently selected window. Otherwise, if
6579 they include (same-frame . t), the buffer is displayed in a new window
6580 in the currently selected frame.
6582 If this variable appears \"not to work\", because you add a regexp to it
6583 but the matching buffers still appear in the selected window, look at the
6584 values of `same-window-buffer-names' and `same-window-regexps'.
6585 Those variables take precedence over this one. */);
6586 Vspecial_display_regexps = Qnil;
6588 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
6589 doc: /* Function to call to make a new frame for a special buffer.
6590 It is called with two arguments, the buffer and optional buffer specific
6591 data, and should return a window displaying that buffer.
6592 The default value normally makes a separate frame for the buffer,
6593 using `special-display-frame-alist' to specify the frame parameters.
6594 But if the buffer specific data includes (same-buffer . t) then the
6595 buffer is displayed in the current selected window.
6596 Otherwise if it includes (same-frame . t) then the buffer is displayed in
6597 a new window in the currently selected frame.
6599 A buffer is special if it is listed in `special-display-buffer-names'
6600 or matches a regexp in `special-display-regexps'. */);
6601 Vspecial_display_function = Qnil;
6603 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
6604 doc: /* *List of buffer names that should appear in the selected window.
6605 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
6606 switches to it in the selected window, rather than making it appear
6607 in some other window.
6609 An element of the list can be a cons cell instead of just a string.
6610 Then the car must be a string, which specifies the buffer name.
6611 This is for compatibility with `special-display-buffer-names';
6612 the cdr of the cons cell is ignored.
6614 See also `same-window-regexps'. */);
6615 Vsame_window_buffer_names = Qnil;
6617 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
6618 doc: /* *List of regexps saying which buffers should appear in the selected window.
6619 If a buffer name matches one of these regexps, then displaying it
6620 using `display-buffer' or `pop-to-buffer' switches to it
6621 in the selected window, rather than making it appear in some other window.
6623 An element of the list can be a cons cell instead of just a string.
6624 Then the car must be a string, which specifies the buffer name.
6625 This is for compatibility with `special-display-buffer-names';
6626 the cdr of the cons cell is ignored.
6628 See also `same-window-buffer-names'. */);
6629 Vsame_window_regexps = Qnil;
6631 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
6632 doc: /* *Non-nil means display-buffer should make new windows. */);
6633 pop_up_windows = 1;
6635 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
6636 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
6637 next_screen_context_lines = 2;
6639 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
6640 doc: /* *A window must be at least this tall to be eligible for splitting by `display-buffer'.
6641 If there is only one window, it is split regardless of this value. */);
6642 split_height_threshold = 500;
6644 DEFVAR_INT ("window-min-height", &window_min_height,
6645 doc: /* *Delete any window less than this tall (including its mode line). */);
6646 window_min_height = 4;
6648 DEFVAR_INT ("window-min-width", &window_min_width,
6649 doc: /* *Delete any window less than this wide. */);
6650 window_min_width = 10;
6652 DEFVAR_LISP ("scroll-preserve-screen-position",
6653 &Vscroll_preserve_screen_position,
6654 doc: /* *Non-nil means scroll commands move point to keep its screen line unchanged. */);
6655 Vscroll_preserve_screen_position = Qnil;
6657 DEFVAR_LISP ("window-configuration-change-hook",
6658 &Vwindow_configuration_change_hook,
6659 doc: /* Functions to call when window configuration changes.
6660 The selected frame is the one whose configuration has changed. */);
6661 Vwindow_configuration_change_hook = Qnil;
6663 DEFVAR_BOOL ("window-size-fixed", &window_size_fixed,
6664 doc: /* Non-nil in a buffer means windows displaying the buffer are fixed-size.
6665 If the value is`height', then only the window's height is fixed.
6666 If the value is `width', then only the window's width is fixed.
6667 Any other non-nil value fixes both the width and the height.
6668 Emacs won't change the size of any window displaying that buffer,
6669 unless you explicitly change the size, or Emacs has no other choice. */);
6670 Fmake_variable_buffer_local (Qwindow_size_fixed);
6671 window_size_fixed = 0;
6673 defsubr (&Sselected_window);
6674 defsubr (&Sminibuffer_window);
6675 defsubr (&Swindow_minibuffer_p);
6676 defsubr (&Swindowp);
6677 defsubr (&Swindow_live_p);
6678 defsubr (&Spos_visible_in_window_p);
6679 defsubr (&Swindow_buffer);
6680 defsubr (&Swindow_height);
6681 defsubr (&Swindow_width);
6682 defsubr (&Swindow_hscroll);
6683 defsubr (&Sset_window_hscroll);
6684 defsubr (&Swindow_redisplay_end_trigger);
6685 defsubr (&Sset_window_redisplay_end_trigger);
6686 defsubr (&Swindow_edges);
6687 defsubr (&Swindow_pixel_edges);
6688 defsubr (&Swindow_inside_edges);
6689 defsubr (&Swindow_inside_pixel_edges);
6690 defsubr (&Scoordinates_in_window_p);
6691 defsubr (&Swindow_at);
6692 defsubr (&Swindow_point);
6693 defsubr (&Swindow_start);
6694 defsubr (&Swindow_end);
6695 defsubr (&Sset_window_point);
6696 defsubr (&Sset_window_start);
6697 defsubr (&Swindow_dedicated_p);
6698 defsubr (&Sset_window_dedicated_p);
6699 defsubr (&Swindow_display_table);
6700 defsubr (&Sset_window_display_table);
6701 defsubr (&Snext_window);
6702 defsubr (&Sprevious_window);
6703 defsubr (&Sother_window);
6704 defsubr (&Sget_lru_window);
6705 defsubr (&Sget_largest_window);
6706 defsubr (&Sget_buffer_window);
6707 defsubr (&Sdelete_other_windows);
6708 defsubr (&Sdelete_windows_on);
6709 defsubr (&Sreplace_buffer_in_windows);
6710 defsubr (&Sdelete_window);
6711 defsubr (&Sset_window_buffer);
6712 defsubr (&Sselect_window);
6713 defsubr (&Sspecial_display_p);
6714 defsubr (&Ssame_window_p);
6715 defsubr (&Sdisplay_buffer);
6716 defsubr (&Sforce_window_update);
6717 defsubr (&Ssplit_window);
6718 defsubr (&Senlarge_window);
6719 defsubr (&Sshrink_window);
6720 defsubr (&Sscroll_up);
6721 defsubr (&Sscroll_down);
6722 defsubr (&Sscroll_left);
6723 defsubr (&Sscroll_right);
6724 defsubr (&Sother_window_for_scrolling);
6725 defsubr (&Sscroll_other_window);
6726 defsubr (&Sminibuffer_selected_window);
6727 defsubr (&Srecenter);
6728 defsubr (&Swindow_text_height);
6729 defsubr (&Smove_to_window_line);
6730 defsubr (&Swindow_configuration_p);
6731 defsubr (&Swindow_configuration_frame);
6732 defsubr (&Sset_window_configuration);
6733 defsubr (&Scurrent_window_configuration);
6734 defsubr (&Ssave_window_excursion);
6735 defsubr (&Sset_window_margins);
6736 defsubr (&Swindow_margins);
6737 defsubr (&Sset_window_fringes);
6738 defsubr (&Swindow_fringes);
6739 defsubr (&Sset_window_scroll_bars);
6740 defsubr (&Swindow_scroll_bars);
6741 defsubr (&Swindow_vscroll);
6742 defsubr (&Sset_window_vscroll);
6743 defsubr (&Scompare_window_configurations);
6744 defsubr (&Swindow_list);
6747 void
6748 keys_of_window ()
6750 initial_define_key (control_x_map, '1', "delete-other-windows");
6751 initial_define_key (control_x_map, '2', "split-window");
6752 initial_define_key (control_x_map, '0', "delete-window");
6753 initial_define_key (control_x_map, 'o', "other-window");
6754 initial_define_key (control_x_map, '^', "enlarge-window");
6755 initial_define_key (control_x_map, '<', "scroll-left");
6756 initial_define_key (control_x_map, '>', "scroll-right");
6758 initial_define_key (global_map, Ctl ('V'), "scroll-up");
6759 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6760 initial_define_key (meta_map, 'v', "scroll-down");
6762 initial_define_key (global_map, Ctl('L'), "recenter");
6763 initial_define_key (meta_map, 'r', "move-to-window-line");
6766 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
6767 (do not change this comment) */