* keyboard.c (Qratio): New symbol.
[emacs.git] / src / window.c
blob0d7cc27b39d1687d4fa73264a491ffb861790f29
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 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "frame.h"
26 #include "window.h"
27 #include "commands.h"
28 #include "indent.h"
29 #include "termchar.h"
30 #include "disptab.h"
31 #include "keyboard.h"
32 #include "dispextern.h"
33 #include "blockinput.h"
34 #include "intervals.h"
36 #ifdef HAVE_X_WINDOWS
37 #include "xterm.h"
38 #endif /* HAVE_X_WINDOWS */
40 #ifndef max
41 #define max(a, b) ((a) < (b) ? (b) : (a))
42 #endif
45 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
46 Lisp_Object Qwindow_size_fixed, Qleft_bitmap_area, Qright_bitmap_area;
47 extern Lisp_Object Qheight, Qwidth;
49 static struct window *decode_window P_ ((Lisp_Object));
50 static Lisp_Object select_window_1 P_ ((Lisp_Object, int));
51 static int count_windows P_ ((struct window *));
52 static int get_leaf_windows P_ ((struct window *, struct window **, int));
53 static void window_scroll P_ ((Lisp_Object, int, int, int));
54 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
55 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
56 static int window_min_size_1 P_ ((struct window *, int));
57 static int window_min_size P_ ((struct window *, int, int, int *));
58 static void size_window P_ ((Lisp_Object, int, int, int));
59 static void foreach_window_1 P_ ((struct window *, void (*fn) (), int, int,
60 int, int));
61 static void freeze_window_start P_ ((struct window *, int));
62 static int window_fixed_size_p P_ ((struct window *, int, int));
63 static void enlarge_window P_ ((Lisp_Object, int, int));
66 /* This is the window in which the terminal's cursor should
67 be left when nothing is being done with it. This must
68 always be a leaf window, and its buffer is selected by
69 the top level editing loop at the end of each command.
71 This value is always the same as
72 FRAME_SELECTED_WINDOW (selected_frame). */
74 Lisp_Object selected_window;
76 /* The mini-buffer window of the selected frame.
77 Note that you cannot test for mini-bufferness of an arbitrary window
78 by comparing against this; but you can test for mini-bufferness of
79 the selected window. */
81 Lisp_Object minibuf_window;
83 /* Non-nil means it is the window for C-M-v to scroll
84 when the mini-buffer is selected. */
86 Lisp_Object Vminibuf_scroll_window;
88 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
90 Lisp_Object Vother_window_scroll_buffer;
92 /* Non-nil means it's function to call to display temp buffers. */
94 Lisp_Object Vtemp_buffer_show_function;
96 /* If a window gets smaller than either of these, it is removed. */
98 int window_min_height;
99 int window_min_width;
101 /* Nonzero implies Fdisplay_buffer should create windows. */
103 int pop_up_windows;
105 /* Nonzero implies make new frames for Fdisplay_buffer. */
107 int pop_up_frames;
109 /* Non-nil means use this function instead of default */
111 Lisp_Object Vpop_up_frame_function;
113 /* Function to call to handle Fdisplay_buffer. */
115 Lisp_Object Vdisplay_buffer_function;
117 /* List of buffer *names* for buffers that should have their own frames. */
119 Lisp_Object Vspecial_display_buffer_names;
121 /* List of regexps for buffer names that should have their own frames. */
123 Lisp_Object Vspecial_display_regexps;
125 /* Function to pop up a special frame. */
127 Lisp_Object Vspecial_display_function;
129 /* List of buffer *names* for buffers to appear in selected window. */
131 Lisp_Object Vsame_window_buffer_names;
133 /* List of regexps for buffer names to appear in selected window. */
135 Lisp_Object Vsame_window_regexps;
137 /* Hook run at end of temp_output_buffer_show. */
139 Lisp_Object Qtemp_buffer_show_hook;
141 /* Fdisplay_buffer always splits the largest window
142 if that window is more than this high. */
144 int split_height_threshold;
146 /* Number of lines of continuity in scrolling by screenfuls. */
148 int next_screen_context_lines;
150 /* Incremented for each window created. */
152 static int sequence_number;
154 /* Nonzero after init_window_once has finished. */
156 static int window_initialized;
158 /* Hook to run when window config changes. */
160 Lisp_Object Qwindow_configuration_change_hook;
161 Lisp_Object Vwindow_configuration_change_hook;
163 /* Nonzero means scroll commands try to put point
164 at the same screen height as previously. */
166 Lisp_Object Vscroll_preserve_screen_position;
168 #if 0 /* This isn't used anywhere. */
169 /* Nonzero means we can split a frame even if it is "unsplittable". */
170 static int inhibit_frame_unsplittable;
171 #endif /* 0 */
173 #define min(a, b) ((a) < (b) ? (a) : (b))
175 extern int scroll_margin;
177 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
179 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
180 "Returns t if OBJECT is a window.")
181 (object)
182 Lisp_Object object;
184 return WINDOWP (object) ? Qt : Qnil;
187 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
188 "Returns t if OBJECT is a window which is currently visible.")
189 (object)
190 Lisp_Object object;
192 return (WINDOWP (object) && ! NILP (XWINDOW (object)->buffer) ? Qt : Qnil);
195 Lisp_Object
196 make_window ()
198 Lisp_Object val;
199 register struct window *p;
200 register struct Lisp_Vector *vec;
201 int i;
203 vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct window));
204 for (i = 0; i < VECSIZE (struct window); i++)
205 vec->contents[i] = Qnil;
206 vec->size = VECSIZE (struct window);
207 p = (struct window *) vec;
208 XSETFASTINT (p->sequence_number, ++sequence_number);
209 XSETFASTINT (p->left, 0);
210 XSETFASTINT (p->top, 0);
211 XSETFASTINT (p->height, 0);
212 XSETFASTINT (p->width, 0);
213 XSETFASTINT (p->hscroll, 0);
214 p->orig_top = p->orig_height = Qnil;
215 p->start = Fmake_marker ();
216 p->pointm = Fmake_marker ();
217 XSETFASTINT (p->use_time, 0);
218 p->frame = Qnil;
219 p->display_table = Qnil;
220 p->dedicated = Qnil;
221 p->pseudo_window_p = 0;
222 bzero (&p->cursor, sizeof (p->cursor));
223 bzero (&p->last_cursor, sizeof (p->last_cursor));
224 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
225 p->desired_matrix = p->current_matrix = 0;
226 p->phys_cursor_type = -1;
227 p->must_be_updated_p = 0;
228 XSETFASTINT (p->window_end_vpos, 0);
229 XSETFASTINT (p->window_end_pos, 0);
230 p->window_end_valid = Qnil;
231 p->vscroll = 0;
232 XSETWINDOW (val, p);
233 XSETFASTINT (p->last_point, 0);
234 p->frozen_window_start_p = 0;
235 return val;
238 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
239 "Return the window that the cursor now appears in and commands apply to.")
242 return selected_window;
245 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
246 "Return the window used now for minibuffers.\n\
247 If the optional argument FRAME is specified, return the minibuffer window\n\
248 used by that frame.")
249 (frame)
250 Lisp_Object frame;
252 if (NILP (frame))
253 frame = selected_frame;
254 CHECK_LIVE_FRAME (frame, 0);
255 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
258 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
259 "Returns non-nil if WINDOW is a minibuffer window.")
260 (window)
261 Lisp_Object window;
263 struct window *w = decode_window (window);
264 return (MINI_WINDOW_P (w) ? Qt : Qnil);
267 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
268 Spos_visible_in_window_p, 0, 2, 0,
269 "Return t if position POS is currently on the frame in WINDOW.\n\
270 Returns nil if that position is scrolled vertically out of view.\n\
271 POS defaults to point; WINDOW, to the selected window.")
272 (pos, window)
273 Lisp_Object pos, window;
275 register struct window *w;
276 register int posint;
277 register struct buffer *buf;
278 struct text_pos top;
279 Lisp_Object in_window;
281 if (NILP (pos))
282 posint = PT;
283 else
285 CHECK_NUMBER_COERCE_MARKER (pos, 0);
286 posint = XINT (pos);
289 w = decode_window (window);
290 buf = XBUFFER (w->buffer);
291 SET_TEXT_POS_FROM_MARKER (top, w->start);
293 /* If position above window, it's not visible. */
294 if (posint < CHARPOS (top))
295 in_window = Qnil;
296 else if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
297 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf)
298 && posint < BUF_Z (buf) - XFASTINT (w->window_end_pos))
299 /* If frame is up to date, and POSINT is < window end pos, use
300 that info. This doesn't work for POSINT == end pos, because
301 the window end pos is actually the position _after_ the last
302 char in the window. */
303 in_window = Qt;
304 else if (posint > BUF_ZV (buf))
305 in_window = Qnil;
306 else if (CHARPOS (top) < BUF_BEGV (buf) || CHARPOS (top) > BUF_ZV (buf))
307 /* If window start is out of range, do something reasonable. */
308 in_window = Qnil;
309 else
311 struct it it;
312 start_display (&it, w, top);
313 move_it_to (&it, posint, 0, it.last_visible_y, -1,
314 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
315 in_window = IT_CHARPOS (it) == posint ? Qt : Qnil;
318 return in_window;
321 static struct window *
322 decode_window (window)
323 register Lisp_Object window;
325 if (NILP (window))
326 return XWINDOW (selected_window);
328 CHECK_LIVE_WINDOW (window, 0);
329 return XWINDOW (window);
332 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
333 "Return the buffer that WINDOW is displaying.")
334 (window)
335 Lisp_Object window;
337 return decode_window (window)->buffer;
340 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
341 "Return the number of lines in WINDOW (including its mode line).")
342 (window)
343 Lisp_Object window;
345 return decode_window (window)->height;
348 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
349 "Return the number of display columns in WINDOW.\n\
350 This is the width that is usable columns available for text in WINDOW.\n\
351 If you want to find out how many columns WINDOW takes up,\n\
352 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
353 (window)
354 Lisp_Object window;
356 return make_number (window_internal_width (decode_window (window)));
359 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
360 "Return the number of columns by which WINDOW is scrolled from left margin.")
361 (window)
362 Lisp_Object window;
364 return decode_window (window)->hscroll;
367 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
368 "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
369 NCOL should be zero or positive.")
370 (window, ncol)
371 register Lisp_Object window, ncol;
373 register struct window *w;
375 CHECK_NUMBER (ncol, 1);
376 if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
377 w = decode_window (window);
378 if (XINT (w->hscroll) != XINT (ncol))
379 /* Prevent redisplay shortcuts */
380 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
381 w->hscroll = ncol;
382 return ncol;
385 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
386 Swindow_redisplay_end_trigger, 0, 1, 0,
387 "Return WINDOW's redisplay end trigger value.\n\
388 See `set-window-redisplay-end-trigger' for more information.")
389 (window)
390 Lisp_Object window;
392 return decode_window (window)->redisplay_end_trigger;
395 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
396 Sset_window_redisplay_end_trigger, 2, 2, 0,
397 "Set WINDOW's redisplay end trigger value to VALUE.\n\
398 VALUE should be a buffer position (typically a marker) or nil.\n\
399 If it is a buffer position, then if redisplay in WINDOW reaches a position\n\
400 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called\n\
401 with two arguments: WINDOW, and the end trigger value.\n\
402 Afterwards the end-trigger value is reset to nil.")
403 (window, value)
404 register Lisp_Object window, value;
406 register struct window *w;
408 w = decode_window (window);
409 w->redisplay_end_trigger = value;
410 return value;
413 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
414 "Return a list of the edge coordinates of WINDOW.\n\
415 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.\n\
416 RIGHT is one more than the rightmost column used by WINDOW,\n\
417 and BOTTOM is one more than the bottommost row used by WINDOW\n\
418 and its mode-line.")
419 (window)
420 Lisp_Object window;
422 register struct window *w = decode_window (window);
424 return Fcons (w->left, Fcons (w->top,
425 Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
426 Fcons (make_number (XFASTINT (w->top)
427 + XFASTINT (w->height)),
428 Qnil))));
431 /* Test if the character at column *X, row *Y is within window W.
432 If it is not, return 0;
433 if it is in the window's text area,
434 set *x and *y to its location relative to the upper left corner
435 of the window, and
436 return 1;
437 if it is on the window's modeline, return 2;
438 if it is on the border between the window and its right sibling,
439 return 3.
440 if it is on the window's top line, return 4;
441 if it is in the bitmap area to the left/right of the window,
442 return 5 or 6, and convert *X and *Y to window-relative corrdinates.
444 X and Y are frame relative pixel coordinates. */
446 static int
447 coordinates_in_window (w, x, y)
448 register struct window *w;
449 register int *x, *y;
451 struct frame *f = XFRAME (WINDOW_FRAME (w));
452 int left_x, right_x, top_y, bottom_y;
453 int flags_area_width = FRAME_LEFT_FLAGS_AREA_WIDTH (f);
455 if (w->pseudo_window_p)
457 left_x = 0;
458 right_x = XFASTINT (w->width) * CANON_Y_UNIT (f);
459 top_y = WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
460 bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
462 else
464 left_x = WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w);
465 right_x = WINDOW_DISPLAY_RIGHT_EDGE_PIXEL_X (w);
466 top_y = WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
467 bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
470 if (*y < top_y
471 || *y >= bottom_y
472 || *x < (left_x
473 - flags_area_width
474 - (FRAME_LEFT_SCROLL_BAR_WIDTH (f)
475 * CANON_X_UNIT (f)))
476 || *x > right_x + flags_area_width)
477 /* Completely outside anything interesting. */
478 return 0;
479 else if (WINDOW_WANTS_MODELINE_P (w)
480 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
481 /* On the mode line. */
482 return 2;
483 else if (WINDOW_WANTS_HEADER_LINE_P (w)
484 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
485 /* On the top line. */
486 return 4;
487 else if (*x < left_x || *x >= right_x)
489 /* Other lines than the mode line don't include flags areas and
490 scroll bars on the left. */
492 /* Convert X and Y to window-relative pixel coordinates. */
493 *x -= left_x;
494 *y -= top_y;
495 return *x < left_x ? 5 : 6;
497 else if (!w->pseudo_window_p
498 && !WINDOW_RIGHTMOST_P (w)
499 && *x >= right_x - CANON_X_UNIT (f))
500 /* On the border on the right side of the window? Assume that
501 this area begins at RIGHT_X minus a canonical char width. */
502 return 3;
503 else
505 /* Convert X and Y to window-relative pixel coordinates. */
506 *x -= left_x;
507 *y -= top_y;
508 return 1;
512 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
513 Scoordinates_in_window_p, 2, 2, 0,
514 "Return non-nil if COORDINATES are in WINDOW.\n\
515 COORDINATES is a cons of the form (X . Y), X and Y being distances\n\
516 measured in characters from the upper-left corner of the frame.\n\
517 (0 . 0) denotes the character in the upper left corner of the\n\
518 frame.\n\
519 If COORDINATES are in the text portion of WINDOW,\n\
520 the coordinates relative to the window are returned.\n\
521 If they are in the mode line of WINDOW, `mode-line' is returned.\n\
522 If they are in the top mode line of WINDOW, `header-line' is returned.\n\
523 If they are in the bitmap-area to the left of the window,\n\
524 `left-bitmap-area' is returned, if they are in the area on the right of\n\
525 the window, `right-bitmap-area' is returned.\n\
526 If they are on the border between WINDOW and its right sibling,\n\
527 `vertical-line' is returned.")
528 (coordinates, window)
529 register Lisp_Object coordinates, window;
531 struct window *w;
532 struct frame *f;
533 int x, y;
534 Lisp_Object lx, ly;
536 CHECK_LIVE_WINDOW (window, 0);
537 w = XWINDOW (window);
538 f = XFRAME (w->frame);
539 CHECK_CONS (coordinates, 1);
540 lx = Fcar (coordinates);
541 ly = Fcdr (coordinates);
542 CHECK_NUMBER_OR_FLOAT (lx, 1);
543 CHECK_NUMBER_OR_FLOAT (ly, 1);
544 x = PIXEL_X_FROM_CANON_X (f, lx);
545 y = PIXEL_Y_FROM_CANON_Y (f, ly);
547 switch (coordinates_in_window (w, &x, &y))
549 case 0: /* NOT in window at all. */
550 return Qnil;
552 case 1: /* In text part of window. */
553 /* X and Y are now window relative pixel coordinates.
554 Convert them to canonical char units before returning
555 them. */
556 return Fcons (CANON_X_FROM_PIXEL_X (f, x),
557 CANON_Y_FROM_PIXEL_Y (f, y));
559 case 2: /* In mode line of window. */
560 return Qmode_line;
562 case 3: /* On right border of window. */
563 return Qvertical_line;
565 case 4:
566 return Qheader_line;
568 case 5:
569 return Qleft_bitmap_area;
571 case 6:
572 return Qright_bitmap_area;
574 default:
575 abort ();
579 /* Find the window containing frame-relative pixel position X/Y and
580 return it as a Lisp_Object. If X, Y is on the window's modeline,
581 set *PART to 1; if it is on the separating line between the window
582 and its right sibling, set it to 2; otherwise set it to 0. If
583 there is no window under X, Y return nil and leave *PART
584 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows. */
586 Lisp_Object
587 window_from_coordinates (frame, x, y, part, tool_bar_p)
588 FRAME_PTR frame;
589 int x, y;
590 int *part;
591 int tool_bar_p;
593 register Lisp_Object tem, first;
594 int found;
596 tem = first = FRAME_SELECTED_WINDOW (frame);
600 found = coordinates_in_window (XWINDOW (tem), &x, &y);
602 if (found)
604 *part = found - 1;
605 return tem;
608 tem = Fnext_window (tem, Qt, Qlambda);
610 while (!EQ (tem, first));
612 /* See if it's in the tool bar window, if a tool bar exists. */
613 if (tool_bar_p
614 && WINDOWP (frame->tool_bar_window)
615 && XFASTINT (XWINDOW (frame->tool_bar_window)->height)
616 && coordinates_in_window (XWINDOW (frame->tool_bar_window), &x, &y))
618 *part = 0;
619 return frame->tool_bar_window;
622 return Qnil;
625 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
626 "Return window containing coordinates X and Y on FRAME.\n\
627 If omitted, FRAME defaults to the currently selected frame.\n\
628 The top left corner of the frame is considered to be row 0,\n\
629 column 0.")
630 (x, y, frame)
631 Lisp_Object x, y, frame;
633 int part;
634 struct frame *f;
636 if (NILP (frame))
637 frame = selected_frame;
638 CHECK_LIVE_FRAME (frame, 2);
639 f = XFRAME (frame);
641 /* Check that arguments are integers or floats. */
642 CHECK_NUMBER_OR_FLOAT (x, 0);
643 CHECK_NUMBER_OR_FLOAT (y, 1);
645 return window_from_coordinates (f,
646 PIXEL_X_FROM_CANON_X (f, x),
647 PIXEL_Y_FROM_CANON_Y (f, y),
648 &part, 0);
651 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
652 "Return current value of point in WINDOW.\n\
653 For a nonselected window, this is the value point would have\n\
654 if that window were selected.\n\
656 Note that, when WINDOW is the selected window and its buffer\n\
657 is also currently selected, the value returned is the same as (point).\n\
658 It would be more strictly correct to return the `top-level' value\n\
659 of point, outside of any save-excursion forms.\n\
660 But that is hard to define.")
661 (window)
662 Lisp_Object window;
664 register struct window *w = decode_window (window);
666 if (w == XWINDOW (selected_window)
667 && current_buffer == XBUFFER (w->buffer))
668 return Fpoint ();
669 return Fmarker_position (w->pointm);
672 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
673 "Return position at which display currently starts in WINDOW.\n\
674 This is updated by redisplay or by calling `set-window-start'.")
675 (window)
676 Lisp_Object window;
678 return Fmarker_position (decode_window (window)->start);
681 /* This is text temporarily removed from the doc string below.
683 This function returns nil if the position is not currently known.\n\
684 That happens when redisplay is preempted and doesn't finish.\n\
685 If in that case you want to compute where the end of the window would\n\
686 have been if redisplay had finished, do this:\n\
687 (save-excursion\n\
688 (goto-char (window-start window))\n\
689 (vertical-motion (1- (window-height window)) window)\n\
690 (point))") */
692 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
693 "Return position at which display currently ends in WINDOW.\n\
694 This is updated by redisplay, when it runs to completion.\n\
695 Simply changing the buffer text or setting `window-start'\n\
696 does not update this value.\n\
697 If UP-TO-DATE is non-nil, compute the up-to-date position\n\
698 if it isn't already recorded.")
699 (window, update)
700 Lisp_Object window, update;
702 Lisp_Object value;
703 struct window *w = decode_window (window);
704 Lisp_Object buf;
706 buf = w->buffer;
707 CHECK_BUFFER (buf, 0);
709 #if 0 /* This change broke some things. We should make it later. */
710 /* If we don't know the end position, return nil.
711 The user can compute it with vertical-motion if he wants to.
712 It would be nicer to do it automatically,
713 but that's so slow that it would probably bother people. */
714 if (NILP (w->window_end_valid))
715 return Qnil;
716 #endif
718 if (! NILP (update)
719 && ! (! NILP (w->window_end_valid)
720 && XFASTINT (w->last_modified) >= MODIFF))
722 int opoint = PT, opoint_byte = PT_BYTE;
724 /* In case W->start is out of the range, use something
725 reasonable. This situation occured when loading a file with
726 `-l' containing a call to `rmail' with subsequent other
727 commands. At the end, W->start happened to be BEG, while
728 rmail had already narrowed the buffer. This leads to an
729 abort in temp_set_pt_both. */
730 if (XMARKER (w->start)->charpos < BEGV)
731 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
732 else if (XMARKER (w->start)->charpos > ZV)
733 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
734 else
735 TEMP_SET_PT_BOTH (XMARKER (w->start)->charpos,
736 XMARKER (w->start)->bytepos);
738 Fvertical_motion (make_number (window_internal_height (w)), Qnil);
739 XSETINT (value, PT);
740 TEMP_SET_PT_BOTH (opoint, opoint_byte);
742 else
743 XSETINT (value,
744 BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
746 return value;
749 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
750 "Make point value in WINDOW be at position POS in WINDOW's buffer.")
751 (window, pos)
752 Lisp_Object window, pos;
754 register struct window *w = decode_window (window);
756 CHECK_NUMBER_COERCE_MARKER (pos, 1);
757 if (w == XWINDOW (selected_window)
758 && XBUFFER (w->buffer) == current_buffer)
759 Fgoto_char (pos);
760 else
761 set_marker_restricted (w->pointm, pos, w->buffer);
763 return pos;
766 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
767 "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
768 Optional third arg NOFORCE non-nil inhibits next redisplay\n\
769 from overriding motion of point in order to display at this exact start.")
770 (window, pos, noforce)
771 Lisp_Object window, pos, noforce;
773 register struct window *w = decode_window (window);
775 CHECK_NUMBER_COERCE_MARKER (pos, 1);
776 set_marker_restricted (w->start, pos, w->buffer);
777 /* this is not right, but much easier than doing what is right. */
778 w->start_at_line_beg = Qnil;
779 if (NILP (noforce))
780 w->force_start = Qt;
781 w->update_mode_line = Qt;
782 XSETFASTINT (w->last_modified, 0);
783 XSETFASTINT (w->last_overlay_modified, 0);
784 if (!EQ (window, selected_window))
785 windows_or_buffers_changed++;
787 return pos;
790 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
791 1, 1, 0,
792 "Return WINDOW's dedicated object, usually t or nil.\n\
793 See also `set-window-dedicated-p'.")
794 (window)
795 Lisp_Object window;
797 return decode_window (window)->dedicated;
800 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
801 Sset_window_dedicated_p, 2, 2, 0,
802 "Control whether WINDOW is dedicated to the buffer it displays.\n\
803 If it is dedicated, Emacs will not automatically change\n\
804 which buffer appears in it.\n\
805 The second argument is the new value for the dedication flag;\n\
806 non-nil means yes.")
807 (window, arg)
808 Lisp_Object window, arg;
810 register struct window *w = decode_window (window);
812 if (NILP (arg))
813 w->dedicated = Qnil;
814 else
815 w->dedicated = Qt;
817 return w->dedicated;
820 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
821 0, 1, 0,
822 "Return the display-table that WINDOW is using.")
823 (window)
824 Lisp_Object window;
826 return decode_window (window)->display_table;
829 /* Get the display table for use on window W. This is either W's
830 display table or W's buffer's display table. Ignore the specified
831 tables if they are not valid; if no valid table is specified,
832 return 0. */
834 struct Lisp_Char_Table *
835 window_display_table (w)
836 struct window *w;
838 Lisp_Object tem;
839 tem = w->display_table;
840 if (DISP_TABLE_P (tem))
841 return XCHAR_TABLE (tem);
842 if (NILP (w->buffer))
843 return 0;
845 tem = XBUFFER (w->buffer)->display_table;
846 if (DISP_TABLE_P (tem))
847 return XCHAR_TABLE (tem);
848 tem = Vstandard_display_table;
849 if (DISP_TABLE_P (tem))
850 return XCHAR_TABLE (tem);
851 return 0;
854 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
855 "Set WINDOW's display-table to TABLE.")
856 (window, table)
857 register Lisp_Object window, table;
859 register struct window *w;
861 w = decode_window (window);
862 w->display_table = table;
863 return table;
866 /* Record info on buffer window w is displaying
867 when it is about to cease to display that buffer. */
868 static void
869 unshow_buffer (w)
870 register struct window *w;
872 Lisp_Object buf;
873 struct buffer *b;
875 buf = w->buffer;
876 b = XBUFFER (buf);
877 if (b != XMARKER (w->pointm)->buffer)
878 abort ();
880 if (w == XWINDOW (b->last_selected_window))
881 b->last_selected_window = Qnil;
883 #if 0
884 if (w == XWINDOW (selected_window)
885 || ! EQ (buf, XWINDOW (selected_window)->buffer))
886 /* Do this except when the selected window's buffer
887 is being removed from some other window. */
888 #endif
889 /* last_window_start records the start position that this buffer
890 had in the last window to be disconnected from it.
891 Now that this statement is unconditional,
892 it is possible for the buffer to be displayed in the
893 selected window, while last_window_start reflects another
894 window which was recently showing the same buffer.
895 Some people might say that might be a good thing. Let's see. */
896 b->last_window_start = marker_position (w->start);
898 /* Point in the selected window's buffer
899 is actually stored in that buffer, and the window's pointm isn't used.
900 So don't clobber point in that buffer. */
901 if (! EQ (buf, XWINDOW (selected_window)->buffer))
902 temp_set_point_both (b,
903 clip_to_bounds (BUF_BEGV (b),
904 XMARKER (w->pointm)->charpos,
905 BUF_ZV (b)),
906 clip_to_bounds (BUF_BEGV_BYTE (b),
907 marker_byte_position (w->pointm),
908 BUF_ZV_BYTE (b)));
911 /* Put replacement into the window structure in place of old. */
912 static void
913 replace_window (old, replacement)
914 Lisp_Object old, replacement;
916 register Lisp_Object tem;
917 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
919 /* If OLD is its frame's root_window, then replacement is the new
920 root_window for that frame. */
922 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
923 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
925 p->left = o->left;
926 p->top = o->top;
927 p->width = o->width;
928 p->height = o->height;
929 p->desired_matrix = p->current_matrix = 0;
930 p->vscroll = 0;
931 bzero (&p->cursor, sizeof (p->cursor));
932 bzero (&p->last_cursor, sizeof (p->last_cursor));
933 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
934 p->phys_cursor_type = -1;
935 p->must_be_updated_p = 0;
936 p->pseudo_window_p = 0;
937 XSETFASTINT (p->window_end_vpos, 0);
938 XSETFASTINT (p->window_end_pos, 0);
939 p->window_end_valid = Qnil;
940 p->frozen_window_start_p = 0;
941 p->orig_top = p->orig_height = Qnil;
943 p->next = tem = o->next;
944 if (!NILP (tem))
945 XWINDOW (tem)->prev = replacement;
947 p->prev = tem = o->prev;
948 if (!NILP (tem))
949 XWINDOW (tem)->next = replacement;
951 p->parent = tem = o->parent;
952 if (!NILP (tem))
954 if (EQ (XWINDOW (tem)->vchild, old))
955 XWINDOW (tem)->vchild = replacement;
956 if (EQ (XWINDOW (tem)->hchild, old))
957 XWINDOW (tem)->hchild = replacement;
960 /*** Here, if replacement is a vertical combination
961 and so is its new parent, we should make replacement's
962 children be children of that parent instead. ***/
965 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
966 "Remove WINDOW from the display. Default is selected window.")
967 (window)
968 register Lisp_Object window;
970 delete_window (window);
972 if (! NILP (Vwindow_configuration_change_hook)
973 && ! NILP (Vrun_hooks))
974 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
976 return Qnil;
979 void
980 delete_window (window)
981 register Lisp_Object window;
983 register Lisp_Object tem, parent, sib;
984 register struct window *p;
985 register struct window *par;
986 FRAME_PTR frame;
988 /* Because this function is called by other C code on non-leaf
989 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
990 so we can't decode_window here. */
991 if (NILP (window))
992 window = selected_window;
993 else
994 CHECK_WINDOW (window, 0);
995 p = XWINDOW (window);
997 /* It's okay to delete an already-deleted window. */
998 if (NILP (p->buffer)
999 && NILP (p->hchild)
1000 && NILP (p->vchild))
1001 return;
1003 parent = p->parent;
1004 if (NILP (parent))
1005 error ("Attempt to delete minibuffer or sole ordinary window");
1006 par = XWINDOW (parent);
1008 windows_or_buffers_changed++;
1009 frame = XFRAME (WINDOW_FRAME (p));
1010 FRAME_WINDOW_SIZES_CHANGED (frame) = 1;
1012 /* Are we trying to delete any frame's selected window? */
1014 Lisp_Object frame, pwindow;
1016 /* See if the frame's selected window is either WINDOW
1017 or any subwindow of it, by finding all that window's parents
1018 and comparing each one with WINDOW. */
1019 frame = WINDOW_FRAME (XWINDOW (window));
1020 pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
1022 while (!NILP (pwindow))
1024 if (EQ (window, pwindow))
1025 break;
1026 pwindow = XWINDOW (pwindow)->parent;
1029 if (EQ (window, pwindow))
1031 Lisp_Object alternative;
1032 alternative = Fnext_window (window, Qlambda, Qnil);
1034 /* If we're about to delete the selected window on the
1035 selected frame, then we should use Fselect_window to select
1036 the new window. On the other hand, if we're about to
1037 delete the selected window on any other frame, we shouldn't do
1038 anything but set the frame's selected_window slot. */
1039 if (EQ (window, selected_window))
1040 Fselect_window (alternative);
1041 else
1042 FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
1046 tem = p->buffer;
1047 /* tem is null for dummy parent windows
1048 (which have inferiors but not any contents themselves) */
1049 if (!NILP (tem))
1051 unshow_buffer (p);
1052 unchain_marker (p->pointm);
1053 unchain_marker (p->start);
1056 /* Free window glyph matrices. It is sure that they are allocated
1057 again when ADJUST_GLYPHS is called. Block input so that expose
1058 events and other events that access glyph matrices are not
1059 processed while we are changing them. */
1060 BLOCK_INPUT;
1061 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (frame)));
1063 tem = p->next;
1064 if (!NILP (tem))
1065 XWINDOW (tem)->prev = p->prev;
1067 tem = p->prev;
1068 if (!NILP (tem))
1069 XWINDOW (tem)->next = p->next;
1071 if (EQ (window, par->hchild))
1072 par->hchild = p->next;
1073 if (EQ (window, par->vchild))
1074 par->vchild = p->next;
1076 /* Find one of our siblings to give our space to. */
1077 sib = p->prev;
1078 if (NILP (sib))
1080 /* If p gives its space to its next sibling, that sibling needs
1081 to have its top/left side pulled back to where p's is.
1082 set_window_{height,width} will re-position the sibling's
1083 children. */
1084 sib = p->next;
1085 XWINDOW (sib)->top = p->top;
1086 XWINDOW (sib)->left = p->left;
1089 /* Stretch that sibling. */
1090 if (!NILP (par->vchild))
1091 set_window_height (sib,
1092 XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
1094 if (!NILP (par->hchild))
1095 set_window_width (sib,
1096 XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
1099 /* If parent now has only one child,
1100 put the child into the parent's place. */
1101 tem = par->hchild;
1102 if (NILP (tem))
1103 tem = par->vchild;
1104 if (NILP (XWINDOW (tem)->next))
1105 replace_window (parent, tem);
1107 /* Since we may be deleting combination windows, we must make sure that
1108 not only p but all its children have been marked as deleted. */
1109 if (! NILP (p->hchild))
1110 delete_all_subwindows (XWINDOW (p->hchild));
1111 else if (! NILP (p->vchild))
1112 delete_all_subwindows (XWINDOW (p->vchild));
1114 /* Mark this window as deleted. */
1115 p->buffer = p->hchild = p->vchild = Qnil;
1117 /* Adjust glyph matrices. */
1118 adjust_glyphs (frame);
1119 UNBLOCK_INPUT;
1123 extern Lisp_Object next_frame (), prev_frame ();
1125 /* This comment supplies the doc string for `next-window',
1126 for make-docfile to see. We cannot put this in the real DEFUN
1127 due to limits in the Unix cpp.
1129 DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
1130 "Return next window after WINDOW in canonical ordering of windows.\n\
1131 If omitted, WINDOW defaults to the selected window.\n\
1133 Optional second arg MINIBUF t means count the minibuffer window even\n\
1134 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
1135 it is active. MINIBUF neither t nor nil means not to count the\n\
1136 minibuffer even if it is active.\n\
1138 Several frames may share a single minibuffer; if the minibuffer\n\
1139 counts, all windows on all frames that share that minibuffer count\n\
1140 too. Therefore, `next-window' can be used to iterate through the\n\
1141 set of windows even when the minibuffer is on another frame. If the\n\
1142 minibuffer does not count, only windows from WINDOW's frame count.\n\
1144 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
1145 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
1146 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
1147 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1148 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
1149 Anything else means restrict to WINDOW's frame.\n\
1151 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
1152 `next-window' to iterate through the entire cycle of acceptable\n\
1153 windows, eventually ending up back at the window you started with.\n\
1154 `previous-window' traverses the same cycle, in the reverse order.")
1155 (window, minibuf, all_frames) */
1157 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1159 (window, minibuf, all_frames)
1160 register Lisp_Object window, minibuf, all_frames;
1162 register Lisp_Object tem;
1163 Lisp_Object start_window;
1165 if (NILP (window))
1166 window = selected_window;
1167 else
1168 CHECK_LIVE_WINDOW (window, 0);
1170 start_window = window;
1172 /* minibuf == nil may or may not include minibuffers.
1173 Decide if it does. */
1174 if (NILP (minibuf))
1175 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1176 else if (! EQ (minibuf, Qt))
1177 minibuf = Qlambda;
1178 /* Now minibuf can be t => count all minibuffer windows,
1179 lambda => count none of them,
1180 or a specific minibuffer window (the active one) to count. */
1182 /* all_frames == nil doesn't specify which frames to include. */
1183 if (NILP (all_frames))
1184 all_frames = (! EQ (minibuf, Qlambda)
1185 ? (FRAME_MINIBUF_WINDOW
1186 (XFRAME
1187 (WINDOW_FRAME
1188 (XWINDOW (window)))))
1189 : Qnil);
1190 else if (EQ (all_frames, Qvisible))
1192 else if (XFASTINT (all_frames) == 0)
1194 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1195 /* If all_frames is a frame and window arg isn't on that frame, just
1196 return the first window on the frame. */
1197 return Fframe_first_window (all_frames);
1198 else if (! EQ (all_frames, Qt))
1199 all_frames = Qnil;
1200 /* Now all_frames is t meaning search all frames,
1201 nil meaning search just current frame,
1202 visible meaning search just visible frames,
1203 0 meaning search visible and iconified frames,
1204 or a window, meaning search the frame that window belongs to. */
1206 /* Do this loop at least once, to get the next window, and perhaps
1207 again, if we hit the minibuffer and that is not acceptable. */
1210 /* Find a window that actually has a next one. This loop
1211 climbs up the tree. */
1212 while (tem = XWINDOW (window)->next, NILP (tem))
1213 if (tem = XWINDOW (window)->parent, !NILP (tem))
1214 window = tem;
1215 else
1217 /* We've reached the end of this frame.
1218 Which other frames are acceptable? */
1219 tem = WINDOW_FRAME (XWINDOW (window));
1220 if (! NILP (all_frames))
1222 Lisp_Object tem1;
1224 tem1 = tem;
1225 tem = next_frame (tem, all_frames);
1226 /* In the case where the minibuffer is active,
1227 and we include its frame as well as the selected one,
1228 next_frame may get stuck in that frame.
1229 If that happens, go back to the selected frame
1230 so we can complete the cycle. */
1231 if (EQ (tem, tem1))
1232 tem = selected_frame;
1234 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1236 break;
1239 window = tem;
1241 /* If we're in a combination window, find its first child and
1242 recurse on that. Otherwise, we've found the window we want. */
1243 while (1)
1245 if (!NILP (XWINDOW (window)->hchild))
1246 window = XWINDOW (window)->hchild;
1247 else if (!NILP (XWINDOW (window)->vchild))
1248 window = XWINDOW (window)->vchild;
1249 else break;
1252 QUIT;
1254 /* Which windows are acceptable?
1255 Exit the loop and accept this window if
1256 this isn't a minibuffer window,
1257 or we're accepting all minibuffer windows,
1258 or this is the active minibuffer and we are accepting that one, or
1259 we've come all the way around and we're back at the original window. */
1260 while (MINI_WINDOW_P (XWINDOW (window))
1261 && ! EQ (minibuf, Qt)
1262 && ! EQ (minibuf, window)
1263 && ! EQ (window, start_window));
1265 return window;
1268 /* This comment supplies the doc string for `previous-window',
1269 for make-docfile to see. We cannot put this in the real DEFUN
1270 due to limits in the Unix cpp.
1272 DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
1273 "Return the window preceding WINDOW in canonical ordering of windows.\n\
1274 If omitted, WINDOW defaults to the selected window.\n\
1276 Optional second arg MINIBUF t means count the minibuffer window even\n\
1277 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
1278 it is active. MINIBUF neither t nor nil means not to count the\n\
1279 minibuffer even if it is active.\n\
1281 Several frames may share a single minibuffer; if the minibuffer\n\
1282 counts, all windows on all frames that share that minibuffer count\n\
1283 too. Therefore, `previous-window' can be used to iterate through\n\
1284 the set of windows even when the minibuffer is on another frame. If\n\
1285 the minibuffer does not count, only windows from WINDOW's frame count\n\
1287 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
1288 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
1289 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
1290 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1291 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
1292 Anything else means restrict to WINDOW's frame.\n\
1294 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
1295 `previous-window' to iterate through the entire cycle of acceptable\n\
1296 windows, eventually ending up back at the window you started with.\n\
1297 `next-window' traverses the same cycle, in the reverse order.")
1298 (window, minibuf, all_frames) */
1301 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1303 (window, minibuf, all_frames)
1304 register Lisp_Object window, minibuf, all_frames;
1306 register Lisp_Object tem;
1307 Lisp_Object start_window;
1309 if (NILP (window))
1310 window = selected_window;
1311 else
1312 CHECK_LIVE_WINDOW (window, 0);
1314 start_window = window;
1316 /* minibuf == nil may or may not include minibuffers.
1317 Decide if it does. */
1318 if (NILP (minibuf))
1319 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1320 else if (! EQ (minibuf, Qt))
1321 minibuf = Qlambda;
1322 /* Now minibuf can be t => count all minibuffer windows,
1323 lambda => count none of them,
1324 or a specific minibuffer window (the active one) to count. */
1326 /* all_frames == nil doesn't specify which frames to include.
1327 Decide which frames it includes. */
1328 if (NILP (all_frames))
1329 all_frames = (! EQ (minibuf, Qlambda)
1330 ? (FRAME_MINIBUF_WINDOW
1331 (XFRAME
1332 (WINDOW_FRAME
1333 (XWINDOW (window)))))
1334 : Qnil);
1335 else if (EQ (all_frames, Qvisible))
1337 else if (XFASTINT (all_frames) == 0)
1339 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1340 /* If all_frames is a frame and window arg isn't on that frame, just
1341 return the first window on the frame. */
1342 return Fframe_first_window (all_frames);
1343 else if (! EQ (all_frames, Qt))
1344 all_frames = Qnil;
1345 /* Now all_frames is t meaning search all frames,
1346 nil meaning search just current frame,
1347 visible meaning search just visible frames,
1348 0 meaning search visible and iconified frames,
1349 or a window, meaning search the frame that window belongs to. */
1351 /* Do this loop at least once, to get the previous window, and perhaps
1352 again, if we hit the minibuffer and that is not acceptable. */
1355 /* Find a window that actually has a previous one. This loop
1356 climbs up the tree. */
1357 while (tem = XWINDOW (window)->prev, NILP (tem))
1358 if (tem = XWINDOW (window)->parent, !NILP (tem))
1359 window = tem;
1360 else
1362 /* We have found the top window on the frame.
1363 Which frames are acceptable? */
1364 tem = WINDOW_FRAME (XWINDOW (window));
1365 if (! NILP (all_frames))
1366 /* It's actually important that we use prev_frame here,
1367 rather than next_frame. All the windows acceptable
1368 according to the given parameters should form a ring;
1369 Fnext_window and Fprevious_window should go back and
1370 forth around the ring. If we use next_frame here,
1371 then Fnext_window and Fprevious_window take different
1372 paths through the set of acceptable windows.
1373 window_loop assumes that these `ring' requirement are
1374 met. */
1376 Lisp_Object tem1;
1378 tem1 = tem;
1379 tem = prev_frame (tem, all_frames);
1380 /* In the case where the minibuffer is active,
1381 and we include its frame as well as the selected one,
1382 next_frame may get stuck in that frame.
1383 If that happens, go back to the selected frame
1384 so we can complete the cycle. */
1385 if (EQ (tem, tem1))
1386 tem = selected_frame;
1388 /* If this frame has a minibuffer, find that window first,
1389 because it is conceptually the last window in that frame. */
1390 if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
1391 tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
1392 else
1393 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1395 break;
1398 window = tem;
1399 /* If we're in a combination window, find its last child and
1400 recurse on that. Otherwise, we've found the window we want. */
1401 while (1)
1403 if (!NILP (XWINDOW (window)->hchild))
1404 window = XWINDOW (window)->hchild;
1405 else if (!NILP (XWINDOW (window)->vchild))
1406 window = XWINDOW (window)->vchild;
1407 else break;
1408 while (tem = XWINDOW (window)->next, !NILP (tem))
1409 window = tem;
1412 /* Which windows are acceptable?
1413 Exit the loop and accept this window if
1414 this isn't a minibuffer window,
1415 or we're accepting all minibuffer windows,
1416 or this is the active minibuffer and we are accepting that one, or
1417 we've come all the way around and we're back at the original window. */
1418 while (MINI_WINDOW_P (XWINDOW (window))
1419 && ! EQ (minibuf, Qt)
1420 && ! EQ (minibuf, window)
1421 && ! EQ (window, start_window));
1423 return window;
1426 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1427 "Select the ARG'th different window on this frame.\n\
1428 All windows on current frame are arranged in a cyclic order.\n\
1429 This command selects the window ARG steps away in that order.\n\
1430 A negative ARG moves in the opposite order. If the optional second\n\
1431 argument ALL_FRAMES is non-nil, cycle through all frames.")
1432 (arg, all_frames)
1433 register Lisp_Object arg, all_frames;
1435 register int i;
1436 register Lisp_Object w;
1438 CHECK_NUMBER (arg, 0);
1439 w = selected_window;
1440 i = XINT (arg);
1442 while (i > 0)
1444 w = Fnext_window (w, Qnil, all_frames);
1445 i--;
1447 while (i < 0)
1449 w = Fprevious_window (w, Qnil, all_frames);
1450 i++;
1452 Fselect_window (w);
1453 return Qnil;
1456 /* Look at all windows, performing an operation specified by TYPE
1457 with argument OBJ.
1458 If FRAMES is Qt, look at all frames;
1459 Qnil, look at just the selected frame;
1460 Qvisible, look at visible frames;
1461 a frame, just look at windows on that frame.
1462 If MINI is non-zero, perform the operation on minibuffer windows too.
1465 enum window_loop
1467 WINDOW_LOOP_UNUSED,
1468 GET_BUFFER_WINDOW, /* Arg is buffer */
1469 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1470 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1471 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1472 GET_LARGEST_WINDOW,
1473 UNSHOW_BUFFER, /* Arg is buffer */
1474 CHECK_ALL_WINDOWS
1477 static Lisp_Object
1478 window_loop (type, obj, mini, frames)
1479 enum window_loop type;
1480 register Lisp_Object obj, frames;
1481 int mini;
1483 register Lisp_Object w;
1484 register Lisp_Object best_window;
1485 register Lisp_Object next_window;
1486 register Lisp_Object last_window;
1487 FRAME_PTR frame;
1488 Lisp_Object frame_arg;
1489 frame_arg = Qt;
1491 /* If we're only looping through windows on a particular frame,
1492 frame points to that frame. If we're looping through windows
1493 on all frames, frame is 0. */
1494 if (FRAMEP (frames))
1495 frame = XFRAME (frames);
1496 else if (NILP (frames))
1497 frame = SELECTED_FRAME ();
1498 else
1499 frame = 0;
1500 if (frame)
1501 frame_arg = Qlambda;
1502 else if (XFASTINT (frames) == 0)
1503 frame_arg = frames;
1504 else if (EQ (frames, Qvisible))
1505 frame_arg = frames;
1507 /* frame_arg is Qlambda to stick to one frame,
1508 Qvisible to consider all visible frames,
1509 or Qt otherwise. */
1511 /* Pick a window to start with. */
1512 if (WINDOWP (obj))
1513 w = obj;
1514 else if (frame)
1515 w = FRAME_SELECTED_WINDOW (frame);
1516 else
1517 w = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
1519 /* Figure out the last window we're going to mess with. Since
1520 Fnext_window, given the same options, is guaranteed to go in a
1521 ring, we can just use Fprevious_window to find the last one.
1523 We can't just wait until we hit the first window again, because
1524 it might be deleted. */
1526 last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
1528 best_window = Qnil;
1529 for (;;)
1531 /* Pick the next window now, since some operations will delete
1532 the current window. */
1533 next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
1535 /* Note that we do not pay attention here to whether
1536 the frame is visible, since Fnext_window skips non-visible frames
1537 if that is desired, under the control of frame_arg. */
1538 if (! MINI_WINDOW_P (XWINDOW (w))
1539 /* For UNSHOW_BUFFER, we must always consider all windows. */
1540 || type == UNSHOW_BUFFER
1541 || (mini && minibuf_level > 0))
1542 switch (type)
1544 case GET_BUFFER_WINDOW:
1545 if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj)
1546 /* Don't find any minibuffer window
1547 except the one that is currently in use. */
1548 && (MINI_WINDOW_P (XWINDOW (w))
1549 ? EQ (w, minibuf_window) : 1))
1550 return w;
1551 break;
1553 case GET_LRU_WINDOW:
1554 /* t as arg means consider only full-width windows */
1555 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (XWINDOW (w)))
1556 break;
1557 /* Ignore dedicated windows and minibuffers. */
1558 if (MINI_WINDOW_P (XWINDOW (w))
1559 || !NILP (XWINDOW (w)->dedicated))
1560 break;
1561 if (NILP (best_window)
1562 || (XFASTINT (XWINDOW (best_window)->use_time)
1563 > XFASTINT (XWINDOW (w)->use_time)))
1564 best_window = w;
1565 break;
1567 case DELETE_OTHER_WINDOWS:
1568 if (XWINDOW (w) != XWINDOW (obj))
1569 Fdelete_window (w);
1570 break;
1572 case DELETE_BUFFER_WINDOWS:
1573 if (EQ (XWINDOW (w)->buffer, obj))
1575 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1577 /* If this window is dedicated, and in a frame of its own,
1578 kill the frame. */
1579 if (EQ (w, FRAME_ROOT_WINDOW (f))
1580 && !NILP (XWINDOW (w)->dedicated)
1581 && other_visible_frames (f))
1583 /* Skip the other windows on this frame.
1584 There might be one, the minibuffer! */
1585 if (! EQ (w, last_window))
1586 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1588 /* As we go, check for the end of the loop.
1589 We mustn't start going around a second time. */
1590 if (EQ (next_window, last_window))
1592 last_window = w;
1593 break;
1595 next_window = Fnext_window (next_window,
1596 mini ? Qt : Qnil,
1597 frame_arg);
1599 /* Now we can safely delete the frame. */
1600 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1602 else
1603 /* If we're deleting the buffer displayed in the only window
1604 on the frame, find a new buffer to display there. */
1605 if (NILP (XWINDOW (w)->parent))
1607 Lisp_Object new_buffer;
1608 new_buffer = Fother_buffer (obj, Qnil,
1609 XWINDOW (w)->frame);
1610 if (NILP (new_buffer))
1611 new_buffer
1612 = Fget_buffer_create (build_string ("*scratch*"));
1613 Fset_window_buffer (w, new_buffer);
1614 if (EQ (w, selected_window))
1615 Fset_buffer (XWINDOW (w)->buffer);
1617 else
1618 Fdelete_window (w);
1620 break;
1622 case GET_LARGEST_WINDOW:
1623 /* Ignore dedicated windows and minibuffers. */
1624 if (MINI_WINDOW_P (XWINDOW (w))
1625 || !NILP (XWINDOW (w)->dedicated))
1626 break;
1628 struct window *best_window_ptr = XWINDOW (best_window);
1629 struct window *w_ptr = XWINDOW (w);
1630 if (NILP (best_window)
1631 || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
1632 > (XFASTINT (best_window_ptr->height)
1633 * XFASTINT (best_window_ptr->width))))
1634 best_window = w;
1636 break;
1638 case UNSHOW_BUFFER:
1639 if (EQ (XWINDOW (w)->buffer, obj))
1641 /* Find another buffer to show in this window. */
1642 Lisp_Object another_buffer;
1643 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1644 another_buffer = Fother_buffer (obj, Qnil, XWINDOW (w)->frame);
1645 if (NILP (another_buffer))
1646 another_buffer
1647 = Fget_buffer_create (build_string ("*scratch*"));
1648 /* If this window is dedicated, and in a frame of its own,
1649 kill the frame. */
1650 if (EQ (w, FRAME_ROOT_WINDOW (f))
1651 && !NILP (XWINDOW (w)->dedicated)
1652 && other_visible_frames (f))
1654 /* Skip the other windows on this frame.
1655 There might be one, the minibuffer! */
1656 if (! EQ (w, last_window))
1657 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1659 /* As we go, check for the end of the loop.
1660 We mustn't start going around a second time. */
1661 if (EQ (next_window, last_window))
1663 last_window = w;
1664 break;
1666 next_window = Fnext_window (next_window,
1667 mini ? Qt : Qnil,
1668 frame_arg);
1670 /* Now we can safely delete the frame. */
1671 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1673 else
1675 /* Otherwise show a different buffer in the window. */
1676 XWINDOW (w)->dedicated = Qnil;
1677 Fset_window_buffer (w, another_buffer);
1678 if (EQ (w, selected_window))
1679 Fset_buffer (XWINDOW (w)->buffer);
1682 break;
1684 /* Check for a window that has a killed buffer. */
1685 case CHECK_ALL_WINDOWS:
1686 if (! NILP (XWINDOW (w)->buffer)
1687 && NILP (XBUFFER (XWINDOW (w)->buffer)->name))
1688 abort ();
1691 if (EQ (w, last_window))
1692 break;
1694 w = next_window;
1697 return best_window;
1700 /* Used for debugging. Abort if any window has a dead buffer. */
1702 void
1703 check_all_windows ()
1705 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
1708 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
1709 "Return the window least recently selected or used for display.\n\
1710 If optional argument FRAME is `visible', search all visible frames.\n\
1711 If FRAME is 0, search all visible and iconified frames.\n\
1712 If FRAME is t, search all frames.\n\
1713 If FRAME is nil, search only the selected frame.\n\
1714 If FRAME is a frame, search only that frame.")
1715 (frame)
1716 Lisp_Object frame;
1718 register Lisp_Object w;
1719 /* First try for a window that is full-width */
1720 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
1721 if (!NILP (w) && !EQ (w, selected_window))
1722 return w;
1723 /* If none of them, try the rest */
1724 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
1727 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
1728 "Return the largest window in area.\n\
1729 If optional argument FRAME is `visible', search all visible frames.\n\
1730 If FRAME is 0, search all visible and iconified frames.\n\
1731 If FRAME is t, search all frames.\n\
1732 If FRAME is nil, search only the selected frame.\n\
1733 If FRAME is a frame, search only that frame.")
1734 (frame)
1735 Lisp_Object frame;
1737 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
1738 frame);
1741 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
1742 "Return a window currently displaying BUFFER, or nil if none.\n\
1743 If optional argument FRAME is `visible', search all visible frames.\n\
1744 If optional argument FRAME is 0, search all visible and iconified frames.\n\
1745 If FRAME is t, search all frames.\n\
1746 If FRAME is nil, search only the selected frame.\n\
1747 If FRAME is a frame, search only that frame.")
1748 (buffer, frame)
1749 Lisp_Object buffer, frame;
1751 buffer = Fget_buffer (buffer);
1752 if (BUFFERP (buffer))
1753 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
1754 else
1755 return Qnil;
1758 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
1759 0, 1, "",
1760 "Make WINDOW (or the selected window) fill its frame.\n\
1761 Only the frame WINDOW is on is affected.\n\
1762 This function tries to reduce display jumps\n\
1763 by keeping the text previously visible in WINDOW\n\
1764 in the same place on the frame. Doing this depends on\n\
1765 the value of (window-start WINDOW), so if calling this function\n\
1766 in a program gives strange scrolling, make sure the window-start\n\
1767 value is reasonable when this function is called.")
1768 (window)
1769 Lisp_Object window;
1771 struct window *w;
1772 int startpos;
1773 int top;
1775 if (NILP (window))
1776 window = selected_window;
1777 else
1778 CHECK_LIVE_WINDOW (window, 0);
1780 w = XWINDOW (window);
1782 startpos = marker_position (w->start);
1783 top = XFASTINT (w->top) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
1785 if (MINI_WINDOW_P (w) && top > 0)
1786 error ("Can't expand minibuffer to full frame");
1788 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
1790 /* Try to minimize scrolling, by setting the window start to the point
1791 will cause the text at the old window start to be at the same place
1792 on the frame. But don't try to do this if the window start is
1793 outside the visible portion (as might happen when the display is
1794 not current, due to typeahead). */
1795 if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
1796 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
1798 struct position pos;
1799 struct buffer *obuf = current_buffer;
1801 Fset_buffer (w->buffer);
1802 /* This computation used to temporarily move point, but that can
1803 have unwanted side effects due to text properties. */
1804 pos = *vmotion (startpos, -top, w);
1806 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
1807 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
1808 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
1809 : Qnil);
1810 /* We need to do this, so that the window-scroll-functions
1811 get called. */
1812 w->optional_new_start = Qt;
1814 set_buffer_internal (obuf);
1817 return Qnil;
1820 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
1821 1, 2, "bDelete windows on (buffer): ",
1822 "Delete all windows showing BUFFER.\n\
1823 Optional second argument FRAME controls which frames are affected.\n\
1824 If optional argument FRAME is `visible', search all visible frames.\n\
1825 If FRAME is 0, search all visible and iconified frames.\n\
1826 If FRAME is nil, search all frames.\n\
1827 If FRAME is t, search only the selected frame.\n\
1828 If FRAME is a frame, search only that frame.")
1829 (buffer, frame)
1830 Lisp_Object buffer, frame;
1832 /* FRAME uses t and nil to mean the opposite of what window_loop
1833 expects. */
1834 if (NILP (frame))
1835 frame = Qt;
1836 else if (EQ (frame, Qt))
1837 frame = Qnil;
1839 if (!NILP (buffer))
1841 buffer = Fget_buffer (buffer);
1842 CHECK_BUFFER (buffer, 0);
1843 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
1846 return Qnil;
1849 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
1850 Sreplace_buffer_in_windows,
1851 1, 1, "bReplace buffer in windows: ",
1852 "Replace BUFFER with some other buffer in all windows showing it.")
1853 (buffer)
1854 Lisp_Object buffer;
1856 if (!NILP (buffer))
1858 buffer = Fget_buffer (buffer);
1859 CHECK_BUFFER (buffer, 0);
1860 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
1862 return Qnil;
1865 /* Replace BUFFER with some other buffer in all windows
1866 of all frames, even those on other keyboards. */
1868 void
1869 replace_buffer_in_all_windows (buffer)
1870 Lisp_Object buffer;
1872 #ifdef MULTI_KBOARD
1873 Lisp_Object tail, frame;
1875 /* A single call to window_loop won't do the job
1876 because it only considers frames on the current keyboard.
1877 So loop manually over frames, and handle each one. */
1878 FOR_EACH_FRAME (tail, frame)
1879 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
1880 #else
1881 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
1882 #endif
1885 /* Set the height of WINDOW and all its inferiors. */
1887 /* The smallest acceptable dimensions for a window. Anything smaller
1888 might crash Emacs. */
1890 #define MIN_SAFE_WINDOW_WIDTH (2)
1891 #define MIN_SAFE_WINDOW_HEIGHT (2)
1893 /* Make sure that window_min_height and window_min_width are
1894 not too small; if they are, set them to safe minima. */
1896 static void
1897 check_min_window_sizes ()
1899 /* Smaller values might permit a crash. */
1900 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
1901 window_min_width = MIN_SAFE_WINDOW_WIDTH;
1902 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
1903 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
1906 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
1907 minimum allowable size. */
1909 void
1910 check_frame_size (frame, rows, cols)
1911 FRAME_PTR frame;
1912 int *rows, *cols;
1914 /* For height, we have to see:
1915 whether the frame has a minibuffer,
1916 whether it wants a mode line, and
1917 whether it has a menu bar. */
1918 int min_height =
1919 (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
1920 : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
1921 : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
1923 if (FRAME_TOP_MARGIN (frame) > 0)
1924 min_height += FRAME_TOP_MARGIN (frame);
1926 if (*rows < min_height)
1927 *rows = min_height;
1928 if (*cols < MIN_SAFE_WINDOW_WIDTH)
1929 *cols = MIN_SAFE_WINDOW_WIDTH;
1933 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
1934 check if W's width can be changed, otherwise check W's height.
1935 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
1936 siblings, too. If none of the siblings is resizable, WINDOW isn't
1937 either. */
1939 static int
1940 window_fixed_size_p (w, width_p, check_siblings_p)
1941 struct window *w;
1942 int width_p, check_siblings_p;
1944 int fixed_p;
1945 struct window *c;
1947 if (!NILP (w->hchild))
1949 c = XWINDOW (w->hchild);
1951 if (width_p)
1953 /* A horiz. combination is fixed-width if all of if its
1954 children are. */
1955 while (c && window_fixed_size_p (c, width_p, 0))
1956 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
1957 fixed_p = c == NULL;
1959 else
1961 /* A horiz. combination is fixed-height if one of if its
1962 children is. */
1963 while (c && !window_fixed_size_p (c, width_p, 0))
1964 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
1965 fixed_p = c != NULL;
1968 else if (!NILP (w->vchild))
1970 c = XWINDOW (w->vchild);
1972 if (width_p)
1974 /* A vert. combination is fixed-width if one of if its
1975 children is. */
1976 while (c && !window_fixed_size_p (c, width_p, 0))
1977 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
1978 fixed_p = c != NULL;
1980 else
1982 /* A vert. combination is fixed-height if all of if its
1983 children are. */
1984 while (c && window_fixed_size_p (c, width_p, 0))
1985 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
1986 fixed_p = c == NULL;
1989 else if (BUFFERP (w->buffer))
1991 if (w->height_fixed_p && !width_p)
1992 fixed_p = 1;
1993 else
1995 struct buffer *old = current_buffer;
1996 Lisp_Object val;
1998 current_buffer = XBUFFER (w->buffer);
1999 val = find_symbol_value (Qwindow_size_fixed);
2000 current_buffer = old;
2002 fixed_p = 0;
2003 if (!EQ (val, Qunbound))
2005 fixed_p = !NILP (val);
2007 if (fixed_p
2008 && ((EQ (val, Qheight) && width_p)
2009 || (EQ (val, Qwidth) && !width_p)))
2010 fixed_p = 0;
2014 /* Can't tell if this one is resizable without looking at
2015 siblings. If all siblings are fixed-size this one is too. */
2016 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2018 Lisp_Object child;
2020 for (child = w->prev; !NILP (child); child = XWINDOW (child)->prev)
2021 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2022 break;
2024 if (NILP (child))
2025 for (child = w->next; !NILP (child); child = XWINDOW (child)->next)
2026 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2027 break;
2029 if (NILP (child))
2030 fixed_p = 1;
2033 else
2034 fixed_p = 1;
2036 return fixed_p;
2040 /* Return the minimum size of window W, not taking fixed-width windows
2041 into account. WIDTH_P non-zero means return the minimum width,
2042 otherwise return the minimum height. If W is a combination window,
2043 compute the minimum size from the minimum sizes of W's children. */
2045 static int
2046 window_min_size_1 (w, width_p)
2047 struct window *w;
2048 int width_p;
2050 struct window *c;
2051 int size;
2053 if (!NILP (w->hchild))
2055 c = XWINDOW (w->hchild);
2056 size = 0;
2058 if (width_p)
2060 /* The min width of a horizontal combination is
2061 the sum of the min widths of its children. */
2062 while (c)
2064 size += window_min_size_1 (c, width_p);
2065 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2068 else
2070 /* The min height a horizontal combination equals
2071 the maximum of all min height of its children. */
2072 while (c)
2074 int min_size = window_min_size_1 (c, width_p);
2075 size = max (min_size, size);
2076 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2080 else if (!NILP (w->vchild))
2082 c = XWINDOW (w->vchild);
2083 size = 0;
2085 if (width_p)
2087 /* The min width of a vertical combination is
2088 the maximum of the min widths of its children. */
2089 while (c)
2091 int min_size = window_min_size_1 (c, width_p);
2092 size = max (min_size, size);
2093 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2096 else
2098 /* The min height of a vertical combination equals
2099 the sum of the min height of its children. */
2100 while (c)
2102 size += window_min_size_1 (c, width_p);
2103 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2107 else
2109 if (width_p)
2110 size = window_min_width;
2111 else
2113 if (MINI_WINDOW_P (w)
2114 || (!WINDOW_WANTS_MODELINE_P (w)
2115 && !WINDOW_WANTS_HEADER_LINE_P (w)))
2116 size = 1;
2117 else
2118 size = window_min_height;
2122 return size;
2126 /* Return the minimum size of window W, taking fixed-size windows into
2127 account. WIDTH_P non-zero means return the minimum width,
2128 otherwise return the minimum height. IGNORE_FIXED_P non-zero means
2129 ignore if W is fixed-size. Set *FIXED to 1 if W is fixed-size
2130 unless FIXED is null. */
2132 static int
2133 window_min_size (w, width_p, ignore_fixed_p, fixed)
2134 struct window *w;
2135 int width_p, ignore_fixed_p, *fixed;
2137 int size, fixed_p;
2139 if (ignore_fixed_p)
2140 fixed_p = 0;
2141 else
2142 fixed_p = window_fixed_size_p (w, width_p, 1);
2144 if (fixed)
2145 *fixed = fixed_p;
2147 if (fixed_p)
2148 size = width_p ? XFASTINT (w->width) : XFASTINT (w->height);
2149 else
2150 size = window_min_size_1 (w, width_p);
2152 return size;
2156 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
2157 WINDOW's width. Resize WINDOW's children, if any, so that they
2158 keep their proportionate size relative to WINDOW. Propagate
2159 WINDOW's top or left edge position to children. Delete windows
2160 that become too small unless NODELETE_P is non-zero. */
2162 static void
2163 size_window (window, size, width_p, nodelete_p)
2164 Lisp_Object window;
2165 int size, width_p, nodelete_p;
2167 struct window *w = XWINDOW (window);
2168 struct window *c;
2169 Lisp_Object child, *forward, *sideward;
2170 int old_size, min_size;
2172 check_min_window_sizes ();
2174 /* If the window has been "too small" at one point,
2175 don't delete it for being "too small" in the future.
2176 Preserve it as long as that is at all possible. */
2177 if (width_p)
2179 old_size = XFASTINT (w->width);
2180 min_size = window_min_width;
2182 else
2184 old_size = XFASTINT (w->height);
2185 min_size = window_min_height;
2188 if (old_size < window_min_width)
2189 w->too_small_ok = Qt;
2191 /* Maybe delete WINDOW if it's too small. */
2192 if (!nodelete_p && !NILP (w->parent))
2194 int min_size;
2196 if (!MINI_WINDOW_P (w) && !NILP (w->too_small_ok))
2197 min_size = width_p ? MIN_SAFE_WINDOW_WIDTH : MIN_SAFE_WINDOW_HEIGHT;
2198 else
2199 min_size = width_p ? window_min_width : window_min_height;
2201 if (size < min_size)
2203 delete_window (window);
2204 return;
2208 /* Set redisplay hints. */
2209 XSETFASTINT (w->last_modified, 0);
2210 XSETFASTINT (w->last_overlay_modified, 0);
2211 windows_or_buffers_changed++;
2212 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
2214 if (width_p)
2216 sideward = &w->vchild;
2217 forward = &w->hchild;
2218 XSETFASTINT (w->width, size);
2220 else
2222 sideward = &w->hchild;
2223 forward = &w->vchild;
2224 XSETFASTINT (w->height, size);
2227 if (!NILP (*sideward))
2229 for (child = *sideward; !NILP (child); child = c->next)
2231 c = XWINDOW (child);
2232 if (width_p)
2233 c->left = w->left;
2234 else
2235 c->top = w->top;
2236 size_window (child, size, width_p, nodelete_p);
2239 else if (!NILP (*forward))
2241 int fixed_size, each, extra, n;
2242 int resize_fixed_p, nfixed;
2243 int last_pos, first_pos, nchildren;
2245 /* Determine the fixed-size portion of the this window, and the
2246 number of child windows. */
2247 fixed_size = nchildren = nfixed = 0;
2248 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
2250 c = XWINDOW (child);
2251 if (window_fixed_size_p (c, width_p, 0))
2253 fixed_size += (width_p
2254 ? XFASTINT (c->width) : XFASTINT (c->height));
2255 ++nfixed;
2259 /* If the new size is smaller than fixed_size, or if there
2260 aren't any resizable windows, allow resizing fixed-size
2261 windows. */
2262 resize_fixed_p = nfixed == nchildren || size < fixed_size;
2264 /* Compute how many lines/columns to add to each child. The
2265 value of extra takes care of rounding errors. */
2266 n = resize_fixed_p ? nchildren : nchildren - nfixed;
2267 each = (size - old_size) / n;
2268 extra = (size - old_size) - n * each;
2270 /* Compute new children heights and edge positions. */
2271 first_pos = width_p ? XFASTINT (w->left) : XFASTINT (w->top);
2272 last_pos = first_pos;
2273 for (child = *forward; !NILP (child); child = c->next)
2275 int new_size, old_size;
2277 c = XWINDOW (child);
2278 old_size = width_p ? XFASTINT (c->width) : XFASTINT (c->height);
2279 new_size = old_size;
2281 /* The top or left edge position of this child equals the
2282 bottom or right edge of its predecessor. */
2283 if (width_p)
2284 c->left = make_number (last_pos);
2285 else
2286 c->top = make_number (last_pos);
2288 /* If this child can be resized, do it. */
2289 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
2291 new_size = old_size + each + extra;
2292 extra = 0;
2295 /* Set new height. Note that size_window also propagates
2296 edge positions to children, so it's not a no-op if we
2297 didn't change the child's size. */
2298 size_window (child, new_size, width_p, 1);
2300 /* Remember the bottom/right edge position of this child; it
2301 will be used to set the top/left edge of the next child. */
2302 last_pos += new_size;
2305 /* We should have covered the parent exactly with child windows. */
2306 xassert (size == last_pos - first_pos);
2308 /* Now delete any children that became too small. */
2309 if (!nodelete_p)
2310 for (child = *forward; !NILP (child); child = c->next)
2312 int child_size;
2313 c = XWINDOW (child);
2314 child_size = width_p ? XFASTINT (c->width) : XFASTINT (c->height);
2315 size_window (child, child_size, width_p, 0);
2320 /* Set WINDOW's height to HEIGHT, and recursively change the height of
2321 WINDOW's children. NODELETE non-zero means don't delete windows
2322 that become too small in the process. (The caller should check
2323 later and do so if appropriate.) */
2325 void
2326 set_window_height (window, height, nodelete)
2327 Lisp_Object window;
2328 int height;
2329 int nodelete;
2331 size_window (window, height, 0, nodelete);
2335 /* Set WINDOW's width to WIDTH, and recursively change the width of
2336 WINDOW's children. NODELETE non-zero means don't delete windows
2337 that become too small in the process. (The caller should check
2338 later and do so if appropriate.) */
2340 void
2341 set_window_width (window, width, nodelete)
2342 Lisp_Object window;
2343 int width;
2344 int nodelete;
2346 size_window (window, width, 1, nodelete);
2350 int window_select_count;
2352 Lisp_Object
2353 Fset_window_buffer_unwind (obuf)
2354 Lisp_Object obuf;
2356 Fset_buffer (obuf);
2357 return Qnil;
2361 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2362 means it's allowed to run hooks. See make_frame for a case where
2363 it's not allowed. */
2365 void
2366 set_window_buffer (window, buffer, run_hooks_p)
2367 Lisp_Object window, buffer;
2368 int run_hooks_p;
2370 struct window *w = XWINDOW (window);
2371 struct buffer *b = XBUFFER (buffer);
2372 int count = specpdl_ptr - specpdl;
2374 w->buffer = buffer;
2376 if (EQ (window, selected_window))
2377 b->last_selected_window = window;
2379 /* Update time stamps of buffer display. */
2380 if (INTEGERP (b->display_count))
2381 XSETINT (b->display_count, XINT (b->display_count) + 1);
2382 b->display_time = Fcurrent_time ();
2384 XSETFASTINT (w->window_end_pos, 0);
2385 XSETFASTINT (w->window_end_vpos, 0);
2386 bzero (&w->last_cursor, sizeof w->last_cursor);
2387 w->window_end_valid = Qnil;
2388 XSETFASTINT (w->hscroll, 0);
2389 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
2390 set_marker_restricted (w->start,
2391 make_number (b->last_window_start),
2392 buffer);
2393 w->start_at_line_beg = Qnil;
2394 w->force_start = Qnil;
2395 XSETFASTINT (w->last_modified, 0);
2396 XSETFASTINT (w->last_overlay_modified, 0);
2397 windows_or_buffers_changed++;
2399 /* We must select BUFFER for running the window-scroll-functions.
2400 If WINDOW is selected, switch permanently.
2401 Otherwise, switch but go back to the ambient buffer afterward. */
2402 if (EQ (window, selected_window))
2403 Fset_buffer (buffer);
2404 /* We can't check ! NILP (Vwindow_scroll_functions) here
2405 because that might itself be a local variable. */
2406 else if (window_initialized)
2408 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
2409 Fset_buffer (buffer);
2412 /* Set left and right marginal area width from buffer. */
2413 Fset_window_margins (window, b->left_margin_width, b->right_margin_width);
2415 if (run_hooks_p)
2417 if (! NILP (Vwindow_scroll_functions))
2418 run_hook_with_args_2 (Qwindow_scroll_functions, window,
2419 Fmarker_position (w->start));
2421 if (! NILP (Vwindow_configuration_change_hook)
2422 && ! NILP (Vrun_hooks))
2423 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2426 unbind_to (count, Qnil);
2430 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
2431 "Make WINDOW display BUFFER as its contents.\n\
2432 BUFFER can be a buffer or buffer name.")
2433 (window, buffer)
2434 register Lisp_Object window, buffer;
2436 register Lisp_Object tem;
2437 register struct window *w = decode_window (window);
2439 buffer = Fget_buffer (buffer);
2440 CHECK_BUFFER (buffer, 1);
2442 if (NILP (XBUFFER (buffer)->name))
2443 error ("Attempt to display deleted buffer");
2445 tem = w->buffer;
2446 if (NILP (tem))
2447 error ("Window is deleted");
2448 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
2449 is first being set up. */
2451 if (!NILP (w->dedicated) && !EQ (tem, buffer))
2452 error ("Window is dedicated to `%s'",
2453 XSTRING (XBUFFER (tem)->name)->data);
2455 unshow_buffer (w);
2458 set_window_buffer (window, buffer, 1);
2459 return Qnil;
2462 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
2463 "Select WINDOW. Most editing will apply to WINDOW's buffer.\n\
2464 If WINDOW is not already selected, also make WINDOW's buffer current.\n\
2465 Note that the main editor command loop\n\
2466 selects the buffer of the selected window before each command.")
2467 (window)
2468 register Lisp_Object window;
2470 return select_window_1 (window, 1);
2473 static Lisp_Object
2474 select_window_1 (window, recordflag)
2475 register Lisp_Object window;
2476 int recordflag;
2478 register struct window *w;
2479 register struct window *ow = XWINDOW (selected_window);
2480 struct frame *sf;
2482 CHECK_LIVE_WINDOW (window, 0);
2484 w = XWINDOW (window);
2486 if (NILP (w->buffer))
2487 error ("Trying to select deleted window or non-leaf window");
2489 XSETFASTINT (w->use_time, ++window_select_count);
2490 if (EQ (window, selected_window))
2491 return window;
2493 if (! NILP (ow->buffer))
2494 set_marker_both (ow->pointm, ow->buffer,
2495 BUF_PT (XBUFFER (ow->buffer)),
2496 BUF_PT_BYTE (XBUFFER (ow->buffer)));
2498 selected_window = window;
2499 sf = SELECTED_FRAME ();
2500 if (XFRAME (WINDOW_FRAME (w)) != sf)
2502 XFRAME (WINDOW_FRAME (w))->selected_window = window;
2503 /* Use this rather than Fhandle_switch_frame
2504 so that FRAME_FOCUS_FRAME is moved appropriately as we
2505 move around in the state where a minibuffer in a separate
2506 frame is active. */
2507 Fselect_frame (WINDOW_FRAME (w), Qnil);
2509 else
2510 sf->selected_window = window;
2512 if (recordflag)
2513 record_buffer (w->buffer);
2514 Fset_buffer (w->buffer);
2516 XBUFFER (w->buffer)->last_selected_window = window;
2518 /* Go to the point recorded in the window.
2519 This is important when the buffer is in more
2520 than one window. It also matters when
2521 redisplay_window has altered point after scrolling,
2522 because it makes the change only in the window. */
2524 register int new_point = marker_position (w->pointm);
2525 if (new_point < BEGV)
2526 SET_PT (BEGV);
2527 else if (new_point > ZV)
2528 SET_PT (ZV);
2529 else
2530 SET_PT (new_point);
2533 windows_or_buffers_changed++;
2534 return window;
2537 /* Deiconify the frame containing the window WINDOW,
2538 unless it is the selected frame;
2539 then return WINDOW.
2541 The reason for the exception for the selected frame
2542 is that it seems better not to change the selected frames visibility
2543 merely because of displaying a different buffer in it.
2544 The deiconification is useful when a buffer gets shown in
2545 another frame that you were not using lately. */
2547 static Lisp_Object
2548 display_buffer_1 (window)
2549 Lisp_Object window;
2551 Lisp_Object frame = XWINDOW (window)->frame;
2552 FRAME_PTR f = XFRAME (frame);
2554 FRAME_SAMPLE_VISIBILITY (f);
2556 if (!EQ (frame, selected_frame))
2558 if (FRAME_ICONIFIED_P (f))
2559 Fmake_frame_visible (frame);
2560 else if (FRAME_VISIBLE_P (f))
2561 Fraise_frame (frame);
2564 return window;
2567 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
2568 "Returns non-nil if a buffer named BUFFER-NAME would be created specially.\n\
2569 The value is actually t if the frame should be called with default frame\n\
2570 parameters, and a list of frame parameters if they were specified.\n\
2571 See `special-display-buffer-names', and `special-display-regexps'.")
2572 (buffer_name)
2573 Lisp_Object buffer_name;
2575 Lisp_Object tem;
2577 CHECK_STRING (buffer_name, 1);
2579 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
2580 if (!NILP (tem))
2581 return Qt;
2583 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
2584 if (!NILP (tem))
2585 return XCDR (tem);
2587 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
2589 Lisp_Object car = XCAR (tem);
2590 if (STRINGP (car)
2591 && fast_string_match (car, buffer_name) >= 0)
2592 return Qt;
2593 else if (CONSP (car)
2594 && STRINGP (XCAR (car))
2595 && fast_string_match (XCAR (car), buffer_name) >= 0)
2596 return XCDR (car);
2598 return Qnil;
2601 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
2602 "Returns non-nil if a new buffer named BUFFER-NAME would use the same window.\n\
2603 See `same-window-buffer-names' and `same-window-regexps'.")
2604 (buffer_name)
2605 Lisp_Object buffer_name;
2607 Lisp_Object tem;
2609 CHECK_STRING (buffer_name, 1);
2611 tem = Fmember (buffer_name, Vsame_window_buffer_names);
2612 if (!NILP (tem))
2613 return Qt;
2615 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
2616 if (!NILP (tem))
2617 return Qt;
2619 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
2621 Lisp_Object car = XCAR (tem);
2622 if (STRINGP (car)
2623 && fast_string_match (car, buffer_name) >= 0)
2624 return Qt;
2625 else if (CONSP (car)
2626 && STRINGP (XCAR (car))
2627 && fast_string_match (XCAR (car), buffer_name) >= 0)
2628 return Qt;
2630 return Qnil;
2633 /* Use B so the default is (other-buffer). */
2634 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
2635 "BDisplay buffer: \nP",
2636 "Make BUFFER appear in some window but don't select it.\n\
2637 BUFFER can be a buffer or a buffer name.\n\
2638 If BUFFER is shown already in some window, just use that one,\n\
2639 unless the window is the selected window and the optional second\n\
2640 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
2641 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
2642 Returns the window displaying BUFFER.\n\
2644 The variables `special-display-buffer-names', `special-display-regexps',\n\
2645 `same-window-buffer-names', and `same-window-regexps' customize how certain\n\
2646 buffer names are handled.\n\
2648 If optional argument FRAME is `visible', search all visible frames.\n\
2649 If FRAME is 0, search all visible and iconified frames.\n\
2650 If FRAME is t, search all frames.\n\
2651 If FRAME is a frame, search only that frame.\n\
2652 If FRAME is nil, search only the selected frame\n\
2653 (actually the last nonminibuffer frame),\n\
2654 unless `pop-up-frames' is non-nil,\n\
2655 which means search visible and iconified frames.")
2656 (buffer, not_this_window, frame)
2657 register Lisp_Object buffer, not_this_window, frame;
2659 register Lisp_Object window, tem, swp;
2660 struct frame *f;
2662 swp = Qnil;
2663 buffer = Fget_buffer (buffer);
2664 CHECK_BUFFER (buffer, 0);
2666 if (!NILP (Vdisplay_buffer_function))
2667 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2669 if (NILP (not_this_window)
2670 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
2671 return display_buffer_1 (selected_window);
2673 /* See if the user has specified this buffer should appear
2674 in the selected window. */
2675 if (NILP (not_this_window))
2677 swp = Fsame_window_p (XBUFFER (buffer)->name);
2678 if (!NILP (swp) && !no_switch_window (selected_window))
2680 Fswitch_to_buffer (buffer, Qnil);
2681 return display_buffer_1 (selected_window);
2685 /* If pop_up_frames,
2686 look for a window showing BUFFER on any visible or iconified frame.
2687 Otherwise search only the current frame. */
2688 if (! NILP (frame))
2689 tem = frame;
2690 else if (pop_up_frames || last_nonminibuf_frame == 0)
2691 XSETFASTINT (tem, 0);
2692 else
2693 XSETFRAME (tem, last_nonminibuf_frame);
2694 window = Fget_buffer_window (buffer, tem);
2695 if (!NILP (window)
2696 && (NILP (not_this_window) || !EQ (window, selected_window)))
2698 return display_buffer_1 (window);
2701 /* Certain buffer names get special handling. */
2702 if (!NILP (Vspecial_display_function) && NILP (swp))
2704 tem = Fspecial_display_p (XBUFFER (buffer)->name);
2705 if (EQ (tem, Qt))
2706 return call1 (Vspecial_display_function, buffer);
2707 if (CONSP (tem))
2708 return call2 (Vspecial_display_function, buffer, tem);
2711 /* If there are no frames open that have more than a minibuffer,
2712 we need to create a new frame. */
2713 if (pop_up_frames || last_nonminibuf_frame == 0)
2715 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2716 Fset_window_buffer (window, buffer);
2717 return display_buffer_1 (window);
2720 f = SELECTED_FRAME ();
2721 if (pop_up_windows
2722 || FRAME_MINIBUF_ONLY_P (f)
2723 /* If the current frame is a special display frame,
2724 don't try to reuse its windows. */
2725 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (f))->dedicated))
2727 Lisp_Object frames;
2729 frames = Qnil;
2730 if (FRAME_MINIBUF_ONLY_P (f))
2731 XSETFRAME (frames, last_nonminibuf_frame);
2732 /* Don't try to create a window if would get an error */
2733 if (split_height_threshold < window_min_height << 1)
2734 split_height_threshold = window_min_height << 1;
2736 /* Note that both Fget_largest_window and Fget_lru_window
2737 ignore minibuffers and dedicated windows.
2738 This means they can return nil. */
2740 /* If the frame we would try to split cannot be split,
2741 try other frames. */
2742 if (FRAME_NO_SPLIT_P (NILP (frames) ? f : last_nonminibuf_frame))
2744 /* Try visible frames first. */
2745 window = Fget_largest_window (Qvisible);
2746 /* If that didn't work, try iconified frames. */
2747 if (NILP (window))
2748 window = Fget_largest_window (make_number (0));
2749 if (NILP (window))
2750 window = Fget_largest_window (Qt);
2752 else
2753 window = Fget_largest_window (frames);
2755 /* If we got a tall enough full-width window that can be split,
2756 split it. */
2757 if (!NILP (window)
2758 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2759 && window_height (window) >= split_height_threshold
2760 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
2761 window = Fsplit_window (window, Qnil, Qnil);
2762 else
2764 Lisp_Object upper, lower, other;
2766 window = Fget_lru_window (frames);
2767 /* If the LRU window is selected, and big enough,
2768 and can be split, split it. */
2769 if (!NILP (window)
2770 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2771 && (EQ (window, selected_window)
2772 || EQ (XWINDOW (window)->parent, Qnil))
2773 && window_height (window) >= window_min_height << 1)
2774 window = Fsplit_window (window, Qnil, Qnil);
2775 /* If Fget_lru_window returned nil, try other approaches. */
2777 /* Try visible frames first. */
2778 if (NILP (window))
2779 window = Fget_buffer_window (buffer, Qvisible);
2780 if (NILP (window))
2781 window = Fget_largest_window (Qvisible);
2782 /* If that didn't work, try iconified frames. */
2783 if (NILP (window))
2784 window = Fget_buffer_window (buffer, make_number (0));
2785 if (NILP (window))
2786 window = Fget_largest_window (make_number (0));
2787 /* Try invisible frames. */
2788 if (NILP (window))
2789 window = Fget_buffer_window (buffer, Qt);
2790 if (NILP (window))
2791 window = Fget_largest_window (Qt);
2792 /* As a last resort, make a new frame. */
2793 if (NILP (window))
2794 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2795 /* If window appears above or below another,
2796 even out their heights. */
2797 other = upper = lower = Qnil;
2798 if (!NILP (XWINDOW (window)->prev))
2799 other = upper = XWINDOW (window)->prev, lower = window;
2800 if (!NILP (XWINDOW (window)->next))
2801 other = lower = XWINDOW (window)->next, upper = window;
2802 if (!NILP (other)
2803 /* Check that OTHER and WINDOW are vertically arrayed. */
2804 && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
2805 && (XFASTINT (XWINDOW (other)->height)
2806 > XFASTINT (XWINDOW (window)->height)))
2808 int total = (XFASTINT (XWINDOW (other)->height)
2809 + XFASTINT (XWINDOW (window)->height));
2810 enlarge_window (upper,
2811 total / 2 - XFASTINT (XWINDOW (upper)->height),
2816 else
2817 window = Fget_lru_window (Qnil);
2819 Fset_window_buffer (window, buffer);
2820 return display_buffer_1 (window);
2823 void
2824 temp_output_buffer_show (buf)
2825 register Lisp_Object buf;
2827 register struct buffer *old = current_buffer;
2828 register Lisp_Object window;
2829 register struct window *w;
2831 XBUFFER (buf)->directory = current_buffer->directory;
2833 Fset_buffer (buf);
2834 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
2835 BEGV = BEG;
2836 ZV = Z;
2837 SET_PT (BEG);
2838 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
2839 set_buffer_internal (old);
2841 if (!EQ (Vtemp_buffer_show_function, Qnil))
2842 call1 (Vtemp_buffer_show_function, buf);
2843 else
2845 window = Fdisplay_buffer (buf, Qnil, Qnil);
2847 if (!EQ (XWINDOW (window)->frame, selected_frame))
2848 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2849 Vminibuf_scroll_window = window;
2850 w = XWINDOW (window);
2851 XSETFASTINT (w->hscroll, 0);
2852 set_marker_restricted_both (w->start, buf, 1, 1);
2853 set_marker_restricted_both (w->pointm, buf, 1, 1);
2855 /* Run temp-buffer-show-hook, with the chosen window selected
2856 and it sbuffer current. */
2857 if (!NILP (Vrun_hooks))
2859 Lisp_Object tem;
2860 tem = Fboundp (Qtemp_buffer_show_hook);
2861 if (!NILP (tem))
2863 tem = Fsymbol_value (Qtemp_buffer_show_hook);
2864 if (!NILP (tem))
2866 int count = specpdl_ptr - specpdl;
2867 Lisp_Object prev_window;
2868 prev_window = selected_window;
2870 /* Select the window that was chosen, for running the hook. */
2871 record_unwind_protect (Fselect_window, prev_window);
2872 select_window_1 (window, 0);
2873 Fset_buffer (w->buffer);
2874 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
2875 select_window_1 (prev_window, 0);
2876 unbind_to (count, Qnil);
2883 static void
2884 make_dummy_parent (window)
2885 Lisp_Object window;
2887 Lisp_Object new;
2888 register struct window *o, *p;
2889 register struct Lisp_Vector *vec;
2890 int i;
2892 o = XWINDOW (window);
2893 vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
2894 for (i = 0; i < VECSIZE (struct window); ++i)
2895 vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
2896 vec->size = VECSIZE (struct window);
2897 p = (struct window *)vec;
2898 XSETWINDOW (new, p);
2900 XSETFASTINT (p->sequence_number, ++sequence_number);
2902 /* Put new into window structure in place of window */
2903 replace_window (window, new);
2905 o->next = Qnil;
2906 o->prev = Qnil;
2907 o->vchild = Qnil;
2908 o->hchild = Qnil;
2909 o->parent = new;
2911 p->start = Qnil;
2912 p->pointm = Qnil;
2913 p->buffer = Qnil;
2916 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
2917 "Split WINDOW, putting SIZE lines in the first of the pair.\n\
2918 WINDOW defaults to selected one and SIZE to half its size.\n\
2919 If optional third arg HORFLAG is non-nil, split side by side\n\
2920 and put SIZE columns in the first of the pair. In that case,\n\
2921 SIZE includes that window's scroll bar, or the divider column to its right.")
2922 (window, size, horflag)
2923 Lisp_Object window, size, horflag;
2925 register Lisp_Object new;
2926 register struct window *o, *p;
2927 FRAME_PTR fo;
2928 register int size_int;
2930 if (NILP (window))
2931 window = selected_window;
2932 else
2933 CHECK_LIVE_WINDOW (window, 0);
2935 o = XWINDOW (window);
2936 fo = XFRAME (WINDOW_FRAME (o));
2938 if (NILP (size))
2940 if (!NILP (horflag))
2941 /* Calculate the size of the left-hand window, by dividing
2942 the usable space in columns by two.
2943 We round up, since the left-hand window may include
2944 a dividing line, while the right-hand may not. */
2945 size_int = (XFASTINT (o->width) + 1) >> 1;
2946 else
2947 size_int = XFASTINT (o->height) >> 1;
2949 else
2951 CHECK_NUMBER (size, 1);
2952 size_int = XINT (size);
2955 if (MINI_WINDOW_P (o))
2956 error ("Attempt to split minibuffer window");
2957 else if (window_fixed_size_p (o, !NILP (horflag), 0))
2958 error ("Attempt to split fixed-size window");
2960 check_min_window_sizes ();
2962 if (NILP (horflag))
2964 if (size_int < window_min_height)
2965 error ("Window height %d too small (after splitting)", size_int);
2966 if (size_int + window_min_height > XFASTINT (o->height))
2967 error ("Window height %d too small (after splitting)",
2968 XFASTINT (o->height) - size_int);
2969 if (NILP (o->parent)
2970 || NILP (XWINDOW (o->parent)->vchild))
2972 make_dummy_parent (window);
2973 new = o->parent;
2974 XWINDOW (new)->vchild = window;
2977 else
2979 if (size_int < window_min_width)
2980 error ("Window width %d too small (after splitting)", size_int);
2982 if (size_int + window_min_width > XFASTINT (o->width))
2983 error ("Window width %d too small (after splitting)",
2984 XFASTINT (o->width) - size_int);
2985 if (NILP (o->parent)
2986 || NILP (XWINDOW (o->parent)->hchild))
2988 make_dummy_parent (window);
2989 new = o->parent;
2990 XWINDOW (new)->hchild = window;
2994 /* Now we know that window's parent is a vertical combination
2995 if we are dividing vertically, or a horizontal combination
2996 if we are making side-by-side windows */
2998 windows_or_buffers_changed++;
2999 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3000 new = make_window ();
3001 p = XWINDOW (new);
3003 p->frame = o->frame;
3004 p->next = o->next;
3005 if (!NILP (p->next))
3006 XWINDOW (p->next)->prev = new;
3007 p->prev = window;
3008 o->next = new;
3009 p->parent = o->parent;
3010 p->buffer = Qt;
3011 p->window_end_valid = Qnil;
3012 bzero (&p->last_cursor, sizeof p->last_cursor);
3014 /* Apportion the available frame space among the two new windows */
3016 if (!NILP (horflag))
3018 p->height = o->height;
3019 p->top = o->top;
3020 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
3021 XSETFASTINT (o->width, size_int);
3022 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
3024 else
3026 p->left = o->left;
3027 p->width = o->width;
3028 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
3029 XSETFASTINT (o->height, size_int);
3030 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
3033 /* Adjust glyph matrices. */
3034 adjust_glyphs (fo);
3035 Fset_window_buffer (new, o->buffer);
3036 return new;
3039 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3040 "Make current window ARG lines bigger.\n\
3041 From program, optional second arg non-nil means grow sideways ARG columns.")
3042 (arg, side)
3043 register Lisp_Object arg, side;
3045 CHECK_NUMBER (arg, 0);
3046 enlarge_window (selected_window, XINT (arg), !NILP (side));
3048 if (! NILP (Vwindow_configuration_change_hook))
3049 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3051 return Qnil;
3054 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3055 "Make current window ARG lines smaller.\n\
3056 From program, optional second arg non-nil means shrink sideways arg columns.")
3057 (arg, side)
3058 register Lisp_Object arg, side;
3060 CHECK_NUMBER (arg, 0);
3061 enlarge_window (selected_window, -XINT (arg), !NILP (side));
3063 if (! NILP (Vwindow_configuration_change_hook))
3064 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3066 return Qnil;
3070 window_height (window)
3071 Lisp_Object window;
3073 register struct window *p = XWINDOW (window);
3074 return XFASTINT (p->height);
3078 window_width (window)
3079 Lisp_Object window;
3081 register struct window *p = XWINDOW (window);
3082 return XFASTINT (p->width);
3086 #define CURBEG(w) \
3087 *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
3089 #define CURSIZE(w) \
3090 *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
3093 /* Enlarge selected_window by DELTA. WIDTHFLAG non-zero means
3094 increase its width. Siblings of the selected window are resized to
3095 fullfil the size request. If they become too small in the process,
3096 they will be deleted. */
3098 static void
3099 enlarge_window (window, delta, widthflag)
3100 Lisp_Object window;
3101 int delta, widthflag;
3103 Lisp_Object parent, next, prev;
3104 struct window *p;
3105 int *sizep, maximum;
3106 int (*sizefun) P_ ((Lisp_Object))
3107 = widthflag ? window_width : window_height;
3108 void (*setsizefun) P_ ((Lisp_Object, int, int))
3109 = (widthflag ? set_window_width : set_window_height);
3111 /* Check values of window_min_width and window_min_height for
3112 validity. */
3113 check_min_window_sizes ();
3115 /* Give up if this window cannot be resized. */
3116 if (window_fixed_size_p (XWINDOW (window), widthflag, 1))
3117 error ("Window is not resizable");
3119 /* Find the parent of the selected window. */
3120 while (1)
3122 p = XWINDOW (window);
3123 parent = p->parent;
3125 if (NILP (parent))
3127 if (widthflag)
3128 error ("No other window to side of this one");
3129 break;
3132 if (widthflag
3133 ? !NILP (XWINDOW (parent)->hchild)
3134 : !NILP (XWINDOW (parent)->vchild))
3135 break;
3137 window = parent;
3140 sizep = &CURSIZE (window);
3143 register int maxdelta;
3145 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
3146 : !NILP (p->next) ? ((*sizefun) (p->next)
3147 - window_min_size (XWINDOW (p->next),
3148 widthflag, 0, 0))
3149 : !NILP (p->prev) ? ((*sizefun) (p->prev)
3150 - window_min_size (XWINDOW (p->prev),
3151 widthflag, 0, 0))
3152 /* This is a frame with only one window, a minibuffer-only
3153 or a minibufferless frame. */
3154 : (delta = 0));
3156 if (delta > maxdelta)
3157 /* This case traps trying to make the minibuffer
3158 the full frame, or make the only window aside from the
3159 minibuffer the full frame. */
3160 delta = maxdelta;
3163 if (*sizep + delta < window_min_size (XWINDOW (window), widthflag, 0, 0))
3165 delete_window (window);
3166 return;
3169 if (delta == 0)
3170 return;
3172 /* Find the total we can get from other siblings. */
3173 maximum = 0;
3174 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
3175 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
3176 widthflag, 0, 0);
3177 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
3178 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
3179 widthflag, 0, 0);
3181 /* If we can get it all from them, do so. */
3182 if (delta <= maximum)
3184 Lisp_Object first_unaffected;
3185 Lisp_Object first_affected;
3186 int fixed_p;
3188 next = p->next;
3189 prev = p->prev;
3190 first_affected = window;
3191 /* Look at one sibling at a time,
3192 moving away from this window in both directions alternately,
3193 and take as much as we can get without deleting that sibling. */
3194 while (delta != 0 && (!NILP (next) || !NILP (prev)))
3196 if (! NILP (next))
3198 int this_one = ((*sizefun) (next)
3199 - window_min_size (XWINDOW (next),
3200 widthflag, 0, &fixed_p));
3201 if (!fixed_p)
3203 if (this_one > delta)
3204 this_one = delta;
3206 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
3207 (*setsizefun) (window, *sizep + this_one, 0);
3209 delta -= this_one;
3212 next = XWINDOW (next)->next;
3215 if (delta == 0)
3216 break;
3218 if (! NILP (prev))
3220 int this_one = ((*sizefun) (prev)
3221 - window_min_size (XWINDOW (prev),
3222 widthflag, 0, &fixed_p));
3223 if (!fixed_p)
3225 if (this_one > delta)
3226 this_one = delta;
3228 first_affected = prev;
3230 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
3231 (*setsizefun) (window, *sizep + this_one, 0);
3233 delta -= this_one;
3236 prev = XWINDOW (prev)->prev;
3240 xassert (delta == 0);
3242 /* Now recalculate the edge positions of all the windows affected,
3243 based on the new sizes. */
3244 first_unaffected = next;
3245 prev = first_affected;
3246 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
3247 prev = next, next = XWINDOW (next)->next)
3249 CURBEG (next) = CURBEG (prev) + (*sizefun) (prev);
3250 /* This does not change size of NEXT,
3251 but it propagates the new top edge to its children */
3252 (*setsizefun) (next, (*sizefun) (next), 0);
3255 else
3257 register int delta1;
3258 register int opht = (*sizefun) (parent);
3260 /* If trying to grow this window to or beyond size of the parent,
3261 make delta1 so big that, on shrinking back down,
3262 all the siblings end up with less than one line and are deleted. */
3263 if (opht <= *sizep + delta)
3264 delta1 = opht * opht * 2;
3265 else
3267 /* Otherwise, make delta1 just right so that if we add
3268 delta1 lines to this window and to the parent, and then
3269 shrink the parent back to its original size, the new
3270 proportional size of this window will increase by delta.
3272 The function size_window will compute the new height h'
3273 of the window from delta1 as:
3275 e = delta1/n
3276 x = delta1 - delta1/n * n for the 1st resizable child
3277 h' = h + e + x
3279 where n is the number of children that can be resized.
3280 We can ignore x by choosing a delta1 that is a multiple of
3281 n. We want the height of this window to come out as
3283 h' = h + delta
3285 So, delta1 must be
3287 h + e = h + delta
3288 delta1/n = delta
3289 delta1 = n * delta.
3291 The number of children n rquals the number of resizable
3292 children of this window + 1 because we know window itself
3293 is resizable (otherwise we would have signalled an error. */
3295 struct window *w = XWINDOW (window);
3296 Lisp_Object s;
3297 int n = 1;
3299 for (s = w->next; !NILP (s); s = XWINDOW (s)->next)
3300 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
3301 ++n;
3302 for (s = w->prev; !NILP (s); s = XWINDOW (s)->prev)
3303 if (!window_fixed_size_p (XWINDOW (s), widthflag, 0))
3304 ++n;
3306 delta1 = n * delta;
3309 /* Add delta1 lines or columns to this window, and to the parent,
3310 keeping things consistent while not affecting siblings. */
3311 CURSIZE (parent) = opht + delta1;
3312 (*setsizefun) (window, *sizep + delta1, 0);
3314 /* Squeeze out delta1 lines or columns from our parent,
3315 shriking this window and siblings proportionately.
3316 This brings parent back to correct size.
3317 Delta1 was calculated so this makes this window the desired size,
3318 taking it all out of the siblings. */
3319 (*setsizefun) (parent, opht, 0);
3322 XSETFASTINT (p->last_modified, 0);
3323 XSETFASTINT (p->last_overlay_modified, 0);
3325 /* Adjust glyph matrices. */
3326 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
3329 #undef CURBEG
3330 #undef CURSIZE
3334 /***********************************************************************
3335 Resizing Mini-Windows
3336 ***********************************************************************/
3338 static void shrink_window_lowest_first P_ ((struct window *, int));
3340 enum save_restore_action
3342 CHECK_ORIG_SIZES,
3343 SAVE_ORIG_SIZES,
3344 RESTORE_ORIG_SIZES
3347 static int save_restore_orig_size P_ ((struct window *,
3348 enum save_restore_action));
3350 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
3351 from lowest windows first. */
3353 static void
3354 shrink_window_lowest_first (w, height)
3355 struct window *w;
3356 int height;
3358 struct window *c;
3359 Lisp_Object child;
3360 int old_height;
3362 xassert (!MINI_WINDOW_P (w));
3364 /* Set redisplay hints. */
3365 XSETFASTINT (w->last_modified, 0);
3366 XSETFASTINT (w->last_overlay_modified, 0);
3367 windows_or_buffers_changed++;
3368 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
3370 old_height = XFASTINT (w->height);
3371 XSETFASTINT (w->height, height);
3373 if (!NILP (w->hchild))
3375 for (child = w->hchild; !NILP (child); child = c->next)
3377 c = XWINDOW (child);
3378 c->top = w->top;
3379 shrink_window_lowest_first (c, height);
3382 else if (!NILP (w->vchild))
3384 Lisp_Object last_child;
3385 int delta = old_height - height;
3386 int last_top;
3388 /* Find the last child. We are taking space from lowest windows
3389 first, so we iterate over children from the last child
3390 backwards. */
3391 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
3392 last_child = child;
3394 /* Assign new heights. We leave only MIN_SAFE_WINDOW_HEIGHT. */
3395 for (child = last_child; delta && !NILP (child); child = c->prev)
3397 int this_one;
3399 c = XWINDOW (child);
3400 this_one = XFASTINT (c->height) - MIN_SAFE_WINDOW_HEIGHT;
3402 if (this_one > delta)
3403 this_one = delta;
3405 shrink_window_lowest_first (c, XFASTINT (c->height) - this_one);
3406 delta -= this_one;
3409 /* Compute new positions. */
3410 last_top = w->top;
3411 for (child = w->vchild; !NILP (child); child = c->next)
3413 c = XWINDOW (child);
3414 c->top = make_number (last_top);
3415 shrink_window_lowest_first (c, XFASTINT (c->height));
3416 last_top += XFASTINT (c->height);
3422 /* Save, restore, or check positions and sizes in the window tree
3423 rooted at W. ACTION says what to do.
3425 If ACTION is CHECK_ORIG_SIZES, check if orig_top and orig_height
3426 members are valid for all windows in the window tree. Value is
3427 non-zero if they are valid.
3429 If ACTION is SAVE_ORIG_SIZES, save members top and height in
3430 orig_top and orig_height for all windows in the tree.
3432 If ACTION is RESTORE_ORIG_SIZES, restore top and height from
3433 values stored in orig_top and orig_height for all windows. */
3435 static int
3436 save_restore_orig_size (w, action)
3437 struct window *w;
3438 enum save_restore_action action;
3440 int success_p = 1;
3442 while (w)
3444 if (!NILP (w->hchild))
3446 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
3447 success_p = 0;
3449 else if (!NILP (w->vchild))
3451 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
3452 success_p = 0;
3455 switch (action)
3457 case CHECK_ORIG_SIZES:
3458 if (!INTEGERP (w->orig_top) || !INTEGERP (w->orig_height))
3459 return 0;
3460 break;
3462 case SAVE_ORIG_SIZES:
3463 w->orig_top = w->top;
3464 w->orig_height = w->height;
3465 XSETFASTINT (w->last_modified, 0);
3466 XSETFASTINT (w->last_overlay_modified, 0);
3467 break;
3469 case RESTORE_ORIG_SIZES:
3470 xassert (INTEGERP (w->orig_top) && INTEGERP (w->orig_height));
3471 w->top = w->orig_top;
3472 w->height = w->orig_height;
3473 w->orig_height = w->orig_top = Qnil;
3474 XSETFASTINT (w->last_modified, 0);
3475 XSETFASTINT (w->last_overlay_modified, 0);
3476 break;
3478 default:
3479 abort ();
3482 w = NILP (w->next) ? NULL : XWINDOW (w->next);
3485 return success_p;
3489 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
3490 without deleting other windows. */
3492 void
3493 grow_mini_window (w, delta)
3494 struct window *w;
3495 int delta;
3497 struct frame *f = XFRAME (w->frame);
3498 struct window *root;
3500 xassert (MINI_WINDOW_P (w));
3501 xassert (delta >= 0);
3503 /* Check values of window_min_width and window_min_height for
3504 validity. */
3505 check_min_window_sizes ();
3507 /* Compute how much we can enlarge the mini-window without deleting
3508 other windows. */
3509 root = XWINDOW (FRAME_ROOT_WINDOW (f));
3510 if (delta)
3512 int min_height = window_min_size (root, 0, 0, 0);
3513 if (XFASTINT (root->height) - delta < min_height)
3514 delta = XFASTINT (root->height) - min_height;
3517 if (delta)
3519 /* Save original window sizes and positions, if not already done. */
3520 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
3521 save_restore_orig_size (root, SAVE_ORIG_SIZES);
3523 /* Shrink other windows. */
3524 shrink_window_lowest_first (root, XFASTINT (root->height) - delta);
3526 /* Grow the mini-window. */
3527 w->top = make_number (XFASTINT (root->top) + XFASTINT (root)->height);
3528 w->height = make_number (XFASTINT (w->height) + delta);
3529 XSETFASTINT (w->last_modified, 0);
3530 XSETFASTINT (w->last_overlay_modified, 0);
3532 adjust_glyphs (f);
3537 /* Shrink mini-window W. If there is recorded info about window sizes
3538 before a call to grow_mini_window, restore recorded window sizes.
3539 Otherwise, if the mini-window is higher than 1 line, resize it to 1
3540 line. */
3542 void
3543 shrink_mini_window (w)
3544 struct window *w;
3546 struct frame *f = XFRAME (w->frame);
3547 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
3549 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
3551 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
3552 adjust_glyphs (f);
3553 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3554 windows_or_buffers_changed = 1;
3556 else if (XFASTINT (w->height) > 1)
3558 Lisp_Object window;
3559 XSETWINDOW (window, w);
3560 enlarge_window (window, 1 - XFASTINT (w->height), 0);
3566 /* Mark window cursors off for all windows in the window tree rooted
3567 at W by setting their phys_cursor_on_p flag to zero. Called from
3568 xterm.c, e.g. when a frame is cleared and thereby all cursors on
3569 the frame are cleared. */
3571 void
3572 mark_window_cursors_off (w)
3573 struct window *w;
3575 while (w)
3577 if (!NILP (w->hchild))
3578 mark_window_cursors_off (XWINDOW (w->hchild));
3579 else if (!NILP (w->vchild))
3580 mark_window_cursors_off (XWINDOW (w->vchild));
3581 else
3582 w->phys_cursor_on_p = 0;
3584 w = NILP (w->next) ? 0 : XWINDOW (w->next);
3589 /* Return number of lines of text (not counting mode line) in W. */
3592 window_internal_height (w)
3593 struct window *w;
3595 int ht = XFASTINT (w->height);
3597 if (MINI_WINDOW_P (w))
3598 return ht;
3600 if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
3601 || !NILP (w->next) || !NILP (w->prev)
3602 || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
3603 return ht - 1;
3605 return ht;
3609 /* Return the number of columns in W.
3610 Don't count columns occupied by scroll bars or the vertical bar
3611 separating W from the sibling to its right. */
3614 window_internal_width (w)
3615 struct window *w;
3617 struct frame *f = XFRAME (WINDOW_FRAME (w));
3618 int width = XINT (w->width);
3620 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
3621 /* Scroll bars occupy a few columns. */
3622 width -= FRAME_SCROLL_BAR_COLS (f);
3623 else if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
3624 /* The column of `|' characters separating side-by-side windows
3625 occupies one column only. */
3626 width -= 1;
3628 /* On window-systems, areas to the left and right of the window
3629 are used to display bitmaps there. */
3630 if (FRAME_WINDOW_P (f))
3631 width -= FRAME_FLAGS_AREA_COLS (f);
3633 return width;
3637 /************************************************************************
3638 Window Scrolling
3639 ***********************************************************************/
3641 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
3642 one screen-full, which is defined as the height of the window minus
3643 next_screen_context_lines. If WHOLE is zero, scroll up N lines
3644 instead. Negative values of N mean scroll down. NOERROR non-zero
3645 means don't signal an error if we try to move over BEGV or ZV,
3646 respectively. */
3648 static void
3649 window_scroll (window, n, whole, noerror)
3650 Lisp_Object window;
3651 int n;
3652 int whole;
3653 int noerror;
3655 /* If we must, use the pixel-based version which is much slower than
3656 the line-based one but can handle varying line heights. */
3657 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
3658 window_scroll_pixel_based (window, n, whole, noerror);
3659 else
3660 window_scroll_line_based (window, n, whole, noerror);
3664 /* Implementation of window_scroll that works based on pixel line
3665 heights. See the comment of window_scroll for parameter
3666 descriptions. */
3668 static void
3669 window_scroll_pixel_based (window, n, whole, noerror)
3670 Lisp_Object window;
3671 int n;
3672 int whole;
3673 int noerror;
3675 struct it it;
3676 struct window *w = XWINDOW (window);
3677 struct text_pos start;
3678 Lisp_Object tem;
3679 int this_scroll_margin;
3680 int preserve_y;
3682 SET_TEXT_POS_FROM_MARKER (start, w->start);
3684 /* If PT is not visible in WINDOW, move back one half of
3685 the screen. */
3686 XSETFASTINT (tem, PT);
3687 tem = Fpos_visible_in_window_p (tem, window);
3688 if (NILP (tem))
3690 /* Move backward half the height of the window. Performance note:
3691 vmotion used here is about 10% faster, but would give wrong
3692 results for variable height lines. */
3693 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
3694 it.current_y = it.last_visible_y;
3695 move_it_vertically (&it, -it.last_visible_y / 2);
3697 /* The function move_iterator_vertically may move over more than
3698 the specified y-distance. If it->w is small, e.g. a
3699 mini-buffer window, we may end up in front of the window's
3700 display area. This is the case when Start displaying at the
3701 start of the line containing PT in this case. */
3702 if (it.current_y <= 0)
3704 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
3705 move_it_vertically (&it, 0);
3706 it.current_y = 0;
3709 start = it.current.pos;
3712 /* If scroll_preserve_screen_position is non-zero, we try to set
3713 point in the same window line as it is now, so get that line. */
3714 if (!NILP (Vscroll_preserve_screen_position))
3716 start_display (&it, w, start);
3717 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
3718 preserve_y = it.current_y;
3720 else
3721 preserve_y = -1;
3723 /* Move iterator it from start the specified distance forward or
3724 backward. The result is the new window start. */
3725 start_display (&it, w, start);
3726 if (whole)
3728 int screen_full = (it.last_visible_y
3729 - next_screen_context_lines * CANON_Y_UNIT (it.f));
3730 int direction = n < 0 ? -1 : 1;
3731 move_it_vertically (&it, direction * screen_full);
3733 else
3734 move_it_by_lines (&it, n, 1);
3736 /* End if we end up at ZV or BEGV. */
3737 if ((n > 0 && IT_CHARPOS (it) == ZV)
3738 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
3740 if (noerror)
3741 return;
3742 else if (IT_CHARPOS (it) == ZV)
3743 Fsignal (Qend_of_buffer, Qnil);
3744 else
3745 Fsignal (Qbeginning_of_buffer, Qnil);
3748 /* Set the window start, and set up the window for redisplay. */
3749 set_marker_restricted (w->start, make_number (IT_CHARPOS (it)), w->buffer);
3750 w->start_at_line_beg = Fbolp ();
3751 w->update_mode_line = Qt;
3752 XSETFASTINT (w->last_modified, 0);
3753 XSETFASTINT (w->last_overlay_modified, 0);
3754 /* Set force_start so that redisplay_window will run the
3755 window-scroll-functions. */
3756 w->force_start = Qt;
3758 it.current_y = it.vpos = 0;
3760 /* Preserve the screen position if we must. */
3761 if (preserve_y >= 0)
3763 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
3764 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3766 else
3768 /* Move PT out of scroll margins. */
3769 this_scroll_margin = max (0, scroll_margin);
3770 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
3771 this_scroll_margin *= CANON_Y_UNIT (it.f);
3773 if (n > 0)
3775 /* We moved the window start towards ZV, so PT may be now
3776 in the scroll margin at the top. */
3777 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
3778 while (it.current_y < this_scroll_margin)
3779 move_it_by_lines (&it, 1, 1);
3780 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3782 else if (n < 0)
3784 /* We moved the window start towards BEGV, so PT may be now
3785 in the scroll margin at the bottom. */
3786 move_it_to (&it, PT, -1,
3787 it.last_visible_y - this_scroll_margin - 1, -1,
3788 MOVE_TO_POS | MOVE_TO_Y);
3790 /* Don't put point on a partially visible line at the end. */
3791 if (it.current_y + it.max_ascent + it.max_descent
3792 > it.last_visible_y)
3793 move_it_by_lines (&it, -1, 0);
3795 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3801 /* Implementation of window_scroll that works based on screen lines.
3802 See the comment of window_scroll for parameter descriptions. */
3804 static void
3805 window_scroll_line_based (window, n, whole, noerror)
3806 Lisp_Object window;
3807 int n;
3808 int whole;
3809 int noerror;
3811 register struct window *w = XWINDOW (window);
3812 register int opoint = PT, opoint_byte = PT_BYTE;
3813 register int pos, pos_byte;
3814 register int ht = window_internal_height (w);
3815 register Lisp_Object tem;
3816 int lose;
3817 Lisp_Object bolp;
3818 int startpos;
3819 struct position posit;
3820 int original_vpos;
3822 startpos = marker_position (w->start);
3824 posit = *compute_motion (startpos, 0, 0, 0,
3825 PT, ht, 0,
3826 window_internal_width (w), XINT (w->hscroll),
3827 0, w);
3828 original_vpos = posit.vpos;
3830 XSETFASTINT (tem, PT);
3831 tem = Fpos_visible_in_window_p (tem, window);
3833 if (NILP (tem))
3835 Fvertical_motion (make_number (- (ht / 2)), window);
3836 startpos = PT;
3839 SET_PT (startpos);
3840 lose = n < 0 && PT == BEGV;
3841 Fvertical_motion (make_number (n), window);
3842 pos = PT;
3843 pos_byte = PT_BYTE;
3844 bolp = Fbolp ();
3845 SET_PT_BOTH (opoint, opoint_byte);
3847 if (lose)
3849 if (noerror)
3850 return;
3851 else
3852 Fsignal (Qbeginning_of_buffer, Qnil);
3855 if (pos < ZV)
3857 int this_scroll_margin = scroll_margin;
3859 /* Don't use a scroll margin that is negative or too large. */
3860 if (this_scroll_margin < 0)
3861 this_scroll_margin = 0;
3863 if (XINT (w->height) < 4 * scroll_margin)
3864 this_scroll_margin = XINT (w->height) / 4;
3866 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
3867 w->start_at_line_beg = bolp;
3868 w->update_mode_line = Qt;
3869 XSETFASTINT (w->last_modified, 0);
3870 XSETFASTINT (w->last_overlay_modified, 0);
3871 /* Set force_start so that redisplay_window will run
3872 the window-scroll-functions. */
3873 w->force_start = Qt;
3875 if (whole && !NILP (Vscroll_preserve_screen_position))
3877 SET_PT_BOTH (pos, pos_byte);
3878 Fvertical_motion (make_number (original_vpos), window);
3880 /* If we scrolled forward, put point enough lines down
3881 that it is outside the scroll margin. */
3882 else if (n > 0)
3884 int top_margin;
3886 if (this_scroll_margin > 0)
3888 SET_PT_BOTH (pos, pos_byte);
3889 Fvertical_motion (make_number (this_scroll_margin), window);
3890 top_margin = PT;
3892 else
3893 top_margin = pos;
3895 if (top_margin <= opoint)
3896 SET_PT_BOTH (opoint, opoint_byte);
3897 else if (!NILP (Vscroll_preserve_screen_position))
3899 SET_PT_BOTH (pos, pos_byte);
3900 Fvertical_motion (make_number (original_vpos), window);
3902 else
3903 SET_PT (top_margin);
3905 else if (n < 0)
3907 int bottom_margin;
3909 /* If we scrolled backward, put point near the end of the window
3910 but not within the scroll margin. */
3911 SET_PT_BOTH (pos, pos_byte);
3912 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
3913 if (XFASTINT (tem) == ht - this_scroll_margin)
3914 bottom_margin = PT;
3915 else
3916 bottom_margin = PT + 1;
3918 if (bottom_margin > opoint)
3919 SET_PT_BOTH (opoint, opoint_byte);
3920 else
3922 if (!NILP (Vscroll_preserve_screen_position))
3924 SET_PT_BOTH (pos, pos_byte);
3925 Fvertical_motion (make_number (original_vpos), window);
3927 else
3928 Fvertical_motion (make_number (-1), window);
3932 else
3934 if (noerror)
3935 return;
3936 else
3937 Fsignal (Qend_of_buffer, Qnil);
3942 /* Scroll selected_window up or down. If N is nil, scroll a
3943 screen-full which is defined as the height of the window minus
3944 next_screen_context_lines. If N is the symbol `-', scroll.
3945 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
3946 up. This is the guts of Fscroll_up and Fscroll_down. */
3948 static void
3949 scroll_command (n, direction)
3950 Lisp_Object n;
3951 int direction;
3953 register int defalt;
3954 int count = specpdl_ptr - specpdl;
3956 xassert (abs (direction) == 1);
3958 /* If selected window's buffer isn't current, make it current for
3959 the moment. But don't screw up if window_scroll gets an error. */
3960 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
3962 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3963 Fset_buffer (XWINDOW (selected_window)->buffer);
3965 /* Make redisplay consider other windows than just selected_window. */
3966 ++windows_or_buffers_changed;
3969 defalt = (window_internal_height (XWINDOW (selected_window))
3970 - next_screen_context_lines);
3971 defalt = direction * (defalt < 1 ? 1 : defalt);
3973 if (NILP (n))
3974 window_scroll (selected_window, defalt, 1, 0);
3975 else if (EQ (n, Qminus))
3976 window_scroll (selected_window, - defalt, 1, 0);
3977 else
3979 n = Fprefix_numeric_value (n);
3980 window_scroll (selected_window, XINT (n) * direction, 0, 0);
3983 unbind_to (count, Qnil);
3986 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
3987 "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
3988 A near full screen is `next-screen-context-lines' less than a full screen.\n\
3989 Negative ARG means scroll downward.\n\
3990 If ARG is the atom `-', scroll downward by nearly full screen.\n\
3991 When calling from a program, supply as argument a number, nil, or `-'.")
3992 (arg)
3993 Lisp_Object arg;
3995 scroll_command (arg, 1);
3996 return Qnil;
3999 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
4000 "Scroll text of current window down ARG lines; or near full screen if no ARG.\n\
4001 A near full screen is `next-screen-context-lines' less than a full screen.\n\
4002 Negative ARG means scroll upward.\n\
4003 If ARG is the atom `-', scroll upward by nearly full screen.\n\
4004 When calling from a program, supply as argument a number, nil, or `-'.")
4005 (arg)
4006 Lisp_Object arg;
4008 scroll_command (arg, -1);
4009 return Qnil;
4012 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4013 "Return the other window for \"other window scroll\" commands.\n\
4014 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
4015 specifies the window.\n\
4016 If `other-window-scroll-buffer' is non-nil, a window\n\
4017 showing that buffer is used.")
4020 Lisp_Object window;
4022 if (MINI_WINDOW_P (XWINDOW (selected_window))
4023 && !NILP (Vminibuf_scroll_window))
4024 window = Vminibuf_scroll_window;
4025 /* If buffer is specified, scroll that buffer. */
4026 else if (!NILP (Vother_window_scroll_buffer))
4028 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4029 if (NILP (window))
4030 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4032 else
4034 /* Nothing specified; look for a neighboring window on the same
4035 frame. */
4036 window = Fnext_window (selected_window, Qnil, Qnil);
4038 if (EQ (window, selected_window))
4039 /* That didn't get us anywhere; look for a window on another
4040 visible frame. */
4042 window = Fnext_window (window, Qnil, Qt);
4043 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4044 && ! EQ (window, selected_window));
4047 CHECK_LIVE_WINDOW (window, 0);
4049 if (EQ (window, selected_window))
4050 error ("There is no other window");
4052 return window;
4055 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4056 "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
4057 A near full screen is `next-screen-context-lines' less than a full screen.\n\
4058 The next window is the one below the current one; or the one at the top\n\
4059 if the current one is at the bottom. Negative ARG means scroll downward.\n\
4060 If ARG is the atom `-', scroll downward by nearly full screen.\n\
4061 When calling from a program, supply as argument a number, nil, or `-'.\n\
4063 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
4064 specifies the window to scroll.\n\
4065 If `other-window-scroll-buffer' is non-nil, scroll the window\n\
4066 showing that buffer, popping the buffer up if necessary.")
4067 (arg)
4068 register Lisp_Object arg;
4070 register Lisp_Object window;
4071 register int defalt;
4072 register struct window *w;
4073 register int count = specpdl_ptr - specpdl;
4075 window = Fother_window_for_scrolling ();
4077 w = XWINDOW (window);
4078 defalt = window_internal_height (w) - next_screen_context_lines;
4079 if (defalt < 1) defalt = 1;
4081 /* Don't screw up if window_scroll gets an error. */
4082 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4083 ++windows_or_buffers_changed;
4085 Fset_buffer (w->buffer);
4086 SET_PT (marker_position (w->pointm));
4088 if (NILP (arg))
4089 window_scroll (window, defalt, 1, 1);
4090 else if (EQ (arg, Qminus))
4091 window_scroll (window, -defalt, 1, 1);
4092 else
4094 if (CONSP (arg))
4095 arg = Fcar (arg);
4096 CHECK_NUMBER (arg, 0);
4097 window_scroll (window, XINT (arg), 0, 1);
4100 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4101 unbind_to (count, Qnil);
4103 return Qnil;
4106 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
4107 "Scroll selected window display ARG columns left.\n\
4108 Default for ARG is window width minus 2.")
4109 (arg)
4110 register Lisp_Object arg;
4113 if (NILP (arg))
4114 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
4115 else
4116 arg = Fprefix_numeric_value (arg);
4118 return
4119 Fset_window_hscroll (selected_window,
4120 make_number (XINT (XWINDOW (selected_window)->hscroll)
4121 + XINT (arg)));
4124 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
4125 "Scroll selected window display ARG columns right.\n\
4126 Default for ARG is window width minus 2.")
4127 (arg)
4128 register Lisp_Object arg;
4130 if (NILP (arg))
4131 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
4132 else
4133 arg = Fprefix_numeric_value (arg);
4135 return
4136 Fset_window_hscroll (selected_window,
4137 make_number (XINT (XWINDOW (selected_window)->hscroll)
4138 - XINT (arg)));
4141 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
4142 "Center point in window and redisplay frame. With ARG, put point on line ARG.\n\
4143 The desired position of point is always relative to the current window.\n\
4144 Just C-u as prefix means put point in the center of the window.\n\
4145 If ARG is omitted or nil, erases the entire frame and then\n\
4146 redraws with point in the center of the current window.")
4147 (arg)
4148 register Lisp_Object arg;
4150 register struct window *w = XWINDOW (selected_window);
4151 register int ht = window_internal_height (w);
4152 struct position pos;
4153 struct buffer *buf = XBUFFER (w->buffer);
4154 struct buffer *obuf = current_buffer;
4156 if (NILP (arg))
4158 extern int frame_garbaged;
4160 Fredraw_frame (w->frame);
4161 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
4162 XSETFASTINT (arg, ht / 2);
4164 else if (CONSP (arg)) /* Just C-u. */
4166 XSETFASTINT (arg, ht / 2);
4168 else
4170 arg = Fprefix_numeric_value (arg);
4171 CHECK_NUMBER (arg, 0);
4174 if (XINT (arg) < 0)
4175 XSETINT (arg, XINT (arg) + ht);
4177 set_buffer_internal (buf);
4178 pos = *vmotion (PT, - XINT (arg), w);
4180 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
4181 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
4182 || FETCH_BYTE (pos.bytepos - 1) == '\n')
4183 ? Qt : Qnil);
4184 w->force_start = Qt;
4185 set_buffer_internal (obuf);
4187 return Qnil;
4190 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
4191 1, 1, "P",
4192 "Position point relative to window.\n\
4193 With no argument, position point at center of window.\n\
4194 An argument specifies vertical position within the window;\n\
4195 zero means top of window, negative means relative to bottom of window.")
4196 (arg)
4197 register Lisp_Object arg;
4199 register struct window *w = XWINDOW (selected_window);
4200 register int height = window_internal_height (w);
4201 register int start;
4202 Lisp_Object window;
4204 if (NILP (arg))
4205 XSETFASTINT (arg, height / 2);
4206 else
4208 arg = Fprefix_numeric_value (arg);
4209 if (XINT (arg) < 0)
4210 XSETINT (arg, XINT (arg) + height);
4213 start = marker_position (w->start);
4214 XSETWINDOW (window, w);
4215 if (start < BEGV || start > ZV)
4217 Fvertical_motion (make_number (- (height / 2)), window);
4218 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
4219 w->start_at_line_beg = Fbolp ();
4220 w->force_start = Qt;
4222 else
4223 Fgoto_char (w->start);
4225 return Fvertical_motion (arg, window);
4230 /***********************************************************************
4231 Window Configuration
4232 ***********************************************************************/
4234 struct save_window_data
4236 EMACS_INT size_from_Lisp_Vector_struct;
4237 struct Lisp_Vector *next_from_Lisp_Vector_struct;
4238 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
4239 Lisp_Object frame_tool_bar_lines;
4240 Lisp_Object selected_frame;
4241 Lisp_Object current_window;
4242 Lisp_Object current_buffer;
4243 Lisp_Object minibuf_scroll_window;
4244 Lisp_Object root_window;
4245 Lisp_Object focus_frame;
4246 /* Record the values of window-min-width and window-min-height
4247 so that window sizes remain consistent with them. */
4248 Lisp_Object min_width, min_height;
4249 /* A vector, each of whose elements is a struct saved_window
4250 for one window. */
4251 Lisp_Object saved_windows;
4254 /* This is saved as a Lisp_Vector */
4255 struct saved_window
4257 /* these first two must agree with struct Lisp_Vector in lisp.h */
4258 EMACS_INT size_from_Lisp_Vector_struct;
4259 struct Lisp_Vector *next_from_Lisp_Vector_struct;
4261 Lisp_Object window;
4262 Lisp_Object buffer, start, pointm, mark;
4263 Lisp_Object left, top, width, height, hscroll;
4264 Lisp_Object parent, prev;
4265 Lisp_Object start_at_line_beg;
4266 Lisp_Object display_table;
4268 #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
4270 #define SAVED_WINDOW_N(swv,n) \
4271 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
4273 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
4274 "Return t if OBJECT is a window-configuration object.")
4275 (object)
4276 Lisp_Object object;
4278 if (WINDOW_CONFIGURATIONP (object))
4279 return Qt;
4280 return Qnil;
4283 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
4284 "Return the frame that CONFIG, a window-configuration object, is about.")
4285 (config)
4286 Lisp_Object config;
4288 register struct save_window_data *data;
4289 struct Lisp_Vector *saved_windows;
4291 if (! WINDOW_CONFIGURATIONP (config))
4292 wrong_type_argument (Qwindow_configuration_p, config);
4294 data = (struct save_window_data *) XVECTOR (config);
4295 saved_windows = XVECTOR (data->saved_windows);
4296 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
4299 DEFUN ("set-window-configuration", Fset_window_configuration,
4300 Sset_window_configuration, 1, 1, 0,
4301 "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
4302 CONFIGURATION must be a value previously returned\n\
4303 by `current-window-configuration' (which see).\n\
4304 If CONFIGURATION was made from a frame that is now deleted,\n\
4305 only frame-independent values can be restored. In this case,\n\
4306 the return value is nil. Otherwise the value is t.")
4307 (configuration)
4308 Lisp_Object configuration;
4310 register struct save_window_data *data;
4311 struct Lisp_Vector *saved_windows;
4312 Lisp_Object new_current_buffer;
4313 Lisp_Object frame;
4314 FRAME_PTR f;
4315 int old_point = -1;
4317 while (!WINDOW_CONFIGURATIONP (configuration))
4318 wrong_type_argument (Qwindow_configuration_p, configuration);
4320 data = (struct save_window_data *) XVECTOR (configuration);
4321 saved_windows = XVECTOR (data->saved_windows);
4323 new_current_buffer = data->current_buffer;
4324 if (NILP (XBUFFER (new_current_buffer)->name))
4325 new_current_buffer = Qnil;
4326 else
4328 if (XBUFFER (new_current_buffer) == current_buffer)
4329 old_point = PT;
4333 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
4334 f = XFRAME (frame);
4336 /* If f is a dead frame, don't bother rebuilding its window tree.
4337 However, there is other stuff we should still try to do below. */
4338 if (FRAME_LIVE_P (f))
4340 register struct window *w;
4341 register struct saved_window *p;
4342 struct window *root_window;
4343 struct window **leaf_windows;
4344 int n_leaf_windows;
4345 int k, i;
4347 /* If the frame has been resized since this window configuration was
4348 made, we change the frame to the size specified in the
4349 configuration, restore the configuration, and then resize it
4350 back. We keep track of the prevailing height in these variables. */
4351 int previous_frame_height = FRAME_HEIGHT (f);
4352 int previous_frame_width = FRAME_WIDTH (f);
4353 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
4354 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
4356 /* The mouse highlighting code could get screwed up
4357 if it runs during this. */
4358 BLOCK_INPUT;
4360 if (XFASTINT (data->frame_height) != previous_frame_height
4361 || XFASTINT (data->frame_width) != previous_frame_width)
4362 change_frame_size (f, XFASTINT (data->frame_height),
4363 XFASTINT (data->frame_width), 0, 0, 0);
4364 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
4365 if (XFASTINT (data->frame_menu_bar_lines)
4366 != previous_frame_menu_bar_lines)
4367 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
4368 #ifdef HAVE_WINDOW_SYSTEM
4369 if (XFASTINT (data->frame_tool_bar_lines)
4370 != previous_frame_tool_bar_lines)
4371 x_set_tool_bar_lines (f, data->frame_tool_bar_lines, make_number (0));
4372 #endif
4373 #endif
4375 if (! NILP (XWINDOW (selected_window)->buffer))
4377 w = XWINDOW (selected_window);
4378 set_marker_both (w->pointm,
4379 w->buffer,
4380 BUF_PT (XBUFFER (w->buffer)),
4381 BUF_PT_BYTE (XBUFFER (w->buffer)));
4384 windows_or_buffers_changed++;
4385 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4387 /* Problem: Freeing all matrices and later allocating them again
4388 is a serious redisplay flickering problem. What we would
4389 really like to do is to free only those matrices not reused
4390 below. */
4391 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
4392 leaf_windows
4393 = (struct window **) alloca (count_windows (root_window)
4394 * sizeof (struct window *));
4395 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
4397 /* Temporarily avoid any problems with windows that are smaller
4398 than they are supposed to be. */
4399 window_min_height = 1;
4400 window_min_width = 1;
4402 /* Kludge Alert!
4403 Mark all windows now on frame as "deleted".
4404 Restoring the new configuration "undeletes" any that are in it.
4406 Save their current buffers in their height fields, since we may
4407 need it later, if a buffer saved in the configuration is now
4408 dead. */
4409 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
4411 for (k = 0; k < saved_windows->size; k++)
4413 p = SAVED_WINDOW_N (saved_windows, k);
4414 w = XWINDOW (p->window);
4415 w->next = Qnil;
4417 if (!NILP (p->parent))
4418 w->parent = SAVED_WINDOW_N (saved_windows,
4419 XFASTINT (p->parent))->window;
4420 else
4421 w->parent = Qnil;
4423 if (!NILP (p->prev))
4425 w->prev = SAVED_WINDOW_N (saved_windows,
4426 XFASTINT (p->prev))->window;
4427 XWINDOW (w->prev)->next = p->window;
4429 else
4431 w->prev = Qnil;
4432 if (!NILP (w->parent))
4434 if (EQ (p->width, XWINDOW (w->parent)->width))
4436 XWINDOW (w->parent)->vchild = p->window;
4437 XWINDOW (w->parent)->hchild = Qnil;
4439 else
4441 XWINDOW (w->parent)->hchild = p->window;
4442 XWINDOW (w->parent)->vchild = Qnil;
4447 /* If we squirreled away the buffer in the window's height,
4448 restore it now. */
4449 if (BUFFERP (w->height))
4450 w->buffer = w->height;
4451 w->left = p->left;
4452 w->top = p->top;
4453 w->width = p->width;
4454 w->height = p->height;
4455 w->hscroll = p->hscroll;
4456 w->display_table = p->display_table;
4457 XSETFASTINT (w->last_modified, 0);
4458 XSETFASTINT (w->last_overlay_modified, 0);
4460 /* Reinstall the saved buffer and pointers into it. */
4461 if (NILP (p->buffer))
4462 w->buffer = p->buffer;
4463 else
4465 if (!NILP (XBUFFER (p->buffer)->name))
4466 /* If saved buffer is alive, install it. */
4468 w->buffer = p->buffer;
4469 w->start_at_line_beg = p->start_at_line_beg;
4470 set_marker_restricted (w->start, p->start, w->buffer);
4471 set_marker_restricted (w->pointm, p->pointm, w->buffer);
4472 Fset_marker (XBUFFER (w->buffer)->mark,
4473 p->mark, w->buffer);
4475 /* As documented in Fcurrent_window_configuration, don't
4476 save the location of point in the buffer which was current
4477 when the window configuration was recorded. */
4478 if (!EQ (p->buffer, new_current_buffer)
4479 && XBUFFER (p->buffer) == current_buffer)
4480 Fgoto_char (w->pointm);
4482 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
4483 /* Else unless window has a live buffer, get one. */
4485 w->buffer = Fcdr (Fcar (Vbuffer_alist));
4486 /* This will set the markers to beginning of visible
4487 range. */
4488 set_marker_restricted (w->start, make_number (0), w->buffer);
4489 set_marker_restricted (w->pointm, make_number (0),w->buffer);
4490 w->start_at_line_beg = Qt;
4492 else
4493 /* Keeping window's old buffer; make sure the markers
4494 are real. */
4496 /* Set window markers at start of visible range. */
4497 if (XMARKER (w->start)->buffer == 0)
4498 set_marker_restricted (w->start, make_number (0),
4499 w->buffer);
4500 if (XMARKER (w->pointm)->buffer == 0)
4501 set_marker_restricted_both (w->pointm, w->buffer,
4502 BUF_PT (XBUFFER (w->buffer)),
4503 BUF_PT_BYTE (XBUFFER (w->buffer)));
4504 w->start_at_line_beg = Qt;
4509 FRAME_ROOT_WINDOW (f) = data->root_window;
4510 Fselect_window (data->current_window);
4511 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
4512 = selected_window;
4514 if (NILP (data->focus_frame)
4515 || (FRAMEP (data->focus_frame)
4516 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
4517 Fredirect_frame_focus (frame, data->focus_frame);
4519 #if 0 /* I don't understand why this is needed, and it causes problems
4520 when the frame's old selected window has been deleted. */
4521 if (f != selected_frame && FRAME_WINDOW_P (f))
4522 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
4523 Qnil, 0);
4524 #endif
4526 /* Set the screen height to the value it had before this function. */
4527 if (previous_frame_height != FRAME_HEIGHT (f)
4528 || previous_frame_width != FRAME_WIDTH (f))
4529 change_frame_size (f, previous_frame_height, previous_frame_width,
4530 0, 0, 0);
4531 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
4532 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
4533 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
4534 make_number (0));
4535 #ifdef HAVE_WINDOW_SYSTEM
4536 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
4537 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
4538 make_number (0));
4539 #endif
4540 #endif
4542 /* Now, free glyph matrices in windows that were not reused. */
4543 for (i = 0; i < n_leaf_windows; ++i)
4544 if (NILP (leaf_windows[i]->buffer))
4546 /* Assert it's not reused as a combination. */
4547 xassert (NILP (leaf_windows[i]->hchild)
4548 && NILP (leaf_windows[i]->vchild));
4549 free_window_matrices (leaf_windows[i]);
4550 SET_FRAME_GARBAGED (f);
4553 adjust_glyphs (f);
4555 UNBLOCK_INPUT;
4557 /* Fselect_window will have made f the selected frame, so we
4558 reselect the proper frame here. Fhandle_switch_frame will change the
4559 selected window too, but that doesn't make the call to
4560 Fselect_window above totally superfluous; it still sets f's
4561 selected window. */
4562 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
4563 do_switch_frame (data->selected_frame, Qnil, 0);
4565 if (! NILP (Vwindow_configuration_change_hook)
4566 && ! NILP (Vrun_hooks))
4567 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
4570 if (!NILP (new_current_buffer))
4572 Fset_buffer (new_current_buffer);
4574 /* If the buffer that is current now is the same
4575 that was current before setting the window configuration,
4576 don't alter its PT. */
4577 if (old_point >= 0)
4578 SET_PT (old_point);
4581 /* Restore the minimum heights recorded in the configuration. */
4582 window_min_height = XINT (data->min_height);
4583 window_min_width = XINT (data->min_width);
4585 Vminibuf_scroll_window = data->minibuf_scroll_window;
4587 return (FRAME_LIVE_P (f) ? Qt : Qnil);
4590 /* Mark all windows now on frame as deleted
4591 by setting their buffers to nil. */
4593 void
4594 delete_all_subwindows (w)
4595 register struct window *w;
4597 if (!NILP (w->next))
4598 delete_all_subwindows (XWINDOW (w->next));
4599 if (!NILP (w->vchild))
4600 delete_all_subwindows (XWINDOW (w->vchild));
4601 if (!NILP (w->hchild))
4602 delete_all_subwindows (XWINDOW (w->hchild));
4604 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
4606 if (!NILP (w->buffer))
4607 unshow_buffer (w);
4609 /* We set all three of these fields to nil, to make sure that we can
4610 distinguish this dead window from any live window. Live leaf
4611 windows will have buffer set, and combination windows will have
4612 vchild or hchild set. */
4613 w->buffer = Qnil;
4614 w->vchild = Qnil;
4615 w->hchild = Qnil;
4618 static int
4619 count_windows (window)
4620 register struct window *window;
4622 register int count = 1;
4623 if (!NILP (window->next))
4624 count += count_windows (XWINDOW (window->next));
4625 if (!NILP (window->vchild))
4626 count += count_windows (XWINDOW (window->vchild));
4627 if (!NILP (window->hchild))
4628 count += count_windows (XWINDOW (window->hchild));
4629 return count;
4633 /* Fill vector FLAT with leaf windows under W, starting at index I.
4634 Value is last index + 1. */
4636 static int
4637 get_leaf_windows (w, flat, i)
4638 struct window *w;
4639 struct window **flat;
4640 int i;
4642 while (w)
4644 if (!NILP (w->hchild))
4645 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
4646 else if (!NILP (w->vchild))
4647 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
4648 else
4649 flat[i++] = w;
4651 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4654 return i;
4658 /* Return a pointer to the glyph W's physical cursor is on. Value is
4659 null if W's current matrix is invalid, so that no meaningfull glyph
4660 can be returned. */
4662 struct glyph *
4663 get_phys_cursor_glyph (w)
4664 struct window *w;
4666 struct glyph_row *row;
4667 struct glyph *glyph;
4669 if (w->phys_cursor.vpos >= 0
4670 && w->phys_cursor.vpos < w->current_matrix->nrows
4671 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
4672 row->enabled_p)
4673 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
4674 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
4675 else
4676 glyph = NULL;
4678 return glyph;
4682 static int
4683 save_window_save (window, vector, i)
4684 Lisp_Object window;
4685 struct Lisp_Vector *vector;
4686 int i;
4688 register struct saved_window *p;
4689 register struct window *w;
4690 register Lisp_Object tem;
4692 for (;!NILP (window); window = w->next)
4694 p = SAVED_WINDOW_N (vector, i);
4695 w = XWINDOW (window);
4697 XSETFASTINT (w->temslot, i++);
4698 p->window = window;
4699 p->buffer = w->buffer;
4700 p->left = w->left;
4701 p->top = w->top;
4702 p->width = w->width;
4703 p->height = w->height;
4704 p->hscroll = w->hscroll;
4705 p->display_table = w->display_table;
4706 if (!NILP (w->buffer))
4708 /* Save w's value of point in the window configuration.
4709 If w is the selected window, then get the value of point
4710 from the buffer; pointm is garbage in the selected window. */
4711 if (EQ (window, selected_window))
4713 p->pointm = Fmake_marker ();
4714 set_marker_both (p->pointm, w->buffer,
4715 BUF_PT (XBUFFER (w->buffer)),
4716 BUF_PT_BYTE (XBUFFER (w->buffer)));
4718 else
4719 p->pointm = Fcopy_marker (w->pointm, Qnil);
4721 p->start = Fcopy_marker (w->start, Qnil);
4722 p->start_at_line_beg = w->start_at_line_beg;
4724 tem = XBUFFER (w->buffer)->mark;
4725 p->mark = Fcopy_marker (tem, Qnil);
4727 else
4729 p->pointm = Qnil;
4730 p->start = Qnil;
4731 p->mark = Qnil;
4732 p->start_at_line_beg = Qnil;
4735 if (NILP (w->parent))
4736 p->parent = Qnil;
4737 else
4738 p->parent = XWINDOW (w->parent)->temslot;
4740 if (NILP (w->prev))
4741 p->prev = Qnil;
4742 else
4743 p->prev = XWINDOW (w->prev)->temslot;
4745 if (!NILP (w->vchild))
4746 i = save_window_save (w->vchild, vector, i);
4747 if (!NILP (w->hchild))
4748 i = save_window_save (w->hchild, vector, i);
4751 return i;
4754 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
4755 Scurrent_window_configuration, 0, 1, 0,
4756 "Return an object representing the current window configuration of FRAME.\n\
4757 If FRAME is nil or omitted, use the selected frame.\n\
4758 This describes the number of windows, their sizes and current buffers,\n\
4759 and for each displayed buffer, where display starts, and the positions of\n\
4760 point and mark. An exception is made for point in the current buffer:\n\
4761 its value is -not- saved.\n\
4762 This also records the currently selected frame, and FRAME's focus\n\
4763 redirection (see `redirect-frame-focus').")
4764 (frame)
4765 Lisp_Object frame;
4767 register Lisp_Object tem;
4768 register int n_windows;
4769 register struct save_window_data *data;
4770 register struct Lisp_Vector *vec;
4771 register int i;
4772 FRAME_PTR f;
4774 if (NILP (frame))
4775 frame = selected_frame;
4776 CHECK_LIVE_FRAME (frame, 0);
4777 f = XFRAME (frame);
4779 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
4780 vec = allocate_vectorlike (VECSIZE (struct save_window_data));
4781 for (i = 0; i < VECSIZE (struct save_window_data); i++)
4782 vec->contents[i] = Qnil;
4783 vec->size = VECSIZE (struct save_window_data);
4784 data = (struct save_window_data *)vec;
4786 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
4787 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
4788 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
4789 XSETFASTINT (data->frame_tool_bar_lines, FRAME_TOOL_BAR_LINES (f));
4790 data->selected_frame = selected_frame;
4791 data->current_window = FRAME_SELECTED_WINDOW (f);
4792 XSETBUFFER (data->current_buffer, current_buffer);
4793 data->minibuf_scroll_window = Vminibuf_scroll_window;
4794 data->root_window = FRAME_ROOT_WINDOW (f);
4795 data->focus_frame = FRAME_FOCUS_FRAME (f);
4796 XSETINT (data->min_height, window_min_height);
4797 XSETINT (data->min_width, window_min_width);
4798 tem = Fmake_vector (make_number (n_windows), Qnil);
4799 data->saved_windows = tem;
4800 for (i = 0; i < n_windows; i++)
4801 XVECTOR (tem)->contents[i]
4802 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
4803 save_window_save (FRAME_ROOT_WINDOW (f),
4804 XVECTOR (tem), 0);
4805 XSETWINDOW_CONFIGURATION (tem, data);
4806 return (tem);
4809 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
4810 0, UNEVALLED, 0,
4811 "Execute body, preserving window sizes and contents.\n\
4812 Restore which buffer appears in which window, where display starts,\n\
4813 and the value of point and mark for each window.\n\
4814 Also restore which buffer is current.\n\
4815 But do not preserve point in the current buffer.\n\
4816 Does not restore the value of point in current buffer.")
4817 (args)
4818 Lisp_Object args;
4820 register Lisp_Object val;
4821 register int count = specpdl_ptr - specpdl;
4823 record_unwind_protect (Fset_window_configuration,
4824 Fcurrent_window_configuration (Qnil));
4825 val = Fprogn (args);
4826 return unbind_to (count, val);
4830 /***********************************************************************
4831 Marginal Areas
4832 ***********************************************************************/
4834 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
4835 2, 3, "",
4836 "Set width of marginal areas of window WINDOW.\n\
4837 If window is nil or omitted, set margins of the currently selected window.\n\
4838 First parameter LEFT-WIDTH specifies the number of character\n\
4839 cells to reserve for the left marginal area. Second parameter\n\
4840 RIGHT-WIDTH does the same for the right marginal area.\n\
4841 A nil width parameter means no margin.")
4842 (window, left, right)
4843 Lisp_Object window, left, right;
4845 struct window *w = decode_window (window);
4847 if (!NILP (left))
4848 CHECK_NUMBER_OR_FLOAT (left, 1);
4849 if (!NILP (right))
4850 CHECK_NUMBER_OR_FLOAT (right, 2);
4852 /* Check widths < 0 and translate a zero width to nil.
4853 Margins that are too wide have to be checked elsewhere. */
4854 if ((INTEGERP (left) && XINT (left) < 0)
4855 || (FLOATP (left) && XFLOAT_DATA (left) <= 0))
4856 XSETFASTINT (left, 0);
4857 if (INTEGERP (left) && XFASTINT (left) == 0)
4858 left = Qnil;
4860 if ((INTEGERP (right) && XINT (right) < 0)
4861 || (FLOATP (right) && XFLOAT_DATA (right) <= 0))
4862 XSETFASTINT (right, 0);
4863 if (INTEGERP (right) && XFASTINT (right) == 0)
4864 right = Qnil;
4866 w->left_margin_width = left;
4867 w->right_margin_width = right;
4869 ++windows_or_buffers_changed;
4870 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
4871 return Qnil;
4875 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
4876 0, 1, 0,
4877 "Get width of marginal areas of window WINDOW.\n\
4878 If WINDOW is omitted or nil, use the currently selected window.\n\
4879 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).\n\
4880 If a marginal area does not exist, its width will be returned\n\
4881 as nil.")
4882 (window)
4883 Lisp_Object window;
4885 struct window *w = decode_window (window);
4886 return Fcons (w->left_margin_width, w->right_margin_width);
4891 /***********************************************************************
4892 Smooth scrolling
4893 ***********************************************************************/
4895 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 1, 0,
4896 "Return the amount by which WINDOW is scrolled vertically.\n\
4897 Use the selected window if WINDOW is nil or omitted.\n\
4898 Value is a multiple of the canonical character height of WINDOW.")
4899 (window)
4900 Lisp_Object window;
4902 Lisp_Object result;
4903 struct frame *f;
4904 struct window *w;
4906 if (NILP (window))
4907 window = selected_window;
4908 else
4909 CHECK_WINDOW (window, 0);
4910 w = XWINDOW (window);
4911 f = XFRAME (w->frame);
4913 if (FRAME_WINDOW_P (f))
4914 result = CANON_Y_FROM_PIXEL_Y (f, -w->vscroll);
4915 else
4916 result = make_number (0);
4917 return result;
4921 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
4922 2, 2, 0,
4923 "Set amount by which WINDOW should be scrolled vertically to VSCROLL.\n\
4924 WINDOW nil or omitted means use the selected window. VSCROLL is a\n\
4925 non-negative multiple of the canonical character height of WINDOW.")
4926 (window, vscroll)
4927 Lisp_Object window, vscroll;
4929 struct window *w;
4930 struct frame *f;
4932 if (NILP (window))
4933 window = selected_window;
4934 else
4935 CHECK_WINDOW (window, 0);
4936 CHECK_NUMBER_OR_FLOAT (vscroll, 1);
4938 w = XWINDOW (window);
4939 f = XFRAME (w->frame);
4941 if (FRAME_WINDOW_P (f))
4943 int old_dy = w->vscroll;
4945 w->vscroll = - CANON_Y_UNIT (f) * XFLOATINT (vscroll);
4946 w->vscroll = min (w->vscroll, 0);
4948 /* Adjust glyph matrix of the frame if the virtual display
4949 area becomes larger than before. */
4950 if (w->vscroll < 0 && w->vscroll < old_dy)
4951 adjust_glyphs (f);
4953 /* Prevent redisplay shortcuts. */
4954 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4957 return Fwindow_vscroll (window);
4961 /* Call FN for all leaf windows on frame F. FN is called with the
4962 first argument being a pointer to the leaf window, and with
4963 additional arguments A1..A4. */
4965 void
4966 foreach_window (f, fn, a1, a2, a3, a4)
4967 struct frame *f;
4968 void (* fn) ();
4969 int a1, a2, a3, a4;
4971 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, a1, a2, a3, a4);
4975 /* Helper function for foreach_window. Call FN for all leaf windows
4976 reachable from W. FN is called with the first argument being a
4977 pointer to the leaf window, and with additional arguments A1..A4. */
4979 static void
4980 foreach_window_1 (w, fn, a1, a2, a3, a4)
4981 struct window *w;
4982 void (* fn) ();
4983 int a1, a2, a3, a4;
4985 while (w)
4987 if (!NILP (w->hchild))
4988 foreach_window_1 (XWINDOW (w->hchild), fn, a1, a2, a3, a4);
4989 else if (!NILP (w->vchild))
4990 foreach_window_1 (XWINDOW (w->vchild), fn, a1, a2, a3, a4);
4991 else
4992 fn (w, a1, a2, a3, a4);
4994 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4999 /* Freeze or unfreeze the window start of W if unless it is a
5000 mini-window or the selected window. FREEZE_P non-zero means freeze
5001 the window start. */
5003 static void
5004 freeze_window_start (w, freeze_p)
5005 struct window *w;
5006 int freeze_p;
5008 if (w == XWINDOW (selected_window)
5009 || MINI_WINDOW_P (w)
5010 || (MINI_WINDOW_P (XWINDOW (selected_window))
5011 && w == XWINDOW (Vminibuf_scroll_window)))
5012 freeze_p = 0;
5014 w->frozen_window_start_p = freeze_p;
5018 /* Freeze or unfreeze the window starts of all leaf windows on frame
5019 F, except the selected window and a mini-window. FREEZE_P non-zero
5020 means freeze the window start. */
5022 void
5023 freeze_window_starts (f, freeze_p)
5024 struct frame *f;
5025 int freeze_p;
5027 foreach_window (f, freeze_window_start, freeze_p);
5031 /***********************************************************************
5032 Initialization
5033 ***********************************************************************/
5035 /* Return 1 if window configurations C1 and C2
5036 describe the same state of affairs. This is used by Fequal. */
5039 compare_window_configurations (c1, c2, ignore_positions)
5040 Lisp_Object c1, c2;
5041 int ignore_positions;
5043 register struct save_window_data *d1, *d2;
5044 struct Lisp_Vector *sw1, *sw2;
5045 int i;
5047 d1 = (struct save_window_data *) XVECTOR (c1);
5048 d2 = (struct save_window_data *) XVECTOR (c2);
5049 sw1 = XVECTOR (d1->saved_windows);
5050 sw2 = XVECTOR (d2->saved_windows);
5052 if (! EQ (d1->frame_width, d2->frame_width))
5053 return 0;
5054 if (! EQ (d1->frame_height, d2->frame_height))
5055 return 0;
5056 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
5057 return 0;
5058 if (! EQ (d1->selected_frame, d2->selected_frame))
5059 return 0;
5060 /* Don't compare the current_window field directly.
5061 Instead see w1_is_current and w2_is_current, below. */
5062 if (! EQ (d1->current_buffer, d2->current_buffer))
5063 return 0;
5064 if (! ignore_positions)
5065 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
5066 return 0;
5067 /* Don't compare the root_window field.
5068 We don't require the two configurations
5069 to use the same window object,
5070 and the two root windows must be equivalent
5071 if everything else compares equal. */
5072 if (! EQ (d1->focus_frame, d2->focus_frame))
5073 return 0;
5074 if (! EQ (d1->min_width, d2->min_width))
5075 return 0;
5076 if (! EQ (d1->min_height, d2->min_height))
5077 return 0;
5079 /* Verify that the two confis have the same number of windows. */
5080 if (sw1->size != sw2->size)
5081 return 0;
5083 for (i = 0; i < sw1->size; i++)
5085 struct saved_window *p1, *p2;
5086 int w1_is_current, w2_is_current;
5088 p1 = SAVED_WINDOW_N (sw1, i);
5089 p2 = SAVED_WINDOW_N (sw2, i);
5091 /* Verify that the current windows in the two
5092 configurations correspond to each other. */
5093 w1_is_current = EQ (d1->current_window, p1->window);
5094 w2_is_current = EQ (d2->current_window, p2->window);
5096 if (w1_is_current != w2_is_current)
5097 return 0;
5099 /* Verify that the corresponding windows do match. */
5100 if (! EQ (p1->buffer, p2->buffer))
5101 return 0;
5102 if (! EQ (p1->left, p2->left))
5103 return 0;
5104 if (! EQ (p1->top, p2->top))
5105 return 0;
5106 if (! EQ (p1->width, p2->width))
5107 return 0;
5108 if (! EQ (p1->height, p2->height))
5109 return 0;
5110 if (! EQ (p1->display_table, p2->display_table))
5111 return 0;
5112 if (! EQ (p1->parent, p2->parent))
5113 return 0;
5114 if (! EQ (p1->prev, p2->prev))
5115 return 0;
5116 if (! ignore_positions)
5118 if (! EQ (p1->hscroll, p2->hscroll))
5119 return 0;
5120 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
5121 return 0;
5122 if (NILP (Fequal (p1->start, p2->start)))
5123 return 0;
5124 if (NILP (Fequal (p1->pointm, p2->pointm)))
5125 return 0;
5126 if (NILP (Fequal (p1->mark, p2->mark)))
5127 return 0;
5131 return 1;
5134 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
5135 Scompare_window_configurations, 2, 2, 0,
5136 "Compare two window configurations as regards the structure of windows.\n\
5137 This function ignores details such as the values of point and mark\n\
5138 and scrolling positions.")
5139 (x, y)
5140 Lisp_Object x, y;
5142 if (compare_window_configurations (x, y, 1))
5143 return Qt;
5144 return Qnil;
5147 void
5148 init_window_once ()
5150 struct frame *f = make_terminal_frame ();
5151 XSETFRAME (selected_frame, f);
5152 Vterminal_frame = selected_frame;
5153 minibuf_window = f->minibuffer_window;
5154 selected_window = f->selected_window;
5155 last_nonminibuf_frame = f;
5157 window_initialized = 1;
5160 void
5161 syms_of_window ()
5163 Qleft_bitmap_area = intern ("left-bitmap-area");
5164 staticpro (&Qleft_bitmap_area);
5165 Qright_bitmap_area = intern ("right-bitmap-area");
5166 staticpro (&Qright_bitmap_area);
5168 Qwindow_size_fixed = intern ("window-size-fixed");
5169 staticpro (&Qwindow_size_fixed);
5171 staticpro (&Qwindow_configuration_change_hook);
5172 Qwindow_configuration_change_hook
5173 = intern ("window-configuration-change-hook");
5175 Qwindowp = intern ("windowp");
5176 staticpro (&Qwindowp);
5178 Qwindow_configuration_p = intern ("window-configuration-p");
5179 staticpro (&Qwindow_configuration_p);
5181 Qwindow_live_p = intern ("window-live-p");
5182 staticpro (&Qwindow_live_p);
5184 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
5185 staticpro (&Qtemp_buffer_show_hook);
5187 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
5188 "Non-nil means call as function to display a help buffer.\n\
5189 The function is called with one argument, the buffer to be displayed.\n\
5190 Used by `with-output-to-temp-buffer'.\n\
5191 If this function is used, then it must do the entire job of showing\n\
5192 the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
5193 Vtemp_buffer_show_function = Qnil;
5195 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
5196 "If non-nil, function to call to handle `display-buffer'.\n\
5197 It will receive two args, the buffer and a flag which if non-nil means\n\
5198 that the currently selected window is not acceptable.\n\
5199 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
5200 work using this function.");
5201 Vdisplay_buffer_function = Qnil;
5203 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
5204 "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
5205 Vminibuf_scroll_window = Qnil;
5207 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
5208 "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
5209 Vother_window_scroll_buffer = Qnil;
5211 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
5212 "*Non-nil means `display-buffer' should make a separate frame.");
5213 pop_up_frames = 0;
5215 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
5216 "Function to call to handle automatic new frame creation.\n\
5217 It is called with no arguments and should return a newly created frame.\n\
5219 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
5220 where `pop-up-frame-alist' would hold the default frame parameters.");
5221 Vpop_up_frame_function = Qnil;
5223 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
5224 "*List of buffer names that should have their own special frames.\n\
5225 Displaying a buffer whose name is in this list makes a special frame for it\n\
5226 using `special-display-function'. See also `special-display-regexps'.\n\
5228 An element of the list can be a list instead of just a string.\n\
5229 There are two ways to use a list as an element:\n\
5230 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)\n\
5231 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
5232 In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
5233 followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
5234 All this is done by the function found in `special-display-function'.\n\
5236 If this variable appears \"not to work\", because you add a name to it\n\
5237 but that buffer still appears in the selected window, look at the\n\
5238 values of `same-window-buffer-names' and `same-window-regexps'.\n\
5239 Those variables take precedence over this one.");
5240 Vspecial_display_buffer_names = Qnil;
5242 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
5243 "*List of regexps saying which buffers should have their own special frames.\n\
5244 If a buffer name matches one of these regexps, it gets its own frame.\n\
5245 Displaying a buffer whose name is in this list makes a special frame for it\n\
5246 using `special-display-function'.\n\
5248 An element of the list can be a list instead of just a string.\n\
5249 There are two ways to use a list as an element:\n\
5250 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)\n\
5251 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
5252 In the latter case, FUNCTION is called with the buffer as first argument,\n\
5253 followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
5254 All this is done by the function found in `special-display-function'.\n\
5256 If this variable appears \"not to work\", because you add a regexp to it\n\
5257 but the matching buffers still appear in the selected window, look at the\n\
5258 values of `same-window-buffer-names' and `same-window-regexps'.\n\
5259 Those variables take precedence over this one.");
5260 Vspecial_display_regexps = Qnil;
5262 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
5263 "Function to call to make a new frame for a special buffer.\n\
5264 It is called with two arguments, the buffer and optional buffer specific\n\
5265 data, and should return a window displaying that buffer.\n\
5266 The default value makes a separate frame for the buffer,\n\
5267 using `special-display-frame-alist' to specify the frame parameters.\n\
5269 A buffer is special if its is listed in `special-display-buffer-names'\n\
5270 or matches a regexp in `special-display-regexps'.");
5271 Vspecial_display_function = Qnil;
5273 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
5274 "*List of buffer names that should appear in the selected window.\n\
5275 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
5276 switches to it in the selected window, rather than making it appear\n\
5277 in some other window.\n\
5279 An element of the list can be a cons cell instead of just a string.\n\
5280 Then the car must be a string, which specifies the buffer name.\n\
5281 This is for compatibility with `special-display-buffer-names';\n\
5282 the cdr of the cons cell is ignored.\n\
5284 See also `same-window-regexps'.");
5285 Vsame_window_buffer_names = Qnil;
5287 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
5288 "*List of regexps saying which buffers should appear in the selected window.\n\
5289 If a buffer name matches one of these regexps, then displaying it\n\
5290 using `display-buffer' or `pop-to-buffer' switches to it\n\
5291 in the selected window, rather than making it appear in some other window.\n\
5293 An element of the list can be a cons cell instead of just a string.\n\
5294 Then the car must be a string, which specifies the buffer name.\n\
5295 This is for compatibility with `special-display-buffer-names';\n\
5296 the cdr of the cons cell is ignored.\n\
5298 See also `same-window-buffer-names'.");
5299 Vsame_window_regexps = Qnil;
5301 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
5302 "*Non-nil means display-buffer should make new windows.");
5303 pop_up_windows = 1;
5305 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
5306 "*Number of lines of continuity when scrolling by screenfuls.");
5307 next_screen_context_lines = 2;
5309 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
5310 "*display-buffer would prefer to split the largest window if this large.\n\
5311 If there is only one window, it is split regardless of this value.");
5312 split_height_threshold = 500;
5314 DEFVAR_INT ("window-min-height", &window_min_height,
5315 "*Delete any window less than this tall (including its mode line).");
5316 window_min_height = 4;
5318 DEFVAR_INT ("window-min-width", &window_min_width,
5319 "*Delete any window less than this wide.");
5320 window_min_width = 10;
5322 DEFVAR_LISP ("scroll-preserve-screen-position",
5323 &Vscroll_preserve_screen_position,
5324 "*Nonzero means scroll commands move point to keep its screen line unchanged.");
5325 Vscroll_preserve_screen_position = Qnil;
5327 DEFVAR_LISP ("window-configuration-change-hook",
5328 &Vwindow_configuration_change_hook,
5329 "Functions to call when window configuration changes.\n\
5330 The selected frame is the one whose configuration has changed.");
5331 Vwindow_configuration_change_hook = Qnil;
5333 defsubr (&Sselected_window);
5334 defsubr (&Sminibuffer_window);
5335 defsubr (&Swindow_minibuffer_p);
5336 defsubr (&Swindowp);
5337 defsubr (&Swindow_live_p);
5338 defsubr (&Spos_visible_in_window_p);
5339 defsubr (&Swindow_buffer);
5340 defsubr (&Swindow_height);
5341 defsubr (&Swindow_width);
5342 defsubr (&Swindow_hscroll);
5343 defsubr (&Sset_window_hscroll);
5344 defsubr (&Swindow_redisplay_end_trigger);
5345 defsubr (&Sset_window_redisplay_end_trigger);
5346 defsubr (&Swindow_edges);
5347 defsubr (&Scoordinates_in_window_p);
5348 defsubr (&Swindow_at);
5349 defsubr (&Swindow_point);
5350 defsubr (&Swindow_start);
5351 defsubr (&Swindow_end);
5352 defsubr (&Sset_window_point);
5353 defsubr (&Sset_window_start);
5354 defsubr (&Swindow_dedicated_p);
5355 defsubr (&Sset_window_dedicated_p);
5356 defsubr (&Swindow_display_table);
5357 defsubr (&Sset_window_display_table);
5358 defsubr (&Snext_window);
5359 defsubr (&Sprevious_window);
5360 defsubr (&Sother_window);
5361 defsubr (&Sget_lru_window);
5362 defsubr (&Sget_largest_window);
5363 defsubr (&Sget_buffer_window);
5364 defsubr (&Sdelete_other_windows);
5365 defsubr (&Sdelete_windows_on);
5366 defsubr (&Sreplace_buffer_in_windows);
5367 defsubr (&Sdelete_window);
5368 defsubr (&Sset_window_buffer);
5369 defsubr (&Sselect_window);
5370 defsubr (&Sspecial_display_p);
5371 defsubr (&Ssame_window_p);
5372 defsubr (&Sdisplay_buffer);
5373 defsubr (&Ssplit_window);
5374 defsubr (&Senlarge_window);
5375 defsubr (&Sshrink_window);
5376 defsubr (&Sscroll_up);
5377 defsubr (&Sscroll_down);
5378 defsubr (&Sscroll_left);
5379 defsubr (&Sscroll_right);
5380 defsubr (&Sother_window_for_scrolling);
5381 defsubr (&Sscroll_other_window);
5382 defsubr (&Srecenter);
5383 defsubr (&Smove_to_window_line);
5384 defsubr (&Swindow_configuration_p);
5385 defsubr (&Swindow_configuration_frame);
5386 defsubr (&Sset_window_configuration);
5387 defsubr (&Scurrent_window_configuration);
5388 defsubr (&Ssave_window_excursion);
5389 defsubr (&Sset_window_margins);
5390 defsubr (&Swindow_margins);
5391 defsubr (&Swindow_vscroll);
5392 defsubr (&Sset_window_vscroll);
5393 defsubr (&Scompare_window_configurations);
5396 void
5397 keys_of_window ()
5399 initial_define_key (control_x_map, '1', "delete-other-windows");
5400 initial_define_key (control_x_map, '2', "split-window");
5401 initial_define_key (control_x_map, '0', "delete-window");
5402 initial_define_key (control_x_map, 'o', "other-window");
5403 initial_define_key (control_x_map, '^', "enlarge-window");
5404 initial_define_key (control_x_map, '<', "scroll-left");
5405 initial_define_key (control_x_map, '>', "scroll-right");
5407 initial_define_key (global_map, Ctl ('V'), "scroll-up");
5408 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
5409 initial_define_key (meta_map, 'v', "scroll-down");
5411 initial_define_key (global_map, Ctl('L'), "recenter");
5412 initial_define_key (meta_map, 'r', "move-to-window-line");