* doc/lispref/variables.texi (Scope): Mention the availability of lexbind.
[bpt/emacs.git] / src / window.c
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.
5
6 This file is part of GNU Emacs.
7
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.
12
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.
17
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/>. */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <setjmp.h>
24
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. */
39
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
52
53 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
54 Lisp_Object Qdisplay_buffer;
55 Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
56 Lisp_Object Qwindow_size_fixed;
57
58 static int displayed_window_lines (struct window *);
59 static struct window *decode_window (Lisp_Object);
60 static int count_windows (struct window *);
61 static int get_leaf_windows (struct window *, struct window **, int);
62 static void window_scroll (Lisp_Object, int, int, int);
63 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
64 static void window_scroll_line_based (Lisp_Object, int, int, int);
65 static int window_min_size_1 (struct window *, int, int);
66 static int window_min_size_2 (struct window *, int, int);
67 static int window_min_size (struct window *, int, int, int, int *);
68 static void size_window (Lisp_Object, int, int, int, int, int);
69 static int freeze_window_start (struct window *, void *);
70 static int window_fixed_size_p (struct window *, int, int);
71 static void enlarge_window (Lisp_Object, int, int);
72 static Lisp_Object window_list (void);
73 static int add_window_to_list (struct window *, void *);
74 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
75 Lisp_Object);
76 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
77 Lisp_Object, int);
78 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
79 Lisp_Object *);
80 static void foreach_window (struct frame *,
81 int (* fn) (struct window *, void *),
82 void *);
83 static int foreach_window_1 (struct window *,
84 int (* fn) (struct window *, void *),
85 void *);
86 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
87 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
88
89 /* This is the window in which the terminal's cursor should
90 be left when nothing is being done with it. This must
91 always be a leaf window, and its buffer is selected by
92 the top level editing loop at the end of each command.
93
94 This value is always the same as
95 FRAME_SELECTED_WINDOW (selected_frame). */
96
97 Lisp_Object selected_window;
98
99 /* A list of all windows for use by next_window and Fwindow_list.
100 Functions creating or deleting windows should invalidate this cache
101 by setting it to nil. */
102
103 Lisp_Object Vwindow_list;
104
105 /* The mini-buffer window of the selected frame.
106 Note that you cannot test for mini-bufferness of an arbitrary window
107 by comparing against this; but you can test for mini-bufferness of
108 the selected window. */
109
110 Lisp_Object minibuf_window;
111
112 /* Non-nil means it is the window whose mode line should be
113 shown as the selected window when the minibuffer is selected. */
114
115 Lisp_Object minibuf_selected_window;
116
117 /* Hook run at end of temp_output_buffer_show. */
118
119 Lisp_Object Qtemp_buffer_show_hook;
120
121 /* Incremented for each window created. */
122
123 static int sequence_number;
124
125 /* Nonzero after init_window_once has finished. */
126
127 static int window_initialized;
128
129 /* Hook to run when window config changes. */
130
131 static Lisp_Object Qwindow_configuration_change_hook;
132 /* Incremented by 1 whenever a window is deleted. */
133
134 int window_deletion_count;
135
136 /* Used by the function window_scroll_pixel_based */
137
138 static int window_scroll_pixel_based_preserve_x;
139 static int window_scroll_pixel_based_preserve_y;
140
141 /* Same for window_scroll_line_based. */
142
143 static int window_scroll_preserve_hpos;
144 static int window_scroll_preserve_vpos;
145
146 #if 0 /* This isn't used anywhere. */
147 /* Nonzero means we can split a frame even if it is "unsplittable". */
148 static int inhibit_frame_unsplittable;
149 #endif
150
151 \f
152 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
153 doc: /* Return t if OBJECT is a window. */)
154 (Lisp_Object object)
155 {
156 return WINDOWP (object) ? Qt : Qnil;
157 }
158
159 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
160 doc: /* Return t if OBJECT is a window which is currently visible. */)
161 (Lisp_Object object)
162 {
163 return WINDOW_LIVE_P (object) ? Qt : Qnil;
164 }
165
166 Lisp_Object
167 make_window (void)
168 {
169 Lisp_Object val;
170 register struct window *p;
171
172 p = allocate_window ();
173 ++sequence_number;
174 XSETFASTINT (p->sequence_number, sequence_number);
175 XSETFASTINT (p->left_col, 0);
176 XSETFASTINT (p->top_line, 0);
177 XSETFASTINT (p->total_lines, 0);
178 XSETFASTINT (p->total_cols, 0);
179 XSETFASTINT (p->hscroll, 0);
180 XSETFASTINT (p->min_hscroll, 0);
181 p->orig_top_line = p->orig_total_lines = Qnil;
182 p->start = Fmake_marker ();
183 p->pointm = Fmake_marker ();
184 XSETFASTINT (p->use_time, 0);
185 p->frame = Qnil;
186 p->display_table = Qnil;
187 p->dedicated = Qnil;
188 p->window_parameters = Qnil;
189 p->pseudo_window_p = 0;
190 memset (&p->cursor, 0, sizeof (p->cursor));
191 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
192 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
193 p->desired_matrix = p->current_matrix = 0;
194 p->nrows_scale_factor = p->ncols_scale_factor = 1;
195 p->phys_cursor_type = -1;
196 p->phys_cursor_width = -1;
197 p->must_be_updated_p = 0;
198 XSETFASTINT (p->window_end_vpos, 0);
199 XSETFASTINT (p->window_end_pos, 0);
200 p->window_end_valid = Qnil;
201 p->vscroll = 0;
202 XSETWINDOW (val, p);
203 XSETFASTINT (p->last_point, 0);
204 p->frozen_window_start_p = 0;
205 p->last_cursor_off_p = p->cursor_off_p = 0;
206 p->left_margin_cols = Qnil;
207 p->right_margin_cols = Qnil;
208 p->left_fringe_width = Qnil;
209 p->right_fringe_width = Qnil;
210 p->fringes_outside_margins = Qnil;
211 p->scroll_bar_width = Qnil;
212 p->vertical_scroll_bar_type = Qt;
213 p->resize_proportionally = Qnil;
214
215 Vwindow_list = Qnil;
216 return val;
217 }
218
219 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
220 doc: /* Return the window that the cursor now appears in and commands apply to. */)
221 (void)
222 {
223 return selected_window;
224 }
225
226 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
227 doc: /* Return the window used now for minibuffers.
228 If the optional argument FRAME is specified, return the minibuffer window
229 used by that frame. */)
230 (Lisp_Object frame)
231 {
232 if (NILP (frame))
233 frame = selected_frame;
234 CHECK_LIVE_FRAME (frame);
235 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
236 }
237
238 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
239 doc: /* Return non-nil if WINDOW is a minibuffer window.
240 WINDOW defaults to the selected window. */)
241 (Lisp_Object window)
242 {
243 struct window *w = decode_window (window);
244 return MINI_WINDOW_P (w) ? Qt : Qnil;
245 }
246
247
248 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
249 Spos_visible_in_window_p, 0, 3, 0,
250 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
251 Return nil if that position is scrolled vertically out of view.
252 If a character is only partially visible, nil is returned, unless the
253 optional argument PARTIALLY is non-nil.
254 If POS is only out of view because of horizontal scrolling, return non-nil.
255 If POS is t, it specifies the position of the last visible glyph in WINDOW.
256 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
257
258 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
259 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
260 where X and Y are the pixel coordinates relative to the top left corner
261 of the window. The remaining elements are omitted if the character after
262 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
263 off-window at the top and bottom of the row, ROWH is the height of the
264 display row, and VPOS is the row number (0-based) containing POS. */)
265 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
266 {
267 register struct window *w;
268 register EMACS_INT posint;
269 register struct buffer *buf;
270 struct text_pos top;
271 Lisp_Object in_window = Qnil;
272 int rtop, rbot, rowh, vpos, fully_p = 1;
273 int x, y;
274
275 w = decode_window (window);
276 buf = XBUFFER (w->buffer);
277 SET_TEXT_POS_FROM_MARKER (top, w->start);
278
279 if (EQ (pos, Qt))
280 posint = -1;
281 else if (!NILP (pos))
282 {
283 CHECK_NUMBER_COERCE_MARKER (pos);
284 posint = XINT (pos);
285 }
286 else if (w == XWINDOW (selected_window))
287 posint = PT;
288 else
289 posint = XMARKER (w->pointm)->charpos;
290
291 /* If position is above window start or outside buffer boundaries,
292 or if window start is out of range, position is not visible. */
293 if ((EQ (pos, Qt)
294 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
295 && CHARPOS (top) >= BUF_BEGV (buf)
296 && CHARPOS (top) <= BUF_ZV (buf)
297 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
298 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
299 in_window = Qt;
300
301 if (!NILP (in_window) && !NILP (partially))
302 {
303 Lisp_Object part = Qnil;
304 if (!fully_p)
305 part = list4 (make_number (rtop), make_number (rbot),
306 make_number (rowh), make_number (vpos));
307 in_window = Fcons (make_number (x),
308 Fcons (make_number (y), part));
309 }
310
311 return in_window;
312 }
313
314 DEFUN ("window-line-height", Fwindow_line_height,
315 Swindow_line_height, 0, 2, 0,
316 doc: /* Return height in pixels of text line LINE in window WINDOW.
317 If WINDOW is nil or omitted, use selected window.
318
319 Return height of current line if LINE is omitted or nil. Return height of
320 header or mode line if LINE is `header-line' and `mode-line'.
321 Otherwise, LINE is a text line number starting from 0. A negative number
322 counts from the end of the window.
323
324 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
325 in pixels of the visible part of the line, VPOS and YPOS are the
326 vertical position in lines and pixels of the line, relative to the top
327 of the first text line, and OFFBOT is the number of off-window pixels at
328 the bottom of the text line. If there are off-window pixels at the top
329 of the (first) text line, YPOS is negative.
330
331 Return nil if window display is not up-to-date. In that case, use
332 `pos-visible-in-window-p' to obtain the information. */)
333 (Lisp_Object line, Lisp_Object window)
334 {
335 register struct window *w;
336 register struct buffer *b;
337 struct glyph_row *row, *end_row;
338 int max_y, crop, i, n;
339
340 w = decode_window (window);
341
342 if (noninteractive
343 || w->pseudo_window_p)
344 return Qnil;
345
346 CHECK_BUFFER (w->buffer);
347 b = XBUFFER (w->buffer);
348
349 /* Fail if current matrix is not up-to-date. */
350 if (NILP (w->window_end_valid)
351 || current_buffer->clip_changed
352 || current_buffer->prevent_redisplay_optimizations_p
353 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
354 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
355 return Qnil;
356
357 if (NILP (line))
358 {
359 i = w->cursor.vpos;
360 if (i < 0 || i >= w->current_matrix->nrows
361 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
362 return Qnil;
363 max_y = window_text_bottom_y (w);
364 goto found_row;
365 }
366
367 if (EQ (line, Qheader_line))
368 {
369 if (!WINDOW_WANTS_HEADER_LINE_P (w))
370 return Qnil;
371 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
372 if (!row->enabled_p)
373 return Qnil;
374 return list4 (make_number (row->height),
375 make_number (0), make_number (0),
376 make_number (0));
377 }
378
379 if (EQ (line, Qmode_line))
380 {
381 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
382 if (!row->enabled_p)
383 return Qnil;
384 return list4 (make_number (row->height),
385 make_number (0), /* not accurate */
386 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
387 + window_text_bottom_y (w)),
388 make_number (0));
389 }
390
391 CHECK_NUMBER (line);
392 n = XINT (line);
393
394 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
395 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
396 max_y = window_text_bottom_y (w);
397 i = 0;
398
399 while ((n < 0 || i < n)
400 && row <= end_row && row->enabled_p
401 && row->y + row->height < max_y)
402 row++, i++;
403
404 if (row > end_row || !row->enabled_p)
405 return Qnil;
406
407 if (++n < 0)
408 {
409 if (-n > i)
410 return Qnil;
411 row += n;
412 i += n;
413 }
414
415 found_row:
416 crop = max (0, (row->y + row->height) - max_y);
417 return list4 (make_number (row->height + min (0, row->y) - crop),
418 make_number (i),
419 make_number (row->y),
420 make_number (crop));
421 }
422
423
424 \f
425 static struct window *
426 decode_window (register Lisp_Object window)
427 {
428 if (NILP (window))
429 return XWINDOW (selected_window);
430
431 CHECK_LIVE_WINDOW (window);
432 return XWINDOW (window);
433 }
434
435 static struct window *
436 decode_any_window (register Lisp_Object window)
437 {
438 if (NILP (window))
439 return XWINDOW (selected_window);
440
441 CHECK_WINDOW (window);
442 return XWINDOW (window);
443 }
444
445 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
446 doc: /* Return the buffer that WINDOW is displaying.
447 WINDOW defaults to the selected window. */)
448 (Lisp_Object window)
449 {
450 return decode_window (window)->buffer;
451 }
452
453 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
454 doc: /* Return the number of lines in WINDOW.
455 WINDOW defaults to the selected window.
456
457 The return value includes WINDOW's mode line and header line, if any.
458
459 Note: The function does not take into account the value of `line-spacing'
460 when calculating the number of lines in WINDOW. */)
461 (Lisp_Object window)
462 {
463 return decode_any_window (window)->total_lines;
464 }
465
466 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
467 doc: /* Return the number of display columns in WINDOW.
468 WINDOW defaults to the selected window.
469
470 Note: The return value is the number of columns available for text in
471 WINDOW. If you want to find out how many columns WINDOW takes up, use
472 (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))). */)
473 (Lisp_Object window)
474 {
475 return make_number (window_box_text_cols (decode_any_window (window)));
476 }
477
478 DEFUN ("window-full-width-p", Fwindow_full_width_p, Swindow_full_width_p, 0, 1, 0,
479 doc: /* Return t if WINDOW is as wide as its frame.
480 WINDOW defaults to the selected window. */)
481 (Lisp_Object window)
482 {
483 return WINDOW_FULL_WIDTH_P (decode_any_window (window)) ? Qt : Qnil;
484 }
485
486 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
487 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
488 WINDOW defaults to the selected window. */)
489 (Lisp_Object window)
490 {
491 return decode_window (window)->hscroll;
492 }
493
494 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
495 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
496 Return NCOL. NCOL should be zero or positive.
497
498 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
499 window so that the location of point moves off-window. */)
500 (Lisp_Object window, Lisp_Object ncol)
501 {
502 struct window *w = decode_window (window);
503 int hscroll;
504
505 CHECK_NUMBER (ncol);
506 hscroll = max (0, XINT (ncol));
507
508 /* Prevent redisplay shortcuts when changing the hscroll. */
509 if (XINT (w->hscroll) != hscroll)
510 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
511
512 w->hscroll = make_number (hscroll);
513 return ncol;
514 }
515
516 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
517 Swindow_redisplay_end_trigger, 0, 1, 0,
518 doc: /* Return WINDOW's redisplay end trigger value.
519 WINDOW defaults to the selected window.
520 See `set-window-redisplay-end-trigger' for more information. */)
521 (Lisp_Object window)
522 {
523 return decode_window (window)->redisplay_end_trigger;
524 }
525
526 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
527 Sset_window_redisplay_end_trigger, 2, 2, 0,
528 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
529 VALUE should be a buffer position (typically a marker) or nil.
530 If it is a buffer position, then if redisplay in WINDOW reaches a position
531 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
532 with two arguments: WINDOW, and the end trigger value.
533 Afterwards the end-trigger value is reset to nil. */)
534 (register Lisp_Object window, Lisp_Object value)
535 {
536 register struct window *w;
537
538 w = decode_window (window);
539 w->redisplay_end_trigger = value;
540 return value;
541 }
542
543 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
544 doc: /* Return a list of the edge coordinates of WINDOW.
545 The list has the form (LEFT TOP RIGHT BOTTOM).
546 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
547 all relative to 0, 0 at top left corner of frame.
548
549 RIGHT is one more than the rightmost column occupied by WINDOW.
550 BOTTOM is one more than the bottommost row occupied by WINDOW.
551 The edges include the space used by WINDOW's scroll bar, display
552 margins, fringes, header line, and/or mode line. For the edges of
553 just the text area, use `window-inside-edges'. */)
554 (Lisp_Object window)
555 {
556 register struct window *w = decode_any_window (window);
557
558 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
559 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
560 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
561 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
562 Qnil))));
563 }
564
565 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
566 doc: /* Return a list of the edge pixel coordinates of WINDOW.
567 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
568 the top left corner of the frame.
569
570 RIGHT is one more than the rightmost x position occupied by WINDOW.
571 BOTTOM is one more than the bottommost y position occupied by WINDOW.
572 The pixel edges include the space used by WINDOW's scroll bar, display
573 margins, fringes, header line, and/or mode line. For the pixel edges
574 of just the text area, use `window-inside-pixel-edges'. */)
575 (Lisp_Object window)
576 {
577 register struct window *w = decode_any_window (window);
578
579 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
580 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
581 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
582 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
583 Qnil))));
584 }
585
586 static void
587 calc_absolute_offset(struct window *w, int *add_x, int *add_y)
588 {
589 struct frame *f = XFRAME (w->frame);
590 *add_y = f->top_pos;
591 #ifdef FRAME_MENUBAR_HEIGHT
592 *add_y += FRAME_MENUBAR_HEIGHT (f);
593 #endif
594 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
595 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
596 #elif FRAME_TOOLBAR_HEIGHT
597 *add_y += FRAME_TOOLBAR_HEIGHT (f);
598 #endif
599 #ifdef FRAME_NS_TITLEBAR_HEIGHT
600 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
601 #endif
602 *add_x = f->left_pos;
603 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
604 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
605 #endif
606 }
607
608 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
609 Swindow_absolute_pixel_edges, 0, 1, 0,
610 doc: /* Return a list of the edge pixel coordinates of WINDOW.
611 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
612 the top left corner of the display.
613
614 RIGHT is one more than the rightmost x position occupied by WINDOW.
615 BOTTOM is one more than the bottommost y position occupied by WINDOW.
616 The pixel edges include the space used by WINDOW's scroll bar, display
617 margins, fringes, header line, and/or mode line. For the pixel edges
618 of just the text area, use `window-inside-absolute-pixel-edges'. */)
619 (Lisp_Object window)
620 {
621 register struct window *w = decode_any_window (window);
622 int add_x, add_y;
623 calc_absolute_offset (w, &add_x, &add_y);
624
625 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
626 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
627 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
628 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
629 Qnil))));
630 }
631
632 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
633 doc: /* Return a list of the edge coordinates of WINDOW.
634 The list has the form (LEFT TOP RIGHT BOTTOM).
635 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
636 all relative to 0, 0 at top left corner of frame.
637
638 RIGHT is one more than the rightmost column of WINDOW's text area.
639 BOTTOM is one more than the bottommost row of WINDOW's text area.
640 The inside edges do not include the space used by the WINDOW's scroll
641 bar, display margins, fringes, header line, and/or mode line. */)
642 (Lisp_Object window)
643 {
644 register struct window *w = decode_any_window (window);
645
646 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
647 + WINDOW_LEFT_MARGIN_COLS (w)
648 + WINDOW_LEFT_FRINGE_COLS (w)),
649 make_number (WINDOW_TOP_EDGE_LINE (w)
650 + WINDOW_HEADER_LINE_LINES (w)),
651 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
652 - WINDOW_RIGHT_MARGIN_COLS (w)
653 - WINDOW_RIGHT_FRINGE_COLS (w)),
654 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
655 - WINDOW_MODE_LINE_LINES (w)));
656 }
657
658 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
659 doc: /* Return a list of the edge pixel coordinates of WINDOW.
660 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
661 the top left corner of the frame.
662
663 RIGHT is one more than the rightmost x position of WINDOW's text area.
664 BOTTOM is one more than the bottommost y position of WINDOW's text area.
665 The inside edges do not include the space used by WINDOW's scroll bar,
666 display margins, fringes, header line, and/or mode line. */)
667 (Lisp_Object window)
668 {
669 register struct window *w = decode_any_window (window);
670
671 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
672 + WINDOW_LEFT_MARGIN_WIDTH (w)
673 + WINDOW_LEFT_FRINGE_WIDTH (w)),
674 make_number (WINDOW_TOP_EDGE_Y (w)
675 + WINDOW_HEADER_LINE_HEIGHT (w)),
676 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
677 - WINDOW_RIGHT_MARGIN_WIDTH (w)
678 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
679 make_number (WINDOW_BOTTOM_EDGE_Y (w)
680 - WINDOW_MODE_LINE_HEIGHT (w)));
681 }
682
683 DEFUN ("window-inside-absolute-pixel-edges",
684 Fwindow_inside_absolute_pixel_edges,
685 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
686 doc: /* Return a list of the edge pixel coordinates of WINDOW.
687 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
688 the top left corner of the display.
689
690 RIGHT is one more than the rightmost x position of WINDOW's text area.
691 BOTTOM is one more than the bottommost y position of WINDOW's text area.
692 The inside edges do not include the space used by WINDOW's scroll bar,
693 display margins, fringes, header line, and/or mode line. */)
694 (Lisp_Object window)
695 {
696 register struct window *w = decode_any_window (window);
697 int add_x, add_y;
698 calc_absolute_offset (w, &add_x, &add_y);
699
700 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
701 + WINDOW_LEFT_MARGIN_WIDTH (w)
702 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
703 make_number (WINDOW_TOP_EDGE_Y (w)
704 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
705 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
706 - WINDOW_RIGHT_MARGIN_WIDTH (w)
707 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
708 make_number (WINDOW_BOTTOM_EDGE_Y (w)
709 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
710 }
711
712 /* Test if the character at column X, row Y is within window W.
713 If it is not, return ON_NOTHING;
714 if it is in the window's text area, return ON_TEXT;
715 if it is on the window's modeline, return ON_MODE_LINE;
716 if it is on the border between the window and its right sibling,
717 return ON_VERTICAL_BORDER.
718 if it is on a scroll bar, return ON_SCROLL_BAR.
719 if it is on the window's top line, return ON_HEADER_LINE;
720 if it is in left or right fringe of the window,
721 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
722 if it is in the marginal area to the left/right of the window,
723 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
724
725 X and Y are frame relative pixel coordinates. */
726
727 static enum window_part
728 coordinates_in_window (register struct window *w, int x, int y)
729 {
730 struct frame *f = XFRAME (WINDOW_FRAME (w));
731 int left_x, right_x;
732 enum window_part part;
733 int ux = FRAME_COLUMN_WIDTH (f);
734 int x0 = WINDOW_LEFT_EDGE_X (w);
735 int x1 = WINDOW_RIGHT_EDGE_X (w);
736 /* The width of the area where the vertical line can be dragged.
737 (Between mode lines for instance. */
738 int grabbable_width = ux;
739 int lmargin_width, rmargin_width, text_left, text_right;
740 int top_y = WINDOW_TOP_EDGE_Y (w);
741 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
742
743 /* Outside any interesting row? */
744 if (y < top_y || y >= bottom_y)
745 return ON_NOTHING;
746
747 /* In what's below, we subtract 1 when computing right_x because we
748 want the rightmost pixel, which is given by left_pixel+width-1. */
749 if (w->pseudo_window_p)
750 {
751 left_x = 0;
752 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
753 }
754 else
755 {
756 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
757 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
758 }
759
760 /* On the mode line or header line? If it's near the start of
761 the mode or header line of window that's has a horizontal
762 sibling, say it's on the vertical line. That's to be able
763 to resize windows horizontally in case we're using toolkit
764 scroll bars. */
765
766 if (WINDOW_WANTS_MODELINE_P (w)
767 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
768 {
769 part = ON_MODE_LINE;
770
771 header_vertical_border_check:
772 /* We're somewhere on the mode line. We consider the place
773 between mode lines of horizontally adjacent mode lines
774 as the vertical border. If scroll bars on the left,
775 return the right window. */
776 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
777 || WINDOW_RIGHTMOST_P (w))
778 && !WINDOW_LEFTMOST_P (w)
779 && eabs (x - x0) < grabbable_width)
780 return ON_VERTICAL_BORDER;
781
782 /* Make sure we're not at the rightmost position of a
783 mode-/header-line and there's yet another window on the
784 right. (Bug#1372) */
785 else if ((WINDOW_RIGHTMOST_P (w) || x < x1)
786 && eabs (x - x1) < grabbable_width)
787 return ON_VERTICAL_BORDER;
788
789 if (x < x0 || x >= x1)
790 return ON_NOTHING;
791
792 return part;
793 }
794
795 if (WINDOW_WANTS_HEADER_LINE_P (w)
796 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w))
797 {
798 part = ON_HEADER_LINE;
799 goto header_vertical_border_check;
800 }
801
802 if (x < x0 || x >= x1) return ON_NOTHING;
803
804 /* Outside any interesting column? */
805 if (x < left_x || x > right_x)
806 return ON_SCROLL_BAR;
807
808 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
809 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
810
811 text_left = window_box_left (w, TEXT_AREA);
812 text_right = text_left + window_box_width (w, TEXT_AREA);
813
814 if (FRAME_WINDOW_P (f))
815 {
816 if (!w->pseudo_window_p
817 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
818 && !WINDOW_RIGHTMOST_P (w)
819 && (eabs (x - right_x) < grabbable_width))
820 return ON_VERTICAL_BORDER;
821 }
822 /* Need to say "x > right_x" rather than >=, since on character
823 terminals, the vertical line's x coordinate is right_x. */
824 else if (!w->pseudo_window_p
825 && !WINDOW_RIGHTMOST_P (w)
826 && x > right_x - ux)
827 return ON_VERTICAL_BORDER;
828
829 if (x < text_left)
830 {
831 if (lmargin_width > 0
832 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
833 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
834 : (x < left_x + lmargin_width)))
835 return ON_LEFT_MARGIN;
836
837 return ON_LEFT_FRINGE;
838 }
839
840 if (x >= text_right)
841 {
842 if (rmargin_width > 0
843 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
844 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
845 : (x >= right_x - rmargin_width)))
846 return ON_RIGHT_MARGIN;
847
848 return ON_RIGHT_FRINGE;
849 }
850
851 /* Everything special ruled out - must be on text area */
852 return ON_TEXT;
853 }
854
855 /* Take X is the frame-relative pixel x-coordinate, and return the
856 x-coordinate relative to part PART of window W. */
857 int
858 window_relative_x_coord (struct window *w, enum window_part part, int x)
859 {
860 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
861
862 switch (part)
863 {
864 case ON_TEXT:
865 return x - window_box_left (w, TEXT_AREA);
866
867 case ON_LEFT_FRINGE:
868 return x - left_x;
869
870 case ON_RIGHT_FRINGE:
871 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
872
873 case ON_LEFT_MARGIN:
874 return (x - left_x
875 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
876 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
877
878 case ON_RIGHT_MARGIN:
879 return (x + 1
880 - ((w->pseudo_window_p)
881 ? WINDOW_TOTAL_WIDTH (w)
882 : WINDOW_BOX_RIGHT_EDGE_X (w))
883 + window_box_width (w, RIGHT_MARGIN_AREA)
884 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
885 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
886 }
887
888 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
889 return 0;
890 }
891
892
893 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
894 Scoordinates_in_window_p, 2, 2, 0,
895 doc: /* Return non-nil if COORDINATES are in WINDOW.
896 COORDINATES is a cons of the form (X . Y), X and Y being distances
897 measured in characters from the upper-left corner of the frame.
898 \(0 . 0) denotes the character in the upper left corner of the
899 frame.
900 If COORDINATES are in the text portion of WINDOW,
901 the coordinates relative to the window are returned.
902 If they are in the mode line of WINDOW, `mode-line' is returned.
903 If they are in the top mode line of WINDOW, `header-line' is returned.
904 If they are in the left fringe of WINDOW, `left-fringe' is returned.
905 If they are in the right fringe of WINDOW, `right-fringe' is returned.
906 If they are on the border between WINDOW and its right sibling,
907 `vertical-line' is returned.
908 If they are in the windows's left or right marginal areas, `left-margin'\n\
909 or `right-margin' is returned. */)
910 (register Lisp_Object coordinates, Lisp_Object window)
911 {
912 struct window *w;
913 struct frame *f;
914 int x, y;
915 Lisp_Object lx, ly;
916
917 CHECK_WINDOW (window);
918 w = XWINDOW (window);
919 f = XFRAME (w->frame);
920 CHECK_CONS (coordinates);
921 lx = Fcar (coordinates);
922 ly = Fcdr (coordinates);
923 CHECK_NUMBER_OR_FLOAT (lx);
924 CHECK_NUMBER_OR_FLOAT (ly);
925 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
926 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
927
928 switch (coordinates_in_window (w, x, y))
929 {
930 case ON_NOTHING:
931 return Qnil;
932
933 case ON_TEXT:
934 /* Convert X and Y to window relative pixel coordinates, and
935 return the canonical char units. */
936 x -= window_box_left (w, TEXT_AREA);
937 y -= WINDOW_TOP_EDGE_Y (w);
938 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
939 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
940
941 case ON_MODE_LINE:
942 return Qmode_line;
943
944 case ON_VERTICAL_BORDER:
945 return Qvertical_line;
946
947 case ON_HEADER_LINE:
948 return Qheader_line;
949
950 case ON_LEFT_FRINGE:
951 return Qleft_fringe;
952
953 case ON_RIGHT_FRINGE:
954 return Qright_fringe;
955
956 case ON_LEFT_MARGIN:
957 return Qleft_margin;
958
959 case ON_RIGHT_MARGIN:
960 return Qright_margin;
961
962 case ON_SCROLL_BAR:
963 /* Historically we are supposed to return nil in this case. */
964 return Qnil;
965
966 default:
967 abort ();
968 }
969 }
970
971
972 /* Callback for foreach_window, used in window_from_coordinates.
973 Check if window W contains coordinates specified by USER_DATA which
974 is actually a pointer to a struct check_window_data CW.
975
976 Check if window W contains coordinates *CW->x and *CW->y. If it
977 does, return W in *CW->window, as Lisp_Object, and return in
978 *CW->part the part of the window under coordinates *X,*Y. Return
979 zero from this function to stop iterating over windows. */
980
981 struct check_window_data
982 {
983 Lisp_Object *window;
984 int x, y;
985 enum window_part *part;
986 };
987
988 static int
989 check_window_containing (struct window *w, void *user_data)
990 {
991 struct check_window_data *cw = (struct check_window_data *) user_data;
992 enum window_part found;
993 int continue_p = 1;
994
995 found = coordinates_in_window (w, cw->x, cw->y);
996 if (found != ON_NOTHING)
997 {
998 *cw->part = found;
999 XSETWINDOW (*cw->window, w);
1000 continue_p = 0;
1001 }
1002
1003 return continue_p;
1004 }
1005
1006
1007 /* Find the window containing frame-relative pixel position X/Y and
1008 return it as a Lisp_Object.
1009
1010 If X, Y is on one of the window's special `window_part' elements,
1011 set *PART to the id of that element.
1012
1013 If there is no window under X, Y return nil and leave *PART
1014 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1015
1016 This function was previously implemented with a loop cycling over
1017 windows with Fnext_window, and starting with the frame's selected
1018 window. It turned out that this doesn't work with an
1019 implementation of next_window using Vwindow_list, because
1020 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1021 tree of F when this function is called asynchronously from
1022 note_mouse_highlight. The original loop didn't terminate in this
1023 case. */
1024
1025 Lisp_Object
1026 window_from_coordinates (struct frame *f, int x, int y,
1027 enum window_part *part, int tool_bar_p)
1028 {
1029 Lisp_Object window;
1030 struct check_window_data cw;
1031 enum window_part dummy;
1032
1033 if (part == 0)
1034 part = &dummy;
1035
1036 window = Qnil;
1037 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1038 foreach_window (f, check_window_containing, &cw);
1039
1040 /* If not found above, see if it's in the tool bar window, if a tool
1041 bar exists. */
1042 if (NILP (window)
1043 && tool_bar_p
1044 && WINDOWP (f->tool_bar_window)
1045 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1046 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1047 != ON_NOTHING))
1048 {
1049 *part = ON_TEXT;
1050 window = f->tool_bar_window;
1051 }
1052
1053 return window;
1054 }
1055
1056 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1057 doc: /* Return window containing coordinates X and Y on FRAME.
1058 If omitted, FRAME defaults to the currently selected frame.
1059 The top left corner of the frame is considered to be row 0,
1060 column 0. */)
1061 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1062 {
1063 struct frame *f;
1064
1065 if (NILP (frame))
1066 frame = selected_frame;
1067 CHECK_LIVE_FRAME (frame);
1068 f = XFRAME (frame);
1069
1070 /* Check that arguments are integers or floats. */
1071 CHECK_NUMBER_OR_FLOAT (x);
1072 CHECK_NUMBER_OR_FLOAT (y);
1073
1074 return window_from_coordinates (f,
1075 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1076 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1077 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1078 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1079 0, 0);
1080 }
1081
1082 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1083 doc: /* Return current value of point in WINDOW.
1084 WINDOW defaults to the selected window.
1085
1086 For a nonselected window, this is the value point would have
1087 if that window were selected.
1088
1089 Note that, when WINDOW is the selected window and its buffer
1090 is also currently selected, the value returned is the same as (point).
1091 It would be more strictly correct to return the `top-level' value
1092 of point, outside of any save-excursion forms.
1093 But that is hard to define. */)
1094 (Lisp_Object window)
1095 {
1096 register struct window *w = decode_window (window);
1097
1098 if (w == XWINDOW (selected_window)
1099 && current_buffer == XBUFFER (w->buffer))
1100 return Fpoint ();
1101 return Fmarker_position (w->pointm);
1102 }
1103
1104 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1105 doc: /* Return position at which display currently starts in WINDOW.
1106 WINDOW defaults to the selected window.
1107 This is updated by redisplay or by calling `set-window-start'. */)
1108 (Lisp_Object window)
1109 {
1110 return Fmarker_position (decode_window (window)->start);
1111 }
1112
1113 /* This is text temporarily removed from the doc string below.
1114
1115 This function returns nil if the position is not currently known.
1116 That happens when redisplay is preempted and doesn't finish.
1117 If in that case you want to compute where the end of the window would
1118 have been if redisplay had finished, do this:
1119 (save-excursion
1120 (goto-char (window-start window))
1121 (vertical-motion (1- (window-height window)) window)
1122 (point))") */
1123
1124 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1125 doc: /* Return position at which display currently ends in WINDOW.
1126 WINDOW defaults to the selected window.
1127 This is updated by redisplay, when it runs to completion.
1128 Simply changing the buffer text or setting `window-start'
1129 does not update this value.
1130 Return nil if there is no recorded value. \(This can happen if the
1131 last redisplay of WINDOW was preempted, and did not finish.)
1132 If UPDATE is non-nil, compute the up-to-date position
1133 if it isn't already recorded. */)
1134 (Lisp_Object window, Lisp_Object update)
1135 {
1136 Lisp_Object value;
1137 struct window *w = decode_window (window);
1138 Lisp_Object buf;
1139 struct buffer *b;
1140
1141 buf = w->buffer;
1142 CHECK_BUFFER (buf);
1143 b = XBUFFER (buf);
1144
1145 #if 0 /* This change broke some things. We should make it later. */
1146 /* If we don't know the end position, return nil.
1147 The user can compute it with vertical-motion if he wants to.
1148 It would be nicer to do it automatically,
1149 but that's so slow that it would probably bother people. */
1150 if (NILP (w->window_end_valid))
1151 return Qnil;
1152 #endif
1153
1154 if (! NILP (update)
1155 && ! (! NILP (w->window_end_valid)
1156 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1157 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1158 && !noninteractive)
1159 {
1160 struct text_pos startp;
1161 struct it it;
1162 struct buffer *old_buffer = NULL;
1163
1164 /* Cannot use Fvertical_motion because that function doesn't
1165 cope with variable-height lines. */
1166 if (b != current_buffer)
1167 {
1168 old_buffer = current_buffer;
1169 set_buffer_internal (b);
1170 }
1171
1172 /* In case W->start is out of the range, use something
1173 reasonable. This situation occurred when loading a file with
1174 `-l' containing a call to `rmail' with subsequent other
1175 commands. At the end, W->start happened to be BEG, while
1176 rmail had already narrowed the buffer. */
1177 if (XMARKER (w->start)->charpos < BEGV)
1178 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1179 else if (XMARKER (w->start)->charpos > ZV)
1180 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1181 else
1182 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1183
1184 start_display (&it, w, startp);
1185 move_it_vertically (&it, window_box_height (w));
1186 if (it.current_y < it.last_visible_y)
1187 move_it_past_eol (&it);
1188 value = make_number (IT_CHARPOS (it));
1189
1190 if (old_buffer)
1191 set_buffer_internal (old_buffer);
1192 }
1193 else
1194 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1195
1196 return value;
1197 }
1198
1199 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1200 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1201 Return POS. */)
1202 (Lisp_Object window, Lisp_Object pos)
1203 {
1204 register struct window *w = decode_window (window);
1205
1206 CHECK_NUMBER_COERCE_MARKER (pos);
1207 if (w == XWINDOW (selected_window)
1208 && XBUFFER (w->buffer) == current_buffer)
1209 Fgoto_char (pos);
1210 else
1211 set_marker_restricted (w->pointm, pos, w->buffer);
1212
1213 /* We have to make sure that redisplay updates the window to show
1214 the new value of point. */
1215 if (!EQ (window, selected_window))
1216 ++windows_or_buffers_changed;
1217
1218 return pos;
1219 }
1220
1221 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1222 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1223 WINDOW defaults to the selected window. Return POS.
1224 Optional third arg NOFORCE non-nil inhibits next redisplay from
1225 overriding motion of point in order to display at this exact start. */)
1226 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1227 {
1228 register struct window *w = decode_window (window);
1229
1230 CHECK_NUMBER_COERCE_MARKER (pos);
1231 set_marker_restricted (w->start, pos, w->buffer);
1232 /* this is not right, but much easier than doing what is right. */
1233 w->start_at_line_beg = Qnil;
1234 if (NILP (noforce))
1235 w->force_start = Qt;
1236 w->update_mode_line = Qt;
1237 XSETFASTINT (w->last_modified, 0);
1238 XSETFASTINT (w->last_overlay_modified, 0);
1239 if (!EQ (window, selected_window))
1240 windows_or_buffers_changed++;
1241
1242 return pos;
1243 }
1244
1245
1246 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1247 0, 1, 0,
1248 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1249 More precisely, return the value assigned by the last call of
1250 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1251 never called with WINDOW as its argument, or the value set by that
1252 function was internally reset since its last call. WINDOW defaults to
1253 the selected window.
1254
1255 When a window is dedicated to its buffer, `display-buffer' will refrain
1256 from displaying another buffer in it. `get-lru-window' and
1257 `get-largest-window' treat dedicated windows specially.
1258 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1259 `kill-buffer' can delete a dedicated window and the containing frame.
1260
1261 Functions like `set-window-buffer' may change the buffer displayed by a
1262 window, unless that window is "strongly" dedicated to its buffer, that
1263 is the value returned by `window-dedicated-p' is t. */)
1264 (Lisp_Object window)
1265 {
1266 return decode_window (window)->dedicated;
1267 }
1268
1269 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1270 Sset_window_dedicated_p, 2, 2, 0,
1271 doc: /* Mark WINDOW as dedicated according to FLAG.
1272 WINDOW defaults to the selected window. FLAG non-nil means mark WINDOW
1273 as dedicated to its buffer. FLAG nil means mark WINDOW as non-dedicated.
1274 Return FLAG.
1275
1276 When a window is dedicated to its buffer, `display-buffer' will refrain
1277 from displaying another buffer in it. `get-lru-window' and
1278 `get-largest-window' treat dedicated windows specially.
1279 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1280 `kill-buffer' can delete a dedicated window and the containing
1281 frame.
1282
1283 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1284 its buffer. Functions like `set-window-buffer' may change the buffer
1285 displayed by a window, unless that window is strongly dedicated to its
1286 buffer. If and when `set-window-buffer' displays another buffer in a
1287 window, it also makes sure that the window is not marked as dedicated. */)
1288 (Lisp_Object window, Lisp_Object flag)
1289 {
1290 register struct window *w = decode_window (window);
1291
1292 w->dedicated = flag;
1293 return w->dedicated;
1294 }
1295
1296
1297 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1298 0, 1, 0,
1299 doc: /* Return the parameters of WINDOW and their values.
1300 WINDOW defaults to the selected window. The return value is a list of
1301 elements of the form (PARAMETER . VALUE). */)
1302 (Lisp_Object window)
1303 {
1304 return Fcopy_alist (decode_window (window)->window_parameters);
1305 }
1306
1307 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1308 2, 2, 0,
1309 doc: /* Return WINDOW's value for PARAMETER.
1310 WINDOW defaults to the selected window. */)
1311 (Lisp_Object window, Lisp_Object parameter)
1312 {
1313 Lisp_Object result;
1314
1315 result = Fassq (parameter, decode_window (window)->window_parameters);
1316 return CDR_SAFE (result);
1317 }
1318
1319 DEFUN ("set-window-parameter", Fset_window_parameter,
1320 Sset_window_parameter, 3, 3, 0,
1321 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1322 WINDOW defaults to the selected window. Return VALUE. */)
1323 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1324 {
1325 register struct window *w = decode_window (window);
1326 Lisp_Object old_alist_elt;
1327
1328 old_alist_elt = Fassq (parameter, w->window_parameters);
1329 if (NILP (old_alist_elt))
1330 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1331 else
1332 Fsetcdr (old_alist_elt, value);
1333 return value;
1334 }
1335
1336
1337 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1338 0, 1, 0,
1339 doc: /* Return the display-table that WINDOW is using.
1340 WINDOW defaults to the selected window. */)
1341 (Lisp_Object window)
1342 {
1343 return decode_window (window)->display_table;
1344 }
1345
1346 /* Get the display table for use on window W. This is either W's
1347 display table or W's buffer's display table. Ignore the specified
1348 tables if they are not valid; if no valid table is specified,
1349 return 0. */
1350
1351 struct Lisp_Char_Table *
1352 window_display_table (struct window *w)
1353 {
1354 struct Lisp_Char_Table *dp = NULL;
1355
1356 if (DISP_TABLE_P (w->display_table))
1357 dp = XCHAR_TABLE (w->display_table);
1358 else if (BUFFERP (w->buffer))
1359 {
1360 struct buffer *b = XBUFFER (w->buffer);
1361
1362 if (DISP_TABLE_P (BVAR (b, display_table)))
1363 dp = XCHAR_TABLE (BVAR (b, display_table));
1364 else if (DISP_TABLE_P (Vstandard_display_table))
1365 dp = XCHAR_TABLE (Vstandard_display_table);
1366 }
1367
1368 return dp;
1369 }
1370
1371 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1372 doc: /* Set WINDOW's display-table to TABLE. */)
1373 (register Lisp_Object window, Lisp_Object table)
1374 {
1375 register struct window *w;
1376
1377 w = decode_window (window);
1378 w->display_table = table;
1379 return table;
1380 }
1381 \f
1382 static void delete_window (Lisp_Object);
1383
1384 /* Record info on buffer window w is displaying
1385 when it is about to cease to display that buffer. */
1386 static void
1387 unshow_buffer (register struct window *w)
1388 {
1389 Lisp_Object buf;
1390 struct buffer *b;
1391
1392 buf = w->buffer;
1393 b = XBUFFER (buf);
1394 if (b != XMARKER (w->pointm)->buffer)
1395 abort ();
1396
1397 #if 0
1398 if (w == XWINDOW (selected_window)
1399 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1400 /* Do this except when the selected window's buffer
1401 is being removed from some other window. */
1402 #endif
1403 /* last_window_start records the start position that this buffer
1404 had in the last window to be disconnected from it.
1405 Now that this statement is unconditional,
1406 it is possible for the buffer to be displayed in the
1407 selected window, while last_window_start reflects another
1408 window which was recently showing the same buffer.
1409 Some people might say that might be a good thing. Let's see. */
1410 b->last_window_start = marker_position (w->start);
1411
1412 /* Point in the selected window's buffer
1413 is actually stored in that buffer, and the window's pointm isn't used.
1414 So don't clobber point in that buffer. */
1415 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1416 /* This line helps to fix Horsley's testbug.el bug. */
1417 && !(WINDOWP (BVAR (b, last_selected_window))
1418 && w != XWINDOW (BVAR (b, last_selected_window))
1419 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1420 temp_set_point_both (b,
1421 clip_to_bounds (BUF_BEGV (b),
1422 XMARKER (w->pointm)->charpos,
1423 BUF_ZV (b)),
1424 clip_to_bounds (BUF_BEGV_BYTE (b),
1425 marker_byte_position (w->pointm),
1426 BUF_ZV_BYTE (b)));
1427
1428 if (WINDOWP (BVAR (b, last_selected_window))
1429 && w == XWINDOW (BVAR (b, last_selected_window)))
1430 BVAR (b, last_selected_window) = Qnil;
1431 }
1432
1433 /* Put replacement into the window structure in place of old. */
1434 static void
1435 replace_window (Lisp_Object old, Lisp_Object replacement)
1436 {
1437 register Lisp_Object tem;
1438 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
1439
1440 /* If OLD is its frame's root_window, then replacement is the new
1441 root_window for that frame. */
1442
1443 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1444 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
1445
1446 p->left_col = o->left_col;
1447 p->top_line = o->top_line;
1448 p->total_cols = o->total_cols;
1449 p->total_lines = o->total_lines;
1450 p->desired_matrix = p->current_matrix = 0;
1451 p->vscroll = 0;
1452 memset (&p->cursor, 0, sizeof (p->cursor));
1453 memset (&p->last_cursor, 0, sizeof (p->last_cursor));
1454 memset (&p->phys_cursor, 0, sizeof (p->phys_cursor));
1455 p->phys_cursor_type = -1;
1456 p->phys_cursor_width = -1;
1457 p->must_be_updated_p = 0;
1458 p->pseudo_window_p = 0;
1459 XSETFASTINT (p->window_end_vpos, 0);
1460 XSETFASTINT (p->window_end_pos, 0);
1461 p->window_end_valid = Qnil;
1462 p->frozen_window_start_p = 0;
1463 p->orig_top_line = p->orig_total_lines = Qnil;
1464
1465 p->next = tem = o->next;
1466 if (!NILP (tem))
1467 XWINDOW (tem)->prev = replacement;
1468
1469 p->prev = tem = o->prev;
1470 if (!NILP (tem))
1471 XWINDOW (tem)->next = replacement;
1472
1473 p->parent = tem = o->parent;
1474 if (!NILP (tem))
1475 {
1476 if (EQ (XWINDOW (tem)->vchild, old))
1477 XWINDOW (tem)->vchild = replacement;
1478 if (EQ (XWINDOW (tem)->hchild, old))
1479 XWINDOW (tem)->hchild = replacement;
1480 }
1481
1482 /*** Here, if replacement is a vertical combination
1483 and so is its new parent, we should make replacement's
1484 children be children of that parent instead. ***/
1485 }
1486
1487 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
1488 doc: /* Remove WINDOW from its frame.
1489 WINDOW defaults to the selected window. Return nil.
1490 Signal an error when WINDOW is the only window on its frame. */)
1491 (register Lisp_Object window)
1492 {
1493 struct frame *f;
1494 if (NILP (window))
1495 window = selected_window;
1496 else
1497 CHECK_LIVE_WINDOW (window);
1498
1499 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1500 delete_window (window);
1501
1502 run_window_configuration_change_hook (f);
1503
1504 return Qnil;
1505 }
1506
1507 static void
1508 delete_window (register Lisp_Object window)
1509 {
1510 register Lisp_Object tem, parent, sib;
1511 register struct window *p;
1512 register struct window *par;
1513 struct frame *f;
1514
1515 /* Because this function is called by other C code on non-leaf
1516 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
1517 so we can't decode_window here. */
1518 CHECK_WINDOW (window);
1519 p = XWINDOW (window);
1520
1521 /* It's a no-op to delete an already-deleted window. */
1522 if (NILP (p->buffer)
1523 && NILP (p->hchild)
1524 && NILP (p->vchild))
1525 return;
1526
1527 parent = p->parent;
1528 if (NILP (parent))
1529 error ("Attempt to delete minibuffer or sole ordinary window");
1530 par = XWINDOW (parent);
1531
1532 windows_or_buffers_changed++;
1533 Vwindow_list = Qnil;
1534 f = XFRAME (WINDOW_FRAME (p));
1535 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
1536
1537 /* Are we trying to delete any frame's selected window? */
1538 {
1539 Lisp_Object swindow, pwindow;
1540
1541 /* See if the frame's selected window is either WINDOW
1542 or any subwindow of it, by finding all that window's parents
1543 and comparing each one with WINDOW. */
1544 swindow = FRAME_SELECTED_WINDOW (f);
1545
1546 while (1)
1547 {
1548 pwindow = swindow;
1549 while (!NILP (pwindow))
1550 {
1551 if (EQ (window, pwindow))
1552 break;
1553 pwindow = XWINDOW (pwindow)->parent;
1554 }
1555
1556 /* If the window being deleted is not a parent of SWINDOW,
1557 then SWINDOW is ok as the new selected window. */
1558 if (!EQ (window, pwindow))
1559 break;
1560 /* Otherwise, try another window for SWINDOW. */
1561 swindow = Fnext_window (swindow, Qlambda, Qnil);
1562
1563 /* If we get back to the frame's selected window,
1564 it means there was no acceptable alternative,
1565 so we cannot delete. */
1566 if (EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1567 error ("Cannot delete window");
1568 }
1569
1570 /* If we need to change SWINDOW, do it. */
1571 if (! EQ (swindow, FRAME_SELECTED_WINDOW (f)))
1572 {
1573 /* If we're about to delete the selected window on the
1574 selected frame, then we should use Fselect_window to select
1575 the new window. On the other hand, if we're about to
1576 delete the selected window on any other frame, we shouldn't do
1577 anything but set the frame's selected_window slot. */
1578 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
1579 Fselect_window (swindow, Qnil);
1580 else
1581 FRAME_SELECTED_WINDOW (f) = swindow;
1582 }
1583 }
1584
1585 /* Now we know we can delete this one. */
1586 window_deletion_count++;
1587
1588 tem = p->buffer;
1589 /* tem is null for dummy parent windows
1590 (which have inferiors but not any contents themselves) */
1591 if (!NILP (tem))
1592 {
1593 unshow_buffer (p);
1594 unchain_marker (XMARKER (p->pointm));
1595 unchain_marker (XMARKER (p->start));
1596 }
1597
1598 /* Free window glyph matrices. It is sure that they are allocated
1599 again when ADJUST_GLYPHS is called. Block input so that expose
1600 events and other events that access glyph matrices are not
1601 processed while we are changing them. */
1602 BLOCK_INPUT;
1603 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (f)));
1604
1605 tem = p->next;
1606 if (!NILP (tem))
1607 XWINDOW (tem)->prev = p->prev;
1608
1609 tem = p->prev;
1610 if (!NILP (tem))
1611 XWINDOW (tem)->next = p->next;
1612
1613 if (EQ (window, par->hchild))
1614 par->hchild = p->next;
1615 if (EQ (window, par->vchild))
1616 par->vchild = p->next;
1617
1618 /* Find one of our siblings to give our space to. */
1619 sib = p->prev;
1620 if (NILP (sib))
1621 {
1622 /* If p gives its space to its next sibling, that sibling needs
1623 to have its top/left side pulled back to where p's is.
1624 set_window_{height,width} will re-position the sibling's
1625 children. */
1626 sib = p->next;
1627 XWINDOW (sib)->top_line = p->top_line;
1628 XWINDOW (sib)->left_col = p->left_col;
1629 }
1630
1631 /* Stretch that sibling. */
1632 if (!NILP (par->vchild))
1633 set_window_height (sib,
1634 XFASTINT (XWINDOW (sib)->total_lines) + XFASTINT (p->total_lines),
1635 1);
1636 if (!NILP (par->hchild))
1637 set_window_width (sib,
1638 XFASTINT (XWINDOW (sib)->total_cols) + XFASTINT (p->total_cols),
1639 1);
1640
1641 /* If parent now has only one child,
1642 put the child into the parent's place. */
1643 tem = par->hchild;
1644 if (NILP (tem))
1645 tem = par->vchild;
1646 if (NILP (XWINDOW (tem)->next)) {
1647 replace_window (parent, tem);
1648 par = XWINDOW (tem);
1649 }
1650
1651 /* Since we may be deleting combination windows, we must make sure that
1652 not only p but all its children have been marked as deleted. */
1653 if (! NILP (p->hchild))
1654 delete_all_subwindows (XWINDOW (p->hchild));
1655 else if (! NILP (p->vchild))
1656 delete_all_subwindows (XWINDOW (p->vchild));
1657
1658 /* Mark this window as deleted. */
1659 p->buffer = p->hchild = p->vchild = Qnil;
1660
1661 if (! NILP (par->parent))
1662 par = XWINDOW (par->parent);
1663
1664 /* Check if we have a v/hchild with a v/hchild. In that case remove
1665 one of them. */
1666
1667 if (! NILP (par->vchild) && ! NILP (XWINDOW (par->vchild)->vchild))
1668 {
1669 p = XWINDOW (par->vchild);
1670 par->vchild = p->vchild;
1671 tem = p->vchild;
1672 }
1673 else if (! NILP (par->hchild) && ! NILP (XWINDOW (par->hchild)->hchild))
1674 {
1675 p = XWINDOW (par->hchild);
1676 par->hchild = p->hchild;
1677 tem = p->hchild;
1678 }
1679 else
1680 p = 0;
1681
1682 if (p)
1683 {
1684 while (! NILP (tem)) {
1685 XWINDOW (tem)->parent = p->parent;
1686 if (NILP (XWINDOW (tem)->next))
1687 break;
1688 tem = XWINDOW (tem)->next;
1689 }
1690 if (! NILP (tem)) {
1691 /* The next of the v/hchild we are removing is now the next of the
1692 last child for the v/hchild:
1693 Before v/hchild -> v/hchild -> next1 -> next2
1694 |
1695 -> next3
1696 After: v/hchild -> next1 -> next2 -> next3
1697 */
1698 XWINDOW (tem)->next = p->next;
1699 if (! NILP (p->next))
1700 XWINDOW (p->next)->prev = tem;
1701 }
1702 p->next = p->prev = p->vchild = p->hchild = p->buffer = Qnil;
1703 }
1704
1705
1706 /* Adjust glyph matrices. */
1707 adjust_glyphs (f);
1708 UNBLOCK_INPUT;
1709 }
1710
1711
1712 \f
1713 /***********************************************************************
1714 Window List
1715 ***********************************************************************/
1716
1717 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1718 pointer. This is a callback function for foreach_window, used in
1719 function window_list. */
1720
1721 static int
1722 add_window_to_list (struct window *w, void *user_data)
1723 {
1724 Lisp_Object *list = (Lisp_Object *) user_data;
1725 Lisp_Object window;
1726 XSETWINDOW (window, w);
1727 *list = Fcons (window, *list);
1728 return 1;
1729 }
1730
1731
1732 /* Return a list of all windows, for use by next_window. If
1733 Vwindow_list is a list, return that list. Otherwise, build a new
1734 list, cache it in Vwindow_list, and return that. */
1735
1736 static Lisp_Object
1737 window_list (void)
1738 {
1739 if (!CONSP (Vwindow_list))
1740 {
1741 Lisp_Object tail;
1742
1743 Vwindow_list = Qnil;
1744 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1745 {
1746 Lisp_Object args[2];
1747
1748 /* We are visiting windows in canonical order, and add
1749 new windows at the front of args[1], which means we
1750 have to reverse this list at the end. */
1751 args[1] = Qnil;
1752 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1753 args[0] = Vwindow_list;
1754 args[1] = Fnreverse (args[1]);
1755 Vwindow_list = Fnconc (2, args);
1756 }
1757 }
1758
1759 return Vwindow_list;
1760 }
1761
1762
1763 /* Value is non-zero if WINDOW satisfies the constraints given by
1764 OWINDOW, MINIBUF and ALL_FRAMES.
1765
1766 MINIBUF t means WINDOW may be minibuffer windows.
1767 `lambda' means WINDOW may not be a minibuffer window.
1768 a window means a specific minibuffer window
1769
1770 ALL_FRAMES t means search all frames,
1771 nil means search just current frame,
1772 `visible' means search just visible frames,
1773 0 means search visible and iconified frames,
1774 a window means search the frame that window belongs to,
1775 a frame means consider windows on that frame, only. */
1776
1777 static int
1778 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
1779 {
1780 struct window *w = XWINDOW (window);
1781 struct frame *f = XFRAME (w->frame);
1782 int candidate_p = 1;
1783
1784 if (!BUFFERP (w->buffer))
1785 candidate_p = 0;
1786 else if (MINI_WINDOW_P (w)
1787 && (EQ (minibuf, Qlambda)
1788 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
1789 {
1790 /* If MINIBUF is `lambda' don't consider any mini-windows.
1791 If it is a window, consider only that one. */
1792 candidate_p = 0;
1793 }
1794 else if (EQ (all_frames, Qt))
1795 candidate_p = 1;
1796 else if (NILP (all_frames))
1797 {
1798 xassert (WINDOWP (owindow));
1799 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
1800 }
1801 else if (EQ (all_frames, Qvisible))
1802 {
1803 FRAME_SAMPLE_VISIBILITY (f);
1804 candidate_p = FRAME_VISIBLE_P (f)
1805 && (FRAME_TERMINAL (XFRAME (w->frame))
1806 == FRAME_TERMINAL (XFRAME (selected_frame)));
1807
1808 }
1809 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
1810 {
1811 FRAME_SAMPLE_VISIBILITY (f);
1812 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
1813 #ifdef HAVE_X_WINDOWS
1814 /* Yuck!! If we've just created the frame and the
1815 window-manager requested the user to place it
1816 manually, the window may still not be considered
1817 `visible'. I'd argue it should be at least
1818 something like `iconified', but don't know how to do
1819 that yet. --Stef */
1820 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
1821 && !f->output_data.x->has_been_visible)
1822 #endif
1823 )
1824 && (FRAME_TERMINAL (XFRAME (w->frame))
1825 == FRAME_TERMINAL (XFRAME (selected_frame)));
1826 }
1827 else if (WINDOWP (all_frames))
1828 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
1829 || EQ (XWINDOW (all_frames)->frame, w->frame)
1830 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
1831 else if (FRAMEP (all_frames))
1832 candidate_p = EQ (all_frames, w->frame);
1833
1834 return candidate_p;
1835 }
1836
1837
1838 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
1839 Fwindow_list. See there for the meaning of WINDOW, MINIBUF, and
1840 ALL_FRAMES. */
1841
1842 static void
1843 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
1844 {
1845 if (NILP (*window))
1846 *window = selected_window;
1847 else
1848 CHECK_LIVE_WINDOW (*window);
1849
1850 /* MINIBUF nil may or may not include minibuffers. Decide if it
1851 does. */
1852 if (NILP (*minibuf))
1853 *minibuf = minibuf_level ? minibuf_window : Qlambda;
1854 else if (!EQ (*minibuf, Qt))
1855 *minibuf = Qlambda;
1856
1857 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
1858 => count none of them, or a specific minibuffer window (the
1859 active one) to count. */
1860
1861 /* ALL_FRAMES nil doesn't specify which frames to include. */
1862 if (NILP (*all_frames))
1863 *all_frames = (!EQ (*minibuf, Qlambda)
1864 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
1865 : Qnil);
1866 else if (EQ (*all_frames, Qvisible))
1867 ;
1868 else if (EQ (*all_frames, make_number (0)))
1869 ;
1870 else if (FRAMEP (*all_frames))
1871 ;
1872 else if (!EQ (*all_frames, Qt))
1873 *all_frames = Qnil;
1874
1875 /* Now *ALL_FRAMES is t meaning search all frames, nil meaning
1876 search just current frame, `visible' meaning search just visible
1877 frames, 0 meaning search visible and iconified frames, or a
1878 window, meaning search the frame that window belongs to, or a
1879 frame, meaning consider windows on that frame, only. */
1880 }
1881
1882
1883 /* Return the next or previous window of WINDOW in cyclic ordering
1884 of windows. NEXT_P non-zero means return the next window. See the
1885 documentation string of next-window for the meaning of MINIBUF and
1886 ALL_FRAMES. */
1887
1888 static Lisp_Object
1889 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
1890 {
1891 decode_next_window_args (&window, &minibuf, &all_frames);
1892
1893 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
1894 return the first window on the frame. */
1895 if (FRAMEP (all_frames)
1896 && !EQ (all_frames, XWINDOW (window)->frame))
1897 return Fframe_first_window (all_frames);
1898
1899 if (next_p)
1900 {
1901 Lisp_Object list;
1902
1903 /* Find WINDOW in the list of all windows. */
1904 list = Fmemq (window, window_list ());
1905
1906 /* Scan forward from WINDOW to the end of the window list. */
1907 if (CONSP (list))
1908 for (list = XCDR (list); CONSP (list); list = XCDR (list))
1909 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1910 break;
1911
1912 /* Scan from the start of the window list up to WINDOW. */
1913 if (!CONSP (list))
1914 for (list = Vwindow_list;
1915 CONSP (list) && !EQ (XCAR (list), window);
1916 list = XCDR (list))
1917 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
1918 break;
1919
1920 if (CONSP (list))
1921 window = XCAR (list);
1922 }
1923 else
1924 {
1925 Lisp_Object candidate, list;
1926
1927 /* Scan through the list of windows for candidates. If there are
1928 candidate windows in front of WINDOW, the last one of these
1929 is the one we want. If there are candidates following WINDOW
1930 in the list, again the last one of these is the one we want. */
1931 candidate = Qnil;
1932 for (list = window_list (); CONSP (list); list = XCDR (list))
1933 {
1934 if (EQ (XCAR (list), window))
1935 {
1936 if (WINDOWP (candidate))
1937 break;
1938 }
1939 else if (candidate_window_p (XCAR (list), window, minibuf,
1940 all_frames))
1941 candidate = XCAR (list);
1942 }
1943
1944 if (WINDOWP (candidate))
1945 window = candidate;
1946 }
1947
1948 return window;
1949 }
1950
1951
1952 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1953 doc: /* Return window following WINDOW in cyclic ordering of windows.
1954 WINDOW defaults to the selected window. The optional arguments
1955 MINIBUF and ALL-FRAMES specify the set of windows to consider.
1956
1957 MINIBUF t means consider the minibuffer window even if the
1958 minibuffer is not active. MINIBUF nil or omitted means consider
1959 the minibuffer window only if the minibuffer is active. Any
1960 other value means do not consider the minibuffer window even if
1961 the minibuffer is active.
1962
1963 Several frames may share a single minibuffer; if the minibuffer
1964 is active, all windows on all frames that share that minibuffer
1965 are considered too. Therefore, if you are using a separate
1966 minibuffer frame and the minibuffer is active and MINIBUF says it
1967 counts, `next-window' considers the windows in the frame from
1968 which you entered the minibuffer, as well as the minibuffer
1969 window.
1970
1971 ALL-FRAMES nil or omitted means consider all windows on WINDOW's
1972 frame, plus the minibuffer window if specified by the MINIBUF
1973 argument, see above. If the minibuffer counts, consider all
1974 windows on all frames that share that minibuffer too.
1975 ALL-FRAMES t means consider all windows on all existing frames.
1976 ALL-FRAMES `visible' means consider all windows on all visible
1977 frames.
1978 ALL-FRAMES 0 means consider all windows on all visible and
1979 iconified frames.
1980 ALL-FRAMES a frame means consider all windows on that frame only.
1981 Anything else means consider all windows on WINDOW's frame and no
1982 others.
1983
1984 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
1985 `next-window' to iterate through the entire cycle of acceptable
1986 windows, eventually ending up back at the window you started with.
1987 `previous-window' traverses the same cycle, in the reverse order. */)
1988 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
1989 {
1990 return next_window (window, minibuf, all_frames, 1);
1991 }
1992
1993
1994 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1995 doc: /* Return window preceding WINDOW in cyclic ordering of windows.
1996 WINDOW defaults to the selected window. The optional arguments
1997 MINIBUF and ALL-FRAMES specify the set of windows to consider.
1998 For the precise meaning of these arguments see `next-window'.
1999
2000 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2001 use `previous-window' to iterate through the entire cycle of
2002 acceptable windows, eventually ending up back at the window you
2003 started with. `next-window' traverses the same cycle, in the
2004 reverse order. */)
2005 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2006 {
2007 return next_window (window, minibuf, all_frames, 0);
2008 }
2009
2010
2011 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
2012 doc: /* Select another window in cyclic ordering of windows.
2013 COUNT specifies the number of windows to skip, starting with the
2014 selected window, before making the selection. If COUNT is
2015 positive, skip COUNT windows forwards. If COUNT is negative,
2016 skip -COUNT windows backwards. COUNT zero means do not skip any
2017 window, so select the selected window. In an interactive call,
2018 COUNT is the numeric prefix argument. Return nil.
2019
2020 This function uses `next-window' for finding the window to select.
2021 The argument ALL-FRAMES has the same meaning as in `next-window',
2022 but the MINIBUF argument of `next-window' is always effectively
2023 nil. */)
2024 (Lisp_Object count, Lisp_Object all_frames)
2025 {
2026 Lisp_Object window;
2027 int i;
2028
2029 CHECK_NUMBER (count);
2030 window = selected_window;
2031
2032 for (i = XINT (count); i > 0; --i)
2033 window = Fnext_window (window, Qnil, all_frames);
2034 for (; i < 0; ++i)
2035 window = Fprevious_window (window, Qnil, all_frames);
2036
2037 Fselect_window (window, Qnil);
2038 return Qnil;
2039 }
2040
2041
2042 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2043 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2044 FRAME nil or omitted means use the selected frame.
2045 WINDOW nil or omitted means use the selected window.
2046 MINIBUF t means include the minibuffer window, even if it isn't active.
2047 MINIBUF nil or omitted means include the minibuffer window only
2048 if it's active.
2049 MINIBUF neither nil nor t means never include the minibuffer window. */)
2050 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2051 {
2052 if (NILP (window))
2053 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2054 CHECK_WINDOW (window);
2055 if (NILP (frame))
2056 frame = selected_frame;
2057
2058 if (!EQ (frame, XWINDOW (window)->frame))
2059 error ("Window is on a different frame");
2060
2061 return window_list_1 (window, minibuf, frame);
2062 }
2063
2064
2065 /* Return a list of windows in cyclic ordering. Arguments are like
2066 for `next-window'. */
2067
2068 static Lisp_Object
2069 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2070 {
2071 Lisp_Object tail, list, rest;
2072
2073 decode_next_window_args (&window, &minibuf, &all_frames);
2074 list = Qnil;
2075
2076 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2077 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2078 list = Fcons (XCAR (tail), list);
2079
2080 /* Rotate the list to start with WINDOW. */
2081 list = Fnreverse (list);
2082 rest = Fmemq (window, list);
2083 if (!NILP (rest) && !EQ (rest, list))
2084 {
2085 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2086 ;
2087 XSETCDR (tail, Qnil);
2088 list = nconc2 (rest, list);
2089 }
2090 return list;
2091 }
2092
2093
2094 \f
2095 /* Look at all windows, performing an operation specified by TYPE
2096 with argument OBJ.
2097 If FRAMES is Qt, look at all frames;
2098 Qnil, look at just the selected frame;
2099 Qvisible, look at visible frames;
2100 a frame, just look at windows on that frame.
2101 If MINI is non-zero, perform the operation on minibuffer windows too. */
2102
2103 enum window_loop
2104 {
2105 WINDOW_LOOP_UNUSED,
2106 GET_BUFFER_WINDOW, /* Arg is buffer */
2107 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
2108 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
2109 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
2110 GET_LARGEST_WINDOW,
2111 UNSHOW_BUFFER, /* Arg is buffer */
2112 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2113 CHECK_ALL_WINDOWS
2114 };
2115
2116 static Lisp_Object
2117 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2118 {
2119 Lisp_Object window, windows, best_window, frame_arg;
2120 struct frame *f;
2121 struct gcpro gcpro1;
2122
2123 /* If we're only looping through windows on a particular frame,
2124 frame points to that frame. If we're looping through windows
2125 on all frames, frame is 0. */
2126 if (FRAMEP (frames))
2127 f = XFRAME (frames);
2128 else if (NILP (frames))
2129 f = SELECTED_FRAME ();
2130 else
2131 f = NULL;
2132
2133 if (f)
2134 frame_arg = Qlambda;
2135 else if (EQ (frames, make_number (0)))
2136 frame_arg = frames;
2137 else if (EQ (frames, Qvisible))
2138 frame_arg = frames;
2139 else
2140 frame_arg = Qt;
2141
2142 /* frame_arg is Qlambda to stick to one frame,
2143 Qvisible to consider all visible frames,
2144 or Qt otherwise. */
2145
2146 /* Pick a window to start with. */
2147 if (WINDOWP (obj))
2148 window = obj;
2149 else if (f)
2150 window = FRAME_SELECTED_WINDOW (f);
2151 else
2152 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2153
2154 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2155 GCPRO1 (windows);
2156 best_window = Qnil;
2157
2158 for (; CONSP (windows); windows = XCDR (windows))
2159 {
2160 struct window *w;
2161
2162 window = XCAR (windows);
2163 w = XWINDOW (window);
2164
2165 /* Note that we do not pay attention here to whether the frame
2166 is visible, since Fwindow_list skips non-visible frames if
2167 that is desired, under the control of frame_arg. */
2168 if (!MINI_WINDOW_P (w)
2169 /* For UNSHOW_BUFFER, we must always consider all windows. */
2170 || type == UNSHOW_BUFFER
2171 || (mini && minibuf_level > 0))
2172 switch (type)
2173 {
2174 case GET_BUFFER_WINDOW:
2175 if (EQ (w->buffer, obj)
2176 /* Don't find any minibuffer window
2177 except the one that is currently in use. */
2178 && (MINI_WINDOW_P (w)
2179 ? EQ (window, minibuf_window)
2180 : 1))
2181 {
2182 if (NILP (best_window))
2183 best_window = window;
2184 else if (EQ (window, selected_window))
2185 /* Prefer to return selected-window. */
2186 RETURN_UNGCPRO (window);
2187 else if (EQ (Fwindow_frame (window), selected_frame))
2188 /* Prefer windows on the current frame. */
2189 best_window = window;
2190 }
2191 break;
2192
2193 case GET_LRU_WINDOW:
2194 /* `obj' is an integer encoding a bitvector.
2195 `obj & 1' means consider only full-width windows.
2196 `obj & 2' means consider also dedicated windows. */
2197 if (((XINT (obj) & 1) && !WINDOW_FULL_WIDTH_P (w))
2198 || (!(XINT (obj) & 2) && !NILP (w->dedicated))
2199 /* Minibuffer windows are always ignored. */
2200 || MINI_WINDOW_P (w))
2201 break;
2202 if (NILP (best_window)
2203 || (XFASTINT (XWINDOW (best_window)->use_time)
2204 > XFASTINT (w->use_time)))
2205 best_window = window;
2206 break;
2207
2208 case DELETE_OTHER_WINDOWS:
2209 if (!EQ (window, obj))
2210 Fdelete_window (window);
2211 break;
2212
2213 case DELETE_BUFFER_WINDOWS:
2214 if (EQ (w->buffer, obj))
2215 {
2216 struct frame *f = XFRAME (WINDOW_FRAME (w));
2217
2218 /* If this window is dedicated, and in a frame of its own,
2219 kill the frame. */
2220 if (EQ (window, FRAME_ROOT_WINDOW (f))
2221 && !NILP (w->dedicated)
2222 && other_visible_frames (f))
2223 {
2224 /* Skip the other windows on this frame.
2225 There might be one, the minibuffer! */
2226 while (CONSP (XCDR (windows))
2227 && EQ (XWINDOW (XCAR (windows))->frame,
2228 XWINDOW (XCAR (XCDR (windows)))->frame))
2229 windows = XCDR (windows);
2230
2231 /* Now we can safely delete the frame. */
2232 delete_frame (w->frame, Qnil);
2233 }
2234 else if (NILP (w->parent))
2235 {
2236 /* If we're deleting the buffer displayed in the
2237 only window on the frame, find a new buffer to
2238 display there. */
2239 Lisp_Object buffer;
2240 buffer = Fother_buffer (obj, Qnil, w->frame);
2241 /* Reset dedicated state of window. */
2242 w->dedicated = Qnil;
2243 Fset_window_buffer (window, buffer, Qnil);
2244 if (EQ (window, selected_window))
2245 Fset_buffer (w->buffer);
2246 }
2247 else
2248 Fdelete_window (window);
2249 }
2250 break;
2251
2252 case GET_LARGEST_WINDOW:
2253 { /* nil `obj' means to ignore dedicated windows. */
2254 /* Ignore dedicated windows and minibuffers. */
2255 if (MINI_WINDOW_P (w) || (NILP (obj) && !NILP (w->dedicated)))
2256 break;
2257
2258 if (NILP (best_window))
2259 best_window = window;
2260 else
2261 {
2262 struct window *b = XWINDOW (best_window);
2263 if (XFASTINT (w->total_lines) * XFASTINT (w->total_cols)
2264 > XFASTINT (b->total_lines) * XFASTINT (b->total_cols))
2265 best_window = window;
2266 }
2267 }
2268 break;
2269
2270 case UNSHOW_BUFFER:
2271 if (EQ (w->buffer, obj))
2272 {
2273 Lisp_Object buffer;
2274 struct frame *f = XFRAME (w->frame);
2275
2276 /* Find another buffer to show in this window. */
2277 buffer = Fother_buffer (obj, Qnil, w->frame);
2278
2279 /* If this window is dedicated, and in a frame of its own,
2280 kill the frame. */
2281 if (EQ (window, FRAME_ROOT_WINDOW (f))
2282 && !NILP (w->dedicated)
2283 && other_visible_frames (f))
2284 {
2285 /* Skip the other windows on this frame.
2286 There might be one, the minibuffer! */
2287 while (CONSP (XCDR (windows))
2288 && EQ (XWINDOW (XCAR (windows))->frame,
2289 XWINDOW (XCAR (XCDR (windows)))->frame))
2290 windows = XCDR (windows);
2291
2292 /* Now we can safely delete the frame. */
2293 delete_frame (w->frame, Qnil);
2294 }
2295 else if (!NILP (w->dedicated) && !NILP (w->parent))
2296 {
2297 Lisp_Object window;
2298 XSETWINDOW (window, w);
2299 /* If this window is dedicated and not the only window
2300 in its frame, then kill it. */
2301 Fdelete_window (window);
2302 }
2303 else
2304 {
2305 /* Otherwise show a different buffer in the window. */
2306 w->dedicated = Qnil;
2307 Fset_window_buffer (window, buffer, Qnil);
2308 if (EQ (window, selected_window))
2309 Fset_buffer (w->buffer);
2310 }
2311 }
2312 break;
2313
2314 case REDISPLAY_BUFFER_WINDOWS:
2315 if (EQ (w->buffer, obj))
2316 {
2317 mark_window_display_accurate (window, 0);
2318 w->update_mode_line = Qt;
2319 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2320 ++update_mode_lines;
2321 best_window = window;
2322 }
2323 break;
2324
2325 /* Check for a window that has a killed buffer. */
2326 case CHECK_ALL_WINDOWS:
2327 if (! NILP (w->buffer)
2328 && NILP (BVAR (XBUFFER (w->buffer), name)))
2329 abort ();
2330 break;
2331
2332 case WINDOW_LOOP_UNUSED:
2333 break;
2334 }
2335 }
2336
2337 UNGCPRO;
2338 return best_window;
2339 }
2340
2341 /* Used for debugging. Abort if any window has a dead buffer. */
2342
2343 void
2344 check_all_windows (void)
2345 {
2346 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2347 }
2348
2349 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
2350 doc: /* Return WINDOW's use time.
2351 WINDOW defaults to the selected window. The window with the highest use
2352 time is the most recently selected one. The window with the lowest use
2353 time is the least recently selected one. */)
2354 (Lisp_Object window)
2355 {
2356 return decode_window (window)->use_time;
2357 }
2358
2359 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 2, 0,
2360 doc: /* Return the window least recently selected or used for display.
2361 \(LRU means Least Recently Used.)
2362
2363 Return a full-width window if possible.
2364 A minibuffer window is never a candidate.
2365 A dedicated window is never a candidate, unless DEDICATED is non-nil,
2366 so if all windows are dedicated, the value is nil.
2367 If optional argument FRAME is `visible', search all visible frames.
2368 If FRAME is 0, search all visible and iconified frames.
2369 If FRAME is t, search all frames.
2370 If FRAME is nil, search only the selected frame.
2371 If FRAME is a frame, search only that frame. */)
2372 (Lisp_Object frame, Lisp_Object dedicated)
2373 {
2374 register Lisp_Object w;
2375 /* First try for a window that is full-width */
2376 w = window_loop (GET_LRU_WINDOW,
2377 NILP (dedicated) ? make_number (1) : make_number (3),
2378 0, frame);
2379 if (!NILP (w) && !EQ (w, selected_window))
2380 return w;
2381 /* If none of them, try the rest */
2382 return window_loop (GET_LRU_WINDOW,
2383 NILP (dedicated) ? make_number (0) : make_number (2),
2384 0, frame);
2385 }
2386
2387 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 2, 0,
2388 doc: /* Return the largest window in area.
2389 A minibuffer window is never a candidate.
2390 A dedicated window is never a candidate unless DEDICATED is non-nil,
2391 so if all windows are dedicated, the value is nil.
2392 If optional argument FRAME is `visible', search all visible frames.
2393 If FRAME is 0, search all visible and iconified frames.
2394 If FRAME is t, search all frames.
2395 If FRAME is nil, search only the selected frame.
2396 If FRAME is a frame, search only that frame. */)
2397 (Lisp_Object frame, Lisp_Object dedicated)
2398 {
2399 return window_loop (GET_LARGEST_WINDOW, dedicated, 0,
2400 frame);
2401 }
2402
2403 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2404 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2405 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to the
2406 current buffer.
2407 If optional argument FRAME is `visible', search all visible frames.
2408 If optional argument FRAME is 0, search all visible and iconified frames.
2409 If FRAME is t, search all frames.
2410 If FRAME is nil, search only the selected frame.
2411 If FRAME is a frame, search only that frame. */)
2412 (Lisp_Object buffer_or_name, Lisp_Object frame)
2413 {
2414 Lisp_Object buffer;
2415
2416 if (NILP (buffer_or_name))
2417 buffer = Fcurrent_buffer ();
2418 else
2419 buffer = Fget_buffer (buffer_or_name);
2420
2421 if (BUFFERP (buffer))
2422 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
2423 else
2424 return Qnil;
2425 }
2426
2427 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
2428 0, 1, "",
2429 doc: /* Make WINDOW (or the selected window) fill its frame.
2430 Only the frame WINDOW is on is affected.
2431 This function tries to reduce display jumps by keeping the text
2432 previously visible in WINDOW in the same place on the frame. Doing this
2433 depends on the value of (window-start WINDOW), so if calling this
2434 function in a program gives strange scrolling, make sure the
2435 window-start value is reasonable when this function is called. */)
2436 (Lisp_Object window)
2437 {
2438 struct window *w;
2439 EMACS_INT startpos;
2440 int top, new_top;
2441
2442 if (NILP (window))
2443 window = selected_window;
2444 else
2445 CHECK_LIVE_WINDOW (window);
2446 w = XWINDOW (window);
2447
2448 startpos = marker_position (w->start);
2449 top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2450
2451 if (MINI_WINDOW_P (w) && top > 0)
2452 error ("Can't expand minibuffer to full frame");
2453
2454 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
2455
2456 /* Try to minimize scrolling, by setting the window start to the point
2457 will cause the text at the old window start to be at the same place
2458 on the frame. But don't try to do this if the window start is
2459 outside the visible portion (as might happen when the display is
2460 not current, due to typeahead). */
2461 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2462 if (new_top != top
2463 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2464 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2465 {
2466 struct position pos;
2467 struct buffer *obuf = current_buffer;
2468
2469 Fset_buffer (w->buffer);
2470 /* This computation used to temporarily move point, but that can
2471 have unwanted side effects due to text properties. */
2472 pos = *vmotion (startpos, -top, w);
2473
2474 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2475 w->window_end_valid = Qnil;
2476 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
2477 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
2478 : Qnil);
2479 /* We need to do this, so that the window-scroll-functions
2480 get called. */
2481 w->optional_new_start = Qt;
2482
2483 set_buffer_internal (obuf);
2484 }
2485
2486 return Qnil;
2487 }
2488
2489 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
2490 0, 2, "bDelete windows on (buffer): ",
2491 doc: /* Delete all windows showing BUFFER-OR-NAME.
2492 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2493 defaults to the current buffer.
2494
2495 Optional second argument FRAME controls which frames are affected.
2496 If optional argument FRAME is `visible', search all visible frames.
2497 If FRAME is 0, search all visible and iconified frames.
2498 If FRAME is nil, search all frames.
2499 If FRAME is t, search only the selected frame.
2500 If FRAME is a frame, search only that frame.
2501 When a window showing BUFFER-OR-NAME is dedicated and the only window of
2502 its frame, that frame is deleted when there are other frames left. */)
2503 (Lisp_Object buffer_or_name, Lisp_Object frame)
2504 {
2505 Lisp_Object buffer;
2506
2507 /* FRAME uses t and nil to mean the opposite of what window_loop
2508 expects. */
2509 if (NILP (frame))
2510 frame = Qt;
2511 else if (EQ (frame, Qt))
2512 frame = Qnil;
2513
2514 if (NILP (buffer_or_name))
2515 buffer = Fcurrent_buffer ();
2516 else
2517 {
2518 buffer = Fget_buffer (buffer_or_name);
2519 CHECK_BUFFER (buffer);
2520 }
2521
2522 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
2523
2524 return Qnil;
2525 }
2526
2527 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
2528 Sreplace_buffer_in_windows,
2529 0, 1, "bReplace buffer in windows: ",
2530 doc: /* Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
2531 BUFFER-OR-NAME may be a buffer or the name of an existing buffer and
2532 defaults to the current buffer.
2533
2534 When a window showing BUFFER-OR-NAME is dedicated that window is
2535 deleted. If that window is the only window on its frame, that frame is
2536 deleted too when there are other frames left. If there are no other
2537 frames left, some other buffer is displayed in that window. */)
2538 (Lisp_Object buffer_or_name)
2539 {
2540 Lisp_Object buffer;
2541
2542 if (NILP (buffer_or_name))
2543 buffer = Fcurrent_buffer ();
2544 else
2545 {
2546 buffer = Fget_buffer (buffer_or_name);
2547 CHECK_BUFFER (buffer);
2548 }
2549
2550 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
2551
2552 return Qnil;
2553 }
2554
2555 /* Replace BUFFER with some other buffer in all windows
2556 of all frames, even those on other keyboards. */
2557
2558 void
2559 replace_buffer_in_all_windows (Lisp_Object buffer)
2560 {
2561 Lisp_Object tail, frame;
2562
2563 /* A single call to window_loop won't do the job
2564 because it only considers frames on the current keyboard.
2565 So loop manually over frames, and handle each one. */
2566 FOR_EACH_FRAME (tail, frame)
2567 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
2568 }
2569 \f
2570 /* Set the height of WINDOW and all its inferiors. */
2571
2572 /* The smallest acceptable dimensions for a window. Anything smaller
2573 might crash Emacs. */
2574
2575 #define MIN_SAFE_WINDOW_WIDTH (2)
2576 #define MIN_SAFE_WINDOW_HEIGHT (1)
2577
2578 /* For wp non-zero the total number of columns of window w. Otherwise
2579 the total number of lines of w. */
2580
2581 #define WINDOW_TOTAL_SIZE(w, wp) \
2582 (wp ? WINDOW_TOTAL_COLS (w) : WINDOW_TOTAL_LINES (w))
2583
2584 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2585 minimum allowable size. */
2586
2587 void
2588 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
2589 {
2590 /* For height, we have to see:
2591 how many windows the frame has at minimum (one or two),
2592 and whether it has a menu bar or other special stuff at the top. */
2593 int min_height
2594 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2595 ? MIN_SAFE_WINDOW_HEIGHT
2596 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2597
2598 if (FRAME_TOP_MARGIN (frame) > 0)
2599 min_height += FRAME_TOP_MARGIN (frame);
2600
2601 if (*rows < min_height)
2602 *rows = min_height;
2603 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2604 *cols = MIN_SAFE_WINDOW_WIDTH;
2605 }
2606
2607 /* Value is non-zero if window W is fixed-size. WIDTH_P non-zero means
2608 check if W's width can be changed, otherwise check W's height.
2609 CHECK_SIBLINGS_P non-zero means check resizablity of WINDOW's
2610 siblings, too. If none of the siblings is resizable, WINDOW isn't
2611 either. */
2612
2613 static int
2614 window_fixed_size_p (struct window *w, int width_p, int check_siblings_p)
2615 {
2616 int fixed_p;
2617 struct window *c;
2618
2619 if (!NILP (w->hchild))
2620 {
2621 c = XWINDOW (w->hchild);
2622
2623 if (width_p)
2624 {
2625 /* A horizontal combination is fixed-width if all of if its
2626 children are. */
2627 while (c && window_fixed_size_p (c, width_p, 0))
2628 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2629 fixed_p = c == NULL;
2630 }
2631 else
2632 {
2633 /* A horizontal combination is fixed-height if one of if its
2634 children is. */
2635 while (c && !window_fixed_size_p (c, width_p, 0))
2636 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2637 fixed_p = c != NULL;
2638 }
2639 }
2640 else if (!NILP (w->vchild))
2641 {
2642 c = XWINDOW (w->vchild);
2643
2644 if (width_p)
2645 {
2646 /* A vertical combination is fixed-width if one of if its
2647 children is. */
2648 while (c && !window_fixed_size_p (c, width_p, 0))
2649 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2650 fixed_p = c != NULL;
2651 }
2652 else
2653 {
2654 /* A vertical combination is fixed-height if all of if its
2655 children are. */
2656 while (c && window_fixed_size_p (c, width_p, 0))
2657 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2658 fixed_p = c == NULL;
2659 }
2660 }
2661 else if (BUFFERP (w->buffer))
2662 {
2663 struct buffer *old = current_buffer;
2664 Lisp_Object val;
2665
2666 current_buffer = XBUFFER (w->buffer);
2667 val = find_symbol_value (Qwindow_size_fixed);
2668 current_buffer = old;
2669
2670 fixed_p = 0;
2671 if (!EQ (val, Qunbound))
2672 {
2673 fixed_p = !NILP (val);
2674
2675 if (fixed_p
2676 && ((EQ (val, Qheight) && width_p)
2677 || (EQ (val, Qwidth) && !width_p)))
2678 fixed_p = 0;
2679 }
2680
2681 /* Can't tell if this one is resizable without looking at
2682 siblings. If all siblings are fixed-size this one is too. */
2683 if (!fixed_p && check_siblings_p && WINDOWP (w->parent))
2684 {
2685 Lisp_Object child;
2686
2687 for (child = w->prev; WINDOWP (child); child = XWINDOW (child)->prev)
2688 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2689 break;
2690
2691 if (NILP (child))
2692 for (child = w->next; WINDOWP (child); child = XWINDOW (child)->next)
2693 if (!window_fixed_size_p (XWINDOW (child), width_p, 0))
2694 break;
2695
2696 if (NILP (child))
2697 fixed_p = 1;
2698 }
2699 }
2700 else
2701 fixed_p = 1;
2702
2703 return fixed_p;
2704 }
2705
2706 /* Return minimum size of leaf window W. WIDTH_P non-zero means return
2707 the minimum width of W, WIDTH_P zero means return the minimum height
2708 of W. SAFE_P non-zero means ignore window-min-height|width but just
2709 return values that won't crash Emacs and don't hide components like
2710 fringes, scrollbars, or modelines. If WIDTH_P is zero and W is the
2711 minibuffer window, always return 1. */
2712
2713 static int
2714 window_min_size_2 (struct window *w, int width_p, int safe_p)
2715 {
2716 /* We should consider buffer-local values of window_min_height and
2717 window_min_width here. */
2718 if (width_p)
2719 {
2720 int safe_size = (MIN_SAFE_WINDOW_WIDTH
2721 + WINDOW_FRINGE_COLS (w)
2722 + WINDOW_SCROLL_BAR_COLS (w));
2723
2724 return safe_p ? safe_size : max (window_min_width, safe_size);
2725 }
2726 else if (MINI_WINDOW_P (w))
2727 return 1;
2728 else
2729 {
2730 int safe_size = (MIN_SAFE_WINDOW_HEIGHT
2731 + ((BUFFERP (w->buffer)
2732 && !NILP (BVAR (XBUFFER (w->buffer), mode_line_format)))
2733 ? 1 : 0));
2734
2735 return safe_p ? safe_size : max (window_min_height, safe_size);
2736 }
2737 }
2738
2739 /* Return minimum size of window W, not taking fixed-width windows into
2740 account. WIDTH_P non-zero means return the minimum width, otherwise
2741 return the minimum height. SAFE_P non-zero means ignore
2742 window-min-height|width but just return values that won't crash Emacs
2743 and don't hide components like fringes, scrollbars, or modelines. If
2744 W is a combination window, compute the minimum size from the minimum
2745 sizes of W's children. */
2746
2747 static int
2748 window_min_size_1 (struct window *w, int width_p, int safe_p)
2749 {
2750 struct window *c;
2751 int size;
2752
2753 if (!NILP (w->hchild))
2754 {
2755 /* W is a horizontal combination. */
2756 c = XWINDOW (w->hchild);
2757 size = 0;
2758
2759 if (width_p)
2760 {
2761 /* The minimum width of a horizontal combination is the sum of
2762 the minimum widths of its children. */
2763 while (c)
2764 {
2765 size += window_min_size_1 (c, 1, safe_p);
2766 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2767 }
2768 }
2769 else
2770 {
2771 /* The minimum height of a horizontal combination is the
2772 maximum of the minimum heights of its children. */
2773 while (c)
2774 {
2775 size = max (window_min_size_1 (c, 0, safe_p), size);
2776 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2777 }
2778 }
2779 }
2780 else if (!NILP (w->vchild))
2781 {
2782 /* W is a vertical combination. */
2783 c = XWINDOW (w->vchild);
2784 size = 0;
2785
2786 if (width_p)
2787 {
2788 /* The minimum width of a vertical combination is the maximum
2789 of the minimum widths of its children. */
2790 while (c)
2791 {
2792 size = max (window_min_size_1 (c, 1, safe_p), size);
2793 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2794 }
2795 }
2796 else
2797 {
2798 /* The minimum height of a vertical combination is the sum of
2799 the minimum height of its children. */
2800 while (c)
2801 {
2802 size += window_min_size_1 (c, 0, safe_p);
2803 c = WINDOWP (c->next) ? XWINDOW (c->next) : NULL;
2804 }
2805 }
2806 }
2807 else
2808 /* W is a leaf window. */
2809 size = window_min_size_2 (w, width_p, safe_p);
2810
2811 return size;
2812 }
2813
2814 /* Return the minimum size of window W, taking fixed-size windows into
2815 account. WIDTH_P non-zero means return the minimum width, otherwise
2816 return the minimum height. SAFE_P non-zero means ignore
2817 window-min-height|width but just return values that won't crash Emacs
2818 and don't hide components like fringes, scrollbars, or modelines.
2819 IGNORE_FIXED_P non-zero means ignore if W is fixed-size. Set *FIXED
2820 to 1 if W is fixed-size unless FIXED is null. */
2821
2822 static int
2823 window_min_size (struct window *w, int width_p, int safe_p, int ignore_fixed_p, int *fixed)
2824 {
2825 int size, fixed_p;
2826
2827 if (ignore_fixed_p)
2828 fixed_p = 0;
2829 else
2830 fixed_p = window_fixed_size_p (w, width_p, 1);
2831
2832 if (fixed)
2833 *fixed = fixed_p;
2834
2835 if (fixed_p)
2836 size = WINDOW_TOTAL_SIZE (w, width_p);
2837 else
2838 size = window_min_size_1 (w, width_p, safe_p);
2839
2840 return size;
2841 }
2842
2843
2844 /* Adjust the margins of window W if text area is too small.
2845 Return 1 if window width is ok after adjustment; 0 if window
2846 is still too narrow. */
2847
2848 static int
2849 adjust_window_margins (struct window *w)
2850 {
2851 int box_cols = (WINDOW_TOTAL_COLS (w)
2852 - WINDOW_FRINGE_COLS (w)
2853 - WINDOW_SCROLL_BAR_COLS (w));
2854 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2855 + WINDOW_RIGHT_MARGIN_COLS (w));
2856
2857 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2858 return 1;
2859
2860 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2861 return 0;
2862
2863 /* Window's text area is too narrow, but reducing the window
2864 margins will fix that. */
2865 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2866 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2867 {
2868 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2869 w->left_margin_cols = w->right_margin_cols
2870 = make_number (margin_cols/2);
2871 else
2872 w->right_margin_cols = make_number (margin_cols);
2873 }
2874 else
2875 w->left_margin_cols = make_number (margin_cols);
2876 return 1;
2877 }
2878
2879 /* Calculate new sizes for windows in the list FORWARD when their
2880 compound size goes from TOTAL to SIZE. TOTAL must be greater than
2881 SIZE. The number of windows in FORWARD is NCHILDREN, and the number
2882 that can shrink is SHRINKABLE. Fixed-size windows may be shrunk if
2883 and only if RESIZE_FIXED_P is non-zero. WIDTH_P non-zero means
2884 shrink columns, otherwise shrink lines.
2885
2886 SAFE_P zero means windows may be sized down to window-min-height
2887 lines (window-min-window columns for WIDTH_P non-zero). SAFE_P
2888 non-zero means windows may be sized down to their minimum safe sizes
2889 taking into account the space needed to display modelines, fringes,
2890 and scrollbars.
2891
2892 This function returns an allocated array of new sizes that the caller
2893 must free. A size -1 means the window is fixed and RESIZE_FIXED_P is
2894 zero. A size zero means the window shall be deleted. Array index 0
2895 refers to the first window in FORWARD, 1 to the second, and so on.
2896
2897 This function resizes windows proportionally to their size. It also
2898 tries to preserve smaller windows by resizing larger windows before
2899 resizing any window to zero. If resize_proportionally is non-nil for
2900 a specific window, it will attempt to strictly resize that window
2901 proportionally, even at the expense of deleting smaller windows. */
2902 static int *
2903 shrink_windows (int total, int size, int nchildren, int shrinkable,
2904 int resize_fixed_p, Lisp_Object forward, int width_p, int safe_p)
2905 {
2906 int available_resize = 0;
2907 int *new_sizes, *min_sizes;
2908 struct window *c;
2909 Lisp_Object child;
2910 int smallest = total;
2911 int total_removed = 0;
2912 int total_shrink = total - size;
2913 int i;
2914
2915 new_sizes = xmalloc (sizeof (*new_sizes) * nchildren);
2916 min_sizes = xmalloc (sizeof (*min_sizes) * nchildren);
2917
2918 for (i = 0, child = forward; !NILP (child); child = c->next, ++i)
2919 {
2920 int child_size;
2921
2922 c = XWINDOW (child);
2923 child_size = WINDOW_TOTAL_SIZE (c, width_p);
2924
2925 if (!resize_fixed_p && window_fixed_size_p (c, width_p, 0))
2926 new_sizes[i] = -1;
2927 else
2928 {
2929 new_sizes[i] = child_size;
2930 min_sizes[i] = window_min_size_1 (c, width_p, safe_p);
2931 if (child_size > min_sizes[i]
2932 && NILP (c->resize_proportionally))
2933 available_resize += child_size - min_sizes[i];
2934 }
2935 }
2936 /* We might need to shrink some windows to zero. Find the smallest
2937 windows and set them to 0 until we can fulfil the new size. */
2938
2939 while (shrinkable > 1 && size + available_resize < total)
2940 {
2941 for (i = 0; i < nchildren; ++i)
2942 if (new_sizes[i] > 0 && smallest > new_sizes[i])
2943 smallest = new_sizes[i];
2944
2945 for (i = 0; i < nchildren; ++i)
2946 if (new_sizes[i] == smallest)
2947 {
2948 /* Resize this window down to zero. */
2949 new_sizes[i] = 0;
2950 if (smallest > min_sizes[i])
2951 available_resize -= smallest - min_sizes[i];
2952 available_resize += smallest;
2953 --shrinkable;
2954 total_removed += smallest;
2955
2956 /* We don't know what the smallest is now. */
2957 smallest = total;
2958
2959 /* Out of for, just remove one window at the time and
2960 check again if we have enough space. */
2961 break;
2962 }
2963 }
2964
2965 /* Now, calculate the new sizes. Try to shrink each window
2966 proportional to its size. */
2967 for (i = 0; i < nchildren; ++i)
2968 {
2969 if (new_sizes[i] > min_sizes[i])
2970 {
2971 int to_shrink = total_shrink * new_sizes[i] / total;
2972
2973 if (new_sizes[i] - to_shrink < min_sizes[i])
2974 to_shrink = new_sizes[i] - min_sizes[i];
2975 new_sizes[i] -= to_shrink;
2976 total_removed += to_shrink;
2977 }
2978 }
2979
2980 /* Any reminder due to rounding, we just subtract from windows
2981 that are left and still can be shrunk. */
2982 while (total_shrink > total_removed)
2983 {
2984 int nonzero_sizes = 0;
2985 int nonzero_idx = -1;
2986
2987 for (i = 0; i < nchildren; ++i)
2988 if (new_sizes[i] > 0)
2989 {
2990 ++nonzero_sizes;
2991 nonzero_idx = i;
2992 }
2993
2994 for (i = 0; i < nchildren; ++i)
2995 if (new_sizes[i] > min_sizes[i])
2996 {
2997 --new_sizes[i];
2998 ++total_removed;
2999
3000 /* Out of for, just shrink one window at the time and
3001 check again if we have enough space. */
3002 break;
3003 }
3004
3005 /* Special case, only one window left. */
3006 if (nonzero_sizes == 1)
3007 break;
3008 }
3009
3010 /* Any surplus due to rounding, we add to windows that are left. */
3011 while (total_shrink < total_removed)
3012 {
3013 for (i = 0; i < nchildren; ++i)
3014 {
3015 if (new_sizes[i] != 0 && total_shrink < total_removed)
3016 {
3017 ++new_sizes[i];
3018 --total_removed;
3019 break;
3020 }
3021 }
3022 }
3023
3024 xfree (min_sizes);
3025
3026 return new_sizes;
3027 }
3028
3029 /* Set WINDOW's height or width to SIZE. WIDTH_P non-zero means set
3030 WINDOW's width. Resize WINDOW's children, if any, so that they keep
3031 their proportionate size relative to WINDOW.
3032
3033 If FIRST_ONLY is 1, change only the first of WINDOW's children when
3034 they are in series. If LAST_ONLY is 1, change only the last of
3035 WINDOW's children when they are in series.
3036
3037 Propagate WINDOW's top or left edge position to children. Delete
3038 windows that become too small unless NODELETE_P is 1. When
3039 NODELETE_P equals 2 do not honor settings for window-min-height and
3040 window-min-width when resizing windows but use safe defaults instead.
3041 This should give better behavior when resizing frames. */
3042
3043 static void
3044 size_window (Lisp_Object window, int size, int width_p, int nodelete_p, int first_only, int last_only)
3045 {
3046 struct window *w = XWINDOW (window);
3047 struct window *c;
3048 Lisp_Object child, *forward, *sideward;
3049 int old_size = WINDOW_TOTAL_SIZE (w, width_p);
3050
3051 size = max (0, size);
3052
3053 /* Delete WINDOW if it's too small. */
3054 if (nodelete_p != 1 && !NILP (w->parent)
3055 && size < window_min_size_1 (w, width_p, nodelete_p == 2))
3056 {
3057 delete_window (window);
3058 return;
3059 }
3060
3061 /* Set redisplay hints. */
3062 w->last_modified = make_number (0);
3063 w->last_overlay_modified = make_number (0);
3064 windows_or_buffers_changed++;
3065 FRAME_WINDOW_SIZES_CHANGED (XFRAME (w->frame)) = 1;
3066
3067 if (width_p)
3068 {
3069 sideward = &w->vchild;
3070 forward = &w->hchild;
3071 w->total_cols = make_number (size);
3072 adjust_window_margins (w);
3073 }
3074 else
3075 {
3076 sideward = &w->hchild;
3077 forward = &w->vchild;
3078 w->total_lines = make_number (size);
3079 w->orig_total_lines = Qnil;
3080 }
3081
3082 if (!NILP (*sideward))
3083 {
3084 /* We have a chain of parallel siblings whose size should all change. */
3085 for (child = *sideward; !NILP (child); child = c->next)
3086 {
3087 c = XWINDOW (child);
3088 if (width_p)
3089 c->left_col = w->left_col;
3090 else
3091 c->top_line = w->top_line;
3092 size_window (child, size, width_p, nodelete_p,
3093 first_only, last_only);
3094 }
3095 }
3096 else if (!NILP (*forward) && last_only)
3097 {
3098 /* Change the last in a series of siblings. */
3099 Lisp_Object last_child;
3100 int child_size;
3101
3102 for (child = *forward; !NILP (child); child = c->next)
3103 {
3104 c = XWINDOW (child);
3105 last_child = child;
3106 }
3107
3108 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3109 size_window (last_child, size - old_size + child_size,
3110 width_p, nodelete_p, first_only, last_only);
3111 }
3112 else if (!NILP (*forward) && first_only)
3113 {
3114 /* Change the first in a series of siblings. */
3115 int child_size;
3116
3117 child = *forward;
3118 c = XWINDOW (child);
3119
3120 if (width_p)
3121 c->left_col = w->left_col;
3122 else
3123 c->top_line = w->top_line;
3124
3125 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3126 size_window (child, size - old_size + child_size,
3127 width_p, nodelete_p, first_only, last_only);
3128 }
3129 else if (!NILP (*forward))
3130 {
3131 int fixed_size, each, extra, n;
3132 int resize_fixed_p, nfixed;
3133 int last_pos, first_pos, nchildren, total;
3134 int *new_sizes = NULL;
3135
3136 /* Determine the fixed-size portion of this window, and the
3137 number of child windows. */
3138 fixed_size = nchildren = nfixed = total = 0;
3139 for (child = *forward; !NILP (child); child = c->next, ++nchildren)
3140 {
3141 int child_size;
3142
3143 c = XWINDOW (child);
3144 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3145 total += child_size;
3146
3147 if (window_fixed_size_p (c, width_p, 0))
3148 {
3149 fixed_size += child_size;
3150 ++nfixed;
3151 }
3152 }
3153
3154 /* If the new size is smaller than fixed_size, or if there
3155 aren't any resizable windows, allow resizing fixed-size
3156 windows. */
3157 resize_fixed_p = nfixed == nchildren || size < fixed_size;
3158
3159 /* Compute how many lines/columns to add/remove to each child. The
3160 value of extra takes care of rounding errors. */
3161 n = resize_fixed_p ? nchildren : nchildren - nfixed;
3162 if (size < total && n > 1)
3163 new_sizes = shrink_windows (total, size, nchildren, n,
3164 resize_fixed_p, *forward, width_p,
3165 nodelete_p == 2);
3166 else
3167 {
3168 each = (size - total) / n;
3169 extra = (size - total) - n * each;
3170 }
3171
3172 /* Compute new children heights and edge positions. */
3173 first_pos = width_p ? XINT (w->left_col) : XINT (w->top_line);
3174 last_pos = first_pos;
3175 for (n = 0, child = *forward; !NILP (child); child = c->next, ++n)
3176 {
3177 int new_size, old_size;
3178
3179 c = XWINDOW (child);
3180 old_size = WINDOW_TOTAL_SIZE (c, width_p);
3181 new_size = old_size;
3182
3183 /* The top or left edge position of this child equals the
3184 bottom or right edge of its predecessor. */
3185 if (width_p)
3186 c->left_col = make_number (last_pos);
3187 else
3188 c->top_line = make_number (last_pos);
3189
3190 /* If this child can be resized, do it. */
3191 if (resize_fixed_p || !window_fixed_size_p (c, width_p, 0))
3192 {
3193 new_size = new_sizes ? new_sizes[n] : old_size + each + extra;
3194 extra = 0;
3195 }
3196
3197 /* Set new size. Note that size_window also propagates
3198 edge positions to children, so it's not a no-op if we
3199 didn't change the child's size. */
3200 size_window (child, new_size, width_p, 1, first_only, last_only);
3201
3202 /* Remember the bottom/right edge position of this child; it
3203 will be used to set the top/left edge of the next child. */
3204 last_pos += new_size;
3205 }
3206
3207 xfree (new_sizes);
3208
3209 /* We should have covered the parent exactly with child windows. */
3210 xassert (size == last_pos - first_pos);
3211
3212 /* Now delete any children that became too small. */
3213 if (nodelete_p != 1)
3214 for (child = *forward; !NILP (child); child = c->next)
3215 {
3216 int child_size;
3217
3218 c = XWINDOW (child);
3219 child_size = WINDOW_TOTAL_SIZE (c, width_p);
3220 size_window (child, child_size, width_p, nodelete_p,
3221 first_only, last_only);
3222 }
3223 }
3224 }
3225
3226 /* Set WINDOW's height to HEIGHT, and recursively change the height of
3227 WINDOW's children. NODELETE zero means windows that have become
3228 smaller than window-min-height in the process may be deleted.
3229 NODELETE 1 means never delete windows that become too small in the
3230 process. (The caller should check later and do so if appropriate.)
3231 NODELETE 2 means delete only windows that have become too small to be
3232 displayed correctly. */
3233
3234 void
3235 set_window_height (Lisp_Object window, int height, int nodelete)
3236 {
3237 size_window (window, height, 0, nodelete, 0, 0);
3238 }
3239
3240 /* Set WINDOW's width to WIDTH, and recursively change the width of
3241 WINDOW's children. NODELETE zero means windows that have become
3242 smaller than window-min-width in the process may be deleted.
3243 NODELETE 1 means never delete windows that become too small in the
3244 process. (The caller should check later and do so if appropriate.)
3245 NODELETE 2 means delete only windows that have become too small to be
3246 displayed correctly. */
3247
3248 void
3249 set_window_width (Lisp_Object window, int width, int nodelete)
3250 {
3251 size_window (window, width, 1, nodelete, 0, 0);
3252 }
3253
3254 /* Change window heights in windows rooted in WINDOW by N lines. */
3255
3256 void
3257 change_window_heights (Lisp_Object window, int n)
3258 {
3259 struct window *w = XWINDOW (window);
3260
3261 XSETFASTINT (w->top_line, XFASTINT (w->top_line) + n);
3262 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - n);
3263
3264 if (INTEGERP (w->orig_top_line))
3265 XSETFASTINT (w->orig_top_line, XFASTINT (w->orig_top_line) + n);
3266 if (INTEGERP (w->orig_total_lines))
3267 XSETFASTINT (w->orig_total_lines, XFASTINT (w->orig_total_lines) - n);
3268
3269 /* Handle just the top child in a vertical split. */
3270 if (!NILP (w->vchild))
3271 change_window_heights (w->vchild, n);
3272
3273 /* Adjust all children in a horizontal split. */
3274 for (window = w->hchild; !NILP (window); window = w->next)
3275 {
3276 w = XWINDOW (window);
3277 change_window_heights (window, n);
3278 }
3279 }
3280
3281 \f
3282 int window_select_count;
3283
3284 EXFUN (Fset_window_fringes, 4);
3285 EXFUN (Fset_window_scroll_bars, 4);
3286
3287 static void
3288 run_funs (Lisp_Object funs)
3289 {
3290 for (; CONSP (funs); funs = XCDR (funs))
3291 if (!EQ (XCAR (funs), Qt))
3292 call0 (XCAR (funs));
3293 }
3294
3295 static Lisp_Object select_window_norecord (Lisp_Object window);
3296 static Lisp_Object select_frame_norecord (Lisp_Object frame);
3297
3298 void
3299 run_window_configuration_change_hook (struct frame *f)
3300 {
3301 int count = SPECPDL_INDEX ();
3302 Lisp_Object frame, global_wcch
3303 = Fdefault_value (Qwindow_configuration_change_hook);
3304 XSETFRAME (frame, f);
3305
3306 if (NILP (Vrun_hooks))
3307 return;
3308
3309 if (SELECTED_FRAME () != f)
3310 {
3311 record_unwind_protect (select_frame_norecord, Fselected_frame ());
3312 Fselect_frame (frame, Qt);
3313 }
3314
3315 /* Use the right buffer. Matters when running the local hooks. */
3316 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
3317 {
3318 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3319 Fset_buffer (Fwindow_buffer (Qnil));
3320 }
3321
3322 /* Look for buffer-local values. */
3323 {
3324 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
3325 for (; CONSP (windows); windows = XCDR (windows))
3326 {
3327 Lisp_Object window = XCAR (windows);
3328 Lisp_Object buffer = Fwindow_buffer (window);
3329 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
3330 buffer)))
3331 {
3332 int count = SPECPDL_INDEX ();
3333 record_unwind_protect (select_window_norecord, Fselected_window ());
3334 select_window_norecord (window);
3335 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
3336 buffer));
3337 unbind_to (count, Qnil);
3338 }
3339 }
3340 }
3341
3342 run_funs (global_wcch);
3343 unbind_to (count, Qnil);
3344 }
3345
3346 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
3347 means it's allowed to run hooks. See make_frame for a case where
3348 it's not allowed. KEEP_MARGINS_P non-zero means that the current
3349 margins, fringes, and scroll-bar settings of the window are not
3350 reset from the buffer's local settings. */
3351
3352 void
3353 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
3354 {
3355 struct window *w = XWINDOW (window);
3356 struct buffer *b = XBUFFER (buffer);
3357 int count = SPECPDL_INDEX ();
3358 int samebuf = EQ (buffer, w->buffer);
3359
3360 w->buffer = buffer;
3361
3362 if (EQ (window, selected_window))
3363 BVAR (b, last_selected_window) = window;
3364
3365 /* Let redisplay errors through. */
3366 b->display_error_modiff = 0;
3367
3368 /* Update time stamps of buffer display. */
3369 if (INTEGERP (BVAR (b, display_count)))
3370 XSETINT (BVAR (b, display_count), XINT (BVAR (b, display_count)) + 1);
3371 BVAR (b, display_time) = Fcurrent_time ();
3372
3373 XSETFASTINT (w->window_end_pos, 0);
3374 XSETFASTINT (w->window_end_vpos, 0);
3375 memset (&w->last_cursor, 0, sizeof w->last_cursor);
3376 w->window_end_valid = Qnil;
3377 if (!(keep_margins_p && samebuf))
3378 { /* If we're not actually changing the buffer, don't reset hscroll and
3379 vscroll. This case happens for example when called from
3380 change_frame_size_1, where we use a dummy call to
3381 Fset_window_buffer on the frame's selected window (and no other)
3382 just in order to run window-configuration-change-hook.
3383 Resetting hscroll and vscroll here is problematic for things like
3384 image-mode and doc-view-mode since it resets the image's position
3385 whenever we resize the frame. */
3386 w->hscroll = w->min_hscroll = make_number (0);
3387 w->vscroll = 0;
3388 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3389 set_marker_restricted (w->start,
3390 make_number (b->last_window_start),
3391 buffer);
3392 w->start_at_line_beg = Qnil;
3393 w->force_start = Qnil;
3394 XSETFASTINT (w->last_modified, 0);
3395 XSETFASTINT (w->last_overlay_modified, 0);
3396 }
3397 /* Maybe we could move this into the `if' but it's not obviously safe and
3398 I doubt it's worth the trouble. */
3399 windows_or_buffers_changed++;
3400
3401 /* We must select BUFFER for running the window-scroll-functions. */
3402 /* We can't check ! NILP (Vwindow_scroll_functions) here
3403 because that might itself be a local variable. */
3404 if (window_initialized)
3405 {
3406 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3407 Fset_buffer (buffer);
3408 }
3409
3410 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3411
3412 if (!keep_margins_p)
3413 {
3414 /* Set left and right marginal area width etc. from buffer. */
3415
3416 /* This may call adjust_window_margins three times, so
3417 temporarily disable window margins. */
3418 Lisp_Object save_left = w->left_margin_cols;
3419 Lisp_Object save_right = w->right_margin_cols;
3420
3421 w->left_margin_cols = w->right_margin_cols = Qnil;
3422
3423 Fset_window_fringes (window,
3424 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3425 BVAR (b, fringes_outside_margins));
3426
3427 Fset_window_scroll_bars (window,
3428 BVAR (b, scroll_bar_width),
3429 BVAR (b, vertical_scroll_bar_type), Qnil);
3430
3431 w->left_margin_cols = save_left;
3432 w->right_margin_cols = save_right;
3433
3434 Fset_window_margins (window,
3435 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3436 }
3437
3438 if (run_hooks_p)
3439 {
3440 if (! NILP (Vwindow_scroll_functions))
3441 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3442 Fmarker_position (w->start));
3443 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3444 }
3445
3446 unbind_to (count, Qnil);
3447 }
3448
3449
3450 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3451 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3452 WINDOW defaults to the selected window. BUFFER-OR-NAME must be a buffer
3453 or the name of an existing buffer. Optional third argument KEEP-MARGINS
3454 non-nil means that WINDOW's current display margins, fringe widths, and
3455 scroll bar settings are preserved; the default is to reset these from
3456 the local settings for BUFFER-OR-NAME or the frame defaults. Return nil.
3457
3458 This function throws an error when WINDOW is strongly dedicated to its
3459 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3460 already display BUFFER-OR-NAME.
3461
3462 This function runs `window-scroll-functions' before running
3463 `window-configuration-change-hook'. */)
3464 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3465 {
3466 register Lisp_Object tem, buffer;
3467 register struct window *w = decode_window (window);
3468
3469 XSETWINDOW (window, w);
3470 buffer = Fget_buffer (buffer_or_name);
3471 CHECK_BUFFER (buffer);
3472 if (NILP (BVAR (XBUFFER (buffer), name)))
3473 error ("Attempt to display deleted buffer");
3474
3475 tem = w->buffer;
3476 if (NILP (tem))
3477 error ("Window is deleted");
3478 else if (!EQ (tem, Qt))
3479 /* w->buffer is t when the window is first being set up. */
3480 {
3481 if (EQ (tem, buffer))
3482 return Qnil;
3483 else if (EQ (w->dedicated, Qt))
3484 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3485 else
3486 w->dedicated = Qnil;
3487
3488 unshow_buffer (w);
3489 }
3490
3491 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3492 return Qnil;
3493 }
3494
3495 /* If select_window is called with inhibit_point_swap non-zero it will
3496 not store point of the old selected window's buffer back into that
3497 window's pointm slot. This is needed by Fset_window_configuration to
3498 avoid that the display routine is called with selected_window set to
3499 Qnil causing a subsequent crash. */
3500
3501 static Lisp_Object
3502 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
3503 {
3504 register struct window *w;
3505 register struct window *ow;
3506 struct frame *sf;
3507
3508 CHECK_LIVE_WINDOW (window);
3509
3510 w = XWINDOW (window);
3511 w->frozen_window_start_p = 0;
3512
3513 if (NILP (norecord))
3514 {
3515 ++window_select_count;
3516 XSETFASTINT (w->use_time, window_select_count);
3517 record_buffer (w->buffer);
3518 }
3519
3520 if (EQ (window, selected_window) && !inhibit_point_swap)
3521 return window;
3522
3523 sf = SELECTED_FRAME ();
3524 if (XFRAME (WINDOW_FRAME (w)) != sf)
3525 {
3526 XFRAME (WINDOW_FRAME (w))->selected_window = window;
3527 /* Use this rather than Fhandle_switch_frame
3528 so that FRAME_FOCUS_FRAME is moved appropriately as we
3529 move around in the state where a minibuffer in a separate
3530 frame is active. */
3531 Fselect_frame (WINDOW_FRAME (w), norecord);
3532 /* Fselect_frame called us back so we've done all the work already. */
3533 eassert (EQ (window, selected_window));
3534 return window;
3535 }
3536 else
3537 sf->selected_window = window;
3538
3539 /* Store the current buffer's actual point into the
3540 old selected window. It belongs to that window,
3541 and when the window is not selected, must be in the window. */
3542 if (!inhibit_point_swap)
3543 {
3544 ow = XWINDOW (selected_window);
3545 if (! NILP (ow->buffer))
3546 set_marker_both (ow->pointm, ow->buffer,
3547 BUF_PT (XBUFFER (ow->buffer)),
3548 BUF_PT_BYTE (XBUFFER (ow->buffer)));
3549 }
3550
3551 selected_window = window;
3552
3553 Fset_buffer (w->buffer);
3554
3555 BVAR (XBUFFER (w->buffer), last_selected_window) = window;
3556
3557 /* Go to the point recorded in the window.
3558 This is important when the buffer is in more
3559 than one window. It also matters when
3560 redisplay_window has altered point after scrolling,
3561 because it makes the change only in the window. */
3562 {
3563 register EMACS_INT new_point = marker_position (w->pointm);
3564 if (new_point < BEGV)
3565 SET_PT (BEGV);
3566 else if (new_point > ZV)
3567 SET_PT (ZV);
3568 else
3569 SET_PT (new_point);
3570 }
3571
3572 windows_or_buffers_changed++;
3573 return window;
3574 }
3575
3576
3577 /* Note that selected_window can be nil when this is called from
3578 Fset_window_configuration. */
3579
3580 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
3581 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
3582 If WINDOW is not already selected, make WINDOW's buffer current
3583 and make WINDOW the frame's selected window. Return WINDOW.
3584 Optional second arg NORECORD non-nil means do not put this buffer
3585 at the front of the list of recently selected ones and do not
3586 make this window the most recently selected one.
3587
3588 Note that the main editor command loop selects the buffer of the
3589 selected window before each command. */)
3590 (register Lisp_Object window, Lisp_Object norecord)
3591 {
3592 return select_window (window, norecord, 0);
3593 }
3594
3595 static Lisp_Object
3596 select_window_norecord (Lisp_Object window)
3597 {
3598 return WINDOW_LIVE_P (window)
3599 ? Fselect_window (window, Qt) : selected_window;
3600 }
3601
3602 static Lisp_Object
3603 select_frame_norecord (Lisp_Object frame)
3604 {
3605 return FRAME_LIVE_P (XFRAME (frame))
3606 ? Fselect_frame (frame, Qt) : selected_frame;
3607 }
3608 \f
3609 Lisp_Object
3610 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3611 {
3612 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3613 }
3614
3615 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3616 0, 1, 0,
3617 doc: /* Force all windows to be updated on next redisplay.
3618 If optional arg OBJECT is a window, force redisplay of that window only.
3619 If OBJECT is a buffer or buffer name, force redisplay of all windows
3620 displaying that buffer. */)
3621 (Lisp_Object object)
3622 {
3623 if (NILP (object))
3624 {
3625 windows_or_buffers_changed++;
3626 update_mode_lines++;
3627 return Qt;
3628 }
3629
3630 if (WINDOWP (object))
3631 {
3632 struct window *w = XWINDOW (object);
3633 mark_window_display_accurate (object, 0);
3634 w->update_mode_line = Qt;
3635 if (BUFFERP (w->buffer))
3636 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3637 ++update_mode_lines;
3638 return Qt;
3639 }
3640
3641 if (STRINGP (object))
3642 object = Fget_buffer (object);
3643 if (BUFFERP (object) && !NILP (BVAR (XBUFFER (object), name)))
3644 {
3645 /* Walk all windows looking for buffer, and force update
3646 of each of those windows. */
3647
3648 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3649 return NILP (object) ? Qnil : Qt;
3650 }
3651
3652 /* If nothing suitable was found, just return.
3653 We could signal an error, but this feature will typically be used
3654 asynchronously in timers or process sentinels, so we don't. */
3655 return Qnil;
3656 }
3657
3658 void
3659 temp_output_buffer_show (register Lisp_Object buf)
3660 {
3661 register struct buffer *old = current_buffer;
3662 register Lisp_Object window;
3663 register struct window *w;
3664
3665 BVAR (XBUFFER (buf), directory) = BVAR (current_buffer, directory);
3666
3667 Fset_buffer (buf);
3668 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3669 BEGV = BEG;
3670 ZV = Z;
3671 SET_PT (BEG);
3672 #if 0 /* rms: there should be no reason for this. */
3673 XBUFFER (buf)->prevent_redisplay_optimizations_p = 1;
3674 #endif
3675 set_buffer_internal (old);
3676
3677 if (!NILP (Vtemp_buffer_show_function))
3678 call1 (Vtemp_buffer_show_function, buf);
3679 else
3680 {
3681 window = display_buffer (buf, Qnil, Qnil);
3682
3683 if (!EQ (XWINDOW (window)->frame, selected_frame))
3684 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3685 Vminibuf_scroll_window = window;
3686 w = XWINDOW (window);
3687 XSETFASTINT (w->hscroll, 0);
3688 XSETFASTINT (w->min_hscroll, 0);
3689 set_marker_restricted_both (w->start, buf, BEG, BEG);
3690 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3691
3692 /* Run temp-buffer-show-hook, with the chosen window selected
3693 and its buffer current. */
3694
3695 if (!NILP (Vrun_hooks)
3696 && !NILP (Fboundp (Qtemp_buffer_show_hook))
3697 && !NILP (Fsymbol_value (Qtemp_buffer_show_hook)))
3698 {
3699 int count = SPECPDL_INDEX ();
3700 Lisp_Object prev_window, prev_buffer;
3701 prev_window = selected_window;
3702 XSETBUFFER (prev_buffer, old);
3703
3704 /* Select the window that was chosen, for running the hook.
3705 Note: Both Fselect_window and select_window_norecord may
3706 set-buffer to the buffer displayed in the window,
3707 so we need to save the current buffer. --stef */
3708 record_unwind_protect (Fset_buffer, prev_buffer);
3709 record_unwind_protect (select_window_norecord, prev_window);
3710 Fselect_window (window, Qt);
3711 Fset_buffer (w->buffer);
3712 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
3713 unbind_to (count, Qnil);
3714 }
3715 }
3716 }
3717
3718 DEFUN ("internal-temp-output-buffer-show",
3719 Ftemp_output_buffer_show, Stemp_output_buffer_show,
3720 1, 1, 0,
3721 doc: /* Internal function for `with-output-to-temp-buffer''. */)
3722 (Lisp_Object buf)
3723 {
3724 temp_output_buffer_show (buf);
3725 return Qnil;
3726 }
3727 \f
3728 static void
3729 make_dummy_parent (Lisp_Object window)
3730 {
3731 Lisp_Object new;
3732 register struct window *o, *p;
3733 int i;
3734
3735 o = XWINDOW (window);
3736 p = allocate_window ();
3737 for (i = 0; i < VECSIZE (struct window); ++i)
3738 ((struct Lisp_Vector *) p)->contents[i]
3739 = ((struct Lisp_Vector *)o)->contents[i];
3740 XSETWINDOW (new, p);
3741
3742 ++sequence_number;
3743 XSETFASTINT (p->sequence_number, sequence_number);
3744
3745 /* Put new into window structure in place of window */
3746 replace_window (window, new);
3747
3748 o->next = Qnil;
3749 o->prev = Qnil;
3750 o->vchild = Qnil;
3751 o->hchild = Qnil;
3752 o->parent = new;
3753
3754 p->start = Qnil;
3755 p->pointm = Qnil;
3756 p->buffer = Qnil;
3757 }
3758
3759 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
3760 doc: /* Split WINDOW, putting SIZE lines in the first of the pair.
3761 WINDOW defaults to selected one and SIZE to half its size.
3762 If optional third arg HORIZONTAL is non-nil, split side by side and put
3763 SIZE columns in the first of the pair. In that case, SIZE includes that
3764 window's scroll bar, or the divider column to its right.
3765 Interactively, all arguments are nil.
3766 Returns the newly created window (which is the lower or rightmost one).
3767 The upper or leftmost window is the original one, and remains selected
3768 if it was selected before.
3769
3770 See Info node `(elisp)Splitting Windows' for more details and examples. */)
3771 (Lisp_Object window, Lisp_Object size, Lisp_Object horizontal)
3772 {
3773 register Lisp_Object new;
3774 register struct window *o, *p;
3775 FRAME_PTR fo;
3776 register int size_int;
3777
3778 if (NILP (window))
3779 window = selected_window;
3780 else
3781 CHECK_LIVE_WINDOW (window);
3782
3783 o = XWINDOW (window);
3784 fo = XFRAME (WINDOW_FRAME (o));
3785
3786 if (NILP (size))
3787 {
3788 if (!NILP (horizontal))
3789 /* Calculate the size of the left-hand window, by dividing
3790 the usable space in columns by two.
3791 We round up, since the left-hand window may include
3792 a dividing line, while the right-hand may not. */
3793 size_int = (XFASTINT (o->total_cols) + 1) >> 1;
3794 else
3795 size_int = XFASTINT (o->total_lines) >> 1;
3796 }
3797 else
3798 {
3799 CHECK_NUMBER (size);
3800 size_int = XINT (size);
3801 }
3802
3803 if (MINI_WINDOW_P (o))
3804 error ("Attempt to split minibuffer window");
3805 else if (window_fixed_size_p (o, !NILP (horizontal), 0))
3806 error ("Attempt to split fixed-size window");
3807
3808 if (NILP (horizontal))
3809 {
3810 int window_safe_height = window_min_size_2 (o, 0, 0);
3811
3812 if (size_int < window_safe_height)
3813 error ("Window height %d too small (after splitting)", size_int);
3814 if (size_int + window_safe_height > XFASTINT (o->total_lines))
3815 error ("Window height %d too small (after splitting)",
3816 XFASTINT (o->total_lines) - size_int);
3817 if (NILP (o->parent)
3818 || NILP (XWINDOW (o->parent)->vchild))
3819 {
3820 make_dummy_parent (window);
3821 new = o->parent;
3822 XWINDOW (new)->vchild = window;
3823 }
3824 }
3825 else
3826 {
3827 int window_safe_width = window_min_size_2 (o, 1, 0);
3828
3829 if (size_int < window_safe_width)
3830 error ("Window width %d too small (after splitting)", size_int);
3831 if (size_int + window_safe_width > XFASTINT (o->total_cols))
3832 error ("Window width %d too small (after splitting)",
3833 XFASTINT (o->total_cols) - size_int);
3834 if (NILP (o->parent)
3835 || NILP (XWINDOW (o->parent)->hchild))
3836 {
3837 make_dummy_parent (window);
3838 new = o->parent;
3839 XWINDOW (new)->hchild = window;
3840 }
3841 }
3842
3843 /* Now we know that window's parent is a vertical combination
3844 if we are dividing vertically, or a horizontal combination
3845 if we are making side-by-side windows */
3846
3847 windows_or_buffers_changed++;
3848 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
3849 new = make_window ();
3850 p = XWINDOW (new);
3851
3852 p->frame = o->frame;
3853 p->next = o->next;
3854 if (!NILP (p->next))
3855 XWINDOW (p->next)->prev = new;
3856 p->prev = window;
3857 o->next = new;
3858 p->parent = o->parent;
3859 p->buffer = Qt;
3860 p->window_end_valid = Qnil;
3861 memset (&p->last_cursor, 0, sizeof p->last_cursor);
3862
3863 /* Duplicate special geometry settings. */
3864
3865 p->left_margin_cols = o->left_margin_cols;
3866 p->right_margin_cols = o->right_margin_cols;
3867 p->left_fringe_width = o->left_fringe_width;
3868 p->right_fringe_width = o->right_fringe_width;
3869 p->fringes_outside_margins = o->fringes_outside_margins;
3870 p->scroll_bar_width = o->scroll_bar_width;
3871 p->vertical_scroll_bar_type = o->vertical_scroll_bar_type;
3872
3873 /* Apportion the available frame space among the two new windows */
3874
3875 if (!NILP (horizontal))
3876 {
3877 p->total_lines = o->total_lines;
3878 p->top_line = o->top_line;
3879 XSETFASTINT (p->total_cols, XFASTINT (o->total_cols) - size_int);
3880 XSETFASTINT (o->total_cols, size_int);
3881 XSETFASTINT (p->left_col, XFASTINT (o->left_col) + size_int);
3882 adjust_window_margins (p);
3883 adjust_window_margins (o);
3884 }
3885 else
3886 {
3887 p->left_col = o->left_col;
3888 p->total_cols = o->total_cols;
3889 XSETFASTINT (p->total_lines, XFASTINT (o->total_lines) - size_int);
3890 XSETFASTINT (o->total_lines, size_int);
3891 XSETFASTINT (p->top_line, XFASTINT (o->top_line) + size_int);
3892 }
3893
3894 /* Adjust glyph matrices. */
3895 adjust_glyphs (fo);
3896
3897 Fset_window_buffer (new, o->buffer, Qt);
3898 return new;
3899 }
3900 \f
3901 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
3902 doc: /* Make selected window SIZE lines taller.
3903 Interactively, if no argument is given, make the selected window one
3904 line taller. If optional argument HORIZONTAL is non-nil, make selected
3905 window wider by SIZE columns. If SIZE is negative, shrink the window by
3906 -SIZE lines or columns. Return nil.
3907
3908 This function can delete windows if they get too small. The size of
3909 fixed size windows is not altered by this function. */)
3910 (Lisp_Object size, Lisp_Object horizontal)
3911 {
3912 CHECK_NUMBER (size);
3913 enlarge_window (selected_window, XINT (size), !NILP (horizontal));
3914
3915 run_window_configuration_change_hook (SELECTED_FRAME ());
3916
3917 return Qnil;
3918 }
3919
3920 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
3921 doc: /* Make selected window SIZE lines smaller.
3922 Interactively, if no argument is given, make the selected window one
3923 line smaller. If optional argument HORIZONTAL is non-nil, make the
3924 window narrower by SIZE columns. If SIZE is negative, enlarge selected
3925 window by -SIZE lines or columns. Return nil.
3926
3927 This function can delete windows if they get too small. The size of
3928 fixed size windows is not altered by this function. */)
3929 (Lisp_Object size, Lisp_Object horizontal)
3930 {
3931 CHECK_NUMBER (size);
3932 enlarge_window (selected_window, -XINT (size), !NILP (horizontal));
3933
3934 run_window_configuration_change_hook (SELECTED_FRAME ());
3935
3936 return Qnil;
3937 }
3938
3939 static int
3940 window_height (Lisp_Object window)
3941 {
3942 register struct window *p = XWINDOW (window);
3943 return WINDOW_TOTAL_LINES (p);
3944 }
3945
3946 static int
3947 window_width (Lisp_Object window)
3948 {
3949 register struct window *p = XWINDOW (window);
3950 return WINDOW_TOTAL_COLS (p);
3951 }
3952
3953
3954 #define CURBEG(w) \
3955 *(horiz_flag ? &(XWINDOW (w)->left_col) : &(XWINDOW (w)->top_line))
3956
3957 #define CURSIZE(w) \
3958 *(horiz_flag ? &(XWINDOW (w)->total_cols) : &(XWINDOW (w)->total_lines))
3959
3960
3961 /* Enlarge WINDOW by DELTA. HORIZ_FLAG nonzero means enlarge it
3962 horizontally; zero means do it vertically.
3963
3964 Siblings of the selected window are resized to fulfill the size
3965 request. If they become too small in the process, they may be
3966 deleted. */
3967
3968 static void
3969 enlarge_window (Lisp_Object window, int delta, int horiz_flag)
3970 {
3971 Lisp_Object parent, next, prev;
3972 struct window *p;
3973 Lisp_Object *sizep;
3974 int maximum;
3975 int (*sizefun) (Lisp_Object)
3976 = horiz_flag ? window_width : window_height;
3977 void (*setsizefun) (Lisp_Object, int, int)
3978 = (horiz_flag ? set_window_width : set_window_height);
3979
3980 /* Give up if this window cannot be resized. */
3981 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
3982 error ("Window is not resizable");
3983
3984 /* Find the parent of the selected window. */
3985 while (1)
3986 {
3987 p = XWINDOW (window);
3988 parent = p->parent;
3989
3990 if (NILP (parent))
3991 {
3992 if (horiz_flag)
3993 error ("No other window to side of this one");
3994 break;
3995 }
3996
3997 if (horiz_flag
3998 ? !NILP (XWINDOW (parent)->hchild)
3999 : !NILP (XWINDOW (parent)->vchild))
4000 break;
4001
4002 window = parent;
4003 }
4004
4005 sizep = &CURSIZE (window);
4006
4007 {
4008 register int maxdelta;
4009
4010 /* Compute the maximum size increment this window can have. */
4011
4012 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - XINT (*sizep)
4013 /* This is a main window followed by a minibuffer. */
4014 : !NILP (p->next) ? ((*sizefun) (p->next)
4015 - window_min_size (XWINDOW (p->next),
4016 horiz_flag, 0, 0, 0))
4017 /* This is a minibuffer following a main window. */
4018 : !NILP (p->prev) ? ((*sizefun) (p->prev)
4019 - window_min_size (XWINDOW (p->prev),
4020 horiz_flag, 0, 0, 0))
4021 /* This is a frame with only one window, a minibuffer-only
4022 or a minibufferless frame. */
4023 : (delta = 0));
4024
4025 if (delta > maxdelta)
4026 /* This case traps trying to make the minibuffer
4027 the full frame, or make the only window aside from the
4028 minibuffer the full frame. */
4029 delta = maxdelta;
4030 }
4031
4032 if (XINT (*sizep) + delta < window_min_size (XWINDOW (window),
4033 horiz_flag, 0, 0, 0))
4034 {
4035 delete_window (window);
4036 return;
4037 }
4038
4039 if (delta == 0)
4040 return;
4041
4042 /* Find the total we can get from other siblings without deleting them. */
4043 maximum = 0;
4044 for (next = p->next; WINDOWP (next); next = XWINDOW (next)->next)
4045 maximum += (*sizefun) (next) - window_min_size (XWINDOW (next),
4046 horiz_flag, 0, 0, 0);
4047 for (prev = p->prev; WINDOWP (prev); prev = XWINDOW (prev)->prev)
4048 maximum += (*sizefun) (prev) - window_min_size (XWINDOW (prev),
4049 horiz_flag, 0, 0, 0);
4050
4051 /* If we can get it all from them without deleting them, do so. */
4052 if (delta <= maximum)
4053 {
4054 Lisp_Object first_unaffected;
4055 Lisp_Object first_affected;
4056 int fixed_p;
4057
4058 next = p->next;
4059 prev = p->prev;
4060 first_affected = window;
4061 /* Look at one sibling at a time,
4062 moving away from this window in both directions alternately,
4063 and take as much as we can get without deleting that sibling. */
4064 while (delta != 0
4065 && (!NILP (next) || !NILP (prev)))
4066 {
4067 if (! NILP (next))
4068 {
4069 int this_one = ((*sizefun) (next)
4070 - window_min_size (XWINDOW (next), horiz_flag,
4071 0, 0, &fixed_p));
4072 if (!fixed_p)
4073 {
4074 if (this_one > delta)
4075 this_one = delta;
4076
4077 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
4078 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4079
4080 delta -= this_one;
4081 }
4082
4083 next = XWINDOW (next)->next;
4084 }
4085
4086 if (delta == 0)
4087 break;
4088
4089 if (! NILP (prev))
4090 {
4091 int this_one = ((*sizefun) (prev)
4092 - window_min_size (XWINDOW (prev), horiz_flag,
4093 0, 0, &fixed_p));
4094 if (!fixed_p)
4095 {
4096 if (this_one > delta)
4097 this_one = delta;
4098
4099 first_affected = prev;
4100
4101 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
4102 (*setsizefun) (window, XINT (*sizep) + this_one, 0);
4103
4104 delta -= this_one;
4105 }
4106
4107 prev = XWINDOW (prev)->prev;
4108 }
4109 }
4110
4111 xassert (delta == 0);
4112
4113 /* Now recalculate the edge positions of all the windows affected,
4114 based on the new sizes. */
4115 first_unaffected = next;
4116 prev = first_affected;
4117 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
4118 prev = next, next = XWINDOW (next)->next)
4119 {
4120 XSETINT (CURBEG (next), XINT (CURBEG (prev)) + (*sizefun) (prev));
4121 /* This does not change size of NEXT,
4122 but it propagates the new top edge to its children */
4123 (*setsizefun) (next, (*sizefun) (next), 0);
4124 }
4125 }
4126 else
4127 {
4128 register int delta1;
4129 register int opht = (*sizefun) (parent);
4130
4131 if (opht <= XINT (*sizep) + delta)
4132 {
4133 /* If trying to grow this window to or beyond size of the parent,
4134 just delete all the sibling windows. */
4135 Lisp_Object start, tem, next;
4136
4137 start = XWINDOW (parent)->vchild;
4138 if (NILP (start))
4139 start = XWINDOW (parent)->hchild;
4140
4141 /* Delete any siblings that come after WINDOW. */
4142 tem = XWINDOW (window)->next;
4143 while (! NILP (tem))
4144 {
4145 next = XWINDOW (tem)->next;
4146 delete_window (tem);
4147 tem = next;
4148 }
4149
4150 /* Delete any siblings that come after WINDOW.
4151 Note that if START is not WINDOW, then WINDOW still
4152 has siblings, so WINDOW has not yet replaced its parent. */
4153 tem = start;
4154 while (! EQ (tem, window))
4155 {
4156 next = XWINDOW (tem)->next;
4157 delete_window (tem);
4158 tem = next;
4159 }
4160 }
4161 else
4162 {
4163 /* Otherwise, make delta1 just right so that if we add
4164 delta1 lines to this window and to the parent, and then
4165 shrink the parent back to its original size, the new
4166 proportional size of this window will increase by delta.
4167
4168 The function size_window will compute the new height h'
4169 of the window from delta1 as:
4170
4171 e = delta1/n
4172 x = delta1 - delta1/n * n for the 1st resizable child
4173 h' = h + e + x
4174
4175 where n is the number of children that can be resized.
4176 We can ignore x by choosing a delta1 that is a multiple of
4177 n. We want the height of this window to come out as
4178
4179 h' = h + delta
4180
4181 So, delta1 must be
4182
4183 h + e = h + delta
4184 delta1/n = delta
4185 delta1 = n * delta.
4186
4187 The number of children n equals the number of resizable
4188 children of this window + 1 because we know window itself
4189 is resizable (otherwise we would have signaled an error).
4190
4191 This reasoning is not correct when other windows become too
4192 small and shrink_windows refuses to delete them. Below we
4193 use resize_proportionally to work around this problem. */
4194
4195 struct window *w = XWINDOW (window);
4196 Lisp_Object s;
4197 int n = 1;
4198
4199 for (s = w->next; WINDOWP (s); s = XWINDOW (s)->next)
4200 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4201 ++n;
4202 for (s = w->prev; WINDOWP (s); s = XWINDOW (s)->prev)
4203 if (!window_fixed_size_p (XWINDOW (s), horiz_flag, 0))
4204 ++n;
4205
4206 delta1 = n * delta;
4207
4208 /* Add delta1 lines or columns to this window, and to the parent,
4209 keeping things consistent while not affecting siblings. */
4210 XSETINT (CURSIZE (parent), opht + delta1);
4211 (*setsizefun) (window, XINT (*sizep) + delta1, 0);
4212
4213 /* Squeeze out delta1 lines or columns from our parent,
4214 shrinking this window and siblings proportionately. This
4215 brings parent back to correct size. Delta1 was calculated
4216 so this makes this window the desired size, taking it all
4217 out of the siblings.
4218
4219 Temporarily set resize_proportionally to Qt to assure that,
4220 if necessary, shrink_windows deletes smaller windows rather
4221 than shrink this window. */
4222 w->resize_proportionally = Qt;
4223 (*setsizefun) (parent, opht, 0);
4224 w->resize_proportionally = Qnil;
4225 }
4226 }
4227
4228 XSETFASTINT (p->last_modified, 0);
4229 XSETFASTINT (p->last_overlay_modified, 0);
4230
4231 /* Adjust glyph matrices. */
4232 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4233 }
4234
4235
4236 /* Adjust the size of WINDOW by DELTA, moving only its trailing edge.
4237 HORIZ_FLAG nonzero means adjust the width, moving the right edge.
4238 zero means adjust the height, moving the bottom edge.
4239
4240 Following siblings of the selected window are resized to fulfill
4241 the size request. If they become too small in the process, they
4242 are not deleted; instead, we signal an error. */
4243
4244 static void
4245 adjust_window_trailing_edge (Lisp_Object window, int delta, int horiz_flag)
4246 {
4247 Lisp_Object parent, child;
4248 struct window *p;
4249 Lisp_Object old_config = Fcurrent_window_configuration (Qnil);
4250 int delcount = window_deletion_count;
4251
4252 CHECK_WINDOW (window);
4253
4254 /* Give up if this window cannot be resized. */
4255 if (window_fixed_size_p (XWINDOW (window), horiz_flag, 1))
4256 error ("Window is not resizable");
4257
4258 while (1)
4259 {
4260 Lisp_Object first_parallel = Qnil;
4261
4262 if (NILP (window))
4263 {
4264 /* This happens if WINDOW on the previous iteration was
4265 at top level of the window tree. */
4266 Fset_window_configuration (old_config);
4267 error ("Specified window edge is fixed");
4268 }
4269
4270 p = XWINDOW (window);
4271 parent = p->parent;
4272
4273 /* See if this level has windows in parallel in the specified
4274 direction. If so, set FIRST_PARALLEL to the first one. */
4275 if (horiz_flag)
4276 {
4277 if (! NILP (parent) && !NILP (XWINDOW (parent)->vchild))
4278 first_parallel = XWINDOW (parent)->vchild;
4279 else if (NILP (parent) && !NILP (p->next))
4280 {
4281 /* Handle the vertical chain of main window and minibuffer
4282 which has no parent. */
4283 first_parallel = window;
4284 while (! NILP (XWINDOW (first_parallel)->prev))
4285 first_parallel = XWINDOW (first_parallel)->prev;
4286 }
4287 }
4288 else
4289 {
4290 if (! NILP (parent) && !NILP (XWINDOW (parent)->hchild))
4291 first_parallel = XWINDOW (parent)->hchild;
4292 }
4293
4294 /* If this level's succession is in the desired dimension,
4295 and this window is the last one, and there is no higher level,
4296 its trailing edge is fixed. */
4297 if (NILP (XWINDOW (window)->next) && NILP (first_parallel)
4298 && NILP (parent))
4299 {
4300 Fset_window_configuration (old_config);
4301 error ("Specified window edge is fixed");
4302 }
4303
4304 /* Don't make this window too small. */
4305 if (XINT (CURSIZE (window)) + delta
4306 < window_min_size_2 (XWINDOW (window), horiz_flag, 0))
4307 {
4308 Fset_window_configuration (old_config);
4309 error ("Cannot adjust window size as specified");
4310 }
4311
4312 /* Clear out some redisplay caches. */
4313 XSETFASTINT (p->last_modified, 0);
4314 XSETFASTINT (p->last_overlay_modified, 0);
4315
4316 /* Adjust this window's edge. */
4317 XSETINT (CURSIZE (window),
4318 XINT (CURSIZE (window)) + delta);
4319
4320 /* If this window has following siblings in the desired dimension,
4321 make them smaller, and exit the loop.
4322
4323 (If we reach the top of the tree and can never do this,
4324 we will fail and report an error, above.) */
4325 if (NILP (first_parallel))
4326 {
4327 if (!NILP (p->next))
4328 {
4329 /* This may happen for the minibuffer. In that case
4330 the window_deletion_count check below does not work. */
4331 if (XINT (CURSIZE (p->next)) - delta <= 0)
4332 {
4333 Fset_window_configuration (old_config);
4334 error ("Cannot adjust window size as specified");
4335 }
4336
4337 XSETINT (CURBEG (p->next),
4338 XINT (CURBEG (p->next)) + delta);
4339 size_window (p->next, XINT (CURSIZE (p->next)) - delta,
4340 horiz_flag, 0, 1, 0);
4341 break;
4342 }
4343 }
4344 else
4345 /* Here we have a chain of parallel siblings, in the other dimension.
4346 Change the size of the other siblings. */
4347 for (child = first_parallel;
4348 ! NILP (child);
4349 child = XWINDOW (child)->next)
4350 if (! EQ (child, window))
4351 size_window (child, XINT (CURSIZE (child)) + delta,
4352 horiz_flag, 0, 0, 1);
4353
4354 window = parent;
4355 }
4356
4357 /* If we made a window so small it got deleted,
4358 we failed. Report failure. */
4359 if (delcount != window_deletion_count)
4360 {
4361 Fset_window_configuration (old_config);
4362 error ("Cannot adjust window size as specified");
4363 }
4364
4365 /* Adjust glyph matrices. */
4366 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4367 }
4368
4369 #undef CURBEG
4370 #undef CURSIZE
4371
4372 DEFUN ("adjust-window-trailing-edge", Fadjust_window_trailing_edge,
4373 Sadjust_window_trailing_edge, 3, 3, 0,
4374 doc: /* Adjust the bottom or right edge of WINDOW by DELTA.
4375 If HORIZONTAL is non-nil, that means adjust the width, moving the right edge.
4376 Otherwise, adjust the height, moving the bottom edge.
4377
4378 Following siblings of the selected window are resized to fulfill
4379 the size request. If they become too small in the process, they
4380 are not deleted; instead, we signal an error. */)
4381 (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal)
4382 {
4383 CHECK_NUMBER (delta);
4384 if (NILP (window))
4385 window = selected_window;
4386 adjust_window_trailing_edge (window, XINT (delta), !NILP (horizontal));
4387
4388 run_window_configuration_change_hook
4389 (XFRAME (WINDOW_FRAME (XWINDOW (window))));
4390
4391 return Qnil;
4392 }
4393
4394
4395 \f
4396 /***********************************************************************
4397 Resizing Mini-Windows
4398 ***********************************************************************/
4399
4400 static void shrink_window_lowest_first (struct window *, int);
4401
4402 enum save_restore_action
4403 {
4404 CHECK_ORIG_SIZES,
4405 SAVE_ORIG_SIZES,
4406 RESTORE_ORIG_SIZES
4407 };
4408
4409 static int save_restore_orig_size (struct window *,
4410 enum save_restore_action);
4411
4412 /* Shrink windows rooted in window W to HEIGHT. Take the space needed
4413 from lowest windows first. */
4414
4415 static void
4416 shrink_window_lowest_first (struct window *w, int height)
4417 {
4418 struct window *c;
4419 Lisp_Object child;
4420 int old_height;
4421
4422 xassert (!MINI_WINDOW_P (w));
4423
4424 /* Set redisplay hints. */
4425 XSETFASTINT (w->last_modified, 0);
4426 XSETFASTINT (w->last_overlay_modified, 0);
4427 windows_or_buffers_changed++;
4428 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
4429
4430 old_height = XFASTINT (w->total_lines);
4431 XSETFASTINT (w->total_lines, height);
4432
4433 if (!NILP (w->hchild))
4434 {
4435 for (child = w->hchild; !NILP (child); child = c->next)
4436 {
4437 c = XWINDOW (child);
4438 c->top_line = w->top_line;
4439 shrink_window_lowest_first (c, height);
4440 }
4441 }
4442 else if (!NILP (w->vchild))
4443 {
4444 Lisp_Object last_child;
4445 int delta = old_height - height;
4446 int last_top;
4447
4448 last_child = Qnil;
4449
4450 /* Find the last child. We are taking space from lowest windows
4451 first, so we iterate over children from the last child
4452 backwards. */
4453 for (child = w->vchild; WINDOWP (child); child = XWINDOW (child)->next)
4454 last_child = child;
4455
4456 /* Size children down to their safe heights. */
4457 for (child = last_child; delta && !NILP (child); child = c->prev)
4458 {
4459 int this_one;
4460
4461 c = XWINDOW (child);
4462 this_one = XFASTINT (c->total_lines) - window_min_size_1 (c, 0, 1);
4463
4464 if (this_one > delta)
4465 this_one = delta;
4466
4467 shrink_window_lowest_first (c, XFASTINT (c->total_lines) - this_one);
4468 delta -= this_one;
4469 }
4470
4471 /* Compute new positions. */
4472 last_top = XINT (w->top_line);
4473 for (child = w->vchild; !NILP (child); child = c->next)
4474 {
4475 c = XWINDOW (child);
4476 c->top_line = make_number (last_top);
4477 shrink_window_lowest_first (c, XFASTINT (c->total_lines));
4478 last_top += XFASTINT (c->total_lines);
4479 }
4480 }
4481 }
4482
4483
4484 /* Save, restore, or check positions and sizes in the window tree
4485 rooted at W. ACTION says what to do.
4486
4487 If ACTION is CHECK_ORIG_SIZES, check if orig_top_line and
4488 orig_total_lines members are valid for all windows in the window
4489 tree. Value is non-zero if they are valid.
4490
4491 If ACTION is SAVE_ORIG_SIZES, save members top and height in
4492 orig_top_line and orig_total_lines for all windows in the tree.
4493
4494 If ACTION is RESTORE_ORIG_SIZES, restore top and height from values
4495 stored in orig_top_line and orig_total_lines for all windows. */
4496
4497 static int
4498 save_restore_orig_size (struct window *w, enum save_restore_action action)
4499 {
4500 int success_p = 1;
4501
4502 while (w)
4503 {
4504 if (!NILP (w->hchild))
4505 {
4506 if (!save_restore_orig_size (XWINDOW (w->hchild), action))
4507 success_p = 0;
4508 }
4509 else if (!NILP (w->vchild))
4510 {
4511 if (!save_restore_orig_size (XWINDOW (w->vchild), action))
4512 success_p = 0;
4513 }
4514
4515 switch (action)
4516 {
4517 case CHECK_ORIG_SIZES:
4518 if (!INTEGERP (w->orig_top_line) || !INTEGERP (w->orig_total_lines))
4519 return 0;
4520 break;
4521
4522 case SAVE_ORIG_SIZES:
4523 w->orig_top_line = w->top_line;
4524 w->orig_total_lines = w->total_lines;
4525 XSETFASTINT (w->last_modified, 0);
4526 XSETFASTINT (w->last_overlay_modified, 0);
4527 break;
4528
4529 case RESTORE_ORIG_SIZES:
4530 xassert (INTEGERP (w->orig_top_line) && INTEGERP (w->orig_total_lines));
4531 w->top_line = w->orig_top_line;
4532 w->total_lines = w->orig_total_lines;
4533 w->orig_total_lines = w->orig_top_line = Qnil;
4534 XSETFASTINT (w->last_modified, 0);
4535 XSETFASTINT (w->last_overlay_modified, 0);
4536 break;
4537
4538 default:
4539 abort ();
4540 }
4541
4542 w = NILP (w->next) ? NULL : XWINDOW (w->next);
4543 }
4544
4545 return success_p;
4546 }
4547
4548
4549 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we can
4550 without deleting other windows. */
4551
4552 void
4553 grow_mini_window (struct window *w, int delta)
4554 {
4555 struct frame *f = XFRAME (w->frame);
4556 struct window *root;
4557
4558 xassert (MINI_WINDOW_P (w));
4559 /* Commenting out the following assertion goes against the stated interface
4560 of the function, but it currently does not seem to do anything useful.
4561 See discussion of this issue in the thread for bug#4534.
4562 xassert (delta >= 0); */
4563
4564 /* Compute how much we can enlarge the mini-window without deleting
4565 other windows. */
4566 root = XWINDOW (FRAME_ROOT_WINDOW (f));
4567 if (delta > 0)
4568 {
4569 int min_height = window_min_size (root, 0, 0, 0, 0);
4570 if (XFASTINT (root->total_lines) - delta < min_height)
4571 /* Note that the root window may already be smaller than
4572 min_height. */
4573 delta = max (0, XFASTINT (root->total_lines) - min_height);
4574 }
4575
4576 if (delta)
4577 {
4578 /* Save original window sizes and positions, if not already done. */
4579 if (!save_restore_orig_size (root, CHECK_ORIG_SIZES))
4580 save_restore_orig_size (root, SAVE_ORIG_SIZES);
4581
4582 /* Shrink other windows. */
4583 shrink_window_lowest_first (root, XFASTINT (root->total_lines) - delta);
4584
4585 /* Grow the mini-window. */
4586 w->top_line = make_number (XFASTINT (root->top_line) + XFASTINT (root->total_lines));
4587 w->total_lines = make_number (XFASTINT (w->total_lines) + delta);
4588 XSETFASTINT (w->last_modified, 0);
4589 XSETFASTINT (w->last_overlay_modified, 0);
4590
4591 adjust_glyphs (f);
4592 }
4593 }
4594
4595
4596 /* Shrink mini-window W. If there is recorded info about window sizes
4597 before a call to grow_mini_window, restore recorded window sizes.
4598 Otherwise, if the mini-window is higher than 1 line, resize it to 1
4599 line. */
4600
4601 void
4602 shrink_mini_window (struct window *w)
4603 {
4604 struct frame *f = XFRAME (w->frame);
4605 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
4606
4607 if (save_restore_orig_size (root, CHECK_ORIG_SIZES))
4608 {
4609 save_restore_orig_size (root, RESTORE_ORIG_SIZES);
4610 adjust_glyphs (f);
4611 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4612 windows_or_buffers_changed = 1;
4613 }
4614 else if (XFASTINT (w->total_lines) > 1)
4615 {
4616 /* Distribute the additional lines of the mini-window
4617 among the other windows. */
4618 Lisp_Object window;
4619 XSETWINDOW (window, w);
4620 enlarge_window (window, 1 - XFASTINT (w->total_lines), 0);
4621 }
4622 }
4623
4624
4625 \f
4626 /* Mark window cursors off for all windows in the window tree rooted
4627 at W by setting their phys_cursor_on_p flag to zero. Called from
4628 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4629 the frame are cleared. */
4630
4631 void
4632 mark_window_cursors_off (struct window *w)
4633 {
4634 while (w)
4635 {
4636 if (!NILP (w->hchild))
4637 mark_window_cursors_off (XWINDOW (w->hchild));
4638 else if (!NILP (w->vchild))
4639 mark_window_cursors_off (XWINDOW (w->vchild));
4640 else
4641 w->phys_cursor_on_p = 0;
4642
4643 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4644 }
4645 }
4646
4647
4648 /* Return number of lines of text (not counting mode lines) in W. */
4649
4650 int
4651 window_internal_height (struct window *w)
4652 {
4653 int ht = XFASTINT (w->total_lines);
4654
4655 if (!MINI_WINDOW_P (w))
4656 {
4657 if (!NILP (w->parent)
4658 || !NILP (w->vchild)
4659 || !NILP (w->hchild)
4660 || !NILP (w->next)
4661 || !NILP (w->prev)
4662 || WINDOW_WANTS_MODELINE_P (w))
4663 --ht;
4664
4665 if (WINDOW_WANTS_HEADER_LINE_P (w))
4666 --ht;
4667 }
4668
4669 return ht;
4670 }
4671
4672
4673 /* Return the number of columns in W.
4674 Don't count columns occupied by scroll bars or the vertical bar
4675 separating W from the sibling to its right. */
4676
4677 int
4678 window_box_text_cols (struct window *w)
4679 {
4680 struct frame *f = XFRAME (WINDOW_FRAME (w));
4681 int width = XINT (w->total_cols);
4682
4683 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
4684 /* Scroll bars occupy a few columns. */
4685 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
4686 else if (!FRAME_WINDOW_P (f)
4687 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
4688 /* The column of `|' characters separating side-by-side windows
4689 occupies one column only. */
4690 width -= 1;
4691
4692 if (FRAME_WINDOW_P (f))
4693 /* On window-systems, fringes and display margins cannot be
4694 used for normal text. */
4695 width -= (WINDOW_FRINGE_COLS (w)
4696 + WINDOW_LEFT_MARGIN_COLS (w)
4697 + WINDOW_RIGHT_MARGIN_COLS (w));
4698
4699 return width;
4700 }
4701
4702 \f
4703 /************************************************************************
4704 Window Scrolling
4705 ***********************************************************************/
4706
4707 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4708 N screen-fulls, which is defined as the height of the window minus
4709 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4710 instead. Negative values of N mean scroll down. NOERROR non-zero
4711 means don't signal an error if we try to move over BEGV or ZV,
4712 respectively. */
4713
4714 static void
4715 window_scroll (Lisp_Object window, int n, int whole, int noerror)
4716 {
4717 immediate_quit = 1;
4718
4719 /* If we must, use the pixel-based version which is much slower than
4720 the line-based one but can handle varying line heights. */
4721 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4722 window_scroll_pixel_based (window, n, whole, noerror);
4723 else
4724 window_scroll_line_based (window, n, whole, noerror);
4725
4726 immediate_quit = 0;
4727 }
4728
4729
4730 /* Implementation of window_scroll that works based on pixel line
4731 heights. See the comment of window_scroll for parameter
4732 descriptions. */
4733
4734 static void
4735 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4736 {
4737 struct it it;
4738 struct window *w = XWINDOW (window);
4739 struct text_pos start;
4740 int this_scroll_margin;
4741 /* True if we fiddled the window vscroll field without really scrolling. */
4742 int vscrolled = 0;
4743 int x, y, rtop, rbot, rowh, vpos;
4744
4745 SET_TEXT_POS_FROM_MARKER (start, w->start);
4746
4747 /* If PT is not visible in WINDOW, move back one half of
4748 the screen. Allow PT to be partially visible, otherwise
4749 something like (scroll-down 1) with PT in the line before
4750 the partially visible one would recenter. */
4751
4752 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4753 {
4754 /* Move backward half the height of the window. Performance note:
4755 vmotion used here is about 10% faster, but would give wrong
4756 results for variable height lines. */
4757 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4758 it.current_y = it.last_visible_y;
4759 move_it_vertically_backward (&it, window_box_height (w) / 2);
4760
4761 /* The function move_iterator_vertically may move over more than
4762 the specified y-distance. If it->w is small, e.g. a
4763 mini-buffer window, we may end up in front of the window's
4764 display area. This is the case when Start displaying at the
4765 start of the line containing PT in this case. */
4766 if (it.current_y <= 0)
4767 {
4768 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4769 move_it_vertically_backward (&it, 0);
4770 it.current_y = 0;
4771 }
4772
4773 start = it.current.pos;
4774 }
4775 else if (auto_window_vscroll_p)
4776 {
4777 if (rtop || rbot) /* partially visible */
4778 {
4779 int px;
4780 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4781 if (whole)
4782 dy = max ((window_box_height (w)
4783 - next_screen_context_lines * dy),
4784 dy);
4785 dy *= n;
4786
4787 if (n < 0)
4788 {
4789 /* Only vscroll backwards if already vscrolled forwards. */
4790 if (w->vscroll < 0 && rtop > 0)
4791 {
4792 px = max (0, -w->vscroll - min (rtop, -dy));
4793 Fset_window_vscroll (window, make_number (px), Qt);
4794 return;
4795 }
4796 }
4797 if (n > 0)
4798 {
4799 /* Do vscroll if already vscrolled or only display line. */
4800 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4801 {
4802 px = max (0, -w->vscroll + min (rbot, dy));
4803 Fset_window_vscroll (window, make_number (px), Qt);
4804 return;
4805 }
4806
4807 /* Maybe modify window start instead of scrolling. */
4808 if (rbot > 0 || w->vscroll < 0)
4809 {
4810 EMACS_INT spos;
4811
4812 Fset_window_vscroll (window, make_number (0), Qt);
4813 /* If there are other text lines above the current row,
4814 move window start to current row. Else to next row. */
4815 if (rbot > 0)
4816 spos = XINT (Fline_beginning_position (Qnil));
4817 else
4818 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4819 set_marker_restricted (w->start, make_number (spos),
4820 w->buffer);
4821 w->start_at_line_beg = Qt;
4822 w->update_mode_line = Qt;
4823 XSETFASTINT (w->last_modified, 0);
4824 XSETFASTINT (w->last_overlay_modified, 0);
4825 /* Set force_start so that redisplay_window will run the
4826 window-scroll-functions. */
4827 w->force_start = Qt;
4828 return;
4829 }
4830 }
4831 }
4832 /* Cancel previous vscroll. */
4833 Fset_window_vscroll (window, make_number (0), Qt);
4834 }
4835
4836 /* If scroll_preserve_screen_position is non-nil, we try to set
4837 point in the same window line as it is now, so get that line. */
4838 if (!NILP (Vscroll_preserve_screen_position))
4839 {
4840 /* We preserve the goal pixel coordinate across consecutive
4841 calls to scroll-up, scroll-down and other commands that
4842 have the `scroll-command' property. This avoids the
4843 possibility of point becoming "stuck" on a tall line when
4844 scrolling by one line. */
4845 if (window_scroll_pixel_based_preserve_y < 0
4846 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4847 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4848 {
4849 start_display (&it, w, start);
4850 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4851 window_scroll_pixel_based_preserve_y = it.current_y;
4852 window_scroll_pixel_based_preserve_x = it.current_x;
4853 }
4854 }
4855 else
4856 window_scroll_pixel_based_preserve_y
4857 = window_scroll_pixel_based_preserve_x = -1;
4858
4859 /* Move iterator it from start the specified distance forward or
4860 backward. The result is the new window start. */
4861 start_display (&it, w, start);
4862 if (whole)
4863 {
4864 EMACS_INT start_pos = IT_CHARPOS (it);
4865 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4866 dy = max ((window_box_height (w)
4867 - next_screen_context_lines * dy),
4868 dy) * n;
4869
4870 /* Note that move_it_vertically always moves the iterator to the
4871 start of a line. So, if the last line doesn't have a newline,
4872 we would end up at the start of the line ending at ZV. */
4873 if (dy <= 0)
4874 {
4875 move_it_vertically_backward (&it, -dy);
4876 /* Ensure we actually do move, e.g. in case we are currently
4877 looking at an image that is taller that the window height. */
4878 while (start_pos == IT_CHARPOS (it)
4879 && start_pos > BEGV)
4880 move_it_by_lines (&it, -1, 1);
4881 }
4882 else if (dy > 0)
4883 {
4884 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4885 MOVE_TO_POS | MOVE_TO_Y);
4886 /* Ensure we actually do move, e.g. in case we are currently
4887 looking at an image that is taller that the window height. */
4888 while (start_pos == IT_CHARPOS (it)
4889 && start_pos < ZV)
4890 move_it_by_lines (&it, 1, 1);
4891 }
4892 }
4893 else
4894 move_it_by_lines (&it, n, 1);
4895
4896 /* We failed if we find ZV is already on the screen (scrolling up,
4897 means there's nothing past the end), or if we can't start any
4898 earlier (scrolling down, means there's nothing past the top). */
4899 if ((n > 0 && IT_CHARPOS (it) == ZV)
4900 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4901 {
4902 if (IT_CHARPOS (it) == ZV)
4903 {
4904 if (it.current_y < it.last_visible_y
4905 && (it.current_y + it.max_ascent + it.max_descent
4906 > it.last_visible_y))
4907 {
4908 /* The last line was only partially visible, make it fully
4909 visible. */
4910 w->vscroll = (it.last_visible_y
4911 - it.current_y + it.max_ascent + it.max_descent);
4912 adjust_glyphs (it.f);
4913 }
4914 else if (noerror)
4915 return;
4916 else if (n < 0) /* could happen with empty buffers */
4917 xsignal0 (Qbeginning_of_buffer);
4918 else
4919 xsignal0 (Qend_of_buffer);
4920 }
4921 else
4922 {
4923 if (w->vscroll != 0)
4924 /* The first line was only partially visible, make it fully
4925 visible. */
4926 w->vscroll = 0;
4927 else if (noerror)
4928 return;
4929 else
4930 xsignal0 (Qbeginning_of_buffer);
4931 }
4932
4933 /* If control gets here, then we vscrolled. */
4934
4935 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4936
4937 /* Don't try to change the window start below. */
4938 vscrolled = 1;
4939 }
4940
4941 if (! vscrolled)
4942 {
4943 EMACS_INT pos = IT_CHARPOS (it);
4944 EMACS_INT bytepos;
4945
4946 /* If in the middle of a multi-glyph character move forward to
4947 the next character. */
4948 if (in_display_vector_p (&it))
4949 {
4950 ++pos;
4951 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4952 }
4953
4954 /* Set the window start, and set up the window for redisplay. */
4955 set_marker_restricted (w->start, make_number (pos),
4956 w->buffer);
4957 bytepos = XMARKER (w->start)->bytepos;
4958 w->start_at_line_beg = ((pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n')
4959 ? Qt : Qnil);
4960 w->update_mode_line = Qt;
4961 XSETFASTINT (w->last_modified, 0);
4962 XSETFASTINT (w->last_overlay_modified, 0);
4963 /* Set force_start so that redisplay_window will run the
4964 window-scroll-functions. */
4965 w->force_start = Qt;
4966 }
4967
4968 /* The rest of this function uses current_y in a nonstandard way,
4969 not including the height of the header line if any. */
4970 it.current_y = it.vpos = 0;
4971
4972 /* Move PT out of scroll margins.
4973 This code wants current_y to be zero at the window start position
4974 even if there is a header line. */
4975 this_scroll_margin = max (0, scroll_margin);
4976 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4977 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4978
4979 if (n > 0)
4980 {
4981 /* We moved the window start towards ZV, so PT may be now
4982 in the scroll margin at the top. */
4983 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4984 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4985 && (NILP (Vscroll_preserve_screen_position)
4986 || EQ (Vscroll_preserve_screen_position, Qt)))
4987 /* We found PT at a legitimate height. Leave it alone. */
4988 ;
4989 else if (window_scroll_pixel_based_preserve_y >= 0)
4990 {
4991 /* If we have a header line, take account of it.
4992 This is necessary because we set it.current_y to 0, above. */
4993 move_it_to (&it, -1,
4994 window_scroll_pixel_based_preserve_x,
4995 window_scroll_pixel_based_preserve_y
4996 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4997 -1, MOVE_TO_Y | MOVE_TO_X);
4998 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4999 }
5000 else
5001 {
5002 while (it.current_y < this_scroll_margin)
5003 {
5004 int prev = it.current_y;
5005 move_it_by_lines (&it, 1, 1);
5006 if (prev == it.current_y)
5007 break;
5008 }
5009 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5010 }
5011 }
5012 else if (n < 0)
5013 {
5014 EMACS_INT charpos, bytepos;
5015 int partial_p;
5016
5017 /* Save our position, for the
5018 window_scroll_pixel_based_preserve_y case. */
5019 charpos = IT_CHARPOS (it);
5020 bytepos = IT_BYTEPOS (it);
5021
5022 /* We moved the window start towards BEGV, so PT may be now
5023 in the scroll margin at the bottom. */
5024 move_it_to (&it, PT, -1,
5025 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
5026 - this_scroll_margin - 1),
5027 -1,
5028 MOVE_TO_POS | MOVE_TO_Y);
5029
5030 /* Save our position, in case it's correct. */
5031 charpos = IT_CHARPOS (it);
5032 bytepos = IT_BYTEPOS (it);
5033
5034 /* See if point is on a partially visible line at the end. */
5035 if (it.what == IT_EOB)
5036 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
5037 else
5038 {
5039 move_it_by_lines (&it, 1, 1);
5040 partial_p = it.current_y > it.last_visible_y;
5041 }
5042
5043 if (charpos == PT && !partial_p
5044 && (NILP (Vscroll_preserve_screen_position)
5045 || EQ (Vscroll_preserve_screen_position, Qt)))
5046 /* We found PT before we found the display margin, so PT is ok. */
5047 ;
5048 else if (window_scroll_pixel_based_preserve_y >= 0)
5049 {
5050 SET_TEXT_POS_FROM_MARKER (start, w->start);
5051 start_display (&it, w, start);
5052 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
5053 here because we called start_display again and did not
5054 alter it.current_y this time. */
5055 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
5056 window_scroll_pixel_based_preserve_y, -1,
5057 MOVE_TO_Y | MOVE_TO_X);
5058 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5059 }
5060 else
5061 {
5062 if (partial_p)
5063 /* The last line was only partially visible, so back up two
5064 lines to make sure we're on a fully visible line. */
5065 {
5066 move_it_by_lines (&it, -2, 0);
5067 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
5068 }
5069 else
5070 /* No, the position we saved is OK, so use it. */
5071 SET_PT_BOTH (charpos, bytepos);
5072 }
5073 }
5074 }
5075
5076
5077 /* Implementation of window_scroll that works based on screen lines.
5078 See the comment of window_scroll for parameter descriptions. */
5079
5080 static void
5081 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
5082 {
5083 register struct window *w = XWINDOW (window);
5084 register EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
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;
5092
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);
5097
5098 startpos = marker_position (w->start);
5099
5100 if (!NILP (Vscroll_preserve_screen_position))
5101 {
5102 if (window_scroll_preserve_vpos <= 0
5103 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
5104 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
5105 {
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);
5113 }
5114
5115 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
5116 make_number (window_scroll_preserve_vpos));
5117 }
5118
5119 XSETFASTINT (tem, PT);
5120 tem = Fpos_visible_in_window_p (tem, window, Qnil);
5121
5122 if (NILP (tem))
5123 {
5124 Fvertical_motion (make_number (- (ht / 2)), window);
5125 startpos = PT;
5126 }
5127
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 (opoint, opoint_byte);
5135
5136 if (lose)
5137 {
5138 if (noerror)
5139 return;
5140 else
5141 xsignal0 (Qbeginning_of_buffer);
5142 }
5143
5144 if (pos < ZV)
5145 {
5146 int this_scroll_margin = scroll_margin;
5147
5148 /* Don't use a scroll margin that is negative or too large. */
5149 if (this_scroll_margin < 0)
5150 this_scroll_margin = 0;
5151
5152 if (XINT (w->total_lines) < 4 * scroll_margin)
5153 this_scroll_margin = XINT (w->total_lines) / 4;
5154
5155 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
5156 w->start_at_line_beg = bolp;
5157 w->update_mode_line = Qt;
5158 XSETFASTINT (w->last_modified, 0);
5159 XSETFASTINT (w->last_overlay_modified, 0);
5160 /* Set force_start so that redisplay_window will run
5161 the window-scroll-functions. */
5162 w->force_start = Qt;
5163
5164 if (!NILP (Vscroll_preserve_screen_position)
5165 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
5166 {
5167 SET_PT_BOTH (pos, pos_byte);
5168 Fvertical_motion (original_pos, window);
5169 }
5170 /* If we scrolled forward, put point enough lines down
5171 that it is outside the scroll margin. */
5172 else if (n > 0)
5173 {
5174 int top_margin;
5175
5176 if (this_scroll_margin > 0)
5177 {
5178 SET_PT_BOTH (pos, pos_byte);
5179 Fvertical_motion (make_number (this_scroll_margin), window);
5180 top_margin = PT;
5181 }
5182 else
5183 top_margin = pos;
5184
5185 if (top_margin <= opoint)
5186 SET_PT_BOTH (opoint, opoint_byte);
5187 else if (!NILP (Vscroll_preserve_screen_position))
5188 {
5189 SET_PT_BOTH (pos, pos_byte);
5190 Fvertical_motion (original_pos, window);
5191 }
5192 else
5193 SET_PT (top_margin);
5194 }
5195 else if (n < 0)
5196 {
5197 int bottom_margin;
5198
5199 /* If we scrolled backward, put point near the end of the window
5200 but not within the scroll margin. */
5201 SET_PT_BOTH (pos, pos_byte);
5202 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
5203 if (XFASTINT (tem) == ht - this_scroll_margin)
5204 bottom_margin = PT;
5205 else
5206 bottom_margin = PT + 1;
5207
5208 if (bottom_margin > opoint)
5209 SET_PT_BOTH (opoint, opoint_byte);
5210 else
5211 {
5212 if (!NILP (Vscroll_preserve_screen_position))
5213 {
5214 SET_PT_BOTH (pos, pos_byte);
5215 Fvertical_motion (original_pos, window);
5216 }
5217 else
5218 Fvertical_motion (make_number (-1), window);
5219 }
5220 }
5221 }
5222 else
5223 {
5224 if (noerror)
5225 return;
5226 else
5227 xsignal0 (Qend_of_buffer);
5228 }
5229 }
5230
5231
5232 /* Scroll selected_window up or down. If N is nil, scroll a
5233 screen-full which is defined as the height of the window minus
5234 next_screen_context_lines. If N is the symbol `-', scroll.
5235 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
5236 up. This is the guts of Fscroll_up and Fscroll_down. */
5237
5238 static void
5239 scroll_command (Lisp_Object n, int direction)
5240 {
5241 int count = SPECPDL_INDEX ();
5242
5243 xassert (eabs (direction) == 1);
5244
5245 /* If selected window's buffer isn't current, make it current for
5246 the moment. But don't screw up if window_scroll gets an error. */
5247 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
5248 {
5249 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5250 Fset_buffer (XWINDOW (selected_window)->buffer);
5251
5252 /* Make redisplay consider other windows than just selected_window. */
5253 ++windows_or_buffers_changed;
5254 }
5255
5256 if (NILP (n))
5257 window_scroll (selected_window, direction, 1, 0);
5258 else if (EQ (n, Qminus))
5259 window_scroll (selected_window, -direction, 1, 0);
5260 else
5261 {
5262 n = Fprefix_numeric_value (n);
5263 window_scroll (selected_window, XINT (n) * direction, 0, 0);
5264 }
5265
5266 unbind_to (count, Qnil);
5267 }
5268
5269 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
5270 doc: /* Scroll text of selected window upward ARG lines.
5271 If ARG is omitted or nil, scroll upward by a near full screen.
5272 A near full screen is `next-screen-context-lines' less than a full screen.
5273 Negative ARG means scroll downward.
5274 If ARG is the atom `-', scroll downward by nearly full screen.
5275 When calling from a program, supply as argument a number, nil, or `-'. */)
5276 (Lisp_Object arg)
5277 {
5278 scroll_command (arg, 1);
5279 return Qnil;
5280 }
5281
5282 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
5283 doc: /* Scroll text of selected window down ARG lines.
5284 If ARG is omitted or nil, scroll down by a near full screen.
5285 A near full screen is `next-screen-context-lines' less than a full screen.
5286 Negative ARG means scroll upward.
5287 If ARG is the atom `-', scroll upward by nearly full screen.
5288 When calling from a program, supply as argument a number, nil, or `-'. */)
5289 (Lisp_Object arg)
5290 {
5291 scroll_command (arg, -1);
5292 return Qnil;
5293 }
5294 \f
5295 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
5296 doc: /* Return the other window for \"other window scroll\" commands.
5297 If `other-window-scroll-buffer' is non-nil, a window
5298 showing that buffer is used.
5299 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5300 specifies the window. This takes precedence over
5301 `other-window-scroll-buffer'. */)
5302 (void)
5303 {
5304 Lisp_Object window;
5305
5306 if (MINI_WINDOW_P (XWINDOW (selected_window))
5307 && !NILP (Vminibuf_scroll_window))
5308 window = Vminibuf_scroll_window;
5309 /* If buffer is specified, scroll that buffer. */
5310 else if (!NILP (Vother_window_scroll_buffer))
5311 {
5312 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
5313 if (NILP (window))
5314 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
5315 }
5316 else
5317 {
5318 /* Nothing specified; look for a neighboring window on the same
5319 frame. */
5320 window = Fnext_window (selected_window, Qnil, Qnil);
5321
5322 if (EQ (window, selected_window))
5323 /* That didn't get us anywhere; look for a window on another
5324 visible frame. */
5325 do
5326 window = Fnext_window (window, Qnil, Qt);
5327 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
5328 && ! EQ (window, selected_window));
5329 }
5330
5331 CHECK_LIVE_WINDOW (window);
5332
5333 if (EQ (window, selected_window))
5334 error ("There is no other window");
5335
5336 return window;
5337 }
5338
5339 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
5340 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
5341 A near full screen is `next-screen-context-lines' less than a full screen.
5342 The next window is the one below the current one; or the one at the top
5343 if the current one is at the bottom. Negative ARG means scroll downward.
5344 If ARG is the atom `-', scroll downward by nearly full screen.
5345 When calling from a program, supply as argument a number, nil, or `-'.
5346
5347 If `other-window-scroll-buffer' is non-nil, scroll the window
5348 showing that buffer, popping the buffer up if necessary.
5349 If in the minibuffer, `minibuffer-scroll-window' if non-nil
5350 specifies the window to scroll. This takes precedence over
5351 `other-window-scroll-buffer'. */)
5352 (Lisp_Object arg)
5353 {
5354 Lisp_Object window;
5355 struct window *w;
5356 int count = SPECPDL_INDEX ();
5357
5358 window = Fother_window_for_scrolling ();
5359 w = XWINDOW (window);
5360
5361 /* Don't screw up if window_scroll gets an error. */
5362 record_unwind_protect (save_excursion_restore, save_excursion_save ());
5363 ++windows_or_buffers_changed;
5364
5365 Fset_buffer (w->buffer);
5366 SET_PT (marker_position (w->pointm));
5367
5368 if (NILP (arg))
5369 window_scroll (window, 1, 1, 1);
5370 else if (EQ (arg, Qminus))
5371 window_scroll (window, -1, 1, 1);
5372 else
5373 {
5374 if (CONSP (arg))
5375 arg = Fcar (arg);
5376 CHECK_NUMBER (arg);
5377 window_scroll (window, XINT (arg), 0, 1);
5378 }
5379
5380 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
5381 unbind_to (count, Qnil);
5382
5383 return Qnil;
5384 }
5385 \f
5386 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
5387 doc: /* Scroll selected window display ARG columns left.
5388 Default for ARG is window width minus 2.
5389 Value is the total amount of leftward horizontal scrolling in
5390 effect after the change.
5391 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5392 lower bound for automatic scrolling, i.e. automatic scrolling
5393 will not scroll a window to a column less than the value returned
5394 by this function. This happens in an interactive call. */)
5395 (register Lisp_Object arg, Lisp_Object set_minimum)
5396 {
5397 Lisp_Object result;
5398 int hscroll;
5399 struct window *w = XWINDOW (selected_window);
5400
5401 if (NILP (arg))
5402 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5403 else
5404 arg = Fprefix_numeric_value (arg);
5405
5406 hscroll = XINT (w->hscroll) + XINT (arg);
5407 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5408
5409 if (!NILP (set_minimum))
5410 w->min_hscroll = w->hscroll;
5411
5412 return result;
5413 }
5414
5415 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
5416 doc: /* Scroll selected window display ARG columns right.
5417 Default for ARG is window width minus 2.
5418 Value is the total amount of leftward horizontal scrolling in
5419 effect after the change.
5420 If SET-MINIMUM is non-nil, the new scroll amount becomes the
5421 lower bound for automatic scrolling, i.e. automatic scrolling
5422 will not scroll a window to a column less than the value returned
5423 by this function. This happens in an interactive call. */)
5424 (register Lisp_Object arg, Lisp_Object set_minimum)
5425 {
5426 Lisp_Object result;
5427 int hscroll;
5428 struct window *w = XWINDOW (selected_window);
5429
5430 if (NILP (arg))
5431 XSETFASTINT (arg, window_box_text_cols (w) - 2);
5432 else
5433 arg = Fprefix_numeric_value (arg);
5434
5435 hscroll = XINT (w->hscroll) - XINT (arg);
5436 result = Fset_window_hscroll (selected_window, make_number (hscroll));
5437
5438 if (!NILP (set_minimum))
5439 w->min_hscroll = w->hscroll;
5440
5441 return result;
5442 }
5443
5444 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
5445 doc: /* Return the window which was selected when entering the minibuffer.
5446 Returns nil, if selected window is not a minibuffer window. */)
5447 (void)
5448 {
5449 if (minibuf_level > 0
5450 && MINI_WINDOW_P (XWINDOW (selected_window))
5451 && WINDOW_LIVE_P (minibuf_selected_window))
5452 return minibuf_selected_window;
5453
5454 return Qnil;
5455 }
5456
5457 /* Value is the number of lines actually displayed in window W,
5458 as opposed to its height. */
5459
5460 static int
5461 displayed_window_lines (struct window *w)
5462 {
5463 struct it it;
5464 struct text_pos start;
5465 int height = window_box_height (w);
5466 struct buffer *old_buffer;
5467 int bottom_y;
5468
5469 if (XBUFFER (w->buffer) != current_buffer)
5470 {
5471 old_buffer = current_buffer;
5472 set_buffer_internal (XBUFFER (w->buffer));
5473 }
5474 else
5475 old_buffer = NULL;
5476
5477 /* In case W->start is out of the accessible range, do something
5478 reasonable. This happens in Info mode when Info-scroll-down
5479 calls (recenter -1) while W->start is 1. */
5480 if (XMARKER (w->start)->charpos < BEGV)
5481 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
5482 else if (XMARKER (w->start)->charpos > ZV)
5483 SET_TEXT_POS (start, ZV, ZV_BYTE);
5484 else
5485 SET_TEXT_POS_FROM_MARKER (start, w->start);
5486
5487 start_display (&it, w, start);
5488 move_it_vertically (&it, height);
5489 bottom_y = line_bottom_y (&it);
5490
5491 /* rms: On a non-window display,
5492 the value of it.vpos at the bottom of the screen
5493 seems to be 1 larger than window_box_height (w).
5494 This kludge fixes a bug whereby (move-to-window-line -1)
5495 when ZV is on the last screen line
5496 moves to the previous screen line instead of the last one. */
5497 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5498 height++;
5499
5500 /* Add in empty lines at the bottom of the window. */
5501 if (bottom_y < height)
5502 {
5503 int uy = FRAME_LINE_HEIGHT (it.f);
5504 it.vpos += (height - bottom_y + uy - 1) / uy;
5505 }
5506
5507 if (old_buffer)
5508 set_buffer_internal (old_buffer);
5509
5510 return it.vpos;
5511 }
5512
5513
5514 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5515 doc: /* Center point in selected window and maybe redisplay frame.
5516 With prefix argument ARG, recenter putting point on screen line ARG
5517 relative to the selected window. If ARG is negative, it counts up from the
5518 bottom of the window. (ARG should be less than the height of the window.)
5519
5520 If ARG is omitted or nil, then recenter with point on the middle line of
5521 the selected window; if the variable `recenter-redisplay' is non-nil,
5522 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5523 is set to `grow-only', this resets the tool-bar's height to the minimum
5524 height needed); if `recenter-redisplay' has the special value `tty',
5525 then only tty frame are redrawn.
5526
5527 Just C-u as prefix means put point in the center of the window
5528 and redisplay normally--don't erase and redraw the frame. */)
5529 (register Lisp_Object arg)
5530 {
5531 struct window *w = XWINDOW (selected_window);
5532 struct buffer *buf = XBUFFER (w->buffer);
5533 struct buffer *obuf = current_buffer;
5534 int center_p = 0;
5535 EMACS_INT charpos, bytepos;
5536 int iarg;
5537 int this_scroll_margin;
5538
5539 /* If redisplay is suppressed due to an error, try again. */
5540 obuf->display_error_modiff = 0;
5541
5542 if (NILP (arg))
5543 {
5544 if (!NILP (Vrecenter_redisplay)
5545 && (!EQ (Vrecenter_redisplay, Qtty)
5546 || !NILP (Ftty_type (selected_frame))))
5547 {
5548 int i;
5549
5550 /* Invalidate pixel data calculated for all compositions. */
5551 for (i = 0; i < n_compositions; i++)
5552 composition_table[i]->font = NULL;
5553
5554 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5555
5556 Fredraw_frame (WINDOW_FRAME (w));
5557 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5558 }
5559
5560 center_p = 1;
5561 }
5562 else if (CONSP (arg)) /* Just C-u. */
5563 center_p = 1;
5564 else
5565 {
5566 arg = Fprefix_numeric_value (arg);
5567 CHECK_NUMBER (arg);
5568 iarg = XINT (arg);
5569 }
5570
5571 set_buffer_internal (buf);
5572
5573 /* Do this after making BUF current
5574 in case scroll_margin is buffer-local. */
5575 this_scroll_margin = max (0, scroll_margin);
5576 this_scroll_margin = min (this_scroll_margin,
5577 XFASTINT (w->total_lines) / 4);
5578
5579 /* Handle centering on a graphical frame specially. Such frames can
5580 have variable-height lines and centering point on the basis of
5581 line counts would lead to strange effects. */
5582 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5583 {
5584 if (center_p)
5585 {
5586 struct it it;
5587 struct text_pos pt;
5588
5589 SET_TEXT_POS (pt, PT, PT_BYTE);
5590 start_display (&it, w, pt);
5591 move_it_vertically_backward (&it, window_box_height (w) / 2);
5592 charpos = IT_CHARPOS (it);
5593 bytepos = IT_BYTEPOS (it);
5594 }
5595 else if (iarg < 0)
5596 {
5597 struct it it;
5598 struct text_pos pt;
5599 int nlines = -iarg;
5600 int extra_line_spacing;
5601 int h = window_box_height (w);
5602
5603 iarg = - max (-iarg, this_scroll_margin);
5604
5605 SET_TEXT_POS (pt, PT, PT_BYTE);
5606 start_display (&it, w, pt);
5607
5608 /* Be sure we have the exact height of the full line containing PT. */
5609 move_it_by_lines (&it, 0, 1);
5610
5611 /* The amount of pixels we have to move back is the window
5612 height minus what's displayed in the line containing PT,
5613 and the lines below. */
5614 it.current_y = 0;
5615 it.vpos = 0;
5616 move_it_by_lines (&it, nlines, 1);
5617
5618 if (it.vpos == nlines)
5619 h -= it.current_y;
5620 else
5621 {
5622 /* Last line has no newline */
5623 h -= line_bottom_y (&it);
5624 it.vpos++;
5625 }
5626
5627 /* Don't reserve space for extra line spacing of last line. */
5628 extra_line_spacing = it.max_extra_line_spacing;
5629
5630 /* If we can't move down NLINES lines because we hit
5631 the end of the buffer, count in some empty lines. */
5632 if (it.vpos < nlines)
5633 {
5634 nlines -= it.vpos;
5635 extra_line_spacing = it.extra_line_spacing;
5636 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5637 }
5638 if (h <= 0)
5639 return Qnil;
5640
5641 /* Now find the new top line (starting position) of the window. */
5642 start_display (&it, w, pt);
5643 it.current_y = 0;
5644 move_it_vertically_backward (&it, h);
5645
5646 /* If extra line spacing is present, we may move too far
5647 back. This causes the last line to be only partially
5648 visible (which triggers redisplay to recenter that line
5649 in the middle), so move forward.
5650 But ignore extra line spacing on last line, as it is not
5651 considered to be part of the visible height of the line.
5652 */
5653 h += extra_line_spacing;
5654 while (-it.current_y > h)
5655 move_it_by_lines (&it, 1, 1);
5656
5657 charpos = IT_CHARPOS (it);
5658 bytepos = IT_BYTEPOS (it);
5659 }
5660 else
5661 {
5662 struct position pos;
5663
5664 iarg = max (iarg, this_scroll_margin);
5665
5666 pos = *vmotion (PT, -iarg, w);
5667 charpos = pos.bufpos;
5668 bytepos = pos.bytepos;
5669 }
5670 }
5671 else
5672 {
5673 struct position pos;
5674 int ht = window_internal_height (w);
5675
5676 if (center_p)
5677 iarg = ht / 2;
5678 else if (iarg < 0)
5679 iarg += ht;
5680
5681 /* Don't let it get into the margin at either top or bottom. */
5682 iarg = max (iarg, this_scroll_margin);
5683 iarg = min (iarg, ht - this_scroll_margin - 1);
5684
5685 pos = *vmotion (PT, - iarg, w);
5686 charpos = pos.bufpos;
5687 bytepos = pos.bytepos;
5688 }
5689
5690 /* Set the new window start. */
5691 set_marker_both (w->start, w->buffer, charpos, bytepos);
5692 w->window_end_valid = Qnil;
5693
5694 w->optional_new_start = Qt;
5695
5696 if (bytepos == BEGV_BYTE || FETCH_BYTE (bytepos - 1) == '\n')
5697 w->start_at_line_beg = Qt;
5698 else
5699 w->start_at_line_beg = Qnil;
5700
5701 set_buffer_internal (obuf);
5702 return Qnil;
5703 }
5704
5705
5706 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5707 0, 1, 0,
5708 doc: /* Return the height in lines of the text display area of WINDOW.
5709 WINDOW defaults to the selected window.
5710
5711 The return value does not include the mode line, any header line, nor
5712 any partial-height lines in the text display area. */)
5713 (Lisp_Object window)
5714 {
5715 struct window *w = decode_window (window);
5716 int pixel_height = window_box_height (w);
5717 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5718 return make_number (line_height);
5719 }
5720
5721
5722 \f
5723 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5724 1, 1, "P",
5725 doc: /* Position point relative to window.
5726 With no argument, position point at center of window.
5727 An argument specifies vertical position within the window;
5728 zero means top of window, negative means relative to bottom of window. */)
5729 (Lisp_Object arg)
5730 {
5731 struct window *w = XWINDOW (selected_window);
5732 int lines, start;
5733 Lisp_Object window;
5734 #if 0
5735 int this_scroll_margin;
5736 #endif
5737
5738 if (!(BUFFERP (w->buffer)
5739 && XBUFFER (w->buffer) == current_buffer))
5740 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5741 when passed below to set_marker_both. */
5742 error ("move-to-window-line called from unrelated buffer");
5743
5744 window = selected_window;
5745 start = marker_position (w->start);
5746 if (start < BEGV || start > ZV)
5747 {
5748 int height = window_internal_height (w);
5749 Fvertical_motion (make_number (- (height / 2)), window);
5750 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5751 w->start_at_line_beg = Fbolp ();
5752 w->force_start = Qt;
5753 }
5754 else
5755 Fgoto_char (w->start);
5756
5757 lines = displayed_window_lines (w);
5758
5759 #if 0
5760 this_scroll_margin = max (0, scroll_margin);
5761 this_scroll_margin = min (this_scroll_margin, lines / 4);
5762 #endif
5763
5764 if (NILP (arg))
5765 XSETFASTINT (arg, lines / 2);
5766 else
5767 {
5768 int iarg = XINT (Fprefix_numeric_value (arg));
5769
5770 if (iarg < 0)
5771 iarg = iarg + lines;
5772
5773 #if 0 /* This code would prevent move-to-window-line from moving point
5774 to a place inside the scroll margins (which would cause the
5775 next redisplay to scroll). I wrote this code, but then concluded
5776 it is probably better not to install it. However, it is here
5777 inside #if 0 so as not to lose it. -- rms. */
5778
5779 /* Don't let it get into the margin at either top or bottom. */
5780 iarg = max (iarg, this_scroll_margin);
5781 iarg = min (iarg, lines - this_scroll_margin - 1);
5782 #endif
5783
5784 arg = make_number (iarg);
5785 }
5786
5787 /* Skip past a partially visible first line. */
5788 if (w->vscroll)
5789 XSETINT (arg, XINT (arg) + 1);
5790
5791 return Fvertical_motion (arg, window);
5792 }
5793
5794
5795 \f
5796 /***********************************************************************
5797 Window Configuration
5798 ***********************************************************************/
5799
5800 struct save_window_data
5801 {
5802 EMACS_UINT size;
5803 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5804 Lisp_Object selected_frame;
5805 Lisp_Object current_window;
5806 Lisp_Object current_buffer;
5807 Lisp_Object minibuf_scroll_window;
5808 Lisp_Object minibuf_selected_window;
5809 Lisp_Object root_window;
5810 Lisp_Object focus_frame;
5811 /* A vector, each of whose elements is a struct saved_window
5812 for one window. */
5813 Lisp_Object saved_windows;
5814
5815 /* All fields above are traced by the GC.
5816 From `fame-cols' down, the fields are ignored by the GC. */
5817
5818 int frame_cols, frame_lines, frame_menu_bar_lines;
5819 int frame_tool_bar_lines;
5820 };
5821
5822 /* This is saved as a Lisp_Vector */
5823 struct saved_window
5824 {
5825 /* these first two must agree with struct Lisp_Vector in lisp.h */
5826 EMACS_UINT size;
5827 struct Lisp_Vector *next_from_Lisp_Vector_struct;
5828
5829 Lisp_Object window;
5830 Lisp_Object buffer, start, pointm, mark;
5831 Lisp_Object left_col, top_line, total_cols, total_lines;
5832 Lisp_Object hscroll, min_hscroll;
5833 Lisp_Object parent, prev;
5834 Lisp_Object start_at_line_beg;
5835 Lisp_Object display_table;
5836 Lisp_Object orig_top_line, orig_total_lines;
5837 Lisp_Object left_margin_cols, right_margin_cols;
5838 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5839 Lisp_Object scroll_bar_width, vertical_scroll_bar_type;
5840 Lisp_Object dedicated, resize_proportionally;
5841 };
5842
5843 #define SAVED_WINDOW_N(swv,n) \
5844 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5845
5846 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5847 doc: /* Return t if OBJECT is a window-configuration object. */)
5848 (Lisp_Object object)
5849 {
5850 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5851 }
5852
5853 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5854 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5855 (Lisp_Object config)
5856 {
5857 register struct save_window_data *data;
5858 struct Lisp_Vector *saved_windows;
5859
5860 CHECK_WINDOW_CONFIGURATION (config);
5861
5862 data = (struct save_window_data *) XVECTOR (config);
5863 saved_windows = XVECTOR (data->saved_windows);
5864 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5865 }
5866
5867 DEFUN ("set-window-configuration", Fset_window_configuration,
5868 Sset_window_configuration, 1, 1, 0,
5869 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5870 CONFIGURATION must be a value previously returned
5871 by `current-window-configuration' (which see).
5872 If CONFIGURATION was made from a frame that is now deleted,
5873 only frame-independent values can be restored. In this case,
5874 the return value is nil. Otherwise the value is t. */)
5875 (Lisp_Object configuration)
5876 {
5877 register struct save_window_data *data;
5878 struct Lisp_Vector *saved_windows;
5879 Lisp_Object new_current_buffer;
5880 Lisp_Object frame;
5881 FRAME_PTR f;
5882 EMACS_INT old_point = -1;
5883
5884 CHECK_WINDOW_CONFIGURATION (configuration);
5885
5886 data = (struct save_window_data *) XVECTOR (configuration);
5887 saved_windows = XVECTOR (data->saved_windows);
5888
5889 new_current_buffer = data->current_buffer;
5890 if (NILP (BVAR (XBUFFER (new_current_buffer), name)))
5891 new_current_buffer = Qnil;
5892 else
5893 {
5894 if (XBUFFER (new_current_buffer) == current_buffer)
5895 /* The code further down "preserves point" by saving here PT in
5896 old_point and then setting it later back into PT. When the
5897 current-selected-window and the final-selected-window both show
5898 the current buffer, this suffers from the problem that the
5899 current PT is the window-point of the current-selected-window,
5900 while the final PT is the point of the final-selected-window, so
5901 this copy from one PT to the other would end up moving the
5902 window-point of the final-selected-window to the window-point of
5903 the current-selected-window. So we have to be careful which
5904 point of the current-buffer we copy into old_point. */
5905 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5906 && WINDOWP (selected_window)
5907 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5908 && !EQ (selected_window, data->current_window))
5909 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5910 else
5911 old_point = PT;
5912 else
5913 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5914 point in new_current_buffer as of the last time this buffer was
5915 used. This can be non-deterministic since it can be changed by
5916 things like jit-lock by mere temporary selection of some random
5917 window that happens to show this buffer.
5918 So if possible we want this arbitrary choice of "which point" to
5919 be the one from the to-be-selected-window so as to prevent this
5920 window's cursor from being copied from another window. */
5921 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5922 /* If current_window = selected_window, its point is in BUF_PT. */
5923 && !EQ (selected_window, data->current_window))
5924 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5925 else
5926 old_point = BUF_PT (XBUFFER (new_current_buffer));
5927 }
5928
5929 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5930 f = XFRAME (frame);
5931
5932 /* If f is a dead frame, don't bother rebuilding its window tree.
5933 However, there is other stuff we should still try to do below. */
5934 if (FRAME_LIVE_P (f))
5935 {
5936 register struct window *w;
5937 register struct saved_window *p;
5938 struct window *root_window;
5939 struct window **leaf_windows;
5940 int n_leaf_windows;
5941 int k, i, n;
5942
5943 /* If the frame has been resized since this window configuration was
5944 made, we change the frame to the size specified in the
5945 configuration, restore the configuration, and then resize it
5946 back. We keep track of the prevailing height in these variables. */
5947 int previous_frame_lines = FRAME_LINES (f);
5948 int previous_frame_cols = FRAME_COLS (f);
5949 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5950 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5951
5952 /* The mouse highlighting code could get screwed up
5953 if it runs during this. */
5954 BLOCK_INPUT;
5955
5956 if (data->frame_lines != previous_frame_lines
5957 || data->frame_cols != previous_frame_cols)
5958 change_frame_size (f, data->frame_lines,
5959 data->frame_cols, 0, 0, 0);
5960 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5961 if (data->frame_menu_bar_lines
5962 != previous_frame_menu_bar_lines)
5963 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5964 make_number (0));
5965 #ifdef HAVE_WINDOW_SYSTEM
5966 if (data->frame_tool_bar_lines
5967 != previous_frame_tool_bar_lines)
5968 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5969 make_number (0));
5970 #endif
5971 #endif
5972
5973 /* "Swap out" point from the selected window's buffer
5974 into the window itself. (Normally the pointm of the selected
5975 window holds garbage.) We do this now, before
5976 restoring the window contents, and prevent it from
5977 being done later on when we select a new window. */
5978 if (! NILP (XWINDOW (selected_window)->buffer))
5979 {
5980 w = XWINDOW (selected_window);
5981 set_marker_both (w->pointm,
5982 w->buffer,
5983 BUF_PT (XBUFFER (w->buffer)),
5984 BUF_PT_BYTE (XBUFFER (w->buffer)));
5985 }
5986
5987 windows_or_buffers_changed++;
5988 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5989
5990 /* Problem: Freeing all matrices and later allocating them again
5991 is a serious redisplay flickering problem. What we would
5992 really like to do is to free only those matrices not reused
5993 below. */
5994 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5995 leaf_windows
5996 = (struct window **) alloca (count_windows (root_window)
5997 * sizeof (struct window *));
5998 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5999
6000 /* Kludge Alert!
6001 Mark all windows now on frame as "deleted".
6002 Restoring the new configuration "undeletes" any that are in it.
6003
6004 Save their current buffers in their height fields, since we may
6005 need it later, if a buffer saved in the configuration is now
6006 dead. */
6007 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6008
6009 for (k = 0; k < saved_windows->size; k++)
6010 {
6011 p = SAVED_WINDOW_N (saved_windows, k);
6012 w = XWINDOW (p->window);
6013 w->next = Qnil;
6014
6015 if (!NILP (p->parent))
6016 w->parent = SAVED_WINDOW_N (saved_windows,
6017 XFASTINT (p->parent))->window;
6018 else
6019 w->parent = Qnil;
6020
6021 if (!NILP (p->prev))
6022 {
6023 w->prev = SAVED_WINDOW_N (saved_windows,
6024 XFASTINT (p->prev))->window;
6025 XWINDOW (w->prev)->next = p->window;
6026 }
6027 else
6028 {
6029 w->prev = Qnil;
6030 if (!NILP (w->parent))
6031 {
6032 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
6033 {
6034 XWINDOW (w->parent)->vchild = p->window;
6035 XWINDOW (w->parent)->hchild = Qnil;
6036 }
6037 else
6038 {
6039 XWINDOW (w->parent)->hchild = p->window;
6040 XWINDOW (w->parent)->vchild = Qnil;
6041 }
6042 }
6043 }
6044
6045 /* If we squirreled away the buffer in the window's height,
6046 restore it now. */
6047 if (BUFFERP (w->total_lines))
6048 w->buffer = w->total_lines;
6049 w->left_col = p->left_col;
6050 w->top_line = p->top_line;
6051 w->total_cols = p->total_cols;
6052 w->total_lines = p->total_lines;
6053 w->hscroll = p->hscroll;
6054 w->min_hscroll = p->min_hscroll;
6055 w->display_table = p->display_table;
6056 w->orig_top_line = p->orig_top_line;
6057 w->orig_total_lines = p->orig_total_lines;
6058 w->left_margin_cols = p->left_margin_cols;
6059 w->right_margin_cols = p->right_margin_cols;
6060 w->left_fringe_width = p->left_fringe_width;
6061 w->right_fringe_width = p->right_fringe_width;
6062 w->fringes_outside_margins = p->fringes_outside_margins;
6063 w->scroll_bar_width = p->scroll_bar_width;
6064 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
6065 w->dedicated = p->dedicated;
6066 w->resize_proportionally = p->resize_proportionally;
6067 XSETFASTINT (w->last_modified, 0);
6068 XSETFASTINT (w->last_overlay_modified, 0);
6069
6070 /* Reinstall the saved buffer and pointers into it. */
6071 if (NILP (p->buffer))
6072 w->buffer = p->buffer;
6073 else
6074 {
6075 if (!NILP (BVAR (XBUFFER (p->buffer), name)))
6076 /* If saved buffer is alive, install it. */
6077 {
6078 w->buffer = p->buffer;
6079 w->start_at_line_beg = p->start_at_line_beg;
6080 set_marker_restricted (w->start, p->start, w->buffer);
6081 set_marker_restricted (w->pointm, p->pointm, w->buffer);
6082 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
6083 p->mark, w->buffer);
6084
6085 /* As documented in Fcurrent_window_configuration, don't
6086 restore the location of point in the buffer which was
6087 current when the window configuration was recorded. */
6088 if (!EQ (p->buffer, new_current_buffer)
6089 && XBUFFER (p->buffer) == current_buffer)
6090 Fgoto_char (w->pointm);
6091 }
6092 else if (NILP (w->buffer) || NILP (BVAR (XBUFFER (w->buffer), name)))
6093 /* Else unless window has a live buffer, get one. */
6094 {
6095 w->buffer = Fcdr (Fcar (Vbuffer_alist));
6096 /* This will set the markers to beginning of visible
6097 range. */
6098 set_marker_restricted (w->start, make_number (0), w->buffer);
6099 set_marker_restricted (w->pointm, make_number (0),w->buffer);
6100 w->start_at_line_beg = Qt;
6101 }
6102 else
6103 /* Keeping window's old buffer; make sure the markers
6104 are real. */
6105 {
6106 /* Set window markers at start of visible range. */
6107 if (XMARKER (w->start)->buffer == 0)
6108 set_marker_restricted (w->start, make_number (0),
6109 w->buffer);
6110 if (XMARKER (w->pointm)->buffer == 0)
6111 set_marker_restricted_both (w->pointm, w->buffer,
6112 BUF_PT (XBUFFER (w->buffer)),
6113 BUF_PT_BYTE (XBUFFER (w->buffer)));
6114 w->start_at_line_beg = Qt;
6115 }
6116 }
6117 }
6118
6119 FRAME_ROOT_WINDOW (f) = data->root_window;
6120
6121 /* Arrange *not* to restore point in the buffer that was
6122 current when the window configuration was saved. */
6123 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
6124 set_marker_restricted (XWINDOW (data->current_window)->pointm,
6125 make_number (old_point),
6126 XWINDOW (data->current_window)->buffer);
6127
6128 /* In the following call to `select-window, prevent "swapping
6129 out point" in the old selected window using the buffer that
6130 has been restored into it. We already swapped out that point
6131 from that window's old buffer. */
6132 select_window (data->current_window, Qnil, 1);
6133 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
6134 = selected_window;
6135
6136 if (NILP (data->focus_frame)
6137 || (FRAMEP (data->focus_frame)
6138 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
6139 Fredirect_frame_focus (frame, data->focus_frame);
6140
6141 /* Set the screen height to the value it had before this function. */
6142 if (previous_frame_lines != FRAME_LINES (f)
6143 || previous_frame_cols != FRAME_COLS (f))
6144 change_frame_size (f, previous_frame_lines, previous_frame_cols,
6145 0, 0, 0);
6146 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
6147 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
6148 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
6149 make_number (0));
6150 #ifdef HAVE_WINDOW_SYSTEM
6151 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
6152 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
6153 make_number (0));
6154 #endif
6155 #endif
6156
6157 /* Now, free glyph matrices in windows that were not reused. */
6158 for (i = n = 0; i < n_leaf_windows; ++i)
6159 {
6160 if (NILP (leaf_windows[i]->buffer))
6161 {
6162 /* Assert it's not reused as a combination. */
6163 xassert (NILP (leaf_windows[i]->hchild)
6164 && NILP (leaf_windows[i]->vchild));
6165 free_window_matrices (leaf_windows[i]);
6166 }
6167 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
6168 ++n;
6169 }
6170
6171 adjust_glyphs (f);
6172
6173 UNBLOCK_INPUT;
6174
6175 /* Fselect_window will have made f the selected frame, so we
6176 reselect the proper frame here. Fhandle_switch_frame will change the
6177 selected window too, but that doesn't make the call to
6178 Fselect_window above totally superfluous; it still sets f's
6179 selected window. */
6180 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
6181 do_switch_frame (data->selected_frame, 0, 0, Qnil);
6182
6183 run_window_configuration_change_hook (f);
6184 }
6185
6186 if (!NILP (new_current_buffer))
6187 Fset_buffer (new_current_buffer);
6188
6189 Vminibuf_scroll_window = data->minibuf_scroll_window;
6190 minibuf_selected_window = data->minibuf_selected_window;
6191
6192 return (FRAME_LIVE_P (f) ? Qt : Qnil);
6193 }
6194
6195 /* Mark all windows now on frame as deleted
6196 by setting their buffers to nil. */
6197
6198 void
6199 delete_all_subwindows (register struct window *w)
6200 {
6201 if (!NILP (w->next))
6202 delete_all_subwindows (XWINDOW (w->next));
6203 if (!NILP (w->vchild))
6204 delete_all_subwindows (XWINDOW (w->vchild));
6205 if (!NILP (w->hchild))
6206 delete_all_subwindows (XWINDOW (w->hchild));
6207
6208 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
6209
6210 if (!NILP (w->buffer))
6211 unshow_buffer (w);
6212
6213 /* We set all three of these fields to nil, to make sure that we can
6214 distinguish this dead window from any live window. Live leaf
6215 windows will have buffer set, and combination windows will have
6216 vchild or hchild set. */
6217 w->buffer = Qnil;
6218 w->vchild = Qnil;
6219 w->hchild = Qnil;
6220
6221 Vwindow_list = Qnil;
6222 }
6223 \f
6224 static int
6225 count_windows (register struct window *window)
6226 {
6227 register int count = 1;
6228 if (!NILP (window->next))
6229 count += count_windows (XWINDOW (window->next));
6230 if (!NILP (window->vchild))
6231 count += count_windows (XWINDOW (window->vchild));
6232 if (!NILP (window->hchild))
6233 count += count_windows (XWINDOW (window->hchild));
6234 return count;
6235 }
6236
6237
6238 /* Fill vector FLAT with leaf windows under W, starting at index I.
6239 Value is last index + 1. */
6240
6241 static int
6242 get_leaf_windows (struct window *w, struct window **flat, int i)
6243 {
6244 while (w)
6245 {
6246 if (!NILP (w->hchild))
6247 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
6248 else if (!NILP (w->vchild))
6249 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
6250 else
6251 flat[i++] = w;
6252
6253 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6254 }
6255
6256 return i;
6257 }
6258
6259
6260 /* Return a pointer to the glyph W's physical cursor is on. Value is
6261 null if W's current matrix is invalid, so that no meaningfull glyph
6262 can be returned. */
6263
6264 struct glyph *
6265 get_phys_cursor_glyph (struct window *w)
6266 {
6267 struct glyph_row *row;
6268 struct glyph *glyph;
6269
6270 if (w->phys_cursor.vpos >= 0
6271 && w->phys_cursor.vpos < w->current_matrix->nrows
6272 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
6273 row->enabled_p)
6274 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
6275 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
6276 else
6277 glyph = NULL;
6278
6279 return glyph;
6280 }
6281
6282
6283 static int
6284 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
6285 {
6286 register struct saved_window *p;
6287 register struct window *w;
6288 register Lisp_Object tem;
6289
6290 for (;!NILP (window); window = w->next)
6291 {
6292 p = SAVED_WINDOW_N (vector, i);
6293 w = XWINDOW (window);
6294
6295 XSETFASTINT (w->temslot, i); i++;
6296 p->window = window;
6297 p->buffer = w->buffer;
6298 p->left_col = w->left_col;
6299 p->top_line = w->top_line;
6300 p->total_cols = w->total_cols;
6301 p->total_lines = w->total_lines;
6302 p->hscroll = w->hscroll;
6303 p->min_hscroll = w->min_hscroll;
6304 p->display_table = w->display_table;
6305 p->orig_top_line = w->orig_top_line;
6306 p->orig_total_lines = w->orig_total_lines;
6307 p->left_margin_cols = w->left_margin_cols;
6308 p->right_margin_cols = w->right_margin_cols;
6309 p->left_fringe_width = w->left_fringe_width;
6310 p->right_fringe_width = w->right_fringe_width;
6311 p->fringes_outside_margins = w->fringes_outside_margins;
6312 p->scroll_bar_width = w->scroll_bar_width;
6313 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
6314 p->dedicated = w->dedicated;
6315 p->resize_proportionally = w->resize_proportionally;
6316 if (!NILP (w->buffer))
6317 {
6318 /* Save w's value of point in the window configuration.
6319 If w is the selected window, then get the value of point
6320 from the buffer; pointm is garbage in the selected window. */
6321 if (EQ (window, selected_window))
6322 {
6323 p->pointm = Fmake_marker ();
6324 set_marker_both (p->pointm, w->buffer,
6325 BUF_PT (XBUFFER (w->buffer)),
6326 BUF_PT_BYTE (XBUFFER (w->buffer)));
6327 }
6328 else
6329 p->pointm = Fcopy_marker (w->pointm, Qnil);
6330
6331 p->start = Fcopy_marker (w->start, Qnil);
6332 p->start_at_line_beg = w->start_at_line_beg;
6333
6334 tem = BVAR (XBUFFER (w->buffer), mark);
6335 p->mark = Fcopy_marker (tem, Qnil);
6336 }
6337 else
6338 {
6339 p->pointm = Qnil;
6340 p->start = Qnil;
6341 p->mark = Qnil;
6342 p->start_at_line_beg = Qnil;
6343 }
6344
6345 if (NILP (w->parent))
6346 p->parent = Qnil;
6347 else
6348 p->parent = XWINDOW (w->parent)->temslot;
6349
6350 if (NILP (w->prev))
6351 p->prev = Qnil;
6352 else
6353 p->prev = XWINDOW (w->prev)->temslot;
6354
6355 if (!NILP (w->vchild))
6356 i = save_window_save (w->vchild, vector, i);
6357 if (!NILP (w->hchild))
6358 i = save_window_save (w->hchild, vector, i);
6359 }
6360
6361 return i;
6362 }
6363
6364 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
6365 Scurrent_window_configuration, 0, 1, 0,
6366 doc: /* Return an object representing the current window configuration of FRAME.
6367 If FRAME is nil or omitted, use the selected frame.
6368 This describes the number of windows, their sizes and current buffers,
6369 and for each displayed buffer, where display starts, and the positions of
6370 point and mark. An exception is made for point in the current buffer:
6371 its value is -not- saved.
6372 This also records the currently selected frame, and FRAME's focus
6373 redirection (see `redirect-frame-focus'). */)
6374 (Lisp_Object frame)
6375 {
6376 register Lisp_Object tem;
6377 register int n_windows;
6378 register struct save_window_data *data;
6379 register int i;
6380 FRAME_PTR f;
6381
6382 if (NILP (frame))
6383 frame = selected_frame;
6384 CHECK_LIVE_FRAME (frame);
6385 f = XFRAME (frame);
6386
6387 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6388 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6389 PVEC_WINDOW_CONFIGURATION);
6390
6391 data->frame_cols = FRAME_COLS (f);
6392 data->frame_lines = FRAME_LINES (f);
6393 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6394 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6395 data->selected_frame = selected_frame;
6396 data->current_window = FRAME_SELECTED_WINDOW (f);
6397 XSETBUFFER (data->current_buffer, current_buffer);
6398 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6399 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6400 data->root_window = FRAME_ROOT_WINDOW (f);
6401 data->focus_frame = FRAME_FOCUS_FRAME (f);
6402 tem = Fmake_vector (make_number (n_windows), Qnil);
6403 data->saved_windows = tem;
6404 for (i = 0; i < n_windows; i++)
6405 XVECTOR (tem)->contents[i]
6406 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6407 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6408 XSETWINDOW_CONFIGURATION (tem, data);
6409 return (tem);
6410 }
6411
6412 \f
6413 /***********************************************************************
6414 Window Split Tree
6415 ***********************************************************************/
6416
6417 static Lisp_Object
6418 window_tree (struct window *w)
6419 {
6420 Lisp_Object tail = Qnil;
6421 Lisp_Object result = Qnil;
6422
6423 while (w)
6424 {
6425 Lisp_Object wn;
6426
6427 XSETWINDOW (wn, w);
6428 if (!NILP (w->hchild))
6429 wn = Fcons (Qnil, Fcons (Fwindow_edges (wn),
6430 window_tree (XWINDOW (w->hchild))));
6431 else if (!NILP (w->vchild))
6432 wn = Fcons (Qt, Fcons (Fwindow_edges (wn),
6433 window_tree (XWINDOW (w->vchild))));
6434
6435 if (NILP (result))
6436 {
6437 result = tail = Fcons (wn, Qnil);
6438 }
6439 else
6440 {
6441 XSETCDR (tail, Fcons (wn, Qnil));
6442 tail = XCDR (tail);
6443 }
6444
6445 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6446 }
6447
6448 return result;
6449 }
6450
6451
6452
6453 DEFUN ("window-tree", Fwindow_tree, Swindow_tree,
6454 0, 1, 0,
6455 doc: /* Return the window tree for frame FRAME.
6456
6457 The return value is a list of the form (ROOT MINI), where ROOT
6458 represents the window tree of the frame's root window, and MINI
6459 is the frame's minibuffer window.
6460
6461 If the root window is not split, ROOT is the root window itself.
6462 Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil for a
6463 horizontal split, and t for a vertical split, EDGES gives the combined
6464 size and position of the subwindows in the split, and the rest of the
6465 elements are the subwindows in the split. Each of the subwindows may
6466 again be a window or a list representing a window split, and so on.
6467 EDGES is a list \(LEFT TOP RIGHT BOTTOM) as returned by `window-edges'.
6468
6469 If FRAME is nil or omitted, return information on the currently
6470 selected frame. */)
6471 (Lisp_Object frame)
6472 {
6473 FRAME_PTR f;
6474
6475 if (NILP (frame))
6476 frame = selected_frame;
6477
6478 CHECK_FRAME (frame);
6479 f = XFRAME (frame);
6480
6481 if (!FRAME_LIVE_P (f))
6482 return Qnil;
6483
6484 return window_tree (XWINDOW (FRAME_ROOT_WINDOW (f)));
6485 }
6486
6487 \f
6488 /***********************************************************************
6489 Marginal Areas
6490 ***********************************************************************/
6491
6492 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6493 2, 3, 0,
6494 doc: /* Set width of marginal areas of window WINDOW.
6495 If WINDOW is nil, set margins of the currently selected window.
6496 Second arg LEFT-WIDTH specifies the number of character cells to
6497 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6498 does the same for the right marginal area. A nil width parameter
6499 means no margin. */)
6500 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6501 {
6502 struct window *w = decode_window (window);
6503
6504 /* Translate negative or zero widths to nil.
6505 Margins that are too wide have to be checked elsewhere. */
6506
6507 if (!NILP (left_width))
6508 {
6509 CHECK_NUMBER (left_width);
6510 if (XINT (left_width) <= 0)
6511 left_width = Qnil;
6512 }
6513
6514 if (!NILP (right_width))
6515 {
6516 CHECK_NUMBER (right_width);
6517 if (XINT (right_width) <= 0)
6518 right_width = Qnil;
6519 }
6520
6521 if (!EQ (w->left_margin_cols, left_width)
6522 || !EQ (w->right_margin_cols, right_width))
6523 {
6524 w->left_margin_cols = left_width;
6525 w->right_margin_cols = right_width;
6526
6527 adjust_window_margins (w);
6528
6529 ++windows_or_buffers_changed;
6530 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6531 }
6532
6533 return Qnil;
6534 }
6535
6536
6537 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6538 0, 1, 0,
6539 doc: /* Get width of marginal areas of window WINDOW.
6540 If WINDOW is omitted or nil, use the currently selected window.
6541 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6542 If a marginal area does not exist, its width will be returned
6543 as nil. */)
6544 (Lisp_Object window)
6545 {
6546 struct window *w = decode_window (window);
6547 return Fcons (w->left_margin_cols, w->right_margin_cols);
6548 }
6549
6550
6551 \f
6552 /***********************************************************************
6553 Fringes
6554 ***********************************************************************/
6555
6556 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6557 2, 4, 0,
6558 doc: /* Set the fringe widths of window WINDOW.
6559 If WINDOW is nil, set the fringe widths of the currently selected
6560 window.
6561 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6562 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6563 fringe width. If a fringe width arg is nil, that means to use the
6564 frame's default fringe width. Default fringe widths can be set with
6565 the command `set-fringe-style'.
6566 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6567 outside of the display margins. By default, fringes are drawn between
6568 display marginal areas and the text area. */)
6569 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6570 {
6571 struct window *w = decode_window (window);
6572
6573 if (!NILP (left_width))
6574 CHECK_NATNUM (left_width);
6575 if (!NILP (right_width))
6576 CHECK_NATNUM (right_width);
6577
6578 /* Do nothing on a tty. */
6579 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6580 && (!EQ (w->left_fringe_width, left_width)
6581 || !EQ (w->right_fringe_width, right_width)
6582 || !EQ (w->fringes_outside_margins, outside_margins)))
6583 {
6584 w->left_fringe_width = left_width;
6585 w->right_fringe_width = right_width;
6586 w->fringes_outside_margins = outside_margins;
6587
6588 adjust_window_margins (w);
6589
6590 clear_glyph_matrix (w->current_matrix);
6591 w->window_end_valid = Qnil;
6592
6593 ++windows_or_buffers_changed;
6594 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6595 }
6596
6597 return Qnil;
6598 }
6599
6600
6601 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6602 0, 1, 0,
6603 doc: /* Get width of fringes of window WINDOW.
6604 If WINDOW is omitted or nil, use the currently selected window.
6605 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6606 (Lisp_Object window)
6607 {
6608 struct window *w = decode_window (window);
6609
6610 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6611 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6612 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6613 ? Qt : Qnil), Qnil)));
6614 }
6615
6616
6617 \f
6618 /***********************************************************************
6619 Scroll bars
6620 ***********************************************************************/
6621
6622 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars, Sset_window_scroll_bars,
6623 2, 4, 0,
6624 doc: /* Set width and type of scroll bars of window WINDOW.
6625 If window is nil, set scroll bars of the currently selected window.
6626 Second parameter WIDTH specifies the pixel width for the scroll bar;
6627 this is automatically adjusted to a multiple of the frame column width.
6628 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6629 bar: left, right, or nil.
6630 If WIDTH is nil, use the frame's scroll-bar width.
6631 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6632 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6633 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6634 {
6635 struct window *w = decode_window (window);
6636
6637 if (!NILP (width))
6638 {
6639 CHECK_NATNUM (width);
6640
6641 if (XINT (width) == 0)
6642 vertical_type = Qnil;
6643 }
6644
6645 if (!(NILP (vertical_type)
6646 || EQ (vertical_type, Qleft)
6647 || EQ (vertical_type, Qright)
6648 || EQ (vertical_type, Qt)))
6649 error ("Invalid type of vertical scroll bar");
6650
6651 if (!EQ (w->scroll_bar_width, width)
6652 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6653 {
6654 w->scroll_bar_width = width;
6655 w->vertical_scroll_bar_type = vertical_type;
6656
6657 adjust_window_margins (w);
6658
6659 clear_glyph_matrix (w->current_matrix);
6660 w->window_end_valid = Qnil;
6661
6662 ++windows_or_buffers_changed;
6663 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6664 }
6665
6666 return Qnil;
6667 }
6668
6669
6670 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6671 0, 1, 0,
6672 doc: /* Get width and type of scroll bars of window WINDOW.
6673 If WINDOW is omitted or nil, use the currently selected window.
6674 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6675 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6676 value. */)
6677 (Lisp_Object window)
6678 {
6679 struct window *w = decode_window (window);
6680 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6681 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6682 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6683 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6684 Fcons (w->vertical_scroll_bar_type,
6685 Fcons (Qnil, Qnil))));
6686 }
6687
6688
6689 \f
6690 /***********************************************************************
6691 Smooth scrolling
6692 ***********************************************************************/
6693
6694 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6695 doc: /* Return the amount by which WINDOW is scrolled vertically.
6696 Use the selected window if WINDOW is nil or omitted.
6697 Normally, value is a multiple of the canonical character height of WINDOW;
6698 optional second arg PIXELS-P means value is measured in pixels. */)
6699 (Lisp_Object window, Lisp_Object pixels_p)
6700 {
6701 Lisp_Object result;
6702 struct frame *f;
6703 struct window *w;
6704
6705 if (NILP (window))
6706 window = selected_window;
6707 else
6708 CHECK_WINDOW (window);
6709 w = XWINDOW (window);
6710 f = XFRAME (w->frame);
6711
6712 if (FRAME_WINDOW_P (f))
6713 result = (NILP (pixels_p)
6714 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6715 : make_number (-w->vscroll));
6716 else
6717 result = make_number (0);
6718 return result;
6719 }
6720
6721
6722 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6723 2, 3, 0,
6724 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6725 WINDOW nil means use the selected window. Normally, VSCROLL is a
6726 non-negative multiple of the canonical character height of WINDOW;
6727 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6728 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6729 corresponds to an integral number of pixels. The return value is the
6730 result of this rounding.
6731 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6732 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6733 {
6734 struct window *w;
6735 struct frame *f;
6736
6737 if (NILP (window))
6738 window = selected_window;
6739 else
6740 CHECK_WINDOW (window);
6741 CHECK_NUMBER_OR_FLOAT (vscroll);
6742
6743 w = XWINDOW (window);
6744 f = XFRAME (w->frame);
6745
6746 if (FRAME_WINDOW_P (f))
6747 {
6748 int old_dy = w->vscroll;
6749
6750 w->vscroll = - (NILP (pixels_p)
6751 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6752 : XFLOATINT (vscroll));
6753 w->vscroll = min (w->vscroll, 0);
6754
6755 if (w->vscroll != old_dy)
6756 {
6757 /* Adjust glyph matrix of the frame if the virtual display
6758 area becomes larger than before. */
6759 if (w->vscroll < 0 && w->vscroll < old_dy)
6760 adjust_glyphs (f);
6761
6762 /* Prevent redisplay shortcuts. */
6763 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6764 }
6765 }
6766
6767 return Fwindow_vscroll (window, pixels_p);
6768 }
6769
6770 \f
6771 /* Call FN for all leaf windows on frame F. FN is called with the
6772 first argument being a pointer to the leaf window, and with
6773 additional argument USER_DATA. Stops when FN returns 0. */
6774
6775 static void
6776 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6777 void *user_data)
6778 {
6779 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6780 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6781 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6782 }
6783
6784
6785 /* Helper function for foreach_window. Call FN for all leaf windows
6786 reachable from W. FN is called with the first argument being a
6787 pointer to the leaf window, and with additional argument USER_DATA.
6788 Stop when FN returns 0. Value is 0 if stopped by FN. */
6789
6790 static int
6791 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6792 {
6793 int cont;
6794
6795 for (cont = 1; w && cont;)
6796 {
6797 if (!NILP (w->hchild))
6798 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6799 else if (!NILP (w->vchild))
6800 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6801 else
6802 cont = fn (w, user_data);
6803
6804 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6805 }
6806
6807 return cont;
6808 }
6809
6810
6811 /* Freeze or unfreeze the window start of W unless it is a
6812 mini-window or the selected window. FREEZE_P non-null means freeze
6813 the window start. */
6814
6815 static int
6816 freeze_window_start (struct window *w, void *freeze_p)
6817 {
6818 if (MINI_WINDOW_P (w)
6819 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6820 && (w == XWINDOW (selected_window)
6821 || (MINI_WINDOW_P (XWINDOW (selected_window))
6822 && ! NILP (Vminibuf_scroll_window)
6823 && w == XWINDOW (Vminibuf_scroll_window)))))
6824 freeze_p = NULL;
6825
6826 w->frozen_window_start_p = freeze_p != NULL;
6827 return 1;
6828 }
6829
6830
6831 /* Freeze or unfreeze the window starts of all leaf windows on frame
6832 F, except the selected window and a mini-window. FREEZE_P non-zero
6833 means freeze the window start. */
6834
6835 void
6836 freeze_window_starts (struct frame *f, int freeze_p)
6837 {
6838 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6839 }
6840
6841 \f
6842 /***********************************************************************
6843 Initialization
6844 ***********************************************************************/
6845
6846 /* Return 1 if window configurations C1 and C2
6847 describe the same state of affairs. This is used by Fequal. */
6848
6849 int
6850 compare_window_configurations (Lisp_Object c1, Lisp_Object c2, int ignore_positions)
6851 {
6852 register struct save_window_data *d1, *d2;
6853 struct Lisp_Vector *sw1, *sw2;
6854 int i;
6855
6856 CHECK_WINDOW_CONFIGURATION (c1);
6857 CHECK_WINDOW_CONFIGURATION (c2);
6858
6859 d1 = (struct save_window_data *) XVECTOR (c1);
6860 d2 = (struct save_window_data *) XVECTOR (c2);
6861 sw1 = XVECTOR (d1->saved_windows);
6862 sw2 = XVECTOR (d2->saved_windows);
6863
6864 if (d1->frame_cols != d2->frame_cols)
6865 return 0;
6866 if (d1->frame_lines != d2->frame_lines)
6867 return 0;
6868 if (d1->frame_menu_bar_lines != d2->frame_menu_bar_lines)
6869 return 0;
6870 if (! EQ (d1->selected_frame, d2->selected_frame))
6871 return 0;
6872 /* Don't compare the current_window field directly.
6873 Instead see w1_is_current and w2_is_current, below. */
6874 if (! EQ (d1->current_buffer, d2->current_buffer))
6875 return 0;
6876 if (! ignore_positions)
6877 {
6878 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
6879 return 0;
6880 if (! EQ (d1->minibuf_selected_window, d2->minibuf_selected_window))
6881 return 0;
6882 }
6883 /* Don't compare the root_window field.
6884 We don't require the two configurations
6885 to use the same window object,
6886 and the two root windows must be equivalent
6887 if everything else compares equal. */
6888 if (! EQ (d1->focus_frame, d2->focus_frame))
6889 return 0;
6890
6891 /* Verify that the two confis have the same number of windows. */
6892 if (sw1->size != sw2->size)
6893 return 0;
6894
6895 for (i = 0; i < sw1->size; i++)
6896 {
6897 struct saved_window *p1, *p2;
6898 int w1_is_current, w2_is_current;
6899
6900 p1 = SAVED_WINDOW_N (sw1, i);
6901 p2 = SAVED_WINDOW_N (sw2, i);
6902
6903 /* Verify that the current windows in the two
6904 configurations correspond to each other. */
6905 w1_is_current = EQ (d1->current_window, p1->window);
6906 w2_is_current = EQ (d2->current_window, p2->window);
6907
6908 if (w1_is_current != w2_is_current)
6909 return 0;
6910
6911 /* Verify that the corresponding windows do match. */
6912 if (! EQ (p1->buffer, p2->buffer))
6913 return 0;
6914 if (! EQ (p1->left_col, p2->left_col))
6915 return 0;
6916 if (! EQ (p1->top_line, p2->top_line))
6917 return 0;
6918 if (! EQ (p1->total_cols, p2->total_cols))
6919 return 0;
6920 if (! EQ (p1->total_lines, p2->total_lines))
6921 return 0;
6922 if (! EQ (p1->display_table, p2->display_table))
6923 return 0;
6924 if (! EQ (p1->parent, p2->parent))
6925 return 0;
6926 if (! EQ (p1->prev, p2->prev))
6927 return 0;
6928 if (! ignore_positions)
6929 {
6930 if (! EQ (p1->hscroll, p2->hscroll))
6931 return 0;
6932 if (!EQ (p1->min_hscroll, p2->min_hscroll))
6933 return 0;
6934 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
6935 return 0;
6936 if (NILP (Fequal (p1->start, p2->start)))
6937 return 0;
6938 if (NILP (Fequal (p1->pointm, p2->pointm)))
6939 return 0;
6940 if (NILP (Fequal (p1->mark, p2->mark)))
6941 return 0;
6942 }
6943 if (! EQ (p1->left_margin_cols, p2->left_margin_cols))
6944 return 0;
6945 if (! EQ (p1->right_margin_cols, p2->right_margin_cols))
6946 return 0;
6947 if (! EQ (p1->left_fringe_width, p2->left_fringe_width))
6948 return 0;
6949 if (! EQ (p1->right_fringe_width, p2->right_fringe_width))
6950 return 0;
6951 if (! EQ (p1->fringes_outside_margins, p2->fringes_outside_margins))
6952 return 0;
6953 if (! EQ (p1->scroll_bar_width, p2->scroll_bar_width))
6954 return 0;
6955 if (! EQ (p1->vertical_scroll_bar_type, p2->vertical_scroll_bar_type))
6956 return 0;
6957 }
6958
6959 return 1;
6960 }
6961
6962 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6963 Scompare_window_configurations, 2, 2, 0,
6964 doc: /* Compare two window configurations as regards the structure of windows.
6965 This function ignores details such as the values of point and mark
6966 and scrolling positions. */)
6967 (Lisp_Object x, Lisp_Object y)
6968 {
6969 if (compare_window_configurations (x, y, 1))
6970 return Qt;
6971 return Qnil;
6972 }
6973 \f
6974 void
6975 init_window_once (void)
6976 {
6977 struct frame *f = make_initial_frame ();
6978 XSETFRAME (selected_frame, f);
6979 Vterminal_frame = selected_frame;
6980 minibuf_window = f->minibuffer_window;
6981 selected_window = f->selected_window;
6982 last_nonminibuf_frame = f;
6983
6984 window_initialized = 1;
6985 }
6986
6987 void
6988 init_window (void)
6989 {
6990 Vwindow_list = Qnil;
6991 }
6992
6993 void
6994 syms_of_window (void)
6995 {
6996 Qscroll_up = intern_c_string ("scroll-up");
6997 staticpro (&Qscroll_up);
6998
6999 Qscroll_down = intern_c_string ("scroll-down");
7000 staticpro (&Qscroll_down);
7001
7002 Qscroll_command = intern_c_string ("scroll-command");
7003 staticpro (&Qscroll_command);
7004
7005 Fput (Qscroll_up, Qscroll_command, Qt);
7006 Fput (Qscroll_down, Qscroll_command, Qt);
7007
7008 Qwindow_size_fixed = intern_c_string ("window-size-fixed");
7009 staticpro (&Qwindow_size_fixed);
7010 Fset (Qwindow_size_fixed, Qnil);
7011
7012 staticpro (&Qwindow_configuration_change_hook);
7013 Qwindow_configuration_change_hook
7014 = intern_c_string ("window-configuration-change-hook");
7015
7016 Qwindowp = intern_c_string ("windowp");
7017 staticpro (&Qwindowp);
7018
7019 Qwindow_configuration_p = intern_c_string ("window-configuration-p");
7020 staticpro (&Qwindow_configuration_p);
7021
7022 Qwindow_live_p = intern_c_string ("window-live-p");
7023 staticpro (&Qwindow_live_p);
7024
7025 Qdisplay_buffer = intern_c_string ("display-buffer");
7026 staticpro (&Qdisplay_buffer);
7027
7028 Qtemp_buffer_show_hook = intern_c_string ("temp-buffer-show-hook");
7029 staticpro (&Qtemp_buffer_show_hook);
7030
7031 staticpro (&Vwindow_list);
7032
7033 minibuf_selected_window = Qnil;
7034 staticpro (&minibuf_selected_window);
7035
7036 window_scroll_pixel_based_preserve_x = -1;
7037 window_scroll_pixel_based_preserve_y = -1;
7038 window_scroll_preserve_hpos = -1;
7039 window_scroll_preserve_vpos = -1;
7040
7041 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
7042 doc: /* Non-nil means call as function to display a help buffer.
7043 The function is called with one argument, the buffer to be displayed.
7044 Used by `with-output-to-temp-buffer'.
7045 If this function is used, then it must do the entire job of showing
7046 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
7047 Vtemp_buffer_show_function = Qnil;
7048
7049 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
7050 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
7051 Vminibuf_scroll_window = Qnil;
7052
7053 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
7054 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
7055 If the minibuffer is active, the `minibuffer-scroll-window' mode line
7056 is displayed in the `mode-line' face. */);
7057 mode_line_in_non_selected_windows = 1;
7058
7059 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
7060 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
7061 Vother_window_scroll_buffer = Qnil;
7062
7063 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
7064 doc: /* *Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
7065 auto_window_vscroll_p = 1;
7066
7067 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
7068 doc: /* *Number of lines of continuity when scrolling by screenfuls. */);
7069 next_screen_context_lines = 2;
7070
7071 DEFVAR_INT ("window-min-height", window_min_height,
7072 doc: /* Allow deleting windows less than this tall.
7073 The value is measured in line units. If a window wants a modeline it
7074 is counted as one line.
7075
7076 Emacs honors settings of this variable when enlarging or shrinking
7077 windows vertically. A value less than 1 is invalid. */);
7078 window_min_height = 4;
7079
7080 DEFVAR_INT ("window-min-width", window_min_width,
7081 doc: /* Allow deleting windows less than this wide.
7082 The value is measured in characters and includes any fringes or
7083 the scrollbar.
7084
7085 Emacs honors settings of this variable when enlarging or shrinking
7086 windows horizontally. A value less than 2 is invalid. */);
7087 window_min_width = 10;
7088
7089 DEFVAR_LISP ("scroll-preserve-screen-position",
7090 Vscroll_preserve_screen_position,
7091 doc: /* *Controls if scroll commands move point to keep its screen position unchanged.
7092 A value of nil means point does not keep its screen position except
7093 at the scroll margin or window boundary respectively.
7094 A value of t means point keeps its screen position if the scroll
7095 command moved it vertically out of the window, e.g. when scrolling
7096 by full screens.
7097 Any other value means point always keeps its screen position.
7098 Scroll commands should have the `scroll-command' property
7099 on their symbols to be controlled by this variable. */);
7100 Vscroll_preserve_screen_position = Qnil;
7101
7102 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
7103 doc: /* Type of marker to use for `window-point'. */);
7104 Vwindow_point_insertion_type = Qnil;
7105
7106 DEFVAR_LISP ("window-configuration-change-hook",
7107 Vwindow_configuration_change_hook,
7108 doc: /* Functions to call when window configuration changes.
7109 The buffer-local part is run once per window, with the relevant window
7110 selected; while the global part is run only once for the modified frame,
7111 with the relevant frame selected. */);
7112 Vwindow_configuration_change_hook = Qnil;
7113
7114 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
7115 doc: /* If non-nil, then the `recenter' command with a nil argument
7116 will redraw the entire frame; the special value `tty' causes the
7117 frame to be redrawn only if it is a tty frame. */);
7118 Vrecenter_redisplay = Qtty;
7119
7120
7121 defsubr (&Sselected_window);
7122 defsubr (&Sminibuffer_window);
7123 defsubr (&Swindow_minibuffer_p);
7124 defsubr (&Swindowp);
7125 defsubr (&Swindow_live_p);
7126 defsubr (&Spos_visible_in_window_p);
7127 defsubr (&Swindow_line_height);
7128 defsubr (&Swindow_buffer);
7129 defsubr (&Swindow_height);
7130 defsubr (&Swindow_width);
7131 defsubr (&Swindow_full_width_p);
7132 defsubr (&Swindow_hscroll);
7133 defsubr (&Sset_window_hscroll);
7134 defsubr (&Swindow_redisplay_end_trigger);
7135 defsubr (&Sset_window_redisplay_end_trigger);
7136 defsubr (&Swindow_edges);
7137 defsubr (&Swindow_pixel_edges);
7138 defsubr (&Swindow_absolute_pixel_edges);
7139 defsubr (&Swindow_inside_edges);
7140 defsubr (&Swindow_inside_pixel_edges);
7141 defsubr (&Swindow_inside_absolute_pixel_edges);
7142 defsubr (&Scoordinates_in_window_p);
7143 defsubr (&Swindow_at);
7144 defsubr (&Swindow_point);
7145 defsubr (&Swindow_start);
7146 defsubr (&Swindow_end);
7147 defsubr (&Sset_window_point);
7148 defsubr (&Sset_window_start);
7149 defsubr (&Swindow_dedicated_p);
7150 defsubr (&Sset_window_dedicated_p);
7151 defsubr (&Swindow_display_table);
7152 defsubr (&Sset_window_display_table);
7153 defsubr (&Snext_window);
7154 defsubr (&Sprevious_window);
7155 defsubr (&Sother_window);
7156 defsubr (&Sget_lru_window);
7157 defsubr (&Swindow_use_time);
7158 defsubr (&Sget_largest_window);
7159 defsubr (&Sget_buffer_window);
7160 defsubr (&Sdelete_other_windows);
7161 defsubr (&Sdelete_windows_on);
7162 defsubr (&Sreplace_buffer_in_windows);
7163 defsubr (&Sdelete_window);
7164 defsubr (&Sset_window_buffer);
7165 defsubr (&Sselect_window);
7166 defsubr (&Sforce_window_update);
7167 defsubr (&Stemp_output_buffer_show);
7168 defsubr (&Ssplit_window);
7169 defsubr (&Senlarge_window);
7170 defsubr (&Sshrink_window);
7171 defsubr (&Sadjust_window_trailing_edge);
7172 defsubr (&Sscroll_up);
7173 defsubr (&Sscroll_down);
7174 defsubr (&Sscroll_left);
7175 defsubr (&Sscroll_right);
7176 defsubr (&Sother_window_for_scrolling);
7177 defsubr (&Sscroll_other_window);
7178 defsubr (&Sminibuffer_selected_window);
7179 defsubr (&Srecenter);
7180 defsubr (&Swindow_text_height);
7181 defsubr (&Smove_to_window_line);
7182 defsubr (&Swindow_configuration_p);
7183 defsubr (&Swindow_configuration_frame);
7184 defsubr (&Sset_window_configuration);
7185 defsubr (&Scurrent_window_configuration);
7186 defsubr (&Swindow_tree);
7187 defsubr (&Sset_window_margins);
7188 defsubr (&Swindow_margins);
7189 defsubr (&Sset_window_fringes);
7190 defsubr (&Swindow_fringes);
7191 defsubr (&Sset_window_scroll_bars);
7192 defsubr (&Swindow_scroll_bars);
7193 defsubr (&Swindow_vscroll);
7194 defsubr (&Sset_window_vscroll);
7195 defsubr (&Scompare_window_configurations);
7196 defsubr (&Swindow_list);
7197 defsubr (&Swindow_parameters);
7198 defsubr (&Swindow_parameter);
7199 defsubr (&Sset_window_parameter);
7200
7201 }
7202
7203 void
7204 keys_of_window (void)
7205 {
7206 initial_define_key (control_x_map, '1', "delete-other-windows");
7207 initial_define_key (control_x_map, '2', "split-window");
7208 initial_define_key (control_x_map, '0', "delete-window");
7209 initial_define_key (control_x_map, 'o', "other-window");
7210 initial_define_key (control_x_map, '^', "enlarge-window");
7211 initial_define_key (control_x_map, '<', "scroll-left");
7212 initial_define_key (control_x_map, '>', "scroll-right");
7213
7214 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
7215 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
7216 initial_define_key (meta_map, 'v', "scroll-down-command");
7217 }
7218