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