Change default values.
[emacs.git] / src / window.c
blob24a95dc53a1074a5a9affceeac20c79a8fa0981a
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985,86,87,93,94,95,96,97,1998,2000, 2001, 2002
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
51 /* Values returned from coordinates_in_window. */
53 enum window_part
55 ON_NOTHING,
56 ON_TEXT,
57 ON_MODE_LINE,
58 ON_VERTICAL_BORDER,
59 ON_HEADER_LINE,
60 ON_LEFT_FRINGE,
61 ON_RIGHT_FRINGE,
62 ON_LEFT_MARGIN,
63 ON_RIGHT_MARGIN
67 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
68 Lisp_Object Qwindow_size_fixed;
69 extern Lisp_Object Qleft_margin, Qright_margin;
70 extern Lisp_Object Qheight, Qwidth;
72 static int displayed_window_lines P_ ((struct window *));
73 static struct window *decode_window P_ ((Lisp_Object));
74 static Lisp_Object select_window_1 P_ ((Lisp_Object, int));
75 static int count_windows P_ ((struct window *));
76 static int get_leaf_windows P_ ((struct window *, struct window **, int));
77 static void window_scroll P_ ((Lisp_Object, int, int, int));
78 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
79 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
80 static int window_min_size_1 P_ ((struct window *, int));
81 static int window_min_size P_ ((struct window *, int, int, int *));
82 static void size_window P_ ((Lisp_Object, int, int, int));
83 static int freeze_window_start P_ ((struct window *, void *));
84 static int window_fixed_size_p P_ ((struct window *, int, int));
85 static void enlarge_window P_ ((Lisp_Object, int, int, int));
86 static Lisp_Object window_list P_ ((void));
87 static int add_window_to_list P_ ((struct window *, void *));
88 static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
89 Lisp_Object));
90 static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
91 Lisp_Object, int));
92 static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
93 Lisp_Object *));
94 static int foreach_window_1 P_ ((struct window *,
95 int (* fn) (struct window *, void *),
96 void *));
97 static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
99 /* The value of `window-size-fixed'. */
101 int window_size_fixed;
103 /* This is the window in which the terminal's cursor should
104 be left when nothing is being done with it. This must
105 always be a leaf window, and its buffer is selected by
106 the top level editing loop at the end of each command.
108 This value is always the same as
109 FRAME_SELECTED_WINDOW (selected_frame). */
111 Lisp_Object selected_window;
113 /* A list of all windows for use by next_window and Fwindow_list.
114 Functions creating or deleting windows should invalidate this cache
115 by setting it to nil. */
117 Lisp_Object Vwindow_list;
119 /* The mini-buffer window of the selected frame.
120 Note that you cannot test for mini-bufferness of an arbitrary window
121 by comparing against this; but you can test for mini-bufferness of
122 the selected window. */
124 Lisp_Object minibuf_window;
126 /* Non-nil means it is the window whose mode line should be
127 shown as the selected window when the minibuffer is selected. */
129 Lisp_Object minibuf_selected_window;
131 /* Non-nil means it is the window for C-M-v to scroll
132 when the mini-buffer is selected. */
134 Lisp_Object Vminibuf_scroll_window;
136 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
138 Lisp_Object Vother_window_scroll_buffer;
140 /* Non-nil means it's function to call to display temp buffers. */
142 Lisp_Object Vtemp_buffer_show_function;
144 /* Non-zero means to use mode-line-inactive face in all windows but the
145 selected-window and the minibuffer-scroll-window when the
146 minibuffer is active. */
147 int mode_line_in_non_selected_windows;
149 /* If a window gets smaller than either of these, it is removed. */
151 EMACS_INT window_min_height;
152 EMACS_INT window_min_width;
154 /* Nonzero implies Fdisplay_buffer should create windows. */
156 int pop_up_windows;
158 /* Nonzero implies make new frames for Fdisplay_buffer. */
160 int pop_up_frames;
162 /* Nonzero means reuse existing frames for displaying buffers. */
164 int display_buffer_reuse_frames;
166 /* Non-nil means use this function instead of default */
168 Lisp_Object Vpop_up_frame_function;
170 /* Function to call to handle Fdisplay_buffer. */
172 Lisp_Object Vdisplay_buffer_function;
174 /* Non-nil means that Fdisplay_buffer should even the heights of windows. */
176 Lisp_Object Veven_window_heights;
178 /* List of buffer *names* for buffers that should have their own frames. */
180 Lisp_Object Vspecial_display_buffer_names;
182 /* List of regexps for buffer names that should have their own frames. */
184 Lisp_Object Vspecial_display_regexps;
186 /* Function to pop up a special frame. */
188 Lisp_Object Vspecial_display_function;
190 /* List of buffer *names* for buffers to appear in selected window. */
192 Lisp_Object Vsame_window_buffer_names;
194 /* List of regexps for buffer names to appear in selected window. */
196 Lisp_Object Vsame_window_regexps;
198 /* Hook run at end of temp_output_buffer_show. */
200 Lisp_Object Qtemp_buffer_show_hook;
202 /* Fdisplay_buffer always splits the largest window
203 if that window is more than this high. */
205 EMACS_INT split_height_threshold;
207 /* Number of lines of continuity in scrolling by screenfuls. */
209 EMACS_INT next_screen_context_lines;
211 /* Incremented for each window created. */
213 static int sequence_number;
215 /* Nonzero after init_window_once has finished. */
217 static int window_initialized;
219 /* Hook to run when window config changes. */
221 Lisp_Object Qwindow_configuration_change_hook;
222 Lisp_Object Vwindow_configuration_change_hook;
224 /* Nonzero means scroll commands try to put point
225 at the same screen height as previously. */
227 Lisp_Object Vscroll_preserve_screen_position;
229 #if 0 /* This isn't used anywhere. */
230 /* Nonzero means we can split a frame even if it is "unsplittable". */
231 static int inhibit_frame_unsplittable;
232 #endif /* 0 */
234 extern EMACS_INT scroll_margin;
236 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
238 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
239 doc: /* Returns t if OBJECT is a window. */)
240 (object)
241 Lisp_Object object;
243 return WINDOWP (object) ? Qt : Qnil;
246 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
247 doc: /* Returns t if OBJECT is a window which is currently visible. */)
248 (object)
249 Lisp_Object object;
251 return WINDOW_LIVE_P (object) ? Qt : Qnil;
254 Lisp_Object
255 make_window ()
257 Lisp_Object val;
258 register struct window *p;
260 p = allocate_window ();
261 XSETFASTINT (p->sequence_number, ++sequence_number);
262 XSETFASTINT (p->left, 0);
263 XSETFASTINT (p->top, 0);
264 XSETFASTINT (p->height, 0);
265 XSETFASTINT (p->width, 0);
266 XSETFASTINT (p->hscroll, 0);
267 XSETFASTINT (p->min_hscroll, 0);
268 p->orig_top = p->orig_height = Qnil;
269 p->start = Fmake_marker ();
270 p->pointm = Fmake_marker ();
271 XSETFASTINT (p->use_time, 0);
272 p->frame = Qnil;
273 p->display_table = Qnil;
274 p->dedicated = Qnil;
275 p->pseudo_window_p = 0;
276 bzero (&p->cursor, sizeof (p->cursor));
277 bzero (&p->last_cursor, sizeof (p->last_cursor));
278 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
279 p->desired_matrix = p->current_matrix = 0;
280 p->phys_cursor_type = -1;
281 p->phys_cursor_width = -1;
282 p->must_be_updated_p = 0;
283 XSETFASTINT (p->window_end_vpos, 0);
284 XSETFASTINT (p->window_end_pos, 0);
285 p->window_end_valid = Qnil;
286 p->vscroll = 0;
287 XSETWINDOW (val, p);
288 XSETFASTINT (p->last_point, 0);
289 p->frozen_window_start_p = 0;
290 p->height_fixed_p = 0;
291 p->last_cursor_off_p = p->cursor_off_p = 0;
293 Vwindow_list = Qnil;
294 return val;
297 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
298 doc: /* Return the window that the cursor now appears in and commands apply to. */)
301 return selected_window;
304 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
305 doc: /* Return the window used now for minibuffers.
306 If the optional argument FRAME is specified, return the minibuffer window
307 used by that frame. */)
308 (frame)
309 Lisp_Object frame;
311 if (NILP (frame))
312 frame = selected_frame;
313 CHECK_LIVE_FRAME (frame);
314 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
317 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
318 doc: /* Returns non-nil if WINDOW is a minibuffer window. */)
319 (window)
320 Lisp_Object window;
322 struct window *w = decode_window (window);
323 return MINI_WINDOW_P (w) ? Qt : Qnil;
327 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
328 Spos_visible_in_window_p, 0, 3, 0,
329 doc: /* Return t if position POS is currently on the frame in WINDOW.
330 Return nil if that position is scrolled vertically out of view.
331 If a character is only partially visible, nil is returned, unless the
332 optional argument PARTIALLY is non-nil.
333 POS defaults to point in WINDOW; WINDOW defaults to the selected window. */)
334 (pos, window, partially)
335 Lisp_Object pos, window, partially;
337 register struct window *w;
338 register int posint;
339 register struct buffer *buf;
340 struct text_pos top;
341 Lisp_Object in_window;
342 int fully_p;
344 w = decode_window (window);
345 buf = XBUFFER (w->buffer);
346 SET_TEXT_POS_FROM_MARKER (top, w->start);
348 if (!NILP (pos))
350 CHECK_NUMBER_COERCE_MARKER (pos);
351 posint = XINT (pos);
353 else if (w == XWINDOW (selected_window))
354 posint = PT;
355 else
356 posint = XMARKER (w->pointm)->charpos;
358 /* If position is above window start, it's not visible. */
359 if (posint < CHARPOS (top))
360 in_window = Qnil;
361 else if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
362 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf)
363 && posint < BUF_Z (buf) - XFASTINT (w->window_end_pos))
365 /* If frame is up-to-date, and POSINT is < window end pos, use
366 that info. This doesn't work for POSINT == end pos, because
367 the window end pos is actually the position _after_ the last
368 char in the window. */
369 if (NILP (partially))
371 pos_visible_p (w, posint, &fully_p, NILP (partially));
372 in_window = fully_p ? Qt : Qnil;
374 else
375 in_window = Qt;
377 else if (posint > BUF_ZV (buf))
378 in_window = Qnil;
379 else if (CHARPOS (top) < BUF_BEGV (buf) || CHARPOS (top) > BUF_ZV (buf))
380 /* If window start is out of range, do something reasonable. */
381 in_window = Qnil;
382 else
384 if (pos_visible_p (w, posint, &fully_p, NILP (partially)))
385 in_window = !NILP (partially) || fully_p ? Qt : Qnil;
386 else
387 in_window = Qnil;
390 return in_window;
394 static struct window *
395 decode_window (window)
396 register Lisp_Object window;
398 if (NILP (window))
399 return XWINDOW (selected_window);
401 CHECK_LIVE_WINDOW (window);
402 return XWINDOW (window);
405 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
406 doc: /* Return the buffer that WINDOW is displaying. */)
407 (window)
408 Lisp_Object window;
410 return decode_window (window)->buffer;
413 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
414 doc: /* Return the number of lines in WINDOW (including its mode line). */)
415 (window)
416 Lisp_Object window;
418 return decode_window (window)->height;
421 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
422 doc: /* Return the number of display columns in WINDOW.
423 This is the width that is usable columns available for text in WINDOW.
424 If you want to find out how many columns WINDOW takes up,
425 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
426 (window)
427 Lisp_Object window;
429 return make_number (window_internal_width (decode_window (window)));
432 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
433 doc: /* Return the number of columns by which WINDOW is scrolled from left margin. */)
434 (window)
435 Lisp_Object window;
437 return decode_window (window)->hscroll;
440 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
441 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
442 NCOL should be zero or positive. */)
443 (window, ncol)
444 Lisp_Object window, ncol;
446 struct window *w = decode_window (window);
447 int hscroll;
449 CHECK_NUMBER (ncol);
450 hscroll = max (0, XINT (ncol));
452 /* Prevent redisplay shortcuts when changing the hscroll. */
453 if (XINT (w->hscroll) != hscroll)
454 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
456 w->hscroll = make_number (hscroll);
457 return ncol;
460 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
461 Swindow_redisplay_end_trigger, 0, 1, 0,
462 doc: /* Return WINDOW's redisplay end trigger value.
463 See `set-window-redisplay-end-trigger' for more information. */)
464 (window)
465 Lisp_Object window;
467 return decode_window (window)->redisplay_end_trigger;
470 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
471 Sset_window_redisplay_end_trigger, 2, 2, 0,
472 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
473 VALUE should be a buffer position (typically a marker) or nil.
474 If it is a buffer position, then if redisplay in WINDOW reaches a position
475 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
476 with two arguments: WINDOW, and the end trigger value.
477 Afterwards the end-trigger value is reset to nil. */)
478 (window, value)
479 register Lisp_Object window, value;
481 register struct window *w;
483 w = decode_window (window);
484 w->redisplay_end_trigger = value;
485 return value;
488 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
489 doc: /* Return a list of the edge coordinates of WINDOW.
490 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.
491 RIGHT is one more than the rightmost column used by WINDOW,
492 and BOTTOM is one more than the bottommost row used by WINDOW
493 and its mode-line. */)
494 (window)
495 Lisp_Object window;
497 register struct window *w = decode_window (window);
499 return Fcons (w->left, Fcons (w->top,
500 Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
501 Fcons (make_number (XFASTINT (w->top)
502 + XFASTINT (w->height)),
503 Qnil))));
506 /* Test if the character at column *X, row *Y is within window W.
507 If it is not, return 0;
508 if it is in the window's text area,
509 set *x and *y to its location relative to the upper left corner
510 of the window, and
511 return 1;
512 if it is on the window's modeline, return 2;
513 if it is on the border between the window and its right sibling,
514 return 3.
515 if it is on the window's top line, return 4;
516 if it is in left or right fringe of the window,
517 return 5 or 6, and convert *X and *Y to window-relative coordinates;
518 if it is in the marginal area to the left/right of the window,
519 return 7 or 8, and convert *X and *Y to window-relative coordinates.
521 X and Y are frame relative pixel coordinates. */
523 static enum window_part
524 coordinates_in_window (w, x, y)
525 register struct window *w;
526 register int *x, *y;
528 /* Let's make this a global enum later, instead of using numbers
529 everywhere. */
530 struct frame *f = XFRAME (WINDOW_FRAME (w));
531 int left_x, right_x, top_y, bottom_y;
532 enum window_part part;
533 int ux = CANON_X_UNIT (f);
534 int x0 = XFASTINT (w->left) * ux;
535 int x1 = x0 + XFASTINT (w->width) * ux;
536 /* The width of the area where the vertical line can be dragged.
537 (Between mode lines for instance. */
538 int grabbable_width = ux;
539 int lmargin_width = 0, rmargin_width = 0;
541 if (*x < x0 || *x >= x1)
542 return ON_NOTHING;
544 /* In what's below, we subtract 1 when computing right_x because we
545 want the rightmost pixel, which is given by left_pixel+width-1. */
546 if (w->pseudo_window_p)
548 left_x = 0;
549 right_x = XFASTINT (w->width) * CANON_X_UNIT (f) - 1;
550 top_y = WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
551 bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
553 else
555 left_x = (WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w)
556 - FRAME_INTERNAL_BORDER_WIDTH_SAFE (f));
557 right_x = WINDOW_DISPLAY_RIGHT_EDGE_PIXEL_X (w) - 1;
558 top_y = (WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w)
559 - FRAME_INTERNAL_BORDER_WIDTH_SAFE (f));
560 bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
563 /* On the mode line or header line? If it's near the start of
564 the mode or header line of window that's has a horizontal
565 sibling, say it's on the vertical line. That's to be able
566 to resize windows horizontally in case we're using toolkit
567 scroll bars. */
569 if (WINDOW_WANTS_MODELINE_P (w)
570 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
571 && *y < bottom_y)
573 /* We're somewhere on the mode line. We consider the place
574 between mode lines of horizontally adjacent mode lines
575 as the vertical border. If scroll bars on the left,
576 return the right window. */
577 part = ON_MODE_LINE;
579 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
581 if (abs (*x - x0) < grabbable_width)
582 part = ON_VERTICAL_BORDER;
584 else if (!WINDOW_RIGHTMOST_P (w) && abs (*x - x1) < grabbable_width)
585 part = ON_VERTICAL_BORDER;
587 else if (WINDOW_WANTS_HEADER_LINE_P (w)
588 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
589 && *y >= top_y)
591 part = ON_HEADER_LINE;
593 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
595 if (abs (*x - x0) < grabbable_width)
596 part = ON_VERTICAL_BORDER;
598 else if (!WINDOW_RIGHTMOST_P (w) && abs (*x - x1) < grabbable_width)
599 part = ON_VERTICAL_BORDER;
601 /* Outside anything interesting? */
602 else if (*y < top_y
603 || *y >= bottom_y
604 || *x < (left_x
605 - FRAME_LEFT_FRINGE_WIDTH (f)
606 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * ux)
607 || *x > (right_x
608 + FRAME_RIGHT_FRINGE_WIDTH (f)
609 + FRAME_RIGHT_SCROLL_BAR_WIDTH (f) * ux))
611 part = ON_NOTHING;
613 else if (FRAME_WINDOW_P (f))
615 if (!w->pseudo_window_p
616 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f)
617 && !WINDOW_RIGHTMOST_P (w)
618 && (abs (*x - right_x - FRAME_RIGHT_FRINGE_WIDTH (f)) < grabbable_width))
620 part = ON_VERTICAL_BORDER;
622 else if (*x < left_x || *x > right_x)
624 /* Other lines than the mode line don't include fringes and
625 scroll bars on the left. */
627 /* Convert X and Y to window-relative pixel coordinates. */
628 *x -= left_x;
629 *y -= top_y;
630 part = *x < left_x ? ON_LEFT_FRINGE : ON_RIGHT_FRINGE;
632 else
634 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
635 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
636 /* You can never be on a margin area if its width is zero. */
637 if (lmargin_width
638 && *x <= window_box_right (w, LEFT_MARGIN_AREA))
639 part = ON_LEFT_MARGIN;
640 else if (rmargin_width
641 && *x >= window_box_left (w, RIGHT_MARGIN_AREA))
642 part = ON_RIGHT_MARGIN;
643 else
645 part = ON_TEXT;
646 *x -= left_x;
647 *y -= top_y;
651 else
653 /* Need to say "*x > right_x" rather than >=, since on character
654 terminals, the vertical line's x coordinate is right_x. */
655 if (*x < left_x || *x > right_x)
657 /* Other lines than the mode line don't include fringes and
658 scroll bars on the left. */
660 /* Convert X and Y to window-relative pixel coordinates. */
661 *x -= left_x;
662 *y -= top_y;
663 part = *x < left_x ? ON_LEFT_FRINGE : ON_RIGHT_FRINGE;
665 /* Here, too, "*x > right_x" is because of character terminals. */
666 else if (!w->pseudo_window_p
667 && !WINDOW_RIGHTMOST_P (w)
668 && *x > right_x - ux)
670 /* On the border on the right side of the window? Assume that
671 this area begins at RIGHT_X minus a canonical char width. */
672 part = ON_VERTICAL_BORDER;
674 else
676 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
677 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
678 /* You can never be on a margin area if its width is zero.
679 This is especially important for character terminals. */
680 if (lmargin_width
681 && *x <= window_box_right (w, LEFT_MARGIN_AREA))
682 part = ON_LEFT_MARGIN;
683 else if (rmargin_width
684 && *x >= window_box_left (w, RIGHT_MARGIN_AREA))
685 part = ON_RIGHT_MARGIN;
686 else
688 part = ON_TEXT;
689 /* Convert X and Y to window-relative pixel coordinates. */
690 *x -= left_x;
691 *y -= top_y;
696 return part;
700 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
701 Scoordinates_in_window_p, 2, 2, 0,
702 doc: /* Return non-nil if COORDINATES are in WINDOW.
703 COORDINATES is a cons of the form (X . Y), X and Y being distances
704 measured in characters from the upper-left corner of the frame.
705 \(0 . 0) denotes the character in the upper left corner of the
706 frame.
707 If COORDINATES are in the text portion of WINDOW,
708 the coordinates relative to the window are returned.
709 If they are in the mode line of WINDOW, `mode-line' is returned.
710 If they are in the top mode line of WINDOW, `header-line' is returned.
711 If they are in the left fringe of WINDOW, `left-fringe' is returned.
712 If they are in the right fringe of WINDOW, `right-fringe' is returned.
713 If they are on the border between WINDOW and its right sibling,
714 `vertical-line' is returned.
715 If they are in the windows's left or right marginal areas, `left-margin'\n\
716 or `right-margin' is returned. */)
717 (coordinates, window)
718 register Lisp_Object coordinates, window;
720 struct window *w;
721 struct frame *f;
722 int x, y;
723 Lisp_Object lx, ly;
725 CHECK_LIVE_WINDOW (window);
726 w = XWINDOW (window);
727 f = XFRAME (w->frame);
728 CHECK_CONS (coordinates);
729 lx = Fcar (coordinates);
730 ly = Fcdr (coordinates);
731 CHECK_NUMBER_OR_FLOAT (lx);
732 CHECK_NUMBER_OR_FLOAT (ly);
733 x = PIXEL_X_FROM_CANON_X (f, lx);
734 y = PIXEL_Y_FROM_CANON_Y (f, ly);
736 switch (coordinates_in_window (w, &x, &y))
738 case ON_NOTHING:
739 return Qnil;
741 case ON_TEXT:
742 /* X and Y are now window relative pixel coordinates. Convert
743 them to canonical char units before returning them. */
744 return Fcons (CANON_X_FROM_PIXEL_X (f, x),
745 CANON_Y_FROM_PIXEL_Y (f, y));
747 case ON_MODE_LINE:
748 return Qmode_line;
750 case ON_VERTICAL_BORDER:
751 return Qvertical_line;
753 case ON_HEADER_LINE:
754 return Qheader_line;
756 case ON_LEFT_FRINGE:
757 return Qleft_fringe;
759 case ON_RIGHT_FRINGE:
760 return Qright_fringe;
762 case ON_LEFT_MARGIN:
763 return Qleft_margin;
765 case ON_RIGHT_MARGIN:
766 return Qright_margin;
768 default:
769 abort ();
774 /* Callback for foreach_window, used in window_from_coordinates.
775 Check if window W contains coordinates specified by USER_DATA which
776 is actually a pointer to a struct check_window_data CW.
778 Check if window W contains coordinates *CW->x and *CW->y. If it
779 does, return W in *CW->window, as Lisp_Object, and return in
780 *CW->part the part of the window under coordinates *X,*Y. Return
781 zero from this function to stop iterating over windows. */
783 struct check_window_data
785 Lisp_Object *window;
786 int *x, *y, *part;
789 static int
790 check_window_containing (w, user_data)
791 struct window *w;
792 void *user_data;
794 struct check_window_data *cw = (struct check_window_data *) user_data;
795 enum window_part found;
796 int continue_p = 1;
798 found = coordinates_in_window (w, cw->x, cw->y);
799 if (found != ON_NOTHING)
801 *cw->part = found - 1;
802 XSETWINDOW (*cw->window, w);
803 continue_p = 0;
806 return continue_p;
810 /* Find the window containing frame-relative pixel position X/Y and
811 return it as a Lisp_Object. If X, Y is on the window's modeline,
812 set *PART to 1; if it is on the separating line between the window
813 and its right sibling, set it to 2; otherwise set it to 0. If
814 there is no window under X, Y return nil and leave *PART
815 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
817 This function was previously implemented with a loop cycling over
818 windows with Fnext_window, and starting with the frame's selected
819 window. It turned out that this doesn't work with an
820 implementation of next_window using Vwindow_list, because
821 FRAME_SELECTED_WINDOW (F) is not always contained in the window
822 tree of F when this function is called asynchronously from
823 note_mouse_highlight. The original loop didn't terminate in this
824 case. */
826 Lisp_Object
827 window_from_coordinates (f, x, y, part, tool_bar_p)
828 struct frame *f;
829 int x, y;
830 int *part;
831 int tool_bar_p;
833 Lisp_Object window;
834 struct check_window_data cw;
836 window = Qnil;
837 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
838 foreach_window (f, check_window_containing, &cw);
840 /* If not found above, see if it's in the tool bar window, if a tool
841 bar exists. */
842 if (NILP (window)
843 && tool_bar_p
844 && WINDOWP (f->tool_bar_window)
845 && XINT (XWINDOW (f->tool_bar_window)->height) > 0
846 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
847 != ON_NOTHING))
849 *part = 0;
850 window = f->tool_bar_window;
853 return window;
856 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
857 doc: /* Return window containing coordinates X and Y on FRAME.
858 If omitted, FRAME defaults to the currently selected frame.
859 The top left corner of the frame is considered to be row 0,
860 column 0. */)
861 (x, y, frame)
862 Lisp_Object x, y, frame;
864 int part;
865 struct frame *f;
867 if (NILP (frame))
868 frame = selected_frame;
869 CHECK_LIVE_FRAME (frame);
870 f = XFRAME (frame);
872 /* Check that arguments are integers or floats. */
873 CHECK_NUMBER_OR_FLOAT (x);
874 CHECK_NUMBER_OR_FLOAT (y);
876 return window_from_coordinates (f,
877 PIXEL_X_FROM_CANON_X (f, x),
878 PIXEL_Y_FROM_CANON_Y (f, y),
879 &part, 0);
882 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
883 doc: /* Return current value of point in WINDOW.
884 For a nonselected window, this is the value point would have
885 if that window were selected.
887 Note that, when WINDOW is the selected window and its buffer
888 is also currently selected, the value returned is the same as (point).
889 It would be more strictly correct to return the `top-level' value
890 of point, outside of any save-excursion forms.
891 But that is hard to define. */)
892 (window)
893 Lisp_Object window;
895 register struct window *w = decode_window (window);
897 if (w == XWINDOW (selected_window)
898 && current_buffer == XBUFFER (w->buffer))
899 return Fpoint ();
900 return Fmarker_position (w->pointm);
903 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
904 doc: /* Return position at which display currently starts in WINDOW.
905 This is updated by redisplay or by calling `set-window-start'. */)
906 (window)
907 Lisp_Object window;
909 return Fmarker_position (decode_window (window)->start);
912 /* This is text temporarily removed from the doc string below.
914 This function returns nil if the position is not currently known.
915 That happens when redisplay is preempted and doesn't finish.
916 If in that case you want to compute where the end of the window would
917 have been if redisplay had finished, do this:
918 (save-excursion
919 (goto-char (window-start window))
920 (vertical-motion (1- (window-height window)) window)
921 (point))") */
923 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
924 doc: /* Return position at which display currently ends in WINDOW.
925 This is updated by redisplay, when it runs to completion.
926 Simply changing the buffer text or setting `window-start'
927 does not update this value.
928 If UPDATE is non-nil, compute the up-to-date position
929 if it isn't already recorded. */)
930 (window, update)
931 Lisp_Object window, update;
933 Lisp_Object value;
934 struct window *w = decode_window (window);
935 Lisp_Object buf;
937 buf = w->buffer;
938 CHECK_BUFFER (buf);
940 #if 0 /* This change broke some things. We should make it later. */
941 /* If we don't know the end position, return nil.
942 The user can compute it with vertical-motion if he wants to.
943 It would be nicer to do it automatically,
944 but that's so slow that it would probably bother people. */
945 if (NILP (w->window_end_valid))
946 return Qnil;
947 #endif
949 if (! NILP (update)
950 && ! (! NILP (w->window_end_valid)
951 && XFASTINT (w->last_modified) >= MODIFF))
953 struct text_pos startp;
954 struct it it;
955 struct buffer *old_buffer = NULL, *b = XBUFFER (buf);
957 /* In case W->start is out of the range, use something
958 reasonable. This situation occured when loading a file with
959 `-l' containing a call to `rmail' with subsequent other
960 commands. At the end, W->start happened to be BEG, while
961 rmail had already narrowed the buffer. */
962 if (XMARKER (w->start)->charpos < BEGV)
963 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
964 else if (XMARKER (w->start)->charpos > ZV)
965 SET_TEXT_POS (startp, ZV, ZV_BYTE);
966 else
967 SET_TEXT_POS_FROM_MARKER (startp, w->start);
969 /* Cannot use Fvertical_motion because that function doesn't
970 cope with variable-height lines. */
971 if (b != current_buffer)
973 old_buffer = current_buffer;
974 set_buffer_internal (b);
977 start_display (&it, w, startp);
978 move_it_vertically (&it, window_box_height (w));
979 if (it.current_y < it.last_visible_y)
980 move_it_past_eol (&it);
981 value = make_number (IT_CHARPOS (it));
983 if (old_buffer)
984 set_buffer_internal (old_buffer);
986 else
987 XSETINT (value, BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
989 return value;
992 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
993 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer. */)
994 (window, pos)
995 Lisp_Object window, pos;
997 register struct window *w = decode_window (window);
999 CHECK_NUMBER_COERCE_MARKER (pos);
1000 if (w == XWINDOW (selected_window)
1001 && XBUFFER (w->buffer) == current_buffer)
1002 Fgoto_char (pos);
1003 else
1004 set_marker_restricted (w->pointm, pos, w->buffer);
1006 /* We have to make sure that redisplay updates the window to show
1007 the new value of point. */
1008 if (!EQ (window, selected_window))
1009 ++windows_or_buffers_changed;
1011 return pos;
1014 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1015 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1016 Optional third arg NOFORCE non-nil inhibits next redisplay
1017 from overriding motion of point in order to display at this exact start. */)
1018 (window, pos, noforce)
1019 Lisp_Object window, pos, noforce;
1021 register struct window *w = decode_window (window);
1023 CHECK_NUMBER_COERCE_MARKER (pos);
1024 set_marker_restricted (w->start, pos, w->buffer);
1025 /* this is not right, but much easier than doing what is right. */
1026 w->start_at_line_beg = Qnil;
1027 if (NILP (noforce))
1028 w->force_start = Qt;
1029 w->update_mode_line = Qt;
1030 XSETFASTINT (w->last_modified, 0);
1031 XSETFASTINT (w->last_overlay_modified, 0);
1032 if (!EQ (window, selected_window))
1033 windows_or_buffers_changed++;
1035 return pos;
1038 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1039 1, 1, 0,
1040 doc: /* Return WINDOW's dedicated object, usually t or nil.
1041 See also `set-window-dedicated-p'. */)
1042 (window)
1043 Lisp_Object window;
1045 return decode_window (window)->dedicated;
1048 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1049 Sset_window_dedicated_p, 2, 2, 0,
1050 doc: /* Control whether WINDOW is dedicated to the buffer it displays.
1051 If it is dedicated, Emacs will not automatically change
1052 which buffer appears in it.
1053 The second argument is the new value for the dedication flag;
1054 non-nil means yes. */)
1055 (window, arg)
1056 Lisp_Object window, arg;
1058 register struct window *w = decode_window (window);
1060 if (NILP (arg))
1061 w->dedicated = Qnil;
1062 else
1063 w->dedicated = Qt;
1065 return w->dedicated;
1068 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1069 0, 1, 0,
1070 doc: /* Return the display-table that WINDOW is using. */)
1071 (window)
1072 Lisp_Object window;
1074 return decode_window (window)->display_table;
1077 /* Get the display table for use on window W. This is either W's
1078 display table or W's buffer's display table. Ignore the specified
1079 tables if they are not valid; if no valid table is specified,
1080 return 0. */
1082 struct Lisp_Char_Table *
1083 window_display_table (w)
1084 struct window *w;
1086 struct Lisp_Char_Table *dp = NULL;
1088 if (DISP_TABLE_P (w->display_table))
1089 dp = XCHAR_TABLE (w->display_table);
1090 else if (BUFFERP (w->buffer))
1092 struct buffer *b = XBUFFER (w->buffer);
1094 if (DISP_TABLE_P (b->display_table))
1095 dp = XCHAR_TABLE (b->display_table);
1096 else if (DISP_TABLE_P (Vstandard_display_table))
1097 dp = XCHAR_TABLE (Vstandard_display_table);
1100 return dp;
1103 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1104 doc: /* Set WINDOW's display-table to TABLE. */)
1105 (window, table)
1106 register Lisp_Object window, table;
1108 register struct window *w;
1110 w = decode_window (window);
1111 w->display_table = table;
1112 return table;
1115 /* Record info on buffer window w is displaying
1116 when it is about to cease to display that buffer. */
1117 static void
1118 unshow_buffer (w)
1119 register struct window *w;
1121 Lisp_Object buf;
1122 struct buffer *b;
1124 buf = w->buffer;
1125 b = XBUFFER (buf);
1126 if (b != XMARKER (w->pointm)->buffer)
1127 abort ();
1129 #if 0
1130 if (w == XWINDOW (selected_window)
1131 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1132 /* Do this except when the selected window's buffer
1133 is being removed from some other window. */
1134 #endif
1135 /* last_window_start records the start position that this buffer
1136 had in the last window to be disconnected from it.
1137 Now that this statement is unconditional,
1138 it is possible for the buffer to be displayed in the
1139 selected window, while last_window_start reflects another
1140 window which was recently showing the same buffer.
1141 Some people might say that might be a good thing. Let's see. */
1142 b->last_window_start = marker_position (w->start);
1144 /* Point in the selected window's buffer
1145 is actually stored in that buffer, and the window's pointm isn't used.
1146 So don't clobber point in that buffer. */
1147 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1148 /* This line helps to fix Horsley's testbug.el bug. */
1149 && !(WINDOWP (b->last_selected_window)
1150 && w != XWINDOW (b->last_selected_window)
1151 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1152 temp_set_point_both (b,
1153 clip_to_bounds (BUF_BEGV (b),
1154 XMARKER (w->pointm)->charpos,
1155 BUF_ZV (b)),
1156 clip_to_bounds (BUF_BEGV_BYTE (b),
1157 marker_byte_position (w->pointm),
1158 BUF_ZV_BYTE (b)));
1160 if (WINDOWP (b->last_selected_window)
1161 && w == XWINDOW (b->last_selected_window))
1162 b->last_selected_window = Qnil;
1165 /* Put replacement into the window structure in place of old. */
1166 static void
1167 replace_window (old, replacement)
1168 Lisp_Object old, replacement;
1170 register Lisp_Object tem;
1171 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1173 /* If OLD is its frame's root_window, then replacement is the new
1174 root_window for that frame. */
1176 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1177 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1179 p->left = o->left;
1180 p->top = o->top;
1181 p->width = o->width;
1182 p->height = o->height;
1183 p->desired_matrix = p->current_matrix = 0;
1184 p->vscroll = 0;
1185 bzero (&p->cursor, sizeof (p->cursor));
1186 bzero (&p->last_cursor, sizeof (p->last_cursor));
1187 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1188 p->phys_cursor_type = -1;
1189 p->phys_cursor_width = -1;
1190 p->must_be_updated_p = 0;
1191 p->pseudo_window_p = 0;
1192 XSETFASTINT (p->window_end_vpos, 0);
1193 XSETFASTINT (p->window_end_pos, 0);
1194 p->window_end_valid = Qnil;
1195 p->frozen_window_start_p = 0;
1196 p->orig_top = p->orig_height = Qnil;
1198 p->next = tem = o->next;
1199 if (!NILP (tem))
1200 XWINDOW (tem)->prev = replacement;
1202 p->prev = tem = o->prev;
1203 if (!NILP (tem))
1204 XWINDOW (tem)->next = replacement;
1206 p->parent = tem = o->parent;
1207 if (!NILP (tem))
1209 if (EQ (XWINDOW (tem)->vchild, old))
1210 XWINDOW (tem)->vchild = replacement;
1211 if (EQ (XWINDOW (tem)->hchild, old))
1212 XWINDOW (tem)->hchild = replacement;
1215 /*** Here, if replacement is a vertical combination
1216 and so is its new parent, we should make replacement's
1217 children be children of that parent instead. ***/
1220 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1221 doc: /* Remove WINDOW from the display. Default is selected window. */)
1222 (window)
1223 register Lisp_Object window;
1225 delete_window (window);
1227 if (! NILP (Vwindow_configuration_change_hook)
1228 && ! NILP (Vrun_hooks))
1229 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1231 return Qnil;
1234 void
1235 delete_window (window)
1236 register Lisp_Object window;
1238 register Lisp_Object tem, parent, sib;
1239 register struct window *p;
1240 register struct window *par;
1241 struct frame *f;
1243 /* Because this function is called by other C code on non-leaf
1244 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1245 so we can't decode_window here. */
1246 if (NILP (window))
1247 window = selected_window;
1248 else
1249 CHECK_WINDOW (window);
1250 p = XWINDOW (window);
1252 /* It's okay to delete an already-deleted window. */
1253 if (NILP (p->buffer)
1254 && NILP (p->hchild)
1255 && NILP (p->vchild))
1256 return;
1258 parent = p->parent;
1259 if (NILP (parent))
1260 error ("Attempt to delete minibuffer or sole ordinary window");
1261 par = XWINDOW (parent);
1263 windows_or_buffers_changed++;
1264 Vwindow_list = Qnil;
1265 f = XFRAME (WINDOW_FRAME (p));
1266 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1268 /* Are we trying to delete any frame's selected window? */
1270 Lisp_Object swindow, pwindow;
1272 /* See if the frame's selected window is either WINDOW
1273 or any subwindow of it, by finding all that window's parents
1274 and comparing each one with WINDOW. */
1275 swindow = FRAME_SELECTED_WINDOW (f);
1277 while (1)
1279 pwindow = swindow;
1280 while (!NILP (pwindow))
1282 if (EQ (window, pwindow))
1283 break;
1284 pwindow = XWINDOW (pwindow)->parent;
1287 /* If the window being deleted is not a parent of SWINDOW,
1288 then SWINDOW is ok as the new selected window. */
1289 if (!EQ (window, pwindow))
1290 break;
1291 /* Otherwise, try another window for SWINDOW. */
1292 swindow = Fnext_window (swindow, Qlambda, Qnil);;
1294 /* If we get back to the frame's selected window,
1295 it means there was no acceptable alternative,
1296 so we cannot delete. */
1297 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1298 error ("Cannot delete window");
1301 /* If we need to change SWINDOW, do it. */
1302 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1304 /* If we're about to delete the selected window on the
1305 selected frame, then we should use Fselect_window to select
1306 the new window. On the other hand, if we're about to
1307 delete the selected window on any other frame, we shouldn't do
1308 anything but set the frame's selected_window slot. */
1309 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1310 Fselect_window (swindow);
1311 else
1312 FRAME_SELECTED_WINDOW (f) = swindow;
1316 tem = p->buffer;
1317 /* tem is null for dummy parent windows
1318 (which have inferiors but not any contents themselves) */
1319 if (!NILP (tem))
1321 unshow_buffer (p);
1322 unchain_marker (p->pointm);
1323 unchain_marker (p->start);
1326 /* Free window glyph matrices. It is sure that they are allocated
1327 again when ADJUST_GLYPHS is called. Block input so that expose
1328 events and other events that access glyph matrices are not
1329 processed while we are changing them. */
1330 BLOCK_INPUT;
1331 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1333 tem = p->next;
1334 if (!NILP (tem))
1335 XWINDOW (tem)->prev = p->prev;
1337 tem = p->prev;
1338 if (!NILP (tem))
1339 XWINDOW (tem)->next = p->next;
1341 if (EQ (window, par->hchild))
1342 par->hchild = p->next;
1343 if (EQ (window, par->vchild))
1344 par->vchild = p->next;
1346 /* Find one of our siblings to give our space to. */
1347 sib = p->prev;
1348 if (NILP (sib))
1350 /* If p gives its space to its next sibling, that sibling needs
1351 to have its top/left side pulled back to where p's is.
1352 set_window_{height,width} will re-position the sibling's
1353 children. */
1354 sib = p->next;
1355 XWINDOW (sib)->top = p->top;
1356 XWINDOW (sib)->left = p->left;
1359 /* Stretch that sibling. */
1360 if (!NILP (par->vchild))
1361 set_window_height (sib,
1362 XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
1364 if (!NILP (par->hchild))
1365 set_window_width (sib,
1366 XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
1369 /* If parent now has only one child,
1370 put the child into the parent's place. */
1371 tem = par->hchild;
1372 if (NILP (tem))
1373 tem = par->vchild;
1374 if (NILP (XWINDOW (tem)->next))
1375 replace_window (parent, tem);
1377 /* Since we may be deleting combination windows, we must make sure that
1378 not only p but all its children have been marked as deleted. */
1379 if (! NILP (p->hchild))
1380 delete_all_subwindows (XWINDOW (p->hchild));
1381 else if (! NILP (p->vchild))
1382 delete_all_subwindows (XWINDOW (p->vchild));
1384 /* Mark this window as deleted. */
1385 p->buffer = p->hchild = p->vchild = Qnil;
1387 /* Adjust glyph matrices. */
1388 adjust_glyphs (f);
1389 UNBLOCK_INPUT;
1394 /***********************************************************************
1395 Window List
1396 ***********************************************************************/
1398 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1399 pointer. This is a callback function for foreach_window, used in
1400 function window_list. */
1402 static int
1403 add_window_to_list (w, user_data)
1404 struct window *w;
1405 void *user_data;
1407 Lisp_Object *list = (Lisp_Object *) user_data;
1408 Lisp_Object window;
1409 XSETWINDOW (window, w);
1410 *list = Fcons (window, *list);
1411 return 1;
1415 /* Return a list of all windows, for use by next_window. If
1416 Vwindow_list is a list, return that list. Otherwise, build a new
1417 list, cache it in Vwindow_list, and return that. */
1419 static Lisp_Object
1420 window_list ()
1422 if (!CONSP (Vwindow_list))
1424 Lisp_Object tail;
1426 Vwindow_list = Qnil;
1427 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1429 Lisp_Object args[2];
1431 /* We are visiting windows in canonical order, and add
1432 new windows at the front of args[1], which means we
1433 have to reverse this list at the end. */
1434 args[1] = Qnil;
1435 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1436 args[0] = Vwindow_list;
1437 args[1] = Fnreverse (args[1]);
1438 Vwindow_list = Fnconc (2, args);
1442 return Vwindow_list;
1446 /* Value is non-zero if WINDOW satisfies the constraints given by
1447 OWINDOW, MINIBUF and ALL_FRAMES.
1449 MINIBUF t means WINDOW may be minibuffer windows.
1450 `lambda' means WINDOW may not be a minibuffer window.
1451 a window means a specific minibuffer window
1453 ALL_FRAMES t means search all frames,
1454 nil means search just current frame,
1455 `visible' means search just visible frames,
1456 0 means search visible and iconified frames,
1457 a window means search the frame that window belongs to,
1458 a frame means consider windows on that frame, only. */
1460 static int
1461 candidate_window_p (window, owindow, minibuf, all_frames)
1462 Lisp_Object window, owindow, minibuf, all_frames;
1464 struct window *w = XWINDOW (window);
1465 struct frame *f = XFRAME (w->frame);
1466 int candidate_p = 1;
1468 if (!BUFFERP (w->buffer))
1469 candidate_p = 0;
1470 else if (MINI_WINDOW_P (w)
1471 && (EQ (minibuf, Qlambda)
1472 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1474 /* If MINIBUF is `lambda' don't consider any mini-windows.
1475 If it is a window, consider only that one. */
1476 candidate_p = 0;
1478 else if (EQ (all_frames, Qt))
1479 candidate_p = 1;
1480 else if (NILP (all_frames))
1482 xassert (WINDOWP (owindow));
1483 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1485 else if (EQ (all_frames, Qvisible))
1487 FRAME_SAMPLE_VISIBILITY (f);
1488 candidate_p = FRAME_VISIBLE_P (f);
1490 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1492 FRAME_SAMPLE_VISIBILITY (f);
1493 candidate_p = FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f);
1495 else if (WINDOWP (all_frames))
1496 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1497 || EQ (XWINDOW (all_frames)->frame, w->frame)
1498 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1499 else if (FRAMEP (all_frames))
1500 candidate_p = EQ (all_frames, w->frame);
1502 return candidate_p;
1506 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1507 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1508 ALL_FRAMES. */
1510 static void
1511 decode_next_window_args (window, minibuf, all_frames)
1512 Lisp_Object *window, *minibuf, *all_frames;
1514 if (NILP (*window))
1515 *window = selected_window;
1516 else
1517 CHECK_LIVE_WINDOW (*window);
1519 /* MINIBUF nil may or may not include minibuffers. Decide if it
1520 does. */
1521 if (NILP (*minibuf))
1522 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1523 else if (!EQ (*minibuf, Qt))
1524 *minibuf = Qlambda;
1526 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1527 => count none of them, or a specific minibuffer window (the
1528 active one) to count. */
1530 /* ALL_FRAMES nil doesn't specify which frames to include. */
1531 if (NILP (*all_frames))
1532 *all_frames = (!EQ (*minibuf, Qlambda)
1533 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1534 : Qnil);
1535 else if (EQ (*all_frames, Qvisible))
1537 else if (XFASTINT (*all_frames) == 0)
1539 else if (FRAMEP (*all_frames))
1541 else if (!EQ (*all_frames, Qt))
1542 *all_frames = Qnil;
1544 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1545 search just current frame, `visible' meaning search just visible
1546 frames, 0 meaning search visible and iconified frames, or a
1547 window, meaning search the frame that window belongs to, or a
1548 frame, meaning consider windows on that frame, only. */
1552 /* Return the next or previous window of WINDOW in canonical ordering
1553 of windows. NEXT_P non-zero means return the next window. See the
1554 documentation string of next-window for the meaning of MINIBUF and
1555 ALL_FRAMES. */
1557 static Lisp_Object
1558 next_window (window, minibuf, all_frames, next_p)
1559 Lisp_Object window, minibuf, all_frames;
1560 int next_p;
1562 decode_next_window_args (&window, &minibuf, &all_frames);
1564 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1565 return the first window on the frame. */
1566 if (FRAMEP (all_frames)
1567 && !EQ (all_frames, XWINDOW (window)->frame))
1568 return Fframe_first_window (all_frames);
1570 if (next_p)
1572 Lisp_Object list;
1574 /* Find WINDOW in the list of all windows. */
1575 list = Fmemq (window, window_list ());
1577 /* Scan forward from WINDOW to the end of the window list. */
1578 if (CONSP (list))
1579 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1580 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1581 break;
1583 /* Scan from the start of the window list up to WINDOW. */
1584 if (!CONSP (list))
1585 for (list = Vwindow_list;
1586 CONSP (list) && !EQ (XCAR (list), window);
1587 list = XCDR (list))
1588 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1589 break;
1591 if (CONSP (list))
1592 window = XCAR (list);
1594 else
1596 Lisp_Object candidate, list;
1598 /* Scan through the list of windows for candidates. If there are
1599 candidate windows in front of WINDOW, the last one of these
1600 is the one we want. If there are candidates following WINDOW
1601 in the list, again the last one of these is the one we want. */
1602 candidate = Qnil;
1603 for (list = window_list (); CONSP (list); list = XCDR (list))
1605 if (EQ (XCAR (list), window))
1607 if (WINDOWP (candidate))
1608 break;
1610 else if (candidate_window_p (XCAR (list), window, minibuf,
1611 all_frames))
1612 candidate = XCAR (list);
1615 if (WINDOWP (candidate))
1616 window = candidate;
1619 return window;
1623 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1624 doc: /* Return next window after WINDOW in canonical ordering of windows.
1625 If omitted, WINDOW defaults to the selected window.
1627 Optional second arg MINIBUF t means count the minibuffer window even
1628 if not active. MINIBUF nil or omitted means count the minibuffer iff
1629 it is active. MINIBUF neither t nor nil means not to count the
1630 minibuffer even if it is active.
1632 Several frames may share a single minibuffer; if the minibuffer
1633 counts, all windows on all frames that share that minibuffer count
1634 too. Therefore, `next-window' can be used to iterate through the
1635 set of windows even when the minibuffer is on another frame. If the
1636 minibuffer does not count, only windows from WINDOW's frame count.
1638 Optional third arg ALL-FRAMES t means include windows on all frames.
1639 ALL-FRAMES nil or omitted means cycle within the frames as specified
1640 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1641 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1642 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1643 Anything else means restrict to WINDOW's frame.
1645 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1646 `next-window' to iterate through the entire cycle of acceptable
1647 windows, eventually ending up back at the window you started with.
1648 `previous-window' traverses the same cycle, in the reverse order. */)
1649 (window, minibuf, all_frames)
1650 Lisp_Object window, minibuf, all_frames;
1652 return next_window (window, minibuf, all_frames, 1);
1656 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1657 doc: /* Return the window preceding WINDOW in canonical ordering of windows.
1658 If omitted, WINDOW defaults to the selected window.
1660 Optional second arg MINIBUF t means count the minibuffer window even
1661 if not active. MINIBUF nil or omitted means count the minibuffer iff
1662 it is active. MINIBUF neither t nor nil means not to count the
1663 minibuffer even if it is active.
1665 Several frames may share a single minibuffer; if the minibuffer
1666 counts, all windows on all frames that share that minibuffer count
1667 too. Therefore, `previous-window' can be used to iterate through
1668 the set of windows even when the minibuffer is on another frame. If
1669 the minibuffer does not count, only windows from WINDOW's frame count
1671 Optional third arg ALL-FRAMES t means include windows on all frames.
1672 ALL-FRAMES nil or omitted means cycle within the frames as specified
1673 above. ALL-FRAMES = `visible' means include windows on all visible frames.
1674 ALL-FRAMES = 0 means include windows on all visible and iconified frames.
1675 If ALL-FRAMES is a frame, restrict search to windows on that frame.
1676 Anything else means restrict to WINDOW's frame.
1678 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1679 `previous-window' to iterate through the entire cycle of acceptable
1680 windows, eventually ending up back at the window you started with.
1681 `next-window' traverses the same cycle, in the reverse order. */)
1682 (window, minibuf, all_frames)
1683 Lisp_Object window, minibuf, all_frames;
1685 return next_window (window, minibuf, all_frames, 0);
1689 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1690 doc: /* Select the ARG'th different window on this frame.
1691 All windows on current frame are arranged in a cyclic order.
1692 This command selects the window ARG steps away in that order.
1693 A negative ARG moves in the opposite order. If the optional second
1694 argument ALL_FRAMES is non-nil, cycle through all frames. */)
1695 (arg, all_frames)
1696 Lisp_Object arg, all_frames;
1698 Lisp_Object window;
1699 int i;
1701 CHECK_NUMBER (arg);
1702 window = selected_window;
1704 for (i = XINT (arg); i > 0; --i)
1705 window = Fnext_window (window, Qnil, all_frames);
1706 for (; i < 0; ++i)
1707 window = Fprevious_window (window, Qnil, all_frames);
1709 Fselect_window (window);
1710 return Qnil;
1714 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
1715 doc: /* Return a list of windows on FRAME, starting with WINDOW.
1716 FRAME nil or omitted means use the selected frame.
1717 WINDOW nil or omitted means use the selected window.
1718 MINIBUF t means include the minibuffer window, even if it isn't active.
1719 MINIBUF nil or omitted means include the minibuffer window only
1720 if it's active.
1721 MINIBUF neither nil nor t means never include the minibuffer window. */)
1722 (frame, minibuf, window)
1723 Lisp_Object frame, minibuf, window;
1725 if (NILP (window))
1726 window = selected_window;
1727 if (NILP (frame))
1728 frame = selected_frame;
1730 if (!EQ (frame, XWINDOW (window)->frame))
1731 error ("Window is on a different frame");
1733 return window_list_1 (window, minibuf, frame);
1737 /* Return a list of windows in canonical ordering. Arguments are like
1738 for `next-window'. */
1740 static Lisp_Object
1741 window_list_1 (window, minibuf, all_frames)
1742 Lisp_Object window, minibuf, all_frames;
1744 Lisp_Object tail, list;
1746 decode_next_window_args (&window, &minibuf, &all_frames);
1747 list = Qnil;
1749 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
1750 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
1751 list = Fcons (XCAR (tail), list);
1753 return Fnreverse (list);
1758 /* Look at all windows, performing an operation specified by TYPE
1759 with argument OBJ.
1760 If FRAMES is Qt, look at all frames;
1761 Qnil, look at just the selected frame;
1762 Qvisible, look at visible frames;
1763 a frame, just look at windows on that frame.
1764 If MINI is non-zero, perform the operation on minibuffer windows too. */
1766 enum window_loop
1768 WINDOW_LOOP_UNUSED,
1769 GET_BUFFER_WINDOW, /* Arg is buffer */
1770 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1771 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1772 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1773 GET_LARGEST_WINDOW,
1774 UNSHOW_BUFFER, /* Arg is buffer */
1775 CHECK_ALL_WINDOWS
1778 static Lisp_Object
1779 window_loop (type, obj, mini, frames)
1780 enum window_loop type;
1781 Lisp_Object obj, frames;
1782 int mini;
1784 Lisp_Object window, windows, best_window, frame_arg;
1785 struct frame *f;
1786 struct gcpro gcpro1;
1788 /* If we're only looping through windows on a particular frame,
1789 frame points to that frame. If we're looping through windows
1790 on all frames, frame is 0. */
1791 if (FRAMEP (frames))
1792 f = XFRAME (frames);
1793 else if (NILP (frames))
1794 f = SELECTED_FRAME ();
1795 else
1796 f = NULL;
1798 if (f)
1799 frame_arg = Qlambda;
1800 else if (XFASTINT (frames) == 0)
1801 frame_arg = frames;
1802 else if (EQ (frames, Qvisible))
1803 frame_arg = frames;
1804 else
1805 frame_arg = Qt;
1807 /* frame_arg is Qlambda to stick to one frame,
1808 Qvisible to consider all visible frames,
1809 or Qt otherwise. */
1811 /* Pick a window to start with. */
1812 if (WINDOWP (obj))
1813 window = obj;
1814 else if (f)
1815 window = FRAME_SELECTED_WINDOW (f);
1816 else
1817 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
1819 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
1820 GCPRO1 (windows);
1821 best_window = Qnil;
1823 for (; CONSP (windows); windows = CDR (windows))
1825 struct window *w;
1827 window = XCAR (windows);
1828 w = XWINDOW (window);
1830 /* Note that we do not pay attention here to whether the frame
1831 is visible, since Fwindow_list skips non-visible frames if
1832 that is desired, under the control of frame_arg. */
1833 if (!MINI_WINDOW_P (w)
1834 /* For UNSHOW_BUFFER, we must always consider all windows. */
1835 || type == UNSHOW_BUFFER
1836 || (mini && minibuf_level > 0))
1837 switch (type)
1839 case GET_BUFFER_WINDOW:
1840 if (EQ (w->buffer, obj)
1841 /* Don't find any minibuffer window
1842 except the one that is currently in use. */
1843 && (MINI_WINDOW_P (w)
1844 ? EQ (window, minibuf_window)
1845 : 1))
1847 if (NILP (best_window))
1848 best_window = window;
1849 else if (EQ (window, selected_window))
1850 /* For compatibility with 20.x, prefer to return
1851 selected-window. */
1852 best_window = window;
1854 break;
1856 case GET_LRU_WINDOW:
1857 /* t as arg means consider only full-width windows */
1858 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (w))
1859 break;
1860 /* Ignore dedicated windows and minibuffers. */
1861 if (MINI_WINDOW_P (w) || !NILP (w->dedicated))
1862 break;
1863 if (NILP (best_window)
1864 || (XFASTINT (XWINDOW (best_window)->use_time)
1865 > XFASTINT (w->use_time)))
1866 best_window = window;
1867 break;
1869 case DELETE_OTHER_WINDOWS:
1870 if (!EQ (window, obj))
1871 Fdelete_window (window);
1872 break;
1874 case DELETE_BUFFER_WINDOWS:
1875 if (EQ (w->buffer, obj))
1877 struct frame *f = XFRAME (WINDOW_FRAME (w));
1879 /* If this window is dedicated, and in a frame of its own,
1880 kill the frame. */
1881 if (EQ (window, FRAME_ROOT_WINDOW (f))
1882 && !NILP (w->dedicated)
1883 && other_visible_frames (f))
1885 /* Skip the other windows on this frame.
1886 There might be one, the minibuffer! */
1887 while (CONSP (XCDR (windows))
1888 && EQ (XWINDOW (XCAR (windows))->frame,
1889 XWINDOW (XCAR (XCDR (windows)))->frame))
1890 windows = XCDR (windows);
1892 /* Now we can safely delete the frame. */
1893 Fdelete_frame (w->frame, Qnil);
1895 else if (NILP (w->parent))
1897 /* If we're deleting the buffer displayed in the
1898 only window on the frame, find a new buffer to
1899 display there. */
1900 Lisp_Object buffer;
1901 buffer = Fother_buffer (obj, Qnil, w->frame);
1902 Fset_window_buffer (window, buffer);
1903 if (EQ (window, selected_window))
1904 Fset_buffer (w->buffer);
1906 else
1907 Fdelete_window (window);
1909 break;
1911 case GET_LARGEST_WINDOW:
1913 /* Ignore dedicated windows and minibuffers. */
1914 if (MINI_WINDOW_P (w) || !NILP (w->dedicated))
1915 break;
1917 if (NILP (best_window))
1918 best_window = window;
1919 else
1921 struct window *b = XWINDOW (best_window);
1922 if (XFASTINT (w->height) * XFASTINT (w->width)
1923 > XFASTINT (b->height) * XFASTINT (b->width))
1924 best_window = window;
1927 break;
1929 case UNSHOW_BUFFER:
1930 if (EQ (w->buffer, obj))
1932 Lisp_Object buffer;
1933 struct frame *f = XFRAME (w->frame);
1935 /* Find another buffer to show in this window. */
1936 buffer = Fother_buffer (obj, Qnil, w->frame);
1938 /* If this window is dedicated, and in a frame of its own,
1939 kill the frame. */
1940 if (EQ (window, FRAME_ROOT_WINDOW (f))
1941 && !NILP (w->dedicated)
1942 && other_visible_frames (f))
1944 /* Skip the other windows on this frame.
1945 There might be one, the minibuffer! */
1946 while (CONSP (XCDR (windows))
1947 && EQ (XWINDOW (XCAR (windows))->frame,
1948 XWINDOW (XCAR (XCDR (windows)))->frame))
1949 windows = XCDR (windows);
1951 /* Now we can safely delete the frame. */
1952 Fdelete_frame (w->frame, Qnil);
1954 else
1956 /* Otherwise show a different buffer in the window. */
1957 w->dedicated = Qnil;
1958 Fset_window_buffer (window, buffer);
1959 if (EQ (window, selected_window))
1960 Fset_buffer (w->buffer);
1963 break;
1965 /* Check for a window that has a killed buffer. */
1966 case CHECK_ALL_WINDOWS:
1967 if (! NILP (w->buffer)
1968 && NILP (XBUFFER (w->buffer)->name))
1969 abort ();
1970 break;
1972 case WINDOW_LOOP_UNUSED:
1973 break;
1977 UNGCPRO;
1978 return best_window;
1981 /* Used for debugging. Abort if any window has a dead buffer. */
1983 void
1984 check_all_windows ()
1986 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
1989 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
1990 doc: /* Return the window least recently selected or used for display.
1991 If optional argument FRAME is `visible', search all visible frames.
1992 If FRAME is 0, search all visible and iconified frames.
1993 If FRAME is t, search all frames.
1994 If FRAME is nil, search only the selected frame.
1995 If FRAME is a frame, search only that frame. */)
1996 (frame)
1997 Lisp_Object frame;
1999 register Lisp_Object w;
2000 /* First try for a window that is full-width */
2001 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
2002 if (!NILP (w) && !EQ (w, selected_window))
2003 return w;
2004 /* If none of them, try the rest */
2005 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
2008 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
2009 doc: /* Return the largest window in area.
2010 If optional argument FRAME is `visible', search all visible frames.
2011 If FRAME is 0, search all visible and iconified frames.
2012 If FRAME is t, search all frames.
2013 If FRAME is nil, search only the selected frame.
2014 If FRAME is a frame, search only that frame. */)
2015 (frame)
2016 Lisp_Object frame;
2018 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
2019 frame);
2022 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
2023 doc: /* Return a window currently displaying BUFFER, or nil if none.
2024 If optional argument FRAME is `visible', search all visible frames.
2025 If optional argument FRAME is 0, search all visible and iconified frames.
2026 If FRAME is t, search all frames.
2027 If FRAME is nil, search only the selected frame.
2028 If FRAME is a frame, search only that frame. */)
2029 (buffer, frame)
2030 Lisp_Object buffer, frame;
2032 buffer = Fget_buffer (buffer);
2033 if (BUFFERP (buffer))
2034 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2035 else
2036 return Qnil;
2039 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2040 0, 1, "",
2041 doc: /* Make WINDOW (or the selected window) fill its frame.
2042 Only the frame WINDOW is on is affected.
2043 This function tries to reduce display jumps
2044 by keeping the text previously visible in WINDOW
2045 in the same place on the frame. Doing this depends on
2046 the value of (window-start WINDOW), so if calling this function
2047 in a program gives strange scrolling, make sure the window-start
2048 value is reasonable when this function is called. */)
2049 (window)
2050 Lisp_Object window;
2052 struct window *w;
2053 int startpos;
2054 int top, new_top;
2056 if (NILP (window))
2057 window = selected_window;
2058 else
2059 CHECK_LIVE_WINDOW (window);
2060 w = XWINDOW (window);
2062 startpos = marker_position (w->start);
2063 top = XFASTINT (w->top) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2065 if (MINI_WINDOW_P (w) && top > 0)
2066 error ("Can't expand minibuffer to full frame");
2068 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2070 /* Try to minimize scrolling, by setting the window start to the point
2071 will cause the text at the old window start to be at the same place
2072 on the frame. But don't try to do this if the window start is
2073 outside the visible portion (as might happen when the display is
2074 not current, due to typeahead). */
2075 new_top = XFASTINT (w->top) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2076 if (new_top != top
2077 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2078 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2080 struct position pos;
2081 struct buffer *obuf = current_buffer;
2083 Fset_buffer (w->buffer);
2084 /* This computation used to temporarily move point, but that can
2085 have unwanted side effects due to text properties. */
2086 pos = *vmotion (startpos, -top, w);
2088 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2089 w->window_end_valid = Qnil;
2090 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2091 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2092 : Qnil);
2093 /* We need to do this, so that the window-scroll-functions
2094 get called. */
2095 w->optional_new_start = Qt;
2097 set_buffer_internal (obuf);
2100 return Qnil;
2103 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2104 1, 2, "bDelete windows on (buffer): ",
2105 doc: /* Delete all windows showing BUFFER.
2106 Optional second argument FRAME controls which frames are affected.
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 nil, search all frames.
2110 If FRAME is t, search only the selected frame.
2111 If FRAME is a frame, search only that frame. */)
2112 (buffer, frame)
2113 Lisp_Object buffer, frame;
2115 /* FRAME uses t and nil to mean the opposite of what window_loop
2116 expects. */
2117 if (NILP (frame))
2118 frame = Qt;
2119 else if (EQ (frame, Qt))
2120 frame = Qnil;
2122 if (!NILP (buffer))
2124 buffer = Fget_buffer (buffer);
2125 CHECK_BUFFER (buffer);
2126 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2129 return Qnil;
2132 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2133 Sreplace_buffer_in_windows,
2134 1, 1, "bReplace buffer in windows: ",
2135 doc: /* Replace BUFFER with some other buffer in all windows showing it. */)
2136 (buffer)
2137 Lisp_Object buffer;
2139 if (!NILP (buffer))
2141 buffer = Fget_buffer (buffer);
2142 CHECK_BUFFER (buffer);
2143 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2145 return Qnil;
2148 /* Replace BUFFER with some other buffer in all windows
2149 of all frames, even those on other keyboards. */
2151 void
2152 replace_buffer_in_all_windows (buffer)
2153 Lisp_Object buffer;
2155 #ifdef MULTI_KBOARD
2156 Lisp_Object tail, frame;
2158 /* A single call to window_loop won't do the job
2159 because it only considers frames on the current keyboard.
2160 So loop manually over frames, and handle each one. */
2161 FOR_EACH_FRAME (tail, frame)
2162 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2163 #else
2164 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
2165 #endif
2168 /* Set the height of WINDOW and all its inferiors. */
2170 /* The smallest acceptable dimensions for a window. Anything smaller
2171 might crash Emacs. */
2173 #define MIN_SAFE_WINDOW_WIDTH (2)
2174 #define MIN_SAFE_WINDOW_HEIGHT (1)
2176 /* Make sure that window_min_height and window_min_width are
2177 not too small; if they are, set them to safe minima. */
2179 static void
2180 check_min_window_sizes ()
2182 /* Smaller values might permit a crash. */
2183 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
2184 window_min_width = MIN_SAFE_WINDOW_WIDTH;
2185 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
2186 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
2189 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2190 minimum allowable size. */
2192 void
2193 check_frame_size (frame, rows, cols)
2194 FRAME_PTR frame;
2195 int *rows, *cols;
2197 /* For height, we have to see:
2198 how many windows the frame has at minimum (one or two),
2199 and whether it has a menu bar or other special stuff at the top. */
2200 int min_height
2201 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2202 ? MIN_SAFE_WINDOW_HEIGHT
2203 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2205 if (FRAME_TOP_MARGIN (frame) > 0)
2206 min_height += FRAME_TOP_MARGIN (frame);
2208 if (*rows < min_height)
2209 *rows = min_height;
2210 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2211 *cols = MIN_SAFE_WINDOW_WIDTH;
2215 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2216 check if W's width can be changed, otherwise check W's height.
2217 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2218 siblings, too. If none of the siblings is resizable, WINDOW isn't
2219 either. */
2221 static int
2222 window_fixed_size_p (w, width_p, check_siblings_p)
2223 struct window *w;
2224 int width_p, check_siblings_p;
2226 int fixed_p;
2227 struct window *c;
2229 if (!NILP (w->hchild))
2231 c = XWINDOW (w->hchild);
2233 if (width_p)
2235 /* A horiz. combination is fixed-width if all of if its
2236 children are. */
2237 while (c && window_fixed_size_p (c, width_p, 0))
2238 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2239 fixed_p = c == NULL;
2241 else
2243 /* A horiz. combination is fixed-height if one of if its
2244 children is. */
2245 while (c && !window_fixed_size_p (c, width_p, 0))
2246 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2247 fixed_p = c != NULL;
2250 else if (!NILP (w->vchild))
2252 c = XWINDOW (w->vchild);
2254 if (width_p)
2256 /* A vert. combination is fixed-width if one of if its
2257 children is. */
2258 while (c && !window_fixed_size_p (c, width_p, 0))
2259 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2260 fixed_p = c != NULL;
2262 else
2264 /* A vert. combination is fixed-height if all of if its
2265 children are. */
2266 while (c && window_fixed_size_p (c, width_p, 0))
2267 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2268 fixed_p = c == NULL;
2271 else if (BUFFERP (w->buffer))
2273 if (w->height_fixed_p && !width_p)
2274 fixed_p = 1;
2275 else
2277 struct buffer *old = current_buffer;
2278 Lisp_Object val;
2280 current_buffer = XBUFFER (w->buffer);
2281 val = find_symbol_value (Qwindow_size_fixed);
2282 current_buffer = old;
2284 fixed_p = 0;
2285 if (!EQ (val, Qunbound))
2287 fixed_p = !NILP (val);
2289 if (fixed_p
2290 && ((EQ (val, Qheight) && width_p)
2291 || (EQ (val, Qwidth) && !width_p)))
2292 fixed_p = 0;
2296 /* Can't tell if this one is resizable without looking at
2297 siblings. If all siblings are fixed-size this one is too. */
2298 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2300 Lisp_Object child;
2302 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2303 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2304 break;
2306 if (NILP (child))
2307 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2308 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2309 break;
2311 if (NILP (child))
2312 fixed_p = 1;
2315 else
2316 fixed_p = 1;
2318 return fixed_p;
2322 /* Return the minimum size of window W, not taking fixed-width windows
2323 into account. WIDTH_P non-zero means return the minimum width,
2324 otherwise return the minimum height. If W is a combination window,
2325 compute the minimum size from the minimum sizes of W's children. */
2327 static int
2328 window_min_size_1 (w, width_p)
2329 struct window *w;
2330 int width_p;
2332 struct window *c;
2333 int size;
2335 if (!NILP (w->hchild))
2337 c = XWINDOW (w->hchild);
2338 size = 0;
2340 if (width_p)
2342 /* The min width of a horizontal combination is
2343 the sum of the min widths of its children. */
2344 while (c)
2346 size += window_min_size_1 (c, width_p);
2347 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2350 else
2352 /* The min height a horizontal combination equals
2353 the maximum of all min height of its children. */
2354 while (c)
2356 int min_size = window_min_size_1 (c, width_p);
2357 size = max (min_size, size);
2358 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2362 else if (!NILP (w->vchild))
2364 c = XWINDOW (w->vchild);
2365 size = 0;
2367 if (width_p)
2369 /* The min width of a vertical combination is
2370 the maximum of the min widths of its children. */
2371 while (c)
2373 int min_size = window_min_size_1 (c, width_p);
2374 size = max (min_size, size);
2375 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2378 else
2380 /* The min height of a vertical combination equals
2381 the sum of the min height of its children. */
2382 while (c)
2384 size += window_min_size_1 (c, width_p);
2385 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2389 else
2391 if (width_p)
2392 size = window_min_width;
2393 else
2395 if (MINI_WINDOW_P (w)
2396 || (!WINDOW_WANTS_MODELINE_P (w)
2397 && !WINDOW_WANTS_HEADER_LINE_P (w)))
2398 size = 1;
2399 else
2400 size = window_min_height;
2404 return size;
2408 /* Return the minimum size of window W, taking fixed-size windows into
2409 account. WIDTH_P non-zero means return the minimum width,
2410 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2411 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2412 unless FIXED is null. */
2414 static int
2415 window_min_size (w, width_p, ignore_fixed_p, fixed)
2416 struct window *w;
2417 int width_p, ignore_fixed_p, *fixed;
2419 int size, fixed_p;
2421 if (ignore_fixed_p)
2422 fixed_p = 0;
2423 else
2424 fixed_p = window_fixed_size_p (w, width_p, 1);
2426 if (fixed)
2427 *fixed = fixed_p;
2429 if (fixed_p)
2430 size = width_p ? XFASTINT (w->width) : XFASTINT (w->height);
2431 else
2432 size = window_min_size_1 (w, width_p);
2434 return size;
2438 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2439 WINDOW's width. Resize WINDOW's children, if any, so that they
2440 keep their proportionate size relative to WINDOW. Propagate
2441 WINDOW's top or left edge position to children. Delete windows
2442 that become too small unless NODELETE_P is non-zero.
2444 If NODELETE_P is 2, that means we do delete windows that are
2445 too small, even if they were too small before! */
2447 static void
2448 size_window (window, size, width_p, nodelete_p)
2449 Lisp_Object window;
2450 int size, width_p, nodelete_p;
2452 struct window *w = XWINDOW (window);
2453 struct window *c;
2454 Lisp_Object child, *forward, *sideward;
2455 int old_size, min_size;
2457 if (nodelete_p == 2)
2458 nodelete_p = 0;
2460 check_min_window_sizes ();
2461 size = max (0, size);
2463 /* If the window has been "too small" at one point,
2464 don't delete it for being "too small" in the future.
2465 Preserve it as long as that is at all possible. */
2466 if (width_p)
2468 old_size = XINT (w->width);
2469 min_size = window_min_width;
2471 else
2473 old_size = XINT (w->height);
2474 min_size = window_min_height;
2477 if (old_size < min_size && nodelete_p != 2)
2478 w->too_small_ok = Qt;
2480 /* Maybe delete WINDOW if it's too small. */
2481 if (nodelete_p != 1 && !NILP (w->parent))
2483 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
2484 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
2485 else
2486 min_size = width_p ? window_min_width : window_min_height;
2488 if (size < min_size)
2490 delete_window (window);
2491 return;
2495 /* Set redisplay hints. */
2496 w->last_modified = make_number (0);
2497 w->last_overlay_modified = make_number (0);
2498 windows_or_buffers_changed++;
2499 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
2501 if (width_p)
2503 sideward = &w->vchild;
2504 forward = &w->hchild;
2505 w->width = make_number (size);
2507 else
2509 sideward = &w->hchild;
2510 forward = &w->vchild;
2511 w->height = make_number (size);
2512 w->orig_height = Qnil;
2515 if (!NILP (*sideward))
2517 for (child = *sideward; !NILP (child); child = c->next)
2519 c = XWINDOW (child);
2520 if (width_p)
2521 c->left = w->left;
2522 else
2523 c->top = w->top;
2524 size_window (child, size, width_p, nodelete_p);
2527 else if (!NILP (*forward))
2529 int fixed_size, each, extra, n;
2530 int resize_fixed_p, nfixed;
2531 int last_pos, first_pos, nchildren, total;
2533 /* Determine the fixed-size portion of the this window, and the
2534 number of child windows. */
2535 fixed_size = nchildren = nfixed = total = 0;
2536 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
2538 int child_size;
2540 c = XWINDOW (child);
2541 child_size = width_p ? XINT (c->width) : XINT (c->height);
2542 total += child_size;
2544 if (window_fixed_size_p (c, width_p, 0))
2546 fixed_size += child_size;
2547 ++nfixed;
2551 /* If the new size is smaller than fixed_size, or if there
2552 aren't any resizable windows, allow resizing fixed-size
2553 windows. */
2554 resize_fixed_p = nfixed == nchildren || size < fixed_size;
2556 /* Compute how many lines/columns to add to each child. The
2557 value of extra takes care of rounding errors. */
2558 n = resize_fixed_p ? nchildren : nchildren - nfixed;
2559 each = (size - total) / n;
2560 extra = (size - total) - n * each;
2562 /* Compute new children heights and edge positions. */
2563 first_pos = width_p ? XINT (w->left) : XINT (w->top);
2564 last_pos = first_pos;
2565 for (child = *forward; !NILP (child); child = c->next)
2567 int new_size, old_size;
2569 c = XWINDOW (child);
2570 old_size = width_p ? XFASTINT (c->width) : XFASTINT (c->height);
2571 new_size = old_size;
2573 /* The top or left edge position of this child equals the
2574 bottom or right edge of its predecessor. */
2575 if (width_p)
2576 c->left = make_number (last_pos);
2577 else
2578 c->top = make_number (last_pos);
2580 /* If this child can be resized, do it. */
2581 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
2583 new_size = old_size + each + extra;
2584 extra = 0;
2587 /* Set new height. Note that size_window also propagates
2588 edge positions to children, so it's not a no-op if we
2589 didn't change the child's size. */
2590 size_window (child, new_size, width_p, 1);
2592 /* Remember the bottom/right edge position of this child; it
2593 will be used to set the top/left edge of the next child. */
2594 last_pos += new_size;
2597 /* We should have covered the parent exactly with child windows. */
2598 xassert (size == last_pos - first_pos);
2600 /* Now delete any children that became too small. */
2601 if (!nodelete_p)
2602 for (child = *forward; !NILP (child); child = c->next)
2604 int child_size;
2605 c = XWINDOW (child);
2606 child_size = width_p ? XINT (c->width) : XINT (c->height);
2607 size_window (child, child_size, width_p, 2);
2612 /* Set WINDOW's height to HEIGHT, and recursively change the height of
2613 WINDOW's children. NODELETE non-zero means don't delete windows
2614 that become too small in the process. (The caller should check
2615 later and do so if appropriate.) */
2617 void
2618 set_window_height (window, height, nodelete)
2619 Lisp_Object window;
2620 int height;
2621 int nodelete;
2623 size_window (window, height, 0, nodelete);
2627 /* Set WINDOW's width to WIDTH, and recursively change the width of
2628 WINDOW's children. NODELETE non-zero means don't delete windows
2629 that become too small in the process. (The caller should check
2630 later and do so if appropriate.) */
2632 void
2633 set_window_width (window, width, nodelete)
2634 Lisp_Object window;
2635 int width;
2636 int nodelete;
2638 size_window (window, width, 1, nodelete);
2642 int window_select_count;
2644 Lisp_Object
2645 Fset_window_buffer_unwind (obuf)
2646 Lisp_Object obuf;
2648 Fset_buffer (obuf);
2649 return Qnil;
2653 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2654 means it's allowed to run hooks. See make_frame for a case where
2655 it's not allowed. */
2657 void
2658 set_window_buffer (window, buffer, run_hooks_p)
2659 Lisp_Object window, buffer;
2660 int run_hooks_p;
2662 struct window *w = XWINDOW (window);
2663 struct buffer *b = XBUFFER (buffer);
2664 int count = specpdl_ptr - specpdl;
2666 w->buffer = buffer;
2668 if (EQ (window, selected_window))
2669 b->last_selected_window = window;
2671 /* Update time stamps of buffer display. */
2672 if (INTEGERP (b->display_count))
2673 XSETINT (b->display_count, XINT (b->display_count) + 1);
2674 b->display_time = Fcurrent_time ();
2676 XSETFASTINT (w->window_end_pos, 0);
2677 XSETFASTINT (w->window_end_vpos, 0);
2678 bzero (&w->last_cursor, sizeof w->last_cursor);
2679 w->window_end_valid = Qnil;
2680 w->hscroll = w->min_hscroll = make_number (0);
2681 w->vscroll = 0;
2682 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
2683 set_marker_restricted (w->start,
2684 make_number (b->last_window_start),
2685 buffer);
2686 w->start_at_line_beg = Qnil;
2687 w->force_start = Qnil;
2688 XSETFASTINT (w->last_modified, 0);
2689 XSETFASTINT (w->last_overlay_modified, 0);
2690 windows_or_buffers_changed++;
2692 /* We must select BUFFER for running the window-scroll-functions.
2693 If WINDOW is selected, switch permanently.
2694 Otherwise, switch but go back to the ambient buffer afterward. */
2695 if (EQ (window, selected_window))
2696 Fset_buffer (buffer);
2697 /* We can't check ! NILP (Vwindow_scroll_functions) here
2698 because that might itself be a local variable. */
2699 else if (window_initialized)
2701 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
2702 Fset_buffer (buffer);
2705 /* Set left and right marginal area width from buffer. */
2706 Fset_window_margins (window, b->left_margin_width, b->right_margin_width);
2708 if (run_hooks_p)
2710 if (! NILP (Vwindow_scroll_functions))
2711 run_hook_with_args_2 (Qwindow_scroll_functions, window,
2712 Fmarker_position (w->start));
2714 if (! NILP (Vwindow_configuration_change_hook)
2715 && ! NILP (Vrun_hooks))
2716 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2719 unbind_to (count, Qnil);
2723 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
2724 doc: /* Make WINDOW display BUFFER as its contents.
2725 BUFFER can be a buffer or buffer name. */)
2726 (window, buffer)
2727 register Lisp_Object window, buffer;
2729 register Lisp_Object tem;
2730 register struct window *w = decode_window (window);
2732 XSETWINDOW (window, w);
2733 buffer = Fget_buffer (buffer);
2734 CHECK_BUFFER (buffer);
2736 if (NILP (XBUFFER (buffer)->name))
2737 error ("Attempt to display deleted buffer");
2739 tem = w->buffer;
2740 if (NILP (tem))
2741 error ("Window is deleted");
2742 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
2743 is first being set up. */
2745 if (!NILP (w->dedicated) && !EQ (tem, buffer))
2746 error ("Window is dedicated to `%s'",
2747 XSTRING (XBUFFER (tem)->name)->data);
2749 unshow_buffer (w);
2752 set_window_buffer (window, buffer, 1);
2753 return Qnil;
2756 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
2757 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
2758 If WINDOW is not already selected, also make WINDOW's buffer current.
2759 Note that the main editor command loop
2760 selects the buffer of the selected window before each command. */)
2761 (window)
2762 register Lisp_Object window;
2764 return select_window_1 (window, 1);
2767 /* Note that selected_window can be nil
2768 when this is called from Fset_window_configuration. */
2770 static Lisp_Object
2771 select_window_1 (window, recordflag)
2772 register Lisp_Object window;
2773 int recordflag;
2775 register struct window *w;
2776 register struct window *ow;
2777 struct frame *sf;
2779 CHECK_LIVE_WINDOW (window);
2781 w = XWINDOW (window);
2782 w->frozen_window_start_p = 0;
2784 XSETFASTINT (w->use_time, ++window_select_count);
2785 if (EQ (window, selected_window))
2786 return window;
2788 if (!NILP (selected_window))
2790 ow = XWINDOW (selected_window);
2791 if (! NILP (ow->buffer))
2792 set_marker_both (ow->pointm, ow->buffer,
2793 BUF_PT (XBUFFER (ow->buffer)),
2794 BUF_PT_BYTE (XBUFFER (ow->buffer)));
2797 selected_window = window;
2798 sf = SELECTED_FRAME ();
2799 if (XFRAME (WINDOW_FRAME (w)) != sf)
2801 XFRAME (WINDOW_FRAME (w))->selected_window = window;
2802 /* Use this rather than Fhandle_switch_frame
2803 so that FRAME_FOCUS_FRAME is moved appropriately as we
2804 move around in the state where a minibuffer in a separate
2805 frame is active. */
2806 Fselect_frame (WINDOW_FRAME (w), Qnil);
2808 else
2809 sf->selected_window = window;
2811 if (recordflag)
2812 record_buffer (w->buffer);
2813 Fset_buffer (w->buffer);
2815 XBUFFER (w->buffer)->last_selected_window = window;
2817 /* Go to the point recorded in the window.
2818 This is important when the buffer is in more
2819 than one window. It also matters when
2820 redisplay_window has altered point after scrolling,
2821 because it makes the change only in the window. */
2823 register int new_point = marker_position (w->pointm);
2824 if (new_point < BEGV)
2825 SET_PT (BEGV);
2826 else if (new_point > ZV)
2827 SET_PT (ZV);
2828 else
2829 SET_PT (new_point);
2832 windows_or_buffers_changed++;
2833 return window;
2836 /* Deiconify the frame containing the window WINDOW,
2837 unless it is the selected frame;
2838 then return WINDOW.
2840 The reason for the exception for the selected frame
2841 is that it seems better not to change the selected frames visibility
2842 merely because of displaying a different buffer in it.
2843 The deiconification is useful when a buffer gets shown in
2844 another frame that you were not using lately. */
2846 static Lisp_Object
2847 display_buffer_1 (window)
2848 Lisp_Object window;
2850 Lisp_Object frame = XWINDOW (window)->frame;
2851 FRAME_PTR f = XFRAME (frame);
2853 FRAME_SAMPLE_VISIBILITY (f);
2855 if (!EQ (frame, selected_frame))
2857 if (FRAME_ICONIFIED_P (f))
2858 Fmake_frame_visible (frame);
2859 else if (FRAME_VISIBLE_P (f))
2860 Fraise_frame (frame);
2863 return window;
2866 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
2867 doc: /* Returns non-nil if a buffer named BUFFER-NAME would be created specially.
2868 The value is actually t if the frame should be called with default frame
2869 parameters, and a list of frame parameters if they were specified.
2870 See `special-display-buffer-names', and `special-display-regexps'. */)
2871 (buffer_name)
2872 Lisp_Object buffer_name;
2874 Lisp_Object tem;
2876 CHECK_STRING (buffer_name);
2878 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
2879 if (!NILP (tem))
2880 return Qt;
2882 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
2883 if (!NILP (tem))
2884 return XCDR (tem);
2886 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
2888 Lisp_Object car = XCAR (tem);
2889 if (STRINGP (car)
2890 && fast_string_match (car, buffer_name) >= 0)
2891 return Qt;
2892 else if (CONSP (car)
2893 && STRINGP (XCAR (car))
2894 && fast_string_match (XCAR (car), buffer_name) >= 0)
2895 return XCDR (car);
2897 return Qnil;
2900 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
2901 doc: /* Returns non-nil if a new buffer named BUFFER-NAME would use the same window.
2902 See `same-window-buffer-names' and `same-window-regexps'. */)
2903 (buffer_name)
2904 Lisp_Object buffer_name;
2906 Lisp_Object tem;
2908 CHECK_STRING (buffer_name);
2910 tem = Fmember (buffer_name, Vsame_window_buffer_names);
2911 if (!NILP (tem))
2912 return Qt;
2914 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
2915 if (!NILP (tem))
2916 return Qt;
2918 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
2920 Lisp_Object car = XCAR (tem);
2921 if (STRINGP (car)
2922 && fast_string_match (car, buffer_name) >= 0)
2923 return Qt;
2924 else if (CONSP (car)
2925 && STRINGP (XCAR (car))
2926 && fast_string_match (XCAR (car), buffer_name) >= 0)
2927 return Qt;
2929 return Qnil;
2932 /* Use B so the default is (other-buffer). */
2933 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
2934 "BDisplay buffer: \nP",
2935 doc: /* Make BUFFER appear in some window but don't select it.
2936 BUFFER can be a buffer or a buffer name.
2937 If BUFFER is shown already in some window, just use that one,
2938 unless the window is the selected window and the optional second
2939 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).
2940 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.
2941 Returns the window displaying BUFFER.
2942 If `display-buffer-reuse-frames' is non-nil, and another frame is currently
2943 displaying BUFFER, then simply raise that frame.
2945 The variables `special-display-buffer-names', `special-display-regexps',
2946 `same-window-buffer-names', and `same-window-regexps' customize how certain
2947 buffer names are handled.
2949 If optional argument FRAME is `visible', search all visible frames.
2950 If FRAME is 0, search all visible and iconified frames.
2951 If FRAME is t, search all frames.
2952 If FRAME is a frame, search only that frame.
2953 If FRAME is nil, search only the selected frame
2954 (actually the last nonminibuffer frame),
2955 unless `pop-up-frames' or `display-buffer-reuse-frames' is non-nil,
2956 which means search visible and iconified frames.
2958 If `even-window-heights' is non-nil, window heights will be evened out
2959 if displaying the buffer causes two vertically adjacent windows to be
2960 displayed. */)
2961 (buffer, not_this_window, frame)
2962 register Lisp_Object buffer, not_this_window, frame;
2964 register Lisp_Object window, tem, swp;
2965 struct frame *f;
2967 swp = Qnil;
2968 buffer = Fget_buffer (buffer);
2969 CHECK_BUFFER (buffer);
2971 if (!NILP (Vdisplay_buffer_function))
2972 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2974 if (NILP (not_this_window)
2975 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
2976 return display_buffer_1 (selected_window);
2978 /* See if the user has specified this buffer should appear
2979 in the selected window. */
2980 if (NILP (not_this_window))
2982 swp = Fsame_window_p (XBUFFER (buffer)->name);
2983 if (!NILP (swp) && !no_switch_window (selected_window))
2985 Fswitch_to_buffer (buffer, Qnil);
2986 return display_buffer_1 (selected_window);
2990 /* If the user wants pop-up-frames or display-buffer-reuse-frames,
2991 look for a window showing BUFFER on any visible or iconified frame.
2992 Otherwise search only the current frame. */
2993 if (! NILP (frame))
2994 tem = frame;
2995 else if (pop_up_frames
2996 || display_buffer_reuse_frames
2997 || last_nonminibuf_frame == 0)
2998 XSETFASTINT (tem, 0);
2999 else
3000 XSETFRAME (tem, last_nonminibuf_frame);
3002 window = Fget_buffer_window (buffer, tem);
3003 if (!NILP (window)
3004 && (NILP (not_this_window) || !EQ (window, selected_window)))
3005 return display_buffer_1 (window);
3007 /* Certain buffer names get special handling. */
3008 if (!NILP (Vspecial_display_function) && NILP (swp))
3010 tem = Fspecial_display_p (XBUFFER (buffer)->name);
3011 if (EQ (tem, Qt))
3012 return call1 (Vspecial_display_function, buffer);
3013 if (CONSP (tem))
3014 return call2 (Vspecial_display_function, buffer, tem);
3017 /* If there are no frames open that have more than a minibuffer,
3018 we need to create a new frame. */
3019 if (pop_up_frames || last_nonminibuf_frame == 0)
3021 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3022 Fset_window_buffer (window, buffer);
3023 return display_buffer_1 (window);
3026 f = SELECTED_FRAME ();
3027 if (pop_up_windows
3028 || FRAME_MINIBUF_ONLY_P (f)
3029 /* If the current frame is a special display frame,
3030 don't try to reuse its windows. */
3031 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
3033 Lisp_Object frames;
3035 frames = Qnil;
3036 if (FRAME_MINIBUF_ONLY_P (f))
3037 XSETFRAME (frames, last_nonminibuf_frame);
3038 /* Don't try to create a window if would get an error */
3039 if (split_height_threshold < window_min_height << 1)
3040 split_height_threshold = window_min_height << 1;
3042 /* Note that both Fget_largest_window and Fget_lru_window
3043 ignore minibuffers and dedicated windows.
3044 This means they can return nil. */
3046 /* If the frame we would try to split cannot be split,
3047 try other frames. */
3048 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
3050 /* Try visible frames first. */
3051 window = Fget_largest_window (Qvisible);
3052 /* If that didn't work, try iconified frames. */
3053 if (NILP (window))
3054 window = Fget_largest_window (make_number (0));
3055 if (NILP (window))
3056 window = Fget_largest_window (Qt);
3058 else
3059 window = Fget_largest_window (frames);
3061 /* If we got a tall enough full-width window that can be split,
3062 split it. */
3063 if (!NILP (window)
3064 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3065 && window_height (window) >= split_height_threshold
3066 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
3067 window = Fsplit_window (window, Qnil, Qnil);
3068 else
3070 Lisp_Object upper, lower, other;
3072 window = Fget_lru_window (frames);
3073 /* If the LRU window is selected, and big enough,
3074 and can be split, split it. */
3075 if (!NILP (window)
3076 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
3077 && (EQ (window, selected_window)
3078 || EQ (XWINDOW (window)->parent, Qnil))
3079 && window_height (window) >= window_min_height << 1)
3080 window = Fsplit_window (window, Qnil, Qnil);
3081 /* If Fget_lru_window returned nil, try other approaches. */
3083 /* Try visible frames first. */
3084 if (NILP (window))
3085 window = Fget_buffer_window (buffer, Qvisible);
3086 if (NILP (window))
3087 window = Fget_largest_window (Qvisible);
3088 /* If that didn't work, try iconified frames. */
3089 if (NILP (window))
3090 window = Fget_buffer_window (buffer, make_number (0));
3091 if (NILP (window))
3092 window = Fget_largest_window (make_number (0));
3093 /* Try invisible frames. */
3094 if (NILP (window))
3095 window = Fget_buffer_window (buffer, Qt);
3096 if (NILP (window))
3097 window = Fget_largest_window (Qt);
3098 /* As a last resort, make a new frame. */
3099 if (NILP (window))
3100 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
3101 /* If window appears above or below another,
3102 even out their heights. */
3103 other = upper = lower = Qnil;
3104 if (!NILP (XWINDOW (window)->prev))
3105 other = upper = XWINDOW (window)->prev, lower = window;
3106 if (!NILP (XWINDOW (window)->next))
3107 other = lower = XWINDOW (window)->next, upper = window;
3108 if (!NILP (other)
3109 && !NILP (Veven_window_heights)
3110 /* Check that OTHER and WINDOW are vertically arrayed. */
3111 && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
3112 && (XFASTINT (XWINDOW (other)->height)
3113 > XFASTINT (XWINDOW (window)->height)))
3115 int total = (XFASTINT (XWINDOW (other)->height)
3116 + XFASTINT (XWINDOW (window)->height));
3117 enlarge_window (upper,
3118 total / 2 - XFASTINT (XWINDOW (upper)->height),
3119 0, 0);
3123 else
3124 window = Fget_lru_window (Qnil);
3126 Fset_window_buffer (window, buffer);
3127 return display_buffer_1 (window);
3130 void
3131 temp_output_buffer_show (buf)
3132 register Lisp_Object buf;
3134 register struct buffer *old = current_buffer;
3135 register Lisp_Object window;
3136 register struct window *w;
3138 XBUFFER (buf)->directory = current_buffer->directory;
3140 Fset_buffer (buf);
3141 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3142 BEGV = BEG;
3143 ZV = Z;
3144 SET_PT (BEG);
3145 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3146 set_buffer_internal (old);
3148 if (!EQ (Vtemp_buffer_show_function, Qnil))
3149 call1 (Vtemp_buffer_show_function, buf);
3150 else
3152 window = Fdisplay_buffer (buf, Qnil, Qnil);
3154 if (!EQ (XWINDOW (window)->frame, selected_frame))
3155 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3156 Vminibuf_scroll_window = window;
3157 w = XWINDOW (window);
3158 XSETFASTINT (w->hscroll, 0);
3159 XSETFASTINT (w->min_hscroll, 0);
3160 set_marker_restricted_both (w->start, buf, 1, 1);
3161 set_marker_restricted_both (w->pointm, buf, 1, 1);
3163 /* Run temp-buffer-show-hook, with the chosen window selected
3164 and it sbuffer current. */
3165 if (!NILP (Vrun_hooks))
3167 Lisp_Object tem;
3168 tem = Fboundp (Qtemp_buffer_show_hook);
3169 if (!NILP (tem))
3171 tem = Fsymbol_value (Qtemp_buffer_show_hook);
3172 if (!NILP (tem))
3174 int count = specpdl_ptr - specpdl;
3175 Lisp_Object prev_window;
3176 prev_window = selected_window;
3178 /* Select the window that was chosen, for running the hook. */
3179 record_unwind_protect (Fselect_window, prev_window);
3180 select_window_1 (window, 0);
3181 Fset_buffer (w->buffer);
3182 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3183 select_window_1 (prev_window, 0);
3184 unbind_to (count, Qnil);
3191 static void
3192 make_dummy_parent (window)
3193 Lisp_Object window;
3195 Lisp_Object new;
3196 register struct window *o, *p;
3197 int i;
3199 o = XWINDOW (window);
3200 p = allocate_window ();
3201 for (i = 0; i < VECSIZE (struct window); ++i)
3202 ((struct Lisp_Vector *) p)->contents[i]
3203 = ((struct Lisp_Vector *)o)->contents[i];
3204 XSETWINDOW (new, p);
3206 XSETFASTINT (p->sequence_number, ++sequence_number);
3208 /* Put new into window structure in place of window */
3209 replace_window (window, new);
3211 o->next = Qnil;
3212 o->prev = Qnil;
3213 o->vchild = Qnil;
3214 o->hchild = Qnil;
3215 o->parent = new;
3217 p->start = Qnil;
3218 p->pointm = Qnil;
3219 p->buffer = Qnil;
3222 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3223 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3224 WINDOW defaults to selected one and SIZE to half its size.
3225 If optional third arg HORFLAG is non-nil, split side by side
3226 and put SIZE columns in the first of the pair. In that case,
3227 SIZE includes that window's scroll bar, or the divider column to its right. */)
3228 (window, size, horflag)
3229 Lisp_Object window, size, horflag;
3231 register Lisp_Object new;
3232 register struct window *o, *p;
3233 FRAME_PTR fo;
3234 register int size_int;
3236 if (NILP (window))
3237 window = selected_window;
3238 else
3239 CHECK_LIVE_WINDOW (window);
3241 o = XWINDOW (window);
3242 fo = XFRAME (WINDOW_FRAME (o));
3244 if (NILP (size))
3246 if (!NILP (horflag))
3247 /* Calculate the size of the left-hand window, by dividing
3248 the usable space in columns by two.
3249 We round up, since the left-hand window may include
3250 a dividing line, while the right-hand may not. */
3251 size_int = (XFASTINT (o->width) + 1) >> 1;
3252 else
3253 size_int = XFASTINT (o->height) >> 1;
3255 else
3257 CHECK_NUMBER (size);
3258 size_int = XINT (size);
3261 if (MINI_WINDOW_P (o))
3262 error ("Attempt to split minibuffer window");
3263 else if (window_fixed_size_p (o, !NILP (horflag), 0))
3264 error ("Attempt to split fixed-size window");
3266 check_min_window_sizes ();
3268 if (NILP (horflag))
3270 if (size_int < window_min_height)
3271 error ("Window height %d too small (after splitting)", size_int);
3272 if (size_int + window_min_height > XFASTINT (o->height))
3273 error ("Window height %d too small (after splitting)",
3274 XFASTINT (o->height) - size_int);
3275 if (NILP (o->parent)
3276 || NILP (XWINDOW (o->parent)->vchild))
3278 make_dummy_parent (window);
3279 new = o->parent;
3280 XWINDOW (new)->vchild = window;
3283 else
3285 if (size_int < window_min_width)
3286 error ("Window width %d too small (after splitting)", size_int);
3288 if (size_int + window_min_width > XFASTINT (o->width))
3289 error ("Window width %d too small (after splitting)",
3290 XFASTINT (o->width) - size_int);
3291 if (NILP (o->parent)
3292 || NILP (XWINDOW (o->parent)->hchild))
3294 make_dummy_parent (window);
3295 new = o->parent;
3296 XWINDOW (new)->hchild = window;
3300 /* Now we know that window's parent is a vertical combination
3301 if we are dividing vertically, or a horizontal combination
3302 if we are making side-by-side windows */
3304 windows_or_buffers_changed++;
3305 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3306 new = make_window ();
3307 p = XWINDOW (new);
3309 p->frame = o->frame;
3310 p->next = o->next;
3311 if (!NILP (p->next))
3312 XWINDOW (p->next)->prev = new;
3313 p->prev = window;
3314 o->next = new;
3315 p->parent = o->parent;
3316 p->buffer = Qt;
3317 p->window_end_valid = Qnil;
3318 bzero (&p->last_cursor, sizeof p->last_cursor);
3320 /* Apportion the available frame space among the two new windows */
3322 if (!NILP (horflag))
3324 p->height = o->height;
3325 p->top = o->top;
3326 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
3327 XSETFASTINT (o->width, size_int);
3328 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
3330 else
3332 p->left = o->left;
3333 p->width = o->width;
3334 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
3335 XSETFASTINT (o->height, size_int);
3336 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
3339 /* Adjust glyph matrices. */
3340 adjust_glyphs (fo);
3341 Fset_window_buffer (new, o->buffer);
3342 return new;
3345 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 3, "p",
3346 doc: /* Make current window ARG lines bigger.
3347 From program, optional second arg non-nil means grow sideways ARG columns.
3348 Interactively, if an argument is not given, make the window one line bigger.
3350 Optional third arg PRESERVE-BEFORE, if non-nil, means do not change the size
3351 of the siblings above or to the left of the selected window. Only
3352 siblings to the right or below are changed. */)
3353 (arg, side, preserve_before)
3354 register Lisp_Object arg, side, preserve_before;
3356 CHECK_NUMBER (arg);
3357 enlarge_window (selected_window, XINT (arg), !NILP (side),
3358 !NILP (preserve_before));
3360 if (! NILP (Vwindow_configuration_change_hook))
3361 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3363 return Qnil;
3366 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3367 doc: /* Make current window ARG lines smaller.
3368 From program, optional second arg non-nil means shrink sideways arg columns.
3369 Interactively, if an argument is not given, make the window one line smaller. */)
3370 (arg, side)
3371 register Lisp_Object arg, side;
3373 CHECK_NUMBER (arg);
3374 enlarge_window (selected_window, -XINT (arg), !NILP (side), 0);
3376 if (! NILP (Vwindow_configuration_change_hook))
3377 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3379 return Qnil;
3383 window_height (window)
3384 Lisp_Object window;
3386 register struct window *p = XWINDOW (window);
3387 return XFASTINT (p->height);
3391 window_width (window)
3392 Lisp_Object window;
3394 register struct window *p = XWINDOW (window);
3395 return XFASTINT (p->width);
3399 #define CURBEG(w) \
3400 *(widthflag ? &(XWINDOW (w)->left) : &(XWINDOW (w)->top))
3402 #define CURSIZE(w) \
3403 *(widthflag ? &(XWINDOW (w)->width) : &(XWINDOW (w)->height))
3406 /* Enlarge WINDOW by DELTA. WIDTHFLAG non-zero means
3407 increase its width. Siblings of the selected window are resized to
3408 fulfill the size request. If they become too small in the process,
3409 they will be deleted.
3411 If PRESERVE_BEFORE is nonzero, that means don't alter
3412 the siblings to the left or above WINDOW. */
3414 static void
3415 enlarge_window (window, delta, widthflag, preserve_before)
3416 Lisp_Object window;
3417 int delta, widthflag, preserve_before;
3419 Lisp_Object parent, next, prev;
3420 struct window *p;
3421 Lisp_Object *sizep;
3422 int maximum;
3423 int (*sizefun) P_ ((Lisp_Object))
3424 = widthflag ? window_width : window_height;
3425 void (*setsizefun) P_ ((Lisp_Object, int, int))
3426 = (widthflag ? set_window_width : set_window_height);
3428 /* Check values of window_min_width and window_min_height for
3429 validity. */
3430 check_min_window_sizes ();
3432 /* Give up if this window cannot be resized. */
3433 if (window_fixed_size_p (XWINDOW (window), widthflag, 1))
3434 error ("Window is not resizable");
3436 /* Find the parent of the selected window. */
3437 while (1)
3439 p = XWINDOW (window);
3440 parent = p->parent;
3442 if (NILP (parent))
3444 if (widthflag)
3445 error ("No other window to side of this one");
3446 break;
3449 if (widthflag
3450 ? !NILP (XWINDOW (parent)->hchild)
3451 : !NILP (XWINDOW (parent)->vchild))
3452 break;
3454 window = parent;
3457 sizep = &CURSIZE (window);
3460 register int maxdelta;
3462 /* Compute the maximum size increment this window can have. */
3464 if (preserve_before)
3466 if (!NILP (parent))
3468 maxdelta = (*sizefun) (parent) - XINT (*sizep);
3469 /* Subtract size of siblings before, since we can't take that. */
3470 maxdelta -= XINT (CURBEG (window)) - XINT (CURBEG (parent));
3472 else
3473 maxdelta = (!NILP (p->next) ? ((*sizefun) (p->next)
3474 - window_min_size (XWINDOW (p->next),
3475 widthflag, 0, 0))
3476 : (delta = 0));
3478 else
3479 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
3480 /* This is a main window followed by a minibuffer. */
3481 : !NILP (p->next) ? ((*sizefun) (p->next)
3482 - window_min_size (XWINDOW (p->next),
3483 widthflag, 0, 0))
3484 /* This is a minibuffer following a main window. */
3485 : !NILP (p->prev) ? ((*sizefun) (p->prev)
3486 - window_min_size (XWINDOW (p->prev),
3487 widthflag, 0, 0))
3488 /* This is a frame with only one window, a minibuffer-only
3489 or a minibufferless frame. */
3490 : (delta = 0));
3492 if (delta > maxdelta)
3493 /* This case traps trying to make the minibuffer
3494 the full frame, or make the only window aside from the
3495 minibuffer the full frame. */
3496 delta = maxdelta;
3499 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window), widthflag, 0, 0))
3501 delete_window (window);
3502 return;
3505 if (delta == 0)
3506 return;
3508 /* Find the total we can get from other siblings without deleting them. */
3509 maximum = 0;
3510 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
3511 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
3512 widthflag, 0, 0);
3513 if (! preserve_before)
3514 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
3515 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
3516 widthflag, 0, 0);
3518 /* If we can get it all from them without deleting them, do so. */
3519 if (delta <= maximum)
3521 Lisp_Object first_unaffected;
3522 Lisp_Object first_affected;
3523 int fixed_p;
3525 next = p->next;
3526 prev = p->prev;
3527 first_affected = window;
3528 /* Look at one sibling at a time,
3529 moving away from this window in both directions alternately,
3530 and take as much as we can get without deleting that sibling. */
3531 while (delta != 0
3532 && (!NILP (next) || (!preserve_before && !NILP (prev))))
3534 if (! NILP (next))
3536 int this_one = ((*sizefun) (next)
3537 - window_min_size (XWINDOW (next),
3538 widthflag, 0, &fixed_p));
3539 if (!fixed_p)
3541 if (this_one > delta)
3542 this_one = delta;
3544 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
3545 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
3547 delta -= this_one;
3550 next = XWINDOW (next)->next;
3553 if (delta == 0)
3554 break;
3556 if (!preserve_before && ! NILP (prev))
3558 int this_one = ((*sizefun) (prev)
3559 - window_min_size (XWINDOW (prev),
3560 widthflag, 0, &fixed_p));
3561 if (!fixed_p)
3563 if (this_one > delta)
3564 this_one = delta;
3566 first_affected = prev;
3568 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
3569 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
3571 delta -= this_one;
3574 prev = XWINDOW (prev)->prev;
3578 xassert (delta == 0);
3580 /* Now recalculate the edge positions of all the windows affected,
3581 based on the new sizes. */
3582 first_unaffected = next;
3583 prev = first_affected;
3584 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
3585 prev = next, next = XWINDOW (next)->next)
3587 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
3588 /* This does not change size of NEXT,
3589 but it propagates the new top edge to its children */
3590 (*setsizefun) (next, (*sizefun) (next), 0);
3593 else
3595 register int delta1;
3596 register int opht = (*sizefun) (parent);
3598 if (opht <= XINT (*sizep) + delta)
3600 /* If trying to grow this window to or beyond size of the parent,
3601 just delete all the sibling windows. */
3602 Lisp_Object start, tem, next;
3604 start = XWINDOW (parent)->vchild;
3605 if (NILP (start))
3606 start = XWINDOW (parent)->hchild;
3608 /* Delete any siblings that come after WINDOW. */
3609 tem = XWINDOW (window)->next;
3610 while (! NILP (tem))
3612 next = XWINDOW (tem)->next;
3613 delete_window (tem);
3614 tem = next;
3617 /* Delete any siblings that come after WINDOW.
3618 Note that if START is not WINDOW, then WINDOW still
3619 Fhas siblings, so WINDOW has not yet replaced its parent. */
3620 tem = start;
3621 while (! EQ (tem, window))
3623 next = XWINDOW (tem)->next;
3624 delete_window (tem);
3625 tem = next;
3628 else
3630 /* Otherwise, make delta1 just right so that if we add
3631 delta1 lines to this window and to the parent, and then
3632 shrink the parent back to its original size, the new
3633 proportional size of this window will increase by delta.
3635 The function size_window will compute the new height h'
3636 of the window from delta1 as:
3638 e = delta1/n
3639 x = delta1 - delta1/n * n for the 1st resizable child
3640 h' = h + e + x
3642 where n is the number of children that can be resized.
3643 We can ignore x by choosing a delta1 that is a multiple of
3644 n. We want the height of this window to come out as
3646 h' = h + delta
3648 So, delta1 must be
3650 h + e = h + delta
3651 delta1/n = delta
3652 delta1 = n * delta.
3654 The number of children n rquals the number of resizable
3655 children of this window + 1 because we know window itself
3656 is resizable (otherwise we would have signalled an error. */
3658 struct window *w = XWINDOW (window);
3659 Lisp_Object s;
3660 int n = 1;
3662 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
3663 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
3664 ++n;
3665 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
3666 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
3667 ++n;
3669 delta1 = n * delta;
3671 /* Add delta1 lines or columns to this window, and to the parent,
3672 keeping things consistent while not affecting siblings. */
3673 XSETINT (CURSIZE (parent), opht + delta1);
3674 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
3676 /* Squeeze out delta1 lines or columns from our parent,
3677 shriking this window and siblings proportionately.
3678 This brings parent back to correct size.
3679 Delta1 was calculated so this makes this window the desired size,
3680 taking it all out of the siblings. */
3681 (*setsizefun) (parent, opht, 0);
3686 XSETFASTINT (p->last_modified, 0);
3687 XSETFASTINT (p->last_overlay_modified, 0);
3689 /* Adjust glyph matrices. */
3690 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
3693 #undef CURBEG
3694 #undef CURSIZE
3698 /***********************************************************************
3699 Resizing Mini-Windows
3700 ***********************************************************************/
3702 static void shrink_window_lowest_first P_ ((struct window *, int));
3704 enum save_restore_action
3706 CHECK_ORIG_SIZES,
3707 SAVE_ORIG_SIZES,
3708 RESTORE_ORIG_SIZES
3711 static int save_restore_orig_size P_ ((struct window *,
3712 enum save_restore_action));
3714 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
3715 from lowest windows first. */
3717 static void
3718 shrink_window_lowest_first (w, height)
3719 struct window *w;
3720 int height;
3722 struct window *c;
3723 Lisp_Object child;
3724 int old_height;
3726 xassert (!MINI_WINDOW_P (w));
3728 /* Set redisplay hints. */
3729 XSETFASTINT (w->last_modified, 0);
3730 XSETFASTINT (w->last_overlay_modified, 0);
3731 windows_or_buffers_changed++;
3732 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
3734 old_height = XFASTINT (w->height);
3735 XSETFASTINT (w->height, height);
3737 if (!NILP (w->hchild))
3739 for (child = w->hchild; !NILP (child); child = c->next)
3741 c = XWINDOW (child);
3742 c->top = w->top;
3743 shrink_window_lowest_first (c, height);
3746 else if (!NILP (w->vchild))
3748 Lisp_Object last_child;
3749 int delta = old_height - height;
3750 int last_top;
3752 last_child = Qnil;
3754 /* Find the last child. We are taking space from lowest windows
3755 first, so we iterate over children from the last child
3756 backwards. */
3757 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
3758 last_child = child;
3760 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
3761 for (child = last_child; delta && !NILP (child); child = c->prev)
3763 int this_one;
3765 c = XWINDOW (child);
3766 this_one = XFASTINT (c->height) - MIN_SAFE_WINDOW_HEIGHT;
3768 if (this_one > delta)
3769 this_one = delta;
3771 shrink_window_lowest_first (c, XFASTINT (c->height) - this_one);
3772 delta -= this_one;
3775 /* Compute new positions. */
3776 last_top = XINT (w->top);
3777 for (child = w->vchild; !NILP (child); child = c->next)
3779 c = XWINDOW (child);
3780 c->top = make_number (last_top);
3781 shrink_window_lowest_first (c, XFASTINT (c->height));
3782 last_top += XFASTINT (c->height);
3788 /* Save, restore, or check positions and sizes in the window tree
3789 rooted at W. ACTION says what to do.
3791 If ACTION is CHECK_ORIG_SIZES, check if orig_top and orig_height
3792 members are valid for all windows in the window tree. Value is
3793 non-zero if they are valid.
3795 If ACTION is SAVE_ORIG_SIZES, save members top and height in
3796 orig_top and orig_height for all windows in the tree.
3798 If ACTION is RESTORE_ORIG_SIZES, restore top and height from
3799 values stored in orig_top and orig_height for all windows. */
3801 static int
3802 save_restore_orig_size (w, action)
3803 struct window *w;
3804 enum save_restore_action action;
3806 int success_p = 1;
3808 while (w)
3810 if (!NILP (w->hchild))
3812 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
3813 success_p = 0;
3815 else if (!NILP (w->vchild))
3817 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
3818 success_p = 0;
3821 switch (action)
3823 case CHECK_ORIG_SIZES:
3824 if (!INTEGERP (w->orig_top) || !INTEGERP (w->orig_height))
3825 return 0;
3826 break;
3828 case SAVE_ORIG_SIZES:
3829 w->orig_top = w->top;
3830 w->orig_height = w->height;
3831 XSETFASTINT (w->last_modified, 0);
3832 XSETFASTINT (w->last_overlay_modified, 0);
3833 break;
3835 case RESTORE_ORIG_SIZES:
3836 xassert (INTEGERP (w->orig_top) && INTEGERP (w->orig_height));
3837 w->top = w->orig_top;
3838 w->height = w->orig_height;
3839 w->orig_height = w->orig_top = Qnil;
3840 XSETFASTINT (w->last_modified, 0);
3841 XSETFASTINT (w->last_overlay_modified, 0);
3842 break;
3844 default:
3845 abort ();
3848 w = NILP (w->next) ? NULL : XWINDOW (w->next);
3851 return success_p;
3855 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
3856 without deleting other windows. */
3858 void
3859 grow_mini_window (w, delta)
3860 struct window *w;
3861 int delta;
3863 struct frame *f = XFRAME (w->frame);
3864 struct window *root;
3866 xassert (MINI_WINDOW_P (w));
3867 xassert (delta >= 0);
3869 /* Check values of window_min_width and window_min_height for
3870 validity. */
3871 check_min_window_sizes ();
3873 /* Compute how much we can enlarge the mini-window without deleting
3874 other windows. */
3875 root = XWINDOW (FRAME_ROOT_WINDOW (f));
3876 if (delta)
3878 int min_height = window_min_size (root, 0, 0, 0);
3879 if (XFASTINT (root->height) - delta < min_height)
3880 /* Note that the root window may already be smaller than
3881 min_height. */
3882 delta = max (0, XFASTINT (root->height) - min_height);
3885 if (delta)
3887 /* Save original window sizes and positions, if not already done. */
3888 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
3889 save_restore_orig_size (root, SAVE_ORIG_SIZES);
3891 /* Shrink other windows. */
3892 shrink_window_lowest_first (root, XFASTINT (root->height) - delta);
3894 /* Grow the mini-window. */
3895 w->top = make_number (XFASTINT (root->top) + XFASTINT (root->height));
3896 w->height = make_number (XFASTINT (w->height) + delta);
3897 XSETFASTINT (w->last_modified, 0);
3898 XSETFASTINT (w->last_overlay_modified, 0);
3900 adjust_glyphs (f);
3905 /* Shrink mini-window W. If there is recorded info about window sizes
3906 before a call to grow_mini_window, restore recorded window sizes.
3907 Otherwise, if the mini-window is higher than 1 line, resize it to 1
3908 line. */
3910 void
3911 shrink_mini_window (w)
3912 struct window *w;
3914 struct frame *f = XFRAME (w->frame);
3915 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
3917 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
3919 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
3920 adjust_glyphs (f);
3921 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3922 windows_or_buffers_changed = 1;
3924 else if (XFASTINT (w->height) > 1)
3926 /* Distribute the additional lines of the mini-window
3927 among the other windows. */
3928 Lisp_Object window;
3929 XSETWINDOW (window, w);
3930 enlarge_window (window, 1 - XFASTINT (w->height), 0, 0);
3936 /* Mark window cursors off for all windows in the window tree rooted
3937 at W by setting their phys_cursor_on_p flag to zero. Called from
3938 xterm.c, e.g. when a frame is cleared and thereby all cursors on
3939 the frame are cleared. */
3941 void
3942 mark_window_cursors_off (w)
3943 struct window *w;
3945 while (w)
3947 if (!NILP (w->hchild))
3948 mark_window_cursors_off (XWINDOW (w->hchild));
3949 else if (!NILP (w->vchild))
3950 mark_window_cursors_off (XWINDOW (w->vchild));
3951 else
3952 w->phys_cursor_on_p = 0;
3954 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3959 /* Return number of lines of text (not counting mode lines) in W. */
3962 window_internal_height (w)
3963 struct window *w;
3965 int ht = XFASTINT (w->height);
3967 if (!MINI_WINDOW_P (w))
3969 if (!NILP (w->parent)
3970 || !NILP (w->vchild)
3971 || !NILP (w->hchild)
3972 || !NILP (w->next)
3973 || !NILP (w->prev)
3974 || WINDOW_WANTS_MODELINE_P (w))
3975 --ht;
3977 if (WINDOW_WANTS_HEADER_LINE_P (w))
3978 --ht;
3981 return ht;
3985 /* Return the number of columns in W.
3986 Don't count columns occupied by scroll bars or the vertical bar
3987 separating W from the sibling to its right. */
3990 window_internal_width (w)
3991 struct window *w;
3993 struct frame *f = XFRAME (WINDOW_FRAME (w));
3994 int width = XINT (w->width);
3996 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
3997 /* Scroll bars occupy a few columns. */
3998 width -= FRAME_SCROLL_BAR_COLS (f);
3999 else if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4000 /* The column of `|' characters separating side-by-side windows
4001 occupies one column only. */
4002 width -= 1;
4004 /* On window-systems, areas to the left and right of the window
4005 are used as fringes. */
4006 if (FRAME_WINDOW_P (f))
4007 width -= FRAME_FRINGE_COLS (f);
4009 return width;
4013 /************************************************************************
4014 Window Scrolling
4015 ***********************************************************************/
4017 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4018 N screen-fulls, which is defined as the height of the window minus
4019 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4020 instead. Negative values of N mean scroll down. NOERROR non-zero
4021 means don't signal an error if we try to move over BEGV or ZV,
4022 respectively. */
4024 static void
4025 window_scroll (window, n, whole, noerror)
4026 Lisp_Object window;
4027 int n;
4028 int whole;
4029 int noerror;
4031 /* If we must, use the pixel-based version which is much slower than
4032 the line-based one but can handle varying line heights. */
4033 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4034 window_scroll_pixel_based (window, n, whole, noerror);
4035 else
4036 window_scroll_line_based (window, n, whole, noerror);
4040 /* Implementation of window_scroll that works based on pixel line
4041 heights. See the comment of window_scroll for parameter
4042 descriptions. */
4044 static void
4045 window_scroll_pixel_based (window, n, whole, noerror)
4046 Lisp_Object window;
4047 int n;
4048 int whole;
4049 int noerror;
4051 struct it it;
4052 struct window *w = XWINDOW (window);
4053 struct text_pos start;
4054 Lisp_Object tem;
4055 int this_scroll_margin;
4056 int preserve_y;
4057 /* True if we fiddled the window vscroll field without really scrolling. */
4058 int vscrolled = 0;
4060 SET_TEXT_POS_FROM_MARKER (start, w->start);
4062 /* If PT is not visible in WINDOW, move back one half of
4063 the screen. Allow PT to be partially visible, otherwise
4064 something like (scroll-down 1) with PT in the line before
4065 the partially visible one would recenter. */
4066 tem = Fpos_visible_in_window_p (make_number (PT), window, Qt);
4067 if (NILP (tem))
4069 /* Move backward half the height of the window. Performance note:
4070 vmotion used here is about 10% faster, but would give wrong
4071 results for variable height lines. */
4072 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4073 it.current_y = it.last_visible_y;
4074 move_it_vertically (&it, - window_box_height (w) / 2);
4076 /* The function move_iterator_vertically may move over more than
4077 the specified y-distance. If it->w is small, e.g. a
4078 mini-buffer window, we may end up in front of the window's
4079 display area. This is the case when Start displaying at the
4080 start of the line containing PT in this case. */
4081 if (it.current_y <= 0)
4083 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4084 move_it_vertically (&it, 0);
4085 it.current_y = 0;
4088 start = it.current.pos;
4091 /* If scroll_preserve_screen_position is non-zero, we try to set
4092 point in the same window line as it is now, so get that line. */
4093 if (!NILP (Vscroll_preserve_screen_position))
4095 start_display (&it, w, start);
4096 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4097 preserve_y = it.current_y;
4099 else
4100 preserve_y = -1;
4102 /* Move iterator it from start the specified distance forward or
4103 backward. The result is the new window start. */
4104 start_display (&it, w, start);
4105 if (whole)
4107 int screen_full = (window_box_height (w)
4108 - next_screen_context_lines * CANON_Y_UNIT (it.f));
4109 int dy = n * screen_full;
4111 /* Note that move_it_vertically always moves the iterator to the
4112 start of a line. So, if the last line doesn't have a newline,
4113 we would end up at the start of the line ending at ZV. */
4114 if (dy <= 0)
4115 move_it_vertically_backward (&it, -dy);
4116 else if (dy > 0)
4117 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4118 MOVE_TO_POS | MOVE_TO_Y);
4120 else
4121 move_it_by_lines (&it, n, 1);
4123 /* End if we end up at ZV or BEGV. */
4124 if ((n > 0 && IT_CHARPOS (it) == ZV)
4125 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4127 if (IT_CHARPOS (it) == ZV)
4129 if (it.current_y + it.max_ascent + it.max_descent
4130 > it.last_visible_y)
4132 /* The last line was only partially visible, make it fully
4133 visible. */
4134 w->vscroll = (it.last_visible_y
4135 - it.current_y + it.max_ascent + it.max_descent);
4136 adjust_glyphs (it.f);
4138 else if (noerror)
4139 return;
4140 else
4141 Fsignal (Qend_of_buffer, Qnil);
4143 else
4145 if (w->vscroll != 0)
4146 /* The first line was only partially visible, make it fully
4147 visible. */
4148 w->vscroll = 0;
4149 else if (noerror)
4150 return;
4151 else
4152 Fsignal (Qbeginning_of_buffer, Qnil);
4155 /* If control gets here, then we vscrolled. */
4157 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4159 /* Don't try to change the window start below. */
4160 vscrolled = 1;
4163 if (! vscrolled)
4165 /* Set the window start, and set up the window for redisplay. */
4166 set_marker_restricted (w->start, make_number (IT_CHARPOS (it)),
4167 w->buffer);
4168 w->start_at_line_beg = Fbolp ();
4169 w->update_mode_line = Qt;
4170 XSETFASTINT (w->last_modified, 0);
4171 XSETFASTINT (w->last_overlay_modified, 0);
4172 /* Set force_start so that redisplay_window will run the
4173 window-scroll-functions. */
4174 w->force_start = Qt;
4177 it.current_y = it.vpos = 0;
4179 /* Preserve the screen position if we must. */
4180 if (preserve_y >= 0)
4182 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
4183 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4185 else
4187 /* Move PT out of scroll margins. */
4188 this_scroll_margin = max (0, scroll_margin);
4189 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
4190 this_scroll_margin *= CANON_Y_UNIT (it.f);
4192 if (n > 0)
4194 /* We moved the window start towards ZV, so PT may be now
4195 in the scroll margin at the top. */
4196 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4197 while (it.current_y < this_scroll_margin)
4199 int prev = it.current_y;
4200 move_it_by_lines (&it, 1, 1);
4201 if (prev == it.current_y)
4202 break;
4204 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4206 else if (n < 0)
4208 int charpos, bytepos;
4210 /* We moved the window start towards BEGV, so PT may be now
4211 in the scroll margin at the bottom. */
4212 move_it_to (&it, PT, -1,
4213 it.last_visible_y - this_scroll_margin - 1, -1,
4214 MOVE_TO_POS | MOVE_TO_Y);
4216 /* Save our position, in case it's correct. */
4217 charpos = IT_CHARPOS (it);
4218 bytepos = IT_BYTEPOS (it);
4220 /* See if point is on a partially visible line at the end. */
4221 move_it_by_lines (&it, 1, 1);
4222 if (it.current_y > it.last_visible_y)
4223 /* The last line was only partially visible, so back up two
4224 lines to make sure we're on a fully visible line. */
4226 move_it_by_lines (&it, -2, 0);
4227 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4229 else
4230 /* No, the position we saved is OK, so use it. */
4231 SET_PT_BOTH (charpos, bytepos);
4237 /* Implementation of window_scroll that works based on screen lines.
4238 See the comment of window_scroll for parameter descriptions. */
4240 static void
4241 window_scroll_line_based (window, n, whole, noerror)
4242 Lisp_Object window;
4243 int n;
4244 int whole;
4245 int noerror;
4247 register struct window *w = XWINDOW (window);
4248 register int opoint = PT, opoint_byte = PT_BYTE;
4249 register int pos, pos_byte;
4250 register int ht = window_internal_height (w);
4251 register Lisp_Object tem;
4252 int lose;
4253 Lisp_Object bolp;
4254 int startpos;
4255 struct position posit;
4256 int original_vpos;
4258 /* If scrolling screen-fulls, compute the number of lines to
4259 scroll from the window's height. */
4260 if (whole)
4261 n *= max (1, ht - next_screen_context_lines);
4263 startpos = marker_position (w->start);
4265 posit = *compute_motion (startpos, 0, 0, 0,
4266 PT, ht, 0,
4267 window_internal_width (w), XINT (w->hscroll),
4268 0, w);
4269 original_vpos = posit.vpos;
4271 XSETFASTINT (tem, PT);
4272 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4274 if (NILP (tem))
4276 Fvertical_motion (make_number (- (ht / 2)), window);
4277 startpos = PT;
4280 SET_PT (startpos);
4281 lose = n < 0 && PT == BEGV;
4282 Fvertical_motion (make_number (n), window);
4283 pos = PT;
4284 pos_byte = PT_BYTE;
4285 bolp = Fbolp ();
4286 SET_PT_BOTH (opoint, opoint_byte);
4288 if (lose)
4290 if (noerror)
4291 return;
4292 else
4293 Fsignal (Qbeginning_of_buffer, Qnil);
4296 if (pos < ZV)
4298 int this_scroll_margin = scroll_margin;
4300 /* Don't use a scroll margin that is negative or too large. */
4301 if (this_scroll_margin < 0)
4302 this_scroll_margin = 0;
4304 if (XINT (w->height) < 4 * scroll_margin)
4305 this_scroll_margin = XINT (w->height) / 4;
4307 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4308 w->start_at_line_beg = bolp;
4309 w->update_mode_line = Qt;
4310 XSETFASTINT (w->last_modified, 0);
4311 XSETFASTINT (w->last_overlay_modified, 0);
4312 /* Set force_start so that redisplay_window will run
4313 the window-scroll-functions. */
4314 w->force_start = Qt;
4316 if (whole && !NILP (Vscroll_preserve_screen_position))
4318 SET_PT_BOTH (pos, pos_byte);
4319 Fvertical_motion (make_number (original_vpos), window);
4321 /* If we scrolled forward, put point enough lines down
4322 that it is outside the scroll margin. */
4323 else if (n > 0)
4325 int top_margin;
4327 if (this_scroll_margin > 0)
4329 SET_PT_BOTH (pos, pos_byte);
4330 Fvertical_motion (make_number (this_scroll_margin), window);
4331 top_margin = PT;
4333 else
4334 top_margin = pos;
4336 if (top_margin <= opoint)
4337 SET_PT_BOTH (opoint, opoint_byte);
4338 else if (!NILP (Vscroll_preserve_screen_position))
4340 SET_PT_BOTH (pos, pos_byte);
4341 Fvertical_motion (make_number (original_vpos), window);
4343 else
4344 SET_PT (top_margin);
4346 else if (n < 0)
4348 int bottom_margin;
4350 /* If we scrolled backward, put point near the end of the window
4351 but not within the scroll margin. */
4352 SET_PT_BOTH (pos, pos_byte);
4353 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4354 if (XFASTINT (tem) == ht - this_scroll_margin)
4355 bottom_margin = PT;
4356 else
4357 bottom_margin = PT + 1;
4359 if (bottom_margin > opoint)
4360 SET_PT_BOTH (opoint, opoint_byte);
4361 else
4363 if (!NILP (Vscroll_preserve_screen_position))
4365 SET_PT_BOTH (pos, pos_byte);
4366 Fvertical_motion (make_number (original_vpos), window);
4368 else
4369 Fvertical_motion (make_number (-1), window);
4373 else
4375 if (noerror)
4376 return;
4377 else
4378 Fsignal (Qend_of_buffer, Qnil);
4383 /* Scroll selected_window up or down. If N is nil, scroll a
4384 screen-full which is defined as the height of the window minus
4385 next_screen_context_lines. If N is the symbol `-', scroll.
4386 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4387 up. This is the guts of Fscroll_up and Fscroll_down. */
4389 static void
4390 scroll_command (n, direction)
4391 Lisp_Object n;
4392 int direction;
4394 int count = BINDING_STACK_SIZE ();
4396 xassert (abs (direction) == 1);
4398 /* If selected window's buffer isn't current, make it current for
4399 the moment. But don't screw up if window_scroll gets an error. */
4400 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4402 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4403 Fset_buffer (XWINDOW (selected_window)->buffer);
4405 /* Make redisplay consider other windows than just selected_window. */
4406 ++windows_or_buffers_changed;
4409 if (NILP (n))
4410 window_scroll (selected_window, direction, 1, 0);
4411 else if (EQ (n, Qminus))
4412 window_scroll (selected_window, -direction, 1, 0);
4413 else
4415 n = Fprefix_numeric_value (n);
4416 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4419 unbind_to (count, Qnil);
4422 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
4423 doc: /* Scroll text of current window upward ARG lines; or near full screen if no ARG.
4424 A near full screen is `next-screen-context-lines' less than a full screen.
4425 Negative ARG means scroll downward.
4426 If ARG is the atom `-', scroll downward by nearly full screen.
4427 When calling from a program, supply as argument a number, nil, or `-'. */)
4428 (arg)
4429 Lisp_Object arg;
4431 scroll_command (arg, 1);
4432 return Qnil;
4435 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
4436 doc: /* Scroll text of current window down ARG lines; or near full screen if no ARG.
4437 A near full screen is `next-screen-context-lines' less than a full screen.
4438 Negative ARG means scroll upward.
4439 If ARG is the atom `-', scroll upward by nearly full screen.
4440 When calling from a program, supply as argument a number, nil, or `-'. */)
4441 (arg)
4442 Lisp_Object arg;
4444 scroll_command (arg, -1);
4445 return Qnil;
4448 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4449 doc: /* Return the other window for \"other window scroll\" commands.
4450 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4451 specifies the window.
4452 If `other-window-scroll-buffer' is non-nil, a window
4453 showing that buffer is used. */)
4456 Lisp_Object window;
4458 if (MINI_WINDOW_P (XWINDOW (selected_window))
4459 && !NILP (Vminibuf_scroll_window))
4460 window = Vminibuf_scroll_window;
4461 /* If buffer is specified, scroll that buffer. */
4462 else if (!NILP (Vother_window_scroll_buffer))
4464 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4465 if (NILP (window))
4466 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4468 else
4470 /* Nothing specified; look for a neighboring window on the same
4471 frame. */
4472 window = Fnext_window (selected_window, Qnil, Qnil);
4474 if (EQ (window, selected_window))
4475 /* That didn't get us anywhere; look for a window on another
4476 visible frame. */
4478 window = Fnext_window (window, Qnil, Qt);
4479 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4480 && ! EQ (window, selected_window));
4483 CHECK_LIVE_WINDOW (window);
4485 if (EQ (window, selected_window))
4486 error ("There is no other window");
4488 return window;
4491 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4492 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4493 A near full screen is `next-screen-context-lines' less than a full screen.
4494 The next window is the one below the current one; or the one at the top
4495 if the current one is at the bottom. Negative ARG means scroll downward.
4496 If ARG is the atom `-', scroll downward by nearly full screen.
4497 When calling from a program, supply as argument a number, nil, or `-'.
4499 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4500 specifies the window to scroll.
4501 If `other-window-scroll-buffer' is non-nil, scroll the window
4502 showing that buffer, popping the buffer up if necessary. */)
4503 (arg)
4504 Lisp_Object arg;
4506 Lisp_Object window;
4507 struct window *w;
4508 int count = BINDING_STACK_SIZE ();
4510 window = Fother_window_for_scrolling ();
4511 w = XWINDOW (window);
4513 /* Don't screw up if window_scroll gets an error. */
4514 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4515 ++windows_or_buffers_changed;
4517 Fset_buffer (w->buffer);
4518 SET_PT (marker_position (w->pointm));
4520 if (NILP (arg))
4521 window_scroll (window, 1, 1, 1);
4522 else if (EQ (arg, Qminus))
4523 window_scroll (window, -1, 1, 1);
4524 else
4526 if (CONSP (arg))
4527 arg = Fcar (arg);
4528 CHECK_NUMBER (arg);
4529 window_scroll (window, XINT (arg), 0, 1);
4532 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4533 unbind_to (count, Qnil);
4535 return Qnil;
4538 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
4539 doc: /* Scroll selected window display ARG columns left.
4540 Default for ARG is window width minus 2.
4541 Value is the total amount of leftward horizontal scrolling in
4542 effect after the change.
4543 If `automatic-hscrolling' is non-nil, the argument ARG modifies
4544 a lower bound for automatic scrolling, i.e. automatic scrolling
4545 will not scroll a window to a column less than the value returned
4546 by this function. */)
4547 (arg)
4548 register Lisp_Object arg;
4550 Lisp_Object result;
4551 int hscroll;
4552 struct window *w = XWINDOW (selected_window);
4554 if (NILP (arg))
4555 XSETFASTINT (arg, window_internal_width (w) - 2);
4556 else
4557 arg = Fprefix_numeric_value (arg);
4559 hscroll = XINT (w->hscroll) + XINT (arg);
4560 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4562 if (interactive_p (0))
4563 w->min_hscroll = w->hscroll;
4565 return result;
4568 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
4569 doc: /* Scroll selected window display ARG columns right.
4570 Default for ARG is window width minus 2.
4571 Value is the total amount of leftward horizontal scrolling in
4572 effect after the change.
4573 If `automatic-hscrolling' is non-nil, the argument ARG modifies
4574 a lower bound for automatic scrolling, i.e. automatic scrolling
4575 will not scroll a window to a column less than the value returned
4576 by this function. */)
4577 (arg)
4578 register Lisp_Object arg;
4580 Lisp_Object result;
4581 int hscroll;
4582 struct window *w = XWINDOW (selected_window);
4584 if (NILP (arg))
4585 XSETFASTINT (arg, window_internal_width (w) - 2);
4586 else
4587 arg = Fprefix_numeric_value (arg);
4589 hscroll = XINT (w->hscroll) - XINT (arg);
4590 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4592 if (interactive_p (0))
4593 w->min_hscroll = w->hscroll;
4595 return result;
4598 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
4599 doc: /* Return the window which was selected when entering the minibuffer.
4600 Returns nil, if current window is not a minibuffer window. */)
4603 if (minibuf_level > 0
4604 && MINI_WINDOW_P (XWINDOW (selected_window))
4605 && !NILP (minibuf_selected_window)
4606 && WINDOW_LIVE_P (minibuf_selected_window))
4607 return minibuf_selected_window;
4609 return Qnil;
4612 /* Value is the number of lines actually displayed in window W,
4613 as opposed to its height. */
4615 static int
4616 displayed_window_lines (w)
4617 struct window *w;
4619 struct it it;
4620 struct text_pos start;
4621 int height = window_box_height (w);
4622 struct buffer *old_buffer;
4623 int bottom_y;
4625 if (XBUFFER (w->buffer) != current_buffer)
4627 old_buffer = current_buffer;
4628 set_buffer_internal (XBUFFER (w->buffer));
4630 else
4631 old_buffer = NULL;
4633 /* In case W->start is out of the accessible range, do something
4634 reasonable. This happens in Info mode when Info-scroll-down
4635 calls (recenter -1) while W->start is 1. */
4636 if (XMARKER (w->start)->charpos < BEGV)
4637 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4638 else if (XMARKER (w->start)->charpos > ZV)
4639 SET_TEXT_POS (start, ZV, ZV_BYTE);
4640 else
4641 SET_TEXT_POS_FROM_MARKER (start, w->start);
4643 start_display (&it, w, start);
4644 move_it_vertically (&it, height);
4645 bottom_y = line_bottom_y (&it);
4647 /* Add in empty lines at the bottom of the window. */
4648 if (bottom_y < height)
4650 int uy = CANON_Y_UNIT (it.f);
4651 it.vpos += (height - bottom_y + uy - 1) / uy;
4654 if (old_buffer)
4655 set_buffer_internal (old_buffer);
4657 return it.vpos;
4661 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
4662 doc: /* Center point in window and redisplay frame.
4663 With prefix argument ARG, recenter putting point on screen line ARG
4664 relative to the current window. If ARG is negative, it counts up from the
4665 bottom of the window. (ARG should be less than the height of the window.)
4667 If ARG is omitted or nil, erase the entire frame and then
4668 redraw with point in the center of the current window.
4669 Just C-u as prefix means put point in the center of the window
4670 and redisplay normally--don't erase and redraw the frame. */)
4671 (arg)
4672 register Lisp_Object arg;
4674 struct window *w = XWINDOW (selected_window);
4675 struct buffer *buf = XBUFFER (w->buffer);
4676 struct buffer *obuf = current_buffer;
4677 int center_p = 0;
4678 int charpos, bytepos;
4680 /* If redisplay is suppressed due to an error, try again. */
4681 obuf->display_error_modiff = 0;
4683 if (NILP (arg))
4685 int i;
4687 /* Invalidate pixel data calculated for all compositions. */
4688 for (i = 0; i < n_compositions; i++)
4689 composition_table[i]->font = NULL;
4691 Fredraw_frame (w->frame);
4692 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
4693 center_p = 1;
4695 else if (CONSP (arg)) /* Just C-u. */
4696 center_p = 1;
4697 else
4699 arg = Fprefix_numeric_value (arg);
4700 CHECK_NUMBER (arg);
4703 set_buffer_internal (buf);
4705 /* Handle centering on a graphical frame specially. Such frames can
4706 have variable-height lines and centering point on the basis of
4707 line counts would lead to strange effects. */
4708 if (FRAME_WINDOW_P (XFRAME (w->frame)))
4710 if (center_p)
4712 struct it it;
4713 struct text_pos pt;
4715 SET_TEXT_POS (pt, PT, PT_BYTE);
4716 start_display (&it, w, pt);
4717 move_it_vertically (&it, - window_box_height (w) / 2);
4718 charpos = IT_CHARPOS (it);
4719 bytepos = IT_BYTEPOS (it);
4721 else if (XINT (arg) < 0)
4723 struct it it;
4724 struct text_pos pt;
4725 int y0, y1, h, nlines;
4727 SET_TEXT_POS (pt, PT, PT_BYTE);
4728 start_display (&it, w, pt);
4729 y0 = it.current_y;
4731 /* The amount of pixels we have to move back is the window
4732 height minus what's displayed in the line containing PT,
4733 and the lines below. */
4734 nlines = - XINT (arg) - 1;
4735 move_it_by_lines (&it, nlines, 1);
4737 y1 = line_bottom_y (&it);
4739 /* If we can't move down NLINES lines because we hit
4740 the end of the buffer, count in some empty lines. */
4741 if (it.vpos < nlines)
4742 y1 += (nlines - it.vpos) * CANON_Y_UNIT (it.f);
4744 h = window_box_height (w) - (y1 - y0);
4746 start_display (&it, w, pt);
4747 move_it_vertically (&it, - h);
4748 charpos = IT_CHARPOS (it);
4749 bytepos = IT_BYTEPOS (it);
4751 else
4753 struct position pos;
4754 pos = *vmotion (PT, - XINT (arg), w);
4755 charpos = pos.bufpos;
4756 bytepos = pos.bytepos;
4759 else
4761 struct position pos;
4762 int ht = window_internal_height (w);
4764 if (center_p)
4765 arg = make_number (ht / 2);
4766 else if (XINT (arg) < 0)
4767 arg = make_number (XINT (arg) + ht);
4769 pos = *vmotion (PT, - XINT (arg), w);
4770 charpos = pos.bufpos;
4771 bytepos = pos.bytepos;
4774 /* Set the new window start. */
4775 set_marker_both (w->start, w->buffer, charpos, bytepos);
4776 w->window_end_valid = Qnil;
4777 w->force_start = Qt;
4778 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
4779 w->start_at_line_beg = Qt;
4780 else
4781 w->start_at_line_beg = Qnil;
4783 set_buffer_internal (obuf);
4784 return Qnil;
4788 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
4789 0, 1, 0,
4790 doc: /* Return the height in lines of the text display area of WINDOW.
4791 This doesn't include the mode-line (or header-line if any) or any
4792 partial-height lines in the text display area. */)
4793 (window)
4794 Lisp_Object window;
4796 struct window *w = decode_window (window);
4797 int pixel_height = window_box_height (w);
4798 int line_height = pixel_height / CANON_Y_UNIT (XFRAME (w->frame));
4799 return make_number (line_height);
4804 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
4805 1, 1, "P",
4806 doc: /* Position point relative to window.
4807 With no argument, position point at center of window.
4808 An argument specifies vertical position within the window;
4809 zero means top of window, negative means relative to bottom of window. */)
4810 (arg)
4811 Lisp_Object arg;
4813 struct window *w = XWINDOW (selected_window);
4814 int lines, start;
4815 Lisp_Object window;
4817 window = selected_window;
4818 start = marker_position (w->start);
4819 if (start < BEGV || start > ZV)
4821 int height = window_internal_height (w);
4822 Fvertical_motion (make_number (- (height / 2)), window);
4823 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
4824 w->start_at_line_beg = Fbolp ();
4825 w->force_start = Qt;
4827 else
4828 Fgoto_char (w->start);
4830 lines = displayed_window_lines (w);
4831 if (NILP (arg))
4832 XSETFASTINT (arg, lines / 2);
4833 else
4835 arg = Fprefix_numeric_value (arg);
4836 if (XINT (arg) < 0)
4837 XSETINT (arg, XINT (arg) + lines);
4840 /* Skip past a partially visible first line. */
4841 if (w->vscroll)
4842 XSETINT (arg, XINT (arg) + 1);
4844 return Fvertical_motion (arg, window);
4849 /***********************************************************************
4850 Window Configuration
4851 ***********************************************************************/
4853 struct save_window_data
4855 EMACS_INT size_from_Lisp_Vector_struct;
4856 struct Lisp_Vector *next_from_Lisp_Vector_struct;
4857 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
4858 Lisp_Object frame_tool_bar_lines;
4859 Lisp_Object selected_frame;
4860 Lisp_Object current_window;
4861 Lisp_Object current_buffer;
4862 Lisp_Object minibuf_scroll_window;
4863 Lisp_Object minibuf_selected_window;
4864 Lisp_Object root_window;
4865 Lisp_Object focus_frame;
4866 /* Record the values of window-min-width and window-min-height
4867 so that window sizes remain consistent with them. */
4868 Lisp_Object min_width, min_height;
4869 /* A vector, each of whose elements is a struct saved_window
4870 for one window. */
4871 Lisp_Object saved_windows;
4874 /* This is saved as a Lisp_Vector */
4875 struct saved_window
4877 /* these first two must agree with struct Lisp_Vector in lisp.h */
4878 EMACS_INT size_from_Lisp_Vector_struct;
4879 struct Lisp_Vector *next_from_Lisp_Vector_struct;
4881 Lisp_Object window;
4882 Lisp_Object buffer, start, pointm, mark;
4883 Lisp_Object left, top, width, height, hscroll, min_hscroll;
4884 Lisp_Object parent, prev;
4885 Lisp_Object start_at_line_beg;
4886 Lisp_Object display_table;
4887 Lisp_Object orig_top, orig_height;
4890 #define SAVED_WINDOW_VECTOR_SIZE 17 /* Arg to Fmake_vector */
4892 #define SAVED_WINDOW_N(swv,n) \
4893 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
4895 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
4896 doc: /* Return t if OBJECT is a window-configuration object. */)
4897 (object)
4898 Lisp_Object object;
4900 if (WINDOW_CONFIGURATIONP (object))
4901 return Qt;
4902 return Qnil;
4905 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
4906 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
4907 (config)
4908 Lisp_Object config;
4910 register struct save_window_data *data;
4911 struct Lisp_Vector *saved_windows;
4913 if (! WINDOW_CONFIGURATIONP (config))
4914 wrong_type_argument (Qwindow_configuration_p, config);
4916 data = (struct save_window_data *) XVECTOR (config);
4917 saved_windows = XVECTOR (data->saved_windows);
4918 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
4921 DEFUN ("set-window-configuration", Fset_window_configuration,
4922 Sset_window_configuration, 1, 1, 0,
4923 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
4924 CONFIGURATION must be a value previously returned
4925 by `current-window-configuration' (which see).
4926 If CONFIGURATION was made from a frame that is now deleted,
4927 only frame-independent values can be restored. In this case,
4928 the return value is nil. Otherwise the value is t. */)
4929 (configuration)
4930 Lisp_Object configuration;
4932 register struct save_window_data *data;
4933 struct Lisp_Vector *saved_windows;
4934 Lisp_Object new_current_buffer;
4935 Lisp_Object frame;
4936 FRAME_PTR f;
4937 int old_point = -1;
4939 while (!WINDOW_CONFIGURATIONP (configuration))
4940 wrong_type_argument (Qwindow_configuration_p, configuration);
4942 data = (struct save_window_data *) XVECTOR (configuration);
4943 saved_windows = XVECTOR (data->saved_windows);
4945 new_current_buffer = data->current_buffer;
4946 if (NILP (XBUFFER (new_current_buffer)->name))
4947 new_current_buffer = Qnil;
4948 else
4950 if (XBUFFER (new_current_buffer) == current_buffer)
4951 old_point = PT;
4954 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
4955 f = XFRAME (frame);
4957 /* If f is a dead frame, don't bother rebuilding its window tree.
4958 However, there is other stuff we should still try to do below. */
4959 if (FRAME_LIVE_P (f))
4961 register struct window *w;
4962 register struct saved_window *p;
4963 struct window *root_window;
4964 struct window **leaf_windows;
4965 int n_leaf_windows;
4966 int k, i, n;
4968 /* If the frame has been resized since this window configuration was
4969 made, we change the frame to the size specified in the
4970 configuration, restore the configuration, and then resize it
4971 back. We keep track of the prevailing height in these variables. */
4972 int previous_frame_height = FRAME_HEIGHT (f);
4973 int previous_frame_width = FRAME_WIDTH (f);
4974 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
4975 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
4977 /* The mouse highlighting code could get screwed up
4978 if it runs during this. */
4979 BLOCK_INPUT;
4981 if (XFASTINT (data->frame_height) != previous_frame_height
4982 || XFASTINT (data->frame_width) != previous_frame_width)
4983 change_frame_size (f, XFASTINT (data->frame_height),
4984 XFASTINT (data->frame_width), 0, 0, 0);
4985 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
4986 if (XFASTINT (data->frame_menu_bar_lines)
4987 != previous_frame_menu_bar_lines)
4988 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
4989 #ifdef HAVE_WINDOW_SYSTEM
4990 if (XFASTINT (data->frame_tool_bar_lines)
4991 != previous_frame_tool_bar_lines)
4992 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
4993 #endif
4994 #endif
4996 /* "Swap out" point from the selected window
4997 into its buffer. We do this now, before
4998 restoring the window contents, and prevent it from
4999 being done later on when we select a new window. */
5000 if (! NILP (XWINDOW (selected_window)->buffer))
5002 w = XWINDOW (selected_window);
5003 set_marker_both (w->pointm,
5004 w->buffer,
5005 BUF_PT (XBUFFER (w->buffer)),
5006 BUF_PT_BYTE (XBUFFER (w->buffer)));
5009 windows_or_buffers_changed++;
5010 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5012 /* Problem: Freeing all matrices and later allocating them again
5013 is a serious redisplay flickering problem. What we would
5014 really like to do is to free only those matrices not reused
5015 below. */
5016 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5017 leaf_windows
5018 = (struct window **) alloca (count_windows (root_window)
5019 * sizeof (struct window *));
5020 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5022 /* Temporarily avoid any problems with windows that are smaller
5023 than they are supposed to be. */
5024 window_min_height = 1;
5025 window_min_width = 1;
5027 /* Kludge Alert!
5028 Mark all windows now on frame as "deleted".
5029 Restoring the new configuration "undeletes" any that are in it.
5031 Save their current buffers in their height fields, since we may
5032 need it later, if a buffer saved in the configuration is now
5033 dead. */
5034 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5036 for (k = 0; k < saved_windows->size; k++)
5038 p = SAVED_WINDOW_N (saved_windows, k);
5039 w = XWINDOW (p->window);
5040 w->next = Qnil;
5042 if (!NILP (p->parent))
5043 w->parent = SAVED_WINDOW_N (saved_windows,
5044 XFASTINT (p->parent))->window;
5045 else
5046 w->parent = Qnil;
5048 if (!NILP (p->prev))
5050 w->prev = SAVED_WINDOW_N (saved_windows,
5051 XFASTINT (p->prev))->window;
5052 XWINDOW (w->prev)->next = p->window;
5054 else
5056 w->prev = Qnil;
5057 if (!NILP (w->parent))
5059 if (EQ (p->width, XWINDOW (w->parent)->width))
5061 XWINDOW (w->parent)->vchild = p->window;
5062 XWINDOW (w->parent)->hchild = Qnil;
5064 else
5066 XWINDOW (w->parent)->hchild = p->window;
5067 XWINDOW (w->parent)->vchild = Qnil;
5072 /* If we squirreled away the buffer in the window's height,
5073 restore it now. */
5074 if (BUFFERP (w->height))
5075 w->buffer = w->height;
5076 w->left = p->left;
5077 w->top = p->top;
5078 w->width = p->width;
5079 w->height = p->height;
5080 w->hscroll = p->hscroll;
5081 w->min_hscroll = p->min_hscroll;
5082 w->display_table = p->display_table;
5083 w->orig_top = p->orig_top;
5084 w->orig_height = p->orig_height;
5085 XSETFASTINT (w->last_modified, 0);
5086 XSETFASTINT (w->last_overlay_modified, 0);
5088 /* Reinstall the saved buffer and pointers into it. */
5089 if (NILP (p->buffer))
5090 w->buffer = p->buffer;
5091 else
5093 if (!NILP (XBUFFER (p->buffer)->name))
5094 /* If saved buffer is alive, install it. */
5096 w->buffer = p->buffer;
5097 w->start_at_line_beg = p->start_at_line_beg;
5098 set_marker_restricted (w->start, p->start, w->buffer);
5099 set_marker_restricted (w->pointm, p->pointm, w->buffer);
5100 Fset_marker (XBUFFER (w->buffer)->mark,
5101 p->mark, w->buffer);
5103 /* As documented in Fcurrent_window_configuration, don't
5104 save the location of point in the buffer which was current
5105 when the window configuration was recorded. */
5106 if (!EQ (p->buffer, new_current_buffer)
5107 && XBUFFER (p->buffer) == current_buffer)
5108 Fgoto_char (w->pointm);
5110 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
5111 /* Else unless window has a live buffer, get one. */
5113 w->buffer = Fcdr (Fcar (Vbuffer_alist));
5114 /* This will set the markers to beginning of visible
5115 range. */
5116 set_marker_restricted (w->start, make_number (0), w->buffer);
5117 set_marker_restricted (w->pointm, make_number (0),w->buffer);
5118 w->start_at_line_beg = Qt;
5120 else
5121 /* Keeping window's old buffer; make sure the markers
5122 are real. */
5124 /* Set window markers at start of visible range. */
5125 if (XMARKER (w->start)->buffer == 0)
5126 set_marker_restricted (w->start, make_number (0),
5127 w->buffer);
5128 if (XMARKER (w->pointm)->buffer == 0)
5129 set_marker_restricted_both (w->pointm, w->buffer,
5130 BUF_PT (XBUFFER (w->buffer)),
5131 BUF_PT_BYTE (XBUFFER (w->buffer)));
5132 w->start_at_line_beg = Qt;
5137 FRAME_ROOT_WINDOW (f) = data->root_window;
5138 /* Prevent "swapping out point" in the old selected window
5139 using the buffer that has been restored into it.
5140 That swapping out has already been done,
5141 near the beginning of this function. */
5142 selected_window = Qnil;
5143 Fselect_window (data->current_window);
5144 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
5145 = selected_window;
5147 if (NILP (data->focus_frame)
5148 || (FRAMEP (data->focus_frame)
5149 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5150 Fredirect_frame_focus (frame, data->focus_frame);
5152 #if 0 /* I don't understand why this is needed, and it causes problems
5153 when the frame's old selected window has been deleted. */
5154 if (f != selected_frame && FRAME_WINDOW_P (f))
5155 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
5156 0, 0);
5157 #endif
5159 /* Set the screen height to the value it had before this function. */
5160 if (previous_frame_height != FRAME_HEIGHT (f)
5161 || previous_frame_width != FRAME_WIDTH (f))
5162 change_frame_size (f, previous_frame_height, previous_frame_width,
5163 0, 0, 0);
5164 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5165 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5166 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5167 make_number (0));
5168 #ifdef HAVE_WINDOW_SYSTEM
5169 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5170 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5171 make_number (0));
5172 #endif
5173 #endif
5175 /* Now, free glyph matrices in windows that were not reused. */
5176 for (i = n = 0; i < n_leaf_windows; ++i)
5178 if (NILP (leaf_windows[i]->buffer))
5180 /* Assert it's not reused as a combination. */
5181 xassert (NILP (leaf_windows[i]->hchild)
5182 && NILP (leaf_windows[i]->vchild));
5183 free_window_matrices (leaf_windows[i]);
5185 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5186 ++n;
5189 /* If more than one window shows the new and old current buffer,
5190 don't try to preserve point in that buffer. */
5191 if (old_point > 0 && n > 1)
5192 old_point = -1;
5194 adjust_glyphs (f);
5196 UNBLOCK_INPUT;
5198 /* Fselect_window will have made f the selected frame, so we
5199 reselect the proper frame here. Fhandle_switch_frame will change the
5200 selected window too, but that doesn't make the call to
5201 Fselect_window above totally superfluous; it still sets f's
5202 selected window. */
5203 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5204 do_switch_frame (data->selected_frame, 0, 0);
5206 if (! NILP (Vwindow_configuration_change_hook)
5207 && ! NILP (Vrun_hooks))
5208 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
5211 if (!NILP (new_current_buffer))
5213 Fset_buffer (new_current_buffer);
5215 /* If the buffer that is current now is the same
5216 that was current before setting the window configuration,
5217 don't alter its PT. */
5218 if (old_point >= 0)
5219 SET_PT (old_point);
5222 /* Restore the minimum heights recorded in the configuration. */
5223 window_min_height = XINT (data->min_height);
5224 window_min_width = XINT (data->min_width);
5226 Vminibuf_scroll_window = data->minibuf_scroll_window;
5227 minibuf_selected_window = data->minibuf_selected_window;
5229 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5232 /* Mark all windows now on frame as deleted
5233 by setting their buffers to nil. */
5235 void
5236 delete_all_subwindows (w)
5237 register struct window *w;
5239 if (!NILP (w->next))
5240 delete_all_subwindows (XWINDOW (w->next));
5241 if (!NILP (w->vchild))
5242 delete_all_subwindows (XWINDOW (w->vchild));
5243 if (!NILP (w->hchild))
5244 delete_all_subwindows (XWINDOW (w->hchild));
5246 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
5248 if (!NILP (w->buffer))
5249 unshow_buffer (w);
5251 /* We set all three of these fields to nil, to make sure that we can
5252 distinguish this dead window from any live window. Live leaf
5253 windows will have buffer set, and combination windows will have
5254 vchild or hchild set. */
5255 w->buffer = Qnil;
5256 w->vchild = Qnil;
5257 w->hchild = Qnil;
5259 Vwindow_list = Qnil;
5262 static int
5263 count_windows (window)
5264 register struct window *window;
5266 register int count = 1;
5267 if (!NILP (window->next))
5268 count += count_windows (XWINDOW (window->next));
5269 if (!NILP (window->vchild))
5270 count += count_windows (XWINDOW (window->vchild));
5271 if (!NILP (window->hchild))
5272 count += count_windows (XWINDOW (window->hchild));
5273 return count;
5277 /* Fill vector FLAT with leaf windows under W, starting at index I.
5278 Value is last index + 1. */
5280 static int
5281 get_leaf_windows (w, flat, i)
5282 struct window *w;
5283 struct window **flat;
5284 int i;
5286 while (w)
5288 if (!NILP (w->hchild))
5289 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5290 else if (!NILP (w->vchild))
5291 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5292 else
5293 flat[i++] = w;
5295 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5298 return i;
5302 /* Return a pointer to the glyph W's physical cursor is on. Value is
5303 null if W's current matrix is invalid, so that no meaningfull glyph
5304 can be returned. */
5306 struct glyph *
5307 get_phys_cursor_glyph (w)
5308 struct window *w;
5310 struct glyph_row *row;
5311 struct glyph *glyph;
5313 if (w->phys_cursor.vpos >= 0
5314 && w->phys_cursor.vpos < w->current_matrix->nrows
5315 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
5316 row->enabled_p)
5317 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
5318 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
5319 else
5320 glyph = NULL;
5322 return glyph;
5326 static int
5327 save_window_save (window, vector, i)
5328 Lisp_Object window;
5329 struct Lisp_Vector *vector;
5330 int i;
5332 register struct saved_window *p;
5333 register struct window *w;
5334 register Lisp_Object tem;
5336 for (;!NILP (window); window = w->next)
5338 p = SAVED_WINDOW_N (vector, i);
5339 w = XWINDOW (window);
5341 XSETFASTINT (w->temslot, i++);
5342 p->window = window;
5343 p->buffer = w->buffer;
5344 p->left = w->left;
5345 p->top = w->top;
5346 p->width = w->width;
5347 p->height = w->height;
5348 p->hscroll = w->hscroll;
5349 p->min_hscroll = w->min_hscroll;
5350 p->display_table = w->display_table;
5351 p->orig_top = w->orig_top;
5352 p->orig_height = w->orig_height;
5353 if (!NILP (w->buffer))
5355 /* Save w's value of point in the window configuration.
5356 If w is the selected window, then get the value of point
5357 from the buffer; pointm is garbage in the selected window. */
5358 if (EQ (window, selected_window))
5360 p->pointm = Fmake_marker ();
5361 set_marker_both (p->pointm, w->buffer,
5362 BUF_PT (XBUFFER (w->buffer)),
5363 BUF_PT_BYTE (XBUFFER (w->buffer)));
5365 else
5366 p->pointm = Fcopy_marker (w->pointm, Qnil);
5368 p->start = Fcopy_marker (w->start, Qnil);
5369 p->start_at_line_beg = w->start_at_line_beg;
5371 tem = XBUFFER (w->buffer)->mark;
5372 p->mark = Fcopy_marker (tem, Qnil);
5374 else
5376 p->pointm = Qnil;
5377 p->start = Qnil;
5378 p->mark = Qnil;
5379 p->start_at_line_beg = Qnil;
5382 if (NILP (w->parent))
5383 p->parent = Qnil;
5384 else
5385 p->parent = XWINDOW (w->parent)->temslot;
5387 if (NILP (w->prev))
5388 p->prev = Qnil;
5389 else
5390 p->prev = XWINDOW (w->prev)->temslot;
5392 if (!NILP (w->vchild))
5393 i = save_window_save (w->vchild, vector, i);
5394 if (!NILP (w->hchild))
5395 i = save_window_save (w->hchild, vector, i);
5398 return i;
5401 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
5402 Scurrent_window_configuration, 0, 1, 0,
5403 doc: /* Return an object representing the current window configuration of FRAME.
5404 If FRAME is nil or omitted, use the selected frame.
5405 This describes the number of windows, their sizes and current buffers,
5406 and for each displayed buffer, where display starts, and the positions of
5407 point and mark. An exception is made for point in the current buffer:
5408 its value is -not- saved.
5409 This also records the currently selected frame, and FRAME's focus
5410 redirection (see `redirect-frame-focus'). */)
5411 (frame)
5412 Lisp_Object frame;
5414 register Lisp_Object tem;
5415 register int n_windows;
5416 register struct save_window_data *data;
5417 register struct Lisp_Vector *vec;
5418 register int i;
5419 FRAME_PTR f;
5421 if (NILP (frame))
5422 frame = selected_frame;
5423 CHECK_LIVE_FRAME (frame);
5424 f = XFRAME (frame);
5426 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5427 vec = allocate_other_vector (VECSIZE (struct save_window_data));
5428 data = (struct save_window_data *)vec;
5430 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
5431 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
5432 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
5433 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
5434 data->selected_frame = selected_frame;
5435 data->current_window = FRAME_SELECTED_WINDOW (f);
5436 XSETBUFFER (data->current_buffer, current_buffer);
5437 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
5438 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
5439 data->root_window = FRAME_ROOT_WINDOW (f);
5440 data->focus_frame = FRAME_FOCUS_FRAME (f);
5441 XSETINT (data->min_height, window_min_height);
5442 XSETINT (data->min_width, window_min_width);
5443 tem = Fmake_vector (make_number (n_windows), Qnil);
5444 data->saved_windows = tem;
5445 for (i = 0; i < n_windows; i++)
5446 XVECTOR (tem)->contents[i]
5447 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
5448 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
5449 XSETWINDOW_CONFIGURATION (tem, data);
5450 return (tem);
5453 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
5454 0, UNEVALLED, 0,
5455 doc: /* Execute body, preserving window sizes and contents.
5456 Restore which buffer appears in which window, where display starts,
5457 and the value of point and mark for each window.
5458 Also restore the choice of selected window.
5459 Also restore which buffer is current.
5460 Does not restore the value of point in current buffer.
5461 usage: (save-window-excursion BODY ...) */)
5462 (args)
5463 Lisp_Object args;
5465 register Lisp_Object val;
5466 register int count = specpdl_ptr - specpdl;
5468 record_unwind_protect (Fset_window_configuration,
5469 Fcurrent_window_configuration (Qnil));
5470 val = Fprogn (args);
5471 return unbind_to (count, val);
5475 /***********************************************************************
5476 Marginal Areas
5477 ***********************************************************************/
5479 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
5480 2, 3, 0,
5481 doc: /* Set width of marginal areas of window WINDOW.
5482 If window is nil, set margins of the currently selected window.
5483 First parameter LEFT-WIDTH specifies the number of character
5484 cells to reserve for the left marginal area. Second parameter
5485 RIGHT-WIDTH does the same for the right marginal area.
5486 A nil width parameter means no margin. */)
5487 (window, left, right)
5488 Lisp_Object window, left, right;
5490 struct window *w = decode_window (window);
5492 if (!NILP (left))
5493 CHECK_NUMBER_OR_FLOAT (left);
5494 if (!NILP (right))
5495 CHECK_NUMBER_OR_FLOAT (right);
5497 /* Check widths < 0 and translate a zero width to nil.
5498 Margins that are too wide have to be checked elsewhere. */
5499 if ((INTEGERP (left) && XINT (left) < 0)
5500 || (FLOATP (left) && XFLOAT_DATA (left) <= 0))
5501 XSETFASTINT (left, 0);
5502 if (INTEGERP (left) && XFASTINT (left) == 0)
5503 left = Qnil;
5505 if ((INTEGERP (right) && XINT (right) < 0)
5506 || (FLOATP (right) && XFLOAT_DATA (right) <= 0))
5507 XSETFASTINT (right, 0);
5508 if (INTEGERP (right) && XFASTINT (right) == 0)
5509 right = Qnil;
5511 w->left_margin_width = left;
5512 w->right_margin_width = right;
5514 ++windows_or_buffers_changed;
5515 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
5516 return Qnil;
5520 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
5521 0, 1, 0,
5522 doc: /* Get width of marginal areas of window WINDOW.
5523 If WINDOW is omitted or nil, use the currently selected window.
5524 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
5525 If a marginal area does not exist, its width will be returned
5526 as nil. */)
5527 (window)
5528 Lisp_Object window;
5530 struct window *w = decode_window (window);
5531 return Fcons (w->left_margin_width, w->right_margin_width);
5536 /***********************************************************************
5537 Smooth scrolling
5538 ***********************************************************************/
5540 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 1, 0,
5541 doc: /* Return the amount by which WINDOW is scrolled vertically.
5542 Use the selected window if WINDOW is nil or omitted.
5543 Value is a multiple of the canonical character height of WINDOW. */)
5544 (window)
5545 Lisp_Object window;
5547 Lisp_Object result;
5548 struct frame *f;
5549 struct window *w;
5551 if (NILP (window))
5552 window = selected_window;
5553 else
5554 CHECK_WINDOW (window);
5555 w = XWINDOW (window);
5556 f = XFRAME (w->frame);
5558 if (FRAME_WINDOW_P (f))
5559 result = CANON_Y_FROM_PIXEL_Y (f, -w->vscroll);
5560 else
5561 result = make_number (0);
5562 return result;
5566 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
5567 2, 2, 0,
5568 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
5569 WINDOW nil means use the selected window. VSCROLL is a non-negative
5570 multiple of the canonical character height of WINDOW. */)
5571 (window, vscroll)
5572 Lisp_Object window, vscroll;
5574 struct window *w;
5575 struct frame *f;
5577 if (NILP (window))
5578 window = selected_window;
5579 else
5580 CHECK_WINDOW (window);
5581 CHECK_NUMBER_OR_FLOAT (vscroll);
5583 w = XWINDOW (window);
5584 f = XFRAME (w->frame);
5586 if (FRAME_WINDOW_P (f))
5588 int old_dy = w->vscroll;
5590 w->vscroll = - CANON_Y_UNIT (f) * XFLOATINT (vscroll);
5591 w->vscroll = min (w->vscroll, 0);
5593 /* Adjust glyph matrix of the frame if the virtual display
5594 area becomes larger than before. */
5595 if (w->vscroll < 0 && w->vscroll < old_dy)
5596 adjust_glyphs (f);
5598 /* Prevent redisplay shortcuts. */
5599 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
5602 return Fwindow_vscroll (window);
5606 /* Call FN for all leaf windows on frame F. FN is called with the
5607 first argument being a pointer to the leaf window, and with
5608 additional argument USER_DATA. Stops when FN returns 0. */
5610 void
5611 foreach_window (f, fn, user_data)
5612 struct frame *f;
5613 int (* fn) P_ ((struct window *, void *));
5614 void *user_data;
5616 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
5620 /* Helper function for foreach_window. Call FN for all leaf windows
5621 reachable from W. FN is called with the first argument being a
5622 pointer to the leaf window, and with additional argument USER_DATA.
5623 Stop when FN returns 0. Value is 0 if stopped by FN. */
5625 static int
5626 foreach_window_1 (w, fn, user_data)
5627 struct window *w;
5628 int (* fn) P_ ((struct window *, void *));
5629 void *user_data;
5631 int cont;
5633 for (cont = 1; w && cont;)
5635 if (!NILP (w->hchild))
5636 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
5637 else if (!NILP (w->vchild))
5638 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
5639 else
5640 cont = fn (w, user_data);
5642 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5645 return cont;
5649 /* Freeze or unfreeze the window start of W unless it is a
5650 mini-window or the selected window. FREEZE_P non-null means freeze
5651 the window start. */
5653 static int
5654 freeze_window_start (w, freeze_p)
5655 struct window *w;
5656 void *freeze_p;
5658 if (w == XWINDOW (selected_window)
5659 || MINI_WINDOW_P (w)
5660 || (MINI_WINDOW_P (XWINDOW (selected_window))
5661 && ! NILP (Vminibuf_scroll_window)
5662 && w == XWINDOW (Vminibuf_scroll_window)))
5663 freeze_p = NULL;
5665 w->frozen_window_start_p = freeze_p != NULL;
5666 return 1;
5670 /* Freeze or unfreeze the window starts of all leaf windows on frame
5671 F, except the selected window and a mini-window. FREEZE_P non-zero
5672 means freeze the window start. */
5674 void
5675 freeze_window_starts (f, freeze_p)
5676 struct frame *f;
5677 int freeze_p;
5679 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
5683 /***********************************************************************
5684 Initialization
5685 ***********************************************************************/
5687 /* Return 1 if window configurations C1 and C2
5688 describe the same state of affairs. This is used by Fequal. */
5691 compare_window_configurations (c1, c2, ignore_positions)
5692 Lisp_Object c1, c2;
5693 int ignore_positions;
5695 register struct save_window_data *d1, *d2;
5696 struct Lisp_Vector *sw1, *sw2;
5697 int i;
5699 if (!WINDOW_CONFIGURATIONP (c1))
5700 wrong_type_argument (Qwindow_configuration_p, c1);
5701 if (!WINDOW_CONFIGURATIONP (c2))
5702 wrong_type_argument (Qwindow_configuration_p, c2);
5704 d1 = (struct save_window_data *) XVECTOR (c1);
5705 d2 = (struct save_window_data *) XVECTOR (c2);
5706 sw1 = XVECTOR (d1->saved_windows);
5707 sw2 = XVECTOR (d2->saved_windows);
5709 if (! EQ (d1->frame_width, d2->frame_width))
5710 return 0;
5711 if (! EQ (d1->frame_height, d2->frame_height))
5712 return 0;
5713 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
5714 return 0;
5715 if (! EQ (d1->selected_frame, d2->selected_frame))
5716 return 0;
5717 /* Don't compare the current_window field directly.
5718 Instead see w1_is_current and w2_is_current, below. */
5719 if (! EQ (d1->current_buffer, d2->current_buffer))
5720 return 0;
5721 if (! ignore_positions)
5723 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
5724 return 0;
5725 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
5726 return 0;
5728 /* Don't compare the root_window field.
5729 We don't require the two configurations
5730 to use the same window object,
5731 and the two root windows must be equivalent
5732 if everything else compares equal. */
5733 if (! EQ (d1->focus_frame, d2->focus_frame))
5734 return 0;
5735 if (! EQ (d1->min_width, d2->min_width))
5736 return 0;
5737 if (! EQ (d1->min_height, d2->min_height))
5738 return 0;
5740 /* Verify that the two confis have the same number of windows. */
5741 if (sw1->size != sw2->size)
5742 return 0;
5744 for (i = 0; i < sw1->size; i++)
5746 struct saved_window *p1, *p2;
5747 int w1_is_current, w2_is_current;
5749 p1 = SAVED_WINDOW_N (sw1, i);
5750 p2 = SAVED_WINDOW_N (sw2, i);
5752 /* Verify that the current windows in the two
5753 configurations correspond to each other. */
5754 w1_is_current = EQ (d1->current_window, p1->window);
5755 w2_is_current = EQ (d2->current_window, p2->window);
5757 if (w1_is_current != w2_is_current)
5758 return 0;
5760 /* Verify that the corresponding windows do match. */
5761 if (! EQ (p1->buffer, p2->buffer))
5762 return 0;
5763 if (! EQ (p1->left, p2->left))
5764 return 0;
5765 if (! EQ (p1->top, p2->top))
5766 return 0;
5767 if (! EQ (p1->width, p2->width))
5768 return 0;
5769 if (! EQ (p1->height, p2->height))
5770 return 0;
5771 if (! EQ (p1->display_table, p2->display_table))
5772 return 0;
5773 if (! EQ (p1->parent, p2->parent))
5774 return 0;
5775 if (! EQ (p1->prev, p2->prev))
5776 return 0;
5777 if (! ignore_positions)
5779 if (! EQ (p1->hscroll, p2->hscroll))
5780 return 0;
5781 if (!EQ (p1->min_hscroll, p2->min_hscroll))
5782 return 0;
5783 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
5784 return 0;
5785 if (NILP (Fequal (p1->start, p2->start)))
5786 return 0;
5787 if (NILP (Fequal (p1->pointm, p2->pointm)))
5788 return 0;
5789 if (NILP (Fequal (p1->mark, p2->mark)))
5790 return 0;
5794 return 1;
5797 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
5798 Scompare_window_configurations, 2, 2, 0,
5799 doc: /* Compare two window configurations as regards the structure of windows.
5800 This function ignores details such as the values of point and mark
5801 and scrolling positions. */)
5802 (x, y)
5803 Lisp_Object x, y;
5805 if (compare_window_configurations (x, y, 1))
5806 return Qt;
5807 return Qnil;
5810 void
5811 init_window_once ()
5813 struct frame *f = make_terminal_frame ();
5814 XSETFRAME (selected_frame, f);
5815 Vterminal_frame = selected_frame;
5816 minibuf_window = f->minibuffer_window;
5817 selected_window = f->selected_window;
5818 last_nonminibuf_frame = f;
5820 window_initialized = 1;
5823 void
5824 init_window ()
5826 Vwindow_list = Qnil;
5829 void
5830 syms_of_window ()
5832 Qwindow_size_fixed = intern ("window-size-fixed");
5833 staticpro (&Qwindow_size_fixed);
5835 staticpro (&Qwindow_configuration_change_hook);
5836 Qwindow_configuration_change_hook
5837 = intern ("window-configuration-change-hook");
5839 Qwindowp = intern ("windowp");
5840 staticpro (&Qwindowp);
5842 Qwindow_configuration_p = intern ("window-configuration-p");
5843 staticpro (&Qwindow_configuration_p);
5845 Qwindow_live_p = intern ("window-live-p");
5846 staticpro (&Qwindow_live_p);
5848 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
5849 staticpro (&Qtemp_buffer_show_hook);
5851 staticpro (&Vwindow_list);
5853 minibuf_selected_window = Qnil;
5854 staticpro (&minibuf_selected_window);
5856 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
5857 doc: /* Non-nil means call as function to display a help buffer.
5858 The function is called with one argument, the buffer to be displayed.
5859 Used by `with-output-to-temp-buffer'.
5860 If this function is used, then it must do the entire job of showing
5861 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
5862 Vtemp_buffer_show_function = Qnil;
5864 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
5865 doc: /* If non-nil, function to call to handle `display-buffer'.
5866 It will receive two args, the buffer and a flag which if non-nil means
5867 that the currently selected window is not acceptable.
5868 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'
5869 work using this function. */);
5870 Vdisplay_buffer_function = Qnil;
5872 DEFVAR_LISP ("even-window-heights", &Veven_window_heights,
5873 doc: /* *If non-nil, `display-buffer' should even the window heights.
5874 If nil, `display-buffer' will leave the window configuration alone. */);
5875 Veven_window_heights = Qt;
5877 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
5878 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
5879 Vminibuf_scroll_window = Qnil;
5881 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
5882 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
5883 If the minibuffer is active, the `minibuffer-scroll-window' mode line
5884 is displayed in the `mode-line' face. */);
5885 mode_line_in_non_selected_windows = 1;
5887 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
5888 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
5889 Vother_window_scroll_buffer = Qnil;
5891 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
5892 doc: /* *Non-nil means `display-buffer' should make a separate frame. */);
5893 pop_up_frames = 0;
5895 DEFVAR_BOOL ("display-buffer-reuse-frames", &display_buffer_reuse_frames,
5896 doc: /* *Non-nil means `display-buffer' should reuse frames.
5897 If the buffer in question is already displayed in a frame, raise that frame. */);
5898 display_buffer_reuse_frames = 0;
5900 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
5901 doc: /* Function to call to handle automatic new frame creation.
5902 It is called with no arguments and should return a newly created frame.
5904 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'
5905 where `pop-up-frame-alist' would hold the default frame parameters. */);
5906 Vpop_up_frame_function = Qnil;
5908 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
5909 doc: /* *List of buffer names that should have their own special frames.
5910 Displaying a buffer whose name is in this list makes a special frame for it
5911 using `special-display-function'. See also `special-display-regexps'.
5913 An element of the list can be a list instead of just a string.
5914 There are two ways to use a list as an element:
5915 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)
5916 In the first case, FRAME-PARAMETERS are used to create the frame.
5917 In the latter case, FUNCTION is called with BUFFER as the first argument,
5918 followed by OTHER-ARGS--it can display BUFFER in any way it likes.
5919 All this is done by the function found in `special-display-function'.
5921 If this variable appears \"not to work\", because you add a name to it
5922 but that buffer still appears in the selected window, look at the
5923 values of `same-window-buffer-names' and `same-window-regexps'.
5924 Those variables take precedence over this one. */);
5925 Vspecial_display_buffer_names = Qnil;
5927 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
5928 doc: /* *List of regexps saying which buffers should have their own special frames.
5929 If a buffer name matches one of these regexps, it gets its own frame.
5930 Displaying a buffer whose name is in this list makes a special frame for it
5931 using `special-display-function'.
5933 An element of the list can be a list instead of just a string.
5934 There are two ways to use a list as an element:
5935 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)
5936 In the first case, FRAME-PARAMETERS are used to create the frame.
5937 In the latter case, FUNCTION is called with the buffer as first argument,
5938 followed by OTHER-ARGS--it can display the buffer in any way it likes.
5939 All this is done by the function found in `special-display-function'.
5941 If this variable appears \"not to work\", because you add a regexp to it
5942 but the matching buffers still appear in the selected window, look at the
5943 values of `same-window-buffer-names' and `same-window-regexps'.
5944 Those variables take precedence over this one. */);
5945 Vspecial_display_regexps = Qnil;
5947 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
5948 doc: /* Function to call to make a new frame for a special buffer.
5949 It is called with two arguments, the buffer and optional buffer specific
5950 data, and should return a window displaying that buffer.
5951 The default value makes a separate frame for the buffer,
5952 using `special-display-frame-alist' to specify the frame parameters.
5954 A buffer is special if its is listed in `special-display-buffer-names'
5955 or matches a regexp in `special-display-regexps'. */);
5956 Vspecial_display_function = Qnil;
5958 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
5959 doc: /* *List of buffer names that should appear in the selected window.
5960 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'
5961 switches to it in the selected window, rather than making it appear
5962 in some other window.
5964 An element of the list can be a cons cell instead of just a string.
5965 Then the car must be a string, which specifies the buffer name.
5966 This is for compatibility with `special-display-buffer-names';
5967 the cdr of the cons cell is ignored.
5969 See also `same-window-regexps'. */);
5970 Vsame_window_buffer_names = Qnil;
5972 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
5973 doc: /* *List of regexps saying which buffers should appear in the selected window.
5974 If a buffer name matches one of these regexps, then displaying it
5975 using `display-buffer' or `pop-to-buffer' switches to it
5976 in the selected window, rather than making it appear in some other window.
5978 An element of the list can be a cons cell instead of just a string.
5979 Then the car must be a string, which specifies the buffer name.
5980 This is for compatibility with `special-display-buffer-names';
5981 the cdr of the cons cell is ignored.
5983 See also `same-window-buffer-names'. */);
5984 Vsame_window_regexps = Qnil;
5986 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
5987 doc: /* *Non-nil means display-buffer should make new windows. */);
5988 pop_up_windows = 1;
5990 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
5991 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
5992 next_screen_context_lines = 2;
5994 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
5995 doc: /* *display-buffer would prefer to split the largest window if this large.
5996 If there is only one window, it is split regardless of this value. */);
5997 split_height_threshold = 500;
5999 DEFVAR_INT ("window-min-height", &window_min_height,
6000 doc: /* *Delete any window less than this tall (including its mode line). */);
6001 window_min_height = 4;
6003 DEFVAR_INT ("window-min-width", &window_min_width,
6004 doc: /* *Delete any window less than this wide. */);
6005 window_min_width = 10;
6007 DEFVAR_LISP ("scroll-preserve-screen-position",
6008 &Vscroll_preserve_screen_position,
6009 doc: /* *Non-nil means scroll commands move point to keep its screen line unchanged. */);
6010 Vscroll_preserve_screen_position = Qnil;
6012 DEFVAR_LISP ("window-configuration-change-hook",
6013 &Vwindow_configuration_change_hook,
6014 doc: /* Functions to call when window configuration changes.
6015 The selected frame is the one whose configuration has changed. */);
6016 Vwindow_configuration_change_hook = Qnil;
6018 DEFVAR_BOOL ("window-size-fixed", &window_size_fixed,
6019 doc: /* Non-nil in a buffer means windows displaying the buffer are fixed-size.
6020 Emacs won't change the size of any window displaying that buffer,
6021 unless you explicitly change the size, or Emacs has no other choice.
6022 This variable automatically becomes buffer-local when set. */);
6023 Fmake_variable_buffer_local (Qwindow_size_fixed);
6024 window_size_fixed = 0;
6026 defsubr (&Sselected_window);
6027 defsubr (&Sminibuffer_window);
6028 defsubr (&Swindow_minibuffer_p);
6029 defsubr (&Swindowp);
6030 defsubr (&Swindow_live_p);
6031 defsubr (&Spos_visible_in_window_p);
6032 defsubr (&Swindow_buffer);
6033 defsubr (&Swindow_height);
6034 defsubr (&Swindow_width);
6035 defsubr (&Swindow_hscroll);
6036 defsubr (&Sset_window_hscroll);
6037 defsubr (&Swindow_redisplay_end_trigger);
6038 defsubr (&Sset_window_redisplay_end_trigger);
6039 defsubr (&Swindow_edges);
6040 defsubr (&Scoordinates_in_window_p);
6041 defsubr (&Swindow_at);
6042 defsubr (&Swindow_point);
6043 defsubr (&Swindow_start);
6044 defsubr (&Swindow_end);
6045 defsubr (&Sset_window_point);
6046 defsubr (&Sset_window_start);
6047 defsubr (&Swindow_dedicated_p);
6048 defsubr (&Sset_window_dedicated_p);
6049 defsubr (&Swindow_display_table);
6050 defsubr (&Sset_window_display_table);
6051 defsubr (&Snext_window);
6052 defsubr (&Sprevious_window);
6053 defsubr (&Sother_window);
6054 defsubr (&Sget_lru_window);
6055 defsubr (&Sget_largest_window);
6056 defsubr (&Sget_buffer_window);
6057 defsubr (&Sdelete_other_windows);
6058 defsubr (&Sdelete_windows_on);
6059 defsubr (&Sreplace_buffer_in_windows);
6060 defsubr (&Sdelete_window);
6061 defsubr (&Sset_window_buffer);
6062 defsubr (&Sselect_window);
6063 defsubr (&Sspecial_display_p);
6064 defsubr (&Ssame_window_p);
6065 defsubr (&Sdisplay_buffer);
6066 defsubr (&Ssplit_window);
6067 defsubr (&Senlarge_window);
6068 defsubr (&Sshrink_window);
6069 defsubr (&Sscroll_up);
6070 defsubr (&Sscroll_down);
6071 defsubr (&Sscroll_left);
6072 defsubr (&Sscroll_right);
6073 defsubr (&Sother_window_for_scrolling);
6074 defsubr (&Sscroll_other_window);
6075 defsubr (&Sminibuffer_selected_window);
6076 defsubr (&Srecenter);
6077 defsubr (&Swindow_text_height);
6078 defsubr (&Smove_to_window_line);
6079 defsubr (&Swindow_configuration_p);
6080 defsubr (&Swindow_configuration_frame);
6081 defsubr (&Sset_window_configuration);
6082 defsubr (&Scurrent_window_configuration);
6083 defsubr (&Ssave_window_excursion);
6084 defsubr (&Sset_window_margins);
6085 defsubr (&Swindow_margins);
6086 defsubr (&Swindow_vscroll);
6087 defsubr (&Sset_window_vscroll);
6088 defsubr (&Scompare_window_configurations);
6089 defsubr (&Swindow_list);
6092 void
6093 keys_of_window ()
6095 initial_define_key (control_x_map, '1', "delete-other-windows");
6096 initial_define_key (control_x_map, '2', "split-window");
6097 initial_define_key (control_x_map, '0', "delete-window");
6098 initial_define_key (control_x_map, 'o', "other-window");
6099 initial_define_key (control_x_map, '^', "enlarge-window");
6100 initial_define_key (control_x_map, '<', "scroll-left");
6101 initial_define_key (control_x_map, '>', "scroll-right");
6103 initial_define_key (global_map, Ctl ('V'), "scroll-up");
6104 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6105 initial_define_key (meta_map, 'v', "scroll-down");
6107 initial_define_key (global_map, Ctl('L'), "recenter");
6108 initial_define_key (meta_map, 'r', "move-to-window-line");