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