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