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