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