(change_frame_size_1): Reject new sizes if they cause overflow.
[emacs.git] / src / window.c
blobdd5d472ed7e190a836f853637b9cabbd1ae4e67a
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985, 86, 87, 93, 94, 95, 96 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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. */
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"
33 Lisp_Object Qwindowp, Qwindow_live_p;
35 Lisp_Object Fnext_window (), Fdelete_window (), Fselect_window ();
36 Lisp_Object Fset_window_buffer (), Fsplit_window (), Frecenter ();
38 void delete_all_subwindows ();
39 static struct window *decode_window();
41 /* This is the window in which the terminal's cursor should
42 be left when nothing is being done with it. This must
43 always be a leaf window, and its buffer is selected by
44 the top level editing loop at the end of each command.
46 This value is always the same as
47 FRAME_SELECTED_WINDOW (selected_frame). */
49 Lisp_Object selected_window;
51 /* The minibuffer window of the selected frame.
52 Note that you cannot test for minibufferness of an arbitrary window
53 by comparing against this; but you can test for minibufferness of
54 the selected window. */
55 Lisp_Object minibuf_window;
57 /* Non-nil means it is the window for C-M-v to scroll
58 when the minibuffer is selected. */
59 Lisp_Object Vminibuf_scroll_window;
61 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
62 Lisp_Object Vother_window_scroll_buffer;
64 /* Non-nil means it's function to call to display temp buffers. */
65 Lisp_Object Vtemp_buffer_show_function;
67 /* If a window gets smaller than either of these, it is removed. */
68 int window_min_height;
69 int window_min_width;
71 /* Nonzero implies Fdisplay_buffer should create windows. */
72 int pop_up_windows;
74 /* Nonzero implies make new frames for Fdisplay_buffer. */
75 int pop_up_frames;
77 /* Non-nil means use this function instead of default */
78 Lisp_Object Vpop_up_frame_function;
80 /* Function to call to handle Fdisplay_buffer. */
81 Lisp_Object Vdisplay_buffer_function;
83 /* List of buffer *names* for buffers that should have their own frames. */
84 Lisp_Object Vspecial_display_buffer_names;
86 /* List of regexps for buffer names that should have their own frames. */
87 Lisp_Object Vspecial_display_regexps;
89 /* Function to pop up a special frame. */
90 Lisp_Object Vspecial_display_function;
92 /* List of buffer *names* for buffers to appear in selected window. */
93 Lisp_Object Vsame_window_buffer_names;
95 /* List of regexps for buffer names to appear in selected window. */
96 Lisp_Object Vsame_window_regexps;
98 /* Hook run at end of temp_output_buffer_show. */
99 Lisp_Object Qtemp_buffer_show_hook;
101 /* Fdisplay_buffer always splits the largest window
102 if that window is more than this high. */
103 int split_height_threshold;
105 /* Number of lines of continuity in scrolling by screenfuls. */
106 int next_screen_context_lines;
108 /* Incremented for each window created. */
109 static int sequence_number;
111 /* Nonzero after init_window_once has finished. */
112 static int window_initialized;
114 #define min(a, b) ((a) < (b) ? (a) : (b))
116 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
118 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
119 "Returns t if OBJECT is a window.")
120 (object)
121 Lisp_Object object;
123 return WINDOWP (object) ? Qt : Qnil;
126 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
127 "Returns t if OBJECT is a window which is currently visible.")
128 (object)
129 Lisp_Object object;
131 return (WINDOWP (object) && ! NILP (XWINDOW (object)->buffer) ? Qt : Qnil);
134 Lisp_Object
135 make_window ()
137 Lisp_Object val;
138 register struct window *p;
139 register struct Lisp_Vector *vec;
140 int i;
142 vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct window));
143 for (i = 0; i < VECSIZE (struct window); i++)
144 vec->contents[i] = Qnil;
145 vec->size = VECSIZE (struct window);
146 p = (struct window *)vec;
147 XSETFASTINT (p->sequence_number, ++sequence_number);
148 XSETFASTINT (p->left, 0);
149 XSETFASTINT (p->top, 0);
150 XSETFASTINT (p->height, 0);
151 XSETFASTINT (p->width, 0);
152 XSETFASTINT (p->hscroll, 0);
153 XSETFASTINT (p->last_point_x, 0);
154 XSETFASTINT (p->last_point_y, 0);
155 p->start = Fmake_marker ();
156 p->pointm = Fmake_marker ();
157 XSETFASTINT (p->use_time, 0);
158 p->frame = Qnil;
159 p->display_table = Qnil;
160 p->dedicated = Qnil;
161 XSETWINDOW (val, p);
162 return val;
165 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
166 "Return the window that the cursor now appears in and commands apply to.")
169 return selected_window;
172 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
173 "Return the window used now for minibuffers.\n\
174 If the optional argument FRAME is specified, return the minibuffer window\n\
175 used by that frame.")
176 (frame)
177 Lisp_Object frame;
179 if (NILP (frame))
180 XSETFRAME (frame, selected_frame);
181 else
182 CHECK_LIVE_FRAME (frame, 0);
184 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
187 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
188 "Returns non-nil if WINDOW is a minibuffer window.")
189 (window)
190 Lisp_Object window;
192 struct window *w = decode_window (window);
193 return (MINI_WINDOW_P (w) ? Qt : Qnil);
196 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
197 Spos_visible_in_window_p, 0, 2, 0,
198 "Return t if position POS is currently on the frame in WINDOW.\n\
199 Returns nil if that position is scrolled vertically out of view.\n\
200 POS defaults to point; WINDOW, to the selected window.")
201 (pos, window)
202 Lisp_Object pos, window;
204 register struct window *w;
205 register int top;
206 register int height;
207 register int posint;
208 register struct buffer *buf;
209 struct position posval;
210 int hscroll;
212 if (NILP (pos))
213 posint = PT;
214 else
216 CHECK_NUMBER_COERCE_MARKER (pos, 0);
217 posint = XINT (pos);
220 w = decode_window (window);
221 top = marker_position (w->start);
222 hscroll = XINT (w->hscroll);
224 if (posint < top)
225 return Qnil;
227 height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
229 buf = XBUFFER (w->buffer);
230 if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
231 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf))
233 /* If frame is up to date,
234 use the info recorded about how much text fit on it. */
235 if (posint < BUF_Z (buf) - XFASTINT (w->window_end_pos)
236 || (XFASTINT (w->window_end_vpos) < height))
237 return Qt;
238 return Qnil;
240 else
242 if (posint > BUF_ZV (buf))
243 return Qnil;
245 /* w->start can be out of range. If it is, do something reasonable. */
246 if (top < BUF_BEGV (buf) || top > BUF_ZV (buf))
247 return Qnil;
249 /* If that info is not correct, calculate afresh */
250 posval = *compute_motion (top, 0, (hscroll ? 1 - hscroll : 0), 0,
251 posint, height, 0,
252 window_internal_width (w) - 1,
253 hscroll, 0, w);
255 return posval.vpos < height ? Qt : Qnil;
259 static struct window *
260 decode_window (window)
261 register Lisp_Object window;
263 if (NILP (window))
264 return XWINDOW (selected_window);
266 CHECK_LIVE_WINDOW (window, 0);
267 return XWINDOW (window);
270 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
271 "Return the buffer that WINDOW is displaying.")
272 (window)
273 Lisp_Object window;
275 return decode_window (window)->buffer;
278 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
279 "Return the number of lines in WINDOW (including its mode line).")
280 (window)
281 Lisp_Object window;
283 return decode_window (window)->height;
286 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
287 "Return the number of display columns in WINDOW.\n\
288 This is the width that is usable columns available for text in WINDOW.\n\
289 If you want to find out how many columns WINDOW takes up,\n\
290 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
291 (window)
292 Lisp_Object window;
294 return make_number (window_internal_width (decode_window (window)));
297 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
298 "Return the number of columns by which WINDOW is scrolled from left margin.")
299 (window)
300 Lisp_Object window;
302 return decode_window (window)->hscroll;
305 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
306 "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
307 NCOL should be zero or positive.")
308 (window, ncol)
309 register Lisp_Object window, ncol;
311 register struct window *w;
313 CHECK_NUMBER (ncol, 1);
314 if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
315 w = decode_window (window);
316 if (XINT (w->hscroll) != XINT (ncol))
317 XBUFFER (w->buffer)->clip_changed = 1; /* Prevent redisplay shortcuts */
318 w->hscroll = ncol;
319 return ncol;
322 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
323 Swindow_redisplay_end_trigger, 0, 1, 0,
324 "Return WINDOW's redisplay end trigger value.\n\
325 See `set-window-redisplay-end-trigger' for more information.")
326 (window)
327 Lisp_Object window;
329 return decode_window (window)->redisplay_end_trigger;
332 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
333 Sset_window_redisplay_end_trigger, 2, 2, 0,
334 "Set WINDOW's redisplay end trigger value to VALUE.\n\
335 VALUE should be a buffer position (typically a marker) or nil.\n\
336 If it is a buffer position, then if redisplay in WINDOW reaches a position\n\
337 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called\n\
338 with two arguments: WINDOW, and the end trigger value.\n\
339 Afterwards the end-trigger value is reset to nil.")
340 (window, value)
341 register Lisp_Object window, value;
343 register struct window *w;
345 w = decode_window (window);
346 w->redisplay_end_trigger = value;
347 return value;
350 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
351 "Return a list of the edge coordinates of WINDOW.\n\
352 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.\n\
353 RIGHT is one more than the rightmost column used by WINDOW,\n\
354 and BOTTOM is one more than the bottommost row used by WINDOW\n\
355 and its mode-line.")
356 (window)
357 Lisp_Object window;
359 register struct window *w = decode_window (window);
361 return Fcons (w->left, Fcons (w->top,
362 Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
363 Fcons (make_number (XFASTINT (w->top)
364 + XFASTINT (w->height)),
365 Qnil))));
368 /* Test if the character at column *x, row *y is within window *w.
369 If it is not, return 0;
370 if it is in the window's text area,
371 set *x and *y to its location relative to the upper left corner
372 of the window, and
373 return 1;
374 if it is on the window's modeline, return 2;
375 if it is on the border between the window and its right sibling,
376 return 3. */
377 static int
378 coordinates_in_window (w, x, y)
379 register struct window *w;
380 register int *x, *y;
382 register int left = XINT (w->left);
383 register int right_edge = WINDOW_RIGHT_EDGE (w);
384 register int left_margin = WINDOW_LEFT_MARGIN (w);
385 register int right_margin = WINDOW_RIGHT_MARGIN (w);
386 register int window_height = XINT (w->height);
387 register int top = XFASTINT (w->top);
389 if ( *x < left || *x >= right_edge
390 || *y < top || *y >= top + window_height)
391 return 0;
393 if (left_margin != left && *x < left_margin && *x >= left)
394 return 3;
396 if (right_margin != right_edge && *x >= right_margin && *x < right_edge)
397 return 3;
399 /* Is the character is the mode line? */
400 if (*y == top + window_height - 1
401 && ! MINI_WINDOW_P (w))
402 return 2;
404 *x -= WINDOW_LEFT_MARGIN (w);
405 *y -= top;
406 return 1;
409 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
410 Scoordinates_in_window_p, 2, 2, 0,
411 "Return non-nil if COORDINATES are in WINDOW.\n\
412 COORDINATES is a cons of the form (X . Y), X and Y being distances\n\
413 measured in characters from the upper-left corner of the frame.\n\
414 (0 . 0) denotes the character in the upper left corner of the\n\
415 frame.\n\
416 If COORDINATES are in the text portion of WINDOW,\n\
417 the coordinates relative to the window are returned.\n\
418 If they are in the mode line of WINDOW, `mode-line' is returned.\n\
419 If they are on the border between WINDOW and its right sibling,\n\
420 `vertical-line' is returned.")
421 (coordinates, window)
422 register Lisp_Object coordinates, window;
424 int x, y;
426 CHECK_LIVE_WINDOW (window, 0);
427 CHECK_CONS (coordinates, 1);
428 x = XINT (Fcar (coordinates));
429 y = XINT (Fcdr (coordinates));
431 switch (coordinates_in_window (XWINDOW (window), &x, &y))
433 case 0: /* NOT in window at all. */
434 return Qnil;
436 case 1: /* In text part of window. */
437 return Fcons (x, y);
439 case 2: /* In mode line of window. */
440 return Qmode_line;
442 case 3: /* On right border of window. */
443 return Qvertical_line;
445 default:
446 abort ();
450 /* Find the window containing column x, row y, and return it as a
451 Lisp_Object. If x, y is on the window's modeline, set *part
452 to 1; if it is on the separating line between the window and its
453 right sibling, set it to 2; otherwise set it to 0. If there is no
454 window under x, y return nil and leave *part unmodified. */
455 Lisp_Object
456 window_from_coordinates (frame, x, y, part)
457 FRAME_PTR frame;
458 int x, y;
459 int *part;
461 register Lisp_Object tem, first;
463 tem = first = FRAME_SELECTED_WINDOW (frame);
467 int found = coordinates_in_window (XWINDOW (tem), &x, &y);
469 if (found)
471 *part = found - 1;
472 return tem;
475 tem = Fnext_window (tem, Qt, Qlambda);
477 while (! EQ (tem, first));
479 return Qnil;
482 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
483 "Return window containing coordinates X and Y on FRAME.\n\
484 If omitted, FRAME defaults to the currently selected frame.\n\
485 The top left corner of the frame is considered to be row 0,\n\
486 column 0.")
487 (x, y, frame)
488 Lisp_Object x, y, frame;
490 int part;
492 if (NILP (frame))
493 XSETFRAME (frame, selected_frame);
494 else
495 CHECK_LIVE_FRAME (frame, 2);
496 CHECK_NUMBER (x, 0);
497 CHECK_NUMBER (y, 1);
499 return window_from_coordinates (XFRAME (frame),
500 XINT (x), XINT (y),
501 &part);
504 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
505 "Return current value of point in WINDOW.\n\
506 For a nonselected window, this is the value point would have\n\
507 if that window were selected.\n\
509 Note that, when WINDOW is the selected window and its buffer\n\
510 is also currently selected, the value returned is the same as (point).\n\
511 It would be more strictly correct to return the `top-level' value\n\
512 of point, outside of any save-excursion forms.\n\
513 But that is hard to define.")
514 (window)
515 Lisp_Object window;
517 register struct window *w = decode_window (window);
519 if (w == XWINDOW (selected_window)
520 && current_buffer == XBUFFER (w->buffer))
521 return Fpoint ();
522 return Fmarker_position (w->pointm);
525 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
526 "Return position at which display currently starts in WINDOW.\n\
527 This is updated by redisplay or by calling `set-window-start'.")
528 (window)
529 Lisp_Object window;
531 return Fmarker_position (decode_window (window)->start);
534 /* This is text temporarily removed from the doc string below.
536 This function returns nil if the position is not currently known.\n\
537 That happens when redisplay is preempted and doesn't finish.\n\
538 If in that case you want to compute where the end of the window would\n\
539 have been if redisplay had finished, do this:\n\
540 (save-excursion\n\
541 (goto-char (window-start window))\n\
542 (vertical-motion (1- (window-height window)) window)\n\
543 (point))") */
545 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 1, 0,
546 "Return position at which display currently ends in WINDOW.\n\
547 This is updated by redisplay, when it runs to completion.\n\
548 Simply changing the buffer text or setting `window-start'\n\
549 does not update this value.")
550 (window)
551 Lisp_Object window;
553 Lisp_Object value;
554 struct window *w = decode_window (window);
555 Lisp_Object buf;
557 buf = w->buffer;
558 CHECK_BUFFER (buf, 0);
560 #if 0 /* This change broke some things. We should make it later. */
561 /* If we don't know the end position, return nil.
562 The user can compute it with vertical-motion if he wants to.
563 It would be nicer to do it automatically,
564 but that's so slow that it would probably bother people. */
565 if (NILP (w->window_end_valid))
566 return Qnil;
567 #endif
569 XSETINT (value,
570 BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
572 return value;
575 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
576 "Make point value in WINDOW be at position POS in WINDOW's buffer.")
577 (window, pos)
578 Lisp_Object window, pos;
580 register struct window *w = decode_window (window);
582 CHECK_NUMBER_COERCE_MARKER (pos, 1);
583 if (w == XWINDOW (selected_window))
584 Fgoto_char (pos);
585 else
586 set_marker_restricted (w->pointm, pos, w->buffer);
588 return pos;
591 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
592 "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
593 Optional third arg NOFORCE non-nil inhibits next redisplay\n\
594 from overriding motion of point in order to display at this exact start.")
595 (window, pos, noforce)
596 Lisp_Object window, pos, noforce;
598 register struct window *w = decode_window (window);
600 CHECK_NUMBER_COERCE_MARKER (pos, 1);
601 set_marker_restricted (w->start, pos, w->buffer);
602 /* this is not right, but much easier than doing what is right. */
603 w->start_at_line_beg = Qnil;
604 if (NILP (noforce))
605 w->force_start = Qt;
606 w->update_mode_line = Qt;
607 XSETFASTINT (w->last_modified, 0);
608 XSETFASTINT (w->last_overlay_modified, 0);
609 if (!EQ (window, selected_window))
610 windows_or_buffers_changed++;
611 return pos;
614 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
615 1, 1, 0,
616 "Return WINDOW's dedicated object, usually t or nil.\n\
617 See also `set-window-dedicated-p'.")
618 (window)
619 Lisp_Object window;
621 return decode_window (window)->dedicated;
624 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
625 Sset_window_dedicated_p, 2, 2, 0,
626 "Control whether WINDOW is dedicated to the buffer it displays.\n\
627 If it is dedicated, Emacs will not automatically change\n\
628 which buffer appears in it.\n\
629 The second argument is the new value for the dedication flag;\n\
630 non-nil means yes.")
631 (window, arg)
632 Lisp_Object window, arg;
634 register struct window *w = decode_window (window);
636 if (NILP (arg))
637 w->dedicated = Qnil;
638 else
639 w->dedicated = Qt;
641 return w->dedicated;
644 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
645 0, 1, 0,
646 "Return the display-table that WINDOW is using.")
647 (window)
648 Lisp_Object window;
650 return decode_window (window)->display_table;
653 /* Get the display table for use currently on window W.
654 This is either W's display table or W's buffer's display table.
655 Ignore the specified tables if they are not valid;
656 if no valid table is specified, return 0. */
658 struct Lisp_Char_Table *
659 window_display_table (w)
660 struct window *w;
662 Lisp_Object tem;
663 tem = w->display_table;
664 if (DISP_TABLE_P (tem))
665 return XCHAR_TABLE (tem);
666 tem = XBUFFER (w->buffer)->display_table;
667 if (DISP_TABLE_P (tem))
668 return XCHAR_TABLE (tem);
669 tem = Vstandard_display_table;
670 if (DISP_TABLE_P (tem))
671 return XCHAR_TABLE (tem);
672 return 0;
675 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
676 "Set WINDOW's display-table to TABLE.")
677 (window, table)
678 register Lisp_Object window, table;
680 register struct window *w;
681 register Lisp_Object z; /* Return value. */
683 w = decode_window (window);
684 w->display_table = table;
685 return table;
688 /* Record info on buffer window w is displaying
689 when it is about to cease to display that buffer. */
690 static
691 unshow_buffer (w)
692 register struct window *w;
694 Lisp_Object buf;
696 buf = w->buffer;
697 if (XBUFFER (buf) != XMARKER (w->pointm)->buffer)
698 abort ();
700 if (w == XWINDOW (XBUFFER (buf)->last_selected_window))
701 XBUFFER (buf)->last_selected_window = Qnil;
703 #if 0
704 if (w == XWINDOW (selected_window)
705 || ! EQ (buf, XWINDOW (selected_window)->buffer))
706 /* Do this except when the selected window's buffer
707 is being removed from some other window. */
708 #endif
709 /* last_window_start records the start position that this buffer
710 had in the last window to be disconnected from it.
711 Now that this statement is unconditional,
712 it is possible for the buffer to be displayed in the
713 selected window, while last_window_start reflects another
714 window which was recently showing the same buffer.
715 Some people might say that might be a good thing. Let's see. */
716 XBUFFER (buf)->last_window_start = marker_position (w->start);
718 /* Point in the selected window's buffer
719 is actually stored in that buffer, and the window's pointm isn't used.
720 So don't clobber point in that buffer. */
721 if (! EQ (buf, XWINDOW (selected_window)->buffer))
722 BUF_PT (XBUFFER (buf))
723 = clip_to_bounds (BUF_BEGV (XBUFFER (buf)),
724 marker_position (w->pointm),
725 BUF_ZV (XBUFFER (buf)));
728 /* Put replacement into the window structure in place of old. */
729 static
730 replace_window (old, replacement)
731 Lisp_Object old, replacement;
733 register Lisp_Object tem;
734 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
736 /* If OLD is its frame's root_window, then replacement is the new
737 root_window for that frame. */
739 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
740 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
742 p->left = o->left;
743 p->top = o->top;
744 p->width = o->width;
745 p->height = o->height;
747 p->next = tem = o->next;
748 if (!NILP (tem))
749 XWINDOW (tem)->prev = replacement;
751 p->prev = tem = o->prev;
752 if (!NILP (tem))
753 XWINDOW (tem)->next = replacement;
755 p->parent = tem = o->parent;
756 if (!NILP (tem))
758 if (EQ (XWINDOW (tem)->vchild, old))
759 XWINDOW (tem)->vchild = replacement;
760 if (EQ (XWINDOW (tem)->hchild, old))
761 XWINDOW (tem)->hchild = replacement;
764 /*** Here, if replacement is a vertical combination
765 and so is its new parent, we should make replacement's
766 children be children of that parent instead. ***/
769 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
770 "Remove WINDOW from the display. Default is selected window.")
771 (window)
772 register Lisp_Object window;
774 register Lisp_Object tem, parent, sib;
775 register struct window *p;
776 register struct window *par;
778 /* Because this function is called by other C code on non-leaf
779 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
780 so we can't decode_window here. */
781 if (NILP (window))
782 window = selected_window;
783 else
784 CHECK_WINDOW (window, 0);
785 p = XWINDOW (window);
787 /* It's okay to delete an already-deleted window. */
788 if (NILP (p->buffer)
789 && NILP (p->hchild)
790 && NILP (p->vchild))
791 return Qnil;
793 parent = p->parent;
794 if (NILP (parent))
795 error ("Attempt to delete minibuffer or sole ordinary window");
796 par = XWINDOW (parent);
798 windows_or_buffers_changed++;
799 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (p))) = 1;
801 /* Are we trying to delete any frame's selected window? */
803 Lisp_Object frame, pwindow;
805 /* See if the frame's selected window is either WINDOW
806 or any subwindow of it, by finding all that window's parents
807 and comparing each one with WINDOW. */
808 frame = WINDOW_FRAME (XWINDOW (window));
809 pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
811 while (!NILP (pwindow))
813 if (EQ (window, pwindow))
814 break;
815 pwindow = XWINDOW (pwindow)->parent;
818 if (EQ (window, pwindow))
820 Lisp_Object alternative;
821 alternative = Fnext_window (window, Qlambda, Qnil);
823 /* If we're about to delete the selected window on the
824 selected frame, then we should use Fselect_window to select
825 the new window. On the other hand, if we're about to
826 delete the selected window on any other frame, we shouldn't do
827 anything but set the frame's selected_window slot. */
828 if (EQ (window, selected_window))
829 Fselect_window (alternative);
830 else
831 FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
835 tem = p->buffer;
836 /* tem is null for dummy parent windows
837 (which have inferiors but not any contents themselves) */
838 if (!NILP (tem))
840 unshow_buffer (p);
841 unchain_marker (p->pointm);
842 unchain_marker (p->start);
845 tem = p->next;
846 if (!NILP (tem))
847 XWINDOW (tem)->prev = p->prev;
849 tem = p->prev;
850 if (!NILP (tem))
851 XWINDOW (tem)->next = p->next;
853 if (EQ (window, par->hchild))
854 par->hchild = p->next;
855 if (EQ (window, par->vchild))
856 par->vchild = p->next;
858 /* Find one of our siblings to give our space to. */
859 sib = p->prev;
860 if (NILP (sib))
862 /* If p gives its space to its next sibling, that sibling needs
863 to have its top/left side pulled back to where p's is.
864 set_window_{height,width} will re-position the sibling's
865 children. */
866 sib = p->next;
867 XWINDOW (sib)->top = p->top;
868 XWINDOW (sib)->left = p->left;
871 /* Stretch that sibling. */
872 if (!NILP (par->vchild))
873 set_window_height (sib,
874 XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
876 if (!NILP (par->hchild))
877 set_window_width (sib,
878 XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
881 /* If parent now has only one child,
882 put the child into the parent's place. */
883 tem = par->hchild;
884 if (NILP (tem))
885 tem = par->vchild;
886 if (NILP (XWINDOW (tem)->next))
887 replace_window (parent, tem);
889 /* Since we may be deleting combination windows, we must make sure that
890 not only p but all its children have been marked as deleted. */
891 if (! NILP (p->hchild))
892 delete_all_subwindows (XWINDOW (p->hchild));
893 else if (! NILP (p->vchild))
894 delete_all_subwindows (XWINDOW (p->vchild));
896 /* Mark this window as deleted. */
897 p->buffer = p->hchild = p->vchild = Qnil;
899 return Qnil;
903 extern Lisp_Object next_frame (), prev_frame ();
905 /* This comment supplies the doc string for `next-window',
906 for make-docfile to see. We cannot put this in the real DEFUN
907 due to limits in the Unix cpp.
909 DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
910 "Return next window after WINDOW in canonical ordering of windows.\n\
911 If omitted, WINDOW defaults to the selected window.\n\
913 Optional second arg MINIBUF t means count the minibuffer window even\n\
914 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
915 it is active. MINIBUF neither t nor nil means not to count the\n\
916 minibuffer even if it is active.\n\
918 Several frames may share a single minibuffer; if the minibuffer\n\
919 counts, all windows on all frames that share that minibuffer count\n\
920 too. Therefore, `next-window' can be used to iterate through the\n\
921 set of windows even when the minibuffer is on another frame. If the\n\
922 minibuffer does not count, only windows from WINDOW's frame count.\n\
924 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
925 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
926 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
927 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
928 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
929 Anything else means restrict to WINDOW's frame.\n\
931 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
932 `next-window' to iterate through the entire cycle of acceptable\n\
933 windows, eventually ending up back at the window you started with.\n\
934 `previous-window' traverses the same cycle, in the reverse order.")
935 (window, minibuf, all_frames) */
937 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
939 (window, minibuf, all_frames)
940 register Lisp_Object window, minibuf, all_frames;
942 register Lisp_Object tem;
943 Lisp_Object start_window;
945 if (NILP (window))
946 window = selected_window;
947 else
948 CHECK_LIVE_WINDOW (window, 0);
950 start_window = window;
952 /* minibuf == nil may or may not include minibuffers.
953 Decide if it does. */
954 if (NILP (minibuf))
955 minibuf = (minibuf_level ? minibuf_window : Qlambda);
956 else if (! EQ (minibuf, Qt))
957 minibuf = Qlambda;
958 /* Now minibuf can be t => count all minibuffer windows,
959 lambda => count none of them,
960 or a specific minibuffer window (the active one) to count. */
962 /* all_frames == nil doesn't specify which frames to include. */
963 if (NILP (all_frames))
964 all_frames = (! EQ (minibuf, Qlambda)
965 ? (FRAME_MINIBUF_WINDOW
966 (XFRAME
967 (WINDOW_FRAME
968 (XWINDOW (window)))))
969 : Qnil);
970 else if (EQ (all_frames, Qvisible))
972 else if (XFASTINT (all_frames) == 0)
974 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
975 /* If all_frames is a frame and window arg isn't on that frame, just
976 return the first window on the frame. */
977 return Fframe_first_window (all_frames);
978 else if (! EQ (all_frames, Qt))
979 all_frames = Qnil;
980 /* Now all_frames is t meaning search all frames,
981 nil meaning search just current frame,
982 visible meaning search just visible frames,
983 0 meaning search visible and iconified frames,
984 or a window, meaning search the frame that window belongs to. */
986 /* Do this loop at least once, to get the next window, and perhaps
987 again, if we hit the minibuffer and that is not acceptable. */
990 /* Find a window that actually has a next one. This loop
991 climbs up the tree. */
992 while (tem = XWINDOW (window)->next, NILP (tem))
993 if (tem = XWINDOW (window)->parent, !NILP (tem))
994 window = tem;
995 else
997 /* We've reached the end of this frame.
998 Which other frames are acceptable? */
999 tem = WINDOW_FRAME (XWINDOW (window));
1000 if (! NILP (all_frames))
1002 Lisp_Object tem1;
1004 tem1 = tem;
1005 tem = next_frame (tem, all_frames);
1006 /* In the case where the minibuffer is active,
1007 and we include its frame as well as the selected one,
1008 next_frame may get stuck in that frame.
1009 If that happens, go back to the selected frame
1010 so we can complete the cycle. */
1011 if (EQ (tem, tem1))
1012 XSETFRAME (tem, selected_frame);
1014 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1016 break;
1019 window = tem;
1021 /* If we're in a combination window, find its first child and
1022 recurse on that. Otherwise, we've found the window we want. */
1023 while (1)
1025 if (!NILP (XWINDOW (window)->hchild))
1026 window = XWINDOW (window)->hchild;
1027 else if (!NILP (XWINDOW (window)->vchild))
1028 window = XWINDOW (window)->vchild;
1029 else break;
1032 /* Which windows are acceptable?
1033 Exit the loop and accept this window if
1034 this isn't a minibuffer window,
1035 or we're accepting all minibuffer windows,
1036 or this is the active minibuffer and we are accepting that one, or
1037 we've come all the way around and we're back at the original window. */
1038 while (MINI_WINDOW_P (XWINDOW (window))
1039 && ! EQ (minibuf, Qt)
1040 && ! EQ (minibuf, window)
1041 && ! EQ (window, start_window));
1043 return window;
1046 /* This comment supplies the doc string for `previous-window',
1047 for make-docfile to see. We cannot put this in the real DEFUN
1048 due to limits in the Unix cpp.
1050 DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
1051 "Return the window preceding WINDOW in canonical ordering of windows.\n\
1052 If omitted, WINDOW defaults to the selected window.\n\
1054 Optional second arg MINIBUF t means count the minibuffer window even\n\
1055 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
1056 it is active. MINIBUF neither t nor nil means not to count the\n\
1057 minibuffer even if it is active.\n\
1059 Several frames may share a single minibuffer; if the minibuffer\n\
1060 counts, all windows on all frames that share that minibuffer count\n\
1061 too. Therefore, `previous-window' can be used to iterate through\n\
1062 the set of windows even when the minibuffer is on another frame. If\n\
1063 the minibuffer does not count, only windows from WINDOW's frame count\n\
1065 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
1066 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
1067 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
1068 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1069 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
1070 Anything else means restrict to WINDOW's frame.\n\
1072 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
1073 `previous-window' to iterate through the entire cycle of acceptable\n\
1074 windows, eventually ending up back at the window you started with.\n\
1075 `next-window' traverses the same cycle, in the reverse order.")
1076 (window, minibuf, all_frames) */
1079 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1081 (window, minibuf, all_frames)
1082 register Lisp_Object window, minibuf, all_frames;
1084 register Lisp_Object tem;
1085 Lisp_Object start_window;
1087 if (NILP (window))
1088 window = selected_window;
1089 else
1090 CHECK_LIVE_WINDOW (window, 0);
1092 start_window = window;
1094 /* minibuf == nil may or may not include minibuffers.
1095 Decide if it does. */
1096 if (NILP (minibuf))
1097 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1098 else if (! EQ (minibuf, Qt))
1099 minibuf = Qlambda;
1100 /* Now minibuf can be t => count all minibuffer windows,
1101 lambda => count none of them,
1102 or a specific minibuffer window (the active one) to count. */
1104 /* all_frames == nil doesn't specify which frames to include.
1105 Decide which frames it includes. */
1106 if (NILP (all_frames))
1107 all_frames = (! EQ (minibuf, Qlambda)
1108 ? (FRAME_MINIBUF_WINDOW
1109 (XFRAME
1110 (WINDOW_FRAME
1111 (XWINDOW (window)))))
1112 : Qnil);
1113 else if (EQ (all_frames, Qvisible))
1115 else if (XFASTINT (all_frames) == 0)
1117 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1118 /* If all_frames is a frame and window arg isn't on that frame, just
1119 return the first window on the frame. */
1120 return Fframe_first_window (all_frames);
1121 else if (! EQ (all_frames, Qt))
1122 all_frames = Qnil;
1123 /* Now all_frames is t meaning search all frames,
1124 nil meaning search just current frame,
1125 visible meaning search just visible frames,
1126 0 meaning search visible and iconified frames,
1127 or a window, meaning search the frame that window belongs to. */
1129 /* Do this loop at least once, to get the previous window, and perhaps
1130 again, if we hit the minibuffer and that is not acceptable. */
1133 /* Find a window that actually has a previous one. This loop
1134 climbs up the tree. */
1135 while (tem = XWINDOW (window)->prev, NILP (tem))
1136 if (tem = XWINDOW (window)->parent, !NILP (tem))
1137 window = tem;
1138 else
1140 /* We have found the top window on the frame.
1141 Which frames are acceptable? */
1142 tem = WINDOW_FRAME (XWINDOW (window));
1143 if (! NILP (all_frames))
1144 /* It's actually important that we use prev_frame here,
1145 rather than next_frame. All the windows acceptable
1146 according to the given parameters should form a ring;
1147 Fnext_window and Fprevious_window should go back and
1148 forth around the ring. If we use next_frame here,
1149 then Fnext_window and Fprevious_window take different
1150 paths through the set of acceptable windows.
1151 window_loop assumes that these `ring' requirement are
1152 met. */
1154 Lisp_Object tem1;
1156 tem1 = tem;
1157 tem = prev_frame (tem, all_frames);
1158 /* In the case where the minibuffer is active,
1159 and we include its frame as well as the selected one,
1160 next_frame may get stuck in that frame.
1161 If that happens, go back to the selected frame
1162 so we can complete the cycle. */
1163 if (EQ (tem, tem1))
1164 XSETFRAME (tem, selected_frame);
1166 /* If this frame has a minibuffer, find that window first,
1167 because it is conceptually the last window in that frame. */
1168 if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
1169 tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
1170 else
1171 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1173 break;
1176 window = tem;
1177 /* If we're in a combination window, find its last child and
1178 recurse on that. Otherwise, we've found the window we want. */
1179 while (1)
1181 if (!NILP (XWINDOW (window)->hchild))
1182 window = XWINDOW (window)->hchild;
1183 else if (!NILP (XWINDOW (window)->vchild))
1184 window = XWINDOW (window)->vchild;
1185 else break;
1186 while (tem = XWINDOW (window)->next, !NILP (tem))
1187 window = tem;
1190 /* Which windows are acceptable?
1191 Exit the loop and accept this window if
1192 this isn't a minibuffer window,
1193 or we're accepting all minibuffer windows,
1194 or this is the active minibuffer and we are accepting that one, or
1195 we've come all the way around and we're back at the original window. */
1196 while (MINI_WINDOW_P (XWINDOW (window))
1197 && ! EQ (minibuf, Qt)
1198 && ! EQ (minibuf, window)
1199 && ! EQ (window, start_window));
1201 return window;
1204 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1205 "Select the ARG'th different window on this frame.\n\
1206 All windows on current frame are arranged in a cyclic order.\n\
1207 This command selects the window ARG steps away in that order.\n\
1208 A negative ARG moves in the opposite order. If the optional second\n\
1209 argument ALL_FRAMES is non-nil, cycle through all frames.")
1210 (arg, all_frames)
1211 register Lisp_Object arg, all_frames;
1213 register int i;
1214 register Lisp_Object w;
1216 CHECK_NUMBER (arg, 0);
1217 w = selected_window;
1218 i = XINT (arg);
1220 while (i > 0)
1222 w = Fnext_window (w, Qnil, all_frames);
1223 i--;
1225 while (i < 0)
1227 w = Fprevious_window (w, Qnil, all_frames);
1228 i++;
1230 Fselect_window (w);
1231 return Qnil;
1234 /* Look at all windows, performing an operation specified by TYPE
1235 with argument OBJ.
1236 If FRAMES is Qt, look at all frames;
1237 Qnil, look at just the selected frame;
1238 Qvisible, look at visible frames;
1239 a frame, just look at windows on that frame.
1240 If MINI is non-zero, perform the operation on minibuffer windows too.
1243 enum window_loop
1245 WINDOW_LOOP_UNUSED,
1246 GET_BUFFER_WINDOW, /* Arg is buffer */
1247 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1248 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1249 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1250 GET_LARGEST_WINDOW,
1251 UNSHOW_BUFFER /* Arg is buffer */
1254 static Lisp_Object
1255 window_loop (type, obj, mini, frames)
1256 enum window_loop type;
1257 register Lisp_Object obj, frames;
1258 int mini;
1260 register Lisp_Object w;
1261 register Lisp_Object best_window;
1262 register Lisp_Object next_window;
1263 register Lisp_Object last_window;
1264 FRAME_PTR frame;
1265 Lisp_Object frame_arg;
1266 frame_arg = Qt;
1268 /* If we're only looping through windows on a particular frame,
1269 frame points to that frame. If we're looping through windows
1270 on all frames, frame is 0. */
1271 if (FRAMEP (frames))
1272 frame = XFRAME (frames);
1273 else if (NILP (frames))
1274 frame = selected_frame;
1275 else
1276 frame = 0;
1277 if (frame)
1278 frame_arg = Qlambda;
1279 else if (XFASTINT (frames) == 0)
1280 frame_arg = frames;
1281 else if (EQ (frames, Qvisible))
1282 frame_arg = frames;
1284 /* frame_arg is Qlambda to stick to one frame,
1285 Qvisible to consider all visible frames,
1286 or Qt otherwise. */
1288 /* Pick a window to start with. */
1289 if (WINDOWP (obj))
1290 w = obj;
1291 else if (frame)
1292 w = FRAME_SELECTED_WINDOW (frame);
1293 else
1294 w = FRAME_SELECTED_WINDOW (selected_frame);
1296 /* Figure out the last window we're going to mess with. Since
1297 Fnext_window, given the same options, is guaranteed to go in a
1298 ring, we can just use Fprevious_window to find the last one.
1300 We can't just wait until we hit the first window again, because
1301 it might be deleted. */
1303 last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
1305 best_window = Qnil;
1306 for (;;)
1308 FRAME_PTR w_frame = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1310 /* Pick the next window now, since some operations will delete
1311 the current window. */
1312 next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
1314 /* Note that we do not pay attention here to whether
1315 the frame is visible, since Fnext_window skips non-visible frames
1316 if that is desired, under the control of frame_arg. */
1317 if (! MINI_WINDOW_P (XWINDOW (w))
1318 || (mini && minibuf_level > 0))
1319 switch (type)
1321 case GET_BUFFER_WINDOW:
1322 if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj)
1323 /* Don't find any minibuffer window
1324 except the one that is currently in use. */
1325 && (MINI_WINDOW_P (XWINDOW (w))
1326 ? EQ (w, minibuf_window) : 1))
1327 return w;
1328 break;
1330 case GET_LRU_WINDOW:
1331 /* t as arg means consider only full-width windows */
1332 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (XWINDOW (w)))
1333 break;
1334 /* Ignore dedicated windows and minibuffers. */
1335 if (MINI_WINDOW_P (XWINDOW (w))
1336 || !NILP (XWINDOW (w)->dedicated))
1337 break;
1338 if (NILP (best_window)
1339 || (XFASTINT (XWINDOW (best_window)->use_time)
1340 > XFASTINT (XWINDOW (w)->use_time)))
1341 best_window = w;
1342 break;
1344 case DELETE_OTHER_WINDOWS:
1345 if (XWINDOW (w) != XWINDOW (obj))
1346 Fdelete_window (w);
1347 break;
1349 case DELETE_BUFFER_WINDOWS:
1350 if (EQ (XWINDOW (w)->buffer, obj))
1352 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1354 /* If this window is dedicated, and in a frame of its own,
1355 kill the frame. */
1356 if (EQ (w, FRAME_ROOT_WINDOW (f))
1357 && !NILP (XWINDOW (w)->dedicated)
1358 && other_visible_frames (f))
1360 /* Skip the other windows on this frame.
1361 There might be one, the minibuffer! */
1362 if (! EQ (w, last_window))
1363 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1365 /* As we go, check for the end of the loop.
1366 We mustn't start going around a second time. */
1367 if (EQ (next_window, last_window))
1369 last_window = w;
1370 break;
1372 next_window = Fnext_window (next_window,
1373 mini ? Qt : Qnil,
1374 frame_arg);
1376 /* Now we can safely delete the frame. */
1377 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1379 else
1380 /* If we're deleting the buffer displayed in the only window
1381 on the frame, find a new buffer to display there. */
1382 if (NILP (XWINDOW (w)->parent))
1384 Lisp_Object new_buffer;
1385 new_buffer = Fother_buffer (obj, Qnil);
1386 if (NILP (new_buffer))
1387 new_buffer
1388 = Fget_buffer_create (build_string ("*scratch*"));
1389 Fset_window_buffer (w, new_buffer);
1390 if (EQ (w, selected_window))
1391 Fset_buffer (XWINDOW (w)->buffer);
1393 else
1394 Fdelete_window (w);
1396 break;
1398 case GET_LARGEST_WINDOW:
1399 /* Ignore dedicated windows and minibuffers. */
1400 if (MINI_WINDOW_P (XWINDOW (w))
1401 || !NILP (XWINDOW (w)->dedicated))
1402 break;
1404 struct window *best_window_ptr = XWINDOW (best_window);
1405 struct window *w_ptr = XWINDOW (w);
1406 if (NILP (best_window)
1407 || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
1408 > (XFASTINT (best_window_ptr->height)
1409 * XFASTINT (best_window_ptr->width))))
1410 best_window = w;
1412 break;
1414 case UNSHOW_BUFFER:
1415 if (EQ (XWINDOW (w)->buffer, obj))
1417 /* Find another buffer to show in this window. */
1418 Lisp_Object another_buffer;
1419 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1420 another_buffer = Fother_buffer (obj, Qnil);
1421 if (NILP (another_buffer))
1422 another_buffer
1423 = Fget_buffer_create (build_string ("*scratch*"));
1424 /* If this window is dedicated, and in a frame of its own,
1425 kill the frame. */
1426 if (EQ (w, FRAME_ROOT_WINDOW (f))
1427 && !NILP (XWINDOW (w)->dedicated)
1428 && other_visible_frames (f))
1430 /* Skip the other windows on this frame.
1431 There might be one, the minibuffer! */
1432 if (! EQ (w, last_window))
1433 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1435 /* As we go, check for the end of the loop.
1436 We mustn't start going around a second time. */
1437 if (EQ (next_window, last_window))
1439 last_window = w;
1440 break;
1442 next_window = Fnext_window (next_window,
1443 mini ? Qt : Qnil,
1444 frame_arg);
1446 /* Now we can safely delete the frame. */
1447 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1449 else
1451 /* Otherwise show a different buffer in the window. */
1452 XWINDOW (w)->dedicated = Qnil;
1453 Fset_window_buffer (w, another_buffer);
1454 if (EQ (w, selected_window))
1455 Fset_buffer (XWINDOW (w)->buffer);
1458 break;
1461 if (EQ (w, last_window))
1462 break;
1464 w = next_window;
1467 return best_window;
1470 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
1471 "Return the window least recently selected or used for display.\n\
1472 If optional argument FRAME is `visible', search all visible frames.\n\
1473 If FRAME is 0, search all visible and iconified frames.\n\
1474 If FRAME is t, search all frames.\n\
1475 If FRAME is nil, search only the selected frame.\n\
1476 If FRAME is a frame, search only that frame.")
1477 (frame)
1478 Lisp_Object frame;
1480 register Lisp_Object w;
1481 /* First try for a window that is full-width */
1482 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
1483 if (!NILP (w) && !EQ (w, selected_window))
1484 return w;
1485 /* If none of them, try the rest */
1486 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
1489 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
1490 "Return the largest window in area.\n\
1491 If optional argument FRAME is `visible', search all visible frames.\n\
1492 If FRAME is 0, search all visible and iconified frames.\n\
1493 If FRAME is t, search all frames.\n\
1494 If FRAME is nil, search only the selected frame.\n\
1495 If FRAME is a frame, search only that frame.")
1496 (frame)
1497 Lisp_Object frame;
1499 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
1500 frame);
1503 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
1504 "Return a window currently displaying BUFFER, or nil if none.\n\
1505 If optional argument FRAME is `visible', search all visible frames.\n\
1506 If optional argument FRAME is 0, search all visible and iconified frames.\n\
1507 If FRAME is t, search all frames.\n\
1508 If FRAME is nil, search only the selected frame.\n\
1509 If FRAME is a frame, search only that frame.")
1510 (buffer, frame)
1511 Lisp_Object buffer, frame;
1513 buffer = Fget_buffer (buffer);
1514 if (BUFFERP (buffer))
1515 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
1516 else
1517 return Qnil;
1520 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
1521 0, 1, "",
1522 "Make WINDOW (or the selected window) fill its frame.\n\
1523 Only the frame WINDOW is on is affected.\n\
1524 This function tries to reduce display jumps\n\
1525 by keeping the text previously visible in WINDOW\n\
1526 in the same place on the frame. Doing this depends on\n\
1527 the value of (window-start WINDOW), so if calling this function\n\
1528 in a program gives strange scrolling, make sure the window-start\n\
1529 value is reasonable when this function is called.")
1530 (window)
1531 Lisp_Object window;
1533 struct window *w;
1534 int startpos;
1535 int top;
1537 if (NILP (window))
1538 window = selected_window;
1539 else
1540 CHECK_LIVE_WINDOW (window, 0);
1542 w = XWINDOW (window);
1544 startpos = marker_position (w->start);
1545 top = XFASTINT (w->top) - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w)));
1547 if (MINI_WINDOW_P (w) && top > 0)
1548 error ("Can't expand minibuffer to full frame");
1550 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
1552 /* Try to minimize scrolling, by setting the window start to the point
1553 will cause the text at the old window start to be at the same place
1554 on the frame. But don't try to do this if the window start is
1555 outside the visible portion (as might happen when the display is
1556 not current, due to typeahead). */
1557 if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
1558 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
1560 struct position pos;
1561 struct buffer *obuf = current_buffer;
1563 Fset_buffer (w->buffer);
1564 /* This computation used to temporarily move point, but that can
1565 have unwanted side effects due to text properties. */
1566 pos = *vmotion (startpos, -top, w);
1568 Fset_marker (w->start, make_number (pos.bufpos), w->buffer);
1569 w->start_at_line_beg = ((pos.bufpos == BEGV
1570 || FETCH_CHAR (pos.bufpos - 1) == '\n') ? Qt
1571 : Qnil);
1572 /* We need to do this, so that the window-scroll-functions
1573 get called. */
1574 w->optional_new_start = Qt;
1576 set_buffer_internal (obuf);
1578 return Qnil;
1581 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
1582 1, 2, "bDelete windows on (buffer): ",
1583 "Delete all windows showing BUFFER.\n\
1584 Optional second argument FRAME controls which frames are affected.\n\
1585 If nil or omitted, delete all windows showing BUFFER in any frame.\n\
1586 If t, delete only windows showing BUFFER in the selected frame.\n\
1587 If `visible', delete all windows showing BUFFER in any visible frame.\n\
1588 If a frame, delete only windows showing BUFFER in that frame.")
1589 (buffer, frame)
1590 Lisp_Object buffer, frame;
1592 /* FRAME uses t and nil to mean the opposite of what window_loop
1593 expects. */
1594 if (! FRAMEP (frame))
1595 frame = NILP (frame) ? Qt : Qnil;
1597 if (!NILP (buffer))
1599 buffer = Fget_buffer (buffer);
1600 CHECK_BUFFER (buffer, 0);
1601 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
1603 return Qnil;
1606 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
1607 Sreplace_buffer_in_windows,
1608 1, 1, "bReplace buffer in windows: ",
1609 "Replace BUFFER with some other buffer in all windows showing it.")
1610 (buffer)
1611 Lisp_Object buffer;
1613 if (!NILP (buffer))
1615 buffer = Fget_buffer (buffer);
1616 CHECK_BUFFER (buffer, 0);
1617 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
1619 return Qnil;
1622 /* Replace BUFFER with some other buffer in all windows
1623 of all frames, even those on other keyboards. */
1625 void
1626 replace_buffer_in_all_windows (buffer)
1627 Lisp_Object buffer;
1629 #ifdef MULTI_KBOARD
1630 Lisp_Object tail, frame;
1632 /* A single call to window_loop won't do the job
1633 because it only considers frames on the current keyboard.
1634 So loop manually over frames, and handle each one. */
1635 FOR_EACH_FRAME (tail, frame)
1636 window_loop (UNSHOW_BUFFER, buffer, 0, frame);
1637 #else
1638 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
1639 #endif
1642 /* Set the height of WINDOW and all its inferiors. */
1644 /* The smallest acceptable dimensions for a window. Anything smaller
1645 might crash Emacs. */
1646 #define MIN_SAFE_WINDOW_WIDTH (2)
1647 #define MIN_SAFE_WINDOW_HEIGHT (2)
1649 /* Make sure that window_min_height and window_min_width are
1650 not too small; if they are, set them to safe minima. */
1652 static void
1653 check_min_window_sizes ()
1655 /* Smaller values might permit a crash. */
1656 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
1657 window_min_width = MIN_SAFE_WINDOW_WIDTH;
1658 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
1659 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
1662 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
1663 minimum allowable size. */
1664 void
1665 check_frame_size (frame, rows, cols)
1666 FRAME_PTR frame;
1667 int *rows, *cols;
1669 /* For height, we have to see:
1670 whether the frame has a minibuffer,
1671 whether it wants a mode line, and
1672 whether it has a menu bar. */
1673 int min_height =
1674 (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
1675 : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
1676 : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
1677 if (FRAME_MENU_BAR_LINES (frame) > 0)
1678 min_height += FRAME_MENU_BAR_LINES (frame);
1680 if (*rows < min_height)
1681 *rows = min_height;
1682 if (*cols < MIN_SAFE_WINDOW_WIDTH)
1683 *cols = MIN_SAFE_WINDOW_WIDTH;
1686 /* Normally the window is deleted if it gets too small.
1687 nodelete nonzero means do not do this.
1688 (The caller should check later and do so if appropriate) */
1690 set_window_height (window, height, nodelete)
1691 Lisp_Object window;
1692 int height;
1693 int nodelete;
1695 register struct window *w = XWINDOW (window);
1696 register struct window *c;
1697 int oheight = XFASTINT (w->height);
1698 int top, pos, lastbot, opos, lastobot;
1699 Lisp_Object child;
1701 check_min_window_sizes ();
1703 if (!nodelete
1704 && ! NILP (w->parent)
1705 && height < window_min_height)
1707 Fdelete_window (window);
1708 return;
1711 XSETFASTINT (w->last_modified, 0);
1712 XSETFASTINT (w->last_overlay_modified, 0);
1713 windows_or_buffers_changed++;
1714 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1716 XSETFASTINT (w->height, height);
1717 if (!NILP (w->hchild))
1719 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
1721 XWINDOW (child)->top = w->top;
1722 set_window_height (child, height, nodelete);
1725 else if (!NILP (w->vchild))
1727 lastbot = top = XFASTINT (w->top);
1728 lastobot = 0;
1729 for (child = w->vchild; !NILP (child); child = c->next)
1731 c = XWINDOW (child);
1733 opos = lastobot + XFASTINT (c->height);
1735 XSETFASTINT (c->top, lastbot);
1737 pos = (((opos * height) << 1) + oheight) / (oheight << 1);
1739 /* Avoid confusion: inhibit deletion of child if becomes too small */
1740 set_window_height (child, pos + top - lastbot, 1);
1742 /* Now advance child to next window,
1743 and set lastbot if child was not just deleted. */
1744 lastbot = pos + top;
1745 lastobot = opos;
1747 /* Now delete any children that became too small. */
1748 if (!nodelete)
1749 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
1751 set_window_height (child, XINT (XWINDOW (child)->height), 0);
1756 /* Recursively set width of WINDOW and its inferiors. */
1758 set_window_width (window, width, nodelete)
1759 Lisp_Object window;
1760 int width;
1761 int nodelete;
1763 register struct window *w = XWINDOW (window);
1764 register struct window *c;
1765 int owidth = XFASTINT (w->width);
1766 int left, pos, lastright, opos, lastoright;
1767 Lisp_Object child;
1769 if (!nodelete && width < window_min_width && !NILP (w->parent))
1771 Fdelete_window (window);
1772 return;
1775 XSETFASTINT (w->last_modified, 0);
1776 XSETFASTINT (w->last_overlay_modified, 0);
1777 windows_or_buffers_changed++;
1778 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1780 XSETFASTINT (w->width, width);
1781 if (!NILP (w->vchild))
1783 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
1785 XWINDOW (child)->left = w->left;
1786 set_window_width (child, width, nodelete);
1789 else if (!NILP (w->hchild))
1791 lastright = left = XFASTINT (w->left);
1792 lastoright = 0;
1793 for (child = w->hchild; !NILP (child); child = c->next)
1795 c = XWINDOW (child);
1797 opos = lastoright + XFASTINT (c->width);
1799 XSETFASTINT (c->left, lastright);
1801 pos = (((opos * width) << 1) + owidth) / (owidth << 1);
1803 /* Inhibit deletion for becoming too small */
1804 set_window_width (child, pos + left - lastright, 1);
1806 /* Now advance child to next window,
1807 and set lastright if child was not just deleted. */
1808 lastright = pos + left, lastoright = opos;
1810 /* Delete children that became too small */
1811 if (!nodelete)
1812 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
1814 set_window_width (child, XINT (XWINDOW (child)->width), 0);
1819 int window_select_count;
1821 Lisp_Object
1822 Fset_window_buffer_unwind (obuf)
1823 Lisp_Object obuf;
1825 Fset_buffer (obuf);
1826 return Qnil;
1829 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
1830 "Make WINDOW display BUFFER as its contents.\n\
1831 BUFFER can be a buffer or buffer name.")
1832 (window, buffer)
1833 register Lisp_Object window, buffer;
1835 register Lisp_Object tem;
1836 register struct window *w = decode_window (window);
1837 int count = specpdl_ptr - specpdl;
1839 buffer = Fget_buffer (buffer);
1840 CHECK_BUFFER (buffer, 1);
1842 if (NILP (XBUFFER (buffer)->name))
1843 error ("Attempt to display deleted buffer");
1845 tem = w->buffer;
1846 if (NILP (tem))
1847 error ("Window is deleted");
1848 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
1849 is first being set up. */
1851 if (!NILP (w->dedicated) && !EQ (tem, buffer))
1852 error ("Window is dedicated to `%s'",
1853 XSTRING (XBUFFER (tem)->name)->data);
1855 unshow_buffer (w);
1858 w->buffer = buffer;
1860 if (EQ (window, selected_window))
1861 XBUFFER (w->buffer)->last_selected_window = window;
1863 XSETFASTINT (w->window_end_pos, 0);
1864 w->window_end_valid = Qnil;
1865 XSETFASTINT (w->hscroll, 0);
1866 Fset_marker (w->pointm,
1867 make_number (BUF_PT (XBUFFER (buffer))),
1868 buffer);
1869 set_marker_restricted (w->start,
1870 make_number (XBUFFER (buffer)->last_window_start),
1871 buffer);
1872 w->start_at_line_beg = Qnil;
1873 w->force_start = Qnil;
1874 XSETFASTINT (w->last_modified, 0);
1875 XSETFASTINT (w->last_overlay_modified, 0);
1876 windows_or_buffers_changed++;
1878 /* We must select BUFFER for running the window-scroll-functions.
1879 If WINDOW is selected, switch permanently.
1880 Otherwise, switch but go back to the ambient buffer afterward. */
1881 if (EQ (window, selected_window))
1882 Fset_buffer (buffer);
1883 /* We can't check ! NILP (Vwindow_scroll_functions) here
1884 because that might itself be a local variable. */
1885 else if (window_initialized)
1887 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
1888 Fset_buffer (buffer);
1891 if (! NILP (Vwindow_scroll_functions))
1892 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1893 Fmarker_position (w->start));
1895 unbind_to (count, Qnil);
1897 return Qnil;
1900 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
1901 "Select WINDOW. Most editing will apply to WINDOW's buffer.\n\
1902 The main editor command loop selects the buffer of the selected window\n\
1903 before each command.")
1904 (window)
1905 register Lisp_Object window;
1907 register struct window *w;
1908 register struct window *ow = XWINDOW (selected_window);
1910 CHECK_LIVE_WINDOW (window, 0);
1912 w = XWINDOW (window);
1914 if (NILP (w->buffer))
1915 error ("Trying to select deleted window or non-leaf window");
1917 XSETFASTINT (w->use_time, ++window_select_count);
1918 if (EQ (window, selected_window))
1919 return window;
1921 Fset_marker (ow->pointm, make_number (BUF_PT (XBUFFER (ow->buffer))),
1922 ow->buffer);
1924 selected_window = window;
1925 if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
1927 XFRAME (WINDOW_FRAME (w))->selected_window = window;
1928 /* Use this rather than Fhandle_switch_frame
1929 so that FRAME_FOCUS_FRAME is moved appropriately as we
1930 move around in the state where a minibuffer in a separate
1931 frame is active. */
1932 Fselect_frame (WINDOW_FRAME (w), Qnil);
1934 else
1935 selected_frame->selected_window = window;
1937 record_buffer (w->buffer);
1938 Fset_buffer (w->buffer);
1940 XBUFFER (w->buffer)->last_selected_window = window;
1942 /* Go to the point recorded in the window.
1943 This is important when the buffer is in more
1944 than one window. It also matters when
1945 redisplay_window has altered point after scrolling,
1946 because it makes the change only in the window. */
1948 register int new_point = marker_position (w->pointm);
1949 if (new_point < BEGV)
1950 SET_PT (BEGV);
1951 else if (new_point > ZV)
1952 SET_PT (ZV);
1953 else
1954 SET_PT (new_point);
1957 windows_or_buffers_changed++;
1958 return window;
1961 /* Deiconify the frame containing the window WINDOW,
1962 unless it is the selected frame;
1963 then return WINDOW.
1965 The reason for the exception for the selected frame
1966 is that it seems better not to change the selected frames visibility
1967 merely because of displaying a different buffer in it.
1968 The deiconification is useful when a buffer gets shown in
1969 another frame that you were not using lately. */
1971 static Lisp_Object
1972 display_buffer_1 (window)
1973 Lisp_Object window;
1975 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
1976 FRAME_SAMPLE_VISIBILITY (f);
1977 if (f != selected_frame)
1979 if (FRAME_ICONIFIED_P (f))
1980 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
1981 else if (FRAME_VISIBLE_P (f))
1982 Fraise_frame (WINDOW_FRAME (XWINDOW (window)));
1984 return window;
1987 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
1988 "Returns non-nil if a buffer named BUFFER-NAME would be created specially.\n\
1989 The value is actually t if the frame should be called with default frame\n\
1990 parameters, and a list of frame parameters if they were specified.\n\
1991 See `special-display-buffer-names', and `special-display-regexps'.")
1992 (buffer_name)
1993 Lisp_Object buffer_name;
1995 Lisp_Object tem;
1997 CHECK_STRING (buffer_name, 1);
1999 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
2000 if (!NILP (tem))
2001 return Qt;
2003 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
2004 if (!NILP (tem))
2005 return XCDR (tem);
2007 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
2009 Lisp_Object car = XCAR (tem);
2010 if (STRINGP (car)
2011 && fast_string_match (car, buffer_name) >= 0)
2012 return Qt;
2013 else if (CONSP (car)
2014 && STRINGP (XCAR (car))
2015 && fast_string_match (XCAR (car), buffer_name) >= 0)
2016 return XCDR (tem);
2018 return Qnil;
2021 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
2022 "Returns non-nil if a new buffer named BUFFER-NAME would use the same window.\n\
2023 See `same-window-buffer-names' and `same-window-regexps'.")
2024 (buffer_name)
2025 Lisp_Object buffer_name;
2027 Lisp_Object tem;
2029 CHECK_STRING (buffer_name, 1);
2031 tem = Fmember (buffer_name, Vsame_window_buffer_names);
2032 if (!NILP (tem))
2033 return Qt;
2035 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
2036 if (!NILP (tem))
2037 return Qt;
2039 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
2041 Lisp_Object car = XCAR (tem);
2042 if (STRINGP (car)
2043 && fast_string_match (car, buffer_name) >= 0)
2044 return Qt;
2045 else if (CONSP (car)
2046 && STRINGP (XCAR (car))
2047 && fast_string_match (XCAR (car), buffer_name) >= 0)
2048 return Qt;
2050 return Qnil;
2053 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 2,
2054 "bDisplay buffer: \nP",
2055 "Make BUFFER appear in some window but don't select it.\n\
2056 BUFFER can be a buffer or a buffer name.\n\
2057 If BUFFER is shown already in some window, just use that one,\n\
2058 unless the window is the selected window and the optional second\n\
2059 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
2060 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
2061 Returns the window displaying BUFFER.\n\
2063 The variables `special-display-buffer-names', `special-display-regexps',\n\
2064 `same-window-buffer-names', and `same-window-regexps' customize how certain\n\
2065 buffer names are handled.")
2066 (buffer, not_this_window)
2067 register Lisp_Object buffer, not_this_window;
2069 register Lisp_Object window, tem;
2071 buffer = Fget_buffer (buffer);
2072 CHECK_BUFFER (buffer, 0);
2074 if (!NILP (Vdisplay_buffer_function))
2075 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2077 if (NILP (not_this_window)
2078 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
2079 return display_buffer_1 (selected_window);
2081 /* See if the user has specified this buffer should appear
2082 in the selected window. */
2083 if (NILP (not_this_window))
2085 tem = Fsame_window_p (XBUFFER (buffer)->name);
2086 if (!NILP (tem))
2088 Fswitch_to_buffer (buffer, Qnil);
2089 return display_buffer_1 (selected_window);
2093 /* If pop_up_frames,
2094 look for a window showing BUFFER on any visible or iconified frame.
2095 Otherwise search only the current frame. */
2096 if (pop_up_frames || last_nonminibuf_frame == 0)
2097 XSETFASTINT (tem, 0);
2098 else
2099 XSETFRAME (tem, last_nonminibuf_frame);
2100 window = Fget_buffer_window (buffer, tem);
2101 if (!NILP (window)
2102 && (NILP (not_this_window) || !EQ (window, selected_window)))
2104 return display_buffer_1 (window);
2107 /* Certain buffer names get special handling. */
2108 if (!NILP (Vspecial_display_function))
2110 tem = Fspecial_display_p (XBUFFER (buffer)->name);
2111 if (EQ (tem, Qt))
2112 return call1 (Vspecial_display_function, buffer);
2113 if (CONSP (tem))
2114 return call2 (Vspecial_display_function, buffer, tem);
2117 /* If there are no frames open that have more than a minibuffer,
2118 we need to create a new frame. */
2119 if (pop_up_frames || last_nonminibuf_frame == 0)
2121 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2122 Fset_window_buffer (window, buffer);
2123 return display_buffer_1 (window);
2126 if (pop_up_windows
2127 || FRAME_MINIBUF_ONLY_P (selected_frame)
2128 /* If the current frame is a special display frame,
2129 don't try to reuse its windows. */
2130 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->dedicated)
2133 Lisp_Object frames;
2135 frames = Qnil;
2136 if (FRAME_MINIBUF_ONLY_P (selected_frame))
2137 XSETFRAME (frames, last_nonminibuf_frame);
2138 /* Don't try to create a window if would get an error */
2139 if (split_height_threshold < window_min_height << 1)
2140 split_height_threshold = window_min_height << 1;
2142 /* Note that both Fget_largest_window and Fget_lru_window
2143 ignore minibuffers and dedicated windows.
2144 This means they can return nil. */
2146 /* If the frame we would try to split cannot be split,
2147 try other frames. */
2148 if (FRAME_NO_SPLIT_P (NILP (frames) ? selected_frame
2149 : last_nonminibuf_frame))
2151 /* Try visible frames first. */
2152 window = Fget_largest_window (Qvisible);
2153 /* If that didn't work, try iconified frames. */
2154 if (NILP (window))
2155 window = Fget_largest_window (make_number (0));
2156 if (NILP (window))
2157 window = Fget_largest_window (Qt);
2159 else
2160 window = Fget_largest_window (frames);
2162 /* If we got a tall enough full-width window that can be split,
2163 split it. */
2164 if (!NILP (window)
2165 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2166 && window_height (window) >= split_height_threshold
2167 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
2168 window = Fsplit_window (window, Qnil, Qnil);
2169 else
2171 Lisp_Object upper, lower, other;
2173 window = Fget_lru_window (frames);
2174 /* If the LRU window is selected, and big enough,
2175 and can be split, split it. */
2176 if (!NILP (window)
2177 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2178 && (EQ (window, selected_window)
2179 || EQ (XWINDOW (window)->parent, Qnil))
2180 && window_height (window) >= window_min_height << 1)
2181 window = Fsplit_window (window, Qnil, Qnil);
2182 /* If Fget_lru_window returned nil, try other approaches. */
2183 /* Try visible frames first. */
2184 if (NILP (window))
2185 window = Fget_largest_window (Qvisible);
2186 /* If that didn't work, try iconified frames. */
2187 if (NILP (window))
2188 window = Fget_largest_window (make_number (0));
2189 /* Try invisible frames. */
2190 if (NILP (window))
2191 window = Fget_largest_window (Qt);
2192 /* As a last resort, make a new frame. */
2193 if (NILP (window))
2194 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2195 /* If window appears above or below another,
2196 even out their heights. */
2197 other = upper = lower = Qnil;
2198 if (!NILP (XWINDOW (window)->prev))
2199 other = upper = XWINDOW (window)->prev, lower = window;
2200 if (!NILP (XWINDOW (window)->next))
2201 other = lower = XWINDOW (window)->next, upper = window;
2202 if (!NILP (other)
2203 /* Check that OTHER and WINDOW are vertically arrayed. */
2204 && XWINDOW (other)->top != XWINDOW (window)->top
2205 && XWINDOW (other)->height > XWINDOW (window)->height)
2207 int total = XWINDOW (other)->height + XWINDOW (window)->height;
2208 Lisp_Object old_selected_window;
2209 old_selected_window = selected_window;
2211 selected_window = upper;
2212 change_window_height (total / 2 - XWINDOW (upper)->height, 0);
2213 selected_window = old_selected_window;
2217 else
2218 window = Fget_lru_window (Qnil);
2220 Fset_window_buffer (window, buffer);
2221 return display_buffer_1 (window);
2224 void
2225 temp_output_buffer_show (buf)
2226 register Lisp_Object buf;
2228 register struct buffer *old = current_buffer;
2229 register Lisp_Object window;
2230 register struct window *w;
2232 Fset_buffer (buf);
2233 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
2234 BEGV = BEG;
2235 ZV = Z;
2236 SET_PT (BEG);
2237 XBUFFER (buf)->clip_changed = 1;
2238 set_buffer_internal (old);
2240 if (!EQ (Vtemp_buffer_show_function, Qnil))
2241 call1 (Vtemp_buffer_show_function, buf);
2242 else
2244 window = Fdisplay_buffer (buf, Qnil);
2246 if (XFRAME (XWINDOW (window)->frame) != selected_frame)
2247 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2248 Vminibuf_scroll_window = window;
2249 w = XWINDOW (window);
2250 XSETFASTINT (w->hscroll, 0);
2251 set_marker_restricted (w->start, make_number (1), buf);
2252 set_marker_restricted (w->pointm, make_number (1), buf);
2254 /* Run temp-buffer-show-hook, with the chosen window selected. */
2255 if (!NILP (Vrun_hooks))
2257 Lisp_Object tem;
2258 tem = Fboundp (Qtemp_buffer_show_hook);
2259 if (!NILP (tem))
2261 tem = Fsymbol_value (Qtemp_buffer_show_hook);
2262 if (!NILP (tem))
2264 int count = specpdl_ptr - specpdl;
2266 /* Select the window that was chosen, for running the hook. */
2267 record_unwind_protect (Fset_window_configuration,
2268 Fcurrent_window_configuration (Qnil));
2270 Fselect_window (window);
2271 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
2272 unbind_to (count, Qnil);
2279 static
2280 make_dummy_parent (window)
2281 Lisp_Object window;
2283 Lisp_Object new;
2284 register struct window *o, *p;
2285 register struct Lisp_Vector *vec;
2286 int i;
2288 o = XWINDOW (window);
2289 vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
2290 for (i = 0; i < VECSIZE (struct window); ++i)
2291 vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
2292 vec->size = VECSIZE (struct window);
2293 p = (struct window *)vec;
2294 XSETWINDOW (new, p);
2296 XSETFASTINT (p->sequence_number, ++sequence_number);
2298 /* Put new into window structure in place of window */
2299 replace_window (window, new);
2301 o->next = Qnil;
2302 o->prev = Qnil;
2303 o->vchild = Qnil;
2304 o->hchild = Qnil;
2305 o->parent = new;
2307 p->start = Qnil;
2308 p->pointm = Qnil;
2309 p->buffer = Qnil;
2312 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
2313 "Split WINDOW, putting SIZE lines in the first of the pair.\n\
2314 WINDOW defaults to selected one and SIZE to half its size.\n\
2315 If optional third arg HORFLAG is non-nil, split side by side\n\
2316 and put SIZE columns in the first of the pair.")
2317 (window, size, horflag)
2318 Lisp_Object window, size, horflag;
2320 register Lisp_Object new;
2321 register struct window *o, *p;
2322 FRAME_PTR fo;
2323 register int size_int;
2325 if (NILP (window))
2326 window = selected_window;
2327 else
2328 CHECK_LIVE_WINDOW (window, 0);
2330 o = XWINDOW (window);
2331 fo = XFRAME (WINDOW_FRAME (o));
2333 if (NILP (size))
2335 if (!NILP (horflag))
2336 /* Calculate the size of the left-hand window, by dividing
2337 the usable space in columns by two. */
2338 size_int = XFASTINT (o->width) >> 1;
2339 else
2340 size_int = XFASTINT (o->height) >> 1;
2342 else
2344 CHECK_NUMBER (size, 1);
2345 size_int = XINT (size);
2348 if (MINI_WINDOW_P (o))
2349 error ("Attempt to split minibuffer window");
2350 else if (FRAME_NO_SPLIT_P (fo))
2351 error ("Attempt to split unsplittable frame");
2353 check_min_window_sizes ();
2355 if (NILP (horflag))
2357 if (size_int < window_min_height)
2358 error ("Window height %d too small (after splitting)", size_int);
2359 if (size_int + window_min_height > XFASTINT (o->height))
2360 error ("Window height %d too small (after splitting)",
2361 XFASTINT (o->height) - size_int);
2362 if (NILP (o->parent)
2363 || NILP (XWINDOW (o->parent)->vchild))
2365 make_dummy_parent (window);
2366 new = o->parent;
2367 XWINDOW (new)->vchild = window;
2370 else
2372 if (size_int < window_min_width)
2373 error ("Window width %d too small (after splitting)", size_int);
2375 if (size_int + window_min_width > XFASTINT (o->width))
2376 error ("Window width %d too small (after splitting)",
2377 XFASTINT (o->width) - size_int);
2378 if (NILP (o->parent)
2379 || NILP (XWINDOW (o->parent)->hchild))
2381 make_dummy_parent (window);
2382 new = o->parent;
2383 XWINDOW (new)->hchild = window;
2387 /* Now we know that window's parent is a vertical combination
2388 if we are dividing vertically, or a horizontal combination
2389 if we are making side-by-side windows */
2391 windows_or_buffers_changed++;
2392 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
2393 new = make_window ();
2394 p = XWINDOW (new);
2396 p->frame = o->frame;
2397 p->next = o->next;
2398 if (!NILP (p->next))
2399 XWINDOW (p->next)->prev = new;
2400 p->prev = window;
2401 o->next = new;
2402 p->parent = o->parent;
2403 p->buffer = Qt;
2405 Fset_window_buffer (new, o->buffer);
2407 /* Apportion the available frame space among the two new windows */
2409 if (!NILP (horflag))
2411 p->height = o->height;
2412 p->top = o->top;
2413 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
2414 XSETFASTINT (o->width, size_int);
2415 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
2417 else
2419 p->left = o->left;
2420 p->width = o->width;
2421 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
2422 XSETFASTINT (o->height, size_int);
2423 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
2426 return new;
2429 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
2430 "Make current window ARG lines bigger.\n\
2431 From program, optional second arg non-nil means grow sideways ARG columns.")
2432 (arg, side)
2433 register Lisp_Object arg, side;
2435 CHECK_NUMBER (arg, 0);
2436 change_window_height (XINT (arg), !NILP (side));
2437 return Qnil;
2440 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
2441 "Make current window ARG lines smaller.\n\
2442 From program, optional second arg non-nil means shrink sideways arg columns.")
2443 (arg, side)
2444 register Lisp_Object arg, side;
2446 CHECK_NUMBER (arg, 0);
2447 change_window_height (-XINT (arg), !NILP (side));
2448 return Qnil;
2452 window_height (window)
2453 Lisp_Object window;
2455 register struct window *p = XWINDOW (window);
2456 return XFASTINT (p->height);
2460 window_width (window)
2461 Lisp_Object window;
2463 register struct window *p = XWINDOW (window);
2464 return XFASTINT (p->width);
2467 #define MINSIZE(w) \
2468 (widthflag \
2469 ? window_min_width \
2470 : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
2472 #define CURBEG(w) \
2473 *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
2475 #define CURSIZE(w) \
2476 *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
2478 /* Unlike set_window_height, this function
2479 also changes the heights of the siblings so as to
2480 keep everything consistent. */
2482 change_window_height (delta, widthflag)
2483 register int delta;
2484 int widthflag;
2486 register Lisp_Object parent;
2487 Lisp_Object window;
2488 register struct window *p;
2489 int *sizep;
2490 int (*sizefun) () = widthflag ? window_width : window_height;
2491 register int (*setsizefun) () = (widthflag
2492 ? set_window_width
2493 : set_window_height);
2495 check_min_window_sizes ();
2497 window = selected_window;
2498 while (1)
2500 p = XWINDOW (window);
2501 parent = p->parent;
2502 if (NILP (parent))
2504 if (widthflag)
2505 error ("No other window to side of this one");
2506 break;
2508 if (widthflag ? !NILP (XWINDOW (parent)->hchild)
2509 : !NILP (XWINDOW (parent)->vchild))
2510 break;
2511 window = parent;
2514 sizep = &CURSIZE (window);
2517 register int maxdelta;
2519 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
2520 : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
2521 : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
2522 /* This is a frame with only one window, a minibuffer-only
2523 or a minibufferless frame. */
2524 : (delta = 0));
2526 if (delta > maxdelta)
2527 /* This case traps trying to make the minibuffer
2528 the full frame, or make the only window aside from the
2529 minibuffer the full frame. */
2530 delta = maxdelta;
2533 if (*sizep + delta < MINSIZE (window))
2535 Fdelete_window (window);
2536 return;
2539 if (delta == 0)
2540 return;
2542 if (!NILP (p->next)
2543 && (*sizefun) (p->next) - delta >= MINSIZE (p->next))
2545 (*setsizefun) (p->next, (*sizefun) (p->next) - delta, 0);
2546 (*setsizefun) (window, *sizep + delta, 0);
2547 CURBEG (p->next) += delta;
2548 /* This does not change size of p->next,
2549 but it propagates the new top edge to its children */
2550 (*setsizefun) (p->next, (*sizefun) (p->next), 0);
2552 else if (!NILP (p->prev)
2553 && (*sizefun) (p->prev) - delta >= MINSIZE (p->prev))
2555 (*setsizefun) (p->prev, (*sizefun) (p->prev) - delta, 0);
2556 CURBEG (window) -= delta;
2557 (*setsizefun) (window, *sizep + delta, 0);
2559 else
2561 register int delta1;
2562 register int opht = (*sizefun) (parent);
2564 /* If trying to grow this window to or beyond size of the parent,
2565 make delta1 so big that, on shrinking back down,
2566 all the siblings end up with less than one line and are deleted. */
2567 if (opht <= *sizep + delta)
2568 delta1 = opht * opht * 2;
2569 /* Otherwise, make delta1 just right so that if we add delta1
2570 lines to this window and to the parent, and then shrink
2571 the parent back to its original size, the new proportional
2572 size of this window will increase by delta. */
2573 else
2574 delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
2576 /* Add delta1 lines or columns to this window, and to the parent,
2577 keeping things consistent while not affecting siblings. */
2578 CURSIZE (parent) = opht + delta1;
2579 (*setsizefun) (window, *sizep + delta1, 0);
2581 /* Squeeze out delta1 lines or columns from our parent,
2582 shriking this window and siblings proportionately.
2583 This brings parent back to correct size.
2584 Delta1 was calculated so this makes this window the desired size,
2585 taking it all out of the siblings. */
2586 (*setsizefun) (parent, opht, 0);
2589 XSETFASTINT (p->last_modified, 0);
2590 XSETFASTINT (p->last_overlay_modified, 0);
2592 #undef MINSIZE
2593 #undef CURBEG
2594 #undef CURSIZE
2597 /* Return number of lines of text (not counting mode line) in W. */
2600 window_internal_height (w)
2601 struct window *w;
2603 int ht = XFASTINT (w->height);
2605 if (MINI_WINDOW_P (w))
2606 return ht;
2608 if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
2609 || !NILP (w->next) || !NILP (w->prev)
2610 || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
2611 return ht - 1;
2613 return ht;
2617 /* Return the number of columns in W.
2618 Don't count columns occupied by scroll bars or the vertical bar
2619 separating W from the sibling to its right. */
2621 window_internal_width (w)
2622 struct window *w;
2624 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2625 int width = XINT (w->width);
2627 /* Scroll bars occupy a few columns. */
2628 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2629 return width - FRAME_SCROLL_BAR_COLS (f);
2631 /* The column of `|' characters separating side-by-side windows
2632 occupies one column only. */
2633 if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
2634 return width - 1;
2636 return width;
2640 /* Scroll contents of window WINDOW up N lines.
2641 If WHOLE is nonzero, it means we wanted to scroll
2642 by entire screenfuls. */
2644 static void
2645 window_scroll (window, n, whole, noerror)
2646 Lisp_Object window;
2647 int n;
2648 int whole;
2649 int noerror;
2651 register struct window *w = XWINDOW (window);
2652 register int opoint = PT;
2653 register int pos;
2654 register int ht = window_internal_height (w);
2655 register Lisp_Object tem;
2656 int lose;
2657 Lisp_Object bolp, nmoved;
2658 int startpos;
2659 struct position posit;
2660 int original_vpos;
2662 startpos = marker_position (w->start);
2664 posit = *compute_motion (startpos, 0, 0, 0,
2665 PT, ht, 0,
2666 window_internal_width (w), XINT (w->hscroll),
2667 0, w);
2668 original_vpos = posit.vpos;
2670 XSETFASTINT (tem, PT);
2671 tem = Fpos_visible_in_window_p (tem, window);
2673 if (NILP (tem))
2675 Fvertical_motion (make_number (- (ht / 2)), window);
2676 startpos = PT;
2679 SET_PT (startpos);
2680 lose = n < 0 && PT == BEGV;
2681 Fvertical_motion (make_number (n), window);
2682 pos = PT;
2683 bolp = Fbolp ();
2684 SET_PT (opoint);
2686 if (lose)
2688 if (noerror)
2689 return;
2690 else
2691 Fsignal (Qbeginning_of_buffer, Qnil);
2694 if (pos < ZV)
2696 extern int scroll_margin;
2698 int this_scroll_margin = scroll_margin;
2700 /* Don't use a scroll margin that is negative or too large. */
2701 if (this_scroll_margin < 0)
2702 this_scroll_margin = 0;
2704 if (XINT (w->height) < 4 * scroll_margin)
2705 this_scroll_margin = XINT (w->height) / 4;
2707 set_marker_restricted (w->start, make_number (pos), w->buffer);
2708 w->start_at_line_beg = bolp;
2709 w->update_mode_line = Qt;
2710 XSETFASTINT (w->last_modified, 0);
2711 XSETFASTINT (w->last_overlay_modified, 0);
2712 /* Set force_start so that redisplay_window will run
2713 the window-scroll-functions. */
2714 w->force_start = Qt;
2716 if (whole)
2718 SET_PT (pos);
2719 Fvertical_motion (make_number (original_vpos), window);
2721 /* If we scrolled forward, put point enough lines down
2722 that it is outside the scroll margin. */
2723 else if (n > 0)
2725 int top_margin;
2727 if (this_scroll_margin > 0)
2729 SET_PT (pos);
2730 Fvertical_motion (make_number (this_scroll_margin), window);
2731 top_margin = PT;
2733 else
2734 top_margin = pos;
2736 if (top_margin <= opoint)
2737 SET_PT (opoint);
2738 else
2740 SET_PT (pos);
2741 Fvertical_motion (make_number (original_vpos), window);
2744 else if (n < 0)
2746 int bottom_margin;
2748 /* If we scrolled backward, put point near the end of the window
2749 but not within the scroll margin. */
2750 SET_PT (pos);
2751 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
2752 if (XFASTINT (tem) == ht - this_scroll_margin)
2753 bottom_margin = PT;
2754 else
2755 bottom_margin = PT + 1;
2757 if (bottom_margin > opoint)
2758 SET_PT (opoint);
2759 else
2761 SET_PT (pos);
2762 Fvertical_motion (make_number (original_vpos), window);
2766 else
2768 if (noerror)
2769 return;
2770 else
2771 Fsignal (Qend_of_buffer, Qnil);
2775 /* This is the guts of Fscroll_up and Fscroll_down. */
2777 static void
2778 scroll_command (n, direction)
2779 register Lisp_Object n;
2780 int direction;
2782 register int defalt;
2783 int count = specpdl_ptr - specpdl;
2785 /* If selected window's buffer isn't current, make it current for the moment.
2786 But don't screw up if window_scroll gets an error. */
2787 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2789 record_unwind_protect (save_excursion_restore, save_excursion_save ());
2790 Fset_buffer (XWINDOW (selected_window)->buffer);
2793 defalt = (window_internal_height (XWINDOW (selected_window))
2794 - next_screen_context_lines);
2795 defalt = direction * (defalt < 1 ? 1 : defalt);
2797 if (NILP (n))
2798 window_scroll (selected_window, defalt, 1, 0);
2799 else if (EQ (n, Qminus))
2800 window_scroll (selected_window, - defalt, 1, 0);
2801 else
2803 n = Fprefix_numeric_value (n);
2804 window_scroll (selected_window, XINT (n) * direction, 0, 0);
2807 unbind_to (count, Qnil);
2810 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
2811 "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
2812 A near full screen is `next-screen-context-lines' less than a full screen.\n\
2813 Negative ARG means scroll downward.\n\
2814 When calling from a program, supply a number as argument or nil.")
2815 (arg)
2816 Lisp_Object arg;
2818 scroll_command (arg, 1);
2819 return Qnil;
2822 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
2823 "Scroll text of current window downward ARG lines; or near full screen if no ARG.\n\
2824 A near full screen is `next-screen-context-lines' less than a full screen.\n\
2825 Negative ARG means scroll upward.\n\
2826 When calling from a program, supply a number as argument or nil.")
2827 (arg)
2828 Lisp_Object arg;
2830 scroll_command (arg, -1);
2831 return Qnil;
2834 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
2835 "Return the other window for \"other window scroll\" commands.\n\
2836 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
2837 specifies the window.\n\
2838 If `other-window-scroll-buffer' is non-nil, a window\n\
2839 showing that buffer is used.")
2842 Lisp_Object window;
2844 if (MINI_WINDOW_P (XWINDOW (selected_window))
2845 && !NILP (Vminibuf_scroll_window))
2846 window = Vminibuf_scroll_window;
2847 /* If buffer is specified, scroll that buffer. */
2848 else if (!NILP (Vother_window_scroll_buffer))
2850 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
2851 if (NILP (window))
2852 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt);
2854 else
2856 /* Nothing specified; look for a neighboring window on the same
2857 frame. */
2858 window = Fnext_window (selected_window, Qnil, Qnil);
2860 if (EQ (window, selected_window))
2861 /* That didn't get us anywhere; look for a window on another
2862 visible frame. */
2864 window = Fnext_window (window, Qnil, Qt);
2865 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
2866 && ! EQ (window, selected_window));
2869 CHECK_LIVE_WINDOW (window, 0);
2871 if (EQ (window, selected_window))
2872 error ("There is no other window");
2874 return window;
2877 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
2878 "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
2879 The next window is the one below the current one; or the one at the top\n\
2880 if the current one is at the bottom. Negative ARG means scroll downward.\n\
2881 When calling from a program, supply a number as argument or nil.\n\
2883 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
2884 specifies the window to scroll.\n\
2885 If `other-window-scroll-buffer' is non-nil, scroll the window\n\
2886 showing that buffer, popping the buffer up if necessary.")
2887 (arg)
2888 register Lisp_Object arg;
2890 register Lisp_Object window;
2891 register int defalt;
2892 register struct window *w;
2893 register int count = specpdl_ptr - specpdl;
2895 window = Fother_window_for_scrolling ();
2897 w = XWINDOW (window);
2898 defalt = window_internal_height (w) - next_screen_context_lines;
2899 if (defalt < 1) defalt = 1;
2901 /* Don't screw up if window_scroll gets an error. */
2902 record_unwind_protect (save_excursion_restore, save_excursion_save ());
2904 Fset_buffer (w->buffer);
2905 SET_PT (marker_position (w->pointm));
2907 if (NILP (arg))
2908 window_scroll (window, defalt, 1, 1);
2909 else if (EQ (arg, Qminus))
2910 window_scroll (window, -defalt, 1, 1);
2911 else
2913 if (CONSP (arg))
2914 arg = Fcar (arg);
2915 CHECK_NUMBER (arg, 0);
2916 window_scroll (window, XINT (arg), 0, 1);
2919 Fset_marker (w->pointm, make_number (PT), Qnil);
2920 unbind_to (count, Qnil);
2922 return Qnil;
2925 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
2926 "Scroll selected window display ARG columns left.\n\
2927 Default for ARG is window width minus 2.")
2928 (arg)
2929 register Lisp_Object arg;
2932 if (NILP (arg))
2933 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
2934 else
2935 arg = Fprefix_numeric_value (arg);
2937 return
2938 Fset_window_hscroll (selected_window,
2939 make_number (XINT (XWINDOW (selected_window)->hscroll)
2940 + XINT (arg)));
2943 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
2944 "Scroll selected window display ARG columns right.\n\
2945 Default for ARG is window width minus 2.")
2946 (arg)
2947 register Lisp_Object arg;
2949 if (NILP (arg))
2950 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
2951 else
2952 arg = Fprefix_numeric_value (arg);
2954 return
2955 Fset_window_hscroll (selected_window,
2956 make_number (XINT (XWINDOW (selected_window)->hscroll)
2957 - XINT (arg)));
2960 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
2961 "Center point in window and redisplay frame. With ARG, put point on line ARG.\n\
2962 The desired position of point is always relative to the current window.\n\
2963 Just C-u as prefix means put point in the center of the window.\n\
2964 If ARG is omitted or nil, erases the entire frame and then\n\
2965 redraws with point in the center of the current window.")
2966 (arg)
2967 register Lisp_Object arg;
2969 register struct window *w = XWINDOW (selected_window);
2970 register int ht = window_internal_height (w);
2971 struct position pos;
2973 if (NILP (arg))
2975 extern int frame_garbaged;
2977 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
2978 XSETFASTINT (arg, ht / 2);
2980 else if (CONSP (arg)) /* Just C-u. */
2982 XSETFASTINT (arg, ht / 2);
2984 else
2986 arg = Fprefix_numeric_value (arg);
2987 CHECK_NUMBER (arg, 0);
2990 if (XINT (arg) < 0)
2991 XSETINT (arg, XINT (arg) + ht);
2993 pos = *vmotion (PT, - XINT (arg), w);
2995 Fset_marker (w->start, make_number (pos.bufpos), w->buffer);
2996 w->start_at_line_beg = ((pos.bufpos == BEGV
2997 || FETCH_CHAR (pos.bufpos - 1) == '\n')
2998 ? Qt : Qnil);
2999 w->force_start = Qt;
3001 return Qnil;
3004 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
3005 1, 1, "P",
3006 "Position point relative to window.\n\
3007 With no argument, position point at center of window.\n\
3008 An argument specifies frame line; zero means top of window,\n\
3009 negative means relative to bottom of window.")
3010 (arg)
3011 register Lisp_Object arg;
3013 register struct window *w = XWINDOW (selected_window);
3014 register int height = window_internal_height (w);
3015 register int start;
3016 Lisp_Object window;
3018 if (NILP (arg))
3019 XSETFASTINT (arg, height / 2);
3020 else
3022 arg = Fprefix_numeric_value (arg);
3023 if (XINT (arg) < 0)
3024 XSETINT (arg, XINT (arg) + height);
3027 start = marker_position (w->start);
3028 XSETWINDOW (window, w);
3029 if (start < BEGV || start > ZV)
3031 Fvertical_motion (make_number (- (height / 2)), window);
3032 Fset_marker (w->start, make_number (PT), w->buffer);
3033 w->start_at_line_beg = Fbolp ();
3034 w->force_start = Qt;
3036 else
3037 SET_PT (start);
3039 return Fvertical_motion (arg, window);
3042 struct save_window_data
3044 EMACS_INT size_from_Lisp_Vector_struct;
3045 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3046 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
3047 Lisp_Object selected_frame;
3048 Lisp_Object current_window;
3049 Lisp_Object current_buffer;
3050 Lisp_Object minibuf_scroll_window;
3051 Lisp_Object root_window;
3052 Lisp_Object focus_frame;
3053 /* Record the values of window-min-width and window-min-height
3054 so that window sizes remain consistent with them. */
3055 Lisp_Object min_width, min_height;
3056 /* A vector, interpreted as a struct saved_window */
3057 Lisp_Object saved_windows;
3060 /* This is saved as a Lisp_Vector */
3061 struct saved_window
3063 /* these first two must agree with struct Lisp_Vector in lisp.h */
3064 EMACS_INT size_from_Lisp_Vector_struct;
3065 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3067 Lisp_Object window;
3068 Lisp_Object buffer, start, pointm, mark;
3069 Lisp_Object left, top, width, height, hscroll;
3070 Lisp_Object parent, prev;
3071 Lisp_Object start_at_line_beg;
3072 Lisp_Object display_table;
3074 #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
3076 #define SAVED_WINDOW_N(swv,n) \
3077 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
3079 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
3080 "T if OBJECT is a window-configuration object.")
3081 (object)
3082 Lisp_Object object;
3084 if (WINDOW_CONFIGURATIONP (object))
3085 return Qt;
3086 return Qnil;
3090 DEFUN ("set-window-configuration", Fset_window_configuration,
3091 Sset_window_configuration, 1, 1, 0,
3092 "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
3093 CONFIGURATION must be a value previously returned\n\
3094 by `current-window-configuration' (which see).")
3095 (configuration)
3096 Lisp_Object configuration;
3098 register struct save_window_data *data;
3099 struct Lisp_Vector *saved_windows;
3100 Lisp_Object new_current_buffer;
3101 Lisp_Object frame;
3102 FRAME_PTR f;
3104 while (!WINDOW_CONFIGURATIONP (configuration))
3106 configuration = wrong_type_argument (intern ("window-configuration-p"),
3107 configuration);
3110 data = (struct save_window_data *) XVECTOR (configuration);
3111 saved_windows = XVECTOR (data->saved_windows);
3113 new_current_buffer = data->current_buffer;
3114 if (NILP (XBUFFER (new_current_buffer)->name))
3115 new_current_buffer = Qnil;
3117 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3118 f = XFRAME (frame);
3120 /* If f is a dead frame, don't bother rebuilding its window tree.
3121 However, there is other stuff we should still try to do below. */
3122 if (FRAME_LIVE_P (f))
3124 register struct window *w;
3125 register struct saved_window *p;
3126 int k;
3128 /* If the frame has been resized since this window configuration was
3129 made, we change the frame to the size specified in the
3130 configuration, restore the configuration, and then resize it
3131 back. We keep track of the prevailing height in these variables. */
3132 int previous_frame_height = FRAME_HEIGHT (f);
3133 int previous_frame_width = FRAME_WIDTH (f);
3134 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
3136 if (XFASTINT (data->frame_height) != previous_frame_height
3137 || XFASTINT (data->frame_width) != previous_frame_width)
3138 change_frame_size (f, data->frame_height, data->frame_width, 0, 0);
3139 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3140 if (XFASTINT (data->frame_menu_bar_lines)
3141 != previous_frame_menu_bar_lines)
3142 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, 0);
3143 #endif
3145 windows_or_buffers_changed++;
3146 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3148 /* Temporarily avoid any problems with windows that are smaller
3149 than they are supposed to be. */
3150 window_min_height = 1;
3151 window_min_width = 1;
3153 /* Kludge Alert!
3154 Mark all windows now on frame as "deleted".
3155 Restoring the new configuration "undeletes" any that are in it.
3157 Save their current buffers in their height fields, since we may
3158 need it later, if a buffer saved in the configuration is now
3159 dead. */
3160 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3162 for (k = 0; k < saved_windows->size; k++)
3164 p = SAVED_WINDOW_N (saved_windows, k);
3165 w = XWINDOW (p->window);
3166 w->next = Qnil;
3168 if (!NILP (p->parent))
3169 w->parent = SAVED_WINDOW_N (saved_windows,
3170 XFASTINT (p->parent))->window;
3171 else
3172 w->parent = Qnil;
3174 if (!NILP (p->prev))
3176 w->prev = SAVED_WINDOW_N (saved_windows,
3177 XFASTINT (p->prev))->window;
3178 XWINDOW (w->prev)->next = p->window;
3180 else
3182 w->prev = Qnil;
3183 if (!NILP (w->parent))
3185 if (EQ (p->width, XWINDOW (w->parent)->width))
3187 XWINDOW (w->parent)->vchild = p->window;
3188 XWINDOW (w->parent)->hchild = Qnil;
3190 else
3192 XWINDOW (w->parent)->hchild = p->window;
3193 XWINDOW (w->parent)->vchild = Qnil;
3198 /* If we squirreled away the buffer in the window's height,
3199 restore it now. */
3200 if (BUFFERP (w->height))
3201 w->buffer = w->height;
3202 w->left = p->left;
3203 w->top = p->top;
3204 w->width = p->width;
3205 w->height = p->height;
3206 w->hscroll = p->hscroll;
3207 w->display_table = p->display_table;
3208 XSETFASTINT (w->last_modified, 0);
3209 XSETFASTINT (w->last_overlay_modified, 0);
3211 /* Reinstall the saved buffer and pointers into it. */
3212 if (NILP (p->buffer))
3213 w->buffer = p->buffer;
3214 else
3216 if (!NILP (XBUFFER (p->buffer)->name))
3217 /* If saved buffer is alive, install it. */
3219 w->buffer = p->buffer;
3220 w->start_at_line_beg = p->start_at_line_beg;
3221 set_marker_restricted (w->start,
3222 Fmarker_position (p->start),
3223 w->buffer);
3224 set_marker_restricted (w->pointm,
3225 Fmarker_position (p->pointm),
3226 w->buffer);
3227 Fset_marker (XBUFFER (w->buffer)->mark,
3228 Fmarker_position (p->mark), w->buffer);
3230 /* As documented in Fcurrent_window_configuration, don't
3231 save the location of point in the buffer which was current
3232 when the window configuration was recorded. */
3233 if (!EQ (p->buffer, new_current_buffer)
3234 && XBUFFER (p->buffer) == current_buffer)
3235 Fgoto_char (w->pointm);
3237 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
3238 /* Else unless window has a live buffer, get one. */
3240 w->buffer = Fcdr (Fcar (Vbuffer_alist));
3241 /* This will set the markers to beginning of visible
3242 range. */
3243 set_marker_restricted (w->start, make_number (0), w->buffer);
3244 set_marker_restricted (w->pointm, make_number (0),w->buffer);
3245 w->start_at_line_beg = Qt;
3247 else
3248 /* Keeping window's old buffer; make sure the markers
3249 are real. */
3251 /* Set window markers at start of visible range. */
3252 if (XMARKER (w->start)->buffer == 0)
3253 set_marker_restricted (w->start, make_number (0),
3254 w->buffer);
3255 if (XMARKER (w->pointm)->buffer == 0)
3256 set_marker_restricted (w->pointm,
3257 (make_number
3258 (BUF_PT (XBUFFER (w->buffer)))),
3259 w->buffer);
3260 w->start_at_line_beg = Qt;
3265 FRAME_ROOT_WINDOW (f) = data->root_window;
3266 Fselect_window (data->current_window);
3268 if (NILP (data->focus_frame)
3269 || (FRAMEP (data->focus_frame)
3270 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
3271 Fredirect_frame_focus (frame, data->focus_frame);
3273 #if 0 /* I don't understand why this is needed, and it causes problems
3274 when the frame's old selected window has been deleted. */
3275 if (f != selected_frame && FRAME_WINDOW_P (f))
3276 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
3277 Qnil, 0);
3278 #endif
3280 /* Set the screen height to the value it had before this function. */
3281 if (previous_frame_height != FRAME_HEIGHT (f)
3282 || previous_frame_width != FRAME_WIDTH (f))
3283 change_frame_size (f, previous_frame_height, previous_frame_width,
3284 0, 0);
3285 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3286 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
3287 x_set_menu_bar_lines (f, previous_frame_menu_bar_lines, 0);
3288 #endif
3291 /* Restore the minimum heights recorded in the configuration. */
3292 window_min_height = XINT (data->min_height);
3293 window_min_width = XINT (data->min_width);
3295 /* Fselect_window will have made f the selected frame, so we
3296 reselect the proper frame here. Fhandle_switch_frame will change the
3297 selected window too, but that doesn't make the call to
3298 Fselect_window above totally superfluous; it still sets f's
3299 selected window. */
3300 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
3301 do_switch_frame (data->selected_frame, Qnil, 0);
3303 if (!NILP (new_current_buffer))
3304 Fset_buffer (new_current_buffer);
3306 Vminibuf_scroll_window = data->minibuf_scroll_window;
3307 return (Qnil);
3310 /* Mark all windows now on frame as deleted
3311 by setting their buffers to nil. */
3313 void
3314 delete_all_subwindows (w)
3315 register struct window *w;
3317 if (!NILP (w->next))
3318 delete_all_subwindows (XWINDOW (w->next));
3319 if (!NILP (w->vchild))
3320 delete_all_subwindows (XWINDOW (w->vchild));
3321 if (!NILP (w->hchild))
3322 delete_all_subwindows (XWINDOW (w->hchild));
3324 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
3326 if (!NILP (w->buffer))
3327 unshow_buffer (w);
3329 /* We set all three of these fields to nil, to make sure that we can
3330 distinguish this dead window from any live window. Live leaf
3331 windows will have buffer set, and combination windows will have
3332 vchild or hchild set. */
3333 w->buffer = Qnil;
3334 w->vchild = Qnil;
3335 w->hchild = Qnil;
3338 static int
3339 count_windows (window)
3340 register struct window *window;
3342 register int count = 1;
3343 if (!NILP (window->next))
3344 count += count_windows (XWINDOW (window->next));
3345 if (!NILP (window->vchild))
3346 count += count_windows (XWINDOW (window->vchild));
3347 if (!NILP (window->hchild))
3348 count += count_windows (XWINDOW (window->hchild));
3349 return count;
3352 static int
3353 save_window_save (window, vector, i)
3354 Lisp_Object window;
3355 struct Lisp_Vector *vector;
3356 int i;
3358 register struct saved_window *p;
3359 register struct window *w;
3360 register Lisp_Object tem;
3362 for (;!NILP (window); window = w->next)
3364 p = SAVED_WINDOW_N (vector, i);
3365 w = XWINDOW (window);
3367 XSETFASTINT (w->temslot, i++);
3368 p->window = window;
3369 p->buffer = w->buffer;
3370 p->left = w->left;
3371 p->top = w->top;
3372 p->width = w->width;
3373 p->height = w->height;
3374 p->hscroll = w->hscroll;
3375 p->display_table = w->display_table;
3376 if (!NILP (w->buffer))
3378 /* Save w's value of point in the window configuration.
3379 If w is the selected window, then get the value of point
3380 from the buffer; pointm is garbage in the selected window. */
3381 if (EQ (window, selected_window))
3383 p->pointm = Fmake_marker ();
3384 Fset_marker (p->pointm, BUF_PT (XBUFFER (w->buffer)),
3385 w->buffer);
3387 else
3388 p->pointm = Fcopy_marker (w->pointm, Qnil);
3390 p->start = Fcopy_marker (w->start, Qnil);
3391 p->start_at_line_beg = w->start_at_line_beg;
3393 tem = XBUFFER (w->buffer)->mark;
3394 p->mark = Fcopy_marker (tem, Qnil);
3396 else
3398 p->pointm = Qnil;
3399 p->start = Qnil;
3400 p->mark = Qnil;
3401 p->start_at_line_beg = Qnil;
3404 if (NILP (w->parent))
3405 p->parent = Qnil;
3406 else
3407 p->parent = XWINDOW (w->parent)->temslot;
3409 if (NILP (w->prev))
3410 p->prev = Qnil;
3411 else
3412 p->prev = XWINDOW (w->prev)->temslot;
3414 if (!NILP (w->vchild))
3415 i = save_window_save (w->vchild, vector, i);
3416 if (!NILP (w->hchild))
3417 i = save_window_save (w->hchild, vector, i);
3420 return i;
3423 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
3424 Scurrent_window_configuration, 0, 1, 0,
3425 "Return an object representing the current window configuration of FRAME.\n\
3426 If FRAME is nil or omitted, use the selected frame.\n\
3427 This describes the number of windows, their sizes and current buffers,\n\
3428 and for each displayed buffer, where display starts, and the positions of\n\
3429 point and mark. An exception is made for point in the current buffer:\n\
3430 its value is -not- saved.\n\
3431 This also records the currently selected frame, and FRAME's focus\n\
3432 redirection (see `redirect-frame-focus').")
3433 (frame)
3434 Lisp_Object frame;
3436 register Lisp_Object tem;
3437 register int n_windows;
3438 register struct save_window_data *data;
3439 register struct Lisp_Vector *vec;
3440 register int i;
3441 FRAME_PTR f;
3443 if (NILP (frame))
3444 f = selected_frame;
3445 else
3447 CHECK_LIVE_FRAME (frame, 0);
3448 f = XFRAME (frame);
3451 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3452 vec = allocate_vectorlike (VECSIZE (struct save_window_data));
3453 for (i = 0; i < VECSIZE (struct save_window_data); i++)
3454 vec->contents[i] = Qnil;
3455 vec->size = VECSIZE (struct save_window_data);
3456 data = (struct save_window_data *)vec;
3458 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
3459 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
3460 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
3461 XSETFRAME (data->selected_frame, selected_frame);
3462 data->current_window = FRAME_SELECTED_WINDOW (f);
3463 XSETBUFFER (data->current_buffer, current_buffer);
3464 data->minibuf_scroll_window = Vminibuf_scroll_window;
3465 data->root_window = FRAME_ROOT_WINDOW (f);
3466 data->focus_frame = FRAME_FOCUS_FRAME (f);
3467 XSETINT (data->min_height, window_min_height);
3468 XSETINT (data->min_width, window_min_width);
3469 tem = Fmake_vector (make_number (n_windows), Qnil);
3470 data->saved_windows = tem;
3471 for (i = 0; i < n_windows; i++)
3472 XVECTOR (tem)->contents[i]
3473 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
3474 save_window_save (FRAME_ROOT_WINDOW (f),
3475 XVECTOR (tem), 0);
3476 XSETWINDOW_CONFIGURATION (tem, data);
3477 return (tem);
3480 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
3481 0, UNEVALLED, 0,
3482 "Execute body, preserving window sizes and contents.\n\
3483 Restore which buffer appears in which window, where display starts,\n\
3484 and the value of point and mark for each window.\n\
3485 Also restore which buffer is current.\n\
3486 But do not preserve point in the current buffer.\n\
3487 Does not restore the value of point in current buffer.")
3488 (args)
3489 Lisp_Object args;
3491 register Lisp_Object val;
3492 register int count = specpdl_ptr - specpdl;
3494 record_unwind_protect (Fset_window_configuration,
3495 Fcurrent_window_configuration (Qnil));
3496 val = Fprogn (args);
3497 return unbind_to (count, val);
3500 init_window_once ()
3502 selected_frame = make_terminal_frame ();
3503 XSETFRAME (Vterminal_frame, selected_frame);
3504 minibuf_window = selected_frame->minibuffer_window;
3505 selected_window = selected_frame->selected_window;
3506 last_nonminibuf_frame = selected_frame;
3508 window_initialized = 1;
3511 syms_of_window ()
3513 Qwindowp = intern ("windowp");
3514 staticpro (&Qwindowp);
3516 Qwindow_live_p = intern ("window-live-p");
3517 staticpro (&Qwindow_live_p);
3519 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
3520 staticpro (&Qtemp_buffer_show_hook);
3522 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
3523 "Non-nil means call as function to display a help buffer.\n\
3524 The function is called with one argument, the buffer to be displayed.\n\
3525 Used by `with-output-to-temp-buffer'.\n\
3526 If this function is used, then it must do the entire job of showing\n\
3527 the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
3528 Vtemp_buffer_show_function = Qnil;
3530 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
3531 "If non-nil, function to call to handle `display-buffer'.\n\
3532 It will receive two args, the buffer and a flag which if non-nil means\n\
3533 that the currently selected window is not acceptable.\n\
3534 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
3535 work using this function.");
3536 Vdisplay_buffer_function = Qnil;
3538 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
3539 "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
3540 Vminibuf_scroll_window = Qnil;
3542 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
3543 "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
3544 Vother_window_scroll_buffer = Qnil;
3546 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
3547 "*Non-nil means `display-buffer' should make a separate frame.");
3548 pop_up_frames = 0;
3550 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
3551 "Function to call to handle automatic new frame creation.\n\
3552 It is called with no arguments and should return a newly created frame.\n\
3554 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
3555 where `pop-up-frame-alist' would hold the default frame parameters.");
3556 Vpop_up_frame_function = Qnil;
3558 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
3559 "*List of buffer names that should have their own special frames.\n\
3560 Displaying a buffer whose name is in this list makes a special frame for it\n\
3561 using `special-display-function'. See also `special-display-regexps'.\n\
3563 An element of the list can be a list instead of just a string.\n\
3564 There are two ways to use a list as an element:\n\
3565 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)\n\
3566 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3567 In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
3568 followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
3569 All this is done by the function found in `special-display-function'.");
3570 Vspecial_display_buffer_names = Qnil;
3572 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
3573 "*List of regexps saying which buffers should have their own special frames.\n\
3574 If a buffer name matches one of these regexps, it gets its own frame.\n\
3575 Displaying a buffer whose name is in this list makes a special frame for it\n\
3576 using `special-display-function'.\n\
3578 An element of the list can be a list instead of just a string.\n\
3579 There are two ways to use a list as an element:\n\
3580 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)\n\
3581 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3582 In the latter case, FUNCTION is called with the buffer as first argument,\n\
3583 followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
3584 All this is done by the function found in `special-display-function'.");
3585 Vspecial_display_regexps = Qnil;
3587 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
3588 "Function to call to make a new frame for a special buffer.\n\
3589 It is called with two arguments, the buffer and optional buffer specific\n\
3590 data, and should return a window displaying that buffer.\n\
3591 The default value makes a separate frame for the buffer,\n\
3592 using `special-display-frame-alist' to specify the frame parameters.\n\
3594 A buffer is special if its is listed in `special-display-buffer-names'\n\
3595 or matches a regexp in `special-display-regexps'.");
3596 Vspecial_display_function = Qnil;
3598 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
3599 "*List of buffer names that should appear in the selected window.\n\
3600 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
3601 switches to it in the selected window, rather than making it appear\n\
3602 in some other window.\n\
3604 An element of the list can be a cons cell instead of just a string.\n\
3605 Then the car must be a string, which specifies the buffer name.\n\
3606 This is for compatibility with `special-display-buffer-names';\n\
3607 the cdr of the cons cell is ignored.\n\
3609 See also `same-window-regexps'.");
3610 Vsame_window_buffer_names = Qnil;
3612 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
3613 "*List of regexps saying which buffers should appear in the selected window.\n\
3614 If a buffer name matches one of these regexps, then displaying it\n\
3615 using `display-buffer' or `pop-to-buffer' switches to it\n\
3616 in the selected window, rather than making it appear in some other window.\n\
3618 An element of the list can be a cons cell instead of just a string.\n\
3619 Then the car must be a string, which specifies the buffer name.\n\
3620 This is for compatibility with `special-display-buffer-names';\n\
3621 the cdr of the cons cell is ignored.\n\
3623 See also `same-window-buffer-names'.");
3624 Vsame_window_regexps = Qnil;
3626 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
3627 "*Non-nil means display-buffer should make new windows.");
3628 pop_up_windows = 1;
3630 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
3631 "*Number of lines of continuity when scrolling by screenfuls.");
3632 next_screen_context_lines = 2;
3634 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
3635 "*display-buffer would prefer to split the largest window if this large.\n\
3636 If there is only one window, it is split regardless of this value.");
3637 split_height_threshold = 500;
3639 DEFVAR_INT ("window-min-height", &window_min_height,
3640 "*Delete any window less than this tall (including its mode line).");
3641 window_min_height = 4;
3643 DEFVAR_INT ("window-min-width", &window_min_width,
3644 "*Delete any window less than this wide.");
3645 window_min_width = 10;
3647 defsubr (&Sselected_window);
3648 defsubr (&Sminibuffer_window);
3649 defsubr (&Swindow_minibuffer_p);
3650 defsubr (&Swindowp);
3651 defsubr (&Swindow_live_p);
3652 defsubr (&Spos_visible_in_window_p);
3653 defsubr (&Swindow_buffer);
3654 defsubr (&Swindow_height);
3655 defsubr (&Swindow_width);
3656 defsubr (&Swindow_hscroll);
3657 defsubr (&Sset_window_hscroll);
3658 defsubr (&Swindow_redisplay_end_trigger);
3659 defsubr (&Sset_window_redisplay_end_trigger);
3660 defsubr (&Swindow_edges);
3661 defsubr (&Scoordinates_in_window_p);
3662 defsubr (&Swindow_at);
3663 defsubr (&Swindow_point);
3664 defsubr (&Swindow_start);
3665 defsubr (&Swindow_end);
3666 defsubr (&Sset_window_point);
3667 defsubr (&Sset_window_start);
3668 defsubr (&Swindow_dedicated_p);
3669 defsubr (&Sset_window_dedicated_p);
3670 defsubr (&Swindow_display_table);
3671 defsubr (&Sset_window_display_table);
3672 defsubr (&Snext_window);
3673 defsubr (&Sprevious_window);
3674 defsubr (&Sother_window);
3675 defsubr (&Sget_lru_window);
3676 defsubr (&Sget_largest_window);
3677 defsubr (&Sget_buffer_window);
3678 defsubr (&Sdelete_other_windows);
3679 defsubr (&Sdelete_windows_on);
3680 defsubr (&Sreplace_buffer_in_windows);
3681 defsubr (&Sdelete_window);
3682 defsubr (&Sset_window_buffer);
3683 defsubr (&Sselect_window);
3684 defsubr (&Sspecial_display_p);
3685 defsubr (&Ssame_window_p);
3686 defsubr (&Sdisplay_buffer);
3687 defsubr (&Ssplit_window);
3688 defsubr (&Senlarge_window);
3689 defsubr (&Sshrink_window);
3690 defsubr (&Sscroll_up);
3691 defsubr (&Sscroll_down);
3692 defsubr (&Sscroll_left);
3693 defsubr (&Sscroll_right);
3694 defsubr (&Sother_window_for_scrolling);
3695 defsubr (&Sscroll_other_window);
3696 defsubr (&Srecenter);
3697 defsubr (&Smove_to_window_line);
3698 defsubr (&Swindow_configuration_p);
3699 defsubr (&Sset_window_configuration);
3700 defsubr (&Scurrent_window_configuration);
3701 defsubr (&Ssave_window_excursion);
3704 keys_of_window ()
3706 initial_define_key (control_x_map, '1', "delete-other-windows");
3707 initial_define_key (control_x_map, '2', "split-window");
3708 initial_define_key (control_x_map, '0', "delete-window");
3709 initial_define_key (control_x_map, 'o', "other-window");
3710 initial_define_key (control_x_map, '^', "enlarge-window");
3711 initial_define_key (control_x_map, '<', "scroll-left");
3712 initial_define_key (control_x_map, '>', "scroll-right");
3714 initial_define_key (global_map, Ctl ('V'), "scroll-up");
3715 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
3716 initial_define_key (meta_map, 'v', "scroll-down");
3718 initial_define_key (global_map, Ctl('L'), "recenter");
3719 initial_define_key (meta_map, 'r', "move-to-window-line");