(display-buffer): Don't get confused
[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,97,1998 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 #include "dispextern.h"
33 #include "blockinput.h"
34 #include "intervals.h"
35
36 #ifdef HAVE_X_WINDOWS
37 #include "xterm.h"
38 #endif /* HAVE_X_WINDOWS */
39
40 #ifndef max
41 #define max(a, b) ((a) < (b) ? (b) : (a))
42 #endif
43
44
45 Lisp_Object Qwindowp, Qwindow_live_p, Qwindow_configuration_p;
46
47 static struct window *decode_window P_ ((Lisp_Object));
48 static Lisp_Object select_window_1 P_ ((Lisp_Object, int));
49 static int count_windows P_ ((struct window *));
50 static int get_leaf_windows P_ ((struct window *, struct window **, int));
51 static void window_scroll P_ ((Lisp_Object, int, int, int));
52 static void window_scroll_pixel_based P_ ((Lisp_Object, int, int, int));
53 static void window_scroll_line_based P_ ((Lisp_Object, int, int, int));
54
55
56
57 /* This is the window in which the terminal's cursor should
58 be left when nothing is being done with it. This must
59 always be a leaf window, and its buffer is selected by
60 the top level editing loop at the end of each command.
61
62 This value is always the same as
63 FRAME_SELECTED_WINDOW (selected_frame). */
64
65 Lisp_Object selected_window;
66
67 /* The mini-buffer window of the selected frame.
68 Note that you cannot test for mini-bufferness of an arbitrary window
69 by comparing against this; but you can test for mini-bufferness of
70 the selected window. */
71
72 Lisp_Object minibuf_window;
73
74 /* Non-nil means it is the window for C-M-v to scroll
75 when the mini-buffer is selected. */
76
77 Lisp_Object Vminibuf_scroll_window;
78
79 /* Non-nil means this is the buffer whose window C-M-v should scroll. */
80
81 Lisp_Object Vother_window_scroll_buffer;
82
83 /* Non-nil means it's function to call to display temp buffers. */
84
85 Lisp_Object Vtemp_buffer_show_function;
86
87 /* If a window gets smaller than either of these, it is removed. */
88
89 int window_min_height;
90 int window_min_width;
91
92 /* Nonzero implies Fdisplay_buffer should create windows. */
93
94 int pop_up_windows;
95
96 /* Nonzero implies make new frames for Fdisplay_buffer. */
97
98 int pop_up_frames;
99
100 /* Non-nil means use this function instead of default */
101
102 Lisp_Object Vpop_up_frame_function;
103
104 /* Function to call to handle Fdisplay_buffer. */
105
106 Lisp_Object Vdisplay_buffer_function;
107
108 /* List of buffer *names* for buffers that should have their own frames. */
109
110 Lisp_Object Vspecial_display_buffer_names;
111
112 /* List of regexps for buffer names that should have their own frames. */
113
114 Lisp_Object Vspecial_display_regexps;
115
116 /* Function to pop up a special frame. */
117
118 Lisp_Object Vspecial_display_function;
119
120 /* List of buffer *names* for buffers to appear in selected window. */
121
122 Lisp_Object Vsame_window_buffer_names;
123
124 /* List of regexps for buffer names to appear in selected window. */
125
126 Lisp_Object Vsame_window_regexps;
127
128 /* Hook run at end of temp_output_buffer_show. */
129
130 Lisp_Object Qtemp_buffer_show_hook;
131
132 /* Fdisplay_buffer always splits the largest window
133 if that window is more than this high. */
134
135 int split_height_threshold;
136
137 /* Number of lines of continuity in scrolling by screenfuls. */
138
139 int next_screen_context_lines;
140
141 /* Incremented for each window created. */
142
143 static int sequence_number;
144
145 /* Nonzero after init_window_once has finished. */
146
147 static int window_initialized;
148
149 /* Hook to run when window config changes. */
150
151 Lisp_Object Qwindow_configuration_change_hook;
152 Lisp_Object Vwindow_configuration_change_hook;
153
154 /* Nonzero means scroll commands try to put point
155 at the same screen height as previously. */
156
157 Lisp_Object Vscroll_preserve_screen_position;
158
159 #if 0 /* This isn't used anywhere. */
160 /* Nonzero means we can split a frame even if it is "unsplittable". */
161 static int inhibit_frame_unsplittable;
162 #endif /* 0 */
163
164 #define min(a, b) ((a) < (b) ? (a) : (b))
165
166 extern int scroll_margin;
167
168 extern Lisp_Object Qwindow_scroll_functions, Vwindow_scroll_functions;
169 \f
170 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
171 "Returns t if OBJECT is a window.")
172 (object)
173 Lisp_Object object;
174 {
175 return WINDOWP (object) ? Qt : Qnil;
176 }
177
178 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
179 "Returns t if OBJECT is a window which is currently visible.")
180 (object)
181 Lisp_Object object;
182 {
183 return (WINDOWP (object) && ! NILP (XWINDOW (object)->buffer) ? Qt : Qnil);
184 }
185
186 Lisp_Object
187 make_window ()
188 {
189 Lisp_Object val;
190 register struct window *p;
191 register struct Lisp_Vector *vec;
192 int i;
193
194 vec = allocate_vectorlike ((EMACS_INT) VECSIZE (struct window));
195 for (i = 0; i < VECSIZE (struct window); i++)
196 vec->contents[i] = Qnil;
197 vec->size = VECSIZE (struct window);
198 p = (struct window *) vec;
199 XSETFASTINT (p->sequence_number, ++sequence_number);
200 XSETFASTINT (p->left, 0);
201 XSETFASTINT (p->top, 0);
202 XSETFASTINT (p->height, 0);
203 XSETFASTINT (p->width, 0);
204 XSETFASTINT (p->hscroll, 0);
205 p->start = Fmake_marker ();
206 p->pointm = Fmake_marker ();
207 XSETFASTINT (p->use_time, 0);
208 p->frame = Qnil;
209 p->display_table = Qnil;
210 p->dedicated = Qnil;
211 p->pseudo_window_p = 0;
212 bzero (&p->cursor, sizeof (p->cursor));
213 bzero (&p->last_cursor, sizeof (p->last_cursor));
214 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
215 p->desired_matrix = p->current_matrix = 0;
216 p->phys_cursor_type = -1;
217 p->must_be_updated_p = 0;
218 XSETFASTINT (p->window_end_vpos, 0);
219 XSETFASTINT (p->window_end_pos, 0);
220 p->window_end_valid = Qnil;
221 p->vscroll = 0;
222 XSETWINDOW (val, p);
223 XSETFASTINT (p->last_point, 0);
224 return val;
225 }
226
227 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
228 "Return the window that the cursor now appears in and commands apply to.")
229 ()
230 {
231 return selected_window;
232 }
233
234 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
235 "Return the window used now for minibuffers.\n\
236 If the optional argument FRAME is specified, return the minibuffer window\n\
237 used by that frame.")
238 (frame)
239 Lisp_Object frame;
240 {
241 if (NILP (frame))
242 XSETFRAME (frame, selected_frame);
243 else
244 CHECK_LIVE_FRAME (frame, 0);
245
246 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
247 }
248
249 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p, Swindow_minibuffer_p, 0, 1, 0,
250 "Returns non-nil if WINDOW is a minibuffer window.")
251 (window)
252 Lisp_Object window;
253 {
254 struct window *w = decode_window (window);
255 return (MINI_WINDOW_P (w) ? Qt : Qnil);
256 }
257
258 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
259 Spos_visible_in_window_p, 0, 2, 0,
260 "Return t if position POS is currently on the frame in WINDOW.\n\
261 Returns nil if that position is scrolled vertically out of view.\n\
262 POS defaults to point; WINDOW, to the selected window.")
263 (pos, window)
264 Lisp_Object pos, window;
265 {
266 register struct window *w;
267 struct text_pos top;
268 register int posint;
269 register struct buffer *buf;
270
271 if (NILP (pos))
272 posint = PT;
273 else
274 {
275 CHECK_NUMBER_COERCE_MARKER (pos, 0);
276 posint = XINT (pos);
277 }
278
279 w = decode_window (window);
280 SET_TEXT_POS_FROM_MARKER (top, w->start);
281
282 /* If position above window, it's not visible. */
283 if (posint < CHARPOS (top))
284 return Qnil;
285
286 buf = XBUFFER (w->buffer);
287 if (XFASTINT (w->last_modified) >= BUF_MODIFF (buf)
288 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (buf))
289 {
290 /* If frame is up to date,
291 use the info recorded about how much text fit on it. */
292 if (posint < BUF_Z (buf) - XFASTINT (w->window_end_pos))
293 return Qt;
294 return Qnil;
295 }
296 else
297 {
298 struct it it;
299
300 if (posint > BUF_ZV (buf))
301 return Qnil;
302
303 /* w->start can be out of range. If it is, do something reasonable. */
304 if (CHARPOS (top) < BUF_BEGV (buf)
305 || CHARPOS (top) > BUF_ZV (buf))
306 return Qnil;
307
308 start_display (&it, w, top);
309 move_it_to (&it, posint, 0, it.last_visible_y, -1,
310 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
311 return IT_CHARPOS (it) == posint ? Qt : Qnil;
312 }
313 }
314 \f
315 static struct window *
316 decode_window (window)
317 register Lisp_Object window;
318 {
319 if (NILP (window))
320 return XWINDOW (selected_window);
321
322 CHECK_LIVE_WINDOW (window, 0);
323 return XWINDOW (window);
324 }
325
326 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
327 "Return the buffer that WINDOW is displaying.")
328 (window)
329 Lisp_Object window;
330 {
331 return decode_window (window)->buffer;
332 }
333
334 DEFUN ("window-height", Fwindow_height, Swindow_height, 0, 1, 0,
335 "Return the number of lines in WINDOW (including its mode line).")
336 (window)
337 Lisp_Object window;
338 {
339 return decode_window (window)->height;
340 }
341
342 DEFUN ("window-width", Fwindow_width, Swindow_width, 0, 1, 0,
343 "Return the number of display columns in WINDOW.\n\
344 This is the width that is usable columns available for text in WINDOW.\n\
345 If you want to find out how many columns WINDOW takes up,\n\
346 use (let ((edges (window-edges))) (- (nth 2 edges) (nth 0 edges))).")
347 (window)
348 Lisp_Object window;
349 {
350 return make_number (window_internal_width (decode_window (window)));
351 }
352
353 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
354 "Return the number of columns by which WINDOW is scrolled from left margin.")
355 (window)
356 Lisp_Object window;
357 {
358 return decode_window (window)->hscroll;
359 }
360
361 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
362 "Set number of columns WINDOW is scrolled from left margin to NCOL.\n\
363 NCOL should be zero or positive.")
364 (window, ncol)
365 register Lisp_Object window, ncol;
366 {
367 register struct window *w;
368
369 CHECK_NUMBER (ncol, 1);
370 if (XINT (ncol) < 0) XSETFASTINT (ncol, 0);
371 w = decode_window (window);
372 if (XINT (w->hscroll) != XINT (ncol))
373 XBUFFER (w->buffer)->clip_changed = 1; /* Prevent redisplay shortcuts */
374 w->hscroll = ncol;
375 return ncol;
376 }
377
378 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
379 Swindow_redisplay_end_trigger, 0, 1, 0,
380 "Return WINDOW's redisplay end trigger value.\n\
381 See `set-window-redisplay-end-trigger' for more information.")
382 (window)
383 Lisp_Object window;
384 {
385 return decode_window (window)->redisplay_end_trigger;
386 }
387
388 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
389 Sset_window_redisplay_end_trigger, 2, 2, 0,
390 "Set WINDOW's redisplay end trigger value to VALUE.\n\
391 VALUE should be a buffer position (typically a marker) or nil.\n\
392 If it is a buffer position, then if redisplay in WINDOW reaches a position\n\
393 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called\n\
394 with two arguments: WINDOW, and the end trigger value.\n\
395 Afterwards the end-trigger value is reset to nil.")
396 (window, value)
397 register Lisp_Object window, value;
398 {
399 register struct window *w;
400
401 w = decode_window (window);
402 w->redisplay_end_trigger = value;
403 return value;
404 }
405
406 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
407 "Return a list of the edge coordinates of WINDOW.\n\
408 \(LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at top left corner of frame.\n\
409 RIGHT is one more than the rightmost column used by WINDOW,\n\
410 and BOTTOM is one more than the bottommost row used by WINDOW\n\
411 and its mode-line.")
412 (window)
413 Lisp_Object window;
414 {
415 register struct window *w = decode_window (window);
416
417 return Fcons (w->left, Fcons (w->top,
418 Fcons (make_number (WINDOW_RIGHT_EDGE (w)),
419 Fcons (make_number (XFASTINT (w->top)
420 + XFASTINT (w->height)),
421 Qnil))));
422 }
423
424 /* Test if the character at column *x, row *y is within window *w.
425 If it is not, return 0;
426 if it is in the window's text area,
427 set *x and *y to its location relative to the upper left corner
428 of the window, and
429 return 1;
430 if it is on the window's modeline, return 2;
431 if it is on the border between the window and its right sibling,
432 return 3.
433 if it is on the window's top line, return 4;
434
435 X and Y are frame relative pixel coordinates. */
436
437 static int
438 coordinates_in_window (w, x, y)
439 register struct window *w;
440 register int *x, *y;
441 {
442 struct frame *f = XFRAME (WINDOW_FRAME (w));
443 int left_x, right_x, top_y, bottom_y;
444 int flags_area_width = FRAME_FLAGS_AREA_WIDTH (f);
445
446 if (w->pseudo_window_p)
447 {
448 left_x = 0;
449 right_x = XFASTINT (w->width) * CANON_Y_UNIT (f);
450 top_y = WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
451 bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
452 }
453 else
454 {
455 left_x = WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w);
456 right_x = WINDOW_DISPLAY_RIGHT_EDGE_PIXEL_X (w);
457 top_y = WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
458 bottom_y = WINDOW_DISPLAY_BOTTOM_EDGE_PIXEL_Y (w);
459 }
460
461 if (*y < top_y
462 || *y >= bottom_y
463 || *x < (left_x
464 - flags_area_width
465 - (FRAME_LEFT_SCROLL_BAR_WIDTH (f)
466 * CANON_X_UNIT (f)))
467 || *x > right_x + flags_area_width)
468 /* Completely outside anything interesting. */
469 return 0;
470 else if (WINDOW_WANTS_MODELINE_P (w)
471 && *y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w))
472 /* On the mode line. */
473 return 2;
474 else if (WINDOW_WANTS_TOP_LINE_P (w)
475 && *y < top_y + CURRENT_TOP_LINE_HEIGHT (w))
476 /* On the top line. */
477 return 4;
478 else if (*x < left_x || *x >= right_x)
479 /* Other lines than the mode line don't include flags areas and
480 scroll bars on the left. */
481 return 0;
482 else if (!w->pseudo_window_p
483 && !WINDOW_RIGHTMOST_P (w)
484 && *x >= right_x - CANON_X_UNIT (f))
485 /* On the border on the right side of the window? Assume that
486 this area begins at RIGHT_X minus a canonical char width. */
487 return 3;
488 else
489 {
490 /* Convert X and Y to window-relative pixel coordinates. */
491 *x -= left_x;
492 *y -= top_y;
493 return 1;
494 }
495 }
496
497 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
498 Scoordinates_in_window_p, 2, 2, 0,
499 "Return non-nil if COORDINATES are in WINDOW.\n\
500 COORDINATES is a cons of the form (X . Y), X and Y being distances\n\
501 measured in characters from the upper-left corner of the frame.\n\
502 (0 . 0) denotes the character in the upper left corner of the\n\
503 frame.\n\
504 If COORDINATES are in the text portion of WINDOW,\n\
505 the coordinates relative to the window are returned.\n\
506 If they are in the mode line of WINDOW, `mode-line' is returned.\n\
507 If they are in the top mode line of WINDOW, `top-line' is returned.\n\
508 If they are on the border between WINDOW and its right sibling,\n\
509 `vertical-line' is returned.")
510 (coordinates, window)
511 register Lisp_Object coordinates, window;
512 {
513 struct window *w;
514 struct frame *f;
515 int x, y;
516 Lisp_Object lx, ly;
517
518 CHECK_LIVE_WINDOW (window, 0);
519 w = XWINDOW (window);
520 f = XFRAME (w->frame);
521 CHECK_CONS (coordinates, 1);
522 lx = Fcar (coordinates);
523 ly = Fcdr (coordinates);
524 CHECK_NUMBER_OR_FLOAT (lx, 1);
525 CHECK_NUMBER_OR_FLOAT (ly, 1);
526 x = PIXEL_X_FROM_CANON_X (f, lx);
527 y = PIXEL_Y_FROM_CANON_Y (f, ly);
528
529 switch (coordinates_in_window (w, &x, &y))
530 {
531 case 0: /* NOT in window at all. */
532 return Qnil;
533
534 case 1: /* In text part of window. */
535 /* X and Y are now window relative pixel coordinates.
536 Convert them to canonical char units before returning
537 them. */
538 return Fcons (CANON_X_FROM_PIXEL_X (f, x),
539 CANON_Y_FROM_PIXEL_Y (f, y));
540
541 case 2: /* In mode line of window. */
542 return Qmode_line;
543
544 case 3: /* On right border of window. */
545 return Qvertical_line;
546
547 case 4:
548 return Qtop_line;
549
550 default:
551 abort ();
552 }
553 }
554
555 /* Find the window containing frame-relative pixel position X/Y and
556 return it as a Lisp_Object. If X, Y is on the window's modeline,
557 set *PART to 1; if it is on the separating line between the window
558 and its right sibling, set it to 2; otherwise set it to 0. If
559 there is no window under X, Y return nil and leave *PART
560 unmodified. TOOLBAR_P non-zero means detect toolbar windows. */
561
562 Lisp_Object
563 window_from_coordinates (frame, x, y, part, toolbar_p)
564 FRAME_PTR frame;
565 int x, y;
566 int *part;
567 int toolbar_p;
568 {
569 register Lisp_Object tem, first;
570 int found;
571
572 tem = first = FRAME_SELECTED_WINDOW (frame);
573
574 do
575 {
576 found = coordinates_in_window (XWINDOW (tem), &x, &y);
577
578 if (found)
579 {
580 *part = found - 1;
581 return tem;
582 }
583
584 tem = Fnext_window (tem, Qt, Qlambda);
585 }
586 while (!EQ (tem, first));
587
588 /* See if it's in the toolbar window, if a toolbar exists. */
589 if (toolbar_p
590 && WINDOWP (frame->toolbar_window)
591 && XFASTINT (XWINDOW (frame->toolbar_window)->height)
592 && coordinates_in_window (XWINDOW (frame->toolbar_window), &x, &y))
593 {
594 *part = 0;
595 return frame->toolbar_window;
596 }
597
598 return Qnil;
599 }
600
601 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
602 "Return window containing coordinates X and Y on FRAME.\n\
603 If omitted, FRAME defaults to the currently selected frame.\n\
604 The top left corner of the frame is considered to be row 0,\n\
605 column 0.")
606 (x, y, frame)
607 Lisp_Object x, y, frame;
608 {
609 int part;
610 struct frame *f;
611
612 if (NILP (frame))
613 XSETFRAME (frame, selected_frame);
614 else
615 CHECK_LIVE_FRAME (frame, 2);
616 f = XFRAME (frame);
617
618 /* Check that arguments are integers or floats. */
619 CHECK_NUMBER_OR_FLOAT (x, 0);
620 CHECK_NUMBER_OR_FLOAT (y, 1);
621
622 return window_from_coordinates (f,
623 PIXEL_X_FROM_CANON_X (f, x),
624 PIXEL_Y_FROM_CANON_Y (f, y),
625 &part, 0);
626 }
627
628 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
629 "Return current value of point in WINDOW.\n\
630 For a nonselected window, this is the value point would have\n\
631 if that window were selected.\n\
632 \n\
633 Note that, when WINDOW is the selected window and its buffer\n\
634 is also currently selected, the value returned is the same as (point).\n\
635 It would be more strictly correct to return the `top-level' value\n\
636 of point, outside of any save-excursion forms.\n\
637 But that is hard to define.")
638 (window)
639 Lisp_Object window;
640 {
641 register struct window *w = decode_window (window);
642
643 if (w == XWINDOW (selected_window)
644 && current_buffer == XBUFFER (w->buffer))
645 return Fpoint ();
646 return Fmarker_position (w->pointm);
647 }
648
649 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
650 "Return position at which display currently starts in WINDOW.\n\
651 This is updated by redisplay or by calling `set-window-start'.")
652 (window)
653 Lisp_Object window;
654 {
655 return Fmarker_position (decode_window (window)->start);
656 }
657
658 /* This is text temporarily removed from the doc string below.
659
660 This function returns nil if the position is not currently known.\n\
661 That happens when redisplay is preempted and doesn't finish.\n\
662 If in that case you want to compute where the end of the window would\n\
663 have been if redisplay had finished, do this:\n\
664 (save-excursion\n\
665 (goto-char (window-start window))\n\
666 (vertical-motion (1- (window-height window)) window)\n\
667 (point))") */
668
669 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
670 "Return position at which display currently ends in WINDOW.\n\
671 This is updated by redisplay, when it runs to completion.\n\
672 Simply changing the buffer text or setting `window-start'\n\
673 does not update this value.\n\
674 If UP-TO-DATE is non-nil, compute the up-to-date position\n\
675 if it isn't already recorded.")
676 (window, update)
677 Lisp_Object window, update;
678 {
679 Lisp_Object value;
680 struct window *w = decode_window (window);
681 Lisp_Object buf;
682
683 buf = w->buffer;
684 CHECK_BUFFER (buf, 0);
685
686 #if 0 /* This change broke some things. We should make it later. */
687 /* If we don't know the end position, return nil.
688 The user can compute it with vertical-motion if he wants to.
689 It would be nicer to do it automatically,
690 but that's so slow that it would probably bother people. */
691 if (NILP (w->window_end_valid))
692 return Qnil;
693 #endif
694
695 if (! NILP (update)
696 && ! (! NILP (w->window_end_valid)
697 && XFASTINT (w->last_modified) >= MODIFF))
698 {
699 int opoint = PT, opoint_byte = PT_BYTE;
700 TEMP_SET_PT_BOTH (XMARKER (w->start)->charpos,
701 XMARKER (w->start)->bytepos);
702 Fvertical_motion (make_number (window_internal_height (w)), Qnil);
703 XSETINT (value, PT);
704 TEMP_SET_PT_BOTH (opoint, opoint_byte);
705 }
706 else
707 XSETINT (value,
708 BUF_Z (XBUFFER (buf)) - XFASTINT (w->window_end_pos));
709
710 return value;
711 }
712
713 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
714 "Make point value in WINDOW be at position POS in WINDOW's buffer.")
715 (window, pos)
716 Lisp_Object window, pos;
717 {
718 register struct window *w = decode_window (window);
719
720 CHECK_NUMBER_COERCE_MARKER (pos, 1);
721 if (w == XWINDOW (selected_window)
722 && XBUFFER (w->buffer) == current_buffer)
723 Fgoto_char (pos);
724 else
725 set_marker_restricted (w->pointm, pos, w->buffer);
726
727 return pos;
728 }
729
730 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
731 "Make display in WINDOW start at position POS in WINDOW's buffer.\n\
732 Optional third arg NOFORCE non-nil inhibits next redisplay\n\
733 from overriding motion of point in order to display at this exact start.")
734 (window, pos, noforce)
735 Lisp_Object window, pos, noforce;
736 {
737 register struct window *w = decode_window (window);
738
739 CHECK_NUMBER_COERCE_MARKER (pos, 1);
740 set_marker_restricted (w->start, pos, w->buffer);
741 /* this is not right, but much easier than doing what is right. */
742 w->start_at_line_beg = Qnil;
743 if (NILP (noforce))
744 w->force_start = Qt;
745 w->update_mode_line = Qt;
746 XSETFASTINT (w->last_modified, 0);
747 XSETFASTINT (w->last_overlay_modified, 0);
748 if (!EQ (window, selected_window))
749 windows_or_buffers_changed++;
750 return pos;
751 }
752
753 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
754 1, 1, 0,
755 "Return WINDOW's dedicated object, usually t or nil.\n\
756 See also `set-window-dedicated-p'.")
757 (window)
758 Lisp_Object window;
759 {
760 return decode_window (window)->dedicated;
761 }
762
763 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
764 Sset_window_dedicated_p, 2, 2, 0,
765 "Control whether WINDOW is dedicated to the buffer it displays.\n\
766 If it is dedicated, Emacs will not automatically change\n\
767 which buffer appears in it.\n\
768 The second argument is the new value for the dedication flag;\n\
769 non-nil means yes.")
770 (window, arg)
771 Lisp_Object window, arg;
772 {
773 register struct window *w = decode_window (window);
774
775 if (NILP (arg))
776 w->dedicated = Qnil;
777 else
778 w->dedicated = Qt;
779
780 return w->dedicated;
781 }
782
783 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
784 0, 1, 0,
785 "Return the display-table that WINDOW is using.")
786 (window)
787 Lisp_Object window;
788 {
789 return decode_window (window)->display_table;
790 }
791
792 /* Get the display table for use on window W. This is either W's
793 display table or W's buffer's display table. Ignore the specified
794 tables if they are not valid; if no valid table is specified,
795 return 0. */
796
797 struct Lisp_Char_Table *
798 window_display_table (w)
799 struct window *w;
800 {
801 Lisp_Object tem;
802 tem = w->display_table;
803 if (DISP_TABLE_P (tem))
804 return XCHAR_TABLE (tem);
805 if (NILP (w->buffer))
806 return 0;
807
808 tem = XBUFFER (w->buffer)->display_table;
809 if (DISP_TABLE_P (tem))
810 return XCHAR_TABLE (tem);
811 tem = Vstandard_display_table;
812 if (DISP_TABLE_P (tem))
813 return XCHAR_TABLE (tem);
814 return 0;
815 }
816
817 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
818 "Set WINDOW's display-table to TABLE.")
819 (window, table)
820 register Lisp_Object window, table;
821 {
822 register struct window *w;
823
824 w = decode_window (window);
825 w->display_table = table;
826 return table;
827 }
828 \f
829 /* Record info on buffer window w is displaying
830 when it is about to cease to display that buffer. */
831 static void
832 unshow_buffer (w)
833 register struct window *w;
834 {
835 Lisp_Object buf;
836 struct buffer *b;
837
838 buf = w->buffer;
839 b = XBUFFER (buf);
840 if (b != XMARKER (w->pointm)->buffer)
841 abort ();
842
843 if (w == XWINDOW (b->last_selected_window))
844 b->last_selected_window = Qnil;
845
846 #if 0
847 if (w == XWINDOW (selected_window)
848 || ! EQ (buf, XWINDOW (selected_window)->buffer))
849 /* Do this except when the selected window's buffer
850 is being removed from some other window. */
851 #endif
852 /* last_window_start records the start position that this buffer
853 had in the last window to be disconnected from it.
854 Now that this statement is unconditional,
855 it is possible for the buffer to be displayed in the
856 selected window, while last_window_start reflects another
857 window which was recently showing the same buffer.
858 Some people might say that might be a good thing. Let's see. */
859 b->last_window_start = marker_position (w->start);
860
861 /* Point in the selected window's buffer
862 is actually stored in that buffer, and the window's pointm isn't used.
863 So don't clobber point in that buffer. */
864 if (! EQ (buf, XWINDOW (selected_window)->buffer))
865 temp_set_point_both (b,
866 clip_to_bounds (BUF_BEGV (b),
867 XMARKER (w->pointm)->charpos,
868 BUF_ZV (b)),
869 clip_to_bounds (BUF_BEGV_BYTE (b),
870 marker_byte_position (w->pointm),
871 BUF_ZV_BYTE (b)));
872 }
873
874 /* Put replacement into the window structure in place of old. */
875 static void
876 replace_window (old, replacement)
877 Lisp_Object old, replacement;
878 {
879 register Lisp_Object tem;
880 register struct window *o = XWINDOW (old), *p = XWINDOW (replacement);
881
882 /* If OLD is its frame's root_window, then replacement is the new
883 root_window for that frame. */
884
885 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
886 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = replacement;
887
888 p->left = o->left;
889 p->top = o->top;
890 p->width = o->width;
891 p->height = o->height;
892 p->desired_matrix = p->current_matrix = 0;
893 p->vscroll = 0;
894 bzero (&p->cursor, sizeof (p->cursor));
895 bzero (&p->last_cursor, sizeof (p->last_cursor));
896 bzero (&p->phys_cursor, sizeof (p->phys_cursor));
897 p->phys_cursor_type = -1;
898 p->must_be_updated_p = 0;
899 p->pseudo_window_p = 0;
900 XSETFASTINT (p->window_end_vpos, 0);
901 XSETFASTINT (p->window_end_pos, 0);
902 p->window_end_valid = Qnil;
903
904 p->next = tem = o->next;
905 if (!NILP (tem))
906 XWINDOW (tem)->prev = replacement;
907
908 p->prev = tem = o->prev;
909 if (!NILP (tem))
910 XWINDOW (tem)->next = replacement;
911
912 p->parent = tem = o->parent;
913 if (!NILP (tem))
914 {
915 if (EQ (XWINDOW (tem)->vchild, old))
916 XWINDOW (tem)->vchild = replacement;
917 if (EQ (XWINDOW (tem)->hchild, old))
918 XWINDOW (tem)->hchild = replacement;
919 }
920
921 /*** Here, if replacement is a vertical combination
922 and so is its new parent, we should make replacement's
923 children be children of that parent instead. ***/
924 }
925
926 DEFUN ("delete-window", Fdelete_window, Sdelete_window, 0, 1, "",
927 "Remove WINDOW from the display. Default is selected window.")
928 (window)
929 register Lisp_Object window;
930 {
931 delete_window (window);
932
933 if (! NILP (Vwindow_configuration_change_hook)
934 && ! NILP (Vrun_hooks))
935 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
936
937 return Qnil;
938 }
939
940 void
941 delete_window (window)
942 register Lisp_Object window;
943 {
944 register Lisp_Object tem, parent, sib;
945 register struct window *p;
946 register struct window *par;
947 FRAME_PTR frame;
948
949 /* Because this function is called by other C code on non-leaf
950 windows, the CHECK_LIVE_WINDOW macro would choke inappropriately,
951 so we can't decode_window here. */
952 if (NILP (window))
953 window = selected_window;
954 else
955 CHECK_WINDOW (window, 0);
956 p = XWINDOW (window);
957
958 /* It's okay to delete an already-deleted window. */
959 if (NILP (p->buffer)
960 && NILP (p->hchild)
961 && NILP (p->vchild))
962 return;
963
964 parent = p->parent;
965 if (NILP (parent))
966 error ("Attempt to delete minibuffer or sole ordinary window");
967 par = XWINDOW (parent);
968
969 windows_or_buffers_changed++;
970 frame = XFRAME (WINDOW_FRAME (p));
971 FRAME_WINDOW_SIZES_CHANGED (frame) = 1;
972
973 /* Are we trying to delete any frame's selected window? */
974 {
975 Lisp_Object frame, pwindow;
976
977 /* See if the frame's selected window is either WINDOW
978 or any subwindow of it, by finding all that window's parents
979 and comparing each one with WINDOW. */
980 frame = WINDOW_FRAME (XWINDOW (window));
981 pwindow = FRAME_SELECTED_WINDOW (XFRAME (frame));
982
983 while (!NILP (pwindow))
984 {
985 if (EQ (window, pwindow))
986 break;
987 pwindow = XWINDOW (pwindow)->parent;
988 }
989
990 if (EQ (window, pwindow))
991 {
992 Lisp_Object alternative;
993 alternative = Fnext_window (window, Qlambda, Qnil);
994
995 /* If we're about to delete the selected window on the
996 selected frame, then we should use Fselect_window to select
997 the new window. On the other hand, if we're about to
998 delete the selected window on any other frame, we shouldn't do
999 anything but set the frame's selected_window slot. */
1000 if (EQ (window, selected_window))
1001 Fselect_window (alternative);
1002 else
1003 FRAME_SELECTED_WINDOW (XFRAME (frame)) = alternative;
1004 }
1005 }
1006
1007 tem = p->buffer;
1008 /* tem is null for dummy parent windows
1009 (which have inferiors but not any contents themselves) */
1010 if (!NILP (tem))
1011 {
1012 unshow_buffer (p);
1013 unchain_marker (p->pointm);
1014 unchain_marker (p->start);
1015 }
1016
1017 /* Free window glyph matrices.
1018 It is sure that they are allocated again when ADJUST_GLYPHS
1019 is called. */
1020 free_window_matrices (XWINDOW (FRAME_ROOT_WINDOW (frame)));
1021
1022 tem = p->next;
1023 if (!NILP (tem))
1024 XWINDOW (tem)->prev = p->prev;
1025
1026 tem = p->prev;
1027 if (!NILP (tem))
1028 XWINDOW (tem)->next = p->next;
1029
1030 if (EQ (window, par->hchild))
1031 par->hchild = p->next;
1032 if (EQ (window, par->vchild))
1033 par->vchild = p->next;
1034
1035 /* Find one of our siblings to give our space to. */
1036 sib = p->prev;
1037 if (NILP (sib))
1038 {
1039 /* If p gives its space to its next sibling, that sibling needs
1040 to have its top/left side pulled back to where p's is.
1041 set_window_{height,width} will re-position the sibling's
1042 children. */
1043 sib = p->next;
1044 XWINDOW (sib)->top = p->top;
1045 XWINDOW (sib)->left = p->left;
1046 }
1047
1048 /* Stretch that sibling. */
1049 if (!NILP (par->vchild))
1050 set_window_height (sib,
1051 XFASTINT (XWINDOW (sib)->height) + XFASTINT (p->height),
1052 1);
1053 if (!NILP (par->hchild))
1054 set_window_width (sib,
1055 XFASTINT (XWINDOW (sib)->width) + XFASTINT (p->width),
1056 1);
1057
1058 /* If parent now has only one child,
1059 put the child into the parent's place. */
1060 tem = par->hchild;
1061 if (NILP (tem))
1062 tem = par->vchild;
1063 if (NILP (XWINDOW (tem)->next))
1064 replace_window (parent, tem);
1065
1066 /* Since we may be deleting combination windows, we must make sure that
1067 not only p but all its children have been marked as deleted. */
1068 if (! NILP (p->hchild))
1069 delete_all_subwindows (XWINDOW (p->hchild));
1070 else if (! NILP (p->vchild))
1071 delete_all_subwindows (XWINDOW (p->vchild));
1072
1073 /* Mark this window as deleted. */
1074 p->buffer = p->hchild = p->vchild = Qnil;
1075
1076 /* Adjust glyph matrices. */
1077 adjust_glyphs (frame);
1078 }
1079 \f
1080
1081 extern Lisp_Object next_frame (), prev_frame ();
1082
1083 /* This comment supplies the doc string for `next-window',
1084 for make-docfile to see. We cannot put this in the real DEFUN
1085 due to limits in the Unix cpp.
1086
1087 DEFUN ("next-window", Ffoo, Sfoo, 0, 3, 0,
1088 "Return next window after WINDOW in canonical ordering of windows.\n\
1089 If omitted, WINDOW defaults to the selected window.\n\
1090 \n\
1091 Optional second arg MINIBUF t means count the minibuffer window even\n\
1092 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
1093 it is active. MINIBUF neither t nor nil means not to count the\n\
1094 minibuffer even if it is active.\n\
1095 \n\
1096 Several frames may share a single minibuffer; if the minibuffer\n\
1097 counts, all windows on all frames that share that minibuffer count\n\
1098 too. Therefore, `next-window' can be used to iterate through the\n\
1099 set of windows even when the minibuffer is on another frame. If the\n\
1100 minibuffer does not count, only windows from WINDOW's frame count.\n\
1101 \n\
1102 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
1103 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
1104 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
1105 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1106 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
1107 Anything else means restrict to WINDOW's frame.\n\
1108 \n\
1109 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
1110 `next-window' to iterate through the entire cycle of acceptable\n\
1111 windows, eventually ending up back at the window you started with.\n\
1112 `previous-window' traverses the same cycle, in the reverse order.")
1113 (window, minibuf, all_frames) */
1114
1115 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
1116 0)
1117 (window, minibuf, all_frames)
1118 register Lisp_Object window, minibuf, all_frames;
1119 {
1120 register Lisp_Object tem;
1121 Lisp_Object start_window;
1122
1123 if (NILP (window))
1124 window = selected_window;
1125 else
1126 CHECK_LIVE_WINDOW (window, 0);
1127
1128 start_window = window;
1129
1130 /* minibuf == nil may or may not include minibuffers.
1131 Decide if it does. */
1132 if (NILP (minibuf))
1133 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1134 else if (! EQ (minibuf, Qt))
1135 minibuf = Qlambda;
1136 /* Now minibuf can be t => count all minibuffer windows,
1137 lambda => count none of them,
1138 or a specific minibuffer window (the active one) to count. */
1139
1140 /* all_frames == nil doesn't specify which frames to include. */
1141 if (NILP (all_frames))
1142 all_frames = (! EQ (minibuf, Qlambda)
1143 ? (FRAME_MINIBUF_WINDOW
1144 (XFRAME
1145 (WINDOW_FRAME
1146 (XWINDOW (window)))))
1147 : Qnil);
1148 else if (EQ (all_frames, Qvisible))
1149 ;
1150 else if (XFASTINT (all_frames) == 0)
1151 ;
1152 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1153 /* If all_frames is a frame and window arg isn't on that frame, just
1154 return the first window on the frame. */
1155 return Fframe_first_window (all_frames);
1156 else if (! EQ (all_frames, Qt))
1157 all_frames = Qnil;
1158 /* Now all_frames is t meaning search all frames,
1159 nil meaning search just current frame,
1160 visible meaning search just visible frames,
1161 0 meaning search visible and iconified frames,
1162 or a window, meaning search the frame that window belongs to. */
1163
1164 /* Do this loop at least once, to get the next window, and perhaps
1165 again, if we hit the minibuffer and that is not acceptable. */
1166 do
1167 {
1168 /* Find a window that actually has a next one. This loop
1169 climbs up the tree. */
1170 while (tem = XWINDOW (window)->next, NILP (tem))
1171 if (tem = XWINDOW (window)->parent, !NILP (tem))
1172 window = tem;
1173 else
1174 {
1175 /* We've reached the end of this frame.
1176 Which other frames are acceptable? */
1177 tem = WINDOW_FRAME (XWINDOW (window));
1178 if (! NILP (all_frames))
1179 {
1180 Lisp_Object tem1;
1181
1182 tem1 = tem;
1183 tem = next_frame (tem, all_frames);
1184 /* In the case where the minibuffer is active,
1185 and we include its frame as well as the selected one,
1186 next_frame may get stuck in that frame.
1187 If that happens, go back to the selected frame
1188 so we can complete the cycle. */
1189 if (EQ (tem, tem1))
1190 XSETFRAME (tem, selected_frame);
1191 }
1192 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1193
1194 break;
1195 }
1196
1197 window = tem;
1198
1199 /* If we're in a combination window, find its first child and
1200 recurse on that. Otherwise, we've found the window we want. */
1201 while (1)
1202 {
1203 if (!NILP (XWINDOW (window)->hchild))
1204 window = XWINDOW (window)->hchild;
1205 else if (!NILP (XWINDOW (window)->vchild))
1206 window = XWINDOW (window)->vchild;
1207 else break;
1208 }
1209 }
1210 /* Which windows are acceptable?
1211 Exit the loop and accept this window if
1212 this isn't a minibuffer window,
1213 or we're accepting all minibuffer windows,
1214 or this is the active minibuffer and we are accepting that one, or
1215 we've come all the way around and we're back at the original window. */
1216 while (MINI_WINDOW_P (XWINDOW (window))
1217 && ! EQ (minibuf, Qt)
1218 && ! EQ (minibuf, window)
1219 && ! EQ (window, start_window));
1220
1221 return window;
1222 }
1223
1224 /* This comment supplies the doc string for `previous-window',
1225 for make-docfile to see. We cannot put this in the real DEFUN
1226 due to limits in the Unix cpp.
1227
1228 DEFUN ("previous-window", Ffoo, Sfoo, 0, 3, 0,
1229 "Return the window preceding WINDOW in canonical ordering of windows.\n\
1230 If omitted, WINDOW defaults to the selected window.\n\
1231 \n\
1232 Optional second arg MINIBUF t means count the minibuffer window even\n\
1233 if not active. MINIBUF nil or omitted means count the minibuffer iff\n\
1234 it is active. MINIBUF neither t nor nil means not to count the\n\
1235 minibuffer even if it is active.\n\
1236 \n\
1237 Several frames may share a single minibuffer; if the minibuffer\n\
1238 counts, all windows on all frames that share that minibuffer count\n\
1239 too. Therefore, `previous-window' can be used to iterate through\n\
1240 the set of windows even when the minibuffer is on another frame. If\n\
1241 the minibuffer does not count, only windows from WINDOW's frame count\n\
1242 \n\
1243 Optional third arg ALL-FRAMES t means include windows on all frames.\n\
1244 ALL-FRAMES nil or omitted means cycle within the frames as specified\n\
1245 above. ALL-FRAMES = `visible' means include windows on all visible frames.\n\
1246 ALL-FRAMES = 0 means include windows on all visible and iconified frames.\n\
1247 If ALL-FRAMES is a frame, restrict search to windows on that frame.\n\
1248 Anything else means restrict to WINDOW's frame.\n\
1249 \n\
1250 If you use consistent values for MINIBUF and ALL-FRAMES, you can use\n\
1251 `previous-window' to iterate through the entire cycle of acceptable\n\
1252 windows, eventually ending up back at the window you started with.\n\
1253 `next-window' traverses the same cycle, in the reverse order.")
1254 (window, minibuf, all_frames) */
1255
1256
1257 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
1258 0)
1259 (window, minibuf, all_frames)
1260 register Lisp_Object window, minibuf, all_frames;
1261 {
1262 register Lisp_Object tem;
1263 Lisp_Object start_window;
1264
1265 if (NILP (window))
1266 window = selected_window;
1267 else
1268 CHECK_LIVE_WINDOW (window, 0);
1269
1270 start_window = window;
1271
1272 /* minibuf == nil may or may not include minibuffers.
1273 Decide if it does. */
1274 if (NILP (minibuf))
1275 minibuf = (minibuf_level ? minibuf_window : Qlambda);
1276 else if (! EQ (minibuf, Qt))
1277 minibuf = Qlambda;
1278 /* Now minibuf can be t => count all minibuffer windows,
1279 lambda => count none of them,
1280 or a specific minibuffer window (the active one) to count. */
1281
1282 /* all_frames == nil doesn't specify which frames to include.
1283 Decide which frames it includes. */
1284 if (NILP (all_frames))
1285 all_frames = (! EQ (minibuf, Qlambda)
1286 ? (FRAME_MINIBUF_WINDOW
1287 (XFRAME
1288 (WINDOW_FRAME
1289 (XWINDOW (window)))))
1290 : Qnil);
1291 else if (EQ (all_frames, Qvisible))
1292 ;
1293 else if (XFASTINT (all_frames) == 0)
1294 ;
1295 else if (FRAMEP (all_frames) && ! EQ (all_frames, Fwindow_frame (window)))
1296 /* If all_frames is a frame and window arg isn't on that frame, just
1297 return the first window on the frame. */
1298 return Fframe_first_window (all_frames);
1299 else if (! EQ (all_frames, Qt))
1300 all_frames = Qnil;
1301 /* Now all_frames is t meaning search all frames,
1302 nil meaning search just current frame,
1303 visible meaning search just visible frames,
1304 0 meaning search visible and iconified frames,
1305 or a window, meaning search the frame that window belongs to. */
1306
1307 /* Do this loop at least once, to get the previous window, and perhaps
1308 again, if we hit the minibuffer and that is not acceptable. */
1309 do
1310 {
1311 /* Find a window that actually has a previous one. This loop
1312 climbs up the tree. */
1313 while (tem = XWINDOW (window)->prev, NILP (tem))
1314 if (tem = XWINDOW (window)->parent, !NILP (tem))
1315 window = tem;
1316 else
1317 {
1318 /* We have found the top window on the frame.
1319 Which frames are acceptable? */
1320 tem = WINDOW_FRAME (XWINDOW (window));
1321 if (! NILP (all_frames))
1322 /* It's actually important that we use prev_frame here,
1323 rather than next_frame. All the windows acceptable
1324 according to the given parameters should form a ring;
1325 Fnext_window and Fprevious_window should go back and
1326 forth around the ring. If we use next_frame here,
1327 then Fnext_window and Fprevious_window take different
1328 paths through the set of acceptable windows.
1329 window_loop assumes that these `ring' requirement are
1330 met. */
1331 {
1332 Lisp_Object tem1;
1333
1334 tem1 = tem;
1335 tem = prev_frame (tem, all_frames);
1336 /* In the case where the minibuffer is active,
1337 and we include its frame as well as the selected one,
1338 next_frame may get stuck in that frame.
1339 If that happens, go back to the selected frame
1340 so we can complete the cycle. */
1341 if (EQ (tem, tem1))
1342 XSETFRAME (tem, selected_frame);
1343 }
1344 /* If this frame has a minibuffer, find that window first,
1345 because it is conceptually the last window in that frame. */
1346 if (FRAME_HAS_MINIBUF_P (XFRAME (tem)))
1347 tem = FRAME_MINIBUF_WINDOW (XFRAME (tem));
1348 else
1349 tem = FRAME_ROOT_WINDOW (XFRAME (tem));
1350
1351 break;
1352 }
1353
1354 window = tem;
1355 /* If we're in a combination window, find its last child and
1356 recurse on that. Otherwise, we've found the window we want. */
1357 while (1)
1358 {
1359 if (!NILP (XWINDOW (window)->hchild))
1360 window = XWINDOW (window)->hchild;
1361 else if (!NILP (XWINDOW (window)->vchild))
1362 window = XWINDOW (window)->vchild;
1363 else break;
1364 while (tem = XWINDOW (window)->next, !NILP (tem))
1365 window = tem;
1366 }
1367 }
1368 /* Which windows are acceptable?
1369 Exit the loop and accept this window if
1370 this isn't a minibuffer window,
1371 or we're accepting all minibuffer windows,
1372 or this is the active minibuffer and we are accepting that one, or
1373 we've come all the way around and we're back at the original window. */
1374 while (MINI_WINDOW_P (XWINDOW (window))
1375 && ! EQ (minibuf, Qt)
1376 && ! EQ (minibuf, window)
1377 && ! EQ (window, start_window));
1378
1379 return window;
1380 }
1381
1382 DEFUN ("other-window", Fother_window, Sother_window, 1, 2, "p",
1383 "Select the ARG'th different window on this frame.\n\
1384 All windows on current frame are arranged in a cyclic order.\n\
1385 This command selects the window ARG steps away in that order.\n\
1386 A negative ARG moves in the opposite order. If the optional second\n\
1387 argument ALL_FRAMES is non-nil, cycle through all frames.")
1388 (arg, all_frames)
1389 register Lisp_Object arg, all_frames;
1390 {
1391 register int i;
1392 register Lisp_Object w;
1393
1394 CHECK_NUMBER (arg, 0);
1395 w = selected_window;
1396 i = XINT (arg);
1397
1398 while (i > 0)
1399 {
1400 w = Fnext_window (w, Qnil, all_frames);
1401 i--;
1402 }
1403 while (i < 0)
1404 {
1405 w = Fprevious_window (w, Qnil, all_frames);
1406 i++;
1407 }
1408 Fselect_window (w);
1409 return Qnil;
1410 }
1411 \f
1412 /* Look at all windows, performing an operation specified by TYPE
1413 with argument OBJ.
1414 If FRAMES is Qt, look at all frames;
1415 Qnil, look at just the selected frame;
1416 Qvisible, look at visible frames;
1417 a frame, just look at windows on that frame.
1418 If MINI is non-zero, perform the operation on minibuffer windows too.
1419 */
1420
1421 enum window_loop
1422 {
1423 WINDOW_LOOP_UNUSED,
1424 GET_BUFFER_WINDOW, /* Arg is buffer */
1425 GET_LRU_WINDOW, /* Arg is t for full-width windows only */
1426 DELETE_OTHER_WINDOWS, /* Arg is window not to delete */
1427 DELETE_BUFFER_WINDOWS, /* Arg is buffer */
1428 GET_LARGEST_WINDOW,
1429 UNSHOW_BUFFER, /* Arg is buffer */
1430 CHECK_ALL_WINDOWS
1431 };
1432
1433 static Lisp_Object
1434 window_loop (type, obj, mini, frames)
1435 enum window_loop type;
1436 register Lisp_Object obj, frames;
1437 int mini;
1438 {
1439 register Lisp_Object w;
1440 register Lisp_Object best_window;
1441 register Lisp_Object next_window;
1442 register Lisp_Object last_window;
1443 FRAME_PTR frame;
1444 Lisp_Object frame_arg;
1445 frame_arg = Qt;
1446
1447 /* If we're only looping through windows on a particular frame,
1448 frame points to that frame. If we're looping through windows
1449 on all frames, frame is 0. */
1450 if (FRAMEP (frames))
1451 frame = XFRAME (frames);
1452 else if (NILP (frames))
1453 frame = selected_frame;
1454 else
1455 frame = 0;
1456 if (frame)
1457 frame_arg = Qlambda;
1458 else if (XFASTINT (frames) == 0)
1459 frame_arg = frames;
1460 else if (EQ (frames, Qvisible))
1461 frame_arg = frames;
1462
1463 /* frame_arg is Qlambda to stick to one frame,
1464 Qvisible to consider all visible frames,
1465 or Qt otherwise. */
1466
1467 /* Pick a window to start with. */
1468 if (WINDOWP (obj))
1469 w = obj;
1470 else if (frame)
1471 w = FRAME_SELECTED_WINDOW (frame);
1472 else
1473 w = FRAME_SELECTED_WINDOW (selected_frame);
1474
1475 /* Figure out the last window we're going to mess with. Since
1476 Fnext_window, given the same options, is guaranteed to go in a
1477 ring, we can just use Fprevious_window to find the last one.
1478
1479 We can't just wait until we hit the first window again, because
1480 it might be deleted. */
1481
1482 last_window = Fprevious_window (w, mini ? Qt : Qnil, frame_arg);
1483
1484 best_window = Qnil;
1485 for (;;)
1486 {
1487 /* Pick the next window now, since some operations will delete
1488 the current window. */
1489 next_window = Fnext_window (w, mini ? Qt : Qnil, frame_arg);
1490
1491 /* Note that we do not pay attention here to whether
1492 the frame is visible, since Fnext_window skips non-visible frames
1493 if that is desired, under the control of frame_arg. */
1494 if (! MINI_WINDOW_P (XWINDOW (w))
1495 /* For UNSHOW_BUFFER, we must always consider all windows. */
1496 || type == UNSHOW_BUFFER
1497 || (mini && minibuf_level > 0))
1498 switch (type)
1499 {
1500 case GET_BUFFER_WINDOW:
1501 if (XBUFFER (XWINDOW (w)->buffer) == XBUFFER (obj)
1502 /* Don't find any minibuffer window
1503 except the one that is currently in use. */
1504 && (MINI_WINDOW_P (XWINDOW (w))
1505 ? EQ (w, minibuf_window) : 1))
1506 return w;
1507 break;
1508
1509 case GET_LRU_WINDOW:
1510 /* t as arg means consider only full-width windows */
1511 if (!NILP (obj) && !WINDOW_FULL_WIDTH_P (XWINDOW (w)))
1512 break;
1513 /* Ignore dedicated windows and minibuffers. */
1514 if (MINI_WINDOW_P (XWINDOW (w))
1515 || !NILP (XWINDOW (w)->dedicated))
1516 break;
1517 if (NILP (best_window)
1518 || (XFASTINT (XWINDOW (best_window)->use_time)
1519 > XFASTINT (XWINDOW (w)->use_time)))
1520 best_window = w;
1521 break;
1522
1523 case DELETE_OTHER_WINDOWS:
1524 if (XWINDOW (w) != XWINDOW (obj))
1525 Fdelete_window (w);
1526 break;
1527
1528 case DELETE_BUFFER_WINDOWS:
1529 if (EQ (XWINDOW (w)->buffer, obj))
1530 {
1531 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1532
1533 /* If this window is dedicated, and in a frame of its own,
1534 kill the frame. */
1535 if (EQ (w, FRAME_ROOT_WINDOW (f))
1536 && !NILP (XWINDOW (w)->dedicated)
1537 && other_visible_frames (f))
1538 {
1539 /* Skip the other windows on this frame.
1540 There might be one, the minibuffer! */
1541 if (! EQ (w, last_window))
1542 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1543 {
1544 /* As we go, check for the end of the loop.
1545 We mustn't start going around a second time. */
1546 if (EQ (next_window, last_window))
1547 {
1548 last_window = w;
1549 break;
1550 }
1551 next_window = Fnext_window (next_window,
1552 mini ? Qt : Qnil,
1553 frame_arg);
1554 }
1555 /* Now we can safely delete the frame. */
1556 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1557 }
1558 else
1559 /* If we're deleting the buffer displayed in the only window
1560 on the frame, find a new buffer to display there. */
1561 if (NILP (XWINDOW (w)->parent))
1562 {
1563 Lisp_Object new_buffer;
1564 new_buffer = Fother_buffer (obj, Qnil,
1565 XWINDOW (w)->frame);
1566 if (NILP (new_buffer))
1567 new_buffer
1568 = Fget_buffer_create (build_string ("*scratch*"));
1569 Fset_window_buffer (w, new_buffer);
1570 if (EQ (w, selected_window))
1571 Fset_buffer (XWINDOW (w)->buffer);
1572 }
1573 else
1574 Fdelete_window (w);
1575 }
1576 break;
1577
1578 case GET_LARGEST_WINDOW:
1579 /* Ignore dedicated windows and minibuffers. */
1580 if (MINI_WINDOW_P (XWINDOW (w))
1581 || !NILP (XWINDOW (w)->dedicated))
1582 break;
1583 {
1584 struct window *best_window_ptr = XWINDOW (best_window);
1585 struct window *w_ptr = XWINDOW (w);
1586 if (NILP (best_window)
1587 || (XFASTINT (w_ptr->height) * XFASTINT (w_ptr->width)
1588 > (XFASTINT (best_window_ptr->height)
1589 * XFASTINT (best_window_ptr->width))))
1590 best_window = w;
1591 }
1592 break;
1593
1594 case UNSHOW_BUFFER:
1595 if (EQ (XWINDOW (w)->buffer, obj))
1596 {
1597 /* Find another buffer to show in this window. */
1598 Lisp_Object another_buffer;
1599 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (w)));
1600 another_buffer = Fother_buffer (obj, Qnil, XWINDOW (w)->frame);
1601 if (NILP (another_buffer))
1602 another_buffer
1603 = Fget_buffer_create (build_string ("*scratch*"));
1604 /* If this window is dedicated, and in a frame of its own,
1605 kill the frame. */
1606 if (EQ (w, FRAME_ROOT_WINDOW (f))
1607 && !NILP (XWINDOW (w)->dedicated)
1608 && other_visible_frames (f))
1609 {
1610 /* Skip the other windows on this frame.
1611 There might be one, the minibuffer! */
1612 if (! EQ (w, last_window))
1613 while (f == XFRAME (WINDOW_FRAME (XWINDOW (next_window))))
1614 {
1615 /* As we go, check for the end of the loop.
1616 We mustn't start going around a second time. */
1617 if (EQ (next_window, last_window))
1618 {
1619 last_window = w;
1620 break;
1621 }
1622 next_window = Fnext_window (next_window,
1623 mini ? Qt : Qnil,
1624 frame_arg);
1625 }
1626 /* Now we can safely delete the frame. */
1627 Fdelete_frame (WINDOW_FRAME (XWINDOW (w)), Qnil);
1628 }
1629 else
1630 {
1631 /* Otherwise show a different buffer in the window. */
1632 XWINDOW (w)->dedicated = Qnil;
1633 Fset_window_buffer (w, another_buffer);
1634 if (EQ (w, selected_window))
1635 Fset_buffer (XWINDOW (w)->buffer);
1636 }
1637 }
1638 break;
1639
1640 /* Check for a window that has a killed buffer. */
1641 case CHECK_ALL_WINDOWS:
1642 if (! NILP (XWINDOW (w)->buffer)
1643 && NILP (XBUFFER (XWINDOW (w)->buffer)->name))
1644 abort ();
1645 }
1646
1647 if (EQ (w, last_window))
1648 break;
1649
1650 w = next_window;
1651 }
1652
1653 return best_window;
1654 }
1655
1656 /* Used for debugging. Abort if any window has a dead buffer. */
1657
1658 check_all_windows ()
1659 {
1660 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
1661 }
1662
1663 DEFUN ("get-lru-window", Fget_lru_window, Sget_lru_window, 0, 1, 0,
1664 "Return the window least recently selected or used for display.\n\
1665 If optional argument FRAME is `visible', search all visible frames.\n\
1666 If FRAME is 0, search all visible and iconified frames.\n\
1667 If FRAME is t, search all frames.\n\
1668 If FRAME is nil, search only the selected frame.\n\
1669 If FRAME is a frame, search only that frame.")
1670 (frame)
1671 Lisp_Object frame;
1672 {
1673 register Lisp_Object w;
1674 /* First try for a window that is full-width */
1675 w = window_loop (GET_LRU_WINDOW, Qt, 0, frame);
1676 if (!NILP (w) && !EQ (w, selected_window))
1677 return w;
1678 /* If none of them, try the rest */
1679 return window_loop (GET_LRU_WINDOW, Qnil, 0, frame);
1680 }
1681
1682 DEFUN ("get-largest-window", Fget_largest_window, Sget_largest_window, 0, 1, 0,
1683 "Return the largest window in area.\n\
1684 If optional argument FRAME is `visible', search all visible frames.\n\
1685 If FRAME is 0, search all visible and iconified frames.\n\
1686 If FRAME is t, search all frames.\n\
1687 If FRAME is nil, search only the selected frame.\n\
1688 If FRAME is a frame, search only that frame.")
1689 (frame)
1690 Lisp_Object frame;
1691 {
1692 return window_loop (GET_LARGEST_WINDOW, Qnil, 0,
1693 frame);
1694 }
1695
1696 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 1, 2, 0,
1697 "Return a window currently displaying BUFFER, or nil if none.\n\
1698 If optional argument FRAME is `visible', search all visible frames.\n\
1699 If optional argument FRAME is 0, search all visible and iconified frames.\n\
1700 If FRAME is t, search all frames.\n\
1701 If FRAME is nil, search only the selected frame.\n\
1702 If FRAME is a frame, search only that frame.")
1703 (buffer, frame)
1704 Lisp_Object buffer, frame;
1705 {
1706 buffer = Fget_buffer (buffer);
1707 if (BUFFERP (buffer))
1708 return window_loop (GET_BUFFER_WINDOW, buffer, 1, frame);
1709 else
1710 return Qnil;
1711 }
1712
1713 DEFUN ("delete-other-windows", Fdelete_other_windows, Sdelete_other_windows,
1714 0, 1, "",
1715 "Make WINDOW (or the selected window) fill its frame.\n\
1716 Only the frame WINDOW is on is affected.\n\
1717 This function tries to reduce display jumps\n\
1718 by keeping the text previously visible in WINDOW\n\
1719 in the same place on the frame. Doing this depends on\n\
1720 the value of (window-start WINDOW), so if calling this function\n\
1721 in a program gives strange scrolling, make sure the window-start\n\
1722 value is reasonable when this function is called.")
1723 (window)
1724 Lisp_Object window;
1725 {
1726 struct window *w;
1727 int startpos;
1728 int top;
1729
1730 if (NILP (window))
1731 window = selected_window;
1732 else
1733 CHECK_LIVE_WINDOW (window, 0);
1734
1735 w = XWINDOW (window);
1736
1737 startpos = marker_position (w->start);
1738 top = XFASTINT (w->top) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
1739
1740 if (MINI_WINDOW_P (w) && top > 0)
1741 error ("Can't expand minibuffer to full frame");
1742
1743 window_loop (DELETE_OTHER_WINDOWS, window, 0, WINDOW_FRAME (w));
1744
1745 /* Try to minimize scrolling, by setting the window start to the point
1746 will cause the text at the old window start to be at the same place
1747 on the frame. But don't try to do this if the window start is
1748 outside the visible portion (as might happen when the display is
1749 not current, due to typeahead). */
1750 if (startpos >= BUF_BEGV (XBUFFER (w->buffer))
1751 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
1752 {
1753 struct position pos;
1754 struct buffer *obuf = current_buffer;
1755
1756 Fset_buffer (w->buffer);
1757 /* This computation used to temporarily move point, but that can
1758 have unwanted side effects due to text properties. */
1759 pos = *vmotion (startpos, -top, w);
1760
1761 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
1762 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
1763 || FETCH_BYTE (pos.bytepos - 1) == '\n') ? Qt
1764 : Qnil);
1765 /* We need to do this, so that the window-scroll-functions
1766 get called. */
1767 w->optional_new_start = Qt;
1768
1769 set_buffer_internal (obuf);
1770 }
1771
1772 return Qnil;
1773 }
1774
1775 DEFUN ("delete-windows-on", Fdelete_windows_on, Sdelete_windows_on,
1776 1, 2, "bDelete windows on (buffer): ",
1777 "Delete all windows showing BUFFER.\n\
1778 Optional second argument FRAME controls which frames are affected.\n\
1779 If optional argument FRAME is `visible', search all visible frames.\n\
1780 If FRAME is 0, search all visible and iconified frames.\n\
1781 If FRAME is nil, search all frames.\n\
1782 If FRAME is t, search only the selected frame.\n\
1783 If FRAME is a frame, search only that frame.")
1784 (buffer, frame)
1785 Lisp_Object buffer, frame;
1786 {
1787 /* FRAME uses t and nil to mean the opposite of what window_loop
1788 expects. */
1789 if (NILP (frame))
1790 frame = Qt;
1791 else if (EQ (frame, Qt))
1792 frame = Qnil;
1793
1794 if (!NILP (buffer))
1795 {
1796 buffer = Fget_buffer (buffer);
1797 CHECK_BUFFER (buffer, 0);
1798 window_loop (DELETE_BUFFER_WINDOWS, buffer, 0, frame);
1799 }
1800
1801 return Qnil;
1802 }
1803
1804 DEFUN ("replace-buffer-in-windows", Freplace_buffer_in_windows,
1805 Sreplace_buffer_in_windows,
1806 1, 1, "bReplace buffer in windows: ",
1807 "Replace BUFFER with some other buffer in all windows showing it.")
1808 (buffer)
1809 Lisp_Object buffer;
1810 {
1811 if (!NILP (buffer))
1812 {
1813 buffer = Fget_buffer (buffer);
1814 CHECK_BUFFER (buffer, 0);
1815 window_loop (UNSHOW_BUFFER, buffer, 0, Qt);
1816 }
1817 return Qnil;
1818 }
1819
1820 /* Replace BUFFER with some other buffer in all windows
1821 of all frames, even those on other keyboards. */
1822
1823 void
1824 replace_buffer_in_all_windows (buffer)
1825 Lisp_Object buffer;
1826 {
1827 #ifdef MULTI_KBOARD
1828 Lisp_Object tail, frame;
1829
1830 /* A single call to window_loop won't do the job
1831 because it only considers frames on the current keyboard.
1832 So loop manually over frames, and handle each one. */
1833 FOR_EACH_FRAME (tail, frame)
1834 window_loop (UNSHOW_BUFFER, buffer, 1, frame);
1835 #else
1836 window_loop (UNSHOW_BUFFER, buffer, 1, Qt);
1837 #endif
1838 }
1839 \f
1840 /* Set the height of WINDOW and all its inferiors. */
1841
1842 /* The smallest acceptable dimensions for a window. Anything smaller
1843 might crash Emacs. */
1844
1845 #define MIN_SAFE_WINDOW_WIDTH (2)
1846 #define MIN_SAFE_WINDOW_HEIGHT (2)
1847
1848 /* Make sure that window_min_height and window_min_width are
1849 not too small; if they are, set them to safe minima. */
1850
1851 static void
1852 check_min_window_sizes ()
1853 {
1854 /* Smaller values might permit a crash. */
1855 if (window_min_width < MIN_SAFE_WINDOW_WIDTH)
1856 window_min_width = MIN_SAFE_WINDOW_WIDTH;
1857 if (window_min_height < MIN_SAFE_WINDOW_HEIGHT)
1858 window_min_height = MIN_SAFE_WINDOW_HEIGHT;
1859 }
1860
1861 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
1862 minimum allowable size. */
1863
1864 void
1865 check_frame_size (frame, rows, cols)
1866 FRAME_PTR frame;
1867 int *rows, *cols;
1868 {
1869 /* For height, we have to see:
1870 whether the frame has a minibuffer,
1871 whether it wants a mode line, and
1872 whether it has a menu bar. */
1873 int min_height =
1874 (FRAME_MINIBUF_ONLY_P (frame) ? MIN_SAFE_WINDOW_HEIGHT - 1
1875 : (! FRAME_HAS_MINIBUF_P (frame)) ? MIN_SAFE_WINDOW_HEIGHT
1876 : 2 * MIN_SAFE_WINDOW_HEIGHT - 1);
1877
1878 if (FRAME_TOP_MARGIN (frame) > 0)
1879 min_height += FRAME_TOP_MARGIN (frame);
1880
1881 if (*rows < min_height)
1882 *rows = min_height;
1883 if (*cols < MIN_SAFE_WINDOW_WIDTH)
1884 *cols = MIN_SAFE_WINDOW_WIDTH;
1885 }
1886
1887 /* Normally the window is deleted if it gets too small. nodelete
1888 nonzero means do not do this. (The caller should check later and
1889 do so if appropriate) */
1890
1891 void
1892 set_window_height (window, height, nodelete)
1893 Lisp_Object window;
1894 int height;
1895 int nodelete;
1896 {
1897 register struct window *w = XWINDOW (window);
1898 register struct window *c;
1899 int oheight = XFASTINT (w->height);
1900 int top, pos, lastbot, opos, lastobot;
1901 Lisp_Object child;
1902
1903 check_min_window_sizes ();
1904
1905 if (!nodelete
1906 && ! NILP (w->parent)
1907 && (MINI_WINDOW_P (w)
1908 ? height < 1
1909 : height < window_min_height))
1910 {
1911 delete_window (window);
1912 return;
1913 }
1914
1915 XSETFASTINT (w->last_modified, 0);
1916 XSETFASTINT (w->last_overlay_modified, 0);
1917 windows_or_buffers_changed++;
1918 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1919
1920 XSETFASTINT (w->height, height);
1921 if (!NILP (w->hchild))
1922 {
1923 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
1924 {
1925 XWINDOW (child)->top = w->top;
1926 set_window_height (child, height, nodelete);
1927 }
1928 }
1929 else if (!NILP (w->vchild))
1930 {
1931 lastbot = top = XFASTINT (w->top);
1932 lastobot = 0;
1933 for (child = w->vchild; !NILP (child); child = c->next)
1934 {
1935 c = XWINDOW (child);
1936
1937 opos = lastobot + XFASTINT (c->height);
1938
1939 XSETFASTINT (c->top, lastbot);
1940
1941 pos = (((opos * height) << 1) + oheight) / (oheight << 1);
1942
1943 /* Avoid confusion: inhibit deletion of child if becomes too small */
1944 set_window_height (child, pos + top - lastbot, 1);
1945
1946 /* Now advance child to next window,
1947 and set lastbot if child was not just deleted. */
1948 lastbot = pos + top;
1949 lastobot = opos;
1950 }
1951 /* Now delete any children that became too small. */
1952 if (!nodelete)
1953 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
1954 {
1955 set_window_height (child, XINT (XWINDOW (child)->height), 0);
1956 }
1957 }
1958 }
1959
1960 /* Recursively set width of WINDOW and its inferiors. */
1961
1962 void
1963 set_window_width (window, width, nodelete)
1964 Lisp_Object window;
1965 int width;
1966 int nodelete;
1967 {
1968 register struct window *w = XWINDOW (window);
1969 register struct window *c;
1970 int owidth = XFASTINT (w->width);
1971 int left, pos, lastright, opos, lastoright;
1972 Lisp_Object child;
1973
1974 if (!nodelete && width < window_min_width && !NILP (w->parent))
1975 {
1976 delete_window (window);
1977 return;
1978 }
1979
1980 XSETFASTINT (w->last_modified, 0);
1981 XSETFASTINT (w->last_overlay_modified, 0);
1982 windows_or_buffers_changed++;
1983 FRAME_WINDOW_SIZES_CHANGED (XFRAME (WINDOW_FRAME (w))) = 1;
1984
1985 XSETFASTINT (w->width, width);
1986 if (!NILP (w->vchild))
1987 {
1988 for (child = w->vchild; !NILP (child); child = XWINDOW (child)->next)
1989 {
1990 XWINDOW (child)->left = w->left;
1991 set_window_width (child, width, nodelete);
1992 }
1993 }
1994 else if (!NILP (w->hchild))
1995 {
1996 lastright = left = XFASTINT (w->left);
1997 lastoright = 0;
1998 for (child = w->hchild; !NILP (child); child = c->next)
1999 {
2000 c = XWINDOW (child);
2001
2002 opos = lastoright + XFASTINT (c->width);
2003
2004 XSETFASTINT (c->left, lastright);
2005
2006 pos = (((opos * width) << 1) + owidth) / (owidth << 1);
2007
2008 /* Inhibit deletion for becoming too small */
2009 set_window_width (child, pos + left - lastright, 1);
2010
2011 /* Now advance child to next window,
2012 and set lastright if child was not just deleted. */
2013 lastright = pos + left, lastoright = opos;
2014 }
2015 /* Delete children that became too small */
2016 if (!nodelete)
2017 for (child = w->hchild; !NILP (child); child = XWINDOW (child)->next)
2018 {
2019 set_window_width (child, XINT (XWINDOW (child)->width), 0);
2020 }
2021 }
2022 }
2023 \f
2024 int window_select_count;
2025
2026 Lisp_Object
2027 Fset_window_buffer_unwind (obuf)
2028 Lisp_Object obuf;
2029 {
2030 Fset_buffer (obuf);
2031 return Qnil;
2032 }
2033
2034
2035 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2036 means it's allowed to run hooks. See make_frame for a case where
2037 it's not allowed. */
2038
2039 void
2040 set_window_buffer (window, buffer, run_hooks_p)
2041 Lisp_Object window, buffer;
2042 int run_hooks_p;
2043 {
2044 struct window *w = XWINDOW (window);
2045 struct buffer *b = XBUFFER (buffer);
2046 int count = specpdl_ptr - specpdl;
2047
2048 w->buffer = buffer;
2049
2050 if (EQ (window, selected_window))
2051 b->last_selected_window = window;
2052
2053 /* Update time stamps of buffer display. */
2054 if (INTEGERP (b->display_count))
2055 XSETINT (b->display_count, XINT (b->display_count) + 1);
2056 b->display_time = Fcurrent_time ();
2057
2058 XSETFASTINT (w->window_end_pos, 0);
2059 XSETFASTINT (w->window_end_vpos, 0);
2060 bzero (&w->last_cursor, sizeof w->last_cursor);
2061 w->window_end_valid = Qnil;
2062 XSETFASTINT (w->hscroll, 0);
2063 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
2064 set_marker_restricted (w->start,
2065 make_number (b->last_window_start),
2066 buffer);
2067 w->start_at_line_beg = Qnil;
2068 w->force_start = Qnil;
2069 XSETFASTINT (w->last_modified, 0);
2070 XSETFASTINT (w->last_overlay_modified, 0);
2071 windows_or_buffers_changed++;
2072
2073 /* We must select BUFFER for running the window-scroll-functions.
2074 If WINDOW is selected, switch permanently.
2075 Otherwise, switch but go back to the ambient buffer afterward. */
2076 if (EQ (window, selected_window))
2077 Fset_buffer (buffer);
2078 /* We can't check ! NILP (Vwindow_scroll_functions) here
2079 because that might itself be a local variable. */
2080 else if (window_initialized)
2081 {
2082 record_unwind_protect (Fset_window_buffer_unwind, Fcurrent_buffer ());
2083 Fset_buffer (buffer);
2084 }
2085
2086 /* Set left and right marginal area width from buffer. */
2087 Fset_window_margins (b->left_margin_width, b->right_margin_width,
2088 window);
2089
2090 if (run_hooks_p)
2091 {
2092 if (! NILP (Vwindow_scroll_functions))
2093 run_hook_with_args_2 (Qwindow_scroll_functions, window,
2094 Fmarker_position (w->start));
2095
2096 if (! NILP (Vwindow_configuration_change_hook)
2097 && ! NILP (Vrun_hooks))
2098 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2099 }
2100
2101 unbind_to (count, Qnil);
2102 }
2103
2104
2105 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 2, 0,
2106 "Make WINDOW display BUFFER as its contents.\n\
2107 BUFFER can be a buffer or buffer name.")
2108 (window, buffer)
2109 register Lisp_Object window, buffer;
2110 {
2111 register Lisp_Object tem;
2112 register struct window *w = decode_window (window);
2113 struct buffer *b;
2114
2115 buffer = Fget_buffer (buffer);
2116 CHECK_BUFFER (buffer, 1);
2117
2118 if (NILP (XBUFFER (buffer)->name))
2119 error ("Attempt to display deleted buffer");
2120
2121 tem = w->buffer;
2122 if (NILP (tem))
2123 error ("Window is deleted");
2124 else if (! EQ (tem, Qt)) /* w->buffer is t when the window
2125 is first being set up. */
2126 {
2127 if (!NILP (w->dedicated) && !EQ (tem, buffer))
2128 error ("Window is dedicated to `%s'",
2129 XSTRING (XBUFFER (tem)->name)->data);
2130
2131 unshow_buffer (w);
2132 }
2133
2134 set_window_buffer (window, buffer, 1);
2135 return Qnil;
2136 }
2137
2138 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 1, 0,
2139 "Select WINDOW. Most editing will apply to WINDOW's buffer.\n\
2140 If WINDOW is not already selected, also make WINDOW's buffer current.\n\
2141 Note that the main editor command loop\n\
2142 selects the buffer of the selected window before each command.")
2143 (window)
2144 register Lisp_Object window;
2145 {
2146 return select_window_1 (window, 1);
2147 }
2148 \f
2149 static Lisp_Object
2150 select_window_1 (window, recordflag)
2151 register Lisp_Object window;
2152 int recordflag;
2153 {
2154 register struct window *w;
2155 register struct window *ow = XWINDOW (selected_window);
2156
2157 CHECK_LIVE_WINDOW (window, 0);
2158
2159 w = XWINDOW (window);
2160
2161 if (NILP (w->buffer))
2162 error ("Trying to select deleted window or non-leaf window");
2163
2164 XSETFASTINT (w->use_time, ++window_select_count);
2165 if (EQ (window, selected_window))
2166 return window;
2167
2168 if (! NILP (ow->buffer))
2169 set_marker_both (ow->pointm, ow->buffer,
2170 BUF_PT (XBUFFER (ow->buffer)),
2171 BUF_PT_BYTE (XBUFFER (ow->buffer)));
2172
2173 selected_window = window;
2174 if (XFRAME (WINDOW_FRAME (w)) != selected_frame)
2175 {
2176 XFRAME (WINDOW_FRAME (w))->selected_window = window;
2177 /* Use this rather than Fhandle_switch_frame
2178 so that FRAME_FOCUS_FRAME is moved appropriately as we
2179 move around in the state where a minibuffer in a separate
2180 frame is active. */
2181 Fselect_frame (WINDOW_FRAME (w), Qnil);
2182 }
2183 else
2184 selected_frame->selected_window = window;
2185
2186 if (recordflag)
2187 record_buffer (w->buffer);
2188 Fset_buffer (w->buffer);
2189
2190 XBUFFER (w->buffer)->last_selected_window = window;
2191
2192 /* Go to the point recorded in the window.
2193 This is important when the buffer is in more
2194 than one window. It also matters when
2195 redisplay_window has altered point after scrolling,
2196 because it makes the change only in the window. */
2197 {
2198 register int new_point = marker_position (w->pointm);
2199 if (new_point < BEGV)
2200 SET_PT (BEGV);
2201 else if (new_point > ZV)
2202 SET_PT (ZV);
2203 else
2204 SET_PT (new_point);
2205 }
2206
2207 windows_or_buffers_changed++;
2208 return window;
2209 }
2210 \f
2211 /* Deiconify the frame containing the window WINDOW,
2212 unless it is the selected frame;
2213 then return WINDOW.
2214
2215 The reason for the exception for the selected frame
2216 is that it seems better not to change the selected frames visibility
2217 merely because of displaying a different buffer in it.
2218 The deiconification is useful when a buffer gets shown in
2219 another frame that you were not using lately. */
2220
2221 static Lisp_Object
2222 display_buffer_1 (window)
2223 Lisp_Object window;
2224 {
2225 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
2226 FRAME_SAMPLE_VISIBILITY (f);
2227 if (f != selected_frame)
2228 {
2229 if (FRAME_ICONIFIED_P (f))
2230 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2231 else if (FRAME_VISIBLE_P (f))
2232 Fraise_frame (WINDOW_FRAME (XWINDOW (window)));
2233 }
2234 return window;
2235 }
2236
2237 DEFUN ("special-display-p", Fspecial_display_p, Sspecial_display_p, 1, 1, 0,
2238 "Returns non-nil if a buffer named BUFFER-NAME would be created specially.\n\
2239 The value is actually t if the frame should be called with default frame\n\
2240 parameters, and a list of frame parameters if they were specified.\n\
2241 See `special-display-buffer-names', and `special-display-regexps'.")
2242 (buffer_name)
2243 Lisp_Object buffer_name;
2244 {
2245 Lisp_Object tem;
2246
2247 CHECK_STRING (buffer_name, 1);
2248
2249 tem = Fmember (buffer_name, Vspecial_display_buffer_names);
2250 if (!NILP (tem))
2251 return Qt;
2252
2253 tem = Fassoc (buffer_name, Vspecial_display_buffer_names);
2254 if (!NILP (tem))
2255 return XCDR (tem);
2256
2257 for (tem = Vspecial_display_regexps; CONSP (tem); tem = XCDR (tem))
2258 {
2259 Lisp_Object car = XCAR (tem);
2260 if (STRINGP (car)
2261 && fast_string_match (car, buffer_name) >= 0)
2262 return Qt;
2263 else if (CONSP (car)
2264 && STRINGP (XCAR (car))
2265 && fast_string_match (XCAR (car), buffer_name) >= 0)
2266 return XCDR (car);
2267 }
2268 return Qnil;
2269 }
2270
2271 DEFUN ("same-window-p", Fsame_window_p, Ssame_window_p, 1, 1, 0,
2272 "Returns non-nil if a new buffer named BUFFER-NAME would use the same window.\n\
2273 See `same-window-buffer-names' and `same-window-regexps'.")
2274 (buffer_name)
2275 Lisp_Object buffer_name;
2276 {
2277 Lisp_Object tem;
2278
2279 CHECK_STRING (buffer_name, 1);
2280
2281 tem = Fmember (buffer_name, Vsame_window_buffer_names);
2282 if (!NILP (tem))
2283 return Qt;
2284
2285 tem = Fassoc (buffer_name, Vsame_window_buffer_names);
2286 if (!NILP (tem))
2287 return Qt;
2288
2289 for (tem = Vsame_window_regexps; CONSP (tem); tem = XCDR (tem))
2290 {
2291 Lisp_Object car = XCAR (tem);
2292 if (STRINGP (car)
2293 && fast_string_match (car, buffer_name) >= 0)
2294 return Qt;
2295 else if (CONSP (car)
2296 && STRINGP (XCAR (car))
2297 && fast_string_match (XCAR (car), buffer_name) >= 0)
2298 return Qt;
2299 }
2300 return Qnil;
2301 }
2302
2303 /* Use B so the default is (other-buffer). */
2304 DEFUN ("display-buffer", Fdisplay_buffer, Sdisplay_buffer, 1, 3,
2305 "BDisplay buffer: \nP",
2306 "Make BUFFER appear in some window but don't select it.\n\
2307 BUFFER can be a buffer or a buffer name.\n\
2308 If BUFFER is shown already in some window, just use that one,\n\
2309 unless the window is the selected window and the optional second\n\
2310 argument NOT-THIS-WINDOW is non-nil (interactively, with prefix arg).\n\
2311 If `pop-up-frames' is non-nil, make a new frame if no window shows BUFFER.\n\
2312 Returns the window displaying BUFFER.\n\
2313 \n\
2314 The variables `special-display-buffer-names', `special-display-regexps',\n\
2315 `same-window-buffer-names', and `same-window-regexps' customize how certain\n\
2316 buffer names are handled.\n\
2317 \n\
2318 If optional argument FRAME is `visible', search all visible frames.\n\
2319 If FRAME is 0, search all visible and iconified frames.\n\
2320 If FRAME is t, search all frames.\n\
2321 If FRAME is a frame, search only that frame.\n\
2322 If FRAME is nil, search only the selected frame\n\
2323 (actually the last nonminibuffer frame),\n\
2324 unless `pop-up-frames' is non-nil,\n\
2325 which means search visible and iconified frames.")
2326 (buffer, not_this_window, frame)
2327 register Lisp_Object buffer, not_this_window, frame;
2328 {
2329 register Lisp_Object window, tem, swp;
2330
2331 swp = Qnil;
2332 buffer = Fget_buffer (buffer);
2333 CHECK_BUFFER (buffer, 0);
2334
2335 if (!NILP (Vdisplay_buffer_function))
2336 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2337
2338 if (NILP (not_this_window)
2339 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
2340 return display_buffer_1 (selected_window);
2341
2342 /* See if the user has specified this buffer should appear
2343 in the selected window. */
2344 if (NILP (not_this_window))
2345 {
2346 swp = Fsame_window_p (XBUFFER (buffer)->name);
2347 if (!NILP (swp) && !no_switch_window (selected_window))
2348 {
2349 Fswitch_to_buffer (buffer, Qnil);
2350 return display_buffer_1 (selected_window);
2351 }
2352 }
2353
2354 /* If pop_up_frames,
2355 look for a window showing BUFFER on any visible or iconified frame.
2356 Otherwise search only the current frame. */
2357 if (! NILP (frame))
2358 tem = frame;
2359 else if (pop_up_frames || last_nonminibuf_frame == 0)
2360 XSETFASTINT (tem, 0);
2361 else
2362 XSETFRAME (tem, last_nonminibuf_frame);
2363 window = Fget_buffer_window (buffer, tem);
2364 if (!NILP (window)
2365 && (NILP (not_this_window) || !EQ (window, selected_window)))
2366 {
2367 return display_buffer_1 (window);
2368 }
2369
2370 /* Certain buffer names get special handling. */
2371 if (!NILP (Vspecial_display_function) && NILP (swp))
2372 {
2373 tem = Fspecial_display_p (XBUFFER (buffer)->name);
2374 if (EQ (tem, Qt))
2375 return call1 (Vspecial_display_function, buffer);
2376 if (CONSP (tem))
2377 return call2 (Vspecial_display_function, buffer, tem);
2378 }
2379
2380 /* If there are no frames open that have more than a minibuffer,
2381 we need to create a new frame. */
2382 if (pop_up_frames || last_nonminibuf_frame == 0)
2383 {
2384 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2385 Fset_window_buffer (window, buffer);
2386 return display_buffer_1 (window);
2387 }
2388
2389 if (pop_up_windows
2390 || FRAME_MINIBUF_ONLY_P (selected_frame)
2391 /* If the current frame is a special display frame,
2392 don't try to reuse its windows. */
2393 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->dedicated)
2394 )
2395 {
2396 Lisp_Object frames;
2397
2398 frames = Qnil;
2399 if (FRAME_MINIBUF_ONLY_P (selected_frame))
2400 XSETFRAME (frames, last_nonminibuf_frame);
2401 /* Don't try to create a window if would get an error */
2402 if (split_height_threshold < window_min_height << 1)
2403 split_height_threshold = window_min_height << 1;
2404
2405 /* Note that both Fget_largest_window and Fget_lru_window
2406 ignore minibuffers and dedicated windows.
2407 This means they can return nil. */
2408
2409 /* If the frame we would try to split cannot be split,
2410 try other frames. */
2411 if (FRAME_NO_SPLIT_P (NILP (frames) ? selected_frame
2412 : last_nonminibuf_frame))
2413 {
2414 /* Try visible frames first. */
2415 window = Fget_largest_window (Qvisible);
2416 /* If that didn't work, try iconified frames. */
2417 if (NILP (window))
2418 window = Fget_largest_window (make_number (0));
2419 if (NILP (window))
2420 window = Fget_largest_window (Qt);
2421 }
2422 else
2423 window = Fget_largest_window (frames);
2424
2425 /* If we got a tall enough full-width window that can be split,
2426 split it. */
2427 if (!NILP (window)
2428 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2429 && window_height (window) >= split_height_threshold
2430 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
2431 window = Fsplit_window (window, Qnil, Qnil);
2432 else
2433 {
2434 Lisp_Object upper, lower, other;
2435
2436 window = Fget_lru_window (frames);
2437 /* If the LRU window is selected, and big enough,
2438 and can be split, split it. */
2439 if (!NILP (window)
2440 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2441 && (EQ (window, selected_window)
2442 || EQ (XWINDOW (window)->parent, Qnil))
2443 && window_height (window) >= window_min_height << 1)
2444 window = Fsplit_window (window, Qnil, Qnil);
2445 /* If Fget_lru_window returned nil, try other approaches. */
2446
2447 /* Try visible frames first. */
2448 if (NILP (window))
2449 window = Fget_buffer_window (buffer, Qvisible);
2450 if (NILP (window))
2451 window = Fget_largest_window (Qvisible);
2452 /* If that didn't work, try iconified frames. */
2453 if (NILP (window))
2454 window = Fget_buffer_window (buffer, make_number (0));
2455 if (NILP (window))
2456 window = Fget_largest_window (make_number (0));
2457 /* Try invisible frames. */
2458 if (NILP (window))
2459 window = Fget_buffer_window (buffer, Qt);
2460 if (NILP (window))
2461 window = Fget_largest_window (Qt);
2462 /* As a last resort, make a new frame. */
2463 if (NILP (window))
2464 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2465 /* If window appears above or below another,
2466 even out their heights. */
2467 other = upper = lower = Qnil;
2468 if (!NILP (XWINDOW (window)->prev))
2469 other = upper = XWINDOW (window)->prev, lower = window;
2470 if (!NILP (XWINDOW (window)->next))
2471 other = lower = XWINDOW (window)->next, upper = window;
2472 if (!NILP (other)
2473 /* Check that OTHER and WINDOW are vertically arrayed. */
2474 && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
2475 && (XFASTINT (XWINDOW (other)->height)
2476 > XFASTINT (XWINDOW (window)->height)))
2477 {
2478 int total = (XFASTINT (XWINDOW (other)->height)
2479 + XFASTINT (XWINDOW (window)->height));
2480 Lisp_Object old_selected_window;
2481 old_selected_window = selected_window;
2482
2483 selected_window = upper;
2484 change_window_height ((total / 2
2485 - XFASTINT (XWINDOW (upper)->height)),
2486 0);
2487 selected_window = old_selected_window;
2488 }
2489 }
2490 }
2491 else
2492 window = Fget_lru_window (Qnil);
2493
2494 Fset_window_buffer (window, buffer);
2495 return display_buffer_1 (window);
2496 }
2497
2498 void
2499 temp_output_buffer_show (buf)
2500 register Lisp_Object buf;
2501 {
2502 register struct buffer *old = current_buffer;
2503 register Lisp_Object window;
2504 register struct window *w;
2505
2506 XBUFFER (buf)->directory = current_buffer->directory;
2507
2508 Fset_buffer (buf);
2509 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
2510 BEGV = BEG;
2511 ZV = Z;
2512 SET_PT (BEG);
2513 XBUFFER (buf)->clip_changed = 1;
2514 set_buffer_internal (old);
2515
2516 if (!EQ (Vtemp_buffer_show_function, Qnil))
2517 call1 (Vtemp_buffer_show_function, buf);
2518 else
2519 {
2520 window = Fdisplay_buffer (buf, Qnil, Qnil);
2521
2522 if (XFRAME (XWINDOW (window)->frame) != selected_frame)
2523 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2524 Vminibuf_scroll_window = window;
2525 w = XWINDOW (window);
2526 XSETFASTINT (w->hscroll, 0);
2527 set_marker_restricted_both (w->start, buf, 1, 1);
2528 set_marker_restricted_both (w->pointm, buf, 1, 1);
2529
2530 /* Run temp-buffer-show-hook, with the chosen window selected
2531 and it sbuffer current. */
2532 if (!NILP (Vrun_hooks))
2533 {
2534 Lisp_Object tem;
2535 tem = Fboundp (Qtemp_buffer_show_hook);
2536 if (!NILP (tem))
2537 {
2538 tem = Fsymbol_value (Qtemp_buffer_show_hook);
2539 if (!NILP (tem))
2540 {
2541 int count = specpdl_ptr - specpdl;
2542 Lisp_Object prev_window;
2543 prev_window = selected_window;
2544
2545 /* Select the window that was chosen, for running the hook. */
2546 record_unwind_protect (Fselect_window, prev_window);
2547 select_window_1 (window, 0);
2548 Fset_buffer (w->buffer);
2549 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
2550 select_window_1 (prev_window, 0);
2551 unbind_to (count, Qnil);
2552 }
2553 }
2554 }
2555 }
2556 }
2557 \f
2558 static void
2559 make_dummy_parent (window)
2560 Lisp_Object window;
2561 {
2562 Lisp_Object new;
2563 register struct window *o, *p;
2564 register struct Lisp_Vector *vec;
2565 int i;
2566
2567 o = XWINDOW (window);
2568 vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
2569 for (i = 0; i < VECSIZE (struct window); ++i)
2570 vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
2571 vec->size = VECSIZE (struct window);
2572 p = (struct window *)vec;
2573 XSETWINDOW (new, p);
2574
2575 XSETFASTINT (p->sequence_number, ++sequence_number);
2576
2577 /* Put new into window structure in place of window */
2578 replace_window (window, new);
2579
2580 o->next = Qnil;
2581 o->prev = Qnil;
2582 o->vchild = Qnil;
2583 o->hchild = Qnil;
2584 o->parent = new;
2585
2586 p->start = Qnil;
2587 p->pointm = Qnil;
2588 p->buffer = Qnil;
2589 }
2590
2591 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
2592 "Split WINDOW, putting SIZE lines in the first of the pair.\n\
2593 WINDOW defaults to selected one and SIZE to half its size.\n\
2594 If optional third arg HORFLAG is non-nil, split side by side\n\
2595 and put SIZE columns in the first of the pair. In that case,\n\
2596 SIZE includes that window's scroll bar, or the divider column to its right.")
2597 (window, size, horflag)
2598 Lisp_Object window, size, horflag;
2599 {
2600 register Lisp_Object new;
2601 register struct window *o, *p;
2602 FRAME_PTR fo;
2603 register int size_int;
2604
2605 if (NILP (window))
2606 window = selected_window;
2607 else
2608 CHECK_LIVE_WINDOW (window, 0);
2609
2610 o = XWINDOW (window);
2611 fo = XFRAME (WINDOW_FRAME (o));
2612
2613 if (NILP (size))
2614 {
2615 if (!NILP (horflag))
2616 /* Calculate the size of the left-hand window, by dividing
2617 the usable space in columns by two.
2618 We round up, since the left-hand window may include
2619 a dividing line, while the right-hand may not. */
2620 size_int = (XFASTINT (o->width) + 1) >> 1;
2621 else
2622 size_int = XFASTINT (o->height) >> 1;
2623 }
2624 else
2625 {
2626 CHECK_NUMBER (size, 1);
2627 size_int = XINT (size);
2628 }
2629
2630 if (MINI_WINDOW_P (o))
2631 error ("Attempt to split minibuffer window");
2632
2633 check_min_window_sizes ();
2634
2635 if (NILP (horflag))
2636 {
2637 if (size_int < window_min_height)
2638 error ("Window height %d too small (after splitting)", size_int);
2639 if (size_int + window_min_height > XFASTINT (o->height))
2640 error ("Window height %d too small (after splitting)",
2641 XFASTINT (o->height) - size_int);
2642 if (NILP (o->parent)
2643 || NILP (XWINDOW (o->parent)->vchild))
2644 {
2645 make_dummy_parent (window);
2646 new = o->parent;
2647 XWINDOW (new)->vchild = window;
2648 }
2649 }
2650 else
2651 {
2652 if (size_int < window_min_width)
2653 error ("Window width %d too small (after splitting)", size_int);
2654
2655 if (size_int + window_min_width > XFASTINT (o->width))
2656 error ("Window width %d too small (after splitting)",
2657 XFASTINT (o->width) - size_int);
2658 if (NILP (o->parent)
2659 || NILP (XWINDOW (o->parent)->hchild))
2660 {
2661 make_dummy_parent (window);
2662 new = o->parent;
2663 XWINDOW (new)->hchild = window;
2664 }
2665 }
2666
2667 /* Now we know that window's parent is a vertical combination
2668 if we are dividing vertically, or a horizontal combination
2669 if we are making side-by-side windows */
2670
2671 windows_or_buffers_changed++;
2672 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
2673 new = make_window ();
2674 p = XWINDOW (new);
2675
2676 p->frame = o->frame;
2677 p->next = o->next;
2678 if (!NILP (p->next))
2679 XWINDOW (p->next)->prev = new;
2680 p->prev = window;
2681 o->next = new;
2682 p->parent = o->parent;
2683 p->buffer = Qt;
2684 p->window_end_valid = Qnil;
2685 bzero (&p->last_cursor, sizeof p->last_cursor);
2686
2687 /* Apportion the available frame space among the two new windows */
2688
2689 if (!NILP (horflag))
2690 {
2691 p->height = o->height;
2692 p->top = o->top;
2693 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
2694 XSETFASTINT (o->width, size_int);
2695 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
2696 }
2697 else
2698 {
2699 p->left = o->left;
2700 p->width = o->width;
2701 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
2702 XSETFASTINT (o->height, size_int);
2703 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
2704 }
2705
2706 /* Adjust glyph matrices. */
2707 adjust_glyphs (fo);
2708 Fset_window_buffer (new, o->buffer);
2709 return new;
2710 }
2711 \f
2712 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
2713 "Make current window ARG lines bigger.\n\
2714 From program, optional second arg non-nil means grow sideways ARG columns.")
2715 (arg, side)
2716 register Lisp_Object arg, side;
2717 {
2718 CHECK_NUMBER (arg, 0);
2719 change_window_height (XINT (arg), !NILP (side));
2720
2721 if (! NILP (Vwindow_configuration_change_hook))
2722 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2723
2724 return Qnil;
2725 }
2726
2727 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
2728 "Make current window ARG lines smaller.\n\
2729 From program, optional second arg non-nil means shrink sideways arg columns.")
2730 (arg, side)
2731 register Lisp_Object arg, side;
2732 {
2733 CHECK_NUMBER (arg, 0);
2734 change_window_height (-XINT (arg), !NILP (side));
2735
2736 if (! NILP (Vwindow_configuration_change_hook))
2737 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2738
2739 return Qnil;
2740 }
2741
2742 int
2743 window_height (window)
2744 Lisp_Object window;
2745 {
2746 register struct window *p = XWINDOW (window);
2747 return XFASTINT (p->height);
2748 }
2749
2750 int
2751 window_width (window)
2752 Lisp_Object window;
2753 {
2754 register struct window *p = XWINDOW (window);
2755 return XFASTINT (p->width);
2756 }
2757
2758 #define MINSIZE(w) \
2759 (widthflag \
2760 ? window_min_width \
2761 : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
2762
2763 #define CURBEG(w) \
2764 *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
2765
2766 #define CURSIZE(w) \
2767 *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
2768
2769 /* Unlike set_window_height, this function
2770 also changes the heights of the siblings so as to
2771 keep everything consistent. */
2772
2773 void
2774 change_window_height (delta, widthflag)
2775 register int delta;
2776 int widthflag;
2777 {
2778 register Lisp_Object parent;
2779 Lisp_Object window;
2780 register struct window *p;
2781 int *sizep;
2782 int (*sizefun) P_ ((Lisp_Object))
2783 = widthflag ? window_width : window_height;
2784 register void (*setsizefun) P_ ((Lisp_Object, int, int))
2785 = (widthflag ? set_window_width : set_window_height);
2786 int maximum;
2787 Lisp_Object next, prev;
2788
2789 check_min_window_sizes ();
2790
2791 window = selected_window;
2792 while (1)
2793 {
2794 p = XWINDOW (window);
2795 parent = p->parent;
2796 if (NILP (parent))
2797 {
2798 if (widthflag)
2799 error ("No other window to side of this one");
2800 break;
2801 }
2802 if (widthflag ? !NILP (XWINDOW (parent)->hchild)
2803 : !NILP (XWINDOW (parent)->vchild))
2804 break;
2805 window = parent;
2806 }
2807
2808 sizep = &CURSIZE (window);
2809
2810 {
2811 register int maxdelta;
2812
2813 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
2814 : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
2815 : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
2816 /* This is a frame with only one window, a minibuffer-only
2817 or a minibufferless frame. */
2818 : (delta = 0));
2819
2820 if (delta > maxdelta)
2821 /* This case traps trying to make the minibuffer
2822 the full frame, or make the only window aside from the
2823 minibuffer the full frame. */
2824 delta = maxdelta;
2825 }
2826
2827 if (*sizep + delta < MINSIZE (window))
2828 {
2829 delete_window (window);
2830 return;
2831 }
2832
2833 if (delta == 0)
2834 return;
2835
2836 /* Find the total we can get from other siblings. */
2837 maximum = 0;
2838 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
2839 maximum += (*sizefun) (next) - MINSIZE (next);
2840 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
2841 maximum += (*sizefun) (prev) - MINSIZE (prev);
2842
2843 /* If we can get it all from them, do so. */
2844 if (delta <= maximum)
2845 {
2846 Lisp_Object first_unaffected;
2847 Lisp_Object first_affected;
2848
2849 next = p->next;
2850 prev = p->prev;
2851 first_affected = window;
2852 /* Look at one sibling at a time,
2853 moving away from this window in both directions alternately,
2854 and take as much as we can get without deleting that sibling. */
2855 while (delta != 0)
2856 {
2857 if (delta == 0)
2858 break;
2859 if (! NILP (next))
2860 {
2861 int this_one = (*sizefun) (next) - MINSIZE (next);
2862 if (this_one > delta)
2863 this_one = delta;
2864
2865 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
2866 (*setsizefun) (window, *sizep + this_one, 0);
2867
2868 delta -= this_one;
2869 next = XWINDOW (next)->next;
2870 }
2871 if (delta == 0)
2872 break;
2873 if (! NILP (prev))
2874 {
2875 int this_one = (*sizefun) (prev) - MINSIZE (prev);
2876 if (this_one > delta)
2877 this_one = delta;
2878
2879 first_affected = prev;
2880
2881 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
2882 (*setsizefun) (window, *sizep + this_one, 0);
2883
2884 delta -= this_one;
2885 prev = XWINDOW (prev)->prev;
2886 }
2887 }
2888
2889 /* Now recalculate the edge positions of all the windows affected,
2890 based on the new sizes. */
2891 first_unaffected = next;
2892 prev = first_affected;
2893 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
2894 prev = next, next = XWINDOW (next)->next)
2895 {
2896 CURBEG (next) = CURBEG (prev) + (*sizefun) (prev);
2897 /* This does not change size of NEXT,
2898 but it propagates the new top edge to its children */
2899 (*setsizefun) (next, (*sizefun) (next), 0);
2900 }
2901 }
2902 else
2903 {
2904 register int delta1;
2905 register int opht = (*sizefun) (parent);
2906
2907 /* If trying to grow this window to or beyond size of the parent,
2908 make delta1 so big that, on shrinking back down,
2909 all the siblings end up with less than one line and are deleted. */
2910 if (opht <= *sizep + delta)
2911 delta1 = opht * opht * 2;
2912 /* Otherwise, make delta1 just right so that if we add delta1
2913 lines to this window and to the parent, and then shrink
2914 the parent back to its original size, the new proportional
2915 size of this window will increase by delta. */
2916 else
2917 delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
2918
2919 /* Add delta1 lines or columns to this window, and to the parent,
2920 keeping things consistent while not affecting siblings. */
2921 CURSIZE (parent) = opht + delta1;
2922 (*setsizefun) (window, *sizep + delta1, 0);
2923
2924 /* Squeeze out delta1 lines or columns from our parent,
2925 shriking this window and siblings proportionately.
2926 This brings parent back to correct size.
2927 Delta1 was calculated so this makes this window the desired size,
2928 taking it all out of the siblings. */
2929 (*setsizefun) (parent, opht, 0);
2930 }
2931
2932 XSETFASTINT (p->last_modified, 0);
2933 XSETFASTINT (p->last_overlay_modified, 0);
2934
2935 /* Adjust glyph matrices. */
2936 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
2937 }
2938 #undef MINSIZE
2939 #undef CURBEG
2940 #undef CURSIZE
2941
2942
2943 /* Mark window cursors off for all windows in the window tree rooted
2944 at W by setting their phys_cursor_on_p flag to zero. Called from
2945 xterm.c, e.g. when a frame is cleared and thereby all cursors on
2946 the frame are cleared. */
2947
2948 void
2949 mark_window_cursors_off (w)
2950 struct window *w;
2951 {
2952 while (w)
2953 {
2954 if (!NILP (w->hchild))
2955 mark_window_cursors_off (XWINDOW (w->hchild));
2956 else if (!NILP (w->vchild))
2957 mark_window_cursors_off (XWINDOW (w->vchild));
2958 else
2959 w->phys_cursor_on_p = 0;
2960
2961 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2962 }
2963 }
2964
2965
2966 /* Return number of lines of text (not counting mode line) in W. */
2967
2968 int
2969 window_internal_height (w)
2970 struct window *w;
2971 {
2972 int ht = XFASTINT (w->height);
2973
2974 if (MINI_WINDOW_P (w))
2975 return ht;
2976
2977 if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
2978 || !NILP (w->next) || !NILP (w->prev)
2979 || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
2980 return ht - 1;
2981
2982 return ht;
2983 }
2984
2985
2986 /* Return the number of columns in W.
2987 Don't count columns occupied by scroll bars or the vertical bar
2988 separating W from the sibling to its right. */
2989
2990 int
2991 window_internal_width (w)
2992 struct window *w;
2993 {
2994 struct frame *f = XFRAME (WINDOW_FRAME (w));
2995 int width = XINT (w->width);
2996
2997 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2998 /* Scroll bars occupy a few columns. */
2999 width -= FRAME_SCROLL_BAR_COLS (f);
3000 else if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
3001 /* The column of `|' characters separating side-by-side windows
3002 occupies one column only. */
3003 width -= 1;
3004
3005 /* On window-systems, areas to the left and right of the window
3006 are used to display bitmaps there. */
3007 if (FRAME_WINDOW_P (f))
3008 width -= 2 * FRAME_FLAGS_AREA_COLS (f);
3009
3010 return width;
3011 }
3012
3013 \f
3014 /************************************************************************
3015 Window Scrolling
3016 ***********************************************************************/
3017
3018 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
3019 one screen-full, which is defined as the height of the window minus
3020 next_screen_context_lines. If WHOLE is zero, scroll up N lines
3021 instead. Negative values of N mean scroll down. NOERROR non-zero
3022 means don't signal an error if we try to move over BEGV or ZV,
3023 respectively. */
3024
3025 static void
3026 window_scroll (window, n, whole, noerror)
3027 Lisp_Object window;
3028 int n;
3029 int whole;
3030 int noerror;
3031 {
3032 /* If we must, use the pixel-based version which is much slower than
3033 the line-based one but can handle varying line heights. */
3034 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
3035 window_scroll_pixel_based (window, n, whole, noerror);
3036 else
3037 window_scroll_line_based (window, n, whole, noerror);
3038 }
3039
3040
3041 /* Implementation of window_scroll that works based on pixel line
3042 heights. See the comment of window_scroll for parameter
3043 descriptions. */
3044
3045 static void
3046 window_scroll_pixel_based (window, n, whole, noerror)
3047 Lisp_Object window;
3048 int n;
3049 int whole;
3050 int noerror;
3051 {
3052 struct it it;
3053 struct window *w = XWINDOW (window);
3054 struct text_pos start;
3055 Lisp_Object tem;
3056 int this_scroll_margin;
3057 int preserve_y;
3058
3059 SET_TEXT_POS_FROM_MARKER (start, w->start);
3060
3061 /* If PT is not visible in WINDOW, move back one half of
3062 the screen. */
3063 XSETFASTINT (tem, PT);
3064 tem = Fpos_visible_in_window_p (tem, window);
3065 if (NILP (tem))
3066 {
3067 /* Move backward half the height of the window. Performance note:
3068 vmotion used here is about 10% faster, but would give wrong
3069 results for variable height lines. */
3070 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
3071 it.current_y = it.last_visible_y;
3072 move_it_vertically (&it, -it.last_visible_y / 2);
3073
3074 /* The function move_iterator_vertically may move over more than
3075 the specified y-distance. If it->w is small, e.g. a
3076 mini-buffer window, we may end up in front of the window's
3077 display area. This is the case when Start displaying at the
3078 start of the line containing PT in this case. */
3079 if (it.current_y <= 0)
3080 {
3081 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
3082 move_it_vertically (&it, 0);
3083 it.current_y = 0;
3084 }
3085
3086 start = it.current.pos;
3087 }
3088
3089 /* If scroll_preserve_screen_position is non-zero, we try to set
3090 point in the same window line as it is now, so get that line. */
3091 if (!NILP (Vscroll_preserve_screen_position))
3092 {
3093 start_display (&it, w, start);
3094 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
3095 preserve_y = it.current_y;
3096 }
3097 else
3098 preserve_y = -1;
3099
3100 /* Move iterator it from start the specified distance forward or
3101 backward. The result is the new window start. */
3102 start_display (&it, w, start);
3103 if (whole)
3104 {
3105 int screen_full = (it.last_visible_y
3106 - next_screen_context_lines * CANON_Y_UNIT (it.f));
3107 int direction = n < 0 ? -1 : 1;
3108 move_it_vertically (&it, direction * screen_full);
3109 }
3110 else
3111 move_it_by_lines (&it, n, 1);
3112
3113 /* End if we end up at ZV or BEGV. */
3114 if ((n > 0 && IT_CHARPOS (it) == ZV)
3115 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
3116 {
3117 if (noerror)
3118 return;
3119 else if (IT_CHARPOS (it) == ZV)
3120 Fsignal (Qend_of_buffer, Qnil);
3121 else
3122 Fsignal (Qbeginning_of_buffer, Qnil);
3123 }
3124
3125 /* Set the window start, and set up the window for redisplay. */
3126 set_marker_restricted (w->start, make_number (IT_CHARPOS (it)), w->buffer);
3127 w->start_at_line_beg = Fbolp ();
3128 w->update_mode_line = Qt;
3129 XSETFASTINT (w->last_modified, 0);
3130 XSETFASTINT (w->last_overlay_modified, 0);
3131 /* Set force_start so that redisplay_window will run the
3132 window-scroll-functions. */
3133 w->force_start = Qt;
3134
3135 it.current_y = it.vpos = 0;
3136
3137 /* Preserve the screen position if we must. */
3138 if (preserve_y >= 0)
3139 {
3140 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
3141 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3142 }
3143 else
3144 {
3145 /* Move PT out of scroll margins. */
3146 this_scroll_margin = max (0, scroll_margin);
3147 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
3148 this_scroll_margin *= CANON_Y_UNIT (it.f);
3149
3150 if (n > 0)
3151 {
3152 /* We moved the window start towards ZV, so PT may be now
3153 in the scroll margin at the top. */
3154 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
3155 while (it.current_y < this_scroll_margin)
3156 move_it_by_lines (&it, 1, 1);
3157 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3158 }
3159 else if (n < 0)
3160 {
3161 /* We moved the window start towards BEGV, so PT may be now
3162 in the scroll margin at the bottom. */
3163 move_it_to (&it, PT, -1,
3164 it.last_visible_y - this_scroll_margin - 1, -1,
3165 MOVE_TO_POS | MOVE_TO_Y);
3166
3167 /* Don't put point on a partially visible line at the end. */
3168 if (it.current_y + it.max_ascent + it.max_descent
3169 > it.last_visible_y)
3170 move_it_by_lines (&it, -1, 0);
3171
3172 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3173 }
3174 }
3175 }
3176
3177
3178 /* Implementation of window_scroll that works based on screen lines.
3179 See the comment of window_scroll for parameter descriptions. */
3180
3181 static void
3182 window_scroll_line_based (window, n, whole, noerror)
3183 Lisp_Object window;
3184 int n;
3185 int whole;
3186 int noerror;
3187 {
3188 register struct window *w = XWINDOW (window);
3189 register int opoint = PT, opoint_byte = PT_BYTE;
3190 register int pos, pos_byte;
3191 register int ht = window_internal_height (w);
3192 register Lisp_Object tem;
3193 int lose;
3194 Lisp_Object bolp;
3195 int startpos;
3196 struct position posit;
3197 int original_vpos;
3198
3199 startpos = marker_position (w->start);
3200
3201 posit = *compute_motion (startpos, 0, 0, 0,
3202 PT, ht, 0,
3203 window_internal_width (w), XINT (w->hscroll),
3204 0, w);
3205 original_vpos = posit.vpos;
3206
3207 XSETFASTINT (tem, PT);
3208 tem = Fpos_visible_in_window_p (tem, window);
3209
3210 if (NILP (tem))
3211 {
3212 Fvertical_motion (make_number (- (ht / 2)), window);
3213 startpos = PT;
3214 }
3215
3216 SET_PT (startpos);
3217 lose = n < 0 && PT == BEGV;
3218 Fvertical_motion (make_number (n), window);
3219 pos = PT;
3220 pos_byte = PT_BYTE;
3221 bolp = Fbolp ();
3222 SET_PT_BOTH (opoint, opoint_byte);
3223
3224 if (lose)
3225 {
3226 if (noerror)
3227 return;
3228 else
3229 Fsignal (Qbeginning_of_buffer, Qnil);
3230 }
3231
3232 if (pos < ZV)
3233 {
3234 int this_scroll_margin = scroll_margin;
3235
3236 /* Don't use a scroll margin that is negative or too large. */
3237 if (this_scroll_margin < 0)
3238 this_scroll_margin = 0;
3239
3240 if (XINT (w->height) < 4 * scroll_margin)
3241 this_scroll_margin = XINT (w->height) / 4;
3242
3243 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
3244 w->start_at_line_beg = bolp;
3245 w->update_mode_line = Qt;
3246 XSETFASTINT (w->last_modified, 0);
3247 XSETFASTINT (w->last_overlay_modified, 0);
3248 /* Set force_start so that redisplay_window will run
3249 the window-scroll-functions. */
3250 w->force_start = Qt;
3251
3252 if (whole && !NILP (Vscroll_preserve_screen_position))
3253 {
3254 SET_PT_BOTH (pos, pos_byte);
3255 Fvertical_motion (make_number (original_vpos), window);
3256 }
3257 /* If we scrolled forward, put point enough lines down
3258 that it is outside the scroll margin. */
3259 else if (n > 0)
3260 {
3261 int top_margin;
3262
3263 if (this_scroll_margin > 0)
3264 {
3265 SET_PT_BOTH (pos, pos_byte);
3266 Fvertical_motion (make_number (this_scroll_margin), window);
3267 top_margin = PT;
3268 }
3269 else
3270 top_margin = pos;
3271
3272 if (top_margin <= opoint)
3273 SET_PT_BOTH (opoint, opoint_byte);
3274 else if (!NILP (Vscroll_preserve_screen_position))
3275 {
3276 SET_PT_BOTH (pos, pos_byte);
3277 Fvertical_motion (make_number (original_vpos), window);
3278 }
3279 else
3280 SET_PT (top_margin);
3281 }
3282 else if (n < 0)
3283 {
3284 int bottom_margin;
3285
3286 /* If we scrolled backward, put point near the end of the window
3287 but not within the scroll margin. */
3288 SET_PT_BOTH (pos, pos_byte);
3289 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
3290 if (XFASTINT (tem) == ht - this_scroll_margin)
3291 bottom_margin = PT;
3292 else
3293 bottom_margin = PT + 1;
3294
3295 if (bottom_margin > opoint)
3296 SET_PT_BOTH (opoint, opoint_byte);
3297 else
3298 {
3299 if (!NILP (Vscroll_preserve_screen_position))
3300 {
3301 SET_PT_BOTH (pos, pos_byte);
3302 Fvertical_motion (make_number (original_vpos), window);
3303 }
3304 else
3305 Fvertical_motion (make_number (-1), window);
3306 }
3307 }
3308 }
3309 else
3310 {
3311 if (noerror)
3312 return;
3313 else
3314 Fsignal (Qend_of_buffer, Qnil);
3315 }
3316 }
3317
3318
3319 /* Scroll selected_window up or down. If N is nil, scroll a
3320 screen-full which is defined as the height of the window minus
3321 next_screen_context_lines. If N is the symbol `-', scroll.
3322 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
3323 up. This is the guts of Fscroll_up and Fscroll_down. */
3324
3325 static void
3326 scroll_command (n, direction)
3327 Lisp_Object n;
3328 int direction;
3329 {
3330 register int defalt;
3331 int count = specpdl_ptr - specpdl;
3332
3333 xassert (abs (direction) == 1);
3334
3335 /* If selected window's buffer isn't current, make it current for
3336 the moment. But don't screw up if window_scroll gets an error. */
3337 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
3338 {
3339 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3340 Fset_buffer (XWINDOW (selected_window)->buffer);
3341
3342 /* Make redisplay consider other windows than just selected_window. */
3343 ++windows_or_buffers_changed;
3344 }
3345
3346 defalt = (window_internal_height (XWINDOW (selected_window))
3347 - next_screen_context_lines);
3348 defalt = direction * (defalt < 1 ? 1 : defalt);
3349
3350 if (NILP (n))
3351 window_scroll (selected_window, defalt, 1, 0);
3352 else if (EQ (n, Qminus))
3353 window_scroll (selected_window, - defalt, 1, 0);
3354 else
3355 {
3356 n = Fprefix_numeric_value (n);
3357 window_scroll (selected_window, XINT (n) * direction, 0, 0);
3358 }
3359
3360 unbind_to (count, Qnil);
3361 }
3362
3363 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
3364 "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
3365 A near full screen is `next-screen-context-lines' less than a full screen.\n\
3366 Negative ARG means scroll downward.\n\
3367 If ARG is the atom `-', scroll downward by nearly full screen.\n\
3368 When calling from a program, supply as argument a number, nil, or `-'.")
3369 (arg)
3370 Lisp_Object arg;
3371 {
3372 scroll_command (arg, 1);
3373 return Qnil;
3374 }
3375
3376 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
3377 "Scroll text of current window down ARG lines; or near full screen if no ARG.\n\
3378 A near full screen is `next-screen-context-lines' less than a full screen.\n\
3379 Negative ARG means scroll upward.\n\
3380 If ARG is the atom `-', scroll upward by nearly full screen.\n\
3381 When calling from a program, supply as argument a number, nil, or `-'.")
3382 (arg)
3383 Lisp_Object arg;
3384 {
3385 scroll_command (arg, -1);
3386 return Qnil;
3387 }
3388 \f
3389 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
3390 "Return the other window for \"other window scroll\" commands.\n\
3391 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
3392 specifies the window.\n\
3393 If `other-window-scroll-buffer' is non-nil, a window\n\
3394 showing that buffer is used.")
3395 ()
3396 {
3397 Lisp_Object window;
3398
3399 if (MINI_WINDOW_P (XWINDOW (selected_window))
3400 && !NILP (Vminibuf_scroll_window))
3401 window = Vminibuf_scroll_window;
3402 /* If buffer is specified, scroll that buffer. */
3403 else if (!NILP (Vother_window_scroll_buffer))
3404 {
3405 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
3406 if (NILP (window))
3407 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
3408 }
3409 else
3410 {
3411 /* Nothing specified; look for a neighboring window on the same
3412 frame. */
3413 window = Fnext_window (selected_window, Qnil, Qnil);
3414
3415 if (EQ (window, selected_window))
3416 /* That didn't get us anywhere; look for a window on another
3417 visible frame. */
3418 do
3419 window = Fnext_window (window, Qnil, Qt);
3420 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
3421 && ! EQ (window, selected_window));
3422 }
3423
3424 CHECK_LIVE_WINDOW (window, 0);
3425
3426 if (EQ (window, selected_window))
3427 error ("There is no other window");
3428
3429 return window;
3430 }
3431
3432 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
3433 "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
3434 A near full screen is `next-screen-context-lines' less than a full screen.\n\
3435 The next window is the one below the current one; or the one at the top\n\
3436 if the current one is at the bottom. Negative ARG means scroll downward.\n\
3437 If ARG is the atom `-', scroll downward by nearly full screen.\n\
3438 When calling from a program, supply as argument a number, nil, or `-'.\n\
3439 \n\
3440 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
3441 specifies the window to scroll.\n\
3442 If `other-window-scroll-buffer' is non-nil, scroll the window\n\
3443 showing that buffer, popping the buffer up if necessary.")
3444 (arg)
3445 register Lisp_Object arg;
3446 {
3447 register Lisp_Object window;
3448 register int defalt;
3449 register struct window *w;
3450 register int count = specpdl_ptr - specpdl;
3451
3452 window = Fother_window_for_scrolling ();
3453
3454 w = XWINDOW (window);
3455 defalt = window_internal_height (w) - next_screen_context_lines;
3456 if (defalt < 1) defalt = 1;
3457
3458 /* Don't screw up if window_scroll gets an error. */
3459 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3460 ++windows_or_buffers_changed;
3461
3462 Fset_buffer (w->buffer);
3463 SET_PT (marker_position (w->pointm));
3464
3465 if (NILP (arg))
3466 window_scroll (window, defalt, 1, 1);
3467 else if (EQ (arg, Qminus))
3468 window_scroll (window, -defalt, 1, 1);
3469 else
3470 {
3471 if (CONSP (arg))
3472 arg = Fcar (arg);
3473 CHECK_NUMBER (arg, 0);
3474 window_scroll (window, XINT (arg), 0, 1);
3475 }
3476
3477 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
3478 unbind_to (count, Qnil);
3479
3480 return Qnil;
3481 }
3482 \f
3483 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
3484 "Scroll selected window display ARG columns left.\n\
3485 Default for ARG is window width minus 2.")
3486 (arg)
3487 register Lisp_Object arg;
3488 {
3489
3490 if (NILP (arg))
3491 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3492 else
3493 arg = Fprefix_numeric_value (arg);
3494
3495 return
3496 Fset_window_hscroll (selected_window,
3497 make_number (XINT (XWINDOW (selected_window)->hscroll)
3498 + XINT (arg)));
3499 }
3500
3501 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
3502 "Scroll selected window display ARG columns right.\n\
3503 Default for ARG is window width minus 2.")
3504 (arg)
3505 register Lisp_Object arg;
3506 {
3507 if (NILP (arg))
3508 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3509 else
3510 arg = Fprefix_numeric_value (arg);
3511
3512 return
3513 Fset_window_hscroll (selected_window,
3514 make_number (XINT (XWINDOW (selected_window)->hscroll)
3515 - XINT (arg)));
3516 }
3517
3518 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
3519 "Center point in window and redisplay frame. With ARG, put point on line ARG.\n\
3520 The desired position of point is always relative to the current window.\n\
3521 Just C-u as prefix means put point in the center of the window.\n\
3522 If ARG is omitted or nil, erases the entire frame and then\n\
3523 redraws with point in the center of the current window.")
3524 (arg)
3525 register Lisp_Object arg;
3526 {
3527 register struct window *w = XWINDOW (selected_window);
3528 register int ht = window_internal_height (w);
3529 struct position pos;
3530 struct buffer *buf = XBUFFER (w->buffer);
3531 struct buffer *obuf = current_buffer;
3532
3533 if (NILP (arg))
3534 {
3535 extern int frame_garbaged;
3536
3537 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
3538 XSETFASTINT (arg, ht / 2);
3539 }
3540 else if (CONSP (arg)) /* Just C-u. */
3541 {
3542 XSETFASTINT (arg, ht / 2);
3543 }
3544 else
3545 {
3546 arg = Fprefix_numeric_value (arg);
3547 CHECK_NUMBER (arg, 0);
3548 }
3549
3550 if (XINT (arg) < 0)
3551 XSETINT (arg, XINT (arg) + ht);
3552
3553 set_buffer_internal (buf);
3554 pos = *vmotion (PT, - XINT (arg), w);
3555
3556 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
3557 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
3558 || FETCH_BYTE (pos.bytepos - 1) == '\n')
3559 ? Qt : Qnil);
3560 w->force_start = Qt;
3561 set_buffer_internal (obuf);
3562
3563 return Qnil;
3564 }
3565 \f
3566 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
3567 1, 1, "P",
3568 "Position point relative to window.\n\
3569 With no argument, position point at center of window.\n\
3570 An argument specifies vertical position within the window;\n\
3571 zero means top of window, negative means relative to bottom of window.")
3572 (arg)
3573 register Lisp_Object arg;
3574 {
3575 register struct window *w = XWINDOW (selected_window);
3576 register int height = window_internal_height (w);
3577 register int start;
3578 Lisp_Object window;
3579
3580 if (NILP (arg))
3581 XSETFASTINT (arg, height / 2);
3582 else
3583 {
3584 arg = Fprefix_numeric_value (arg);
3585 if (XINT (arg) < 0)
3586 XSETINT (arg, XINT (arg) + height);
3587 }
3588
3589 start = marker_position (w->start);
3590 XSETWINDOW (window, w);
3591 if (start < BEGV || start > ZV)
3592 {
3593 Fvertical_motion (make_number (- (height / 2)), window);
3594 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
3595 w->start_at_line_beg = Fbolp ();
3596 w->force_start = Qt;
3597 }
3598 else
3599 Fgoto_char (w->start);
3600
3601 return Fvertical_motion (arg, window);
3602 }
3603
3604
3605 \f
3606 /***********************************************************************
3607 Window Configuration
3608 ***********************************************************************/
3609
3610 struct save_window_data
3611 {
3612 EMACS_INT size_from_Lisp_Vector_struct;
3613 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3614 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
3615 Lisp_Object frame_toolbar_lines;
3616 Lisp_Object selected_frame;
3617 Lisp_Object current_window;
3618 Lisp_Object current_buffer;
3619 Lisp_Object minibuf_scroll_window;
3620 Lisp_Object root_window;
3621 Lisp_Object focus_frame;
3622 /* Record the values of window-min-width and window-min-height
3623 so that window sizes remain consistent with them. */
3624 Lisp_Object min_width, min_height;
3625 /* A vector, each of whose elements is a struct saved_window
3626 for one window. */
3627 Lisp_Object saved_windows;
3628 };
3629
3630 /* This is saved as a Lisp_Vector */
3631 struct saved_window
3632 {
3633 /* these first two must agree with struct Lisp_Vector in lisp.h */
3634 EMACS_INT size_from_Lisp_Vector_struct;
3635 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3636
3637 Lisp_Object window;
3638 Lisp_Object buffer, start, pointm, mark;
3639 Lisp_Object left, top, width, height, hscroll;
3640 Lisp_Object parent, prev;
3641 Lisp_Object start_at_line_beg;
3642 Lisp_Object display_table;
3643 };
3644 #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
3645
3646 #define SAVED_WINDOW_N(swv,n) \
3647 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
3648
3649 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
3650 "Return t if OBJECT is a window-configuration object.")
3651 (object)
3652 Lisp_Object object;
3653 {
3654 if (WINDOW_CONFIGURATIONP (object))
3655 return Qt;
3656 return Qnil;
3657 }
3658
3659 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
3660 "Return the frame that CONFIG, a window-configuration object, is about.")
3661 (config)
3662 Lisp_Object config;
3663 {
3664 register struct save_window_data *data;
3665 struct Lisp_Vector *saved_windows;
3666
3667 if (! WINDOW_CONFIGURATIONP (config))
3668 wrong_type_argument (Qwindow_configuration_p, config);
3669
3670 data = (struct save_window_data *) XVECTOR (config);
3671 saved_windows = XVECTOR (data->saved_windows);
3672 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3673 }
3674
3675 DEFUN ("set-window-configuration", Fset_window_configuration,
3676 Sset_window_configuration, 1, 1, 0,
3677 "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
3678 CONFIGURATION must be a value previously returned\n\
3679 by `current-window-configuration' (which see).\n\
3680 If CONFIGURATION was made from a frame that is now deleted,\n\
3681 only frame-independent values can be restored. In this case,\n\
3682 the return value is nil. Otherwise the value is t.")
3683 (configuration)
3684 Lisp_Object configuration;
3685 {
3686 register struct save_window_data *data;
3687 struct Lisp_Vector *saved_windows;
3688 Lisp_Object new_current_buffer;
3689 Lisp_Object frame;
3690 FRAME_PTR f;
3691 int old_point = -1;
3692
3693 while (!WINDOW_CONFIGURATIONP (configuration))
3694 wrong_type_argument (Qwindow_configuration_p, configuration);
3695
3696 data = (struct save_window_data *) XVECTOR (configuration);
3697 saved_windows = XVECTOR (data->saved_windows);
3698
3699 new_current_buffer = data->current_buffer;
3700 if (NILP (XBUFFER (new_current_buffer)->name))
3701 new_current_buffer = Qnil;
3702 else
3703 {
3704 if (XBUFFER (new_current_buffer) == current_buffer)
3705 old_point = PT;
3706
3707 }
3708
3709 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3710 f = XFRAME (frame);
3711
3712 /* If f is a dead frame, don't bother rebuilding its window tree.
3713 However, there is other stuff we should still try to do below. */
3714 if (FRAME_LIVE_P (f))
3715 {
3716 register struct window *w;
3717 register struct saved_window *p;
3718 struct window *root_window;
3719 struct window **leaf_windows;
3720 int n_leaf_windows;
3721 int k, i;
3722
3723 /* If the frame has been resized since this window configuration was
3724 made, we change the frame to the size specified in the
3725 configuration, restore the configuration, and then resize it
3726 back. We keep track of the prevailing height in these variables. */
3727 int previous_frame_height = FRAME_HEIGHT (f);
3728 int previous_frame_width = FRAME_WIDTH (f);
3729 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
3730 int previous_frame_toolbar_lines = FRAME_TOOLBAR_LINES (f);
3731
3732 /* The mouse highlighting code could get screwed up
3733 if it runs during this. */
3734 BLOCK_INPUT;
3735
3736 if (XFASTINT (data->frame_height) != previous_frame_height
3737 || XFASTINT (data->frame_width) != previous_frame_width)
3738 change_frame_size (f, XFASTINT (data->frame_height),
3739 XFASTINT (data->frame_width), 0, 0);
3740 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3741 if (XFASTINT (data->frame_menu_bar_lines)
3742 != previous_frame_menu_bar_lines)
3743 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
3744 #ifdef HAVE_WINDOW_SYSTEM
3745 if (XFASTINT (data->frame_toolbar_lines)
3746 != previous_frame_toolbar_lines)
3747 x_set_toolbar_lines (f, data->frame_toolbar_lines, make_number (0));
3748 #endif
3749 #endif
3750
3751 if (! NILP (XWINDOW (selected_window)->buffer))
3752 {
3753 w = XWINDOW (selected_window);
3754 set_marker_both (w->pointm,
3755 w->buffer,
3756 BUF_PT (XBUFFER (w->buffer)),
3757 BUF_PT_BYTE (XBUFFER (w->buffer)));
3758 }
3759
3760 windows_or_buffers_changed++;
3761 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3762
3763 /* Problem: Freeing all matrices and later allocating them again
3764 is a serious redisplay flickering problem. What we would
3765 really like to do is to free only those matrices not reused
3766 below. */
3767 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
3768 leaf_windows
3769 = (struct window **) alloca (count_windows (root_window)
3770 * sizeof (struct window *));
3771 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
3772
3773 /* Temporarily avoid any problems with windows that are smaller
3774 than they are supposed to be. */
3775 window_min_height = 1;
3776 window_min_width = 1;
3777
3778 /* Kludge Alert!
3779 Mark all windows now on frame as "deleted".
3780 Restoring the new configuration "undeletes" any that are in it.
3781
3782 Save their current buffers in their height fields, since we may
3783 need it later, if a buffer saved in the configuration is now
3784 dead. */
3785 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3786
3787 for (k = 0; k < saved_windows->size; k++)
3788 {
3789 p = SAVED_WINDOW_N (saved_windows, k);
3790 w = XWINDOW (p->window);
3791 w->next = Qnil;
3792
3793 if (!NILP (p->parent))
3794 w->parent = SAVED_WINDOW_N (saved_windows,
3795 XFASTINT (p->parent))->window;
3796 else
3797 w->parent = Qnil;
3798
3799 if (!NILP (p->prev))
3800 {
3801 w->prev = SAVED_WINDOW_N (saved_windows,
3802 XFASTINT (p->prev))->window;
3803 XWINDOW (w->prev)->next = p->window;
3804 }
3805 else
3806 {
3807 w->prev = Qnil;
3808 if (!NILP (w->parent))
3809 {
3810 if (EQ (p->width, XWINDOW (w->parent)->width))
3811 {
3812 XWINDOW (w->parent)->vchild = p->window;
3813 XWINDOW (w->parent)->hchild = Qnil;
3814 }
3815 else
3816 {
3817 XWINDOW (w->parent)->hchild = p->window;
3818 XWINDOW (w->parent)->vchild = Qnil;
3819 }
3820 }
3821 }
3822
3823 /* If we squirreled away the buffer in the window's height,
3824 restore it now. */
3825 if (BUFFERP (w->height))
3826 w->buffer = w->height;
3827 w->left = p->left;
3828 w->top = p->top;
3829 w->width = p->width;
3830 w->height = p->height;
3831 w->hscroll = p->hscroll;
3832 w->display_table = p->display_table;
3833 XSETFASTINT (w->last_modified, 0);
3834 XSETFASTINT (w->last_overlay_modified, 0);
3835
3836 /* Reinstall the saved buffer and pointers into it. */
3837 if (NILP (p->buffer))
3838 w->buffer = p->buffer;
3839 else
3840 {
3841 if (!NILP (XBUFFER (p->buffer)->name))
3842 /* If saved buffer is alive, install it. */
3843 {
3844 w->buffer = p->buffer;
3845 w->start_at_line_beg = p->start_at_line_beg;
3846 set_marker_restricted (w->start, p->start, w->buffer);
3847 set_marker_restricted (w->pointm, p->pointm, w->buffer);
3848 Fset_marker (XBUFFER (w->buffer)->mark,
3849 p->mark, w->buffer);
3850
3851 /* As documented in Fcurrent_window_configuration, don't
3852 save the location of point in the buffer which was current
3853 when the window configuration was recorded. */
3854 if (!EQ (p->buffer, new_current_buffer)
3855 && XBUFFER (p->buffer) == current_buffer)
3856 Fgoto_char (w->pointm);
3857 }
3858 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
3859 /* Else unless window has a live buffer, get one. */
3860 {
3861 w->buffer = Fcdr (Fcar (Vbuffer_alist));
3862 /* This will set the markers to beginning of visible
3863 range. */
3864 set_marker_restricted (w->start, make_number (0), w->buffer);
3865 set_marker_restricted (w->pointm, make_number (0),w->buffer);
3866 w->start_at_line_beg = Qt;
3867 }
3868 else
3869 /* Keeping window's old buffer; make sure the markers
3870 are real. */
3871 {
3872 /* Set window markers at start of visible range. */
3873 if (XMARKER (w->start)->buffer == 0)
3874 set_marker_restricted (w->start, make_number (0),
3875 w->buffer);
3876 if (XMARKER (w->pointm)->buffer == 0)
3877 set_marker_restricted_both (w->pointm, w->buffer,
3878 BUF_PT (XBUFFER (w->buffer)),
3879 BUF_PT_BYTE (XBUFFER (w->buffer)));
3880 w->start_at_line_beg = Qt;
3881 }
3882 }
3883 }
3884
3885 FRAME_ROOT_WINDOW (f) = data->root_window;
3886 Fselect_window (data->current_window);
3887 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
3888 = selected_window;
3889
3890 if (NILP (data->focus_frame)
3891 || (FRAMEP (data->focus_frame)
3892 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
3893 Fredirect_frame_focus (frame, data->focus_frame);
3894
3895 #if 0 /* I don't understand why this is needed, and it causes problems
3896 when the frame's old selected window has been deleted. */
3897 if (f != selected_frame && FRAME_WINDOW_P (f))
3898 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
3899 Qnil, 0);
3900 #endif
3901
3902 /* Set the screen height to the value it had before this function. */
3903 if (previous_frame_height != FRAME_HEIGHT (f)
3904 || previous_frame_width != FRAME_WIDTH (f))
3905 change_frame_size (f, previous_frame_height, previous_frame_width,
3906 0, 0);
3907 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3908 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
3909 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
3910 make_number (0));
3911 #ifdef HAVE_WINDOW_SYSTEM
3912 if (previous_frame_toolbar_lines != FRAME_TOOLBAR_LINES (f))
3913 x_set_toolbar_lines (f, make_number (previous_frame_toolbar_lines),
3914 make_number (0));
3915 #endif
3916 #endif
3917
3918 /* Now, free glyph matrices in windows that were not reused. */
3919 for (i = 0; i < n_leaf_windows; ++i)
3920 if (NILP (leaf_windows[i]->buffer))
3921 {
3922 /* Assert it's not reused as a combination. */
3923 xassert (NILP (leaf_windows[i]->hchild)
3924 && NILP (leaf_windows[i]->vchild));
3925 free_window_matrices (leaf_windows[i]);
3926 SET_FRAME_GARBAGED (f);
3927 }
3928
3929 adjust_glyphs (f);
3930
3931 UNBLOCK_INPUT;
3932
3933 /* Fselect_window will have made f the selected frame, so we
3934 reselect the proper frame here. Fhandle_switch_frame will change the
3935 selected window too, but that doesn't make the call to
3936 Fselect_window above totally superfluous; it still sets f's
3937 selected window. */
3938 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
3939 do_switch_frame (data->selected_frame, Qnil, 0);
3940
3941 if (! NILP (Vwindow_configuration_change_hook)
3942 && ! NILP (Vrun_hooks))
3943 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3944 }
3945
3946 if (!NILP (new_current_buffer))
3947 {
3948 Fset_buffer (new_current_buffer);
3949
3950 /* If the buffer that is current now is the same
3951 that was current before setting the window configuration,
3952 don't alter its PT. */
3953 if (old_point >= 0)
3954 SET_PT (old_point);
3955 }
3956
3957 /* Restore the minimum heights recorded in the configuration. */
3958 window_min_height = XINT (data->min_height);
3959 window_min_width = XINT (data->min_width);
3960
3961 Vminibuf_scroll_window = data->minibuf_scroll_window;
3962
3963 return (FRAME_LIVE_P (f) ? Qt : Qnil);
3964 }
3965
3966 /* Mark all windows now on frame as deleted
3967 by setting their buffers to nil. */
3968
3969 void
3970 delete_all_subwindows (w)
3971 register struct window *w;
3972 {
3973 if (!NILP (w->next))
3974 delete_all_subwindows (XWINDOW (w->next));
3975 if (!NILP (w->vchild))
3976 delete_all_subwindows (XWINDOW (w->vchild));
3977 if (!NILP (w->hchild))
3978 delete_all_subwindows (XWINDOW (w->hchild));
3979
3980 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
3981
3982 if (!NILP (w->buffer))
3983 unshow_buffer (w);
3984
3985 /* We set all three of these fields to nil, to make sure that we can
3986 distinguish this dead window from any live window. Live leaf
3987 windows will have buffer set, and combination windows will have
3988 vchild or hchild set. */
3989 w->buffer = Qnil;
3990 w->vchild = Qnil;
3991 w->hchild = Qnil;
3992 }
3993 \f
3994 static int
3995 count_windows (window)
3996 register struct window *window;
3997 {
3998 register int count = 1;
3999 if (!NILP (window->next))
4000 count += count_windows (XWINDOW (window->next));
4001 if (!NILP (window->vchild))
4002 count += count_windows (XWINDOW (window->vchild));
4003 if (!NILP (window->hchild))
4004 count += count_windows (XWINDOW (window->hchild));
4005 return count;
4006 }
4007
4008
4009 /* Fill vector FLAT with leaf windows under W, starting at index I.
4010 Value is last index + 1. */
4011
4012 static int
4013 get_leaf_windows (w, flat, i)
4014 struct window *w;
4015 struct window **flat;
4016 int i;
4017 {
4018 while (w)
4019 {
4020 if (!NILP (w->hchild))
4021 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
4022 else if (!NILP (w->vchild))
4023 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
4024 else
4025 flat[i++] = w;
4026
4027 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4028 }
4029
4030 return i;
4031 }
4032
4033
4034 /* Return a pointer to the glyph W's physical cursor is on. Value is
4035 null if W's current matrix is invalid, so that no meaningfull glyph
4036 can be returned. */
4037
4038 struct glyph *
4039 get_phys_cursor_glyph (w)
4040 struct window *w;
4041 {
4042 struct glyph_row *row;
4043 struct glyph *glyph;
4044
4045 if (w->phys_cursor.vpos >= 0
4046 && w->phys_cursor.vpos < w->current_matrix->nrows
4047 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
4048 row->enabled_p)
4049 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
4050 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
4051 else
4052 glyph = NULL;
4053
4054 return glyph;
4055 }
4056
4057
4058 static int
4059 save_window_save (window, vector, i)
4060 Lisp_Object window;
4061 struct Lisp_Vector *vector;
4062 int i;
4063 {
4064 register struct saved_window *p;
4065 register struct window *w;
4066 register Lisp_Object tem;
4067
4068 for (;!NILP (window); window = w->next)
4069 {
4070 p = SAVED_WINDOW_N (vector, i);
4071 w = XWINDOW (window);
4072
4073 XSETFASTINT (w->temslot, i++);
4074 p->window = window;
4075 p->buffer = w->buffer;
4076 p->left = w->left;
4077 p->top = w->top;
4078 p->width = w->width;
4079 p->height = w->height;
4080 p->hscroll = w->hscroll;
4081 p->display_table = w->display_table;
4082 if (!NILP (w->buffer))
4083 {
4084 /* Save w's value of point in the window configuration.
4085 If w is the selected window, then get the value of point
4086 from the buffer; pointm is garbage in the selected window. */
4087 if (EQ (window, selected_window))
4088 {
4089 p->pointm = Fmake_marker ();
4090 set_marker_both (p->pointm, w->buffer,
4091 BUF_PT (XBUFFER (w->buffer)),
4092 BUF_PT_BYTE (XBUFFER (w->buffer)));
4093 }
4094 else
4095 p->pointm = Fcopy_marker (w->pointm, Qnil);
4096
4097 p->start = Fcopy_marker (w->start, Qnil);
4098 p->start_at_line_beg = w->start_at_line_beg;
4099
4100 tem = XBUFFER (w->buffer)->mark;
4101 p->mark = Fcopy_marker (tem, Qnil);
4102 }
4103 else
4104 {
4105 p->pointm = Qnil;
4106 p->start = Qnil;
4107 p->mark = Qnil;
4108 p->start_at_line_beg = Qnil;
4109 }
4110
4111 if (NILP (w->parent))
4112 p->parent = Qnil;
4113 else
4114 p->parent = XWINDOW (w->parent)->temslot;
4115
4116 if (NILP (w->prev))
4117 p->prev = Qnil;
4118 else
4119 p->prev = XWINDOW (w->prev)->temslot;
4120
4121 if (!NILP (w->vchild))
4122 i = save_window_save (w->vchild, vector, i);
4123 if (!NILP (w->hchild))
4124 i = save_window_save (w->hchild, vector, i);
4125 }
4126
4127 return i;
4128 }
4129
4130 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
4131 Scurrent_window_configuration, 0, 1, 0,
4132 "Return an object representing the current window configuration of FRAME.\n\
4133 If FRAME is nil or omitted, use the selected frame.\n\
4134 This describes the number of windows, their sizes and current buffers,\n\
4135 and for each displayed buffer, where display starts, and the positions of\n\
4136 point and mark. An exception is made for point in the current buffer:\n\
4137 its value is -not- saved.\n\
4138 This also records the currently selected frame, and FRAME's focus\n\
4139 redirection (see `redirect-frame-focus').")
4140 (frame)
4141 Lisp_Object frame;
4142 {
4143 register Lisp_Object tem;
4144 register int n_windows;
4145 register struct save_window_data *data;
4146 register struct Lisp_Vector *vec;
4147 register int i;
4148 FRAME_PTR f;
4149
4150 if (NILP (frame))
4151 f = selected_frame;
4152 else
4153 {
4154 CHECK_LIVE_FRAME (frame, 0);
4155 f = XFRAME (frame);
4156 }
4157
4158 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
4159 vec = allocate_vectorlike (VECSIZE (struct save_window_data));
4160 for (i = 0; i < VECSIZE (struct save_window_data); i++)
4161 vec->contents[i] = Qnil;
4162 vec->size = VECSIZE (struct save_window_data);
4163 data = (struct save_window_data *)vec;
4164
4165 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
4166 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
4167 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
4168 XSETFASTINT (data->frame_toolbar_lines, FRAME_TOOLBAR_LINES (f));
4169 XSETFRAME (data->selected_frame, selected_frame);
4170 data->current_window = FRAME_SELECTED_WINDOW (f);
4171 XSETBUFFER (data->current_buffer, current_buffer);
4172 data->minibuf_scroll_window = Vminibuf_scroll_window;
4173 data->root_window = FRAME_ROOT_WINDOW (f);
4174 data->focus_frame = FRAME_FOCUS_FRAME (f);
4175 XSETINT (data->min_height, window_min_height);
4176 XSETINT (data->min_width, window_min_width);
4177 tem = Fmake_vector (make_number (n_windows), Qnil);
4178 data->saved_windows = tem;
4179 for (i = 0; i < n_windows; i++)
4180 XVECTOR (tem)->contents[i]
4181 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
4182 save_window_save (FRAME_ROOT_WINDOW (f),
4183 XVECTOR (tem), 0);
4184 XSETWINDOW_CONFIGURATION (tem, data);
4185 return (tem);
4186 }
4187
4188 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
4189 0, UNEVALLED, 0,
4190 "Execute body, preserving window sizes and contents.\n\
4191 Restore which buffer appears in which window, where display starts,\n\
4192 and the value of point and mark for each window.\n\
4193 Also restore which buffer is current.\n\
4194 But do not preserve point in the current buffer.\n\
4195 Does not restore the value of point in current buffer.")
4196 (args)
4197 Lisp_Object args;
4198 {
4199 register Lisp_Object val;
4200 register int count = specpdl_ptr - specpdl;
4201
4202 record_unwind_protect (Fset_window_configuration,
4203 Fcurrent_window_configuration (Qnil));
4204 val = Fprogn (args);
4205 return unbind_to (count, val);
4206 }
4207
4208 \f
4209 /***********************************************************************
4210 Marginal Areas
4211 ***********************************************************************/
4212
4213 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
4214 2, 3, "",
4215 "Set width of marginal areas of window WINDOW.\n\
4216 If window is nil or omitted, set margins of the currently selected window.\n\
4217 First parameter LEFT-WIDTH specifies the number of character\n\
4218 cells to reserve for the left marginal area. Second parameter\n\
4219 RIGHT-WIDTH does the same for the right marginal area.\n\
4220 A nil width parameter means no margin.")
4221 (left, right, window)
4222 Lisp_Object window, left, right;
4223 {
4224 struct window *w = decode_window (window);
4225 struct frame *f = XFRAME (w->frame);
4226
4227 if (!NILP (left))
4228 CHECK_NUMBER_OR_FLOAT (left, 0);
4229 if (!NILP (right))
4230 CHECK_NUMBER_OR_FLOAT (right, 0);
4231
4232 /* Check widths < 0 and translate a zero width to nil.
4233 Margins that are too wide have to be checked elsewhere. */
4234 if ((INTEGERP (left) && XINT (left) < 0)
4235 || (FLOATP (left) && XFLOAT (left)->data <= 0))
4236 XSETFASTINT (left, 0);
4237 if (INTEGERP (left) && XFASTINT (left) == 0)
4238 left = Qnil;
4239
4240 if ((INTEGERP (right) && XINT (right) < 0)
4241 || (FLOATP (right) && XFLOAT (right)->data <= 0))
4242 XSETFASTINT (right, 0);
4243 if (INTEGERP (right) && XFASTINT (right) == 0)
4244 right = Qnil;
4245
4246 w->left_margin_width = left;
4247 w->right_margin_width = right;
4248
4249 ++windows_or_buffers_changed;
4250 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
4251 return Qnil;
4252 }
4253
4254
4255 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
4256 0, 1, 0,
4257 "Get width of marginal areas of window WINDOW.\n\
4258 If WINDOW is omitted or nil, use the currently selected window.\n\
4259 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).\n\
4260 If a marginal area does not exist, its width will be returned\n\
4261 as nil.")
4262 (window)
4263 Lisp_Object window;
4264 {
4265 struct window *w = decode_window (window);
4266 return Fcons (w->left_margin_width, w->right_margin_width);
4267 }
4268
4269
4270 \f
4271 /***********************************************************************
4272 Smooth scrolling
4273 ***********************************************************************/
4274
4275 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 1, 0,
4276 "Return the amount by which WINDOW is scrolled vertically.\n\
4277 Use the selected window if WINDOW is nil or omitted.\n\
4278 Value is a multiple of the canonical character height of WINDOW.")
4279 (window)
4280 Lisp_Object window;
4281 {
4282 struct frame *f;
4283 struct window *w;
4284
4285 if (NILP (window))
4286 window = selected_window;
4287 w = XWINDOW (window);
4288 f = XFRAME (w->frame);
4289
4290 if (FRAME_WINDOW_P (f))
4291 return CANON_Y_FROM_PIXEL_Y (f, w->vscroll);
4292 else
4293 return make_number (0);
4294 }
4295
4296
4297 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
4298 1, 2, 0,
4299 "Set amount by WINDOW should be scrolled vertically to VSCROLL.\n\
4300 WINDOW nil or omitted means use the selected window. VSCROLL is a\n\
4301 multiple of the canonical character height of WINDOW.")
4302 (vscroll, window)
4303 Lisp_Object vscroll, window;
4304 {
4305 struct window *w;
4306 struct frame *f;
4307
4308 CHECK_NUMBER_OR_FLOAT (vscroll, 0);
4309
4310 if (NILP (window))
4311 window = selected_window;
4312 w = XWINDOW (window);
4313 f = XFRAME (w->frame);
4314
4315 if (FRAME_WINDOW_P (f))
4316 {
4317 int old_dy = w->vscroll;
4318 w->vscroll = min (0, CANON_Y_UNIT (f) * XFLOATINT (vscroll));
4319
4320 /* Adjust glyph matrix of the frame if the virtual display
4321 area becomes larger than before. */
4322 if (w->vscroll < 0 && w->vscroll < old_dy)
4323 adjust_glyphs (f);
4324
4325 /* Prevent redisplay shortcuts. */
4326 XBUFFER (w->buffer)->clip_changed = 1;
4327 }
4328
4329 return Qnil;
4330 }
4331
4332
4333 \f
4334 /***********************************************************************
4335 Initialization
4336 ***********************************************************************/
4337
4338 /* Return 1 if window configurations C1 and C2
4339 describe the same state of affairs. This is used by Fequal. */
4340
4341 int
4342 compare_window_configurations (c1, c2, ignore_positions)
4343 Lisp_Object c1, c2;
4344 int ignore_positions;
4345 {
4346 register struct save_window_data *d1, *d2;
4347 struct Lisp_Vector *sw1, *sw2;
4348 int i;
4349
4350 d1 = (struct save_window_data *) XVECTOR (c1);
4351 d2 = (struct save_window_data *) XVECTOR (c2);
4352 sw1 = XVECTOR (d1->saved_windows);
4353 sw2 = XVECTOR (d2->saved_windows);
4354
4355 if (! EQ (d1->frame_width, d2->frame_width))
4356 return 0;
4357 if (! EQ (d1->frame_height, d2->frame_height))
4358 return 0;
4359 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
4360 return 0;
4361 if (! EQ (d1->selected_frame, d2->selected_frame))
4362 return 0;
4363 /* Don't compare the current_window field directly.
4364 Instead see w1_is_current and w2_is_current, below. */
4365 if (! EQ (d1->current_buffer, d2->current_buffer))
4366 return 0;
4367 if (! ignore_positions)
4368 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
4369 return 0;
4370 /* Don't compare the root_window field.
4371 We don't require the two configurations
4372 to use the same window object,
4373 and the two root windows must be equivalent
4374 if everything else compares equal. */
4375 if (! EQ (d1->focus_frame, d2->focus_frame))
4376 return 0;
4377 if (! EQ (d1->min_width, d2->min_width))
4378 return 0;
4379 if (! EQ (d1->min_height, d2->min_height))
4380 return 0;
4381
4382 /* Verify that the two confis have the same number of windows. */
4383 if (sw1->size != sw2->size)
4384 return 0;
4385
4386 for (i = 0; i < sw1->size; i++)
4387 {
4388 struct saved_window *p1, *p2;
4389 int w1_is_current, w2_is_current;
4390
4391 p1 = SAVED_WINDOW_N (sw1, i);
4392 p2 = SAVED_WINDOW_N (sw2, i);
4393
4394 /* Verify that the current windows in the two
4395 configurations correspond to each other. */
4396 w1_is_current = EQ (d1->current_window, p1->window);
4397 w2_is_current = EQ (d2->current_window, p2->window);
4398
4399 if (w1_is_current != w2_is_current)
4400 return 0;
4401
4402 /* Verify that the corresponding windows do match. */
4403 if (! EQ (p1->buffer, p2->buffer))
4404 return 0;
4405 if (! EQ (p1->left, p2->left))
4406 return 0;
4407 if (! EQ (p1->top, p2->top))
4408 return 0;
4409 if (! EQ (p1->width, p2->width))
4410 return 0;
4411 if (! EQ (p1->height, p2->height))
4412 return 0;
4413 if (! EQ (p1->display_table, p2->display_table))
4414 return 0;
4415 if (! EQ (p1->parent, p2->parent))
4416 return 0;
4417 if (! EQ (p1->prev, p2->prev))
4418 return 0;
4419 if (! ignore_positions)
4420 {
4421 if (! EQ (p1->hscroll, p2->hscroll))
4422 return 0;
4423 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
4424 return 0;
4425 if (NILP (Fequal (p1->start, p2->start)))
4426 return 0;
4427 if (NILP (Fequal (p1->pointm, p2->pointm)))
4428 return 0;
4429 if (NILP (Fequal (p1->mark, p2->mark)))
4430 return 0;
4431 }
4432 }
4433
4434 return 1;
4435 }
4436
4437 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
4438 Scompare_window_configurations, 2, 2, 0,
4439 "Compare two window configurations as regards the structure of windows.\n\
4440 This function ignores details such as the values of point and mark\n\
4441 and scrolling positions.")
4442 (x, y)
4443 Lisp_Object x, y;
4444 {
4445 if (compare_window_configurations (x, y, 1))
4446 return Qt;
4447 return Qnil;
4448 }
4449 \f
4450 void
4451 init_window_once ()
4452 {
4453 selected_frame = make_terminal_frame ();
4454 XSETFRAME (Vterminal_frame, selected_frame);
4455 minibuf_window = selected_frame->minibuffer_window;
4456 selected_window = selected_frame->selected_window;
4457 last_nonminibuf_frame = selected_frame;
4458
4459 window_initialized = 1;
4460 }
4461
4462 void
4463 syms_of_window ()
4464 {
4465 staticpro (&Qwindow_configuration_change_hook);
4466 Qwindow_configuration_change_hook
4467 = intern ("window-configuration-change-hook");
4468
4469 Qwindowp = intern ("windowp");
4470 staticpro (&Qwindowp);
4471
4472 Qwindow_configuration_p = intern ("window-configuration-p");
4473 staticpro (&Qwindow_configuration_p);
4474
4475 Qwindow_live_p = intern ("window-live-p");
4476 staticpro (&Qwindow_live_p);
4477
4478 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
4479 staticpro (&Qtemp_buffer_show_hook);
4480
4481 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
4482 "Non-nil means call as function to display a help buffer.\n\
4483 The function is called with one argument, the buffer to be displayed.\n\
4484 Used by `with-output-to-temp-buffer'.\n\
4485 If this function is used, then it must do the entire job of showing\n\
4486 the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
4487 Vtemp_buffer_show_function = Qnil;
4488
4489 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
4490 "If non-nil, function to call to handle `display-buffer'.\n\
4491 It will receive two args, the buffer and a flag which if non-nil means\n\
4492 that the currently selected window is not acceptable.\n\
4493 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
4494 work using this function.");
4495 Vdisplay_buffer_function = Qnil;
4496
4497 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
4498 "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
4499 Vminibuf_scroll_window = Qnil;
4500
4501 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
4502 "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
4503 Vother_window_scroll_buffer = Qnil;
4504
4505 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
4506 "*Non-nil means `display-buffer' should make a separate frame.");
4507 pop_up_frames = 0;
4508
4509 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
4510 "Function to call to handle automatic new frame creation.\n\
4511 It is called with no arguments and should return a newly created frame.\n\
4512 \n\
4513 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
4514 where `pop-up-frame-alist' would hold the default frame parameters.");
4515 Vpop_up_frame_function = Qnil;
4516
4517 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
4518 "*List of buffer names that should have their own special frames.\n\
4519 Displaying a buffer whose name is in this list makes a special frame for it\n\
4520 using `special-display-function'. See also `special-display-regexps'.\n\
4521 \n\
4522 An element of the list can be a list instead of just a string.\n\
4523 There are two ways to use a list as an element:\n\
4524 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)\n\
4525 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
4526 In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
4527 followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
4528 All this is done by the function found in `special-display-function'.\n\
4529 \n\
4530 If this variable appears \"not to work\", because you add a name to it\n\
4531 but that buffer still appears in the selected window, look at the\n\
4532 values of `same-window-buffer-names' and `same-window-regexps'.\n\
4533 Those variables take precedence over this one.");
4534 Vspecial_display_buffer_names = Qnil;
4535
4536 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
4537 "*List of regexps saying which buffers should have their own special frames.\n\
4538 If a buffer name matches one of these regexps, it gets its own frame.\n\
4539 Displaying a buffer whose name is in this list makes a special frame for it\n\
4540 using `special-display-function'.\n\
4541 \n\
4542 An element of the list can be a list instead of just a string.\n\
4543 There are two ways to use a list as an element:\n\
4544 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)\n\
4545 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
4546 In the latter case, FUNCTION is called with the buffer as first argument,\n\
4547 followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
4548 All this is done by the function found in `special-display-function'.\n\
4549 \n\
4550 If this variable appears \"not to work\", because you add a regexp to it\n\
4551 but the matching buffers still appear in the selected window, look at the\n\
4552 values of `same-window-buffer-names' and `same-window-regexps'.\n\
4553 Those variables take precedence over this one.");
4554 Vspecial_display_regexps = Qnil;
4555
4556 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
4557 "Function to call to make a new frame for a special buffer.\n\
4558 It is called with two arguments, the buffer and optional buffer specific\n\
4559 data, and should return a window displaying that buffer.\n\
4560 The default value makes a separate frame for the buffer,\n\
4561 using `special-display-frame-alist' to specify the frame parameters.\n\
4562 \n\
4563 A buffer is special if its is listed in `special-display-buffer-names'\n\
4564 or matches a regexp in `special-display-regexps'.");
4565 Vspecial_display_function = Qnil;
4566
4567 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
4568 "*List of buffer names that should appear in the selected window.\n\
4569 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
4570 switches to it in the selected window, rather than making it appear\n\
4571 in some other window.\n\
4572 \n\
4573 An element of the list can be a cons cell instead of just a string.\n\
4574 Then the car must be a string, which specifies the buffer name.\n\
4575 This is for compatibility with `special-display-buffer-names';\n\
4576 the cdr of the cons cell is ignored.\n\
4577 \n\
4578 See also `same-window-regexps'.");
4579 Vsame_window_buffer_names = Qnil;
4580
4581 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
4582 "*List of regexps saying which buffers should appear in the selected window.\n\
4583 If a buffer name matches one of these regexps, then displaying it\n\
4584 using `display-buffer' or `pop-to-buffer' switches to it\n\
4585 in the selected window, rather than making it appear in some other window.\n\
4586 \n\
4587 An element of the list can be a cons cell instead of just a string.\n\
4588 Then the car must be a string, which specifies the buffer name.\n\
4589 This is for compatibility with `special-display-buffer-names';\n\
4590 the cdr of the cons cell is ignored.\n\
4591 \n\
4592 See also `same-window-buffer-names'.");
4593 Vsame_window_regexps = Qnil;
4594
4595 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
4596 "*Non-nil means display-buffer should make new windows.");
4597 pop_up_windows = 1;
4598
4599 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
4600 "*Number of lines of continuity when scrolling by screenfuls.");
4601 next_screen_context_lines = 2;
4602
4603 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
4604 "*display-buffer would prefer to split the largest window if this large.\n\
4605 If there is only one window, it is split regardless of this value.");
4606 split_height_threshold = 500;
4607
4608 DEFVAR_INT ("window-min-height", &window_min_height,
4609 "*Delete any window less than this tall (including its mode line).");
4610 window_min_height = 4;
4611
4612 DEFVAR_INT ("window-min-width", &window_min_width,
4613 "*Delete any window less than this wide.");
4614 window_min_width = 10;
4615
4616 DEFVAR_LISP ("scroll-preserve-screen-position",
4617 &Vscroll_preserve_screen_position,
4618 "*Nonzero means scroll commands move point to keep its screen line unchanged.");
4619 Vscroll_preserve_screen_position = Qnil;
4620
4621 DEFVAR_LISP ("window-configuration-change-hook",
4622 &Vwindow_configuration_change_hook,
4623 "Functions to call when window configuration changes.\n\
4624 The selected frame is the one whose configuration has changed.");
4625 Vwindow_configuration_change_hook = Qnil;
4626
4627 defsubr (&Sselected_window);
4628 defsubr (&Sminibuffer_window);
4629 defsubr (&Swindow_minibuffer_p);
4630 defsubr (&Swindowp);
4631 defsubr (&Swindow_live_p);
4632 defsubr (&Spos_visible_in_window_p);
4633 defsubr (&Swindow_buffer);
4634 defsubr (&Swindow_height);
4635 defsubr (&Swindow_width);
4636 defsubr (&Swindow_hscroll);
4637 defsubr (&Sset_window_hscroll);
4638 defsubr (&Swindow_redisplay_end_trigger);
4639 defsubr (&Sset_window_redisplay_end_trigger);
4640 defsubr (&Swindow_edges);
4641 defsubr (&Scoordinates_in_window_p);
4642 defsubr (&Swindow_at);
4643 defsubr (&Swindow_point);
4644 defsubr (&Swindow_start);
4645 defsubr (&Swindow_end);
4646 defsubr (&Sset_window_point);
4647 defsubr (&Sset_window_start);
4648 defsubr (&Swindow_dedicated_p);
4649 defsubr (&Sset_window_dedicated_p);
4650 defsubr (&Swindow_display_table);
4651 defsubr (&Sset_window_display_table);
4652 defsubr (&Snext_window);
4653 defsubr (&Sprevious_window);
4654 defsubr (&Sother_window);
4655 defsubr (&Sget_lru_window);
4656 defsubr (&Sget_largest_window);
4657 defsubr (&Sget_buffer_window);
4658 defsubr (&Sdelete_other_windows);
4659 defsubr (&Sdelete_windows_on);
4660 defsubr (&Sreplace_buffer_in_windows);
4661 defsubr (&Sdelete_window);
4662 defsubr (&Sset_window_buffer);
4663 defsubr (&Sselect_window);
4664 defsubr (&Sspecial_display_p);
4665 defsubr (&Ssame_window_p);
4666 defsubr (&Sdisplay_buffer);
4667 defsubr (&Ssplit_window);
4668 defsubr (&Senlarge_window);
4669 defsubr (&Sshrink_window);
4670 defsubr (&Sscroll_up);
4671 defsubr (&Sscroll_down);
4672 defsubr (&Sscroll_left);
4673 defsubr (&Sscroll_right);
4674 defsubr (&Sother_window_for_scrolling);
4675 defsubr (&Sscroll_other_window);
4676 defsubr (&Srecenter);
4677 defsubr (&Smove_to_window_line);
4678 defsubr (&Swindow_configuration_p);
4679 defsubr (&Swindow_configuration_frame);
4680 defsubr (&Sset_window_configuration);
4681 defsubr (&Scurrent_window_configuration);
4682 defsubr (&Ssave_window_excursion);
4683 defsubr (&Sset_window_margins);
4684 defsubr (&Swindow_margins);
4685 defsubr (&Swindow_vscroll);
4686 defsubr (&Sset_window_vscroll);
4687 defsubr (&Scompare_window_configurations);
4688 }
4689
4690 void
4691 keys_of_window ()
4692 {
4693 initial_define_key (control_x_map, '1', "delete-other-windows");
4694 initial_define_key (control_x_map, '2', "split-window");
4695 initial_define_key (control_x_map, '0', "delete-window");
4696 initial_define_key (control_x_map, 'o', "other-window");
4697 initial_define_key (control_x_map, '^', "enlarge-window");
4698 initial_define_key (control_x_map, '<', "scroll-left");
4699 initial_define_key (control_x_map, '>', "scroll-right");
4700
4701 initial_define_key (global_map, Ctl ('V'), "scroll-up");
4702 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
4703 initial_define_key (meta_map, 'v', "scroll-down");
4704
4705 initial_define_key (global_map, Ctl('L'), "recenter");
4706 initial_define_key (meta_map, 'r', "move-to-window-line");
4707 }