Merge from trunk
[emacs.git] / src / window.c
blob4bd533c22acfa8df8fd3aab84be168b52f76d749
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2011
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <setjmp.h>
25 #include "lisp.h"
26 #include "buffer.h"
27 #include "keyboard.h"
28 #include "keymap.h"
29 #include "frame.h"
30 #include "window.h"
31 #include "commands.h"
32 #include "indent.h"
33 #include "termchar.h"
34 #include "disptab.h"
35 #include "dispextern.h"
36 #include "blockinput.h"
37 #include "intervals.h"
38 #include "termhooks.h" /* For FRAME_TERMINAL. */
40 #ifdef HAVE_X_WINDOWS
41 #include "xterm.h"
42 #endif /* HAVE_X_WINDOWS */
43 #ifdef WINDOWSNT
44 #include "w32term.h"
45 #endif
46 #ifdef MSDOS
47 #include "msdos.h"
48 #endif
49 #ifdef HAVE_NS
50 #include "nsterm.h"
51 #endif
53 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
54 Lisp_Object Qdisplay_buffer;
55 Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
56 Lisp_Object Qwindow_size_fixed;
58 static int displayed_window_lines (struct window *);
59 static struct window *decode_window (Lisp_Object);
60 static int count_windows (struct window *);
61 static int get_leaf_windows (struct window *, struct window **, int);
62 static void window_scroll (Lisp_Object, int, int, int);
63 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
64 static void window_scroll_line_based (Lisp_Object, int, int, int);
65 static int window_min_size_1 (struct window *, int, int);
66 static int window_min_size_2 (struct window *, int, int);
67 static int window_min_size (struct window *, int, int, int, int *);
68 static void size_window (Lisp_Object, int, int, int, int, int);
69 static int freeze_window_start (struct window *, void *);
70 static int window_fixed_size_p (struct window *, int, int);
71 static void enlarge_window (Lisp_Object, int, int);
72 static Lisp_Object window_list (void);
73 static int add_window_to_list (struct window *, void *);
74 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
75 Lisp_Object);
76 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
77 Lisp_Object, int);
78 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
79 Lisp_Object *);
80 static void foreach_window (struct frame *,
81 int (* fn) (struct window *, void *),
82 void *);
83 static int foreach_window_1 (struct window *,
84 int (* fn) (struct window *, void *),
85 void *);
86 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
87 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
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 /* Hook run at end of temp_output_buffer_show. */
119 Lisp_Object Qtemp_buffer_show_hook;
121 /* Incremented for each window created. */
123 static int sequence_number;
125 /* Nonzero after init_window_once has finished. */
127 static int window_initialized;
129 /* Hook to run when window config changes. */
131 static Lisp_Object Qwindow_configuration_change_hook;
132 /* Incremented by 1 whenever a window is deleted. */
134 int window_deletion_count;
136 /* Used by the function window_scroll_pixel_based */
138 static int window_scroll_pixel_based_preserve_x;
139 static int window_scroll_pixel_based_preserve_y;
141 /* Same for window_scroll_line_based. */
143 static int window_scroll_preserve_hpos;
144 static int window_scroll_preserve_vpos;
146 #if 0 /* This isn't used anywhere. */
147 /* Nonzero means we can split a frame even if it is "unsplittable". */
148 static int inhibit_frame_unsplittable;
149 #endif
152 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
153 doc: /* Return t if OBJECT is a window. */)
154 (Lisp_Object object)
156 return WINDOWP (object) ? Qt : Qnil;
159 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
160 doc: /* Return t if OBJECT is a window which is currently visible. */)
161 (Lisp_Object object)
163 return WINDOW_LIVE_P (object) ? Qt : Qnil;
166 Lisp_Object
167 make_window (void)
169 Lisp_Object val;
170 register struct window *p;
172 p = allocate_window ();
173 ++sequence_number;
174 XSETFASTINT (p->sequence_number, sequence_number);
175 XSETFASTINT (p->left_col, 0);
176 XSETFASTINT (p->top_line, 0);
177 XSETFASTINT (p->total_lines, 0);
178 XSETFASTINT (p->total_cols, 0);
179 XSETFASTINT (p->hscroll, 0);
180 XSETFASTINT (p->min_hscroll, 0);
181 p->orig_top_line = p->orig_total_lines = Qnil;
182 p->start = Fmake_marker ();
183 p->pointm = Fmake_marker ();
184 XSETFASTINT (p->use_time, 0);
185 p->frame = Qnil;
186 p->display_table = Qnil;
187 p->dedicated = Qnil;
188 p->window_parameters = Qnil;
189 p->pseudo_window_p = 0;
190 memset (&p->cursor, 0, sizeof (p->cursor));
191 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
192 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
193 p->desired_matrix = p->current_matrix = 0;
194 p->nrows_scale_factor = p->ncols_scale_factor = 1;
195 p->phys_cursor_type = -1;
196 p->phys_cursor_width = -1;
197 p->must_be_updated_p = 0;
198 XSETFASTINT (p->window_end_vpos, 0);
199 XSETFASTINT (p->window_end_pos, 0);
200 p->window_end_valid = Qnil;
201 p->vscroll = 0;
202 XSETWINDOW (val, p);
203 XSETFASTINT (p->last_point, 0);
204 p->frozen_window_start_p = 0;
205 p->last_cursor_off_p = p->cursor_off_p = 0;
206 p->left_margin_cols = Qnil;
207 p->right_margin_cols = Qnil;
208 p->left_fringe_width = Qnil;
209 p->right_fringe_width = Qnil;
210 p->fringes_outside_margins = Qnil;
211 p->scroll_bar_width = Qnil;
212 p->vertical_scroll_bar_type = Qt;
213 p->resize_proportionally = Qnil;
215 Vwindow_list = Qnil;
216 return val;
219 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
220 doc: /* Return the window that the cursor now appears in and commands apply to. */)
221 (void)
223 return selected_window;
226 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
227 doc: /* Return the window used now for minibuffers.
228 If the optional argument FRAME is specified, return the minibuffer window
229 used by that frame. */)
230 (Lisp_Object frame)
232 if (NILP (frame))
233 frame = selected_frame;
234 CHECK_LIVE_FRAME (frame);
235 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
238 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
239 doc: /* Return non-nil if WINDOW is a minibuffer window.
240 WINDOW defaults to the selected window. */)
241 (Lisp_Object window)
243 struct window *w = decode_window (window);
244 return MINI_WINDOW_P (w) ? Qt : Qnil;
248 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
249 Spos_visible_in_window_p, 0, 3, 0,
250 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
251 Return nil if that position is scrolled vertically out of view.
252 If a character is only partially visible, nil is returned, unless the
253 optional argument PARTIALLY is non-nil.
254 If POS is only out of view because of horizontal scrolling, return non-nil.
255 If POS is t, it specifies the position of the last visible glyph in WINDOW.
256 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
258 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
259 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
260 where X and Y are the pixel coordinates relative to the top left corner
261 of the window. The remaining elements are omitted if the character after
262 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
263 off-window at the top and bottom of the row, ROWH is the height of the
264 display row, and VPOS is the row number (0-based) containing POS. */)
265 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
267 register struct window *w;
268 register EMACS_INT posint;
269 register struct buffer *buf;
270 struct text_pos top;
271 Lisp_Object in_window = Qnil;
272 int rtop, rbot, rowh, vpos, fully_p = 1;
273 int x, y;
275 w = decode_window (window);
276 buf = XBUFFER (w->buffer);
277 SET_TEXT_POS_FROM_MARKER (top, w->start);
279 if (EQ (pos, Qt))
280 posint = -1;
281 else if (!NILP (pos))
283 CHECK_NUMBER_COERCE_MARKER (pos);
284 posint = XINT (pos);
286 else if (w == XWINDOW (selected_window))
287 posint = PT;
288 else
289 posint = XMARKER (w->pointm)->charpos;
291 /* If position is above window start or outside buffer boundaries,
292 or if window start is out of range, position is not visible. */
293 if ((EQ (pos, Qt)
294 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
295 && CHARPOS (top) >= BUF_BEGV (buf)
296 && CHARPOS (top) <= BUF_ZV (buf)
297 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
298 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
299 in_window = Qt;
301 if (!NILP (in_window) && !NILP (partially))
303 Lisp_Object part = Qnil;
304 if (!fully_p)
305 part = list4 (make_number (rtop), make_number (rbot),
306 make_number (rowh), make_number (vpos));
307 in_window = Fcons (make_number (x),
308 Fcons (make_number (y), part));
311 return in_window;
314 DEFUN ("window-line-height", Fwindow_line_height,
315 Swindow_line_height, 0, 2, 0,
316 doc: /* Return height in pixels of text line LINE in window WINDOW.
317 If WINDOW is nil or omitted, use selected window.
319 Return height of current line if LINE is omitted or nil. Return height of
320 header or mode line if LINE is `header-line' and `mode-line'.
321 Otherwise, LINE is a text line number starting from 0. A negative number
322 counts from the end of the window.
324 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
325 in pixels of the visible part of the line, VPOS and YPOS are the
326 vertical position in lines and pixels of the line, relative to the top
327 of the first text line, and OFFBOT is the number of off-window pixels at
328 the bottom of the text line. If there are off-window pixels at the top
329 of the (first) text line, YPOS is negative.
331 Return nil if window display is not up-to-date. In that case, use
332 `pos-visible-in-window-p' to obtain the information. */)
333 (Lisp_Object line, Lisp_Object window)
335 register struct window *w;
336 register struct buffer *b;
337 struct glyph_row *row, *end_row;
338 int max_y, crop, i, n;
340 w = decode_window (window);
342 if (noninteractive
343 || w->pseudo_window_p)
344 return Qnil;
346 CHECK_BUFFER (w->buffer);
347 b = XBUFFER (w->buffer);
349 /* Fail if current matrix is not up-to-date. */
350 if (NILP (w->window_end_valid)
351 || current_buffer->clip_changed
352 || current_buffer->prevent_redisplay_optimizations_p
353 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
354 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
355 return Qnil;
357 if (NILP (line))
359 i = w->cursor.vpos;
360 if (i < 0 || i >= w->current_matrix->nrows
361 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
362 return Qnil;
363 max_y = window_text_bottom_y (w);
364 goto found_row;
367 if (EQ (line, Qheader_line))
369 if (!WINDOW_WANTS_HEADER_LINE_P (w))
370 return Qnil;
371 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
372 if (!row->enabled_p)
373 return Qnil;
374 return list4 (make_number (row->height),
375 make_number (0), make_number (0),
376 make_number (0));
379 if (EQ (line, Qmode_line))
381 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
382 if (!row->enabled_p)
383 return Qnil;
384 return list4 (make_number (row->height),
385 make_number (0), /* not accurate */
386 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
387 + window_text_bottom_y (w)),
388 make_number (0));
391 CHECK_NUMBER (line);
392 n = XINT (line);
394 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
395 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
396 max_y = window_text_bottom_y (w);
397 i = 0;
399 while ((n < 0 || i < n)
400 && row <= end_row && row->enabled_p
401 && row->y + row->height < max_y)
402 row++, i++;
404 if (row > end_row || !row->enabled_p)
405 return Qnil;
407 if (++n < 0)
409 if (-n > i)
410 return Qnil;
411 row += n;
412 i += n;
415 found_row:
416 crop = max (0, (row->y + row->height) - max_y);
417 return list4 (make_number (row->height + min (0, row->y) - crop),
418 make_number (i),
419 make_number (row->y),
420 make_number (crop));
425 static struct window *
426 decode_window (register Lisp_Object window)
428 if (NILP (window))
429 return XWINDOW (selected_window);
431 CHECK_LIVE_WINDOW (window);
432 return XWINDOW (window);
435 static struct window *
436 decode_any_window (register Lisp_Object window)
438 if (NILP (window))
439 return XWINDOW (selected_window);
441 CHECK_WINDOW (window);
442 return XWINDOW (window);
445 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
446 doc: /* Return the buffer that WINDOW is displaying.
447 WINDOW defaults to the selected window. */)
448 (Lisp_Object window)
450 return decode_window (window)->buffer;
453 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
454 doc: /* Return the number of lines in WINDOW.
455 WINDOW defaults to the selected window.
457 The return value includes WINDOW's mode line and header line, if any.
459 Note: The function does not take into account the value of `line-spacing'
460 when calculating the number of lines in WINDOW. */)
461 (Lisp_Object window)
463 return decode_any_window (window)->total_lines;
466 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
467 doc: /* Return the number of display columns in WINDOW.
468 WINDOW defaults to the selected window.
470 Note: The return value is the number of columns available for text in
471 WINDOW. If you want to find out how many columns WINDOW takes up, use
472 (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
473 (Lisp_Object window)
475 return make_number (window_box_text_cols (decode_any_window (window)));
478 DEFUN ("window-full-width-p", Fwindow_full_width_p, Swindow_full_width_p, 0, 1, 0,
479 doc: /* Return t if WINDOW is as wide as its frame.
480 WINDOW defaults to the selected window. */)
481 (Lisp_Object window)
483 return WINDOW_FULL_WIDTH_P (decode_any_window (window)) ? Qt : Qnil;
486 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
487 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
488 WINDOW defaults to the selected window. */)
489 (Lisp_Object window)
491 return decode_window (window)->hscroll;
494 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
495 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
496 Return NCOL. NCOL should be zero or positive.
498 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
499 window so that the location of point moves off-window. */)
500 (Lisp_Object window, Lisp_Object ncol)
502 struct window *w = decode_window (window);
503 int hscroll;
505 CHECK_NUMBER (ncol);
506 hscroll = max (0, XINT (ncol));
508 /* Prevent redisplay shortcuts when changing the hscroll. */
509 if (XINT (w->hscroll) != hscroll)
510 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
512 w->hscroll = make_number (hscroll);
513 return ncol;
516 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
517 Swindow_redisplay_end_trigger, 0, 1, 0,
518 doc: /* Return WINDOW's redisplay end trigger value.
519 WINDOW defaults to the selected window.
520 See `set-window-redisplay-end-trigger' for more information. */)
521 (Lisp_Object window)
523 return decode_window (window)->redisplay_end_trigger;
526 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
527 Sset_window_redisplay_end_trigger, 2, 2, 0,
528 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
529 VALUE should be a buffer position (typically a marker) or nil.
530 If it is a buffer position, then if redisplay in WINDOW reaches a position
531 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
532 with two arguments: WINDOW, and the end trigger value.
533 Afterwards the end-trigger value is reset to nil. */)
534 (register Lisp_Object window, Lisp_Object value)
536 register struct window *w;
538 w = decode_window (window);
539 w->redisplay_end_trigger = value;
540 return value;
543 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
544 doc: /* Return a list of the edge coordinates of WINDOW.
545 The list has the form (LEFT TOP RIGHT BOTTOM).
546 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
547 all relative to 0, 0 at top left corner of frame.
549 RIGHT is one more than the rightmost column occupied by WINDOW.
550 BOTTOM is one more than the bottommost row occupied by WINDOW.
551 The edges include the space used by WINDOW's scroll bar, display
552 margins, fringes, header line, and/or mode line. For the edges of
553 just the text area, use `window-inside-edges'. */)
554 (Lisp_Object window)
556 register struct window *w = decode_any_window (window);
558 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
559 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
560 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
561 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
562 Qnil))));
565 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
566 doc: /* Return a list of the edge pixel coordinates of WINDOW.
567 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
568 the top left corner of the frame.
570 RIGHT is one more than the rightmost x position occupied by WINDOW.
571 BOTTOM is one more than the bottommost y position occupied by WINDOW.
572 The pixel edges include the space used by WINDOW's scroll bar, display
573 margins, fringes, header line, and/or mode line. For the pixel edges
574 of just the text area, use `window-inside-pixel-edges'. */)
575 (Lisp_Object window)
577 register struct window *w = decode_any_window (window);
579 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
580 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
581 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
582 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
583 Qnil))));
586 static void
587 calc_absolute_offset(struct window *w, int *add_x, int *add_y)
589 struct frame *f = XFRAME (w->frame);
590 *add_y = f->top_pos;
591 #ifdef FRAME_MENUBAR_HEIGHT
592 *add_y += FRAME_MENUBAR_HEIGHT (f);
593 #endif
594 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
595 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
596 #elif FRAME_TOOLBAR_HEIGHT
597 *add_y += FRAME_TOOLBAR_HEIGHT (f);
598 #endif
599 #ifdef FRAME_NS_TITLEBAR_HEIGHT
600 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
601 #endif
602 *add_x = f->left_pos;
603 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
604 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
605 #endif
608 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
609 Swindow_absolute_pixel_edges, 0, 1, 0,
610 doc: /* Return a list of the edge pixel coordinates of WINDOW.
611 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
612 the top left corner of the display.
614 RIGHT is one more than the rightmost x position occupied by WINDOW.
615 BOTTOM is one more than the bottommost y position occupied by WINDOW.
616 The pixel edges include the space used by WINDOW's scroll bar, display
617 margins, fringes, header line, and/or mode line. For the pixel edges
618 of just the text area, use `window-inside-absolute-pixel-edges'. */)
619 (Lisp_Object window)
621 register struct window *w = decode_any_window (window);
622 int add_x, add_y;
623 calc_absolute_offset (w, &add_x, &add_y);
625 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
626 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
627 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
628 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
629 Qnil))));
632 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
633 doc: /* Return a list of the edge coordinates of WINDOW.
634 The list has the form (LEFT TOP RIGHT BOTTOM).
635 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
636 all relative to 0, 0 at top left corner of frame.
638 RIGHT is one more than the rightmost column of WINDOW's text area.
639 BOTTOM is one more than the bottommost row of WINDOW's text area.
640 The inside edges do not include the space used by the WINDOW's scroll
641 bar, display margins, fringes, header line, and/or mode line. */)
642 (Lisp_Object window)
644 register struct window *w = decode_any_window (window);
646 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
647 + WINDOW_LEFT_MARGIN_COLS (w)
648 + WINDOW_LEFT_FRINGE_COLS (w)),
649 make_number (WINDOW_TOP_EDGE_LINE (w)
650 + WINDOW_HEADER_LINE_LINES (w)),
651 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
652 - WINDOW_RIGHT_MARGIN_COLS (w)
653 - WINDOW_RIGHT_FRINGE_COLS (w)),
654 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
655 - WINDOW_MODE_LINE_LINES (w)));
658 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
659 doc: /* Return a list of the edge pixel coordinates of WINDOW.
660 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
661 the top left corner of the frame.
663 RIGHT is one more than the rightmost x position of WINDOW's text area.
664 BOTTOM is one more than the bottommost y position of WINDOW's text area.
665 The inside edges do not include the space used by WINDOW's scroll bar,
666 display margins, fringes, header line, and/or mode line. */)
667 (Lisp_Object window)
669 register struct window *w = decode_any_window (window);
671 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
672 + WINDOW_LEFT_MARGIN_WIDTH (w)
673 + WINDOW_LEFT_FRINGE_WIDTH (w)),
674 make_number (WINDOW_TOP_EDGE_Y (w)
675 + WINDOW_HEADER_LINE_HEIGHT (w)),
676 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
677 - WINDOW_RIGHT_MARGIN_WIDTH (w)
678 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
679 make_number (WINDOW_BOTTOM_EDGE_Y (w)
680 - WINDOW_MODE_LINE_HEIGHT (w)));
683 DEFUN ("window-inside-absolute-pixel-edges",
684 Fwindow_inside_absolute_pixel_edges,
685 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
686 doc: /* Return a list of the edge pixel coordinates of WINDOW.
687 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
688 the top left corner of the display.
690 RIGHT is one more than the rightmost x position of WINDOW's text area.
691 BOTTOM is one more than the bottommost y position of WINDOW's text area.
692 The inside edges do not include the space used by WINDOW's scroll bar,
693 display margins, fringes, header line, and/or mode line. */)
694 (Lisp_Object window)
696 register struct window *w = decode_any_window (window);
697 int add_x, add_y;
698 calc_absolute_offset (w, &add_x, &add_y);
700 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
701 + WINDOW_LEFT_MARGIN_WIDTH (w)
702 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
703 make_number (WINDOW_TOP_EDGE_Y (w)
704 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
705 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
706 - WINDOW_RIGHT_MARGIN_WIDTH (w)
707 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
708 make_number (WINDOW_BOTTOM_EDGE_Y (w)
709 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
712 /* Test if the character at column X, row Y is within window W.
713 If it is not, return ON_NOTHING;
714 if it is in the window's text area, return ON_TEXT;
715 if it is on the window's modeline, return ON_MODE_LINE;
716 if it is on the border between the window and its right sibling,
717 return ON_VERTICAL_BORDER.
718 if it is on a scroll bar, 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;
722 if it is in the marginal area to the left/right of the window,
723 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
725 X and Y are frame relative pixel coordinates. */
727 static enum window_part
728 coordinates_in_window (register struct window *w, int x, int y)
730 struct frame *f = XFRAME (WINDOW_FRAME (w));
731 int left_x, right_x;
732 enum window_part part;
733 int ux = FRAME_COLUMN_WIDTH (f);
734 int x0 = WINDOW_LEFT_EDGE_X (w);
735 int x1 = WINDOW_RIGHT_EDGE_X (w);
736 /* The width of the area where the vertical line can be dragged.
737 (Between mode lines for instance. */
738 int grabbable_width = ux;
739 int lmargin_width, rmargin_width, text_left, text_right;
740 int top_y = WINDOW_TOP_EDGE_Y (w);
741 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
743 /* Outside any interesting row? */
744 if (y < top_y || y >= bottom_y)
745 return ON_NOTHING;
747 /* In what's below, we subtract 1 when computing right_x because we
748 want the rightmost pixel, which is given by left_pixel+width-1. */
749 if (w->pseudo_window_p)
751 left_x = 0;
752 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
754 else
756 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
757 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
760 /* On the mode line or header line? If it's near the start of
761 the mode or header line of window that's has a horizontal
762 sibling, say it's on the vertical line. That's to be able
763 to resize windows horizontally in case we're using toolkit
764 scroll bars. */
766 if (WINDOW_WANTS_MODELINE_P (w)
767 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
769 part = ON_MODE_LINE;
771 header_vertical_border_check:
772 /* We're somewhere on the mode line. We consider the place
773 between mode lines of horizontally adjacent mode lines
774 as the vertical border. If scroll bars on the left,
775 return the right window. */
776 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
777 || WINDOW_RIGHTMOST_P (w))
778 && !WINDOW_LEFTMOST_P (w)
779 && eabs (x - x0) < grabbable_width)
780 return ON_VERTICAL_BORDER;
782 /* Make sure we're not at the rightmost position of a
783 mode-/header-line and there's yet another window on the
784 right. (Bug#1372) */
785 else if ((WINDOW_RIGHTMOST_P (w) || x < x1)
786 && eabs (x - x1) < grabbable_width)
787 return ON_VERTICAL_BORDER;
789 if (x < x0 || x >= x1)
790 return ON_NOTHING;
792 return part;
795 if (WINDOW_WANTS_HEADER_LINE_P (w)
796 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
798 part = ON_HEADER_LINE;
799 goto header_vertical_border_check;
802 if (x < x0 || x >= x1) return ON_NOTHING;
804 /* Outside any interesting column? */
805 if (x < left_x || x > right_x)
806 return ON_SCROLL_BAR;
808 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
809 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
811 text_left = window_box_left (w, TEXT_AREA);
812 text_right = text_left + window_box_width (w, TEXT_AREA);
814 if (FRAME_WINDOW_P (f))
816 if (!w->pseudo_window_p
817 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
818 && !WINDOW_RIGHTMOST_P (w)
819 && (eabs (x - right_x) < grabbable_width))
820 return ON_VERTICAL_BORDER;
822 /* Need to say "x > right_x" rather than >=, since on character
823 terminals, the vertical line's x coordinate is right_x. */
824 else if (!w->pseudo_window_p
825 && !WINDOW_RIGHTMOST_P (w)
826 && x > right_x - ux)
827 return ON_VERTICAL_BORDER;
829 if (x < text_left)
831 if (lmargin_width > 0
832 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
833 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
834 : (x < left_x + lmargin_width)))
835 return ON_LEFT_MARGIN;
837 return ON_LEFT_FRINGE;
840 if (x >= text_right)
842 if (rmargin_width > 0
843 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
844 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
845 : (x >= right_x - rmargin_width)))
846 return ON_RIGHT_MARGIN;
848 return ON_RIGHT_FRINGE;
851 /* Everything special ruled out - must be on text area */
852 return ON_TEXT;
855 /* Take X is the frame-relative pixel x-coordinate, and return the
856 x-coordinate relative to part PART of window W. */
858 window_relative_x_coord (struct window *w, enum window_part part, int x)
860 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
862 switch (part)
864 case ON_TEXT:
865 return x - window_box_left (w, TEXT_AREA);
867 case ON_LEFT_FRINGE:
868 return x - left_x;
870 case ON_RIGHT_FRINGE:
871 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
873 case ON_LEFT_MARGIN:
874 return (x - left_x
875 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
876 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
878 case ON_RIGHT_MARGIN:
879 return (x + 1
880 - ((w->pseudo_window_p)
881 ? WINDOW_TOTAL_WIDTH (w)
882 : WINDOW_BOX_RIGHT_EDGE_X (w))
883 + window_box_width (w, RIGHT_MARGIN_AREA)
884 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
885 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
888 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
889 return 0;
893 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
894 Scoordinates_in_window_p, 2, 2, 0,
895 doc: /* Return non-nil if COORDINATES are in WINDOW.
896 COORDINATES is a cons of the form (X . Y), X and Y being distances
897 measured in characters from the upper-left corner of the frame.
898 \(0 . 0) denotes the character in the upper left corner of the
899 frame.
900 If COORDINATES are in the text portion of WINDOW,
901 the coordinates relative to the window are returned.
902 If they are in the mode line of WINDOW, `mode-line' is returned.
903 If they are in the top mode line of WINDOW, `header-line' is returned.
904 If they are in the left fringe of WINDOW, `left-fringe' is returned.
905 If they are in the right fringe of WINDOW, `right-fringe' is returned.
906 If they are on the border between WINDOW and its right sibling,
907 `vertical-line' is returned.
908 If they are in the windows's left or right marginal areas, `left-margin'\n\
909 or `right-margin' is returned. */)
910 (register Lisp_Object coordinates, Lisp_Object window)
912 struct window *w;
913 struct frame *f;
914 int x, y;
915 Lisp_Object lx, ly;
917 CHECK_WINDOW (window);
918 w = XWINDOW (window);
919 f = XFRAME (w->frame);
920 CHECK_CONS (coordinates);
921 lx = Fcar (coordinates);
922 ly = Fcdr (coordinates);
923 CHECK_NUMBER_OR_FLOAT (lx);
924 CHECK_NUMBER_OR_FLOAT (ly);
925 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
926 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
928 switch (coordinates_in_window (w, x, y))
930 case ON_NOTHING:
931 return Qnil;
933 case ON_TEXT:
934 /* Convert X and Y to window relative pixel coordinates, and
935 return the canonical char units. */
936 x -= window_box_left (w, TEXT_AREA);
937 y -= WINDOW_TOP_EDGE_Y (w);
938 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
939 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
941 case ON_MODE_LINE:
942 return Qmode_line;
944 case ON_VERTICAL_BORDER:
945 return Qvertical_line;
947 case ON_HEADER_LINE:
948 return Qheader_line;
950 case ON_LEFT_FRINGE:
951 return Qleft_fringe;
953 case ON_RIGHT_FRINGE:
954 return Qright_fringe;
956 case ON_LEFT_MARGIN:
957 return Qleft_margin;
959 case ON_RIGHT_MARGIN:
960 return Qright_margin;
962 case ON_SCROLL_BAR:
963 /* Historically we are supposed to return nil in this case. */
964 return Qnil;
966 default:
967 abort ();
972 /* Callback for foreach_window, used in window_from_coordinates.
973 Check if window W contains coordinates specified by USER_DATA which
974 is actually a pointer to a struct check_window_data CW.
976 Check if window W contains coordinates *CW->x and *CW->y. If it
977 does, return W in *CW->window, as Lisp_Object, and return in
978 *CW->part the part of the window under coordinates *X,*Y. Return
979 zero from this function to stop iterating over windows. */
981 struct check_window_data
983 Lisp_Object *window;
984 int x, y;
985 enum window_part *part;
988 static int
989 check_window_containing (struct window *w, void *user_data)
991 struct check_window_data *cw = (struct check_window_data *) user_data;
992 enum window_part found;
993 int continue_p = 1;
995 found = coordinates_in_window (w, cw->x, cw->y);
996 if (found != ON_NOTHING)
998 *cw->part = found;
999 XSETWINDOW (*cw->window, w);
1000 continue_p = 0;
1003 return continue_p;
1007 /* Find the window containing frame-relative pixel position X/Y and
1008 return it as a Lisp_Object.
1010 If X, Y is on one of the window's special `window_part' elements,
1011 set *PART to the id of that element.
1013 If there is no window under X, Y return nil and leave *PART
1014 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1016 This function was previously implemented with a loop cycling over
1017 windows with Fnext_window, and starting with the frame's selected
1018 window. It turned out that this doesn't work with an
1019 implementation of next_window using Vwindow_list, because
1020 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1021 tree of F when this function is called asynchronously from
1022 note_mouse_highlight. The original loop didn't terminate in this
1023 case. */
1025 Lisp_Object
1026 window_from_coordinates (struct frame *f, int x, int y,
1027 enum window_part *part, int tool_bar_p)
1029 Lisp_Object window;
1030 struct check_window_data cw;
1031 enum window_part dummy;
1033 if (part == 0)
1034 part = &dummy;
1036 window = Qnil;
1037 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1038 foreach_window (f, check_window_containing, &cw);
1040 /* If not found above, see if it's in the tool bar window, if a tool
1041 bar exists. */
1042 if (NILP (window)
1043 && tool_bar_p
1044 && WINDOWP (f->tool_bar_window)
1045 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1046 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1047 != ON_NOTHING))
1049 *part = ON_TEXT;
1050 window = f->tool_bar_window;
1053 return window;
1056 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1057 doc: /* Return window containing coordinates X and Y on FRAME.
1058 If omitted, FRAME defaults to the currently selected frame.
1059 The top left corner of the frame is considered to be row 0,
1060 column 0. */)
1061 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1063 struct frame *f;
1065 if (NILP (frame))
1066 frame = selected_frame;
1067 CHECK_LIVE_FRAME (frame);
1068 f = XFRAME (frame);
1070 /* Check that arguments are integers or floats. */
1071 CHECK_NUMBER_OR_FLOAT (x);
1072 CHECK_NUMBER_OR_FLOAT (y);
1074 return window_from_coordinates (f,
1075 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1076 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1077 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1078 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1079 0, 0);
1082 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1083 doc: /* Return current value of point in WINDOW.
1084 WINDOW defaults to the selected window.
1086 For a nonselected window, this is the value point would have
1087 if that window were selected.
1089 Note that, when WINDOW is the selected window and its buffer
1090 is also currently selected, the value returned is the same as (point).
1091 It would be more strictly correct to return the `top-level' value
1092 of point, outside of any save-excursion forms.
1093 But that is hard to define. */)
1094 (Lisp_Object window)
1096 register struct window *w = decode_window (window);
1098 if (w == XWINDOW (selected_window)
1099 && current_buffer == XBUFFER (w->buffer))
1100 return Fpoint ();
1101 return Fmarker_position (w->pointm);
1104 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1105 doc: /* Return position at which display currently starts in WINDOW.
1106 WINDOW defaults to the selected window.
1107 This is updated by redisplay or by calling `set-window-start'. */)
1108 (Lisp_Object window)
1110 return Fmarker_position (decode_window (window)->start);
1113 /* This is text temporarily removed from the doc string below.
1115 This function returns nil if the position is not currently known.
1116 That happens when redisplay is preempted and doesn't finish.
1117 If in that case you want to compute where the end of the window would
1118 have been if redisplay had finished, do this:
1119 (save-excursion
1120 (goto-char (window-start window))
1121 (vertical-motion (1- (window-height window)) window)
1122 (point))") */
1124 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1125 doc: /* Return position at which display currently ends in WINDOW.
1126 WINDOW defaults to the selected window.
1127 This is updated by redisplay, when it runs to completion.
1128 Simply changing the buffer text or setting `window-start'
1129 does not update this value.
1130 Return nil if there is no recorded value. \(This can happen if the
1131 last redisplay of WINDOW was preempted, and did not finish.)
1132 If UPDATE is non-nil, compute the up-to-date position
1133 if it isn't already recorded. */)
1134 (Lisp_Object window, Lisp_Object update)
1136 Lisp_Object value;
1137 struct window *w = decode_window (window);
1138 Lisp_Object buf;
1139 struct buffer *b;
1141 buf = w->buffer;
1142 CHECK_BUFFER (buf);
1143 b = XBUFFER (buf);
1145 #if 0 /* This change broke some things. We should make it later. */
1146 /* If we don't know the end position, return nil.
1147 The user can compute it with vertical-motion if he wants to.
1148 It would be nicer to do it automatically,
1149 but that's so slow that it would probably bother people. */
1150 if (NILP (w->window_end_valid))
1151 return Qnil;
1152 #endif
1154 if (! NILP (update)
1155 && ! (! NILP (w->window_end_valid)
1156 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1157 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1158 && !noninteractive)
1160 struct text_pos startp;
1161 struct it it;
1162 struct buffer *old_buffer = NULL;
1164 /* Cannot use Fvertical_motion because that function doesn't
1165 cope with variable-height lines. */
1166 if (b != current_buffer)
1168 old_buffer = current_buffer;
1169 set_buffer_internal (b);
1172 /* In case W->start is out of the range, use something
1173 reasonable. This situation occurred when loading a file with
1174 `-l' containing a call to `rmail' with subsequent other
1175 commands. At the end, W->start happened to be BEG, while
1176 rmail had already narrowed the buffer. */
1177 if (XMARKER (w->start)->charpos < BEGV)
1178 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1179 else if (XMARKER (w->start)->charpos > ZV)
1180 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1181 else
1182 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1184 start_display (&it, w, startp);
1185 move_it_vertically (&it, window_box_height (w));
1186 if (it.current_y < it.last_visible_y)
1187 move_it_past_eol (&it);
1188 value = make_number (IT_CHARPOS (it));
1190 if (old_buffer)
1191 set_buffer_internal (old_buffer);
1193 else
1194 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1196 return value;
1199 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1200 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1201 Return POS. */)
1202 (Lisp_Object window, Lisp_Object pos)
1204 register struct window *w = decode_window (window);
1206 CHECK_NUMBER_COERCE_MARKER (pos);
1207 if (w == XWINDOW (selected_window)
1208 && XBUFFER (w->buffer) == current_buffer)
1209 Fgoto_char (pos);
1210 else
1211 set_marker_restricted (w->pointm, pos, w->buffer);
1213 /* We have to make sure that redisplay updates the window to show
1214 the new value of point. */
1215 if (!EQ (window, selected_window))
1216 ++windows_or_buffers_changed;
1218 return pos;
1221 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1222 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1223 WINDOW defaults to the selected window. Return POS.
1224 Optional third arg NOFORCE non-nil inhibits next redisplay from
1225 overriding motion of point in order to display at this exact start. */)
1226 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1228 register struct window *w = decode_window (window);
1230 CHECK_NUMBER_COERCE_MARKER (pos);
1231 set_marker_restricted (w->start, pos, w->buffer);
1232 /* this is not right, but much easier than doing what is right. */
1233 w->start_at_line_beg = Qnil;
1234 if (NILP (noforce))
1235 w->force_start = Qt;
1236 w->update_mode_line = Qt;
1237 XSETFASTINT (w->last_modified, 0);
1238 XSETFASTINT (w->last_overlay_modified, 0);
1239 if (!EQ (window, selected_window))
1240 windows_or_buffers_changed++;
1242 return pos;
1246 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1247 0, 1, 0,
1248 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1249 More precisely, return the value assigned by the last call of
1250 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1251 never called with WINDOW as its argument, or the value set by that
1252 function was internally reset since its last call. WINDOW defaults to
1253 the selected window.
1255 When a window is dedicated to its buffer, `display-buffer' will refrain
1256 from displaying another buffer in it. `get-lru-window' and
1257 `get-largest-window' treat dedicated windows specially.
1258 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1259 `kill-buffer' can delete a dedicated window and the containing frame.
1261 Functions like `set-window-buffer' may change the buffer displayed by a
1262 window, unless that window is "strongly" dedicated to its buffer, that
1263 is the value returned by `window-dedicated-p' is t. */)
1264 (Lisp_Object window)
1266 return decode_window (window)->dedicated;
1269 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1270 Sset_window_dedicated_p, 2, 2, 0,
1271 doc: /* Mark WINDOW as dedicated according to FLAG.
1272 WINDOW defaults to the selected window. FLAG non-nil means mark WINDOW
1273 as dedicated to its buffer. FLAG nil means mark WINDOW as non-dedicated.
1274 Return FLAG.
1276 When a window is dedicated to its buffer, `display-buffer' will refrain
1277 from displaying another buffer in it. `get-lru-window' and
1278 `get-largest-window' treat dedicated windows specially.
1279 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1280 `kill-buffer' can delete a dedicated window and the containing
1281 frame.
1283 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1284 its buffer. Functions like `set-window-buffer' may change the buffer
1285 displayed by a window, unless that window is strongly dedicated to its
1286 buffer. If and when `set-window-buffer' displays another buffer in a
1287 window, it also makes sure that the window is not marked as dedicated. */)
1288 (Lisp_Object window, Lisp_Object flag)
1290 register struct window *w = decode_window (window);
1292 w->dedicated = flag;
1293 return w->dedicated;
1297 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1298 0, 1, 0,
1299 doc: /* Return the parameters of WINDOW and their values.
1300 WINDOW defaults to the selected window. The return value is a list of
1301 elements of the form (PARAMETER . VALUE). */)
1302 (Lisp_Object window)
1304 return Fcopy_alist (decode_window (window)->window_parameters);
1307 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1308 2, 2, 0,
1309 doc: /* Return WINDOW's value for PARAMETER.
1310 WINDOW defaults to the selected window. */)
1311 (Lisp_Object window, Lisp_Object parameter)
1313 Lisp_Object result;
1315 result = Fassq (parameter, decode_window (window)->window_parameters);
1316 return CDR_SAFE (result);
1319 DEFUN ("set-window-parameter", Fset_window_parameter,
1320 Sset_window_parameter, 3, 3, 0,
1321 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1322 WINDOW defaults to the selected window. Return VALUE. */)
1323 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1325 register struct window *w = decode_window (window);
1326 Lisp_Object old_alist_elt;
1328 old_alist_elt = Fassq (parameter, w->window_parameters);
1329 if (NILP (old_alist_elt))
1330 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1331 else
1332 Fsetcdr (old_alist_elt, value);
1333 return value;
1337 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1338 0, 1, 0,
1339 doc: /* Return the display-table that WINDOW is using.
1340 WINDOW defaults to the selected window. */)
1341 (Lisp_Object window)
1343 return decode_window (window)->display_table;
1346 /* Get the display table for use on window W. This is either W's
1347 display table or W's buffer's display table. Ignore the specified
1348 tables if they are not valid; if no valid table is specified,
1349 return 0. */
1351 struct Lisp_Char_Table *
1352 window_display_table (struct window *w)
1354 struct Lisp_Char_Table *dp = NULL;
1356 if (DISP_TABLE_P (w->display_table))
1357 dp = XCHAR_TABLE (w->display_table);
1358 else if (BUFFERP (w->buffer))
1360 struct buffer *b = XBUFFER (w->buffer);
1362 if (DISP_TABLE_P (BVAR (b, display_table)))
1363 dp = XCHAR_TABLE (BVAR (b, display_table));
1364 else if (DISP_TABLE_P (Vstandard_display_table))
1365 dp = XCHAR_TABLE (Vstandard_display_table);
1368 return dp;
1371 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1372 doc: /* Set WINDOW's display-table to TABLE. */)
1373 (register Lisp_Object window, Lisp_Object table)
1375 register struct window *w;
1377 w = decode_window (window);
1378 w->display_table = table;
1379 return table;
1382 static void delete_window (Lisp_Object);
1384 /* Record info on buffer window w is displaying
1385 when it is about to cease to display that buffer. */
1386 static void
1387 unshow_buffer (register struct window *w)
1389 Lisp_Object buf;
1390 struct buffer *b;
1392 buf = w->buffer;
1393 b = XBUFFER (buf);
1394 if (b != XMARKER (w->pointm)->buffer)
1395 abort ();
1397 #if 0
1398 if (w == XWINDOW (selected_window)
1399 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1400 /* Do this except when the selected window's buffer
1401 is being removed from some other window. */
1402 #endif
1403 /* last_window_start records the start position that this buffer
1404 had in the last window to be disconnected from it.
1405 Now that this statement is unconditional,
1406 it is possible for the buffer to be displayed in the
1407 selected window, while last_window_start reflects another
1408 window which was recently showing the same buffer.
1409 Some people might say that might be a good thing. Let's see. */
1410 b->last_window_start = marker_position (w->start);
1412 /* Point in the selected window's buffer
1413 is actually stored in that buffer, and the window's pointm isn't used.
1414 So don't clobber point in that buffer. */
1415 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1416 /* This line helps to fix Horsley's testbug.el bug. */
1417 && !(WINDOWP (BVAR (b, last_selected_window))
1418 && w != XWINDOW (BVAR (b, last_selected_window))
1419 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1420 temp_set_point_both (b,
1421 clip_to_bounds (BUF_BEGV (b),
1422 XMARKER (w->pointm)->charpos,
1423 BUF_ZV (b)),
1424 clip_to_bounds (BUF_BEGV_BYTE (b),
1425 marker_byte_position (w->pointm),
1426 BUF_ZV_BYTE (b)));
1428 if (WINDOWP (BVAR (b, last_selected_window))
1429 && w == XWINDOW (BVAR (b, last_selected_window)))
1430 BVAR (b, last_selected_window) = Qnil;
1433 /* Put replacement into the window structure in place of old. */
1434 static void
1435 replace_window (Lisp_Object old, Lisp_Object replacement)
1437 register Lisp_Object tem;
1438 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1440 /* If OLD is its frame's root_window, then replacement is the new
1441 root_window for that frame. */
1443 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1444 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1446 p->left_col = o->left_col;
1447 p->top_line = o->top_line;
1448 p->total_cols = o->total_cols;
1449 p->total_lines = o->total_lines;
1450 p->desired_matrix = p->current_matrix = 0;
1451 p->vscroll = 0;
1452 memset (&p->cursor, 0, sizeof (p->cursor));
1453 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
1454 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
1455 p->phys_cursor_type = -1;
1456 p->phys_cursor_width = -1;
1457 p->must_be_updated_p = 0;
1458 p->pseudo_window_p = 0;
1459 XSETFASTINT (p->window_end_vpos, 0);
1460 XSETFASTINT (p->window_end_pos, 0);
1461 p->window_end_valid = Qnil;
1462 p->frozen_window_start_p = 0;
1463 p->orig_top_line = p->orig_total_lines = Qnil;
1465 p->next = tem = o->next;
1466 if (!NILP (tem))
1467 XWINDOW (tem)->prev = replacement;
1469 p->prev = tem = o->prev;
1470 if (!NILP (tem))
1471 XWINDOW (tem)->next = replacement;
1473 p->parent = tem = o->parent;
1474 if (!NILP (tem))
1476 if (EQ (XWINDOW (tem)->vchild, old))
1477 XWINDOW (tem)->vchild = replacement;
1478 if (EQ (XWINDOW (tem)->hchild, old))
1479 XWINDOW (tem)->hchild = replacement;
1482 /*** Here, if replacement is a vertical combination
1483 and so is its new parent, we should make replacement's
1484 children be children of that parent instead. ***/
1487 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1488 doc: /* Remove WINDOW from its frame.
1489 WINDOW defaults to the selected window. Return nil.
1490 Signal an error when WINDOW is the only window on its frame. */)
1491 (register Lisp_Object window)
1493 struct frame *f;
1494 if (NILP (window))
1495 window = selected_window;
1496 else
1497 CHECK_LIVE_WINDOW (window);
1499 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1500 delete_window (window);
1502 run_window_configuration_change_hook (f);
1504 return Qnil;
1507 static void
1508 delete_window (register Lisp_Object window)
1510 register Lisp_Object tem, parent, sib;
1511 register struct window *p;
1512 register struct window *par;
1513 struct frame *f;
1515 /* Because this function is called by other C code on non-leaf
1516 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1517 so we can't decode_window here. */
1518 CHECK_WINDOW (window);
1519 p = XWINDOW (window);
1521 /* It's a no-op to delete an already-deleted window. */
1522 if (NILP (p->buffer)
1523 && NILP (p->hchild)
1524 && NILP (p->vchild))
1525 return;
1527 parent = p->parent;
1528 if (NILP (parent))
1529 error ("Attempt to delete minibuffer or sole ordinary window");
1530 par = XWINDOW (parent);
1532 windows_or_buffers_changed++;
1533 Vwindow_list = Qnil;
1534 f = XFRAME (WINDOW_FRAME (p));
1535 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1537 /* Are we trying to delete any frame's selected window? */
1539 Lisp_Object swindow, pwindow;
1541 /* See if the frame's selected window is either WINDOW
1542 or any subwindow of it, by finding all that window's parents
1543 and comparing each one with WINDOW. */
1544 swindow = FRAME_SELECTED_WINDOW (f);
1546 while (1)
1548 pwindow = swindow;
1549 while (!NILP (pwindow))
1551 if (EQ (window, pwindow))
1552 break;
1553 pwindow = XWINDOW (pwindow)->parent;
1556 /* If the window being deleted is not a parent of SWINDOW,
1557 then SWINDOW is ok as the new selected window. */
1558 if (!EQ (window, pwindow))
1559 break;
1560 /* Otherwise, try another window for SWINDOW. */
1561 swindow = Fnext_window (swindow, Qlambda, Qnil);
1563 /* If we get back to the frame's selected window,
1564 it means there was no acceptable alternative,
1565 so we cannot delete. */
1566 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1567 error ("Cannot delete window");
1570 /* If we need to change SWINDOW, do it. */
1571 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1573 /* If we're about to delete the selected window on the
1574 selected frame, then we should use Fselect_window to select
1575 the new window. On the other hand, if we're about to
1576 delete the selected window on any other frame, we shouldn't do
1577 anything but set the frame's selected_window slot. */
1578 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1579 Fselect_window (swindow, Qnil);
1580 else
1581 FRAME_SELECTED_WINDOW (f) = swindow;
1585 /* Now we know we can delete this one. */
1586 window_deletion_count++;
1588 tem = p->buffer;
1589 /* tem is null for dummy parent windows
1590 (which have inferiors but not any contents themselves) */
1591 if (!NILP (tem))
1593 unshow_buffer (p);
1594 unchain_marker (XMARKER (p->pointm));
1595 unchain_marker (XMARKER (p->start));
1598 /* Free window glyph matrices. It is sure that they are allocated
1599 again when ADJUST_GLYPHS is called. Block input so that expose
1600 events and other events that access glyph matrices are not
1601 processed while we are changing them. */
1602 BLOCK_INPUT;
1603 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1605 tem = p->next;
1606 if (!NILP (tem))
1607 XWINDOW (tem)->prev = p->prev;
1609 tem = p->prev;
1610 if (!NILP (tem))
1611 XWINDOW (tem)->next = p->next;
1613 if (EQ (window, par->hchild))
1614 par->hchild = p->next;
1615 if (EQ (window, par->vchild))
1616 par->vchild = p->next;
1618 /* Find one of our siblings to give our space to. */
1619 sib = p->prev;
1620 if (NILP (sib))
1622 /* If p gives its space to its next sibling, that sibling needs
1623 to have its top/left side pulled back to where p's is.
1624 set_window_{height,width} will re-position the sibling's
1625 children. */
1626 sib = p->next;
1627 XWINDOW (sib)->top_line = p->top_line;
1628 XWINDOW (sib)->left_col = p->left_col;
1631 /* Stretch that sibling. */
1632 if (!NILP (par->vchild))
1633 set_window_height (sib,
1634 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1636 if (!NILP (par->hchild))
1637 set_window_width (sib,
1638 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1641 /* If parent now has only one child,
1642 put the child into the parent's place. */
1643 tem = par->hchild;
1644 if (NILP (tem))
1645 tem = par->vchild;
1646 if (NILP (XWINDOW (tem)->next)) {
1647 replace_window (parent, tem);
1648 par = XWINDOW (tem);
1651 /* Since we may be deleting combination windows, we must make sure that
1652 not only p but all its children have been marked as deleted. */
1653 if (! NILP (p->hchild))
1654 delete_all_subwindows (XWINDOW (p->hchild));
1655 else if (! NILP (p->vchild))
1656 delete_all_subwindows (XWINDOW (p->vchild));
1658 /* Mark this window as deleted. */
1659 p->buffer = p->hchild = p->vchild = Qnil;
1661 if (! NILP (par->parent))
1662 par = XWINDOW (par->parent);
1664 /* Check if we have a v/hchild with a v/hchild. In that case remove
1665 one of them. */
1667 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1669 p = XWINDOW (par->vchild);
1670 par->vchild = p->vchild;
1671 tem = p->vchild;
1673 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1675 p = XWINDOW (par->hchild);
1676 par->hchild = p->hchild;
1677 tem = p->hchild;
1679 else
1680 p = 0;
1682 if (p)
1684 while (! NILP (tem)) {
1685 XWINDOW (tem)->parent = p->parent;
1686 if (NILP (XWINDOW (tem)->next))
1687 break;
1688 tem = XWINDOW (tem)->next;
1690 if (! NILP (tem)) {
1691 /* The next of the v/hchild we are removing is now the next of the
1692 last child for the v/hchild:
1693 Before v/hchild -> v/hchild -> next1 -> next2
1695 -> next3
1696 After: v/hchild -> next1 -> next2 -> next3
1698 XWINDOW (tem)->next = p->next;
1699 if (! NILP (p->next))
1700 XWINDOW (p->next)->prev = tem;
1702 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1706 /* Adjust glyph matrices. */
1707 adjust_glyphs (f);
1708 UNBLOCK_INPUT;
1713 /***********************************************************************
1714 Window List
1715 ***********************************************************************/
1717 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1718 pointer. This is a callback function for foreach_window, used in
1719 function window_list. */
1721 static int
1722 add_window_to_list (struct window *w, void *user_data)
1724 Lisp_Object *list = (Lisp_Object *) user_data;
1725 Lisp_Object window;
1726 XSETWINDOW (window, w);
1727 *list = Fcons (window, *list);
1728 return 1;
1732 /* Return a list of all windows, for use by next_window. If
1733 Vwindow_list is a list, return that list. Otherwise, build a new
1734 list, cache it in Vwindow_list, and return that. */
1736 static Lisp_Object
1737 window_list (void)
1739 if (!CONSP (Vwindow_list))
1741 Lisp_Object tail;
1743 Vwindow_list = Qnil;
1744 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1746 Lisp_Object args[2];
1748 /* We are visiting windows in canonical order, and add
1749 new windows at the front of args[1], which means we
1750 have to reverse this list at the end. */
1751 args[1] = Qnil;
1752 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1753 args[0] = Vwindow_list;
1754 args[1] = Fnreverse (args[1]);
1755 Vwindow_list = Fnconc (2, args);
1759 return Vwindow_list;
1763 /* Value is non-zero if WINDOW satisfies the constraints given by
1764 OWINDOW, MINIBUF and ALL_FRAMES.
1766 MINIBUF t means WINDOW may be minibuffer windows.
1767 `lambda' means WINDOW may not be a minibuffer window.
1768 a window means a specific minibuffer window
1770 ALL_FRAMES t means search all frames,
1771 nil means search just current frame,
1772 `visible' means search just visible frames on the
1773 current terminal,
1774 0 means search visible and iconified frames on the
1775 current terminal,
1776 a window means search the frame that window belongs to,
1777 a frame means consider windows on that frame, only. */
1779 static int
1780 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
1782 struct window *w = XWINDOW (window);
1783 struct frame *f = XFRAME (w->frame);
1784 int candidate_p = 1;
1786 if (!BUFFERP (w->buffer))
1787 candidate_p = 0;
1788 else if (MINI_WINDOW_P (w)
1789 && (EQ (minibuf, Qlambda)
1790 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1792 /* If MINIBUF is `lambda' don't consider any mini-windows.
1793 If it is a window, consider only that one. */
1794 candidate_p = 0;
1796 else if (EQ (all_frames, Qt))
1797 candidate_p = 1;
1798 else if (NILP (all_frames))
1800 xassert (WINDOWP (owindow));
1801 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1803 else if (EQ (all_frames, Qvisible))
1805 FRAME_SAMPLE_VISIBILITY (f);
1806 candidate_p = FRAME_VISIBLE_P (f)
1807 && (FRAME_TERMINAL (XFRAME (w->frame))
1808 == FRAME_TERMINAL (XFRAME (selected_frame)));
1811 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1813 FRAME_SAMPLE_VISIBILITY (f);
1814 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
1815 #ifdef HAVE_X_WINDOWS
1816 /* Yuck!! If we've just created the frame and the
1817 window-manager requested the user to place it
1818 manually, the window may still not be considered
1819 `visible'. I'd argue it should be at least
1820 something like `iconified', but don't know how to do
1821 that yet. --Stef */
1822 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
1823 && !f->output_data.x->has_been_visible)
1824 #endif
1826 && (FRAME_TERMINAL (XFRAME (w->frame))
1827 == FRAME_TERMINAL (XFRAME (selected_frame)));
1829 else if (WINDOWP (all_frames))
1830 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1831 || EQ (XWINDOW (all_frames)->frame, w->frame)
1832 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1833 else if (FRAMEP (all_frames))
1834 candidate_p = EQ (all_frames, w->frame);
1836 return candidate_p;
1840 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1841 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
1842 MINIBUF, and ALL_FRAMES. */
1844 static void
1845 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
1847 if (NILP (*window))
1848 *window = selected_window;
1849 else
1850 CHECK_LIVE_WINDOW (*window);
1852 /* MINIBUF nil may or may not include minibuffers. Decide if it
1853 does. */
1854 if (NILP (*minibuf))
1855 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1856 else if (!EQ (*minibuf, Qt))
1857 *minibuf = Qlambda;
1859 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1860 => count none of them, or a specific minibuffer window (the
1861 active one) to count. */
1863 /* ALL_FRAMES nil doesn't specify which frames to include. */
1864 if (NILP (*all_frames))
1865 *all_frames = (!EQ (*minibuf, Qlambda)
1866 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1867 : Qnil);
1868 else if (EQ (*all_frames, Qvisible))
1870 else if (EQ (*all_frames, make_number (0)))
1872 else if (FRAMEP (*all_frames))
1874 else if (!EQ (*all_frames, Qt))
1875 *all_frames = Qnil;
1879 /* Return the next or previous window of WINDOW in cyclic ordering
1880 of windows. NEXT_P non-zero means return the next window. See the
1881 documentation string of next-window for the meaning of MINIBUF and
1882 ALL_FRAMES. */
1884 static Lisp_Object
1885 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
1887 decode_next_window_args (&window, &minibuf, &all_frames);
1889 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1890 return the first window on the frame. */
1891 if (FRAMEP (all_frames)
1892 && !EQ (all_frames, XWINDOW (window)->frame))
1893 return Fframe_first_window (all_frames);
1895 if (next_p)
1897 Lisp_Object list;
1899 /* Find WINDOW in the list of all windows. */
1900 list = Fmemq (window, window_list ());
1902 /* Scan forward from WINDOW to the end of the window list. */
1903 if (CONSP (list))
1904 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1905 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1906 break;
1908 /* Scan from the start of the window list up to WINDOW. */
1909 if (!CONSP (list))
1910 for (list = Vwindow_list;
1911 CONSP (list) && !EQ (XCAR (list), window);
1912 list = XCDR (list))
1913 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1914 break;
1916 if (CONSP (list))
1917 window = XCAR (list);
1919 else
1921 Lisp_Object candidate, list;
1923 /* Scan through the list of windows for candidates. If there are
1924 candidate windows in front of WINDOW, the last one of these
1925 is the one we want. If there are candidates following WINDOW
1926 in the list, again the last one of these is the one we want. */
1927 candidate = Qnil;
1928 for (list = window_list (); CONSP (list); list = XCDR (list))
1930 if (EQ (XCAR (list), window))
1932 if (WINDOWP (candidate))
1933 break;
1935 else if (candidate_window_p (XCAR (list), window, minibuf,
1936 all_frames))
1937 candidate = XCAR (list);
1940 if (WINDOWP (candidate))
1941 window = candidate;
1944 return window;
1948 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1949 doc: /* Return window following WINDOW in cyclic ordering of windows.
1950 WINDOW defaults to the selected window. The optional arguments
1951 MINIBUF and ALL-FRAMES specify the set of windows to consider.
1953 MINIBUF t means consider the minibuffer window even if the
1954 minibuffer is not active. MINIBUF nil or omitted means consider
1955 the minibuffer window only if the minibuffer is active. Any
1956 other value means do not consider the minibuffer window even if
1957 the minibuffer is active.
1959 Several frames may share a single minibuffer; if the minibuffer
1960 is active, all windows on all frames that share that minibuffer
1961 are considered too. Therefore, if you are using a separate
1962 minibuffer frame and the minibuffer is active and MINIBUF says it
1963 counts, `next-window' considers the windows in the frame from
1964 which you entered the minibuffer, as well as the minibuffer
1965 window.
1967 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
1968 frame, plus the minibuffer window if specified by the MINIBUF
1969 argument, see above. If the minibuffer counts, consider all
1970 windows on all frames that share that minibuffer too.
1971 ALL-FRAMES t means consider all windows on all existing frames.
1972 ALL-FRAMES `visible' means consider all windows on all visible
1973 frames on the current terminal.
1974 ALL-FRAMES 0 means consider all windows on all visible and
1975 iconified frames on the current terminal.
1976 ALL-FRAMES a frame means consider all windows on that frame only.
1977 Anything else means consider all windows on WINDOW's frame and no
1978 others.
1980 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1981 `next-window' to iterate through the entire cycle of acceptable
1982 windows, eventually ending up back at the window you started with.
1983 `previous-window' traverses the same cycle, in the reverse order. */)
1984 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
1986 return next_window (window, minibuf, all_frames, 1);
1990 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1991 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
1992 WINDOW defaults to the selected window. The optional arguments
1993 MINIBUF and ALL-FRAMES specify the set of windows to consider.
1994 For the precise meaning of these arguments see `next-window'.
1996 If you use consistent values for MINIBUF and ALL-FRAMES, you can
1997 use `previous-window' to iterate through the entire cycle of
1998 acceptable windows, eventually ending up back at the window you
1999 started with. `next-window' traverses the same cycle, in the
2000 reverse order. */)
2001 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2003 return next_window (window, minibuf, all_frames, 0);
2007 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
2008 doc: /* Select another window in cyclic ordering of windows.
2009 COUNT specifies the number of windows to skip, starting with the
2010 selected window, before making the selection. If COUNT is
2011 positive, skip COUNT windows forwards. If COUNT is negative,
2012 skip -COUNT windows backwards. COUNT zero means do not skip any
2013 window, so select the selected window. In an interactive call,
2014 COUNT is the numeric prefix argument. Return nil.
2016 This function uses `next-window' for finding the window to select.
2017 The argument ALL-FRAMES has the same meaning as in `next-window',
2018 but the MINIBUF argument of `next-window' is always effectively
2019 nil. */)
2020 (Lisp_Object count, Lisp_Object all_frames)
2022 Lisp_Object window;
2023 int i;
2025 CHECK_NUMBER (count);
2026 window = selected_window;
2028 for (i = XINT (count); i > 0; --i)
2029 window = Fnext_window (window, Qnil, all_frames);
2030 for (; i < 0; ++i)
2031 window = Fprevious_window (window, Qnil, all_frames);
2033 Fselect_window (window, Qnil);
2034 return Qnil;
2038 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2039 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2040 FRAME nil or omitted means use the selected frame.
2041 WINDOW nil or omitted means use the selected window.
2042 MINIBUF t means include the minibuffer window, even if it isn't active.
2043 MINIBUF nil or omitted means include the minibuffer window only
2044 if it's active.
2045 MINIBUF neither nil nor t means never include the minibuffer window. */)
2046 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2048 if (NILP (window))
2049 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2050 CHECK_WINDOW (window);
2051 if (NILP (frame))
2052 frame = selected_frame;
2054 if (!EQ (frame, XWINDOW (window)->frame))
2055 error ("Window is on a different frame");
2057 return window_list_1 (window, minibuf, frame);
2061 /* Return a list of windows in cyclic ordering. Arguments are like
2062 for `next-window'. */
2064 static Lisp_Object
2065 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2067 Lisp_Object tail, list, rest;
2069 decode_next_window_args (&window, &minibuf, &all_frames);
2070 list = Qnil;
2072 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2073 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2074 list = Fcons (XCAR (tail), list);
2076 /* Rotate the list to start with WINDOW. */
2077 list = Fnreverse (list);
2078 rest = Fmemq (window, list);
2079 if (!NILP (rest) && !EQ (rest, list))
2081 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2083 XSETCDR (tail, Qnil);
2084 list = nconc2 (rest, list);
2086 return list;
2091 /* Look at all windows, performing an operation specified by TYPE
2092 with argument OBJ.
2093 If FRAMES is Qt, look at all frames;
2094 Qnil, look at just the selected frame;
2095 Qvisible, look at visible frames;
2096 a frame, just look at windows on that frame.
2097 If MINI is non-zero, perform the operation on minibuffer windows too. */
2099 enum window_loop
2101 WINDOW_LOOP_UNUSED,
2102 GET_BUFFER_WINDOW, /* Arg is buffer */
2103 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2104 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2105 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2106 GET_LARGEST_WINDOW,
2107 UNSHOW_BUFFER, /* Arg is buffer */
2108 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2109 CHECK_ALL_WINDOWS
2112 static Lisp_Object
2113 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2115 Lisp_Object window, windows, best_window, frame_arg;
2116 struct frame *f;
2117 struct gcpro gcpro1;
2119 /* If we're only looping through windows on a particular frame,
2120 frame points to that frame. If we're looping through windows
2121 on all frames, frame is 0. */
2122 if (FRAMEP (frames))
2123 f = XFRAME (frames);
2124 else if (NILP (frames))
2125 f = SELECTED_FRAME ();
2126 else
2127 f = NULL;
2129 if (f)
2130 frame_arg = Qlambda;
2131 else if (EQ (frames, make_number (0)))
2132 frame_arg = frames;
2133 else if (EQ (frames, Qvisible))
2134 frame_arg = frames;
2135 else
2136 frame_arg = Qt;
2138 /* frame_arg is Qlambda to stick to one frame,
2139 Qvisible to consider all visible frames,
2140 or Qt otherwise. */
2142 /* Pick a window to start with. */
2143 if (WINDOWP (obj))
2144 window = obj;
2145 else if (f)
2146 window = FRAME_SELECTED_WINDOW (f);
2147 else
2148 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2150 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2151 GCPRO1 (windows);
2152 best_window = Qnil;
2154 for (; CONSP (windows); windows = XCDR (windows))
2156 struct window *w;
2158 window = XCAR (windows);
2159 w = XWINDOW (window);
2161 /* Note that we do not pay attention here to whether the frame
2162 is visible, since Fwindow_list skips non-visible frames if
2163 that is desired, under the control of frame_arg. */
2164 if (!MINI_WINDOW_P (w)
2165 /* For UNSHOW_BUFFER, we must always consider all windows. */
2166 || type == UNSHOW_BUFFER
2167 || (mini && minibuf_level > 0))
2168 switch (type)
2170 case GET_BUFFER_WINDOW:
2171 if (EQ (w->buffer, obj)
2172 /* Don't find any minibuffer window
2173 except the one that is currently in use. */
2174 && (MINI_WINDOW_P (w)
2175 ? EQ (window, minibuf_window)
2176 : 1))
2178 if (NILP (best_window))
2179 best_window = window;
2180 else if (EQ (window, selected_window))
2181 /* Prefer to return selected-window. */
2182 RETURN_UNGCPRO (window);
2183 else if (EQ (Fwindow_frame (window), selected_frame))
2184 /* Prefer windows on the current frame. */
2185 best_window = window;
2187 break;
2189 case GET_LRU_WINDOW:
2190 /* `obj' is an integer encoding a bitvector.
2191 `obj & 1' means consider only full-width windows.
2192 `obj & 2' means consider also dedicated windows. */
2193 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2194 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2195 /* Minibuffer windows are always ignored. */
2196 || MINI_WINDOW_P (w))
2197 break;
2198 if (NILP (best_window)
2199 || (XFASTINT (XWINDOW (best_window)->use_time)
2200 > XFASTINT (w->use_time)))
2201 best_window = window;
2202 break;
2204 case DELETE_OTHER_WINDOWS:
2205 if (!EQ (window, obj))
2206 Fdelete_window (window);
2207 break;
2209 case DELETE_BUFFER_WINDOWS:
2210 if (EQ (w->buffer, obj))
2212 struct frame *fr = XFRAME (WINDOW_FRAME (w));
2214 /* If this window is dedicated, and in a frame of its own,
2215 kill the frame. */
2216 if (EQ (window, FRAME_ROOT_WINDOW (fr))
2217 && !NILP (w->dedicated)
2218 && other_visible_frames (fr))
2220 /* Skip the other windows on this frame.
2221 There might be one, the minibuffer! */
2222 while (CONSP (XCDR (windows))
2223 && EQ (XWINDOW (XCAR (windows))->frame,
2224 XWINDOW (XCAR (XCDR (windows)))->frame))
2225 windows = XCDR (windows);
2227 /* Now we can safely delete the frame. */
2228 delete_frame (w->frame, Qnil);
2230 else if (NILP (w->parent))
2232 /* If we're deleting the buffer displayed in the
2233 only window on the frame, find a new buffer to
2234 display there. */
2235 Lisp_Object buffer;
2236 buffer = Fother_buffer (obj, Qnil, w->frame);
2237 /* Reset dedicated state of window. */
2238 w->dedicated = Qnil;
2239 Fset_window_buffer (window, buffer, Qnil);
2240 if (EQ (window, selected_window))
2241 Fset_buffer (w->buffer);
2243 else
2244 Fdelete_window (window);
2246 break;
2248 case GET_LARGEST_WINDOW:
2249 { /* nil `obj' means to ignore dedicated windows. */
2250 /* Ignore dedicated windows and minibuffers. */
2251 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2252 break;
2254 if (NILP (best_window))
2255 best_window = window;
2256 else
2258 struct window *b = XWINDOW (best_window);
2259 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2260 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2261 best_window = window;
2264 break;
2266 case UNSHOW_BUFFER:
2267 if (EQ (w->buffer, obj))
2269 Lisp_Object buffer;
2270 struct frame *fr = XFRAME (w->frame);
2272 /* Find another buffer to show in this window. */
2273 buffer = Fother_buffer (obj, Qnil, w->frame);
2275 /* If this window is dedicated, and in a frame of its own,
2276 kill the frame. */
2277 if (EQ (window, FRAME_ROOT_WINDOW (fr))
2278 && !NILP (w->dedicated)
2279 && other_visible_frames (fr))
2281 /* Skip the other windows on this frame.
2282 There might be one, the minibuffer! */
2283 while (CONSP (XCDR (windows))
2284 && EQ (XWINDOW (XCAR (windows))->frame,
2285 XWINDOW (XCAR (XCDR (windows)))->frame))
2286 windows = XCDR (windows);
2288 /* Now we can safely delete the frame. */
2289 delete_frame (w->frame, Qnil);
2291 else if (!NILP (w->dedicated) && !NILP (w->parent))
2293 Lisp_Object window_to_delete;
2294 XSETWINDOW (window_to_delete, w);
2295 /* If this window is dedicated and not the only window
2296 in its frame, then kill it. */
2297 Fdelete_window (window_to_delete);
2299 else
2301 /* Otherwise show a different buffer in the window. */
2302 w->dedicated = Qnil;
2303 Fset_window_buffer (window, buffer, Qnil);
2304 if (EQ (window, selected_window))
2305 Fset_buffer (w->buffer);
2308 break;
2310 case REDISPLAY_BUFFER_WINDOWS:
2311 if (EQ (w->buffer, obj))
2313 mark_window_display_accurate (window, 0);
2314 w->update_mode_line = Qt;
2315 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2316 ++update_mode_lines;
2317 best_window = window;
2319 break;
2321 /* Check for a window that has a killed buffer. */
2322 case CHECK_ALL_WINDOWS:
2323 if (! NILP (w->buffer)
2324 && NILP (BVAR (XBUFFER (w->buffer), name)))
2325 abort ();
2326 break;
2328 case WINDOW_LOOP_UNUSED:
2329 break;
2333 UNGCPRO;
2334 return best_window;
2337 /* Used for debugging. Abort if any window has a dead buffer. */
2339 void
2340 check_all_windows (void)
2342 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2345 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
2346 doc: /* Return WINDOW's use time.
2347 WINDOW defaults to the selected window. The window with the highest use
2348 time is the most recently selected one. The window with the lowest use
2349 time is the least recently selected one. */)
2350 (Lisp_Object window)
2352 return decode_window (window)->use_time;
2355 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2356 doc: /* Return the window least recently selected or used for display.
2357 \(LRU means Least Recently Used.)
2359 Return a full-width window if possible.
2360 A minibuffer window is never a candidate.
2361 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2362 so if all windows are dedicated, the value is nil.
2363 If optional argument FRAME is `visible', search all visible frames.
2364 If FRAME is 0, search all visible and iconified frames.
2365 If FRAME is t, search all frames.
2366 If FRAME is nil, search only the selected frame.
2367 If FRAME is a frame, search only that frame. */)
2368 (Lisp_Object frame, Lisp_Object dedicated)
2370 register Lisp_Object w;
2371 /* First try for a window that is full-width */
2372 w = window_loop (GET_LRU_WINDOW,
2373 NILP (dedicated) ? make_number (1) : make_number (3),
2374 0, frame);
2375 if (!NILP (w) && !EQ (w, selected_window))
2376 return w;
2377 /* If none of them, try the rest */
2378 return window_loop (GET_LRU_WINDOW,
2379 NILP (dedicated) ? make_number (0) : make_number (2),
2380 0, frame);
2383 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2384 doc: /* Return the largest window in area.
2385 A minibuffer window is never a candidate.
2386 A dedicated window is never a candidate unless DEDICATED is non-nil,
2387 so if all windows are dedicated, the value is nil.
2388 If optional argument FRAME is `visible', search all visible frames.
2389 If FRAME is 0, search all visible and iconified frames.
2390 If FRAME is t, search all frames.
2391 If FRAME is nil, search only the selected frame.
2392 If FRAME is a frame, search only that frame. */)
2393 (Lisp_Object frame, Lisp_Object dedicated)
2395 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2396 frame);
2399 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2400 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2401 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to the
2402 current buffer.
2403 If optional argument FRAME is `visible', search all visible frames.
2404 If optional argument FRAME is 0, search all visible and iconified frames.
2405 If FRAME is t, search all frames.
2406 If FRAME is nil, search only the selected frame.
2407 If FRAME is a frame, search only that frame. */)
2408 (Lisp_Object buffer_or_name, Lisp_Object frame)
2410 Lisp_Object buffer;
2412 if (NILP (buffer_or_name))
2413 buffer = Fcurrent_buffer ();
2414 else
2415 buffer = Fget_buffer (buffer_or_name);
2417 if (BUFFERP (buffer))
2418 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2419 else
2420 return Qnil;
2423 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2424 0, 1, "",
2425 doc: /* Make WINDOW (or the selected window) fill its frame.
2426 Only the frame WINDOW is on is affected.
2427 This function tries to reduce display jumps by keeping the text
2428 previously visible in WINDOW in the same place on the frame. Doing this
2429 depends on the value of (window-start WINDOW), so if calling this
2430 function in a program gives strange scrolling, make sure the
2431 window-start value is reasonable when this function is called. */)
2432 (Lisp_Object window)
2434 struct window *w;
2435 EMACS_INT startpos;
2436 int top, new_top;
2438 if (NILP (window))
2439 window = selected_window;
2440 else
2441 CHECK_LIVE_WINDOW (window);
2442 w = XWINDOW (window);
2444 startpos = marker_position (w->start);
2445 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2447 if (MINI_WINDOW_P (w) && top > 0)
2448 error ("Can't expand minibuffer to full frame");
2450 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2452 /* Try to minimize scrolling, by setting the window start to the point
2453 will cause the text at the old window start to be at the same place
2454 on the frame. But don't try to do this if the window start is
2455 outside the visible portion (as might happen when the display is
2456 not current, due to typeahead). */
2457 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2458 if (new_top != top
2459 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2460 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2462 struct position pos;
2463 struct buffer *obuf = current_buffer;
2465 Fset_buffer (w->buffer);
2466 /* This computation used to temporarily move point, but that can
2467 have unwanted side effects due to text properties. */
2468 pos = *vmotion (startpos, -top, w);
2470 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2471 w->window_end_valid = Qnil;
2472 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2473 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2474 : Qnil);
2475 /* We need to do this, so that the window-scroll-functions
2476 get called. */
2477 w->optional_new_start = Qt;
2479 set_buffer_internal (obuf);
2482 return Qnil;
2485 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2486 0, 2, "bDelete windows on (buffer): ",
2487 doc: /* Delete all windows showing BUFFER-OR-NAME.
2488 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2489 defaults to the current buffer.
2491 Optional second argument FRAME controls which frames are affected.
2492 If optional argument FRAME is `visible', search all visible frames.
2493 If FRAME is 0, search all visible and iconified frames.
2494 If FRAME is nil, search all frames.
2495 If FRAME is t, search only the selected frame.
2496 If FRAME is a frame, search only that frame.
2497 When a window showing BUFFER-OR-NAME is dedicated and the only window of
2498 its frame, that frame is deleted when there are other frames left. */)
2499 (Lisp_Object buffer_or_name, Lisp_Object frame)
2501 Lisp_Object buffer;
2503 /* FRAME uses t and nil to mean the opposite of what window_loop
2504 expects. */
2505 if (NILP (frame))
2506 frame = Qt;
2507 else if (EQ (frame, Qt))
2508 frame = Qnil;
2510 if (NILP (buffer_or_name))
2511 buffer = Fcurrent_buffer ();
2512 else
2514 buffer = Fget_buffer (buffer_or_name);
2515 CHECK_BUFFER (buffer);
2518 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2520 return Qnil;
2523 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2524 Sreplace_buffer_in_windows,
2525 0, 1, "bReplace buffer in windows: ",
2526 doc: /* Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2527 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2528 defaults to the current buffer.
2530 When a window showing BUFFER-OR-NAME is dedicated that window is
2531 deleted. If that window is the only window on its frame, that frame is
2532 deleted too when there are other frames left. If there are no other
2533 frames left, some other buffer is displayed in that window. */)
2534 (Lisp_Object buffer_or_name)
2536 Lisp_Object buffer;
2538 if (NILP (buffer_or_name))
2539 buffer = Fcurrent_buffer ();
2540 else
2542 buffer = Fget_buffer (buffer_or_name);
2543 CHECK_BUFFER (buffer);
2546 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2548 return Qnil;
2551 /* Replace BUFFER with some other buffer in all windows
2552 of all frames, even those on other keyboards. */
2554 void
2555 replace_buffer_in_all_windows (Lisp_Object buffer)
2557 Lisp_Object tail, frame;
2559 /* A single call to window_loop won't do the job
2560 because it only considers frames on the current keyboard.
2561 So loop manually over frames, and handle each one. */
2562 FOR_EACH_FRAME (tail, frame)
2563 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2566 /* Set the height of WINDOW and all its inferiors. */
2568 /* The smallest acceptable dimensions for a window. Anything smaller
2569 might crash Emacs. */
2571 #define MIN_SAFE_WINDOW_WIDTH (2)
2572 #define MIN_SAFE_WINDOW_HEIGHT (1)
2574 /* For wp non-zero the total number of columns of window w. Otherwise
2575 the total number of lines of w. */
2577 #define WINDOW_TOTAL_SIZE(w, wp) \
2578 (wp ? WINDOW_TOTAL_COLS (w) : WINDOW_TOTAL_LINES (w))
2580 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2581 minimum allowable size. */
2583 void
2584 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
2586 /* For height, we have to see:
2587 how many windows the frame has at minimum (one or two),
2588 and whether it has a menu bar or other special stuff at the top. */
2589 int min_height
2590 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2591 ? MIN_SAFE_WINDOW_HEIGHT
2592 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2594 if (FRAME_TOP_MARGIN (frame) > 0)
2595 min_height += FRAME_TOP_MARGIN (frame);
2597 if (*rows < min_height)
2598 *rows = min_height;
2599 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2600 *cols = MIN_SAFE_WINDOW_WIDTH;
2603 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2604 check if W's width can be changed, otherwise check W's height.
2605 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2606 siblings, too. If none of the siblings is resizable, WINDOW isn't
2607 either. */
2609 static int
2610 window_fixed_size_p (struct window *w, int width_p, int check_siblings_p)
2612 int fixed_p;
2613 struct window *c;
2615 if (!NILP (w->hchild))
2617 c = XWINDOW (w->hchild);
2619 if (width_p)
2621 /* A horizontal combination is fixed-width if all of if its
2622 children are. */
2623 while (c && window_fixed_size_p (c, width_p, 0))
2624 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2625 fixed_p = c == NULL;
2627 else
2629 /* A horizontal combination is fixed-height if one of if its
2630 children is. */
2631 while (c && !window_fixed_size_p (c, width_p, 0))
2632 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2633 fixed_p = c != NULL;
2636 else if (!NILP (w->vchild))
2638 c = XWINDOW (w->vchild);
2640 if (width_p)
2642 /* A vertical combination is fixed-width if one of if its
2643 children is. */
2644 while (c && !window_fixed_size_p (c, width_p, 0))
2645 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2646 fixed_p = c != NULL;
2648 else
2650 /* A vertical combination is fixed-height if all of if its
2651 children are. */
2652 while (c && window_fixed_size_p (c, width_p, 0))
2653 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2654 fixed_p = c == NULL;
2657 else if (BUFFERP (w->buffer))
2659 struct buffer *old = current_buffer;
2660 Lisp_Object val;
2662 current_buffer = XBUFFER (w->buffer);
2663 val = find_symbol_value (Qwindow_size_fixed);
2664 current_buffer = old;
2666 fixed_p = 0;
2667 if (!EQ (val, Qunbound))
2669 fixed_p = !NILP (val);
2671 if (fixed_p
2672 && ((EQ (val, Qheight) && width_p)
2673 || (EQ (val, Qwidth) && !width_p)))
2674 fixed_p = 0;
2677 /* Can't tell if this one is resizable without looking at
2678 siblings. If all siblings are fixed-size this one is too. */
2679 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2681 Lisp_Object child;
2683 for (child = w->prev; WINDOWP (child); child = XWINDOW (child)->prev)
2684 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2685 break;
2687 if (NILP (child))
2688 for (child = w->next; WINDOWP (child); child = XWINDOW (child)->next)
2689 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2690 break;
2692 if (NILP (child))
2693 fixed_p = 1;
2696 else
2697 fixed_p = 1;
2699 return fixed_p;
2702 /* Return minimum size of leaf window W. WIDTH_P non-zero means return
2703 the minimum width of W, WIDTH_P zero means return the minimum height
2704 of W. SAFE_P non-zero means ignore window-min-height|width but just
2705 return values that won't crash Emacs and don't hide components like
2706 fringes, scrollbars, or modelines. If WIDTH_P is zero and W is the
2707 minibuffer window, always return 1. */
2709 static int
2710 window_min_size_2 (struct window *w, int width_p, int safe_p)
2712 /* We should consider buffer-local values of window_min_height and
2713 window_min_width here. */
2714 if (width_p)
2716 int safe_size = (MIN_SAFE_WINDOW_WIDTH
2717 + WINDOW_FRINGE_COLS (w)
2718 + WINDOW_SCROLL_BAR_COLS (w));
2720 return safe_p ? safe_size : max (window_min_width, safe_size);
2722 else if (MINI_WINDOW_P (w))
2723 return 1;
2724 else
2726 int safe_size = (MIN_SAFE_WINDOW_HEIGHT
2727 + ((BUFFERP (w->buffer)
2728 && !NILP (BVAR (XBUFFER (w->buffer), mode_line_format)))
2729 ? 1 : 0));
2731 return safe_p ? safe_size : max (window_min_height, safe_size);
2735 /* Return minimum size of window W, not taking fixed-width windows into
2736 account. WIDTH_P non-zero means return the minimum width, otherwise
2737 return the minimum height. SAFE_P non-zero means ignore
2738 window-min-height|width but just return values that won't crash Emacs
2739 and don't hide components like fringes, scrollbars, or modelines. If
2740 W is a combination window, compute the minimum size from the minimum
2741 sizes of W's children. */
2743 static int
2744 window_min_size_1 (struct window *w, int width_p, int safe_p)
2746 struct window *c;
2747 int size;
2749 if (!NILP (w->hchild))
2751 /* W is a horizontal combination. */
2752 c = XWINDOW (w->hchild);
2753 size = 0;
2755 if (width_p)
2757 /* The minimum width of a horizontal combination is the sum of
2758 the minimum widths of its children. */
2759 while (c)
2761 size += window_min_size_1 (c, 1, safe_p);
2762 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2765 else
2767 /* The minimum height of a horizontal combination is the
2768 maximum of the minimum heights of its children. */
2769 while (c)
2771 size = max (window_min_size_1 (c, 0, safe_p), size);
2772 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2776 else if (!NILP (w->vchild))
2778 /* W is a vertical combination. */
2779 c = XWINDOW (w->vchild);
2780 size = 0;
2782 if (width_p)
2784 /* The minimum width of a vertical combination is the maximum
2785 of the minimum widths of its children. */
2786 while (c)
2788 size = max (window_min_size_1 (c, 1, safe_p), size);
2789 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2792 else
2794 /* The minimum height of a vertical combination is the sum of
2795 the minimum height of its children. */
2796 while (c)
2798 size += window_min_size_1 (c, 0, safe_p);
2799 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2803 else
2804 /* W is a leaf window. */
2805 size = window_min_size_2 (w, width_p, safe_p);
2807 return size;
2810 /* Return the minimum size of window W, taking fixed-size windows into
2811 account. WIDTH_P non-zero means return the minimum width, otherwise
2812 return the minimum height. SAFE_P non-zero means ignore
2813 window-min-height|width but just return values that won't crash Emacs
2814 and don't hide components like fringes, scrollbars, or modelines.
2815 IGNORE_FIXED_P non-zero means ignore if W is fixed-size. Set *FIXED
2816 to 1 if W is fixed-size unless FIXED is null. */
2818 static int
2819 window_min_size (struct window *w, int width_p, int safe_p, int ignore_fixed_p, int *fixed)
2821 int size, fixed_p;
2823 if (ignore_fixed_p)
2824 fixed_p = 0;
2825 else
2826 fixed_p = window_fixed_size_p (w, width_p, 1);
2828 if (fixed)
2829 *fixed = fixed_p;
2831 if (fixed_p)
2832 size = WINDOW_TOTAL_SIZE (w, width_p);
2833 else
2834 size = window_min_size_1 (w, width_p, safe_p);
2836 return size;
2840 /* Adjust the margins of window W if text area is too small.
2841 Return 1 if window width is ok after adjustment; 0 if window
2842 is still too narrow. */
2844 static int
2845 adjust_window_margins (struct window *w)
2847 int box_cols = (WINDOW_TOTAL_COLS (w)
2848 - WINDOW_FRINGE_COLS (w)
2849 - WINDOW_SCROLL_BAR_COLS (w));
2850 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2851 + WINDOW_RIGHT_MARGIN_COLS (w));
2853 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2854 return 1;
2856 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2857 return 0;
2859 /* Window's text area is too narrow, but reducing the window
2860 margins will fix that. */
2861 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2862 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2864 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2865 w->left_margin_cols = w->right_margin_cols
2866 = make_number (margin_cols/2);
2867 else
2868 w->right_margin_cols = make_number (margin_cols);
2870 else
2871 w->left_margin_cols = make_number (margin_cols);
2872 return 1;
2875 /* Calculate new sizes for windows in the list FORWARD when their
2876 compound size goes from TOTAL to SIZE. TOTAL must be greater than
2877 SIZE. The number of windows in FORWARD is NCHILDREN, and the number
2878 that can shrink is SHRINKABLE. Fixed-size windows may be shrunk if
2879 and only if RESIZE_FIXED_P is non-zero. WIDTH_P non-zero means
2880 shrink columns, otherwise shrink lines.
2882 SAFE_P zero means windows may be sized down to window-min-height
2883 lines (window-min-window columns for WIDTH_P non-zero). SAFE_P
2884 non-zero means windows may be sized down to their minimum safe sizes
2885 taking into account the space needed to display modelines, fringes,
2886 and scrollbars.
2888 This function returns an allocated array of new sizes that the caller
2889 must free. A size -1 means the window is fixed and RESIZE_FIXED_P is
2890 zero. A size zero means the window shall be deleted. Array index 0
2891 refers to the first window in FORWARD, 1 to the second, and so on.
2893 This function resizes windows proportionally to their size. It also
2894 tries to preserve smaller windows by resizing larger windows before
2895 resizing any window to zero. If resize_proportionally is non-nil for
2896 a specific window, it will attempt to strictly resize that window
2897 proportionally, even at the expense of deleting smaller windows. */
2898 static int *
2899 shrink_windows (int total, int size, int nchildren, int shrinkable,
2900 int resize_fixed_p, Lisp_Object forward, int width_p, int safe_p)
2902 int available_resize = 0;
2903 int *new_sizes, *min_sizes;
2904 struct window *c;
2905 Lisp_Object child;
2906 int smallest = total;
2907 int total_removed = 0;
2908 int total_shrink = total - size;
2909 int i;
2911 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2912 min_sizes = xmalloc (sizeof (*min_sizes) * nchildren);
2914 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2916 int child_size;
2918 c = XWINDOW (child);
2919 child_size = WINDOW_TOTAL_SIZE (c, width_p);
2921 if (!resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2922 new_sizes[i] = -1;
2923 else
2925 new_sizes[i] = child_size;
2926 min_sizes[i] = window_min_size_1 (c, width_p, safe_p);
2927 if (child_size > min_sizes[i]
2928 && NILP (c->resize_proportionally))
2929 available_resize += child_size - min_sizes[i];
2932 /* We might need to shrink some windows to zero. Find the smallest
2933 windows and set them to 0 until we can fulfil the new size. */
2935 while (shrinkable > 1 && size + available_resize < total)
2937 for (i = 0; i < nchildren; ++i)
2938 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2939 smallest = new_sizes[i];
2941 for (i = 0; i < nchildren; ++i)
2942 if (new_sizes[i] == smallest)
2944 /* Resize this window down to zero. */
2945 new_sizes[i] = 0;
2946 if (smallest > min_sizes[i])
2947 available_resize -= smallest - min_sizes[i];
2948 available_resize += smallest;
2949 --shrinkable;
2950 total_removed += smallest;
2952 /* We don't know what the smallest is now. */
2953 smallest = total;
2955 /* Out of for, just remove one window at the time and
2956 check again if we have enough space. */
2957 break;
2961 /* Now, calculate the new sizes. Try to shrink each window
2962 proportional to its size. */
2963 for (i = 0; i < nchildren; ++i)
2965 if (new_sizes[i] > min_sizes[i])
2967 int to_shrink = total_shrink * new_sizes[i] / total;
2969 if (new_sizes[i] - to_shrink < min_sizes[i])
2970 to_shrink = new_sizes[i] - min_sizes[i];
2971 new_sizes[i] -= to_shrink;
2972 total_removed += to_shrink;
2976 /* Any reminder due to rounding, we just subtract from windows
2977 that are left and still can be shrunk. */
2978 while (total_shrink > total_removed)
2980 int nonzero_sizes = 0;
2982 for (i = 0; i < nchildren; ++i)
2983 if (new_sizes[i] > 0)
2984 ++nonzero_sizes;
2986 for (i = 0; i < nchildren; ++i)
2987 if (new_sizes[i] > min_sizes[i])
2989 --new_sizes[i];
2990 ++total_removed;
2992 /* Out of for, just shrink one window at the time and
2993 check again if we have enough space. */
2994 break;
2997 /* Special case, only one window left. */
2998 if (nonzero_sizes == 1)
2999 break;
3002 /* Any surplus due to rounding, we add to windows that are left. */
3003 while (total_shrink < total_removed)
3005 for (i = 0; i < nchildren; ++i)
3007 if (new_sizes[i] != 0 && total_shrink < total_removed)
3009 ++new_sizes[i];
3010 --total_removed;
3011 break;
3016 xfree (min_sizes);
3018 return new_sizes;
3021 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
3022 WINDOW's width. Resize WINDOW's children, if any, so that they keep
3023 their proportionate size relative to WINDOW.
3025 If FIRST_ONLY is 1, change only the first of WINDOW's children when
3026 they are in series. If LAST_ONLY is 1, change only the last of
3027 WINDOW's children when they are in series.
3029 Propagate WINDOW's top or left edge position to children. Delete
3030 windows that become too small unless NODELETE_P is 1. When
3031 NODELETE_P equals 2 do not honor settings for window-min-height and
3032 window-min-width when resizing windows but use safe defaults instead.
3033 This should give better behavior when resizing frames. */
3035 static void
3036 size_window (Lisp_Object window, int size, int width_p, int nodelete_p, int first_only, int last_only)
3038 struct window *w = XWINDOW (window);
3039 struct window *c;
3040 Lisp_Object child, *forward, *sideward;
3041 int old_size = WINDOW_TOTAL_SIZE (w, width_p);
3043 size = max (0, size);
3045 /* Delete WINDOW if it's too small. */
3046 if (nodelete_p != 1 && !NILP (w->parent)
3047 && size < window_min_size_1 (w, width_p, nodelete_p == 2))
3049 delete_window (window);
3050 return;
3053 /* Set redisplay hints. */
3054 w->last_modified = make_number (0);
3055 w->last_overlay_modified = make_number (0);
3056 windows_or_buffers_changed++;
3057 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
3059 if (width_p)
3061 sideward = &w->vchild;
3062 forward = &w->hchild;
3063 w->total_cols = make_number (size);
3064 adjust_window_margins (w);
3066 else
3068 sideward = &w->hchild;
3069 forward = &w->vchild;
3070 w->total_lines = make_number (size);
3071 w->orig_total_lines = Qnil;
3074 if (!NILP (*sideward))
3076 /* We have a chain of parallel siblings whose size should all change. */
3077 for (child = *sideward; !NILP (child); child = c->next)
3079 c = XWINDOW (child);
3080 if (width_p)
3081 c->left_col = w->left_col;
3082 else
3083 c->top_line = w->top_line;
3084 size_window (child, size, width_p, nodelete_p,
3085 first_only, last_only);
3088 else if (!NILP (*forward) && last_only)
3090 /* Change the last in a series of siblings. */
3091 Lisp_Object last_child;
3092 int child_size;
3094 for (child = *forward; !NILP (child); child = c->next)
3096 c = XWINDOW (child);
3097 last_child = child;
3100 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3101 size_window (last_child, size - old_size + child_size,
3102 width_p, nodelete_p, first_only, last_only);
3104 else if (!NILP (*forward) && first_only)
3106 /* Change the first in a series of siblings. */
3107 int child_size;
3109 child = *forward;
3110 c = XWINDOW (child);
3112 if (width_p)
3113 c->left_col = w->left_col;
3114 else
3115 c->top_line = w->top_line;
3117 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3118 size_window (child, size - old_size + child_size,
3119 width_p, nodelete_p, first_only, last_only);
3121 else if (!NILP (*forward))
3123 int fixed_size, each IF_LINT (= 0), extra IF_LINT (= 0), n;
3124 int resize_fixed_p, nfixed;
3125 int last_pos, first_pos, nchildren, total;
3126 int *new_sizes = NULL;
3128 /* Determine the fixed-size portion of this window, and the
3129 number of child windows. */
3130 fixed_size = nchildren = nfixed = total = 0;
3131 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
3133 int child_size;
3135 c = XWINDOW (child);
3136 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3137 total += child_size;
3139 if (window_fixed_size_p (c, width_p, 0))
3141 fixed_size += child_size;
3142 ++nfixed;
3146 /* If the new size is smaller than fixed_size, or if there
3147 aren't any resizable windows, allow resizing fixed-size
3148 windows. */
3149 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3151 /* Compute how many lines/columns to add/remove to each child. The
3152 value of extra takes care of rounding errors. */
3153 n = resize_fixed_p ? nchildren : nchildren - nfixed;
3154 if (size < total && n > 1)
3155 new_sizes = shrink_windows (total, size, nchildren, n,
3156 resize_fixed_p, *forward, width_p,
3157 nodelete_p == 2);
3158 else
3160 each = (size - total) / n;
3161 extra = (size - total) - n * each;
3164 /* Compute new children heights and edge positions. */
3165 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
3166 last_pos = first_pos;
3167 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
3169 int new_child_size, old_child_size;
3171 c = XWINDOW (child);
3172 old_child_size = WINDOW_TOTAL_SIZE (c, width_p);
3173 new_child_size = old_child_size;
3175 /* The top or left edge position of this child equals the
3176 bottom or right edge of its predecessor. */
3177 if (width_p)
3178 c->left_col = make_number (last_pos);
3179 else
3180 c->top_line = make_number (last_pos);
3182 /* If this child can be resized, do it. */
3183 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3185 new_child_size =
3186 new_sizes ? new_sizes[n] : old_child_size + each + extra;
3187 extra = 0;
3190 /* Set new size. Note that size_window also propagates
3191 edge positions to children, so it's not a no-op if we
3192 didn't change the child's size. */
3193 size_window (child, new_child_size, width_p, 1,
3194 first_only, last_only);
3196 /* Remember the bottom/right edge position of this child; it
3197 will be used to set the top/left edge of the next child. */
3198 last_pos += new_child_size;
3201 xfree (new_sizes);
3203 /* We should have covered the parent exactly with child windows. */
3204 xassert (size == last_pos - first_pos);
3206 /* Now delete any children that became too small. */
3207 if (nodelete_p != 1)
3208 for (child = *forward; !NILP (child); child = c->next)
3210 int child_size;
3212 c = XWINDOW (child);
3213 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3214 size_window (child, child_size, width_p, nodelete_p,
3215 first_only, last_only);
3220 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3221 WINDOW's children. NODELETE zero means windows that have become
3222 smaller than window-min-height in the process may be deleted.
3223 NODELETE 1 means never delete windows that become too small in the
3224 process. (The caller should check later and do so if appropriate.)
3225 NODELETE 2 means delete only windows that have become too small to be
3226 displayed correctly. */
3228 void
3229 set_window_height (Lisp_Object window, int height, int nodelete)
3231 size_window (window, height, 0, nodelete, 0, 0);
3234 /* Set WINDOW's width to WIDTH, and recursively change the width of
3235 WINDOW's children. NODELETE zero means windows that have become
3236 smaller than window-min-width in the process may be deleted.
3237 NODELETE 1 means never delete windows that become too small in the
3238 process. (The caller should check later and do so if appropriate.)
3239 NODELETE 2 means delete only windows that have become too small to be
3240 displayed correctly. */
3242 void
3243 set_window_width (Lisp_Object window, int width, int nodelete)
3245 size_window (window, width, 1, nodelete, 0, 0);
3248 /* Change window heights in windows rooted in WINDOW by N lines. */
3250 void
3251 change_window_heights (Lisp_Object window, int n)
3253 struct window *w = XWINDOW (window);
3255 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3256 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3258 if (INTEGERP (w->orig_top_line))
3259 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3260 if (INTEGERP (w->orig_total_lines))
3261 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3263 /* Handle just the top child in a vertical split. */
3264 if (!NILP (w->vchild))
3265 change_window_heights (w->vchild, n);
3267 /* Adjust all children in a horizontal split. */
3268 for (window = w->hchild; !NILP (window); window = w->next)
3270 w = XWINDOW (window);
3271 change_window_heights (window, n);
3276 int window_select_count;
3278 EXFUN (Fset_window_fringes, 4);
3279 EXFUN (Fset_window_scroll_bars, 4);
3281 static void
3282 run_funs (Lisp_Object funs)
3284 for (; CONSP (funs); funs = XCDR (funs))
3285 if (!EQ (XCAR (funs), Qt))
3286 call0 (XCAR (funs));
3289 static Lisp_Object select_window_norecord (Lisp_Object window);
3290 static Lisp_Object select_frame_norecord (Lisp_Object frame);
3292 void
3293 run_window_configuration_change_hook (struct frame *f)
3295 int count = SPECPDL_INDEX ();
3296 Lisp_Object frame, global_wcch
3297 = Fdefault_value (Qwindow_configuration_change_hook);
3298 XSETFRAME (frame, f);
3300 if (NILP (Vrun_hooks))
3301 return;
3303 if (SELECTED_FRAME () != f)
3305 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3306 Fselect_frame (frame, Qt);
3309 /* Use the right buffer. Matters when running the local hooks. */
3310 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3312 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3313 Fset_buffer (Fwindow_buffer (Qnil));
3316 /* Look for buffer-local values. */
3318 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3319 for (; CONSP (windows); windows = XCDR (windows))
3321 Lisp_Object window = XCAR (windows);
3322 Lisp_Object buffer = Fwindow_buffer (window);
3323 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3324 buffer)))
3326 int count1 = SPECPDL_INDEX ();
3327 record_unwind_protect (select_window_norecord, Fselected_window ());
3328 select_window_norecord (window);
3329 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3330 buffer));
3331 unbind_to (count1, Qnil);
3336 run_funs (global_wcch);
3337 unbind_to (count, Qnil);
3340 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3341 means it's allowed to run hooks. See make_frame for a case where
3342 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3343 margins, fringes, and scroll-bar settings of the window are not
3344 reset from the buffer's local settings. */
3346 void
3347 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
3349 struct window *w = XWINDOW (window);
3350 struct buffer *b = XBUFFER (buffer);
3351 int count = SPECPDL_INDEX ();
3352 int samebuf = EQ (buffer, w->buffer);
3354 w->buffer = buffer;
3356 if (EQ (window, selected_window))
3357 BVAR (b, last_selected_window) = window;
3359 /* Let redisplay errors through. */
3360 b->display_error_modiff = 0;
3362 /* Update time stamps of buffer display. */
3363 if (INTEGERP (BVAR (b, display_count)))
3364 XSETINT (BVAR (b, display_count), XINT (BVAR (b, display_count)) + 1);
3365 BVAR (b, display_time) = Fcurrent_time ();
3367 XSETFASTINT (w->window_end_pos, 0);
3368 XSETFASTINT (w->window_end_vpos, 0);
3369 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3370 w->window_end_valid = Qnil;
3371 if (!(keep_margins_p && samebuf))
3372 { /* If we're not actually changing the buffer, don't reset hscroll and
3373 vscroll. This case happens for example when called from
3374 change_frame_size_1, where we use a dummy call to
3375 Fset_window_buffer on the frame's selected window (and no other)
3376 just in order to run window-configuration-change-hook.
3377 Resetting hscroll and vscroll here is problematic for things like
3378 image-mode and doc-view-mode since it resets the image's position
3379 whenever we resize the frame. */
3380 w->hscroll = w->min_hscroll = make_number (0);
3381 w->vscroll = 0;
3382 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3383 set_marker_restricted (w->start,
3384 make_number (b->last_window_start),
3385 buffer);
3386 w->start_at_line_beg = Qnil;
3387 w->force_start = Qnil;
3388 XSETFASTINT (w->last_modified, 0);
3389 XSETFASTINT (w->last_overlay_modified, 0);
3391 /* Maybe we could move this into the `if' but it's not obviously safe and
3392 I doubt it's worth the trouble. */
3393 windows_or_buffers_changed++;
3395 /* We must select BUFFER for running the window-scroll-functions. */
3396 /* We can't check ! NILP (Vwindow_scroll_functions) here
3397 because that might itself be a local variable. */
3398 if (window_initialized)
3400 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3401 Fset_buffer (buffer);
3404 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3406 if (!keep_margins_p)
3408 /* Set left and right marginal area width etc. from buffer. */
3410 /* This may call adjust_window_margins three times, so
3411 temporarily disable window margins. */
3412 Lisp_Object save_left = w->left_margin_cols;
3413 Lisp_Object save_right = w->right_margin_cols;
3415 w->left_margin_cols = w->right_margin_cols = Qnil;
3417 Fset_window_fringes (window,
3418 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3419 BVAR (b, fringes_outside_margins));
3421 Fset_window_scroll_bars (window,
3422 BVAR (b, scroll_bar_width),
3423 BVAR (b, vertical_scroll_bar_type), Qnil);
3425 w->left_margin_cols = save_left;
3426 w->right_margin_cols = save_right;
3428 Fset_window_margins (window,
3429 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3432 if (run_hooks_p)
3434 if (! NILP (Vwindow_scroll_functions))
3435 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3436 Fmarker_position (w->start));
3437 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3440 unbind_to (count, Qnil);
3444 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3445 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3446 WINDOW defaults to the selected window. BUFFER-OR-NAME must be a buffer
3447 or the name of an existing buffer. Optional third argument KEEP-MARGINS
3448 non-nil means that WINDOW's current display margins, fringe widths, and
3449 scroll bar settings are preserved; the default is to reset these from
3450 the local settings for BUFFER-OR-NAME or the frame defaults. Return nil.
3452 This function throws an error when WINDOW is strongly dedicated to its
3453 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3454 already display BUFFER-OR-NAME.
3456 This function runs `window-scroll-functions' before running
3457 `window-configuration-change-hook'. */)
3458 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3460 register Lisp_Object tem, buffer;
3461 register struct window *w = decode_window (window);
3463 XSETWINDOW (window, w);
3464 buffer = Fget_buffer (buffer_or_name);
3465 CHECK_BUFFER (buffer);
3466 if (NILP (BVAR (XBUFFER (buffer), name)))
3467 error ("Attempt to display deleted buffer");
3469 tem = w->buffer;
3470 if (NILP (tem))
3471 error ("Window is deleted");
3472 else if (!EQ (tem, Qt))
3473 /* w->buffer is t when the window is first being set up. */
3475 if (EQ (tem, buffer))
3476 return Qnil;
3477 else if (EQ (w->dedicated, Qt))
3478 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3479 else
3480 w->dedicated = Qnil;
3482 unshow_buffer (w);
3485 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3486 return Qnil;
3489 /* If select_window is called with inhibit_point_swap non-zero it will
3490 not store point of the old selected window's buffer back into that
3491 window's pointm slot. This is needed by Fset_window_configuration to
3492 avoid that the display routine is called with selected_window set to
3493 Qnil causing a subsequent crash. */
3495 static Lisp_Object
3496 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
3498 register struct window *w;
3499 register struct window *ow;
3500 struct frame *sf;
3502 CHECK_LIVE_WINDOW (window);
3504 w = XWINDOW (window);
3505 w->frozen_window_start_p = 0;
3507 if (NILP (norecord))
3509 ++window_select_count;
3510 XSETFASTINT (w->use_time, window_select_count);
3511 record_buffer (w->buffer);
3514 if (EQ (window, selected_window) && !inhibit_point_swap)
3515 return window;
3517 sf = SELECTED_FRAME ();
3518 if (XFRAME (WINDOW_FRAME (w)) != sf)
3520 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3521 /* Use this rather than Fhandle_switch_frame
3522 so that FRAME_FOCUS_FRAME is moved appropriately as we
3523 move around in the state where a minibuffer in a separate
3524 frame is active. */
3525 Fselect_frame (WINDOW_FRAME (w), norecord);
3526 /* Fselect_frame called us back so we've done all the work already. */
3527 eassert (EQ (window, selected_window));
3528 return window;
3530 else
3531 sf->selected_window = window;
3533 /* Store the current buffer's actual point into the
3534 old selected window. It belongs to that window,
3535 and when the window is not selected, must be in the window. */
3536 if (!inhibit_point_swap)
3538 ow = XWINDOW (selected_window);
3539 if (! NILP (ow->buffer))
3540 set_marker_both (ow->pointm, ow->buffer,
3541 BUF_PT (XBUFFER (ow->buffer)),
3542 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3545 selected_window = window;
3547 Fset_buffer (w->buffer);
3549 BVAR (XBUFFER (w->buffer), last_selected_window) = window;
3551 /* Go to the point recorded in the window.
3552 This is important when the buffer is in more
3553 than one window. It also matters when
3554 redisplay_window has altered point after scrolling,
3555 because it makes the change only in the window. */
3557 register EMACS_INT new_point = marker_position (w->pointm);
3558 if (new_point < BEGV)
3559 SET_PT (BEGV);
3560 else if (new_point > ZV)
3561 SET_PT (ZV);
3562 else
3563 SET_PT (new_point);
3566 windows_or_buffers_changed++;
3567 return window;
3571 /* Note that selected_window can be nil when this is called from
3572 Fset_window_configuration. */
3574 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3575 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3576 If WINDOW is not already selected, make WINDOW's buffer current
3577 and make WINDOW the frame's selected window. Return WINDOW.
3578 Optional second arg NORECORD non-nil means do not put this buffer
3579 at the front of the list of recently selected ones and do not
3580 make this window the most recently selected one.
3582 Note that the main editor command loop selects the buffer of the
3583 selected window before each command. */)
3584 (register Lisp_Object window, Lisp_Object norecord)
3586 return select_window (window, norecord, 0);
3589 static Lisp_Object
3590 select_window_norecord (Lisp_Object window)
3592 return WINDOW_LIVE_P (window)
3593 ? Fselect_window (window, Qt) : selected_window;
3596 static Lisp_Object
3597 select_frame_norecord (Lisp_Object frame)
3599 return FRAME_LIVE_P (XFRAME (frame))
3600 ? Fselect_frame (frame, Qt) : selected_frame;
3603 static Lisp_Object
3604 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3606 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3609 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3610 0, 1, 0,
3611 doc: /* Force all windows to be updated on next redisplay.
3612 If optional arg OBJECT is a window, force redisplay of that window only.
3613 If OBJECT is a buffer or buffer name, force redisplay of all windows
3614 displaying that buffer. */)
3615 (Lisp_Object object)
3617 if (NILP (object))
3619 windows_or_buffers_changed++;
3620 update_mode_lines++;
3621 return Qt;
3624 if (WINDOWP (object))
3626 struct window *w = XWINDOW (object);
3627 mark_window_display_accurate (object, 0);
3628 w->update_mode_line = Qt;
3629 if (BUFFERP (w->buffer))
3630 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3631 ++update_mode_lines;
3632 return Qt;
3635 if (STRINGP (object))
3636 object = Fget_buffer (object);
3637 if (BUFFERP (object) && !NILP (BVAR (XBUFFER (object), name)))
3639 /* Walk all windows looking for buffer, and force update
3640 of each of those windows. */
3642 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3643 return NILP (object) ? Qnil : Qt;
3646 /* If nothing suitable was found, just return.
3647 We could signal an error, but this feature will typically be used
3648 asynchronously in timers or process sentinels, so we don't. */
3649 return Qnil;
3652 void
3653 temp_output_buffer_show (register Lisp_Object buf)
3655 register struct buffer *old = current_buffer;
3656 register Lisp_Object window;
3657 register struct window *w;
3659 BVAR (XBUFFER (buf), directory) = BVAR (current_buffer, directory);
3661 Fset_buffer (buf);
3662 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3663 BEGV = BEG;
3664 ZV = Z;
3665 SET_PT (BEG);
3666 #if 0 /* rms: there should be no reason for this. */
3667 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3668 #endif
3669 set_buffer_internal (old);
3671 if (!NILP (Vtemp_buffer_show_function))
3672 call1 (Vtemp_buffer_show_function, buf);
3673 else
3675 window = display_buffer (buf, Qnil, Qnil);
3677 if (!EQ (XWINDOW (window)->frame, selected_frame))
3678 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3679 Vminibuf_scroll_window = window;
3680 w = XWINDOW (window);
3681 XSETFASTINT (w->hscroll, 0);
3682 XSETFASTINT (w->min_hscroll, 0);
3683 set_marker_restricted_both (w->start, buf, BEG, BEG);
3684 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3686 /* Run temp-buffer-show-hook, with the chosen window selected
3687 and its buffer current. */
3689 int count = SPECPDL_INDEX ();
3690 Lisp_Object prev_window, prev_buffer;
3691 prev_window = selected_window;
3692 XSETBUFFER (prev_buffer, old);
3694 /* Select the window that was chosen, for running the hook.
3695 Note: Both Fselect_window and select_window_norecord may
3696 set-buffer to the buffer displayed in the window,
3697 so we need to save the current buffer. --stef */
3698 record_unwind_protect (Fset_buffer, prev_buffer);
3699 record_unwind_protect (select_window_norecord, prev_window);
3700 Fselect_window (window, Qt);
3701 Fset_buffer (w->buffer);
3702 Frun_hooks (1, &Qtemp_buffer_show_hook);
3703 unbind_to (count, Qnil);
3708 DEFUN ("internal-temp-output-buffer-show",
3709 Ftemp_output_buffer_show, Stemp_output_buffer_show,
3710 1, 1, 0,
3711 doc: /* Internal function for `with-output-to-temp-buffer''. */)
3712 (Lisp_Object buf)
3714 temp_output_buffer_show (buf);
3715 return Qnil;
3718 static void
3719 make_dummy_parent (Lisp_Object window)
3721 Lisp_Object new;
3722 register struct window *o, *p;
3723 int i;
3725 o = XWINDOW (window);
3726 p = allocate_window ();
3727 for (i = 0; i < VECSIZE (struct window); ++i)
3728 ((struct Lisp_Vector *) p)->contents[i]
3729 = ((struct Lisp_Vector *)o)->contents[i];
3730 XSETWINDOW (new, p);
3732 ++sequence_number;
3733 XSETFASTINT (p->sequence_number, sequence_number);
3735 /* Put new into window structure in place of window */
3736 replace_window (window, new);
3738 o->next = Qnil;
3739 o->prev = Qnil;
3740 o->vchild = Qnil;
3741 o->hchild = Qnil;
3742 o->parent = new;
3744 p->start = Qnil;
3745 p->pointm = Qnil;
3746 p->buffer = Qnil;
3749 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3750 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3751 WINDOW defaults to selected one and SIZE to half its size.
3752 If optional third arg HORIZONTAL is non-nil, split side by side and put
3753 SIZE columns in the first of the pair. In that case, SIZE includes that
3754 window's scroll bar, or the divider column to its right.
3755 Interactively, all arguments are nil.
3756 Returns the newly created window (which is the lower or rightmost one).
3757 The upper or leftmost window is the original one, and remains selected
3758 if it was selected before.
3760 See Info node `(elisp)Splitting Windows' for more details and examples. */)
3761 (Lisp_Object window, Lisp_Object size, Lisp_Object horizontal)
3763 register Lisp_Object new;
3764 register struct window *o, *p;
3765 FRAME_PTR fo;
3766 register int size_int;
3768 if (NILP (window))
3769 window = selected_window;
3770 else
3771 CHECK_LIVE_WINDOW (window);
3773 o = XWINDOW (window);
3774 fo = XFRAME (WINDOW_FRAME (o));
3776 if (NILP (size))
3778 if (!NILP (horizontal))
3779 /* Calculate the size of the left-hand window, by dividing
3780 the usable space in columns by two.
3781 We round up, since the left-hand window may include
3782 a dividing line, while the right-hand may not. */
3783 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3784 else
3785 size_int = XFASTINT (o->total_lines) >> 1;
3787 else
3789 CHECK_NUMBER (size);
3790 size_int = XINT (size);
3793 if (MINI_WINDOW_P (o))
3794 error ("Attempt to split minibuffer window");
3795 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
3796 error ("Attempt to split fixed-size window");
3798 if (NILP (horizontal))
3800 int window_safe_height = window_min_size_2 (o, 0, 0);
3802 if (size_int < window_safe_height)
3803 error ("Window height %d too small (after splitting)", size_int);
3804 if (size_int + window_safe_height > XFASTINT (o->total_lines))
3805 error ("Window height %d too small (after splitting)",
3806 XFASTINT (o->total_lines) - size_int);
3807 if (NILP (o->parent)
3808 || NILP (XWINDOW (o->parent)->vchild))
3810 make_dummy_parent (window);
3811 new = o->parent;
3812 XWINDOW (new)->vchild = window;
3815 else
3817 int window_safe_width = window_min_size_2 (o, 1, 0);
3819 if (size_int < window_safe_width)
3820 error ("Window width %d too small (after splitting)", size_int);
3821 if (size_int + window_safe_width > XFASTINT (o->total_cols))
3822 error ("Window width %d too small (after splitting)",
3823 XFASTINT (o->total_cols) - size_int);
3824 if (NILP (o->parent)
3825 || NILP (XWINDOW (o->parent)->hchild))
3827 make_dummy_parent (window);
3828 new = o->parent;
3829 XWINDOW (new)->hchild = window;
3833 /* Now we know that window's parent is a vertical combination
3834 if we are dividing vertically, or a horizontal combination
3835 if we are making side-by-side windows */
3837 windows_or_buffers_changed++;
3838 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3839 new = make_window ();
3840 p = XWINDOW (new);
3842 p->frame = o->frame;
3843 p->next = o->next;
3844 if (!NILP (p->next))
3845 XWINDOW (p->next)->prev = new;
3846 p->prev = window;
3847 o->next = new;
3848 p->parent = o->parent;
3849 p->buffer = Qt;
3850 p->window_end_valid = Qnil;
3851 memset (&p->last_cursor, 0, sizeof p->last_cursor);
3853 /* Duplicate special geometry settings. */
3855 p->left_margin_cols = o->left_margin_cols;
3856 p->right_margin_cols = o->right_margin_cols;
3857 p->left_fringe_width = o->left_fringe_width;
3858 p->right_fringe_width = o->right_fringe_width;
3859 p->fringes_outside_margins = o->fringes_outside_margins;
3860 p->scroll_bar_width = o->scroll_bar_width;
3861 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3863 /* Apportion the available frame space among the two new windows */
3865 if (!NILP (horizontal))
3867 p->total_lines = o->total_lines;
3868 p->top_line = o->top_line;
3869 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3870 XSETFASTINT (o->total_cols, size_int);
3871 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3872 adjust_window_margins (p);
3873 adjust_window_margins (o);
3875 else
3877 p->left_col = o->left_col;
3878 p->total_cols = o->total_cols;
3879 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3880 XSETFASTINT (o->total_lines, size_int);
3881 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3884 /* Adjust glyph matrices. */
3885 adjust_glyphs (fo);
3887 Fset_window_buffer (new, o->buffer, Qt);
3888 return new;
3891 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3892 doc: /* Make selected window SIZE lines taller.
3893 Interactively, if no argument is given, make the selected window one
3894 line taller. If optional argument HORIZONTAL is non-nil, make selected
3895 window wider by SIZE columns. If SIZE is negative, shrink the window by
3896 -SIZE lines or columns. Return nil.
3898 This function can delete windows if they get too small. The size of
3899 fixed size windows is not altered by this function. */)
3900 (Lisp_Object size, Lisp_Object horizontal)
3902 CHECK_NUMBER (size);
3903 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
3905 run_window_configuration_change_hook (SELECTED_FRAME ());
3907 return Qnil;
3910 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3911 doc: /* Make selected window SIZE lines smaller.
3912 Interactively, if no argument is given, make the selected window one
3913 line smaller. If optional argument HORIZONTAL is non-nil, make the
3914 window narrower by SIZE columns. If SIZE is negative, enlarge selected
3915 window by -SIZE lines or columns. Return nil.
3917 This function can delete windows if they get too small. The size of
3918 fixed size windows is not altered by this function. */)
3919 (Lisp_Object size, Lisp_Object horizontal)
3921 CHECK_NUMBER (size);
3922 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
3924 run_window_configuration_change_hook (SELECTED_FRAME ());
3926 return Qnil;
3929 static int
3930 window_height (Lisp_Object window)
3932 register struct window *p = XWINDOW (window);
3933 return WINDOW_TOTAL_LINES (p);
3936 static int
3937 window_width (Lisp_Object window)
3939 register struct window *p = XWINDOW (window);
3940 return WINDOW_TOTAL_COLS (p);
3944 #define CURBEG(w) \
3945 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3947 #define CURSIZE(w) \
3948 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3951 /* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
3952 horizontally; zero means do it vertically.
3954 Siblings of the selected window are resized to fulfill the size
3955 request. If they become too small in the process, they may be
3956 deleted. */
3958 static void
3959 enlarge_window (Lisp_Object window, int delta, int horiz_flag)
3961 Lisp_Object parent, next, prev;
3962 struct window *p;
3963 Lisp_Object *sizep;
3964 int maximum;
3965 int (*sizefun) (Lisp_Object)
3966 = horiz_flag ? window_width : window_height;
3967 void (*setsizefun) (Lisp_Object, int, int)
3968 = (horiz_flag ? set_window_width : set_window_height);
3970 /* Give up if this window cannot be resized. */
3971 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
3972 error ("Window is not resizable");
3974 /* Find the parent of the selected window. */
3975 while (1)
3977 p = XWINDOW (window);
3978 parent = p->parent;
3980 if (NILP (parent))
3982 if (horiz_flag)
3983 error ("No other window to side of this one");
3984 break;
3987 if (horiz_flag
3988 ? !NILP (XWINDOW (parent)->hchild)
3989 : !NILP (XWINDOW (parent)->vchild))
3990 break;
3992 window = parent;
3995 sizep = &CURSIZE (window);
3998 register int maxdelta;
4000 /* Compute the maximum size increment this window can have. */
4002 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4003 /* This is a main window followed by a minibuffer. */
4004 : !NILP (p->next) ? ((*sizefun) (p->next)
4005 - window_min_size (XWINDOW (p->next),
4006 horiz_flag, 0, 0, 0))
4007 /* This is a minibuffer following a main window. */
4008 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4009 - window_min_size (XWINDOW (p->prev),
4010 horiz_flag, 0, 0, 0))
4011 /* This is a frame with only one window, a minibuffer-only
4012 or a minibufferless frame. */
4013 : (delta = 0));
4015 if (delta > maxdelta)
4016 /* This case traps trying to make the minibuffer
4017 the full frame, or make the only window aside from the
4018 minibuffer the full frame. */
4019 delta = maxdelta;
4022 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4023 horiz_flag, 0, 0, 0))
4025 delete_window (window);
4026 return;
4029 if (delta == 0)
4030 return;
4032 /* Find the total we can get from other siblings without deleting them. */
4033 maximum = 0;
4034 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
4035 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4036 horiz_flag, 0, 0, 0);
4037 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
4038 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4039 horiz_flag, 0, 0, 0);
4041 /* If we can get it all from them without deleting them, do so. */
4042 if (delta <= maximum)
4044 Lisp_Object first_unaffected;
4045 Lisp_Object first_affected;
4046 int fixed_p;
4048 next = p->next;
4049 prev = p->prev;
4050 first_affected = window;
4051 /* Look at one sibling at a time,
4052 moving away from this window in both directions alternately,
4053 and take as much as we can get without deleting that sibling. */
4054 while (delta != 0
4055 && (!NILP (next) || !NILP (prev)))
4057 if (! NILP (next))
4059 int this_one = ((*sizefun) (next)
4060 - window_min_size (XWINDOW (next), horiz_flag,
4061 0, 0, &fixed_p));
4062 if (!fixed_p)
4064 if (this_one > delta)
4065 this_one = delta;
4067 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4068 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4070 delta -= this_one;
4073 next = XWINDOW (next)->next;
4076 if (delta == 0)
4077 break;
4079 if (! NILP (prev))
4081 int this_one = ((*sizefun) (prev)
4082 - window_min_size (XWINDOW (prev), horiz_flag,
4083 0, 0, &fixed_p));
4084 if (!fixed_p)
4086 if (this_one > delta)
4087 this_one = delta;
4089 first_affected = prev;
4091 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4092 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4094 delta -= this_one;
4097 prev = XWINDOW (prev)->prev;
4101 xassert (delta == 0);
4103 /* Now recalculate the edge positions of all the windows affected,
4104 based on the new sizes. */
4105 first_unaffected = next;
4106 prev = first_affected;
4107 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4108 prev = next, next = XWINDOW (next)->next)
4110 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4111 /* This does not change size of NEXT,
4112 but it propagates the new top edge to its children */
4113 (*setsizefun) (next, (*sizefun) (next), 0);
4116 else
4118 register int delta1;
4119 register int opht = (*sizefun) (parent);
4121 if (opht <= XINT (*sizep) + delta)
4123 /* If trying to grow this window to or beyond size of the parent,
4124 just delete all the sibling windows. */
4125 Lisp_Object start, tem;
4127 start = XWINDOW (parent)->vchild;
4128 if (NILP (start))
4129 start = XWINDOW (parent)->hchild;
4131 /* Delete any siblings that come after WINDOW. */
4132 tem = XWINDOW (window)->next;
4133 while (! NILP (tem))
4135 Lisp_Object next1 = XWINDOW (tem)->next;
4136 delete_window (tem);
4137 tem = next1;
4140 /* Delete any siblings that come after WINDOW.
4141 Note that if START is not WINDOW, then WINDOW still
4142 has siblings, so WINDOW has not yet replaced its parent. */
4143 tem = start;
4144 while (! EQ (tem, window))
4146 Lisp_Object next1 = XWINDOW (tem)->next;
4147 delete_window (tem);
4148 tem = next1;
4151 else
4153 /* Otherwise, make delta1 just right so that if we add
4154 delta1 lines to this window and to the parent, and then
4155 shrink the parent back to its original size, the new
4156 proportional size of this window will increase by delta.
4158 The function size_window will compute the new height h'
4159 of the window from delta1 as:
4161 e = delta1/n
4162 x = delta1 - delta1/n * n for the 1st resizable child
4163 h' = h + e + x
4165 where n is the number of children that can be resized.
4166 We can ignore x by choosing a delta1 that is a multiple of
4167 n. We want the height of this window to come out as
4169 h' = h + delta
4171 So, delta1 must be
4173 h + e = h + delta
4174 delta1/n = delta
4175 delta1 = n * delta.
4177 The number of children n equals the number of resizable
4178 children of this window + 1 because we know window itself
4179 is resizable (otherwise we would have signaled an error).
4181 This reasoning is not correct when other windows become too
4182 small and shrink_windows refuses to delete them. Below we
4183 use resize_proportionally to work around this problem. */
4185 struct window *w = XWINDOW (window);
4186 Lisp_Object s;
4187 int n = 1;
4189 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
4190 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4191 ++n;
4192 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
4193 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4194 ++n;
4196 delta1 = n * delta;
4198 /* Add delta1 lines or columns to this window, and to the parent,
4199 keeping things consistent while not affecting siblings. */
4200 XSETINT (CURSIZE (parent), opht + delta1);
4201 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4203 /* Squeeze out delta1 lines or columns from our parent,
4204 shrinking this window and siblings proportionately. This
4205 brings parent back to correct size. Delta1 was calculated
4206 so this makes this window the desired size, taking it all
4207 out of the siblings.
4209 Temporarily set resize_proportionally to Qt to assure that,
4210 if necessary, shrink_windows deletes smaller windows rather
4211 than shrink this window. */
4212 w->resize_proportionally = Qt;
4213 (*setsizefun) (parent, opht, 0);
4214 w->resize_proportionally = Qnil;
4218 XSETFASTINT (p->last_modified, 0);
4219 XSETFASTINT (p->last_overlay_modified, 0);
4221 /* Adjust glyph matrices. */
4222 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4226 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4227 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4228 zero means adjust the height, moving the bottom edge.
4230 Following siblings of the selected window are resized to fulfill
4231 the size request. If they become too small in the process, they
4232 are not deleted; instead, we signal an error. */
4234 static void
4235 adjust_window_trailing_edge (Lisp_Object window, int delta, int horiz_flag)
4237 Lisp_Object parent, child;
4238 struct window *p;
4239 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4240 int delcount = window_deletion_count;
4242 CHECK_WINDOW (window);
4244 /* Give up if this window cannot be resized. */
4245 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4246 error ("Window is not resizable");
4248 while (1)
4250 Lisp_Object first_parallel = Qnil;
4252 if (NILP (window))
4254 /* This happens if WINDOW on the previous iteration was
4255 at top level of the window tree. */
4256 Fset_window_configuration (old_config);
4257 error ("Specified window edge is fixed");
4260 p = XWINDOW (window);
4261 parent = p->parent;
4263 /* See if this level has windows in parallel in the specified
4264 direction. If so, set FIRST_PARALLEL to the first one. */
4265 if (horiz_flag)
4267 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4268 first_parallel = XWINDOW (parent)->vchild;
4269 else if (NILP (parent) && !NILP (p->next))
4271 /* Handle the vertical chain of main window and minibuffer
4272 which has no parent. */
4273 first_parallel = window;
4274 while (! NILP (XWINDOW (first_parallel)->prev))
4275 first_parallel = XWINDOW (first_parallel)->prev;
4278 else
4280 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4281 first_parallel = XWINDOW (parent)->hchild;
4284 /* If this level's succession is in the desired dimension,
4285 and this window is the last one, and there is no higher level,
4286 its trailing edge is fixed. */
4287 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4288 && NILP (parent))
4290 Fset_window_configuration (old_config);
4291 error ("Specified window edge is fixed");
4294 /* Don't make this window too small. */
4295 if (XINT (CURSIZE (window)) + delta
4296 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
4298 Fset_window_configuration (old_config);
4299 error ("Cannot adjust window size as specified");
4302 /* Clear out some redisplay caches. */
4303 XSETFASTINT (p->last_modified, 0);
4304 XSETFASTINT (p->last_overlay_modified, 0);
4306 /* Adjust this window's edge. */
4307 XSETINT (CURSIZE (window),
4308 XINT (CURSIZE (window)) + delta);
4310 /* If this window has following siblings in the desired dimension,
4311 make them smaller, and exit the loop.
4313 (If we reach the top of the tree and can never do this,
4314 we will fail and report an error, above.) */
4315 if (NILP (first_parallel))
4317 if (!NILP (p->next))
4319 /* This may happen for the minibuffer. In that case
4320 the window_deletion_count check below does not work. */
4321 if (XINT (CURSIZE (p->next)) - delta <= 0)
4323 Fset_window_configuration (old_config);
4324 error ("Cannot adjust window size as specified");
4327 XSETINT (CURBEG (p->next),
4328 XINT (CURBEG (p->next)) + delta);
4329 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4330 horiz_flag, 0, 1, 0);
4331 break;
4334 else
4335 /* Here we have a chain of parallel siblings, in the other dimension.
4336 Change the size of the other siblings. */
4337 for (child = first_parallel;
4338 ! NILP (child);
4339 child = XWINDOW (child)->next)
4340 if (! EQ (child, window))
4341 size_window (child, XINT (CURSIZE (child)) + delta,
4342 horiz_flag, 0, 0, 1);
4344 window = parent;
4347 /* If we made a window so small it got deleted,
4348 we failed. Report failure. */
4349 if (delcount != window_deletion_count)
4351 Fset_window_configuration (old_config);
4352 error ("Cannot adjust window size as specified");
4355 /* Adjust glyph matrices. */
4356 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4359 #undef CURBEG
4360 #undef CURSIZE
4362 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4363 Sadjust_window_trailing_edge, 3, 3, 0,
4364 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4365 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4366 Otherwise, adjust the height, moving the bottom edge.
4368 Following siblings of the selected window are resized to fulfill
4369 the size request. If they become too small in the process, they
4370 are not deleted; instead, we signal an error. */)
4371 (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal)
4373 CHECK_NUMBER (delta);
4374 if (NILP (window))
4375 window = selected_window;
4376 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4378 run_window_configuration_change_hook
4379 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4381 return Qnil;
4386 /***********************************************************************
4387 Resizing Mini-Windows
4388 ***********************************************************************/
4390 static void shrink_window_lowest_first (struct window *, int);
4392 enum save_restore_action
4394 CHECK_ORIG_SIZES,
4395 SAVE_ORIG_SIZES,
4396 RESTORE_ORIG_SIZES
4399 static int save_restore_orig_size (struct window *,
4400 enum save_restore_action);
4402 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4403 from lowest windows first. */
4405 static void
4406 shrink_window_lowest_first (struct window *w, int height)
4408 struct window *c;
4409 Lisp_Object child;
4410 int old_height;
4412 xassert (!MINI_WINDOW_P (w));
4414 /* Set redisplay hints. */
4415 XSETFASTINT (w->last_modified, 0);
4416 XSETFASTINT (w->last_overlay_modified, 0);
4417 windows_or_buffers_changed++;
4418 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4420 old_height = XFASTINT (w->total_lines);
4421 XSETFASTINT (w->total_lines, height);
4423 if (!NILP (w->hchild))
4425 for (child = w->hchild; !NILP (child); child = c->next)
4427 c = XWINDOW (child);
4428 c->top_line = w->top_line;
4429 shrink_window_lowest_first (c, height);
4432 else if (!NILP (w->vchild))
4434 Lisp_Object last_child;
4435 int delta = old_height - height;
4436 int last_top;
4438 last_child = Qnil;
4440 /* Find the last child. We are taking space from lowest windows
4441 first, so we iterate over children from the last child
4442 backwards. */
4443 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
4444 last_child = child;
4446 /* Size children down to their safe heights. */
4447 for (child = last_child; delta && !NILP (child); child = c->prev)
4449 int this_one;
4451 c = XWINDOW (child);
4452 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
4454 if (this_one > delta)
4455 this_one = delta;
4457 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4458 delta -= this_one;
4461 /* Compute new positions. */
4462 last_top = XINT (w->top_line);
4463 for (child = w->vchild; !NILP (child); child = c->next)
4465 c = XWINDOW (child);
4466 c->top_line = make_number (last_top);
4467 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4468 last_top += XFASTINT (c->total_lines);
4474 /* Save, restore, or check positions and sizes in the window tree
4475 rooted at W. ACTION says what to do.
4477 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4478 orig_total_lines members are valid for all windows in the window
4479 tree. Value is non-zero if they are valid.
4481 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4482 orig_top_line and orig_total_lines for all windows in the tree.
4484 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4485 stored in orig_top_line and orig_total_lines for all windows. */
4487 static int
4488 save_restore_orig_size (struct window *w, enum save_restore_action action)
4490 int success_p = 1;
4492 while (w)
4494 if (!NILP (w->hchild))
4496 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4497 success_p = 0;
4499 else if (!NILP (w->vchild))
4501 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4502 success_p = 0;
4505 switch (action)
4507 case CHECK_ORIG_SIZES:
4508 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4509 return 0;
4510 break;
4512 case SAVE_ORIG_SIZES:
4513 w->orig_top_line = w->top_line;
4514 w->orig_total_lines = w->total_lines;
4515 XSETFASTINT (w->last_modified, 0);
4516 XSETFASTINT (w->last_overlay_modified, 0);
4517 break;
4519 case RESTORE_ORIG_SIZES:
4520 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4521 w->top_line = w->orig_top_line;
4522 w->total_lines = w->orig_total_lines;
4523 w->orig_total_lines = w->orig_top_line = Qnil;
4524 XSETFASTINT (w->last_modified, 0);
4525 XSETFASTINT (w->last_overlay_modified, 0);
4526 break;
4528 default:
4529 abort ();
4532 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4535 return success_p;
4539 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4540 without deleting other windows. */
4542 void
4543 grow_mini_window (struct window *w, int delta)
4545 struct frame *f = XFRAME (w->frame);
4546 struct window *root;
4548 xassert (MINI_WINDOW_P (w));
4549 /* Commenting out the following assertion goes against the stated interface
4550 of the function, but it currently does not seem to do anything useful.
4551 See discussion of this issue in the thread for bug#4534.
4552 xassert (delta >= 0); */
4554 /* Compute how much we can enlarge the mini-window without deleting
4555 other windows. */
4556 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4557 if (delta > 0)
4559 int min_height = window_min_size (root, 0, 0, 0, 0);
4560 if (XFASTINT (root->total_lines) - delta < min_height)
4561 /* Note that the root window may already be smaller than
4562 min_height. */
4563 delta = max (0, XFASTINT (root->total_lines) - min_height);
4566 if (delta)
4568 /* Save original window sizes and positions, if not already done. */
4569 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4570 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4572 /* Shrink other windows. */
4573 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4575 /* Grow the mini-window. */
4576 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4577 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4578 XSETFASTINT (w->last_modified, 0);
4579 XSETFASTINT (w->last_overlay_modified, 0);
4581 adjust_glyphs (f);
4586 /* Shrink mini-window W. If there is recorded info about window sizes
4587 before a call to grow_mini_window, restore recorded window sizes.
4588 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4589 line. */
4591 void
4592 shrink_mini_window (struct window *w)
4594 struct frame *f = XFRAME (w->frame);
4595 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4597 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4599 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4600 adjust_glyphs (f);
4601 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4602 windows_or_buffers_changed = 1;
4604 else if (XFASTINT (w->total_lines) > 1)
4606 /* Distribute the additional lines of the mini-window
4607 among the other windows. */
4608 Lisp_Object window;
4609 XSETWINDOW (window, w);
4610 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4616 /* Mark window cursors off for all windows in the window tree rooted
4617 at W by setting their phys_cursor_on_p flag to zero. Called from
4618 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4619 the frame are cleared. */
4621 void
4622 mark_window_cursors_off (struct window *w)
4624 while (w)
4626 if (!NILP (w->hchild))
4627 mark_window_cursors_off (XWINDOW (w->hchild));
4628 else if (!NILP (w->vchild))
4629 mark_window_cursors_off (XWINDOW (w->vchild));
4630 else
4631 w->phys_cursor_on_p = 0;
4633 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4638 /* Return number of lines of text (not counting mode lines) in W. */
4641 window_internal_height (struct window *w)
4643 int ht = XFASTINT (w->total_lines);
4645 if (!MINI_WINDOW_P (w))
4647 if (!NILP (w->parent)
4648 || !NILP (w->vchild)
4649 || !NILP (w->hchild)
4650 || !NILP (w->next)
4651 || !NILP (w->prev)
4652 || WINDOW_WANTS_MODELINE_P (w))
4653 --ht;
4655 if (WINDOW_WANTS_HEADER_LINE_P (w))
4656 --ht;
4659 return ht;
4663 /* Return the number of columns in W.
4664 Don't count columns occupied by scroll bars or the vertical bar
4665 separating W from the sibling to its right. */
4668 window_box_text_cols (struct window *w)
4670 struct frame *f = XFRAME (WINDOW_FRAME (w));
4671 int width = XINT (w->total_cols);
4673 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4674 /* Scroll bars occupy a few columns. */
4675 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4676 else if (!FRAME_WINDOW_P (f)
4677 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4678 /* The column of `|' characters separating side-by-side windows
4679 occupies one column only. */
4680 width -= 1;
4682 if (FRAME_WINDOW_P (f))
4683 /* On window-systems, fringes and display margins cannot be
4684 used for normal text. */
4685 width -= (WINDOW_FRINGE_COLS (w)
4686 + WINDOW_LEFT_MARGIN_COLS (w)
4687 + WINDOW_RIGHT_MARGIN_COLS (w));
4689 return width;
4693 /************************************************************************
4694 Window Scrolling
4695 ***********************************************************************/
4697 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4698 N screen-fulls, which is defined as the height of the window minus
4699 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4700 instead. Negative values of N mean scroll down. NOERROR non-zero
4701 means don't signal an error if we try to move over BEGV or ZV,
4702 respectively. */
4704 static void
4705 window_scroll (Lisp_Object window, int n, int whole, int noerror)
4707 immediate_quit = 1;
4709 /* If we must, use the pixel-based version which is much slower than
4710 the line-based one but can handle varying line heights. */
4711 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4712 window_scroll_pixel_based (window, n, whole, noerror);
4713 else
4714 window_scroll_line_based (window, n, whole, noerror);
4716 immediate_quit = 0;
4720 /* Implementation of window_scroll that works based on pixel line
4721 heights. See the comment of window_scroll for parameter
4722 descriptions. */
4724 static void
4725 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4727 struct it it;
4728 struct window *w = XWINDOW (window);
4729 struct text_pos start;
4730 int this_scroll_margin;
4731 /* True if we fiddled the window vscroll field without really scrolling. */
4732 int vscrolled = 0;
4733 int x, y, rtop, rbot, rowh, vpos;
4735 SET_TEXT_POS_FROM_MARKER (start, w->start);
4737 /* If PT is not visible in WINDOW, move back one half of
4738 the screen. Allow PT to be partially visible, otherwise
4739 something like (scroll-down 1) with PT in the line before
4740 the partially visible one would recenter. */
4742 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4744 /* Move backward half the height of the window. Performance note:
4745 vmotion used here is about 10% faster, but would give wrong
4746 results for variable height lines. */
4747 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4748 it.current_y = it.last_visible_y;
4749 move_it_vertically_backward (&it, window_box_height (w) / 2);
4751 /* The function move_iterator_vertically may move over more than
4752 the specified y-distance. If it->w is small, e.g. a
4753 mini-buffer window, we may end up in front of the window's
4754 display area. This is the case when Start displaying at the
4755 start of the line containing PT in this case. */
4756 if (it.current_y <= 0)
4758 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4759 move_it_vertically_backward (&it, 0);
4760 it.current_y = 0;
4763 start = it.current.pos;
4765 else if (auto_window_vscroll_p)
4767 if (rtop || rbot) /* partially visible */
4769 int px;
4770 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4771 if (whole)
4772 dy = max ((window_box_height (w)
4773 - next_screen_context_lines * dy),
4774 dy);
4775 dy *= n;
4777 if (n < 0)
4779 /* Only vscroll backwards if already vscrolled forwards. */
4780 if (w->vscroll < 0 && rtop > 0)
4782 px = max (0, -w->vscroll - min (rtop, -dy));
4783 Fset_window_vscroll (window, make_number (px), Qt);
4784 return;
4787 if (n > 0)
4789 /* Do vscroll if already vscrolled or only display line. */
4790 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4792 px = max (0, -w->vscroll + min (rbot, dy));
4793 Fset_window_vscroll (window, make_number (px), Qt);
4794 return;
4797 /* Maybe modify window start instead of scrolling. */
4798 if (rbot > 0 || w->vscroll < 0)
4800 EMACS_INT spos;
4802 Fset_window_vscroll (window, make_number (0), Qt);
4803 /* If there are other text lines above the current row,
4804 move window start to current row. Else to next row. */
4805 if (rbot > 0)
4806 spos = XINT (Fline_beginning_position (Qnil));
4807 else
4808 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4809 set_marker_restricted (w->start, make_number (spos),
4810 w->buffer);
4811 w->start_at_line_beg = Qt;
4812 w->update_mode_line = Qt;
4813 XSETFASTINT (w->last_modified, 0);
4814 XSETFASTINT (w->last_overlay_modified, 0);
4815 /* Set force_start so that redisplay_window will run the
4816 window-scroll-functions. */
4817 w->force_start = Qt;
4818 return;
4822 /* Cancel previous vscroll. */
4823 Fset_window_vscroll (window, make_number (0), Qt);
4826 /* If scroll_preserve_screen_position is non-nil, we try to set
4827 point in the same window line as it is now, so get that line. */
4828 if (!NILP (Vscroll_preserve_screen_position))
4830 /* We preserve the goal pixel coordinate across consecutive
4831 calls to scroll-up, scroll-down and other commands that
4832 have the `scroll-command' property. This avoids the
4833 possibility of point becoming "stuck" on a tall line when
4834 scrolling by one line. */
4835 if (window_scroll_pixel_based_preserve_y < 0
4836 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4837 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4839 start_display (&it, w, start);
4840 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4841 window_scroll_pixel_based_preserve_y = it.current_y;
4842 window_scroll_pixel_based_preserve_x = it.current_x;
4845 else
4846 window_scroll_pixel_based_preserve_y
4847 = window_scroll_pixel_based_preserve_x = -1;
4849 /* Move iterator it from start the specified distance forward or
4850 backward. The result is the new window start. */
4851 start_display (&it, w, start);
4852 if (whole)
4854 EMACS_INT start_pos = IT_CHARPOS (it);
4855 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4856 dy = max ((window_box_height (w)
4857 - next_screen_context_lines * dy),
4858 dy) * n;
4860 /* Note that move_it_vertically always moves the iterator to the
4861 start of a line. So, if the last line doesn't have a newline,
4862 we would end up at the start of the line ending at ZV. */
4863 if (dy <= 0)
4865 move_it_vertically_backward (&it, -dy);
4866 /* Ensure we actually do move, e.g. in case we are currently
4867 looking at an image that is taller that the window height. */
4868 while (start_pos == IT_CHARPOS (it)
4869 && start_pos > BEGV)
4870 move_it_by_lines (&it, -1, 1);
4872 else if (dy > 0)
4874 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4875 MOVE_TO_POS | MOVE_TO_Y);
4876 /* Ensure we actually do move, e.g. in case we are currently
4877 looking at an image that is taller that the window height. */
4878 while (start_pos == IT_CHARPOS (it)
4879 && start_pos < ZV)
4880 move_it_by_lines (&it, 1, 1);
4883 else
4884 move_it_by_lines (&it, n, 1);
4886 /* We failed if we find ZV is already on the screen (scrolling up,
4887 means there's nothing past the end), or if we can't start any
4888 earlier (scrolling down, means there's nothing past the top). */
4889 if ((n > 0 && IT_CHARPOS (it) == ZV)
4890 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4892 if (IT_CHARPOS (it) == ZV)
4894 if (it.current_y < it.last_visible_y
4895 && (it.current_y + it.max_ascent + it.max_descent
4896 > it.last_visible_y))
4898 /* The last line was only partially visible, make it fully
4899 visible. */
4900 w->vscroll = (it.last_visible_y
4901 - it.current_y + it.max_ascent + it.max_descent);
4902 adjust_glyphs (it.f);
4904 else if (noerror)
4905 return;
4906 else if (n < 0) /* could happen with empty buffers */
4907 xsignal0 (Qbeginning_of_buffer);
4908 else
4909 xsignal0 (Qend_of_buffer);
4911 else
4913 if (w->vscroll != 0)
4914 /* The first line was only partially visible, make it fully
4915 visible. */
4916 w->vscroll = 0;
4917 else if (noerror)
4918 return;
4919 else
4920 xsignal0 (Qbeginning_of_buffer);
4923 /* If control gets here, then we vscrolled. */
4925 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4927 /* Don't try to change the window start below. */
4928 vscrolled = 1;
4931 if (! vscrolled)
4933 EMACS_INT pos = IT_CHARPOS (it);
4934 EMACS_INT bytepos;
4936 /* If in the middle of a multi-glyph character move forward to
4937 the next character. */
4938 if (in_display_vector_p (&it))
4940 ++pos;
4941 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4944 /* Set the window start, and set up the window for redisplay. */
4945 set_marker_restricted (w->start, make_number (pos),
4946 w->buffer);
4947 bytepos = XMARKER (w->start)->bytepos;
4948 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4949 ? Qt : Qnil);
4950 w->update_mode_line = Qt;
4951 XSETFASTINT (w->last_modified, 0);
4952 XSETFASTINT (w->last_overlay_modified, 0);
4953 /* Set force_start so that redisplay_window will run the
4954 window-scroll-functions. */
4955 w->force_start = Qt;
4958 /* The rest of this function uses current_y in a nonstandard way,
4959 not including the height of the header line if any. */
4960 it.current_y = it.vpos = 0;
4962 /* Move PT out of scroll margins.
4963 This code wants current_y to be zero at the window start position
4964 even if there is a header line. */
4965 this_scroll_margin = max (0, scroll_margin);
4966 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4967 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4969 if (n > 0)
4971 /* We moved the window start towards ZV, so PT may be now
4972 in the scroll margin at the top. */
4973 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4974 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4975 && (NILP (Vscroll_preserve_screen_position)
4976 || EQ (Vscroll_preserve_screen_position, Qt)))
4977 /* We found PT at a legitimate height. Leave it alone. */
4979 else if (window_scroll_pixel_based_preserve_y >= 0)
4981 /* If we have a header line, take account of it.
4982 This is necessary because we set it.current_y to 0, above. */
4983 move_it_to (&it, -1,
4984 window_scroll_pixel_based_preserve_x,
4985 window_scroll_pixel_based_preserve_y
4986 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4987 -1, MOVE_TO_Y | MOVE_TO_X);
4988 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4990 else
4992 while (it.current_y < this_scroll_margin)
4994 int prev = it.current_y;
4995 move_it_by_lines (&it, 1, 1);
4996 if (prev == it.current_y)
4997 break;
4999 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5002 else if (n < 0)
5004 EMACS_INT charpos, bytepos;
5005 int partial_p;
5007 /* Save our position, for the
5008 window_scroll_pixel_based_preserve_y case. */
5009 charpos = IT_CHARPOS (it);
5010 bytepos = IT_BYTEPOS (it);
5012 /* We moved the window start towards BEGV, so PT may be now
5013 in the scroll margin at the bottom. */
5014 move_it_to (&it, PT, -1,
5015 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5016 - this_scroll_margin - 1),
5018 MOVE_TO_POS | MOVE_TO_Y);
5020 /* Save our position, in case it's correct. */
5021 charpos = IT_CHARPOS (it);
5022 bytepos = IT_BYTEPOS (it);
5024 /* See if point is on a partially visible line at the end. */
5025 if (it.what == IT_EOB)
5026 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5027 else
5029 move_it_by_lines (&it, 1, 1);
5030 partial_p = it.current_y > it.last_visible_y;
5033 if (charpos == PT && !partial_p
5034 && (NILP (Vscroll_preserve_screen_position)
5035 || EQ (Vscroll_preserve_screen_position, Qt)))
5036 /* We found PT before we found the display margin, so PT is ok. */
5038 else if (window_scroll_pixel_based_preserve_y >= 0)
5040 SET_TEXT_POS_FROM_MARKER (start, w->start);
5041 start_display (&it, w, start);
5042 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5043 here because we called start_display again and did not
5044 alter it.current_y this time. */
5045 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5046 window_scroll_pixel_based_preserve_y, -1,
5047 MOVE_TO_Y | MOVE_TO_X);
5048 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5050 else
5052 if (partial_p)
5053 /* The last line was only partially visible, so back up two
5054 lines to make sure we're on a fully visible line. */
5056 move_it_by_lines (&it, -2, 0);
5057 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5059 else
5060 /* No, the position we saved is OK, so use it. */
5061 SET_PT_BOTH (charpos, bytepos);
5067 /* Implementation of window_scroll that works based on screen lines.
5068 See the comment of window_scroll for parameter descriptions. */
5070 static void
5071 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
5073 register struct window *w = XWINDOW (window);
5074 register EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
5075 register EMACS_INT pos, pos_byte;
5076 register int ht = window_internal_height (w);
5077 register Lisp_Object tem;
5078 int lose;
5079 Lisp_Object bolp;
5080 EMACS_INT startpos;
5081 Lisp_Object original_pos = Qnil;
5083 /* If scrolling screen-fulls, compute the number of lines to
5084 scroll from the window's height. */
5085 if (whole)
5086 n *= max (1, ht - next_screen_context_lines);
5088 startpos = marker_position (w->start);
5090 if (!NILP (Vscroll_preserve_screen_position))
5092 if (window_scroll_preserve_vpos <= 0
5093 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5094 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5096 struct position posit
5097 = *compute_motion (startpos, 0, 0, 0,
5098 PT, ht, 0,
5099 -1, XINT (w->hscroll),
5100 0, w);
5101 window_scroll_preserve_vpos = posit.vpos;
5102 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
5105 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5106 make_number (window_scroll_preserve_vpos));
5109 XSETFASTINT (tem, PT);
5110 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5112 if (NILP (tem))
5114 Fvertical_motion (make_number (- (ht / 2)), window);
5115 startpos = PT;
5118 SET_PT (startpos);
5119 lose = n < 0 && PT == BEGV;
5120 Fvertical_motion (make_number (n), window);
5121 pos = PT;
5122 pos_byte = PT_BYTE;
5123 bolp = Fbolp ();
5124 SET_PT_BOTH (opoint, opoint_byte);
5126 if (lose)
5128 if (noerror)
5129 return;
5130 else
5131 xsignal0 (Qbeginning_of_buffer);
5134 if (pos < ZV)
5136 int this_scroll_margin = scroll_margin;
5138 /* Don't use a scroll margin that is negative or too large. */
5139 if (this_scroll_margin < 0)
5140 this_scroll_margin = 0;
5142 if (XINT (w->total_lines) < 4 * scroll_margin)
5143 this_scroll_margin = XINT (w->total_lines) / 4;
5145 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5146 w->start_at_line_beg = bolp;
5147 w->update_mode_line = Qt;
5148 XSETFASTINT (w->last_modified, 0);
5149 XSETFASTINT (w->last_overlay_modified, 0);
5150 /* Set force_start so that redisplay_window will run
5151 the window-scroll-functions. */
5152 w->force_start = Qt;
5154 if (!NILP (Vscroll_preserve_screen_position)
5155 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5157 SET_PT_BOTH (pos, pos_byte);
5158 Fvertical_motion (original_pos, window);
5160 /* If we scrolled forward, put point enough lines down
5161 that it is outside the scroll margin. */
5162 else if (n > 0)
5164 int top_margin;
5166 if (this_scroll_margin > 0)
5168 SET_PT_BOTH (pos, pos_byte);
5169 Fvertical_motion (make_number (this_scroll_margin), window);
5170 top_margin = PT;
5172 else
5173 top_margin = pos;
5175 if (top_margin <= opoint)
5176 SET_PT_BOTH (opoint, opoint_byte);
5177 else if (!NILP (Vscroll_preserve_screen_position))
5179 SET_PT_BOTH (pos, pos_byte);
5180 Fvertical_motion (original_pos, window);
5182 else
5183 SET_PT (top_margin);
5185 else if (n < 0)
5187 int bottom_margin;
5189 /* If we scrolled backward, put point near the end of the window
5190 but not within the scroll margin. */
5191 SET_PT_BOTH (pos, pos_byte);
5192 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5193 if (XFASTINT (tem) == ht - this_scroll_margin)
5194 bottom_margin = PT;
5195 else
5196 bottom_margin = PT + 1;
5198 if (bottom_margin > opoint)
5199 SET_PT_BOTH (opoint, opoint_byte);
5200 else
5202 if (!NILP (Vscroll_preserve_screen_position))
5204 SET_PT_BOTH (pos, pos_byte);
5205 Fvertical_motion (original_pos, window);
5207 else
5208 Fvertical_motion (make_number (-1), window);
5212 else
5214 if (noerror)
5215 return;
5216 else
5217 xsignal0 (Qend_of_buffer);
5222 /* Scroll selected_window up or down. If N is nil, scroll a
5223 screen-full which is defined as the height of the window minus
5224 next_screen_context_lines. If N is the symbol `-', scroll.
5225 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5226 up. This is the guts of Fscroll_up and Fscroll_down. */
5228 static void
5229 scroll_command (Lisp_Object n, int direction)
5231 int count = SPECPDL_INDEX ();
5233 xassert (eabs (direction) == 1);
5235 /* If selected window's buffer isn't current, make it current for
5236 the moment. But don't screw up if window_scroll gets an error. */
5237 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5239 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5240 Fset_buffer (XWINDOW (selected_window)->buffer);
5242 /* Make redisplay consider other windows than just selected_window. */
5243 ++windows_or_buffers_changed;
5246 if (NILP (n))
5247 window_scroll (selected_window, direction, 1, 0);
5248 else if (EQ (n, Qminus))
5249 window_scroll (selected_window, -direction, 1, 0);
5250 else
5252 n = Fprefix_numeric_value (n);
5253 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5256 unbind_to (count, Qnil);
5259 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5260 doc: /* Scroll text of selected window upward ARG lines.
5261 If ARG is omitted or nil, scroll upward by a near full screen.
5262 A near full screen is `next-screen-context-lines' less than a full screen.
5263 Negative ARG means scroll downward.
5264 If ARG is the atom `-', scroll downward by nearly full screen.
5265 When calling from a program, supply as argument a number, nil, or `-'. */)
5266 (Lisp_Object arg)
5268 scroll_command (arg, 1);
5269 return Qnil;
5272 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5273 doc: /* Scroll text of selected window down ARG lines.
5274 If ARG is omitted or nil, scroll down by a near full screen.
5275 A near full screen is `next-screen-context-lines' less than a full screen.
5276 Negative ARG means scroll upward.
5277 If ARG is the atom `-', scroll upward by nearly full screen.
5278 When calling from a program, supply as argument a number, nil, or `-'. */)
5279 (Lisp_Object arg)
5281 scroll_command (arg, -1);
5282 return Qnil;
5285 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5286 doc: /* Return the other window for \"other window scroll\" commands.
5287 If `other-window-scroll-buffer' is non-nil, a window
5288 showing that buffer is used.
5289 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5290 specifies the window. This takes precedence over
5291 `other-window-scroll-buffer'. */)
5292 (void)
5294 Lisp_Object window;
5296 if (MINI_WINDOW_P (XWINDOW (selected_window))
5297 && !NILP (Vminibuf_scroll_window))
5298 window = Vminibuf_scroll_window;
5299 /* If buffer is specified, scroll that buffer. */
5300 else if (!NILP (Vother_window_scroll_buffer))
5302 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5303 if (NILP (window))
5304 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5306 else
5308 /* Nothing specified; look for a neighboring window on the same
5309 frame. */
5310 window = Fnext_window (selected_window, Qnil, Qnil);
5312 if (EQ (window, selected_window))
5313 /* That didn't get us anywhere; look for a window on another
5314 visible frame. */
5316 window = Fnext_window (window, Qnil, Qt);
5317 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5318 && ! EQ (window, selected_window));
5321 CHECK_LIVE_WINDOW (window);
5323 if (EQ (window, selected_window))
5324 error ("There is no other window");
5326 return window;
5329 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5330 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5331 A near full screen is `next-screen-context-lines' less than a full screen.
5332 The next window is the one below the current one; or the one at the top
5333 if the current one is at the bottom. Negative ARG means scroll downward.
5334 If ARG is the atom `-', scroll downward by nearly full screen.
5335 When calling from a program, supply as argument a number, nil, or `-'.
5337 If `other-window-scroll-buffer' is non-nil, scroll the window
5338 showing that buffer, popping the buffer up if necessary.
5339 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5340 specifies the window to scroll. This takes precedence over
5341 `other-window-scroll-buffer'. */)
5342 (Lisp_Object arg)
5344 Lisp_Object window;
5345 struct window *w;
5346 int count = SPECPDL_INDEX ();
5348 window = Fother_window_for_scrolling ();
5349 w = XWINDOW (window);
5351 /* Don't screw up if window_scroll gets an error. */
5352 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5353 ++windows_or_buffers_changed;
5355 Fset_buffer (w->buffer);
5356 SET_PT (marker_position (w->pointm));
5358 if (NILP (arg))
5359 window_scroll (window, 1, 1, 1);
5360 else if (EQ (arg, Qminus))
5361 window_scroll (window, -1, 1, 1);
5362 else
5364 if (CONSP (arg))
5365 arg = Fcar (arg);
5366 CHECK_NUMBER (arg);
5367 window_scroll (window, XINT (arg), 0, 1);
5370 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5371 unbind_to (count, Qnil);
5373 return Qnil;
5376 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5377 doc: /* Scroll selected window display ARG columns left.
5378 Default for ARG is window width minus 2.
5379 Value is the total amount of leftward horizontal scrolling in
5380 effect after the change.
5381 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5382 lower bound for automatic scrolling, i.e. automatic scrolling
5383 will not scroll a window to a column less than the value returned
5384 by this function. This happens in an interactive call. */)
5385 (register Lisp_Object arg, Lisp_Object set_minimum)
5387 Lisp_Object result;
5388 int hscroll;
5389 struct window *w = XWINDOW (selected_window);
5391 if (NILP (arg))
5392 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5393 else
5394 arg = Fprefix_numeric_value (arg);
5396 hscroll = XINT (w->hscroll) + XINT (arg);
5397 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5399 if (!NILP (set_minimum))
5400 w->min_hscroll = w->hscroll;
5402 return result;
5405 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5406 doc: /* Scroll selected window display ARG columns right.
5407 Default for ARG is window width minus 2.
5408 Value is the total amount of leftward horizontal scrolling in
5409 effect after the change.
5410 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5411 lower bound for automatic scrolling, i.e. automatic scrolling
5412 will not scroll a window to a column less than the value returned
5413 by this function. This happens in an interactive call. */)
5414 (register Lisp_Object arg, Lisp_Object set_minimum)
5416 Lisp_Object result;
5417 int hscroll;
5418 struct window *w = XWINDOW (selected_window);
5420 if (NILP (arg))
5421 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5422 else
5423 arg = Fprefix_numeric_value (arg);
5425 hscroll = XINT (w->hscroll) - XINT (arg);
5426 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5428 if (!NILP (set_minimum))
5429 w->min_hscroll = w->hscroll;
5431 return result;
5434 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5435 doc: /* Return the window which was selected when entering the minibuffer.
5436 Returns nil, if selected window is not a minibuffer window. */)
5437 (void)
5439 if (minibuf_level > 0
5440 && MINI_WINDOW_P (XWINDOW (selected_window))
5441 && WINDOW_LIVE_P (minibuf_selected_window))
5442 return minibuf_selected_window;
5444 return Qnil;
5447 /* Value is the number of lines actually displayed in window W,
5448 as opposed to its height. */
5450 static int
5451 displayed_window_lines (struct window *w)
5453 struct it it;
5454 struct text_pos start;
5455 int height = window_box_height (w);
5456 struct buffer *old_buffer;
5457 int bottom_y;
5459 if (XBUFFER (w->buffer) != current_buffer)
5461 old_buffer = current_buffer;
5462 set_buffer_internal (XBUFFER (w->buffer));
5464 else
5465 old_buffer = NULL;
5467 /* In case W->start is out of the accessible range, do something
5468 reasonable. This happens in Info mode when Info-scroll-down
5469 calls (recenter -1) while W->start is 1. */
5470 if (XMARKER (w->start)->charpos < BEGV)
5471 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5472 else if (XMARKER (w->start)->charpos > ZV)
5473 SET_TEXT_POS (start, ZV, ZV_BYTE);
5474 else
5475 SET_TEXT_POS_FROM_MARKER (start, w->start);
5477 start_display (&it, w, start);
5478 move_it_vertically (&it, height);
5479 bottom_y = line_bottom_y (&it);
5481 /* rms: On a non-window display,
5482 the value of it.vpos at the bottom of the screen
5483 seems to be 1 larger than window_box_height (w).
5484 This kludge fixes a bug whereby (move-to-window-line -1)
5485 when ZV is on the last screen line
5486 moves to the previous screen line instead of the last one. */
5487 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5488 height++;
5490 /* Add in empty lines at the bottom of the window. */
5491 if (bottom_y < height)
5493 int uy = FRAME_LINE_HEIGHT (it.f);
5494 it.vpos += (height - bottom_y + uy - 1) / uy;
5497 if (old_buffer)
5498 set_buffer_internal (old_buffer);
5500 return it.vpos;
5504 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5505 doc: /* Center point in selected window and maybe redisplay frame.
5506 With prefix argument ARG, recenter putting point on screen line ARG
5507 relative to the selected window. If ARG is negative, it counts up from the
5508 bottom of the window. (ARG should be less than the height of the window.)
5510 If ARG is omitted or nil, then recenter with point on the middle line of
5511 the selected window; if the variable `recenter-redisplay' is non-nil,
5512 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5513 is set to `grow-only', this resets the tool-bar's height to the minimum
5514 height needed); if `recenter-redisplay' has the special value `tty',
5515 then only tty frame are redrawn.
5517 Just C-u as prefix means put point in the center of the window
5518 and redisplay normally--don't erase and redraw the frame. */)
5519 (register Lisp_Object arg)
5521 struct window *w = XWINDOW (selected_window);
5522 struct buffer *buf = XBUFFER (w->buffer);
5523 struct buffer *obuf = current_buffer;
5524 int center_p = 0;
5525 EMACS_INT charpos, bytepos;
5526 int iarg IF_LINT (= 0);
5527 int this_scroll_margin;
5529 /* If redisplay is suppressed due to an error, try again. */
5530 obuf->display_error_modiff = 0;
5532 if (NILP (arg))
5534 if (!NILP (Vrecenter_redisplay)
5535 && (!EQ (Vrecenter_redisplay, Qtty)
5536 || !NILP (Ftty_type (selected_frame))))
5538 int i;
5540 /* Invalidate pixel data calculated for all compositions. */
5541 for (i = 0; i < n_compositions; i++)
5542 composition_table[i]->font = NULL;
5544 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5546 Fredraw_frame (WINDOW_FRAME (w));
5547 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5550 center_p = 1;
5552 else if (CONSP (arg)) /* Just C-u. */
5553 center_p = 1;
5554 else
5556 arg = Fprefix_numeric_value (arg);
5557 CHECK_NUMBER (arg);
5558 iarg = XINT (arg);
5561 set_buffer_internal (buf);
5563 /* Do this after making BUF current
5564 in case scroll_margin is buffer-local. */
5565 this_scroll_margin = max (0, scroll_margin);
5566 this_scroll_margin = min (this_scroll_margin,
5567 XFASTINT (w->total_lines) / 4);
5569 /* Handle centering on a graphical frame specially. Such frames can
5570 have variable-height lines and centering point on the basis of
5571 line counts would lead to strange effects. */
5572 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5574 if (center_p)
5576 struct it it;
5577 struct text_pos pt;
5579 SET_TEXT_POS (pt, PT, PT_BYTE);
5580 start_display (&it, w, pt);
5581 move_it_vertically_backward (&it, window_box_height (w) / 2);
5582 charpos = IT_CHARPOS (it);
5583 bytepos = IT_BYTEPOS (it);
5585 else if (iarg < 0)
5587 struct it it;
5588 struct text_pos pt;
5589 int nlines = -iarg;
5590 int extra_line_spacing;
5591 int h = window_box_height (w);
5593 iarg = - max (-iarg, this_scroll_margin);
5595 SET_TEXT_POS (pt, PT, PT_BYTE);
5596 start_display (&it, w, pt);
5598 /* Be sure we have the exact height of the full line containing PT. */
5599 move_it_by_lines (&it, 0, 1);
5601 /* The amount of pixels we have to move back is the window
5602 height minus what's displayed in the line containing PT,
5603 and the lines below. */
5604 it.current_y = 0;
5605 it.vpos = 0;
5606 move_it_by_lines (&it, nlines, 1);
5608 if (it.vpos == nlines)
5609 h -= it.current_y;
5610 else
5612 /* Last line has no newline */
5613 h -= line_bottom_y (&it);
5614 it.vpos++;
5617 /* Don't reserve space for extra line spacing of last line. */
5618 extra_line_spacing = it.max_extra_line_spacing;
5620 /* If we can't move down NLINES lines because we hit
5621 the end of the buffer, count in some empty lines. */
5622 if (it.vpos < nlines)
5624 nlines -= it.vpos;
5625 extra_line_spacing = it.extra_line_spacing;
5626 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5628 if (h <= 0)
5629 return Qnil;
5631 /* Now find the new top line (starting position) of the window. */
5632 start_display (&it, w, pt);
5633 it.current_y = 0;
5634 move_it_vertically_backward (&it, h);
5636 /* If extra line spacing is present, we may move too far
5637 back. This causes the last line to be only partially
5638 visible (which triggers redisplay to recenter that line
5639 in the middle), so move forward.
5640 But ignore extra line spacing on last line, as it is not
5641 considered to be part of the visible height of the line.
5643 h += extra_line_spacing;
5644 while (-it.current_y > h)
5645 move_it_by_lines (&it, 1, 1);
5647 charpos = IT_CHARPOS (it);
5648 bytepos = IT_BYTEPOS (it);
5650 else
5652 struct position pos;
5654 iarg = max (iarg, this_scroll_margin);
5656 pos = *vmotion (PT, -iarg, w);
5657 charpos = pos.bufpos;
5658 bytepos = pos.bytepos;
5661 else
5663 struct position pos;
5664 int ht = window_internal_height (w);
5666 if (center_p)
5667 iarg = ht / 2;
5668 else if (iarg < 0)
5669 iarg += ht;
5671 /* Don't let it get into the margin at either top or bottom. */
5672 iarg = max (iarg, this_scroll_margin);
5673 iarg = min (iarg, ht - this_scroll_margin - 1);
5675 pos = *vmotion (PT, - iarg, w);
5676 charpos = pos.bufpos;
5677 bytepos = pos.bytepos;
5680 /* Set the new window start. */
5681 set_marker_both (w->start, w->buffer, charpos, bytepos);
5682 w->window_end_valid = Qnil;
5684 w->optional_new_start = Qt;
5686 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5687 w->start_at_line_beg = Qt;
5688 else
5689 w->start_at_line_beg = Qnil;
5691 set_buffer_internal (obuf);
5692 return Qnil;
5696 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5697 0, 1, 0,
5698 doc: /* Return the height in lines of the text display area of WINDOW.
5699 WINDOW defaults to the selected window.
5701 The return value does not include the mode line, any header line, nor
5702 any partial-height lines in the text display area. */)
5703 (Lisp_Object window)
5705 struct window *w = decode_window (window);
5706 int pixel_height = window_box_height (w);
5707 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5708 return make_number (line_height);
5713 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5714 1, 1, "P",
5715 doc: /* Position point relative to window.
5716 With no argument, position point at center of window.
5717 An argument specifies vertical position within the window;
5718 zero means top of window, negative means relative to bottom of window. */)
5719 (Lisp_Object arg)
5721 struct window *w = XWINDOW (selected_window);
5722 int lines, start;
5723 Lisp_Object window;
5724 #if 0
5725 int this_scroll_margin;
5726 #endif
5728 if (!(BUFFERP (w->buffer)
5729 && XBUFFER (w->buffer) == current_buffer))
5730 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5731 when passed below to set_marker_both. */
5732 error ("move-to-window-line called from unrelated buffer");
5734 window = selected_window;
5735 start = marker_position (w->start);
5736 if (start < BEGV || start > ZV)
5738 int height = window_internal_height (w);
5739 Fvertical_motion (make_number (- (height / 2)), window);
5740 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5741 w->start_at_line_beg = Fbolp ();
5742 w->force_start = Qt;
5744 else
5745 Fgoto_char (w->start);
5747 lines = displayed_window_lines (w);
5749 #if 0
5750 this_scroll_margin = max (0, scroll_margin);
5751 this_scroll_margin = min (this_scroll_margin, lines / 4);
5752 #endif
5754 if (NILP (arg))
5755 XSETFASTINT (arg, lines / 2);
5756 else
5758 int iarg = XINT (Fprefix_numeric_value (arg));
5760 if (iarg < 0)
5761 iarg = iarg + lines;
5763 #if 0 /* This code would prevent move-to-window-line from moving point
5764 to a place inside the scroll margins (which would cause the
5765 next redisplay to scroll). I wrote this code, but then concluded
5766 it is probably better not to install it. However, it is here
5767 inside #if 0 so as not to lose it. -- rms. */
5769 /* Don't let it get into the margin at either top or bottom. */
5770 iarg = max (iarg, this_scroll_margin);
5771 iarg = min (iarg, lines - this_scroll_margin - 1);
5772 #endif
5774 arg = make_number (iarg);
5777 /* Skip past a partially visible first line. */
5778 if (w->vscroll)
5779 XSETINT (arg, XINT (arg) + 1);
5781 return Fvertical_motion (arg, window);
5786 /***********************************************************************
5787 Window Configuration
5788 ***********************************************************************/
5790 struct save_window_data
5792 EMACS_UINT size;
5793 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5794 Lisp_Object selected_frame;
5795 Lisp_Object current_window;
5796 Lisp_Object current_buffer;
5797 Lisp_Object minibuf_scroll_window;
5798 Lisp_Object minibuf_selected_window;
5799 Lisp_Object root_window;
5800 Lisp_Object focus_frame;
5801 /* A vector, each of whose elements is a struct saved_window
5802 for one window. */
5803 Lisp_Object saved_windows;
5805 /* All fields above are traced by the GC.
5806 From `fame-cols' down, the fields are ignored by the GC. */
5808 int frame_cols, frame_lines, frame_menu_bar_lines;
5809 int frame_tool_bar_lines;
5812 /* This is saved as a Lisp_Vector */
5813 struct saved_window
5815 /* these first two must agree with struct Lisp_Vector in lisp.h */
5816 EMACS_UINT size;
5817 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5819 Lisp_Object window;
5820 Lisp_Object buffer, start, pointm, mark;
5821 Lisp_Object left_col, top_line, total_cols, total_lines;
5822 Lisp_Object hscroll, min_hscroll;
5823 Lisp_Object parent, prev;
5824 Lisp_Object start_at_line_beg;
5825 Lisp_Object display_table;
5826 Lisp_Object orig_top_line, orig_total_lines;
5827 Lisp_Object left_margin_cols, right_margin_cols;
5828 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5829 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5830 Lisp_Object dedicated, resize_proportionally;
5833 #define SAVED_WINDOW_N(swv,n) \
5834 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5836 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5837 doc: /* Return t if OBJECT is a window-configuration object. */)
5838 (Lisp_Object object)
5840 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5843 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5844 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5845 (Lisp_Object config)
5847 register struct save_window_data *data;
5848 struct Lisp_Vector *saved_windows;
5850 CHECK_WINDOW_CONFIGURATION (config);
5852 data = (struct save_window_data *) XVECTOR (config);
5853 saved_windows = XVECTOR (data->saved_windows);
5854 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5857 DEFUN ("set-window-configuration", Fset_window_configuration,
5858 Sset_window_configuration, 1, 1, 0,
5859 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5860 CONFIGURATION must be a value previously returned
5861 by `current-window-configuration' (which see).
5862 If CONFIGURATION was made from a frame that is now deleted,
5863 only frame-independent values can be restored. In this case,
5864 the return value is nil. Otherwise the value is t. */)
5865 (Lisp_Object configuration)
5867 register struct save_window_data *data;
5868 struct Lisp_Vector *saved_windows;
5869 Lisp_Object new_current_buffer;
5870 Lisp_Object frame;
5871 FRAME_PTR f;
5872 EMACS_INT old_point = -1;
5874 CHECK_WINDOW_CONFIGURATION (configuration);
5876 data = (struct save_window_data *) XVECTOR (configuration);
5877 saved_windows = XVECTOR (data->saved_windows);
5879 new_current_buffer = data->current_buffer;
5880 if (NILP (BVAR (XBUFFER (new_current_buffer), name)))
5881 new_current_buffer = Qnil;
5882 else
5884 if (XBUFFER (new_current_buffer) == current_buffer)
5885 /* The code further down "preserves point" by saving here PT in
5886 old_point and then setting it later back into PT. When the
5887 current-selected-window and the final-selected-window both show
5888 the current buffer, this suffers from the problem that the
5889 current PT is the window-point of the current-selected-window,
5890 while the final PT is the point of the final-selected-window, so
5891 this copy from one PT to the other would end up moving the
5892 window-point of the final-selected-window to the window-point of
5893 the current-selected-window. So we have to be careful which
5894 point of the current-buffer we copy into old_point. */
5895 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5896 && WINDOWP (selected_window)
5897 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5898 && !EQ (selected_window, data->current_window))
5899 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5900 else
5901 old_point = PT;
5902 else
5903 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5904 point in new_current_buffer as of the last time this buffer was
5905 used. This can be non-deterministic since it can be changed by
5906 things like jit-lock by mere temporary selection of some random
5907 window that happens to show this buffer.
5908 So if possible we want this arbitrary choice of "which point" to
5909 be the one from the to-be-selected-window so as to prevent this
5910 window's cursor from being copied from another window. */
5911 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5912 /* If current_window = selected_window, its point is in BUF_PT. */
5913 && !EQ (selected_window, data->current_window))
5914 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5915 else
5916 old_point = BUF_PT (XBUFFER (new_current_buffer));
5919 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5920 f = XFRAME (frame);
5922 /* If f is a dead frame, don't bother rebuilding its window tree.
5923 However, there is other stuff we should still try to do below. */
5924 if (FRAME_LIVE_P (f))
5926 register struct window *w;
5927 register struct saved_window *p;
5928 struct window *root_window;
5929 struct window **leaf_windows;
5930 int n_leaf_windows;
5931 int k, i, n;
5933 /* If the frame has been resized since this window configuration was
5934 made, we change the frame to the size specified in the
5935 configuration, restore the configuration, and then resize it
5936 back. We keep track of the prevailing height in these variables. */
5937 int previous_frame_lines = FRAME_LINES (f);
5938 int previous_frame_cols = FRAME_COLS (f);
5939 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5940 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5942 /* The mouse highlighting code could get screwed up
5943 if it runs during this. */
5944 BLOCK_INPUT;
5946 if (data->frame_lines != previous_frame_lines
5947 || data->frame_cols != previous_frame_cols)
5948 change_frame_size (f, data->frame_lines,
5949 data->frame_cols, 0, 0, 0);
5950 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5951 if (data->frame_menu_bar_lines
5952 != previous_frame_menu_bar_lines)
5953 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5954 make_number (0));
5955 #ifdef HAVE_WINDOW_SYSTEM
5956 if (data->frame_tool_bar_lines
5957 != previous_frame_tool_bar_lines)
5958 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5959 make_number (0));
5960 #endif
5961 #endif
5963 /* "Swap out" point from the selected window's buffer
5964 into the window itself. (Normally the pointm of the selected
5965 window holds garbage.) We do this now, before
5966 restoring the window contents, and prevent it from
5967 being done later on when we select a new window. */
5968 if (! NILP (XWINDOW (selected_window)->buffer))
5970 w = XWINDOW (selected_window);
5971 set_marker_both (w->pointm,
5972 w->buffer,
5973 BUF_PT (XBUFFER (w->buffer)),
5974 BUF_PT_BYTE (XBUFFER (w->buffer)));
5977 windows_or_buffers_changed++;
5978 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5980 /* Problem: Freeing all matrices and later allocating them again
5981 is a serious redisplay flickering problem. What we would
5982 really like to do is to free only those matrices not reused
5983 below. */
5984 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5985 leaf_windows
5986 = (struct window **) alloca (count_windows (root_window)
5987 * sizeof (struct window *));
5988 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5990 /* Kludge Alert!
5991 Mark all windows now on frame as "deleted".
5992 Restoring the new configuration "undeletes" any that are in it.
5994 Save their current buffers in their height fields, since we may
5995 need it later, if a buffer saved in the configuration is now
5996 dead. */
5997 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5999 for (k = 0; k < saved_windows->size; k++)
6001 p = SAVED_WINDOW_N (saved_windows, k);
6002 w = XWINDOW (p->window);
6003 w->next = Qnil;
6005 if (!NILP (p->parent))
6006 w->parent = SAVED_WINDOW_N (saved_windows,
6007 XFASTINT (p->parent))->window;
6008 else
6009 w->parent = Qnil;
6011 if (!NILP (p->prev))
6013 w->prev = SAVED_WINDOW_N (saved_windows,
6014 XFASTINT (p->prev))->window;
6015 XWINDOW (w->prev)->next = p->window;
6017 else
6019 w->prev = Qnil;
6020 if (!NILP (w->parent))
6022 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6024 XWINDOW (w->parent)->vchild = p->window;
6025 XWINDOW (w->parent)->hchild = Qnil;
6027 else
6029 XWINDOW (w->parent)->hchild = p->window;
6030 XWINDOW (w->parent)->vchild = Qnil;
6035 /* If we squirreled away the buffer in the window's height,
6036 restore it now. */
6037 if (BUFFERP (w->total_lines))
6038 w->buffer = w->total_lines;
6039 w->left_col = p->left_col;
6040 w->top_line = p->top_line;
6041 w->total_cols = p->total_cols;
6042 w->total_lines = p->total_lines;
6043 w->hscroll = p->hscroll;
6044 w->min_hscroll = p->min_hscroll;
6045 w->display_table = p->display_table;
6046 w->orig_top_line = p->orig_top_line;
6047 w->orig_total_lines = p->orig_total_lines;
6048 w->left_margin_cols = p->left_margin_cols;
6049 w->right_margin_cols = p->right_margin_cols;
6050 w->left_fringe_width = p->left_fringe_width;
6051 w->right_fringe_width = p->right_fringe_width;
6052 w->fringes_outside_margins = p->fringes_outside_margins;
6053 w->scroll_bar_width = p->scroll_bar_width;
6054 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6055 w->dedicated = p->dedicated;
6056 w->resize_proportionally = p->resize_proportionally;
6057 XSETFASTINT (w->last_modified, 0);
6058 XSETFASTINT (w->last_overlay_modified, 0);
6060 /* Reinstall the saved buffer and pointers into it. */
6061 if (NILP (p->buffer))
6062 w->buffer = p->buffer;
6063 else
6065 if (!NILP (BVAR (XBUFFER (p->buffer), name)))
6066 /* If saved buffer is alive, install it. */
6068 w->buffer = p->buffer;
6069 w->start_at_line_beg = p->start_at_line_beg;
6070 set_marker_restricted (w->start, p->start, w->buffer);
6071 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6072 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
6073 p->mark, w->buffer);
6075 /* As documented in Fcurrent_window_configuration, don't
6076 restore the location of point in the buffer which was
6077 current when the window configuration was recorded. */
6078 if (!EQ (p->buffer, new_current_buffer)
6079 && XBUFFER (p->buffer) == current_buffer)
6080 Fgoto_char (w->pointm);
6082 else if (NILP (w->buffer) || NILP (BVAR (XBUFFER (w->buffer), name)))
6083 /* Else unless window has a live buffer, get one. */
6085 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6086 /* This will set the markers to beginning of visible
6087 range. */
6088 set_marker_restricted (w->start, make_number (0), w->buffer);
6089 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6090 w->start_at_line_beg = Qt;
6092 else
6093 /* Keeping window's old buffer; make sure the markers
6094 are real. */
6096 /* Set window markers at start of visible range. */
6097 if (XMARKER (w->start)->buffer == 0)
6098 set_marker_restricted (w->start, make_number (0),
6099 w->buffer);
6100 if (XMARKER (w->pointm)->buffer == 0)
6101 set_marker_restricted_both (w->pointm, w->buffer,
6102 BUF_PT (XBUFFER (w->buffer)),
6103 BUF_PT_BYTE (XBUFFER (w->buffer)));
6104 w->start_at_line_beg = Qt;
6109 FRAME_ROOT_WINDOW (f) = data->root_window;
6111 /* Arrange *not* to restore point in the buffer that was
6112 current when the window configuration was saved. */
6113 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6114 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6115 make_number (old_point),
6116 XWINDOW (data->current_window)->buffer);
6118 /* In the following call to `select-window, prevent "swapping
6119 out point" in the old selected window using the buffer that
6120 has been restored into it. We already swapped out that point
6121 from that window's old buffer. */
6122 select_window (data->current_window, Qnil, 1);
6123 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
6124 = selected_window;
6126 if (NILP (data->focus_frame)
6127 || (FRAMEP (data->focus_frame)
6128 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6129 Fredirect_frame_focus (frame, data->focus_frame);
6131 /* Set the screen height to the value it had before this function. */
6132 if (previous_frame_lines != FRAME_LINES (f)
6133 || previous_frame_cols != FRAME_COLS (f))
6134 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6135 0, 0, 0);
6136 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6137 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6138 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6139 make_number (0));
6140 #ifdef HAVE_WINDOW_SYSTEM
6141 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6142 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6143 make_number (0));
6144 #endif
6145 #endif
6147 /* Now, free glyph matrices in windows that were not reused. */
6148 for (i = n = 0; i < n_leaf_windows; ++i)
6150 if (NILP (leaf_windows[i]->buffer))
6152 /* Assert it's not reused as a combination. */
6153 xassert (NILP (leaf_windows[i]->hchild)
6154 && NILP (leaf_windows[i]->vchild));
6155 free_window_matrices (leaf_windows[i]);
6157 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6158 ++n;
6161 adjust_glyphs (f);
6163 UNBLOCK_INPUT;
6165 /* Fselect_window will have made f the selected frame, so we
6166 reselect the proper frame here. Fhandle_switch_frame will change the
6167 selected window too, but that doesn't make the call to
6168 Fselect_window above totally superfluous; it still sets f's
6169 selected window. */
6170 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6171 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6173 run_window_configuration_change_hook (f);
6176 if (!NILP (new_current_buffer))
6177 Fset_buffer (new_current_buffer);
6179 Vminibuf_scroll_window = data->minibuf_scroll_window;
6180 minibuf_selected_window = data->minibuf_selected_window;
6182 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6185 /* Mark all windows now on frame as deleted
6186 by setting their buffers to nil. */
6188 void
6189 delete_all_subwindows (register struct window *w)
6191 if (!NILP (w->next))
6192 delete_all_subwindows (XWINDOW (w->next));
6193 if (!NILP (w->vchild))
6194 delete_all_subwindows (XWINDOW (w->vchild));
6195 if (!NILP (w->hchild))
6196 delete_all_subwindows (XWINDOW (w->hchild));
6198 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6200 if (!NILP (w->buffer))
6201 unshow_buffer (w);
6203 /* We set all three of these fields to nil, to make sure that we can
6204 distinguish this dead window from any live window. Live leaf
6205 windows will have buffer set, and combination windows will have
6206 vchild or hchild set. */
6207 w->buffer = Qnil;
6208 w->vchild = Qnil;
6209 w->hchild = Qnil;
6211 Vwindow_list = Qnil;
6214 static int
6215 count_windows (register struct window *window)
6217 register int count = 1;
6218 if (!NILP (window->next))
6219 count += count_windows (XWINDOW (window->next));
6220 if (!NILP (window->vchild))
6221 count += count_windows (XWINDOW (window->vchild));
6222 if (!NILP (window->hchild))
6223 count += count_windows (XWINDOW (window->hchild));
6224 return count;
6228 /* Fill vector FLAT with leaf windows under W, starting at index I.
6229 Value is last index + 1. */
6231 static int
6232 get_leaf_windows (struct window *w, struct window **flat, int i)
6234 while (w)
6236 if (!NILP (w->hchild))
6237 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6238 else if (!NILP (w->vchild))
6239 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6240 else
6241 flat[i++] = w;
6243 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6246 return i;
6250 /* Return a pointer to the glyph W's physical cursor is on. Value is
6251 null if W's current matrix is invalid, so that no meaningfull glyph
6252 can be returned. */
6254 struct glyph *
6255 get_phys_cursor_glyph (struct window *w)
6257 struct glyph_row *row;
6258 struct glyph *glyph;
6260 if (w->phys_cursor.vpos >= 0
6261 && w->phys_cursor.vpos < w->current_matrix->nrows
6262 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6263 row->enabled_p)
6264 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6265 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6266 else
6267 glyph = NULL;
6269 return glyph;
6273 static int
6274 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6276 register struct saved_window *p;
6277 register struct window *w;
6278 register Lisp_Object tem;
6280 for (;!NILP (window); window = w->next)
6282 p = SAVED_WINDOW_N (vector, i);
6283 w = XWINDOW (window);
6285 XSETFASTINT (w->temslot, i); i++;
6286 p->window = window;
6287 p->buffer = w->buffer;
6288 p->left_col = w->left_col;
6289 p->top_line = w->top_line;
6290 p->total_cols = w->total_cols;
6291 p->total_lines = w->total_lines;
6292 p->hscroll = w->hscroll;
6293 p->min_hscroll = w->min_hscroll;
6294 p->display_table = w->display_table;
6295 p->orig_top_line = w->orig_top_line;
6296 p->orig_total_lines = w->orig_total_lines;
6297 p->left_margin_cols = w->left_margin_cols;
6298 p->right_margin_cols = w->right_margin_cols;
6299 p->left_fringe_width = w->left_fringe_width;
6300 p->right_fringe_width = w->right_fringe_width;
6301 p->fringes_outside_margins = w->fringes_outside_margins;
6302 p->scroll_bar_width = w->scroll_bar_width;
6303 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6304 p->dedicated = w->dedicated;
6305 p->resize_proportionally = w->resize_proportionally;
6306 if (!NILP (w->buffer))
6308 /* Save w's value of point in the window configuration.
6309 If w is the selected window, then get the value of point
6310 from the buffer; pointm is garbage in the selected window. */
6311 if (EQ (window, selected_window))
6313 p->pointm = Fmake_marker ();
6314 set_marker_both (p->pointm, w->buffer,
6315 BUF_PT (XBUFFER (w->buffer)),
6316 BUF_PT_BYTE (XBUFFER (w->buffer)));
6318 else
6319 p->pointm = Fcopy_marker (w->pointm, Qnil);
6321 p->start = Fcopy_marker (w->start, Qnil);
6322 p->start_at_line_beg = w->start_at_line_beg;
6324 tem = BVAR (XBUFFER (w->buffer), mark);
6325 p->mark = Fcopy_marker (tem, Qnil);
6327 else
6329 p->pointm = Qnil;
6330 p->start = Qnil;
6331 p->mark = Qnil;
6332 p->start_at_line_beg = Qnil;
6335 if (NILP (w->parent))
6336 p->parent = Qnil;
6337 else
6338 p->parent = XWINDOW (w->parent)->temslot;
6340 if (NILP (w->prev))
6341 p->prev = Qnil;
6342 else
6343 p->prev = XWINDOW (w->prev)->temslot;
6345 if (!NILP (w->vchild))
6346 i = save_window_save (w->vchild, vector, i);
6347 if (!NILP (w->hchild))
6348 i = save_window_save (w->hchild, vector, i);
6351 return i;
6354 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6355 Scurrent_window_configuration, 0, 1, 0,
6356 doc: /* Return an object representing the current window configuration of FRAME.
6357 If FRAME is nil or omitted, use the selected frame.
6358 This describes the number of windows, their sizes and current buffers,
6359 and for each displayed buffer, where display starts, and the positions of
6360 point and mark. An exception is made for point in the current buffer:
6361 its value is -not- saved.
6362 This also records the currently selected frame, and FRAME's focus
6363 redirection (see `redirect-frame-focus'). */)
6364 (Lisp_Object frame)
6366 register Lisp_Object tem;
6367 register int n_windows;
6368 register struct save_window_data *data;
6369 register int i;
6370 FRAME_PTR f;
6372 if (NILP (frame))
6373 frame = selected_frame;
6374 CHECK_LIVE_FRAME (frame);
6375 f = XFRAME (frame);
6377 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6378 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6379 PVEC_WINDOW_CONFIGURATION);
6381 data->frame_cols = FRAME_COLS (f);
6382 data->frame_lines = FRAME_LINES (f);
6383 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6384 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6385 data->selected_frame = selected_frame;
6386 data->current_window = FRAME_SELECTED_WINDOW (f);
6387 XSETBUFFER (data->current_buffer, current_buffer);
6388 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6389 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6390 data->root_window = FRAME_ROOT_WINDOW (f);
6391 data->focus_frame = FRAME_FOCUS_FRAME (f);
6392 tem = Fmake_vector (make_number (n_windows), Qnil);
6393 data->saved_windows = tem;
6394 for (i = 0; i < n_windows; i++)
6395 XVECTOR (tem)->contents[i]
6396 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6397 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6398 XSETWINDOW_CONFIGURATION (tem, data);
6399 return (tem);
6403 /***********************************************************************
6404 Window Split Tree
6405 ***********************************************************************/
6407 static Lisp_Object
6408 window_tree (struct window *w)
6410 Lisp_Object tail = Qnil;
6411 Lisp_Object result = Qnil;
6413 while (w)
6415 Lisp_Object wn;
6417 XSETWINDOW (wn, w);
6418 if (!NILP (w->hchild))
6419 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6420 window_tree (XWINDOW (w->hchild))));
6421 else if (!NILP (w->vchild))
6422 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6423 window_tree (XWINDOW (w->vchild))));
6425 if (NILP (result))
6427 result = tail = Fcons (wn, Qnil);
6429 else
6431 XSETCDR (tail, Fcons (wn, Qnil));
6432 tail = XCDR (tail);
6435 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6438 return result;
6443 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6444 0, 1, 0,
6445 doc: /* Return the window tree for frame FRAME.
6447 The return value is a list of the form (ROOT MINI), where ROOT
6448 represents the window tree of the frame's root window, and MINI
6449 is the frame's minibuffer window.
6451 If the root window is not split, ROOT is the root window itself.
6452 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6453 horizontal split, and t for a vertical split, EDGES gives the combined
6454 size and position of the subwindows in the split, and the rest of the
6455 elements are the subwindows in the split. Each of the subwindows may
6456 again be a window or a list representing a window split, and so on.
6457 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6459 If FRAME is nil or omitted, return information on the currently
6460 selected frame. */)
6461 (Lisp_Object frame)
6463 FRAME_PTR f;
6465 if (NILP (frame))
6466 frame = selected_frame;
6468 CHECK_FRAME (frame);
6469 f = XFRAME (frame);
6471 if (!FRAME_LIVE_P (f))
6472 return Qnil;
6474 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6478 /***********************************************************************
6479 Marginal Areas
6480 ***********************************************************************/
6482 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6483 2, 3, 0,
6484 doc: /* Set width of marginal areas of window WINDOW.
6485 If WINDOW is nil, set margins of the currently selected window.
6486 Second arg LEFT-WIDTH specifies the number of character cells to
6487 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6488 does the same for the right marginal area. A nil width parameter
6489 means no margin. */)
6490 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6492 struct window *w = decode_window (window);
6494 /* Translate negative or zero widths to nil.
6495 Margins that are too wide have to be checked elsewhere. */
6497 if (!NILP (left_width))
6499 CHECK_NUMBER (left_width);
6500 if (XINT (left_width) <= 0)
6501 left_width = Qnil;
6504 if (!NILP (right_width))
6506 CHECK_NUMBER (right_width);
6507 if (XINT (right_width) <= 0)
6508 right_width = Qnil;
6511 if (!EQ (w->left_margin_cols, left_width)
6512 || !EQ (w->right_margin_cols, right_width))
6514 w->left_margin_cols = left_width;
6515 w->right_margin_cols = right_width;
6517 adjust_window_margins (w);
6519 ++windows_or_buffers_changed;
6520 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6523 return Qnil;
6527 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6528 0, 1, 0,
6529 doc: /* Get width of marginal areas of window WINDOW.
6530 If WINDOW is omitted or nil, use the currently selected window.
6531 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6532 If a marginal area does not exist, its width will be returned
6533 as nil. */)
6534 (Lisp_Object window)
6536 struct window *w = decode_window (window);
6537 return Fcons (w->left_margin_cols, w->right_margin_cols);
6542 /***********************************************************************
6543 Fringes
6544 ***********************************************************************/
6546 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6547 2, 4, 0,
6548 doc: /* Set the fringe widths of window WINDOW.
6549 If WINDOW is nil, set the fringe widths of the currently selected
6550 window.
6551 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6552 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6553 fringe width. If a fringe width arg is nil, that means to use the
6554 frame's default fringe width. Default fringe widths can be set with
6555 the command `set-fringe-style'.
6556 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6557 outside of the display margins. By default, fringes are drawn between
6558 display marginal areas and the text area. */)
6559 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6561 struct window *w = decode_window (window);
6563 if (!NILP (left_width))
6564 CHECK_NATNUM (left_width);
6565 if (!NILP (right_width))
6566 CHECK_NATNUM (right_width);
6568 /* Do nothing on a tty. */
6569 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6570 && (!EQ (w->left_fringe_width, left_width)
6571 || !EQ (w->right_fringe_width, right_width)
6572 || !EQ (w->fringes_outside_margins, outside_margins)))
6574 w->left_fringe_width = left_width;
6575 w->right_fringe_width = right_width;
6576 w->fringes_outside_margins = outside_margins;
6578 adjust_window_margins (w);
6580 clear_glyph_matrix (w->current_matrix);
6581 w->window_end_valid = Qnil;
6583 ++windows_or_buffers_changed;
6584 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6587 return Qnil;
6591 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6592 0, 1, 0,
6593 doc: /* Get width of fringes of window WINDOW.
6594 If WINDOW is omitted or nil, use the currently selected window.
6595 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6596 (Lisp_Object window)
6598 struct window *w = decode_window (window);
6600 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6601 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6602 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6603 ? Qt : Qnil), Qnil)));
6608 /***********************************************************************
6609 Scroll bars
6610 ***********************************************************************/
6612 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6613 2, 4, 0,
6614 doc: /* Set width and type of scroll bars of window WINDOW.
6615 If window is nil, set scroll bars of the currently selected window.
6616 Second parameter WIDTH specifies the pixel width for the scroll bar;
6617 this is automatically adjusted to a multiple of the frame column width.
6618 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6619 bar: left, right, or nil.
6620 If WIDTH is nil, use the frame's scroll-bar width.
6621 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6622 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6623 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6625 struct window *w = decode_window (window);
6627 if (!NILP (width))
6629 CHECK_NATNUM (width);
6631 if (XINT (width) == 0)
6632 vertical_type = Qnil;
6635 if (!(NILP (vertical_type)
6636 || EQ (vertical_type, Qleft)
6637 || EQ (vertical_type, Qright)
6638 || EQ (vertical_type, Qt)))
6639 error ("Invalid type of vertical scroll bar");
6641 if (!EQ (w->scroll_bar_width, width)
6642 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6644 w->scroll_bar_width = width;
6645 w->vertical_scroll_bar_type = vertical_type;
6647 adjust_window_margins (w);
6649 clear_glyph_matrix (w->current_matrix);
6650 w->window_end_valid = Qnil;
6652 ++windows_or_buffers_changed;
6653 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6656 return Qnil;
6660 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6661 0, 1, 0,
6662 doc: /* Get width and type of scroll bars of window WINDOW.
6663 If WINDOW is omitted or nil, use the currently selected window.
6664 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6665 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6666 value. */)
6667 (Lisp_Object window)
6669 struct window *w = decode_window (window);
6670 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6671 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6672 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6673 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6674 Fcons (w->vertical_scroll_bar_type,
6675 Fcons (Qnil, Qnil))));
6680 /***********************************************************************
6681 Smooth scrolling
6682 ***********************************************************************/
6684 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6685 doc: /* Return the amount by which WINDOW is scrolled vertically.
6686 Use the selected window if WINDOW is nil or omitted.
6687 Normally, value is a multiple of the canonical character height of WINDOW;
6688 optional second arg PIXELS-P means value is measured in pixels. */)
6689 (Lisp_Object window, Lisp_Object pixels_p)
6691 Lisp_Object result;
6692 struct frame *f;
6693 struct window *w;
6695 if (NILP (window))
6696 window = selected_window;
6697 else
6698 CHECK_WINDOW (window);
6699 w = XWINDOW (window);
6700 f = XFRAME (w->frame);
6702 if (FRAME_WINDOW_P (f))
6703 result = (NILP (pixels_p)
6704 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6705 : make_number (-w->vscroll));
6706 else
6707 result = make_number (0);
6708 return result;
6712 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6713 2, 3, 0,
6714 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6715 WINDOW nil means use the selected window. Normally, VSCROLL is a
6716 non-negative multiple of the canonical character height of WINDOW;
6717 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6718 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6719 corresponds to an integral number of pixels. The return value is the
6720 result of this rounding.
6721 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6722 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6724 struct window *w;
6725 struct frame *f;
6727 if (NILP (window))
6728 window = selected_window;
6729 else
6730 CHECK_WINDOW (window);
6731 CHECK_NUMBER_OR_FLOAT (vscroll);
6733 w = XWINDOW (window);
6734 f = XFRAME (w->frame);
6736 if (FRAME_WINDOW_P (f))
6738 int old_dy = w->vscroll;
6740 w->vscroll = - (NILP (pixels_p)
6741 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6742 : XFLOATINT (vscroll));
6743 w->vscroll = min (w->vscroll, 0);
6745 if (w->vscroll != old_dy)
6747 /* Adjust glyph matrix of the frame if the virtual display
6748 area becomes larger than before. */
6749 if (w->vscroll < 0 && w->vscroll < old_dy)
6750 adjust_glyphs (f);
6752 /* Prevent redisplay shortcuts. */
6753 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6757 return Fwindow_vscroll (window, pixels_p);
6761 /* Call FN for all leaf windows on frame F. FN is called with the
6762 first argument being a pointer to the leaf window, and with
6763 additional argument USER_DATA. Stops when FN returns 0. */
6765 static void
6766 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6767 void *user_data)
6769 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6770 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6771 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6775 /* Helper function for foreach_window. Call FN for all leaf windows
6776 reachable from W. FN is called with the first argument being a
6777 pointer to the leaf window, and with additional argument USER_DATA.
6778 Stop when FN returns 0. Value is 0 if stopped by FN. */
6780 static int
6781 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6783 int cont;
6785 for (cont = 1; w && cont;)
6787 if (!NILP (w->hchild))
6788 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6789 else if (!NILP (w->vchild))
6790 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6791 else
6792 cont = fn (w, user_data);
6794 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6797 return cont;
6801 /* Freeze or unfreeze the window start of W unless it is a
6802 mini-window or the selected window. FREEZE_P non-null means freeze
6803 the window start. */
6805 static int
6806 freeze_window_start (struct window *w, void *freeze_p)
6808 if (MINI_WINDOW_P (w)
6809 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6810 && (w == XWINDOW (selected_window)
6811 || (MINI_WINDOW_P (XWINDOW (selected_window))
6812 && ! NILP (Vminibuf_scroll_window)
6813 && w == XWINDOW (Vminibuf_scroll_window)))))
6814 freeze_p = NULL;
6816 w->frozen_window_start_p = freeze_p != NULL;
6817 return 1;
6821 /* Freeze or unfreeze the window starts of all leaf windows on frame
6822 F, except the selected window and a mini-window. FREEZE_P non-zero
6823 means freeze the window start. */
6825 void
6826 freeze_window_starts (struct frame *f, int freeze_p)
6828 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6832 /***********************************************************************
6833 Initialization
6834 ***********************************************************************/
6836 /* Return 1 if window configurations C1 and C2
6837 describe the same state of affairs. This is used by Fequal. */
6840 compare_window_configurations (Lisp_Object c1, Lisp_Object c2, int ignore_positions)
6842 register struct save_window_data *d1, *d2;
6843 struct Lisp_Vector *sw1, *sw2;
6844 int i;
6846 CHECK_WINDOW_CONFIGURATION (c1);
6847 CHECK_WINDOW_CONFIGURATION (c2);
6849 d1 = (struct save_window_data *) XVECTOR (c1);
6850 d2 = (struct save_window_data *) XVECTOR (c2);
6851 sw1 = XVECTOR (d1->saved_windows);
6852 sw2 = XVECTOR (d2->saved_windows);
6854 if (d1->frame_cols != d2->frame_cols)
6855 return 0;
6856 if (d1->frame_lines != d2->frame_lines)
6857 return 0;
6858 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
6859 return 0;
6860 if (! EQ (d1->selected_frame, d2->selected_frame))
6861 return 0;
6862 /* Don't compare the current_window field directly.
6863 Instead see w1_is_current and w2_is_current, below. */
6864 if (! EQ (d1->current_buffer, d2->current_buffer))
6865 return 0;
6866 if (! ignore_positions)
6868 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6869 return 0;
6870 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6871 return 0;
6873 /* Don't compare the root_window field.
6874 We don't require the two configurations
6875 to use the same window object,
6876 and the two root windows must be equivalent
6877 if everything else compares equal. */
6878 if (! EQ (d1->focus_frame, d2->focus_frame))
6879 return 0;
6881 /* Verify that the two confis have the same number of windows. */
6882 if (sw1->size != sw2->size)
6883 return 0;
6885 for (i = 0; i < sw1->size; i++)
6887 struct saved_window *p1, *p2;
6888 int w1_is_current, w2_is_current;
6890 p1 = SAVED_WINDOW_N (sw1, i);
6891 p2 = SAVED_WINDOW_N (sw2, i);
6893 /* Verify that the current windows in the two
6894 configurations correspond to each other. */
6895 w1_is_current = EQ (d1->current_window, p1->window);
6896 w2_is_current = EQ (d2->current_window, p2->window);
6898 if (w1_is_current != w2_is_current)
6899 return 0;
6901 /* Verify that the corresponding windows do match. */
6902 if (! EQ (p1->buffer, p2->buffer))
6903 return 0;
6904 if (! EQ (p1->left_col, p2->left_col))
6905 return 0;
6906 if (! EQ (p1->top_line, p2->top_line))
6907 return 0;
6908 if (! EQ (p1->total_cols, p2->total_cols))
6909 return 0;
6910 if (! EQ (p1->total_lines, p2->total_lines))
6911 return 0;
6912 if (! EQ (p1->display_table, p2->display_table))
6913 return 0;
6914 if (! EQ (p1->parent, p2->parent))
6915 return 0;
6916 if (! EQ (p1->prev, p2->prev))
6917 return 0;
6918 if (! ignore_positions)
6920 if (! EQ (p1->hscroll, p2->hscroll))
6921 return 0;
6922 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6923 return 0;
6924 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6925 return 0;
6926 if (NILP (Fequal (p1->start, p2->start)))
6927 return 0;
6928 if (NILP (Fequal (p1->pointm, p2->pointm)))
6929 return 0;
6930 if (NILP (Fequal (p1->mark, p2->mark)))
6931 return 0;
6933 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6934 return 0;
6935 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6936 return 0;
6937 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6938 return 0;
6939 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6940 return 0;
6941 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6942 return 0;
6943 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6944 return 0;
6945 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6946 return 0;
6949 return 1;
6952 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6953 Scompare_window_configurations, 2, 2, 0,
6954 doc: /* Compare two window configurations as regards the structure of windows.
6955 This function ignores details such as the values of point and mark
6956 and scrolling positions. */)
6957 (Lisp_Object x, Lisp_Object y)
6959 if (compare_window_configurations (x, y, 1))
6960 return Qt;
6961 return Qnil;
6964 void
6965 init_window_once (void)
6967 struct frame *f = make_initial_frame ();
6968 XSETFRAME (selected_frame, f);
6969 Vterminal_frame = selected_frame;
6970 minibuf_window = f->minibuffer_window;
6971 selected_window = f->selected_window;
6972 last_nonminibuf_frame = f;
6974 window_initialized = 1;
6977 void
6978 init_window (void)
6980 Vwindow_list = Qnil;
6983 void
6984 syms_of_window (void)
6986 Qscroll_up = intern_c_string ("scroll-up");
6987 staticpro (&Qscroll_up);
6989 Qscroll_down = intern_c_string ("scroll-down");
6990 staticpro (&Qscroll_down);
6992 Qscroll_command = intern_c_string ("scroll-command");
6993 staticpro (&Qscroll_command);
6995 Fput (Qscroll_up, Qscroll_command, Qt);
6996 Fput (Qscroll_down, Qscroll_command, Qt);
6998 Qwindow_size_fixed = intern_c_string ("window-size-fixed");
6999 staticpro (&Qwindow_size_fixed);
7000 Fset (Qwindow_size_fixed, Qnil);
7002 staticpro (&Qwindow_configuration_change_hook);
7003 Qwindow_configuration_change_hook
7004 = intern_c_string ("window-configuration-change-hook");
7006 Qwindowp = intern_c_string ("windowp");
7007 staticpro (&Qwindowp);
7009 Qwindow_configuration_p = intern_c_string ("window-configuration-p");
7010 staticpro (&Qwindow_configuration_p);
7012 Qwindow_live_p = intern_c_string ("window-live-p");
7013 staticpro (&Qwindow_live_p);
7015 Qdisplay_buffer = intern_c_string ("display-buffer");
7016 staticpro (&Qdisplay_buffer);
7018 Qtemp_buffer_show_hook = intern_c_string ("temp-buffer-show-hook");
7019 staticpro (&Qtemp_buffer_show_hook);
7021 staticpro (&Vwindow_list);
7023 minibuf_selected_window = Qnil;
7024 staticpro (&minibuf_selected_window);
7026 window_scroll_pixel_based_preserve_x = -1;
7027 window_scroll_pixel_based_preserve_y = -1;
7028 window_scroll_preserve_hpos = -1;
7029 window_scroll_preserve_vpos = -1;
7031 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7032 doc: /* Non-nil means call as function to display a help buffer.
7033 The function is called with one argument, the buffer to be displayed.
7034 Used by `with-output-to-temp-buffer'.
7035 If this function is used, then it must do the entire job of showing
7036 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7037 Vtemp_buffer_show_function = Qnil;
7039 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7040 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7041 Vminibuf_scroll_window = Qnil;
7043 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7044 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7045 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7046 is displayed in the `mode-line' face. */);
7047 mode_line_in_non_selected_windows = 1;
7049 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7050 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7051 Vother_window_scroll_buffer = Qnil;
7053 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7054 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7055 auto_window_vscroll_p = 1;
7057 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7058 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7059 next_screen_context_lines = 2;
7061 DEFVAR_INT ("window-min-height", window_min_height,
7062 doc: /* Allow deleting windows less than this tall.
7063 The value is measured in line units. If a window wants a modeline it
7064 is counted as one line.
7066 Emacs honors settings of this variable when enlarging or shrinking
7067 windows vertically. A value less than 1 is invalid. */);
7068 window_min_height = 4;
7070 DEFVAR_INT ("window-min-width", window_min_width,
7071 doc: /* Allow deleting windows less than this wide.
7072 The value is measured in characters and includes any fringes or
7073 the scrollbar.
7075 Emacs honors settings of this variable when enlarging or shrinking
7076 windows horizontally. A value less than 2 is invalid. */);
7077 window_min_width = 10;
7079 DEFVAR_LISP ("scroll-preserve-screen-position",
7080 Vscroll_preserve_screen_position,
7081 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
7082 A value of nil means point does not keep its screen position except
7083 at the scroll margin or window boundary respectively.
7084 A value of t means point keeps its screen position if the scroll
7085 command moved it vertically out of the window, e.g. when scrolling
7086 by full screens.
7087 Any other value means point always keeps its screen position.
7088 Scroll commands should have the `scroll-command' property
7089 on their symbols to be controlled by this variable. */);
7090 Vscroll_preserve_screen_position = Qnil;
7092 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7093 doc: /* Type of marker to use for `window-point'. */);
7094 Vwindow_point_insertion_type = Qnil;
7096 DEFVAR_LISP ("window-configuration-change-hook",
7097 Vwindow_configuration_change_hook,
7098 doc: /* Functions to call when window configuration changes.
7099 The buffer-local part is run once per window, with the relevant window
7100 selected; while the global part is run only once for the modified frame,
7101 with the relevant frame selected. */);
7102 Vwindow_configuration_change_hook = Qnil;
7104 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7105 doc: /* If non-nil, then the `recenter' command with a nil argument
7106 will redraw the entire frame; the special value `tty' causes the
7107 frame to be redrawn only if it is a tty frame. */);
7108 Vrecenter_redisplay = Qtty;
7111 defsubr (&Sselected_window);
7112 defsubr (&Sminibuffer_window);
7113 defsubr (&Swindow_minibuffer_p);
7114 defsubr (&Swindowp);
7115 defsubr (&Swindow_live_p);
7116 defsubr (&Spos_visible_in_window_p);
7117 defsubr (&Swindow_line_height);
7118 defsubr (&Swindow_buffer);
7119 defsubr (&Swindow_height);
7120 defsubr (&Swindow_width);
7121 defsubr (&Swindow_full_width_p);
7122 defsubr (&Swindow_hscroll);
7123 defsubr (&Sset_window_hscroll);
7124 defsubr (&Swindow_redisplay_end_trigger);
7125 defsubr (&Sset_window_redisplay_end_trigger);
7126 defsubr (&Swindow_edges);
7127 defsubr (&Swindow_pixel_edges);
7128 defsubr (&Swindow_absolute_pixel_edges);
7129 defsubr (&Swindow_inside_edges);
7130 defsubr (&Swindow_inside_pixel_edges);
7131 defsubr (&Swindow_inside_absolute_pixel_edges);
7132 defsubr (&Scoordinates_in_window_p);
7133 defsubr (&Swindow_at);
7134 defsubr (&Swindow_point);
7135 defsubr (&Swindow_start);
7136 defsubr (&Swindow_end);
7137 defsubr (&Sset_window_point);
7138 defsubr (&Sset_window_start);
7139 defsubr (&Swindow_dedicated_p);
7140 defsubr (&Sset_window_dedicated_p);
7141 defsubr (&Swindow_display_table);
7142 defsubr (&Sset_window_display_table);
7143 defsubr (&Snext_window);
7144 defsubr (&Sprevious_window);
7145 defsubr (&Sother_window);
7146 defsubr (&Sget_lru_window);
7147 defsubr (&Swindow_use_time);
7148 defsubr (&Sget_largest_window);
7149 defsubr (&Sget_buffer_window);
7150 defsubr (&Sdelete_other_windows);
7151 defsubr (&Sdelete_windows_on);
7152 defsubr (&Sreplace_buffer_in_windows);
7153 defsubr (&Sdelete_window);
7154 defsubr (&Sset_window_buffer);
7155 defsubr (&Sselect_window);
7156 defsubr (&Sforce_window_update);
7157 defsubr (&Stemp_output_buffer_show);
7158 defsubr (&Ssplit_window);
7159 defsubr (&Senlarge_window);
7160 defsubr (&Sshrink_window);
7161 defsubr (&Sadjust_window_trailing_edge);
7162 defsubr (&Sscroll_up);
7163 defsubr (&Sscroll_down);
7164 defsubr (&Sscroll_left);
7165 defsubr (&Sscroll_right);
7166 defsubr (&Sother_window_for_scrolling);
7167 defsubr (&Sscroll_other_window);
7168 defsubr (&Sminibuffer_selected_window);
7169 defsubr (&Srecenter);
7170 defsubr (&Swindow_text_height);
7171 defsubr (&Smove_to_window_line);
7172 defsubr (&Swindow_configuration_p);
7173 defsubr (&Swindow_configuration_frame);
7174 defsubr (&Sset_window_configuration);
7175 defsubr (&Scurrent_window_configuration);
7176 defsubr (&Swindow_tree);
7177 defsubr (&Sset_window_margins);
7178 defsubr (&Swindow_margins);
7179 defsubr (&Sset_window_fringes);
7180 defsubr (&Swindow_fringes);
7181 defsubr (&Sset_window_scroll_bars);
7182 defsubr (&Swindow_scroll_bars);
7183 defsubr (&Swindow_vscroll);
7184 defsubr (&Sset_window_vscroll);
7185 defsubr (&Scompare_window_configurations);
7186 defsubr (&Swindow_list);
7187 defsubr (&Swindow_parameters);
7188 defsubr (&Swindow_parameter);
7189 defsubr (&Sset_window_parameter);
7193 void
7194 keys_of_window (void)
7196 initial_define_key (control_x_map, '1', "delete-other-windows");
7197 initial_define_key (control_x_map, '2', "split-window");
7198 initial_define_key (control_x_map, '0', "delete-window");
7199 initial_define_key (control_x_map, 'o', "other-window");
7200 initial_define_key (control_x_map, '^', "enlarge-window");
7201 initial_define_key (control_x_map, '<', "scroll-left");
7202 initial_define_key (control_x_map, '>', "scroll-right");
7204 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7205 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7206 initial_define_key (meta_map, 'v', "scroll-down-command");