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