(make_lispy_event): Distinguish S-SPC from SPC.
[emacs.git] / src / window.c
blob87807ac1de399bd20dddfae884a731d6b9d504ba
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985,86,87,93,94,95,96,1997 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"
32 #include "blockinput.h"
34 Lisp_Object Qwindowp, Qwindow_live_p;
36 Lisp_Object Fnext_window (), Fdelete_window (), Fselect_window ();
37 Lisp_Object Fset_window_buffer (), Fsplit_window (), Frecenter ();
39 void delete_all_subwindows ();
40 static struct window *decode_window();
42 /* This is the window in which the terminal's cursor should
43 be left when nothing is being done with it. This must
44 always be a leaf window, and its buffer is selected by
45 the top level editing loop at the end of each command.
47 This value is always the same as
48 FRAME_SELECTED_WINDOW (selected_frame). */
50 Lisp_Object selected_window;
52 /* The minibuffer window of the selected frame.
53 Note that you cannot test for minibufferness of an arbitrary window
54 by comparing against this; but you can test for minibufferness of
55 the selected window. */
56 Lisp_Object minibuf_window;
58 /* Non-nil means it is the window for C-M-v to scroll
59 when the minibuffer is selected. */
60 Lisp_Object Vminibuf_scroll_window;
62 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
63 Lisp_Object Vother_window_scroll_buffer;
65 /* Non-nil means it's function to call to display temp buffers. */
66 Lisp_Object Vtemp_buffer_show_function;
68 /* If a window gets smaller than either of these, it is removed. */
69 int window_min_height;
70 int window_min_width;
72 /* Nonzero implies Fdisplay_buffer should create windows. */
73 int pop_up_windows;
75 /* Nonzero implies make new frames for Fdisplay_buffer. */
76 int pop_up_frames;
78 /* Non-nil means use this function instead of default */
79 Lisp_Object Vpop_up_frame_function;
81 /* Function to call to handle Fdisplay_buffer. */
82 Lisp_Object Vdisplay_buffer_function;
84 /* List of buffer *names* for buffers that should have their own frames. */
85 Lisp_Object Vspecial_display_buffer_names;
87 /* List of regexps for buffer names that should have their own frames. */
88 Lisp_Object Vspecial_display_regexps;
90 /* Function to pop up a special frame. */
91 Lisp_Object Vspecial_display_function;
93 /* List of buffer *names* for buffers to appear in selected window. */
94 Lisp_Object Vsame_window_buffer_names;
96 /* List of regexps for buffer names to appear in selected window. */
97 Lisp_Object Vsame_window_regexps;
99 /* Hook run at end of temp_output_buffer_show. */
100 Lisp_Object Qtemp_buffer_show_hook;
102 /* Fdisplay_buffer always splits the largest window
103 if that window is more than this high. */
104 int split_height_threshold;
106 /* Number of lines of continuity in scrolling by screenfuls. */
107 int next_screen_context_lines;
109 /* Incremented for each window created. */
110 static int sequence_number;
112 /* Nonzero after init_window_once has finished. */
113 static int window_initialized;
115 /* Hook to run when window config changes. */
116 Lisp_Object Qwindow_configuration_change_hook;
117 Lisp_Object Vwindow_configuration_change_hook;
119 /* Nonzero means scroll commands try to put point
120 at the same screen height as previously. */
121 static int scroll_preserve_screen_position;
123 /* Nonzero means we can split a frame even if it is "unsplittable". */
124 static int inhibit_frame_unsplittable;
126 #define min(a, b) ((a) < (b) ? (a) : (b))
128 extern int scroll_margin;
130 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
132 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
133 "Returns t if OBJECT is a window.")
134 (object)
135 Lisp_Object object;
137 return WINDOWP (object) ? Qt : Qnil;
140 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
141 "Returns t if OBJECT is a window which is currently visible.")
142 (object)
143 Lisp_Object object;
145 return (WINDOWP (object) && ! NILP (XWINDOW (object)->buffer) ? Qt : Qnil);
148 Lisp_Object
149 make_window ()
151 Lisp_Object val;
152 register struct window *p;
153 register struct Lisp_Vector *vec;
154 int i;
156 vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct window));
157 for (i = 0; i < VECSIZE (struct window); i++)
158 vec->contents[i] = Qnil;
159 vec->size = VECSIZE (struct window);
160 p = (struct window *)vec;
161 XSETFASTINT (p->sequence_number, ++sequence_number);
162 XSETFASTINT (p->left, 0);
163 XSETFASTINT (p->top, 0);
164 XSETFASTINT (p->height, 0);
165 XSETFASTINT (p->width, 0);
166 XSETFASTINT (p->hscroll, 0);
167 XSETFASTINT (p->last_point_x, 0);
168 XSETFASTINT (p->last_point_y, 0);
169 p->start = Fmake_marker ();
170 p->pointm = Fmake_marker ();
171 XSETFASTINT (p->use_time, 0);
172 p->frame = Qnil;
173 p->display_table = Qnil;
174 p->dedicated = Qnil;
175 XSETWINDOW (val, p);
176 return val;
179 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
180 "Return the window that the cursor now appears in and commands apply to.")
183 return selected_window;
186 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
187 "Return the window used now for minibuffers.\n\
188 If the optional argument FRAME is specified, return the minibuffer window\n\
189 used by that frame.")
190 (frame)
191 Lisp_Object frame;
193 if (NILP (frame))
194 XSETFRAME (frame, selected_frame);
195 else
196 CHECK_LIVE_FRAME (frame, 0);
198 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
201 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
202 "Returns non-nil if WINDOW is a minibuffer window.")
203 (window)
204 Lisp_Object window;
206 struct window *w = decode_window (window);
207 return (MINI_WINDOW_P (w) ? Qt : Qnil);
210 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
211 Spos_visible_in_window_p, 0, 2, 0,
212 "Return t if position POS is currently on the frame in WINDOW.\n\
213 Returns nil if that position is scrolled vertically out of view.\n\
214 POS defaults to point; WINDOW, to the selected window.")
215 (pos, window)
216 Lisp_Object pos, window;
218 register struct window *w;
219 register int top;
220 register int height;
221 register int posint;
222 register struct buffer *buf;
223 struct position posval;
224 int hscroll;
226 if (NILP (pos))
227 posint = PT;
228 else
230 CHECK_NUMBER_COERCE_MARKER (pos, 0);
231 posint = XINT (pos);
234 w = decode_window (window);
235 top = marker_position (w->start);
236 hscroll = XINT (w->hscroll);
238 if (posint < top)
239 return Qnil;
241 height = XFASTINT (w->height) - ! MINI_WINDOW_P (w);
243 buf = XBUFFER (w->buffer);
244 if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
245 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf))
247 /* If frame is up to date,
248 use the info recorded about how much text fit on it. */
249 if (posint < BUF_Z (buf) - XFASTINT (w->window_end_pos)
250 || (XFASTINT (w->window_end_vpos) < height))
251 return Qt;
252 return Qnil;
254 else
256 if (posint > BUF_ZV (buf))
257 return Qnil;
259 /* w->start can be out of range. If it is, do something reasonable. */
260 if (top < BUF_BEGV (buf) || top > BUF_ZV (buf))
261 return Qnil;
263 /* If that info is not correct, calculate afresh */
264 /* BUG FIX for the 7th arg (TOHPOS).
266 '0' is harmless, however, ' - (1 << (BITS_PER_SHORT - 1))' is
267 more appropriate here. In case of HSCROLL > 0, this can avoid
268 needless calculation done until (HPOS == 0).
270 We want to determine if the position POSINT is in HEIGHT or
271 not. We don't have to do calculation until (HPOS == 0). We
272 can stop it when VPOS goes beyond HEIGHT. */
273 posval = *compute_motion (top, 0, (hscroll ? 1 - hscroll : 0), 0,
274 posint, height, - (1 << (BITS_PER_SHORT - 1)),
275 window_internal_width (w) - 1,
276 hscroll, 0, w);
278 return posval.vpos < height ? Qt : Qnil;
282 static struct window *
283 decode_window (window)
284 register Lisp_Object window;
286 if (NILP (window))
287 return XWINDOW (selected_window);
289 CHECK_LIVE_WINDOW (window, 0);
290 return XWINDOW (window);
293 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
294 "Return the buffer that WINDOW is displaying.")
295 (window)
296 Lisp_Object window;
298 return decode_window (window)->buffer;
301 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
302 "Return the number of lines in WINDOW (including its mode line).")
303 (window)
304 Lisp_Object window;
306 return decode_window (window)->height;
309 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
310 "Return the number of display columns in WINDOW.\n\
311 This is the width that is usable columns available for text in WINDOW.\n\
312 If you want to find out how many columns WINDOW takes up,\n\
313 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
314 (window)
315 Lisp_Object window;
317 return make_number (window_internal_width (decode_window (window)));
320 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
321 "Return the number of columns by which WINDOW is scrolled from left margin.")
322 (window)
323 Lisp_Object window;
325 return decode_window (window)->hscroll;
328 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
329 "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
330 NCOL should be zero or positive.")
331 (window, ncol)
332 register Lisp_Object window, ncol;
334 register struct window *w;
336 CHECK_NUMBER (ncol, 1);
337 if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
338 w = decode_window (window);
339 if (XINT (w->hscroll) != XINT (ncol))
340 XBUFFER (w->buffer)->clip_changed = 1; /* Prevent redisplay shortcuts */
341 w->hscroll = ncol;
342 return ncol;
345 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
346 Swindow_redisplay_end_trigger, 0, 1, 0,
347 "Return WINDOW's redisplay end trigger value.\n\
348 See `set-window-redisplay-end-trigger' for more information.")
349 (window)
350 Lisp_Object window;
352 return decode_window (window)->redisplay_end_trigger;
355 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
356 Sset_window_redisplay_end_trigger, 2, 2, 0,
357 "Set WINDOW's redisplay end trigger value to VALUE.\n\
358 VALUE should be a buffer position (typically a marker) or nil.\n\
359 If it is a buffer position, then if redisplay in WINDOW reaches a position\n\
360 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called\n\
361 with two arguments: WINDOW, and the end trigger value.\n\
362 Afterwards the end-trigger value is reset to nil.")
363 (window, value)
364 register Lisp_Object window, value;
366 register struct window *w;
368 w = decode_window (window);
369 w->redisplay_end_trigger = value;
370 return value;
373 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
374 "Return a list of the edge coordinates of WINDOW.\n\
375 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.\n\
376 RIGHT is one more than the rightmost column used by WINDOW,\n\
377 and BOTTOM is one more than the bottommost row used by WINDOW\n\
378 and its mode-line.")
379 (window)
380 Lisp_Object window;
382 register struct window *w = decode_window (window);
384 return Fcons (w->left, Fcons (w->top,
385 Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
386 Fcons (make_number (XFASTINT (w->top)
387 + XFASTINT (w->height)),
388 Qnil))));
391 /* Test if the character at column *x, row *y is within window *w.
392 If it is not, return 0;
393 if it is in the window's text area,
394 set *x and *y to its location relative to the upper left corner
395 of the window, and
396 return 1;
397 if it is on the window's modeline, return 2;
398 if it is on the border between the window and its right sibling,
399 return 3. */
400 static int
401 coordinates_in_window (w, x, y)
402 register struct window *w;
403 register int *x, *y;
405 register int left = XINT (w->left);
406 register int right_edge = WINDOW_RIGHT_EDGE (w);
407 register int left_margin = WINDOW_LEFT_MARGIN (w);
408 register int right_margin = WINDOW_RIGHT_MARGIN (w);
409 register int window_height = XINT (w->height);
410 register int top = XFASTINT (w->top);
412 if ( *x < left || *x >= right_edge
413 || *y < top || *y >= top + window_height)
414 return 0;
416 if (left_margin != left && *x < left_margin && *x >= left)
417 return 3;
419 if (right_margin != right_edge && *x >= right_margin && *x < right_edge)
420 return 3;
422 /* Is the character is the mode line? */
423 if (*y == top + window_height - 1
424 && ! MINI_WINDOW_P (w))
425 return 2;
427 *x -= WINDOW_LEFT_MARGIN (w);
428 *y -= top;
429 return 1;
432 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
433 Scoordinates_in_window_p, 2, 2, 0,
434 "Return non-nil if COORDINATES are in WINDOW.\n\
435 COORDINATES is a cons of the form (X . Y), X and Y being distances\n\
436 measured in characters from the upper-left corner of the frame.\n\
437 (0 . 0) denotes the character in the upper left corner of the\n\
438 frame.\n\
439 If COORDINATES are in the text portion of WINDOW,\n\
440 the coordinates relative to the window are returned.\n\
441 If they are in the mode line of WINDOW, `mode-line' is returned.\n\
442 If they are on the border between WINDOW and its right sibling,\n\
443 `vertical-line' is returned.")
444 (coordinates, window)
445 register Lisp_Object coordinates, window;
447 int x, y;
449 CHECK_LIVE_WINDOW (window, 0);
450 CHECK_CONS (coordinates, 1);
451 x = XINT (Fcar (coordinates));
452 y = XINT (Fcdr (coordinates));
454 switch (coordinates_in_window (XWINDOW (window), &x, &y))
456 case 0: /* NOT in window at all. */
457 return Qnil;
459 case 1: /* In text part of window. */
460 return Fcons (make_number (x), make_number (y));
462 case 2: /* In mode line of window. */
463 return Qmode_line;
465 case 3: /* On right border of window. */
466 return Qvertical_line;
468 default:
469 abort ();
473 /* Find the window containing column x, row y, and return it as a
474 Lisp_Object. If x, y is on the window's modeline, set *part
475 to 1; if it is on the separating line between the window and its
476 right sibling, set it to 2; otherwise set it to 0. If there is no
477 window under x, y return nil and leave *part unmodified. */
478 Lisp_Object
479 window_from_coordinates (frame, x, y, part)
480 FRAME_PTR frame;
481 int x, y;
482 int *part;
484 register Lisp_Object tem, first;
486 tem = first = FRAME_SELECTED_WINDOW (frame);
490 int found = coordinates_in_window (XWINDOW (tem), &x, &y);
492 if (found)
494 *part = found - 1;
495 return tem;
498 tem = Fnext_window (tem, Qt, Qlambda);
500 while (! EQ (tem, first));
502 return Qnil;
505 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
506 "Return window containing coordinates X and Y on FRAME.\n\
507 If omitted, FRAME defaults to the currently selected frame.\n\
508 The top left corner of the frame is considered to be row 0,\n\
509 column 0.")
510 (x, y, frame)
511 Lisp_Object x, y, frame;
513 int part;
515 if (NILP (frame))
516 XSETFRAME (frame, selected_frame);
517 else
518 CHECK_LIVE_FRAME (frame, 2);
519 CHECK_NUMBER (x, 0);
520 CHECK_NUMBER (y, 1);
522 return window_from_coordinates (XFRAME (frame),
523 XINT (x), XINT (y),
524 &part);
527 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
528 "Return current value of point in WINDOW.\n\
529 For a nonselected window, this is the value point would have\n\
530 if that window were selected.\n\
532 Note that, when WINDOW is the selected window and its buffer\n\
533 is also currently selected, the value returned is the same as (point).\n\
534 It would be more strictly correct to return the `top-level' value\n\
535 of point, outside of any save-excursion forms.\n\
536 But that is hard to define.")
537 (window)
538 Lisp_Object window;
540 register struct window *w = decode_window (window);
542 if (w == XWINDOW (selected_window)
543 && current_buffer == XBUFFER (w->buffer))
544 return Fpoint ();
545 return Fmarker_position (w->pointm);
548 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
549 "Return position at which display currently starts in WINDOW.\n\
550 This is updated by redisplay or by calling `set-window-start'.")
551 (window)
552 Lisp_Object window;
554 return Fmarker_position (decode_window (window)->start);
557 /* This is text temporarily removed from the doc string below.
559 This function returns nil if the position is not currently known.\n\
560 That happens when redisplay is preempted and doesn't finish.\n\
561 If in that case you want to compute where the end of the window would\n\
562 have been if redisplay had finished, do this:\n\
563 (save-excursion\n\
564 (goto-char (window-start window))\n\
565 (vertical-motion (1- (window-height window)) window)\n\
566 (point))") */
568 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 1, 0,
569 "Return position at which display currently ends in WINDOW.\n\
570 This is updated by redisplay, when it runs to completion.\n\
571 Simply changing the buffer text or setting `window-start'\n\
572 does not update this value.")
573 (window)
574 Lisp_Object window;
576 Lisp_Object value;
577 struct window *w = decode_window (window);
578 Lisp_Object buf;
580 buf = w->buffer;
581 CHECK_BUFFER (buf, 0);
583 #if 0 /* This change broke some things. We should make it later. */
584 /* If we don't know the end position, return nil.
585 The user can compute it with vertical-motion if he wants to.
586 It would be nicer to do it automatically,
587 but that's so slow that it would probably bother people. */
588 if (NILP (w->window_end_valid))
589 return Qnil;
590 #endif
592 XSETINT (value,
593 BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
595 return value;
598 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
599 "Make point value in WINDOW be at position POS in WINDOW's buffer.")
600 (window, pos)
601 Lisp_Object window, pos;
603 register struct window *w = decode_window (window);
605 CHECK_NUMBER_COERCE_MARKER (pos, 1);
606 if (w == XWINDOW (selected_window))
607 Fgoto_char (pos);
608 else
609 set_marker_restricted (w->pointm, pos, w->buffer);
611 return pos;
614 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
615 "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
616 Optional third arg NOFORCE non-nil inhibits next redisplay\n\
617 from overriding motion of point in order to display at this exact start.")
618 (window, pos, noforce)
619 Lisp_Object window, pos, noforce;
621 register struct window *w = decode_window (window);
623 CHECK_NUMBER_COERCE_MARKER (pos, 1);
624 set_marker_restricted (w->start, pos, w->buffer);
625 /* this is not right, but much easier than doing what is right. */
626 w->start_at_line_beg = Qnil;
627 if (NILP (noforce))
628 w->force_start = Qt;
629 w->update_mode_line = Qt;
630 XSETFASTINT (w->last_modified, 0);
631 XSETFASTINT (w->last_overlay_modified, 0);
632 if (!EQ (window, selected_window))
633 windows_or_buffers_changed++;
634 return pos;
637 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
638 1, 1, 0,
639 "Return WINDOW's dedicated object, usually t or nil.\n\
640 See also `set-window-dedicated-p'.")
641 (window)
642 Lisp_Object window;
644 return decode_window (window)->dedicated;
647 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
648 Sset_window_dedicated_p, 2, 2, 0,
649 "Control whether WINDOW is dedicated to the buffer it displays.\n\
650 If it is dedicated, Emacs will not automatically change\n\
651 which buffer appears in it.\n\
652 The second argument is the new value for the dedication flag;\n\
653 non-nil means yes.")
654 (window, arg)
655 Lisp_Object window, arg;
657 register struct window *w = decode_window (window);
659 if (NILP (arg))
660 w->dedicated = Qnil;
661 else
662 w->dedicated = Qt;
664 return w->dedicated;
667 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
668 0, 1, 0,
669 "Return the display-table that WINDOW is using.")
670 (window)
671 Lisp_Object window;
673 return decode_window (window)->display_table;
676 /* Get the display table for use currently on window W.
677 This is either W's display table or W's buffer's display table.
678 Ignore the specified tables if they are not valid;
679 if no valid table is specified, return 0. */
681 struct Lisp_Char_Table *
682 window_display_table (w)
683 struct window *w;
685 Lisp_Object tem;
686 tem = w->display_table;
687 if (DISP_TABLE_P (tem))
688 return XCHAR_TABLE (tem);
689 tem = XBUFFER (w->buffer)->display_table;
690 if (DISP_TABLE_P (tem))
691 return XCHAR_TABLE (tem);
692 tem = Vstandard_display_table;
693 if (DISP_TABLE_P (tem))
694 return XCHAR_TABLE (tem);
695 return 0;
698 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
699 "Set WINDOW's display-table to TABLE.")
700 (window, table)
701 register Lisp_Object window, table;
703 register struct window *w;
704 register Lisp_Object z; /* Return value. */
706 w = decode_window (window);
707 w->display_table = table;
708 return table;
711 /* Record info on buffer window w is displaying
712 when it is about to cease to display that buffer. */
713 static
714 unshow_buffer (w)
715 register struct window *w;
717 Lisp_Object buf;
719 buf = w->buffer;
720 if (XBUFFER (buf) != XMARKER (w->pointm)->buffer)
721 abort ();
723 if (w == XWINDOW (XBUFFER (buf)->last_selected_window))
724 XBUFFER (buf)->last_selected_window = Qnil;
726 #if 0
727 if (w == XWINDOW (selected_window)
728 || ! EQ (buf, XWINDOW (selected_window)->buffer))
729 /* Do this except when the selected window's buffer
730 is being removed from some other window. */
731 #endif
732 /* last_window_start records the start position that this buffer
733 had in the last window to be disconnected from it.
734 Now that this statement is unconditional,
735 it is possible for the buffer to be displayed in the
736 selected window, while last_window_start reflects another
737 window which was recently showing the same buffer.
738 Some people might say that might be a good thing. Let's see. */
739 XBUFFER (buf)->last_window_start = marker_position (w->start);
741 /* Point in the selected window's buffer
742 is actually stored in that buffer, and the window's pointm isn't used.
743 So don't clobber point in that buffer. */
744 if (! EQ (buf, XWINDOW (selected_window)->buffer))
745 BUF_PT (XBUFFER (buf))
746 = clip_to_bounds (BUF_BEGV (XBUFFER (buf)),
747 marker_position (w->pointm),
748 BUF_ZV (XBUFFER (buf)));
751 /* Put replacement into the window structure in place of old. */
752 static
753 replace_window (old, replacement)
754 Lisp_Object old, replacement;
756 register Lisp_Object tem;
757 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
759 /* If OLD is its frame's root_window, then replacement is the new
760 root_window for that frame. */
762 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
763 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
765 p->left = o->left;
766 p->top = o->top;
767 p->width = o->width;
768 p->height = o->height;
770 p->next = tem = o->next;
771 if (!NILP (tem))
772 XWINDOW (tem)->prev = replacement;
774 p->prev = tem = o->prev;
775 if (!NILP (tem))
776 XWINDOW (tem)->next = replacement;
778 p->parent = tem = o->parent;
779 if (!NILP (tem))
781 if (EQ (XWINDOW (tem)->vchild, old))
782 XWINDOW (tem)->vchild = replacement;
783 if (EQ (XWINDOW (tem)->hchild, old))
784 XWINDOW (tem)->hchild = replacement;
787 /*** Here, if replacement is a vertical combination
788 and so is its new parent, we should make replacement's
789 children be children of that parent instead. ***/
792 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
793 "Remove WINDOW from the display. Default is selected window.")
794 (window)
795 register Lisp_Object window;
797 delete_window (window);
799 if (! NILP (Vwindow_configuration_change_hook)
800 && ! NILP (Vrun_hooks))
801 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
803 return Qnil;
806 delete_window (window)
807 register Lisp_Object window;
809 register Lisp_Object tem, parent, sib;
810 register struct window *p;
811 register struct window *par;
813 /* Because this function is called by other C code on non-leaf
814 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
815 so we can't decode_window here. */
816 if (NILP (window))
817 window = selected_window;
818 else
819 CHECK_WINDOW (window, 0);
820 p = XWINDOW (window);
822 /* It's okay to delete an already-deleted window. */
823 if (NILP (p->buffer)
824 && NILP (p->hchild)
825 && NILP (p->vchild))
826 return;
828 parent = p->parent;
829 if (NILP (parent))
830 error ("Attempt to delete minibuffer or sole ordinary window");
831 par = XWINDOW (parent);
833 windows_or_buffers_changed++;
834 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (p))) = 1;
836 /* Are we trying to delete any frame's selected window? */
838 Lisp_Object frame, pwindow;
840 /* See if the frame's selected window is either WINDOW
841 or any subwindow of it, by finding all that window's parents
842 and comparing each one with WINDOW. */
843 frame = WINDOW_FRAME (XWINDOW (window));
844 pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
846 while (!NILP (pwindow))
848 if (EQ (window, pwindow))
849 break;
850 pwindow = XWINDOW (pwindow)->parent;
853 if (EQ (window, pwindow))
855 Lisp_Object alternative;
856 alternative = Fnext_window (window, Qlambda, Qnil);
858 /* If we're about to delete the selected window on the
859 selected frame, then we should use Fselect_window to select
860 the new window. On the other hand, if we're about to
861 delete the selected window on any other frame, we shouldn't do
862 anything but set the frame's selected_window slot. */
863 if (EQ (window, selected_window))
864 Fselect_window (alternative);
865 else
866 FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
870 tem = p->buffer;
871 /* tem is null for dummy parent windows
872 (which have inferiors but not any contents themselves) */
873 if (!NILP (tem))
875 unshow_buffer (p);
876 unchain_marker (p->pointm);
877 unchain_marker (p->start);
880 tem = p->next;
881 if (!NILP (tem))
882 XWINDOW (tem)->prev = p->prev;
884 tem = p->prev;
885 if (!NILP (tem))
886 XWINDOW (tem)->next = p->next;
888 if (EQ (window, par->hchild))
889 par->hchild = p->next;
890 if (EQ (window, par->vchild))
891 par->vchild = p->next;
893 /* Find one of our siblings to give our space to. */
894 sib = p->prev;
895 if (NILP (sib))
897 /* If p gives its space to its next sibling, that sibling needs
898 to have its top/left side pulled back to where p's is.
899 set_window_{height,width} will re-position the sibling's
900 children. */
901 sib = p->next;
902 XWINDOW (sib)->top = p->top;
903 XWINDOW (sib)->left = p->left;
906 /* Stretch that sibling. */
907 if (!NILP (par->vchild))
908 set_window_height (sib,
909 XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
911 if (!NILP (par->hchild))
912 set_window_width (sib,
913 XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
916 /* If parent now has only one child,
917 put the child into the parent's place. */
918 tem = par->hchild;
919 if (NILP (tem))
920 tem = par->vchild;
921 if (NILP (XWINDOW (tem)->next))
922 replace_window (parent, tem);
924 /* Since we may be deleting combination windows, we must make sure that
925 not only p but all its children have been marked as deleted. */
926 if (! NILP (p->hchild))
927 delete_all_subwindows (XWINDOW (p->hchild));
928 else if (! NILP (p->vchild))
929 delete_all_subwindows (XWINDOW (p->vchild));
931 /* Mark this window as deleted. */
932 p->buffer = p->hchild = p->vchild = Qnil;
936 extern Lisp_Object next_frame (), prev_frame ();
938 /* This comment supplies the doc string for `next-window',
939 for make-docfile to see. We cannot put this in the real DEFUN
940 due to limits in the Unix cpp.
942 DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
943 "Return next window after WINDOW in canonical ordering of windows.\n\
944 If omitted, WINDOW defaults to the selected window.\n\
946 Optional second arg MINIBUF t means count the minibuffer window even\n\
947 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
948 it is active. MINIBUF neither t nor nil means not to count the\n\
949 minibuffer even if it is active.\n\
951 Several frames may share a single minibuffer; if the minibuffer\n\
952 counts, all windows on all frames that share that minibuffer count\n\
953 too. Therefore, `next-window' can be used to iterate through the\n\
954 set of windows even when the minibuffer is on another frame. If the\n\
955 minibuffer does not count, only windows from WINDOW's frame count.\n\
957 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
958 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
959 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
960 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
961 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
962 Anything else means restrict to WINDOW's frame.\n\
964 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
965 `next-window' to iterate through the entire cycle of acceptable\n\
966 windows, eventually ending up back at the window you started with.\n\
967 `previous-window' traverses the same cycle, in the reverse order.")
968 (window, minibuf, all_frames) */
970 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
972 (window, minibuf, all_frames)
973 register Lisp_Object window, minibuf, all_frames;
975 register Lisp_Object tem;
976 Lisp_Object start_window;
978 if (NILP (window))
979 window = selected_window;
980 else
981 CHECK_LIVE_WINDOW (window, 0);
983 start_window = window;
985 /* minibuf == nil may or may not include minibuffers.
986 Decide if it does. */
987 if (NILP (minibuf))
988 minibuf = (minibuf_level ? minibuf_window : Qlambda);
989 else if (! EQ (minibuf, Qt))
990 minibuf = Qlambda;
991 /* Now minibuf can be t => count all minibuffer windows,
992 lambda => count none of them,
993 or a specific minibuffer window (the active one) to count. */
995 /* all_frames == nil doesn't specify which frames to include. */
996 if (NILP (all_frames))
997 all_frames = (! EQ (minibuf, Qlambda)
998 ? (FRAME_MINIBUF_WINDOW
999 (XFRAME
1000 (WINDOW_FRAME
1001 (XWINDOW (window)))))
1002 : Qnil);
1003 else if (EQ (all_frames, Qvisible))
1005 else if (XFASTINT (all_frames) == 0)
1007 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1008 /* If all_frames is a frame and window arg isn't on that frame, just
1009 return the first window on the frame. */
1010 return Fframe_first_window (all_frames);
1011 else if (! EQ (all_frames, Qt))
1012 all_frames = Qnil;
1013 /* Now all_frames is t meaning search all frames,
1014 nil meaning search just current frame,
1015 visible meaning search just visible frames,
1016 0 meaning search visible and iconified frames,
1017 or a window, meaning search the frame that window belongs to. */
1019 /* Do this loop at least once, to get the next window, and perhaps
1020 again, if we hit the minibuffer and that is not acceptable. */
1023 /* Find a window that actually has a next one. This loop
1024 climbs up the tree. */
1025 while (tem = XWINDOW (window)->next, NILP (tem))
1026 if (tem = XWINDOW (window)->parent, !NILP (tem))
1027 window = tem;
1028 else
1030 /* We've reached the end of this frame.
1031 Which other frames are acceptable? */
1032 tem = WINDOW_FRAME (XWINDOW (window));
1033 if (! NILP (all_frames))
1035 Lisp_Object tem1;
1037 tem1 = tem;
1038 tem = next_frame (tem, all_frames);
1039 /* In the case where the minibuffer is active,
1040 and we include its frame as well as the selected one,
1041 next_frame may get stuck in that frame.
1042 If that happens, go back to the selected frame
1043 so we can complete the cycle. */
1044 if (EQ (tem, tem1))
1045 XSETFRAME (tem, selected_frame);
1047 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1049 break;
1052 window = tem;
1054 /* If we're in a combination window, find its first child and
1055 recurse on that. Otherwise, we've found the window we want. */
1056 while (1)
1058 if (!NILP (XWINDOW (window)->hchild))
1059 window = XWINDOW (window)->hchild;
1060 else if (!NILP (XWINDOW (window)->vchild))
1061 window = XWINDOW (window)->vchild;
1062 else break;
1065 /* Which windows are acceptable?
1066 Exit the loop and accept this window if
1067 this isn't a minibuffer window,
1068 or we're accepting all minibuffer windows,
1069 or this is the active minibuffer and we are accepting that one, or
1070 we've come all the way around and we're back at the original window. */
1071 while (MINI_WINDOW_P (XWINDOW (window))
1072 && ! EQ (minibuf, Qt)
1073 && ! EQ (minibuf, window)
1074 && ! EQ (window, start_window));
1076 return window;
1079 /* This comment supplies the doc string for `previous-window',
1080 for make-docfile to see. We cannot put this in the real DEFUN
1081 due to limits in the Unix cpp.
1083 DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
1084 "Return the window preceding WINDOW in canonical ordering of windows.\n\
1085 If omitted, WINDOW defaults to the selected window.\n\
1087 Optional second arg MINIBUF t means count the minibuffer window even\n\
1088 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
1089 it is active. MINIBUF neither t nor nil means not to count the\n\
1090 minibuffer even if it is active.\n\
1092 Several frames may share a single minibuffer; if the minibuffer\n\
1093 counts, all windows on all frames that share that minibuffer count\n\
1094 too. Therefore, `previous-window' can be used to iterate through\n\
1095 the set of windows even when the minibuffer is on another frame. If\n\
1096 the minibuffer does not count, only windows from WINDOW's frame count\n\
1098 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
1099 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
1100 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
1101 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1102 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
1103 Anything else means restrict to WINDOW's frame.\n\
1105 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
1106 `previous-window' to iterate through the entire cycle of acceptable\n\
1107 windows, eventually ending up back at the window you started with.\n\
1108 `next-window' traverses the same cycle, in the reverse order.")
1109 (window, minibuf, all_frames) */
1112 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1114 (window, minibuf, all_frames)
1115 register Lisp_Object window, minibuf, all_frames;
1117 register Lisp_Object tem;
1118 Lisp_Object start_window;
1120 if (NILP (window))
1121 window = selected_window;
1122 else
1123 CHECK_LIVE_WINDOW (window, 0);
1125 start_window = window;
1127 /* minibuf == nil may or may not include minibuffers.
1128 Decide if it does. */
1129 if (NILP (minibuf))
1130 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1131 else if (! EQ (minibuf, Qt))
1132 minibuf = Qlambda;
1133 /* Now minibuf can be t => count all minibuffer windows,
1134 lambda => count none of them,
1135 or a specific minibuffer window (the active one) to count. */
1137 /* all_frames == nil doesn't specify which frames to include.
1138 Decide which frames it includes. */
1139 if (NILP (all_frames))
1140 all_frames = (! EQ (minibuf, Qlambda)
1141 ? (FRAME_MINIBUF_WINDOW
1142 (XFRAME
1143 (WINDOW_FRAME
1144 (XWINDOW (window)))))
1145 : Qnil);
1146 else if (EQ (all_frames, Qvisible))
1148 else if (XFASTINT (all_frames) == 0)
1150 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1151 /* If all_frames is a frame and window arg isn't on that frame, just
1152 return the first window on the frame. */
1153 return Fframe_first_window (all_frames);
1154 else if (! EQ (all_frames, Qt))
1155 all_frames = Qnil;
1156 /* Now all_frames is t meaning search all frames,
1157 nil meaning search just current frame,
1158 visible meaning search just visible frames,
1159 0 meaning search visible and iconified frames,
1160 or a window, meaning search the frame that window belongs to. */
1162 /* Do this loop at least once, to get the previous window, and perhaps
1163 again, if we hit the minibuffer and that is not acceptable. */
1166 /* Find a window that actually has a previous one. This loop
1167 climbs up the tree. */
1168 while (tem = XWINDOW (window)->prev, NILP (tem))
1169 if (tem = XWINDOW (window)->parent, !NILP (tem))
1170 window = tem;
1171 else
1173 /* We have found the top window on the frame.
1174 Which frames are acceptable? */
1175 tem = WINDOW_FRAME (XWINDOW (window));
1176 if (! NILP (all_frames))
1177 /* It's actually important that we use prev_frame here,
1178 rather than next_frame. All the windows acceptable
1179 according to the given parameters should form a ring;
1180 Fnext_window and Fprevious_window should go back and
1181 forth around the ring. If we use next_frame here,
1182 then Fnext_window and Fprevious_window take different
1183 paths through the set of acceptable windows.
1184 window_loop assumes that these `ring' requirement are
1185 met. */
1187 Lisp_Object tem1;
1189 tem1 = tem;
1190 tem = prev_frame (tem, all_frames);
1191 /* In the case where the minibuffer is active,
1192 and we include its frame as well as the selected one,
1193 next_frame may get stuck in that frame.
1194 If that happens, go back to the selected frame
1195 so we can complete the cycle. */
1196 if (EQ (tem, tem1))
1197 XSETFRAME (tem, selected_frame);
1199 /* If this frame has a minibuffer, find that window first,
1200 because it is conceptually the last window in that frame. */
1201 if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
1202 tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
1203 else
1204 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1206 break;
1209 window = tem;
1210 /* If we're in a combination window, find its last child and
1211 recurse on that. Otherwise, we've found the window we want. */
1212 while (1)
1214 if (!NILP (XWINDOW (window)->hchild))
1215 window = XWINDOW (window)->hchild;
1216 else if (!NILP (XWINDOW (window)->vchild))
1217 window = XWINDOW (window)->vchild;
1218 else break;
1219 while (tem = XWINDOW (window)->next, !NILP (tem))
1220 window = tem;
1223 /* Which windows are acceptable?
1224 Exit the loop and accept this window if
1225 this isn't a minibuffer window,
1226 or we're accepting all minibuffer windows,
1227 or this is the active minibuffer and we are accepting that one, or
1228 we've come all the way around and we're back at the original window. */
1229 while (MINI_WINDOW_P (XWINDOW (window))
1230 && ! EQ (minibuf, Qt)
1231 && ! EQ (minibuf, window)
1232 && ! EQ (window, start_window));
1234 return window;
1237 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1238 "Select the ARG'th different window on this frame.\n\
1239 All windows on current frame are arranged in a cyclic order.\n\
1240 This command selects the window ARG steps away in that order.\n\
1241 A negative ARG moves in the opposite order. If the optional second\n\
1242 argument ALL_FRAMES is non-nil, cycle through all frames.")
1243 (arg, all_frames)
1244 register Lisp_Object arg, all_frames;
1246 register int i;
1247 register Lisp_Object w;
1249 CHECK_NUMBER (arg, 0);
1250 w = selected_window;
1251 i = XINT (arg);
1253 while (i > 0)
1255 w = Fnext_window (w, Qnil, all_frames);
1256 i--;
1258 while (i < 0)
1260 w = Fprevious_window (w, Qnil, all_frames);
1261 i++;
1263 Fselect_window (w);
1264 return Qnil;
1267 /* Look at all windows, performing an operation specified by TYPE
1268 with argument OBJ.
1269 If FRAMES is Qt, look at all frames;
1270 Qnil, look at just the selected frame;
1271 Qvisible, look at visible frames;
1272 a frame, just look at windows on that frame.
1273 If MINI is non-zero, perform the operation on minibuffer windows too.
1276 enum window_loop
1278 WINDOW_LOOP_UNUSED,
1279 GET_BUFFER_WINDOW, /* Arg is buffer */
1280 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1281 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1282 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1283 GET_LARGEST_WINDOW,
1284 UNSHOW_BUFFER /* Arg is buffer */
1287 static Lisp_Object
1288 window_loop (type, obj, mini, frames)
1289 enum window_loop type;
1290 register Lisp_Object obj, frames;
1291 int mini;
1293 register Lisp_Object w;
1294 register Lisp_Object best_window;
1295 register Lisp_Object next_window;
1296 register Lisp_Object last_window;
1297 FRAME_PTR frame;
1298 Lisp_Object frame_arg;
1299 frame_arg = Qt;
1301 /* If we're only looping through windows on a particular frame,
1302 frame points to that frame. If we're looping through windows
1303 on all frames, frame is 0. */
1304 if (FRAMEP (frames))
1305 frame = XFRAME (frames);
1306 else if (NILP (frames))
1307 frame = selected_frame;
1308 else
1309 frame = 0;
1310 if (frame)
1311 frame_arg = Qlambda;
1312 else if (XFASTINT (frames) == 0)
1313 frame_arg = frames;
1314 else if (EQ (frames, Qvisible))
1315 frame_arg = frames;
1317 /* frame_arg is Qlambda to stick to one frame,
1318 Qvisible to consider all visible frames,
1319 or Qt otherwise. */
1321 /* Pick a window to start with. */
1322 if (WINDOWP (obj))
1323 w = obj;
1324 else if (frame)
1325 w = FRAME_SELECTED_WINDOW (frame);
1326 else
1327 w = FRAME_SELECTED_WINDOW (selected_frame);
1329 /* Figure out the last window we're going to mess with. Since
1330 Fnext_window, given the same options, is guaranteed to go in a
1331 ring, we can just use Fprevious_window to find the last one.
1333 We can't just wait until we hit the first window again, because
1334 it might be deleted. */
1336 last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
1338 best_window = Qnil;
1339 for (;;)
1341 FRAME_PTR w_frame = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1343 /* Pick the next window now, since some operations will delete
1344 the current window. */
1345 next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
1347 /* Note that we do not pay attention here to whether
1348 the frame is visible, since Fnext_window skips non-visible frames
1349 if that is desired, under the control of frame_arg. */
1350 if (! MINI_WINDOW_P (XWINDOW (w))
1351 || (mini && minibuf_level > 0))
1352 switch (type)
1354 case GET_BUFFER_WINDOW:
1355 if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj)
1356 /* Don't find any minibuffer window
1357 except the one that is currently in use. */
1358 && (MINI_WINDOW_P (XWINDOW (w))
1359 ? EQ (w, minibuf_window) : 1))
1360 return w;
1361 break;
1363 case GET_LRU_WINDOW:
1364 /* t as arg means consider only full-width windows */
1365 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (XWINDOW (w)))
1366 break;
1367 /* Ignore dedicated windows and minibuffers. */
1368 if (MINI_WINDOW_P (XWINDOW (w))
1369 || !NILP (XWINDOW (w)->dedicated))
1370 break;
1371 if (NILP (best_window)
1372 || (XFASTINT (XWINDOW (best_window)->use_time)
1373 > XFASTINT (XWINDOW (w)->use_time)))
1374 best_window = w;
1375 break;
1377 case DELETE_OTHER_WINDOWS:
1378 if (XWINDOW (w) != XWINDOW (obj))
1379 Fdelete_window (w);
1380 break;
1382 case DELETE_BUFFER_WINDOWS:
1383 if (EQ (XWINDOW (w)->buffer, obj))
1385 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1387 /* If this window is dedicated, and in a frame of its own,
1388 kill the frame. */
1389 if (EQ (w, FRAME_ROOT_WINDOW (f))
1390 && !NILP (XWINDOW (w)->dedicated)
1391 && other_visible_frames (f))
1393 /* Skip the other windows on this frame.
1394 There might be one, the minibuffer! */
1395 if (! EQ (w, last_window))
1396 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1398 /* As we go, check for the end of the loop.
1399 We mustn't start going around a second time. */
1400 if (EQ (next_window, last_window))
1402 last_window = w;
1403 break;
1405 next_window = Fnext_window (next_window,
1406 mini ? Qt : Qnil,
1407 frame_arg);
1409 /* Now we can safely delete the frame. */
1410 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1412 else
1413 /* If we're deleting the buffer displayed in the only window
1414 on the frame, find a new buffer to display there. */
1415 if (NILP (XWINDOW (w)->parent))
1417 Lisp_Object new_buffer;
1418 new_buffer = Fother_buffer (obj, Qnil);
1419 if (NILP (new_buffer))
1420 new_buffer
1421 = Fget_buffer_create (build_string ("*scratch*"));
1422 Fset_window_buffer (w, new_buffer);
1423 if (EQ (w, selected_window))
1424 Fset_buffer (XWINDOW (w)->buffer);
1426 else
1427 Fdelete_window (w);
1429 break;
1431 case GET_LARGEST_WINDOW:
1432 /* Ignore dedicated windows and minibuffers. */
1433 if (MINI_WINDOW_P (XWINDOW (w))
1434 || !NILP (XWINDOW (w)->dedicated))
1435 break;
1437 struct window *best_window_ptr = XWINDOW (best_window);
1438 struct window *w_ptr = XWINDOW (w);
1439 if (NILP (best_window)
1440 || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
1441 > (XFASTINT (best_window_ptr->height)
1442 * XFASTINT (best_window_ptr->width))))
1443 best_window = w;
1445 break;
1447 case UNSHOW_BUFFER:
1448 if (EQ (XWINDOW (w)->buffer, obj))
1450 /* Find another buffer to show in this window. */
1451 Lisp_Object another_buffer;
1452 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1453 another_buffer = Fother_buffer (obj, Qnil);
1454 if (NILP (another_buffer))
1455 another_buffer
1456 = Fget_buffer_create (build_string ("*scratch*"));
1457 /* If this window is dedicated, and in a frame of its own,
1458 kill the frame. */
1459 if (EQ (w, FRAME_ROOT_WINDOW (f))
1460 && !NILP (XWINDOW (w)->dedicated)
1461 && other_visible_frames (f))
1463 /* Skip the other windows on this frame.
1464 There might be one, the minibuffer! */
1465 if (! EQ (w, last_window))
1466 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1468 /* As we go, check for the end of the loop.
1469 We mustn't start going around a second time. */
1470 if (EQ (next_window, last_window))
1472 last_window = w;
1473 break;
1475 next_window = Fnext_window (next_window,
1476 mini ? Qt : Qnil,
1477 frame_arg);
1479 /* Now we can safely delete the frame. */
1480 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1482 else
1484 /* Otherwise show a different buffer in the window. */
1485 XWINDOW (w)->dedicated = Qnil;
1486 Fset_window_buffer (w, another_buffer);
1487 if (EQ (w, selected_window))
1488 Fset_buffer (XWINDOW (w)->buffer);
1491 break;
1494 if (EQ (w, last_window))
1495 break;
1497 w = next_window;
1500 return best_window;
1503 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
1504 "Return the window least recently selected or used for display.\n\
1505 If optional argument FRAME is `visible', search all visible frames.\n\
1506 If 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 (frame)
1511 Lisp_Object frame;
1513 register Lisp_Object w;
1514 /* First try for a window that is full-width */
1515 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
1516 if (!NILP (w) && !EQ (w, selected_window))
1517 return w;
1518 /* If none of them, try the rest */
1519 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
1522 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
1523 "Return the largest window in area.\n\
1524 If optional argument FRAME is `visible', search all visible frames.\n\
1525 If FRAME is 0, search all visible and iconified frames.\n\
1526 If FRAME is t, search all frames.\n\
1527 If FRAME is nil, search only the selected frame.\n\
1528 If FRAME is a frame, search only that frame.")
1529 (frame)
1530 Lisp_Object frame;
1532 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
1533 frame);
1536 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
1537 "Return a window currently displaying BUFFER, or nil if none.\n\
1538 If optional argument FRAME is `visible', search all visible frames.\n\
1539 If optional argument FRAME is 0, search all visible and iconified frames.\n\
1540 If FRAME is t, search all frames.\n\
1541 If FRAME is nil, search only the selected frame.\n\
1542 If FRAME is a frame, search only that frame.")
1543 (buffer, frame)
1544 Lisp_Object buffer, frame;
1546 buffer = Fget_buffer (buffer);
1547 if (BUFFERP (buffer))
1548 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
1549 else
1550 return Qnil;
1553 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
1554 0, 1, "",
1555 "Make WINDOW (or the selected window) fill its frame.\n\
1556 Only the frame WINDOW is on is affected.\n\
1557 This function tries to reduce display jumps\n\
1558 by keeping the text previously visible in WINDOW\n\
1559 in the same place on the frame. Doing this depends on\n\
1560 the value of (window-start WINDOW), so if calling this function\n\
1561 in a program gives strange scrolling, make sure the window-start\n\
1562 value is reasonable when this function is called.")
1563 (window)
1564 Lisp_Object window;
1566 struct window *w;
1567 int startpos;
1568 int top;
1570 if (NILP (window))
1571 window = selected_window;
1572 else
1573 CHECK_LIVE_WINDOW (window, 0);
1575 w = XWINDOW (window);
1577 startpos = marker_position (w->start);
1578 top = XFASTINT (w->top) - FRAME_MENU_BAR_LINES (XFRAME (WINDOW_FRAME (w)));
1580 if (MINI_WINDOW_P (w) && top > 0)
1581 error ("Can't expand minibuffer to full frame");
1583 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
1585 /* Try to minimize scrolling, by setting the window start to the point
1586 will cause the text at the old window start to be at the same place
1587 on the frame. But don't try to do this if the window start is
1588 outside the visible portion (as might happen when the display is
1589 not current, due to typeahead). */
1590 if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
1591 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
1593 struct position pos;
1594 struct buffer *obuf = current_buffer;
1596 Fset_buffer (w->buffer);
1597 /* This computation used to temporarily move point, but that can
1598 have unwanted side effects due to text properties. */
1599 pos = *vmotion (startpos, -top, w);
1601 Fset_marker (w->start, make_number (pos.bufpos), w->buffer);
1602 w->start_at_line_beg = ((pos.bufpos == BEGV
1603 || FETCH_BYTE (pos.bufpos - 1) == '\n') ? Qt
1604 : Qnil);
1605 /* We need to do this, so that the window-scroll-functions
1606 get called. */
1607 w->optional_new_start = Qt;
1609 set_buffer_internal (obuf);
1611 return Qnil;
1614 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
1615 1, 2, "bDelete windows on (buffer): ",
1616 "Delete all windows showing BUFFER.\n\
1617 Optional second argument FRAME controls which frames are affected.\n\
1618 If nil or omitted, delete all windows showing BUFFER in any frame.\n\
1619 If t, delete only windows showing BUFFER in the selected frame.\n\
1620 If `visible', delete all windows showing BUFFER in any visible frame.\n\
1621 If a frame, delete only windows showing BUFFER in that frame.")
1622 (buffer, frame)
1623 Lisp_Object buffer, frame;
1625 /* FRAME uses t and nil to mean the opposite of what window_loop
1626 expects. */
1627 if (! FRAMEP (frame))
1628 frame = NILP (frame) ? Qt : Qnil;
1630 if (!NILP (buffer))
1632 buffer = Fget_buffer (buffer);
1633 CHECK_BUFFER (buffer, 0);
1634 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
1636 return Qnil;
1639 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
1640 Sreplace_buffer_in_windows,
1641 1, 1, "bReplace buffer in windows: ",
1642 "Replace BUFFER with some other buffer in all windows showing it.")
1643 (buffer)
1644 Lisp_Object buffer;
1646 if (!NILP (buffer))
1648 buffer = Fget_buffer (buffer);
1649 CHECK_BUFFER (buffer, 0);
1650 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
1652 return Qnil;
1655 /* Replace BUFFER with some other buffer in all windows
1656 of all frames, even those on other keyboards. */
1658 void
1659 replace_buffer_in_all_windows (buffer)
1660 Lisp_Object buffer;
1662 #ifdef MULTI_KBOARD
1663 Lisp_Object tail, frame;
1665 /* A single call to window_loop won't do the job
1666 because it only considers frames on the current keyboard.
1667 So loop manually over frames, and handle each one. */
1668 FOR_EACH_FRAME (tail, frame)
1669 window_loop (UNSHOW_BUFFER, buffer, 0, frame);
1670 #else
1671 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
1672 #endif
1675 /* Set the height of WINDOW and all its inferiors. */
1677 /* The smallest acceptable dimensions for a window. Anything smaller
1678 might crash Emacs. */
1679 #define MIN_SAFE_WINDOW_WIDTH (2)
1680 #define MIN_SAFE_WINDOW_HEIGHT (2)
1682 /* Make sure that window_min_height and window_min_width are
1683 not too small; if they are, set them to safe minima. */
1685 static void
1686 check_min_window_sizes ()
1688 /* Smaller values might permit a crash. */
1689 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
1690 window_min_width = MIN_SAFE_WINDOW_WIDTH;
1691 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
1692 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
1695 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
1696 minimum allowable size. */
1697 void
1698 check_frame_size (frame, rows, cols)
1699 FRAME_PTR frame;
1700 int *rows, *cols;
1702 /* For height, we have to see:
1703 whether the frame has a minibuffer,
1704 whether it wants a mode line, and
1705 whether it has a menu bar. */
1706 int min_height =
1707 (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
1708 : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
1709 : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
1710 if (FRAME_MENU_BAR_LINES (frame) > 0)
1711 min_height += FRAME_MENU_BAR_LINES (frame);
1713 if (*rows < min_height)
1714 *rows = min_height;
1715 if (*cols < MIN_SAFE_WINDOW_WIDTH)
1716 *cols = MIN_SAFE_WINDOW_WIDTH;
1719 /* Normally the window is deleted if it gets too small.
1720 nodelete nonzero means do not do this.
1721 (The caller should check later and do so if appropriate) */
1723 set_window_height (window, height, nodelete)
1724 Lisp_Object window;
1725 int height;
1726 int nodelete;
1728 register struct window *w = XWINDOW (window);
1729 register struct window *c;
1730 int oheight = XFASTINT (w->height);
1731 int top, pos, lastbot, opos, lastobot;
1732 Lisp_Object child;
1734 check_min_window_sizes ();
1736 if (!nodelete
1737 && ! NILP (w->parent)
1738 && height < window_min_height)
1740 delete_window (window);
1741 return;
1744 XSETFASTINT (w->last_modified, 0);
1745 XSETFASTINT (w->last_overlay_modified, 0);
1746 windows_or_buffers_changed++;
1747 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1749 XSETFASTINT (w->height, height);
1750 if (!NILP (w->hchild))
1752 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
1754 XWINDOW (child)->top = w->top;
1755 set_window_height (child, height, nodelete);
1758 else if (!NILP (w->vchild))
1760 lastbot = top = XFASTINT (w->top);
1761 lastobot = 0;
1762 for (child = w->vchild; !NILP (child); child = c->next)
1764 c = XWINDOW (child);
1766 opos = lastobot + XFASTINT (c->height);
1768 XSETFASTINT (c->top, lastbot);
1770 pos = (((opos * height) << 1) + oheight) / (oheight << 1);
1772 /* Avoid confusion: inhibit deletion of child if becomes too small */
1773 set_window_height (child, pos + top - lastbot, 1);
1775 /* Now advance child to next window,
1776 and set lastbot if child was not just deleted. */
1777 lastbot = pos + top;
1778 lastobot = opos;
1780 /* Now delete any children that became too small. */
1781 if (!nodelete)
1782 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
1784 set_window_height (child, XINT (XWINDOW (child)->height), 0);
1789 /* Recursively set width of WINDOW and its inferiors. */
1791 set_window_width (window, width, nodelete)
1792 Lisp_Object window;
1793 int width;
1794 int nodelete;
1796 register struct window *w = XWINDOW (window);
1797 register struct window *c;
1798 int owidth = XFASTINT (w->width);
1799 int left, pos, lastright, opos, lastoright;
1800 Lisp_Object child;
1802 if (!nodelete && width < window_min_width && !NILP (w->parent))
1804 delete_window (window);
1805 return;
1808 XSETFASTINT (w->last_modified, 0);
1809 XSETFASTINT (w->last_overlay_modified, 0);
1810 windows_or_buffers_changed++;
1811 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1813 XSETFASTINT (w->width, width);
1814 if (!NILP (w->vchild))
1816 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
1818 XWINDOW (child)->left = w->left;
1819 set_window_width (child, width, nodelete);
1822 else if (!NILP (w->hchild))
1824 lastright = left = XFASTINT (w->left);
1825 lastoright = 0;
1826 for (child = w->hchild; !NILP (child); child = c->next)
1828 c = XWINDOW (child);
1830 opos = lastoright + XFASTINT (c->width);
1832 XSETFASTINT (c->left, lastright);
1834 pos = (((opos * width) << 1) + owidth) / (owidth << 1);
1836 /* Inhibit deletion for becoming too small */
1837 set_window_width (child, pos + left - lastright, 1);
1839 /* Now advance child to next window,
1840 and set lastright if child was not just deleted. */
1841 lastright = pos + left, lastoright = opos;
1843 /* Delete children that became too small */
1844 if (!nodelete)
1845 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
1847 set_window_width (child, XINT (XWINDOW (child)->width), 0);
1852 int window_select_count;
1854 Lisp_Object
1855 Fset_window_buffer_unwind (obuf)
1856 Lisp_Object obuf;
1858 Fset_buffer (obuf);
1859 return Qnil;
1862 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
1863 "Make WINDOW display BUFFER as its contents.\n\
1864 BUFFER can be a buffer or buffer name.")
1865 (window, buffer)
1866 register Lisp_Object window, buffer;
1868 register Lisp_Object tem;
1869 register struct window *w = decode_window (window);
1870 int count = specpdl_ptr - specpdl;
1872 buffer = Fget_buffer (buffer);
1873 CHECK_BUFFER (buffer, 1);
1875 if (NILP (XBUFFER (buffer)->name))
1876 error ("Attempt to display deleted buffer");
1878 tem = w->buffer;
1879 if (NILP (tem))
1880 error ("Window is deleted");
1881 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
1882 is first being set up. */
1884 if (!NILP (w->dedicated) && !EQ (tem, buffer))
1885 error ("Window is dedicated to `%s'",
1886 XSTRING (XBUFFER (tem)->name)->data);
1888 unshow_buffer (w);
1891 w->buffer = buffer;
1893 if (EQ (window, selected_window))
1894 XBUFFER (w->buffer)->last_selected_window = window;
1895 if (INTEGERP (XBUFFER (buffer)->display_count))
1896 XSETINT (XBUFFER (buffer)->display_count,
1897 XINT (XBUFFER (buffer)->display_count) + 1);
1899 XSETFASTINT (w->window_end_pos, 0);
1900 w->window_end_valid = Qnil;
1901 XSETFASTINT (w->hscroll, 0);
1902 Fset_marker (w->pointm,
1903 make_number (BUF_PT (XBUFFER (buffer))),
1904 buffer);
1905 set_marker_restricted (w->start,
1906 make_number (XBUFFER (buffer)->last_window_start),
1907 buffer);
1908 w->start_at_line_beg = Qnil;
1909 w->force_start = Qnil;
1910 XSETFASTINT (w->last_modified, 0);
1911 XSETFASTINT (w->last_overlay_modified, 0);
1912 windows_or_buffers_changed++;
1914 /* We must select BUFFER for running the window-scroll-functions.
1915 If WINDOW is selected, switch permanently.
1916 Otherwise, switch but go back to the ambient buffer afterward. */
1917 if (EQ (window, selected_window))
1918 Fset_buffer (buffer);
1919 /* We can't check ! NILP (Vwindow_scroll_functions) here
1920 because that might itself be a local variable. */
1921 else if (window_initialized)
1923 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
1924 Fset_buffer (buffer);
1927 if (! NILP (Vwindow_scroll_functions))
1928 run_hook_with_args_2 (Qwindow_scroll_functions, window,
1929 Fmarker_position (w->start));
1931 if (! NILP (Vwindow_configuration_change_hook)
1932 && ! NILP (Vrun_hooks))
1933 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
1935 unbind_to (count, Qnil);
1937 return Qnil;
1940 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
1941 "Select WINDOW. Most editing will apply to WINDOW's buffer.\n\
1942 The main editor command loop selects the buffer of the selected window\n\
1943 before each command.")
1944 (window)
1945 register Lisp_Object window;
1947 register struct window *w;
1948 register struct window *ow = XWINDOW (selected_window);
1950 CHECK_LIVE_WINDOW (window, 0);
1952 w = XWINDOW (window);
1954 if (NILP (w->buffer))
1955 error ("Trying to select deleted window or non-leaf window");
1957 XSETFASTINT (w->use_time, ++window_select_count);
1958 if (EQ (window, selected_window))
1959 return window;
1961 Fset_marker (ow->pointm, make_number (BUF_PT (XBUFFER (ow->buffer))),
1962 ow->buffer);
1964 selected_window = window;
1965 if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
1967 XFRAME (WINDOW_FRAME (w))->selected_window = window;
1968 /* Use this rather than Fhandle_switch_frame
1969 so that FRAME_FOCUS_FRAME is moved appropriately as we
1970 move around in the state where a minibuffer in a separate
1971 frame is active. */
1972 Fselect_frame (WINDOW_FRAME (w), Qnil);
1974 else
1975 selected_frame->selected_window = window;
1977 record_buffer (w->buffer);
1978 Fset_buffer (w->buffer);
1980 XBUFFER (w->buffer)->last_selected_window = window;
1982 /* Go to the point recorded in the window.
1983 This is important when the buffer is in more
1984 than one window. It also matters when
1985 redisplay_window has altered point after scrolling,
1986 because it makes the change only in the window. */
1988 register int new_point = marker_position (w->pointm);
1989 if (new_point < BEGV)
1990 SET_PT (BEGV);
1991 else if (new_point > ZV)
1992 SET_PT (ZV);
1993 else
1994 SET_PT (new_point);
1997 windows_or_buffers_changed++;
1998 return window;
2001 /* Deiconify the frame containing the window WINDOW,
2002 unless it is the selected frame;
2003 then return WINDOW.
2005 The reason for the exception for the selected frame
2006 is that it seems better not to change the selected frames visibility
2007 merely because of displaying a different buffer in it.
2008 The deiconification is useful when a buffer gets shown in
2009 another frame that you were not using lately. */
2011 static Lisp_Object
2012 display_buffer_1 (window)
2013 Lisp_Object window;
2015 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
2016 FRAME_SAMPLE_VISIBILITY (f);
2017 if (f != selected_frame)
2019 if (FRAME_ICONIFIED_P (f))
2020 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2021 else if (FRAME_VISIBLE_P (f))
2022 Fraise_frame (WINDOW_FRAME (XWINDOW (window)));
2024 return window;
2027 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
2028 "Returns non-nil if a buffer named BUFFER-NAME would be created specially.\n\
2029 The value is actually t if the frame should be called with default frame\n\
2030 parameters, and a list of frame parameters if they were specified.\n\
2031 See `special-display-buffer-names', and `special-display-regexps'.")
2032 (buffer_name)
2033 Lisp_Object buffer_name;
2035 Lisp_Object tem;
2037 CHECK_STRING (buffer_name, 1);
2039 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
2040 if (!NILP (tem))
2041 return Qt;
2043 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
2044 if (!NILP (tem))
2045 return XCDR (tem);
2047 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
2049 Lisp_Object car = XCAR (tem);
2050 if (STRINGP (car)
2051 && fast_string_match (car, buffer_name) >= 0)
2052 return Qt;
2053 else if (CONSP (car)
2054 && STRINGP (XCAR (car))
2055 && fast_string_match (XCAR (car), buffer_name) >= 0)
2056 return XCDR (tem);
2058 return Qnil;
2061 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
2062 "Returns non-nil if a new buffer named BUFFER-NAME would use the same window.\n\
2063 See `same-window-buffer-names' and `same-window-regexps'.")
2064 (buffer_name)
2065 Lisp_Object buffer_name;
2067 Lisp_Object tem;
2069 CHECK_STRING (buffer_name, 1);
2071 tem = Fmember (buffer_name, Vsame_window_buffer_names);
2072 if (!NILP (tem))
2073 return Qt;
2075 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
2076 if (!NILP (tem))
2077 return Qt;
2079 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
2081 Lisp_Object car = XCAR (tem);
2082 if (STRINGP (car)
2083 && fast_string_match (car, buffer_name) >= 0)
2084 return Qt;
2085 else if (CONSP (car)
2086 && STRINGP (XCAR (car))
2087 && fast_string_match (XCAR (car), buffer_name) >= 0)
2088 return Qt;
2090 return Qnil;
2093 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 2,
2094 "BDisplay buffer: \nP", /* Use B so the default is (other-buffer). */
2095 "Make BUFFER appear in some window but don't select it.\n\
2096 BUFFER can be a buffer or a buffer name.\n\
2097 If BUFFER is shown already in some window, just use that one,\n\
2098 unless the window is the selected window and the optional second\n\
2099 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
2100 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
2101 Returns the window displaying BUFFER.\n\
2103 The variables `special-display-buffer-names', `special-display-regexps',\n\
2104 `same-window-buffer-names', and `same-window-regexps' customize how certain\n\
2105 buffer names are handled.")
2106 (buffer, not_this_window)
2107 register Lisp_Object buffer, not_this_window;
2109 register Lisp_Object window, tem;
2111 buffer = Fget_buffer (buffer);
2112 CHECK_BUFFER (buffer, 0);
2114 if (!NILP (Vdisplay_buffer_function))
2115 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2117 if (NILP (not_this_window)
2118 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
2119 return display_buffer_1 (selected_window);
2121 /* See if the user has specified this buffer should appear
2122 in the selected window. */
2123 if (NILP (not_this_window))
2125 tem = Fsame_window_p (XBUFFER (buffer)->name);
2126 if (!NILP (tem))
2128 Fswitch_to_buffer (buffer, Qnil);
2129 return display_buffer_1 (selected_window);
2133 /* If pop_up_frames,
2134 look for a window showing BUFFER on any visible or iconified frame.
2135 Otherwise search only the current frame. */
2136 if (pop_up_frames || last_nonminibuf_frame == 0)
2137 XSETFASTINT (tem, 0);
2138 else
2139 XSETFRAME (tem, last_nonminibuf_frame);
2140 window = Fget_buffer_window (buffer, tem);
2141 if (!NILP (window)
2142 && (NILP (not_this_window) || !EQ (window, selected_window)))
2144 return display_buffer_1 (window);
2147 /* Certain buffer names get special handling. */
2148 if (!NILP (Vspecial_display_function))
2150 tem = Fspecial_display_p (XBUFFER (buffer)->name);
2151 if (EQ (tem, Qt))
2152 return call1 (Vspecial_display_function, buffer);
2153 if (CONSP (tem))
2154 return call2 (Vspecial_display_function, buffer, tem);
2157 /* If there are no frames open that have more than a minibuffer,
2158 we need to create a new frame. */
2159 if (pop_up_frames || last_nonminibuf_frame == 0)
2161 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2162 Fset_window_buffer (window, buffer);
2163 return display_buffer_1 (window);
2166 if (pop_up_windows
2167 || FRAME_MINIBUF_ONLY_P (selected_frame)
2168 /* If the current frame is a special display frame,
2169 don't try to reuse its windows. */
2170 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->dedicated)
2173 Lisp_Object frames;
2175 frames = Qnil;
2176 if (FRAME_MINIBUF_ONLY_P (selected_frame))
2177 XSETFRAME (frames, last_nonminibuf_frame);
2178 /* Don't try to create a window if would get an error */
2179 if (split_height_threshold < window_min_height << 1)
2180 split_height_threshold = window_min_height << 1;
2182 /* Note that both Fget_largest_window and Fget_lru_window
2183 ignore minibuffers and dedicated windows.
2184 This means they can return nil. */
2186 /* If the frame we would try to split cannot be split,
2187 try other frames. */
2188 if (FRAME_NO_SPLIT_P (NILP (frames) ? selected_frame
2189 : last_nonminibuf_frame))
2191 /* Try visible frames first. */
2192 window = Fget_largest_window (Qvisible);
2193 /* If that didn't work, try iconified frames. */
2194 if (NILP (window))
2195 window = Fget_largest_window (make_number (0));
2196 if (NILP (window))
2197 window = Fget_largest_window (Qt);
2199 else
2200 window = Fget_largest_window (frames);
2202 /* If we got a tall enough full-width window that can be split,
2203 split it. */
2204 if (!NILP (window)
2205 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2206 && window_height (window) >= split_height_threshold
2207 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
2208 window = Fsplit_window (window, Qnil, Qnil);
2209 else
2211 Lisp_Object upper, lower, other;
2213 window = Fget_lru_window (frames);
2214 /* If the LRU window is selected, and big enough,
2215 and can be split, split it. */
2216 if (!NILP (window)
2217 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2218 && (EQ (window, selected_window)
2219 || EQ (XWINDOW (window)->parent, Qnil))
2220 && window_height (window) >= window_min_height << 1)
2221 window = Fsplit_window (window, Qnil, Qnil);
2222 /* If Fget_lru_window returned nil, try other approaches. */
2224 /* Try visible frames first. */
2225 if (NILP (window))
2226 window = Fget_buffer_window (buffer, Qvisible);
2227 if (NILP (window))
2228 window = Fget_largest_window (Qvisible);
2229 /* If that didn't work, try iconified frames. */
2230 if (NILP (window))
2231 window = Fget_buffer_window (buffer, make_number (0));
2232 if (NILP (window))
2233 window = Fget_largest_window (make_number (0));
2234 /* Try invisible frames. */
2235 if (NILP (window))
2236 window = Fget_buffer_window (buffer, Qt);
2237 if (NILP (window))
2238 window = Fget_largest_window (Qt);
2239 /* As a last resort, make a new frame. */
2240 if (NILP (window))
2241 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2242 /* If window appears above or below another,
2243 even out their heights. */
2244 other = upper = lower = Qnil;
2245 if (!NILP (XWINDOW (window)->prev))
2246 other = upper = XWINDOW (window)->prev, lower = window;
2247 if (!NILP (XWINDOW (window)->next))
2248 other = lower = XWINDOW (window)->next, upper = window;
2249 if (!NILP (other)
2250 /* Check that OTHER and WINDOW are vertically arrayed. */
2251 && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
2252 && (XFASTINT (XWINDOW (other)->height)
2253 > XFASTINT (XWINDOW (window)->height)))
2255 int total = (XFASTINT (XWINDOW (other)->height)
2256 + XFASTINT (XWINDOW (window)->height));
2257 Lisp_Object old_selected_window;
2258 old_selected_window = selected_window;
2260 selected_window = upper;
2261 change_window_height ((total / 2
2262 - XFASTINT (XWINDOW (upper)->height)),
2264 selected_window = old_selected_window;
2268 else
2269 window = Fget_lru_window (Qnil);
2271 Fset_window_buffer (window, buffer);
2272 return display_buffer_1 (window);
2275 void
2276 temp_output_buffer_show (buf)
2277 register Lisp_Object buf;
2279 register struct buffer *old = current_buffer;
2280 register Lisp_Object window;
2281 register struct window *w;
2283 XBUFFER (buf)->directory = current_buffer->directory;
2285 Fset_buffer (buf);
2286 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
2287 BEGV = BEG;
2288 ZV = Z;
2289 SET_PT (BEG);
2290 XBUFFER (buf)->clip_changed = 1;
2291 set_buffer_internal (old);
2293 if (!EQ (Vtemp_buffer_show_function, Qnil))
2294 call1 (Vtemp_buffer_show_function, buf);
2295 else
2297 window = Fdisplay_buffer (buf, Qnil);
2299 if (XFRAME (XWINDOW (window)->frame) != selected_frame)
2300 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2301 Vminibuf_scroll_window = window;
2302 w = XWINDOW (window);
2303 XSETFASTINT (w->hscroll, 0);
2304 set_marker_restricted (w->start, make_number (1), buf);
2305 set_marker_restricted (w->pointm, make_number (1), buf);
2307 /* Run temp-buffer-show-hook, with the chosen window selected. */
2308 if (!NILP (Vrun_hooks))
2310 Lisp_Object tem;
2311 tem = Fboundp (Qtemp_buffer_show_hook);
2312 if (!NILP (tem))
2314 tem = Fsymbol_value (Qtemp_buffer_show_hook);
2315 if (!NILP (tem))
2317 int count = specpdl_ptr - specpdl;
2319 /* Select the window that was chosen, for running the hook. */
2320 record_unwind_protect (Fset_window_configuration,
2321 Fcurrent_window_configuration (Qnil));
2323 Fselect_window (window);
2324 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
2325 unbind_to (count, Qnil);
2332 static
2333 make_dummy_parent (window)
2334 Lisp_Object window;
2336 Lisp_Object new;
2337 register struct window *o, *p;
2338 register struct Lisp_Vector *vec;
2339 int i;
2341 o = XWINDOW (window);
2342 vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
2343 for (i = 0; i < VECSIZE (struct window); ++i)
2344 vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
2345 vec->size = VECSIZE (struct window);
2346 p = (struct window *)vec;
2347 XSETWINDOW (new, p);
2349 XSETFASTINT (p->sequence_number, ++sequence_number);
2351 /* Put new into window structure in place of window */
2352 replace_window (window, new);
2354 o->next = Qnil;
2355 o->prev = Qnil;
2356 o->vchild = Qnil;
2357 o->hchild = Qnil;
2358 o->parent = new;
2360 p->start = Qnil;
2361 p->pointm = Qnil;
2362 p->buffer = Qnil;
2365 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
2366 "Split WINDOW, putting SIZE lines in the first of the pair.\n\
2367 WINDOW defaults to selected one and SIZE to half its size.\n\
2368 If optional third arg HORFLAG is non-nil, split side by side\n\
2369 and put SIZE columns in the first of the pair.")
2370 (window, size, horflag)
2371 Lisp_Object window, size, horflag;
2373 register Lisp_Object new;
2374 register struct window *o, *p;
2375 FRAME_PTR fo;
2376 register int size_int;
2378 if (NILP (window))
2379 window = selected_window;
2380 else
2381 CHECK_LIVE_WINDOW (window, 0);
2383 o = XWINDOW (window);
2384 fo = XFRAME (WINDOW_FRAME (o));
2386 if (NILP (size))
2388 if (!NILP (horflag))
2389 /* Calculate the size of the left-hand window, by dividing
2390 the usable space in columns by two. */
2391 size_int = XFASTINT (o->width) >> 1;
2392 else
2393 size_int = XFASTINT (o->height) >> 1;
2395 else
2397 CHECK_NUMBER (size, 1);
2398 size_int = XINT (size);
2401 if (MINI_WINDOW_P (o))
2402 error ("Attempt to split minibuffer window");
2404 check_min_window_sizes ();
2406 if (NILP (horflag))
2408 if (size_int < window_min_height)
2409 error ("Window height %d too small (after splitting)", size_int);
2410 if (size_int + window_min_height > XFASTINT (o->height))
2411 error ("Window height %d too small (after splitting)",
2412 XFASTINT (o->height) - size_int);
2413 if (NILP (o->parent)
2414 || NILP (XWINDOW (o->parent)->vchild))
2416 make_dummy_parent (window);
2417 new = o->parent;
2418 XWINDOW (new)->vchild = window;
2421 else
2423 if (size_int < window_min_width)
2424 error ("Window width %d too small (after splitting)", size_int);
2426 if (size_int + window_min_width > XFASTINT (o->width))
2427 error ("Window width %d too small (after splitting)",
2428 XFASTINT (o->width) - size_int);
2429 if (NILP (o->parent)
2430 || NILP (XWINDOW (o->parent)->hchild))
2432 make_dummy_parent (window);
2433 new = o->parent;
2434 XWINDOW (new)->hchild = window;
2438 /* Now we know that window's parent is a vertical combination
2439 if we are dividing vertically, or a horizontal combination
2440 if we are making side-by-side windows */
2442 windows_or_buffers_changed++;
2443 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
2444 new = make_window ();
2445 p = XWINDOW (new);
2447 p->frame = o->frame;
2448 p->next = o->next;
2449 if (!NILP (p->next))
2450 XWINDOW (p->next)->prev = new;
2451 p->prev = window;
2452 o->next = new;
2453 p->parent = o->parent;
2454 p->buffer = Qt;
2456 /* Apportion the available frame space among the two new windows */
2458 if (!NILP (horflag))
2460 p->height = o->height;
2461 p->top = o->top;
2462 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
2463 XSETFASTINT (o->width, size_int);
2464 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
2466 else
2468 p->left = o->left;
2469 p->width = o->width;
2470 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
2471 XSETFASTINT (o->height, size_int);
2472 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
2475 Fset_window_buffer (new, o->buffer);
2477 return new;
2480 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
2481 "Make current window ARG lines bigger.\n\
2482 From program, optional second arg non-nil means grow sideways ARG columns.")
2483 (arg, side)
2484 register Lisp_Object arg, side;
2486 CHECK_NUMBER (arg, 0);
2487 change_window_height (XINT (arg), !NILP (side));
2489 if (! NILP (Vwindow_configuration_change_hook))
2490 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2492 return Qnil;
2495 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
2496 "Make current window ARG lines smaller.\n\
2497 From program, optional second arg non-nil means shrink sideways arg columns.")
2498 (arg, side)
2499 register Lisp_Object arg, side;
2501 CHECK_NUMBER (arg, 0);
2502 change_window_height (-XINT (arg), !NILP (side));
2504 if (! NILP (Vwindow_configuration_change_hook))
2505 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2507 return Qnil;
2511 window_height (window)
2512 Lisp_Object window;
2514 register struct window *p = XWINDOW (window);
2515 return XFASTINT (p->height);
2519 window_width (window)
2520 Lisp_Object window;
2522 register struct window *p = XWINDOW (window);
2523 return XFASTINT (p->width);
2526 #define MINSIZE(w) \
2527 (widthflag \
2528 ? window_min_width \
2529 : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
2531 #define CURBEG(w) \
2532 *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
2534 #define CURSIZE(w) \
2535 *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
2537 /* Unlike set_window_height, this function
2538 also changes the heights of the siblings so as to
2539 keep everything consistent. */
2541 change_window_height (delta, widthflag)
2542 register int delta;
2543 int widthflag;
2545 register Lisp_Object parent;
2546 Lisp_Object window;
2547 register struct window *p;
2548 int *sizep;
2549 int (*sizefun) () = widthflag ? window_width : window_height;
2550 register int (*setsizefun) () = (widthflag
2551 ? set_window_width
2552 : set_window_height);
2553 int maximum;
2554 Lisp_Object next, prev;
2556 check_min_window_sizes ();
2558 window = selected_window;
2559 while (1)
2561 p = XWINDOW (window);
2562 parent = p->parent;
2563 if (NILP (parent))
2565 if (widthflag)
2566 error ("No other window to side of this one");
2567 break;
2569 if (widthflag ? !NILP (XWINDOW (parent)->hchild)
2570 : !NILP (XWINDOW (parent)->vchild))
2571 break;
2572 window = parent;
2575 sizep = &CURSIZE (window);
2578 register int maxdelta;
2580 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
2581 : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
2582 : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
2583 /* This is a frame with only one window, a minibuffer-only
2584 or a minibufferless frame. */
2585 : (delta = 0));
2587 if (delta > maxdelta)
2588 /* This case traps trying to make the minibuffer
2589 the full frame, or make the only window aside from the
2590 minibuffer the full frame. */
2591 delta = maxdelta;
2594 if (*sizep + delta < MINSIZE (window))
2596 delete_window (window);
2597 return;
2600 if (delta == 0)
2601 return;
2603 /* Find the total we can get from other siblings. */
2604 maximum = 0;
2605 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
2606 maximum += (*sizefun) (next) - MINSIZE (next);
2607 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
2608 maximum += (*sizefun) (prev) - MINSIZE (prev);
2610 /* If we can get it all from them, do so. */
2611 if (delta < maximum)
2613 Lisp_Object first_unaffected;
2614 Lisp_Object first_affected;
2616 next = p->next;
2617 prev = p->prev;
2618 first_affected = window;
2619 /* Look at one sibling at a time,
2620 moving away from this window in both directions alternately,
2621 and take as much as we can get without deleting that sibling. */
2622 while (delta != 0)
2624 if (delta == 0)
2625 break;
2626 if (! NILP (next))
2628 int this_one = (*sizefun) (next) - MINSIZE (next);
2629 if (this_one > delta)
2630 this_one = delta;
2632 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
2633 (*setsizefun) (window, *sizep + this_one, 0);
2635 delta -= this_one;
2636 next = XWINDOW (next)->next;
2638 if (delta == 0)
2639 break;
2640 if (! NILP (prev))
2642 int this_one = (*sizefun) (prev) - MINSIZE (prev);
2643 if (this_one > delta)
2644 this_one = delta;
2646 first_affected = prev;
2648 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
2649 (*setsizefun) (window, *sizep + this_one, 0);
2651 delta -= this_one;
2652 prev = XWINDOW (prev)->prev;
2656 /* Now recalculate the edge positions of all the windows affected,
2657 based on the new sizes. */
2658 first_unaffected = next;
2659 prev = first_affected;
2660 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
2661 prev = next, next = XWINDOW (next)->next)
2663 CURBEG (next) = CURBEG (prev) + (*sizefun) (prev);
2664 /* This does not change size of NEXT,
2665 but it propagates the new top edge to its children */
2666 (*setsizefun) (next, (*sizefun) (next), 0);
2669 else
2671 register int delta1;
2672 register int opht = (*sizefun) (parent);
2674 /* If trying to grow this window to or beyond size of the parent,
2675 make delta1 so big that, on shrinking back down,
2676 all the siblings end up with less than one line and are deleted. */
2677 if (opht <= *sizep + delta)
2678 delta1 = opht * opht * 2;
2679 /* Otherwise, make delta1 just right so that if we add delta1
2680 lines to this window and to the parent, and then shrink
2681 the parent back to its original size, the new proportional
2682 size of this window will increase by delta. */
2683 else
2684 delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
2686 /* Add delta1 lines or columns to this window, and to the parent,
2687 keeping things consistent while not affecting siblings. */
2688 CURSIZE (parent) = opht + delta1;
2689 (*setsizefun) (window, *sizep + delta1, 0);
2691 /* Squeeze out delta1 lines or columns from our parent,
2692 shriking this window and siblings proportionately.
2693 This brings parent back to correct size.
2694 Delta1 was calculated so this makes this window the desired size,
2695 taking it all out of the siblings. */
2696 (*setsizefun) (parent, opht, 0);
2699 XSETFASTINT (p->last_modified, 0);
2700 XSETFASTINT (p->last_overlay_modified, 0);
2702 #undef MINSIZE
2703 #undef CURBEG
2704 #undef CURSIZE
2707 /* Return number of lines of text (not counting mode line) in W. */
2710 window_internal_height (w)
2711 struct window *w;
2713 int ht = XFASTINT (w->height);
2715 if (MINI_WINDOW_P (w))
2716 return ht;
2718 if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
2719 || !NILP (w->next) || !NILP (w->prev)
2720 || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
2721 return ht - 1;
2723 return ht;
2727 /* Return the number of columns in W.
2728 Don't count columns occupied by scroll bars or the vertical bar
2729 separating W from the sibling to its right. */
2731 window_internal_width (w)
2732 struct window *w;
2734 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2735 int width = XINT (w->width);
2737 /* Scroll bars occupy a few columns. */
2738 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2739 return width - FRAME_SCROLL_BAR_COLS (f);
2741 /* The column of `|' characters separating side-by-side windows
2742 occupies one column only. */
2743 if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
2744 return width - 1;
2746 return width;
2750 /* Scroll contents of window WINDOW up N lines.
2751 If WHOLE is nonzero, it means scroll N screenfuls instead. */
2753 static void
2754 window_scroll (window, n, whole, noerror)
2755 Lisp_Object window;
2756 int n;
2757 int whole;
2758 int noerror;
2760 register struct window *w = XWINDOW (window);
2761 register int opoint = PT;
2762 register int pos;
2763 register int ht = window_internal_height (w);
2764 register Lisp_Object tem;
2765 int lose;
2766 Lisp_Object bolp, nmoved;
2767 int startpos;
2768 struct position posit;
2769 int original_vpos;
2771 startpos = marker_position (w->start);
2773 posit = *compute_motion (startpos, 0, 0, 0,
2774 PT, ht, 0,
2775 window_internal_width (w), XINT (w->hscroll),
2776 0, w);
2777 original_vpos = posit.vpos;
2779 XSETFASTINT (tem, PT);
2780 tem = Fpos_visible_in_window_p (tem, window);
2782 if (NILP (tem))
2784 Fvertical_motion (make_number (- (ht / 2)), window);
2785 startpos = PT;
2788 SET_PT (startpos);
2789 lose = n < 0 && PT == BEGV;
2790 Fvertical_motion (make_number (n), window);
2791 pos = PT;
2792 bolp = Fbolp ();
2793 SET_PT (opoint);
2795 if (lose)
2797 if (noerror)
2798 return;
2799 else
2800 Fsignal (Qbeginning_of_buffer, Qnil);
2803 if (pos < ZV)
2805 int this_scroll_margin = scroll_margin;
2807 /* Don't use a scroll margin that is negative or too large. */
2808 if (this_scroll_margin < 0)
2809 this_scroll_margin = 0;
2811 if (XINT (w->height) < 4 * scroll_margin)
2812 this_scroll_margin = XINT (w->height) / 4;
2814 set_marker_restricted (w->start, make_number (pos), w->buffer);
2815 w->start_at_line_beg = bolp;
2816 w->update_mode_line = Qt;
2817 XSETFASTINT (w->last_modified, 0);
2818 XSETFASTINT (w->last_overlay_modified, 0);
2819 /* Set force_start so that redisplay_window will run
2820 the window-scroll-functions. */
2821 w->force_start = Qt;
2823 if (whole && scroll_preserve_screen_position)
2825 SET_PT (pos);
2826 Fvertical_motion (make_number (original_vpos), window);
2828 /* If we scrolled forward, put point enough lines down
2829 that it is outside the scroll margin. */
2830 else if (n > 0)
2832 int top_margin;
2834 if (this_scroll_margin > 0)
2836 SET_PT (pos);
2837 Fvertical_motion (make_number (this_scroll_margin), window);
2838 top_margin = PT;
2840 else
2841 top_margin = pos;
2843 if (top_margin <= opoint)
2844 SET_PT (opoint);
2845 else if (scroll_preserve_screen_position)
2847 SET_PT (pos);
2848 Fvertical_motion (make_number (original_vpos), window);
2850 else
2851 SET_PT (pos);
2853 else if (n < 0)
2855 int bottom_margin;
2857 /* If we scrolled backward, put point near the end of the window
2858 but not within the scroll margin. */
2859 SET_PT (pos);
2860 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
2861 if (XFASTINT (tem) == ht - this_scroll_margin)
2862 bottom_margin = PT;
2863 else
2864 bottom_margin = PT + 1;
2866 if (bottom_margin > opoint)
2867 SET_PT (opoint);
2868 else
2870 if (scroll_preserve_screen_position)
2872 SET_PT (pos);
2873 Fvertical_motion (make_number (original_vpos), window);
2875 else
2876 Fvertical_motion (make_number (-1), window);
2880 else
2882 if (noerror)
2883 return;
2884 else
2885 Fsignal (Qend_of_buffer, Qnil);
2889 /* This is the guts of Fscroll_up and Fscroll_down. */
2891 static void
2892 scroll_command (n, direction)
2893 register Lisp_Object n;
2894 int direction;
2896 register int defalt;
2897 int count = specpdl_ptr - specpdl;
2899 /* If selected window's buffer isn't current, make it current for the moment.
2900 But don't screw up if window_scroll gets an error. */
2901 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2903 record_unwind_protect (save_excursion_restore, save_excursion_save ());
2904 Fset_buffer (XWINDOW (selected_window)->buffer);
2907 defalt = (window_internal_height (XWINDOW (selected_window))
2908 - next_screen_context_lines);
2909 defalt = direction * (defalt < 1 ? 1 : defalt);
2911 if (NILP (n))
2912 window_scroll (selected_window, defalt, 1, 0);
2913 else if (EQ (n, Qminus))
2914 window_scroll (selected_window, - defalt, 1, 0);
2915 else
2917 n = Fprefix_numeric_value (n);
2918 window_scroll (selected_window, XINT (n) * direction, 0, 0);
2921 unbind_to (count, Qnil);
2924 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
2925 "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
2926 A near full screen is `next-screen-context-lines' less than a full screen.\n\
2927 Negative ARG means scroll downward.\n\
2928 When calling from a program, supply a number as argument or nil.")
2929 (arg)
2930 Lisp_Object arg;
2932 scroll_command (arg, 1);
2933 return Qnil;
2936 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
2937 "Scroll text of current window downward ARG lines; or near full screen if no ARG.\n\
2938 A near full screen is `next-screen-context-lines' less than a full screen.\n\
2939 Negative ARG means scroll upward.\n\
2940 When calling from a program, supply a number as argument or nil.")
2941 (arg)
2942 Lisp_Object arg;
2944 scroll_command (arg, -1);
2945 return Qnil;
2948 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
2949 "Return the other window for \"other window scroll\" commands.\n\
2950 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
2951 specifies the window.\n\
2952 If `other-window-scroll-buffer' is non-nil, a window\n\
2953 showing that buffer is used.")
2956 Lisp_Object window;
2958 if (MINI_WINDOW_P (XWINDOW (selected_window))
2959 && !NILP (Vminibuf_scroll_window))
2960 window = Vminibuf_scroll_window;
2961 /* If buffer is specified, scroll that buffer. */
2962 else if (!NILP (Vother_window_scroll_buffer))
2964 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
2965 if (NILP (window))
2966 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt);
2968 else
2970 /* Nothing specified; look for a neighboring window on the same
2971 frame. */
2972 window = Fnext_window (selected_window, Qnil, Qnil);
2974 if (EQ (window, selected_window))
2975 /* That didn't get us anywhere; look for a window on another
2976 visible frame. */
2978 window = Fnext_window (window, Qnil, Qt);
2979 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
2980 && ! EQ (window, selected_window));
2983 CHECK_LIVE_WINDOW (window, 0);
2985 if (EQ (window, selected_window))
2986 error ("There is no other window");
2988 return window;
2991 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
2992 "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
2993 The next window is the one below the current one; or the one at the top\n\
2994 if the current one is at the bottom. Negative ARG means scroll downward.\n\
2995 When calling from a program, supply a number as argument or nil.\n\
2997 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
2998 specifies the window to scroll.\n\
2999 If `other-window-scroll-buffer' is non-nil, scroll the window\n\
3000 showing that buffer, popping the buffer up if necessary.")
3001 (arg)
3002 register Lisp_Object arg;
3004 register Lisp_Object window;
3005 register int defalt;
3006 register struct window *w;
3007 register int count = specpdl_ptr - specpdl;
3009 window = Fother_window_for_scrolling ();
3011 w = XWINDOW (window);
3012 defalt = window_internal_height (w) - next_screen_context_lines;
3013 if (defalt < 1) defalt = 1;
3015 /* Don't screw up if window_scroll gets an error. */
3016 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3018 Fset_buffer (w->buffer);
3019 SET_PT (marker_position (w->pointm));
3021 if (NILP (arg))
3022 window_scroll (window, defalt, 1, 1);
3023 else if (EQ (arg, Qminus))
3024 window_scroll (window, -defalt, 1, 1);
3025 else
3027 if (CONSP (arg))
3028 arg = Fcar (arg);
3029 CHECK_NUMBER (arg, 0);
3030 window_scroll (window, XINT (arg), 0, 1);
3033 Fset_marker (w->pointm, make_number (PT), Qnil);
3034 unbind_to (count, Qnil);
3036 return Qnil;
3039 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
3040 "Scroll selected window display ARG columns left.\n\
3041 Default for ARG is window width minus 2.")
3042 (arg)
3043 register Lisp_Object arg;
3046 if (NILP (arg))
3047 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3048 else
3049 arg = Fprefix_numeric_value (arg);
3051 return
3052 Fset_window_hscroll (selected_window,
3053 make_number (XINT (XWINDOW (selected_window)->hscroll)
3054 + XINT (arg)));
3057 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
3058 "Scroll selected window display ARG columns right.\n\
3059 Default for ARG is window width minus 2.")
3060 (arg)
3061 register Lisp_Object arg;
3063 if (NILP (arg))
3064 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3065 else
3066 arg = Fprefix_numeric_value (arg);
3068 return
3069 Fset_window_hscroll (selected_window,
3070 make_number (XINT (XWINDOW (selected_window)->hscroll)
3071 - XINT (arg)));
3074 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
3075 "Center point in window and redisplay frame. With ARG, put point on line ARG.\n\
3076 The desired position of point is always relative to the current window.\n\
3077 Just C-u as prefix means put point in the center of the window.\n\
3078 If ARG is omitted or nil, erases the entire frame and then\n\
3079 redraws with point in the center of the current window.")
3080 (arg)
3081 register Lisp_Object arg;
3083 register struct window *w = XWINDOW (selected_window);
3084 register int ht = window_internal_height (w);
3085 struct position pos;
3087 if (NILP (arg))
3089 extern int frame_garbaged;
3091 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
3092 XSETFASTINT (arg, ht / 2);
3094 else if (CONSP (arg)) /* Just C-u. */
3096 XSETFASTINT (arg, ht / 2);
3098 else
3100 arg = Fprefix_numeric_value (arg);
3101 CHECK_NUMBER (arg, 0);
3104 if (XINT (arg) < 0)
3105 XSETINT (arg, XINT (arg) + ht);
3107 pos = *vmotion (PT, - XINT (arg), w);
3109 Fset_marker (w->start, make_number (pos.bufpos), w->buffer);
3110 w->start_at_line_beg = ((pos.bufpos == BEGV
3111 || FETCH_BYTE (pos.bufpos - 1) == '\n')
3112 ? Qt : Qnil);
3113 w->force_start = Qt;
3115 return Qnil;
3118 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
3119 1, 1, "P",
3120 "Position point relative to window.\n\
3121 With no argument, position point at center of window.\n\
3122 An argument specifies frame line; zero means top of window,\n\
3123 negative means relative to bottom of window.")
3124 (arg)
3125 register Lisp_Object arg;
3127 register struct window *w = XWINDOW (selected_window);
3128 register int height = window_internal_height (w);
3129 register int start;
3130 Lisp_Object window;
3132 if (NILP (arg))
3133 XSETFASTINT (arg, height / 2);
3134 else
3136 arg = Fprefix_numeric_value (arg);
3137 if (XINT (arg) < 0)
3138 XSETINT (arg, XINT (arg) + height);
3141 start = marker_position (w->start);
3142 XSETWINDOW (window, w);
3143 if (start < BEGV || start > ZV)
3145 Fvertical_motion (make_number (- (height / 2)), window);
3146 Fset_marker (w->start, make_number (PT), w->buffer);
3147 w->start_at_line_beg = Fbolp ();
3148 w->force_start = Qt;
3150 else
3151 SET_PT (start);
3153 return Fvertical_motion (arg, window);
3156 struct save_window_data
3158 EMACS_INT size_from_Lisp_Vector_struct;
3159 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3160 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
3161 Lisp_Object selected_frame;
3162 Lisp_Object current_window;
3163 Lisp_Object current_buffer;
3164 Lisp_Object minibuf_scroll_window;
3165 Lisp_Object root_window;
3166 Lisp_Object focus_frame;
3167 /* Record the values of window-min-width and window-min-height
3168 so that window sizes remain consistent with them. */
3169 Lisp_Object min_width, min_height;
3170 /* A vector, interpreted as a struct saved_window */
3171 Lisp_Object saved_windows;
3174 /* This is saved as a Lisp_Vector */
3175 struct saved_window
3177 /* these first two must agree with struct Lisp_Vector in lisp.h */
3178 EMACS_INT size_from_Lisp_Vector_struct;
3179 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3181 Lisp_Object window;
3182 Lisp_Object buffer, start, pointm, mark;
3183 Lisp_Object left, top, width, height, hscroll;
3184 Lisp_Object parent, prev;
3185 Lisp_Object start_at_line_beg;
3186 Lisp_Object display_table;
3188 #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
3190 #define SAVED_WINDOW_N(swv,n) \
3191 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
3193 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
3194 "T if OBJECT is a window-configuration object.")
3195 (object)
3196 Lisp_Object object;
3198 if (WINDOW_CONFIGURATIONP (object))
3199 return Qt;
3200 return Qnil;
3204 DEFUN ("set-window-configuration", Fset_window_configuration,
3205 Sset_window_configuration, 1, 1, 0,
3206 "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
3207 CONFIGURATION must be a value previously returned\n\
3208 by `current-window-configuration' (which see).")
3209 (configuration)
3210 Lisp_Object configuration;
3212 register struct save_window_data *data;
3213 struct Lisp_Vector *saved_windows;
3214 Lisp_Object new_current_buffer;
3215 Lisp_Object frame;
3216 FRAME_PTR f;
3217 int old_point = -1;
3219 while (!WINDOW_CONFIGURATIONP (configuration))
3221 configuration = wrong_type_argument (intern ("window-configuration-p"),
3222 configuration);
3225 data = (struct save_window_data *) XVECTOR (configuration);
3226 saved_windows = XVECTOR (data->saved_windows);
3228 new_current_buffer = data->current_buffer;
3229 if (NILP (XBUFFER (new_current_buffer)->name))
3230 new_current_buffer = Qnil;
3231 else
3233 if (XBUFFER (new_current_buffer) == current_buffer)
3234 old_point = PT;
3237 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3238 f = XFRAME (frame);
3240 /* If f is a dead frame, don't bother rebuilding its window tree.
3241 However, there is other stuff we should still try to do below. */
3242 if (FRAME_LIVE_P (f))
3244 register struct window *w;
3245 register struct saved_window *p;
3246 int k;
3248 /* If the frame has been resized since this window configuration was
3249 made, we change the frame to the size specified in the
3250 configuration, restore the configuration, and then resize it
3251 back. We keep track of the prevailing height in these variables. */
3252 int previous_frame_height = FRAME_HEIGHT (f);
3253 int previous_frame_width = FRAME_WIDTH (f);
3254 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
3256 /* The mouse highlighting code could get screwed up
3257 if it runs during this. */
3258 BLOCK_INPUT;
3260 if (XFASTINT (data->frame_height) != previous_frame_height
3261 || XFASTINT (data->frame_width) != previous_frame_width)
3262 change_frame_size (f, data->frame_height, data->frame_width, 0, 0);
3263 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3264 if (XFASTINT (data->frame_menu_bar_lines)
3265 != previous_frame_menu_bar_lines)
3266 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, 0);
3267 #endif
3269 windows_or_buffers_changed++;
3270 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3272 /* Temporarily avoid any problems with windows that are smaller
3273 than they are supposed to be. */
3274 window_min_height = 1;
3275 window_min_width = 1;
3277 /* Kludge Alert!
3278 Mark all windows now on frame as "deleted".
3279 Restoring the new configuration "undeletes" any that are in it.
3281 Save their current buffers in their height fields, since we may
3282 need it later, if a buffer saved in the configuration is now
3283 dead. */
3284 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3286 for (k = 0; k < saved_windows->size; k++)
3288 p = SAVED_WINDOW_N (saved_windows, k);
3289 w = XWINDOW (p->window);
3290 w->next = Qnil;
3292 if (!NILP (p->parent))
3293 w->parent = SAVED_WINDOW_N (saved_windows,
3294 XFASTINT (p->parent))->window;
3295 else
3296 w->parent = Qnil;
3298 if (!NILP (p->prev))
3300 w->prev = SAVED_WINDOW_N (saved_windows,
3301 XFASTINT (p->prev))->window;
3302 XWINDOW (w->prev)->next = p->window;
3304 else
3306 w->prev = Qnil;
3307 if (!NILP (w->parent))
3309 if (EQ (p->width, XWINDOW (w->parent)->width))
3311 XWINDOW (w->parent)->vchild = p->window;
3312 XWINDOW (w->parent)->hchild = Qnil;
3314 else
3316 XWINDOW (w->parent)->hchild = p->window;
3317 XWINDOW (w->parent)->vchild = Qnil;
3322 /* If we squirreled away the buffer in the window's height,
3323 restore it now. */
3324 if (BUFFERP (w->height))
3325 w->buffer = w->height;
3326 w->left = p->left;
3327 w->top = p->top;
3328 w->width = p->width;
3329 w->height = p->height;
3330 w->hscroll = p->hscroll;
3331 w->display_table = p->display_table;
3332 XSETFASTINT (w->last_modified, 0);
3333 XSETFASTINT (w->last_overlay_modified, 0);
3335 /* Reinstall the saved buffer and pointers into it. */
3336 if (NILP (p->buffer))
3337 w->buffer = p->buffer;
3338 else
3340 if (!NILP (XBUFFER (p->buffer)->name))
3341 /* If saved buffer is alive, install it. */
3343 w->buffer = p->buffer;
3344 w->start_at_line_beg = p->start_at_line_beg;
3345 set_marker_restricted (w->start,
3346 Fmarker_position (p->start),
3347 w->buffer);
3348 set_marker_restricted (w->pointm,
3349 Fmarker_position (p->pointm),
3350 w->buffer);
3351 Fset_marker (XBUFFER (w->buffer)->mark,
3352 Fmarker_position (p->mark), w->buffer);
3354 /* As documented in Fcurrent_window_configuration, don't
3355 save the location of point in the buffer which was current
3356 when the window configuration was recorded. */
3357 if (!EQ (p->buffer, new_current_buffer)
3358 && XBUFFER (p->buffer) == current_buffer)
3359 Fgoto_char (w->pointm);
3361 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
3362 /* Else unless window has a live buffer, get one. */
3364 w->buffer = Fcdr (Fcar (Vbuffer_alist));
3365 /* This will set the markers to beginning of visible
3366 range. */
3367 set_marker_restricted (w->start, make_number (0), w->buffer);
3368 set_marker_restricted (w->pointm, make_number (0),w->buffer);
3369 w->start_at_line_beg = Qt;
3371 else
3372 /* Keeping window's old buffer; make sure the markers
3373 are real. */
3375 /* Set window markers at start of visible range. */
3376 if (XMARKER (w->start)->buffer == 0)
3377 set_marker_restricted (w->start, make_number (0),
3378 w->buffer);
3379 if (XMARKER (w->pointm)->buffer == 0)
3380 set_marker_restricted (w->pointm,
3381 (make_number
3382 (BUF_PT (XBUFFER (w->buffer)))),
3383 w->buffer);
3384 w->start_at_line_beg = Qt;
3389 FRAME_ROOT_WINDOW (f) = data->root_window;
3390 Fselect_window (data->current_window);
3391 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
3392 = selected_window;
3394 if (NILP (data->focus_frame)
3395 || (FRAMEP (data->focus_frame)
3396 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
3397 Fredirect_frame_focus (frame, data->focus_frame);
3399 #if 0 /* I don't understand why this is needed, and it causes problems
3400 when the frame's old selected window has been deleted. */
3401 if (f != selected_frame && FRAME_WINDOW_P (f))
3402 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
3403 Qnil, 0);
3404 #endif
3406 /* Set the screen height to the value it had before this function. */
3407 if (previous_frame_height != FRAME_HEIGHT (f)
3408 || previous_frame_width != FRAME_WIDTH (f))
3409 change_frame_size (f, previous_frame_height, previous_frame_width,
3410 0, 0);
3411 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3412 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
3413 x_set_menu_bar_lines (f, previous_frame_menu_bar_lines, 0);
3414 #endif
3416 UNBLOCK_INPUT;
3419 /* Restore the minimum heights recorded in the configuration. */
3420 window_min_height = XINT (data->min_height);
3421 window_min_width = XINT (data->min_width);
3423 /* Fselect_window will have made f the selected frame, so we
3424 reselect the proper frame here. Fhandle_switch_frame will change the
3425 selected window too, but that doesn't make the call to
3426 Fselect_window above totally superfluous; it still sets f's
3427 selected window. */
3428 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
3429 do_switch_frame (data->selected_frame, Qnil, 0);
3431 if (!NILP (new_current_buffer))
3433 Fset_buffer (new_current_buffer);
3435 /* If the buffer that is current now is the same
3436 that was current before setting the window configuration,
3437 don't alter its PT. */
3438 if (old_point >= 0)
3439 SET_PT (old_point);
3442 Vminibuf_scroll_window = data->minibuf_scroll_window;
3444 if (! NILP (Vwindow_configuration_change_hook)
3445 && ! NILP (Vrun_hooks))
3446 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3448 return (Qnil);
3451 /* Mark all windows now on frame as deleted
3452 by setting their buffers to nil. */
3454 void
3455 delete_all_subwindows (w)
3456 register struct window *w;
3458 if (!NILP (w->next))
3459 delete_all_subwindows (XWINDOW (w->next));
3460 if (!NILP (w->vchild))
3461 delete_all_subwindows (XWINDOW (w->vchild));
3462 if (!NILP (w->hchild))
3463 delete_all_subwindows (XWINDOW (w->hchild));
3465 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
3467 if (!NILP (w->buffer))
3468 unshow_buffer (w);
3470 /* We set all three of these fields to nil, to make sure that we can
3471 distinguish this dead window from any live window. Live leaf
3472 windows will have buffer set, and combination windows will have
3473 vchild or hchild set. */
3474 w->buffer = Qnil;
3475 w->vchild = Qnil;
3476 w->hchild = Qnil;
3479 static int
3480 count_windows (window)
3481 register struct window *window;
3483 register int count = 1;
3484 if (!NILP (window->next))
3485 count += count_windows (XWINDOW (window->next));
3486 if (!NILP (window->vchild))
3487 count += count_windows (XWINDOW (window->vchild));
3488 if (!NILP (window->hchild))
3489 count += count_windows (XWINDOW (window->hchild));
3490 return count;
3493 static int
3494 save_window_save (window, vector, i)
3495 Lisp_Object window;
3496 struct Lisp_Vector *vector;
3497 int i;
3499 register struct saved_window *p;
3500 register struct window *w;
3501 register Lisp_Object tem;
3503 for (;!NILP (window); window = w->next)
3505 p = SAVED_WINDOW_N (vector, i);
3506 w = XWINDOW (window);
3508 XSETFASTINT (w->temslot, i++);
3509 p->window = window;
3510 p->buffer = w->buffer;
3511 p->left = w->left;
3512 p->top = w->top;
3513 p->width = w->width;
3514 p->height = w->height;
3515 p->hscroll = w->hscroll;
3516 p->display_table = w->display_table;
3517 if (!NILP (w->buffer))
3519 /* Save w's value of point in the window configuration.
3520 If w is the selected window, then get the value of point
3521 from the buffer; pointm is garbage in the selected window. */
3522 if (EQ (window, selected_window))
3524 p->pointm = Fmake_marker ();
3525 Fset_marker (p->pointm,
3526 make_number (BUF_PT (XBUFFER (w->buffer))),
3527 w->buffer);
3529 else
3530 p->pointm = Fcopy_marker (w->pointm, Qnil);
3532 p->start = Fcopy_marker (w->start, Qnil);
3533 p->start_at_line_beg = w->start_at_line_beg;
3535 tem = XBUFFER (w->buffer)->mark;
3536 p->mark = Fcopy_marker (tem, Qnil);
3538 else
3540 p->pointm = Qnil;
3541 p->start = Qnil;
3542 p->mark = Qnil;
3543 p->start_at_line_beg = Qnil;
3546 if (NILP (w->parent))
3547 p->parent = Qnil;
3548 else
3549 p->parent = XWINDOW (w->parent)->temslot;
3551 if (NILP (w->prev))
3552 p->prev = Qnil;
3553 else
3554 p->prev = XWINDOW (w->prev)->temslot;
3556 if (!NILP (w->vchild))
3557 i = save_window_save (w->vchild, vector, i);
3558 if (!NILP (w->hchild))
3559 i = save_window_save (w->hchild, vector, i);
3562 return i;
3565 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
3566 Scurrent_window_configuration, 0, 1, 0,
3567 "Return an object representing the current window configuration of FRAME.\n\
3568 If FRAME is nil or omitted, use the selected frame.\n\
3569 This describes the number of windows, their sizes and current buffers,\n\
3570 and for each displayed buffer, where display starts, and the positions of\n\
3571 point and mark. An exception is made for point in the current buffer:\n\
3572 its value is -not- saved.\n\
3573 This also records the currently selected frame, and FRAME's focus\n\
3574 redirection (see `redirect-frame-focus').")
3575 (frame)
3576 Lisp_Object frame;
3578 register Lisp_Object tem;
3579 register int n_windows;
3580 register struct save_window_data *data;
3581 register struct Lisp_Vector *vec;
3582 register int i;
3583 FRAME_PTR f;
3585 if (NILP (frame))
3586 f = selected_frame;
3587 else
3589 CHECK_LIVE_FRAME (frame, 0);
3590 f = XFRAME (frame);
3593 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3594 vec = allocate_vectorlike (VECSIZE (struct save_window_data));
3595 for (i = 0; i < VECSIZE (struct save_window_data); i++)
3596 vec->contents[i] = Qnil;
3597 vec->size = VECSIZE (struct save_window_data);
3598 data = (struct save_window_data *)vec;
3600 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
3601 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
3602 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
3603 XSETFRAME (data->selected_frame, selected_frame);
3604 data->current_window = FRAME_SELECTED_WINDOW (f);
3605 XSETBUFFER (data->current_buffer, current_buffer);
3606 data->minibuf_scroll_window = Vminibuf_scroll_window;
3607 data->root_window = FRAME_ROOT_WINDOW (f);
3608 data->focus_frame = FRAME_FOCUS_FRAME (f);
3609 XSETINT (data->min_height, window_min_height);
3610 XSETINT (data->min_width, window_min_width);
3611 tem = Fmake_vector (make_number (n_windows), Qnil);
3612 data->saved_windows = tem;
3613 for (i = 0; i < n_windows; i++)
3614 XVECTOR (tem)->contents[i]
3615 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
3616 save_window_save (FRAME_ROOT_WINDOW (f),
3617 XVECTOR (tem), 0);
3618 XSETWINDOW_CONFIGURATION (tem, data);
3619 return (tem);
3622 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
3623 0, UNEVALLED, 0,
3624 "Execute body, preserving window sizes and contents.\n\
3625 Restore which buffer appears in which window, where display starts,\n\
3626 and the value of point and mark for each window.\n\
3627 Also restore which buffer is current.\n\
3628 But do not preserve point in the current buffer.\n\
3629 Does not restore the value of point in current buffer.")
3630 (args)
3631 Lisp_Object args;
3633 register Lisp_Object val;
3634 register int count = specpdl_ptr - specpdl;
3636 record_unwind_protect (Fset_window_configuration,
3637 Fcurrent_window_configuration (Qnil));
3638 val = Fprogn (args);
3639 return unbind_to (count, val);
3642 init_window_once ()
3644 selected_frame = make_terminal_frame ();
3645 XSETFRAME (Vterminal_frame, selected_frame);
3646 minibuf_window = selected_frame->minibuffer_window;
3647 selected_window = selected_frame->selected_window;
3648 last_nonminibuf_frame = selected_frame;
3650 window_initialized = 1;
3653 syms_of_window ()
3655 staticpro (&Qwindow_configuration_change_hook);
3656 Qwindow_configuration_change_hook
3657 = intern ("window-configuration-change-hook");
3659 Qwindowp = intern ("windowp");
3660 staticpro (&Qwindowp);
3662 Qwindow_live_p = intern ("window-live-p");
3663 staticpro (&Qwindow_live_p);
3665 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
3666 staticpro (&Qtemp_buffer_show_hook);
3668 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
3669 "Non-nil means call as function to display a help buffer.\n\
3670 The function is called with one argument, the buffer to be displayed.\n\
3671 Used by `with-output-to-temp-buffer'.\n\
3672 If this function is used, then it must do the entire job of showing\n\
3673 the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
3674 Vtemp_buffer_show_function = Qnil;
3676 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
3677 "If non-nil, function to call to handle `display-buffer'.\n\
3678 It will receive two args, the buffer and a flag which if non-nil means\n\
3679 that the currently selected window is not acceptable.\n\
3680 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
3681 work using this function.");
3682 Vdisplay_buffer_function = Qnil;
3684 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
3685 "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
3686 Vminibuf_scroll_window = Qnil;
3688 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
3689 "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
3690 Vother_window_scroll_buffer = Qnil;
3692 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
3693 "*Non-nil means `display-buffer' should make a separate frame.");
3694 pop_up_frames = 0;
3696 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
3697 "Function to call to handle automatic new frame creation.\n\
3698 It is called with no arguments and should return a newly created frame.\n\
3700 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
3701 where `pop-up-frame-alist' would hold the default frame parameters.");
3702 Vpop_up_frame_function = Qnil;
3704 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
3705 "*List of buffer names that should have their own special frames.\n\
3706 Displaying a buffer whose name is in this list makes a special frame for it\n\
3707 using `special-display-function'. See also `special-display-regexps'.\n\
3709 An element of the list can be a list instead of just a string.\n\
3710 There are two ways to use a list as an element:\n\
3711 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)\n\
3712 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3713 In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
3714 followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
3715 All this is done by the function found in `special-display-function'.");
3716 Vspecial_display_buffer_names = Qnil;
3718 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
3719 "*List of regexps saying which buffers should have their own special frames.\n\
3720 If a buffer name matches one of these regexps, it gets its own frame.\n\
3721 Displaying a buffer whose name is in this list makes a special frame for it\n\
3722 using `special-display-function'.\n\
3724 An element of the list can be a list instead of just a string.\n\
3725 There are two ways to use a list as an element:\n\
3726 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)\n\
3727 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3728 In the latter case, FUNCTION is called with the buffer as first argument,\n\
3729 followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
3730 All this is done by the function found in `special-display-function'.");
3731 Vspecial_display_regexps = Qnil;
3733 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
3734 "Function to call to make a new frame for a special buffer.\n\
3735 It is called with two arguments, the buffer and optional buffer specific\n\
3736 data, and should return a window displaying that buffer.\n\
3737 The default value makes a separate frame for the buffer,\n\
3738 using `special-display-frame-alist' to specify the frame parameters.\n\
3740 A buffer is special if its is listed in `special-display-buffer-names'\n\
3741 or matches a regexp in `special-display-regexps'.");
3742 Vspecial_display_function = Qnil;
3744 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
3745 "*List of buffer names that should appear in the selected window.\n\
3746 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
3747 switches to it in the selected window, rather than making it appear\n\
3748 in some other window.\n\
3750 An element of the list can be a cons cell instead of just a string.\n\
3751 Then the car must be a string, which specifies the buffer name.\n\
3752 This is for compatibility with `special-display-buffer-names';\n\
3753 the cdr of the cons cell is ignored.\n\
3755 See also `same-window-regexps'.");
3756 Vsame_window_buffer_names = Qnil;
3758 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
3759 "*List of regexps saying which buffers should appear in the selected window.\n\
3760 If a buffer name matches one of these regexps, then displaying it\n\
3761 using `display-buffer' or `pop-to-buffer' switches to it\n\
3762 in the selected window, rather than making it appear in some other window.\n\
3764 An element of the list can be a cons cell instead of just a string.\n\
3765 Then the car must be a string, which specifies the buffer name.\n\
3766 This is for compatibility with `special-display-buffer-names';\n\
3767 the cdr of the cons cell is ignored.\n\
3769 See also `same-window-buffer-names'.");
3770 Vsame_window_regexps = Qnil;
3772 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
3773 "*Non-nil means display-buffer should make new windows.");
3774 pop_up_windows = 1;
3776 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
3777 "*Number of lines of continuity when scrolling by screenfuls.");
3778 next_screen_context_lines = 2;
3780 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
3781 "*display-buffer would prefer to split the largest window if this large.\n\
3782 If there is only one window, it is split regardless of this value.");
3783 split_height_threshold = 500;
3785 DEFVAR_INT ("window-min-height", &window_min_height,
3786 "*Delete any window less than this tall (including its mode line).");
3787 window_min_height = 4;
3789 DEFVAR_INT ("window-min-width", &window_min_width,
3790 "*Delete any window less than this wide.");
3791 window_min_width = 10;
3793 DEFVAR_BOOL ("scroll-preserve-screen-position",
3794 &scroll_preserve_screen_position,
3795 "*Nonzero means scroll commands move point to keep its screen line unchanged.");
3796 scroll_preserve_screen_position = 0;
3798 DEFVAR_LISP ("window-configuration-change-hook",
3799 &Vwindow_configuration_change_hook,
3800 "Functions to call when window configuration changes.\n\
3801 The selected frae is the one whose configuration has changed.");
3802 Vwindow_configuration_change_hook = Qnil;
3804 defsubr (&Sselected_window);
3805 defsubr (&Sminibuffer_window);
3806 defsubr (&Swindow_minibuffer_p);
3807 defsubr (&Swindowp);
3808 defsubr (&Swindow_live_p);
3809 defsubr (&Spos_visible_in_window_p);
3810 defsubr (&Swindow_buffer);
3811 defsubr (&Swindow_height);
3812 defsubr (&Swindow_width);
3813 defsubr (&Swindow_hscroll);
3814 defsubr (&Sset_window_hscroll);
3815 defsubr (&Swindow_redisplay_end_trigger);
3816 defsubr (&Sset_window_redisplay_end_trigger);
3817 defsubr (&Swindow_edges);
3818 defsubr (&Scoordinates_in_window_p);
3819 defsubr (&Swindow_at);
3820 defsubr (&Swindow_point);
3821 defsubr (&Swindow_start);
3822 defsubr (&Swindow_end);
3823 defsubr (&Sset_window_point);
3824 defsubr (&Sset_window_start);
3825 defsubr (&Swindow_dedicated_p);
3826 defsubr (&Sset_window_dedicated_p);
3827 defsubr (&Swindow_display_table);
3828 defsubr (&Sset_window_display_table);
3829 defsubr (&Snext_window);
3830 defsubr (&Sprevious_window);
3831 defsubr (&Sother_window);
3832 defsubr (&Sget_lru_window);
3833 defsubr (&Sget_largest_window);
3834 defsubr (&Sget_buffer_window);
3835 defsubr (&Sdelete_other_windows);
3836 defsubr (&Sdelete_windows_on);
3837 defsubr (&Sreplace_buffer_in_windows);
3838 defsubr (&Sdelete_window);
3839 defsubr (&Sset_window_buffer);
3840 defsubr (&Sselect_window);
3841 defsubr (&Sspecial_display_p);
3842 defsubr (&Ssame_window_p);
3843 defsubr (&Sdisplay_buffer);
3844 defsubr (&Ssplit_window);
3845 defsubr (&Senlarge_window);
3846 defsubr (&Sshrink_window);
3847 defsubr (&Sscroll_up);
3848 defsubr (&Sscroll_down);
3849 defsubr (&Sscroll_left);
3850 defsubr (&Sscroll_right);
3851 defsubr (&Sother_window_for_scrolling);
3852 defsubr (&Sscroll_other_window);
3853 defsubr (&Srecenter);
3854 defsubr (&Smove_to_window_line);
3855 defsubr (&Swindow_configuration_p);
3856 defsubr (&Sset_window_configuration);
3857 defsubr (&Scurrent_window_configuration);
3858 defsubr (&Ssave_window_excursion);
3861 keys_of_window ()
3863 initial_define_key (control_x_map, '1', "delete-other-windows");
3864 initial_define_key (control_x_map, '2', "split-window");
3865 initial_define_key (control_x_map, '0', "delete-window");
3866 initial_define_key (control_x_map, 'o', "other-window");
3867 initial_define_key (control_x_map, '^', "enlarge-window");
3868 initial_define_key (control_x_map, '<', "scroll-left");
3869 initial_define_key (control_x_map, '>', "scroll-right");
3871 initial_define_key (global_map, Ctl ('V'), "scroll-up");
3872 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
3873 initial_define_key (meta_map, 'v', "scroll-down");
3875 initial_define_key (global_map, Ctl('L'), "recenter");
3876 initial_define_key (meta_map, 'r', "move-to-window-line");