(window_display_table): Don't look inside buffer if its nil.
[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 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "frame.h"
26 #include "window.h"
27 #include "commands.h"
28 #include "indent.h"
29 #include "termchar.h"
30 #include "disptab.h"
31 #include "keyboard.h"
32 #include "blockinput.h"
33
34 Lisp_Object Qwindowp, Qwindow_live_p;
35
36 static struct window *decode_window P_ ((Lisp_Object));
37
38 /* This is the window in which the terminal's cursor should
39 be left when nothing is being done with it. This must
40 always be a leaf window, and its buffer is selected by
41 the top level editing loop at the end of each command.
42
43 This value is always the same as
44 FRAME_SELECTED_WINDOW (selected_frame). */
45
46 Lisp_Object selected_window;
47
48 /* The minibuffer window of the selected frame.
49 Note that you cannot test for minibufferness of an arbitrary window
50 by comparing against this; but you can test for minibufferness of
51 the selected window. */
52 Lisp_Object minibuf_window;
53
54 /* Non-nil means it is the window for C-M-v to scroll
55 when the minibuffer is selected. */
56 Lisp_Object Vminibuf_scroll_window;
57
58 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
59 Lisp_Object Vother_window_scroll_buffer;
60
61 /* Non-nil means it's function to call to display temp buffers. */
62 Lisp_Object Vtemp_buffer_show_function;
63
64 /* If a window gets smaller than either of these, it is removed. */
65 int window_min_height;
66 int window_min_width;
67
68 /* Nonzero implies Fdisplay_buffer should create windows. */
69 int pop_up_windows;
70
71 /* Nonzero implies make new frames for Fdisplay_buffer. */
72 int pop_up_frames;
73
74 /* Non-nil means use this function instead of default */
75 Lisp_Object Vpop_up_frame_function;
76
77 /* Function to call to handle Fdisplay_buffer. */
78 Lisp_Object Vdisplay_buffer_function;
79
80 /* List of buffer *names* for buffers that should have their own frames. */
81 Lisp_Object Vspecial_display_buffer_names;
82
83 /* List of regexps for buffer names that should have their own frames. */
84 Lisp_Object Vspecial_display_regexps;
85
86 /* Function to pop up a special frame. */
87 Lisp_Object Vspecial_display_function;
88
89 /* List of buffer *names* for buffers to appear in selected window. */
90 Lisp_Object Vsame_window_buffer_names;
91
92 /* List of regexps for buffer names to appear in selected window. */
93 Lisp_Object Vsame_window_regexps;
94
95 /* Hook run at end of temp_output_buffer_show. */
96 Lisp_Object Qtemp_buffer_show_hook;
97
98 /* Fdisplay_buffer always splits the largest window
99 if that window is more than this high. */
100 int split_height_threshold;
101
102 /* Number of lines of continuity in scrolling by screenfuls. */
103 int next_screen_context_lines;
104
105 /* Incremented for each window created. */
106 static int sequence_number;
107
108 /* Nonzero after init_window_once has finished. */
109 static int window_initialized;
110
111 /* Hook to run when window config changes. */
112 Lisp_Object Qwindow_configuration_change_hook;
113 Lisp_Object Vwindow_configuration_change_hook;
114
115 /* Nonzero means scroll commands try to put point
116 at the same screen height as previously. */
117 static int scroll_preserve_screen_position;
118
119 /* Nonzero means we can split a frame even if it is "unsplittable". */
120 static int inhibit_frame_unsplittable;
121
122 #define min(a, b) ((a) < (b) ? (a) : (b))
123
124 extern int scroll_margin;
125
126 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
127 \f
128 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
129 "Returns t if OBJECT is a window.")
130 (object)
131 Lisp_Object object;
132 {
133 return WINDOWP (object) ? Qt : Qnil;
134 }
135
136 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
137 "Returns t if OBJECT is a window which is currently visible.")
138 (object)
139 Lisp_Object object;
140 {
141 return (WINDOWP (object) && ! NILP (XWINDOW (object)->buffer) ? Qt : Qnil);
142 }
143
144 Lisp_Object
145 make_window ()
146 {
147 Lisp_Object val;
148 register struct window *p;
149 register struct Lisp_Vector *vec;
150 int i;
151
152 vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct window));
153 for (i = 0; i < VECSIZE (struct window); i++)
154 vec->contents[i] = Qnil;
155 vec->size = VECSIZE (struct window);
156 p = (struct window *)vec;
157 XSETFASTINT (p->sequence_number, ++sequence_number);
158 XSETFASTINT (p->left, 0);
159 XSETFASTINT (p->top, 0);
160 XSETFASTINT (p->height, 0);
161 XSETFASTINT (p->width, 0);
162 XSETFASTINT (p->hscroll, 0);
163 XSETFASTINT (p->last_point_x, 0);
164 XSETFASTINT (p->last_point_y, 0);
165 p->start = Fmake_marker ();
166 p->pointm = Fmake_marker ();
167 XSETFASTINT (p->use_time, 0);
168 p->frame = Qnil;
169 p->display_table = Qnil;
170 p->dedicated = Qnil;
171 XSETWINDOW (val, p);
172 return val;
173 }
174
175 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
176 "Return the window that the cursor now appears in and commands apply to.")
177 ()
178 {
179 return selected_window;
180 }
181
182 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
183 "Return the window used now for minibuffers.\n\
184 If the optional argument FRAME is specified, return the minibuffer window\n\
185 used by that frame.")
186 (frame)
187 Lisp_Object frame;
188 {
189 if (NILP (frame))
190 XSETFRAME (frame, selected_frame);
191 else
192 CHECK_LIVE_FRAME (frame, 0);
193
194 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
195 }
196
197 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
198 "Returns non-nil if WINDOW is a minibuffer window.")
199 (window)
200 Lisp_Object window;
201 {
202 struct window *w = decode_window (window);
203 return (MINI_WINDOW_P (w) ? Qt : Qnil);
204 }
205
206 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
207 Spos_visible_in_window_p, 0, 2, 0,
208 "Return t if position POS is currently on the frame in WINDOW.\n\
209 Returns nil if that position is scrolled vertically out of view.\n\
210 POS defaults to point; WINDOW, to the selected window.")
211 (pos, window)
212 Lisp_Object pos, window;
213 {
214 register struct window *w;
215 register int top;
216 register int height;
217 register int posint;
218 register struct buffer *buf;
219 struct position posval;
220 int hscroll;
221
222 if (NILP (pos))
223 posint = PT;
224 else
225 {
226 CHECK_NUMBER_COERCE_MARKER (pos, 0);
227 posint = XINT (pos);
228 }
229
230 w = decode_window (window);
231 top = marker_position (w->start);
232 hscroll = XINT (w->hscroll);
233
234 if (posint < top)
235 return Qnil;
236
237 height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
238
239 buf = XBUFFER (w->buffer);
240 if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
241 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf))
242 {
243 /* If frame is up to date,
244 use the info recorded about how much text fit on it. */
245 if (posint < BUF_Z (buf) - XFASTINT (w->window_end_pos)
246 || (XFASTINT (w->window_end_vpos) < height))
247 return Qt;
248 return Qnil;
249 }
250 else
251 {
252 if (posint > BUF_ZV (buf))
253 return Qnil;
254
255 /* w->start can be out of range. If it is, do something reasonable. */
256 if (top < BUF_BEGV (buf) || top > BUF_ZV (buf))
257 return Qnil;
258
259 /* If that info is not correct, calculate afresh */
260 /* BUG FIX for the 7th arg (TOHPOS).
261
262 '0' is harmless, however, ' - (1 << (BITS_PER_SHORT - 1))' is
263 more appropriate here. In case of HSCROLL > 0, this can avoid
264 needless calculation done until (HPOS == 0).
265
266 We want to determine if the position POSINT is in HEIGHT or
267 not. We don't have to do calculation until (HPOS == 0). We
268 can stop it when VPOS goes beyond HEIGHT. */
269 posval = *compute_motion (top, 0, (hscroll ? 1 - hscroll : 0), 0,
270 posint, height, - (1 << (BITS_PER_SHORT - 1)),
271 window_internal_width (w) - 1,
272 hscroll, 0, w);
273
274 return posval.vpos < height ? Qt : Qnil;
275 }
276 }
277 \f
278 static struct window *
279 decode_window (window)
280 register Lisp_Object window;
281 {
282 if (NILP (window))
283 return XWINDOW (selected_window);
284
285 CHECK_LIVE_WINDOW (window, 0);
286 return XWINDOW (window);
287 }
288
289 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
290 "Return the buffer that WINDOW is displaying.")
291 (window)
292 Lisp_Object window;
293 {
294 return decode_window (window)->buffer;
295 }
296
297 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
298 "Return the number of lines in WINDOW (including its mode line).")
299 (window)
300 Lisp_Object window;
301 {
302 return decode_window (window)->height;
303 }
304
305 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
306 "Return the number of display columns in WINDOW.\n\
307 This is the width that is usable columns available for text in WINDOW.\n\
308 If you want to find out how many columns WINDOW takes up,\n\
309 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
310 (window)
311 Lisp_Object window;
312 {
313 return make_number (window_internal_width (decode_window (window)));
314 }
315
316 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
317 "Return the number of columns by which WINDOW is scrolled from left margin.")
318 (window)
319 Lisp_Object window;
320 {
321 return decode_window (window)->hscroll;
322 }
323
324 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
325 "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
326 NCOL should be zero or positive.")
327 (window, ncol)
328 register Lisp_Object window, ncol;
329 {
330 register struct window *w;
331
332 CHECK_NUMBER (ncol, 1);
333 if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
334 w = decode_window (window);
335 if (XINT (w->hscroll) != XINT (ncol))
336 XBUFFER (w->buffer)->clip_changed = 1; /* Prevent redisplay shortcuts */
337 w->hscroll = ncol;
338 return ncol;
339 }
340
341 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
342 Swindow_redisplay_end_trigger, 0, 1, 0,
343 "Return WINDOW's redisplay end trigger value.\n\
344 See `set-window-redisplay-end-trigger' for more information.")
345 (window)
346 Lisp_Object window;
347 {
348 return decode_window (window)->redisplay_end_trigger;
349 }
350
351 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
352 Sset_window_redisplay_end_trigger, 2, 2, 0,
353 "Set WINDOW's redisplay end trigger value to VALUE.\n\
354 VALUE should be a buffer position (typically a marker) or nil.\n\
355 If it is a buffer position, then if redisplay in WINDOW reaches a position\n\
356 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called\n\
357 with two arguments: WINDOW, and the end trigger value.\n\
358 Afterwards the end-trigger value is reset to nil.")
359 (window, value)
360 register Lisp_Object window, value;
361 {
362 register struct window *w;
363
364 w = decode_window (window);
365 w->redisplay_end_trigger = value;
366 return value;
367 }
368
369 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
370 "Return a list of the edge coordinates of WINDOW.\n\
371 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.\n\
372 RIGHT is one more than the rightmost column used by WINDOW,\n\
373 and BOTTOM is one more than the bottommost row used by WINDOW\n\
374 and its mode-line.")
375 (window)
376 Lisp_Object window;
377 {
378 register struct window *w = decode_window (window);
379
380 return Fcons (w->left, Fcons (w->top,
381 Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
382 Fcons (make_number (XFASTINT (w->top)
383 + XFASTINT (w->height)),
384 Qnil))));
385 }
386
387 /* Test if the character at column *x, row *y is within window *w.
388 If it is not, return 0;
389 if it is in the window's text area,
390 set *x and *y to its location relative to the upper left corner
391 of the window, and
392 return 1;
393 if it is on the window's modeline, return 2;
394 if it is on the border between the window and its right sibling,
395 return 3. */
396 static int
397 coordinates_in_window (w, x, y)
398 register struct window *w;
399 register int *x, *y;
400 {
401 register int left = XINT (w->left);
402 register int right_edge = WINDOW_RIGHT_EDGE (w);
403 register int left_margin = WINDOW_LEFT_MARGIN (w);
404 register int right_margin = WINDOW_RIGHT_MARGIN (w);
405 register int window_height = XINT (w->height);
406 register int top = XFASTINT (w->top);
407
408 if ( *x < left || *x >= right_edge
409 || *y < top || *y >= top + window_height)
410 return 0;
411
412 if (left_margin != left && *x < left_margin && *x >= left)
413 return 3;
414
415 if (right_margin != right_edge && *x >= right_margin && *x < right_edge)
416 return 3;
417
418 /* Is the character is the mode line? */
419 if (*y == top + window_height - 1
420 && ! MINI_WINDOW_P (w))
421 return 2;
422
423 *x -= WINDOW_LEFT_MARGIN (w);
424 *y -= top;
425 return 1;
426 }
427
428 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
429 Scoordinates_in_window_p, 2, 2, 0,
430 "Return non-nil if COORDINATES are in WINDOW.\n\
431 COORDINATES is a cons of the form (X . Y), X and Y being distances\n\
432 measured in characters from the upper-left corner of the frame.\n\
433 (0 . 0) denotes the character in the upper left corner of the\n\
434 frame.\n\
435 If COORDINATES are in the text portion of WINDOW,\n\
436 the coordinates relative to the window are returned.\n\
437 If they are in the mode line of WINDOW, `mode-line' is returned.\n\
438 If they are on the border between WINDOW and its right sibling,\n\
439 `vertical-line' is returned.")
440 (coordinates, window)
441 register Lisp_Object coordinates, window;
442 {
443 int x, y;
444
445 CHECK_LIVE_WINDOW (window, 0);
446 CHECK_CONS (coordinates, 1);
447 x = XINT (Fcar (coordinates));
448 y = XINT (Fcdr (coordinates));
449
450 switch (coordinates_in_window (XWINDOW (window), &x, &y))
451 {
452 case 0: /* NOT in window at all. */
453 return Qnil;
454
455 case 1: /* In text part of window. */
456 return Fcons (make_number (x), make_number (y));
457
458 case 2: /* In mode line of window. */
459 return Qmode_line;
460
461 case 3: /* On right border of window. */
462 return Qvertical_line;
463
464 default:
465 abort ();
466 }
467 }
468
469 /* Find the window containing column x, row y, and return it as a
470 Lisp_Object. If x, y is on the window's modeline, set *part
471 to 1; if it is on the separating line between the window and its
472 right sibling, set it to 2; otherwise set it to 0. If there is no
473 window under x, y return nil and leave *part unmodified. */
474 Lisp_Object
475 window_from_coordinates (frame, x, y, part)
476 FRAME_PTR frame;
477 int x, y;
478 int *part;
479 {
480 register Lisp_Object tem, first;
481
482 tem = first = FRAME_SELECTED_WINDOW (frame);
483
484 do
485 {
486 int found = coordinates_in_window (XWINDOW (tem), &x, &y);
487
488 if (found)
489 {
490 *part = found - 1;
491 return tem;
492 }
493
494 tem = Fnext_window (tem, Qt, Qlambda);
495 }
496 while (! EQ (tem, first));
497
498 return Qnil;
499 }
500
501 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
502 "Return window containing coordinates X and Y on FRAME.\n\
503 If omitted, FRAME defaults to the currently selected frame.\n\
504 The top left corner of the frame is considered to be row 0,\n\
505 column 0.")
506 (x, y, frame)
507 Lisp_Object x, y, frame;
508 {
509 int part;
510
511 if (NILP (frame))
512 XSETFRAME (frame, selected_frame);
513 else
514 CHECK_LIVE_FRAME (frame, 2);
515 CHECK_NUMBER (x, 0);
516 CHECK_NUMBER (y, 1);
517
518 return window_from_coordinates (XFRAME (frame),
519 XINT (x), XINT (y),
520 &part);
521 }
522
523 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
524 "Return current value of point in WINDOW.\n\
525 For a nonselected window, this is the value point would have\n\
526 if that window were selected.\n\
527 \n\
528 Note that, when WINDOW is the selected window and its buffer\n\
529 is also currently selected, the value returned is the same as (point).\n\
530 It would be more strictly correct to return the `top-level' value\n\
531 of point, outside of any save-excursion forms.\n\
532 But that is hard to define.")
533 (window)
534 Lisp_Object window;
535 {
536 register struct window *w = decode_window (window);
537
538 if (w == XWINDOW (selected_window)
539 && current_buffer == XBUFFER (w->buffer))
540 return Fpoint ();
541 return Fmarker_position (w->pointm);
542 }
543
544 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
545 "Return position at which display currently starts in WINDOW.\n\
546 This is updated by redisplay or by calling `set-window-start'.")
547 (window)
548 Lisp_Object window;
549 {
550 return Fmarker_position (decode_window (window)->start);
551 }
552
553 /* This is text temporarily removed from the doc string below.
554
555 This function returns nil if the position is not currently known.\n\
556 That happens when redisplay is preempted and doesn't finish.\n\
557 If in that case you want to compute where the end of the window would\n\
558 have been if redisplay had finished, do this:\n\
559 (save-excursion\n\
560 (goto-char (window-start window))\n\
561 (vertical-motion (1- (window-height window)) window)\n\
562 (point))") */
563
564 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
565 "Return position at which display currently ends in WINDOW.\n\
566 This is updated by redisplay, when it runs to completion.\n\
567 Simply changing the buffer text or setting `window-start'\n\
568 does not update this value.\n\
569 If UP-TO-DATE is non-nil, compute the up-to-date position\n\
570 if it isn't already recorded.")
571 (window, update)
572 Lisp_Object window, update;
573 {
574 Lisp_Object value;
575 struct window *w = decode_window (window);
576 Lisp_Object buf;
577
578 buf = w->buffer;
579 CHECK_BUFFER (buf, 0);
580
581 #if 0 /* This change broke some things. We should make it later. */
582 /* If we don't know the end position, return nil.
583 The user can compute it with vertical-motion if he wants to.
584 It would be nicer to do it automatically,
585 but that's so slow that it would probably bother people. */
586 if (NILP (w->window_end_valid))
587 return Qnil;
588 #endif
589
590 if (! NILP (update)
591 && ! (! NILP (w->window_end_valid)
592 && XFASTINT (w->last_modified) >= MODIFF))
593 {
594 int opoint = PT, opoint_byte = PT_BYTE;
595 TEMP_SET_PT_BOTH (XMARKER (w->start)->charpos,
596 XMARKER (w->start)->bytepos);
597 Fvertical_motion (make_number (window_internal_height (w)), Qnil);
598 XSETINT (value, PT);
599 TEMP_SET_PT_BOTH (opoint, opoint_byte);
600 }
601 else
602 XSETINT (value,
603 BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
604
605 return value;
606 }
607
608 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
609 "Make point value in WINDOW be at position POS in WINDOW's buffer.")
610 (window, pos)
611 Lisp_Object window, pos;
612 {
613 register struct window *w = decode_window (window);
614
615 CHECK_NUMBER_COERCE_MARKER (pos, 1);
616 if (w == XWINDOW (selected_window))
617 Fgoto_char (pos);
618 else
619 set_marker_restricted (w->pointm, pos, w->buffer);
620
621 return pos;
622 }
623
624 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
625 "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
626 Optional third arg NOFORCE non-nil inhibits next redisplay\n\
627 from overriding motion of point in order to display at this exact start.")
628 (window, pos, noforce)
629 Lisp_Object window, pos, noforce;
630 {
631 register struct window *w = decode_window (window);
632
633 CHECK_NUMBER_COERCE_MARKER (pos, 1);
634 set_marker_restricted (w->start, pos, w->buffer);
635 /* this is not right, but much easier than doing what is right. */
636 w->start_at_line_beg = Qnil;
637 if (NILP (noforce))
638 w->force_start = Qt;
639 w->update_mode_line = Qt;
640 XSETFASTINT (w->last_modified, 0);
641 XSETFASTINT (w->last_overlay_modified, 0);
642 if (!EQ (window, selected_window))
643 windows_or_buffers_changed++;
644 return pos;
645 }
646
647 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
648 1, 1, 0,
649 "Return WINDOW's dedicated object, usually t or nil.\n\
650 See also `set-window-dedicated-p'.")
651 (window)
652 Lisp_Object window;
653 {
654 return decode_window (window)->dedicated;
655 }
656
657 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
658 Sset_window_dedicated_p, 2, 2, 0,
659 "Control whether WINDOW is dedicated to the buffer it displays.\n\
660 If it is dedicated, Emacs will not automatically change\n\
661 which buffer appears in it.\n\
662 The second argument is the new value for the dedication flag;\n\
663 non-nil means yes.")
664 (window, arg)
665 Lisp_Object window, arg;
666 {
667 register struct window *w = decode_window (window);
668
669 if (NILP (arg))
670 w->dedicated = Qnil;
671 else
672 w->dedicated = Qt;
673
674 return w->dedicated;
675 }
676
677 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
678 0, 1, 0,
679 "Return the display-table that WINDOW is using.")
680 (window)
681 Lisp_Object window;
682 {
683 return decode_window (window)->display_table;
684 }
685
686 /* Get the display table for use currently on window W.
687 This is either W's display table or W's buffer's display table.
688 Ignore the specified tables if they are not valid;
689 if no valid table is specified, return 0. */
690
691 struct Lisp_Char_Table *
692 window_display_table (w)
693 struct window *w;
694 {
695 Lisp_Object tem;
696 tem = w->display_table;
697 if (DISP_TABLE_P (tem))
698 return XCHAR_TABLE (tem);
699 if (NILP (w->buffer))
700 return 0;
701
702 tem = XBUFFER (w->buffer)->display_table;
703 if (DISP_TABLE_P (tem))
704 return XCHAR_TABLE (tem);
705 tem = Vstandard_display_table;
706 if (DISP_TABLE_P (tem))
707 return XCHAR_TABLE (tem);
708 return 0;
709 }
710
711 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
712 "Set WINDOW's display-table to TABLE.")
713 (window, table)
714 register Lisp_Object window, table;
715 {
716 register struct window *w;
717 register Lisp_Object z; /* Return value. */
718
719 w = decode_window (window);
720 w->display_table = table;
721 return table;
722 }
723 \f
724 /* Record info on buffer window w is displaying
725 when it is about to cease to display that buffer. */
726 static
727 unshow_buffer (w)
728 register struct window *w;
729 {
730 Lisp_Object buf;
731 struct buffer *b;
732
733 buf = w->buffer;
734 b = XBUFFER (buf);
735 if (b != XMARKER (w->pointm)->buffer)
736 abort ();
737
738 if (w == XWINDOW (b->last_selected_window))
739 b->last_selected_window = Qnil;
740
741 #if 0
742 if (w == XWINDOW (selected_window)
743 || ! EQ (buf, XWINDOW (selected_window)->buffer))
744 /* Do this except when the selected window's buffer
745 is being removed from some other window. */
746 #endif
747 /* last_window_start records the start position that this buffer
748 had in the last window to be disconnected from it.
749 Now that this statement is unconditional,
750 it is possible for the buffer to be displayed in the
751 selected window, while last_window_start reflects another
752 window which was recently showing the same buffer.
753 Some people might say that might be a good thing. Let's see. */
754 b->last_window_start = marker_position (w->start);
755
756 /* Point in the selected window's buffer
757 is actually stored in that buffer, and the window's pointm isn't used.
758 So don't clobber point in that buffer. */
759 if (! EQ (buf, XWINDOW (selected_window)->buffer))
760 temp_set_point_both (b,
761 clip_to_bounds (BUF_BEGV (b),
762 XMARKER (w->pointm)->charpos,
763 BUF_ZV (b)),
764 clip_to_bounds (BUF_BEGV_BYTE (b),
765 marker_byte_position (w->pointm),
766 BUF_ZV_BYTE (b)));
767 }
768
769 /* Put replacement into the window structure in place of old. */
770 static
771 replace_window (old, replacement)
772 Lisp_Object old, replacement;
773 {
774 register Lisp_Object tem;
775 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
776
777 /* If OLD is its frame's root_window, then replacement is the new
778 root_window for that frame. */
779
780 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
781 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
782
783 p->left = o->left;
784 p->top = o->top;
785 p->width = o->width;
786 p->height = o->height;
787
788 p->next = tem = o->next;
789 if (!NILP (tem))
790 XWINDOW (tem)->prev = replacement;
791
792 p->prev = tem = o->prev;
793 if (!NILP (tem))
794 XWINDOW (tem)->next = replacement;
795
796 p->parent = tem = o->parent;
797 if (!NILP (tem))
798 {
799 if (EQ (XWINDOW (tem)->vchild, old))
800 XWINDOW (tem)->vchild = replacement;
801 if (EQ (XWINDOW (tem)->hchild, old))
802 XWINDOW (tem)->hchild = replacement;
803 }
804
805 /*** Here, if replacement is a vertical combination
806 and so is its new parent, we should make replacement's
807 children be children of that parent instead. ***/
808 }
809
810 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
811 "Remove WINDOW from the display. Default is selected window.")
812 (window)
813 register Lisp_Object window;
814 {
815 delete_window (window);
816
817 if (! NILP (Vwindow_configuration_change_hook)
818 && ! NILP (Vrun_hooks))
819 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
820
821 return Qnil;
822 }
823
824 void
825 delete_window (window)
826 register Lisp_Object window;
827 {
828 register Lisp_Object tem, parent, sib;
829 register struct window *p;
830 register struct window *par;
831
832 /* Because this function is called by other C code on non-leaf
833 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
834 so we can't decode_window here. */
835 if (NILP (window))
836 window = selected_window;
837 else
838 CHECK_WINDOW (window, 0);
839 p = XWINDOW (window);
840
841 /* It's okay to delete an already-deleted window. */
842 if (NILP (p->buffer)
843 && NILP (p->hchild)
844 && NILP (p->vchild))
845 return;
846
847 parent = p->parent;
848 if (NILP (parent))
849 error ("Attempt to delete minibuffer or sole ordinary window");
850 par = XWINDOW (parent);
851
852 windows_or_buffers_changed++;
853 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (p))) = 1;
854
855 /* Are we trying to delete any frame's selected window? */
856 {
857 Lisp_Object frame, pwindow;
858
859 /* See if the frame's selected window is either WINDOW
860 or any subwindow of it, by finding all that window's parents
861 and comparing each one with WINDOW. */
862 frame = WINDOW_FRAME (XWINDOW (window));
863 pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
864
865 while (!NILP (pwindow))
866 {
867 if (EQ (window, pwindow))
868 break;
869 pwindow = XWINDOW (pwindow)->parent;
870 }
871
872 if (EQ (window, pwindow))
873 {
874 Lisp_Object alternative;
875 alternative = Fnext_window (window, Qlambda, Qnil);
876
877 /* If we're about to delete the selected window on the
878 selected frame, then we should use Fselect_window to select
879 the new window. On the other hand, if we're about to
880 delete the selected window on any other frame, we shouldn't do
881 anything but set the frame's selected_window slot. */
882 if (EQ (window, selected_window))
883 Fselect_window (alternative);
884 else
885 FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
886 }
887 }
888
889 tem = p->buffer;
890 /* tem is null for dummy parent windows
891 (which have inferiors but not any contents themselves) */
892 if (!NILP (tem))
893 {
894 unshow_buffer (p);
895 unchain_marker (p->pointm);
896 unchain_marker (p->start);
897 }
898
899 tem = p->next;
900 if (!NILP (tem))
901 XWINDOW (tem)->prev = p->prev;
902
903 tem = p->prev;
904 if (!NILP (tem))
905 XWINDOW (tem)->next = p->next;
906
907 if (EQ (window, par->hchild))
908 par->hchild = p->next;
909 if (EQ (window, par->vchild))
910 par->vchild = p->next;
911
912 /* Find one of our siblings to give our space to. */
913 sib = p->prev;
914 if (NILP (sib))
915 {
916 /* If p gives its space to its next sibling, that sibling needs
917 to have its top/left side pulled back to where p's is.
918 set_window_{height,width} will re-position the sibling's
919 children. */
920 sib = p->next;
921 XWINDOW (sib)->top = p->top;
922 XWINDOW (sib)->left = p->left;
923 }
924
925 /* Stretch that sibling. */
926 if (!NILP (par->vchild))
927 set_window_height (sib,
928 XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
929 1);
930 if (!NILP (par->hchild))
931 set_window_width (sib,
932 XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
933 1);
934
935 /* If parent now has only one child,
936 put the child into the parent's place. */
937 tem = par->hchild;
938 if (NILP (tem))
939 tem = par->vchild;
940 if (NILP (XWINDOW (tem)->next))
941 replace_window (parent, tem);
942
943 /* Since we may be deleting combination windows, we must make sure that
944 not only p but all its children have been marked as deleted. */
945 if (! NILP (p->hchild))
946 delete_all_subwindows (XWINDOW (p->hchild));
947 else if (! NILP (p->vchild))
948 delete_all_subwindows (XWINDOW (p->vchild));
949
950 /* Mark this window as deleted. */
951 p->buffer = p->hchild = p->vchild = Qnil;
952 }
953 \f
954
955 extern Lisp_Object next_frame (), prev_frame ();
956
957 /* This comment supplies the doc string for `next-window',
958 for make-docfile to see. We cannot put this in the real DEFUN
959 due to limits in the Unix cpp.
960
961 DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
962 "Return next window after WINDOW in canonical ordering of windows.\n\
963 If omitted, WINDOW defaults to the selected window.\n\
964 \n\
965 Optional second arg MINIBUF t means count the minibuffer window even\n\
966 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
967 it is active. MINIBUF neither t nor nil means not to count the\n\
968 minibuffer even if it is active.\n\
969 \n\
970 Several frames may share a single minibuffer; if the minibuffer\n\
971 counts, all windows on all frames that share that minibuffer count\n\
972 too. Therefore, `next-window' can be used to iterate through the\n\
973 set of windows even when the minibuffer is on another frame. If the\n\
974 minibuffer does not count, only windows from WINDOW's frame count.\n\
975 \n\
976 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
977 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
978 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
979 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
980 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
981 Anything else means restrict to WINDOW's frame.\n\
982 \n\
983 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
984 `next-window' to iterate through the entire cycle of acceptable\n\
985 windows, eventually ending up back at the window you started with.\n\
986 `previous-window' traverses the same cycle, in the reverse order.")
987 (window, minibuf, all_frames) */
988
989 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
990 0)
991 (window, minibuf, all_frames)
992 register Lisp_Object window, minibuf, all_frames;
993 {
994 register Lisp_Object tem;
995 Lisp_Object start_window;
996
997 if (NILP (window))
998 window = selected_window;
999 else
1000 CHECK_LIVE_WINDOW (window, 0);
1001
1002 start_window = window;
1003
1004 /* minibuf == nil may or may not include minibuffers.
1005 Decide if it does. */
1006 if (NILP (minibuf))
1007 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1008 else if (! EQ (minibuf, Qt))
1009 minibuf = Qlambda;
1010 /* Now minibuf can be t => count all minibuffer windows,
1011 lambda => count none of them,
1012 or a specific minibuffer window (the active one) to count. */
1013
1014 /* all_frames == nil doesn't specify which frames to include. */
1015 if (NILP (all_frames))
1016 all_frames = (! EQ (minibuf, Qlambda)
1017 ? (FRAME_MINIBUF_WINDOW
1018 (XFRAME
1019 (WINDOW_FRAME
1020 (XWINDOW (window)))))
1021 : Qnil);
1022 else if (EQ (all_frames, Qvisible))
1023 ;
1024 else if (XFASTINT (all_frames) == 0)
1025 ;
1026 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1027 /* If all_frames is a frame and window arg isn't on that frame, just
1028 return the first window on the frame. */
1029 return Fframe_first_window (all_frames);
1030 else if (! EQ (all_frames, Qt))
1031 all_frames = Qnil;
1032 /* Now all_frames is t meaning search all frames,
1033 nil meaning search just current frame,
1034 visible meaning search just visible frames,
1035 0 meaning search visible and iconified frames,
1036 or a window, meaning search the frame that window belongs to. */
1037
1038 /* Do this loop at least once, to get the next window, and perhaps
1039 again, if we hit the minibuffer and that is not acceptable. */
1040 do
1041 {
1042 /* Find a window that actually has a next one. This loop
1043 climbs up the tree. */
1044 while (tem = XWINDOW (window)->next, NILP (tem))
1045 if (tem = XWINDOW (window)->parent, !NILP (tem))
1046 window = tem;
1047 else
1048 {
1049 /* We've reached the end of this frame.
1050 Which other frames are acceptable? */
1051 tem = WINDOW_FRAME (XWINDOW (window));
1052 if (! NILP (all_frames))
1053 {
1054 Lisp_Object tem1;
1055
1056 tem1 = tem;
1057 tem = next_frame (tem, all_frames);
1058 /* In the case where the minibuffer is active,
1059 and we include its frame as well as the selected one,
1060 next_frame may get stuck in that frame.
1061 If that happens, go back to the selected frame
1062 so we can complete the cycle. */
1063 if (EQ (tem, tem1))
1064 XSETFRAME (tem, selected_frame);
1065 }
1066 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1067
1068 break;
1069 }
1070
1071 window = tem;
1072
1073 /* If we're in a combination window, find its first child and
1074 recurse on that. Otherwise, we've found the window we want. */
1075 while (1)
1076 {
1077 if (!NILP (XWINDOW (window)->hchild))
1078 window = XWINDOW (window)->hchild;
1079 else if (!NILP (XWINDOW (window)->vchild))
1080 window = XWINDOW (window)->vchild;
1081 else break;
1082 }
1083 }
1084 /* Which windows are acceptable?
1085 Exit the loop and accept this window if
1086 this isn't a minibuffer window,
1087 or we're accepting all minibuffer windows,
1088 or this is the active minibuffer and we are accepting that one, or
1089 we've come all the way around and we're back at the original window. */
1090 while (MINI_WINDOW_P (XWINDOW (window))
1091 && ! EQ (minibuf, Qt)
1092 && ! EQ (minibuf, window)
1093 && ! EQ (window, start_window));
1094
1095 return window;
1096 }
1097
1098 /* This comment supplies the doc string for `previous-window',
1099 for make-docfile to see. We cannot put this in the real DEFUN
1100 due to limits in the Unix cpp.
1101
1102 DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
1103 "Return the window preceding WINDOW in canonical ordering of windows.\n\
1104 If omitted, WINDOW defaults to the selected window.\n\
1105 \n\
1106 Optional second arg MINIBUF t means count the minibuffer window even\n\
1107 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
1108 it is active. MINIBUF neither t nor nil means not to count the\n\
1109 minibuffer even if it is active.\n\
1110 \n\
1111 Several frames may share a single minibuffer; if the minibuffer\n\
1112 counts, all windows on all frames that share that minibuffer count\n\
1113 too. Therefore, `previous-window' can be used to iterate through\n\
1114 the set of windows even when the minibuffer is on another frame. If\n\
1115 the minibuffer does not count, only windows from WINDOW's frame count\n\
1116 \n\
1117 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
1118 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
1119 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
1120 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1121 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
1122 Anything else means restrict to WINDOW's frame.\n\
1123 \n\
1124 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
1125 `previous-window' to iterate through the entire cycle of acceptable\n\
1126 windows, eventually ending up back at the window you started with.\n\
1127 `next-window' traverses the same cycle, in the reverse order.")
1128 (window, minibuf, all_frames) */
1129
1130
1131 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1132 0)
1133 (window, minibuf, all_frames)
1134 register Lisp_Object window, minibuf, all_frames;
1135 {
1136 register Lisp_Object tem;
1137 Lisp_Object start_window;
1138
1139 if (NILP (window))
1140 window = selected_window;
1141 else
1142 CHECK_LIVE_WINDOW (window, 0);
1143
1144 start_window = window;
1145
1146 /* minibuf == nil may or may not include minibuffers.
1147 Decide if it does. */
1148 if (NILP (minibuf))
1149 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1150 else if (! EQ (minibuf, Qt))
1151 minibuf = Qlambda;
1152 /* Now minibuf can be t => count all minibuffer windows,
1153 lambda => count none of them,
1154 or a specific minibuffer window (the active one) to count. */
1155
1156 /* all_frames == nil doesn't specify which frames to include.
1157 Decide which frames it includes. */
1158 if (NILP (all_frames))
1159 all_frames = (! EQ (minibuf, Qlambda)
1160 ? (FRAME_MINIBUF_WINDOW
1161 (XFRAME
1162 (WINDOW_FRAME
1163 (XWINDOW (window)))))
1164 : Qnil);
1165 else if (EQ (all_frames, Qvisible))
1166 ;
1167 else if (XFASTINT (all_frames) == 0)
1168 ;
1169 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1170 /* If all_frames is a frame and window arg isn't on that frame, just
1171 return the first window on the frame. */
1172 return Fframe_first_window (all_frames);
1173 else if (! EQ (all_frames, Qt))
1174 all_frames = Qnil;
1175 /* Now all_frames is t meaning search all frames,
1176 nil meaning search just current frame,
1177 visible meaning search just visible frames,
1178 0 meaning search visible and iconified frames,
1179 or a window, meaning search the frame that window belongs to. */
1180
1181 /* Do this loop at least once, to get the previous window, and perhaps
1182 again, if we hit the minibuffer and that is not acceptable. */
1183 do
1184 {
1185 /* Find a window that actually has a previous one. This loop
1186 climbs up the tree. */
1187 while (tem = XWINDOW (window)->prev, NILP (tem))
1188 if (tem = XWINDOW (window)->parent, !NILP (tem))
1189 window = tem;
1190 else
1191 {
1192 /* We have found the top window on the frame.
1193 Which frames are acceptable? */
1194 tem = WINDOW_FRAME (XWINDOW (window));
1195 if (! NILP (all_frames))
1196 /* It's actually important that we use prev_frame here,
1197 rather than next_frame. All the windows acceptable
1198 according to the given parameters should form a ring;
1199 Fnext_window and Fprevious_window should go back and
1200 forth around the ring. If we use next_frame here,
1201 then Fnext_window and Fprevious_window take different
1202 paths through the set of acceptable windows.
1203 window_loop assumes that these `ring' requirement are
1204 met. */
1205 {
1206 Lisp_Object tem1;
1207
1208 tem1 = tem;
1209 tem = prev_frame (tem, all_frames);
1210 /* In the case where the minibuffer is active,
1211 and we include its frame as well as the selected one,
1212 next_frame may get stuck in that frame.
1213 If that happens, go back to the selected frame
1214 so we can complete the cycle. */
1215 if (EQ (tem, tem1))
1216 XSETFRAME (tem, selected_frame);
1217 }
1218 /* If this frame has a minibuffer, find that window first,
1219 because it is conceptually the last window in that frame. */
1220 if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
1221 tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
1222 else
1223 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1224
1225 break;
1226 }
1227
1228 window = tem;
1229 /* If we're in a combination window, find its last child and
1230 recurse on that. Otherwise, we've found the window we want. */
1231 while (1)
1232 {
1233 if (!NILP (XWINDOW (window)->hchild))
1234 window = XWINDOW (window)->hchild;
1235 else if (!NILP (XWINDOW (window)->vchild))
1236 window = XWINDOW (window)->vchild;
1237 else break;
1238 while (tem = XWINDOW (window)->next, !NILP (tem))
1239 window = tem;
1240 }
1241 }
1242 /* Which windows are acceptable?
1243 Exit the loop and accept this window if
1244 this isn't a minibuffer window,
1245 or we're accepting all minibuffer windows,
1246 or this is the active minibuffer and we are accepting that one, or
1247 we've come all the way around and we're back at the original window. */
1248 while (MINI_WINDOW_P (XWINDOW (window))
1249 && ! EQ (minibuf, Qt)
1250 && ! EQ (minibuf, window)
1251 && ! EQ (window, start_window));
1252
1253 return window;
1254 }
1255
1256 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1257 "Select the ARG'th different window on this frame.\n\
1258 All windows on current frame are arranged in a cyclic order.\n\
1259 This command selects the window ARG steps away in that order.\n\
1260 A negative ARG moves in the opposite order. If the optional second\n\
1261 argument ALL_FRAMES is non-nil, cycle through all frames.")
1262 (arg, all_frames)
1263 register Lisp_Object arg, all_frames;
1264 {
1265 register int i;
1266 register Lisp_Object w;
1267
1268 CHECK_NUMBER (arg, 0);
1269 w = selected_window;
1270 i = XINT (arg);
1271
1272 while (i > 0)
1273 {
1274 w = Fnext_window (w, Qnil, all_frames);
1275 i--;
1276 }
1277 while (i < 0)
1278 {
1279 w = Fprevious_window (w, Qnil, all_frames);
1280 i++;
1281 }
1282 Fselect_window (w);
1283 return Qnil;
1284 }
1285 \f
1286 /* Look at all windows, performing an operation specified by TYPE
1287 with argument OBJ.
1288 If FRAMES is Qt, look at all frames;
1289 Qnil, look at just the selected frame;
1290 Qvisible, look at visible frames;
1291 a frame, just look at windows on that frame.
1292 If MINI is non-zero, perform the operation on minibuffer windows too.
1293 */
1294
1295 enum window_loop
1296 {
1297 WINDOW_LOOP_UNUSED,
1298 GET_BUFFER_WINDOW, /* Arg is buffer */
1299 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1300 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1301 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1302 GET_LARGEST_WINDOW,
1303 UNSHOW_BUFFER /* Arg is buffer */
1304 };
1305
1306 static Lisp_Object
1307 window_loop (type, obj, mini, frames)
1308 enum window_loop type;
1309 register Lisp_Object obj, frames;
1310 int mini;
1311 {
1312 register Lisp_Object w;
1313 register Lisp_Object best_window;
1314 register Lisp_Object next_window;
1315 register Lisp_Object last_window;
1316 FRAME_PTR frame;
1317 Lisp_Object frame_arg;
1318 frame_arg = Qt;
1319
1320 /* If we're only looping through windows on a particular frame,
1321 frame points to that frame. If we're looping through windows
1322 on all frames, frame is 0. */
1323 if (FRAMEP (frames))
1324 frame = XFRAME (frames);
1325 else if (NILP (frames))
1326 frame = selected_frame;
1327 else
1328 frame = 0;
1329 if (frame)
1330 frame_arg = Qlambda;
1331 else if (XFASTINT (frames) == 0)
1332 frame_arg = frames;
1333 else if (EQ (frames, Qvisible))
1334 frame_arg = frames;
1335
1336 /* frame_arg is Qlambda to stick to one frame,
1337 Qvisible to consider all visible frames,
1338 or Qt otherwise. */
1339
1340 /* Pick a window to start with. */
1341 if (WINDOWP (obj))
1342 w = obj;
1343 else if (frame)
1344 w = FRAME_SELECTED_WINDOW (frame);
1345 else
1346 w = FRAME_SELECTED_WINDOW (selected_frame);
1347
1348 /* Figure out the last window we're going to mess with. Since
1349 Fnext_window, given the same options, is guaranteed to go in a
1350 ring, we can just use Fprevious_window to find the last one.
1351
1352 We can't just wait until we hit the first window again, because
1353 it might be deleted. */
1354
1355 last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
1356
1357 best_window = Qnil;
1358 for (;;)
1359 {
1360 FRAME_PTR w_frame = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1361
1362 /* Pick the next window now, since some operations will delete
1363 the current window. */
1364 next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
1365
1366 /* Note that we do not pay attention here to whether
1367 the frame is visible, since Fnext_window skips non-visible frames
1368 if that is desired, under the control of frame_arg. */
1369 if (! MINI_WINDOW_P (XWINDOW (w))
1370 || (mini && minibuf_level > 0))
1371 switch (type)
1372 {
1373 case GET_BUFFER_WINDOW:
1374 if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj)
1375 /* Don't find any minibuffer window
1376 except the one that is currently in use. */
1377 && (MINI_WINDOW_P (XWINDOW (w))
1378 ? EQ (w, minibuf_window) : 1))
1379 return w;
1380 break;
1381
1382 case GET_LRU_WINDOW:
1383 /* t as arg means consider only full-width windows */
1384 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (XWINDOW (w)))
1385 break;
1386 /* Ignore dedicated windows and minibuffers. */
1387 if (MINI_WINDOW_P (XWINDOW (w))
1388 || !NILP (XWINDOW (w)->dedicated))
1389 break;
1390 if (NILP (best_window)
1391 || (XFASTINT (XWINDOW (best_window)->use_time)
1392 > XFASTINT (XWINDOW (w)->use_time)))
1393 best_window = w;
1394 break;
1395
1396 case DELETE_OTHER_WINDOWS:
1397 if (XWINDOW (w) != XWINDOW (obj))
1398 Fdelete_window (w);
1399 break;
1400
1401 case DELETE_BUFFER_WINDOWS:
1402 if (EQ (XWINDOW (w)->buffer, obj))
1403 {
1404 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1405
1406 /* If this window is dedicated, and in a frame of its own,
1407 kill the frame. */
1408 if (EQ (w, FRAME_ROOT_WINDOW (f))
1409 && !NILP (XWINDOW (w)->dedicated)
1410 && other_visible_frames (f))
1411 {
1412 /* Skip the other windows on this frame.
1413 There might be one, the minibuffer! */
1414 if (! EQ (w, last_window))
1415 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1416 {
1417 /* As we go, check for the end of the loop.
1418 We mustn't start going around a second time. */
1419 if (EQ (next_window, last_window))
1420 {
1421 last_window = w;
1422 break;
1423 }
1424 next_window = Fnext_window (next_window,
1425 mini ? Qt : Qnil,
1426 frame_arg);
1427 }
1428 /* Now we can safely delete the frame. */
1429 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1430 }
1431 else
1432 /* If we're deleting the buffer displayed in the only window
1433 on the frame, find a new buffer to display there. */
1434 if (NILP (XWINDOW (w)->parent))
1435 {
1436 Lisp_Object new_buffer;
1437 new_buffer = Fother_buffer (obj, Qnil);
1438 if (NILP (new_buffer))
1439 new_buffer
1440 = Fget_buffer_create (build_string ("*scratch*"));
1441 Fset_window_buffer (w, new_buffer);
1442 if (EQ (w, selected_window))
1443 Fset_buffer (XWINDOW (w)->buffer);
1444 }
1445 else
1446 Fdelete_window (w);
1447 }
1448 break;
1449
1450 case GET_LARGEST_WINDOW:
1451 /* Ignore dedicated windows and minibuffers. */
1452 if (MINI_WINDOW_P (XWINDOW (w))
1453 || !NILP (XWINDOW (w)->dedicated))
1454 break;
1455 {
1456 struct window *best_window_ptr = XWINDOW (best_window);
1457 struct window *w_ptr = XWINDOW (w);
1458 if (NILP (best_window)
1459 || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
1460 > (XFASTINT (best_window_ptr->height)
1461 * XFASTINT (best_window_ptr->width))))
1462 best_window = w;
1463 }
1464 break;
1465
1466 case UNSHOW_BUFFER:
1467 if (EQ (XWINDOW (w)->buffer, obj))
1468 {
1469 /* Find another buffer to show in this window. */
1470 Lisp_Object another_buffer;
1471 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1472 another_buffer = Fother_buffer (obj, Qnil);
1473 if (NILP (another_buffer))
1474 another_buffer
1475 = Fget_buffer_create (build_string ("*scratch*"));
1476 /* If this window is dedicated, and in a frame of its own,
1477 kill the frame. */
1478 if (EQ (w, FRAME_ROOT_WINDOW (f))
1479 && !NILP (XWINDOW (w)->dedicated)
1480 && other_visible_frames (f))
1481 {
1482 /* Skip the other windows on this frame.
1483 There might be one, the minibuffer! */
1484 if (! EQ (w, last_window))
1485 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1486 {
1487 /* As we go, check for the end of the loop.
1488 We mustn't start going around a second time. */
1489 if (EQ (next_window, last_window))
1490 {
1491 last_window = w;
1492 break;
1493 }
1494 next_window = Fnext_window (next_window,
1495 mini ? Qt : Qnil,
1496 frame_arg);
1497 }
1498 /* Now we can safely delete the frame. */
1499 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1500 }
1501 else
1502 {
1503 /* Otherwise show a different buffer in the window. */
1504 XWINDOW (w)->dedicated = Qnil;
1505 Fset_window_buffer (w, another_buffer);
1506 if (EQ (w, selected_window))
1507 Fset_buffer (XWINDOW (w)->buffer);
1508 }
1509 }
1510 break;
1511 }
1512
1513 if (EQ (w, last_window))
1514 break;
1515
1516 w = next_window;
1517 }
1518
1519 return best_window;
1520 }
1521
1522 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
1523 "Return the window least recently selected or used for display.\n\
1524 If optional argument FRAME is `visible', search all visible frames.\n\
1525 If FRAME is 0, search all visible and iconified frames.\n\
1526 If FRAME is t, search all frames.\n\
1527 If FRAME is nil, search only the selected frame.\n\
1528 If FRAME is a frame, search only that frame.")
1529 (frame)
1530 Lisp_Object frame;
1531 {
1532 register Lisp_Object w;
1533 /* First try for a window that is full-width */
1534 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
1535 if (!NILP (w) && !EQ (w, selected_window))
1536 return w;
1537 /* If none of them, try the rest */
1538 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
1539 }
1540
1541 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
1542 "Return the largest window in area.\n\
1543 If optional argument FRAME is `visible', search all visible frames.\n\
1544 If FRAME is 0, search all visible and iconified frames.\n\
1545 If FRAME is t, search all frames.\n\
1546 If FRAME is nil, search only the selected frame.\n\
1547 If FRAME is a frame, search only that frame.")
1548 (frame)
1549 Lisp_Object frame;
1550 {
1551 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
1552 frame);
1553 }
1554
1555 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
1556 "Return a window currently displaying BUFFER, or nil if none.\n\
1557 If optional argument FRAME is `visible', search all visible frames.\n\
1558 If optional argument FRAME is 0, search all visible and iconified frames.\n\
1559 If FRAME is t, search all frames.\n\
1560 If FRAME is nil, search only the selected frame.\n\
1561 If FRAME is a frame, search only that frame.")
1562 (buffer, frame)
1563 Lisp_Object buffer, frame;
1564 {
1565 buffer = Fget_buffer (buffer);
1566 if (BUFFERP (buffer))
1567 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
1568 else
1569 return Qnil;
1570 }
1571
1572 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
1573 0, 1, "",
1574 "Make WINDOW (or the selected window) fill its frame.\n\
1575 Only the frame WINDOW is on is affected.\n\
1576 This function tries to reduce display jumps\n\
1577 by keeping the text previously visible in WINDOW\n\
1578 in the same place on the frame. Doing this depends on\n\
1579 the value of (window-start WINDOW), so if calling this function\n\
1580 in a program gives strange scrolling, make sure the window-start\n\
1581 value is reasonable when this function is called.")
1582 (window)
1583 Lisp_Object window;
1584 {
1585 struct window *w;
1586 int startpos;
1587 int top;
1588
1589 if (NILP (window))
1590 window = selected_window;
1591 else
1592 CHECK_LIVE_WINDOW (window, 0);
1593
1594 w = XWINDOW (window);
1595
1596 startpos = marker_position (w->start);
1597 top = XFASTINT (w->top) - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w)));
1598
1599 if (MINI_WINDOW_P (w) && top > 0)
1600 error ("Can't expand minibuffer to full frame");
1601
1602 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
1603
1604 /* Try to minimize scrolling, by setting the window start to the point
1605 will cause the text at the old window start to be at the same place
1606 on the frame. But don't try to do this if the window start is
1607 outside the visible portion (as might happen when the display is
1608 not current, due to typeahead). */
1609 if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
1610 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
1611 {
1612 struct position pos;
1613 struct buffer *obuf = current_buffer;
1614
1615 Fset_buffer (w->buffer);
1616 /* This computation used to temporarily move point, but that can
1617 have unwanted side effects due to text properties. */
1618 pos = *vmotion (startpos, -top, w);
1619
1620 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
1621 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
1622 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
1623 : Qnil);
1624 /* We need to do this, so that the window-scroll-functions
1625 get called. */
1626 w->optional_new_start = Qt;
1627
1628 set_buffer_internal (obuf);
1629 }
1630 return Qnil;
1631 }
1632
1633 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
1634 1, 2, "bDelete windows on (buffer): ",
1635 "Delete all windows showing BUFFER.\n\
1636 Optional second argument FRAME controls which frames are affected.\n\
1637 If nil or omitted, delete all windows showing BUFFER in any frame.\n\
1638 If t, delete only windows showing BUFFER in the selected frame.\n\
1639 If `visible', delete all windows showing BUFFER in any visible frame.\n\
1640 If a frame, delete only windows showing BUFFER in that frame.")
1641 (buffer, frame)
1642 Lisp_Object buffer, frame;
1643 {
1644 /* FRAME uses t and nil to mean the opposite of what window_loop
1645 expects. */
1646 if (! FRAMEP (frame))
1647 frame = NILP (frame) ? Qt : Qnil;
1648
1649 if (!NILP (buffer))
1650 {
1651 buffer = Fget_buffer (buffer);
1652 CHECK_BUFFER (buffer, 0);
1653 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
1654 }
1655 return Qnil;
1656 }
1657
1658 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
1659 Sreplace_buffer_in_windows,
1660 1, 1, "bReplace buffer in windows: ",
1661 "Replace BUFFER with some other buffer in all windows showing it.")
1662 (buffer)
1663 Lisp_Object buffer;
1664 {
1665 if (!NILP (buffer))
1666 {
1667 buffer = Fget_buffer (buffer);
1668 CHECK_BUFFER (buffer, 0);
1669 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
1670 }
1671 return Qnil;
1672 }
1673
1674 /* Replace BUFFER with some other buffer in all windows
1675 of all frames, even those on other keyboards. */
1676
1677 void
1678 replace_buffer_in_all_windows (buffer)
1679 Lisp_Object buffer;
1680 {
1681 #ifdef MULTI_KBOARD
1682 Lisp_Object tail, frame;
1683
1684 /* A single call to window_loop won't do the job
1685 because it only considers frames on the current keyboard.
1686 So loop manually over frames, and handle each one. */
1687 FOR_EACH_FRAME (tail, frame)
1688 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
1689 #else
1690 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
1691 #endif
1692 }
1693 \f
1694 /* Set the height of WINDOW and all its inferiors. */
1695
1696 /* The smallest acceptable dimensions for a window. Anything smaller
1697 might crash Emacs. */
1698 #define MIN_SAFE_WINDOW_WIDTH (2)
1699 #define MIN_SAFE_WINDOW_HEIGHT (2)
1700
1701 /* Make sure that window_min_height and window_min_width are
1702 not too small; if they are, set them to safe minima. */
1703
1704 static void
1705 check_min_window_sizes ()
1706 {
1707 /* Smaller values might permit a crash. */
1708 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
1709 window_min_width = MIN_SAFE_WINDOW_WIDTH;
1710 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
1711 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
1712 }
1713
1714 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
1715 minimum allowable size. */
1716 void
1717 check_frame_size (frame, rows, cols)
1718 FRAME_PTR frame;
1719 int *rows, *cols;
1720 {
1721 /* For height, we have to see:
1722 whether the frame has a minibuffer,
1723 whether it wants a mode line, and
1724 whether it has a menu bar. */
1725 int min_height =
1726 (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
1727 : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
1728 : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
1729 if (FRAME_MENU_BAR_LINES (frame) > 0)
1730 min_height += FRAME_MENU_BAR_LINES (frame);
1731
1732 if (*rows < min_height)
1733 *rows = min_height;
1734 if (*cols < MIN_SAFE_WINDOW_WIDTH)
1735 *cols = MIN_SAFE_WINDOW_WIDTH;
1736 }
1737
1738 /* Normally the window is deleted if it gets too small.
1739 nodelete nonzero means do not do this.
1740 (The caller should check later and do so if appropriate) */
1741
1742 void
1743 set_window_height (window, height, nodelete)
1744 Lisp_Object window;
1745 int height;
1746 int nodelete;
1747 {
1748 register struct window *w = XWINDOW (window);
1749 register struct window *c;
1750 int oheight = XFASTINT (w->height);
1751 int top, pos, lastbot, opos, lastobot;
1752 Lisp_Object child;
1753
1754 check_min_window_sizes ();
1755
1756 if (!nodelete
1757 && ! NILP (w->parent)
1758 && (MINI_WINDOW_P (w)
1759 ? height < 1
1760 : height < window_min_height))
1761 {
1762 delete_window (window);
1763 return;
1764 }
1765
1766 XSETFASTINT (w->last_modified, 0);
1767 XSETFASTINT (w->last_overlay_modified, 0);
1768 windows_or_buffers_changed++;
1769 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1770
1771 XSETFASTINT (w->height, height);
1772 if (!NILP (w->hchild))
1773 {
1774 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
1775 {
1776 XWINDOW (child)->top = w->top;
1777 set_window_height (child, height, nodelete);
1778 }
1779 }
1780 else if (!NILP (w->vchild))
1781 {
1782 lastbot = top = XFASTINT (w->top);
1783 lastobot = 0;
1784 for (child = w->vchild; !NILP (child); child = c->next)
1785 {
1786 c = XWINDOW (child);
1787
1788 opos = lastobot + XFASTINT (c->height);
1789
1790 XSETFASTINT (c->top, lastbot);
1791
1792 pos = (((opos * height) << 1) + oheight) / (oheight << 1);
1793
1794 /* Avoid confusion: inhibit deletion of child if becomes too small */
1795 set_window_height (child, pos + top - lastbot, 1);
1796
1797 /* Now advance child to next window,
1798 and set lastbot if child was not just deleted. */
1799 lastbot = pos + top;
1800 lastobot = opos;
1801 }
1802 /* Now delete any children that became too small. */
1803 if (!nodelete)
1804 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
1805 {
1806 set_window_height (child, XINT (XWINDOW (child)->height), 0);
1807 }
1808 }
1809 }
1810
1811 /* Recursively set width of WINDOW and its inferiors. */
1812
1813 void
1814 set_window_width (window, width, nodelete)
1815 Lisp_Object window;
1816 int width;
1817 int nodelete;
1818 {
1819 register struct window *w = XWINDOW (window);
1820 register struct window *c;
1821 int owidth = XFASTINT (w->width);
1822 int left, pos, lastright, opos, lastoright;
1823 Lisp_Object child;
1824
1825 if (!nodelete && width < window_min_width && !NILP (w->parent))
1826 {
1827 delete_window (window);
1828 return;
1829 }
1830
1831 XSETFASTINT (w->last_modified, 0);
1832 XSETFASTINT (w->last_overlay_modified, 0);
1833 windows_or_buffers_changed++;
1834 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1835
1836 XSETFASTINT (w->width, width);
1837 if (!NILP (w->vchild))
1838 {
1839 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
1840 {
1841 XWINDOW (child)->left = w->left;
1842 set_window_width (child, width, nodelete);
1843 }
1844 }
1845 else if (!NILP (w->hchild))
1846 {
1847 lastright = left = XFASTINT (w->left);
1848 lastoright = 0;
1849 for (child = w->hchild; !NILP (child); child = c->next)
1850 {
1851 c = XWINDOW (child);
1852
1853 opos = lastoright + XFASTINT (c->width);
1854
1855 XSETFASTINT (c->left, lastright);
1856
1857 pos = (((opos * width) << 1) + owidth) / (owidth << 1);
1858
1859 /* Inhibit deletion for becoming too small */
1860 set_window_width (child, pos + left - lastright, 1);
1861
1862 /* Now advance child to next window,
1863 and set lastright if child was not just deleted. */
1864 lastright = pos + left, lastoright = opos;
1865 }
1866 /* Delete children that became too small */
1867 if (!nodelete)
1868 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
1869 {
1870 set_window_width (child, XINT (XWINDOW (child)->width), 0);
1871 }
1872 }
1873 }
1874 \f
1875 int window_select_count;
1876
1877 Lisp_Object
1878 Fset_window_buffer_unwind (obuf)
1879 Lisp_Object obuf;
1880 {
1881 Fset_buffer (obuf);
1882 return Qnil;
1883 }
1884
1885 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
1886 "Make WINDOW display BUFFER as its contents.\n\
1887 BUFFER can be a buffer or buffer name.")
1888 (window, buffer)
1889 register Lisp_Object window, buffer;
1890 {
1891 register Lisp_Object tem;
1892 register struct window *w = decode_window (window);
1893 int count = specpdl_ptr - specpdl;
1894
1895 buffer = Fget_buffer (buffer);
1896 CHECK_BUFFER (buffer, 1);
1897
1898 if (NILP (XBUFFER (buffer)->name))
1899 error ("Attempt to display deleted buffer");
1900
1901 tem = w->buffer;
1902 if (NILP (tem))
1903 error ("Window is deleted");
1904 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
1905 is first being set up. */
1906 {
1907 if (!NILP (w->dedicated) && !EQ (tem, buffer))
1908 error ("Window is dedicated to `%s'",
1909 XSTRING (XBUFFER (tem)->name)->data);
1910
1911 unshow_buffer (w);
1912 }
1913
1914 w->buffer = buffer;
1915
1916 if (EQ (window, selected_window))
1917 XBUFFER (w->buffer)->last_selected_window = window;
1918 if (INTEGERP (XBUFFER (buffer)->display_count))
1919 XSETINT (XBUFFER (buffer)->display_count,
1920 XINT (XBUFFER (buffer)->display_count) + 1);
1921
1922 XSETFASTINT (w->window_end_pos, 0);
1923 w->window_end_valid = Qnil;
1924 XSETFASTINT (w->hscroll, 0);
1925 set_marker_both (w->pointm, buffer,
1926 BUF_PT (XBUFFER (buffer)), BUF_PT_BYTE (XBUFFER (buffer)));
1927 set_marker_restricted (w->start,
1928 make_number (XBUFFER (buffer)->last_window_start),
1929 buffer);
1930 w->start_at_line_beg = Qnil;
1931 w->force_start = Qnil;
1932 XSETFASTINT (w->last_modified, 0);
1933 XSETFASTINT (w->last_overlay_modified, 0);
1934 windows_or_buffers_changed++;
1935
1936 /* We must select BUFFER for running the window-scroll-functions.
1937 If WINDOW is selected, switch permanently.
1938 Otherwise, switch but go back to the ambient buffer afterward. */
1939 if (EQ (window, selected_window))
1940 Fset_buffer (buffer);
1941 /* We can't check ! NILP (Vwindow_scroll_functions) here
1942 because that might itself be a local variable. */
1943 else if (window_initialized)
1944 {
1945 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
1946 Fset_buffer (buffer);
1947 }
1948
1949 if (! NILP (Vwindow_scroll_functions))
1950 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1951 Fmarker_position (w->start));
1952
1953 if (! NILP (Vwindow_configuration_change_hook)
1954 && ! NILP (Vrun_hooks))
1955 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1956
1957 unbind_to (count, Qnil);
1958
1959 return Qnil;
1960 }
1961
1962 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
1963 "Select WINDOW. Most editing will apply to WINDOW's buffer.\n\
1964 The main editor command loop selects the buffer of the selected window\n\
1965 before each command.")
1966 (window)
1967 register Lisp_Object window;
1968 {
1969 register struct window *w;
1970 register struct window *ow = XWINDOW (selected_window);
1971
1972 CHECK_LIVE_WINDOW (window, 0);
1973
1974 w = XWINDOW (window);
1975
1976 if (NILP (w->buffer))
1977 error ("Trying to select deleted window or non-leaf window");
1978
1979 XSETFASTINT (w->use_time, ++window_select_count);
1980 if (EQ (window, selected_window))
1981 return window;
1982
1983 if (! NILP (ow->buffer))
1984 set_marker_both (ow->pointm, ow->buffer,
1985 BUF_PT (XBUFFER (ow->buffer)),
1986 BUF_PT_BYTE (XBUFFER (ow->buffer)));
1987
1988 selected_window = window;
1989 if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
1990 {
1991 XFRAME (WINDOW_FRAME (w))->selected_window = window;
1992 /* Use this rather than Fhandle_switch_frame
1993 so that FRAME_FOCUS_FRAME is moved appropriately as we
1994 move around in the state where a minibuffer in a separate
1995 frame is active. */
1996 Fselect_frame (WINDOW_FRAME (w), Qnil);
1997 }
1998 else
1999 selected_frame->selected_window = window;
2000
2001 record_buffer (w->buffer);
2002 Fset_buffer (w->buffer);
2003
2004 XBUFFER (w->buffer)->last_selected_window = window;
2005
2006 /* Go to the point recorded in the window.
2007 This is important when the buffer is in more
2008 than one window. It also matters when
2009 redisplay_window has altered point after scrolling,
2010 because it makes the change only in the window. */
2011 {
2012 register int new_point = marker_position (w->pointm);
2013 if (new_point < BEGV)
2014 SET_PT (BEGV);
2015 else if (new_point > ZV)
2016 SET_PT (ZV);
2017 else
2018 SET_PT (new_point);
2019 }
2020
2021 windows_or_buffers_changed++;
2022 return window;
2023 }
2024
2025 /* Deiconify the frame containing the window WINDOW,
2026 unless it is the selected frame;
2027 then return WINDOW.
2028
2029 The reason for the exception for the selected frame
2030 is that it seems better not to change the selected frames visibility
2031 merely because of displaying a different buffer in it.
2032 The deiconification is useful when a buffer gets shown in
2033 another frame that you were not using lately. */
2034
2035 static Lisp_Object
2036 display_buffer_1 (window)
2037 Lisp_Object window;
2038 {
2039 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
2040 FRAME_SAMPLE_VISIBILITY (f);
2041 if (f != selected_frame)
2042 {
2043 if (FRAME_ICONIFIED_P (f))
2044 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2045 else if (FRAME_VISIBLE_P (f))
2046 Fraise_frame (WINDOW_FRAME (XWINDOW (window)));
2047 }
2048 return window;
2049 }
2050
2051 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
2052 "Returns non-nil if a buffer named BUFFER-NAME would be created specially.\n\
2053 The value is actually t if the frame should be called with default frame\n\
2054 parameters, and a list of frame parameters if they were specified.\n\
2055 See `special-display-buffer-names', and `special-display-regexps'.")
2056 (buffer_name)
2057 Lisp_Object buffer_name;
2058 {
2059 Lisp_Object tem;
2060
2061 CHECK_STRING (buffer_name, 1);
2062
2063 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
2064 if (!NILP (tem))
2065 return Qt;
2066
2067 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
2068 if (!NILP (tem))
2069 return XCDR (tem);
2070
2071 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
2072 {
2073 Lisp_Object car = XCAR (tem);
2074 if (STRINGP (car)
2075 && fast_string_match (car, buffer_name) >= 0)
2076 return Qt;
2077 else if (CONSP (car)
2078 && STRINGP (XCAR (car))
2079 && fast_string_match (XCAR (car), buffer_name) >= 0)
2080 return XCDR (car);
2081 }
2082 return Qnil;
2083 }
2084
2085 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
2086 "Returns non-nil if a new buffer named BUFFER-NAME would use the same window.\n\
2087 See `same-window-buffer-names' and `same-window-regexps'.")
2088 (buffer_name)
2089 Lisp_Object buffer_name;
2090 {
2091 Lisp_Object tem;
2092
2093 CHECK_STRING (buffer_name, 1);
2094
2095 tem = Fmember (buffer_name, Vsame_window_buffer_names);
2096 if (!NILP (tem))
2097 return Qt;
2098
2099 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
2100 if (!NILP (tem))
2101 return Qt;
2102
2103 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
2104 {
2105 Lisp_Object car = XCAR (tem);
2106 if (STRINGP (car)
2107 && fast_string_match (car, buffer_name) >= 0)
2108 return Qt;
2109 else if (CONSP (car)
2110 && STRINGP (XCAR (car))
2111 && fast_string_match (XCAR (car), buffer_name) >= 0)
2112 return Qt;
2113 }
2114 return Qnil;
2115 }
2116
2117 /* Use B so the default is (other-buffer). */
2118 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
2119 "BDisplay buffer: \nP",
2120 "Make BUFFER appear in some window but don't select it.\n\
2121 BUFFER can be a buffer or a buffer name.\n\
2122 If BUFFER is shown already in some window, just use that one,\n\
2123 unless the window is the selected window and the optional second\n\
2124 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
2125 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
2126 Returns the window displaying BUFFER.\n\
2127 \n\
2128 The variables `special-display-buffer-names', `special-display-regexps',\n\
2129 `same-window-buffer-names', and `same-window-regexps' customize how certain\n\
2130 buffer names are handled.\n\
2131 \n\
2132 If optional argument FRAME is `visible', search all visible frames.\n\
2133 If FRAME is 0, search all visible and iconified frames.\n\
2134 If FRAME is t, search all frames.\n\
2135 If FRAME is a frame, search only that frame.\n\
2136 If FRAME is nil, search only the selected frame\n\
2137 (actually the last nonminibuffer frame),\n\
2138 unless `pop-up-frames' is non-nil,\n\
2139 which means search visible and iconified frames.")
2140 (buffer, not_this_window, frame)
2141 register Lisp_Object buffer, not_this_window, frame;
2142 {
2143 register Lisp_Object window, tem;
2144
2145 buffer = Fget_buffer (buffer);
2146 CHECK_BUFFER (buffer, 0);
2147
2148 if (!NILP (Vdisplay_buffer_function))
2149 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2150
2151 if (NILP (not_this_window)
2152 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
2153 return display_buffer_1 (selected_window);
2154
2155 /* See if the user has specified this buffer should appear
2156 in the selected window. */
2157 if (NILP (not_this_window))
2158 {
2159 tem = Fsame_window_p (XBUFFER (buffer)->name);
2160 if (!NILP (tem))
2161 {
2162 Fswitch_to_buffer (buffer, Qnil);
2163 return display_buffer_1 (selected_window);
2164 }
2165 }
2166
2167 /* If pop_up_frames,
2168 look for a window showing BUFFER on any visible or iconified frame.
2169 Otherwise search only the current frame. */
2170 if (! NILP (frame))
2171 tem = frame;
2172 else if (pop_up_frames || last_nonminibuf_frame == 0)
2173 XSETFASTINT (tem, 0);
2174 else
2175 XSETFRAME (tem, last_nonminibuf_frame);
2176 window = Fget_buffer_window (buffer, tem);
2177 if (!NILP (window)
2178 && (NILP (not_this_window) || !EQ (window, selected_window)))
2179 {
2180 return display_buffer_1 (window);
2181 }
2182
2183 /* Certain buffer names get special handling. */
2184 if (!NILP (Vspecial_display_function))
2185 {
2186 tem = Fspecial_display_p (XBUFFER (buffer)->name);
2187 if (EQ (tem, Qt))
2188 return call1 (Vspecial_display_function, buffer);
2189 if (CONSP (tem))
2190 return call2 (Vspecial_display_function, buffer, tem);
2191 }
2192
2193 /* If there are no frames open that have more than a minibuffer,
2194 we need to create a new frame. */
2195 if (pop_up_frames || last_nonminibuf_frame == 0)
2196 {
2197 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2198 Fset_window_buffer (window, buffer);
2199 return display_buffer_1 (window);
2200 }
2201
2202 if (pop_up_windows
2203 || FRAME_MINIBUF_ONLY_P (selected_frame)
2204 /* If the current frame is a special display frame,
2205 don't try to reuse its windows. */
2206 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->dedicated)
2207 )
2208 {
2209 Lisp_Object frames;
2210
2211 frames = Qnil;
2212 if (FRAME_MINIBUF_ONLY_P (selected_frame))
2213 XSETFRAME (frames, last_nonminibuf_frame);
2214 /* Don't try to create a window if would get an error */
2215 if (split_height_threshold < window_min_height << 1)
2216 split_height_threshold = window_min_height << 1;
2217
2218 /* Note that both Fget_largest_window and Fget_lru_window
2219 ignore minibuffers and dedicated windows.
2220 This means they can return nil. */
2221
2222 /* If the frame we would try to split cannot be split,
2223 try other frames. */
2224 if (FRAME_NO_SPLIT_P (NILP (frames) ? selected_frame
2225 : last_nonminibuf_frame))
2226 {
2227 /* Try visible frames first. */
2228 window = Fget_largest_window (Qvisible);
2229 /* If that didn't work, try iconified frames. */
2230 if (NILP (window))
2231 window = Fget_largest_window (make_number (0));
2232 if (NILP (window))
2233 window = Fget_largest_window (Qt);
2234 }
2235 else
2236 window = Fget_largest_window (frames);
2237
2238 /* If we got a tall enough full-width window that can be split,
2239 split it. */
2240 if (!NILP (window)
2241 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2242 && window_height (window) >= split_height_threshold
2243 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
2244 window = Fsplit_window (window, Qnil, Qnil);
2245 else
2246 {
2247 Lisp_Object upper, lower, other;
2248
2249 window = Fget_lru_window (frames);
2250 /* If the LRU window is selected, and big enough,
2251 and can be split, split it. */
2252 if (!NILP (window)
2253 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2254 && (EQ (window, selected_window)
2255 || EQ (XWINDOW (window)->parent, Qnil))
2256 && window_height (window) >= window_min_height << 1)
2257 window = Fsplit_window (window, Qnil, Qnil);
2258 /* If Fget_lru_window returned nil, try other approaches. */
2259
2260 /* Try visible frames first. */
2261 if (NILP (window))
2262 window = Fget_buffer_window (buffer, Qvisible);
2263 if (NILP (window))
2264 window = Fget_largest_window (Qvisible);
2265 /* If that didn't work, try iconified frames. */
2266 if (NILP (window))
2267 window = Fget_buffer_window (buffer, make_number (0));
2268 if (NILP (window))
2269 window = Fget_largest_window (make_number (0));
2270 /* Try invisible frames. */
2271 if (NILP (window))
2272 window = Fget_buffer_window (buffer, Qt);
2273 if (NILP (window))
2274 window = Fget_largest_window (Qt);
2275 /* As a last resort, make a new frame. */
2276 if (NILP (window))
2277 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2278 /* If window appears above or below another,
2279 even out their heights. */
2280 other = upper = lower = Qnil;
2281 if (!NILP (XWINDOW (window)->prev))
2282 other = upper = XWINDOW (window)->prev, lower = window;
2283 if (!NILP (XWINDOW (window)->next))
2284 other = lower = XWINDOW (window)->next, upper = window;
2285 if (!NILP (other)
2286 /* Check that OTHER and WINDOW are vertically arrayed. */
2287 && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
2288 && (XFASTINT (XWINDOW (other)->height)
2289 > XFASTINT (XWINDOW (window)->height)))
2290 {
2291 int total = (XFASTINT (XWINDOW (other)->height)
2292 + XFASTINT (XWINDOW (window)->height));
2293 Lisp_Object old_selected_window;
2294 old_selected_window = selected_window;
2295
2296 selected_window = upper;
2297 change_window_height ((total / 2
2298 - XFASTINT (XWINDOW (upper)->height)),
2299 0);
2300 selected_window = old_selected_window;
2301 }
2302 }
2303 }
2304 else
2305 window = Fget_lru_window (Qnil);
2306
2307 Fset_window_buffer (window, buffer);
2308 return display_buffer_1 (window);
2309 }
2310
2311 void
2312 temp_output_buffer_show (buf)
2313 register Lisp_Object buf;
2314 {
2315 register struct buffer *old = current_buffer;
2316 register Lisp_Object window;
2317 register struct window *w;
2318
2319 XBUFFER (buf)->directory = current_buffer->directory;
2320
2321 Fset_buffer (buf);
2322 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
2323 BEGV = BEG;
2324 ZV = Z;
2325 SET_PT (BEG);
2326 XBUFFER (buf)->clip_changed = 1;
2327 set_buffer_internal (old);
2328
2329 if (!EQ (Vtemp_buffer_show_function, Qnil))
2330 call1 (Vtemp_buffer_show_function, buf);
2331 else
2332 {
2333 window = Fdisplay_buffer (buf, Qnil, Qnil);
2334
2335 if (XFRAME (XWINDOW (window)->frame) != selected_frame)
2336 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2337 Vminibuf_scroll_window = window;
2338 w = XWINDOW (window);
2339 XSETFASTINT (w->hscroll, 0);
2340 set_marker_restricted_both (w->start, buf, 1, 1);
2341 set_marker_restricted_both (w->pointm, buf, 1, 1);
2342
2343 /* Run temp-buffer-show-hook, with the chosen window selected. */
2344 if (!NILP (Vrun_hooks))
2345 {
2346 Lisp_Object tem;
2347 tem = Fboundp (Qtemp_buffer_show_hook);
2348 if (!NILP (tem))
2349 {
2350 tem = Fsymbol_value (Qtemp_buffer_show_hook);
2351 if (!NILP (tem))
2352 {
2353 int count = specpdl_ptr - specpdl;
2354
2355 /* Select the window that was chosen, for running the hook. */
2356 record_unwind_protect (Fset_window_configuration,
2357 Fcurrent_window_configuration (Qnil));
2358
2359 Fselect_window (window);
2360 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
2361 unbind_to (count, Qnil);
2362 }
2363 }
2364 }
2365 }
2366 }
2367 \f
2368 static
2369 make_dummy_parent (window)
2370 Lisp_Object window;
2371 {
2372 Lisp_Object new;
2373 register struct window *o, *p;
2374 register struct Lisp_Vector *vec;
2375 int i;
2376
2377 o = XWINDOW (window);
2378 vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
2379 for (i = 0; i < VECSIZE (struct window); ++i)
2380 vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
2381 vec->size = VECSIZE (struct window);
2382 p = (struct window *)vec;
2383 XSETWINDOW (new, p);
2384
2385 XSETFASTINT (p->sequence_number, ++sequence_number);
2386
2387 /* Put new into window structure in place of window */
2388 replace_window (window, new);
2389
2390 o->next = Qnil;
2391 o->prev = Qnil;
2392 o->vchild = Qnil;
2393 o->hchild = Qnil;
2394 o->parent = new;
2395
2396 p->start = Qnil;
2397 p->pointm = Qnil;
2398 p->buffer = Qnil;
2399 }
2400
2401 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
2402 "Split WINDOW, putting SIZE lines in the first of the pair.\n\
2403 WINDOW defaults to selected one and SIZE to half its size.\n\
2404 If optional third arg HORFLAG is non-nil, split side by side\n\
2405 and put SIZE columns in the first of the pair.")
2406 (window, size, horflag)
2407 Lisp_Object window, size, horflag;
2408 {
2409 register Lisp_Object new;
2410 register struct window *o, *p;
2411 FRAME_PTR fo;
2412 register int size_int;
2413
2414 if (NILP (window))
2415 window = selected_window;
2416 else
2417 CHECK_LIVE_WINDOW (window, 0);
2418
2419 o = XWINDOW (window);
2420 fo = XFRAME (WINDOW_FRAME (o));
2421
2422 if (NILP (size))
2423 {
2424 if (!NILP (horflag))
2425 /* Calculate the size of the left-hand window, by dividing
2426 the usable space in columns by two. */
2427 size_int = XFASTINT (o->width) >> 1;
2428 else
2429 size_int = XFASTINT (o->height) >> 1;
2430 }
2431 else
2432 {
2433 CHECK_NUMBER (size, 1);
2434 size_int = XINT (size);
2435 }
2436
2437 if (MINI_WINDOW_P (o))
2438 error ("Attempt to split minibuffer window");
2439
2440 check_min_window_sizes ();
2441
2442 if (NILP (horflag))
2443 {
2444 if (size_int < window_min_height)
2445 error ("Window height %d too small (after splitting)", size_int);
2446 if (size_int + window_min_height > XFASTINT (o->height))
2447 error ("Window height %d too small (after splitting)",
2448 XFASTINT (o->height) - size_int);
2449 if (NILP (o->parent)
2450 || NILP (XWINDOW (o->parent)->vchild))
2451 {
2452 make_dummy_parent (window);
2453 new = o->parent;
2454 XWINDOW (new)->vchild = window;
2455 }
2456 }
2457 else
2458 {
2459 if (size_int < window_min_width)
2460 error ("Window width %d too small (after splitting)", size_int);
2461
2462 if (size_int + window_min_width > XFASTINT (o->width))
2463 error ("Window width %d too small (after splitting)",
2464 XFASTINT (o->width) - size_int);
2465 if (NILP (o->parent)
2466 || NILP (XWINDOW (o->parent)->hchild))
2467 {
2468 make_dummy_parent (window);
2469 new = o->parent;
2470 XWINDOW (new)->hchild = window;
2471 }
2472 }
2473
2474 /* Now we know that window's parent is a vertical combination
2475 if we are dividing vertically, or a horizontal combination
2476 if we are making side-by-side windows */
2477
2478 windows_or_buffers_changed++;
2479 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
2480 new = make_window ();
2481 p = XWINDOW (new);
2482
2483 p->frame = o->frame;
2484 p->next = o->next;
2485 if (!NILP (p->next))
2486 XWINDOW (p->next)->prev = new;
2487 p->prev = window;
2488 o->next = new;
2489 p->parent = o->parent;
2490 p->buffer = Qt;
2491
2492 /* Apportion the available frame space among the two new windows */
2493
2494 if (!NILP (horflag))
2495 {
2496 p->height = o->height;
2497 p->top = o->top;
2498 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
2499 XSETFASTINT (o->width, size_int);
2500 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
2501 }
2502 else
2503 {
2504 p->left = o->left;
2505 p->width = o->width;
2506 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
2507 XSETFASTINT (o->height, size_int);
2508 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
2509 }
2510
2511 Fset_window_buffer (new, o->buffer);
2512
2513 return new;
2514 }
2515 \f
2516 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
2517 "Make current window ARG lines bigger.\n\
2518 From program, optional second arg non-nil means grow sideways ARG columns.")
2519 (arg, side)
2520 register Lisp_Object arg, side;
2521 {
2522 CHECK_NUMBER (arg, 0);
2523 change_window_height (XINT (arg), !NILP (side));
2524
2525 if (! NILP (Vwindow_configuration_change_hook))
2526 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2527
2528 return Qnil;
2529 }
2530
2531 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
2532 "Make current window ARG lines smaller.\n\
2533 From program, optional second arg non-nil means shrink sideways arg columns.")
2534 (arg, side)
2535 register Lisp_Object arg, side;
2536 {
2537 CHECK_NUMBER (arg, 0);
2538 change_window_height (-XINT (arg), !NILP (side));
2539
2540 if (! NILP (Vwindow_configuration_change_hook))
2541 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2542
2543 return Qnil;
2544 }
2545
2546 int
2547 window_height (window)
2548 Lisp_Object window;
2549 {
2550 register struct window *p = XWINDOW (window);
2551 return XFASTINT (p->height);
2552 }
2553
2554 int
2555 window_width (window)
2556 Lisp_Object window;
2557 {
2558 register struct window *p = XWINDOW (window);
2559 return XFASTINT (p->width);
2560 }
2561
2562 #define MINSIZE(w) \
2563 (widthflag \
2564 ? window_min_width \
2565 : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
2566
2567 #define CURBEG(w) \
2568 *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
2569
2570 #define CURSIZE(w) \
2571 *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
2572
2573 /* Unlike set_window_height, this function
2574 also changes the heights of the siblings so as to
2575 keep everything consistent. */
2576
2577 void
2578 change_window_height (delta, widthflag)
2579 register int delta;
2580 int widthflag;
2581 {
2582 register Lisp_Object parent;
2583 Lisp_Object window;
2584 register struct window *p;
2585 int *sizep;
2586 int (*sizefun) P_ ((Lisp_Object))
2587 = widthflag ? window_width : window_height;
2588 register void (*setsizefun) P_ ((Lisp_Object, int, int))
2589 = (widthflag ? set_window_width : set_window_height);
2590 int maximum;
2591 Lisp_Object next, prev;
2592
2593 check_min_window_sizes ();
2594
2595 window = selected_window;
2596 while (1)
2597 {
2598 p = XWINDOW (window);
2599 parent = p->parent;
2600 if (NILP (parent))
2601 {
2602 if (widthflag)
2603 error ("No other window to side of this one");
2604 break;
2605 }
2606 if (widthflag ? !NILP (XWINDOW (parent)->hchild)
2607 : !NILP (XWINDOW (parent)->vchild))
2608 break;
2609 window = parent;
2610 }
2611
2612 sizep = &CURSIZE (window);
2613
2614 {
2615 register int maxdelta;
2616
2617 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
2618 : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
2619 : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
2620 /* This is a frame with only one window, a minibuffer-only
2621 or a minibufferless frame. */
2622 : (delta = 0));
2623
2624 if (delta > maxdelta)
2625 /* This case traps trying to make the minibuffer
2626 the full frame, or make the only window aside from the
2627 minibuffer the full frame. */
2628 delta = maxdelta;
2629 }
2630
2631 if (*sizep + delta < MINSIZE (window))
2632 {
2633 delete_window (window);
2634 return;
2635 }
2636
2637 if (delta == 0)
2638 return;
2639
2640 /* Find the total we can get from other siblings. */
2641 maximum = 0;
2642 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
2643 maximum += (*sizefun) (next) - MINSIZE (next);
2644 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
2645 maximum += (*sizefun) (prev) - MINSIZE (prev);
2646
2647 /* If we can get it all from them, do so. */
2648 if (delta <= maximum)
2649 {
2650 Lisp_Object first_unaffected;
2651 Lisp_Object first_affected;
2652
2653 next = p->next;
2654 prev = p->prev;
2655 first_affected = window;
2656 /* Look at one sibling at a time,
2657 moving away from this window in both directions alternately,
2658 and take as much as we can get without deleting that sibling. */
2659 while (delta != 0)
2660 {
2661 if (delta == 0)
2662 break;
2663 if (! NILP (next))
2664 {
2665 int this_one = (*sizefun) (next) - MINSIZE (next);
2666 if (this_one > delta)
2667 this_one = delta;
2668
2669 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
2670 (*setsizefun) (window, *sizep + this_one, 0);
2671
2672 delta -= this_one;
2673 next = XWINDOW (next)->next;
2674 }
2675 if (delta == 0)
2676 break;
2677 if (! NILP (prev))
2678 {
2679 int this_one = (*sizefun) (prev) - MINSIZE (prev);
2680 if (this_one > delta)
2681 this_one = delta;
2682
2683 first_affected = prev;
2684
2685 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
2686 (*setsizefun) (window, *sizep + this_one, 0);
2687
2688 delta -= this_one;
2689 prev = XWINDOW (prev)->prev;
2690 }
2691 }
2692
2693 /* Now recalculate the edge positions of all the windows affected,
2694 based on the new sizes. */
2695 first_unaffected = next;
2696 prev = first_affected;
2697 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
2698 prev = next, next = XWINDOW (next)->next)
2699 {
2700 CURBEG (next) = CURBEG (prev) + (*sizefun) (prev);
2701 /* This does not change size of NEXT,
2702 but it propagates the new top edge to its children */
2703 (*setsizefun) (next, (*sizefun) (next), 0);
2704 }
2705 }
2706 else
2707 {
2708 register int delta1;
2709 register int opht = (*sizefun) (parent);
2710
2711 /* If trying to grow this window to or beyond size of the parent,
2712 make delta1 so big that, on shrinking back down,
2713 all the siblings end up with less than one line and are deleted. */
2714 if (opht <= *sizep + delta)
2715 delta1 = opht * opht * 2;
2716 /* Otherwise, make delta1 just right so that if we add delta1
2717 lines to this window and to the parent, and then shrink
2718 the parent back to its original size, the new proportional
2719 size of this window will increase by delta. */
2720 else
2721 delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
2722
2723 /* Add delta1 lines or columns to this window, and to the parent,
2724 keeping things consistent while not affecting siblings. */
2725 CURSIZE (parent) = opht + delta1;
2726 (*setsizefun) (window, *sizep + delta1, 0);
2727
2728 /* Squeeze out delta1 lines or columns from our parent,
2729 shriking this window and siblings proportionately.
2730 This brings parent back to correct size.
2731 Delta1 was calculated so this makes this window the desired size,
2732 taking it all out of the siblings. */
2733 (*setsizefun) (parent, opht, 0);
2734 }
2735
2736 XSETFASTINT (p->last_modified, 0);
2737 XSETFASTINT (p->last_overlay_modified, 0);
2738 }
2739 #undef MINSIZE
2740 #undef CURBEG
2741 #undef CURSIZE
2742
2743 \f
2744 /* Return number of lines of text (not counting mode line) in W. */
2745
2746 int
2747 window_internal_height (w)
2748 struct window *w;
2749 {
2750 int ht = XFASTINT (w->height);
2751
2752 if (MINI_WINDOW_P (w))
2753 return ht;
2754
2755 if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
2756 || !NILP (w->next) || !NILP (w->prev)
2757 || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
2758 return ht - 1;
2759
2760 return ht;
2761 }
2762
2763
2764 /* Return the number of columns in W.
2765 Don't count columns occupied by scroll bars or the vertical bar
2766 separating W from the sibling to its right. */
2767 int
2768 window_internal_width (w)
2769 struct window *w;
2770 {
2771 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2772 int width = XINT (w->width);
2773
2774 /* Scroll bars occupy a few columns. */
2775 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2776 return width - FRAME_SCROLL_BAR_COLS (f);
2777
2778 /* The column of `|' characters separating side-by-side windows
2779 occupies one column only. */
2780 if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
2781 return width - 1;
2782
2783 return width;
2784 }
2785
2786
2787 /* Scroll contents of window WINDOW up N lines.
2788 If WHOLE is nonzero, it means scroll N screenfuls instead. */
2789
2790 static void
2791 window_scroll (window, n, whole, noerror)
2792 Lisp_Object window;
2793 int n;
2794 int whole;
2795 int noerror;
2796 {
2797 register struct window *w = XWINDOW (window);
2798 register int opoint = PT;
2799 register int opoint_byte = PT_BYTE;
2800 register int pos, pos_byte;
2801 register int ht = window_internal_height (w);
2802 register Lisp_Object tem;
2803 int lose;
2804 Lisp_Object bolp, nmoved;
2805 int startpos;
2806 struct position posit;
2807 int original_vpos;
2808
2809 startpos = marker_position (w->start);
2810
2811 posit = *compute_motion (startpos, 0, 0, 0,
2812 PT, ht, 0,
2813 window_internal_width (w), XINT (w->hscroll),
2814 0, w);
2815 original_vpos = posit.vpos;
2816
2817 XSETFASTINT (tem, PT);
2818 tem = Fpos_visible_in_window_p (tem, window);
2819
2820 if (NILP (tem))
2821 {
2822 Fvertical_motion (make_number (- (ht / 2)), window);
2823 startpos = PT;
2824 }
2825
2826 SET_PT (startpos);
2827 lose = n < 0 && PT == BEGV;
2828 Fvertical_motion (make_number (n), window);
2829 pos = PT;
2830 pos_byte = PT_BYTE;
2831 bolp = Fbolp ();
2832 SET_PT_BOTH (opoint, opoint_byte);
2833
2834 if (lose)
2835 {
2836 if (noerror)
2837 return;
2838 else
2839 Fsignal (Qbeginning_of_buffer, Qnil);
2840 }
2841
2842 if (pos < ZV)
2843 {
2844 int this_scroll_margin = scroll_margin;
2845
2846 /* Don't use a scroll margin that is negative or too large. */
2847 if (this_scroll_margin < 0)
2848 this_scroll_margin = 0;
2849
2850 if (XINT (w->height) < 4 * scroll_margin)
2851 this_scroll_margin = XINT (w->height) / 4;
2852
2853 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
2854 w->start_at_line_beg = bolp;
2855 w->update_mode_line = Qt;
2856 XSETFASTINT (w->last_modified, 0);
2857 XSETFASTINT (w->last_overlay_modified, 0);
2858 /* Set force_start so that redisplay_window will run
2859 the window-scroll-functions. */
2860 w->force_start = Qt;
2861
2862 if (whole && scroll_preserve_screen_position)
2863 {
2864 SET_PT_BOTH (pos, pos_byte);
2865 Fvertical_motion (make_number (original_vpos), window);
2866 }
2867 /* If we scrolled forward, put point enough lines down
2868 that it is outside the scroll margin. */
2869 else if (n > 0)
2870 {
2871 int top_margin;
2872
2873 if (this_scroll_margin > 0)
2874 {
2875 SET_PT_BOTH (pos, pos_byte);
2876 Fvertical_motion (make_number (this_scroll_margin), window);
2877 top_margin = PT;
2878 }
2879 else
2880 top_margin = pos;
2881
2882 if (top_margin <= opoint)
2883 SET_PT_BOTH (opoint, opoint_byte);
2884 else if (scroll_preserve_screen_position)
2885 {
2886 SET_PT_BOTH (pos, pos_byte);
2887 Fvertical_motion (make_number (original_vpos), window);
2888 }
2889 else
2890 SET_PT (top_margin);
2891 }
2892 else if (n < 0)
2893 {
2894 int bottom_margin;
2895
2896 /* If we scrolled backward, put point near the end of the window
2897 but not within the scroll margin. */
2898 SET_PT_BOTH (pos, pos_byte);
2899 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
2900 if (XFASTINT (tem) == ht - this_scroll_margin)
2901 bottom_margin = PT;
2902 else
2903 bottom_margin = PT + 1;
2904
2905 if (bottom_margin > opoint)
2906 SET_PT_BOTH (opoint, opoint_byte);
2907 else
2908 {
2909 if (scroll_preserve_screen_position)
2910 {
2911 SET_PT_BOTH (pos, pos_byte);
2912 Fvertical_motion (make_number (original_vpos), window);
2913 }
2914 else
2915 Fvertical_motion (make_number (-1), window);
2916 }
2917 }
2918 }
2919 else
2920 {
2921 if (noerror)
2922 return;
2923 else
2924 Fsignal (Qend_of_buffer, Qnil);
2925 }
2926 }
2927 \f
2928 /* This is the guts of Fscroll_up and Fscroll_down. */
2929
2930 static void
2931 scroll_command (n, direction)
2932 register Lisp_Object n;
2933 int direction;
2934 {
2935 register int defalt;
2936 int count = specpdl_ptr - specpdl;
2937
2938 /* If selected window's buffer isn't current, make it current for the moment.
2939 But don't screw up if window_scroll gets an error. */
2940 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2941 {
2942 record_unwind_protect (save_excursion_restore, save_excursion_save ());
2943 Fset_buffer (XWINDOW (selected_window)->buffer);
2944 }
2945
2946 defalt = (window_internal_height (XWINDOW (selected_window))
2947 - next_screen_context_lines);
2948 defalt = direction * (defalt < 1 ? 1 : defalt);
2949
2950 if (NILP (n))
2951 window_scroll (selected_window, defalt, 1, 0);
2952 else if (EQ (n, Qminus))
2953 window_scroll (selected_window, - defalt, 1, 0);
2954 else
2955 {
2956 n = Fprefix_numeric_value (n);
2957 window_scroll (selected_window, XINT (n) * direction, 0, 0);
2958 }
2959
2960 unbind_to (count, Qnil);
2961 }
2962
2963 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
2964 "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
2965 A near full screen is `next-screen-context-lines' less than a full screen.\n\
2966 Negative ARG means scroll downward.\n\
2967 When calling from a program, supply a number as argument or nil.")
2968 (arg)
2969 Lisp_Object arg;
2970 {
2971 scroll_command (arg, 1);
2972 return Qnil;
2973 }
2974
2975 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
2976 "Scroll text of current window downward ARG lines; or near full screen if no ARG.\n\
2977 A near full screen is `next-screen-context-lines' less than a full screen.\n\
2978 Negative ARG means scroll upward.\n\
2979 When calling from a program, supply a number as argument or nil.")
2980 (arg)
2981 Lisp_Object arg;
2982 {
2983 scroll_command (arg, -1);
2984 return Qnil;
2985 }
2986 \f
2987 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
2988 "Return the other window for \"other window scroll\" commands.\n\
2989 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
2990 specifies the window.\n\
2991 If `other-window-scroll-buffer' is non-nil, a window\n\
2992 showing that buffer is used.")
2993 ()
2994 {
2995 Lisp_Object window;
2996
2997 if (MINI_WINDOW_P (XWINDOW (selected_window))
2998 && !NILP (Vminibuf_scroll_window))
2999 window = Vminibuf_scroll_window;
3000 /* If buffer is specified, scroll that buffer. */
3001 else if (!NILP (Vother_window_scroll_buffer))
3002 {
3003 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
3004 if (NILP (window))
3005 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
3006 }
3007 else
3008 {
3009 /* Nothing specified; look for a neighboring window on the same
3010 frame. */
3011 window = Fnext_window (selected_window, Qnil, Qnil);
3012
3013 if (EQ (window, selected_window))
3014 /* That didn't get us anywhere; look for a window on another
3015 visible frame. */
3016 do
3017 window = Fnext_window (window, Qnil, Qt);
3018 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
3019 && ! EQ (window, selected_window));
3020 }
3021
3022 CHECK_LIVE_WINDOW (window, 0);
3023
3024 if (EQ (window, selected_window))
3025 error ("There is no other window");
3026
3027 return window;
3028 }
3029
3030 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
3031 "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
3032 The next window is the one below the current one; or the one at the top\n\
3033 if the current one is at the bottom. Negative ARG means scroll downward.\n\
3034 When calling from a program, supply a number as argument or nil.\n\
3035 \n\
3036 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
3037 specifies the window to scroll.\n\
3038 If `other-window-scroll-buffer' is non-nil, scroll the window\n\
3039 showing that buffer, popping the buffer up if necessary.")
3040 (arg)
3041 register Lisp_Object arg;
3042 {
3043 register Lisp_Object window;
3044 register int defalt;
3045 register struct window *w;
3046 register int count = specpdl_ptr - specpdl;
3047
3048 window = Fother_window_for_scrolling ();
3049
3050 w = XWINDOW (window);
3051 defalt = window_internal_height (w) - next_screen_context_lines;
3052 if (defalt < 1) defalt = 1;
3053
3054 /* Don't screw up if window_scroll gets an error. */
3055 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3056
3057 Fset_buffer (w->buffer);
3058 SET_PT (marker_position (w->pointm));
3059
3060 if (NILP (arg))
3061 window_scroll (window, defalt, 1, 1);
3062 else if (EQ (arg, Qminus))
3063 window_scroll (window, -defalt, 1, 1);
3064 else
3065 {
3066 if (CONSP (arg))
3067 arg = Fcar (arg);
3068 CHECK_NUMBER (arg, 0);
3069 window_scroll (window, XINT (arg), 0, 1);
3070 }
3071
3072 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
3073 unbind_to (count, Qnil);
3074
3075 return Qnil;
3076 }
3077 \f
3078 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
3079 "Scroll selected window display ARG columns left.\n\
3080 Default for ARG is window width minus 2.")
3081 (arg)
3082 register Lisp_Object arg;
3083 {
3084
3085 if (NILP (arg))
3086 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3087 else
3088 arg = Fprefix_numeric_value (arg);
3089
3090 return
3091 Fset_window_hscroll (selected_window,
3092 make_number (XINT (XWINDOW (selected_window)->hscroll)
3093 + XINT (arg)));
3094 }
3095
3096 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
3097 "Scroll selected window display ARG columns right.\n\
3098 Default for ARG is window width minus 2.")
3099 (arg)
3100 register Lisp_Object arg;
3101 {
3102 if (NILP (arg))
3103 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3104 else
3105 arg = Fprefix_numeric_value (arg);
3106
3107 return
3108 Fset_window_hscroll (selected_window,
3109 make_number (XINT (XWINDOW (selected_window)->hscroll)
3110 - XINT (arg)));
3111 }
3112
3113 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
3114 "Center point in window and redisplay frame. With ARG, put point on line ARG.\n\
3115 The desired position of point is always relative to the current window.\n\
3116 Just C-u as prefix means put point in the center of the window.\n\
3117 If ARG is omitted or nil, erases the entire frame and then\n\
3118 redraws with point in the center of the current window.")
3119 (arg)
3120 register Lisp_Object arg;
3121 {
3122 register struct window *w = XWINDOW (selected_window);
3123 register int ht = window_internal_height (w);
3124 struct position pos;
3125 struct buffer *buf = XBUFFER (w->buffer);
3126 struct buffer *obuf = current_buffer;
3127
3128 if (NILP (arg))
3129 {
3130 extern int frame_garbaged;
3131
3132 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
3133 XSETFASTINT (arg, ht / 2);
3134 }
3135 else if (CONSP (arg)) /* Just C-u. */
3136 {
3137 XSETFASTINT (arg, ht / 2);
3138 }
3139 else
3140 {
3141 arg = Fprefix_numeric_value (arg);
3142 CHECK_NUMBER (arg, 0);
3143 }
3144
3145 if (XINT (arg) < 0)
3146 XSETINT (arg, XINT (arg) + ht);
3147
3148 set_buffer_internal (buf);
3149 pos = *vmotion (PT, - XINT (arg), w);
3150
3151 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
3152 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
3153 || FETCH_BYTE (pos.bytepos - 1) == '\n')
3154 ? Qt : Qnil);
3155 w->force_start = Qt;
3156 set_buffer_internal (obuf);
3157
3158 return Qnil;
3159 }
3160 \f
3161 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
3162 1, 1, "P",
3163 "Position point relative to window.\n\
3164 With no argument, position point at center of window.\n\
3165 An argument specifies frame line; zero means top of window,\n\
3166 negative means relative to bottom of window.")
3167 (arg)
3168 register Lisp_Object arg;
3169 {
3170 register struct window *w = XWINDOW (selected_window);
3171 register int height = window_internal_height (w);
3172 register int start;
3173 Lisp_Object window;
3174
3175 if (NILP (arg))
3176 XSETFASTINT (arg, height / 2);
3177 else
3178 {
3179 arg = Fprefix_numeric_value (arg);
3180 if (XINT (arg) < 0)
3181 XSETINT (arg, XINT (arg) + height);
3182 }
3183
3184 start = marker_position (w->start);
3185 XSETWINDOW (window, w);
3186 if (start < BEGV || start > ZV)
3187 {
3188 Fvertical_motion (make_number (- (height / 2)), window);
3189 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
3190 w->start_at_line_beg = Fbolp ();
3191 w->force_start = Qt;
3192 }
3193 else
3194 Fgoto_char (w->start);
3195
3196 return Fvertical_motion (arg, window);
3197 }
3198 \f
3199 struct save_window_data
3200 {
3201 EMACS_INT size_from_Lisp_Vector_struct;
3202 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3203 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
3204 Lisp_Object selected_frame;
3205 Lisp_Object current_window;
3206 Lisp_Object current_buffer;
3207 Lisp_Object minibuf_scroll_window;
3208 Lisp_Object root_window;
3209 Lisp_Object focus_frame;
3210 /* Record the values of window-min-width and window-min-height
3211 so that window sizes remain consistent with them. */
3212 Lisp_Object min_width, min_height;
3213 /* A vector, each of whose elements is a struct saved_window
3214 for one window. */
3215 Lisp_Object saved_windows;
3216 };
3217
3218 /* This is saved as a Lisp_Vector */
3219 struct saved_window
3220 {
3221 /* these first two must agree with struct Lisp_Vector in lisp.h */
3222 EMACS_INT size_from_Lisp_Vector_struct;
3223 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3224
3225 Lisp_Object window;
3226 Lisp_Object buffer, start, pointm, mark;
3227 Lisp_Object left, top, width, height, hscroll;
3228 Lisp_Object parent, prev;
3229 Lisp_Object start_at_line_beg;
3230 Lisp_Object display_table;
3231 };
3232 #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
3233
3234 #define SAVED_WINDOW_N(swv,n) \
3235 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
3236
3237 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
3238 "Return t if OBJECT is a window-configuration object.")
3239 (object)
3240 Lisp_Object object;
3241 {
3242 if (WINDOW_CONFIGURATIONP (object))
3243 return Qt;
3244 return Qnil;
3245 }
3246
3247 DEFUN ("set-window-configuration", Fset_window_configuration,
3248 Sset_window_configuration, 1, 1, 0,
3249 "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
3250 CONFIGURATION must be a value previously returned\n\
3251 by `current-window-configuration' (which see).")
3252 (configuration)
3253 Lisp_Object configuration;
3254 {
3255 register struct save_window_data *data;
3256 struct Lisp_Vector *saved_windows;
3257 Lisp_Object new_current_buffer;
3258 Lisp_Object frame;
3259 FRAME_PTR f;
3260 int old_point = -1;
3261
3262 while (!WINDOW_CONFIGURATIONP (configuration))
3263 {
3264 configuration = wrong_type_argument (intern ("window-configuration-p"),
3265 configuration);
3266 }
3267
3268 data = (struct save_window_data *) XVECTOR (configuration);
3269 saved_windows = XVECTOR (data->saved_windows);
3270
3271 new_current_buffer = data->current_buffer;
3272 if (NILP (XBUFFER (new_current_buffer)->name))
3273 new_current_buffer = Qnil;
3274 else
3275 {
3276 if (XBUFFER (new_current_buffer) == current_buffer)
3277 old_point = PT;
3278
3279 }
3280
3281 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3282 f = XFRAME (frame);
3283
3284 /* If f is a dead frame, don't bother rebuilding its window tree.
3285 However, there is other stuff we should still try to do below. */
3286 if (FRAME_LIVE_P (f))
3287 {
3288 register struct window *w;
3289 register struct saved_window *p;
3290 int k;
3291
3292 /* If the frame has been resized since this window configuration was
3293 made, we change the frame to the size specified in the
3294 configuration, restore the configuration, and then resize it
3295 back. We keep track of the prevailing height in these variables. */
3296 int previous_frame_height = FRAME_HEIGHT (f);
3297 int previous_frame_width = FRAME_WIDTH (f);
3298 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
3299
3300 /* The mouse highlighting code could get screwed up
3301 if it runs during this. */
3302 BLOCK_INPUT;
3303
3304 if (XFASTINT (data->frame_height) != previous_frame_height
3305 || XFASTINT (data->frame_width) != previous_frame_width)
3306 change_frame_size (f, data->frame_height, data->frame_width, 0, 0);
3307 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3308 if (XFASTINT (data->frame_menu_bar_lines)
3309 != previous_frame_menu_bar_lines)
3310 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, 0);
3311 #endif
3312
3313 if (! NILP (XWINDOW (selected_window)->buffer))
3314 {
3315 w = XWINDOW (selected_window);
3316 set_marker_both (w->pointm,
3317 w->buffer,
3318 BUF_PT (XBUFFER (w->buffer)),
3319 BUF_PT_BYTE (XBUFFER (w->buffer)));
3320 }
3321
3322 windows_or_buffers_changed++;
3323 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3324
3325 /* Temporarily avoid any problems with windows that are smaller
3326 than they are supposed to be. */
3327 window_min_height = 1;
3328 window_min_width = 1;
3329
3330 /* Kludge Alert!
3331 Mark all windows now on frame as "deleted".
3332 Restoring the new configuration "undeletes" any that are in it.
3333
3334 Save their current buffers in their height fields, since we may
3335 need it later, if a buffer saved in the configuration is now
3336 dead. */
3337 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3338
3339 for (k = 0; k < saved_windows->size; k++)
3340 {
3341 p = SAVED_WINDOW_N (saved_windows, k);
3342 w = XWINDOW (p->window);
3343 w->next = Qnil;
3344
3345 if (!NILP (p->parent))
3346 w->parent = SAVED_WINDOW_N (saved_windows,
3347 XFASTINT (p->parent))->window;
3348 else
3349 w->parent = Qnil;
3350
3351 if (!NILP (p->prev))
3352 {
3353 w->prev = SAVED_WINDOW_N (saved_windows,
3354 XFASTINT (p->prev))->window;
3355 XWINDOW (w->prev)->next = p->window;
3356 }
3357 else
3358 {
3359 w->prev = Qnil;
3360 if (!NILP (w->parent))
3361 {
3362 if (EQ (p->width, XWINDOW (w->parent)->width))
3363 {
3364 XWINDOW (w->parent)->vchild = p->window;
3365 XWINDOW (w->parent)->hchild = Qnil;
3366 }
3367 else
3368 {
3369 XWINDOW (w->parent)->hchild = p->window;
3370 XWINDOW (w->parent)->vchild = Qnil;
3371 }
3372 }
3373 }
3374
3375 /* If we squirreled away the buffer in the window's height,
3376 restore it now. */
3377 if (BUFFERP (w->height))
3378 w->buffer = w->height;
3379 w->left = p->left;
3380 w->top = p->top;
3381 w->width = p->width;
3382 w->height = p->height;
3383 w->hscroll = p->hscroll;
3384 w->display_table = p->display_table;
3385 XSETFASTINT (w->last_modified, 0);
3386 XSETFASTINT (w->last_overlay_modified, 0);
3387
3388 /* Reinstall the saved buffer and pointers into it. */
3389 if (NILP (p->buffer))
3390 w->buffer = p->buffer;
3391 else
3392 {
3393 if (!NILP (XBUFFER (p->buffer)->name))
3394 /* If saved buffer is alive, install it. */
3395 {
3396 w->buffer = p->buffer;
3397 w->start_at_line_beg = p->start_at_line_beg;
3398 set_marker_restricted (w->start, p->start, w->buffer);
3399 set_marker_restricted (w->pointm, p->pointm, w->buffer);
3400 Fset_marker (XBUFFER (w->buffer)->mark,
3401 p->mark, w->buffer);
3402
3403 /* As documented in Fcurrent_window_configuration, don't
3404 save the location of point in the buffer which was current
3405 when the window configuration was recorded. */
3406 if (!EQ (p->buffer, new_current_buffer)
3407 && XBUFFER (p->buffer) == current_buffer)
3408 Fgoto_char (w->pointm);
3409 }
3410 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
3411 /* Else unless window has a live buffer, get one. */
3412 {
3413 w->buffer = Fcdr (Fcar (Vbuffer_alist));
3414 /* This will set the markers to beginning of visible
3415 range. */
3416 set_marker_restricted (w->start, make_number (0), w->buffer);
3417 set_marker_restricted (w->pointm, make_number (0),w->buffer);
3418 w->start_at_line_beg = Qt;
3419 }
3420 else
3421 /* Keeping window's old buffer; make sure the markers
3422 are real. */
3423 {
3424 /* Set window markers at start of visible range. */
3425 if (XMARKER (w->start)->buffer == 0)
3426 set_marker_restricted (w->start, make_number (0),
3427 w->buffer);
3428 if (XMARKER (w->pointm)->buffer == 0)
3429 set_marker_restricted_both (w->pointm, w->buffer,
3430 BUF_PT (XBUFFER (w->buffer)),
3431 BUF_PT_BYTE (XBUFFER (w->buffer)));
3432 w->start_at_line_beg = Qt;
3433 }
3434 }
3435 }
3436
3437 FRAME_ROOT_WINDOW (f) = data->root_window;
3438 Fselect_window (data->current_window);
3439 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
3440 = selected_window;
3441
3442 if (NILP (data->focus_frame)
3443 || (FRAMEP (data->focus_frame)
3444 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
3445 Fredirect_frame_focus (frame, data->focus_frame);
3446
3447 #if 0 /* I don't understand why this is needed, and it causes problems
3448 when the frame's old selected window has been deleted. */
3449 if (f != selected_frame && FRAME_WINDOW_P (f))
3450 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
3451 Qnil, 0);
3452 #endif
3453
3454 /* Set the screen height to the value it had before this function. */
3455 if (previous_frame_height != FRAME_HEIGHT (f)
3456 || previous_frame_width != FRAME_WIDTH (f))
3457 change_frame_size (f, previous_frame_height, previous_frame_width,
3458 0, 0);
3459 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3460 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
3461 x_set_menu_bar_lines (f, previous_frame_menu_bar_lines, 0);
3462 #endif
3463
3464 UNBLOCK_INPUT;
3465
3466 /* Fselect_window will have made f the selected frame, so we
3467 reselect the proper frame here. Fhandle_switch_frame will change the
3468 selected window too, but that doesn't make the call to
3469 Fselect_window above totally superfluous; it still sets f's
3470 selected window. */
3471 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
3472 do_switch_frame (data->selected_frame, Qnil, 0);
3473
3474 if (! NILP (Vwindow_configuration_change_hook)
3475 && ! NILP (Vrun_hooks))
3476 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3477 }
3478
3479 if (!NILP (new_current_buffer))
3480 {
3481 Fset_buffer (new_current_buffer);
3482
3483 /* If the buffer that is current now is the same
3484 that was current before setting the window configuration,
3485 don't alter its PT. */
3486 if (old_point >= 0)
3487 SET_PT (old_point);
3488 }
3489
3490 /* Restore the minimum heights recorded in the configuration. */
3491 window_min_height = XINT (data->min_height);
3492 window_min_width = XINT (data->min_width);
3493
3494 Vminibuf_scroll_window = data->minibuf_scroll_window;
3495
3496 return Qnil;
3497 }
3498
3499 /* Mark all windows now on frame as deleted
3500 by setting their buffers to nil. */
3501
3502 void
3503 delete_all_subwindows (w)
3504 register struct window *w;
3505 {
3506 if (!NILP (w->next))
3507 delete_all_subwindows (XWINDOW (w->next));
3508 if (!NILP (w->vchild))
3509 delete_all_subwindows (XWINDOW (w->vchild));
3510 if (!NILP (w->hchild))
3511 delete_all_subwindows (XWINDOW (w->hchild));
3512
3513 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
3514
3515 if (!NILP (w->buffer))
3516 unshow_buffer (w);
3517
3518 /* We set all three of these fields to nil, to make sure that we can
3519 distinguish this dead window from any live window. Live leaf
3520 windows will have buffer set, and combination windows will have
3521 vchild or hchild set. */
3522 w->buffer = Qnil;
3523 w->vchild = Qnil;
3524 w->hchild = Qnil;
3525 }
3526 \f
3527 static int
3528 count_windows (window)
3529 register struct window *window;
3530 {
3531 register int count = 1;
3532 if (!NILP (window->next))
3533 count += count_windows (XWINDOW (window->next));
3534 if (!NILP (window->vchild))
3535 count += count_windows (XWINDOW (window->vchild));
3536 if (!NILP (window->hchild))
3537 count += count_windows (XWINDOW (window->hchild));
3538 return count;
3539 }
3540
3541 static int
3542 save_window_save (window, vector, i)
3543 Lisp_Object window;
3544 struct Lisp_Vector *vector;
3545 int i;
3546 {
3547 register struct saved_window *p;
3548 register struct window *w;
3549 register Lisp_Object tem;
3550
3551 for (;!NILP (window); window = w->next)
3552 {
3553 p = SAVED_WINDOW_N (vector, i);
3554 w = XWINDOW (window);
3555
3556 XSETFASTINT (w->temslot, i++);
3557 p->window = window;
3558 p->buffer = w->buffer;
3559 p->left = w->left;
3560 p->top = w->top;
3561 p->width = w->width;
3562 p->height = w->height;
3563 p->hscroll = w->hscroll;
3564 p->display_table = w->display_table;
3565 if (!NILP (w->buffer))
3566 {
3567 /* Save w's value of point in the window configuration.
3568 If w is the selected window, then get the value of point
3569 from the buffer; pointm is garbage in the selected window. */
3570 if (EQ (window, selected_window))
3571 {
3572 p->pointm = Fmake_marker ();
3573 set_marker_both (p->pointm, w->buffer,
3574 BUF_PT (XBUFFER (w->buffer)),
3575 BUF_PT_BYTE (XBUFFER (w->buffer)));
3576 }
3577 else
3578 p->pointm = Fcopy_marker (w->pointm, Qnil);
3579
3580 p->start = Fcopy_marker (w->start, Qnil);
3581 p->start_at_line_beg = w->start_at_line_beg;
3582
3583 tem = XBUFFER (w->buffer)->mark;
3584 p->mark = Fcopy_marker (tem, Qnil);
3585 }
3586 else
3587 {
3588 p->pointm = Qnil;
3589 p->start = Qnil;
3590 p->mark = Qnil;
3591 p->start_at_line_beg = Qnil;
3592 }
3593
3594 if (NILP (w->parent))
3595 p->parent = Qnil;
3596 else
3597 p->parent = XWINDOW (w->parent)->temslot;
3598
3599 if (NILP (w->prev))
3600 p->prev = Qnil;
3601 else
3602 p->prev = XWINDOW (w->prev)->temslot;
3603
3604 if (!NILP (w->vchild))
3605 i = save_window_save (w->vchild, vector, i);
3606 if (!NILP (w->hchild))
3607 i = save_window_save (w->hchild, vector, i);
3608 }
3609
3610 return i;
3611 }
3612
3613 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
3614 Scurrent_window_configuration, 0, 1, 0,
3615 "Return an object representing the current window configuration of FRAME.\n\
3616 If FRAME is nil or omitted, use the selected frame.\n\
3617 This describes the number of windows, their sizes and current buffers,\n\
3618 and for each displayed buffer, where display starts, and the positions of\n\
3619 point and mark. An exception is made for point in the current buffer:\n\
3620 its value is -not- saved.\n\
3621 This also records the currently selected frame, and FRAME's focus\n\
3622 redirection (see `redirect-frame-focus').")
3623 (frame)
3624 Lisp_Object frame;
3625 {
3626 register Lisp_Object tem;
3627 register int n_windows;
3628 register struct save_window_data *data;
3629 register struct Lisp_Vector *vec;
3630 register int i;
3631 FRAME_PTR f;
3632
3633 if (NILP (frame))
3634 f = selected_frame;
3635 else
3636 {
3637 CHECK_LIVE_FRAME (frame, 0);
3638 f = XFRAME (frame);
3639 }
3640
3641 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3642 vec = allocate_vectorlike (VECSIZE (struct save_window_data));
3643 for (i = 0; i < VECSIZE (struct save_window_data); i++)
3644 vec->contents[i] = Qnil;
3645 vec->size = VECSIZE (struct save_window_data);
3646 data = (struct save_window_data *)vec;
3647
3648 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
3649 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
3650 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
3651 XSETFRAME (data->selected_frame, selected_frame);
3652 data->current_window = FRAME_SELECTED_WINDOW (f);
3653 XSETBUFFER (data->current_buffer, current_buffer);
3654 data->minibuf_scroll_window = Vminibuf_scroll_window;
3655 data->root_window = FRAME_ROOT_WINDOW (f);
3656 data->focus_frame = FRAME_FOCUS_FRAME (f);
3657 XSETINT (data->min_height, window_min_height);
3658 XSETINT (data->min_width, window_min_width);
3659 tem = Fmake_vector (make_number (n_windows), Qnil);
3660 data->saved_windows = tem;
3661 for (i = 0; i < n_windows; i++)
3662 XVECTOR (tem)->contents[i]
3663 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
3664 save_window_save (FRAME_ROOT_WINDOW (f),
3665 XVECTOR (tem), 0);
3666 XSETWINDOW_CONFIGURATION (tem, data);
3667 return (tem);
3668 }
3669
3670 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
3671 0, UNEVALLED, 0,
3672 "Execute body, preserving window sizes and contents.\n\
3673 Restore which buffer appears in which window, where display starts,\n\
3674 and the value of point and mark for each window.\n\
3675 Also restore which buffer is current.\n\
3676 But do not preserve point in the current buffer.\n\
3677 Does not restore the value of point in current buffer.")
3678 (args)
3679 Lisp_Object args;
3680 {
3681 register Lisp_Object val;
3682 register int count = specpdl_ptr - specpdl;
3683
3684 record_unwind_protect (Fset_window_configuration,
3685 Fcurrent_window_configuration (Qnil));
3686 val = Fprogn (args);
3687 return unbind_to (count, val);
3688 }
3689 \f
3690 /* Return 1 if window configurations C1 and C2
3691 describe the same state of affairs. This is used by Fequal. */
3692
3693 int
3694 compare_window_configurations (c1, c2, ignore_positions)
3695 Lisp_Object c1, c2;
3696 int ignore_positions;
3697 {
3698 register struct save_window_data *d1, *d2;
3699 struct Lisp_Vector *sw1, *sw2;
3700 int i;
3701
3702 d1 = (struct save_window_data *) XVECTOR (c1);
3703 d2 = (struct save_window_data *) XVECTOR (c2);
3704 sw1 = XVECTOR (d1->saved_windows);
3705 sw2 = XVECTOR (d2->saved_windows);
3706
3707 if (! EQ (d1->frame_width, d2->frame_width))
3708 return 0;
3709 if (! EQ (d1->frame_height, d2->frame_height))
3710 return 0;
3711 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
3712 return 0;
3713 if (! EQ (d1->selected_frame, d2->selected_frame))
3714 return 0;
3715 /* Don't compare the current_window field directly.
3716 Instead see w1_is_current and w2_is_current, below. */
3717 if (! EQ (d1->current_buffer, d2->current_buffer))
3718 return 0;
3719 if (! ignore_positions)
3720 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
3721 return 0;
3722 /* Don't compare the root_window field.
3723 We don't require the two configurations
3724 to use the same window object,
3725 and the two root windows must be equivalent
3726 if everything else compares equal. */
3727 if (! EQ (d1->focus_frame, d2->focus_frame))
3728 return 0;
3729 if (! EQ (d1->min_width, d2->min_width))
3730 return 0;
3731 if (! EQ (d1->min_height, d2->min_height))
3732 return 0;
3733
3734 /* Verify that the two confis have the same number of windows. */
3735 if (sw1->size != sw2->size)
3736 return 0;
3737
3738 for (i = 0; i < sw1->size; i++)
3739 {
3740 struct saved_window *p1, *p2;
3741 int w1_is_current, w2_is_current;
3742
3743 p1 = SAVED_WINDOW_N (sw1, i);
3744 p2 = SAVED_WINDOW_N (sw2, i);
3745
3746 /* Verify that the current windows in the two
3747 configurations correspond to each other. */
3748 w1_is_current = EQ (d1->current_window, p1->window);
3749 w2_is_current = EQ (d2->current_window, p2->window);
3750
3751 if (w1_is_current != w2_is_current)
3752 return 0;
3753
3754 /* Verify that the corresponding windows do match. */
3755 if (! EQ (p1->buffer, p2->buffer))
3756 return 0;
3757 if (! EQ (p1->left, p2->left))
3758 return 0;
3759 if (! EQ (p1->top, p2->top))
3760 return 0;
3761 if (! EQ (p1->width, p2->width))
3762 return 0;
3763 if (! EQ (p1->height, p2->height))
3764 return 0;
3765 if (! EQ (p1->display_table, p2->display_table))
3766 return 0;
3767 if (! EQ (p1->parent, p2->parent))
3768 return 0;
3769 if (! EQ (p1->prev, p2->prev))
3770 return 0;
3771 if (! ignore_positions)
3772 {
3773 if (! EQ (p1->hscroll, p2->hscroll))
3774 return 0;
3775 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
3776 return 0;
3777 if (NILP (Fequal (p1->start, p2->start)))
3778 return 0;
3779 if (NILP (Fequal (p1->pointm, p2->pointm)))
3780 return 0;
3781 if (NILP (Fequal (p1->mark, p2->mark)))
3782 return 0;
3783 }
3784 }
3785
3786 return 1;
3787 }
3788
3789 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
3790 Scompare_window_configurations, 2, 2, 0,
3791 "Compare two window configurations as regards the structure of windows.\n\
3792 This function ignores details such as the values of point and mark\n\
3793 and scrolling positions.")
3794 (x, y)
3795 Lisp_Object x, y;
3796 {
3797 if (compare_window_configurations (x, y, 1))
3798 return Qt;
3799 return Qnil;
3800 }
3801 \f
3802 init_window_once ()
3803 {
3804 selected_frame = make_terminal_frame ();
3805 XSETFRAME (Vterminal_frame, selected_frame);
3806 minibuf_window = selected_frame->minibuffer_window;
3807 selected_window = selected_frame->selected_window;
3808 last_nonminibuf_frame = selected_frame;
3809
3810 window_initialized = 1;
3811 }
3812
3813 syms_of_window ()
3814 {
3815 staticpro (&Qwindow_configuration_change_hook);
3816 Qwindow_configuration_change_hook
3817 = intern ("window-configuration-change-hook");
3818
3819 Qwindowp = intern ("windowp");
3820 staticpro (&Qwindowp);
3821
3822 Qwindow_live_p = intern ("window-live-p");
3823 staticpro (&Qwindow_live_p);
3824
3825 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
3826 staticpro (&Qtemp_buffer_show_hook);
3827
3828 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
3829 "Non-nil means call as function to display a help buffer.\n\
3830 The function is called with one argument, the buffer to be displayed.\n\
3831 Used by `with-output-to-temp-buffer'.\n\
3832 If this function is used, then it must do the entire job of showing\n\
3833 the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
3834 Vtemp_buffer_show_function = Qnil;
3835
3836 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
3837 "If non-nil, function to call to handle `display-buffer'.\n\
3838 It will receive two args, the buffer and a flag which if non-nil means\n\
3839 that the currently selected window is not acceptable.\n\
3840 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
3841 work using this function.");
3842 Vdisplay_buffer_function = Qnil;
3843
3844 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
3845 "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
3846 Vminibuf_scroll_window = Qnil;
3847
3848 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
3849 "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
3850 Vother_window_scroll_buffer = Qnil;
3851
3852 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
3853 "*Non-nil means `display-buffer' should make a separate frame.");
3854 pop_up_frames = 0;
3855
3856 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
3857 "Function to call to handle automatic new frame creation.\n\
3858 It is called with no arguments and should return a newly created frame.\n\
3859 \n\
3860 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
3861 where `pop-up-frame-alist' would hold the default frame parameters.");
3862 Vpop_up_frame_function = Qnil;
3863
3864 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
3865 "*List of buffer names that should have their own special frames.\n\
3866 Displaying a buffer whose name is in this list makes a special frame for it\n\
3867 using `special-display-function'. See also `special-display-regexps'.\n\
3868 \n\
3869 An element of the list can be a list instead of just a string.\n\
3870 There are two ways to use a list as an element:\n\
3871 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)\n\
3872 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3873 In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
3874 followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
3875 All this is done by the function found in `special-display-function'.\n\
3876 \n\
3877 If this variable appears \"not to work\", because you add a name to it\n\
3878 but that buffer still appears in the selected window, look at the\n\
3879 values of `same-window-buffer-names' and `same-window-regexps'.\n\
3880 Those variables take precedence over this one.");
3881 Vspecial_display_buffer_names = Qnil;
3882
3883 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
3884 "*List of regexps saying which buffers should have their own special frames.\n\
3885 If a buffer name matches one of these regexps, it gets its own frame.\n\
3886 Displaying a buffer whose name is in this list makes a special frame for it\n\
3887 using `special-display-function'.\n\
3888 \n\
3889 An element of the list can be a list instead of just a string.\n\
3890 There are two ways to use a list as an element:\n\
3891 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)\n\
3892 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3893 In the latter case, FUNCTION is called with the buffer as first argument,\n\
3894 followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
3895 All this is done by the function found in `special-display-function'.\n\
3896 \n\
3897 If this variable appears \"not to work\", because you add a regexp to it\n\
3898 but the matching buffers still appear in the selected window, look at the\n\
3899 values of `same-window-buffer-names' and `same-window-regexps'.\n\
3900 Those variables take precedence over this one.");
3901 Vspecial_display_regexps = Qnil;
3902
3903 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
3904 "Function to call to make a new frame for a special buffer.\n\
3905 It is called with two arguments, the buffer and optional buffer specific\n\
3906 data, and should return a window displaying that buffer.\n\
3907 The default value makes a separate frame for the buffer,\n\
3908 using `special-display-frame-alist' to specify the frame parameters.\n\
3909 \n\
3910 A buffer is special if its is listed in `special-display-buffer-names'\n\
3911 or matches a regexp in `special-display-regexps'.");
3912 Vspecial_display_function = Qnil;
3913
3914 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
3915 "*List of buffer names that should appear in the selected window.\n\
3916 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
3917 switches to it in the selected window, rather than making it appear\n\
3918 in some other window.\n\
3919 \n\
3920 An element of the list can be a cons cell instead of just a string.\n\
3921 Then the car must be a string, which specifies the buffer name.\n\
3922 This is for compatibility with `special-display-buffer-names';\n\
3923 the cdr of the cons cell is ignored.\n\
3924 \n\
3925 See also `same-window-regexps'.");
3926 Vsame_window_buffer_names = Qnil;
3927
3928 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
3929 "*List of regexps saying which buffers should appear in the selected window.\n\
3930 If a buffer name matches one of these regexps, then displaying it\n\
3931 using `display-buffer' or `pop-to-buffer' switches to it\n\
3932 in the selected window, rather than making it appear in some other window.\n\
3933 \n\
3934 An element of the list can be a cons cell instead of just a string.\n\
3935 Then the car must be a string, which specifies the buffer name.\n\
3936 This is for compatibility with `special-display-buffer-names';\n\
3937 the cdr of the cons cell is ignored.\n\
3938 \n\
3939 See also `same-window-buffer-names'.");
3940 Vsame_window_regexps = Qnil;
3941
3942 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
3943 "*Non-nil means display-buffer should make new windows.");
3944 pop_up_windows = 1;
3945
3946 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
3947 "*Number of lines of continuity when scrolling by screenfuls.");
3948 next_screen_context_lines = 2;
3949
3950 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
3951 "*display-buffer would prefer to split the largest window if this large.\n\
3952 If there is only one window, it is split regardless of this value.");
3953 split_height_threshold = 500;
3954
3955 DEFVAR_INT ("window-min-height", &window_min_height,
3956 "*Delete any window less than this tall (including its mode line).");
3957 window_min_height = 4;
3958
3959 DEFVAR_INT ("window-min-width", &window_min_width,
3960 "*Delete any window less than this wide.");
3961 window_min_width = 10;
3962
3963 DEFVAR_BOOL ("scroll-preserve-screen-position",
3964 &scroll_preserve_screen_position,
3965 "*Nonzero means scroll commands move point to keep its screen line unchanged.");
3966 scroll_preserve_screen_position = 0;
3967
3968 DEFVAR_LISP ("window-configuration-change-hook",
3969 &Vwindow_configuration_change_hook,
3970 "Functions to call when window configuration changes.\n\
3971 The selected frame is the one whose configuration has changed.");
3972 Vwindow_configuration_change_hook = Qnil;
3973
3974 defsubr (&Sselected_window);
3975 defsubr (&Sminibuffer_window);
3976 defsubr (&Swindow_minibuffer_p);
3977 defsubr (&Swindowp);
3978 defsubr (&Swindow_live_p);
3979 defsubr (&Spos_visible_in_window_p);
3980 defsubr (&Swindow_buffer);
3981 defsubr (&Swindow_height);
3982 defsubr (&Swindow_width);
3983 defsubr (&Swindow_hscroll);
3984 defsubr (&Sset_window_hscroll);
3985 defsubr (&Swindow_redisplay_end_trigger);
3986 defsubr (&Sset_window_redisplay_end_trigger);
3987 defsubr (&Swindow_edges);
3988 defsubr (&Scoordinates_in_window_p);
3989 defsubr (&Swindow_at);
3990 defsubr (&Swindow_point);
3991 defsubr (&Swindow_start);
3992 defsubr (&Swindow_end);
3993 defsubr (&Sset_window_point);
3994 defsubr (&Sset_window_start);
3995 defsubr (&Swindow_dedicated_p);
3996 defsubr (&Sset_window_dedicated_p);
3997 defsubr (&Swindow_display_table);
3998 defsubr (&Sset_window_display_table);
3999 defsubr (&Snext_window);
4000 defsubr (&Sprevious_window);
4001 defsubr (&Sother_window);
4002 defsubr (&Sget_lru_window);
4003 defsubr (&Sget_largest_window);
4004 defsubr (&Sget_buffer_window);
4005 defsubr (&Sdelete_other_windows);
4006 defsubr (&Sdelete_windows_on);
4007 defsubr (&Sreplace_buffer_in_windows);
4008 defsubr (&Sdelete_window);
4009 defsubr (&Sset_window_buffer);
4010 defsubr (&Sselect_window);
4011 defsubr (&Sspecial_display_p);
4012 defsubr (&Ssame_window_p);
4013 defsubr (&Sdisplay_buffer);
4014 defsubr (&Ssplit_window);
4015 defsubr (&Senlarge_window);
4016 defsubr (&Sshrink_window);
4017 defsubr (&Sscroll_up);
4018 defsubr (&Sscroll_down);
4019 defsubr (&Sscroll_left);
4020 defsubr (&Sscroll_right);
4021 defsubr (&Sother_window_for_scrolling);
4022 defsubr (&Sscroll_other_window);
4023 defsubr (&Srecenter);
4024 defsubr (&Smove_to_window_line);
4025 defsubr (&Swindow_configuration_p);
4026 defsubr (&Sset_window_configuration);
4027 defsubr (&Scurrent_window_configuration);
4028 defsubr (&Ssave_window_excursion);
4029 defsubr (&Scompare_window_configurations);
4030 }
4031
4032 keys_of_window ()
4033 {
4034 initial_define_key (control_x_map, '1', "delete-other-windows");
4035 initial_define_key (control_x_map, '2', "split-window");
4036 initial_define_key (control_x_map, '0', "delete-window");
4037 initial_define_key (control_x_map, 'o', "other-window");
4038 initial_define_key (control_x_map, '^', "enlarge-window");
4039 initial_define_key (control_x_map, '<', "scroll-left");
4040 initial_define_key (control_x_map, '>', "scroll-right");
4041
4042 initial_define_key (global_map, Ctl ('V'), "scroll-up");
4043 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
4044 initial_define_key (meta_map, 'v', "scroll-down");
4045
4046 initial_define_key (global_map, Ctl('L'), "recenter");
4047 initial_define_key (meta_map, 'r', "move-to-window-line");
4048 }