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