Merge from gnulib.
[emacs.git] / src / window.c
blob4dbee41c5f4d4d7219d520234b6d95aa19e668c7
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;
54 static Lisp_Object Qwindow_configuration_p;
55 static Lisp_Object Qdisplay_buffer;
56 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
57 static Lisp_Object Qwindow_size_fixed;
59 static int displayed_window_lines (struct window *);
60 static struct window *decode_window (Lisp_Object);
61 static int count_windows (struct window *);
62 static int get_leaf_windows (struct window *, struct window **, int);
63 static void window_scroll (Lisp_Object, int, int, int);
64 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
65 static void window_scroll_line_based (Lisp_Object, int, int, int);
66 static int window_min_size_1 (struct window *, int, int);
67 static int window_min_size_2 (struct window *, int, int);
68 static int window_min_size (struct window *, int, int, int, int *);
69 static void size_window (Lisp_Object, int, int, int, int, int);
70 static int freeze_window_start (struct window *, void *);
71 static int window_fixed_size_p (struct window *, int, int);
72 static void enlarge_window (Lisp_Object, int, int);
73 static Lisp_Object window_list (void);
74 static int add_window_to_list (struct window *, void *);
75 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
76 Lisp_Object);
77 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
78 Lisp_Object, int);
79 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
80 Lisp_Object *);
81 static void foreach_window (struct frame *,
82 int (* fn) (struct window *, void *),
83 void *);
84 static int foreach_window_1 (struct window *,
85 int (* fn) (struct window *, void *),
86 void *);
87 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
88 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
90 /* This is the window in which the terminal's cursor should
91 be left when nothing is being done with it. This must
92 always be a leaf window, and its buffer is selected by
93 the top level editing loop at the end of each command.
95 This value is always the same as
96 FRAME_SELECTED_WINDOW (selected_frame). */
98 Lisp_Object selected_window;
100 /* A list of all windows for use by next_window and Fwindow_list.
101 Functions creating or deleting windows should invalidate this cache
102 by setting it to nil. */
104 Lisp_Object Vwindow_list;
106 /* The mini-buffer window of the selected frame.
107 Note that you cannot test for mini-bufferness of an arbitrary window
108 by comparing against this; but you can test for mini-bufferness of
109 the selected window. */
111 Lisp_Object minibuf_window;
113 /* Non-nil means it is the window whose mode line should be
114 shown as the selected window when the minibuffer is selected. */
116 Lisp_Object minibuf_selected_window;
118 /* Hook run at end of temp_output_buffer_show. */
120 static Lisp_Object Qtemp_buffer_show_hook;
122 /* Incremented for each window created. */
124 static int sequence_number;
126 /* Nonzero after init_window_once has finished. */
128 static int window_initialized;
130 /* Hook to run when window config changes. */
132 static Lisp_Object Qwindow_configuration_change_hook;
133 /* Incremented by 1 whenever a window is deleted. */
135 static int window_deletion_count;
137 /* Used by the function window_scroll_pixel_based */
139 static int window_scroll_pixel_based_preserve_x;
140 static int window_scroll_pixel_based_preserve_y;
142 /* Same for window_scroll_line_based. */
144 static int window_scroll_preserve_hpos;
145 static int window_scroll_preserve_vpos;
147 #if 0 /* This isn't used anywhere. */
148 /* Nonzero means we can split a frame even if it is "unsplittable". */
149 static int inhibit_frame_unsplittable;
150 #endif
153 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
154 doc: /* Return t if OBJECT is a window. */)
155 (Lisp_Object object)
157 return WINDOWP (object) ? Qt : Qnil;
160 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
161 doc: /* Return t if OBJECT is a window which is currently visible. */)
162 (Lisp_Object object)
164 return WINDOW_LIVE_P (object) ? Qt : Qnil;
167 Lisp_Object
168 make_window (void)
170 Lisp_Object val;
171 register struct window *p;
173 p = allocate_window ();
174 ++sequence_number;
175 XSETFASTINT (p->sequence_number, sequence_number);
176 XSETFASTINT (p->left_col, 0);
177 XSETFASTINT (p->top_line, 0);
178 XSETFASTINT (p->total_lines, 0);
179 XSETFASTINT (p->total_cols, 0);
180 XSETFASTINT (p->hscroll, 0);
181 XSETFASTINT (p->min_hscroll, 0);
182 p->orig_top_line = p->orig_total_lines = Qnil;
183 p->start = Fmake_marker ();
184 p->pointm = Fmake_marker ();
185 XSETFASTINT (p->use_time, 0);
186 p->frame = Qnil;
187 p->display_table = Qnil;
188 p->dedicated = Qnil;
189 p->window_parameters = Qnil;
190 p->pseudo_window_p = 0;
191 memset (&p->cursor, 0, sizeof (p->cursor));
192 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
193 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
194 p->desired_matrix = p->current_matrix = 0;
195 p->nrows_scale_factor = p->ncols_scale_factor = 1;
196 p->phys_cursor_type = -1;
197 p->phys_cursor_width = -1;
198 p->must_be_updated_p = 0;
199 XSETFASTINT (p->window_end_vpos, 0);
200 XSETFASTINT (p->window_end_pos, 0);
201 p->window_end_valid = Qnil;
202 p->vscroll = 0;
203 XSETWINDOW (val, p);
204 XSETFASTINT (p->last_point, 0);
205 p->frozen_window_start_p = 0;
206 p->last_cursor_off_p = p->cursor_off_p = 0;
207 p->left_margin_cols = Qnil;
208 p->right_margin_cols = Qnil;
209 p->left_fringe_width = Qnil;
210 p->right_fringe_width = Qnil;
211 p->fringes_outside_margins = Qnil;
212 p->scroll_bar_width = Qnil;
213 p->vertical_scroll_bar_type = Qt;
214 p->resize_proportionally = Qnil;
216 Vwindow_list = Qnil;
217 return val;
220 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
221 doc: /* Return the window that the cursor now appears in and commands apply to. */)
222 (void)
224 return selected_window;
227 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
228 doc: /* Return the window used now for minibuffers.
229 If the optional argument FRAME is specified, return the minibuffer window
230 used by that frame. */)
231 (Lisp_Object frame)
233 if (NILP (frame))
234 frame = selected_frame;
235 CHECK_LIVE_FRAME (frame);
236 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
239 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
240 Swindow_minibuffer_p, 0, 1, 0,
241 doc: /* Return non-nil if WINDOW is a minibuffer window.
242 WINDOW defaults to the selected window. */)
243 (Lisp_Object window)
245 struct window *w = decode_window (window);
246 return MINI_WINDOW_P (w) ? Qt : Qnil;
250 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
251 Spos_visible_in_window_p, 0, 3, 0,
252 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
253 Return nil if that position is scrolled vertically out of view.
254 If a character is only partially visible, nil is returned, unless the
255 optional argument PARTIALLY is non-nil.
256 If POS is only out of view because of horizontal scrolling, return non-nil.
257 If POS is t, it specifies the position of the last visible glyph in WINDOW.
258 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
260 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
261 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
262 where X and Y are the pixel coordinates relative to the top left corner
263 of the window. The remaining elements are omitted if the character after
264 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
265 off-window at the top and bottom of the row, ROWH is the height of the
266 display row, and VPOS is the row number (0-based) containing POS. */)
267 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
269 register struct window *w;
270 register EMACS_INT posint;
271 register struct buffer *buf;
272 struct text_pos top;
273 Lisp_Object in_window = Qnil;
274 int rtop, rbot, rowh, vpos, fully_p = 1;
275 int x, y;
277 w = decode_window (window);
278 buf = XBUFFER (w->buffer);
279 SET_TEXT_POS_FROM_MARKER (top, w->start);
281 if (EQ (pos, Qt))
282 posint = -1;
283 else if (!NILP (pos))
285 CHECK_NUMBER_COERCE_MARKER (pos);
286 posint = XINT (pos);
288 else if (w == XWINDOW (selected_window))
289 posint = PT;
290 else
291 posint = XMARKER (w->pointm)->charpos;
293 /* If position is above window start or outside buffer boundaries,
294 or if window start is out of range, position is not visible. */
295 if ((EQ (pos, Qt)
296 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
297 && CHARPOS (top) >= BUF_BEGV (buf)
298 && CHARPOS (top) <= BUF_ZV (buf)
299 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
300 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
301 in_window = Qt;
303 if (!NILP (in_window) && !NILP (partially))
305 Lisp_Object part = Qnil;
306 if (!fully_p)
307 part = list4 (make_number (rtop), make_number (rbot),
308 make_number (rowh), make_number (vpos));
309 in_window = Fcons (make_number (x),
310 Fcons (make_number (y), part));
313 return in_window;
316 DEFUN ("window-line-height", Fwindow_line_height,
317 Swindow_line_height, 0, 2, 0,
318 doc: /* Return height in pixels of text line LINE in window WINDOW.
319 If WINDOW is nil or omitted, use selected window.
321 Return height of current line if LINE is omitted or nil. Return height of
322 header or mode line if LINE is `header-line' and `mode-line'.
323 Otherwise, LINE is a text line number starting from 0. A negative number
324 counts from the end of the window.
326 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
327 in pixels of the visible part of the line, VPOS and YPOS are the
328 vertical position in lines and pixels of the line, relative to the top
329 of the first text line, and OFFBOT is the number of off-window pixels at
330 the bottom of the text line. If there are off-window pixels at the top
331 of the (first) text line, YPOS is negative.
333 Return nil if window display is not up-to-date. In that case, use
334 `pos-visible-in-window-p' to obtain the information. */)
335 (Lisp_Object line, Lisp_Object window)
337 register struct window *w;
338 register struct buffer *b;
339 struct glyph_row *row, *end_row;
340 int max_y, crop, i, n;
342 w = decode_window (window);
344 if (noninteractive
345 || w->pseudo_window_p)
346 return Qnil;
348 CHECK_BUFFER (w->buffer);
349 b = XBUFFER (w->buffer);
351 /* Fail if current matrix is not up-to-date. */
352 if (NILP (w->window_end_valid)
353 || current_buffer->clip_changed
354 || current_buffer->prevent_redisplay_optimizations_p
355 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
356 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
357 return Qnil;
359 if (NILP (line))
361 i = w->cursor.vpos;
362 if (i < 0 || i >= w->current_matrix->nrows
363 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
364 return Qnil;
365 max_y = window_text_bottom_y (w);
366 goto found_row;
369 if (EQ (line, Qheader_line))
371 if (!WINDOW_WANTS_HEADER_LINE_P (w))
372 return Qnil;
373 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
374 if (!row->enabled_p)
375 return Qnil;
376 return list4 (make_number (row->height),
377 make_number (0), make_number (0),
378 make_number (0));
381 if (EQ (line, Qmode_line))
383 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
384 if (!row->enabled_p)
385 return Qnil;
386 return list4 (make_number (row->height),
387 make_number (0), /* not accurate */
388 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
389 + window_text_bottom_y (w)),
390 make_number (0));
393 CHECK_NUMBER (line);
394 n = XINT (line);
396 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
397 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
398 max_y = window_text_bottom_y (w);
399 i = 0;
401 while ((n < 0 || i < n)
402 && row <= end_row && row->enabled_p
403 && row->y + row->height < max_y)
404 row++, i++;
406 if (row > end_row || !row->enabled_p)
407 return Qnil;
409 if (++n < 0)
411 if (-n > i)
412 return Qnil;
413 row += n;
414 i += n;
417 found_row:
418 crop = max (0, (row->y + row->height) - max_y);
419 return list4 (make_number (row->height + min (0, row->y) - crop),
420 make_number (i),
421 make_number (row->y),
422 make_number (crop));
427 static struct window *
428 decode_window (register Lisp_Object window)
430 if (NILP (window))
431 return XWINDOW (selected_window);
433 CHECK_LIVE_WINDOW (window);
434 return XWINDOW (window);
437 static struct window *
438 decode_any_window (register Lisp_Object window)
440 if (NILP (window))
441 return XWINDOW (selected_window);
443 CHECK_WINDOW (window);
444 return XWINDOW (window);
447 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
448 doc: /* Return the buffer that WINDOW is displaying.
449 WINDOW defaults to the selected window. */)
450 (Lisp_Object window)
452 return decode_window (window)->buffer;
455 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
456 doc: /* Return the number of lines in WINDOW.
457 WINDOW defaults to the selected window.
459 The return value includes WINDOW's mode line and header line, if any.
461 Note: The function does not take into account the value of `line-spacing'
462 when calculating the number of lines in WINDOW. */)
463 (Lisp_Object window)
465 return decode_any_window (window)->total_lines;
468 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
469 doc: /* Return the number of display columns in WINDOW.
470 WINDOW defaults to the selected window.
472 Note: The return value is the number of columns available for text in
473 WINDOW. If you want to find out how many columns WINDOW takes up, use
474 (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
475 (Lisp_Object window)
477 return make_number (window_box_text_cols (decode_any_window (window)));
480 DEFUN ("window-full-width-p", Fwindow_full_width_p, Swindow_full_width_p, 0, 1, 0,
481 doc: /* Return t if WINDOW is as wide as its frame.
482 WINDOW defaults to the selected window. */)
483 (Lisp_Object window)
485 return WINDOW_FULL_WIDTH_P (decode_any_window (window)) ? Qt : Qnil;
488 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
489 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
490 WINDOW defaults to the selected window. */)
491 (Lisp_Object window)
493 return decode_window (window)->hscroll;
496 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
497 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
498 Return NCOL. NCOL should be zero or positive.
500 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
501 window so that the location of point moves off-window. */)
502 (Lisp_Object window, Lisp_Object ncol)
504 struct window *w = decode_window (window);
505 int hscroll;
507 CHECK_NUMBER (ncol);
508 hscroll = max (0, XINT (ncol));
510 /* Prevent redisplay shortcuts when changing the hscroll. */
511 if (XINT (w->hscroll) != hscroll)
512 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
514 w->hscroll = make_number (hscroll);
515 return ncol;
518 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
519 Swindow_redisplay_end_trigger, 0, 1, 0,
520 doc: /* Return WINDOW's redisplay end trigger value.
521 WINDOW defaults to the selected window.
522 See `set-window-redisplay-end-trigger' for more information. */)
523 (Lisp_Object window)
525 return decode_window (window)->redisplay_end_trigger;
528 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
529 Sset_window_redisplay_end_trigger, 2, 2, 0,
530 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
531 VALUE should be a buffer position (typically a marker) or nil.
532 If it is a buffer position, then if redisplay in WINDOW reaches a position
533 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
534 with two arguments: WINDOW, and the end trigger value.
535 Afterwards the end-trigger value is reset to nil. */)
536 (register Lisp_Object window, Lisp_Object value)
538 register struct window *w;
540 w = decode_window (window);
541 w->redisplay_end_trigger = value;
542 return value;
545 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
546 doc: /* Return a list of the edge coordinates of WINDOW.
547 The list has the form (LEFT TOP RIGHT BOTTOM).
548 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
549 all relative to 0, 0 at top left corner of frame.
551 RIGHT is one more than the rightmost column occupied by WINDOW.
552 BOTTOM is one more than the bottommost row occupied by WINDOW.
553 The edges include the space used by WINDOW's scroll bar, display
554 margins, fringes, header line, and/or mode line. For the edges of
555 just the text area, use `window-inside-edges'. */)
556 (Lisp_Object window)
558 register struct window *w = decode_any_window (window);
560 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
561 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
562 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
563 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
564 Qnil))));
567 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
568 doc: /* Return a list of the edge pixel coordinates of WINDOW.
569 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
570 the top left corner of the frame.
572 RIGHT is one more than the rightmost x position occupied by WINDOW.
573 BOTTOM is one more than the bottommost y position occupied by WINDOW.
574 The pixel edges include the space used by WINDOW's scroll bar, display
575 margins, fringes, header line, and/or mode line. For the pixel edges
576 of just the text area, use `window-inside-pixel-edges'. */)
577 (Lisp_Object window)
579 register struct window *w = decode_any_window (window);
581 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
582 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
583 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
584 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
585 Qnil))));
588 static void
589 calc_absolute_offset(struct window *w, int *add_x, int *add_y)
591 struct frame *f = XFRAME (w->frame);
592 *add_y = f->top_pos;
593 #ifdef FRAME_MENUBAR_HEIGHT
594 *add_y += FRAME_MENUBAR_HEIGHT (f);
595 #endif
596 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
597 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
598 #elif FRAME_TOOLBAR_HEIGHT
599 *add_y += FRAME_TOOLBAR_HEIGHT (f);
600 #endif
601 #ifdef FRAME_NS_TITLEBAR_HEIGHT
602 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
603 #endif
604 *add_x = f->left_pos;
605 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
606 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
607 #endif
610 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
611 Swindow_absolute_pixel_edges, 0, 1, 0,
612 doc: /* Return a list of the edge pixel coordinates of WINDOW.
613 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
614 the top left corner of the display.
616 RIGHT is one more than the rightmost x position occupied by WINDOW.
617 BOTTOM is one more than the bottommost y position occupied by WINDOW.
618 The pixel edges include the space used by WINDOW's scroll bar, display
619 margins, fringes, header line, and/or mode line. For the pixel edges
620 of just the text area, use `window-inside-absolute-pixel-edges'. */)
621 (Lisp_Object window)
623 register struct window *w = decode_any_window (window);
624 int add_x, add_y;
625 calc_absolute_offset (w, &add_x, &add_y);
627 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
628 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
629 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
630 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
631 Qnil))));
634 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
635 doc: /* Return a list of the edge coordinates of WINDOW.
636 The list has the form (LEFT TOP RIGHT BOTTOM).
637 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
638 all relative to 0, 0 at top left corner of frame.
640 RIGHT is one more than the rightmost column of WINDOW's text area.
641 BOTTOM is one more than the bottommost row of WINDOW's text area.
642 The inside edges do not include the space used by the WINDOW's scroll
643 bar, display margins, fringes, header line, and/or mode line. */)
644 (Lisp_Object window)
646 register struct window *w = decode_any_window (window);
648 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
649 + WINDOW_LEFT_MARGIN_COLS (w)
650 + WINDOW_LEFT_FRINGE_COLS (w)),
651 make_number (WINDOW_TOP_EDGE_LINE (w)
652 + WINDOW_HEADER_LINE_LINES (w)),
653 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
654 - WINDOW_RIGHT_MARGIN_COLS (w)
655 - WINDOW_RIGHT_FRINGE_COLS (w)),
656 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
657 - WINDOW_MODE_LINE_LINES (w)));
660 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
661 doc: /* Return a list of the edge pixel coordinates of WINDOW.
662 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
663 the top left corner of the frame.
665 RIGHT is one more than the rightmost x position of WINDOW's text area.
666 BOTTOM is one more than the bottommost y position of WINDOW's text area.
667 The inside edges do not include the space used by WINDOW's scroll bar,
668 display margins, fringes, header line, and/or mode line. */)
669 (Lisp_Object window)
671 register struct window *w = decode_any_window (window);
673 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
674 + WINDOW_LEFT_MARGIN_WIDTH (w)
675 + WINDOW_LEFT_FRINGE_WIDTH (w)),
676 make_number (WINDOW_TOP_EDGE_Y (w)
677 + WINDOW_HEADER_LINE_HEIGHT (w)),
678 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
679 - WINDOW_RIGHT_MARGIN_WIDTH (w)
680 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
681 make_number (WINDOW_BOTTOM_EDGE_Y (w)
682 - WINDOW_MODE_LINE_HEIGHT (w)));
685 DEFUN ("window-inside-absolute-pixel-edges",
686 Fwindow_inside_absolute_pixel_edges,
687 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
688 doc: /* Return a list of the edge pixel coordinates of WINDOW.
689 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
690 the top left corner of the display.
692 RIGHT is one more than the rightmost x position of WINDOW's text area.
693 BOTTOM is one more than the bottommost y position of WINDOW's text area.
694 The inside edges do not include the space used by WINDOW's scroll bar,
695 display margins, fringes, header line, and/or mode line. */)
696 (Lisp_Object window)
698 register struct window *w = decode_any_window (window);
699 int add_x, add_y;
700 calc_absolute_offset (w, &add_x, &add_y);
702 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
703 + WINDOW_LEFT_MARGIN_WIDTH (w)
704 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
705 make_number (WINDOW_TOP_EDGE_Y (w)
706 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
707 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
708 - WINDOW_RIGHT_MARGIN_WIDTH (w)
709 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
710 make_number (WINDOW_BOTTOM_EDGE_Y (w)
711 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
714 /* Test if the character at column X, row Y is within window W.
715 If it is not, return ON_NOTHING;
716 if it is in the window's text area, return ON_TEXT;
717 if it is on the window's modeline, return ON_MODE_LINE;
718 if it is on the border between the window and its right sibling,
719 return ON_VERTICAL_BORDER.
720 if it is on a scroll bar, return ON_SCROLL_BAR.
721 if it is on the window's top line, return ON_HEADER_LINE;
722 if it is in left or right fringe of the window,
723 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
724 if it is in the marginal area to the left/right of the window,
725 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
727 X and Y are frame relative pixel coordinates. */
729 static enum window_part
730 coordinates_in_window (register struct window *w, int x, int y)
732 struct frame *f = XFRAME (WINDOW_FRAME (w));
733 int left_x, right_x;
734 enum window_part part;
735 int ux = FRAME_COLUMN_WIDTH (f);
736 int x0 = WINDOW_LEFT_EDGE_X (w);
737 int x1 = WINDOW_RIGHT_EDGE_X (w);
738 /* The width of the area where the vertical line can be dragged.
739 (Between mode lines for instance. */
740 int grabbable_width = ux;
741 int lmargin_width, rmargin_width, text_left, text_right;
742 int top_y = WINDOW_TOP_EDGE_Y (w);
743 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
745 /* Outside any interesting row? */
746 if (y < top_y || y >= bottom_y)
747 return ON_NOTHING;
749 /* In what's below, we subtract 1 when computing right_x because we
750 want the rightmost pixel, which is given by left_pixel+width-1. */
751 if (w->pseudo_window_p)
753 left_x = 0;
754 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
756 else
758 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
759 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
762 /* On the mode line or header line? If it's near the start of
763 the mode or header line of window that's has a horizontal
764 sibling, say it's on the vertical line. That's to be able
765 to resize windows horizontally in case we're using toolkit
766 scroll bars. */
768 if (WINDOW_WANTS_MODELINE_P (w)
769 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
771 part = ON_MODE_LINE;
773 header_vertical_border_check:
774 /* We're somewhere on the mode line. We consider the place
775 between mode lines of horizontally adjacent mode lines
776 as the vertical border. If scroll bars on the left,
777 return the right window. */
778 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
779 || WINDOW_RIGHTMOST_P (w))
780 && !WINDOW_LEFTMOST_P (w)
781 && eabs (x - x0) < grabbable_width)
782 return ON_VERTICAL_BORDER;
784 /* Make sure we're not at the rightmost position of a
785 mode-/header-line and there's yet another window on the
786 right. (Bug#1372) */
787 else if ((WINDOW_RIGHTMOST_P (w) || x < x1)
788 && eabs (x - x1) < grabbable_width)
789 return ON_VERTICAL_BORDER;
791 if (x < x0 || x >= x1)
792 return ON_NOTHING;
794 return part;
797 if (WINDOW_WANTS_HEADER_LINE_P (w)
798 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
800 part = ON_HEADER_LINE;
801 goto header_vertical_border_check;
804 if (x < x0 || x >= x1) return ON_NOTHING;
806 /* Outside any interesting column? */
807 if (x < left_x || x > right_x)
808 return ON_SCROLL_BAR;
810 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
811 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
813 text_left = window_box_left (w, TEXT_AREA);
814 text_right = text_left + window_box_width (w, TEXT_AREA);
816 if (FRAME_WINDOW_P (f))
818 if (!w->pseudo_window_p
819 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
820 && !WINDOW_RIGHTMOST_P (w)
821 && (eabs (x - right_x) < grabbable_width))
822 return ON_VERTICAL_BORDER;
824 /* Need to say "x > right_x" rather than >=, since on character
825 terminals, the vertical line's x coordinate is right_x. */
826 else if (!w->pseudo_window_p
827 && !WINDOW_RIGHTMOST_P (w)
828 && x > right_x - ux)
829 return ON_VERTICAL_BORDER;
831 if (x < text_left)
833 if (lmargin_width > 0
834 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
835 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
836 : (x < left_x + lmargin_width)))
837 return ON_LEFT_MARGIN;
839 return ON_LEFT_FRINGE;
842 if (x >= text_right)
844 if (rmargin_width > 0
845 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
846 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
847 : (x >= right_x - rmargin_width)))
848 return ON_RIGHT_MARGIN;
850 return ON_RIGHT_FRINGE;
853 /* Everything special ruled out - must be on text area */
854 return ON_TEXT;
857 /* Take X is the frame-relative pixel x-coordinate, and return the
858 x-coordinate relative to part PART of window W. */
860 window_relative_x_coord (struct window *w, enum window_part part, int x)
862 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
864 switch (part)
866 case ON_TEXT:
867 return x - window_box_left (w, TEXT_AREA);
869 case ON_LEFT_FRINGE:
870 return x - left_x;
872 case ON_RIGHT_FRINGE:
873 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
875 case ON_LEFT_MARGIN:
876 return (x - left_x
877 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
878 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
880 case ON_RIGHT_MARGIN:
881 return (x + 1
882 - ((w->pseudo_window_p)
883 ? WINDOW_TOTAL_WIDTH (w)
884 : WINDOW_BOX_RIGHT_EDGE_X (w))
885 + window_box_width (w, RIGHT_MARGIN_AREA)
886 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
887 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
890 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
891 return 0;
895 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
896 Scoordinates_in_window_p, 2, 2, 0,
897 doc: /* Return non-nil if COORDINATES are in WINDOW.
898 COORDINATES is a cons of the form (X . Y), X and Y being distances
899 measured in characters from the upper-left corner of the frame.
900 \(0 . 0) denotes the character in the upper left corner of the
901 frame.
902 If COORDINATES are in the text portion of WINDOW,
903 the coordinates relative to the window are returned.
904 If they are in the mode line of WINDOW, `mode-line' is returned.
905 If they are in the top mode line of WINDOW, `header-line' is returned.
906 If they are in the left fringe of WINDOW, `left-fringe' is returned.
907 If they are in the right fringe of WINDOW, `right-fringe' is returned.
908 If they are on the border between WINDOW and its right sibling,
909 `vertical-line' is returned.
910 If they are in the windows's left or right marginal areas, `left-margin'\n\
911 or `right-margin' is returned. */)
912 (register Lisp_Object coordinates, Lisp_Object window)
914 struct window *w;
915 struct frame *f;
916 int x, y;
917 Lisp_Object lx, ly;
919 CHECK_WINDOW (window);
920 w = XWINDOW (window);
921 f = XFRAME (w->frame);
922 CHECK_CONS (coordinates);
923 lx = Fcar (coordinates);
924 ly = Fcdr (coordinates);
925 CHECK_NUMBER_OR_FLOAT (lx);
926 CHECK_NUMBER_OR_FLOAT (ly);
927 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
928 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
930 switch (coordinates_in_window (w, x, y))
932 case ON_NOTHING:
933 return Qnil;
935 case ON_TEXT:
936 /* Convert X and Y to window relative pixel coordinates, and
937 return the canonical char units. */
938 x -= window_box_left (w, TEXT_AREA);
939 y -= WINDOW_TOP_EDGE_Y (w);
940 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
941 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
943 case ON_MODE_LINE:
944 return Qmode_line;
946 case ON_VERTICAL_BORDER:
947 return Qvertical_line;
949 case ON_HEADER_LINE:
950 return Qheader_line;
952 case ON_LEFT_FRINGE:
953 return Qleft_fringe;
955 case ON_RIGHT_FRINGE:
956 return Qright_fringe;
958 case ON_LEFT_MARGIN:
959 return Qleft_margin;
961 case ON_RIGHT_MARGIN:
962 return Qright_margin;
964 case ON_SCROLL_BAR:
965 /* Historically we are supposed to return nil in this case. */
966 return Qnil;
968 default:
969 abort ();
974 /* Callback for foreach_window, used in window_from_coordinates.
975 Check if window W contains coordinates specified by USER_DATA which
976 is actually a pointer to a struct check_window_data CW.
978 Check if window W contains coordinates *CW->x and *CW->y. If it
979 does, return W in *CW->window, as Lisp_Object, and return in
980 *CW->part the part of the window under coordinates *X,*Y. Return
981 zero from this function to stop iterating over windows. */
983 struct check_window_data
985 Lisp_Object *window;
986 int x, y;
987 enum window_part *part;
990 static int
991 check_window_containing (struct window *w, void *user_data)
993 struct check_window_data *cw = (struct check_window_data *) user_data;
994 enum window_part found;
995 int continue_p = 1;
997 found = coordinates_in_window (w, cw->x, cw->y);
998 if (found != ON_NOTHING)
1000 *cw->part = found;
1001 XSETWINDOW (*cw->window, w);
1002 continue_p = 0;
1005 return continue_p;
1009 /* Find the window containing frame-relative pixel position X/Y and
1010 return it as a Lisp_Object.
1012 If X, Y is on one of the window's special `window_part' elements,
1013 set *PART to the id of that element.
1015 If there is no window under X, Y return nil and leave *PART
1016 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1018 This function was previously implemented with a loop cycling over
1019 windows with Fnext_window, and starting with the frame's selected
1020 window. It turned out that this doesn't work with an
1021 implementation of next_window using Vwindow_list, because
1022 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1023 tree of F when this function is called asynchronously from
1024 note_mouse_highlight. The original loop didn't terminate in this
1025 case. */
1027 Lisp_Object
1028 window_from_coordinates (struct frame *f, int x, int y,
1029 enum window_part *part, int tool_bar_p)
1031 Lisp_Object window;
1032 struct check_window_data cw;
1033 enum window_part dummy;
1035 if (part == 0)
1036 part = &dummy;
1038 window = Qnil;
1039 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1040 foreach_window (f, check_window_containing, &cw);
1042 /* If not found above, see if it's in the tool bar window, if a tool
1043 bar exists. */
1044 if (NILP (window)
1045 && tool_bar_p
1046 && WINDOWP (f->tool_bar_window)
1047 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1048 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1049 != ON_NOTHING))
1051 *part = ON_TEXT;
1052 window = f->tool_bar_window;
1055 return window;
1058 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1059 doc: /* Return window containing coordinates X and Y on FRAME.
1060 If omitted, FRAME defaults to the currently selected frame.
1061 The top left corner of the frame is considered to be row 0,
1062 column 0. */)
1063 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1065 struct frame *f;
1067 if (NILP (frame))
1068 frame = selected_frame;
1069 CHECK_LIVE_FRAME (frame);
1070 f = XFRAME (frame);
1072 /* Check that arguments are integers or floats. */
1073 CHECK_NUMBER_OR_FLOAT (x);
1074 CHECK_NUMBER_OR_FLOAT (y);
1076 return window_from_coordinates (f,
1077 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1078 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1079 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1080 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1081 0, 0);
1084 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1085 doc: /* Return current value of point in WINDOW.
1086 WINDOW defaults to the selected window.
1088 For a nonselected window, this is the value point would have
1089 if that window were selected.
1091 Note that, when WINDOW is the selected window and its buffer
1092 is also currently selected, the value returned is the same as (point).
1093 It would be more strictly correct to return the `top-level' value
1094 of point, outside of any save-excursion forms.
1095 But that is hard to define. */)
1096 (Lisp_Object window)
1098 register struct window *w = decode_window (window);
1100 if (w == XWINDOW (selected_window)
1101 && current_buffer == XBUFFER (w->buffer))
1102 return Fpoint ();
1103 return Fmarker_position (w->pointm);
1106 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1107 doc: /* Return position at which display currently starts in WINDOW.
1108 WINDOW defaults to the selected window.
1109 This is updated by redisplay or by calling `set-window-start'. */)
1110 (Lisp_Object window)
1112 return Fmarker_position (decode_window (window)->start);
1115 /* This is text temporarily removed from the doc string below.
1117 This function returns nil if the position is not currently known.
1118 That happens when redisplay is preempted and doesn't finish.
1119 If in that case you want to compute where the end of the window would
1120 have been if redisplay had finished, do this:
1121 (save-excursion
1122 (goto-char (window-start window))
1123 (vertical-motion (1- (window-height window)) window)
1124 (point))") */
1126 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1127 doc: /* Return position at which display currently ends in WINDOW.
1128 WINDOW defaults to the selected window.
1129 This is updated by redisplay, when it runs to completion.
1130 Simply changing the buffer text or setting `window-start'
1131 does not update this value.
1132 Return nil if there is no recorded value. \(This can happen if the
1133 last redisplay of WINDOW was preempted, and did not finish.)
1134 If UPDATE is non-nil, compute the up-to-date position
1135 if it isn't already recorded. */)
1136 (Lisp_Object window, Lisp_Object update)
1138 Lisp_Object value;
1139 struct window *w = decode_window (window);
1140 Lisp_Object buf;
1141 struct buffer *b;
1143 buf = w->buffer;
1144 CHECK_BUFFER (buf);
1145 b = XBUFFER (buf);
1147 #if 0 /* This change broke some things. We should make it later. */
1148 /* If we don't know the end position, return nil.
1149 The user can compute it with vertical-motion if he wants to.
1150 It would be nicer to do it automatically,
1151 but that's so slow that it would probably bother people. */
1152 if (NILP (w->window_end_valid))
1153 return Qnil;
1154 #endif
1156 if (! NILP (update)
1157 && ! (! NILP (w->window_end_valid)
1158 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1159 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1160 && !noninteractive)
1162 struct text_pos startp;
1163 struct it it;
1164 struct buffer *old_buffer = NULL;
1166 /* Cannot use Fvertical_motion because that function doesn't
1167 cope with variable-height lines. */
1168 if (b != current_buffer)
1170 old_buffer = current_buffer;
1171 set_buffer_internal (b);
1174 /* In case W->start is out of the range, use something
1175 reasonable. This situation occurred when loading a file with
1176 `-l' containing a call to `rmail' with subsequent other
1177 commands. At the end, W->start happened to be BEG, while
1178 rmail had already narrowed the buffer. */
1179 if (XMARKER (w->start)->charpos < BEGV)
1180 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1181 else if (XMARKER (w->start)->charpos > ZV)
1182 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1183 else
1184 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1186 start_display (&it, w, startp);
1187 move_it_vertically (&it, window_box_height (w));
1188 if (it.current_y < it.last_visible_y)
1189 move_it_past_eol (&it);
1190 value = make_number (IT_CHARPOS (it));
1192 if (old_buffer)
1193 set_buffer_internal (old_buffer);
1195 else
1196 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1198 return value;
1201 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1202 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1203 Return POS. */)
1204 (Lisp_Object window, Lisp_Object pos)
1206 register struct window *w = decode_window (window);
1208 CHECK_NUMBER_COERCE_MARKER (pos);
1209 if (w == XWINDOW (selected_window)
1210 && XBUFFER (w->buffer) == current_buffer)
1211 Fgoto_char (pos);
1212 else
1213 set_marker_restricted (w->pointm, pos, w->buffer);
1215 /* We have to make sure that redisplay updates the window to show
1216 the new value of point. */
1217 if (!EQ (window, selected_window))
1218 ++windows_or_buffers_changed;
1220 return pos;
1223 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1224 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1225 WINDOW defaults to the selected window. Return POS.
1226 Optional third arg NOFORCE non-nil inhibits next redisplay from
1227 overriding motion of point in order to display at this exact start. */)
1228 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1230 register struct window *w = decode_window (window);
1232 CHECK_NUMBER_COERCE_MARKER (pos);
1233 set_marker_restricted (w->start, pos, w->buffer);
1234 /* this is not right, but much easier than doing what is right. */
1235 w->start_at_line_beg = Qnil;
1236 if (NILP (noforce))
1237 w->force_start = Qt;
1238 w->update_mode_line = Qt;
1239 XSETFASTINT (w->last_modified, 0);
1240 XSETFASTINT (w->last_overlay_modified, 0);
1241 if (!EQ (window, selected_window))
1242 windows_or_buffers_changed++;
1244 return pos;
1248 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1249 0, 1, 0,
1250 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1251 More precisely, return the value assigned by the last call of
1252 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1253 never called with WINDOW as its argument, or the value set by that
1254 function was internally reset since its last call. WINDOW defaults to
1255 the selected window.
1257 When a window is dedicated to its buffer, `display-buffer' will refrain
1258 from displaying another buffer in it. `get-lru-window' and
1259 `get-largest-window' treat dedicated windows specially.
1260 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1261 `kill-buffer' can delete a dedicated window and the containing frame.
1263 Functions like `set-window-buffer' may change the buffer displayed by a
1264 window, unless that window is "strongly" dedicated to its buffer, that
1265 is the value returned by `window-dedicated-p' is t. */)
1266 (Lisp_Object window)
1268 return decode_window (window)->dedicated;
1271 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1272 Sset_window_dedicated_p, 2, 2, 0,
1273 doc: /* Mark WINDOW as dedicated according to FLAG.
1274 WINDOW defaults to the selected window. FLAG non-nil means mark WINDOW
1275 as dedicated to its buffer. FLAG nil means mark WINDOW as non-dedicated.
1276 Return FLAG.
1278 When a window is dedicated to its buffer, `display-buffer' will refrain
1279 from displaying another buffer in it. `get-lru-window' and
1280 `get-largest-window' treat dedicated windows specially.
1281 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1282 `kill-buffer' can delete a dedicated window and the containing
1283 frame.
1285 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1286 its buffer. Functions like `set-window-buffer' may change the buffer
1287 displayed by a window, unless that window is strongly dedicated to its
1288 buffer. If and when `set-window-buffer' displays another buffer in a
1289 window, it also makes sure that the window is not marked as dedicated. */)
1290 (Lisp_Object window, Lisp_Object flag)
1292 register struct window *w = decode_window (window);
1294 w->dedicated = flag;
1295 return w->dedicated;
1299 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1300 0, 1, 0,
1301 doc: /* Return the parameters of WINDOW and their values.
1302 WINDOW defaults to the selected window. The return value is a list of
1303 elements of the form (PARAMETER . VALUE). */)
1304 (Lisp_Object window)
1306 return Fcopy_alist (decode_window (window)->window_parameters);
1309 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1310 2, 2, 0,
1311 doc: /* Return WINDOW's value for PARAMETER.
1312 WINDOW defaults to the selected window. */)
1313 (Lisp_Object window, Lisp_Object parameter)
1315 Lisp_Object result;
1317 result = Fassq (parameter, decode_window (window)->window_parameters);
1318 return CDR_SAFE (result);
1321 DEFUN ("set-window-parameter", Fset_window_parameter,
1322 Sset_window_parameter, 3, 3, 0,
1323 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1324 WINDOW defaults to the selected window. Return VALUE. */)
1325 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1327 register struct window *w = decode_window (window);
1328 Lisp_Object old_alist_elt;
1330 old_alist_elt = Fassq (parameter, w->window_parameters);
1331 if (NILP (old_alist_elt))
1332 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1333 else
1334 Fsetcdr (old_alist_elt, value);
1335 return value;
1339 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1340 0, 1, 0,
1341 doc: /* Return the display-table that WINDOW is using.
1342 WINDOW defaults to the selected window. */)
1343 (Lisp_Object window)
1345 return decode_window (window)->display_table;
1348 /* Get the display table for use on window W. This is either W's
1349 display table or W's buffer's display table. Ignore the specified
1350 tables if they are not valid; if no valid table is specified,
1351 return 0. */
1353 struct Lisp_Char_Table *
1354 window_display_table (struct window *w)
1356 struct Lisp_Char_Table *dp = NULL;
1358 if (DISP_TABLE_P (w->display_table))
1359 dp = XCHAR_TABLE (w->display_table);
1360 else if (BUFFERP (w->buffer))
1362 struct buffer *b = XBUFFER (w->buffer);
1364 if (DISP_TABLE_P (BVAR (b, display_table)))
1365 dp = XCHAR_TABLE (BVAR (b, display_table));
1366 else if (DISP_TABLE_P (Vstandard_display_table))
1367 dp = XCHAR_TABLE (Vstandard_display_table);
1370 return dp;
1373 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1374 doc: /* Set WINDOW's display-table to TABLE. */)
1375 (register Lisp_Object window, Lisp_Object table)
1377 register struct window *w;
1379 w = decode_window (window);
1380 w->display_table = table;
1381 return table;
1384 static void delete_window (Lisp_Object);
1386 /* Record info on buffer window w is displaying
1387 when it is about to cease to display that buffer. */
1388 static void
1389 unshow_buffer (register struct window *w)
1391 Lisp_Object buf;
1392 struct buffer *b;
1394 buf = w->buffer;
1395 b = XBUFFER (buf);
1396 if (b != XMARKER (w->pointm)->buffer)
1397 abort ();
1399 #if 0
1400 if (w == XWINDOW (selected_window)
1401 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1402 /* Do this except when the selected window's buffer
1403 is being removed from some other window. */
1404 #endif
1405 /* last_window_start records the start position that this buffer
1406 had in the last window to be disconnected from it.
1407 Now that this statement is unconditional,
1408 it is possible for the buffer to be displayed in the
1409 selected window, while last_window_start reflects another
1410 window which was recently showing the same buffer.
1411 Some people might say that might be a good thing. Let's see. */
1412 b->last_window_start = marker_position (w->start);
1414 /* Point in the selected window's buffer
1415 is actually stored in that buffer, and the window's pointm isn't used.
1416 So don't clobber point in that buffer. */
1417 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1418 /* This line helps to fix Horsley's testbug.el bug. */
1419 && !(WINDOWP (BVAR (b, last_selected_window))
1420 && w != XWINDOW (BVAR (b, last_selected_window))
1421 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1422 temp_set_point_both (b,
1423 clip_to_bounds (BUF_BEGV (b),
1424 XMARKER (w->pointm)->charpos,
1425 BUF_ZV (b)),
1426 clip_to_bounds (BUF_BEGV_BYTE (b),
1427 marker_byte_position (w->pointm),
1428 BUF_ZV_BYTE (b)));
1430 if (WINDOWP (BVAR (b, last_selected_window))
1431 && w == XWINDOW (BVAR (b, last_selected_window)))
1432 BVAR (b, last_selected_window) = Qnil;
1435 /* Put replacement into the window structure in place of old. */
1436 static void
1437 replace_window (Lisp_Object old, Lisp_Object replacement)
1439 register Lisp_Object tem;
1440 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1442 /* If OLD is its frame's root_window, then replacement is the new
1443 root_window for that frame. */
1445 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1446 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1448 p->left_col = o->left_col;
1449 p->top_line = o->top_line;
1450 p->total_cols = o->total_cols;
1451 p->total_lines = o->total_lines;
1452 p->desired_matrix = p->current_matrix = 0;
1453 p->vscroll = 0;
1454 memset (&p->cursor, 0, sizeof (p->cursor));
1455 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
1456 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
1457 p->phys_cursor_type = -1;
1458 p->phys_cursor_width = -1;
1459 p->must_be_updated_p = 0;
1460 p->pseudo_window_p = 0;
1461 XSETFASTINT (p->window_end_vpos, 0);
1462 XSETFASTINT (p->window_end_pos, 0);
1463 p->window_end_valid = Qnil;
1464 p->frozen_window_start_p = 0;
1465 p->orig_top_line = p->orig_total_lines = Qnil;
1467 p->next = tem = o->next;
1468 if (!NILP (tem))
1469 XWINDOW (tem)->prev = replacement;
1471 p->prev = tem = o->prev;
1472 if (!NILP (tem))
1473 XWINDOW (tem)->next = replacement;
1475 p->parent = tem = o->parent;
1476 if (!NILP (tem))
1478 if (EQ (XWINDOW (tem)->vchild, old))
1479 XWINDOW (tem)->vchild = replacement;
1480 if (EQ (XWINDOW (tem)->hchild, old))
1481 XWINDOW (tem)->hchild = replacement;
1484 /*** Here, if replacement is a vertical combination
1485 and so is its new parent, we should make replacement's
1486 children be children of that parent instead. ***/
1489 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1490 doc: /* Remove WINDOW from its frame.
1491 WINDOW defaults to the selected window. Return nil.
1492 Signal an error when WINDOW is the only window on its frame. */)
1493 (register Lisp_Object window)
1495 struct frame *f;
1496 if (NILP (window))
1497 window = selected_window;
1498 else
1499 CHECK_LIVE_WINDOW (window);
1501 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1502 delete_window (window);
1504 run_window_configuration_change_hook (f);
1506 return Qnil;
1509 static void
1510 delete_window (register Lisp_Object window)
1512 register Lisp_Object tem, parent, sib;
1513 register struct window *p;
1514 register struct window *par;
1515 struct frame *f;
1517 /* Because this function is called by other C code on non-leaf
1518 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1519 so we can't decode_window here. */
1520 CHECK_WINDOW (window);
1521 p = XWINDOW (window);
1523 /* It's a no-op to delete an already-deleted window. */
1524 if (NILP (p->buffer)
1525 && NILP (p->hchild)
1526 && NILP (p->vchild))
1527 return;
1529 parent = p->parent;
1530 if (NILP (parent))
1531 error ("Attempt to delete minibuffer or sole ordinary window");
1532 par = XWINDOW (parent);
1534 windows_or_buffers_changed++;
1535 Vwindow_list = Qnil;
1536 f = XFRAME (WINDOW_FRAME (p));
1537 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1539 /* Are we trying to delete any frame's selected window? */
1541 Lisp_Object swindow, pwindow;
1543 /* See if the frame's selected window is either WINDOW
1544 or any subwindow of it, by finding all that window's parents
1545 and comparing each one with WINDOW. */
1546 swindow = FRAME_SELECTED_WINDOW (f);
1548 while (1)
1550 pwindow = swindow;
1551 while (!NILP (pwindow))
1553 if (EQ (window, pwindow))
1554 break;
1555 pwindow = XWINDOW (pwindow)->parent;
1558 /* If the window being deleted is not a parent of SWINDOW,
1559 then SWINDOW is ok as the new selected window. */
1560 if (!EQ (window, pwindow))
1561 break;
1562 /* Otherwise, try another window for SWINDOW. */
1563 swindow = Fnext_window (swindow, Qlambda, Qnil);
1565 /* If we get back to the frame's selected window,
1566 it means there was no acceptable alternative,
1567 so we cannot delete. */
1568 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1569 error ("Cannot delete window");
1572 /* If we need to change SWINDOW, do it. */
1573 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1575 /* If we're about to delete the selected window on the
1576 selected frame, then we should use Fselect_window to select
1577 the new window. On the other hand, if we're about to
1578 delete the selected window on any other frame, we shouldn't do
1579 anything but set the frame's selected_window slot. */
1580 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1581 Fselect_window (swindow, Qnil);
1582 else
1583 FRAME_SELECTED_WINDOW (f) = swindow;
1587 /* Now we know we can delete this one. */
1588 window_deletion_count++;
1590 tem = p->buffer;
1591 /* tem is null for dummy parent windows
1592 (which have inferiors but not any contents themselves) */
1593 if (!NILP (tem))
1595 unshow_buffer (p);
1596 unchain_marker (XMARKER (p->pointm));
1597 unchain_marker (XMARKER (p->start));
1600 /* Free window glyph matrices. It is sure that they are allocated
1601 again when ADJUST_GLYPHS is called. Block input so that expose
1602 events and other events that access glyph matrices are not
1603 processed while we are changing them. */
1604 BLOCK_INPUT;
1605 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1607 tem = p->next;
1608 if (!NILP (tem))
1609 XWINDOW (tem)->prev = p->prev;
1611 tem = p->prev;
1612 if (!NILP (tem))
1613 XWINDOW (tem)->next = p->next;
1615 if (EQ (window, par->hchild))
1616 par->hchild = p->next;
1617 if (EQ (window, par->vchild))
1618 par->vchild = p->next;
1620 /* Find one of our siblings to give our space to. */
1621 sib = p->prev;
1622 if (NILP (sib))
1624 /* If p gives its space to its next sibling, that sibling needs
1625 to have its top/left side pulled back to where p's is.
1626 set_window_{height,width} will re-position the sibling's
1627 children. */
1628 sib = p->next;
1629 XWINDOW (sib)->top_line = p->top_line;
1630 XWINDOW (sib)->left_col = p->left_col;
1633 /* Stretch that sibling. */
1634 if (!NILP (par->vchild))
1635 set_window_height (sib,
1636 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1638 if (!NILP (par->hchild))
1639 set_window_width (sib,
1640 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1643 /* If parent now has only one child,
1644 put the child into the parent's place. */
1645 tem = par->hchild;
1646 if (NILP (tem))
1647 tem = par->vchild;
1648 if (NILP (XWINDOW (tem)->next)) {
1649 replace_window (parent, tem);
1650 par = XWINDOW (tem);
1653 /* Since we may be deleting combination windows, we must make sure that
1654 not only p but all its children have been marked as deleted. */
1655 if (! NILP (p->hchild))
1656 delete_all_subwindows (XWINDOW (p->hchild));
1657 else if (! NILP (p->vchild))
1658 delete_all_subwindows (XWINDOW (p->vchild));
1660 /* Mark this window as deleted. */
1661 p->buffer = p->hchild = p->vchild = Qnil;
1663 if (! NILP (par->parent))
1664 par = XWINDOW (par->parent);
1666 /* Check if we have a v/hchild with a v/hchild. In that case remove
1667 one of them. */
1669 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1671 p = XWINDOW (par->vchild);
1672 par->vchild = p->vchild;
1673 tem = p->vchild;
1675 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1677 p = XWINDOW (par->hchild);
1678 par->hchild = p->hchild;
1679 tem = p->hchild;
1681 else
1682 p = 0;
1684 if (p)
1686 while (! NILP (tem)) {
1687 XWINDOW (tem)->parent = p->parent;
1688 if (NILP (XWINDOW (tem)->next))
1689 break;
1690 tem = XWINDOW (tem)->next;
1692 if (! NILP (tem)) {
1693 /* The next of the v/hchild we are removing is now the next of the
1694 last child for the v/hchild:
1695 Before v/hchild -> v/hchild -> next1 -> next2
1697 -> next3
1698 After: v/hchild -> next1 -> next2 -> next3
1700 XWINDOW (tem)->next = p->next;
1701 if (! NILP (p->next))
1702 XWINDOW (p->next)->prev = tem;
1704 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1708 /* Adjust glyph matrices. */
1709 adjust_glyphs (f);
1710 UNBLOCK_INPUT;
1715 /***********************************************************************
1716 Window List
1717 ***********************************************************************/
1719 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1720 pointer. This is a callback function for foreach_window, used in
1721 function window_list. */
1723 static int
1724 add_window_to_list (struct window *w, void *user_data)
1726 Lisp_Object *list = (Lisp_Object *) user_data;
1727 Lisp_Object window;
1728 XSETWINDOW (window, w);
1729 *list = Fcons (window, *list);
1730 return 1;
1734 /* Return a list of all windows, for use by next_window. If
1735 Vwindow_list is a list, return that list. Otherwise, build a new
1736 list, cache it in Vwindow_list, and return that. */
1738 static Lisp_Object
1739 window_list (void)
1741 if (!CONSP (Vwindow_list))
1743 Lisp_Object tail;
1745 Vwindow_list = Qnil;
1746 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1748 Lisp_Object args[2];
1750 /* We are visiting windows in canonical order, and add
1751 new windows at the front of args[1], which means we
1752 have to reverse this list at the end. */
1753 args[1] = Qnil;
1754 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1755 args[0] = Vwindow_list;
1756 args[1] = Fnreverse (args[1]);
1757 Vwindow_list = Fnconc (2, args);
1761 return Vwindow_list;
1765 /* Value is non-zero if WINDOW satisfies the constraints given by
1766 OWINDOW, MINIBUF and ALL_FRAMES.
1768 MINIBUF t means WINDOW may be minibuffer windows.
1769 `lambda' means WINDOW may not be a minibuffer window.
1770 a window means a specific minibuffer window
1772 ALL_FRAMES t means search all frames,
1773 nil means search just current frame,
1774 `visible' means search just visible frames on the
1775 current terminal,
1776 0 means search visible and iconified frames on the
1777 current terminal,
1778 a window means search the frame that window belongs to,
1779 a frame means consider windows on that frame, only. */
1781 static int
1782 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
1784 struct window *w = XWINDOW (window);
1785 struct frame *f = XFRAME (w->frame);
1786 int candidate_p = 1;
1788 if (!BUFFERP (w->buffer))
1789 candidate_p = 0;
1790 else if (MINI_WINDOW_P (w)
1791 && (EQ (minibuf, Qlambda)
1792 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1794 /* If MINIBUF is `lambda' don't consider any mini-windows.
1795 If it is a window, consider only that one. */
1796 candidate_p = 0;
1798 else if (EQ (all_frames, Qt))
1799 candidate_p = 1;
1800 else if (NILP (all_frames))
1802 xassert (WINDOWP (owindow));
1803 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1805 else if (EQ (all_frames, Qvisible))
1807 FRAME_SAMPLE_VISIBILITY (f);
1808 candidate_p = FRAME_VISIBLE_P (f)
1809 && (FRAME_TERMINAL (XFRAME (w->frame))
1810 == FRAME_TERMINAL (XFRAME (selected_frame)));
1813 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1815 FRAME_SAMPLE_VISIBILITY (f);
1816 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
1817 #ifdef HAVE_X_WINDOWS
1818 /* Yuck!! If we've just created the frame and the
1819 window-manager requested the user to place it
1820 manually, the window may still not be considered
1821 `visible'. I'd argue it should be at least
1822 something like `iconified', but don't know how to do
1823 that yet. --Stef */
1824 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
1825 && !f->output_data.x->has_been_visible)
1826 #endif
1828 && (FRAME_TERMINAL (XFRAME (w->frame))
1829 == FRAME_TERMINAL (XFRAME (selected_frame)));
1831 else if (WINDOWP (all_frames))
1832 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1833 || EQ (XWINDOW (all_frames)->frame, w->frame)
1834 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1835 else if (FRAMEP (all_frames))
1836 candidate_p = EQ (all_frames, w->frame);
1838 return candidate_p;
1842 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1843 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
1844 MINIBUF, and ALL_FRAMES. */
1846 static void
1847 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
1849 if (NILP (*window))
1850 *window = selected_window;
1851 else
1852 CHECK_LIVE_WINDOW (*window);
1854 /* MINIBUF nil may or may not include minibuffers. Decide if it
1855 does. */
1856 if (NILP (*minibuf))
1857 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1858 else if (!EQ (*minibuf, Qt))
1859 *minibuf = Qlambda;
1861 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1862 => count none of them, or a specific minibuffer window (the
1863 active one) to count. */
1865 /* ALL_FRAMES nil doesn't specify which frames to include. */
1866 if (NILP (*all_frames))
1867 *all_frames = (!EQ (*minibuf, Qlambda)
1868 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1869 : Qnil);
1870 else if (EQ (*all_frames, Qvisible))
1872 else if (EQ (*all_frames, make_number (0)))
1874 else if (FRAMEP (*all_frames))
1876 else if (!EQ (*all_frames, Qt))
1877 *all_frames = Qnil;
1881 /* Return the next or previous window of WINDOW in cyclic ordering
1882 of windows. NEXT_P non-zero means return the next window. See the
1883 documentation string of next-window for the meaning of MINIBUF and
1884 ALL_FRAMES. */
1886 static Lisp_Object
1887 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
1889 decode_next_window_args (&window, &minibuf, &all_frames);
1891 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1892 return the first window on the frame. */
1893 if (FRAMEP (all_frames)
1894 && !EQ (all_frames, XWINDOW (window)->frame))
1895 return Fframe_first_window (all_frames);
1897 if (next_p)
1899 Lisp_Object list;
1901 /* Find WINDOW in the list of all windows. */
1902 list = Fmemq (window, window_list ());
1904 /* Scan forward from WINDOW to the end of the window list. */
1905 if (CONSP (list))
1906 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1907 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1908 break;
1910 /* Scan from the start of the window list up to WINDOW. */
1911 if (!CONSP (list))
1912 for (list = Vwindow_list;
1913 CONSP (list) && !EQ (XCAR (list), window);
1914 list = XCDR (list))
1915 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1916 break;
1918 if (CONSP (list))
1919 window = XCAR (list);
1921 else
1923 Lisp_Object candidate, list;
1925 /* Scan through the list of windows for candidates. If there are
1926 candidate windows in front of WINDOW, the last one of these
1927 is the one we want. If there are candidates following WINDOW
1928 in the list, again the last one of these is the one we want. */
1929 candidate = Qnil;
1930 for (list = window_list (); CONSP (list); list = XCDR (list))
1932 if (EQ (XCAR (list), window))
1934 if (WINDOWP (candidate))
1935 break;
1937 else if (candidate_window_p (XCAR (list), window, minibuf,
1938 all_frames))
1939 candidate = XCAR (list);
1942 if (WINDOWP (candidate))
1943 window = candidate;
1946 return window;
1950 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1951 doc: /* Return window following WINDOW in cyclic ordering of windows.
1952 WINDOW defaults to the selected window. The optional arguments
1953 MINIBUF and ALL-FRAMES specify the set of windows to consider.
1955 MINIBUF t means consider the minibuffer window even if the
1956 minibuffer is not active. MINIBUF nil or omitted means consider
1957 the minibuffer window only if the minibuffer is active. Any
1958 other value means do not consider the minibuffer window even if
1959 the minibuffer is active.
1961 Several frames may share a single minibuffer; if the minibuffer
1962 is active, all windows on all frames that share that minibuffer
1963 are considered too. Therefore, if you are using a separate
1964 minibuffer frame and the minibuffer is active and MINIBUF says it
1965 counts, `next-window' considers the windows in the frame from
1966 which you entered the minibuffer, as well as the minibuffer
1967 window.
1969 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
1970 frame, plus the minibuffer window if specified by the MINIBUF
1971 argument, see above. If the minibuffer counts, consider all
1972 windows on all frames that share that minibuffer too.
1973 ALL-FRAMES t means consider all windows on all existing frames.
1974 ALL-FRAMES `visible' means consider all windows on all visible
1975 frames on the current terminal.
1976 ALL-FRAMES 0 means consider all windows on all visible and
1977 iconified frames on the current terminal.
1978 ALL-FRAMES a frame means consider all windows on that frame only.
1979 Anything else means consider all windows on WINDOW's frame and no
1980 others.
1982 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1983 `next-window' to iterate through the entire cycle of acceptable
1984 windows, eventually ending up back at the window you started with.
1985 `previous-window' traverses the same cycle, in the reverse order. */)
1986 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
1988 return next_window (window, minibuf, all_frames, 1);
1992 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1993 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
1994 WINDOW defaults to the selected window. The optional arguments
1995 MINIBUF and ALL-FRAMES specify the set of windows to consider.
1996 For the precise meaning of these arguments see `next-window'.
1998 If you use consistent values for MINIBUF and ALL-FRAMES, you can
1999 use `previous-window' to iterate through the entire cycle of
2000 acceptable windows, eventually ending up back at the window you
2001 started with. `next-window' traverses the same cycle, in the
2002 reverse order. */)
2003 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2005 return next_window (window, minibuf, all_frames, 0);
2009 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
2010 doc: /* Select another window in cyclic ordering of windows.
2011 COUNT specifies the number of windows to skip, starting with the
2012 selected window, before making the selection. If COUNT is
2013 positive, skip COUNT windows forwards. If COUNT is negative,
2014 skip -COUNT windows backwards. COUNT zero means do not skip any
2015 window, so select the selected window. In an interactive call,
2016 COUNT is the numeric prefix argument. Return nil.
2018 This function uses `next-window' for finding the window to select.
2019 The argument ALL-FRAMES has the same meaning as in `next-window',
2020 but the MINIBUF argument of `next-window' is always effectively
2021 nil. */)
2022 (Lisp_Object count, Lisp_Object all_frames)
2024 Lisp_Object window;
2025 int i;
2027 CHECK_NUMBER (count);
2028 window = selected_window;
2030 for (i = XINT (count); i > 0; --i)
2031 window = Fnext_window (window, Qnil, all_frames);
2032 for (; i < 0; ++i)
2033 window = Fprevious_window (window, Qnil, all_frames);
2035 Fselect_window (window, Qnil);
2036 return Qnil;
2040 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2041 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2042 FRAME nil or omitted means use the selected frame.
2043 WINDOW nil or omitted means use the selected window.
2044 MINIBUF t means include the minibuffer window, even if it isn't active.
2045 MINIBUF nil or omitted means include the minibuffer window only
2046 if it's active.
2047 MINIBUF neither nil nor t means never include the minibuffer window. */)
2048 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2050 if (NILP (window))
2051 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2052 CHECK_WINDOW (window);
2053 if (NILP (frame))
2054 frame = selected_frame;
2056 if (!EQ (frame, XWINDOW (window)->frame))
2057 error ("Window is on a different frame");
2059 return window_list_1 (window, minibuf, frame);
2063 /* Return a list of windows in cyclic ordering. Arguments are like
2064 for `next-window'. */
2066 static Lisp_Object
2067 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2069 Lisp_Object tail, list, rest;
2071 decode_next_window_args (&window, &minibuf, &all_frames);
2072 list = Qnil;
2074 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2075 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2076 list = Fcons (XCAR (tail), list);
2078 /* Rotate the list to start with WINDOW. */
2079 list = Fnreverse (list);
2080 rest = Fmemq (window, list);
2081 if (!NILP (rest) && !EQ (rest, list))
2083 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2085 XSETCDR (tail, Qnil);
2086 list = nconc2 (rest, list);
2088 return list;
2093 /* Look at all windows, performing an operation specified by TYPE
2094 with argument OBJ.
2095 If FRAMES is Qt, look at all frames;
2096 Qnil, look at just the selected frame;
2097 Qvisible, look at visible frames;
2098 a frame, just look at windows on that frame.
2099 If MINI is non-zero, perform the operation on minibuffer windows too. */
2101 enum window_loop
2103 WINDOW_LOOP_UNUSED,
2104 GET_BUFFER_WINDOW, /* Arg is buffer */
2105 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2106 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2107 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2108 GET_LARGEST_WINDOW,
2109 UNSHOW_BUFFER, /* Arg is buffer */
2110 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2111 CHECK_ALL_WINDOWS
2114 static Lisp_Object
2115 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2117 Lisp_Object window, windows, best_window, frame_arg;
2118 struct frame *f;
2119 struct gcpro gcpro1;
2121 /* If we're only looping through windows on a particular frame,
2122 frame points to that frame. If we're looping through windows
2123 on all frames, frame is 0. */
2124 if (FRAMEP (frames))
2125 f = XFRAME (frames);
2126 else if (NILP (frames))
2127 f = SELECTED_FRAME ();
2128 else
2129 f = NULL;
2131 if (f)
2132 frame_arg = Qlambda;
2133 else if (EQ (frames, make_number (0)))
2134 frame_arg = frames;
2135 else if (EQ (frames, Qvisible))
2136 frame_arg = frames;
2137 else
2138 frame_arg = Qt;
2140 /* frame_arg is Qlambda to stick to one frame,
2141 Qvisible to consider all visible frames,
2142 or Qt otherwise. */
2144 /* Pick a window to start with. */
2145 if (WINDOWP (obj))
2146 window = obj;
2147 else if (f)
2148 window = FRAME_SELECTED_WINDOW (f);
2149 else
2150 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2152 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2153 GCPRO1 (windows);
2154 best_window = Qnil;
2156 for (; CONSP (windows); windows = XCDR (windows))
2158 struct window *w;
2160 window = XCAR (windows);
2161 w = XWINDOW (window);
2163 /* Note that we do not pay attention here to whether the frame
2164 is visible, since Fwindow_list skips non-visible frames if
2165 that is desired, under the control of frame_arg. */
2166 if (!MINI_WINDOW_P (w)
2167 /* For UNSHOW_BUFFER, we must always consider all windows. */
2168 || type == UNSHOW_BUFFER
2169 || (mini && minibuf_level > 0))
2170 switch (type)
2172 case GET_BUFFER_WINDOW:
2173 if (EQ (w->buffer, obj)
2174 /* Don't find any minibuffer window
2175 except the one that is currently in use. */
2176 && (MINI_WINDOW_P (w)
2177 ? EQ (window, minibuf_window)
2178 : 1))
2180 if (NILP (best_window))
2181 best_window = window;
2182 else if (EQ (window, selected_window))
2183 /* Prefer to return selected-window. */
2184 RETURN_UNGCPRO (window);
2185 else if (EQ (Fwindow_frame (window), selected_frame))
2186 /* Prefer windows on the current frame. */
2187 best_window = window;
2189 break;
2191 case GET_LRU_WINDOW:
2192 /* `obj' is an integer encoding a bitvector.
2193 `obj & 1' means consider only full-width windows.
2194 `obj & 2' means consider also dedicated windows. */
2195 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2196 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2197 /* Minibuffer windows are always ignored. */
2198 || MINI_WINDOW_P (w))
2199 break;
2200 if (NILP (best_window)
2201 || (XFASTINT (XWINDOW (best_window)->use_time)
2202 > XFASTINT (w->use_time)))
2203 best_window = window;
2204 break;
2206 case DELETE_OTHER_WINDOWS:
2207 if (!EQ (window, obj))
2208 Fdelete_window (window);
2209 break;
2211 case DELETE_BUFFER_WINDOWS:
2212 if (EQ (w->buffer, obj))
2214 struct frame *fr = XFRAME (WINDOW_FRAME (w));
2216 /* If this window is dedicated, and in a frame of its own,
2217 kill the frame. */
2218 if (EQ (window, FRAME_ROOT_WINDOW (fr))
2219 && !NILP (w->dedicated)
2220 && other_visible_frames (fr))
2222 /* Skip the other windows on this frame.
2223 There might be one, the minibuffer! */
2224 while (CONSP (XCDR (windows))
2225 && EQ (XWINDOW (XCAR (windows))->frame,
2226 XWINDOW (XCAR (XCDR (windows)))->frame))
2227 windows = XCDR (windows);
2229 /* Now we can safely delete the frame. */
2230 delete_frame (w->frame, Qnil);
2232 else if (NILP (w->parent))
2234 /* If we're deleting the buffer displayed in the
2235 only window on the frame, find a new buffer to
2236 display there. */
2237 Lisp_Object buffer;
2238 buffer = Fother_buffer (obj, Qnil, w->frame);
2239 /* Reset dedicated state of window. */
2240 w->dedicated = Qnil;
2241 Fset_window_buffer (window, buffer, Qnil);
2242 if (EQ (window, selected_window))
2243 Fset_buffer (w->buffer);
2245 else
2246 Fdelete_window (window);
2248 break;
2250 case GET_LARGEST_WINDOW:
2251 { /* nil `obj' means to ignore dedicated windows. */
2252 /* Ignore dedicated windows and minibuffers. */
2253 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2254 break;
2256 if (NILP (best_window))
2257 best_window = window;
2258 else
2260 struct window *b = XWINDOW (best_window);
2261 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2262 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2263 best_window = window;
2266 break;
2268 case UNSHOW_BUFFER:
2269 if (EQ (w->buffer, obj))
2271 Lisp_Object buffer;
2272 struct frame *fr = XFRAME (w->frame);
2274 /* Find another buffer to show in this window. */
2275 buffer = Fother_buffer (obj, Qnil, w->frame);
2277 /* If this window is dedicated, and in a frame of its own,
2278 kill the frame. */
2279 if (EQ (window, FRAME_ROOT_WINDOW (fr))
2280 && !NILP (w->dedicated)
2281 && other_visible_frames (fr))
2283 /* Skip the other windows on this frame.
2284 There might be one, the minibuffer! */
2285 while (CONSP (XCDR (windows))
2286 && EQ (XWINDOW (XCAR (windows))->frame,
2287 XWINDOW (XCAR (XCDR (windows)))->frame))
2288 windows = XCDR (windows);
2290 /* Now we can safely delete the frame. */
2291 delete_frame (w->frame, Qnil);
2293 else if (!NILP (w->dedicated) && !NILP (w->parent))
2295 Lisp_Object window_to_delete;
2296 XSETWINDOW (window_to_delete, w);
2297 /* If this window is dedicated and not the only window
2298 in its frame, then kill it. */
2299 Fdelete_window (window_to_delete);
2301 else
2303 /* Otherwise show a different buffer in the window. */
2304 w->dedicated = Qnil;
2305 Fset_window_buffer (window, buffer, Qnil);
2306 if (EQ (window, selected_window))
2307 Fset_buffer (w->buffer);
2310 break;
2312 case REDISPLAY_BUFFER_WINDOWS:
2313 if (EQ (w->buffer, obj))
2315 mark_window_display_accurate (window, 0);
2316 w->update_mode_line = Qt;
2317 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2318 ++update_mode_lines;
2319 best_window = window;
2321 break;
2323 /* Check for a window that has a killed buffer. */
2324 case CHECK_ALL_WINDOWS:
2325 if (! NILP (w->buffer)
2326 && NILP (BVAR (XBUFFER (w->buffer), name)))
2327 abort ();
2328 break;
2330 case WINDOW_LOOP_UNUSED:
2331 break;
2335 UNGCPRO;
2336 return best_window;
2339 /* Used for debugging. Abort if any window has a dead buffer. */
2341 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2342 void
2343 check_all_windows (void)
2345 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2348 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
2349 doc: /* Return WINDOW's use time.
2350 WINDOW defaults to the selected window. The window with the highest use
2351 time is the most recently selected one. The window with the lowest use
2352 time is the least recently selected one. */)
2353 (Lisp_Object window)
2355 return decode_window (window)->use_time;
2358 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2359 doc: /* Return the window least recently selected or used for display.
2360 \(LRU means Least Recently Used.)
2362 Return a full-width window if possible.
2363 A minibuffer window is never a candidate.
2364 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2365 so if all windows are dedicated, the value is nil.
2366 If optional argument FRAME is `visible', search all visible frames.
2367 If FRAME is 0, search all visible and iconified frames.
2368 If FRAME is t, search all frames.
2369 If FRAME is nil, search only the selected frame.
2370 If FRAME is a frame, search only that frame. */)
2371 (Lisp_Object frame, Lisp_Object dedicated)
2373 register Lisp_Object w;
2374 /* First try for a window that is full-width */
2375 w = window_loop (GET_LRU_WINDOW,
2376 NILP (dedicated) ? make_number (1) : make_number (3),
2377 0, frame);
2378 if (!NILP (w) && !EQ (w, selected_window))
2379 return w;
2380 /* If none of them, try the rest */
2381 return window_loop (GET_LRU_WINDOW,
2382 NILP (dedicated) ? make_number (0) : make_number (2),
2383 0, frame);
2386 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2387 doc: /* Return the largest window in area.
2388 A minibuffer window is never a candidate.
2389 A dedicated window is never a candidate unless DEDICATED is non-nil,
2390 so if all windows are dedicated, the value is nil.
2391 If optional argument FRAME is `visible', search all visible frames.
2392 If FRAME is 0, search all visible and iconified frames.
2393 If FRAME is t, search all frames.
2394 If FRAME is nil, search only the selected frame.
2395 If FRAME is a frame, search only that frame. */)
2396 (Lisp_Object frame, Lisp_Object dedicated)
2398 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2399 frame);
2402 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2403 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2404 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to the
2405 current buffer.
2406 If optional argument FRAME is `visible', search all visible frames.
2407 If optional argument FRAME is 0, search all visible and iconified frames.
2408 If FRAME is t, search all frames.
2409 If FRAME is nil, search only the selected frame.
2410 If FRAME is a frame, search only that frame. */)
2411 (Lisp_Object buffer_or_name, Lisp_Object frame)
2413 Lisp_Object buffer;
2415 if (NILP (buffer_or_name))
2416 buffer = Fcurrent_buffer ();
2417 else
2418 buffer = Fget_buffer (buffer_or_name);
2420 if (BUFFERP (buffer))
2421 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2422 else
2423 return Qnil;
2426 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2427 0, 1, "",
2428 doc: /* Make WINDOW (or the selected window) fill its frame.
2429 Only the frame WINDOW is on is affected.
2430 This function tries to reduce display jumps by keeping the text
2431 previously visible in WINDOW in the same place on the frame. Doing this
2432 depends on the value of (window-start WINDOW), so if calling this
2433 function in a program gives strange scrolling, make sure the
2434 window-start value is reasonable when this function is called. */)
2435 (Lisp_Object window)
2437 struct window *w;
2438 EMACS_INT startpos;
2439 int top, new_top;
2441 if (NILP (window))
2442 window = selected_window;
2443 else
2444 CHECK_LIVE_WINDOW (window);
2445 w = XWINDOW (window);
2447 startpos = marker_position (w->start);
2448 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2450 if (MINI_WINDOW_P (w) && top > 0)
2451 error ("Can't expand minibuffer to full frame");
2453 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2455 /* Try to minimize scrolling, by setting the window start to the point
2456 will cause the text at the old window start to be at the same place
2457 on the frame. But don't try to do this if the window start is
2458 outside the visible portion (as might happen when the display is
2459 not current, due to typeahead). */
2460 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2461 if (new_top != top
2462 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2463 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2465 struct position pos;
2466 struct buffer *obuf = current_buffer;
2468 Fset_buffer (w->buffer);
2469 /* This computation used to temporarily move point, but that can
2470 have unwanted side effects due to text properties. */
2471 pos = *vmotion (startpos, -top, w);
2473 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2474 w->window_end_valid = Qnil;
2475 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2476 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2477 : Qnil);
2478 /* We need to do this, so that the window-scroll-functions
2479 get called. */
2480 w->optional_new_start = Qt;
2482 set_buffer_internal (obuf);
2485 return Qnil;
2488 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2489 0, 2, "bDelete windows on (buffer): ",
2490 doc: /* Delete all windows showing BUFFER-OR-NAME.
2491 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2492 defaults to the current buffer.
2494 Optional second argument FRAME controls which frames are affected.
2495 If optional argument FRAME is `visible', search all visible frames.
2496 If FRAME is 0, search all visible and iconified frames.
2497 If FRAME is nil, search all frames.
2498 If FRAME is t, search only the selected frame.
2499 If FRAME is a frame, search only that frame.
2500 When a window showing BUFFER-OR-NAME is dedicated and the only window of
2501 its frame, that frame is deleted when there are other frames left. */)
2502 (Lisp_Object buffer_or_name, Lisp_Object frame)
2504 Lisp_Object buffer;
2506 /* FRAME uses t and nil to mean the opposite of what window_loop
2507 expects. */
2508 if (NILP (frame))
2509 frame = Qt;
2510 else if (EQ (frame, Qt))
2511 frame = Qnil;
2513 if (NILP (buffer_or_name))
2514 buffer = Fcurrent_buffer ();
2515 else
2517 buffer = Fget_buffer (buffer_or_name);
2518 CHECK_BUFFER (buffer);
2521 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2523 return Qnil;
2526 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2527 Sreplace_buffer_in_windows,
2528 0, 1, "bReplace buffer in windows: ",
2529 doc: /* Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2530 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2531 defaults to the current buffer.
2533 When a window showing BUFFER-OR-NAME is dedicated that window is
2534 deleted. If that window is the only window on its frame, that frame is
2535 deleted too when there are other frames left. If there are no other
2536 frames left, some other buffer is displayed in that window. */)
2537 (Lisp_Object buffer_or_name)
2539 Lisp_Object buffer;
2541 if (NILP (buffer_or_name))
2542 buffer = Fcurrent_buffer ();
2543 else
2545 buffer = Fget_buffer (buffer_or_name);
2546 CHECK_BUFFER (buffer);
2549 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2551 return Qnil;
2554 /* Replace BUFFER with some other buffer in all windows
2555 of all frames, even those on other keyboards. */
2557 void
2558 replace_buffer_in_all_windows (Lisp_Object buffer)
2560 Lisp_Object tail, frame;
2562 /* A single call to window_loop won't do the job
2563 because it only considers frames on the current keyboard.
2564 So loop manually over frames, and handle each one. */
2565 FOR_EACH_FRAME (tail, frame)
2566 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2569 /* Set the height of WINDOW and all its inferiors. */
2571 /* The smallest acceptable dimensions for a window. Anything smaller
2572 might crash Emacs. */
2574 #define MIN_SAFE_WINDOW_WIDTH (2)
2575 #define MIN_SAFE_WINDOW_HEIGHT (1)
2577 /* For wp non-zero the total number of columns of window w. Otherwise
2578 the total number of lines of w. */
2580 #define WINDOW_TOTAL_SIZE(w, wp) \
2581 (wp ? WINDOW_TOTAL_COLS (w) : WINDOW_TOTAL_LINES (w))
2583 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2584 minimum allowable size. */
2586 void
2587 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
2589 /* For height, we have to see:
2590 how many windows the frame has at minimum (one or two),
2591 and whether it has a menu bar or other special stuff at the top. */
2592 int min_height
2593 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2594 ? MIN_SAFE_WINDOW_HEIGHT
2595 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2597 if (FRAME_TOP_MARGIN (frame) > 0)
2598 min_height += FRAME_TOP_MARGIN (frame);
2600 if (*rows < min_height)
2601 *rows = min_height;
2602 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2603 *cols = MIN_SAFE_WINDOW_WIDTH;
2606 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2607 check if W's width can be changed, otherwise check W's height.
2608 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2609 siblings, too. If none of the siblings is resizable, WINDOW isn't
2610 either. */
2612 static int
2613 window_fixed_size_p (struct window *w, int width_p, int check_siblings_p)
2615 int fixed_p;
2616 struct window *c;
2618 if (!NILP (w->hchild))
2620 c = XWINDOW (w->hchild);
2622 if (width_p)
2624 /* A horizontal combination is fixed-width if all of if its
2625 children are. */
2626 while (c && window_fixed_size_p (c, width_p, 0))
2627 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2628 fixed_p = c == NULL;
2630 else
2632 /* A horizontal combination is fixed-height if one of if its
2633 children is. */
2634 while (c && !window_fixed_size_p (c, width_p, 0))
2635 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2636 fixed_p = c != NULL;
2639 else if (!NILP (w->vchild))
2641 c = XWINDOW (w->vchild);
2643 if (width_p)
2645 /* A vertical combination is fixed-width if one of if its
2646 children is. */
2647 while (c && !window_fixed_size_p (c, width_p, 0))
2648 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2649 fixed_p = c != NULL;
2651 else
2653 /* A vertical combination is fixed-height if all of if its
2654 children are. */
2655 while (c && window_fixed_size_p (c, width_p, 0))
2656 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2657 fixed_p = c == NULL;
2660 else if (BUFFERP (w->buffer))
2662 struct buffer *old = current_buffer;
2663 Lisp_Object val;
2665 current_buffer = XBUFFER (w->buffer);
2666 val = find_symbol_value (Qwindow_size_fixed);
2667 current_buffer = old;
2669 fixed_p = 0;
2670 if (!EQ (val, Qunbound))
2672 fixed_p = !NILP (val);
2674 if (fixed_p
2675 && ((EQ (val, Qheight) && width_p)
2676 || (EQ (val, Qwidth) && !width_p)))
2677 fixed_p = 0;
2680 /* Can't tell if this one is resizable without looking at
2681 siblings. If all siblings are fixed-size this one is too. */
2682 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2684 Lisp_Object child;
2686 for (child = w->prev; WINDOWP (child); child = XWINDOW (child)->prev)
2687 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2688 break;
2690 if (NILP (child))
2691 for (child = w->next; WINDOWP (child); child = XWINDOW (child)->next)
2692 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2693 break;
2695 if (NILP (child))
2696 fixed_p = 1;
2699 else
2700 fixed_p = 1;
2702 return fixed_p;
2705 /* Return minimum size of leaf window W. WIDTH_P non-zero means return
2706 the minimum width of W, WIDTH_P zero means return the minimum height
2707 of W. SAFE_P non-zero means ignore window-min-height|width but just
2708 return values that won't crash Emacs and don't hide components like
2709 fringes, scrollbars, or modelines. If WIDTH_P is zero and W is the
2710 minibuffer window, always return 1. */
2712 static int
2713 window_min_size_2 (struct window *w, int width_p, int safe_p)
2715 /* We should consider buffer-local values of window_min_height and
2716 window_min_width here. */
2717 if (width_p)
2719 int safe_size = (MIN_SAFE_WINDOW_WIDTH
2720 + WINDOW_FRINGE_COLS (w)
2721 + WINDOW_SCROLL_BAR_COLS (w));
2723 return safe_p ? safe_size : max (window_min_width, safe_size);
2725 else if (MINI_WINDOW_P (w))
2726 return 1;
2727 else
2729 int safe_size = (MIN_SAFE_WINDOW_HEIGHT
2730 + ((BUFFERP (w->buffer)
2731 && !NILP (BVAR (XBUFFER (w->buffer), mode_line_format)))
2732 ? 1 : 0));
2734 return safe_p ? safe_size : max (window_min_height, safe_size);
2738 /* Return minimum size of window W, not taking fixed-width windows into
2739 account. WIDTH_P non-zero means return the minimum width, otherwise
2740 return the minimum height. SAFE_P non-zero means ignore
2741 window-min-height|width but just return values that won't crash Emacs
2742 and don't hide components like fringes, scrollbars, or modelines. If
2743 W is a combination window, compute the minimum size from the minimum
2744 sizes of W's children. */
2746 static int
2747 window_min_size_1 (struct window *w, int width_p, int safe_p)
2749 struct window *c;
2750 int size;
2752 if (!NILP (w->hchild))
2754 /* W is a horizontal combination. */
2755 c = XWINDOW (w->hchild);
2756 size = 0;
2758 if (width_p)
2760 /* The minimum width of a horizontal combination is the sum of
2761 the minimum widths of its children. */
2762 while (c)
2764 size += window_min_size_1 (c, 1, safe_p);
2765 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2768 else
2770 /* The minimum height of a horizontal combination is the
2771 maximum of the minimum heights of its children. */
2772 while (c)
2774 size = max (window_min_size_1 (c, 0, safe_p), size);
2775 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2779 else if (!NILP (w->vchild))
2781 /* W is a vertical combination. */
2782 c = XWINDOW (w->vchild);
2783 size = 0;
2785 if (width_p)
2787 /* The minimum width of a vertical combination is the maximum
2788 of the minimum widths of its children. */
2789 while (c)
2791 size = max (window_min_size_1 (c, 1, safe_p), size);
2792 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2795 else
2797 /* The minimum height of a vertical combination is the sum of
2798 the minimum height of its children. */
2799 while (c)
2801 size += window_min_size_1 (c, 0, safe_p);
2802 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2806 else
2807 /* W is a leaf window. */
2808 size = window_min_size_2 (w, width_p, safe_p);
2810 return size;
2813 /* Return the minimum size of window W, taking fixed-size windows into
2814 account. WIDTH_P non-zero means return the minimum width, otherwise
2815 return the minimum height. SAFE_P non-zero means ignore
2816 window-min-height|width but just return values that won't crash Emacs
2817 and don't hide components like fringes, scrollbars, or modelines.
2818 IGNORE_FIXED_P non-zero means ignore if W is fixed-size. Set *FIXED
2819 to 1 if W is fixed-size unless FIXED is null. */
2821 static int
2822 window_min_size (struct window *w, int width_p, int safe_p, int ignore_fixed_p, int *fixed)
2824 int size, fixed_p;
2826 if (ignore_fixed_p)
2827 fixed_p = 0;
2828 else
2829 fixed_p = window_fixed_size_p (w, width_p, 1);
2831 if (fixed)
2832 *fixed = fixed_p;
2834 if (fixed_p)
2835 size = WINDOW_TOTAL_SIZE (w, width_p);
2836 else
2837 size = window_min_size_1 (w, width_p, safe_p);
2839 return size;
2843 /* Adjust the margins of window W if text area is too small.
2844 Return 1 if window width is ok after adjustment; 0 if window
2845 is still too narrow. */
2847 static int
2848 adjust_window_margins (struct window *w)
2850 int box_cols = (WINDOW_TOTAL_COLS (w)
2851 - WINDOW_FRINGE_COLS (w)
2852 - WINDOW_SCROLL_BAR_COLS (w));
2853 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2854 + WINDOW_RIGHT_MARGIN_COLS (w));
2856 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2857 return 1;
2859 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2860 return 0;
2862 /* Window's text area is too narrow, but reducing the window
2863 margins will fix that. */
2864 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2865 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2867 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2868 w->left_margin_cols = w->right_margin_cols
2869 = make_number (margin_cols/2);
2870 else
2871 w->right_margin_cols = make_number (margin_cols);
2873 else
2874 w->left_margin_cols = make_number (margin_cols);
2875 return 1;
2878 /* Calculate new sizes for windows in the list FORWARD when their
2879 compound size goes from TOTAL to SIZE. TOTAL must be greater than
2880 SIZE. The number of windows in FORWARD is NCHILDREN, and the number
2881 that can shrink is SHRINKABLE. Fixed-size windows may be shrunk if
2882 and only if RESIZE_FIXED_P is non-zero. WIDTH_P non-zero means
2883 shrink columns, otherwise shrink lines.
2885 SAFE_P zero means windows may be sized down to window-min-height
2886 lines (window-min-window columns for WIDTH_P non-zero). SAFE_P
2887 non-zero means windows may be sized down to their minimum safe sizes
2888 taking into account the space needed to display modelines, fringes,
2889 and scrollbars.
2891 This function returns an allocated array of new sizes that the caller
2892 must free. A size -1 means the window is fixed and RESIZE_FIXED_P is
2893 zero. A size zero means the window shall be deleted. Array index 0
2894 refers to the first window in FORWARD, 1 to the second, and so on.
2896 This function resizes windows proportionally to their size. It also
2897 tries to preserve smaller windows by resizing larger windows before
2898 resizing any window to zero. If resize_proportionally is non-nil for
2899 a specific window, it will attempt to strictly resize that window
2900 proportionally, even at the expense of deleting smaller windows. */
2901 static int *
2902 shrink_windows (int total, int size, int nchildren, int shrinkable,
2903 int resize_fixed_p, Lisp_Object forward, int width_p, int safe_p)
2905 int available_resize = 0;
2906 int *new_sizes, *min_sizes;
2907 struct window *c;
2908 Lisp_Object child;
2909 int smallest = total;
2910 int total_removed = 0;
2911 int total_shrink = total - size;
2912 int i;
2914 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2915 min_sizes = xmalloc (sizeof (*min_sizes) * nchildren);
2917 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2919 int child_size;
2921 c = XWINDOW (child);
2922 child_size = WINDOW_TOTAL_SIZE (c, width_p);
2924 if (!resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2925 new_sizes[i] = -1;
2926 else
2928 new_sizes[i] = child_size;
2929 min_sizes[i] = window_min_size_1 (c, width_p, safe_p);
2930 if (child_size > min_sizes[i]
2931 && NILP (c->resize_proportionally))
2932 available_resize += child_size - min_sizes[i];
2935 /* We might need to shrink some windows to zero. Find the smallest
2936 windows and set them to 0 until we can fulfil the new size. */
2938 while (shrinkable > 1 && size + available_resize < total)
2940 for (i = 0; i < nchildren; ++i)
2941 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2942 smallest = new_sizes[i];
2944 for (i = 0; i < nchildren; ++i)
2945 if (new_sizes[i] == smallest)
2947 /* Resize this window down to zero. */
2948 new_sizes[i] = 0;
2949 if (smallest > min_sizes[i])
2950 available_resize -= smallest - min_sizes[i];
2951 available_resize += smallest;
2952 --shrinkable;
2953 total_removed += smallest;
2955 /* We don't know what the smallest is now. */
2956 smallest = total;
2958 /* Out of for, just remove one window at the time and
2959 check again if we have enough space. */
2960 break;
2964 /* Now, calculate the new sizes. Try to shrink each window
2965 proportional to its size. */
2966 for (i = 0; i < nchildren; ++i)
2968 if (new_sizes[i] > min_sizes[i])
2970 int to_shrink = total_shrink * new_sizes[i] / total;
2972 if (new_sizes[i] - to_shrink < min_sizes[i])
2973 to_shrink = new_sizes[i] - min_sizes[i];
2974 new_sizes[i] -= to_shrink;
2975 total_removed += to_shrink;
2979 /* Any reminder due to rounding, we just subtract from windows
2980 that are left and still can be shrunk. */
2981 while (total_shrink > total_removed)
2983 int nonzero_sizes = 0;
2985 for (i = 0; i < nchildren; ++i)
2986 if (new_sizes[i] > 0)
2987 ++nonzero_sizes;
2989 for (i = 0; i < nchildren; ++i)
2990 if (new_sizes[i] > min_sizes[i])
2992 --new_sizes[i];
2993 ++total_removed;
2995 /* Out of for, just shrink one window at the time and
2996 check again if we have enough space. */
2997 break;
3000 /* Special case, only one window left. */
3001 if (nonzero_sizes == 1)
3002 break;
3005 /* Any surplus due to rounding, we add to windows that are left. */
3006 while (total_shrink < total_removed)
3008 for (i = 0; i < nchildren; ++i)
3010 if (new_sizes[i] != 0 && total_shrink < total_removed)
3012 ++new_sizes[i];
3013 --total_removed;
3014 break;
3019 xfree (min_sizes);
3021 return new_sizes;
3024 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
3025 WINDOW's width. Resize WINDOW's children, if any, so that they keep
3026 their proportionate size relative to WINDOW.
3028 If FIRST_ONLY is 1, change only the first of WINDOW's children when
3029 they are in series. If LAST_ONLY is 1, change only the last of
3030 WINDOW's children when they are in series.
3032 Propagate WINDOW's top or left edge position to children. Delete
3033 windows that become too small unless NODELETE_P is 1. When
3034 NODELETE_P equals 2 do not honor settings for window-min-height and
3035 window-min-width when resizing windows but use safe defaults instead.
3036 This should give better behavior when resizing frames. */
3038 static void
3039 size_window (Lisp_Object window, int size, int width_p, int nodelete_p, int first_only, int last_only)
3041 struct window *w = XWINDOW (window);
3042 struct window *c;
3043 Lisp_Object child, *forward, *sideward;
3044 int old_size = WINDOW_TOTAL_SIZE (w, width_p);
3046 size = max (0, size);
3048 /* Delete WINDOW if it's too small. */
3049 if (nodelete_p != 1 && !NILP (w->parent)
3050 && size < window_min_size_1 (w, width_p, nodelete_p == 2))
3052 delete_window (window);
3053 return;
3056 /* Set redisplay hints. */
3057 w->last_modified = make_number (0);
3058 w->last_overlay_modified = make_number (0);
3059 windows_or_buffers_changed++;
3060 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
3062 if (width_p)
3064 sideward = &w->vchild;
3065 forward = &w->hchild;
3066 w->total_cols = make_number (size);
3067 adjust_window_margins (w);
3069 else
3071 sideward = &w->hchild;
3072 forward = &w->vchild;
3073 w->total_lines = make_number (size);
3074 w->orig_total_lines = Qnil;
3077 if (!NILP (*sideward))
3079 /* We have a chain of parallel siblings whose size should all change. */
3080 for (child = *sideward; !NILP (child); child = c->next)
3082 c = XWINDOW (child);
3083 if (width_p)
3084 c->left_col = w->left_col;
3085 else
3086 c->top_line = w->top_line;
3087 size_window (child, size, width_p, nodelete_p,
3088 first_only, last_only);
3091 else if (!NILP (*forward) && last_only)
3093 /* Change the last in a series of siblings. */
3094 Lisp_Object last_child;
3095 int child_size;
3097 for (child = *forward; !NILP (child); child = c->next)
3099 c = XWINDOW (child);
3100 last_child = child;
3103 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3104 size_window (last_child, size - old_size + child_size,
3105 width_p, nodelete_p, first_only, last_only);
3107 else if (!NILP (*forward) && first_only)
3109 /* Change the first in a series of siblings. */
3110 int child_size;
3112 child = *forward;
3113 c = XWINDOW (child);
3115 if (width_p)
3116 c->left_col = w->left_col;
3117 else
3118 c->top_line = w->top_line;
3120 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3121 size_window (child, size - old_size + child_size,
3122 width_p, nodelete_p, first_only, last_only);
3124 else if (!NILP (*forward))
3126 int fixed_size, each IF_LINT (= 0), extra IF_LINT (= 0), n;
3127 int resize_fixed_p, nfixed;
3128 int last_pos, first_pos, nchildren, total;
3129 int *new_sizes = NULL;
3131 /* Determine the fixed-size portion of this window, and the
3132 number of child windows. */
3133 fixed_size = nchildren = nfixed = total = 0;
3134 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
3136 int child_size;
3138 c = XWINDOW (child);
3139 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3140 total += child_size;
3142 if (window_fixed_size_p (c, width_p, 0))
3144 fixed_size += child_size;
3145 ++nfixed;
3149 /* If the new size is smaller than fixed_size, or if there
3150 aren't any resizable windows, allow resizing fixed-size
3151 windows. */
3152 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3154 /* Compute how many lines/columns to add/remove to each child. The
3155 value of extra takes care of rounding errors. */
3156 n = resize_fixed_p ? nchildren : nchildren - nfixed;
3157 if (size < total && n > 1)
3158 new_sizes = shrink_windows (total, size, nchildren, n,
3159 resize_fixed_p, *forward, width_p,
3160 nodelete_p == 2);
3161 else
3163 each = (size - total) / n;
3164 extra = (size - total) - n * each;
3167 /* Compute new children heights and edge positions. */
3168 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
3169 last_pos = first_pos;
3170 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
3172 int new_child_size, old_child_size;
3174 c = XWINDOW (child);
3175 old_child_size = WINDOW_TOTAL_SIZE (c, width_p);
3176 new_child_size = old_child_size;
3178 /* The top or left edge position of this child equals the
3179 bottom or right edge of its predecessor. */
3180 if (width_p)
3181 c->left_col = make_number (last_pos);
3182 else
3183 c->top_line = make_number (last_pos);
3185 /* If this child can be resized, do it. */
3186 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3188 new_child_size =
3189 new_sizes ? new_sizes[n] : old_child_size + each + extra;
3190 extra = 0;
3193 /* Set new size. Note that size_window also propagates
3194 edge positions to children, so it's not a no-op if we
3195 didn't change the child's size. */
3196 size_window (child, new_child_size, width_p, 1,
3197 first_only, last_only);
3199 /* Remember the bottom/right edge position of this child; it
3200 will be used to set the top/left edge of the next child. */
3201 last_pos += new_child_size;
3204 xfree (new_sizes);
3206 /* We should have covered the parent exactly with child windows. */
3207 xassert (size == last_pos - first_pos);
3209 /* Now delete any children that became too small. */
3210 if (nodelete_p != 1)
3211 for (child = *forward; !NILP (child); child = c->next)
3213 int child_size;
3215 c = XWINDOW (child);
3216 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3217 size_window (child, child_size, width_p, nodelete_p,
3218 first_only, last_only);
3223 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3224 WINDOW's children. NODELETE zero means windows that have become
3225 smaller than window-min-height in the process may be deleted.
3226 NODELETE 1 means never delete windows that become too small in the
3227 process. (The caller should check later and do so if appropriate.)
3228 NODELETE 2 means delete only windows that have become too small to be
3229 displayed correctly. */
3231 void
3232 set_window_height (Lisp_Object window, int height, int nodelete)
3234 size_window (window, height, 0, nodelete, 0, 0);
3237 /* Set WINDOW's width to WIDTH, and recursively change the width of
3238 WINDOW's children. NODELETE zero means windows that have become
3239 smaller than window-min-width in the process may be deleted.
3240 NODELETE 1 means never delete windows that become too small in the
3241 process. (The caller should check later and do so if appropriate.)
3242 NODELETE 2 means delete only windows that have become too small to be
3243 displayed correctly. */
3245 void
3246 set_window_width (Lisp_Object window, int width, int nodelete)
3248 size_window (window, width, 1, nodelete, 0, 0);
3251 /* Change window heights in windows rooted in WINDOW by N lines. */
3253 void
3254 change_window_heights (Lisp_Object window, int n)
3256 struct window *w = XWINDOW (window);
3258 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3259 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3261 if (INTEGERP (w->orig_top_line))
3262 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3263 if (INTEGERP (w->orig_total_lines))
3264 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3266 /* Handle just the top child in a vertical split. */
3267 if (!NILP (w->vchild))
3268 change_window_heights (w->vchild, n);
3270 /* Adjust all children in a horizontal split. */
3271 for (window = w->hchild; !NILP (window); window = w->next)
3273 w = XWINDOW (window);
3274 change_window_heights (window, n);
3279 int window_select_count;
3281 static Lisp_Object Fset_window_margins (Lisp_Object, Lisp_Object, Lisp_Object);
3282 static Lisp_Object Fset_window_fringes (Lisp_Object, Lisp_Object, Lisp_Object,
3283 Lisp_Object);
3284 static Lisp_Object Fset_window_scroll_bars (Lisp_Object, Lisp_Object,
3285 Lisp_Object, Lisp_Object);
3286 static Lisp_Object Fset_window_vscroll (Lisp_Object, Lisp_Object, Lisp_Object);
3288 static void
3289 run_funs (Lisp_Object funs)
3291 for (; CONSP (funs); funs = XCDR (funs))
3292 if (!EQ (XCAR (funs), Qt))
3293 call0 (XCAR (funs));
3296 static Lisp_Object select_window_norecord (Lisp_Object window);
3297 static Lisp_Object select_frame_norecord (Lisp_Object frame);
3299 void
3300 run_window_configuration_change_hook (struct frame *f)
3302 int count = SPECPDL_INDEX ();
3303 Lisp_Object frame, global_wcch
3304 = Fdefault_value (Qwindow_configuration_change_hook);
3305 XSETFRAME (frame, f);
3307 if (NILP (Vrun_hooks))
3308 return;
3310 if (SELECTED_FRAME () != f)
3312 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3313 Fselect_frame (frame, Qt);
3316 /* Use the right buffer. Matters when running the local hooks. */
3317 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3319 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3320 Fset_buffer (Fwindow_buffer (Qnil));
3323 /* Look for buffer-local values. */
3325 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3326 for (; CONSP (windows); windows = XCDR (windows))
3328 Lisp_Object window = XCAR (windows);
3329 Lisp_Object buffer = Fwindow_buffer (window);
3330 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3331 buffer)))
3333 int count1 = SPECPDL_INDEX ();
3334 record_unwind_protect (select_window_norecord, Fselected_window ());
3335 select_window_norecord (window);
3336 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3337 buffer));
3338 unbind_to (count1, Qnil);
3343 run_funs (global_wcch);
3344 unbind_to (count, Qnil);
3347 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3348 means it's allowed to run hooks. See make_frame for a case where
3349 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3350 margins, fringes, and scroll-bar settings of the window are not
3351 reset from the buffer's local settings. */
3353 void
3354 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
3356 struct window *w = XWINDOW (window);
3357 struct buffer *b = XBUFFER (buffer);
3358 int count = SPECPDL_INDEX ();
3359 int samebuf = EQ (buffer, w->buffer);
3361 w->buffer = buffer;
3363 if (EQ (window, selected_window))
3364 BVAR (b, last_selected_window) = window;
3366 /* Let redisplay errors through. */
3367 b->display_error_modiff = 0;
3369 /* Update time stamps of buffer display. */
3370 if (INTEGERP (BVAR (b, display_count)))
3371 XSETINT (BVAR (b, display_count), XINT (BVAR (b, display_count)) + 1);
3372 BVAR (b, display_time) = Fcurrent_time ();
3374 XSETFASTINT (w->window_end_pos, 0);
3375 XSETFASTINT (w->window_end_vpos, 0);
3376 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3377 w->window_end_valid = Qnil;
3378 if (!(keep_margins_p && samebuf))
3379 { /* If we're not actually changing the buffer, don't reset hscroll and
3380 vscroll. This case happens for example when called from
3381 change_frame_size_1, where we use a dummy call to
3382 Fset_window_buffer on the frame's selected window (and no other)
3383 just in order to run window-configuration-change-hook.
3384 Resetting hscroll and vscroll here is problematic for things like
3385 image-mode and doc-view-mode since it resets the image's position
3386 whenever we resize the frame. */
3387 w->hscroll = w->min_hscroll = make_number (0);
3388 w->vscroll = 0;
3389 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3390 set_marker_restricted (w->start,
3391 make_number (b->last_window_start),
3392 buffer);
3393 w->start_at_line_beg = Qnil;
3394 w->force_start = Qnil;
3395 XSETFASTINT (w->last_modified, 0);
3396 XSETFASTINT (w->last_overlay_modified, 0);
3398 /* Maybe we could move this into the `if' but it's not obviously safe and
3399 I doubt it's worth the trouble. */
3400 windows_or_buffers_changed++;
3402 /* We must select BUFFER for running the window-scroll-functions. */
3403 /* We can't check ! NILP (Vwindow_scroll_functions) here
3404 because that might itself be a local variable. */
3405 if (window_initialized)
3407 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3408 Fset_buffer (buffer);
3411 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3413 if (!keep_margins_p)
3415 /* Set left and right marginal area width etc. from buffer. */
3417 /* This may call adjust_window_margins three times, so
3418 temporarily disable window margins. */
3419 Lisp_Object save_left = w->left_margin_cols;
3420 Lisp_Object save_right = w->right_margin_cols;
3422 w->left_margin_cols = w->right_margin_cols = Qnil;
3424 Fset_window_fringes (window,
3425 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3426 BVAR (b, fringes_outside_margins));
3428 Fset_window_scroll_bars (window,
3429 BVAR (b, scroll_bar_width),
3430 BVAR (b, vertical_scroll_bar_type), Qnil);
3432 w->left_margin_cols = save_left;
3433 w->right_margin_cols = save_right;
3435 Fset_window_margins (window,
3436 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3439 if (run_hooks_p)
3441 if (! NILP (Vwindow_scroll_functions))
3442 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3443 Fmarker_position (w->start));
3444 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3447 unbind_to (count, Qnil);
3451 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3452 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3453 WINDOW defaults to the selected window. BUFFER-OR-NAME must be a buffer
3454 or the name of an existing buffer. Optional third argument KEEP-MARGINS
3455 non-nil means that WINDOW's current display margins, fringe widths, and
3456 scroll bar settings are preserved; the default is to reset these from
3457 the local settings for BUFFER-OR-NAME or the frame defaults. Return nil.
3459 This function throws an error when WINDOW is strongly dedicated to its
3460 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3461 already display BUFFER-OR-NAME.
3463 This function runs `window-scroll-functions' before running
3464 `window-configuration-change-hook'. */)
3465 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3467 register Lisp_Object tem, buffer;
3468 register struct window *w = decode_window (window);
3470 XSETWINDOW (window, w);
3471 buffer = Fget_buffer (buffer_or_name);
3472 CHECK_BUFFER (buffer);
3473 if (NILP (BVAR (XBUFFER (buffer), name)))
3474 error ("Attempt to display deleted buffer");
3476 tem = w->buffer;
3477 if (NILP (tem))
3478 error ("Window is deleted");
3479 else if (!EQ (tem, Qt))
3480 /* w->buffer is t when the window is first being set up. */
3482 if (EQ (tem, buffer))
3483 return Qnil;
3484 else if (EQ (w->dedicated, Qt))
3485 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3486 else
3487 w->dedicated = Qnil;
3489 unshow_buffer (w);
3492 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3493 return Qnil;
3496 /* If select_window is called with inhibit_point_swap non-zero it will
3497 not store point of the old selected window's buffer back into that
3498 window's pointm slot. This is needed by Fset_window_configuration to
3499 avoid that the display routine is called with selected_window set to
3500 Qnil causing a subsequent crash. */
3502 static Lisp_Object
3503 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
3505 register struct window *w;
3506 register struct window *ow;
3507 struct frame *sf;
3509 CHECK_LIVE_WINDOW (window);
3511 w = XWINDOW (window);
3512 w->frozen_window_start_p = 0;
3514 if (NILP (norecord))
3516 ++window_select_count;
3517 XSETFASTINT (w->use_time, window_select_count);
3518 record_buffer (w->buffer);
3521 if (EQ (window, selected_window) && !inhibit_point_swap)
3522 return window;
3524 sf = SELECTED_FRAME ();
3525 if (XFRAME (WINDOW_FRAME (w)) != sf)
3527 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3528 /* Use this rather than Fhandle_switch_frame
3529 so that FRAME_FOCUS_FRAME is moved appropriately as we
3530 move around in the state where a minibuffer in a separate
3531 frame is active. */
3532 Fselect_frame (WINDOW_FRAME (w), norecord);
3533 /* Fselect_frame called us back so we've done all the work already. */
3534 eassert (EQ (window, selected_window));
3535 return window;
3537 else
3538 sf->selected_window = window;
3540 /* Store the current buffer's actual point into the
3541 old selected window. It belongs to that window,
3542 and when the window is not selected, must be in the window. */
3543 if (!inhibit_point_swap)
3545 ow = XWINDOW (selected_window);
3546 if (! NILP (ow->buffer))
3547 set_marker_both (ow->pointm, ow->buffer,
3548 BUF_PT (XBUFFER (ow->buffer)),
3549 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3552 selected_window = window;
3554 Fset_buffer (w->buffer);
3556 BVAR (XBUFFER (w->buffer), last_selected_window) = window;
3558 /* Go to the point recorded in the window.
3559 This is important when the buffer is in more
3560 than one window. It also matters when
3561 redisplay_window has altered point after scrolling,
3562 because it makes the change only in the window. */
3564 register EMACS_INT new_point = marker_position (w->pointm);
3565 if (new_point < BEGV)
3566 SET_PT (BEGV);
3567 else if (new_point > ZV)
3568 SET_PT (ZV);
3569 else
3570 SET_PT (new_point);
3573 windows_or_buffers_changed++;
3574 return window;
3578 /* Note that selected_window can be nil when this is called from
3579 Fset_window_configuration. */
3581 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3582 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3583 If WINDOW is not already selected, make WINDOW's buffer current
3584 and make WINDOW the frame's selected window. Return WINDOW.
3585 Optional second arg NORECORD non-nil means do not put this buffer
3586 at the front of the list of recently selected ones and do not
3587 make this window the most recently selected one.
3589 Note that the main editor command loop selects the buffer of the
3590 selected window before each command. */)
3591 (register Lisp_Object window, Lisp_Object norecord)
3593 return select_window (window, norecord, 0);
3596 static Lisp_Object
3597 select_window_norecord (Lisp_Object window)
3599 return WINDOW_LIVE_P (window)
3600 ? Fselect_window (window, Qt) : selected_window;
3603 static Lisp_Object
3604 select_frame_norecord (Lisp_Object frame)
3606 return FRAME_LIVE_P (XFRAME (frame))
3607 ? Fselect_frame (frame, Qt) : selected_frame;
3610 static Lisp_Object
3611 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3613 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3616 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3617 0, 1, 0,
3618 doc: /* Force all windows to be updated on next redisplay.
3619 If optional arg OBJECT is a window, force redisplay of that window only.
3620 If OBJECT is a buffer or buffer name, force redisplay of all windows
3621 displaying that buffer. */)
3622 (Lisp_Object object)
3624 if (NILP (object))
3626 windows_or_buffers_changed++;
3627 update_mode_lines++;
3628 return Qt;
3631 if (WINDOWP (object))
3633 struct window *w = XWINDOW (object);
3634 mark_window_display_accurate (object, 0);
3635 w->update_mode_line = Qt;
3636 if (BUFFERP (w->buffer))
3637 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3638 ++update_mode_lines;
3639 return Qt;
3642 if (STRINGP (object))
3643 object = Fget_buffer (object);
3644 if (BUFFERP (object) && !NILP (BVAR (XBUFFER (object), name)))
3646 /* Walk all windows looking for buffer, and force update
3647 of each of those windows. */
3649 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3650 return NILP (object) ? Qnil : Qt;
3653 /* If nothing suitable was found, just return.
3654 We could signal an error, but this feature will typically be used
3655 asynchronously in timers or process sentinels, so we don't. */
3656 return Qnil;
3660 void
3661 temp_output_buffer_show (register Lisp_Object buf)
3663 register struct buffer *old = current_buffer;
3664 register Lisp_Object window;
3665 register struct window *w;
3667 BVAR (XBUFFER (buf), directory) = BVAR (current_buffer, directory);
3669 Fset_buffer (buf);
3670 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3671 BEGV = BEG;
3672 ZV = Z;
3673 SET_PT (BEG);
3674 set_buffer_internal (old);
3676 if (!NILP (Vtemp_buffer_show_function))
3677 call1 (Vtemp_buffer_show_function, buf);
3678 else
3680 window = display_buffer (buf, Qnil, Qnil);
3682 if (!EQ (XWINDOW (window)->frame, selected_frame))
3683 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3684 Vminibuf_scroll_window = window;
3685 w = XWINDOW (window);
3686 XSETFASTINT (w->hscroll, 0);
3687 XSETFASTINT (w->min_hscroll, 0);
3688 set_marker_restricted_both (w->start, buf, BEG, BEG);
3689 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3691 /* Run temp-buffer-show-hook, with the chosen window selected
3692 and its buffer current. */
3694 int count = SPECPDL_INDEX ();
3695 Lisp_Object prev_window, prev_buffer;
3696 prev_window = selected_window;
3697 XSETBUFFER (prev_buffer, old);
3699 /* Select the window that was chosen, for running the hook.
3700 Note: Both Fselect_window and select_window_norecord may
3701 set-buffer to the buffer displayed in the window,
3702 so we need to save the current buffer. --stef */
3703 record_unwind_protect (Fset_buffer, prev_buffer);
3704 record_unwind_protect (select_window_norecord, prev_window);
3705 Fselect_window (window, Qt);
3706 Fset_buffer (w->buffer);
3707 Frun_hooks (1, &Qtemp_buffer_show_hook);
3708 unbind_to (count, Qnil);
3713 DEFUN ("internal-temp-output-buffer-show",
3714 Ftemp_output_buffer_show, Stemp_output_buffer_show,
3715 1, 1, 0,
3716 doc: /* Internal function for `with-output-to-temp-buffer''. */)
3717 (Lisp_Object buf)
3719 temp_output_buffer_show (buf);
3720 return Qnil;
3723 static void
3724 make_dummy_parent (Lisp_Object window)
3726 Lisp_Object new;
3727 register struct window *o, *p;
3728 int i;
3730 o = XWINDOW (window);
3731 p = allocate_window ();
3732 for (i = 0; i < VECSIZE (struct window); ++i)
3733 ((struct Lisp_Vector *) p)->contents[i]
3734 = ((struct Lisp_Vector *)o)->contents[i];
3735 XSETWINDOW (new, p);
3737 ++sequence_number;
3738 XSETFASTINT (p->sequence_number, sequence_number);
3740 /* Put new into window structure in place of window */
3741 replace_window (window, new);
3743 o->next = Qnil;
3744 o->prev = Qnil;
3745 o->vchild = Qnil;
3746 o->hchild = Qnil;
3747 o->parent = new;
3749 p->start = Qnil;
3750 p->pointm = Qnil;
3751 p->buffer = Qnil;
3754 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3755 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3756 WINDOW defaults to selected one and SIZE to half its size.
3757 If optional third arg HORIZONTAL is non-nil, split side by side and put
3758 SIZE columns in the first of the pair. In that case, SIZE includes that
3759 window's scroll bar, or the divider column to its right.
3760 Interactively, all arguments are nil.
3761 Returns the newly created window (which is the lower or rightmost one).
3762 The upper or leftmost window is the original one, and remains selected
3763 if it was selected before.
3765 See Info node `(elisp)Splitting Windows' for more details and examples. */)
3766 (Lisp_Object window, Lisp_Object size, Lisp_Object horizontal)
3768 register Lisp_Object new;
3769 register struct window *o, *p;
3770 FRAME_PTR fo;
3771 register int size_int;
3773 if (NILP (window))
3774 window = selected_window;
3775 else
3776 CHECK_LIVE_WINDOW (window);
3778 o = XWINDOW (window);
3779 fo = XFRAME (WINDOW_FRAME (o));
3781 if (NILP (size))
3783 if (!NILP (horizontal))
3784 /* Calculate the size of the left-hand window, by dividing
3785 the usable space in columns by two.
3786 We round up, since the left-hand window may include
3787 a dividing line, while the right-hand may not. */
3788 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3789 else
3790 size_int = XFASTINT (o->total_lines) >> 1;
3792 else
3794 CHECK_NUMBER (size);
3795 size_int = XINT (size);
3798 if (MINI_WINDOW_P (o))
3799 error ("Attempt to split minibuffer window");
3800 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
3801 error ("Attempt to split fixed-size window");
3803 if (NILP (horizontal))
3805 int window_safe_height = window_min_size_2 (o, 0, 0);
3807 if (size_int < window_safe_height)
3808 error ("Window height %d too small (after splitting)", size_int);
3809 if (size_int + window_safe_height > XFASTINT (o->total_lines))
3810 error ("Window height %d too small (after splitting)",
3811 (int) (XFASTINT (o->total_lines) - size_int));
3812 if (NILP (o->parent)
3813 || NILP (XWINDOW (o->parent)->vchild))
3815 make_dummy_parent (window);
3816 new = o->parent;
3817 XWINDOW (new)->vchild = window;
3820 else
3822 int window_safe_width = window_min_size_2 (o, 1, 0);
3824 if (size_int < window_safe_width)
3825 error ("Window width %d too small (after splitting)", size_int);
3826 if (size_int + window_safe_width > XFASTINT (o->total_cols))
3827 error ("Window width %d too small (after splitting)",
3828 (int) (XFASTINT (o->total_cols) - size_int));
3829 if (NILP (o->parent)
3830 || NILP (XWINDOW (o->parent)->hchild))
3832 make_dummy_parent (window);
3833 new = o->parent;
3834 XWINDOW (new)->hchild = window;
3838 /* Now we know that window's parent is a vertical combination
3839 if we are dividing vertically, or a horizontal combination
3840 if we are making side-by-side windows */
3842 windows_or_buffers_changed++;
3843 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3844 new = make_window ();
3845 p = XWINDOW (new);
3847 p->frame = o->frame;
3848 p->next = o->next;
3849 if (!NILP (p->next))
3850 XWINDOW (p->next)->prev = new;
3851 p->prev = window;
3852 o->next = new;
3853 p->parent = o->parent;
3854 p->buffer = Qt;
3855 p->window_end_valid = Qnil;
3856 memset (&p->last_cursor, 0, sizeof p->last_cursor);
3858 /* Duplicate special geometry settings. */
3860 p->left_margin_cols = o->left_margin_cols;
3861 p->right_margin_cols = o->right_margin_cols;
3862 p->left_fringe_width = o->left_fringe_width;
3863 p->right_fringe_width = o->right_fringe_width;
3864 p->fringes_outside_margins = o->fringes_outside_margins;
3865 p->scroll_bar_width = o->scroll_bar_width;
3866 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3868 /* Apportion the available frame space among the two new windows */
3870 if (!NILP (horizontal))
3872 p->total_lines = o->total_lines;
3873 p->top_line = o->top_line;
3874 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3875 XSETFASTINT (o->total_cols, size_int);
3876 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3877 adjust_window_margins (p);
3878 adjust_window_margins (o);
3880 else
3882 p->left_col = o->left_col;
3883 p->total_cols = o->total_cols;
3884 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3885 XSETFASTINT (o->total_lines, size_int);
3886 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3889 /* Adjust glyph matrices. */
3890 adjust_glyphs (fo);
3892 Fset_window_buffer (new, o->buffer, Qt);
3893 return new;
3896 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3897 doc: /* Make selected window SIZE lines taller.
3898 Interactively, if no argument is given, make the selected window one
3899 line taller. If optional argument HORIZONTAL is non-nil, make selected
3900 window wider by SIZE columns. If SIZE is negative, shrink the window by
3901 -SIZE lines or columns. Return nil.
3903 This function can delete windows if they get too small. The size of
3904 fixed size windows is not altered by this function. */)
3905 (Lisp_Object size, Lisp_Object horizontal)
3907 CHECK_NUMBER (size);
3908 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
3910 run_window_configuration_change_hook (SELECTED_FRAME ());
3912 return Qnil;
3915 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3916 doc: /* Make selected window SIZE lines smaller.
3917 Interactively, if no argument is given, make the selected window one
3918 line smaller. If optional argument HORIZONTAL is non-nil, make the
3919 window narrower by SIZE columns. If SIZE is negative, enlarge selected
3920 window by -SIZE lines or columns. Return nil.
3922 This function can delete windows if they get too small. The size of
3923 fixed size windows is not altered by this function. */)
3924 (Lisp_Object size, Lisp_Object horizontal)
3926 CHECK_NUMBER (size);
3927 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
3929 run_window_configuration_change_hook (SELECTED_FRAME ());
3931 return Qnil;
3934 static int
3935 window_height (Lisp_Object window)
3937 register struct window *p = XWINDOW (window);
3938 return WINDOW_TOTAL_LINES (p);
3941 static int
3942 window_width (Lisp_Object window)
3944 register struct window *p = XWINDOW (window);
3945 return WINDOW_TOTAL_COLS (p);
3949 #define CURBEG(w) \
3950 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3952 #define CURSIZE(w) \
3953 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3956 /* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
3957 horizontally; zero means do it vertically.
3959 Siblings of the selected window are resized to fulfill the size
3960 request. If they become too small in the process, they may be
3961 deleted. */
3963 static void
3964 enlarge_window (Lisp_Object window, int delta, int horiz_flag)
3966 Lisp_Object parent, next, prev;
3967 struct window *p;
3968 Lisp_Object *sizep;
3969 int maximum;
3970 int (*sizefun) (Lisp_Object)
3971 = horiz_flag ? window_width : window_height;
3972 void (*setsizefun) (Lisp_Object, int, int)
3973 = (horiz_flag ? set_window_width : set_window_height);
3975 /* Give up if this window cannot be resized. */
3976 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
3977 error ("Window is not resizable");
3979 /* Find the parent of the selected window. */
3980 while (1)
3982 p = XWINDOW (window);
3983 parent = p->parent;
3985 if (NILP (parent))
3987 if (horiz_flag)
3988 error ("No other window to side of this one");
3989 break;
3992 if (horiz_flag
3993 ? !NILP (XWINDOW (parent)->hchild)
3994 : !NILP (XWINDOW (parent)->vchild))
3995 break;
3997 window = parent;
4000 sizep = &CURSIZE (window);
4003 register int maxdelta;
4005 /* Compute the maximum size increment this window can have. */
4007 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4008 /* This is a main window followed by a minibuffer. */
4009 : !NILP (p->next) ? ((*sizefun) (p->next)
4010 - window_min_size (XWINDOW (p->next),
4011 horiz_flag, 0, 0, 0))
4012 /* This is a minibuffer following a main window. */
4013 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4014 - window_min_size (XWINDOW (p->prev),
4015 horiz_flag, 0, 0, 0))
4016 /* This is a frame with only one window, a minibuffer-only
4017 or a minibufferless frame. */
4018 : (delta = 0));
4020 if (delta > maxdelta)
4021 /* This case traps trying to make the minibuffer
4022 the full frame, or make the only window aside from the
4023 minibuffer the full frame. */
4024 delta = maxdelta;
4027 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4028 horiz_flag, 0, 0, 0))
4030 delete_window (window);
4031 return;
4034 if (delta == 0)
4035 return;
4037 /* Find the total we can get from other siblings without deleting them. */
4038 maximum = 0;
4039 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
4040 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4041 horiz_flag, 0, 0, 0);
4042 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
4043 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4044 horiz_flag, 0, 0, 0);
4046 /* If we can get it all from them without deleting them, do so. */
4047 if (delta <= maximum)
4049 Lisp_Object first_unaffected;
4050 Lisp_Object first_affected;
4051 int fixed_p;
4053 next = p->next;
4054 prev = p->prev;
4055 first_affected = window;
4056 /* Look at one sibling at a time,
4057 moving away from this window in both directions alternately,
4058 and take as much as we can get without deleting that sibling. */
4059 while (delta != 0
4060 && (!NILP (next) || !NILP (prev)))
4062 if (! NILP (next))
4064 int this_one = ((*sizefun) (next)
4065 - window_min_size (XWINDOW (next), horiz_flag,
4066 0, 0, &fixed_p));
4067 if (!fixed_p)
4069 if (this_one > delta)
4070 this_one = delta;
4072 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4073 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4075 delta -= this_one;
4078 next = XWINDOW (next)->next;
4081 if (delta == 0)
4082 break;
4084 if (! NILP (prev))
4086 int this_one = ((*sizefun) (prev)
4087 - window_min_size (XWINDOW (prev), horiz_flag,
4088 0, 0, &fixed_p));
4089 if (!fixed_p)
4091 if (this_one > delta)
4092 this_one = delta;
4094 first_affected = prev;
4096 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4097 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4099 delta -= this_one;
4102 prev = XWINDOW (prev)->prev;
4106 xassert (delta == 0);
4108 /* Now recalculate the edge positions of all the windows affected,
4109 based on the new sizes. */
4110 first_unaffected = next;
4111 prev = first_affected;
4112 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4113 prev = next, next = XWINDOW (next)->next)
4115 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4116 /* This does not change size of NEXT,
4117 but it propagates the new top edge to its children */
4118 (*setsizefun) (next, (*sizefun) (next), 0);
4121 else
4123 register int delta1;
4124 register int opht = (*sizefun) (parent);
4126 if (opht <= XINT (*sizep) + delta)
4128 /* If trying to grow this window to or beyond size of the parent,
4129 just delete all the sibling windows. */
4130 Lisp_Object start, tem;
4132 start = XWINDOW (parent)->vchild;
4133 if (NILP (start))
4134 start = XWINDOW (parent)->hchild;
4136 /* Delete any siblings that come after WINDOW. */
4137 tem = XWINDOW (window)->next;
4138 while (! NILP (tem))
4140 Lisp_Object next1 = XWINDOW (tem)->next;
4141 delete_window (tem);
4142 tem = next1;
4145 /* Delete any siblings that come after WINDOW.
4146 Note that if START is not WINDOW, then WINDOW still
4147 has siblings, so WINDOW has not yet replaced its parent. */
4148 tem = start;
4149 while (! EQ (tem, window))
4151 Lisp_Object next1 = XWINDOW (tem)->next;
4152 delete_window (tem);
4153 tem = next1;
4156 else
4158 /* Otherwise, make delta1 just right so that if we add
4159 delta1 lines to this window and to the parent, and then
4160 shrink the parent back to its original size, the new
4161 proportional size of this window will increase by delta.
4163 The function size_window will compute the new height h'
4164 of the window from delta1 as:
4166 e = delta1/n
4167 x = delta1 - delta1/n * n for the 1st resizable child
4168 h' = h + e + x
4170 where n is the number of children that can be resized.
4171 We can ignore x by choosing a delta1 that is a multiple of
4172 n. We want the height of this window to come out as
4174 h' = h + delta
4176 So, delta1 must be
4178 h + e = h + delta
4179 delta1/n = delta
4180 delta1 = n * delta.
4182 The number of children n equals the number of resizable
4183 children of this window + 1 because we know window itself
4184 is resizable (otherwise we would have signaled an error).
4186 This reasoning is not correct when other windows become too
4187 small and shrink_windows refuses to delete them. Below we
4188 use resize_proportionally to work around this problem. */
4190 struct window *w = XWINDOW (window);
4191 Lisp_Object s;
4192 int n = 1;
4194 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
4195 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4196 ++n;
4197 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
4198 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4199 ++n;
4201 delta1 = n * delta;
4203 /* Add delta1 lines or columns to this window, and to the parent,
4204 keeping things consistent while not affecting siblings. */
4205 XSETINT (CURSIZE (parent), opht + delta1);
4206 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4208 /* Squeeze out delta1 lines or columns from our parent,
4209 shrinking this window and siblings proportionately. This
4210 brings parent back to correct size. Delta1 was calculated
4211 so this makes this window the desired size, taking it all
4212 out of the siblings.
4214 Temporarily set resize_proportionally to Qt to assure that,
4215 if necessary, shrink_windows deletes smaller windows rather
4216 than shrink this window. */
4217 w->resize_proportionally = Qt;
4218 (*setsizefun) (parent, opht, 0);
4219 w->resize_proportionally = Qnil;
4223 XSETFASTINT (p->last_modified, 0);
4224 XSETFASTINT (p->last_overlay_modified, 0);
4226 /* Adjust glyph matrices. */
4227 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4231 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4232 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4233 zero means adjust the height, moving the bottom edge.
4235 Following siblings of the selected window are resized to fulfill
4236 the size request. If they become too small in the process, they
4237 are not deleted; instead, we signal an error. */
4239 static void
4240 adjust_window_trailing_edge (Lisp_Object window, int delta, int horiz_flag)
4242 Lisp_Object parent, child;
4243 struct window *p;
4244 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4245 int delcount = window_deletion_count;
4247 CHECK_WINDOW (window);
4249 /* Give up if this window cannot be resized. */
4250 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4251 error ("Window is not resizable");
4253 while (1)
4255 Lisp_Object first_parallel = Qnil;
4257 if (NILP (window))
4259 /* This happens if WINDOW on the previous iteration was
4260 at top level of the window tree. */
4261 Fset_window_configuration (old_config);
4262 error ("Specified window edge is fixed");
4265 p = XWINDOW (window);
4266 parent = p->parent;
4268 /* See if this level has windows in parallel in the specified
4269 direction. If so, set FIRST_PARALLEL to the first one. */
4270 if (horiz_flag)
4272 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4273 first_parallel = XWINDOW (parent)->vchild;
4274 else if (NILP (parent) && !NILP (p->next))
4276 /* Handle the vertical chain of main window and minibuffer
4277 which has no parent. */
4278 first_parallel = window;
4279 while (! NILP (XWINDOW (first_parallel)->prev))
4280 first_parallel = XWINDOW (first_parallel)->prev;
4283 else
4285 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4286 first_parallel = XWINDOW (parent)->hchild;
4289 /* If this level's succession is in the desired dimension,
4290 and this window is the last one, and there is no higher level,
4291 its trailing edge is fixed. */
4292 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4293 && NILP (parent))
4295 Fset_window_configuration (old_config);
4296 error ("Specified window edge is fixed");
4299 /* Don't make this window too small. */
4300 if (XINT (CURSIZE (window)) + delta
4301 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
4303 Fset_window_configuration (old_config);
4304 error ("Cannot adjust window size as specified");
4307 /* Clear out some redisplay caches. */
4308 XSETFASTINT (p->last_modified, 0);
4309 XSETFASTINT (p->last_overlay_modified, 0);
4311 /* Adjust this window's edge. */
4312 XSETINT (CURSIZE (window),
4313 XINT (CURSIZE (window)) + delta);
4315 /* If this window has following siblings in the desired dimension,
4316 make them smaller, and exit the loop.
4318 (If we reach the top of the tree and can never do this,
4319 we will fail and report an error, above.) */
4320 if (NILP (first_parallel))
4322 if (!NILP (p->next))
4324 /* This may happen for the minibuffer. In that case
4325 the window_deletion_count check below does not work. */
4326 if (XINT (CURSIZE (p->next)) - delta <= 0)
4328 Fset_window_configuration (old_config);
4329 error ("Cannot adjust window size as specified");
4332 XSETINT (CURBEG (p->next),
4333 XINT (CURBEG (p->next)) + delta);
4334 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4335 horiz_flag, 0, 1, 0);
4336 break;
4339 else
4340 /* Here we have a chain of parallel siblings, in the other dimension.
4341 Change the size of the other siblings. */
4342 for (child = first_parallel;
4343 ! NILP (child);
4344 child = XWINDOW (child)->next)
4345 if (! EQ (child, window))
4346 size_window (child, XINT (CURSIZE (child)) + delta,
4347 horiz_flag, 0, 0, 1);
4349 window = parent;
4352 /* If we made a window so small it got deleted,
4353 we failed. Report failure. */
4354 if (delcount != window_deletion_count)
4356 Fset_window_configuration (old_config);
4357 error ("Cannot adjust window size as specified");
4360 /* Adjust glyph matrices. */
4361 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4364 #undef CURBEG
4365 #undef CURSIZE
4367 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4368 Sadjust_window_trailing_edge, 3, 3, 0,
4369 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4370 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4371 Otherwise, adjust the height, moving the bottom edge.
4373 Following siblings of the selected window are resized to fulfill
4374 the size request. If they become too small in the process, they
4375 are not deleted; instead, we signal an error. */)
4376 (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal)
4378 CHECK_NUMBER (delta);
4379 if (NILP (window))
4380 window = selected_window;
4381 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4383 run_window_configuration_change_hook
4384 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4386 return Qnil;
4391 /***********************************************************************
4392 Resizing Mini-Windows
4393 ***********************************************************************/
4395 static void shrink_window_lowest_first (struct window *, int);
4397 enum save_restore_action
4399 CHECK_ORIG_SIZES,
4400 SAVE_ORIG_SIZES,
4401 RESTORE_ORIG_SIZES
4404 static int save_restore_orig_size (struct window *,
4405 enum save_restore_action);
4407 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4408 from lowest windows first. */
4410 static void
4411 shrink_window_lowest_first (struct window *w, int height)
4413 struct window *c;
4414 Lisp_Object child;
4415 int old_height;
4417 xassert (!MINI_WINDOW_P (w));
4419 /* Set redisplay hints. */
4420 XSETFASTINT (w->last_modified, 0);
4421 XSETFASTINT (w->last_overlay_modified, 0);
4422 windows_or_buffers_changed++;
4423 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4425 old_height = XFASTINT (w->total_lines);
4426 XSETFASTINT (w->total_lines, height);
4428 if (!NILP (w->hchild))
4430 for (child = w->hchild; !NILP (child); child = c->next)
4432 c = XWINDOW (child);
4433 c->top_line = w->top_line;
4434 shrink_window_lowest_first (c, height);
4437 else if (!NILP (w->vchild))
4439 Lisp_Object last_child;
4440 int delta = old_height - height;
4441 int last_top;
4443 last_child = Qnil;
4445 /* Find the last child. We are taking space from lowest windows
4446 first, so we iterate over children from the last child
4447 backwards. */
4448 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
4449 last_child = child;
4451 /* Size children down to their safe heights. */
4452 for (child = last_child; delta && !NILP (child); child = c->prev)
4454 int this_one;
4456 c = XWINDOW (child);
4457 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
4459 if (this_one > delta)
4460 this_one = delta;
4462 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4463 delta -= this_one;
4466 /* Compute new positions. */
4467 last_top = XINT (w->top_line);
4468 for (child = w->vchild; !NILP (child); child = c->next)
4470 c = XWINDOW (child);
4471 c->top_line = make_number (last_top);
4472 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4473 last_top += XFASTINT (c->total_lines);
4479 /* Save, restore, or check positions and sizes in the window tree
4480 rooted at W. ACTION says what to do.
4482 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4483 orig_total_lines members are valid for all windows in the window
4484 tree. Value is non-zero if they are valid.
4486 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4487 orig_top_line and orig_total_lines for all windows in the tree.
4489 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4490 stored in orig_top_line and orig_total_lines for all windows. */
4492 static int
4493 save_restore_orig_size (struct window *w, enum save_restore_action action)
4495 int success_p = 1;
4497 while (w)
4499 if (!NILP (w->hchild))
4501 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4502 success_p = 0;
4504 else if (!NILP (w->vchild))
4506 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4507 success_p = 0;
4510 switch (action)
4512 case CHECK_ORIG_SIZES:
4513 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4514 return 0;
4515 break;
4517 case SAVE_ORIG_SIZES:
4518 w->orig_top_line = w->top_line;
4519 w->orig_total_lines = w->total_lines;
4520 XSETFASTINT (w->last_modified, 0);
4521 XSETFASTINT (w->last_overlay_modified, 0);
4522 break;
4524 case RESTORE_ORIG_SIZES:
4525 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4526 w->top_line = w->orig_top_line;
4527 w->total_lines = w->orig_total_lines;
4528 w->orig_total_lines = w->orig_top_line = Qnil;
4529 XSETFASTINT (w->last_modified, 0);
4530 XSETFASTINT (w->last_overlay_modified, 0);
4531 break;
4533 default:
4534 abort ();
4537 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4540 return success_p;
4544 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4545 without deleting other windows. */
4547 void
4548 grow_mini_window (struct window *w, int delta)
4550 struct frame *f = XFRAME (w->frame);
4551 struct window *root;
4553 xassert (MINI_WINDOW_P (w));
4554 /* Commenting out the following assertion goes against the stated interface
4555 of the function, but it currently does not seem to do anything useful.
4556 See discussion of this issue in the thread for bug#4534.
4557 xassert (delta >= 0); */
4559 /* Compute how much we can enlarge the mini-window without deleting
4560 other windows. */
4561 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4562 if (delta > 0)
4564 int min_height = window_min_size (root, 0, 0, 0, 0);
4565 if (XFASTINT (root->total_lines) - delta < min_height)
4566 /* Note that the root window may already be smaller than
4567 min_height. */
4568 delta = max (0, XFASTINT (root->total_lines) - min_height);
4571 if (delta)
4573 /* Save original window sizes and positions, if not already done. */
4574 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4575 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4577 /* Shrink other windows. */
4578 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4580 /* Grow the mini-window. */
4581 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4582 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4583 XSETFASTINT (w->last_modified, 0);
4584 XSETFASTINT (w->last_overlay_modified, 0);
4586 adjust_glyphs (f);
4591 /* Shrink mini-window W. If there is recorded info about window sizes
4592 before a call to grow_mini_window, restore recorded window sizes.
4593 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4594 line. */
4596 void
4597 shrink_mini_window (struct window *w)
4599 struct frame *f = XFRAME (w->frame);
4600 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4602 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4604 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4605 adjust_glyphs (f);
4606 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4607 windows_or_buffers_changed = 1;
4609 else if (XFASTINT (w->total_lines) > 1)
4611 /* Distribute the additional lines of the mini-window
4612 among the other windows. */
4613 Lisp_Object window;
4614 XSETWINDOW (window, w);
4615 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4621 /* Mark window cursors off for all windows in the window tree rooted
4622 at W by setting their phys_cursor_on_p flag to zero. Called from
4623 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4624 the frame are cleared. */
4626 void
4627 mark_window_cursors_off (struct window *w)
4629 while (w)
4631 if (!NILP (w->hchild))
4632 mark_window_cursors_off (XWINDOW (w->hchild));
4633 else if (!NILP (w->vchild))
4634 mark_window_cursors_off (XWINDOW (w->vchild));
4635 else
4636 w->phys_cursor_on_p = 0;
4638 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4643 /* Return number of lines of text (not counting mode lines) in W. */
4646 window_internal_height (struct window *w)
4648 int ht = XFASTINT (w->total_lines);
4650 if (!MINI_WINDOW_P (w))
4652 if (!NILP (w->parent)
4653 || !NILP (w->vchild)
4654 || !NILP (w->hchild)
4655 || !NILP (w->next)
4656 || !NILP (w->prev)
4657 || WINDOW_WANTS_MODELINE_P (w))
4658 --ht;
4660 if (WINDOW_WANTS_HEADER_LINE_P (w))
4661 --ht;
4664 return ht;
4668 /* Return the number of columns in W.
4669 Don't count columns occupied by scroll bars or the vertical bar
4670 separating W from the sibling to its right. */
4673 window_box_text_cols (struct window *w)
4675 struct frame *f = XFRAME (WINDOW_FRAME (w));
4676 int width = XINT (w->total_cols);
4678 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4679 /* Scroll bars occupy a few columns. */
4680 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4681 else if (!FRAME_WINDOW_P (f)
4682 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4683 /* The column of `|' characters separating side-by-side windows
4684 occupies one column only. */
4685 width -= 1;
4687 if (FRAME_WINDOW_P (f))
4688 /* On window-systems, fringes and display margins cannot be
4689 used for normal text. */
4690 width -= (WINDOW_FRINGE_COLS (w)
4691 + WINDOW_LEFT_MARGIN_COLS (w)
4692 + WINDOW_RIGHT_MARGIN_COLS (w));
4694 return width;
4698 /************************************************************************
4699 Window Scrolling
4700 ***********************************************************************/
4702 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4703 N screen-fulls, which is defined as the height of the window minus
4704 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4705 instead. Negative values of N mean scroll down. NOERROR non-zero
4706 means don't signal an error if we try to move over BEGV or ZV,
4707 respectively. */
4709 static void
4710 window_scroll (Lisp_Object window, int n, int whole, int noerror)
4712 immediate_quit = 1;
4714 /* If we must, use the pixel-based version which is much slower than
4715 the line-based one but can handle varying line heights. */
4716 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4717 window_scroll_pixel_based (window, n, whole, noerror);
4718 else
4719 window_scroll_line_based (window, n, whole, noerror);
4721 immediate_quit = 0;
4725 /* Implementation of window_scroll that works based on pixel line
4726 heights. See the comment of window_scroll for parameter
4727 descriptions. */
4729 static void
4730 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4732 struct it it;
4733 struct window *w = XWINDOW (window);
4734 struct text_pos start;
4735 int this_scroll_margin;
4736 /* True if we fiddled the window vscroll field without really scrolling. */
4737 int vscrolled = 0;
4738 int x, y, rtop, rbot, rowh, vpos;
4740 SET_TEXT_POS_FROM_MARKER (start, w->start);
4742 /* If PT is not visible in WINDOW, move back one half of
4743 the screen. Allow PT to be partially visible, otherwise
4744 something like (scroll-down 1) with PT in the line before
4745 the partially visible one would recenter. */
4747 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4749 /* Move backward half the height of the window. Performance note:
4750 vmotion used here is about 10% faster, but would give wrong
4751 results for variable height lines. */
4752 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4753 it.current_y = it.last_visible_y;
4754 move_it_vertically_backward (&it, window_box_height (w) / 2);
4756 /* The function move_iterator_vertically may move over more than
4757 the specified y-distance. If it->w is small, e.g. a
4758 mini-buffer window, we may end up in front of the window's
4759 display area. This is the case when Start displaying at the
4760 start of the line containing PT in this case. */
4761 if (it.current_y <= 0)
4763 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4764 move_it_vertically_backward (&it, 0);
4765 it.current_y = 0;
4768 start = it.current.pos;
4770 else if (auto_window_vscroll_p)
4772 if (rtop || rbot) /* partially visible */
4774 int px;
4775 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4776 if (whole)
4777 dy = max ((window_box_height (w)
4778 - next_screen_context_lines * dy),
4779 dy);
4780 dy *= n;
4782 if (n < 0)
4784 /* Only vscroll backwards if already vscrolled forwards. */
4785 if (w->vscroll < 0 && rtop > 0)
4787 px = max (0, -w->vscroll - min (rtop, -dy));
4788 Fset_window_vscroll (window, make_number (px), Qt);
4789 return;
4792 if (n > 0)
4794 /* Do vscroll if already vscrolled or only display line. */
4795 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4797 px = max (0, -w->vscroll + min (rbot, dy));
4798 Fset_window_vscroll (window, make_number (px), Qt);
4799 return;
4802 /* Maybe modify window start instead of scrolling. */
4803 if (rbot > 0 || w->vscroll < 0)
4805 EMACS_INT spos;
4807 Fset_window_vscroll (window, make_number (0), Qt);
4808 /* If there are other text lines above the current row,
4809 move window start to current row. Else to next row. */
4810 if (rbot > 0)
4811 spos = XINT (Fline_beginning_position (Qnil));
4812 else
4813 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4814 set_marker_restricted (w->start, make_number (spos),
4815 w->buffer);
4816 w->start_at_line_beg = Qt;
4817 w->update_mode_line = Qt;
4818 XSETFASTINT (w->last_modified, 0);
4819 XSETFASTINT (w->last_overlay_modified, 0);
4820 /* Set force_start so that redisplay_window will run the
4821 window-scroll-functions. */
4822 w->force_start = Qt;
4823 return;
4827 /* Cancel previous vscroll. */
4828 Fset_window_vscroll (window, make_number (0), Qt);
4831 /* If scroll_preserve_screen_position is non-nil, we try to set
4832 point in the same window line as it is now, so get that line. */
4833 if (!NILP (Vscroll_preserve_screen_position))
4835 /* We preserve the goal pixel coordinate across consecutive
4836 calls to scroll-up, scroll-down and other commands that
4837 have the `scroll-command' property. This avoids the
4838 possibility of point becoming "stuck" on a tall line when
4839 scrolling by one line. */
4840 if (window_scroll_pixel_based_preserve_y < 0
4841 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4842 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4844 start_display (&it, w, start);
4845 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4846 window_scroll_pixel_based_preserve_y = it.current_y;
4847 window_scroll_pixel_based_preserve_x = it.current_x;
4850 else
4851 window_scroll_pixel_based_preserve_y
4852 = window_scroll_pixel_based_preserve_x = -1;
4854 /* Move iterator it from start the specified distance forward or
4855 backward. The result is the new window start. */
4856 start_display (&it, w, start);
4857 if (whole)
4859 EMACS_INT start_pos = IT_CHARPOS (it);
4860 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4861 dy = max ((window_box_height (w)
4862 - next_screen_context_lines * dy),
4863 dy) * n;
4865 /* Note that move_it_vertically always moves the iterator to the
4866 start of a line. So, if the last line doesn't have a newline,
4867 we would end up at the start of the line ending at ZV. */
4868 if (dy <= 0)
4870 move_it_vertically_backward (&it, -dy);
4871 /* Ensure we actually do move, e.g. in case we are currently
4872 looking at an image that is taller that the window height. */
4873 while (start_pos == IT_CHARPOS (it)
4874 && start_pos > BEGV)
4875 move_it_by_lines (&it, -1);
4877 else if (dy > 0)
4879 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4880 MOVE_TO_POS | MOVE_TO_Y);
4881 /* Ensure we actually do move, e.g. in case we are currently
4882 looking at an image that is taller that the window height. */
4883 while (start_pos == IT_CHARPOS (it)
4884 && start_pos < ZV)
4885 move_it_by_lines (&it, 1);
4888 else
4889 move_it_by_lines (&it, n);
4891 /* We failed if we find ZV is already on the screen (scrolling up,
4892 means there's nothing past the end), or if we can't start any
4893 earlier (scrolling down, means there's nothing past the top). */
4894 if ((n > 0 && IT_CHARPOS (it) == ZV)
4895 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4897 if (IT_CHARPOS (it) == ZV)
4899 if (it.current_y < it.last_visible_y
4900 && (it.current_y + it.max_ascent + it.max_descent
4901 > it.last_visible_y))
4903 /* The last line was only partially visible, make it fully
4904 visible. */
4905 w->vscroll = (it.last_visible_y
4906 - it.current_y + it.max_ascent + it.max_descent);
4907 adjust_glyphs (it.f);
4909 else if (noerror)
4910 return;
4911 else if (n < 0) /* could happen with empty buffers */
4912 xsignal0 (Qbeginning_of_buffer);
4913 else
4914 xsignal0 (Qend_of_buffer);
4916 else
4918 if (w->vscroll != 0)
4919 /* The first line was only partially visible, make it fully
4920 visible. */
4921 w->vscroll = 0;
4922 else if (noerror)
4923 return;
4924 else
4925 xsignal0 (Qbeginning_of_buffer);
4928 /* If control gets here, then we vscrolled. */
4930 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4932 /* Don't try to change the window start below. */
4933 vscrolled = 1;
4936 if (! vscrolled)
4938 EMACS_INT pos = IT_CHARPOS (it);
4939 EMACS_INT bytepos;
4941 /* If in the middle of a multi-glyph character move forward to
4942 the next character. */
4943 if (in_display_vector_p (&it))
4945 ++pos;
4946 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4949 /* Set the window start, and set up the window for redisplay. */
4950 set_marker_restricted (w->start, make_number (pos),
4951 w->buffer);
4952 bytepos = XMARKER (w->start)->bytepos;
4953 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4954 ? Qt : Qnil);
4955 w->update_mode_line = Qt;
4956 XSETFASTINT (w->last_modified, 0);
4957 XSETFASTINT (w->last_overlay_modified, 0);
4958 /* Set force_start so that redisplay_window will run the
4959 window-scroll-functions. */
4960 w->force_start = Qt;
4963 /* The rest of this function uses current_y in a nonstandard way,
4964 not including the height of the header line if any. */
4965 it.current_y = it.vpos = 0;
4967 /* Move PT out of scroll margins.
4968 This code wants current_y to be zero at the window start position
4969 even if there is a header line. */
4970 this_scroll_margin = max (0, scroll_margin);
4971 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4972 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4974 if (n > 0)
4976 /* We moved the window start towards ZV, so PT may be now
4977 in the scroll margin at the top. */
4978 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4979 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4980 && (NILP (Vscroll_preserve_screen_position)
4981 || EQ (Vscroll_preserve_screen_position, Qt)))
4982 /* We found PT at a legitimate height. Leave it alone. */
4984 else if (window_scroll_pixel_based_preserve_y >= 0)
4986 /* If we have a header line, take account of it.
4987 This is necessary because we set it.current_y to 0, above. */
4988 move_it_to (&it, -1,
4989 window_scroll_pixel_based_preserve_x,
4990 window_scroll_pixel_based_preserve_y
4991 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4992 -1, MOVE_TO_Y | MOVE_TO_X);
4993 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4995 else
4997 while (it.current_y < this_scroll_margin)
4999 int prev = it.current_y;
5000 move_it_by_lines (&it, 1);
5001 if (prev == it.current_y)
5002 break;
5004 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5007 else if (n < 0)
5009 EMACS_INT charpos, bytepos;
5010 int partial_p;
5012 /* Save our position, for the
5013 window_scroll_pixel_based_preserve_y case. */
5014 charpos = IT_CHARPOS (it);
5015 bytepos = IT_BYTEPOS (it);
5017 /* We moved the window start towards BEGV, so PT may be now
5018 in the scroll margin at the bottom. */
5019 move_it_to (&it, PT, -1,
5020 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5021 - this_scroll_margin - 1),
5023 MOVE_TO_POS | MOVE_TO_Y);
5025 /* Save our position, in case it's correct. */
5026 charpos = IT_CHARPOS (it);
5027 bytepos = IT_BYTEPOS (it);
5029 /* See if point is on a partially visible line at the end. */
5030 if (it.what == IT_EOB)
5031 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5032 else
5034 move_it_by_lines (&it, 1);
5035 partial_p = it.current_y > it.last_visible_y;
5038 if (charpos == PT && !partial_p
5039 && (NILP (Vscroll_preserve_screen_position)
5040 || EQ (Vscroll_preserve_screen_position, Qt)))
5041 /* We found PT before we found the display margin, so PT is ok. */
5043 else if (window_scroll_pixel_based_preserve_y >= 0)
5045 SET_TEXT_POS_FROM_MARKER (start, w->start);
5046 start_display (&it, w, start);
5047 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5048 here because we called start_display again and did not
5049 alter it.current_y this time. */
5050 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5051 window_scroll_pixel_based_preserve_y, -1,
5052 MOVE_TO_Y | MOVE_TO_X);
5053 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5055 else
5057 if (partial_p)
5058 /* The last line was only partially visible, so back up two
5059 lines to make sure we're on a fully visible line. */
5061 move_it_by_lines (&it, -2);
5062 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5064 else
5065 /* No, the position we saved is OK, so use it. */
5066 SET_PT_BOTH (charpos, bytepos);
5072 /* Implementation of window_scroll that works based on screen lines.
5073 See the comment of window_scroll for parameter descriptions. */
5075 static void
5076 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
5078 register struct window *w = XWINDOW (window);
5079 /* Fvertical_motion enters redisplay, which can trigger
5080 fontification, which in turn can modify buffer text (e.g., if the
5081 fontification functions replace escape sequences with faces, as
5082 in `grep-mode-font-lock-keywords'). So we use a marker to record
5083 the old point position, to prevent crashes in SET_PT_BOTH. */
5084 Lisp_Object opoint_marker = Fpoint_marker ();
5085 register EMACS_INT pos, pos_byte;
5086 register int ht = window_internal_height (w);
5087 register Lisp_Object tem;
5088 int lose;
5089 Lisp_Object bolp;
5090 EMACS_INT startpos;
5091 Lisp_Object original_pos = Qnil;
5093 /* If scrolling screen-fulls, compute the number of lines to
5094 scroll from the window's height. */
5095 if (whole)
5096 n *= max (1, ht - next_screen_context_lines);
5098 startpos = marker_position (w->start);
5100 if (!NILP (Vscroll_preserve_screen_position))
5102 if (window_scroll_preserve_vpos <= 0
5103 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5104 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5106 struct position posit
5107 = *compute_motion (startpos, 0, 0, 0,
5108 PT, ht, 0,
5109 -1, XINT (w->hscroll),
5110 0, w);
5111 window_scroll_preserve_vpos = posit.vpos;
5112 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
5115 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5116 make_number (window_scroll_preserve_vpos));
5119 XSETFASTINT (tem, PT);
5120 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5122 if (NILP (tem))
5124 Fvertical_motion (make_number (- (ht / 2)), window);
5125 startpos = PT;
5128 SET_PT (startpos);
5129 lose = n < 0 && PT == BEGV;
5130 Fvertical_motion (make_number (n), window);
5131 pos = PT;
5132 pos_byte = PT_BYTE;
5133 bolp = Fbolp ();
5134 SET_PT_BOTH (marker_position (opoint_marker),
5135 marker_byte_position (opoint_marker));
5137 if (lose)
5139 if (noerror)
5140 return;
5141 else
5142 xsignal0 (Qbeginning_of_buffer);
5145 if (pos < ZV)
5147 int this_scroll_margin = scroll_margin;
5149 /* Don't use a scroll margin that is negative or too large. */
5150 if (this_scroll_margin < 0)
5151 this_scroll_margin = 0;
5153 if (XINT (w->total_lines) < 4 * scroll_margin)
5154 this_scroll_margin = XINT (w->total_lines) / 4;
5156 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5157 w->start_at_line_beg = bolp;
5158 w->update_mode_line = Qt;
5159 XSETFASTINT (w->last_modified, 0);
5160 XSETFASTINT (w->last_overlay_modified, 0);
5161 /* Set force_start so that redisplay_window will run
5162 the window-scroll-functions. */
5163 w->force_start = Qt;
5165 if (!NILP (Vscroll_preserve_screen_position)
5166 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5168 SET_PT_BOTH (pos, pos_byte);
5169 Fvertical_motion (original_pos, window);
5171 /* If we scrolled forward, put point enough lines down
5172 that it is outside the scroll margin. */
5173 else if (n > 0)
5175 int top_margin;
5177 if (this_scroll_margin > 0)
5179 SET_PT_BOTH (pos, pos_byte);
5180 Fvertical_motion (make_number (this_scroll_margin), window);
5181 top_margin = PT;
5183 else
5184 top_margin = pos;
5186 if (top_margin <= marker_position (opoint_marker))
5187 SET_PT_BOTH (marker_position (opoint_marker),
5188 marker_byte_position (opoint_marker));
5189 else if (!NILP (Vscroll_preserve_screen_position))
5191 SET_PT_BOTH (pos, pos_byte);
5192 Fvertical_motion (original_pos, window);
5194 else
5195 SET_PT (top_margin);
5197 else if (n < 0)
5199 int bottom_margin;
5201 /* If we scrolled backward, put point near the end of the window
5202 but not within the scroll margin. */
5203 SET_PT_BOTH (pos, pos_byte);
5204 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5205 if (XFASTINT (tem) == ht - this_scroll_margin)
5206 bottom_margin = PT;
5207 else
5208 bottom_margin = PT + 1;
5210 if (bottom_margin > marker_position (opoint_marker))
5211 SET_PT_BOTH (marker_position (opoint_marker),
5212 marker_byte_position (opoint_marker));
5213 else
5215 if (!NILP (Vscroll_preserve_screen_position))
5217 SET_PT_BOTH (pos, pos_byte);
5218 Fvertical_motion (original_pos, window);
5220 else
5221 Fvertical_motion (make_number (-1), window);
5225 else
5227 if (noerror)
5228 return;
5229 else
5230 xsignal0 (Qend_of_buffer);
5235 /* Scroll selected_window up or down. If N is nil, scroll a
5236 screen-full which is defined as the height of the window minus
5237 next_screen_context_lines. If N is the symbol `-', scroll.
5238 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5239 up. This is the guts of Fscroll_up and Fscroll_down. */
5241 static void
5242 scroll_command (Lisp_Object n, int direction)
5244 int count = SPECPDL_INDEX ();
5246 xassert (eabs (direction) == 1);
5248 /* If selected window's buffer isn't current, make it current for
5249 the moment. But don't screw up if window_scroll gets an error. */
5250 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5252 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5253 Fset_buffer (XWINDOW (selected_window)->buffer);
5255 /* Make redisplay consider other windows than just selected_window. */
5256 ++windows_or_buffers_changed;
5259 if (NILP (n))
5260 window_scroll (selected_window, direction, 1, 0);
5261 else if (EQ (n, Qminus))
5262 window_scroll (selected_window, -direction, 1, 0);
5263 else
5265 n = Fprefix_numeric_value (n);
5266 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5269 unbind_to (count, Qnil);
5272 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5273 doc: /* Scroll text of selected window upward ARG lines.
5274 If ARG is omitted or nil, scroll upward 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 downward.
5277 If ARG is the atom `-', scroll downward 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 ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5286 doc: /* Scroll text of selected window down ARG lines.
5287 If ARG is omitted or nil, scroll down by a near full screen.
5288 A near full screen is `next-screen-context-lines' less than a full screen.
5289 Negative ARG means scroll upward.
5290 If ARG is the atom `-', scroll upward by nearly full screen.
5291 When calling from a program, supply as argument a number, nil, or `-'. */)
5292 (Lisp_Object arg)
5294 scroll_command (arg, -1);
5295 return Qnil;
5298 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5299 doc: /* Return the other window for \"other window scroll\" commands.
5300 If `other-window-scroll-buffer' is non-nil, a window
5301 showing that buffer is used.
5302 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5303 specifies the window. This takes precedence over
5304 `other-window-scroll-buffer'. */)
5305 (void)
5307 Lisp_Object window;
5309 if (MINI_WINDOW_P (XWINDOW (selected_window))
5310 && !NILP (Vminibuf_scroll_window))
5311 window = Vminibuf_scroll_window;
5312 /* If buffer is specified, scroll that buffer. */
5313 else if (!NILP (Vother_window_scroll_buffer))
5315 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5316 if (NILP (window))
5317 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5319 else
5321 /* Nothing specified; look for a neighboring window on the same
5322 frame. */
5323 window = Fnext_window (selected_window, Qnil, Qnil);
5325 if (EQ (window, selected_window))
5326 /* That didn't get us anywhere; look for a window on another
5327 visible frame. */
5329 window = Fnext_window (window, Qnil, Qt);
5330 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5331 && ! EQ (window, selected_window));
5334 CHECK_LIVE_WINDOW (window);
5336 if (EQ (window, selected_window))
5337 error ("There is no other window");
5339 return window;
5342 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5343 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5344 A near full screen is `next-screen-context-lines' less than a full screen.
5345 The next window is the one below the current one; or the one at the top
5346 if the current one is at the bottom. Negative ARG means scroll downward.
5347 If ARG is the atom `-', scroll downward by nearly full screen.
5348 When calling from a program, supply as argument a number, nil, or `-'.
5350 If `other-window-scroll-buffer' is non-nil, scroll the window
5351 showing that buffer, popping the buffer up if necessary.
5352 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5353 specifies the window to scroll. This takes precedence over
5354 `other-window-scroll-buffer'. */)
5355 (Lisp_Object arg)
5357 Lisp_Object window;
5358 struct window *w;
5359 int count = SPECPDL_INDEX ();
5361 window = Fother_window_for_scrolling ();
5362 w = XWINDOW (window);
5364 /* Don't screw up if window_scroll gets an error. */
5365 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5366 ++windows_or_buffers_changed;
5368 Fset_buffer (w->buffer);
5369 SET_PT (marker_position (w->pointm));
5371 if (NILP (arg))
5372 window_scroll (window, 1, 1, 1);
5373 else if (EQ (arg, Qminus))
5374 window_scroll (window, -1, 1, 1);
5375 else
5377 if (CONSP (arg))
5378 arg = Fcar (arg);
5379 CHECK_NUMBER (arg);
5380 window_scroll (window, XINT (arg), 0, 1);
5383 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5384 unbind_to (count, Qnil);
5386 return Qnil;
5389 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5390 doc: /* Scroll selected window display ARG columns left.
5391 Default for ARG is window width minus 2.
5392 Value is the total amount of leftward horizontal scrolling in
5393 effect after the change.
5394 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5395 lower bound for automatic scrolling, i.e. automatic scrolling
5396 will not scroll a window to a column less than the value returned
5397 by this function. This happens in an interactive call. */)
5398 (register Lisp_Object arg, Lisp_Object set_minimum)
5400 Lisp_Object result;
5401 int hscroll;
5402 struct window *w = XWINDOW (selected_window);
5404 if (NILP (arg))
5405 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5406 else
5407 arg = Fprefix_numeric_value (arg);
5409 hscroll = XINT (w->hscroll) + XINT (arg);
5410 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5412 if (!NILP (set_minimum))
5413 w->min_hscroll = w->hscroll;
5415 return result;
5418 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5419 doc: /* Scroll selected window display ARG columns right.
5420 Default for ARG is window width minus 2.
5421 Value is the total amount of leftward horizontal scrolling in
5422 effect after the change.
5423 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5424 lower bound for automatic scrolling, i.e. automatic scrolling
5425 will not scroll a window to a column less than the value returned
5426 by this function. This happens in an interactive call. */)
5427 (register Lisp_Object arg, Lisp_Object set_minimum)
5429 Lisp_Object result;
5430 int hscroll;
5431 struct window *w = XWINDOW (selected_window);
5433 if (NILP (arg))
5434 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5435 else
5436 arg = Fprefix_numeric_value (arg);
5438 hscroll = XINT (w->hscroll) - XINT (arg);
5439 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5441 if (!NILP (set_minimum))
5442 w->min_hscroll = w->hscroll;
5444 return result;
5447 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5448 doc: /* Return the window which was selected when entering the minibuffer.
5449 Returns nil, if selected window is not a minibuffer window. */)
5450 (void)
5452 if (minibuf_level > 0
5453 && MINI_WINDOW_P (XWINDOW (selected_window))
5454 && WINDOW_LIVE_P (minibuf_selected_window))
5455 return minibuf_selected_window;
5457 return Qnil;
5460 /* Value is the number of lines actually displayed in window W,
5461 as opposed to its height. */
5463 static int
5464 displayed_window_lines (struct window *w)
5466 struct it it;
5467 struct text_pos start;
5468 int height = window_box_height (w);
5469 struct buffer *old_buffer;
5470 int bottom_y;
5472 if (XBUFFER (w->buffer) != current_buffer)
5474 old_buffer = current_buffer;
5475 set_buffer_internal (XBUFFER (w->buffer));
5477 else
5478 old_buffer = NULL;
5480 /* In case W->start is out of the accessible range, do something
5481 reasonable. This happens in Info mode when Info-scroll-down
5482 calls (recenter -1) while W->start is 1. */
5483 if (XMARKER (w->start)->charpos < BEGV)
5484 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5485 else if (XMARKER (w->start)->charpos > ZV)
5486 SET_TEXT_POS (start, ZV, ZV_BYTE);
5487 else
5488 SET_TEXT_POS_FROM_MARKER (start, w->start);
5490 start_display (&it, w, start);
5491 move_it_vertically (&it, height);
5492 bottom_y = line_bottom_y (&it);
5494 /* rms: On a non-window display,
5495 the value of it.vpos at the bottom of the screen
5496 seems to be 1 larger than window_box_height (w).
5497 This kludge fixes a bug whereby (move-to-window-line -1)
5498 when ZV is on the last screen line
5499 moves to the previous screen line instead of the last one. */
5500 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5501 height++;
5503 /* Add in empty lines at the bottom of the window. */
5504 if (bottom_y < height)
5506 int uy = FRAME_LINE_HEIGHT (it.f);
5507 it.vpos += (height - bottom_y + uy - 1) / uy;
5510 if (old_buffer)
5511 set_buffer_internal (old_buffer);
5513 return it.vpos;
5517 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5518 doc: /* Center point in selected window and maybe redisplay frame.
5519 With prefix argument ARG, recenter putting point on screen line ARG
5520 relative to the selected window. If ARG is negative, it counts up from the
5521 bottom of the window. (ARG should be less than the height of the window.)
5523 If ARG is omitted or nil, then recenter with point on the middle line of
5524 the selected window; if the variable `recenter-redisplay' is non-nil,
5525 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5526 is set to `grow-only', this resets the tool-bar's height to the minimum
5527 height needed); if `recenter-redisplay' has the special value `tty',
5528 then only tty frame are redrawn.
5530 Just C-u as prefix means put point in the center of the window
5531 and redisplay normally--don't erase and redraw the frame. */)
5532 (register Lisp_Object arg)
5534 struct window *w = XWINDOW (selected_window);
5535 struct buffer *buf = XBUFFER (w->buffer);
5536 struct buffer *obuf = current_buffer;
5537 int center_p = 0;
5538 EMACS_INT charpos, bytepos;
5539 int iarg IF_LINT (= 0);
5540 int this_scroll_margin;
5542 /* If redisplay is suppressed due to an error, try again. */
5543 obuf->display_error_modiff = 0;
5545 if (NILP (arg))
5547 if (!NILP (Vrecenter_redisplay)
5548 && (!EQ (Vrecenter_redisplay, Qtty)
5549 || !NILP (Ftty_type (selected_frame))))
5551 int i;
5553 /* Invalidate pixel data calculated for all compositions. */
5554 for (i = 0; i < n_compositions; i++)
5555 composition_table[i]->font = NULL;
5557 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5559 Fredraw_frame (WINDOW_FRAME (w));
5560 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5563 center_p = 1;
5565 else if (CONSP (arg)) /* Just C-u. */
5566 center_p = 1;
5567 else
5569 arg = Fprefix_numeric_value (arg);
5570 CHECK_NUMBER (arg);
5571 iarg = XINT (arg);
5574 set_buffer_internal (buf);
5576 /* Do this after making BUF current
5577 in case scroll_margin is buffer-local. */
5578 this_scroll_margin = max (0, scroll_margin);
5579 this_scroll_margin = min (this_scroll_margin,
5580 XFASTINT (w->total_lines) / 4);
5582 /* Handle centering on a graphical frame specially. Such frames can
5583 have variable-height lines and centering point on the basis of
5584 line counts would lead to strange effects. */
5585 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5587 if (center_p)
5589 struct it it;
5590 struct text_pos pt;
5592 SET_TEXT_POS (pt, PT, PT_BYTE);
5593 start_display (&it, w, pt);
5594 move_it_vertically_backward (&it, window_box_height (w) / 2);
5595 charpos = IT_CHARPOS (it);
5596 bytepos = IT_BYTEPOS (it);
5598 else if (iarg < 0)
5600 struct it it;
5601 struct text_pos pt;
5602 int nlines = -iarg;
5603 int extra_line_spacing;
5604 int h = window_box_height (w);
5606 iarg = - max (-iarg, this_scroll_margin);
5608 SET_TEXT_POS (pt, PT, PT_BYTE);
5609 start_display (&it, w, pt);
5611 /* Be sure we have the exact height of the full line containing PT. */
5612 move_it_by_lines (&it, 0);
5614 /* The amount of pixels we have to move back is the window
5615 height minus what's displayed in the line containing PT,
5616 and the lines below. */
5617 it.current_y = 0;
5618 it.vpos = 0;
5619 move_it_by_lines (&it, nlines);
5621 if (it.vpos == nlines)
5622 h -= it.current_y;
5623 else
5625 /* Last line has no newline */
5626 h -= line_bottom_y (&it);
5627 it.vpos++;
5630 /* Don't reserve space for extra line spacing of last line. */
5631 extra_line_spacing = it.max_extra_line_spacing;
5633 /* If we can't move down NLINES lines because we hit
5634 the end of the buffer, count in some empty lines. */
5635 if (it.vpos < nlines)
5637 nlines -= it.vpos;
5638 extra_line_spacing = it.extra_line_spacing;
5639 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5641 if (h <= 0)
5642 return Qnil;
5644 /* Now find the new top line (starting position) of the window. */
5645 start_display (&it, w, pt);
5646 it.current_y = 0;
5647 move_it_vertically_backward (&it, h);
5649 /* If extra line spacing is present, we may move too far
5650 back. This causes the last line to be only partially
5651 visible (which triggers redisplay to recenter that line
5652 in the middle), so move forward.
5653 But ignore extra line spacing on last line, as it is not
5654 considered to be part of the visible height of the line.
5656 h += extra_line_spacing;
5657 while (-it.current_y > h)
5658 move_it_by_lines (&it, 1);
5660 charpos = IT_CHARPOS (it);
5661 bytepos = IT_BYTEPOS (it);
5663 else
5665 struct position pos;
5667 iarg = max (iarg, this_scroll_margin);
5669 pos = *vmotion (PT, -iarg, w);
5670 charpos = pos.bufpos;
5671 bytepos = pos.bytepos;
5674 else
5676 struct position pos;
5677 int ht = window_internal_height (w);
5679 if (center_p)
5680 iarg = ht / 2;
5681 else if (iarg < 0)
5682 iarg += ht;
5684 /* Don't let it get into the margin at either top or bottom. */
5685 iarg = max (iarg, this_scroll_margin);
5686 iarg = min (iarg, ht - this_scroll_margin - 1);
5688 pos = *vmotion (PT, - iarg, w);
5689 charpos = pos.bufpos;
5690 bytepos = pos.bytepos;
5693 /* Set the new window start. */
5694 set_marker_both (w->start, w->buffer, charpos, bytepos);
5695 w->window_end_valid = Qnil;
5697 w->optional_new_start = Qt;
5699 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5700 w->start_at_line_beg = Qt;
5701 else
5702 w->start_at_line_beg = Qnil;
5704 set_buffer_internal (obuf);
5705 return Qnil;
5709 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5710 0, 1, 0,
5711 doc: /* Return the height in lines of the text display area of WINDOW.
5712 WINDOW defaults to the selected window.
5714 The return value does not include the mode line, any header line, nor
5715 any partial-height lines in the text display area. */)
5716 (Lisp_Object window)
5718 struct window *w = decode_window (window);
5719 int pixel_height = window_box_height (w);
5720 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5721 return make_number (line_height);
5726 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5727 1, 1, "P",
5728 doc: /* Position point relative to window.
5729 With no argument, position point at center of window.
5730 An argument specifies vertical position within the window;
5731 zero means top of window, negative means relative to bottom of window. */)
5732 (Lisp_Object arg)
5734 struct window *w = XWINDOW (selected_window);
5735 int lines, start;
5736 Lisp_Object window;
5737 #if 0
5738 int this_scroll_margin;
5739 #endif
5741 if (!(BUFFERP (w->buffer)
5742 && XBUFFER (w->buffer) == current_buffer))
5743 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5744 when passed below to set_marker_both. */
5745 error ("move-to-window-line called from unrelated buffer");
5747 window = selected_window;
5748 start = marker_position (w->start);
5749 if (start < BEGV || start > ZV)
5751 int height = window_internal_height (w);
5752 Fvertical_motion (make_number (- (height / 2)), window);
5753 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5754 w->start_at_line_beg = Fbolp ();
5755 w->force_start = Qt;
5757 else
5758 Fgoto_char (w->start);
5760 lines = displayed_window_lines (w);
5762 #if 0
5763 this_scroll_margin = max (0, scroll_margin);
5764 this_scroll_margin = min (this_scroll_margin, lines / 4);
5765 #endif
5767 if (NILP (arg))
5768 XSETFASTINT (arg, lines / 2);
5769 else
5771 int iarg = XINT (Fprefix_numeric_value (arg));
5773 if (iarg < 0)
5774 iarg = iarg + lines;
5776 #if 0 /* This code would prevent move-to-window-line from moving point
5777 to a place inside the scroll margins (which would cause the
5778 next redisplay to scroll). I wrote this code, but then concluded
5779 it is probably better not to install it. However, it is here
5780 inside #if 0 so as not to lose it. -- rms. */
5782 /* Don't let it get into the margin at either top or bottom. */
5783 iarg = max (iarg, this_scroll_margin);
5784 iarg = min (iarg, lines - this_scroll_margin - 1);
5785 #endif
5787 arg = make_number (iarg);
5790 /* Skip past a partially visible first line. */
5791 if (w->vscroll)
5792 XSETINT (arg, XINT (arg) + 1);
5794 return Fvertical_motion (arg, window);
5799 /***********************************************************************
5800 Window Configuration
5801 ***********************************************************************/
5803 struct save_window_data
5805 struct vectorlike_header header;
5806 Lisp_Object selected_frame;
5807 Lisp_Object current_window;
5808 Lisp_Object current_buffer;
5809 Lisp_Object minibuf_scroll_window;
5810 Lisp_Object minibuf_selected_window;
5811 Lisp_Object root_window;
5812 Lisp_Object focus_frame;
5813 /* A vector, each of whose elements is a struct saved_window
5814 for one window. */
5815 Lisp_Object saved_windows;
5817 /* All fields above are traced by the GC.
5818 From `fame-cols' down, the fields are ignored by the GC. */
5820 int frame_cols, frame_lines, frame_menu_bar_lines;
5821 int frame_tool_bar_lines;
5824 /* This is saved as a Lisp_Vector */
5825 struct saved_window
5827 struct vectorlike_header header;
5828 Lisp_Object window;
5829 Lisp_Object buffer, start, pointm, mark;
5830 Lisp_Object left_col, top_line, total_cols, total_lines;
5831 Lisp_Object hscroll, min_hscroll;
5832 Lisp_Object parent, prev;
5833 Lisp_Object start_at_line_beg;
5834 Lisp_Object display_table;
5835 Lisp_Object orig_top_line, orig_total_lines;
5836 Lisp_Object left_margin_cols, right_margin_cols;
5837 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5838 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5839 Lisp_Object dedicated, resize_proportionally;
5842 #define SAVED_WINDOW_N(swv,n) \
5843 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5845 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5846 doc: /* Return t if OBJECT is a window-configuration object. */)
5847 (Lisp_Object object)
5849 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5852 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5853 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5854 (Lisp_Object config)
5856 register struct save_window_data *data;
5857 struct Lisp_Vector *saved_windows;
5859 CHECK_WINDOW_CONFIGURATION (config);
5861 data = (struct save_window_data *) XVECTOR (config);
5862 saved_windows = XVECTOR (data->saved_windows);
5863 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5866 DEFUN ("set-window-configuration", Fset_window_configuration,
5867 Sset_window_configuration, 1, 1, 0,
5868 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5869 CONFIGURATION must be a value previously returned
5870 by `current-window-configuration' (which see).
5871 If CONFIGURATION was made from a frame that is now deleted,
5872 only frame-independent values can be restored. In this case,
5873 the return value is nil. Otherwise the value is t. */)
5874 (Lisp_Object configuration)
5876 register struct save_window_data *data;
5877 struct Lisp_Vector *saved_windows;
5878 Lisp_Object new_current_buffer;
5879 Lisp_Object frame;
5880 FRAME_PTR f;
5881 EMACS_INT old_point = -1;
5883 CHECK_WINDOW_CONFIGURATION (configuration);
5885 data = (struct save_window_data *) XVECTOR (configuration);
5886 saved_windows = XVECTOR (data->saved_windows);
5888 new_current_buffer = data->current_buffer;
5889 if (NILP (BVAR (XBUFFER (new_current_buffer), name)))
5890 new_current_buffer = Qnil;
5891 else
5893 if (XBUFFER (new_current_buffer) == current_buffer)
5894 /* The code further down "preserves point" by saving here PT in
5895 old_point and then setting it later back into PT. When the
5896 current-selected-window and the final-selected-window both show
5897 the current buffer, this suffers from the problem that the
5898 current PT is the window-point of the current-selected-window,
5899 while the final PT is the point of the final-selected-window, so
5900 this copy from one PT to the other would end up moving the
5901 window-point of the final-selected-window to the window-point of
5902 the current-selected-window. So we have to be careful which
5903 point of the current-buffer we copy into old_point. */
5904 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5905 && WINDOWP (selected_window)
5906 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5907 && !EQ (selected_window, data->current_window))
5908 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5909 else
5910 old_point = PT;
5911 else
5912 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5913 point in new_current_buffer as of the last time this buffer was
5914 used. This can be non-deterministic since it can be changed by
5915 things like jit-lock by mere temporary selection of some random
5916 window that happens to show this buffer.
5917 So if possible we want this arbitrary choice of "which point" to
5918 be the one from the to-be-selected-window so as to prevent this
5919 window's cursor from being copied from another window. */
5920 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5921 /* If current_window = selected_window, its point is in BUF_PT. */
5922 && !EQ (selected_window, data->current_window))
5923 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5924 else
5925 old_point = BUF_PT (XBUFFER (new_current_buffer));
5928 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5929 f = XFRAME (frame);
5931 /* If f is a dead frame, don't bother rebuilding its window tree.
5932 However, there is other stuff we should still try to do below. */
5933 if (FRAME_LIVE_P (f))
5935 register struct window *w;
5936 register struct saved_window *p;
5937 struct window *root_window;
5938 struct window **leaf_windows;
5939 int n_leaf_windows;
5940 int k, i, n;
5942 /* If the frame has been resized since this window configuration was
5943 made, we change the frame to the size specified in the
5944 configuration, restore the configuration, and then resize it
5945 back. We keep track of the prevailing height in these variables. */
5946 int previous_frame_lines = FRAME_LINES (f);
5947 int previous_frame_cols = FRAME_COLS (f);
5948 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5949 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5951 /* The mouse highlighting code could get screwed up
5952 if it runs during this. */
5953 BLOCK_INPUT;
5955 if (data->frame_lines != previous_frame_lines
5956 || data->frame_cols != previous_frame_cols)
5957 change_frame_size (f, data->frame_lines,
5958 data->frame_cols, 0, 0, 0);
5959 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5960 if (data->frame_menu_bar_lines
5961 != previous_frame_menu_bar_lines)
5962 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5963 make_number (0));
5964 #ifdef HAVE_WINDOW_SYSTEM
5965 if (data->frame_tool_bar_lines
5966 != previous_frame_tool_bar_lines)
5967 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5968 make_number (0));
5969 #endif
5970 #endif
5972 /* "Swap out" point from the selected window's buffer
5973 into the window itself. (Normally the pointm of the selected
5974 window holds garbage.) We do this now, before
5975 restoring the window contents, and prevent it from
5976 being done later on when we select a new window. */
5977 if (! NILP (XWINDOW (selected_window)->buffer))
5979 w = XWINDOW (selected_window);
5980 set_marker_both (w->pointm,
5981 w->buffer,
5982 BUF_PT (XBUFFER (w->buffer)),
5983 BUF_PT_BYTE (XBUFFER (w->buffer)));
5986 windows_or_buffers_changed++;
5987 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5989 /* Problem: Freeing all matrices and later allocating them again
5990 is a serious redisplay flickering problem. What we would
5991 really like to do is to free only those matrices not reused
5992 below. */
5993 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5994 leaf_windows
5995 = (struct window **) alloca (count_windows (root_window)
5996 * sizeof (struct window *));
5997 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5999 /* Kludge Alert!
6000 Mark all windows now on frame as "deleted".
6001 Restoring the new configuration "undeletes" any that are in it.
6003 Save their current buffers in their height fields, since we may
6004 need it later, if a buffer saved in the configuration is now
6005 dead. */
6006 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6008 for (k = 0; k < saved_windows->header.size; k++)
6010 p = SAVED_WINDOW_N (saved_windows, k);
6011 w = XWINDOW (p->window);
6012 w->next = Qnil;
6014 if (!NILP (p->parent))
6015 w->parent = SAVED_WINDOW_N (saved_windows,
6016 XFASTINT (p->parent))->window;
6017 else
6018 w->parent = Qnil;
6020 if (!NILP (p->prev))
6022 w->prev = SAVED_WINDOW_N (saved_windows,
6023 XFASTINT (p->prev))->window;
6024 XWINDOW (w->prev)->next = p->window;
6026 else
6028 w->prev = Qnil;
6029 if (!NILP (w->parent))
6031 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6033 XWINDOW (w->parent)->vchild = p->window;
6034 XWINDOW (w->parent)->hchild = Qnil;
6036 else
6038 XWINDOW (w->parent)->hchild = p->window;
6039 XWINDOW (w->parent)->vchild = Qnil;
6044 /* If we squirreled away the buffer in the window's height,
6045 restore it now. */
6046 if (BUFFERP (w->total_lines))
6047 w->buffer = w->total_lines;
6048 w->left_col = p->left_col;
6049 w->top_line = p->top_line;
6050 w->total_cols = p->total_cols;
6051 w->total_lines = p->total_lines;
6052 w->hscroll = p->hscroll;
6053 w->min_hscroll = p->min_hscroll;
6054 w->display_table = p->display_table;
6055 w->orig_top_line = p->orig_top_line;
6056 w->orig_total_lines = p->orig_total_lines;
6057 w->left_margin_cols = p->left_margin_cols;
6058 w->right_margin_cols = p->right_margin_cols;
6059 w->left_fringe_width = p->left_fringe_width;
6060 w->right_fringe_width = p->right_fringe_width;
6061 w->fringes_outside_margins = p->fringes_outside_margins;
6062 w->scroll_bar_width = p->scroll_bar_width;
6063 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6064 w->dedicated = p->dedicated;
6065 w->resize_proportionally = p->resize_proportionally;
6066 XSETFASTINT (w->last_modified, 0);
6067 XSETFASTINT (w->last_overlay_modified, 0);
6069 /* Reinstall the saved buffer and pointers into it. */
6070 if (NILP (p->buffer))
6071 w->buffer = p->buffer;
6072 else
6074 if (!NILP (BVAR (XBUFFER (p->buffer), name)))
6075 /* If saved buffer is alive, install it. */
6077 w->buffer = p->buffer;
6078 w->start_at_line_beg = p->start_at_line_beg;
6079 set_marker_restricted (w->start, p->start, w->buffer);
6080 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6081 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
6082 p->mark, w->buffer);
6084 /* As documented in Fcurrent_window_configuration, don't
6085 restore the location of point in the buffer which was
6086 current when the window configuration was recorded. */
6087 if (!EQ (p->buffer, new_current_buffer)
6088 && XBUFFER (p->buffer) == current_buffer)
6089 Fgoto_char (w->pointm);
6091 else if (NILP (w->buffer) || NILP (BVAR (XBUFFER (w->buffer), name)))
6092 /* Else unless window has a live buffer, get one. */
6094 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6095 /* This will set the markers to beginning of visible
6096 range. */
6097 set_marker_restricted (w->start, make_number (0), w->buffer);
6098 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6099 w->start_at_line_beg = Qt;
6101 else
6102 /* Keeping window's old buffer; make sure the markers
6103 are real. */
6105 /* Set window markers at start of visible range. */
6106 if (XMARKER (w->start)->buffer == 0)
6107 set_marker_restricted (w->start, make_number (0),
6108 w->buffer);
6109 if (XMARKER (w->pointm)->buffer == 0)
6110 set_marker_restricted_both (w->pointm, w->buffer,
6111 BUF_PT (XBUFFER (w->buffer)),
6112 BUF_PT_BYTE (XBUFFER (w->buffer)));
6113 w->start_at_line_beg = Qt;
6118 FRAME_ROOT_WINDOW (f) = data->root_window;
6120 /* Arrange *not* to restore point in the buffer that was
6121 current when the window configuration was saved. */
6122 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6123 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6124 make_number (old_point),
6125 XWINDOW (data->current_window)->buffer);
6127 /* In the following call to `select-window, prevent "swapping
6128 out point" in the old selected window using the buffer that
6129 has been restored into it. We already swapped out that point
6130 from that window's old buffer. */
6131 select_window (data->current_window, Qnil, 1);
6132 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
6133 = selected_window;
6135 if (NILP (data->focus_frame)
6136 || (FRAMEP (data->focus_frame)
6137 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6138 Fredirect_frame_focus (frame, data->focus_frame);
6140 /* Set the screen height to the value it had before this function. */
6141 if (previous_frame_lines != FRAME_LINES (f)
6142 || previous_frame_cols != FRAME_COLS (f))
6143 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6144 0, 0, 0);
6145 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6146 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6147 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6148 make_number (0));
6149 #ifdef HAVE_WINDOW_SYSTEM
6150 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6151 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6152 make_number (0));
6153 #endif
6154 #endif
6156 /* Now, free glyph matrices in windows that were not reused. */
6157 for (i = n = 0; i < n_leaf_windows; ++i)
6159 if (NILP (leaf_windows[i]->buffer))
6161 /* Assert it's not reused as a combination. */
6162 xassert (NILP (leaf_windows[i]->hchild)
6163 && NILP (leaf_windows[i]->vchild));
6164 free_window_matrices (leaf_windows[i]);
6166 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6167 ++n;
6170 adjust_glyphs (f);
6172 UNBLOCK_INPUT;
6174 /* Fselect_window will have made f the selected frame, so we
6175 reselect the proper frame here. Fhandle_switch_frame will change the
6176 selected window too, but that doesn't make the call to
6177 Fselect_window above totally superfluous; it still sets f's
6178 selected window. */
6179 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6180 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6182 run_window_configuration_change_hook (f);
6185 if (!NILP (new_current_buffer))
6186 Fset_buffer (new_current_buffer);
6188 Vminibuf_scroll_window = data->minibuf_scroll_window;
6189 minibuf_selected_window = data->minibuf_selected_window;
6191 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6194 /* Mark all windows now on frame as deleted
6195 by setting their buffers to nil. */
6197 void
6198 delete_all_subwindows (register struct window *w)
6200 if (!NILP (w->next))
6201 delete_all_subwindows (XWINDOW (w->next));
6202 if (!NILP (w->vchild))
6203 delete_all_subwindows (XWINDOW (w->vchild));
6204 if (!NILP (w->hchild))
6205 delete_all_subwindows (XWINDOW (w->hchild));
6207 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6209 if (!NILP (w->buffer))
6210 unshow_buffer (w);
6212 /* We set all three of these fields to nil, to make sure that we can
6213 distinguish this dead window from any live window. Live leaf
6214 windows will have buffer set, and combination windows will have
6215 vchild or hchild set. */
6216 w->buffer = Qnil;
6217 w->vchild = Qnil;
6218 w->hchild = Qnil;
6220 Vwindow_list = Qnil;
6223 static int
6224 count_windows (register struct window *window)
6226 register int count = 1;
6227 if (!NILP (window->next))
6228 count += count_windows (XWINDOW (window->next));
6229 if (!NILP (window->vchild))
6230 count += count_windows (XWINDOW (window->vchild));
6231 if (!NILP (window->hchild))
6232 count += count_windows (XWINDOW (window->hchild));
6233 return count;
6237 /* Fill vector FLAT with leaf windows under W, starting at index I.
6238 Value is last index + 1. */
6240 static int
6241 get_leaf_windows (struct window *w, struct window **flat, int i)
6243 while (w)
6245 if (!NILP (w->hchild))
6246 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6247 else if (!NILP (w->vchild))
6248 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6249 else
6250 flat[i++] = w;
6252 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6255 return i;
6259 /* Return a pointer to the glyph W's physical cursor is on. Value is
6260 null if W's current matrix is invalid, so that no meaningfull glyph
6261 can be returned. */
6263 struct glyph *
6264 get_phys_cursor_glyph (struct window *w)
6266 struct glyph_row *row;
6267 struct glyph *glyph;
6269 if (w->phys_cursor.vpos >= 0
6270 && w->phys_cursor.vpos < w->current_matrix->nrows
6271 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6272 row->enabled_p)
6273 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6274 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6275 else
6276 glyph = NULL;
6278 return glyph;
6282 static int
6283 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6285 register struct saved_window *p;
6286 register struct window *w;
6287 register Lisp_Object tem;
6289 for (;!NILP (window); window = w->next)
6291 p = SAVED_WINDOW_N (vector, i);
6292 w = XWINDOW (window);
6294 XSETFASTINT (w->temslot, i); i++;
6295 p->window = window;
6296 p->buffer = w->buffer;
6297 p->left_col = w->left_col;
6298 p->top_line = w->top_line;
6299 p->total_cols = w->total_cols;
6300 p->total_lines = w->total_lines;
6301 p->hscroll = w->hscroll;
6302 p->min_hscroll = w->min_hscroll;
6303 p->display_table = w->display_table;
6304 p->orig_top_line = w->orig_top_line;
6305 p->orig_total_lines = w->orig_total_lines;
6306 p->left_margin_cols = w->left_margin_cols;
6307 p->right_margin_cols = w->right_margin_cols;
6308 p->left_fringe_width = w->left_fringe_width;
6309 p->right_fringe_width = w->right_fringe_width;
6310 p->fringes_outside_margins = w->fringes_outside_margins;
6311 p->scroll_bar_width = w->scroll_bar_width;
6312 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6313 p->dedicated = w->dedicated;
6314 p->resize_proportionally = w->resize_proportionally;
6315 if (!NILP (w->buffer))
6317 /* Save w's value of point in the window configuration.
6318 If w is the selected window, then get the value of point
6319 from the buffer; pointm is garbage in the selected window. */
6320 if (EQ (window, selected_window))
6322 p->pointm = Fmake_marker ();
6323 set_marker_both (p->pointm, w->buffer,
6324 BUF_PT (XBUFFER (w->buffer)),
6325 BUF_PT_BYTE (XBUFFER (w->buffer)));
6327 else
6328 p->pointm = Fcopy_marker (w->pointm, Qnil);
6330 p->start = Fcopy_marker (w->start, Qnil);
6331 p->start_at_line_beg = w->start_at_line_beg;
6333 tem = BVAR (XBUFFER (w->buffer), mark);
6334 p->mark = Fcopy_marker (tem, Qnil);
6336 else
6338 p->pointm = Qnil;
6339 p->start = Qnil;
6340 p->mark = Qnil;
6341 p->start_at_line_beg = Qnil;
6344 if (NILP (w->parent))
6345 p->parent = Qnil;
6346 else
6347 p->parent = XWINDOW (w->parent)->temslot;
6349 if (NILP (w->prev))
6350 p->prev = Qnil;
6351 else
6352 p->prev = XWINDOW (w->prev)->temslot;
6354 if (!NILP (w->vchild))
6355 i = save_window_save (w->vchild, vector, i);
6356 if (!NILP (w->hchild))
6357 i = save_window_save (w->hchild, vector, i);
6360 return i;
6363 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6364 Scurrent_window_configuration, 0, 1, 0,
6365 doc: /* Return an object representing the current window configuration of FRAME.
6366 If FRAME is nil or omitted, use the selected frame.
6367 This describes the number of windows, their sizes and current buffers,
6368 and for each displayed buffer, where display starts, and the positions of
6369 point and mark. An exception is made for point in the current buffer:
6370 its value is -not- saved.
6371 This also records the currently selected frame, and FRAME's focus
6372 redirection (see `redirect-frame-focus'). */)
6373 (Lisp_Object frame)
6375 register Lisp_Object tem;
6376 register int n_windows;
6377 register struct save_window_data *data;
6378 register int i;
6379 FRAME_PTR f;
6381 if (NILP (frame))
6382 frame = selected_frame;
6383 CHECK_LIVE_FRAME (frame);
6384 f = XFRAME (frame);
6386 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6387 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6388 PVEC_WINDOW_CONFIGURATION);
6390 data->frame_cols = FRAME_COLS (f);
6391 data->frame_lines = FRAME_LINES (f);
6392 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6393 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6394 data->selected_frame = selected_frame;
6395 data->current_window = FRAME_SELECTED_WINDOW (f);
6396 XSETBUFFER (data->current_buffer, current_buffer);
6397 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6398 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6399 data->root_window = FRAME_ROOT_WINDOW (f);
6400 data->focus_frame = FRAME_FOCUS_FRAME (f);
6401 tem = Fmake_vector (make_number (n_windows), Qnil);
6402 data->saved_windows = tem;
6403 for (i = 0; i < n_windows; i++)
6404 XVECTOR (tem)->contents[i]
6405 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6406 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6407 XSETWINDOW_CONFIGURATION (tem, data);
6408 return (tem);
6412 /***********************************************************************
6413 Window Split Tree
6414 ***********************************************************************/
6416 static Lisp_Object
6417 window_tree (struct window *w)
6419 Lisp_Object tail = Qnil;
6420 Lisp_Object result = Qnil;
6422 while (w)
6424 Lisp_Object wn;
6426 XSETWINDOW (wn, w);
6427 if (!NILP (w->hchild))
6428 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6429 window_tree (XWINDOW (w->hchild))));
6430 else if (!NILP (w->vchild))
6431 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6432 window_tree (XWINDOW (w->vchild))));
6434 if (NILP (result))
6436 result = tail = Fcons (wn, Qnil);
6438 else
6440 XSETCDR (tail, Fcons (wn, Qnil));
6441 tail = XCDR (tail);
6444 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6447 return result;
6452 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6453 0, 1, 0,
6454 doc: /* Return the window tree for frame FRAME.
6456 The return value is a list of the form (ROOT MINI), where ROOT
6457 represents the window tree of the frame's root window, and MINI
6458 is the frame's minibuffer window.
6460 If the root window is not split, ROOT is the root window itself.
6461 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6462 horizontal split, and t for a vertical split, EDGES gives the combined
6463 size and position of the subwindows in the split, and the rest of the
6464 elements are the subwindows in the split. Each of the subwindows may
6465 again be a window or a list representing a window split, and so on.
6466 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6468 If FRAME is nil or omitted, return information on the currently
6469 selected frame. */)
6470 (Lisp_Object frame)
6472 FRAME_PTR f;
6474 if (NILP (frame))
6475 frame = selected_frame;
6477 CHECK_FRAME (frame);
6478 f = XFRAME (frame);
6480 if (!FRAME_LIVE_P (f))
6481 return Qnil;
6483 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6487 /***********************************************************************
6488 Marginal Areas
6489 ***********************************************************************/
6491 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6492 2, 3, 0,
6493 doc: /* Set width of marginal areas of window WINDOW.
6494 If WINDOW is nil, set margins of the currently selected window.
6495 Second arg LEFT-WIDTH specifies the number of character cells to
6496 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6497 does the same for the right marginal area. A nil width parameter
6498 means no margin. */)
6499 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6501 struct window *w = decode_window (window);
6503 /* Translate negative or zero widths to nil.
6504 Margins that are too wide have to be checked elsewhere. */
6506 if (!NILP (left_width))
6508 CHECK_NUMBER (left_width);
6509 if (XINT (left_width) <= 0)
6510 left_width = Qnil;
6513 if (!NILP (right_width))
6515 CHECK_NUMBER (right_width);
6516 if (XINT (right_width) <= 0)
6517 right_width = Qnil;
6520 if (!EQ (w->left_margin_cols, left_width)
6521 || !EQ (w->right_margin_cols, right_width))
6523 w->left_margin_cols = left_width;
6524 w->right_margin_cols = right_width;
6526 adjust_window_margins (w);
6528 ++windows_or_buffers_changed;
6529 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6532 return Qnil;
6536 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6537 0, 1, 0,
6538 doc: /* Get width of marginal areas of window WINDOW.
6539 If WINDOW is omitted or nil, use the currently selected window.
6540 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6541 If a marginal area does not exist, its width will be returned
6542 as nil. */)
6543 (Lisp_Object window)
6545 struct window *w = decode_window (window);
6546 return Fcons (w->left_margin_cols, w->right_margin_cols);
6551 /***********************************************************************
6552 Fringes
6553 ***********************************************************************/
6555 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6556 2, 4, 0,
6557 doc: /* Set the fringe widths of window WINDOW.
6558 If WINDOW is nil, set the fringe widths of the currently selected
6559 window.
6560 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6561 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6562 fringe width. If a fringe width arg is nil, that means to use the
6563 frame's default fringe width. Default fringe widths can be set with
6564 the command `set-fringe-style'.
6565 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6566 outside of the display margins. By default, fringes are drawn between
6567 display marginal areas and the text area. */)
6568 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6570 struct window *w = decode_window (window);
6572 if (!NILP (left_width))
6573 CHECK_NATNUM (left_width);
6574 if (!NILP (right_width))
6575 CHECK_NATNUM (right_width);
6577 /* Do nothing on a tty. */
6578 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6579 && (!EQ (w->left_fringe_width, left_width)
6580 || !EQ (w->right_fringe_width, right_width)
6581 || !EQ (w->fringes_outside_margins, outside_margins)))
6583 w->left_fringe_width = left_width;
6584 w->right_fringe_width = right_width;
6585 w->fringes_outside_margins = outside_margins;
6587 adjust_window_margins (w);
6589 clear_glyph_matrix (w->current_matrix);
6590 w->window_end_valid = Qnil;
6592 ++windows_or_buffers_changed;
6593 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6596 return Qnil;
6600 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6601 0, 1, 0,
6602 doc: /* Get width of fringes of window WINDOW.
6603 If WINDOW is omitted or nil, use the currently selected window.
6604 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6605 (Lisp_Object window)
6607 struct window *w = decode_window (window);
6609 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6610 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6611 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6612 ? Qt : Qnil), Qnil)));
6617 /***********************************************************************
6618 Scroll bars
6619 ***********************************************************************/
6621 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6622 Sset_window_scroll_bars, 2, 4, 0,
6623 doc: /* Set width and type of scroll bars of window WINDOW.
6624 If window is nil, set scroll bars of the currently selected window.
6625 Second parameter WIDTH specifies the pixel width for the scroll bar;
6626 this is automatically adjusted to a multiple of the frame column width.
6627 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6628 bar: left, right, or nil.
6629 If WIDTH is nil, use the frame's scroll-bar width.
6630 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6631 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6632 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6634 struct window *w = decode_window (window);
6636 if (!NILP (width))
6638 CHECK_NATNUM (width);
6640 if (XINT (width) == 0)
6641 vertical_type = Qnil;
6644 if (!(NILP (vertical_type)
6645 || EQ (vertical_type, Qleft)
6646 || EQ (vertical_type, Qright)
6647 || EQ (vertical_type, Qt)))
6648 error ("Invalid type of vertical scroll bar");
6650 if (!EQ (w->scroll_bar_width, width)
6651 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6653 w->scroll_bar_width = width;
6654 w->vertical_scroll_bar_type = vertical_type;
6656 adjust_window_margins (w);
6658 clear_glyph_matrix (w->current_matrix);
6659 w->window_end_valid = Qnil;
6661 ++windows_or_buffers_changed;
6662 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6665 return Qnil;
6669 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6670 0, 1, 0,
6671 doc: /* Get width and type of scroll bars of window WINDOW.
6672 If WINDOW is omitted or nil, use the currently selected window.
6673 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6674 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6675 value. */)
6676 (Lisp_Object window)
6678 struct window *w = decode_window (window);
6679 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6680 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6681 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6682 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6683 Fcons (w->vertical_scroll_bar_type,
6684 Fcons (Qnil, Qnil))));
6689 /***********************************************************************
6690 Smooth scrolling
6691 ***********************************************************************/
6693 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6694 doc: /* Return the amount by which WINDOW is scrolled vertically.
6695 Use the selected window if WINDOW is nil or omitted.
6696 Normally, value is a multiple of the canonical character height of WINDOW;
6697 optional second arg PIXELS-P means value is measured in pixels. */)
6698 (Lisp_Object window, Lisp_Object pixels_p)
6700 Lisp_Object result;
6701 struct frame *f;
6702 struct window *w;
6704 if (NILP (window))
6705 window = selected_window;
6706 else
6707 CHECK_WINDOW (window);
6708 w = XWINDOW (window);
6709 f = XFRAME (w->frame);
6711 if (FRAME_WINDOW_P (f))
6712 result = (NILP (pixels_p)
6713 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6714 : make_number (-w->vscroll));
6715 else
6716 result = make_number (0);
6717 return result;
6721 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6722 2, 3, 0,
6723 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6724 WINDOW nil means use the selected window. Normally, VSCROLL is a
6725 non-negative multiple of the canonical character height of WINDOW;
6726 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6727 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6728 corresponds to an integral number of pixels. The return value is the
6729 result of this rounding.
6730 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6731 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6733 struct window *w;
6734 struct frame *f;
6736 if (NILP (window))
6737 window = selected_window;
6738 else
6739 CHECK_WINDOW (window);
6740 CHECK_NUMBER_OR_FLOAT (vscroll);
6742 w = XWINDOW (window);
6743 f = XFRAME (w->frame);
6745 if (FRAME_WINDOW_P (f))
6747 int old_dy = w->vscroll;
6749 w->vscroll = - (NILP (pixels_p)
6750 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6751 : XFLOATINT (vscroll));
6752 w->vscroll = min (w->vscroll, 0);
6754 if (w->vscroll != old_dy)
6756 /* Adjust glyph matrix of the frame if the virtual display
6757 area becomes larger than before. */
6758 if (w->vscroll < 0 && w->vscroll < old_dy)
6759 adjust_glyphs (f);
6761 /* Prevent redisplay shortcuts. */
6762 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6766 return Fwindow_vscroll (window, pixels_p);
6770 /* Call FN for all leaf windows on frame F. FN is called with the
6771 first argument being a pointer to the leaf window, and with
6772 additional argument USER_DATA. Stops when FN returns 0. */
6774 static void
6775 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6776 void *user_data)
6778 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6779 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6780 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6784 /* Helper function for foreach_window. Call FN for all leaf windows
6785 reachable from W. FN is called with the first argument being a
6786 pointer to the leaf window, and with additional argument USER_DATA.
6787 Stop when FN returns 0. Value is 0 if stopped by FN. */
6789 static int
6790 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6792 int cont;
6794 for (cont = 1; w && cont;)
6796 if (!NILP (w->hchild))
6797 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6798 else if (!NILP (w->vchild))
6799 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6800 else
6801 cont = fn (w, user_data);
6803 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6806 return cont;
6810 /* Freeze or unfreeze the window start of W unless it is a
6811 mini-window or the selected window. FREEZE_P non-null means freeze
6812 the window start. */
6814 static int
6815 freeze_window_start (struct window *w, void *freeze_p)
6817 if (MINI_WINDOW_P (w)
6818 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6819 && (w == XWINDOW (selected_window)
6820 || (MINI_WINDOW_P (XWINDOW (selected_window))
6821 && ! NILP (Vminibuf_scroll_window)
6822 && w == XWINDOW (Vminibuf_scroll_window)))))
6823 freeze_p = NULL;
6825 w->frozen_window_start_p = freeze_p != NULL;
6826 return 1;
6830 /* Freeze or unfreeze the window starts of all leaf windows on frame
6831 F, except the selected window and a mini-window. FREEZE_P non-zero
6832 means freeze the window start. */
6834 void
6835 freeze_window_starts (struct frame *f, int freeze_p)
6837 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6841 /***********************************************************************
6842 Initialization
6843 ***********************************************************************/
6845 /* Return 1 if window configurations C1 and C2
6846 describe the same state of affairs. This is used by Fequal. */
6849 compare_window_configurations (Lisp_Object c1, Lisp_Object c2, int ignore_positions)
6851 register struct save_window_data *d1, *d2;
6852 struct Lisp_Vector *sw1, *sw2;
6853 int i;
6855 CHECK_WINDOW_CONFIGURATION (c1);
6856 CHECK_WINDOW_CONFIGURATION (c2);
6858 d1 = (struct save_window_data *) XVECTOR (c1);
6859 d2 = (struct save_window_data *) XVECTOR (c2);
6860 sw1 = XVECTOR (d1->saved_windows);
6861 sw2 = XVECTOR (d2->saved_windows);
6863 if (d1->frame_cols != d2->frame_cols)
6864 return 0;
6865 if (d1->frame_lines != d2->frame_lines)
6866 return 0;
6867 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
6868 return 0;
6869 if (! EQ (d1->selected_frame, d2->selected_frame))
6870 return 0;
6871 /* Don't compare the current_window field directly.
6872 Instead see w1_is_current and w2_is_current, below. */
6873 if (! EQ (d1->current_buffer, d2->current_buffer))
6874 return 0;
6875 if (! ignore_positions)
6877 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6878 return 0;
6879 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6880 return 0;
6882 /* Don't compare the root_window field.
6883 We don't require the two configurations
6884 to use the same window object,
6885 and the two root windows must be equivalent
6886 if everything else compares equal. */
6887 if (! EQ (d1->focus_frame, d2->focus_frame))
6888 return 0;
6890 /* Verify that the two confis have the same number of windows. */
6891 if (sw1->header.size != sw2->header.size)
6892 return 0;
6894 for (i = 0; i < sw1->header.size; i++)
6896 struct saved_window *p1, *p2;
6897 int w1_is_current, w2_is_current;
6899 p1 = SAVED_WINDOW_N (sw1, i);
6900 p2 = SAVED_WINDOW_N (sw2, i);
6902 /* Verify that the current windows in the two
6903 configurations correspond to each other. */
6904 w1_is_current = EQ (d1->current_window, p1->window);
6905 w2_is_current = EQ (d2->current_window, p2->window);
6907 if (w1_is_current != w2_is_current)
6908 return 0;
6910 /* Verify that the corresponding windows do match. */
6911 if (! EQ (p1->buffer, p2->buffer))
6912 return 0;
6913 if (! EQ (p1->left_col, p2->left_col))
6914 return 0;
6915 if (! EQ (p1->top_line, p2->top_line))
6916 return 0;
6917 if (! EQ (p1->total_cols, p2->total_cols))
6918 return 0;
6919 if (! EQ (p1->total_lines, p2->total_lines))
6920 return 0;
6921 if (! EQ (p1->display_table, p2->display_table))
6922 return 0;
6923 if (! EQ (p1->parent, p2->parent))
6924 return 0;
6925 if (! EQ (p1->prev, p2->prev))
6926 return 0;
6927 if (! ignore_positions)
6929 if (! EQ (p1->hscroll, p2->hscroll))
6930 return 0;
6931 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6932 return 0;
6933 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6934 return 0;
6935 if (NILP (Fequal (p1->start, p2->start)))
6936 return 0;
6937 if (NILP (Fequal (p1->pointm, p2->pointm)))
6938 return 0;
6939 if (NILP (Fequal (p1->mark, p2->mark)))
6940 return 0;
6942 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6943 return 0;
6944 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6945 return 0;
6946 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6947 return 0;
6948 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6949 return 0;
6950 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6951 return 0;
6952 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6953 return 0;
6954 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6955 return 0;
6958 return 1;
6961 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6962 Scompare_window_configurations, 2, 2, 0,
6963 doc: /* Compare two window configurations as regards the structure of windows.
6964 This function ignores details such as the values of point and mark
6965 and scrolling positions. */)
6966 (Lisp_Object x, Lisp_Object y)
6968 if (compare_window_configurations (x, y, 1))
6969 return Qt;
6970 return Qnil;
6973 void
6974 init_window_once (void)
6976 struct frame *f = make_initial_frame ();
6977 XSETFRAME (selected_frame, f);
6978 Vterminal_frame = selected_frame;
6979 minibuf_window = f->minibuffer_window;
6980 selected_window = f->selected_window;
6981 last_nonminibuf_frame = f;
6983 window_initialized = 1;
6986 void
6987 init_window (void)
6989 Vwindow_list = Qnil;
6992 void
6993 syms_of_window (void)
6995 Qscroll_up = intern_c_string ("scroll-up");
6996 staticpro (&Qscroll_up);
6998 Qscroll_down = intern_c_string ("scroll-down");
6999 staticpro (&Qscroll_down);
7001 Qscroll_command = intern_c_string ("scroll-command");
7002 staticpro (&Qscroll_command);
7004 Fput (Qscroll_up, Qscroll_command, Qt);
7005 Fput (Qscroll_down, Qscroll_command, Qt);
7007 Qwindow_size_fixed = intern_c_string ("window-size-fixed");
7008 staticpro (&Qwindow_size_fixed);
7009 Fset (Qwindow_size_fixed, Qnil);
7011 staticpro (&Qwindow_configuration_change_hook);
7012 Qwindow_configuration_change_hook
7013 = intern_c_string ("window-configuration-change-hook");
7015 Qwindowp = intern_c_string ("windowp");
7016 staticpro (&Qwindowp);
7018 Qwindow_configuration_p = intern_c_string ("window-configuration-p");
7019 staticpro (&Qwindow_configuration_p);
7021 Qwindow_live_p = intern_c_string ("window-live-p");
7022 staticpro (&Qwindow_live_p);
7024 Qdisplay_buffer = intern_c_string ("display-buffer");
7025 staticpro (&Qdisplay_buffer);
7027 Qtemp_buffer_show_hook = intern_c_string ("temp-buffer-show-hook");
7028 staticpro (&Qtemp_buffer_show_hook);
7030 staticpro (&Vwindow_list);
7032 minibuf_selected_window = Qnil;
7033 staticpro (&minibuf_selected_window);
7035 window_scroll_pixel_based_preserve_x = -1;
7036 window_scroll_pixel_based_preserve_y = -1;
7037 window_scroll_preserve_hpos = -1;
7038 window_scroll_preserve_vpos = -1;
7040 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7041 doc: /* Non-nil means call as function to display a help buffer.
7042 The function is called with one argument, the buffer to be displayed.
7043 Used by `with-output-to-temp-buffer'.
7044 If this function is used, then it must do the entire job of showing
7045 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7046 Vtemp_buffer_show_function = Qnil;
7048 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7049 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7050 Vminibuf_scroll_window = Qnil;
7052 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7053 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7054 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7055 is displayed in the `mode-line' face. */);
7056 mode_line_in_non_selected_windows = 1;
7058 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7059 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7060 Vother_window_scroll_buffer = Qnil;
7062 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7063 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7064 auto_window_vscroll_p = 1;
7066 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7067 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7068 next_screen_context_lines = 2;
7070 DEFVAR_INT ("window-min-height", window_min_height,
7071 doc: /* Allow deleting windows less than this tall.
7072 The value is measured in line units. If a window wants a modeline it
7073 is counted as one line.
7075 Emacs honors settings of this variable when enlarging or shrinking
7076 windows vertically. A value less than 1 is invalid. */);
7077 window_min_height = 4;
7079 DEFVAR_INT ("window-min-width", window_min_width,
7080 doc: /* Allow deleting windows less than this wide.
7081 The value is measured in characters and includes any fringes or
7082 the scrollbar.
7084 Emacs honors settings of this variable when enlarging or shrinking
7085 windows horizontally. A value less than 2 is invalid. */);
7086 window_min_width = 10;
7088 DEFVAR_LISP ("scroll-preserve-screen-position",
7089 Vscroll_preserve_screen_position,
7090 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
7091 A value of nil means point does not keep its screen position except
7092 at the scroll margin or window boundary respectively.
7093 A value of t means point keeps its screen position if the scroll
7094 command moved it vertically out of the window, e.g. when scrolling
7095 by full screens.
7096 Any other value means point always keeps its screen position.
7097 Scroll commands should have the `scroll-command' property
7098 on their symbols to be controlled by this variable. */);
7099 Vscroll_preserve_screen_position = Qnil;
7101 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7102 doc: /* Type of marker to use for `window-point'. */);
7103 Vwindow_point_insertion_type = Qnil;
7105 DEFVAR_LISP ("window-configuration-change-hook",
7106 Vwindow_configuration_change_hook,
7107 doc: /* Functions to call when window configuration changes.
7108 The buffer-local part is run once per window, with the relevant window
7109 selected; while the global part is run only once for the modified frame,
7110 with the relevant frame selected. */);
7111 Vwindow_configuration_change_hook = Qnil;
7113 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7114 doc: /* If non-nil, then the `recenter' command with a nil argument
7115 will redraw the entire frame; the special value `tty' causes the
7116 frame to be redrawn only if it is a tty frame. */);
7117 Vrecenter_redisplay = Qtty;
7120 defsubr (&Sselected_window);
7121 defsubr (&Sminibuffer_window);
7122 defsubr (&Swindow_minibuffer_p);
7123 defsubr (&Swindowp);
7124 defsubr (&Swindow_live_p);
7125 defsubr (&Spos_visible_in_window_p);
7126 defsubr (&Swindow_line_height);
7127 defsubr (&Swindow_buffer);
7128 defsubr (&Swindow_height);
7129 defsubr (&Swindow_width);
7130 defsubr (&Swindow_full_width_p);
7131 defsubr (&Swindow_hscroll);
7132 defsubr (&Sset_window_hscroll);
7133 defsubr (&Swindow_redisplay_end_trigger);
7134 defsubr (&Sset_window_redisplay_end_trigger);
7135 defsubr (&Swindow_edges);
7136 defsubr (&Swindow_pixel_edges);
7137 defsubr (&Swindow_absolute_pixel_edges);
7138 defsubr (&Swindow_inside_edges);
7139 defsubr (&Swindow_inside_pixel_edges);
7140 defsubr (&Swindow_inside_absolute_pixel_edges);
7141 defsubr (&Scoordinates_in_window_p);
7142 defsubr (&Swindow_at);
7143 defsubr (&Swindow_point);
7144 defsubr (&Swindow_start);
7145 defsubr (&Swindow_end);
7146 defsubr (&Sset_window_point);
7147 defsubr (&Sset_window_start);
7148 defsubr (&Swindow_dedicated_p);
7149 defsubr (&Sset_window_dedicated_p);
7150 defsubr (&Swindow_display_table);
7151 defsubr (&Sset_window_display_table);
7152 defsubr (&Snext_window);
7153 defsubr (&Sprevious_window);
7154 defsubr (&Sother_window);
7155 defsubr (&Sget_lru_window);
7156 defsubr (&Swindow_use_time);
7157 defsubr (&Sget_largest_window);
7158 defsubr (&Sget_buffer_window);
7159 defsubr (&Sdelete_other_windows);
7160 defsubr (&Sdelete_windows_on);
7161 defsubr (&Sreplace_buffer_in_windows);
7162 defsubr (&Sdelete_window);
7163 defsubr (&Sset_window_buffer);
7164 defsubr (&Sselect_window);
7165 defsubr (&Sforce_window_update);
7166 defsubr (&Stemp_output_buffer_show);
7167 defsubr (&Ssplit_window);
7168 defsubr (&Senlarge_window);
7169 defsubr (&Sshrink_window);
7170 defsubr (&Sadjust_window_trailing_edge);
7171 defsubr (&Sscroll_up);
7172 defsubr (&Sscroll_down);
7173 defsubr (&Sscroll_left);
7174 defsubr (&Sscroll_right);
7175 defsubr (&Sother_window_for_scrolling);
7176 defsubr (&Sscroll_other_window);
7177 defsubr (&Sminibuffer_selected_window);
7178 defsubr (&Srecenter);
7179 defsubr (&Swindow_text_height);
7180 defsubr (&Smove_to_window_line);
7181 defsubr (&Swindow_configuration_p);
7182 defsubr (&Swindow_configuration_frame);
7183 defsubr (&Sset_window_configuration);
7184 defsubr (&Scurrent_window_configuration);
7185 defsubr (&Swindow_tree);
7186 defsubr (&Sset_window_margins);
7187 defsubr (&Swindow_margins);
7188 defsubr (&Sset_window_fringes);
7189 defsubr (&Swindow_fringes);
7190 defsubr (&Sset_window_scroll_bars);
7191 defsubr (&Swindow_scroll_bars);
7192 defsubr (&Swindow_vscroll);
7193 defsubr (&Sset_window_vscroll);
7194 defsubr (&Scompare_window_configurations);
7195 defsubr (&Swindow_list);
7196 defsubr (&Swindow_parameters);
7197 defsubr (&Swindow_parameter);
7198 defsubr (&Sset_window_parameter);
7202 void
7203 keys_of_window (void)
7205 initial_define_key (control_x_map, '1', "delete-other-windows");
7206 initial_define_key (control_x_map, '2', "split-window");
7207 initial_define_key (control_x_map, '0', "delete-window");
7208 initial_define_key (control_x_map, 'o', "other-window");
7209 initial_define_key (control_x_map, '^', "enlarge-window");
7210 initial_define_key (control_x_map, '<', "scroll-left");
7211 initial_define_key (control_x_map, '>', "scroll-right");
7213 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7214 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7215 initial_define_key (meta_map, 'v', "scroll-down-command");