Fix up various corner case problems.
[emacs.git] / src / window.c
blob64a97437939b927a963b0596a680edd23d17fd8d
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 2000,
4 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <setjmp.h>
26 #include "lisp.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "indent.h"
34 #include "termchar.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "blockinput.h"
38 #include "intervals.h"
39 #include "termhooks.h" /* For FRAME_TERMINAL. */
41 #ifdef HAVE_X_WINDOWS
42 #include "xterm.h"
43 #endif /* HAVE_X_WINDOWS */
44 #ifdef WINDOWSNT
45 #include "w32term.h"
46 #endif
47 #ifdef MSDOS
48 #include "msdos.h"
49 #endif
50 #ifdef HAVE_NS
51 #include "nsterm.h"
52 #endif
55 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
56 Lisp_Object Qdisplay_buffer;
57 Lisp_Object Qscroll_up, Qscroll_down;
58 Lisp_Object Qwindow_size_fixed;
60 extern Lisp_Object Qleft_margin, Qright_margin;
62 static int displayed_window_lines P_ ((struct window *));
63 static struct window *decode_window P_ ((Lisp_Object));
64 static int count_windows P_ ((struct window *));
65 static int get_leaf_windows P_ ((struct window *, struct window **, int));
66 static void window_scroll P_ ((Lisp_Object, int, int, int));
67 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
68 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
69 static int window_min_size_1 P_ ((struct window *, int, int));
70 static int window_min_size_2 P_ ((struct window *, int, int));
71 static int window_min_size P_ ((struct window *, int, int, int, int *));
72 static void size_window P_ ((Lisp_Object, int, int, int, int, int));
73 static int freeze_window_start P_ ((struct window *, void *));
74 static int window_fixed_size_p P_ ((struct window *, int, int));
75 static void enlarge_window P_ ((Lisp_Object, int, int));
76 static Lisp_Object window_list P_ ((void));
77 static int add_window_to_list P_ ((struct window *, void *));
78 static int candidate_window_p P_ ((Lisp_Object, Lisp_Object, Lisp_Object,
79 Lisp_Object));
80 static Lisp_Object next_window P_ ((Lisp_Object, Lisp_Object,
81 Lisp_Object, int));
82 static void decode_next_window_args P_ ((Lisp_Object *, Lisp_Object *,
83 Lisp_Object *));
84 static int foreach_window_1 P_ ((struct window *,
85 int (* fn) (struct window *, void *),
86 void *));
87 static Lisp_Object window_list_1 P_ ((Lisp_Object, Lisp_Object, Lisp_Object));
89 /* This is the window in which the terminal's cursor should
90 be left when nothing is being done with it. This must
91 always be a leaf window, and its buffer is selected by
92 the top level editing loop at the end of each command.
94 This value is always the same as
95 FRAME_SELECTED_WINDOW (selected_frame). */
97 Lisp_Object selected_window;
99 /* A list of all windows for use by next_window and Fwindow_list.
100 Functions creating or deleting windows should invalidate this cache
101 by setting it to nil. */
103 Lisp_Object Vwindow_list;
105 /* The mini-buffer window of the selected frame.
106 Note that you cannot test for mini-bufferness of an arbitrary window
107 by comparing against this; but you can test for mini-bufferness of
108 the selected window. */
110 Lisp_Object minibuf_window;
112 /* Non-nil means it is the window whose mode line should be
113 shown as the selected window when the minibuffer is selected. */
115 Lisp_Object minibuf_selected_window;
117 /* Non-nil means it is the window for C-M-v to scroll
118 when the mini-buffer is selected. */
120 Lisp_Object Vminibuf_scroll_window;
122 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
124 Lisp_Object Vother_window_scroll_buffer;
126 /* Non-nil means it's function to call to display temp buffers. */
128 Lisp_Object Vtemp_buffer_show_function;
130 /* Non-zero means line and page scrolling on tall lines (with images)
131 does partial scrolling by modifying window-vscroll. */
133 int auto_window_vscroll_p;
135 /* Non-zero means to use mode-line-inactive face in all windows but the
136 selected-window and the minibuffer-scroll-window when the
137 minibuffer is active. */
138 int mode_line_in_non_selected_windows;
140 /* If a window gets smaller than either of these, it is removed. */
142 EMACS_INT window_min_height;
143 EMACS_INT window_min_width;
145 /* Hook run at end of temp_output_buffer_show. */
147 Lisp_Object Qtemp_buffer_show_hook;
149 /* Number of lines of continuity in scrolling by screenfuls. */
151 EMACS_INT next_screen_context_lines;
153 /* Incremented for each window created. */
155 static int sequence_number;
157 /* Nonzero after init_window_once has finished. */
159 static int window_initialized;
161 /* Hook to run when window config changes. */
163 static Lisp_Object Qwindow_configuration_change_hook;
164 static Lisp_Object Vwindow_configuration_change_hook;
166 /* Non-nil means scroll commands try to put point
167 at the same screen height as previously. */
169 Lisp_Object Vscroll_preserve_screen_position;
171 /* Non-nil means that text is inserted before window's markers. */
173 Lisp_Object Vwindow_point_insertion_type;
175 /* Incremented by 1 whenever a window is deleted. */
177 int window_deletion_count;
179 /* Used by the function window_scroll_pixel_based */
181 static int window_scroll_pixel_based_preserve_x;
182 static int window_scroll_pixel_based_preserve_y;
184 /* Same for window_scroll_line_based. */
186 static int window_scroll_preserve_hpos;
187 static int window_scroll_preserve_vpos;
189 #if 0 /* This isn't used anywhere. */
190 /* Nonzero means we can split a frame even if it is "unsplittable". */
191 static int inhibit_frame_unsplittable;
192 #endif /* 0 */
194 extern EMACS_INT scroll_margin;
196 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
198 /* If non-nil, then the `recenter' command with a nil argument
199 the entire frame to be redrawn; the special value `tty' causes the
200 frame to be redrawn only if it is a tty frame. */
202 static Lisp_Object Vrecenter_redisplay;
203 extern Lisp_Object Qtty;
206 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
207 doc: /* Return t if OBJECT is a window. */)
208 (object)
209 Lisp_Object object;
211 return WINDOWP (object) ? Qt : Qnil;
214 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
215 doc: /* Return t if OBJECT is a window which is currently visible. */)
216 (object)
217 Lisp_Object object;
219 return WINDOW_LIVE_P (object) ? Qt : Qnil;
222 Lisp_Object
223 make_window ()
225 Lisp_Object val;
226 register struct window *p;
228 p = allocate_window ();
229 ++sequence_number;
230 XSETFASTINT (p->sequence_number, sequence_number);
231 XSETFASTINT (p->left_col, 0);
232 XSETFASTINT (p->top_line, 0);
233 XSETFASTINT (p->total_lines, 0);
234 XSETFASTINT (p->total_cols, 0);
235 XSETFASTINT (p->hscroll, 0);
236 XSETFASTINT (p->min_hscroll, 0);
237 p->orig_top_line = p->orig_total_lines = Qnil;
238 p->start = Fmake_marker ();
239 p->pointm = Fmake_marker ();
240 XSETFASTINT (p->use_time, 0);
241 p->frame = Qnil;
242 p->display_table = Qnil;
243 p->dedicated = Qnil;
244 p->window_parameters = Qnil;
245 p->pseudo_window_p = 0;
246 bzero (&p->cursor, sizeof (p->cursor));
247 bzero (&p->last_cursor, sizeof (p->last_cursor));
248 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
249 p->desired_matrix = p->current_matrix = 0;
250 p->nrows_scale_factor = p->ncols_scale_factor = 1;
251 p->phys_cursor_type = -1;
252 p->phys_cursor_width = -1;
253 p->must_be_updated_p = 0;
254 XSETFASTINT (p->window_end_vpos, 0);
255 XSETFASTINT (p->window_end_pos, 0);
256 p->window_end_valid = Qnil;
257 p->vscroll = 0;
258 XSETWINDOW (val, p);
259 XSETFASTINT (p->last_point, 0);
260 p->frozen_window_start_p = 0;
261 p->last_cursor_off_p = p->cursor_off_p = 0;
262 p->left_margin_cols = Qnil;
263 p->right_margin_cols = Qnil;
264 p->left_fringe_width = Qnil;
265 p->right_fringe_width = Qnil;
266 p->fringes_outside_margins = Qnil;
267 p->scroll_bar_width = Qnil;
268 p->vertical_scroll_bar_type = Qt;
269 p->resize_proportionally = Qnil;
271 Vwindow_list = Qnil;
272 return val;
275 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
276 doc: /* Return the window that the cursor now appears in and commands apply to. */)
279 return selected_window;
282 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
283 doc: /* Return the window used now for minibuffers.
284 If the optional argument FRAME is specified, return the minibuffer window
285 used by that frame. */)
286 (frame)
287 Lisp_Object frame;
289 if (NILP (frame))
290 frame = selected_frame;
291 CHECK_LIVE_FRAME (frame);
292 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
295 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
296 doc: /* Return non-nil if WINDOW is a minibuffer window.
297 WINDOW defaults to the selected window. */)
298 (window)
299 Lisp_Object window;
301 struct window *w = decode_window (window);
302 return MINI_WINDOW_P (w) ? Qt : Qnil;
306 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
307 Spos_visible_in_window_p, 0, 3, 0,
308 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
309 Return nil if that position is scrolled vertically out of view.
310 If a character is only partially visible, nil is returned, unless the
311 optional argument PARTIALLY is non-nil.
312 If POS is only out of view because of horizontal scrolling, return non-nil.
313 If POS is t, it specifies the position of the last visible glyph in WINDOW.
314 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
316 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
317 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
318 where X and Y are the pixel coordinates relative to the top left corner
319 of the window. The remaining elements are omitted if the character after
320 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
321 off-window at the top and bottom of the row, ROWH is the height of the
322 display row, and VPOS is the row number (0-based) containing POS. */)
323 (pos, window, partially)
324 Lisp_Object pos, window, partially;
326 register struct window *w;
327 register int posint;
328 register struct buffer *buf;
329 struct text_pos top;
330 Lisp_Object in_window = Qnil;
331 int rtop, rbot, rowh, vpos, fully_p = 1;
332 int x, y;
334 w = decode_window (window);
335 buf = XBUFFER (w->buffer);
336 SET_TEXT_POS_FROM_MARKER (top, w->start);
338 if (EQ (pos, Qt))
339 posint = -1;
340 else if (!NILP (pos))
342 CHECK_NUMBER_COERCE_MARKER (pos);
343 posint = XINT (pos);
345 else if (w == XWINDOW (selected_window))
346 posint = PT;
347 else
348 posint = XMARKER (w->pointm)->charpos;
350 /* If position is above window start or outside buffer boundaries,
351 or if window start is out of range, position is not visible. */
352 if ((EQ (pos, Qt)
353 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
354 && CHARPOS (top) >= BUF_BEGV (buf)
355 && CHARPOS (top) <= BUF_ZV (buf)
356 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
357 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
358 in_window = Qt;
360 if (!NILP (in_window) && !NILP (partially))
362 Lisp_Object part = Qnil;
363 if (!fully_p)
364 part = list4 (make_number (rtop), make_number (rbot),
365 make_number (rowh), make_number (vpos));
366 in_window = Fcons (make_number (x),
367 Fcons (make_number (y), part));
370 return in_window;
373 DEFUN ("window-line-height", Fwindow_line_height,
374 Swindow_line_height, 0, 2, 0,
375 doc: /* Return height in pixels of text line LINE in window WINDOW.
376 If WINDOW is nil or omitted, use selected window.
378 Return height of current line if LINE is omitted or nil. Return height of
379 header or mode line if LINE is `header-line' and `mode-line'.
380 Otherwise, LINE is a text line number starting from 0. A negative number
381 counts from the end of the window.
383 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
384 in pixels of the visible part of the line, VPOS and YPOS are the
385 vertical position in lines and pixels of the line, relative to the top
386 of the first text line, and OFFBOT is the number of off-window pixels at
387 the bottom of the text line. If there are off-window pixels at the top
388 of the (first) text line, YPOS is negative.
390 Return nil if window display is not up-to-date. In that case, use
391 `pos-visible-in-window-p' to obtain the information. */)
392 (line, window)
393 Lisp_Object line, window;
395 register struct window *w;
396 register struct buffer *b;
397 struct glyph_row *row, *end_row;
398 int max_y, crop, i, n;
400 w = decode_window (window);
402 if (noninteractive
403 || w->pseudo_window_p)
404 return Qnil;
406 CHECK_BUFFER (w->buffer);
407 b = XBUFFER (w->buffer);
409 /* Fail if current matrix is not up-to-date. */
410 if (NILP (w->window_end_valid)
411 || current_buffer->clip_changed
412 || current_buffer->prevent_redisplay_optimizations_p
413 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
414 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
415 return Qnil;
417 if (NILP (line))
419 i = w->cursor.vpos;
420 if (i < 0 || i >= w->current_matrix->nrows
421 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
422 return Qnil;
423 max_y = window_text_bottom_y (w);
424 goto found_row;
427 if (EQ (line, Qheader_line))
429 if (!WINDOW_WANTS_HEADER_LINE_P (w))
430 return Qnil;
431 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
432 if (!row->enabled_p)
433 return Qnil;
434 return list4 (make_number (row->height),
435 make_number (0), make_number (0),
436 make_number (0));
439 if (EQ (line, Qmode_line))
441 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
442 if (!row->enabled_p)
443 return Qnil;
444 return list4 (make_number (row->height),
445 make_number (0), /* not accurate */
446 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
447 + window_text_bottom_y (w)),
448 make_number (0));
451 CHECK_NUMBER (line);
452 n = XINT (line);
454 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
455 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
456 max_y = window_text_bottom_y (w);
457 i = 0;
459 while ((n < 0 || i < n)
460 && row <= end_row && row->enabled_p
461 && row->y + row->height < max_y)
462 row++, i++;
464 if (row > end_row || !row->enabled_p)
465 return Qnil;
467 if (++n < 0)
469 if (-n > i)
470 return Qnil;
471 row += n;
472 i += n;
475 found_row:
476 crop = max (0, (row->y + row->height) - max_y);
477 return list4 (make_number (row->height + min (0, row->y) - crop),
478 make_number (i),
479 make_number (row->y),
480 make_number (crop));
485 static struct window *
486 decode_window (window)
487 register Lisp_Object window;
489 if (NILP (window))
490 return XWINDOW (selected_window);
492 CHECK_LIVE_WINDOW (window);
493 return XWINDOW (window);
496 static struct window *
497 decode_any_window (window)
498 register Lisp_Object window;
500 if (NILP (window))
501 return XWINDOW (selected_window);
503 CHECK_WINDOW (window);
504 return XWINDOW (window);
507 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
508 doc: /* Return the buffer that WINDOW is displaying.
509 WINDOW defaults to the selected window. */)
510 (window)
511 Lisp_Object window;
513 return decode_window (window)->buffer;
516 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
517 doc: /* Return the number of lines in WINDOW.
518 WINDOW defaults to the selected window.
520 The return value includes WINDOW's mode line and header line, if any. */)
521 (window)
522 Lisp_Object window;
524 return decode_any_window (window)->total_lines;
527 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
528 doc: /* Return the number of display columns in WINDOW.
529 WINDOW defaults to the selected window.
531 Note: The return value is the number of columns available for text in
532 WINDOW. If you want to find out how many columns WINDOW takes up, use
533 (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
534 (window)
535 Lisp_Object window;
537 return make_number (window_box_text_cols (decode_any_window (window)));
540 DEFUN ("window-full-width-p", Fwindow_full_width_p, Swindow_full_width_p, 0, 1, 0,
541 doc: /* Return t if WINDOW is as wide as its frame.
542 WINDOW defaults to the selected window. */)
543 (window)
544 Lisp_Object window;
546 return WINDOW_FULL_WIDTH_P (decode_any_window (window)) ? Qt : Qnil;
549 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
550 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
551 WINDOW defaults to the selected window. */)
552 (window)
553 Lisp_Object window;
555 return decode_window (window)->hscroll;
558 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
559 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
560 Return NCOL. NCOL should be zero or positive.
562 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
563 window so that the location of point moves off-window. */)
564 (window, ncol)
565 Lisp_Object window, ncol;
567 struct window *w = decode_window (window);
568 int hscroll;
570 CHECK_NUMBER (ncol);
571 hscroll = max (0, XINT (ncol));
573 /* Prevent redisplay shortcuts when changing the hscroll. */
574 if (XINT (w->hscroll) != hscroll)
575 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
577 w->hscroll = make_number (hscroll);
578 return ncol;
581 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
582 Swindow_redisplay_end_trigger, 0, 1, 0,
583 doc: /* Return WINDOW's redisplay end trigger value.
584 WINDOW defaults to the selected window.
585 See `set-window-redisplay-end-trigger' for more information. */)
586 (window)
587 Lisp_Object window;
589 return decode_window (window)->redisplay_end_trigger;
592 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
593 Sset_window_redisplay_end_trigger, 2, 2, 0,
594 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
595 VALUE should be a buffer position (typically a marker) or nil.
596 If it is a buffer position, then if redisplay in WINDOW reaches a position
597 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
598 with two arguments: WINDOW, and the end trigger value.
599 Afterwards the end-trigger value is reset to nil. */)
600 (window, value)
601 register Lisp_Object window, value;
603 register struct window *w;
605 w = decode_window (window);
606 w->redisplay_end_trigger = value;
607 return value;
610 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
611 doc: /* Return a list of the edge coordinates of WINDOW.
612 The list has the form (LEFT TOP RIGHT BOTTOM).
613 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
614 all relative to 0, 0 at top left corner of frame.
616 RIGHT is one more than the rightmost column occupied by WINDOW.
617 BOTTOM is one more than the bottommost row occupied by WINDOW.
618 The edges include the space used by WINDOW's scroll bar, display
619 margins, fringes, header line, and/or mode line. For the edges of
620 just the text area, use `window-inside-edges'. */)
621 (window)
622 Lisp_Object window;
624 register struct window *w = decode_any_window (window);
626 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
627 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
628 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
629 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
630 Qnil))));
633 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
634 doc: /* Return a list of the edge pixel coordinates of WINDOW.
635 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
636 the top left corner of the frame.
638 RIGHT is one more than the rightmost x position occupied by WINDOW.
639 BOTTOM is one more than the bottommost y position occupied by WINDOW.
640 The pixel edges include the space used by WINDOW's scroll bar, display
641 margins, fringes, header line, and/or mode line. For the pixel edges
642 of just the text area, use `window-inside-pixel-edges'. */)
643 (window)
644 Lisp_Object window;
646 register struct window *w = decode_any_window (window);
648 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
649 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
650 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
651 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
652 Qnil))));
655 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
656 doc: /* Return a list of the edge coordinates of WINDOW.
657 The list has the form (LEFT TOP RIGHT BOTTOM).
658 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
659 all relative to 0, 0 at top left corner of frame.
661 RIGHT is one more than the rightmost column of WINDOW's text area.
662 BOTTOM is one more than the bottommost row of WINDOW's text area.
663 The inside edges do not include the space used by the WINDOW's scroll
664 bar, display margins, fringes, header line, and/or mode line. */)
665 (window)
666 Lisp_Object window;
668 register struct window *w = decode_any_window (window);
670 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
671 + WINDOW_LEFT_MARGIN_COLS (w)
672 + WINDOW_LEFT_FRINGE_COLS (w)),
673 make_number (WINDOW_TOP_EDGE_LINE (w)
674 + WINDOW_HEADER_LINE_LINES (w)),
675 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
676 - WINDOW_RIGHT_MARGIN_COLS (w)
677 - WINDOW_RIGHT_FRINGE_COLS (w)),
678 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
679 - WINDOW_MODE_LINE_LINES (w)));
682 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
683 doc: /* Return a list of the edge pixel coordinates of WINDOW.
684 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
685 the top left corner of the frame.
687 RIGHT is one more than the rightmost x position of WINDOW's text area.
688 BOTTOM is one more than the bottommost y position of WINDOW's text area.
689 The inside edges do not include the space used by WINDOW's scroll bar,
690 display margins, fringes, header line, and/or mode line. */)
691 (window)
692 Lisp_Object window;
694 register struct window *w = decode_any_window (window);
696 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
697 + WINDOW_LEFT_MARGIN_WIDTH (w)
698 + WINDOW_LEFT_FRINGE_WIDTH (w)),
699 make_number (WINDOW_TOP_EDGE_Y (w)
700 + WINDOW_HEADER_LINE_HEIGHT (w)),
701 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
702 - WINDOW_RIGHT_MARGIN_WIDTH (w)
703 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
704 make_number (WINDOW_BOTTOM_EDGE_Y (w)
705 - WINDOW_MODE_LINE_HEIGHT (w)));
708 /* Test if the character at column *X, row *Y is within window W.
709 If it is not, return ON_NOTHING;
710 if it is in the window's text area,
711 set *x and *y to its location relative to the upper left corner
712 of the window, and
713 return ON_TEXT;
714 if it is on the window's modeline, return ON_MODE_LINE;
715 if it is on the border between the window and its right sibling,
716 return ON_VERTICAL_BORDER.
717 if it is on a scroll bar,
718 return ON_SCROLL_BAR.
719 if it is on the window's top line, return ON_HEADER_LINE;
720 if it is in left or right fringe of the window,
721 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE, and convert *X and *Y
722 to window-relative coordinates;
723 if it is in the marginal area to the left/right of the window,
724 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN, and convert *X and *Y
725 to window-relative coordinates.
727 X and Y are frame relative pixel coordinates. */
729 static enum window_part
730 coordinates_in_window (w, x, y)
731 register struct window *w;
732 register int *x, *y;
734 struct frame *f = XFRAME (WINDOW_FRAME (w));
735 int left_x, right_x, top_y, bottom_y;
736 enum window_part part;
737 int ux = FRAME_COLUMN_WIDTH (f);
738 int x0 = WINDOW_LEFT_EDGE_X (w);
739 int x1 = WINDOW_RIGHT_EDGE_X (w);
740 /* The width of the area where the vertical line can be dragged.
741 (Between mode lines for instance. */
742 int grabbable_width = ux;
743 int lmargin_width, rmargin_width, text_left, text_right;
745 /* In what's below, we subtract 1 when computing right_x because we
746 want the rightmost pixel, which is given by left_pixel+width-1. */
747 if (w->pseudo_window_p)
749 left_x = 0;
750 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
751 top_y = WINDOW_TOP_EDGE_Y (w);
752 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
754 else
756 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
757 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
758 top_y = WINDOW_TOP_EDGE_Y (w);
759 bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
762 /* Outside any interesting row? */
763 if (*y < top_y || *y >= bottom_y)
764 return ON_NOTHING;
766 /* On the mode line or header line? If it's near the start of
767 the mode or header line of window that's has a horizontal
768 sibling, say it's on the vertical line. That's to be able
769 to resize windows horizontally in case we're using toolkit
770 scroll bars. */
772 if (WINDOW_WANTS_MODELINE_P (w)
773 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
775 part = ON_MODE_LINE;
777 header_vertical_border_check:
778 /* We're somewhere on the mode line. We consider the place
779 between mode lines of horizontally adjacent mode lines
780 as the vertical border. If scroll bars on the left,
781 return the right window. */
782 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
783 || WINDOW_RIGHTMOST_P (w))
785 if (!WINDOW_LEFTMOST_P (w) && eabs (*x - x0) < grabbable_width)
787 /* Convert X and Y to window relative coordinates.
788 Vertical border is at the left edge of window. */
789 *x = max (0, *x - x0);
790 *y -= top_y;
791 return ON_VERTICAL_BORDER;
794 else
796 /* Make sure we're not at the rightmost position of a
797 mode-/header-line and there's yet another window on
798 the right. (Bug#1372) */
799 if ((WINDOW_RIGHTMOST_P (w) || *x < x1)
800 && eabs (*x - x1) < grabbable_width)
802 /* Convert X and Y to window relative coordinates.
803 Vertical border is at the right edge of window. */
804 *x = min (x1, *x) - x0;
805 *y -= top_y;
806 return ON_VERTICAL_BORDER;
810 if (*x < x0 || *x >= x1)
811 return ON_NOTHING;
813 /* Convert X and Y to window relative coordinates.
814 Mode line starts at left edge of window. */
815 *x -= x0;
816 *y -= top_y;
817 return part;
820 if (WINDOW_WANTS_HEADER_LINE_P (w)
821 && *y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
823 part = ON_HEADER_LINE;
824 goto header_vertical_border_check;
827 if (*x < x0 || *x >= x1)
828 return ON_NOTHING;
830 /* Outside any interesting column? */
831 if (*x < left_x || *x > right_x)
833 *y -= top_y;
834 return ON_SCROLL_BAR;
837 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
838 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
840 text_left = window_box_left (w, TEXT_AREA);
841 text_right = text_left + window_box_width (w, TEXT_AREA);
843 if (FRAME_WINDOW_P (f))
845 if (!w->pseudo_window_p
846 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
847 && !WINDOW_RIGHTMOST_P (w)
848 && (eabs (*x - right_x) < grabbable_width))
850 /* Convert X and Y to window relative coordinates.
851 Vertical border is at the right edge of window. */
852 *x = min (right_x, *x) - left_x;
853 *y -= top_y;
854 return ON_VERTICAL_BORDER;
857 else
859 /* Need to say "*x > right_x" rather than >=, since on character
860 terminals, the vertical line's x coordinate is right_x. */
861 if (!w->pseudo_window_p
862 && !WINDOW_RIGHTMOST_P (w)
863 && *x > right_x - ux)
865 /* On the border on the right side of the window? Assume that
866 this area begins at RIGHT_X minus a canonical char width. */
867 *x = min (right_x, *x) - left_x;
868 *y -= top_y;
869 return ON_VERTICAL_BORDER;
873 if (*x < text_left)
875 if (lmargin_width > 0
876 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
877 ? (*x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
878 : (*x < left_x + lmargin_width)))
880 *x -= left_x;
881 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
882 *x -= WINDOW_LEFT_FRINGE_WIDTH (w);
883 *y -= top_y;
884 return ON_LEFT_MARGIN;
887 /* Convert X and Y to window-relative pixel coordinates. */
888 *x -= left_x;
889 *y -= top_y;
890 return ON_LEFT_FRINGE;
893 if (*x >= text_right)
895 if (rmargin_width > 0
896 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
897 ? (*x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
898 : (*x >= right_x - rmargin_width)))
900 *x -= right_x - rmargin_width;
901 if (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
902 *x += WINDOW_RIGHT_FRINGE_WIDTH (w);
903 *y -= top_y;
904 return ON_RIGHT_MARGIN;
907 /* Convert X and Y to window-relative pixel coordinates. */
908 *x -= left_x + WINDOW_LEFT_FRINGE_WIDTH (w);
909 *y -= top_y;
910 return ON_RIGHT_FRINGE;
913 /* Everything special ruled out - must be on text area */
914 *x -= text_left;
915 *y -= top_y;
916 return ON_TEXT;
920 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
921 Scoordinates_in_window_p, 2, 2, 0,
922 doc: /* Return non-nil if COORDINATES are in WINDOW.
923 COORDINATES is a cons of the form (X . Y), X and Y being distances
924 measured in characters from the upper-left corner of the frame.
925 \(0 . 0) denotes the character in the upper left corner of the
926 frame.
927 If COORDINATES are in the text portion of WINDOW,
928 the coordinates relative to the window are returned.
929 If they are in the mode line of WINDOW, `mode-line' is returned.
930 If they are in the top mode line of WINDOW, `header-line' is returned.
931 If they are in the left fringe of WINDOW, `left-fringe' is returned.
932 If they are in the right fringe of WINDOW, `right-fringe' is returned.
933 If they are on the border between WINDOW and its right sibling,
934 `vertical-line' is returned.
935 If they are in the windows's left or right marginal areas, `left-margin'\n\
936 or `right-margin' is returned. */)
937 (coordinates, window)
938 register Lisp_Object coordinates, window;
940 struct window *w;
941 struct frame *f;
942 int x, y;
943 Lisp_Object lx, ly;
945 CHECK_WINDOW (window);
946 w = XWINDOW (window);
947 f = XFRAME (w->frame);
948 CHECK_CONS (coordinates);
949 lx = Fcar (coordinates);
950 ly = Fcdr (coordinates);
951 CHECK_NUMBER_OR_FLOAT (lx);
952 CHECK_NUMBER_OR_FLOAT (ly);
953 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
954 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
956 switch (coordinates_in_window (w, &x, &y))
958 case ON_NOTHING:
959 return Qnil;
961 case ON_TEXT:
962 /* X and Y are now window relative pixel coordinates. Convert
963 them to canonical char units before returning them. */
964 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
965 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
967 case ON_MODE_LINE:
968 return Qmode_line;
970 case ON_VERTICAL_BORDER:
971 return Qvertical_line;
973 case ON_HEADER_LINE:
974 return Qheader_line;
976 case ON_LEFT_FRINGE:
977 return Qleft_fringe;
979 case ON_RIGHT_FRINGE:
980 return Qright_fringe;
982 case ON_LEFT_MARGIN:
983 return Qleft_margin;
985 case ON_RIGHT_MARGIN:
986 return Qright_margin;
988 case ON_SCROLL_BAR:
989 /* Historically we are supposed to return nil in this case. */
990 return Qnil;
992 default:
993 abort ();
998 /* Callback for foreach_window, used in window_from_coordinates.
999 Check if window W contains coordinates specified by USER_DATA which
1000 is actually a pointer to a struct check_window_data CW.
1002 Check if window W contains coordinates *CW->x and *CW->y. If it
1003 does, return W in *CW->window, as Lisp_Object, and return in
1004 *CW->part the part of the window under coordinates *X,*Y. Return
1005 zero from this function to stop iterating over windows. */
1007 struct check_window_data
1009 Lisp_Object *window;
1010 int *x, *y;
1011 enum window_part *part;
1014 static int
1015 check_window_containing (w, user_data)
1016 struct window *w;
1017 void *user_data;
1019 struct check_window_data *cw = (struct check_window_data *) user_data;
1020 enum window_part found;
1021 int continue_p = 1;
1023 found = coordinates_in_window (w, cw->x, cw->y);
1024 if (found != ON_NOTHING)
1026 *cw->part = found;
1027 XSETWINDOW (*cw->window, w);
1028 continue_p = 0;
1031 return continue_p;
1035 /* Find the window containing frame-relative pixel position X/Y and
1036 return it as a Lisp_Object.
1038 If X, Y is on one of the window's special `window_part' elements,
1039 set *PART to the id of that element, and return X and Y converted
1040 to window relative coordinates in WX and WY.
1042 If there is no window under X, Y return nil and leave *PART
1043 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1045 This function was previously implemented with a loop cycling over
1046 windows with Fnext_window, and starting with the frame's selected
1047 window. It turned out that this doesn't work with an
1048 implementation of next_window using Vwindow_list, because
1049 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1050 tree of F when this function is called asynchronously from
1051 note_mouse_highlight. The original loop didn't terminate in this
1052 case. */
1054 Lisp_Object
1055 window_from_coordinates (f, x, y, part, wx, wy, tool_bar_p)
1056 struct frame *f;
1057 int x, y;
1058 enum window_part *part;
1059 int *wx, *wy;
1060 int tool_bar_p;
1062 Lisp_Object window;
1063 struct check_window_data cw;
1064 enum window_part dummy;
1066 if (part == 0)
1067 part = &dummy;
1069 window = Qnil;
1070 cw.window = &window, cw.x = &x, cw.y = &y; cw.part = part;
1071 foreach_window (f, check_window_containing, &cw);
1073 /* If not found above, see if it's in the tool bar window, if a tool
1074 bar exists. */
1075 if (NILP (window)
1076 && tool_bar_p
1077 && WINDOWP (f->tool_bar_window)
1078 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1079 && (coordinates_in_window (XWINDOW (f->tool_bar_window), &x, &y)
1080 != ON_NOTHING))
1082 *part = ON_TEXT;
1083 window = f->tool_bar_window;
1086 if (wx) *wx = x;
1087 if (wy) *wy = y;
1089 return window;
1092 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1093 doc: /* Return window containing coordinates X and Y on FRAME.
1094 If omitted, FRAME defaults to the currently selected frame.
1095 The top left corner of the frame is considered to be row 0,
1096 column 0. */)
1097 (x, y, frame)
1098 Lisp_Object x, y, frame;
1100 struct frame *f;
1102 if (NILP (frame))
1103 frame = selected_frame;
1104 CHECK_LIVE_FRAME (frame);
1105 f = XFRAME (frame);
1107 /* Check that arguments are integers or floats. */
1108 CHECK_NUMBER_OR_FLOAT (x);
1109 CHECK_NUMBER_OR_FLOAT (y);
1111 return window_from_coordinates (f,
1112 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1113 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1114 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1115 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1116 0, 0, 0, 0);
1119 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1120 doc: /* Return current value of point in WINDOW.
1121 WINDOW defaults to the selected window.
1123 For a nonselected window, this is the value point would have
1124 if that window were selected.
1126 Note that, when WINDOW is the selected window and its buffer
1127 is also currently selected, the value returned is the same as (point).
1128 It would be more strictly correct to return the `top-level' value
1129 of point, outside of any save-excursion forms.
1130 But that is hard to define. */)
1131 (window)
1132 Lisp_Object window;
1134 register struct window *w = decode_window (window);
1136 if (w == XWINDOW (selected_window)
1137 && current_buffer == XBUFFER (w->buffer))
1138 return Fpoint ();
1139 return Fmarker_position (w->pointm);
1142 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1143 doc: /* Return position at which display currently starts in WINDOW.
1144 WINDOW defaults to the selected window.
1145 This is updated by redisplay or by calling `set-window-start'. */)
1146 (window)
1147 Lisp_Object window;
1149 return Fmarker_position (decode_window (window)->start);
1152 /* This is text temporarily removed from the doc string below.
1154 This function returns nil if the position is not currently known.
1155 That happens when redisplay is preempted and doesn't finish.
1156 If in that case you want to compute where the end of the window would
1157 have been if redisplay had finished, do this:
1158 (save-excursion
1159 (goto-char (window-start window))
1160 (vertical-motion (1- (window-height window)) window)
1161 (point))") */
1163 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1164 doc: /* Return position at which display currently ends in WINDOW.
1165 WINDOW defaults to the selected window.
1166 This is updated by redisplay, when it runs to completion.
1167 Simply changing the buffer text or setting `window-start'
1168 does not update this value.
1169 Return nil if there is no recorded value. \(This can happen if the
1170 last redisplay of WINDOW was preempted, and did not finish.)
1171 If UPDATE is non-nil, compute the up-to-date position
1172 if it isn't already recorded. */)
1173 (window, update)
1174 Lisp_Object window, update;
1176 Lisp_Object value;
1177 struct window *w = decode_window (window);
1178 Lisp_Object buf;
1179 struct buffer *b;
1181 buf = w->buffer;
1182 CHECK_BUFFER (buf);
1183 b = XBUFFER (buf);
1185 #if 0 /* This change broke some things. We should make it later. */
1186 /* If we don't know the end position, return nil.
1187 The user can compute it with vertical-motion if he wants to.
1188 It would be nicer to do it automatically,
1189 but that's so slow that it would probably bother people. */
1190 if (NILP (w->window_end_valid))
1191 return Qnil;
1192 #endif
1194 if (! NILP (update)
1195 && ! (! NILP (w->window_end_valid)
1196 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1197 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1198 && !noninteractive)
1200 struct text_pos startp;
1201 struct it it;
1202 struct buffer *old_buffer = NULL;
1204 /* Cannot use Fvertical_motion because that function doesn't
1205 cope with variable-height lines. */
1206 if (b != current_buffer)
1208 old_buffer = current_buffer;
1209 set_buffer_internal (b);
1212 /* In case W->start is out of the range, use something
1213 reasonable. This situation occurred when loading a file with
1214 `-l' containing a call to `rmail' with subsequent other
1215 commands. At the end, W->start happened to be BEG, while
1216 rmail had already narrowed the buffer. */
1217 if (XMARKER (w->start)->charpos < BEGV)
1218 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1219 else if (XMARKER (w->start)->charpos > ZV)
1220 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1221 else
1222 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1224 start_display (&it, w, startp);
1225 move_it_vertically (&it, window_box_height (w));
1226 if (it.current_y < it.last_visible_y)
1227 move_it_past_eol (&it);
1228 value = make_number (IT_CHARPOS (it));
1230 if (old_buffer)
1231 set_buffer_internal (old_buffer);
1233 else
1234 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1236 return value;
1239 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1240 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1241 Return POS. */)
1242 (window, pos)
1243 Lisp_Object window, pos;
1245 register struct window *w = decode_window (window);
1247 CHECK_NUMBER_COERCE_MARKER (pos);
1248 if (w == XWINDOW (selected_window)
1249 && XBUFFER (w->buffer) == current_buffer)
1250 Fgoto_char (pos);
1251 else
1252 set_marker_restricted (w->pointm, pos, w->buffer);
1254 /* We have to make sure that redisplay updates the window to show
1255 the new value of point. */
1256 if (!EQ (window, selected_window))
1257 ++windows_or_buffers_changed;
1259 return pos;
1262 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1263 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1264 WINDOW defaults to the selected window. Return POS.
1265 Optional third arg NOFORCE non-nil inhibits next redisplay from
1266 overriding motion of point in order to display at this exact start. */)
1267 (window, pos, noforce)
1268 Lisp_Object window, pos, noforce;
1270 register struct window *w = decode_window (window);
1272 CHECK_NUMBER_COERCE_MARKER (pos);
1273 set_marker_restricted (w->start, pos, w->buffer);
1274 /* this is not right, but much easier than doing what is right. */
1275 w->start_at_line_beg = Qnil;
1276 if (NILP (noforce))
1277 w->force_start = Qt;
1278 w->update_mode_line = Qt;
1279 XSETFASTINT (w->last_modified, 0);
1280 XSETFASTINT (w->last_overlay_modified, 0);
1281 if (!EQ (window, selected_window))
1282 windows_or_buffers_changed++;
1284 return pos;
1288 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1289 0, 1, 0,
1290 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1291 More precisely, return the value assigned by the last call of
1292 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1293 never called with WINDOW as its argument, or the value set by that
1294 function was internally reset since its last call. WINDOW defaults to
1295 the selected window.
1297 When a window is dedicated to its buffer, `display-buffer' will refrain
1298 from displaying another buffer in it. `get-lru-window' and
1299 `get-largest-window' treat dedicated windows specially.
1300 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1301 `kill-buffer' can delete a dedicated window and the containing frame.
1303 Functions like `set-window-buffer' may change the buffer displayed by a
1304 window, unless that window is "strongly" dedicated to its buffer, that
1305 is the value returned by `window-dedicated-p' is t. */)
1306 (window)
1307 Lisp_Object window;
1309 return decode_window (window)->dedicated;
1312 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1313 Sset_window_dedicated_p, 2, 2, 0,
1314 doc: /* Mark WINDOW as dedicated according to FLAG.
1315 WINDOW defaults to the selected window. FLAG non-nil means mark WINDOW
1316 as dedicated to its buffer. FLAG nil means mark WINDOW as non-dedicated.
1317 Return FLAG.
1319 When a window is dedicated to its buffer, `display-buffer' will refrain
1320 from displaying another buffer in it. `get-lru-window' and
1321 `get-largest-window' treat dedicated windows specially.
1322 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1323 `kill-buffer' can delete a dedicated window and the containing
1324 frame.
1326 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1327 its buffer. Functions like `set-window-buffer' may change the buffer
1328 displayed by a window, unless that window is strongly dedicated to its
1329 buffer. If and when `set-window-buffer' displays another buffer in a
1330 window, it also makes sure that the window is not marked as dedicated. */)
1331 (window, flag)
1332 Lisp_Object window, flag;
1334 register struct window *w = decode_window (window);
1336 w->dedicated = flag;
1337 return w->dedicated;
1341 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1342 0, 1, 0,
1343 doc: /* Return the parameters of WINDOW and their values.
1344 WINDOW defaults to the selected window. The return value is a list of
1345 elements of the form (PARAMETER . VALUE). */)
1346 (window)
1347 Lisp_Object window;
1349 return Fcopy_alist (decode_window (window)->window_parameters);
1352 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1353 2, 2, 0,
1354 doc: /* Return WINDOW's value for PARAMETER.
1355 WINDOW defaults to the selected window. */)
1356 (window, parameter)
1357 Lisp_Object window, parameter;
1359 Lisp_Object result;
1361 result = Fassq (parameter, decode_window (window)->window_parameters);
1362 return CDR_SAFE (result);
1365 DEFUN ("set-window-parameter", Fset_window_parameter,
1366 Sset_window_parameter, 3, 3, 0,
1367 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1368 WINDOW defaults to the selected window. Return VALUE. */)
1369 (window, parameter, value)
1370 Lisp_Object window, parameter, value;
1372 register struct window *w = decode_window (window);
1373 Lisp_Object old_alist_elt;
1375 old_alist_elt = Fassq (parameter, w->window_parameters);
1376 if (NILP (old_alist_elt))
1377 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1378 else
1379 Fsetcdr (old_alist_elt, value);
1380 return value;
1384 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1385 0, 1, 0,
1386 doc: /* Return the display-table that WINDOW is using.
1387 WINDOW defaults to the selected window. */)
1388 (window)
1389 Lisp_Object window;
1391 return decode_window (window)->display_table;
1394 /* Get the display table for use on window W. This is either W's
1395 display table or W's buffer's display table. Ignore the specified
1396 tables if they are not valid; if no valid table is specified,
1397 return 0. */
1399 struct Lisp_Char_Table *
1400 window_display_table (w)
1401 struct window *w;
1403 struct Lisp_Char_Table *dp = NULL;
1405 if (DISP_TABLE_P (w->display_table))
1406 dp = XCHAR_TABLE (w->display_table);
1407 else if (BUFFERP (w->buffer))
1409 struct buffer *b = XBUFFER (w->buffer);
1411 if (DISP_TABLE_P (b->display_table))
1412 dp = XCHAR_TABLE (b->display_table);
1413 else if (DISP_TABLE_P (Vstandard_display_table))
1414 dp = XCHAR_TABLE (Vstandard_display_table);
1417 return dp;
1420 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1421 doc: /* Set WINDOW's display-table to TABLE. */)
1422 (window, table)
1423 register Lisp_Object window, table;
1425 register struct window *w;
1427 w = decode_window (window);
1428 w->display_table = table;
1429 return table;
1432 /* Record info on buffer window w is displaying
1433 when it is about to cease to display that buffer. */
1434 static void
1435 unshow_buffer (w)
1436 register struct window *w;
1438 Lisp_Object buf;
1439 struct buffer *b;
1441 buf = w->buffer;
1442 b = XBUFFER (buf);
1443 if (b != XMARKER (w->pointm)->buffer)
1444 abort ();
1446 #if 0
1447 if (w == XWINDOW (selected_window)
1448 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1449 /* Do this except when the selected window's buffer
1450 is being removed from some other window. */
1451 #endif
1452 /* last_window_start records the start position that this buffer
1453 had in the last window to be disconnected from it.
1454 Now that this statement is unconditional,
1455 it is possible for the buffer to be displayed in the
1456 selected window, while last_window_start reflects another
1457 window which was recently showing the same buffer.
1458 Some people might say that might be a good thing. Let's see. */
1459 b->last_window_start = marker_position (w->start);
1461 /* Point in the selected window's buffer
1462 is actually stored in that buffer, and the window's pointm isn't used.
1463 So don't clobber point in that buffer. */
1464 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1465 /* This line helps to fix Horsley's testbug.el bug. */
1466 && !(WINDOWP (b->last_selected_window)
1467 && w != XWINDOW (b->last_selected_window)
1468 && EQ (buf, XWINDOW (b->last_selected_window)->buffer)))
1469 temp_set_point_both (b,
1470 clip_to_bounds (BUF_BEGV (b),
1471 XMARKER (w->pointm)->charpos,
1472 BUF_ZV (b)),
1473 clip_to_bounds (BUF_BEGV_BYTE (b),
1474 marker_byte_position (w->pointm),
1475 BUF_ZV_BYTE (b)));
1477 if (WINDOWP (b->last_selected_window)
1478 && w == XWINDOW (b->last_selected_window))
1479 b->last_selected_window = Qnil;
1482 /* Put replacement into the window structure in place of old. */
1483 static void
1484 replace_window (old, replacement)
1485 Lisp_Object old, replacement;
1487 register Lisp_Object tem;
1488 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1490 /* If OLD is its frame's root_window, then replacement is the new
1491 root_window for that frame. */
1493 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1494 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1496 p->left_col = o->left_col;
1497 p->top_line = o->top_line;
1498 p->total_cols = o->total_cols;
1499 p->total_lines = o->total_lines;
1500 p->desired_matrix = p->current_matrix = 0;
1501 p->vscroll = 0;
1502 bzero (&p->cursor, sizeof (p->cursor));
1503 bzero (&p->last_cursor, sizeof (p->last_cursor));
1504 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
1505 p->phys_cursor_type = -1;
1506 p->phys_cursor_width = -1;
1507 p->must_be_updated_p = 0;
1508 p->pseudo_window_p = 0;
1509 XSETFASTINT (p->window_end_vpos, 0);
1510 XSETFASTINT (p->window_end_pos, 0);
1511 p->window_end_valid = Qnil;
1512 p->frozen_window_start_p = 0;
1513 p->orig_top_line = p->orig_total_lines = Qnil;
1515 p->next = tem = o->next;
1516 if (!NILP (tem))
1517 XWINDOW (tem)->prev = replacement;
1519 p->prev = tem = o->prev;
1520 if (!NILP (tem))
1521 XWINDOW (tem)->next = replacement;
1523 p->parent = tem = o->parent;
1524 if (!NILP (tem))
1526 if (EQ (XWINDOW (tem)->vchild, old))
1527 XWINDOW (tem)->vchild = replacement;
1528 if (EQ (XWINDOW (tem)->hchild, old))
1529 XWINDOW (tem)->hchild = replacement;
1532 /*** Here, if replacement is a vertical combination
1533 and so is its new parent, we should make replacement's
1534 children be children of that parent instead. ***/
1537 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1538 doc: /* Remove WINDOW from its frame.
1539 WINDOW defaults to the selected window. Return nil.
1540 Signal an error when WINDOW is the only window on its frame. */)
1541 (window)
1542 register Lisp_Object window;
1544 struct frame *f;
1545 if (NILP (window))
1546 window = selected_window;
1547 else
1548 CHECK_LIVE_WINDOW (window);
1550 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1551 delete_window (window);
1553 run_window_configuration_change_hook (f);
1555 return Qnil;
1558 void
1559 delete_window (window)
1560 register Lisp_Object window;
1562 register Lisp_Object tem, parent, sib;
1563 register struct window *p;
1564 register struct window *par;
1565 struct frame *f;
1567 /* Because this function is called by other C code on non-leaf
1568 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1569 so we can't decode_window here. */
1570 CHECK_WINDOW (window);
1571 p = XWINDOW (window);
1573 /* It's a no-op to delete an already-deleted window. */
1574 if (NILP (p->buffer)
1575 && NILP (p->hchild)
1576 && NILP (p->vchild))
1577 return;
1579 parent = p->parent;
1580 if (NILP (parent))
1581 error ("Attempt to delete minibuffer or sole ordinary window");
1582 par = XWINDOW (parent);
1584 windows_or_buffers_changed++;
1585 Vwindow_list = Qnil;
1586 f = XFRAME (WINDOW_FRAME (p));
1587 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1589 /* Are we trying to delete any frame's selected window? */
1591 Lisp_Object swindow, pwindow;
1593 /* See if the frame's selected window is either WINDOW
1594 or any subwindow of it, by finding all that window's parents
1595 and comparing each one with WINDOW. */
1596 swindow = FRAME_SELECTED_WINDOW (f);
1598 while (1)
1600 pwindow = swindow;
1601 while (!NILP (pwindow))
1603 if (EQ (window, pwindow))
1604 break;
1605 pwindow = XWINDOW (pwindow)->parent;
1608 /* If the window being deleted is not a parent of SWINDOW,
1609 then SWINDOW is ok as the new selected window. */
1610 if (!EQ (window, pwindow))
1611 break;
1612 /* Otherwise, try another window for SWINDOW. */
1613 swindow = Fnext_window (swindow, Qlambda, Qnil);
1615 /* If we get back to the frame's selected window,
1616 it means there was no acceptable alternative,
1617 so we cannot delete. */
1618 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1619 error ("Cannot delete window");
1622 /* If we need to change SWINDOW, do it. */
1623 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1625 /* If we're about to delete the selected window on the
1626 selected frame, then we should use Fselect_window to select
1627 the new window. On the other hand, if we're about to
1628 delete the selected window on any other frame, we shouldn't do
1629 anything but set the frame's selected_window slot. */
1630 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1631 Fselect_window (swindow, Qnil);
1632 else
1633 FRAME_SELECTED_WINDOW (f) = swindow;
1637 /* Now we know we can delete this one. */
1638 window_deletion_count++;
1640 tem = p->buffer;
1641 /* tem is null for dummy parent windows
1642 (which have inferiors but not any contents themselves) */
1643 if (!NILP (tem))
1645 unshow_buffer (p);
1646 unchain_marker (XMARKER (p->pointm));
1647 unchain_marker (XMARKER (p->start));
1650 /* Free window glyph matrices. It is sure that they are allocated
1651 again when ADJUST_GLYPHS is called. Block input so that expose
1652 events and other events that access glyph matrices are not
1653 processed while we are changing them. */
1654 BLOCK_INPUT;
1655 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1657 tem = p->next;
1658 if (!NILP (tem))
1659 XWINDOW (tem)->prev = p->prev;
1661 tem = p->prev;
1662 if (!NILP (tem))
1663 XWINDOW (tem)->next = p->next;
1665 if (EQ (window, par->hchild))
1666 par->hchild = p->next;
1667 if (EQ (window, par->vchild))
1668 par->vchild = p->next;
1670 /* Find one of our siblings to give our space to. */
1671 sib = p->prev;
1672 if (NILP (sib))
1674 /* If p gives its space to its next sibling, that sibling needs
1675 to have its top/left side pulled back to where p's is.
1676 set_window_{height,width} will re-position the sibling's
1677 children. */
1678 sib = p->next;
1679 XWINDOW (sib)->top_line = p->top_line;
1680 XWINDOW (sib)->left_col = p->left_col;
1683 /* Stretch that sibling. */
1684 if (!NILP (par->vchild))
1685 set_window_height (sib,
1686 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1688 if (!NILP (par->hchild))
1689 set_window_width (sib,
1690 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1693 /* If parent now has only one child,
1694 put the child into the parent's place. */
1695 tem = par->hchild;
1696 if (NILP (tem))
1697 tem = par->vchild;
1698 if (NILP (XWINDOW (tem)->next)) {
1699 replace_window (parent, tem);
1700 par = XWINDOW (tem);
1703 /* Since we may be deleting combination windows, we must make sure that
1704 not only p but all its children have been marked as deleted. */
1705 if (! NILP (p->hchild))
1706 delete_all_subwindows (XWINDOW (p->hchild));
1707 else if (! NILP (p->vchild))
1708 delete_all_subwindows (XWINDOW (p->vchild));
1710 /* Mark this window as deleted. */
1711 p->buffer = p->hchild = p->vchild = Qnil;
1713 if (! NILP (par->parent))
1714 par = XWINDOW (par->parent);
1716 /* Check if we have a v/hchild with a v/hchild. In that case remove
1717 one of them. */
1719 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1721 p = XWINDOW (par->vchild);
1722 par->vchild = p->vchild;
1723 tem = p->vchild;
1725 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1727 p = XWINDOW (par->hchild);
1728 par->hchild = p->hchild;
1729 tem = p->hchild;
1731 else
1732 p = 0;
1734 if (p)
1736 while (! NILP (tem)) {
1737 XWINDOW (tem)->parent = p->parent;
1738 if (NILP (XWINDOW (tem)->next))
1739 break;
1740 tem = XWINDOW (tem)->next;
1742 if (! NILP (tem)) {
1743 /* The next of the v/hchild we are removing is now the next of the
1744 last child for the v/hchild:
1745 Before v/hchild -> v/hchild -> next1 -> next2
1747 -> next3
1748 After: v/hchild -> next1 -> next2 -> next3
1750 XWINDOW (tem)->next = p->next;
1751 if (! NILP (p->next))
1752 XWINDOW (p->next)->prev = tem;
1754 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1758 /* Adjust glyph matrices. */
1759 adjust_glyphs (f);
1760 UNBLOCK_INPUT;
1765 /***********************************************************************
1766 Window List
1767 ***********************************************************************/
1769 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1770 pointer. This is a callback function for foreach_window, used in
1771 function window_list. */
1773 static int
1774 add_window_to_list (w, user_data)
1775 struct window *w;
1776 void *user_data;
1778 Lisp_Object *list = (Lisp_Object *) user_data;
1779 Lisp_Object window;
1780 XSETWINDOW (window, w);
1781 *list = Fcons (window, *list);
1782 return 1;
1786 /* Return a list of all windows, for use by next_window. If
1787 Vwindow_list is a list, return that list. Otherwise, build a new
1788 list, cache it in Vwindow_list, and return that. */
1790 static Lisp_Object
1791 window_list ()
1793 if (!CONSP (Vwindow_list))
1795 Lisp_Object tail;
1797 Vwindow_list = Qnil;
1798 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1800 Lisp_Object args[2];
1802 /* We are visiting windows in canonical order, and add
1803 new windows at the front of args[1], which means we
1804 have to reverse this list at the end. */
1805 args[1] = Qnil;
1806 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1807 args[0] = Vwindow_list;
1808 args[1] = Fnreverse (args[1]);
1809 Vwindow_list = Fnconc (2, args);
1813 return Vwindow_list;
1817 /* Value is non-zero if WINDOW satisfies the constraints given by
1818 OWINDOW, MINIBUF and ALL_FRAMES.
1820 MINIBUF t means WINDOW may be minibuffer windows.
1821 `lambda' means WINDOW may not be a minibuffer window.
1822 a window means a specific minibuffer window
1824 ALL_FRAMES t means search all frames,
1825 nil means search just current frame,
1826 `visible' means search just visible frames,
1827 0 means search visible and iconified frames,
1828 a window means search the frame that window belongs to,
1829 a frame means consider windows on that frame, only. */
1831 static int
1832 candidate_window_p (window, owindow, minibuf, all_frames)
1833 Lisp_Object window, owindow, minibuf, all_frames;
1835 struct window *w = XWINDOW (window);
1836 struct frame *f = XFRAME (w->frame);
1837 int candidate_p = 1;
1839 if (!BUFFERP (w->buffer))
1840 candidate_p = 0;
1841 else if (MINI_WINDOW_P (w)
1842 && (EQ (minibuf, Qlambda)
1843 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1845 /* If MINIBUF is `lambda' don't consider any mini-windows.
1846 If it is a window, consider only that one. */
1847 candidate_p = 0;
1849 else if (EQ (all_frames, Qt))
1850 candidate_p = 1;
1851 else if (NILP (all_frames))
1853 xassert (WINDOWP (owindow));
1854 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1856 else if (EQ (all_frames, Qvisible))
1858 FRAME_SAMPLE_VISIBILITY (f);
1859 candidate_p = FRAME_VISIBLE_P (f)
1860 && (FRAME_TERMINAL (XFRAME (w->frame))
1861 == FRAME_TERMINAL (XFRAME (selected_frame)));
1864 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1866 FRAME_SAMPLE_VISIBILITY (f);
1867 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
1868 #ifdef HAVE_X_WINDOWS
1869 /* Yuck!! If we've just created the frame and the
1870 window-manager requested the user to place it
1871 manually, the window may still not be considered
1872 `visible'. I'd argue it should be at least
1873 something like `iconified', but don't know how to do
1874 that yet. --Stef */
1875 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
1876 && !f->output_data.x->has_been_visible)
1877 #endif
1879 && (FRAME_TERMINAL (XFRAME (w->frame))
1880 == FRAME_TERMINAL (XFRAME (selected_frame)));
1882 else if (WINDOWP (all_frames))
1883 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1884 || EQ (XWINDOW (all_frames)->frame, w->frame)
1885 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1886 else if (FRAMEP (all_frames))
1887 candidate_p = EQ (all_frames, w->frame);
1889 return candidate_p;
1893 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1894 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1895 ALL_FRAMES. */
1897 static void
1898 decode_next_window_args (window, minibuf, all_frames)
1899 Lisp_Object *window, *minibuf, *all_frames;
1901 if (NILP (*window))
1902 *window = selected_window;
1903 else
1904 CHECK_LIVE_WINDOW (*window);
1906 /* MINIBUF nil may or may not include minibuffers. Decide if it
1907 does. */
1908 if (NILP (*minibuf))
1909 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1910 else if (!EQ (*minibuf, Qt))
1911 *minibuf = Qlambda;
1913 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1914 => count none of them, or a specific minibuffer window (the
1915 active one) to count. */
1917 /* ALL_FRAMES nil doesn't specify which frames to include. */
1918 if (NILP (*all_frames))
1919 *all_frames = (!EQ (*minibuf, Qlambda)
1920 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1921 : Qnil);
1922 else if (EQ (*all_frames, Qvisible))
1924 else if (EQ (*all_frames, make_number (0)))
1926 else if (FRAMEP (*all_frames))
1928 else if (!EQ (*all_frames, Qt))
1929 *all_frames = Qnil;
1931 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1932 search just current frame, `visible' meaning search just visible
1933 frames, 0 meaning search visible and iconified frames, or a
1934 window, meaning search the frame that window belongs to, or a
1935 frame, meaning consider windows on that frame, only. */
1939 /* Return the next or previous window of WINDOW in cyclic ordering
1940 of windows. NEXT_P non-zero means return the next window. See the
1941 documentation string of next-window for the meaning of MINIBUF and
1942 ALL_FRAMES. */
1944 static Lisp_Object
1945 next_window (window, minibuf, all_frames, next_p)
1946 Lisp_Object window, minibuf, all_frames;
1947 int next_p;
1949 decode_next_window_args (&window, &minibuf, &all_frames);
1951 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1952 return the first window on the frame. */
1953 if (FRAMEP (all_frames)
1954 && !EQ (all_frames, XWINDOW (window)->frame))
1955 return Fframe_first_window (all_frames);
1957 if (next_p)
1959 Lisp_Object list;
1961 /* Find WINDOW in the list of all windows. */
1962 list = Fmemq (window, window_list ());
1964 /* Scan forward from WINDOW to the end of the window list. */
1965 if (CONSP (list))
1966 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1967 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1968 break;
1970 /* Scan from the start of the window list up to WINDOW. */
1971 if (!CONSP (list))
1972 for (list = Vwindow_list;
1973 CONSP (list) && !EQ (XCAR (list), window);
1974 list = XCDR (list))
1975 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1976 break;
1978 if (CONSP (list))
1979 window = XCAR (list);
1981 else
1983 Lisp_Object candidate, list;
1985 /* Scan through the list of windows for candidates. If there are
1986 candidate windows in front of WINDOW, the last one of these
1987 is the one we want. If there are candidates following WINDOW
1988 in the list, again the last one of these is the one we want. */
1989 candidate = Qnil;
1990 for (list = window_list (); CONSP (list); list = XCDR (list))
1992 if (EQ (XCAR (list), window))
1994 if (WINDOWP (candidate))
1995 break;
1997 else if (candidate_window_p (XCAR (list), window, minibuf,
1998 all_frames))
1999 candidate = XCAR (list);
2002 if (WINDOWP (candidate))
2003 window = candidate;
2006 return window;
2010 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2011 doc: /* Return window following WINDOW in cyclic ordering of windows.
2012 WINDOW defaults to the selected window. The optional arguments
2013 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2015 MINIBUF t means consider the minibuffer window even if the
2016 minibuffer is not active. MINIBUF nil or omitted means consider
2017 the minibuffer window only if the minibuffer is active. Any
2018 other value means do not consider the minibuffer window even if
2019 the minibuffer is active.
2021 Several frames may share a single minibuffer; if the minibuffer
2022 is active, all windows on all frames that share that minibuffer
2023 are considered too. Therefore, if you are using a separate
2024 minibuffer frame and the minibuffer is active and MINIBUF says it
2025 counts, `next-window' considers the windows in the frame from
2026 which you entered the minibuffer, as well as the minibuffer
2027 window.
2029 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
2030 frame, plus the minibuffer window if specified by the MINIBUF
2031 argument, see above. If the minibuffer counts, consider all
2032 windows on all frames that share that minibuffer too.
2033 ALL-FRAMES t means consider all windows on all existing frames.
2034 ALL-FRAMES `visible' means consider all windows on all visible
2035 frames.
2036 ALL-FRAMES 0 means consider all windows on all visible and
2037 iconified frames.
2038 ALL-FRAMES a frame means consider all windows on that frame only.
2039 Anything else means consider all windows on WINDOW's frame and no
2040 others.
2042 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2043 `next-window' to iterate through the entire cycle of acceptable
2044 windows, eventually ending up back at the window you started with.
2045 `previous-window' traverses the same cycle, in the reverse order. */)
2046 (window, minibuf, all_frames)
2047 Lisp_Object window, minibuf, all_frames;
2049 return next_window (window, minibuf, all_frames, 1);
2053 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2054 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
2055 WINDOW defaults to the selected window. The optional arguments
2056 MINIBUF and ALL-FRAMES specify the set of windows to consider.
2057 For the precise meaning of these arguments see `next-window'.
2059 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2060 use `previous-window' to iterate through the entire cycle of
2061 acceptable windows, eventually ending up back at the window you
2062 started with. `next-window' traverses the same cycle, in the
2063 reverse order. */)
2064 (window, minibuf, all_frames)
2065 Lisp_Object window, minibuf, all_frames;
2067 return next_window (window, minibuf, all_frames, 0);
2071 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
2072 doc: /* Select another window in cyclic ordering of windows.
2073 COUNT specifies the number of windows to skip, starting with the
2074 selected window, before making the selection. If COUNT is
2075 positive, skip COUNT windows forwards. If COUNT is negative,
2076 skip -COUNT windows backwards. COUNT zero means do not skip any
2077 window, so select the selected window. In an interactive call,
2078 COUNT is the numeric prefix argument. Return nil.
2080 This function uses `next-window' for finding the window to select.
2081 The argument ALL-FRAMES has the same meaning as in `next-window',
2082 but the MINIBUF argument of `next-window' is always effectively
2083 nil. */)
2084 (count, all_frames)
2085 Lisp_Object count, all_frames;
2087 Lisp_Object window;
2088 int i;
2090 CHECK_NUMBER (count);
2091 window = selected_window;
2093 for (i = XINT (count); i > 0; --i)
2094 window = Fnext_window (window, Qnil, all_frames);
2095 for (; i < 0; ++i)
2096 window = Fprevious_window (window, Qnil, all_frames);
2098 Fselect_window (window, Qnil);
2099 return Qnil;
2103 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2104 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2105 FRAME nil or omitted means use the selected frame.
2106 WINDOW nil or omitted means use the selected window.
2107 MINIBUF t means include the minibuffer window, even if it isn't active.
2108 MINIBUF nil or omitted means include the minibuffer window only
2109 if it's active.
2110 MINIBUF neither nil nor t means never include the minibuffer window. */)
2111 (frame, minibuf, window)
2112 Lisp_Object frame, minibuf, window;
2114 if (NILP (window))
2115 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2116 CHECK_WINDOW (window);
2117 if (NILP (frame))
2118 frame = selected_frame;
2120 if (!EQ (frame, XWINDOW (window)->frame))
2121 error ("Window is on a different frame");
2123 return window_list_1 (window, minibuf, frame);
2127 /* Return a list of windows in cyclic ordering. Arguments are like
2128 for `next-window'. */
2130 static Lisp_Object
2131 window_list_1 (window, minibuf, all_frames)
2132 Lisp_Object window, minibuf, all_frames;
2134 Lisp_Object tail, list, rest;
2136 decode_next_window_args (&window, &minibuf, &all_frames);
2137 list = Qnil;
2139 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2140 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2141 list = Fcons (XCAR (tail), list);
2143 /* Rotate the list to start with WINDOW. */
2144 list = Fnreverse (list);
2145 rest = Fmemq (window, list);
2146 if (!NILP (rest) && !EQ (rest, list))
2148 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2150 XSETCDR (tail, Qnil);
2151 list = nconc2 (rest, list);
2153 return list;
2158 /* Look at all windows, performing an operation specified by TYPE
2159 with argument OBJ.
2160 If FRAMES is Qt, look at all frames;
2161 Qnil, look at just the selected frame;
2162 Qvisible, look at visible frames;
2163 a frame, just look at windows on that frame.
2164 If MINI is non-zero, perform the operation on minibuffer windows too. */
2166 enum window_loop
2168 WINDOW_LOOP_UNUSED,
2169 GET_BUFFER_WINDOW, /* Arg is buffer */
2170 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2171 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2172 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2173 GET_LARGEST_WINDOW,
2174 UNSHOW_BUFFER, /* Arg is buffer */
2175 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2176 CHECK_ALL_WINDOWS
2179 static Lisp_Object
2180 window_loop (type, obj, mini, frames)
2181 enum window_loop type;
2182 Lisp_Object obj, frames;
2183 int mini;
2185 Lisp_Object window, windows, best_window, frame_arg;
2186 struct frame *f;
2187 struct gcpro gcpro1;
2189 /* If we're only looping through windows on a particular frame,
2190 frame points to that frame. If we're looping through windows
2191 on all frames, frame is 0. */
2192 if (FRAMEP (frames))
2193 f = XFRAME (frames);
2194 else if (NILP (frames))
2195 f = SELECTED_FRAME ();
2196 else
2197 f = NULL;
2199 if (f)
2200 frame_arg = Qlambda;
2201 else if (EQ (frames, make_number (0)))
2202 frame_arg = frames;
2203 else if (EQ (frames, Qvisible))
2204 frame_arg = frames;
2205 else
2206 frame_arg = Qt;
2208 /* frame_arg is Qlambda to stick to one frame,
2209 Qvisible to consider all visible frames,
2210 or Qt otherwise. */
2212 /* Pick a window to start with. */
2213 if (WINDOWP (obj))
2214 window = obj;
2215 else if (f)
2216 window = FRAME_SELECTED_WINDOW (f);
2217 else
2218 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2220 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2221 GCPRO1 (windows);
2222 best_window = Qnil;
2224 for (; CONSP (windows); windows = XCDR (windows))
2226 struct window *w;
2228 window = XCAR (windows);
2229 w = XWINDOW (window);
2231 /* Note that we do not pay attention here to whether the frame
2232 is visible, since Fwindow_list skips non-visible frames if
2233 that is desired, under the control of frame_arg. */
2234 if (!MINI_WINDOW_P (w)
2235 /* For UNSHOW_BUFFER, we must always consider all windows. */
2236 || type == UNSHOW_BUFFER
2237 || (mini && minibuf_level > 0))
2238 switch (type)
2240 case GET_BUFFER_WINDOW:
2241 if (EQ (w->buffer, obj)
2242 /* Don't find any minibuffer window
2243 except the one that is currently in use. */
2244 && (MINI_WINDOW_P (w)
2245 ? EQ (window, minibuf_window)
2246 : 1))
2248 if (NILP (best_window))
2249 best_window = window;
2250 else if (EQ (window, selected_window))
2251 /* Prefer to return selected-window. */
2252 RETURN_UNGCPRO (window);
2253 else if (EQ (Fwindow_frame (window), selected_frame))
2254 /* Prefer windows on the current frame. */
2255 best_window = window;
2257 break;
2259 case GET_LRU_WINDOW:
2260 /* `obj' is an integer encoding a bitvector.
2261 `obj & 1' means consider only full-width windows.
2262 `obj & 2' means consider also dedicated windows. */
2263 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2264 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2265 /* Minibuffer windows are always ignored. */
2266 || MINI_WINDOW_P (w))
2267 break;
2268 if (NILP (best_window)
2269 || (XFASTINT (XWINDOW (best_window)->use_time)
2270 > XFASTINT (w->use_time)))
2271 best_window = window;
2272 break;
2274 case DELETE_OTHER_WINDOWS:
2275 if (!EQ (window, obj))
2276 Fdelete_window (window);
2277 break;
2279 case DELETE_BUFFER_WINDOWS:
2280 if (EQ (w->buffer, obj))
2282 struct frame *f = XFRAME (WINDOW_FRAME (w));
2284 /* If this window is dedicated, and in a frame of its own,
2285 kill the frame. */
2286 if (EQ (window, FRAME_ROOT_WINDOW (f))
2287 && !NILP (w->dedicated)
2288 && other_visible_frames (f))
2290 /* Skip the other windows on this frame.
2291 There might be one, the minibuffer! */
2292 while (CONSP (XCDR (windows))
2293 && EQ (XWINDOW (XCAR (windows))->frame,
2294 XWINDOW (XCAR (XCDR (windows)))->frame))
2295 windows = XCDR (windows);
2297 /* Now we can safely delete the frame. */
2298 delete_frame (w->frame, Qnil);
2300 else if (NILP (w->parent))
2302 /* If we're deleting the buffer displayed in the
2303 only window on the frame, find a new buffer to
2304 display there. */
2305 Lisp_Object buffer;
2306 buffer = Fother_buffer (obj, Qnil, w->frame);
2307 /* Reset dedicated state of window. */
2308 w->dedicated = Qnil;
2309 Fset_window_buffer (window, buffer, Qnil);
2310 if (EQ (window, selected_window))
2311 Fset_buffer (w->buffer);
2313 else
2314 Fdelete_window (window);
2316 break;
2318 case GET_LARGEST_WINDOW:
2319 { /* nil `obj' means to ignore dedicated windows. */
2320 /* Ignore dedicated windows and minibuffers. */
2321 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2322 break;
2324 if (NILP (best_window))
2325 best_window = window;
2326 else
2328 struct window *b = XWINDOW (best_window);
2329 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2330 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2331 best_window = window;
2334 break;
2336 case UNSHOW_BUFFER:
2337 if (EQ (w->buffer, obj))
2339 Lisp_Object buffer;
2340 struct frame *f = XFRAME (w->frame);
2342 /* Find another buffer to show in this window. */
2343 buffer = Fother_buffer (obj, Qnil, w->frame);
2345 /* If this window is dedicated, and in a frame of its own,
2346 kill the frame. */
2347 if (EQ (window, FRAME_ROOT_WINDOW (f))
2348 && !NILP (w->dedicated)
2349 && other_visible_frames (f))
2351 /* Skip the other windows on this frame.
2352 There might be one, the minibuffer! */
2353 while (CONSP (XCDR (windows))
2354 && EQ (XWINDOW (XCAR (windows))->frame,
2355 XWINDOW (XCAR (XCDR (windows)))->frame))
2356 windows = XCDR (windows);
2358 /* Now we can safely delete the frame. */
2359 delete_frame (w->frame, Qnil);
2361 else if (!NILP (w->dedicated) && !NILP (w->parent))
2363 Lisp_Object window;
2364 XSETWINDOW (window, w);
2365 /* If this window is dedicated and not the only window
2366 in its frame, then kill it. */
2367 Fdelete_window (window);
2369 else
2371 /* Otherwise show a different buffer in the window. */
2372 w->dedicated = Qnil;
2373 Fset_window_buffer (window, buffer, Qnil);
2374 if (EQ (window, selected_window))
2375 Fset_buffer (w->buffer);
2378 break;
2380 case REDISPLAY_BUFFER_WINDOWS:
2381 if (EQ (w->buffer, obj))
2383 mark_window_display_accurate (window, 0);
2384 w->update_mode_line = Qt;
2385 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2386 ++update_mode_lines;
2387 best_window = window;
2389 break;
2391 /* Check for a window that has a killed buffer. */
2392 case CHECK_ALL_WINDOWS:
2393 if (! NILP (w->buffer)
2394 && NILP (XBUFFER (w->buffer)->name))
2395 abort ();
2396 break;
2398 case WINDOW_LOOP_UNUSED:
2399 break;
2403 UNGCPRO;
2404 return best_window;
2407 /* Used for debugging. Abort if any window has a dead buffer. */
2409 void
2410 check_all_windows ()
2412 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2415 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2416 doc: /* Return the window least recently selected or used for display.
2417 \(LRU means Least Recently Used.)
2419 Return a full-width window if possible.
2420 A minibuffer window is never a candidate.
2421 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2422 so if all windows are dedicated, the value is nil.
2423 If optional argument FRAME is `visible', search all visible frames.
2424 If FRAME is 0, search all visible and iconified frames.
2425 If FRAME is t, search all frames.
2426 If FRAME is nil, search only the selected frame.
2427 If FRAME is a frame, search only that frame. */)
2428 (frame, dedicated)
2429 Lisp_Object frame, dedicated;
2431 register Lisp_Object w;
2432 /* First try for a window that is full-width */
2433 w = window_loop (GET_LRU_WINDOW,
2434 NILP (dedicated) ? make_number (1) : make_number (3),
2435 0, frame);
2436 if (!NILP (w) && !EQ (w, selected_window))
2437 return w;
2438 /* If none of them, try the rest */
2439 return window_loop (GET_LRU_WINDOW,
2440 NILP (dedicated) ? make_number (0) : make_number (2),
2441 0, frame);
2444 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2445 doc: /* Return the largest window in area.
2446 A minibuffer window is never a candidate.
2447 A dedicated window is never a candidate unless DEDICATED is non-nil,
2448 so if all windows are dedicated, the value is nil.
2449 If optional argument FRAME is `visible', search all visible frames.
2450 If FRAME is 0, search all visible and iconified frames.
2451 If FRAME is t, search all frames.
2452 If FRAME is nil, search only the selected frame.
2453 If FRAME is a frame, search only that frame. */)
2454 (frame, dedicated)
2455 Lisp_Object frame, dedicated;
2457 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2458 frame);
2461 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2462 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2463 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to the
2464 current buffer.
2465 If optional argument FRAME is `visible', search all visible frames.
2466 If optional argument FRAME is 0, search all visible and iconified frames.
2467 If FRAME is t, search all frames.
2468 If FRAME is nil, search only the selected frame.
2469 If FRAME is a frame, search only that frame. */)
2470 (buffer_or_name, frame)
2471 Lisp_Object buffer_or_name, frame;
2473 Lisp_Object buffer;
2475 if (NILP (buffer_or_name))
2476 buffer = Fcurrent_buffer ();
2477 else
2478 buffer = Fget_buffer (buffer_or_name);
2480 if (BUFFERP (buffer))
2481 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2482 else
2483 return Qnil;
2486 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2487 0, 1, "",
2488 doc: /* Make WINDOW (or the selected window) fill its frame.
2489 Only the frame WINDOW is on is affected.
2490 This function tries to reduce display jumps by keeping the text
2491 previously visible in WINDOW in the same place on the frame. Doing this
2492 depends on the value of (window-start WINDOW), so if calling this
2493 function in a program gives strange scrolling, make sure the
2494 window-start value is reasonable when this function is called. */)
2495 (window)
2496 Lisp_Object window;
2498 struct window *w;
2499 int startpos;
2500 int top, new_top;
2502 if (NILP (window))
2503 window = selected_window;
2504 else
2505 CHECK_LIVE_WINDOW (window);
2506 w = XWINDOW (window);
2508 startpos = marker_position (w->start);
2509 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2511 if (MINI_WINDOW_P (w) && top > 0)
2512 error ("Can't expand minibuffer to full frame");
2514 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2516 /* Try to minimize scrolling, by setting the window start to the point
2517 will cause the text at the old window start to be at the same place
2518 on the frame. But don't try to do this if the window start is
2519 outside the visible portion (as might happen when the display is
2520 not current, due to typeahead). */
2521 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2522 if (new_top != top
2523 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2524 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2526 struct position pos;
2527 struct buffer *obuf = current_buffer;
2529 Fset_buffer (w->buffer);
2530 /* This computation used to temporarily move point, but that can
2531 have unwanted side effects due to text properties. */
2532 pos = *vmotion (startpos, -top, w);
2534 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2535 w->window_end_valid = Qnil;
2536 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2537 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2538 : Qnil);
2539 /* We need to do this, so that the window-scroll-functions
2540 get called. */
2541 w->optional_new_start = Qt;
2543 set_buffer_internal (obuf);
2546 return Qnil;
2549 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2550 0, 2, "bDelete windows on (buffer): ",
2551 doc: /* Delete all windows showing BUFFER-OR-NAME.
2552 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2553 defaults to the current buffer.
2555 Optional second argument FRAME controls which frames are affected.
2556 If optional argument FRAME is `visible', search all visible frames.
2557 If FRAME is 0, search all visible and iconified frames.
2558 If FRAME is nil, search all frames.
2559 If FRAME is t, search only the selected frame.
2560 If FRAME is a frame, search only that frame.
2561 When a window showing BUFFER-OR-NAME is dedicated and the only window of
2562 its frame, that frame is deleted when there are other frames left. */)
2563 (buffer_or_name, frame)
2564 Lisp_Object buffer_or_name, frame;
2566 Lisp_Object buffer;
2568 /* FRAME uses t and nil to mean the opposite of what window_loop
2569 expects. */
2570 if (NILP (frame))
2571 frame = Qt;
2572 else if (EQ (frame, Qt))
2573 frame = Qnil;
2575 if (NILP (buffer_or_name))
2576 buffer = Fcurrent_buffer ();
2577 else
2579 buffer = Fget_buffer (buffer_or_name);
2580 CHECK_BUFFER (buffer);
2583 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2585 return Qnil;
2588 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2589 Sreplace_buffer_in_windows,
2590 0, 1, "bReplace buffer in windows: ",
2591 doc: /* Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2592 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2593 defaults to the current buffer.
2595 When a window showing BUFFER-OR-NAME is dedicated that window is
2596 deleted. If that window is the only window on its frame, that frame is
2597 deleted too when there are other frames left. If there are no other
2598 frames left, some other buffer is displayed in that window. */)
2599 (buffer_or_name)
2600 Lisp_Object buffer_or_name;
2602 Lisp_Object buffer;
2604 if (NILP (buffer_or_name))
2605 buffer = Fcurrent_buffer ();
2606 else
2608 buffer = Fget_buffer (buffer_or_name);
2609 CHECK_BUFFER (buffer);
2612 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2614 return Qnil;
2617 /* Replace BUFFER with some other buffer in all windows
2618 of all frames, even those on other keyboards. */
2620 void
2621 replace_buffer_in_all_windows (buffer)
2622 Lisp_Object buffer;
2624 Lisp_Object tail, frame;
2626 /* A single call to window_loop won't do the job
2627 because it only considers frames on the current keyboard.
2628 So loop manually over frames, and handle each one. */
2629 FOR_EACH_FRAME (tail, frame)
2630 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2633 /* Set the height of WINDOW and all its inferiors. */
2635 /* The smallest acceptable dimensions for a window. Anything smaller
2636 might crash Emacs. */
2638 #define MIN_SAFE_WINDOW_WIDTH (2)
2639 #define MIN_SAFE_WINDOW_HEIGHT (1)
2641 /* For wp non-zero the total number of columns of window w. Otherwise
2642 the total number of lines of w. */
2644 #define WINDOW_TOTAL_SIZE(w, wp) \
2645 (wp ? WINDOW_TOTAL_COLS (w) : WINDOW_TOTAL_LINES (w))
2647 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2648 minimum allowable size. */
2650 void
2651 check_frame_size (frame, rows, cols)
2652 FRAME_PTR frame;
2653 int *rows, *cols;
2655 /* For height, we have to see:
2656 how many windows the frame has at minimum (one or two),
2657 and whether it has a menu bar or other special stuff at the top. */
2658 int min_height
2659 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2660 ? MIN_SAFE_WINDOW_HEIGHT
2661 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2663 if (FRAME_TOP_MARGIN (frame) > 0)
2664 min_height += FRAME_TOP_MARGIN (frame);
2666 if (*rows < min_height)
2667 *rows = min_height;
2668 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2669 *cols = MIN_SAFE_WINDOW_WIDTH;
2672 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2673 check if W's width can be changed, otherwise check W's height.
2674 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2675 siblings, too. If none of the siblings is resizable, WINDOW isn't
2676 either. */
2678 static int
2679 window_fixed_size_p (w, width_p, check_siblings_p)
2680 struct window *w;
2681 int width_p, check_siblings_p;
2683 int fixed_p;
2684 struct window *c;
2686 if (!NILP (w->hchild))
2688 c = XWINDOW (w->hchild);
2690 if (width_p)
2692 /* A horizontal combination is fixed-width if all of if its
2693 children are. */
2694 while (c && window_fixed_size_p (c, width_p, 0))
2695 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2696 fixed_p = c == NULL;
2698 else
2700 /* A horizontal combination is fixed-height if one of if its
2701 children is. */
2702 while (c && !window_fixed_size_p (c, width_p, 0))
2703 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2704 fixed_p = c != NULL;
2707 else if (!NILP (w->vchild))
2709 c = XWINDOW (w->vchild);
2711 if (width_p)
2713 /* A vertical combination is fixed-width if one of if its
2714 children is. */
2715 while (c && !window_fixed_size_p (c, width_p, 0))
2716 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2717 fixed_p = c != NULL;
2719 else
2721 /* A vertical combination is fixed-height if all of if its
2722 children are. */
2723 while (c && window_fixed_size_p (c, width_p, 0))
2724 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2725 fixed_p = c == NULL;
2728 else if (BUFFERP (w->buffer))
2730 struct buffer *old = current_buffer;
2731 Lisp_Object val;
2733 current_buffer = XBUFFER (w->buffer);
2734 val = find_symbol_value (Qwindow_size_fixed);
2735 current_buffer = old;
2737 fixed_p = 0;
2738 if (!EQ (val, Qunbound))
2740 fixed_p = !NILP (val);
2742 if (fixed_p
2743 && ((EQ (val, Qheight) && width_p)
2744 || (EQ (val, Qwidth) && !width_p)))
2745 fixed_p = 0;
2748 /* Can't tell if this one is resizable without looking at
2749 siblings. If all siblings are fixed-size this one is too. */
2750 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2752 Lisp_Object child;
2754 for (child = w->prev; WINDOWP (child); child = XWINDOW (child)->prev)
2755 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2756 break;
2758 if (NILP (child))
2759 for (child = w->next; WINDOWP (child); child = XWINDOW (child)->next)
2760 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2761 break;
2763 if (NILP (child))
2764 fixed_p = 1;
2767 else
2768 fixed_p = 1;
2770 return fixed_p;
2773 /* Return minimum size of leaf window W. WIDTH_P non-zero means return
2774 the minimum width of W, WIDTH_P zero means return the minimum height
2775 of W. SAFE_P non-zero means ignore window-min-height|width but just
2776 return values that won't crash Emacs and don't hide components like
2777 fringes, scrollbars, or modelines. If WIDTH_P is zero and W is the
2778 minibuffer window, always return 1. */
2780 static int
2781 window_min_size_2 (w, width_p, safe_p)
2782 struct window *w;
2783 int width_p, safe_p;
2785 /* We should consider buffer-local values of window_min_height and
2786 window_min_width here. */
2787 if (width_p)
2789 int safe_size = (MIN_SAFE_WINDOW_WIDTH
2790 + WINDOW_FRINGE_COLS (w)
2791 + WINDOW_SCROLL_BAR_COLS (w));
2793 return safe_p ? safe_size : max (window_min_width, safe_size);
2795 else if (MINI_WINDOW_P (w))
2796 return 1;
2797 else
2799 int safe_size = (MIN_SAFE_WINDOW_HEIGHT
2800 + ((BUFFERP (w->buffer)
2801 && !NILP (XBUFFER (w->buffer)->mode_line_format))
2802 ? 1 : 0));
2804 return safe_p ? safe_size : max (window_min_height, safe_size);
2808 /* Return minimum size of window W, not taking fixed-width windows into
2809 account. WIDTH_P non-zero means return the minimum width, otherwise
2810 return the minimum height. SAFE_P non-zero means ignore
2811 window-min-height|width but just return values that won't crash Emacs
2812 and don't hide components like fringes, scrollbars, or modelines. If
2813 W is a combination window, compute the minimum size from the minimum
2814 sizes of W's children. */
2816 static int
2817 window_min_size_1 (w, width_p, safe_p)
2818 struct window *w;
2819 int width_p, safe_p;
2821 struct window *c;
2822 int size;
2824 if (!NILP (w->hchild))
2826 /* W is a horizontal combination. */
2827 c = XWINDOW (w->hchild);
2828 size = 0;
2830 if (width_p)
2832 /* The minimum width of a horizontal combination is the sum of
2833 the minimum widths of its children. */
2834 while (c)
2836 size += window_min_size_1 (c, 1, safe_p);
2837 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2840 else
2842 /* The minimum height of a horizontal combination is the
2843 maximum of the minimum heights of its children. */
2844 while (c)
2846 size = max (window_min_size_1 (c, 0, safe_p), size);
2847 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2851 else if (!NILP (w->vchild))
2853 /* W is a vertical combination. */
2854 c = XWINDOW (w->vchild);
2855 size = 0;
2857 if (width_p)
2859 /* The minimum width of a vertical combination is the maximum
2860 of the minimum widths of its children. */
2861 while (c)
2863 size = max (window_min_size_1 (c, 1, safe_p), size);
2864 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2867 else
2869 /* The minimum height of a vertical combination is the sum of
2870 the minimum height of its children. */
2871 while (c)
2873 size += window_min_size_1 (c, 0, safe_p);
2874 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2878 else
2879 /* W is a leaf window. */
2880 size = window_min_size_2 (w, width_p, safe_p);
2882 return size;
2885 /* Return the minimum size of window W, taking fixed-size windows into
2886 account. WIDTH_P non-zero means return the minimum width, otherwise
2887 return the minimum height. SAFE_P non-zero means ignore
2888 window-min-height|width but just return values that won't crash Emacs
2889 and don't hide components like fringes, scrollbars, or modelines.
2890 IGNORE_FIXED_P non-zero means ignore if W is fixed-size. Set *FIXED
2891 to 1 if W is fixed-size unless FIXED is null. */
2893 static int
2894 window_min_size (w, width_p, safe_p, ignore_fixed_p, fixed)
2895 struct window *w;
2896 int width_p, safe_p, ignore_fixed_p, *fixed;
2898 int size, fixed_p;
2900 if (ignore_fixed_p)
2901 fixed_p = 0;
2902 else
2903 fixed_p = window_fixed_size_p (w, width_p, 1);
2905 if (fixed)
2906 *fixed = fixed_p;
2908 if (fixed_p)
2909 size = WINDOW_TOTAL_SIZE (w, width_p);
2910 else
2911 size = window_min_size_1 (w, width_p, safe_p);
2913 return size;
2917 /* Adjust the margins of window W if text area is too small.
2918 Return 1 if window width is ok after adjustment; 0 if window
2919 is still too narrow. */
2921 static int
2922 adjust_window_margins (w)
2923 struct window *w;
2925 int box_cols = (WINDOW_TOTAL_COLS (w)
2926 - WINDOW_FRINGE_COLS (w)
2927 - WINDOW_SCROLL_BAR_COLS (w));
2928 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2929 + WINDOW_RIGHT_MARGIN_COLS (w));
2931 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2932 return 1;
2934 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2935 return 0;
2937 /* Window's text area is too narrow, but reducing the window
2938 margins will fix that. */
2939 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2940 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2942 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2943 w->left_margin_cols = w->right_margin_cols
2944 = make_number (margin_cols/2);
2945 else
2946 w->right_margin_cols = make_number (margin_cols);
2948 else
2949 w->left_margin_cols = make_number (margin_cols);
2950 return 1;
2953 /* Calculate new sizes for windows in the list FORWARD when their
2954 compound size goes from TOTAL to SIZE. TOTAL must be greater than
2955 SIZE. The number of windows in FORWARD is NCHILDREN, and the number
2956 that can shrink is SHRINKABLE. Fixed-size windows may be shrunk if
2957 and only if RESIZE_FIXED_P is non-zero. WIDTH_P non-zero means
2958 shrink columns, otherwise shrink lines.
2960 SAFE_P zero means windows may be sized down to window-min-height
2961 lines (window-min-window columns for WIDTH_P non-zero). SAFE_P
2962 non-zero means windows may be sized down to their minimum safe sizes
2963 taking into account the space needed to display modelines, fringes,
2964 and scrollbars.
2966 This function returns an allocated array of new sizes that the caller
2967 must free. A size -1 means the window is fixed and RESIZE_FIXED_P is
2968 zero. A size zero means the window shall be deleted. Array index 0
2969 refers to the first window in FORWARD, 1 to the second, and so on.
2971 This function resizes windows proportionally to their size. It also
2972 tries to preserve smaller windows by resizing larger windows before
2973 resizing any window to zero. If resize_proportionally is non-nil for
2974 a specific window, it will attempt to strictly resize that window
2975 proportionally, even at the expense of deleting smaller windows. */
2976 static int *
2977 shrink_windows (total, size, nchildren, shrinkable, resize_fixed_p,
2978 forward, width_p, safe_p)
2979 int total, size, nchildren, shrinkable;
2980 int resize_fixed_p, width_p, safe_p;
2981 Lisp_Object forward;
2983 int available_resize = 0;
2984 int *new_sizes, *min_sizes;
2985 struct window *c;
2986 Lisp_Object child;
2987 int smallest = total;
2988 int total_removed = 0;
2989 int total_shrink = total - size;
2990 int i;
2992 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2993 min_sizes = xmalloc (sizeof (*min_sizes) * nchildren);
2995 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2997 int child_size;
2999 c = XWINDOW (child);
3000 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3002 if (!resize_fixed_p && window_fixed_size_p (c, width_p, 0))
3003 new_sizes[i] = -1;
3004 else
3006 new_sizes[i] = child_size;
3007 min_sizes[i] = window_min_size_1 (c, width_p, safe_p);
3008 if (child_size > min_sizes[i]
3009 && NILP (c->resize_proportionally))
3010 available_resize += child_size - min_sizes[i];
3013 /* We might need to shrink some windows to zero. Find the smallest
3014 windows and set them to 0 until we can fulfil the new size. */
3016 while (shrinkable > 1 && size + available_resize < total)
3018 for (i = 0; i < nchildren; ++i)
3019 if (new_sizes[i] > 0 && smallest > new_sizes[i])
3020 smallest = new_sizes[i];
3022 for (i = 0; i < nchildren; ++i)
3023 if (new_sizes[i] == smallest)
3025 /* Resize this window down to zero. */
3026 new_sizes[i] = 0;
3027 if (smallest > min_sizes[i])
3028 available_resize -= smallest - min_sizes[i];
3029 available_resize += smallest;
3030 --shrinkable;
3031 total_removed += smallest;
3033 /* We don't know what the smallest is now. */
3034 smallest = total;
3036 /* Out of for, just remove one window at the time and
3037 check again if we have enough space. */
3038 break;
3042 /* Now, calculate the new sizes. Try to shrink each window
3043 proportional to its size. */
3044 for (i = 0; i < nchildren; ++i)
3046 if (new_sizes[i] > min_sizes[i])
3048 int to_shrink = total_shrink * new_sizes[i] / total;
3050 if (new_sizes[i] - to_shrink < min_sizes[i])
3051 to_shrink = new_sizes[i] - min_sizes[i];
3052 new_sizes[i] -= to_shrink;
3053 total_removed += to_shrink;
3057 /* Any reminder due to rounding, we just subtract from windows
3058 that are left and still can be shrunk. */
3059 while (total_shrink > total_removed)
3061 int nonzero_sizes = 0;
3062 int nonzero_idx = -1;
3064 for (i = 0; i < nchildren; ++i)
3065 if (new_sizes[i] > 0)
3067 ++nonzero_sizes;
3068 nonzero_idx = i;
3071 for (i = 0; i < nchildren; ++i)
3072 if (new_sizes[i] > min_sizes[i])
3074 --new_sizes[i];
3075 ++total_removed;
3077 /* Out of for, just shrink one window at the time and
3078 check again if we have enough space. */
3079 break;
3082 /* Special case, only one window left. */
3083 if (nonzero_sizes == 1)
3084 break;
3087 /* Any surplus due to rounding, we add to windows that are left. */
3088 while (total_shrink < total_removed)
3090 for (i = 0; i < nchildren; ++i)
3092 if (new_sizes[i] != 0 && total_shrink < total_removed)
3094 ++new_sizes[i];
3095 --total_removed;
3096 break;
3101 xfree (min_sizes);
3103 return new_sizes;
3106 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
3107 WINDOW's width. Resize WINDOW's children, if any, so that they keep
3108 their proportionate size relative to WINDOW.
3110 If FIRST_ONLY is 1, change only the first of WINDOW's children when
3111 they are in series. If LAST_ONLY is 1, change only the last of
3112 WINDOW's children when they are in series.
3114 Propagate WINDOW's top or left edge position to children. Delete
3115 windows that become too small unless NODELETE_P is 1. When
3116 NODELETE_P equals 2 do not honor settings for window-min-height and
3117 window-min-width when resizing windows but use safe defaults instead.
3118 This should give better behavior when resizing frames. */
3120 static void
3121 size_window (window, size, width_p, nodelete_p, first_only, last_only)
3122 Lisp_Object window;
3123 int size, width_p, nodelete_p;
3124 int first_only, last_only;
3126 struct window *w = XWINDOW (window);
3127 struct window *c;
3128 Lisp_Object child, *forward, *sideward;
3129 int old_size = WINDOW_TOTAL_SIZE (w, width_p);
3131 size = max (0, size);
3133 /* Delete WINDOW if it's too small. */
3134 if (nodelete_p != 1 && !NILP (w->parent)
3135 && size < window_min_size_1 (w, width_p, nodelete_p == 2))
3137 delete_window (window);
3138 return;
3141 /* Set redisplay hints. */
3142 w->last_modified = make_number (0);
3143 w->last_overlay_modified = make_number (0);
3144 windows_or_buffers_changed++;
3145 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
3147 if (width_p)
3149 sideward = &w->vchild;
3150 forward = &w->hchild;
3151 w->total_cols = make_number (size);
3152 adjust_window_margins (w);
3154 else
3156 sideward = &w->hchild;
3157 forward = &w->vchild;
3158 w->total_lines = make_number (size);
3159 w->orig_total_lines = Qnil;
3162 if (!NILP (*sideward))
3164 /* We have a chain of parallel siblings whose size should all change. */
3165 for (child = *sideward; !NILP (child); child = c->next)
3167 c = XWINDOW (child);
3168 if (width_p)
3169 c->left_col = w->left_col;
3170 else
3171 c->top_line = w->top_line;
3172 size_window (child, size, width_p, nodelete_p,
3173 first_only, last_only);
3176 else if (!NILP (*forward) && last_only)
3178 /* Change the last in a series of siblings. */
3179 Lisp_Object last_child;
3180 int child_size;
3182 for (child = *forward; !NILP (child); child = c->next)
3184 c = XWINDOW (child);
3185 last_child = child;
3188 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3189 size_window (last_child, size - old_size + child_size,
3190 width_p, nodelete_p, first_only, last_only);
3192 else if (!NILP (*forward) && first_only)
3194 /* Change the first in a series of siblings. */
3195 int child_size;
3197 child = *forward;
3198 c = XWINDOW (child);
3200 if (width_p)
3201 c->left_col = w->left_col;
3202 else
3203 c->top_line = w->top_line;
3205 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3206 size_window (child, size - old_size + child_size,
3207 width_p, nodelete_p, first_only, last_only);
3209 else if (!NILP (*forward))
3211 int fixed_size, each, extra, n;
3212 int resize_fixed_p, nfixed;
3213 int last_pos, first_pos, nchildren, total;
3214 int *new_sizes = NULL;
3216 /* Determine the fixed-size portion of this window, and the
3217 number of child windows. */
3218 fixed_size = nchildren = nfixed = total = 0;
3219 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
3221 int child_size;
3223 c = XWINDOW (child);
3224 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3225 total += child_size;
3227 if (window_fixed_size_p (c, width_p, 0))
3229 fixed_size += child_size;
3230 ++nfixed;
3234 /* If the new size is smaller than fixed_size, or if there
3235 aren't any resizable windows, allow resizing fixed-size
3236 windows. */
3237 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3239 /* Compute how many lines/columns to add/remove to each child. The
3240 value of extra takes care of rounding errors. */
3241 n = resize_fixed_p ? nchildren : nchildren - nfixed;
3242 if (size < total && n > 1)
3243 new_sizes = shrink_windows (total, size, nchildren, n,
3244 resize_fixed_p, *forward, width_p,
3245 nodelete_p == 2);
3246 else
3248 each = (size - total) / n;
3249 extra = (size - total) - n * each;
3252 /* Compute new children heights and edge positions. */
3253 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
3254 last_pos = first_pos;
3255 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
3257 int new_size, old_size;
3259 c = XWINDOW (child);
3260 old_size = WINDOW_TOTAL_SIZE (c, width_p);
3261 new_size = old_size;
3263 /* The top or left edge position of this child equals the
3264 bottom or right edge of its predecessor. */
3265 if (width_p)
3266 c->left_col = make_number (last_pos);
3267 else
3268 c->top_line = make_number (last_pos);
3270 /* If this child can be resized, do it. */
3271 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3273 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
3274 extra = 0;
3277 /* Set new size. Note that size_window also propagates
3278 edge positions to children, so it's not a no-op if we
3279 didn't change the child's size. */
3280 size_window (child, new_size, width_p, 1, first_only, last_only);
3282 /* Remember the bottom/right edge position of this child; it
3283 will be used to set the top/left edge of the next child. */
3284 last_pos += new_size;
3287 xfree (new_sizes);
3289 /* We should have covered the parent exactly with child windows. */
3290 xassert (size == last_pos - first_pos);
3292 /* Now delete any children that became too small. */
3293 if (nodelete_p != 1)
3294 for (child = *forward; !NILP (child); child = c->next)
3296 int child_size;
3298 c = XWINDOW (child);
3299 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3300 size_window (child, child_size, width_p, nodelete_p,
3301 first_only, last_only);
3306 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3307 WINDOW's children. NODELETE zero means windows that have become
3308 smaller than window-min-height in the process may be deleted.
3309 NODELETE 1 means never delete windows that become too small in the
3310 process. (The caller should check later and do so if appropriate.)
3311 NODELETE 2 means delete only windows that have become too small to be
3312 displayed correctly. */
3314 void
3315 set_window_height (window, height, nodelete)
3316 Lisp_Object window;
3317 int height;
3318 int nodelete;
3320 size_window (window, height, 0, nodelete, 0, 0);
3323 /* Set WINDOW's width to WIDTH, and recursively change the width of
3324 WINDOW's children. NODELETE zero means windows that have become
3325 smaller than window-min-width in the process may be deleted.
3326 NODELETE 1 means never delete windows that become too small in the
3327 process. (The caller should check later and do so if appropriate.)
3328 NODELETE 2 means delete only windows that have become too small to be
3329 displayed correctly. */
3331 void
3332 set_window_width (window, width, nodelete)
3333 Lisp_Object window;
3334 int width;
3335 int nodelete;
3337 size_window (window, width, 1, nodelete, 0, 0);
3340 /* Change window heights in windows rooted in WINDOW by N lines. */
3342 void
3343 change_window_heights (window, n)
3344 Lisp_Object window;
3345 int n;
3347 struct window *w = XWINDOW (window);
3349 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3350 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3352 if (INTEGERP (w->orig_top_line))
3353 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3354 if (INTEGERP (w->orig_total_lines))
3355 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3357 /* Handle just the top child in a vertical split. */
3358 if (!NILP (w->vchild))
3359 change_window_heights (w->vchild, n);
3361 /* Adjust all children in a horizontal split. */
3362 for (window = w->hchild; !NILP (window); window = w->next)
3364 w = XWINDOW (window);
3365 change_window_heights (window, n);
3370 int window_select_count;
3372 EXFUN (Fset_window_fringes, 4);
3373 EXFUN (Fset_window_scroll_bars, 4);
3375 static void
3376 run_funs (Lisp_Object funs)
3378 for (; CONSP (funs); funs = XCDR (funs))
3379 if (!EQ (XCAR (funs), Qt))
3380 call0 (XCAR (funs));
3383 static Lisp_Object select_window_norecord (Lisp_Object window);
3384 static Lisp_Object select_frame_norecord (Lisp_Object frame);
3386 void
3387 run_window_configuration_change_hook (struct frame *f)
3389 int count = SPECPDL_INDEX ();
3390 Lisp_Object frame, global_wcch
3391 = Fdefault_value (Qwindow_configuration_change_hook);
3392 XSETFRAME (frame, f);
3394 if (NILP (Vrun_hooks))
3395 return;
3397 if (SELECTED_FRAME () != f)
3399 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3400 Fselect_frame (frame, Qt);
3403 /* Use the right buffer. Matters when running the local hooks. */
3404 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3406 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3407 Fset_buffer (Fwindow_buffer (Qnil));
3410 /* Look for buffer-local values. */
3412 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3413 for (; CONSP (windows); windows = XCDR (windows))
3415 Lisp_Object window = XCAR (windows);
3416 Lisp_Object buffer = Fwindow_buffer (window);
3417 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3418 buffer)))
3420 int count = SPECPDL_INDEX ();
3421 record_unwind_protect (select_window_norecord, Fselected_window ());
3422 select_window_norecord (window);
3423 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3424 buffer));
3425 unbind_to (count, Qnil);
3430 run_funs (global_wcch);
3431 unbind_to (count, Qnil);
3434 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3435 means it's allowed to run hooks. See make_frame for a case where
3436 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3437 margins, fringes, and scroll-bar settings of the window are not
3438 reset from the buffer's local settings. */
3440 void
3441 set_window_buffer (window, buffer, run_hooks_p, keep_margins_p)
3442 Lisp_Object window, buffer;
3443 int run_hooks_p, keep_margins_p;
3445 struct window *w = XWINDOW (window);
3446 struct buffer *b = XBUFFER (buffer);
3447 int count = SPECPDL_INDEX ();
3448 int samebuf = EQ (buffer, w->buffer);
3450 w->buffer = buffer;
3452 if (EQ (window, selected_window))
3453 b->last_selected_window = window;
3455 /* Let redisplay errors through. */
3456 b->display_error_modiff = 0;
3458 /* Update time stamps of buffer display. */
3459 if (INTEGERP (b->display_count))
3460 XSETINT (b->display_count, XINT (b->display_count) + 1);
3461 b->display_time = Fcurrent_time ();
3463 XSETFASTINT (w->window_end_pos, 0);
3464 XSETFASTINT (w->window_end_vpos, 0);
3465 bzero (&w->last_cursor, sizeof w->last_cursor);
3466 w->window_end_valid = Qnil;
3467 if (!(keep_margins_p && samebuf))
3468 { /* If we're not actually changing the buffer, don't reset hscroll and
3469 vscroll. This case happens for example when called from
3470 change_frame_size_1, where we use a dummy call to
3471 Fset_window_buffer on the frame's selected window (and no other)
3472 just in order to run window-configuration-change-hook.
3473 Resetting hscroll and vscroll here is problematic for things like
3474 image-mode and doc-view-mode since it resets the image's position
3475 whenever we resize the frame. */
3476 w->hscroll = w->min_hscroll = make_number (0);
3477 w->vscroll = 0;
3478 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3479 set_marker_restricted (w->start,
3480 make_number (b->last_window_start),
3481 buffer);
3482 w->start_at_line_beg = Qnil;
3483 w->force_start = Qnil;
3484 XSETFASTINT (w->last_modified, 0);
3485 XSETFASTINT (w->last_overlay_modified, 0);
3487 /* Maybe we could move this into the `if' but it's not obviously safe and
3488 I doubt it's worth the trouble. */
3489 windows_or_buffers_changed++;
3491 /* We must select BUFFER for running the window-scroll-functions. */
3492 /* We can't check ! NILP (Vwindow_scroll_functions) here
3493 because that might itself be a local variable. */
3494 if (window_initialized)
3496 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3497 Fset_buffer (buffer);
3500 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3502 if (!keep_margins_p)
3504 /* Set left and right marginal area width etc. from buffer. */
3506 /* This may call adjust_window_margins three times, so
3507 temporarily disable window margins. */
3508 Lisp_Object save_left = w->left_margin_cols;
3509 Lisp_Object save_right = w->right_margin_cols;
3511 w->left_margin_cols = w->right_margin_cols = Qnil;
3513 Fset_window_fringes (window,
3514 b->left_fringe_width, b->right_fringe_width,
3515 b->fringes_outside_margins);
3517 Fset_window_scroll_bars (window,
3518 b->scroll_bar_width,
3519 b->vertical_scroll_bar_type, Qnil);
3521 w->left_margin_cols = save_left;
3522 w->right_margin_cols = save_right;
3524 Fset_window_margins (window,
3525 b->left_margin_cols, b->right_margin_cols);
3528 if (run_hooks_p)
3530 if (! NILP (Vwindow_scroll_functions))
3531 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3532 Fmarker_position (w->start));
3533 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3536 unbind_to (count, Qnil);
3540 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3541 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3542 WINDOW defaults to the selected window. BUFFER-OR-NAME must be a buffer
3543 or the name of an existing buffer. Optional third argument KEEP-MARGINS
3544 non-nil means that WINDOW's current display margins, fringe widths, and
3545 scroll bar settings are preserved; the default is to reset these from
3546 the local settings for BUFFER-OR-NAME or the frame defaults. Return nil.
3548 This function throws an error when WINDOW is strongly dedicated to its
3549 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3550 already display BUFFER-OR-NAME.
3552 This function runs `window-scroll-functions' before running
3553 `window-configuration-change-hook'. */)
3554 (window, buffer_or_name, keep_margins)
3555 register Lisp_Object window, buffer_or_name, keep_margins;
3557 register Lisp_Object tem, buffer;
3558 register struct window *w = decode_window (window);
3560 XSETWINDOW (window, w);
3561 buffer = Fget_buffer (buffer_or_name);
3562 CHECK_BUFFER (buffer);
3563 if (NILP (XBUFFER (buffer)->name))
3564 error ("Attempt to display deleted buffer");
3566 tem = w->buffer;
3567 if (NILP (tem))
3568 error ("Window is deleted");
3569 else if (!EQ (tem, Qt))
3570 /* w->buffer is t when the window is first being set up. */
3572 if (!EQ (tem, buffer))
3573 if (EQ (w->dedicated, Qt))
3574 error ("Window is dedicated to `%s'", SDATA (XBUFFER (tem)->name));
3575 else
3576 w->dedicated = Qnil;
3578 unshow_buffer (w);
3581 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3582 return Qnil;
3585 /* Note that selected_window can be nil when this is called from
3586 Fset_window_configuration. */
3588 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3589 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3590 If WINDOW is not already selected, make WINDOW's buffer current
3591 and make WINDOW the frame's selected window. Return WINDOW.
3592 Optional second arg NORECORD non-nil means do not put this buffer
3593 at the front of the list of recently selected ones and do not
3594 make this window the most recently selected one.
3596 Note that the main editor command loop selects the buffer of the
3597 selected window before each command. */)
3598 (window, norecord)
3599 register Lisp_Object window, norecord;
3601 register struct window *w;
3602 register struct window *ow;
3603 struct frame *sf;
3605 CHECK_LIVE_WINDOW (window);
3607 w = XWINDOW (window);
3608 w->frozen_window_start_p = 0;
3610 if (NILP (norecord))
3612 ++window_select_count;
3613 XSETFASTINT (w->use_time, window_select_count);
3616 if (EQ (window, selected_window))
3617 return window;
3619 sf = SELECTED_FRAME ();
3620 if (XFRAME (WINDOW_FRAME (w)) != sf)
3622 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3623 /* Use this rather than Fhandle_switch_frame
3624 so that FRAME_FOCUS_FRAME is moved appropriately as we
3625 move around in the state where a minibuffer in a separate
3626 frame is active. */
3627 Fselect_frame (WINDOW_FRAME (w), norecord);
3628 /* Fselect_frame called us back so we've done all the work already. */
3629 eassert (EQ (window, selected_window));
3630 return window;
3632 else
3633 sf->selected_window = window;
3635 /* Store the current buffer's actual point into the
3636 old selected window. It belongs to that window,
3637 and when the window is not selected, must be in the window. */
3638 if (!NILP (selected_window))
3640 ow = XWINDOW (selected_window);
3641 if (! NILP (ow->buffer))
3642 set_marker_both (ow->pointm, ow->buffer,
3643 BUF_PT (XBUFFER (ow->buffer)),
3644 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3647 selected_window = window;
3649 if (NILP (norecord))
3650 record_buffer (w->buffer);
3651 Fset_buffer (w->buffer);
3653 XBUFFER (w->buffer)->last_selected_window = window;
3655 /* Go to the point recorded in the window.
3656 This is important when the buffer is in more
3657 than one window. It also matters when
3658 redisplay_window has altered point after scrolling,
3659 because it makes the change only in the window. */
3661 register int new_point = marker_position (w->pointm);
3662 if (new_point < BEGV)
3663 SET_PT (BEGV);
3664 else if (new_point > ZV)
3665 SET_PT (ZV);
3666 else
3667 SET_PT (new_point);
3670 windows_or_buffers_changed++;
3671 return window;
3674 static Lisp_Object
3675 select_window_norecord (window)
3676 Lisp_Object window;
3678 return WINDOW_LIVE_P (window)
3679 ? Fselect_window (window, Qt) : selected_window;
3682 static Lisp_Object
3683 select_frame_norecord (frame)
3684 Lisp_Object frame;
3686 return FRAME_LIVE_P (XFRAME (frame))
3687 ? Fselect_frame (frame, Qt) : selected_frame;
3690 Lisp_Object
3691 display_buffer (buffer, not_this_window_p, override_frame)
3692 Lisp_Object buffer, not_this_window_p, override_frame;
3694 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3697 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3698 0, 1, 0,
3699 doc: /* Force all windows to be updated on next redisplay.
3700 If optional arg OBJECT is a window, force redisplay of that window only.
3701 If OBJECT is a buffer or buffer name, force redisplay of all windows
3702 displaying that buffer. */)
3703 (object)
3704 Lisp_Object object;
3706 if (NILP (object))
3708 windows_or_buffers_changed++;
3709 update_mode_lines++;
3710 return Qt;
3713 if (WINDOWP (object))
3715 struct window *w = XWINDOW (object);
3716 mark_window_display_accurate (object, 0);
3717 w->update_mode_line = Qt;
3718 if (BUFFERP (w->buffer))
3719 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3720 ++update_mode_lines;
3721 return Qt;
3724 if (STRINGP (object))
3725 object = Fget_buffer (object);
3726 if (BUFFERP (object) && !NILP (XBUFFER (object)->name))
3728 /* Walk all windows looking for buffer, and force update
3729 of each of those windows. */
3731 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3732 return NILP (object) ? Qnil : Qt;
3735 /* If nothing suitable was found, just return.
3736 We could signal an error, but this feature will typically be used
3737 asynchronously in timers or process sentinels, so we don't. */
3738 return Qnil;
3742 void
3743 temp_output_buffer_show (buf)
3744 register Lisp_Object buf;
3746 register struct buffer *old = current_buffer;
3747 register Lisp_Object window;
3748 register struct window *w;
3750 XBUFFER (buf)->directory = current_buffer->directory;
3752 Fset_buffer (buf);
3753 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3754 BEGV = BEG;
3755 ZV = Z;
3756 SET_PT (BEG);
3757 #if 0 /* rms: there should be no reason for this. */
3758 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3759 #endif
3760 set_buffer_internal (old);
3762 if (!NILP (Vtemp_buffer_show_function))
3763 call1 (Vtemp_buffer_show_function, buf);
3764 else
3766 window = display_buffer (buf, Qnil, Qnil);
3768 if (!EQ (XWINDOW (window)->frame, selected_frame))
3769 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3770 Vminibuf_scroll_window = window;
3771 w = XWINDOW (window);
3772 XSETFASTINT (w->hscroll, 0);
3773 XSETFASTINT (w->min_hscroll, 0);
3774 set_marker_restricted_both (w->start, buf, BEG, BEG);
3775 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3777 /* Run temp-buffer-show-hook, with the chosen window selected
3778 and its buffer current. */
3780 if (!NILP (Vrun_hooks)
3781 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3782 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3784 int count = SPECPDL_INDEX ();
3785 Lisp_Object prev_window, prev_buffer;
3786 prev_window = selected_window;
3787 XSETBUFFER (prev_buffer, old);
3789 /* Select the window that was chosen, for running the hook.
3790 Note: Both Fselect_window and select_window_norecord may
3791 set-buffer to the buffer displayed in the window,
3792 so we need to save the current buffer. --stef */
3793 record_unwind_protect (Fset_buffer, prev_buffer);
3794 record_unwind_protect (select_window_norecord, prev_window);
3795 Fselect_window (window, Qt);
3796 Fset_buffer (w->buffer);
3797 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3798 unbind_to (count, Qnil);
3803 static void
3804 make_dummy_parent (window)
3805 Lisp_Object window;
3807 Lisp_Object new;
3808 register struct window *o, *p;
3809 int i;
3811 o = XWINDOW (window);
3812 p = allocate_window ();
3813 for (i = 0; i < VECSIZE (struct window); ++i)
3814 ((struct Lisp_Vector *) p)->contents[i]
3815 = ((struct Lisp_Vector *)o)->contents[i];
3816 XSETWINDOW (new, p);
3818 ++sequence_number;
3819 XSETFASTINT (p->sequence_number, sequence_number);
3821 /* Put new into window structure in place of window */
3822 replace_window (window, new);
3824 o->next = Qnil;
3825 o->prev = Qnil;
3826 o->vchild = Qnil;
3827 o->hchild = Qnil;
3828 o->parent = new;
3830 p->start = Qnil;
3831 p->pointm = Qnil;
3832 p->buffer = Qnil;
3835 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3836 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3837 WINDOW defaults to selected one and SIZE to half its size.
3838 If optional third arg HORIZONTAL is non-nil, split side by side and put
3839 SIZE columns in the first of the pair. In that case, SIZE includes that
3840 window's scroll bar, or the divider column to its right.
3841 Interactively, all arguments are nil.
3842 Returns the newly created window (which is the lower or rightmost one).
3843 The upper or leftmost window is the original one, and remains selected
3844 if it was selected before.
3846 See Info node `(elisp)Splitting Windows' for more details and examples. */)
3847 (window, size, horizontal)
3848 Lisp_Object window, size, horizontal;
3850 register Lisp_Object new;
3851 register struct window *o, *p;
3852 FRAME_PTR fo;
3853 register int size_int;
3855 if (NILP (window))
3856 window = selected_window;
3857 else
3858 CHECK_LIVE_WINDOW (window);
3860 o = XWINDOW (window);
3861 fo = XFRAME (WINDOW_FRAME (o));
3863 if (NILP (size))
3865 if (!NILP (horizontal))
3866 /* Calculate the size of the left-hand window, by dividing
3867 the usable space in columns by two.
3868 We round up, since the left-hand window may include
3869 a dividing line, while the right-hand may not. */
3870 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3871 else
3872 size_int = XFASTINT (o->total_lines) >> 1;
3874 else
3876 CHECK_NUMBER (size);
3877 size_int = XINT (size);
3880 if (MINI_WINDOW_P (o))
3881 error ("Attempt to split minibuffer window");
3882 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
3883 error ("Attempt to split fixed-size window");
3885 if (NILP (horizontal))
3887 int window_safe_height = window_min_size_2 (o, 0, 0);
3889 if (size_int < window_safe_height)
3890 error ("Window height %d too small (after splitting)", size_int);
3891 if (size_int + window_safe_height > XFASTINT (o->total_lines))
3892 error ("Window height %d too small (after splitting)",
3893 XFASTINT (o->total_lines) - size_int);
3894 if (NILP (o->parent)
3895 || NILP (XWINDOW (o->parent)->vchild))
3897 make_dummy_parent (window);
3898 new = o->parent;
3899 XWINDOW (new)->vchild = window;
3902 else
3904 int window_safe_width = window_min_size_2 (o, 1, 0);
3906 if (size_int < window_safe_width)
3907 error ("Window width %d too small (after splitting)", size_int);
3908 if (size_int + window_safe_width > XFASTINT (o->total_cols))
3909 error ("Window width %d too small (after splitting)",
3910 XFASTINT (o->total_cols) - size_int);
3911 if (NILP (o->parent)
3912 || NILP (XWINDOW (o->parent)->hchild))
3914 make_dummy_parent (window);
3915 new = o->parent;
3916 XWINDOW (new)->hchild = window;
3920 /* Now we know that window's parent is a vertical combination
3921 if we are dividing vertically, or a horizontal combination
3922 if we are making side-by-side windows */
3924 windows_or_buffers_changed++;
3925 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3926 new = make_window ();
3927 p = XWINDOW (new);
3929 p->frame = o->frame;
3930 p->next = o->next;
3931 if (!NILP (p->next))
3932 XWINDOW (p->next)->prev = new;
3933 p->prev = window;
3934 o->next = new;
3935 p->parent = o->parent;
3936 p->buffer = Qt;
3937 p->window_end_valid = Qnil;
3938 bzero (&p->last_cursor, sizeof p->last_cursor);
3940 /* Duplicate special geometry settings. */
3942 p->left_margin_cols = o->left_margin_cols;
3943 p->right_margin_cols = o->right_margin_cols;
3944 p->left_fringe_width = o->left_fringe_width;
3945 p->right_fringe_width = o->right_fringe_width;
3946 p->fringes_outside_margins = o->fringes_outside_margins;
3947 p->scroll_bar_width = o->scroll_bar_width;
3948 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3950 /* Apportion the available frame space among the two new windows */
3952 if (!NILP (horizontal))
3954 p->total_lines = o->total_lines;
3955 p->top_line = o->top_line;
3956 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3957 XSETFASTINT (o->total_cols, size_int);
3958 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3959 adjust_window_margins (p);
3960 adjust_window_margins (o);
3962 else
3964 p->left_col = o->left_col;
3965 p->total_cols = o->total_cols;
3966 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3967 XSETFASTINT (o->total_lines, size_int);
3968 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3971 /* Adjust glyph matrices. */
3972 adjust_glyphs (fo);
3974 Fset_window_buffer (new, o->buffer, Qt);
3975 return new;
3978 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3979 doc: /* Make selected window SIZE lines taller.
3980 Interactively, if no argument is given, make the selected window one
3981 line taller. If optional argument HORIZONTAL is non-nil, make selected
3982 window wider by SIZE columns. If SIZE is negative, shrink the window by
3983 -SIZE lines or columns. Return nil.
3985 This function can delete windows if they get too small. The size of
3986 fixed size windows is not altered by this function. */)
3987 (size, horizontal)
3988 Lisp_Object size, horizontal;
3990 CHECK_NUMBER (size);
3991 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
3993 run_window_configuration_change_hook (SELECTED_FRAME ());
3995 return Qnil;
3998 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3999 doc: /* Make selected window SIZE lines smaller.
4000 Interactively, if no argument is given, make the selected window one
4001 line smaller. If optional argument HORIZONTAL is non-nil, make the
4002 window narrower by SIZE columns. If SIZE is negative, enlarge selected
4003 window by -SIZE lines or columns. Return nil.
4005 This function can delete windows if they get too small. The size of
4006 fixed size windows is not altered by this function. */)
4007 (size, horizontal)
4008 Lisp_Object size, horizontal;
4010 CHECK_NUMBER (size);
4011 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
4013 run_window_configuration_change_hook (SELECTED_FRAME ());
4015 return Qnil;
4019 window_height (window)
4020 Lisp_Object window;
4022 register struct window *p = XWINDOW (window);
4023 return WINDOW_TOTAL_LINES (p);
4027 window_width (window)
4028 Lisp_Object window;
4030 register struct window *p = XWINDOW (window);
4031 return WINDOW_TOTAL_COLS (p);
4035 #define CURBEG(w) \
4036 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
4038 #define CURSIZE(w) \
4039 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
4042 /* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
4043 horizontally; zero means do it vertically.
4045 Siblings of the selected window are resized to fulfill the size
4046 request. If they become too small in the process, they may be
4047 deleted. */
4049 static void
4050 enlarge_window (window, delta, horiz_flag)
4051 Lisp_Object window;
4052 int delta, horiz_flag;
4054 Lisp_Object parent, next, prev;
4055 struct window *p;
4056 Lisp_Object *sizep;
4057 int maximum;
4058 int (*sizefun) P_ ((Lisp_Object))
4059 = horiz_flag ? window_width : window_height;
4060 void (*setsizefun) P_ ((Lisp_Object, int, int))
4061 = (horiz_flag ? set_window_width : set_window_height);
4063 /* Give up if this window cannot be resized. */
4064 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4065 error ("Window is not resizable");
4067 /* Find the parent of the selected window. */
4068 while (1)
4070 p = XWINDOW (window);
4071 parent = p->parent;
4073 if (NILP (parent))
4075 if (horiz_flag)
4076 error ("No other window to side of this one");
4077 break;
4080 if (horiz_flag
4081 ? !NILP (XWINDOW (parent)->hchild)
4082 : !NILP (XWINDOW (parent)->vchild))
4083 break;
4085 window = parent;
4088 sizep = &CURSIZE (window);
4091 register int maxdelta;
4093 /* Compute the maximum size increment this window can have. */
4095 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4096 /* This is a main window followed by a minibuffer. */
4097 : !NILP (p->next) ? ((*sizefun) (p->next)
4098 - window_min_size (XWINDOW (p->next),
4099 horiz_flag, 0, 0, 0))
4100 /* This is a minibuffer following a main window. */
4101 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4102 - window_min_size (XWINDOW (p->prev),
4103 horiz_flag, 0, 0, 0))
4104 /* This is a frame with only one window, a minibuffer-only
4105 or a minibufferless frame. */
4106 : (delta = 0));
4108 if (delta > maxdelta)
4109 /* This case traps trying to make the minibuffer
4110 the full frame, or make the only window aside from the
4111 minibuffer the full frame. */
4112 delta = maxdelta;
4115 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4116 horiz_flag, 0, 0, 0))
4118 delete_window (window);
4119 return;
4122 if (delta == 0)
4123 return;
4125 /* Find the total we can get from other siblings without deleting them. */
4126 maximum = 0;
4127 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
4128 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4129 horiz_flag, 0, 0, 0);
4130 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
4131 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4132 horiz_flag, 0, 0, 0);
4134 /* If we can get it all from them without deleting them, do so. */
4135 if (delta <= maximum)
4137 Lisp_Object first_unaffected;
4138 Lisp_Object first_affected;
4139 int fixed_p;
4141 next = p->next;
4142 prev = p->prev;
4143 first_affected = window;
4144 /* Look at one sibling at a time,
4145 moving away from this window in both directions alternately,
4146 and take as much as we can get without deleting that sibling. */
4147 while (delta != 0
4148 && (!NILP (next) || !NILP (prev)))
4150 if (! NILP (next))
4152 int this_one = ((*sizefun) (next)
4153 - window_min_size (XWINDOW (next), horiz_flag,
4154 0, 0, &fixed_p));
4155 if (!fixed_p)
4157 if (this_one > delta)
4158 this_one = delta;
4160 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4161 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4163 delta -= this_one;
4166 next = XWINDOW (next)->next;
4169 if (delta == 0)
4170 break;
4172 if (! NILP (prev))
4174 int this_one = ((*sizefun) (prev)
4175 - window_min_size (XWINDOW (prev), horiz_flag,
4176 0, 0, &fixed_p));
4177 if (!fixed_p)
4179 if (this_one > delta)
4180 this_one = delta;
4182 first_affected = prev;
4184 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4185 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4187 delta -= this_one;
4190 prev = XWINDOW (prev)->prev;
4194 xassert (delta == 0);
4196 /* Now recalculate the edge positions of all the windows affected,
4197 based on the new sizes. */
4198 first_unaffected = next;
4199 prev = first_affected;
4200 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4201 prev = next, next = XWINDOW (next)->next)
4203 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4204 /* This does not change size of NEXT,
4205 but it propagates the new top edge to its children */
4206 (*setsizefun) (next, (*sizefun) (next), 0);
4209 else
4211 register int delta1;
4212 register int opht = (*sizefun) (parent);
4214 if (opht <= XINT (*sizep) + delta)
4216 /* If trying to grow this window to or beyond size of the parent,
4217 just delete all the sibling windows. */
4218 Lisp_Object start, tem, next;
4220 start = XWINDOW (parent)->vchild;
4221 if (NILP (start))
4222 start = XWINDOW (parent)->hchild;
4224 /* Delete any siblings that come after WINDOW. */
4225 tem = XWINDOW (window)->next;
4226 while (! NILP (tem))
4228 next = XWINDOW (tem)->next;
4229 delete_window (tem);
4230 tem = next;
4233 /* Delete any siblings that come after WINDOW.
4234 Note that if START is not WINDOW, then WINDOW still
4235 has siblings, so WINDOW has not yet replaced its parent. */
4236 tem = start;
4237 while (! EQ (tem, window))
4239 next = XWINDOW (tem)->next;
4240 delete_window (tem);
4241 tem = next;
4244 else
4246 /* Otherwise, make delta1 just right so that if we add
4247 delta1 lines to this window and to the parent, and then
4248 shrink the parent back to its original size, the new
4249 proportional size of this window will increase by delta.
4251 The function size_window will compute the new height h'
4252 of the window from delta1 as:
4254 e = delta1/n
4255 x = delta1 - delta1/n * n for the 1st resizable child
4256 h' = h + e + x
4258 where n is the number of children that can be resized.
4259 We can ignore x by choosing a delta1 that is a multiple of
4260 n. We want the height of this window to come out as
4262 h' = h + delta
4264 So, delta1 must be
4266 h + e = h + delta
4267 delta1/n = delta
4268 delta1 = n * delta.
4270 The number of children n equals the number of resizable
4271 children of this window + 1 because we know window itself
4272 is resizable (otherwise we would have signaled an error).
4274 This reasoning is not correct when other windows become too
4275 small and shrink_windows refuses to delete them. Below we
4276 use resize_proportionally to work around this problem. */
4278 struct window *w = XWINDOW (window);
4279 Lisp_Object s;
4280 int n = 1;
4282 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
4283 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4284 ++n;
4285 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
4286 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4287 ++n;
4289 delta1 = n * delta;
4291 /* Add delta1 lines or columns to this window, and to the parent,
4292 keeping things consistent while not affecting siblings. */
4293 XSETINT (CURSIZE (parent), opht + delta1);
4294 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4296 /* Squeeze out delta1 lines or columns from our parent,
4297 shrinking this window and siblings proportionately. This
4298 brings parent back to correct size. Delta1 was calculated
4299 so this makes this window the desired size, taking it all
4300 out of the siblings.
4302 Temporarily set resize_proportionally to Qt to assure that,
4303 if necessary, shrink_windows deletes smaller windows rather
4304 than shrink this window. */
4305 w->resize_proportionally = Qt;
4306 (*setsizefun) (parent, opht, 0);
4307 w->resize_proportionally = Qnil;
4311 XSETFASTINT (p->last_modified, 0);
4312 XSETFASTINT (p->last_overlay_modified, 0);
4314 /* Adjust glyph matrices. */
4315 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4319 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4320 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4321 zero means adjust the height, moving the bottom edge.
4323 Following siblings of the selected window are resized to fulfill
4324 the size request. If they become too small in the process, they
4325 are not deleted; instead, we signal an error. */
4327 static void
4328 adjust_window_trailing_edge (window, delta, horiz_flag)
4329 Lisp_Object window;
4330 int delta, horiz_flag;
4332 Lisp_Object parent, child;
4333 struct window *p;
4334 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4335 int delcount = window_deletion_count;
4337 CHECK_WINDOW (window);
4339 /* Give up if this window cannot be resized. */
4340 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4341 error ("Window is not resizable");
4343 while (1)
4345 Lisp_Object first_parallel = Qnil;
4347 if (NILP (window))
4349 /* This happens if WINDOW on the previous iteration was
4350 at top level of the window tree. */
4351 Fset_window_configuration (old_config);
4352 error ("Specified window edge is fixed");
4355 p = XWINDOW (window);
4356 parent = p->parent;
4358 /* See if this level has windows in parallel in the specified
4359 direction. If so, set FIRST_PARALLEL to the first one. */
4360 if (horiz_flag)
4362 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4363 first_parallel = XWINDOW (parent)->vchild;
4364 else if (NILP (parent) && !NILP (p->next))
4366 /* Handle the vertical chain of main window and minibuffer
4367 which has no parent. */
4368 first_parallel = window;
4369 while (! NILP (XWINDOW (first_parallel)->prev))
4370 first_parallel = XWINDOW (first_parallel)->prev;
4373 else
4375 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4376 first_parallel = XWINDOW (parent)->hchild;
4379 /* If this level's succession is in the desired dimension,
4380 and this window is the last one, and there is no higher level,
4381 its trailing edge is fixed. */
4382 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4383 && NILP (parent))
4385 Fset_window_configuration (old_config);
4386 error ("Specified window edge is fixed");
4389 /* Don't make this window too small. */
4390 if (XINT (CURSIZE (window)) + delta
4391 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
4393 Fset_window_configuration (old_config);
4394 error ("Cannot adjust window size as specified");
4397 /* Clear out some redisplay caches. */
4398 XSETFASTINT (p->last_modified, 0);
4399 XSETFASTINT (p->last_overlay_modified, 0);
4401 /* Adjust this window's edge. */
4402 XSETINT (CURSIZE (window),
4403 XINT (CURSIZE (window)) + delta);
4405 /* If this window has following siblings in the desired dimension,
4406 make them smaller, and exit the loop.
4408 (If we reach the top of the tree and can never do this,
4409 we will fail and report an error, above.) */
4410 if (NILP (first_parallel))
4412 if (!NILP (p->next))
4414 /* This may happen for the minibuffer. In that case
4415 the window_deletion_count check below does not work. */
4416 if (XINT (CURSIZE (p->next)) - delta <= 0)
4418 Fset_window_configuration (old_config);
4419 error ("Cannot adjust window size as specified");
4422 XSETINT (CURBEG (p->next),
4423 XINT (CURBEG (p->next)) + delta);
4424 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4425 horiz_flag, 0, 1, 0);
4426 break;
4429 else
4430 /* Here we have a chain of parallel siblings, in the other dimension.
4431 Change the size of the other siblings. */
4432 for (child = first_parallel;
4433 ! NILP (child);
4434 child = XWINDOW (child)->next)
4435 if (! EQ (child, window))
4436 size_window (child, XINT (CURSIZE (child)) + delta,
4437 horiz_flag, 0, 0, 1);
4439 window = parent;
4442 /* If we made a window so small it got deleted,
4443 we failed. Report failure. */
4444 if (delcount != window_deletion_count)
4446 Fset_window_configuration (old_config);
4447 error ("Cannot adjust window size as specified");
4450 /* Adjust glyph matrices. */
4451 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4454 #undef CURBEG
4455 #undef CURSIZE
4457 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4458 Sadjust_window_trailing_edge, 3, 3, 0,
4459 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4460 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4461 Otherwise, adjust the height, moving the bottom edge.
4463 Following siblings of the selected window are resized to fulfill
4464 the size request. If they become too small in the process, they
4465 are not deleted; instead, we signal an error. */)
4466 (window, delta, horizontal)
4467 Lisp_Object window, delta, horizontal;
4469 CHECK_NUMBER (delta);
4470 if (NILP (window))
4471 window = selected_window;
4472 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4474 run_window_configuration_change_hook
4475 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4477 return Qnil;
4482 /***********************************************************************
4483 Resizing Mini-Windows
4484 ***********************************************************************/
4486 static void shrink_window_lowest_first P_ ((struct window *, int));
4488 enum save_restore_action
4490 CHECK_ORIG_SIZES,
4491 SAVE_ORIG_SIZES,
4492 RESTORE_ORIG_SIZES
4495 static int save_restore_orig_size P_ ((struct window *,
4496 enum save_restore_action));
4498 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4499 from lowest windows first. */
4501 static void
4502 shrink_window_lowest_first (w, height)
4503 struct window *w;
4504 int height;
4506 struct window *c;
4507 Lisp_Object child;
4508 int old_height;
4510 xassert (!MINI_WINDOW_P (w));
4512 /* Set redisplay hints. */
4513 XSETFASTINT (w->last_modified, 0);
4514 XSETFASTINT (w->last_overlay_modified, 0);
4515 windows_or_buffers_changed++;
4516 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4518 old_height = XFASTINT (w->total_lines);
4519 XSETFASTINT (w->total_lines, height);
4521 if (!NILP (w->hchild))
4523 for (child = w->hchild; !NILP (child); child = c->next)
4525 c = XWINDOW (child);
4526 c->top_line = w->top_line;
4527 shrink_window_lowest_first (c, height);
4530 else if (!NILP (w->vchild))
4532 Lisp_Object last_child;
4533 int delta = old_height - height;
4534 int last_top;
4536 last_child = Qnil;
4538 /* Find the last child. We are taking space from lowest windows
4539 first, so we iterate over children from the last child
4540 backwards. */
4541 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
4542 last_child = child;
4544 /* Size children down to their safe heights. */
4545 for (child = last_child; delta && !NILP (child); child = c->prev)
4547 int this_one;
4549 c = XWINDOW (child);
4550 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
4552 if (this_one > delta)
4553 this_one = delta;
4555 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4556 delta -= this_one;
4559 /* Compute new positions. */
4560 last_top = XINT (w->top_line);
4561 for (child = w->vchild; !NILP (child); child = c->next)
4563 c = XWINDOW (child);
4564 c->top_line = make_number (last_top);
4565 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4566 last_top += XFASTINT (c->total_lines);
4572 /* Save, restore, or check positions and sizes in the window tree
4573 rooted at W. ACTION says what to do.
4575 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4576 orig_total_lines members are valid for all windows in the window
4577 tree. Value is non-zero if they are valid.
4579 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4580 orig_top_line and orig_total_lines for all windows in the tree.
4582 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4583 stored in orig_top_line and orig_total_lines for all windows. */
4585 static int
4586 save_restore_orig_size (w, action)
4587 struct window *w;
4588 enum save_restore_action action;
4590 int success_p = 1;
4592 while (w)
4594 if (!NILP (w->hchild))
4596 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4597 success_p = 0;
4599 else if (!NILP (w->vchild))
4601 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4602 success_p = 0;
4605 switch (action)
4607 case CHECK_ORIG_SIZES:
4608 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4609 return 0;
4610 break;
4612 case SAVE_ORIG_SIZES:
4613 w->orig_top_line = w->top_line;
4614 w->orig_total_lines = w->total_lines;
4615 XSETFASTINT (w->last_modified, 0);
4616 XSETFASTINT (w->last_overlay_modified, 0);
4617 break;
4619 case RESTORE_ORIG_SIZES:
4620 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4621 w->top_line = w->orig_top_line;
4622 w->total_lines = w->orig_total_lines;
4623 w->orig_total_lines = w->orig_top_line = Qnil;
4624 XSETFASTINT (w->last_modified, 0);
4625 XSETFASTINT (w->last_overlay_modified, 0);
4626 break;
4628 default:
4629 abort ();
4632 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4635 return success_p;
4639 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4640 without deleting other windows. */
4642 void
4643 grow_mini_window (w, delta)
4644 struct window *w;
4645 int delta;
4647 struct frame *f = XFRAME (w->frame);
4648 struct window *root;
4650 xassert (MINI_WINDOW_P (w));
4651 /* Commenting out the following assertion goes against the stated interface
4652 of the function, but it currently does not seem to do anything useful.
4653 See discussion of this issue in the thread for bug#4534.
4654 xassert (delta >= 0); */
4656 /* Compute how much we can enlarge the mini-window without deleting
4657 other windows. */
4658 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4659 if (delta > 0)
4661 int min_height = window_min_size (root, 0, 0, 0, 0);
4662 if (XFASTINT (root->total_lines) - delta < min_height)
4663 /* Note that the root window may already be smaller than
4664 min_height. */
4665 delta = max (0, XFASTINT (root->total_lines) - min_height);
4668 if (delta)
4670 /* Save original window sizes and positions, if not already done. */
4671 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4672 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4674 /* Shrink other windows. */
4675 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4677 /* Grow the mini-window. */
4678 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4679 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4680 XSETFASTINT (w->last_modified, 0);
4681 XSETFASTINT (w->last_overlay_modified, 0);
4683 adjust_glyphs (f);
4688 /* Shrink mini-window W. If there is recorded info about window sizes
4689 before a call to grow_mini_window, restore recorded window sizes.
4690 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4691 line. */
4693 void
4694 shrink_mini_window (w)
4695 struct window *w;
4697 struct frame *f = XFRAME (w->frame);
4698 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4700 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4702 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4703 adjust_glyphs (f);
4704 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4705 windows_or_buffers_changed = 1;
4707 else if (XFASTINT (w->total_lines) > 1)
4709 /* Distribute the additional lines of the mini-window
4710 among the other windows. */
4711 Lisp_Object window;
4712 XSETWINDOW (window, w);
4713 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4719 /* Mark window cursors off for all windows in the window tree rooted
4720 at W by setting their phys_cursor_on_p flag to zero. Called from
4721 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4722 the frame are cleared. */
4724 void
4725 mark_window_cursors_off (w)
4726 struct window *w;
4728 while (w)
4730 if (!NILP (w->hchild))
4731 mark_window_cursors_off (XWINDOW (w->hchild));
4732 else if (!NILP (w->vchild))
4733 mark_window_cursors_off (XWINDOW (w->vchild));
4734 else
4735 w->phys_cursor_on_p = 0;
4737 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4742 /* Return number of lines of text (not counting mode lines) in W. */
4745 window_internal_height (w)
4746 struct window *w;
4748 int ht = XFASTINT (w->total_lines);
4750 if (!MINI_WINDOW_P (w))
4752 if (!NILP (w->parent)
4753 || !NILP (w->vchild)
4754 || !NILP (w->hchild)
4755 || !NILP (w->next)
4756 || !NILP (w->prev)
4757 || WINDOW_WANTS_MODELINE_P (w))
4758 --ht;
4760 if (WINDOW_WANTS_HEADER_LINE_P (w))
4761 --ht;
4764 return ht;
4768 /* Return the number of columns in W.
4769 Don't count columns occupied by scroll bars or the vertical bar
4770 separating W from the sibling to its right. */
4773 window_box_text_cols (w)
4774 struct window *w;
4776 struct frame *f = XFRAME (WINDOW_FRAME (w));
4777 int width = XINT (w->total_cols);
4779 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4780 /* Scroll bars occupy a few columns. */
4781 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4782 else if (!FRAME_WINDOW_P (f)
4783 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4784 /* The column of `|' characters separating side-by-side windows
4785 occupies one column only. */
4786 width -= 1;
4788 if (FRAME_WINDOW_P (f))
4789 /* On window-systems, fringes and display margins cannot be
4790 used for normal text. */
4791 width -= (WINDOW_FRINGE_COLS (w)
4792 + WINDOW_LEFT_MARGIN_COLS (w)
4793 + WINDOW_RIGHT_MARGIN_COLS (w));
4795 return width;
4799 /************************************************************************
4800 Window Scrolling
4801 ***********************************************************************/
4803 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4804 N screen-fulls, which is defined as the height of the window minus
4805 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4806 instead. Negative values of N mean scroll down. NOERROR non-zero
4807 means don't signal an error if we try to move over BEGV or ZV,
4808 respectively. */
4810 static void
4811 window_scroll (window, n, whole, noerror)
4812 Lisp_Object window;
4813 int n;
4814 int whole;
4815 int noerror;
4817 immediate_quit = 1;
4819 /* If we must, use the pixel-based version which is much slower than
4820 the line-based one but can handle varying line heights. */
4821 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4822 window_scroll_pixel_based (window, n, whole, noerror);
4823 else
4824 window_scroll_line_based (window, n, whole, noerror);
4826 immediate_quit = 0;
4830 /* Implementation of window_scroll that works based on pixel line
4831 heights. See the comment of window_scroll for parameter
4832 descriptions. */
4834 static void
4835 window_scroll_pixel_based (window, n, whole, noerror)
4836 Lisp_Object window;
4837 int n;
4838 int whole;
4839 int noerror;
4841 struct it it;
4842 struct window *w = XWINDOW (window);
4843 struct text_pos start;
4844 int this_scroll_margin;
4845 /* True if we fiddled the window vscroll field without really scrolling. */
4846 int vscrolled = 0;
4847 int x, y, rtop, rbot, rowh, vpos;
4849 SET_TEXT_POS_FROM_MARKER (start, w->start);
4851 /* If PT is not visible in WINDOW, move back one half of
4852 the screen. Allow PT to be partially visible, otherwise
4853 something like (scroll-down 1) with PT in the line before
4854 the partially visible one would recenter. */
4856 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4858 /* Move backward half the height of the window. Performance note:
4859 vmotion used here is about 10% faster, but would give wrong
4860 results for variable height lines. */
4861 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4862 it.current_y = it.last_visible_y;
4863 move_it_vertically_backward (&it, window_box_height (w) / 2);
4865 /* The function move_iterator_vertically may move over more than
4866 the specified y-distance. If it->w is small, e.g. a
4867 mini-buffer window, we may end up in front of the window's
4868 display area. This is the case when Start displaying at the
4869 start of the line containing PT in this case. */
4870 if (it.current_y <= 0)
4872 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4873 move_it_vertically_backward (&it, 0);
4874 it.current_y = 0;
4877 start = it.current.pos;
4879 else if (auto_window_vscroll_p)
4881 if (rtop || rbot) /* partially visible */
4883 int px;
4884 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4885 if (whole)
4886 dy = max ((window_box_height (w)
4887 - next_screen_context_lines * dy),
4888 dy);
4889 dy *= n;
4891 if (n < 0)
4893 /* Only vscroll backwards if already vscrolled forwards. */
4894 if (w->vscroll < 0 && rtop > 0)
4896 px = max (0, -w->vscroll - min (rtop, -dy));
4897 Fset_window_vscroll (window, make_number (px), Qt);
4898 return;
4901 if (n > 0)
4903 /* Do vscroll if already vscrolled or only display line. */
4904 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4906 px = max (0, -w->vscroll + min (rbot, dy));
4907 Fset_window_vscroll (window, make_number (px), Qt);
4908 return;
4911 /* Maybe modify window start instead of scrolling. */
4912 if (rbot > 0 || w->vscroll < 0)
4914 int spos;
4916 Fset_window_vscroll (window, make_number (0), Qt);
4917 /* If there are other text lines above the current row,
4918 move window start to current row. Else to next row. */
4919 if (rbot > 0)
4920 spos = XINT (Fline_beginning_position (Qnil));
4921 else
4922 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4923 set_marker_restricted (w->start, make_number (spos),
4924 w->buffer);
4925 w->start_at_line_beg = Qt;
4926 w->update_mode_line = Qt;
4927 XSETFASTINT (w->last_modified, 0);
4928 XSETFASTINT (w->last_overlay_modified, 0);
4929 /* Set force_start so that redisplay_window will run the
4930 window-scroll-functions. */
4931 w->force_start = Qt;
4932 return;
4936 /* Cancel previous vscroll. */
4937 Fset_window_vscroll (window, make_number (0), Qt);
4940 /* If scroll_preserve_screen_position is non-nil, we try to set
4941 point in the same window line as it is now, so get that line. */
4942 if (!NILP (Vscroll_preserve_screen_position))
4944 /* We preserve the goal pixel coordinate across consecutive
4945 calls to scroll-up or scroll-down. This avoids the
4946 possibility of point becoming "stuck" on a tall line when
4947 scrolling by one line. */
4948 if (window_scroll_pixel_based_preserve_y < 0
4949 || (!EQ (current_kboard->Vlast_command, Qscroll_up)
4950 && !EQ (current_kboard->Vlast_command, Qscroll_down)))
4952 start_display (&it, w, start);
4953 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4954 window_scroll_pixel_based_preserve_y = it.current_y;
4955 window_scroll_pixel_based_preserve_x = it.current_x;
4958 else
4959 window_scroll_pixel_based_preserve_y
4960 = window_scroll_pixel_based_preserve_x = -1;
4962 /* Move iterator it from start the specified distance forward or
4963 backward. The result is the new window start. */
4964 start_display (&it, w, start);
4965 if (whole)
4967 int start_pos = IT_CHARPOS (it);
4968 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4969 dy = max ((window_box_height (w)
4970 - next_screen_context_lines * dy),
4971 dy) * n;
4973 /* Note that move_it_vertically always moves the iterator to the
4974 start of a line. So, if the last line doesn't have a newline,
4975 we would end up at the start of the line ending at ZV. */
4976 if (dy <= 0)
4978 move_it_vertically_backward (&it, -dy);
4979 /* Ensure we actually do move, e.g. in case we are currently
4980 looking at an image that is taller that the window height. */
4981 while (start_pos == IT_CHARPOS (it)
4982 && start_pos > BEGV)
4983 move_it_by_lines (&it, -1, 1);
4985 else if (dy > 0)
4987 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4988 MOVE_TO_POS | MOVE_TO_Y);
4989 /* Ensure we actually do move, e.g. in case we are currently
4990 looking at an image that is taller that the window height. */
4991 while (start_pos == IT_CHARPOS (it)
4992 && start_pos < ZV)
4993 move_it_by_lines (&it, 1, 1);
4996 else
4997 move_it_by_lines (&it, n, 1);
4999 /* We failed if we find ZV is already on the screen (scrolling up,
5000 means there's nothing past the end), or if we can't start any
5001 earlier (scrolling down, means there's nothing past the top). */
5002 if ((n > 0 && IT_CHARPOS (it) == ZV)
5003 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
5005 if (IT_CHARPOS (it) == ZV)
5007 if (it.current_y < it.last_visible_y
5008 && (it.current_y + it.max_ascent + it.max_descent
5009 > it.last_visible_y))
5011 /* The last line was only partially visible, make it fully
5012 visible. */
5013 w->vscroll = (it.last_visible_y
5014 - it.current_y + it.max_ascent + it.max_descent);
5015 adjust_glyphs (it.f);
5017 else if (noerror)
5018 return;
5019 else if (n < 0) /* could happen with empty buffers */
5020 xsignal0 (Qbeginning_of_buffer);
5021 else
5022 xsignal0 (Qend_of_buffer);
5024 else
5026 if (w->vscroll != 0)
5027 /* The first line was only partially visible, make it fully
5028 visible. */
5029 w->vscroll = 0;
5030 else if (noerror)
5031 return;
5032 else
5033 xsignal0 (Qbeginning_of_buffer);
5036 /* If control gets here, then we vscrolled. */
5038 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
5040 /* Don't try to change the window start below. */
5041 vscrolled = 1;
5044 if (! vscrolled)
5046 int pos = IT_CHARPOS (it);
5047 int bytepos;
5049 /* If in the middle of a multi-glyph character move forward to
5050 the next character. */
5051 if (in_display_vector_p (&it))
5053 ++pos;
5054 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
5057 /* Set the window start, and set up the window for redisplay. */
5058 set_marker_restricted (w->start, make_number (pos),
5059 w->buffer);
5060 bytepos = XMARKER (w->start)->bytepos;
5061 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
5062 ? Qt : Qnil);
5063 w->update_mode_line = Qt;
5064 XSETFASTINT (w->last_modified, 0);
5065 XSETFASTINT (w->last_overlay_modified, 0);
5066 /* Set force_start so that redisplay_window will run the
5067 window-scroll-functions. */
5068 w->force_start = Qt;
5071 /* The rest of this function uses current_y in a nonstandard way,
5072 not including the height of the header line if any. */
5073 it.current_y = it.vpos = 0;
5075 /* Move PT out of scroll margins.
5076 This code wants current_y to be zero at the window start position
5077 even if there is a header line. */
5078 this_scroll_margin = max (0, scroll_margin);
5079 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
5080 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
5082 if (n > 0)
5084 /* We moved the window start towards ZV, so PT may be now
5085 in the scroll margin at the top. */
5086 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
5087 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
5088 && (NILP (Vscroll_preserve_screen_position)
5089 || EQ (Vscroll_preserve_screen_position, Qt)))
5090 /* We found PT at a legitimate height. Leave it alone. */
5092 else if (window_scroll_pixel_based_preserve_y >= 0)
5094 /* If we have a header line, take account of it.
5095 This is necessary because we set it.current_y to 0, above. */
5096 move_it_to (&it, -1,
5097 window_scroll_pixel_based_preserve_x,
5098 window_scroll_pixel_based_preserve_y
5099 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
5100 -1, MOVE_TO_Y | MOVE_TO_X);
5101 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5103 else
5105 while (it.current_y < this_scroll_margin)
5107 int prev = it.current_y;
5108 move_it_by_lines (&it, 1, 1);
5109 if (prev == it.current_y)
5110 break;
5112 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5115 else if (n < 0)
5117 int charpos, bytepos;
5118 int partial_p;
5120 /* Save our position, for the
5121 window_scroll_pixel_based_preserve_y case. */
5122 charpos = IT_CHARPOS (it);
5123 bytepos = IT_BYTEPOS (it);
5125 /* We moved the window start towards BEGV, so PT may be now
5126 in the scroll margin at the bottom. */
5127 move_it_to (&it, PT, -1,
5128 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5129 - this_scroll_margin - 1),
5131 MOVE_TO_POS | MOVE_TO_Y);
5133 /* Save our position, in case it's correct. */
5134 charpos = IT_CHARPOS (it);
5135 bytepos = IT_BYTEPOS (it);
5137 /* See if point is on a partially visible line at the end. */
5138 if (it.what == IT_EOB)
5139 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5140 else
5142 move_it_by_lines (&it, 1, 1);
5143 partial_p = it.current_y > it.last_visible_y;
5146 if (charpos == PT && !partial_p
5147 && (NILP (Vscroll_preserve_screen_position)
5148 || EQ (Vscroll_preserve_screen_position, Qt)))
5149 /* We found PT before we found the display margin, so PT is ok. */
5151 else if (window_scroll_pixel_based_preserve_y >= 0)
5153 SET_TEXT_POS_FROM_MARKER (start, w->start);
5154 start_display (&it, w, start);
5155 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5156 here because we called start_display again and did not
5157 alter it.current_y this time. */
5158 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5159 window_scroll_pixel_based_preserve_y, -1,
5160 MOVE_TO_Y | MOVE_TO_X);
5161 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5163 else
5165 if (partial_p)
5166 /* The last line was only partially visible, so back up two
5167 lines to make sure we're on a fully visible line. */
5169 move_it_by_lines (&it, -2, 0);
5170 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5172 else
5173 /* No, the position we saved is OK, so use it. */
5174 SET_PT_BOTH (charpos, bytepos);
5180 /* Implementation of window_scroll that works based on screen lines.
5181 See the comment of window_scroll for parameter descriptions. */
5183 static void
5184 window_scroll_line_based (window, n, whole, noerror)
5185 Lisp_Object window;
5186 int n;
5187 int whole;
5188 int noerror;
5190 register struct window *w = XWINDOW (window);
5191 register int opoint = PT, opoint_byte = PT_BYTE;
5192 register int pos, pos_byte;
5193 register int ht = window_internal_height (w);
5194 register Lisp_Object tem;
5195 int lose;
5196 Lisp_Object bolp;
5197 int startpos;
5198 Lisp_Object original_pos = Qnil;
5200 /* If scrolling screen-fulls, compute the number of lines to
5201 scroll from the window's height. */
5202 if (whole)
5203 n *= max (1, ht - next_screen_context_lines);
5205 startpos = marker_position (w->start);
5207 if (!NILP (Vscroll_preserve_screen_position))
5209 if (window_scroll_preserve_vpos <= 0
5210 || (!EQ (current_kboard->Vlast_command, Qscroll_up)
5211 && !EQ (current_kboard->Vlast_command, Qscroll_down)))
5213 struct position posit
5214 = *compute_motion (startpos, 0, 0, 0,
5215 PT, ht, 0,
5216 -1, XINT (w->hscroll),
5217 0, w);
5218 window_scroll_preserve_vpos = posit.vpos;
5219 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
5222 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5223 make_number (window_scroll_preserve_vpos));
5226 XSETFASTINT (tem, PT);
5227 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5229 if (NILP (tem))
5231 Fvertical_motion (make_number (- (ht / 2)), window);
5232 startpos = PT;
5235 SET_PT (startpos);
5236 lose = n < 0 && PT == BEGV;
5237 Fvertical_motion (make_number (n), window);
5238 pos = PT;
5239 pos_byte = PT_BYTE;
5240 bolp = Fbolp ();
5241 SET_PT_BOTH (opoint, opoint_byte);
5243 if (lose)
5245 if (noerror)
5246 return;
5247 else
5248 xsignal0 (Qbeginning_of_buffer);
5251 if (pos < ZV)
5253 int this_scroll_margin = scroll_margin;
5255 /* Don't use a scroll margin that is negative or too large. */
5256 if (this_scroll_margin < 0)
5257 this_scroll_margin = 0;
5259 if (XINT (w->total_lines) < 4 * scroll_margin)
5260 this_scroll_margin = XINT (w->total_lines) / 4;
5262 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5263 w->start_at_line_beg = bolp;
5264 w->update_mode_line = Qt;
5265 XSETFASTINT (w->last_modified, 0);
5266 XSETFASTINT (w->last_overlay_modified, 0);
5267 /* Set force_start so that redisplay_window will run
5268 the window-scroll-functions. */
5269 w->force_start = Qt;
5271 if (!NILP (Vscroll_preserve_screen_position)
5272 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5274 SET_PT_BOTH (pos, pos_byte);
5275 Fvertical_motion (original_pos, window);
5277 /* If we scrolled forward, put point enough lines down
5278 that it is outside the scroll margin. */
5279 else if (n > 0)
5281 int top_margin;
5283 if (this_scroll_margin > 0)
5285 SET_PT_BOTH (pos, pos_byte);
5286 Fvertical_motion (make_number (this_scroll_margin), window);
5287 top_margin = PT;
5289 else
5290 top_margin = pos;
5292 if (top_margin <= opoint)
5293 SET_PT_BOTH (opoint, opoint_byte);
5294 else if (!NILP (Vscroll_preserve_screen_position))
5296 SET_PT_BOTH (pos, pos_byte);
5297 Fvertical_motion (original_pos, window);
5299 else
5300 SET_PT (top_margin);
5302 else if (n < 0)
5304 int bottom_margin;
5306 /* If we scrolled backward, put point near the end of the window
5307 but not within the scroll margin. */
5308 SET_PT_BOTH (pos, pos_byte);
5309 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5310 if (XFASTINT (tem) == ht - this_scroll_margin)
5311 bottom_margin = PT;
5312 else
5313 bottom_margin = PT + 1;
5315 if (bottom_margin > opoint)
5316 SET_PT_BOTH (opoint, opoint_byte);
5317 else
5319 if (!NILP (Vscroll_preserve_screen_position))
5321 SET_PT_BOTH (pos, pos_byte);
5322 Fvertical_motion (original_pos, window);
5324 else
5325 Fvertical_motion (make_number (-1), window);
5329 else
5331 if (noerror)
5332 return;
5333 else
5334 xsignal0 (Qend_of_buffer);
5339 /* Scroll selected_window up or down. If N is nil, scroll a
5340 screen-full which is defined as the height of the window minus
5341 next_screen_context_lines. If N is the symbol `-', scroll.
5342 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5343 up. This is the guts of Fscroll_up and Fscroll_down. */
5345 static void
5346 scroll_command (n, direction)
5347 Lisp_Object n;
5348 int direction;
5350 int count = SPECPDL_INDEX ();
5352 xassert (eabs (direction) == 1);
5354 /* If selected window's buffer isn't current, make it current for
5355 the moment. But don't screw up if window_scroll gets an error. */
5356 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5358 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5359 Fset_buffer (XWINDOW (selected_window)->buffer);
5361 /* Make redisplay consider other windows than just selected_window. */
5362 ++windows_or_buffers_changed;
5365 if (NILP (n))
5366 window_scroll (selected_window, direction, 1, 0);
5367 else if (EQ (n, Qminus))
5368 window_scroll (selected_window, -direction, 1, 0);
5369 else
5371 n = Fprefix_numeric_value (n);
5372 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5375 unbind_to (count, Qnil);
5378 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5379 doc: /* Scroll text of selected window upward ARG lines.
5380 If ARG is omitted or nil, scroll upward by a near full screen.
5381 A near full screen is `next-screen-context-lines' less than a full screen.
5382 Negative ARG means scroll downward.
5383 If ARG is the atom `-', scroll downward by nearly full screen.
5384 When calling from a program, supply as argument a number, nil, or `-'. */)
5385 (arg)
5386 Lisp_Object arg;
5388 scroll_command (arg, 1);
5389 return Qnil;
5392 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5393 doc: /* Scroll text of selected window down ARG lines.
5394 If ARG is omitted or nil, scroll down by a near full screen.
5395 A near full screen is `next-screen-context-lines' less than a full screen.
5396 Negative ARG means scroll upward.
5397 If ARG is the atom `-', scroll upward by nearly full screen.
5398 When calling from a program, supply as argument a number, nil, or `-'. */)
5399 (arg)
5400 Lisp_Object arg;
5402 scroll_command (arg, -1);
5403 return Qnil;
5406 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5407 doc: /* Return the other window for \"other window scroll\" commands.
5408 If `other-window-scroll-buffer' is non-nil, a window
5409 showing that buffer is used.
5410 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5411 specifies the window. This takes precedence over
5412 `other-window-scroll-buffer'. */)
5415 Lisp_Object window;
5417 if (MINI_WINDOW_P (XWINDOW (selected_window))
5418 && !NILP (Vminibuf_scroll_window))
5419 window = Vminibuf_scroll_window;
5420 /* If buffer is specified, scroll that buffer. */
5421 else if (!NILP (Vother_window_scroll_buffer))
5423 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5424 if (NILP (window))
5425 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5427 else
5429 /* Nothing specified; look for a neighboring window on the same
5430 frame. */
5431 window = Fnext_window (selected_window, Qnil, Qnil);
5433 if (EQ (window, selected_window))
5434 /* That didn't get us anywhere; look for a window on another
5435 visible frame. */
5437 window = Fnext_window (window, Qnil, Qt);
5438 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5439 && ! EQ (window, selected_window));
5442 CHECK_LIVE_WINDOW (window);
5444 if (EQ (window, selected_window))
5445 error ("There is no other window");
5447 return window;
5450 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5451 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5452 A near full screen is `next-screen-context-lines' less than a full screen.
5453 The next window is the one below the current one; or the one at the top
5454 if the current one is at the bottom. Negative ARG means scroll downward.
5455 If ARG is the atom `-', scroll downward by nearly full screen.
5456 When calling from a program, supply as argument a number, nil, or `-'.
5458 If `other-window-scroll-buffer' is non-nil, scroll the window
5459 showing that buffer, popping the buffer up if necessary.
5460 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5461 specifies the window to scroll. This takes precedence over
5462 `other-window-scroll-buffer'. */)
5463 (arg)
5464 Lisp_Object arg;
5466 Lisp_Object window;
5467 struct window *w;
5468 int count = SPECPDL_INDEX ();
5470 window = Fother_window_for_scrolling ();
5471 w = XWINDOW (window);
5473 /* Don't screw up if window_scroll gets an error. */
5474 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5475 ++windows_or_buffers_changed;
5477 Fset_buffer (w->buffer);
5478 SET_PT (marker_position (w->pointm));
5480 if (NILP (arg))
5481 window_scroll (window, 1, 1, 1);
5482 else if (EQ (arg, Qminus))
5483 window_scroll (window, -1, 1, 1);
5484 else
5486 if (CONSP (arg))
5487 arg = Fcar (arg);
5488 CHECK_NUMBER (arg);
5489 window_scroll (window, XINT (arg), 0, 1);
5492 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5493 unbind_to (count, Qnil);
5495 return Qnil;
5498 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5499 doc: /* Scroll selected window display ARG columns left.
5500 Default for ARG is window width minus 2.
5501 Value is the total amount of leftward horizontal scrolling in
5502 effect after the change.
5503 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5504 lower bound for automatic scrolling, i.e. automatic scrolling
5505 will not scroll a window to a column less than the value returned
5506 by this function. This happens in an interactive call. */)
5507 (arg, set_minimum)
5508 register Lisp_Object arg, set_minimum;
5510 Lisp_Object result;
5511 int hscroll;
5512 struct window *w = XWINDOW (selected_window);
5514 if (NILP (arg))
5515 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5516 else
5517 arg = Fprefix_numeric_value (arg);
5519 hscroll = XINT (w->hscroll) + XINT (arg);
5520 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5522 if (!NILP (set_minimum))
5523 w->min_hscroll = w->hscroll;
5525 return result;
5528 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5529 doc: /* Scroll selected window display ARG columns right.
5530 Default for ARG is window width minus 2.
5531 Value is the total amount of leftward horizontal scrolling in
5532 effect after the change.
5533 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5534 lower bound for automatic scrolling, i.e. automatic scrolling
5535 will not scroll a window to a column less than the value returned
5536 by this function. This happens in an interactive call. */)
5537 (arg, set_minimum)
5538 register Lisp_Object arg, set_minimum;
5540 Lisp_Object result;
5541 int hscroll;
5542 struct window *w = XWINDOW (selected_window);
5544 if (NILP (arg))
5545 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5546 else
5547 arg = Fprefix_numeric_value (arg);
5549 hscroll = XINT (w->hscroll) - XINT (arg);
5550 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5552 if (!NILP (set_minimum))
5553 w->min_hscroll = w->hscroll;
5555 return result;
5558 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5559 doc: /* Return the window which was selected when entering the minibuffer.
5560 Returns nil, if selected window is not a minibuffer window. */)
5563 if (minibuf_level > 0
5564 && MINI_WINDOW_P (XWINDOW (selected_window))
5565 && WINDOW_LIVE_P (minibuf_selected_window))
5566 return minibuf_selected_window;
5568 return Qnil;
5571 /* Value is the number of lines actually displayed in window W,
5572 as opposed to its height. */
5574 static int
5575 displayed_window_lines (w)
5576 struct window *w;
5578 struct it it;
5579 struct text_pos start;
5580 int height = window_box_height (w);
5581 struct buffer *old_buffer;
5582 int bottom_y;
5584 if (XBUFFER (w->buffer) != current_buffer)
5586 old_buffer = current_buffer;
5587 set_buffer_internal (XBUFFER (w->buffer));
5589 else
5590 old_buffer = NULL;
5592 /* In case W->start is out of the accessible range, do something
5593 reasonable. This happens in Info mode when Info-scroll-down
5594 calls (recenter -1) while W->start is 1. */
5595 if (XMARKER (w->start)->charpos < BEGV)
5596 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5597 else if (XMARKER (w->start)->charpos > ZV)
5598 SET_TEXT_POS (start, ZV, ZV_BYTE);
5599 else
5600 SET_TEXT_POS_FROM_MARKER (start, w->start);
5602 start_display (&it, w, start);
5603 move_it_vertically (&it, height);
5604 bottom_y = line_bottom_y (&it);
5606 /* rms: On a non-window display,
5607 the value of it.vpos at the bottom of the screen
5608 seems to be 1 larger than window_box_height (w).
5609 This kludge fixes a bug whereby (move-to-window-line -1)
5610 when ZV is on the last screen line
5611 moves to the previous screen line instead of the last one. */
5612 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5613 height++;
5615 /* Add in empty lines at the bottom of the window. */
5616 if (bottom_y < height)
5618 int uy = FRAME_LINE_HEIGHT (it.f);
5619 it.vpos += (height - bottom_y + uy - 1) / uy;
5622 if (old_buffer)
5623 set_buffer_internal (old_buffer);
5625 return it.vpos;
5629 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5630 doc: /* Center point in selected window and maybe redisplay frame.
5631 With prefix argument ARG, recenter putting point on screen line ARG
5632 relative to the selected window. If ARG is negative, it counts up from the
5633 bottom of the window. (ARG should be less than the height of the window.)
5635 If ARG is omitted or nil, then recenter with point on the middle line of
5636 the selected window; if the variable `recenter-redisplay' is non-nil,
5637 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5638 is set to `grow-only', this resets the tool-bar's height to the minimum
5639 height needed); if `recenter-redisplay' has the special value `tty',
5640 then only tty frame are redrawn.
5642 Just C-u as prefix means put point in the center of the window
5643 and redisplay normally--don't erase and redraw the frame. */)
5644 (arg)
5645 register Lisp_Object arg;
5647 struct window *w = XWINDOW (selected_window);
5648 struct buffer *buf = XBUFFER (w->buffer);
5649 struct buffer *obuf = current_buffer;
5650 int center_p = 0;
5651 int charpos, bytepos;
5652 int iarg;
5653 int this_scroll_margin;
5655 /* If redisplay is suppressed due to an error, try again. */
5656 obuf->display_error_modiff = 0;
5658 if (NILP (arg))
5660 if (!NILP (Vrecenter_redisplay)
5661 && (!EQ (Vrecenter_redisplay, Qtty)
5662 || !NILP (Ftty_type (selected_frame))))
5664 int i;
5666 /* Invalidate pixel data calculated for all compositions. */
5667 for (i = 0; i < n_compositions; i++)
5668 composition_table[i]->font = NULL;
5670 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5672 Fredraw_frame (WINDOW_FRAME (w));
5673 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5676 center_p = 1;
5678 else if (CONSP (arg)) /* Just C-u. */
5679 center_p = 1;
5680 else
5682 arg = Fprefix_numeric_value (arg);
5683 CHECK_NUMBER (arg);
5684 iarg = XINT (arg);
5687 set_buffer_internal (buf);
5689 /* Do this after making BUF current
5690 in case scroll_margin is buffer-local. */
5691 this_scroll_margin = max (0, scroll_margin);
5692 this_scroll_margin = min (this_scroll_margin,
5693 XFASTINT (w->total_lines) / 4);
5695 /* Handle centering on a graphical frame specially. Such frames can
5696 have variable-height lines and centering point on the basis of
5697 line counts would lead to strange effects. */
5698 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5700 if (center_p)
5702 struct it it;
5703 struct text_pos pt;
5705 SET_TEXT_POS (pt, PT, PT_BYTE);
5706 start_display (&it, w, pt);
5707 move_it_vertically_backward (&it, window_box_height (w) / 2);
5708 charpos = IT_CHARPOS (it);
5709 bytepos = IT_BYTEPOS (it);
5711 else if (iarg < 0)
5713 struct it it;
5714 struct text_pos pt;
5715 int nlines = -iarg;
5716 int extra_line_spacing;
5717 int h = window_box_height (w);
5719 iarg = - max (-iarg, this_scroll_margin);
5721 SET_TEXT_POS (pt, PT, PT_BYTE);
5722 start_display (&it, w, pt);
5724 /* Be sure we have the exact height of the full line containing PT. */
5725 move_it_by_lines (&it, 0, 1);
5727 /* The amount of pixels we have to move back is the window
5728 height minus what's displayed in the line containing PT,
5729 and the lines below. */
5730 it.current_y = 0;
5731 it.vpos = 0;
5732 move_it_by_lines (&it, nlines, 1);
5734 if (it.vpos == nlines)
5735 h -= it.current_y;
5736 else
5738 /* Last line has no newline */
5739 h -= line_bottom_y (&it);
5740 it.vpos++;
5743 /* Don't reserve space for extra line spacing of last line. */
5744 extra_line_spacing = it.max_extra_line_spacing;
5746 /* If we can't move down NLINES lines because we hit
5747 the end of the buffer, count in some empty lines. */
5748 if (it.vpos < nlines)
5750 nlines -= it.vpos;
5751 extra_line_spacing = it.extra_line_spacing;
5752 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5754 if (h <= 0)
5755 return Qnil;
5757 /* Now find the new top line (starting position) of the window. */
5758 start_display (&it, w, pt);
5759 it.current_y = 0;
5760 move_it_vertically_backward (&it, h);
5762 /* If extra line spacing is present, we may move too far
5763 back. This causes the last line to be only partially
5764 visible (which triggers redisplay to recenter that line
5765 in the middle), so move forward.
5766 But ignore extra line spacing on last line, as it is not
5767 considered to be part of the visible height of the line.
5769 h += extra_line_spacing;
5770 while (-it.current_y > h)
5771 move_it_by_lines (&it, 1, 1);
5773 charpos = IT_CHARPOS (it);
5774 bytepos = IT_BYTEPOS (it);
5776 else
5778 struct position pos;
5780 iarg = max (iarg, this_scroll_margin);
5782 pos = *vmotion (PT, -iarg, w);
5783 charpos = pos.bufpos;
5784 bytepos = pos.bytepos;
5787 else
5789 struct position pos;
5790 int ht = window_internal_height (w);
5792 if (center_p)
5793 iarg = ht / 2;
5794 else if (iarg < 0)
5795 iarg += ht;
5797 /* Don't let it get into the margin at either top or bottom. */
5798 iarg = max (iarg, this_scroll_margin);
5799 iarg = min (iarg, ht - this_scroll_margin - 1);
5801 pos = *vmotion (PT, - iarg, w);
5802 charpos = pos.bufpos;
5803 bytepos = pos.bytepos;
5806 /* Set the new window start. */
5807 set_marker_both (w->start, w->buffer, charpos, bytepos);
5808 w->window_end_valid = Qnil;
5810 w->optional_new_start = Qt;
5812 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5813 w->start_at_line_beg = Qt;
5814 else
5815 w->start_at_line_beg = Qnil;
5817 set_buffer_internal (obuf);
5818 return Qnil;
5822 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5823 0, 1, 0,
5824 doc: /* Return the height in lines of the text display area of WINDOW.
5825 WINDOW defaults to the selected window.
5827 The return value does not include the mode line, any header line, nor
5828 any partial-height lines in the text display area. */)
5829 (window)
5830 Lisp_Object window;
5832 struct window *w = decode_window (window);
5833 int pixel_height = window_box_height (w);
5834 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5835 return make_number (line_height);
5840 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5841 1, 1, "P",
5842 doc: /* Position point relative to window.
5843 With no argument, position point at center of window.
5844 An argument specifies vertical position within the window;
5845 zero means top of window, negative means relative to bottom of window. */)
5846 (arg)
5847 Lisp_Object arg;
5849 struct window *w = XWINDOW (selected_window);
5850 int lines, start;
5851 Lisp_Object window;
5852 #if 0
5853 int this_scroll_margin;
5854 #endif
5856 window = selected_window;
5857 start = marker_position (w->start);
5858 if (start < BEGV || start > ZV)
5860 int height = window_internal_height (w);
5861 Fvertical_motion (make_number (- (height / 2)), window);
5862 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5863 w->start_at_line_beg = Fbolp ();
5864 w->force_start = Qt;
5866 else
5867 Fgoto_char (w->start);
5869 lines = displayed_window_lines (w);
5871 #if 0
5872 this_scroll_margin = max (0, scroll_margin);
5873 this_scroll_margin = min (this_scroll_margin, lines / 4);
5874 #endif
5876 if (NILP (arg))
5877 XSETFASTINT (arg, lines / 2);
5878 else
5880 int iarg = XINT (Fprefix_numeric_value (arg));
5882 if (iarg < 0)
5883 iarg = iarg + lines;
5885 #if 0 /* This code would prevent move-to-window-line from moving point
5886 to a place inside the scroll margins (which would cause the
5887 next redisplay to scroll). I wrote this code, but then concluded
5888 it is probably better not to install it. However, it is here
5889 inside #if 0 so as not to lose it. -- rms. */
5891 /* Don't let it get into the margin at either top or bottom. */
5892 iarg = max (iarg, this_scroll_margin);
5893 iarg = min (iarg, lines - this_scroll_margin - 1);
5894 #endif
5896 arg = make_number (iarg);
5899 /* Skip past a partially visible first line. */
5900 if (w->vscroll)
5901 XSETINT (arg, XINT (arg) + 1);
5903 return Fvertical_motion (arg, window);
5908 /***********************************************************************
5909 Window Configuration
5910 ***********************************************************************/
5912 struct save_window_data
5914 EMACS_UINT size;
5915 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5916 Lisp_Object selected_frame;
5917 Lisp_Object current_window;
5918 Lisp_Object current_buffer;
5919 Lisp_Object minibuf_scroll_window;
5920 Lisp_Object minibuf_selected_window;
5921 Lisp_Object root_window;
5922 Lisp_Object focus_frame;
5923 /* A vector, each of whose elements is a struct saved_window
5924 for one window. */
5925 Lisp_Object saved_windows;
5927 /* All fields above are traced by the GC.
5928 From `fame-cols' down, the fields are ignored by the GC. */
5930 int frame_cols, frame_lines, frame_menu_bar_lines;
5931 int frame_tool_bar_lines;
5934 /* This is saved as a Lisp_Vector */
5935 struct saved_window
5937 /* these first two must agree with struct Lisp_Vector in lisp.h */
5938 EMACS_UINT size;
5939 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5941 Lisp_Object window;
5942 Lisp_Object buffer, start, pointm, mark;
5943 Lisp_Object left_col, top_line, total_cols, total_lines;
5944 Lisp_Object hscroll, min_hscroll;
5945 Lisp_Object parent, prev;
5946 Lisp_Object start_at_line_beg;
5947 Lisp_Object display_table;
5948 Lisp_Object orig_top_line, orig_total_lines;
5949 Lisp_Object left_margin_cols, right_margin_cols;
5950 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5951 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5952 Lisp_Object dedicated, resize_proportionally;
5955 #define SAVED_WINDOW_N(swv,n) \
5956 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5958 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5959 doc: /* Return t if OBJECT is a window-configuration object. */)
5960 (object)
5961 Lisp_Object object;
5963 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5966 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5967 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5968 (config)
5969 Lisp_Object config;
5971 register struct save_window_data *data;
5972 struct Lisp_Vector *saved_windows;
5974 CHECK_WINDOW_CONFIGURATION (config);
5976 data = (struct save_window_data *) XVECTOR (config);
5977 saved_windows = XVECTOR (data->saved_windows);
5978 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5981 DEFUN ("set-window-configuration", Fset_window_configuration,
5982 Sset_window_configuration, 1, 1, 0,
5983 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5984 CONFIGURATION must be a value previously returned
5985 by `current-window-configuration' (which see).
5986 If CONFIGURATION was made from a frame that is now deleted,
5987 only frame-independent values can be restored. In this case,
5988 the return value is nil. Otherwise the value is t. */)
5989 (configuration)
5990 Lisp_Object configuration;
5992 register struct save_window_data *data;
5993 struct Lisp_Vector *saved_windows;
5994 Lisp_Object new_current_buffer;
5995 Lisp_Object frame;
5996 FRAME_PTR f;
5997 int old_point = -1;
5999 CHECK_WINDOW_CONFIGURATION (configuration);
6001 data = (struct save_window_data *) XVECTOR (configuration);
6002 saved_windows = XVECTOR (data->saved_windows);
6004 new_current_buffer = data->current_buffer;
6005 if (NILP (XBUFFER (new_current_buffer)->name))
6006 new_current_buffer = Qnil;
6007 else
6009 if (XBUFFER (new_current_buffer) == current_buffer)
6010 /* The code further down "preserves point" by saving here PT in
6011 old_point and then setting it later back into PT. When the
6012 current-selected-window and the final-selected-window both show
6013 the current buffer, this suffers from the problem that the
6014 current PT is the window-point of the current-selected-window,
6015 while the final PT is the point of the final-selected-window, so
6016 this copy from one PT to the other would end up moving the
6017 window-point of the final-selected-window to the window-point of
6018 the current-selected-window. So we have to be careful which
6019 point of the current-buffer we copy into old_point. */
6020 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6021 && WINDOWP (selected_window)
6022 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
6023 && !EQ (selected_window, data->current_window))
6024 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6025 else
6026 old_point = PT;
6027 else
6028 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
6029 point in new_current_buffer as of the last time this buffer was
6030 used. This can be non-deterministic since it can be changed by
6031 things like jit-lock by mere temporary selection of some random
6032 window that happens to show this buffer.
6033 So if possible we want this arbitrary choice of "which point" to
6034 be the one from the to-be-selected-window so as to prevent this
6035 window's cursor from being copied from another window. */
6036 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
6037 /* If current_window = selected_window, its point is in BUF_PT. */
6038 && !EQ (selected_window, data->current_window))
6039 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
6040 else
6041 old_point = BUF_PT (XBUFFER (new_current_buffer));
6044 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
6045 f = XFRAME (frame);
6047 /* If f is a dead frame, don't bother rebuilding its window tree.
6048 However, there is other stuff we should still try to do below. */
6049 if (FRAME_LIVE_P (f))
6051 register struct window *w;
6052 register struct saved_window *p;
6053 struct window *root_window;
6054 struct window **leaf_windows;
6055 int n_leaf_windows;
6056 int k, i, n;
6058 /* If the frame has been resized since this window configuration was
6059 made, we change the frame to the size specified in the
6060 configuration, restore the configuration, and then resize it
6061 back. We keep track of the prevailing height in these variables. */
6062 int previous_frame_lines = FRAME_LINES (f);
6063 int previous_frame_cols = FRAME_COLS (f);
6064 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6065 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6067 /* The mouse highlighting code could get screwed up
6068 if it runs during this. */
6069 BLOCK_INPUT;
6071 if (data->frame_lines != previous_frame_lines
6072 || data->frame_cols != previous_frame_cols)
6073 change_frame_size (f, data->frame_lines,
6074 data->frame_cols, 0, 0, 0);
6075 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6076 if (data->frame_menu_bar_lines
6077 != previous_frame_menu_bar_lines)
6078 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
6079 make_number (0));
6080 #ifdef HAVE_WINDOW_SYSTEM
6081 if (data->frame_tool_bar_lines
6082 != previous_frame_tool_bar_lines)
6083 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
6084 make_number (0));
6085 #endif
6086 #endif
6088 /* "Swap out" point from the selected window's buffer
6089 into the window itself. (Normally the pointm of the selected
6090 window holds garbage.) We do this now, before
6091 restoring the window contents, and prevent it from
6092 being done later on when we select a new window. */
6093 if (! NILP (XWINDOW (selected_window)->buffer))
6095 w = XWINDOW (selected_window);
6096 set_marker_both (w->pointm,
6097 w->buffer,
6098 BUF_PT (XBUFFER (w->buffer)),
6099 BUF_PT_BYTE (XBUFFER (w->buffer)));
6102 windows_or_buffers_changed++;
6103 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
6105 /* Problem: Freeing all matrices and later allocating them again
6106 is a serious redisplay flickering problem. What we would
6107 really like to do is to free only those matrices not reused
6108 below. */
6109 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
6110 leaf_windows
6111 = (struct window **) alloca (count_windows (root_window)
6112 * sizeof (struct window *));
6113 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
6115 /* Kludge Alert!
6116 Mark all windows now on frame as "deleted".
6117 Restoring the new configuration "undeletes" any that are in it.
6119 Save their current buffers in their height fields, since we may
6120 need it later, if a buffer saved in the configuration is now
6121 dead. */
6122 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6124 for (k = 0; k < saved_windows->size; k++)
6126 p = SAVED_WINDOW_N (saved_windows, k);
6127 w = XWINDOW (p->window);
6128 w->next = Qnil;
6130 if (!NILP (p->parent))
6131 w->parent = SAVED_WINDOW_N (saved_windows,
6132 XFASTINT (p->parent))->window;
6133 else
6134 w->parent = Qnil;
6136 if (!NILP (p->prev))
6138 w->prev = SAVED_WINDOW_N (saved_windows,
6139 XFASTINT (p->prev))->window;
6140 XWINDOW (w->prev)->next = p->window;
6142 else
6144 w->prev = Qnil;
6145 if (!NILP (w->parent))
6147 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6149 XWINDOW (w->parent)->vchild = p->window;
6150 XWINDOW (w->parent)->hchild = Qnil;
6152 else
6154 XWINDOW (w->parent)->hchild = p->window;
6155 XWINDOW (w->parent)->vchild = Qnil;
6160 /* If we squirreled away the buffer in the window's height,
6161 restore it now. */
6162 if (BUFFERP (w->total_lines))
6163 w->buffer = w->total_lines;
6164 w->left_col = p->left_col;
6165 w->top_line = p->top_line;
6166 w->total_cols = p->total_cols;
6167 w->total_lines = p->total_lines;
6168 w->hscroll = p->hscroll;
6169 w->min_hscroll = p->min_hscroll;
6170 w->display_table = p->display_table;
6171 w->orig_top_line = p->orig_top_line;
6172 w->orig_total_lines = p->orig_total_lines;
6173 w->left_margin_cols = p->left_margin_cols;
6174 w->right_margin_cols = p->right_margin_cols;
6175 w->left_fringe_width = p->left_fringe_width;
6176 w->right_fringe_width = p->right_fringe_width;
6177 w->fringes_outside_margins = p->fringes_outside_margins;
6178 w->scroll_bar_width = p->scroll_bar_width;
6179 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6180 w->dedicated = p->dedicated;
6181 w->resize_proportionally = p->resize_proportionally;
6182 XSETFASTINT (w->last_modified, 0);
6183 XSETFASTINT (w->last_overlay_modified, 0);
6185 /* Reinstall the saved buffer and pointers into it. */
6186 if (NILP (p->buffer))
6187 w->buffer = p->buffer;
6188 else
6190 if (!NILP (XBUFFER (p->buffer)->name))
6191 /* If saved buffer is alive, install it. */
6193 w->buffer = p->buffer;
6194 w->start_at_line_beg = p->start_at_line_beg;
6195 set_marker_restricted (w->start, p->start, w->buffer);
6196 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6197 Fset_marker (XBUFFER (w->buffer)->mark,
6198 p->mark, w->buffer);
6200 /* As documented in Fcurrent_window_configuration, don't
6201 restore the location of point in the buffer which was
6202 current when the window configuration was recorded. */
6203 if (!EQ (p->buffer, new_current_buffer)
6204 && XBUFFER (p->buffer) == current_buffer)
6205 Fgoto_char (w->pointm);
6207 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
6208 /* Else unless window has a live buffer, get one. */
6210 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6211 /* This will set the markers to beginning of visible
6212 range. */
6213 set_marker_restricted (w->start, make_number (0), w->buffer);
6214 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6215 w->start_at_line_beg = Qt;
6217 else
6218 /* Keeping window's old buffer; make sure the markers
6219 are real. */
6221 /* Set window markers at start of visible range. */
6222 if (XMARKER (w->start)->buffer == 0)
6223 set_marker_restricted (w->start, make_number (0),
6224 w->buffer);
6225 if (XMARKER (w->pointm)->buffer == 0)
6226 set_marker_restricted_both (w->pointm, w->buffer,
6227 BUF_PT (XBUFFER (w->buffer)),
6228 BUF_PT_BYTE (XBUFFER (w->buffer)));
6229 w->start_at_line_beg = Qt;
6234 FRAME_ROOT_WINDOW (f) = data->root_window;
6235 /* Prevent "swapping out point" in the old selected window
6236 using the buffer that has been restored into it.
6237 We already swapped out point that from that window's old buffer. */
6238 selected_window = Qnil;
6240 /* Arrange *not* to restore point in the buffer that was
6241 current when the window configuration was saved. */
6242 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6243 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6244 make_number (old_point),
6245 XWINDOW (data->current_window)->buffer);
6247 Fselect_window (data->current_window, Qnil);
6248 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
6249 = selected_window;
6251 if (NILP (data->focus_frame)
6252 || (FRAMEP (data->focus_frame)
6253 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6254 Fredirect_frame_focus (frame, data->focus_frame);
6256 #if 0 /* I don't understand why this is needed, and it causes problems
6257 when the frame's old selected window has been deleted. */
6258 if (f != selected_frame && FRAME_WINDOW_P (f))
6259 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
6260 0, 0, Qnil);
6261 #endif
6263 /* Set the screen height to the value it had before this function. */
6264 if (previous_frame_lines != FRAME_LINES (f)
6265 || previous_frame_cols != FRAME_COLS (f))
6266 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6267 0, 0, 0);
6268 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6269 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6270 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6271 make_number (0));
6272 #ifdef HAVE_WINDOW_SYSTEM
6273 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6274 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6275 make_number (0));
6276 #endif
6277 #endif
6279 /* Now, free glyph matrices in windows that were not reused. */
6280 for (i = n = 0; i < n_leaf_windows; ++i)
6282 if (NILP (leaf_windows[i]->buffer))
6284 /* Assert it's not reused as a combination. */
6285 xassert (NILP (leaf_windows[i]->hchild)
6286 && NILP (leaf_windows[i]->vchild));
6287 free_window_matrices (leaf_windows[i]);
6289 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6290 ++n;
6293 adjust_glyphs (f);
6295 UNBLOCK_INPUT;
6297 /* Fselect_window will have made f the selected frame, so we
6298 reselect the proper frame here. Fhandle_switch_frame will change the
6299 selected window too, but that doesn't make the call to
6300 Fselect_window above totally superfluous; it still sets f's
6301 selected window. */
6302 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6303 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6305 run_window_configuration_change_hook (f);
6308 if (!NILP (new_current_buffer))
6309 Fset_buffer (new_current_buffer);
6311 Vminibuf_scroll_window = data->minibuf_scroll_window;
6312 minibuf_selected_window = data->minibuf_selected_window;
6314 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6317 /* Mark all windows now on frame as deleted
6318 by setting their buffers to nil. */
6320 void
6321 delete_all_subwindows (w)
6322 register struct window *w;
6324 if (!NILP (w->next))
6325 delete_all_subwindows (XWINDOW (w->next));
6326 if (!NILP (w->vchild))
6327 delete_all_subwindows (XWINDOW (w->vchild));
6328 if (!NILP (w->hchild))
6329 delete_all_subwindows (XWINDOW (w->hchild));
6331 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6333 if (!NILP (w->buffer))
6334 unshow_buffer (w);
6336 /* We set all three of these fields to nil, to make sure that we can
6337 distinguish this dead window from any live window. Live leaf
6338 windows will have buffer set, and combination windows will have
6339 vchild or hchild set. */
6340 w->buffer = Qnil;
6341 w->vchild = Qnil;
6342 w->hchild = Qnil;
6344 Vwindow_list = Qnil;
6347 static int
6348 count_windows (window)
6349 register struct window *window;
6351 register int count = 1;
6352 if (!NILP (window->next))
6353 count += count_windows (XWINDOW (window->next));
6354 if (!NILP (window->vchild))
6355 count += count_windows (XWINDOW (window->vchild));
6356 if (!NILP (window->hchild))
6357 count += count_windows (XWINDOW (window->hchild));
6358 return count;
6362 /* Fill vector FLAT with leaf windows under W, starting at index I.
6363 Value is last index + 1. */
6365 static int
6366 get_leaf_windows (w, flat, i)
6367 struct window *w;
6368 struct window **flat;
6369 int i;
6371 while (w)
6373 if (!NILP (w->hchild))
6374 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6375 else if (!NILP (w->vchild))
6376 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6377 else
6378 flat[i++] = w;
6380 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6383 return i;
6387 /* Return a pointer to the glyph W's physical cursor is on. Value is
6388 null if W's current matrix is invalid, so that no meaningfull glyph
6389 can be returned. */
6391 struct glyph *
6392 get_phys_cursor_glyph (w)
6393 struct window *w;
6395 struct glyph_row *row;
6396 struct glyph *glyph;
6398 if (w->phys_cursor.vpos >= 0
6399 && w->phys_cursor.vpos < w->current_matrix->nrows
6400 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6401 row->enabled_p)
6402 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6403 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6404 else
6405 glyph = NULL;
6407 return glyph;
6411 static int
6412 save_window_save (window, vector, i)
6413 Lisp_Object window;
6414 struct Lisp_Vector *vector;
6415 int i;
6417 register struct saved_window *p;
6418 register struct window *w;
6419 register Lisp_Object tem;
6421 for (;!NILP (window); window = w->next)
6423 p = SAVED_WINDOW_N (vector, i);
6424 w = XWINDOW (window);
6426 XSETFASTINT (w->temslot, i); i++;
6427 p->window = window;
6428 p->buffer = w->buffer;
6429 p->left_col = w->left_col;
6430 p->top_line = w->top_line;
6431 p->total_cols = w->total_cols;
6432 p->total_lines = w->total_lines;
6433 p->hscroll = w->hscroll;
6434 p->min_hscroll = w->min_hscroll;
6435 p->display_table = w->display_table;
6436 p->orig_top_line = w->orig_top_line;
6437 p->orig_total_lines = w->orig_total_lines;
6438 p->left_margin_cols = w->left_margin_cols;
6439 p->right_margin_cols = w->right_margin_cols;
6440 p->left_fringe_width = w->left_fringe_width;
6441 p->right_fringe_width = w->right_fringe_width;
6442 p->fringes_outside_margins = w->fringes_outside_margins;
6443 p->scroll_bar_width = w->scroll_bar_width;
6444 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6445 p->dedicated = w->dedicated;
6446 p->resize_proportionally = w->resize_proportionally;
6447 if (!NILP (w->buffer))
6449 /* Save w's value of point in the window configuration.
6450 If w is the selected window, then get the value of point
6451 from the buffer; pointm is garbage in the selected window. */
6452 if (EQ (window, selected_window))
6454 p->pointm = Fmake_marker ();
6455 set_marker_both (p->pointm, w->buffer,
6456 BUF_PT (XBUFFER (w->buffer)),
6457 BUF_PT_BYTE (XBUFFER (w->buffer)));
6459 else
6460 p->pointm = Fcopy_marker (w->pointm, Qnil);
6462 p->start = Fcopy_marker (w->start, Qnil);
6463 p->start_at_line_beg = w->start_at_line_beg;
6465 tem = XBUFFER (w->buffer)->mark;
6466 p->mark = Fcopy_marker (tem, Qnil);
6468 else
6470 p->pointm = Qnil;
6471 p->start = Qnil;
6472 p->mark = Qnil;
6473 p->start_at_line_beg = Qnil;
6476 if (NILP (w->parent))
6477 p->parent = Qnil;
6478 else
6479 p->parent = XWINDOW (w->parent)->temslot;
6481 if (NILP (w->prev))
6482 p->prev = Qnil;
6483 else
6484 p->prev = XWINDOW (w->prev)->temslot;
6486 if (!NILP (w->vchild))
6487 i = save_window_save (w->vchild, vector, i);
6488 if (!NILP (w->hchild))
6489 i = save_window_save (w->hchild, vector, i);
6492 return i;
6495 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6496 Scurrent_window_configuration, 0, 1, 0,
6497 doc: /* Return an object representing the current window configuration of FRAME.
6498 If FRAME is nil or omitted, use the selected frame.
6499 This describes the number of windows, their sizes and current buffers,
6500 and for each displayed buffer, where display starts, and the positions of
6501 point and mark. An exception is made for point in the current buffer:
6502 its value is -not- saved.
6503 This also records the currently selected frame, and FRAME's focus
6504 redirection (see `redirect-frame-focus'). */)
6505 (frame)
6506 Lisp_Object frame;
6508 register Lisp_Object tem;
6509 register int n_windows;
6510 register struct save_window_data *data;
6511 register int i;
6512 FRAME_PTR f;
6514 if (NILP (frame))
6515 frame = selected_frame;
6516 CHECK_LIVE_FRAME (frame);
6517 f = XFRAME (frame);
6519 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6520 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6521 PVEC_WINDOW_CONFIGURATION);
6523 data->frame_cols = FRAME_COLS (f);
6524 data->frame_lines = FRAME_LINES (f);
6525 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6526 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6527 data->selected_frame = selected_frame;
6528 data->current_window = FRAME_SELECTED_WINDOW (f);
6529 XSETBUFFER (data->current_buffer, current_buffer);
6530 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6531 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6532 data->root_window = FRAME_ROOT_WINDOW (f);
6533 data->focus_frame = FRAME_FOCUS_FRAME (f);
6534 tem = Fmake_vector (make_number (n_windows), Qnil);
6535 data->saved_windows = tem;
6536 for (i = 0; i < n_windows; i++)
6537 XVECTOR (tem)->contents[i]
6538 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6539 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6540 XSETWINDOW_CONFIGURATION (tem, data);
6541 return (tem);
6544 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
6545 0, UNEVALLED, 0,
6546 doc: /* Execute BODY, preserving window sizes and contents.
6547 Return the value of the last form in BODY.
6548 Restore which buffer appears in which window, where display starts,
6549 and the value of point and mark for each window.
6550 Also restore the choice of selected window.
6551 Also restore which buffer is current.
6552 Does not restore the value of point in current buffer.
6553 usage: (save-window-excursion BODY...) */)
6554 (args)
6555 Lisp_Object args;
6557 register Lisp_Object val;
6558 register int count = SPECPDL_INDEX ();
6560 record_unwind_protect (Fset_window_configuration,
6561 Fcurrent_window_configuration (Qnil));
6562 val = Fprogn (args);
6563 return unbind_to (count, val);
6568 /***********************************************************************
6569 Window Split Tree
6570 ***********************************************************************/
6572 static Lisp_Object
6573 window_tree (w)
6574 struct window *w;
6576 Lisp_Object tail = Qnil;
6577 Lisp_Object result = Qnil;
6579 while (w)
6581 Lisp_Object wn;
6583 XSETWINDOW (wn, w);
6584 if (!NILP (w->hchild))
6585 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6586 window_tree (XWINDOW (w->hchild))));
6587 else if (!NILP (w->vchild))
6588 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6589 window_tree (XWINDOW (w->vchild))));
6591 if (NILP (result))
6593 result = tail = Fcons (wn, Qnil);
6595 else
6597 XSETCDR (tail, Fcons (wn, Qnil));
6598 tail = XCDR (tail);
6601 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6604 return result;
6609 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6610 0, 1, 0,
6611 doc: /* Return the window tree for frame FRAME.
6613 The return value is a list of the form (ROOT MINI), where ROOT
6614 represents the window tree of the frame's root window, and MINI
6615 is the frame's minibuffer window.
6617 If the root window is not split, ROOT is the root window itself.
6618 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6619 horizontal split, and t for a vertical split, EDGES gives the combined
6620 size and position of the subwindows in the split, and the rest of the
6621 elements are the subwindows in the split. Each of the subwindows may
6622 again be a window or a list representing a window split, and so on.
6623 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6625 If FRAME is nil or omitted, return information on the currently
6626 selected frame. */)
6627 (frame)
6628 Lisp_Object frame;
6630 FRAME_PTR f;
6632 if (NILP (frame))
6633 frame = selected_frame;
6635 CHECK_FRAME (frame);
6636 f = XFRAME (frame);
6638 if (!FRAME_LIVE_P (f))
6639 return Qnil;
6641 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6645 /***********************************************************************
6646 Marginal Areas
6647 ***********************************************************************/
6649 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6650 2, 3, 0,
6651 doc: /* Set width of marginal areas of window WINDOW.
6652 If WINDOW is nil, set margins of the currently selected window.
6653 Second arg LEFT-WIDTH specifies the number of character cells to
6654 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6655 does the same for the right marginal area. A nil width parameter
6656 means no margin. */)
6657 (window, left_width, right_width)
6658 Lisp_Object window, left_width, right_width;
6660 struct window *w = decode_window (window);
6662 /* Translate negative or zero widths to nil.
6663 Margins that are too wide have to be checked elsewhere. */
6665 if (!NILP (left_width))
6667 CHECK_NUMBER (left_width);
6668 if (XINT (left_width) <= 0)
6669 left_width = Qnil;
6672 if (!NILP (right_width))
6674 CHECK_NUMBER (right_width);
6675 if (XINT (right_width) <= 0)
6676 right_width = Qnil;
6679 if (!EQ (w->left_margin_cols, left_width)
6680 || !EQ (w->right_margin_cols, right_width))
6682 w->left_margin_cols = left_width;
6683 w->right_margin_cols = right_width;
6685 adjust_window_margins (w);
6687 ++windows_or_buffers_changed;
6688 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6691 return Qnil;
6695 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6696 0, 1, 0,
6697 doc: /* Get width of marginal areas of window WINDOW.
6698 If WINDOW is omitted or nil, use the currently selected window.
6699 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6700 If a marginal area does not exist, its width will be returned
6701 as nil. */)
6702 (window)
6703 Lisp_Object window;
6705 struct window *w = decode_window (window);
6706 return Fcons (w->left_margin_cols, w->right_margin_cols);
6711 /***********************************************************************
6712 Fringes
6713 ***********************************************************************/
6715 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6716 2, 4, 0,
6717 doc: /* Set the fringe widths of window WINDOW.
6718 If WINDOW is nil, set the fringe widths of the currently selected
6719 window.
6720 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6721 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6722 fringe width. If a fringe width arg is nil, that means to use the
6723 frame's default fringe width. Default fringe widths can be set with
6724 the command `set-fringe-style'.
6725 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6726 outside of the display margins. By default, fringes are drawn between
6727 display marginal areas and the text area. */)
6728 (window, left_width, right_width, outside_margins)
6729 Lisp_Object window, left_width, right_width, outside_margins;
6731 struct window *w = decode_window (window);
6733 if (!NILP (left_width))
6734 CHECK_NATNUM (left_width);
6735 if (!NILP (right_width))
6736 CHECK_NATNUM (right_width);
6738 /* Do nothing on a tty. */
6739 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6740 && (!EQ (w->left_fringe_width, left_width)
6741 || !EQ (w->right_fringe_width, right_width)
6742 || !EQ (w->fringes_outside_margins, outside_margins)))
6744 w->left_fringe_width = left_width;
6745 w->right_fringe_width = right_width;
6746 w->fringes_outside_margins = outside_margins;
6748 adjust_window_margins (w);
6750 clear_glyph_matrix (w->current_matrix);
6751 w->window_end_valid = Qnil;
6753 ++windows_or_buffers_changed;
6754 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6757 return Qnil;
6761 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6762 0, 1, 0,
6763 doc: /* Get width of fringes of window WINDOW.
6764 If WINDOW is omitted or nil, use the currently selected window.
6765 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6766 (window)
6767 Lisp_Object window;
6769 struct window *w = decode_window (window);
6771 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6772 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6773 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6774 ? Qt : Qnil), Qnil)));
6779 /***********************************************************************
6780 Scroll bars
6781 ***********************************************************************/
6783 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6784 2, 4, 0,
6785 doc: /* Set width and type of scroll bars of window WINDOW.
6786 If window is nil, set scroll bars of the currently selected window.
6787 Second parameter WIDTH specifies the pixel width for the scroll bar;
6788 this is automatically adjusted to a multiple of the frame column width.
6789 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6790 bar: left, right, or nil.
6791 If WIDTH is nil, use the frame's scroll-bar width.
6792 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6793 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6794 (window, width, vertical_type, horizontal_type)
6795 Lisp_Object window, width, vertical_type, horizontal_type;
6797 struct window *w = decode_window (window);
6799 if (!NILP (width))
6801 CHECK_NATNUM (width);
6803 if (XINT (width) == 0)
6804 vertical_type = Qnil;
6807 if (!(NILP (vertical_type)
6808 || EQ (vertical_type, Qleft)
6809 || EQ (vertical_type, Qright)
6810 || EQ (vertical_type, Qt)))
6811 error ("Invalid type of vertical scroll bar");
6813 if (!EQ (w->scroll_bar_width, width)
6814 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6816 w->scroll_bar_width = width;
6817 w->vertical_scroll_bar_type = vertical_type;
6819 adjust_window_margins (w);
6821 clear_glyph_matrix (w->current_matrix);
6822 w->window_end_valid = Qnil;
6824 ++windows_or_buffers_changed;
6825 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6828 return Qnil;
6832 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6833 0, 1, 0,
6834 doc: /* Get width and type of scroll bars of window WINDOW.
6835 If WINDOW is omitted or nil, use the currently selected window.
6836 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6837 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6838 value. */)
6839 (window)
6840 Lisp_Object window;
6842 struct window *w = decode_window (window);
6843 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6844 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6845 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6846 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6847 Fcons (w->vertical_scroll_bar_type,
6848 Fcons (Qnil, Qnil))));
6853 /***********************************************************************
6854 Smooth scrolling
6855 ***********************************************************************/
6857 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6858 doc: /* Return the amount by which WINDOW is scrolled vertically.
6859 Use the selected window if WINDOW is nil or omitted.
6860 Normally, value is a multiple of the canonical character height of WINDOW;
6861 optional second arg PIXELS-P means value is measured in pixels. */)
6862 (window, pixels_p)
6863 Lisp_Object window, pixels_p;
6865 Lisp_Object result;
6866 struct frame *f;
6867 struct window *w;
6869 if (NILP (window))
6870 window = selected_window;
6871 else
6872 CHECK_WINDOW (window);
6873 w = XWINDOW (window);
6874 f = XFRAME (w->frame);
6876 if (FRAME_WINDOW_P (f))
6877 result = (NILP (pixels_p)
6878 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6879 : make_number (-w->vscroll));
6880 else
6881 result = make_number (0);
6882 return result;
6886 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6887 2, 3, 0,
6888 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6889 WINDOW nil means use the selected window. Normally, VSCROLL is a
6890 non-negative multiple of the canonical character height of WINDOW;
6891 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6892 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6893 corresponds to an integral number of pixels. The return value is the
6894 result of this rounding.
6895 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6896 (window, vscroll, pixels_p)
6897 Lisp_Object window, vscroll, pixels_p;
6899 struct window *w;
6900 struct frame *f;
6902 if (NILP (window))
6903 window = selected_window;
6904 else
6905 CHECK_WINDOW (window);
6906 CHECK_NUMBER_OR_FLOAT (vscroll);
6908 w = XWINDOW (window);
6909 f = XFRAME (w->frame);
6911 if (FRAME_WINDOW_P (f))
6913 int old_dy = w->vscroll;
6915 w->vscroll = - (NILP (pixels_p)
6916 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6917 : XFLOATINT (vscroll));
6918 w->vscroll = min (w->vscroll, 0);
6920 if (w->vscroll != old_dy)
6922 /* Adjust glyph matrix of the frame if the virtual display
6923 area becomes larger than before. */
6924 if (w->vscroll < 0 && w->vscroll < old_dy)
6925 adjust_glyphs (f);
6927 /* Prevent redisplay shortcuts. */
6928 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6932 return Fwindow_vscroll (window, pixels_p);
6936 /* Call FN for all leaf windows on frame F. FN is called with the
6937 first argument being a pointer to the leaf window, and with
6938 additional argument USER_DATA. Stops when FN returns 0. */
6940 void
6941 foreach_window (f, fn, user_data)
6942 struct frame *f;
6943 int (* fn) P_ ((struct window *, void *));
6944 void *user_data;
6946 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6947 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6948 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6952 /* Helper function for foreach_window. Call FN for all leaf windows
6953 reachable from W. FN is called with the first argument being a
6954 pointer to the leaf window, and with additional argument USER_DATA.
6955 Stop when FN returns 0. Value is 0 if stopped by FN. */
6957 static int
6958 foreach_window_1 (w, fn, user_data)
6959 struct window *w;
6960 int (* fn) P_ ((struct window *, void *));
6961 void *user_data;
6963 int cont;
6965 for (cont = 1; w && cont;)
6967 if (!NILP (w->hchild))
6968 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6969 else if (!NILP (w->vchild))
6970 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6971 else
6972 cont = fn (w, user_data);
6974 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6977 return cont;
6981 /* Freeze or unfreeze the window start of W unless it is a
6982 mini-window or the selected window. FREEZE_P non-null means freeze
6983 the window start. */
6985 static int
6986 freeze_window_start (w, freeze_p)
6987 struct window *w;
6988 void *freeze_p;
6990 if (MINI_WINDOW_P (w)
6991 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6992 && (w == XWINDOW (selected_window)
6993 || (MINI_WINDOW_P (XWINDOW (selected_window))
6994 && ! NILP (Vminibuf_scroll_window)
6995 && w == XWINDOW (Vminibuf_scroll_window)))))
6996 freeze_p = NULL;
6998 w->frozen_window_start_p = freeze_p != NULL;
6999 return 1;
7003 /* Freeze or unfreeze the window starts of all leaf windows on frame
7004 F, except the selected window and a mini-window. FREEZE_P non-zero
7005 means freeze the window start. */
7007 void
7008 freeze_window_starts (f, freeze_p)
7009 struct frame *f;
7010 int freeze_p;
7012 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
7016 /***********************************************************************
7017 Initialization
7018 ***********************************************************************/
7020 /* Return 1 if window configurations C1 and C2
7021 describe the same state of affairs. This is used by Fequal. */
7024 compare_window_configurations (c1, c2, ignore_positions)
7025 Lisp_Object c1, c2;
7026 int ignore_positions;
7028 register struct save_window_data *d1, *d2;
7029 struct Lisp_Vector *sw1, *sw2;
7030 int i;
7032 CHECK_WINDOW_CONFIGURATION (c1);
7033 CHECK_WINDOW_CONFIGURATION (c2);
7035 d1 = (struct save_window_data *) XVECTOR (c1);
7036 d2 = (struct save_window_data *) XVECTOR (c2);
7037 sw1 = XVECTOR (d1->saved_windows);
7038 sw2 = XVECTOR (d2->saved_windows);
7040 if (d1->frame_cols != d2->frame_cols)
7041 return 0;
7042 if (d1->frame_lines != d2->frame_lines)
7043 return 0;
7044 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
7045 return 0;
7046 if (! EQ (d1->selected_frame, d2->selected_frame))
7047 return 0;
7048 /* Don't compare the current_window field directly.
7049 Instead see w1_is_current and w2_is_current, below. */
7050 if (! EQ (d1->current_buffer, d2->current_buffer))
7051 return 0;
7052 if (! ignore_positions)
7054 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
7055 return 0;
7056 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
7057 return 0;
7059 /* Don't compare the root_window field.
7060 We don't require the two configurations
7061 to use the same window object,
7062 and the two root windows must be equivalent
7063 if everything else compares equal. */
7064 if (! EQ (d1->focus_frame, d2->focus_frame))
7065 return 0;
7067 /* Verify that the two confis have the same number of windows. */
7068 if (sw1->size != sw2->size)
7069 return 0;
7071 for (i = 0; i < sw1->size; i++)
7073 struct saved_window *p1, *p2;
7074 int w1_is_current, w2_is_current;
7076 p1 = SAVED_WINDOW_N (sw1, i);
7077 p2 = SAVED_WINDOW_N (sw2, i);
7079 /* Verify that the current windows in the two
7080 configurations correspond to each other. */
7081 w1_is_current = EQ (d1->current_window, p1->window);
7082 w2_is_current = EQ (d2->current_window, p2->window);
7084 if (w1_is_current != w2_is_current)
7085 return 0;
7087 /* Verify that the corresponding windows do match. */
7088 if (! EQ (p1->buffer, p2->buffer))
7089 return 0;
7090 if (! EQ (p1->left_col, p2->left_col))
7091 return 0;
7092 if (! EQ (p1->top_line, p2->top_line))
7093 return 0;
7094 if (! EQ (p1->total_cols, p2->total_cols))
7095 return 0;
7096 if (! EQ (p1->total_lines, p2->total_lines))
7097 return 0;
7098 if (! EQ (p1->display_table, p2->display_table))
7099 return 0;
7100 if (! EQ (p1->parent, p2->parent))
7101 return 0;
7102 if (! EQ (p1->prev, p2->prev))
7103 return 0;
7104 if (! ignore_positions)
7106 if (! EQ (p1->hscroll, p2->hscroll))
7107 return 0;
7108 if (!EQ (p1->min_hscroll, p2->min_hscroll))
7109 return 0;
7110 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
7111 return 0;
7112 if (NILP (Fequal (p1->start, p2->start)))
7113 return 0;
7114 if (NILP (Fequal (p1->pointm, p2->pointm)))
7115 return 0;
7116 if (NILP (Fequal (p1->mark, p2->mark)))
7117 return 0;
7119 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
7120 return 0;
7121 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
7122 return 0;
7123 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
7124 return 0;
7125 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
7126 return 0;
7127 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
7128 return 0;
7129 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
7130 return 0;
7131 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
7132 return 0;
7135 return 1;
7138 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
7139 Scompare_window_configurations, 2, 2, 0,
7140 doc: /* Compare two window configurations as regards the structure of windows.
7141 This function ignores details such as the values of point and mark
7142 and scrolling positions. */)
7143 (x, y)
7144 Lisp_Object x, y;
7146 if (compare_window_configurations (x, y, 1))
7147 return Qt;
7148 return Qnil;
7151 void
7152 init_window_once ()
7154 struct frame *f = make_initial_frame ();
7155 XSETFRAME (selected_frame, f);
7156 Vterminal_frame = selected_frame;
7157 minibuf_window = f->minibuffer_window;
7158 selected_window = f->selected_window;
7159 last_nonminibuf_frame = f;
7161 window_initialized = 1;
7164 void
7165 init_window ()
7167 Vwindow_list = Qnil;
7170 void
7171 syms_of_window ()
7173 Qscroll_up = intern_c_string ("scroll-up");
7174 staticpro (&Qscroll_up);
7176 Qscroll_down = intern_c_string ("scroll-down");
7177 staticpro (&Qscroll_down);
7179 Qwindow_size_fixed = intern_c_string ("window-size-fixed");
7180 staticpro (&Qwindow_size_fixed);
7181 Fset (Qwindow_size_fixed, Qnil);
7183 staticpro (&Qwindow_configuration_change_hook);
7184 Qwindow_configuration_change_hook
7185 = intern_c_string ("window-configuration-change-hook");
7187 Qwindowp = intern_c_string ("windowp");
7188 staticpro (&Qwindowp);
7190 Qwindow_configuration_p = intern_c_string ("window-configuration-p");
7191 staticpro (&Qwindow_configuration_p);
7193 Qwindow_live_p = intern_c_string ("window-live-p");
7194 staticpro (&Qwindow_live_p);
7196 Qdisplay_buffer = intern_c_string ("display-buffer");
7197 staticpro (&Qdisplay_buffer);
7199 Qtemp_buffer_show_hook = intern_c_string ("temp-buffer-show-hook");
7200 staticpro (&Qtemp_buffer_show_hook);
7202 staticpro (&Vwindow_list);
7204 minibuf_selected_window = Qnil;
7205 staticpro (&minibuf_selected_window);
7207 window_scroll_pixel_based_preserve_x = -1;
7208 window_scroll_pixel_based_preserve_y = -1;
7209 window_scroll_preserve_hpos = -1;
7210 window_scroll_preserve_vpos = -1;
7212 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
7213 doc: /* Non-nil means call as function to display a help buffer.
7214 The function is called with one argument, the buffer to be displayed.
7215 Used by `with-output-to-temp-buffer'.
7216 If this function is used, then it must do the entire job of showing
7217 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7218 Vtemp_buffer_show_function = Qnil;
7220 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
7221 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7222 Vminibuf_scroll_window = Qnil;
7224 DEFVAR_BOOL ("mode-line-in-non-selected-windows", &mode_line_in_non_selected_windows,
7225 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7226 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7227 is displayed in the `mode-line' face. */);
7228 mode_line_in_non_selected_windows = 1;
7230 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
7231 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7232 Vother_window_scroll_buffer = Qnil;
7234 DEFVAR_BOOL ("auto-window-vscroll", &auto_window_vscroll_p,
7235 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7236 auto_window_vscroll_p = 1;
7238 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
7239 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7240 next_screen_context_lines = 2;
7242 DEFVAR_INT ("window-min-height", &window_min_height,
7243 doc: /* Allow deleting windows less than this tall.
7244 The value is measured in line units. If a window wants a modeline it
7245 is counted as one line.
7247 Emacs honors settings of this variable when enlarging or shrinking
7248 windows vertically. A value less than 1 is invalid. */);
7249 window_min_height = 4;
7251 DEFVAR_INT ("window-min-width", &window_min_width,
7252 doc: /* Allow deleting windows less than this wide.
7253 The value is measured in characters and includes any fringes or
7254 the scrollbar.
7256 Emacs honors settings of this variable when enlarging or shrinking
7257 windows horizontally. A value less than 2 is invalid. */);
7258 window_min_width = 10;
7260 DEFVAR_LISP ("scroll-preserve-screen-position",
7261 &Vscroll_preserve_screen_position,
7262 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
7263 A value of nil means point does not keep its screen position except
7264 at the scroll margin or window boundary respectively.
7265 A value of t means point keeps its screen position if the scroll
7266 command moved it vertically out of the window, e.g. when scrolling
7267 by full screens.
7268 Any other value means point always keeps its screen position. */);
7269 Vscroll_preserve_screen_position = Qnil;
7271 DEFVAR_LISP ("window-point-insertion-type", &Vwindow_point_insertion_type,
7272 doc: /* Type of marker to use for `window-point'. */);
7273 Vwindow_point_insertion_type = Qnil;
7275 DEFVAR_LISP ("window-configuration-change-hook",
7276 &Vwindow_configuration_change_hook,
7277 doc: /* Functions to call when window configuration changes.
7278 The buffer-local part is run once per window, with the relevant window
7279 selected; while the global part is run only once for the modified frame,
7280 with the relevant frame selected. */);
7281 Vwindow_configuration_change_hook = Qnil;
7283 DEFVAR_LISP ("recenter-redisplay", &Vrecenter_redisplay,
7284 doc: /* If non-nil, then the `recenter' command with a nil argument
7285 the entire frame to be redrawn; the special value `tty' causes the
7286 frame to be redrawn only if it is a tty frame. */);
7287 Vrecenter_redisplay = Qtty;
7290 defsubr (&Sselected_window);
7291 defsubr (&Sminibuffer_window);
7292 defsubr (&Swindow_minibuffer_p);
7293 defsubr (&Swindowp);
7294 defsubr (&Swindow_live_p);
7295 defsubr (&Spos_visible_in_window_p);
7296 defsubr (&Swindow_line_height);
7297 defsubr (&Swindow_buffer);
7298 defsubr (&Swindow_height);
7299 defsubr (&Swindow_width);
7300 defsubr (&Swindow_full_width_p);
7301 defsubr (&Swindow_hscroll);
7302 defsubr (&Sset_window_hscroll);
7303 defsubr (&Swindow_redisplay_end_trigger);
7304 defsubr (&Sset_window_redisplay_end_trigger);
7305 defsubr (&Swindow_edges);
7306 defsubr (&Swindow_pixel_edges);
7307 defsubr (&Swindow_inside_edges);
7308 defsubr (&Swindow_inside_pixel_edges);
7309 defsubr (&Scoordinates_in_window_p);
7310 defsubr (&Swindow_at);
7311 defsubr (&Swindow_point);
7312 defsubr (&Swindow_start);
7313 defsubr (&Swindow_end);
7314 defsubr (&Sset_window_point);
7315 defsubr (&Sset_window_start);
7316 defsubr (&Swindow_dedicated_p);
7317 defsubr (&Sset_window_dedicated_p);
7318 defsubr (&Swindow_display_table);
7319 defsubr (&Sset_window_display_table);
7320 defsubr (&Snext_window);
7321 defsubr (&Sprevious_window);
7322 defsubr (&Sother_window);
7323 defsubr (&Sget_lru_window);
7324 defsubr (&Sget_largest_window);
7325 defsubr (&Sget_buffer_window);
7326 defsubr (&Sdelete_other_windows);
7327 defsubr (&Sdelete_windows_on);
7328 defsubr (&Sreplace_buffer_in_windows);
7329 defsubr (&Sdelete_window);
7330 defsubr (&Sset_window_buffer);
7331 defsubr (&Sselect_window);
7332 defsubr (&Sforce_window_update);
7333 defsubr (&Ssplit_window);
7334 defsubr (&Senlarge_window);
7335 defsubr (&Sshrink_window);
7336 defsubr (&Sadjust_window_trailing_edge);
7337 defsubr (&Sscroll_up);
7338 defsubr (&Sscroll_down);
7339 defsubr (&Sscroll_left);
7340 defsubr (&Sscroll_right);
7341 defsubr (&Sother_window_for_scrolling);
7342 defsubr (&Sscroll_other_window);
7343 defsubr (&Sminibuffer_selected_window);
7344 defsubr (&Srecenter);
7345 defsubr (&Swindow_text_height);
7346 defsubr (&Smove_to_window_line);
7347 defsubr (&Swindow_configuration_p);
7348 defsubr (&Swindow_configuration_frame);
7349 defsubr (&Sset_window_configuration);
7350 defsubr (&Scurrent_window_configuration);
7351 defsubr (&Ssave_window_excursion);
7352 defsubr (&Swindow_tree);
7353 defsubr (&Sset_window_margins);
7354 defsubr (&Swindow_margins);
7355 defsubr (&Sset_window_fringes);
7356 defsubr (&Swindow_fringes);
7357 defsubr (&Sset_window_scroll_bars);
7358 defsubr (&Swindow_scroll_bars);
7359 defsubr (&Swindow_vscroll);
7360 defsubr (&Sset_window_vscroll);
7361 defsubr (&Scompare_window_configurations);
7362 defsubr (&Swindow_list);
7363 defsubr (&Swindow_parameters);
7364 defsubr (&Swindow_parameter);
7365 defsubr (&Sset_window_parameter);
7369 void
7370 keys_of_window ()
7372 initial_define_key (control_x_map, '1', "delete-other-windows");
7373 initial_define_key (control_x_map, '2', "split-window");
7374 initial_define_key (control_x_map, '0', "delete-window");
7375 initial_define_key (control_x_map, 'o', "other-window");
7376 initial_define_key (control_x_map, '^', "enlarge-window");
7377 initial_define_key (control_x_map, '<', "scroll-left");
7378 initial_define_key (control_x_map, '>', "scroll-right");
7380 initial_define_key (global_map, Ctl ('V'), "scroll-up");
7381 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7382 initial_define_key (meta_map, 'v', "scroll-down");
7384 initial_define_key (global_map, Ctl('L'), "recenter");
7385 initial_define_key (meta_map, 'r', "move-to-window-line");
7388 /* arch-tag: 90a9c576-0590-48f1-a5f1-6c96a0452d9f
7389 (do not change this comment) */