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