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