]> code.delx.au - gnu-emacs/blob - src/window.c
(Vhighlight_wrong_size_font, Vclip_large_size_font):
[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 Qnil;
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 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 && XWINDOW (other)->top != XWINDOW (window)->top
2241 && XWINDOW (other)->height > XWINDOW (window)->height)
2242 {
2243 int total = XWINDOW (other)->height + XWINDOW (window)->height;
2244 Lisp_Object old_selected_window;
2245 old_selected_window = selected_window;
2246
2247 selected_window = upper;
2248 change_window_height (total / 2 - XWINDOW (upper)->height, 0);
2249 selected_window = old_selected_window;
2250 }
2251 }
2252 }
2253 else
2254 window = Fget_lru_window (Qnil);
2255
2256 Fset_window_buffer (window, buffer);
2257 return display_buffer_1 (window);
2258 }
2259
2260 void
2261 temp_output_buffer_show (buf)
2262 register Lisp_Object buf;
2263 {
2264 register struct buffer *old = current_buffer;
2265 register Lisp_Object window;
2266 register struct window *w;
2267
2268 Fset_buffer (buf);
2269 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
2270 BEGV = BEG;
2271 ZV = Z;
2272 SET_PT (BEG);
2273 XBUFFER (buf)->clip_changed = 1;
2274 set_buffer_internal (old);
2275
2276 if (!EQ (Vtemp_buffer_show_function, Qnil))
2277 call1 (Vtemp_buffer_show_function, buf);
2278 else
2279 {
2280 window = Fdisplay_buffer (buf, Qnil);
2281
2282 if (XFRAME (XWINDOW (window)->frame) != selected_frame)
2283 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2284 Vminibuf_scroll_window = window;
2285 w = XWINDOW (window);
2286 XSETFASTINT (w->hscroll, 0);
2287 set_marker_restricted (w->start, make_number (1), buf);
2288 set_marker_restricted (w->pointm, make_number (1), buf);
2289
2290 /* Run temp-buffer-show-hook, with the chosen window selected. */
2291 if (!NILP (Vrun_hooks))
2292 {
2293 Lisp_Object tem;
2294 tem = Fboundp (Qtemp_buffer_show_hook);
2295 if (!NILP (tem))
2296 {
2297 tem = Fsymbol_value (Qtemp_buffer_show_hook);
2298 if (!NILP (tem))
2299 {
2300 int count = specpdl_ptr - specpdl;
2301
2302 /* Select the window that was chosen, for running the hook. */
2303 record_unwind_protect (Fset_window_configuration,
2304 Fcurrent_window_configuration (Qnil));
2305
2306 Fselect_window (window);
2307 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
2308 unbind_to (count, Qnil);
2309 }
2310 }
2311 }
2312 }
2313 }
2314 \f
2315 static
2316 make_dummy_parent (window)
2317 Lisp_Object window;
2318 {
2319 Lisp_Object new;
2320 register struct window *o, *p;
2321 register struct Lisp_Vector *vec;
2322 int i;
2323
2324 o = XWINDOW (window);
2325 vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
2326 for (i = 0; i < VECSIZE (struct window); ++i)
2327 vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
2328 vec->size = VECSIZE (struct window);
2329 p = (struct window *)vec;
2330 XSETWINDOW (new, p);
2331
2332 XSETFASTINT (p->sequence_number, ++sequence_number);
2333
2334 /* Put new into window structure in place of window */
2335 replace_window (window, new);
2336
2337 o->next = Qnil;
2338 o->prev = Qnil;
2339 o->vchild = Qnil;
2340 o->hchild = Qnil;
2341 o->parent = new;
2342
2343 p->start = Qnil;
2344 p->pointm = Qnil;
2345 p->buffer = Qnil;
2346 }
2347
2348 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
2349 "Split WINDOW, putting SIZE lines in the first of the pair.\n\
2350 WINDOW defaults to selected one and SIZE to half its size.\n\
2351 If optional third arg HORFLAG is non-nil, split side by side\n\
2352 and put SIZE columns in the first of the pair.")
2353 (window, size, horflag)
2354 Lisp_Object window, size, horflag;
2355 {
2356 register Lisp_Object new;
2357 register struct window *o, *p;
2358 FRAME_PTR fo;
2359 register int size_int;
2360
2361 if (NILP (window))
2362 window = selected_window;
2363 else
2364 CHECK_LIVE_WINDOW (window, 0);
2365
2366 o = XWINDOW (window);
2367 fo = XFRAME (WINDOW_FRAME (o));
2368
2369 if (NILP (size))
2370 {
2371 if (!NILP (horflag))
2372 /* Calculate the size of the left-hand window, by dividing
2373 the usable space in columns by two. */
2374 size_int = XFASTINT (o->width) >> 1;
2375 else
2376 size_int = XFASTINT (o->height) >> 1;
2377 }
2378 else
2379 {
2380 CHECK_NUMBER (size, 1);
2381 size_int = XINT (size);
2382 }
2383
2384 if (MINI_WINDOW_P (o))
2385 error ("Attempt to split minibuffer window");
2386 else if (FRAME_NO_SPLIT_P (fo))
2387 error ("Attempt to split unsplittable frame");
2388
2389 check_min_window_sizes ();
2390
2391 if (NILP (horflag))
2392 {
2393 if (size_int < window_min_height)
2394 error ("Window height %d too small (after splitting)", size_int);
2395 if (size_int + window_min_height > XFASTINT (o->height))
2396 error ("Window height %d too small (after splitting)",
2397 XFASTINT (o->height) - size_int);
2398 if (NILP (o->parent)
2399 || NILP (XWINDOW (o->parent)->vchild))
2400 {
2401 make_dummy_parent (window);
2402 new = o->parent;
2403 XWINDOW (new)->vchild = window;
2404 }
2405 }
2406 else
2407 {
2408 if (size_int < window_min_width)
2409 error ("Window width %d too small (after splitting)", size_int);
2410
2411 if (size_int + window_min_width > XFASTINT (o->width))
2412 error ("Window width %d too small (after splitting)",
2413 XFASTINT (o->width) - size_int);
2414 if (NILP (o->parent)
2415 || NILP (XWINDOW (o->parent)->hchild))
2416 {
2417 make_dummy_parent (window);
2418 new = o->parent;
2419 XWINDOW (new)->hchild = window;
2420 }
2421 }
2422
2423 /* Now we know that window's parent is a vertical combination
2424 if we are dividing vertically, or a horizontal combination
2425 if we are making side-by-side windows */
2426
2427 windows_or_buffers_changed++;
2428 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
2429 new = make_window ();
2430 p = XWINDOW (new);
2431
2432 p->frame = o->frame;
2433 p->next = o->next;
2434 if (!NILP (p->next))
2435 XWINDOW (p->next)->prev = new;
2436 p->prev = window;
2437 o->next = new;
2438 p->parent = o->parent;
2439 p->buffer = Qt;
2440
2441 /* Apportion the available frame space among the two new windows */
2442
2443 if (!NILP (horflag))
2444 {
2445 p->height = o->height;
2446 p->top = o->top;
2447 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
2448 XSETFASTINT (o->width, size_int);
2449 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
2450 }
2451 else
2452 {
2453 p->left = o->left;
2454 p->width = o->width;
2455 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
2456 XSETFASTINT (o->height, size_int);
2457 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
2458 }
2459
2460 Fset_window_buffer (new, o->buffer);
2461
2462 return new;
2463 }
2464 \f
2465 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
2466 "Make current window ARG lines bigger.\n\
2467 From program, optional second arg non-nil means grow sideways ARG columns.")
2468 (arg, side)
2469 register Lisp_Object arg, side;
2470 {
2471 CHECK_NUMBER (arg, 0);
2472 change_window_height (XINT (arg), !NILP (side));
2473
2474 if (! NILP (Vwindow_configuration_change_hook))
2475 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2476
2477 return Qnil;
2478 }
2479
2480 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
2481 "Make current window ARG lines smaller.\n\
2482 From program, optional second arg non-nil means shrink sideways arg columns.")
2483 (arg, side)
2484 register Lisp_Object arg, side;
2485 {
2486 CHECK_NUMBER (arg, 0);
2487 change_window_height (-XINT (arg), !NILP (side));
2488
2489 if (! NILP (Vwindow_configuration_change_hook))
2490 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2491
2492 return Qnil;
2493 }
2494
2495 int
2496 window_height (window)
2497 Lisp_Object window;
2498 {
2499 register struct window *p = XWINDOW (window);
2500 return XFASTINT (p->height);
2501 }
2502
2503 int
2504 window_width (window)
2505 Lisp_Object window;
2506 {
2507 register struct window *p = XWINDOW (window);
2508 return XFASTINT (p->width);
2509 }
2510
2511 #define MINSIZE(w) \
2512 (widthflag \
2513 ? window_min_width \
2514 : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
2515
2516 #define CURBEG(w) \
2517 *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
2518
2519 #define CURSIZE(w) \
2520 *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
2521
2522 /* Unlike set_window_height, this function
2523 also changes the heights of the siblings so as to
2524 keep everything consistent. */
2525
2526 change_window_height (delta, widthflag)
2527 register int delta;
2528 int widthflag;
2529 {
2530 register Lisp_Object parent;
2531 Lisp_Object window;
2532 register struct window *p;
2533 int *sizep;
2534 int (*sizefun) () = widthflag ? window_width : window_height;
2535 register int (*setsizefun) () = (widthflag
2536 ? set_window_width
2537 : set_window_height);
2538 int maximum;
2539 Lisp_Object next, prev;
2540
2541 check_min_window_sizes ();
2542
2543 window = selected_window;
2544 while (1)
2545 {
2546 p = XWINDOW (window);
2547 parent = p->parent;
2548 if (NILP (parent))
2549 {
2550 if (widthflag)
2551 error ("No other window to side of this one");
2552 break;
2553 }
2554 if (widthflag ? !NILP (XWINDOW (parent)->hchild)
2555 : !NILP (XWINDOW (parent)->vchild))
2556 break;
2557 window = parent;
2558 }
2559
2560 sizep = &CURSIZE (window);
2561
2562 {
2563 register int maxdelta;
2564
2565 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
2566 : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
2567 : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
2568 /* This is a frame with only one window, a minibuffer-only
2569 or a minibufferless frame. */
2570 : (delta = 0));
2571
2572 if (delta > maxdelta)
2573 /* This case traps trying to make the minibuffer
2574 the full frame, or make the only window aside from the
2575 minibuffer the full frame. */
2576 delta = maxdelta;
2577 }
2578
2579 if (*sizep + delta < MINSIZE (window))
2580 {
2581 delete_window (window);
2582 return;
2583 }
2584
2585 if (delta == 0)
2586 return;
2587
2588 /* Find the total we can get from other siblings. */
2589 maximum = 0;
2590 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
2591 maximum += (*sizefun) (next) - MINSIZE (next);
2592 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
2593 maximum += (*sizefun) (prev) - MINSIZE (prev);
2594
2595 /* If we can get it all from them, do so. */
2596 if (delta < maximum)
2597 {
2598 Lisp_Object first_unaffected;
2599 Lisp_Object first_affected;
2600
2601 next = p->next;
2602 prev = p->prev;
2603 first_affected = window;
2604 /* Look at one sibling at a time,
2605 moving away from this window in both directions alternately,
2606 and take as much as we can get without deleting that sibling. */
2607 while (delta != 0)
2608 {
2609 if (delta == 0)
2610 break;
2611 if (! NILP (next))
2612 {
2613 int this_one = (*sizefun) (next) - MINSIZE (next);
2614 if (this_one > delta)
2615 this_one = delta;
2616
2617 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
2618 (*setsizefun) (window, *sizep + this_one, 0);
2619
2620 delta -= this_one;
2621 next = XWINDOW (next)->next;
2622 }
2623 if (delta == 0)
2624 break;
2625 if (! NILP (prev))
2626 {
2627 int this_one = (*sizefun) (prev) - MINSIZE (prev);
2628 if (this_one > delta)
2629 this_one = delta;
2630
2631 first_affected = prev;
2632
2633 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
2634 (*setsizefun) (window, *sizep + this_one, 0);
2635
2636 delta -= this_one;
2637 prev = XWINDOW (prev)->prev;
2638 }
2639 }
2640
2641 /* Now recalculate the edge positions of all the windows affected,
2642 based on the new sizes. */
2643 first_unaffected = next;
2644 prev = first_affected;
2645 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
2646 prev = next, next = XWINDOW (next)->next)
2647 {
2648 CURBEG (next) = CURBEG (prev) + (*sizefun) (prev);
2649 /* This does not change size of NEXT,
2650 but it propagates the new top edge to its children */
2651 (*setsizefun) (next, (*sizefun) (next), 0);
2652 }
2653 }
2654 else
2655 {
2656 register int delta1;
2657 register int opht = (*sizefun) (parent);
2658
2659 /* If trying to grow this window to or beyond size of the parent,
2660 make delta1 so big that, on shrinking back down,
2661 all the siblings end up with less than one line and are deleted. */
2662 if (opht <= *sizep + delta)
2663 delta1 = opht * opht * 2;
2664 /* Otherwise, make delta1 just right so that if we add delta1
2665 lines to this window and to the parent, and then shrink
2666 the parent back to its original size, the new proportional
2667 size of this window will increase by delta. */
2668 else
2669 delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
2670
2671 /* Add delta1 lines or columns to this window, and to the parent,
2672 keeping things consistent while not affecting siblings. */
2673 CURSIZE (parent) = opht + delta1;
2674 (*setsizefun) (window, *sizep + delta1, 0);
2675
2676 /* Squeeze out delta1 lines or columns from our parent,
2677 shriking this window and siblings proportionately.
2678 This brings parent back to correct size.
2679 Delta1 was calculated so this makes this window the desired size,
2680 taking it all out of the siblings. */
2681 (*setsizefun) (parent, opht, 0);
2682 }
2683
2684 XSETFASTINT (p->last_modified, 0);
2685 XSETFASTINT (p->last_overlay_modified, 0);
2686 }
2687 #undef MINSIZE
2688 #undef CURBEG
2689 #undef CURSIZE
2690
2691 \f
2692 /* Return number of lines of text (not counting mode line) in W. */
2693
2694 int
2695 window_internal_height (w)
2696 struct window *w;
2697 {
2698 int ht = XFASTINT (w->height);
2699
2700 if (MINI_WINDOW_P (w))
2701 return ht;
2702
2703 if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
2704 || !NILP (w->next) || !NILP (w->prev)
2705 || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
2706 return ht - 1;
2707
2708 return ht;
2709 }
2710
2711
2712 /* Return the number of columns in W.
2713 Don't count columns occupied by scroll bars or the vertical bar
2714 separating W from the sibling to its right. */
2715 int
2716 window_internal_width (w)
2717 struct window *w;
2718 {
2719 FRAME_PTR f = XFRAME (WINDOW_FRAME (w));
2720 int width = XINT (w->width);
2721
2722 /* Scroll bars occupy a few columns. */
2723 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2724 return width - FRAME_SCROLL_BAR_COLS (f);
2725
2726 /* The column of `|' characters separating side-by-side windows
2727 occupies one column only. */
2728 if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
2729 return width - 1;
2730
2731 return width;
2732 }
2733
2734
2735 /* Scroll contents of window WINDOW up N lines.
2736 If WHOLE is nonzero, it means scroll N screenfuls instead. */
2737
2738 static void
2739 window_scroll (window, n, whole, noerror)
2740 Lisp_Object window;
2741 int n;
2742 int whole;
2743 int noerror;
2744 {
2745 register struct window *w = XWINDOW (window);
2746 register int opoint = PT;
2747 register int pos;
2748 register int ht = window_internal_height (w);
2749 register Lisp_Object tem;
2750 int lose;
2751 Lisp_Object bolp, nmoved;
2752 int startpos;
2753 struct position posit;
2754 int original_vpos;
2755
2756 startpos = marker_position (w->start);
2757
2758 posit = *compute_motion (startpos, 0, 0, 0,
2759 PT, ht, 0,
2760 window_internal_width (w), XINT (w->hscroll),
2761 0, w);
2762 original_vpos = posit.vpos;
2763
2764 XSETFASTINT (tem, PT);
2765 tem = Fpos_visible_in_window_p (tem, window);
2766
2767 if (NILP (tem))
2768 {
2769 Fvertical_motion (make_number (- (ht / 2)), window);
2770 startpos = PT;
2771 }
2772
2773 SET_PT (startpos);
2774 lose = n < 0 && PT == BEGV;
2775 Fvertical_motion (make_number (n), window);
2776 pos = PT;
2777 bolp = Fbolp ();
2778 SET_PT (opoint);
2779
2780 if (lose)
2781 {
2782 if (noerror)
2783 return;
2784 else
2785 Fsignal (Qbeginning_of_buffer, Qnil);
2786 }
2787
2788 if (pos < ZV)
2789 {
2790 int this_scroll_margin = scroll_margin;
2791
2792 /* Don't use a scroll margin that is negative or too large. */
2793 if (this_scroll_margin < 0)
2794 this_scroll_margin = 0;
2795
2796 if (XINT (w->height) < 4 * scroll_margin)
2797 this_scroll_margin = XINT (w->height) / 4;
2798
2799 set_marker_restricted (w->start, make_number (pos), w->buffer);
2800 w->start_at_line_beg = bolp;
2801 w->update_mode_line = Qt;
2802 XSETFASTINT (w->last_modified, 0);
2803 XSETFASTINT (w->last_overlay_modified, 0);
2804 /* Set force_start so that redisplay_window will run
2805 the window-scroll-functions. */
2806 w->force_start = Qt;
2807
2808 if (whole && scroll_preserve_screen_position)
2809 {
2810 SET_PT (pos);
2811 Fvertical_motion (make_number (original_vpos), window);
2812 }
2813 /* If we scrolled forward, put point enough lines down
2814 that it is outside the scroll margin. */
2815 else if (n > 0)
2816 {
2817 int top_margin;
2818
2819 if (this_scroll_margin > 0)
2820 {
2821 SET_PT (pos);
2822 Fvertical_motion (make_number (this_scroll_margin), window);
2823 top_margin = PT;
2824 }
2825 else
2826 top_margin = pos;
2827
2828 if (top_margin <= opoint)
2829 SET_PT (opoint);
2830 else if (scroll_preserve_screen_position)
2831 {
2832 SET_PT (pos);
2833 Fvertical_motion (make_number (original_vpos), window);
2834 }
2835 else
2836 SET_PT (pos);
2837 }
2838 else if (n < 0)
2839 {
2840 int bottom_margin;
2841
2842 /* If we scrolled backward, put point near the end of the window
2843 but not within the scroll margin. */
2844 SET_PT (pos);
2845 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
2846 if (XFASTINT (tem) == ht - this_scroll_margin)
2847 bottom_margin = PT;
2848 else
2849 bottom_margin = PT + 1;
2850
2851 if (bottom_margin > opoint)
2852 SET_PT (opoint);
2853 else
2854 {
2855 if (scroll_preserve_screen_position)
2856 {
2857 SET_PT (pos);
2858 Fvertical_motion (make_number (original_vpos), window);
2859 }
2860 else
2861 Fvertical_motion (make_number (-1), window);
2862 }
2863 }
2864 }
2865 else
2866 {
2867 if (noerror)
2868 return;
2869 else
2870 Fsignal (Qend_of_buffer, Qnil);
2871 }
2872 }
2873 \f
2874 /* This is the guts of Fscroll_up and Fscroll_down. */
2875
2876 static void
2877 scroll_command (n, direction)
2878 register Lisp_Object n;
2879 int direction;
2880 {
2881 register int defalt;
2882 int count = specpdl_ptr - specpdl;
2883
2884 /* If selected window's buffer isn't current, make it current for the moment.
2885 But don't screw up if window_scroll gets an error. */
2886 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
2887 {
2888 record_unwind_protect (save_excursion_restore, save_excursion_save ());
2889 Fset_buffer (XWINDOW (selected_window)->buffer);
2890 }
2891
2892 defalt = (window_internal_height (XWINDOW (selected_window))
2893 - next_screen_context_lines);
2894 defalt = direction * (defalt < 1 ? 1 : defalt);
2895
2896 if (NILP (n))
2897 window_scroll (selected_window, defalt, 1, 0);
2898 else if (EQ (n, Qminus))
2899 window_scroll (selected_window, - defalt, 1, 0);
2900 else
2901 {
2902 n = Fprefix_numeric_value (n);
2903 window_scroll (selected_window, XINT (n) * direction, 0, 0);
2904 }
2905
2906 unbind_to (count, Qnil);
2907 }
2908
2909 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
2910 "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
2911 A near full screen is `next-screen-context-lines' less than a full screen.\n\
2912 Negative ARG means scroll downward.\n\
2913 When calling from a program, supply a number as argument or nil.")
2914 (arg)
2915 Lisp_Object arg;
2916 {
2917 scroll_command (arg, 1);
2918 return Qnil;
2919 }
2920
2921 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
2922 "Scroll text of current window downward ARG lines; or near full screen if no ARG.\n\
2923 A near full screen is `next-screen-context-lines' less than a full screen.\n\
2924 Negative ARG means scroll upward.\n\
2925 When calling from a program, supply a number as argument or nil.")
2926 (arg)
2927 Lisp_Object arg;
2928 {
2929 scroll_command (arg, -1);
2930 return Qnil;
2931 }
2932 \f
2933 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
2934 "Return the other window for \"other window scroll\" commands.\n\
2935 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
2936 specifies the window.\n\
2937 If `other-window-scroll-buffer' is non-nil, a window\n\
2938 showing that buffer is used.")
2939 ()
2940 {
2941 Lisp_Object window;
2942
2943 if (MINI_WINDOW_P (XWINDOW (selected_window))
2944 && !NILP (Vminibuf_scroll_window))
2945 window = Vminibuf_scroll_window;
2946 /* If buffer is specified, scroll that buffer. */
2947 else if (!NILP (Vother_window_scroll_buffer))
2948 {
2949 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
2950 if (NILP (window))
2951 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt);
2952 }
2953 else
2954 {
2955 /* Nothing specified; look for a neighboring window on the same
2956 frame. */
2957 window = Fnext_window (selected_window, Qnil, Qnil);
2958
2959 if (EQ (window, selected_window))
2960 /* That didn't get us anywhere; look for a window on another
2961 visible frame. */
2962 do
2963 window = Fnext_window (window, Qnil, Qt);
2964 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
2965 && ! EQ (window, selected_window));
2966 }
2967
2968 CHECK_LIVE_WINDOW (window, 0);
2969
2970 if (EQ (window, selected_window))
2971 error ("There is no other window");
2972
2973 return window;
2974 }
2975
2976 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
2977 "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
2978 The next window is the one below the current one; or the one at the top\n\
2979 if the current one is at the bottom. Negative ARG means scroll downward.\n\
2980 When calling from a program, supply a number as argument or nil.\n\
2981 \n\
2982 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
2983 specifies the window to scroll.\n\
2984 If `other-window-scroll-buffer' is non-nil, scroll the window\n\
2985 showing that buffer, popping the buffer up if necessary.")
2986 (arg)
2987 register Lisp_Object arg;
2988 {
2989 register Lisp_Object window;
2990 register int defalt;
2991 register struct window *w;
2992 register int count = specpdl_ptr - specpdl;
2993
2994 window = Fother_window_for_scrolling ();
2995
2996 w = XWINDOW (window);
2997 defalt = window_internal_height (w) - next_screen_context_lines;
2998 if (defalt < 1) defalt = 1;
2999
3000 /* Don't screw up if window_scroll gets an error. */
3001 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3002
3003 Fset_buffer (w->buffer);
3004 SET_PT (marker_position (w->pointm));
3005
3006 if (NILP (arg))
3007 window_scroll (window, defalt, 1, 1);
3008 else if (EQ (arg, Qminus))
3009 window_scroll (window, -defalt, 1, 1);
3010 else
3011 {
3012 if (CONSP (arg))
3013 arg = Fcar (arg);
3014 CHECK_NUMBER (arg, 0);
3015 window_scroll (window, XINT (arg), 0, 1);
3016 }
3017
3018 Fset_marker (w->pointm, make_number (PT), Qnil);
3019 unbind_to (count, Qnil);
3020
3021 return Qnil;
3022 }
3023 \f
3024 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
3025 "Scroll selected window display ARG columns left.\n\
3026 Default for ARG is window width minus 2.")
3027 (arg)
3028 register Lisp_Object arg;
3029 {
3030
3031 if (NILP (arg))
3032 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3033 else
3034 arg = Fprefix_numeric_value (arg);
3035
3036 return
3037 Fset_window_hscroll (selected_window,
3038 make_number (XINT (XWINDOW (selected_window)->hscroll)
3039 + XINT (arg)));
3040 }
3041
3042 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
3043 "Scroll selected window display ARG columns right.\n\
3044 Default for ARG is window width minus 2.")
3045 (arg)
3046 register Lisp_Object arg;
3047 {
3048 if (NILP (arg))
3049 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3050 else
3051 arg = Fprefix_numeric_value (arg);
3052
3053 return
3054 Fset_window_hscroll (selected_window,
3055 make_number (XINT (XWINDOW (selected_window)->hscroll)
3056 - XINT (arg)));
3057 }
3058
3059 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
3060 "Center point in window and redisplay frame. With ARG, put point on line ARG.\n\
3061 The desired position of point is always relative to the current window.\n\
3062 Just C-u as prefix means put point in the center of the window.\n\
3063 If ARG is omitted or nil, erases the entire frame and then\n\
3064 redraws with point in the center of the current window.")
3065 (arg)
3066 register Lisp_Object arg;
3067 {
3068 register struct window *w = XWINDOW (selected_window);
3069 register int ht = window_internal_height (w);
3070 struct position pos;
3071
3072 if (NILP (arg))
3073 {
3074 extern int frame_garbaged;
3075
3076 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
3077 XSETFASTINT (arg, ht / 2);
3078 }
3079 else if (CONSP (arg)) /* Just C-u. */
3080 {
3081 XSETFASTINT (arg, ht / 2);
3082 }
3083 else
3084 {
3085 arg = Fprefix_numeric_value (arg);
3086 CHECK_NUMBER (arg, 0);
3087 }
3088
3089 if (XINT (arg) < 0)
3090 XSETINT (arg, XINT (arg) + ht);
3091
3092 pos = *vmotion (PT, - XINT (arg), w);
3093
3094 Fset_marker (w->start, make_number (pos.bufpos), w->buffer);
3095 w->start_at_line_beg = ((pos.bufpos == BEGV
3096 || FETCH_BYTE (pos.bufpos - 1) == '\n')
3097 ? Qt : Qnil);
3098 w->force_start = Qt;
3099
3100 return Qnil;
3101 }
3102 \f
3103 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
3104 1, 1, "P",
3105 "Position point relative to window.\n\
3106 With no argument, position point at center of window.\n\
3107 An argument specifies frame line; zero means top of window,\n\
3108 negative means relative to bottom of window.")
3109 (arg)
3110 register Lisp_Object arg;
3111 {
3112 register struct window *w = XWINDOW (selected_window);
3113 register int height = window_internal_height (w);
3114 register int start;
3115 Lisp_Object window;
3116
3117 if (NILP (arg))
3118 XSETFASTINT (arg, height / 2);
3119 else
3120 {
3121 arg = Fprefix_numeric_value (arg);
3122 if (XINT (arg) < 0)
3123 XSETINT (arg, XINT (arg) + height);
3124 }
3125
3126 start = marker_position (w->start);
3127 XSETWINDOW (window, w);
3128 if (start < BEGV || start > ZV)
3129 {
3130 Fvertical_motion (make_number (- (height / 2)), window);
3131 Fset_marker (w->start, make_number (PT), w->buffer);
3132 w->start_at_line_beg = Fbolp ();
3133 w->force_start = Qt;
3134 }
3135 else
3136 SET_PT (start);
3137
3138 return Fvertical_motion (arg, window);
3139 }
3140 \f
3141 struct save_window_data
3142 {
3143 EMACS_INT size_from_Lisp_Vector_struct;
3144 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3145 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
3146 Lisp_Object selected_frame;
3147 Lisp_Object current_window;
3148 Lisp_Object current_buffer;
3149 Lisp_Object minibuf_scroll_window;
3150 Lisp_Object root_window;
3151 Lisp_Object focus_frame;
3152 /* Record the values of window-min-width and window-min-height
3153 so that window sizes remain consistent with them. */
3154 Lisp_Object min_width, min_height;
3155 /* A vector, interpreted as a struct saved_window */
3156 Lisp_Object saved_windows;
3157 };
3158
3159 /* This is saved as a Lisp_Vector */
3160 struct saved_window
3161 {
3162 /* these first two must agree with struct Lisp_Vector in lisp.h */
3163 EMACS_INT size_from_Lisp_Vector_struct;
3164 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3165
3166 Lisp_Object window;
3167 Lisp_Object buffer, start, pointm, mark;
3168 Lisp_Object left, top, width, height, hscroll;
3169 Lisp_Object parent, prev;
3170 Lisp_Object start_at_line_beg;
3171 Lisp_Object display_table;
3172 };
3173 #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
3174
3175 #define SAVED_WINDOW_N(swv,n) \
3176 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
3177
3178 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
3179 "T if OBJECT is a window-configuration object.")
3180 (object)
3181 Lisp_Object object;
3182 {
3183 if (WINDOW_CONFIGURATIONP (object))
3184 return Qt;
3185 return Qnil;
3186 }
3187
3188
3189 DEFUN ("set-window-configuration", Fset_window_configuration,
3190 Sset_window_configuration, 1, 1, 0,
3191 "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
3192 CONFIGURATION must be a value previously returned\n\
3193 by `current-window-configuration' (which see).")
3194 (configuration)
3195 Lisp_Object configuration;
3196 {
3197 register struct save_window_data *data;
3198 struct Lisp_Vector *saved_windows;
3199 Lisp_Object new_current_buffer;
3200 Lisp_Object frame;
3201 FRAME_PTR f;
3202
3203 while (!WINDOW_CONFIGURATIONP (configuration))
3204 {
3205 configuration = wrong_type_argument (intern ("window-configuration-p"),
3206 configuration);
3207 }
3208
3209 data = (struct save_window_data *) XVECTOR (configuration);
3210 saved_windows = XVECTOR (data->saved_windows);
3211
3212 new_current_buffer = data->current_buffer;
3213 if (NILP (XBUFFER (new_current_buffer)->name))
3214 new_current_buffer = Qnil;
3215
3216 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3217 f = XFRAME (frame);
3218
3219 /* If f is a dead frame, don't bother rebuilding its window tree.
3220 However, there is other stuff we should still try to do below. */
3221 if (FRAME_LIVE_P (f))
3222 {
3223 register struct window *w;
3224 register struct saved_window *p;
3225 int k;
3226
3227 /* If the frame has been resized since this window configuration was
3228 made, we change the frame to the size specified in the
3229 configuration, restore the configuration, and then resize it
3230 back. We keep track of the prevailing height in these variables. */
3231 int previous_frame_height = FRAME_HEIGHT (f);
3232 int previous_frame_width = FRAME_WIDTH (f);
3233 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
3234
3235 if (XFASTINT (data->frame_height) != previous_frame_height
3236 || XFASTINT (data->frame_width) != previous_frame_width)
3237 change_frame_size (f, data->frame_height, data->frame_width, 0, 0);
3238 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3239 if (XFASTINT (data->frame_menu_bar_lines)
3240 != previous_frame_menu_bar_lines)
3241 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, 0);
3242 #endif
3243
3244 windows_or_buffers_changed++;
3245 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3246
3247 /* Temporarily avoid any problems with windows that are smaller
3248 than they are supposed to be. */
3249 window_min_height = 1;
3250 window_min_width = 1;
3251
3252 /* Kludge Alert!
3253 Mark all windows now on frame as "deleted".
3254 Restoring the new configuration "undeletes" any that are in it.
3255
3256 Save their current buffers in their height fields, since we may
3257 need it later, if a buffer saved in the configuration is now
3258 dead. */
3259 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3260
3261 for (k = 0; k < saved_windows->size; k++)
3262 {
3263 p = SAVED_WINDOW_N (saved_windows, k);
3264 w = XWINDOW (p->window);
3265 w->next = Qnil;
3266
3267 if (!NILP (p->parent))
3268 w->parent = SAVED_WINDOW_N (saved_windows,
3269 XFASTINT (p->parent))->window;
3270 else
3271 w->parent = Qnil;
3272
3273 if (!NILP (p->prev))
3274 {
3275 w->prev = SAVED_WINDOW_N (saved_windows,
3276 XFASTINT (p->prev))->window;
3277 XWINDOW (w->prev)->next = p->window;
3278 }
3279 else
3280 {
3281 w->prev = Qnil;
3282 if (!NILP (w->parent))
3283 {
3284 if (EQ (p->width, XWINDOW (w->parent)->width))
3285 {
3286 XWINDOW (w->parent)->vchild = p->window;
3287 XWINDOW (w->parent)->hchild = Qnil;
3288 }
3289 else
3290 {
3291 XWINDOW (w->parent)->hchild = p->window;
3292 XWINDOW (w->parent)->vchild = Qnil;
3293 }
3294 }
3295 }
3296
3297 /* If we squirreled away the buffer in the window's height,
3298 restore it now. */
3299 if (BUFFERP (w->height))
3300 w->buffer = w->height;
3301 w->left = p->left;
3302 w->top = p->top;
3303 w->width = p->width;
3304 w->height = p->height;
3305 w->hscroll = p->hscroll;
3306 w->display_table = p->display_table;
3307 XSETFASTINT (w->last_modified, 0);
3308 XSETFASTINT (w->last_overlay_modified, 0);
3309
3310 /* Reinstall the saved buffer and pointers into it. */
3311 if (NILP (p->buffer))
3312 w->buffer = p->buffer;
3313 else
3314 {
3315 if (!NILP (XBUFFER (p->buffer)->name))
3316 /* If saved buffer is alive, install it. */
3317 {
3318 w->buffer = p->buffer;
3319 w->start_at_line_beg = p->start_at_line_beg;
3320 set_marker_restricted (w->start,
3321 Fmarker_position (p->start),
3322 w->buffer);
3323 set_marker_restricted (w->pointm,
3324 Fmarker_position (p->pointm),
3325 w->buffer);
3326 Fset_marker (XBUFFER (w->buffer)->mark,
3327 Fmarker_position (p->mark), w->buffer);
3328
3329 /* As documented in Fcurrent_window_configuration, don't
3330 save the location of point in the buffer which was current
3331 when the window configuration was recorded. */
3332 if (!EQ (p->buffer, new_current_buffer)
3333 && XBUFFER (p->buffer) == current_buffer)
3334 Fgoto_char (w->pointm);
3335 }
3336 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
3337 /* Else unless window has a live buffer, get one. */
3338 {
3339 w->buffer = Fcdr (Fcar (Vbuffer_alist));
3340 /* This will set the markers to beginning of visible
3341 range. */
3342 set_marker_restricted (w->start, make_number (0), w->buffer);
3343 set_marker_restricted (w->pointm, make_number (0),w->buffer);
3344 w->start_at_line_beg = Qt;
3345 }
3346 else
3347 /* Keeping window's old buffer; make sure the markers
3348 are real. */
3349 {
3350 /* Set window markers at start of visible range. */
3351 if (XMARKER (w->start)->buffer == 0)
3352 set_marker_restricted (w->start, make_number (0),
3353 w->buffer);
3354 if (XMARKER (w->pointm)->buffer == 0)
3355 set_marker_restricted (w->pointm,
3356 (make_number
3357 (BUF_PT (XBUFFER (w->buffer)))),
3358 w->buffer);
3359 w->start_at_line_beg = Qt;
3360 }
3361 }
3362 }
3363
3364 FRAME_ROOT_WINDOW (f) = data->root_window;
3365 Fselect_window (data->current_window);
3366
3367 if (NILP (data->focus_frame)
3368 || (FRAMEP (data->focus_frame)
3369 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
3370 Fredirect_frame_focus (frame, data->focus_frame);
3371
3372 #if 0 /* I don't understand why this is needed, and it causes problems
3373 when the frame's old selected window has been deleted. */
3374 if (f != selected_frame && FRAME_WINDOW_P (f))
3375 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
3376 Qnil, 0);
3377 #endif
3378
3379 /* Set the screen height to the value it had before this function. */
3380 if (previous_frame_height != FRAME_HEIGHT (f)
3381 || previous_frame_width != FRAME_WIDTH (f))
3382 change_frame_size (f, previous_frame_height, previous_frame_width,
3383 0, 0);
3384 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3385 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
3386 x_set_menu_bar_lines (f, previous_frame_menu_bar_lines, 0);
3387 #endif
3388 }
3389
3390 /* Restore the minimum heights recorded in the configuration. */
3391 window_min_height = XINT (data->min_height);
3392 window_min_width = XINT (data->min_width);
3393
3394 /* Fselect_window will have made f the selected frame, so we
3395 reselect the proper frame here. Fhandle_switch_frame will change the
3396 selected window too, but that doesn't make the call to
3397 Fselect_window above totally superfluous; it still sets f's
3398 selected window. */
3399 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
3400 do_switch_frame (data->selected_frame, Qnil, 0);
3401
3402 if (!NILP (new_current_buffer))
3403 Fset_buffer (new_current_buffer);
3404
3405 Vminibuf_scroll_window = data->minibuf_scroll_window;
3406
3407 if (! NILP (Vwindow_configuration_change_hook)
3408 && ! NILP (Vrun_hooks))
3409 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3410
3411 return (Qnil);
3412 }
3413
3414 /* Mark all windows now on frame as deleted
3415 by setting their buffers to nil. */
3416
3417 void
3418 delete_all_subwindows (w)
3419 register struct window *w;
3420 {
3421 if (!NILP (w->next))
3422 delete_all_subwindows (XWINDOW (w->next));
3423 if (!NILP (w->vchild))
3424 delete_all_subwindows (XWINDOW (w->vchild));
3425 if (!NILP (w->hchild))
3426 delete_all_subwindows (XWINDOW (w->hchild));
3427
3428 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
3429
3430 if (!NILP (w->buffer))
3431 unshow_buffer (w);
3432
3433 /* We set all three of these fields to nil, to make sure that we can
3434 distinguish this dead window from any live window. Live leaf
3435 windows will have buffer set, and combination windows will have
3436 vchild or hchild set. */
3437 w->buffer = Qnil;
3438 w->vchild = Qnil;
3439 w->hchild = Qnil;
3440 }
3441 \f
3442 static int
3443 count_windows (window)
3444 register struct window *window;
3445 {
3446 register int count = 1;
3447 if (!NILP (window->next))
3448 count += count_windows (XWINDOW (window->next));
3449 if (!NILP (window->vchild))
3450 count += count_windows (XWINDOW (window->vchild));
3451 if (!NILP (window->hchild))
3452 count += count_windows (XWINDOW (window->hchild));
3453 return count;
3454 }
3455
3456 static int
3457 save_window_save (window, vector, i)
3458 Lisp_Object window;
3459 struct Lisp_Vector *vector;
3460 int i;
3461 {
3462 register struct saved_window *p;
3463 register struct window *w;
3464 register Lisp_Object tem;
3465
3466 for (;!NILP (window); window = w->next)
3467 {
3468 p = SAVED_WINDOW_N (vector, i);
3469 w = XWINDOW (window);
3470
3471 XSETFASTINT (w->temslot, i++);
3472 p->window = window;
3473 p->buffer = w->buffer;
3474 p->left = w->left;
3475 p->top = w->top;
3476 p->width = w->width;
3477 p->height = w->height;
3478 p->hscroll = w->hscroll;
3479 p->display_table = w->display_table;
3480 if (!NILP (w->buffer))
3481 {
3482 /* Save w's value of point in the window configuration.
3483 If w is the selected window, then get the value of point
3484 from the buffer; pointm is garbage in the selected window. */
3485 if (EQ (window, selected_window))
3486 {
3487 p->pointm = Fmake_marker ();
3488 Fset_marker (p->pointm, BUF_PT (XBUFFER (w->buffer)),
3489 w->buffer);
3490 }
3491 else
3492 p->pointm = Fcopy_marker (w->pointm, Qnil);
3493
3494 p->start = Fcopy_marker (w->start, Qnil);
3495 p->start_at_line_beg = w->start_at_line_beg;
3496
3497 tem = XBUFFER (w->buffer)->mark;
3498 p->mark = Fcopy_marker (tem, Qnil);
3499 }
3500 else
3501 {
3502 p->pointm = Qnil;
3503 p->start = Qnil;
3504 p->mark = Qnil;
3505 p->start_at_line_beg = Qnil;
3506 }
3507
3508 if (NILP (w->parent))
3509 p->parent = Qnil;
3510 else
3511 p->parent = XWINDOW (w->parent)->temslot;
3512
3513 if (NILP (w->prev))
3514 p->prev = Qnil;
3515 else
3516 p->prev = XWINDOW (w->prev)->temslot;
3517
3518 if (!NILP (w->vchild))
3519 i = save_window_save (w->vchild, vector, i);
3520 if (!NILP (w->hchild))
3521 i = save_window_save (w->hchild, vector, i);
3522 }
3523
3524 return i;
3525 }
3526
3527 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
3528 Scurrent_window_configuration, 0, 1, 0,
3529 "Return an object representing the current window configuration of FRAME.\n\
3530 If FRAME is nil or omitted, use the selected frame.\n\
3531 This describes the number of windows, their sizes and current buffers,\n\
3532 and for each displayed buffer, where display starts, and the positions of\n\
3533 point and mark. An exception is made for point in the current buffer:\n\
3534 its value is -not- saved.\n\
3535 This also records the currently selected frame, and FRAME's focus\n\
3536 redirection (see `redirect-frame-focus').")
3537 (frame)
3538 Lisp_Object frame;
3539 {
3540 register Lisp_Object tem;
3541 register int n_windows;
3542 register struct save_window_data *data;
3543 register struct Lisp_Vector *vec;
3544 register int i;
3545 FRAME_PTR f;
3546
3547 if (NILP (frame))
3548 f = selected_frame;
3549 else
3550 {
3551 CHECK_LIVE_FRAME (frame, 0);
3552 f = XFRAME (frame);
3553 }
3554
3555 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3556 vec = allocate_vectorlike (VECSIZE (struct save_window_data));
3557 for (i = 0; i < VECSIZE (struct save_window_data); i++)
3558 vec->contents[i] = Qnil;
3559 vec->size = VECSIZE (struct save_window_data);
3560 data = (struct save_window_data *)vec;
3561
3562 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
3563 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
3564 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
3565 XSETFRAME (data->selected_frame, selected_frame);
3566 data->current_window = FRAME_SELECTED_WINDOW (f);
3567 XSETBUFFER (data->current_buffer, current_buffer);
3568 data->minibuf_scroll_window = Vminibuf_scroll_window;
3569 data->root_window = FRAME_ROOT_WINDOW (f);
3570 data->focus_frame = FRAME_FOCUS_FRAME (f);
3571 XSETINT (data->min_height, window_min_height);
3572 XSETINT (data->min_width, window_min_width);
3573 tem = Fmake_vector (make_number (n_windows), Qnil);
3574 data->saved_windows = tem;
3575 for (i = 0; i < n_windows; i++)
3576 XVECTOR (tem)->contents[i]
3577 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
3578 save_window_save (FRAME_ROOT_WINDOW (f),
3579 XVECTOR (tem), 0);
3580 XSETWINDOW_CONFIGURATION (tem, data);
3581 return (tem);
3582 }
3583
3584 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
3585 0, UNEVALLED, 0,
3586 "Execute body, preserving window sizes and contents.\n\
3587 Restore which buffer appears in which window, where display starts,\n\
3588 and the value of point and mark for each window.\n\
3589 Also restore which buffer is current.\n\
3590 But do not preserve point in the current buffer.\n\
3591 Does not restore the value of point in current buffer.")
3592 (args)
3593 Lisp_Object args;
3594 {
3595 register Lisp_Object val;
3596 register int count = specpdl_ptr - specpdl;
3597
3598 record_unwind_protect (Fset_window_configuration,
3599 Fcurrent_window_configuration (Qnil));
3600 val = Fprogn (args);
3601 return unbind_to (count, val);
3602 }
3603 \f
3604 init_window_once ()
3605 {
3606 selected_frame = make_terminal_frame ();
3607 XSETFRAME (Vterminal_frame, selected_frame);
3608 minibuf_window = selected_frame->minibuffer_window;
3609 selected_window = selected_frame->selected_window;
3610 last_nonminibuf_frame = selected_frame;
3611
3612 window_initialized = 1;
3613 }
3614
3615 syms_of_window ()
3616 {
3617 staticpro (&Qwindow_configuration_change_hook);
3618 Qwindow_configuration_change_hook
3619 = intern ("window-configuration-change-hook");
3620
3621 Qwindowp = intern ("windowp");
3622 staticpro (&Qwindowp);
3623
3624 Qwindow_live_p = intern ("window-live-p");
3625 staticpro (&Qwindow_live_p);
3626
3627 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
3628 staticpro (&Qtemp_buffer_show_hook);
3629
3630 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
3631 "Non-nil means call as function to display a help buffer.\n\
3632 The function is called with one argument, the buffer to be displayed.\n\
3633 Used by `with-output-to-temp-buffer'.\n\
3634 If this function is used, then it must do the entire job of showing\n\
3635 the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
3636 Vtemp_buffer_show_function = Qnil;
3637
3638 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
3639 "If non-nil, function to call to handle `display-buffer'.\n\
3640 It will receive two args, the buffer and a flag which if non-nil means\n\
3641 that the currently selected window is not acceptable.\n\
3642 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
3643 work using this function.");
3644 Vdisplay_buffer_function = Qnil;
3645
3646 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
3647 "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
3648 Vminibuf_scroll_window = Qnil;
3649
3650 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
3651 "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
3652 Vother_window_scroll_buffer = Qnil;
3653
3654 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
3655 "*Non-nil means `display-buffer' should make a separate frame.");
3656 pop_up_frames = 0;
3657
3658 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
3659 "Function to call to handle automatic new frame creation.\n\
3660 It is called with no arguments and should return a newly created frame.\n\
3661 \n\
3662 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
3663 where `pop-up-frame-alist' would hold the default frame parameters.");
3664 Vpop_up_frame_function = Qnil;
3665
3666 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
3667 "*List of buffer names that should have their own special frames.\n\
3668 Displaying a buffer whose name is in this list makes a special frame for it\n\
3669 using `special-display-function'. See also `special-display-regexps'.\n\
3670 \n\
3671 An element of the list can be a list instead of just a string.\n\
3672 There are two ways to use a list as an element:\n\
3673 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)\n\
3674 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3675 In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
3676 followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
3677 All this is done by the function found in `special-display-function'.");
3678 Vspecial_display_buffer_names = Qnil;
3679
3680 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
3681 "*List of regexps saying which buffers should have their own special frames.\n\
3682 If a buffer name matches one of these regexps, it gets its own frame.\n\
3683 Displaying a buffer whose name is in this list makes a special frame for it\n\
3684 using `special-display-function'.\n\
3685 \n\
3686 An element of the list can be a list instead of just a string.\n\
3687 There are two ways to use a list as an element:\n\
3688 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)\n\
3689 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
3690 In the latter case, FUNCTION is called with the buffer as first argument,\n\
3691 followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
3692 All this is done by the function found in `special-display-function'.");
3693 Vspecial_display_regexps = Qnil;
3694
3695 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
3696 "Function to call to make a new frame for a special buffer.\n\
3697 It is called with two arguments, the buffer and optional buffer specific\n\
3698 data, and should return a window displaying that buffer.\n\
3699 The default value makes a separate frame for the buffer,\n\
3700 using `special-display-frame-alist' to specify the frame parameters.\n\
3701 \n\
3702 A buffer is special if its is listed in `special-display-buffer-names'\n\
3703 or matches a regexp in `special-display-regexps'.");
3704 Vspecial_display_function = Qnil;
3705
3706 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
3707 "*List of buffer names that should appear in the selected window.\n\
3708 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
3709 switches to it in the selected window, rather than making it appear\n\
3710 in some other window.\n\
3711 \n\
3712 An element of the list can be a cons cell instead of just a string.\n\
3713 Then the car must be a string, which specifies the buffer name.\n\
3714 This is for compatibility with `special-display-buffer-names';\n\
3715 the cdr of the cons cell is ignored.\n\
3716 \n\
3717 See also `same-window-regexps'.");
3718 Vsame_window_buffer_names = Qnil;
3719
3720 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
3721 "*List of regexps saying which buffers should appear in the selected window.\n\
3722 If a buffer name matches one of these regexps, then displaying it\n\
3723 using `display-buffer' or `pop-to-buffer' switches to it\n\
3724 in the selected window, rather than making it appear in some other window.\n\
3725 \n\
3726 An element of the list can be a cons cell instead of just a string.\n\
3727 Then the car must be a string, which specifies the buffer name.\n\
3728 This is for compatibility with `special-display-buffer-names';\n\
3729 the cdr of the cons cell is ignored.\n\
3730 \n\
3731 See also `same-window-buffer-names'.");
3732 Vsame_window_regexps = Qnil;
3733
3734 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
3735 "*Non-nil means display-buffer should make new windows.");
3736 pop_up_windows = 1;
3737
3738 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
3739 "*Number of lines of continuity when scrolling by screenfuls.");
3740 next_screen_context_lines = 2;
3741
3742 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
3743 "*display-buffer would prefer to split the largest window if this large.\n\
3744 If there is only one window, it is split regardless of this value.");
3745 split_height_threshold = 500;
3746
3747 DEFVAR_INT ("window-min-height", &window_min_height,
3748 "*Delete any window less than this tall (including its mode line).");
3749 window_min_height = 4;
3750
3751 DEFVAR_INT ("window-min-width", &window_min_width,
3752 "*Delete any window less than this wide.");
3753 window_min_width = 10;
3754
3755 DEFVAR_BOOL ("scroll-preserve-screen-position",
3756 &scroll_preserve_screen_position,
3757 "*Nonzero means scroll commands move point to keep its screen line unchanged.");
3758 scroll_preserve_screen_position = 0;
3759
3760 DEFVAR_LISP ("window-configuration-change-hook",
3761 &Vwindow_configuration_change_hook,
3762 "Functions to call when window configuration changes.\n\
3763 The selected frae is the one whose configuration has changed.");
3764 Vwindow_configuration_change_hook = Qnil;
3765
3766 defsubr (&Sselected_window);
3767 defsubr (&Sminibuffer_window);
3768 defsubr (&Swindow_minibuffer_p);
3769 defsubr (&Swindowp);
3770 defsubr (&Swindow_live_p);
3771 defsubr (&Spos_visible_in_window_p);
3772 defsubr (&Swindow_buffer);
3773 defsubr (&Swindow_height);
3774 defsubr (&Swindow_width);
3775 defsubr (&Swindow_hscroll);
3776 defsubr (&Sset_window_hscroll);
3777 defsubr (&Swindow_redisplay_end_trigger);
3778 defsubr (&Sset_window_redisplay_end_trigger);
3779 defsubr (&Swindow_edges);
3780 defsubr (&Scoordinates_in_window_p);
3781 defsubr (&Swindow_at);
3782 defsubr (&Swindow_point);
3783 defsubr (&Swindow_start);
3784 defsubr (&Swindow_end);
3785 defsubr (&Sset_window_point);
3786 defsubr (&Sset_window_start);
3787 defsubr (&Swindow_dedicated_p);
3788 defsubr (&Sset_window_dedicated_p);
3789 defsubr (&Swindow_display_table);
3790 defsubr (&Sset_window_display_table);
3791 defsubr (&Snext_window);
3792 defsubr (&Sprevious_window);
3793 defsubr (&Sother_window);
3794 defsubr (&Sget_lru_window);
3795 defsubr (&Sget_largest_window);
3796 defsubr (&Sget_buffer_window);
3797 defsubr (&Sdelete_other_windows);
3798 defsubr (&Sdelete_windows_on);
3799 defsubr (&Sreplace_buffer_in_windows);
3800 defsubr (&Sdelete_window);
3801 defsubr (&Sset_window_buffer);
3802 defsubr (&Sselect_window);
3803 defsubr (&Sspecial_display_p);
3804 defsubr (&Ssame_window_p);
3805 defsubr (&Sdisplay_buffer);
3806 defsubr (&Ssplit_window);
3807 defsubr (&Senlarge_window);
3808 defsubr (&Sshrink_window);
3809 defsubr (&Sscroll_up);
3810 defsubr (&Sscroll_down);
3811 defsubr (&Sscroll_left);
3812 defsubr (&Sscroll_right);
3813 defsubr (&Sother_window_for_scrolling);
3814 defsubr (&Sscroll_other_window);
3815 defsubr (&Srecenter);
3816 defsubr (&Smove_to_window_line);
3817 defsubr (&Swindow_configuration_p);
3818 defsubr (&Sset_window_configuration);
3819 defsubr (&Scurrent_window_configuration);
3820 defsubr (&Ssave_window_excursion);
3821 }
3822
3823 keys_of_window ()
3824 {
3825 initial_define_key (control_x_map, '1', "delete-other-windows");
3826 initial_define_key (control_x_map, '2', "split-window");
3827 initial_define_key (control_x_map, '0', "delete-window");
3828 initial_define_key (control_x_map, 'o', "other-window");
3829 initial_define_key (control_x_map, '^', "enlarge-window");
3830 initial_define_key (control_x_map, '<', "scroll-left");
3831 initial_define_key (control_x_map, '>', "scroll-right");
3832
3833 initial_define_key (global_map, Ctl ('V'), "scroll-up");
3834 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
3835 initial_define_key (meta_map, 'v', "scroll-down");
3836
3837 initial_define_key (global_map, Ctl('L'), "recenter");
3838 initial_define_key (meta_map, 'r', "move-to-window-line");
3839 }