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