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