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