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