]> code.delx.au - gnu-emacs/blob - src/window.c
(load_face_colors): Load background color if setting
[gnu-emacs] / src / window.c
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985,86,87,93,94,95,96,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;
2330
2331 buffer = Fget_buffer (buffer);
2332 CHECK_BUFFER (buffer, 0);
2333
2334 if (!NILP (Vdisplay_buffer_function))
2335 return call2 (Vdisplay_buffer_function, buffer, not_this_window);
2336
2337 if (NILP (not_this_window)
2338 && XBUFFER (XWINDOW (selected_window)->buffer) == XBUFFER (buffer))
2339 return display_buffer_1 (selected_window);
2340
2341 /* See if the user has specified this buffer should appear
2342 in the selected window. */
2343 if (NILP (not_this_window))
2344 {
2345 tem = Fsame_window_p (XBUFFER (buffer)->name);
2346 if (!NILP (tem))
2347 {
2348 Fswitch_to_buffer (buffer, Qnil);
2349 return display_buffer_1 (selected_window);
2350 }
2351 }
2352
2353 /* If pop_up_frames,
2354 look for a window showing BUFFER on any visible or iconified frame.
2355 Otherwise search only the current frame. */
2356 if (! NILP (frame))
2357 tem = frame;
2358 else if (pop_up_frames || last_nonminibuf_frame == 0)
2359 XSETFASTINT (tem, 0);
2360 else
2361 XSETFRAME (tem, last_nonminibuf_frame);
2362 window = Fget_buffer_window (buffer, tem);
2363 if (!NILP (window)
2364 && (NILP (not_this_window) || !EQ (window, selected_window)))
2365 {
2366 return display_buffer_1 (window);
2367 }
2368
2369 /* Certain buffer names get special handling. */
2370 if (!NILP (Vspecial_display_function))
2371 {
2372 tem = Fspecial_display_p (XBUFFER (buffer)->name);
2373 if (EQ (tem, Qt))
2374 return call1 (Vspecial_display_function, buffer);
2375 if (CONSP (tem))
2376 return call2 (Vspecial_display_function, buffer, tem);
2377 }
2378
2379 /* If there are no frames open that have more than a minibuffer,
2380 we need to create a new frame. */
2381 if (pop_up_frames || last_nonminibuf_frame == 0)
2382 {
2383 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2384 Fset_window_buffer (window, buffer);
2385 return display_buffer_1 (window);
2386 }
2387
2388 if (pop_up_windows
2389 || FRAME_MINIBUF_ONLY_P (selected_frame)
2390 /* If the current frame is a special display frame,
2391 don't try to reuse its windows. */
2392 || !NILP (XWINDOW (FRAME_ROOT_WINDOW (selected_frame))->dedicated)
2393 )
2394 {
2395 Lisp_Object frames;
2396
2397 frames = Qnil;
2398 if (FRAME_MINIBUF_ONLY_P (selected_frame))
2399 XSETFRAME (frames, last_nonminibuf_frame);
2400 /* Don't try to create a window if would get an error */
2401 if (split_height_threshold < window_min_height << 1)
2402 split_height_threshold = window_min_height << 1;
2403
2404 /* Note that both Fget_largest_window and Fget_lru_window
2405 ignore minibuffers and dedicated windows.
2406 This means they can return nil. */
2407
2408 /* If the frame we would try to split cannot be split,
2409 try other frames. */
2410 if (FRAME_NO_SPLIT_P (NILP (frames) ? selected_frame
2411 : last_nonminibuf_frame))
2412 {
2413 /* Try visible frames first. */
2414 window = Fget_largest_window (Qvisible);
2415 /* If that didn't work, try iconified frames. */
2416 if (NILP (window))
2417 window = Fget_largest_window (make_number (0));
2418 if (NILP (window))
2419 window = Fget_largest_window (Qt);
2420 }
2421 else
2422 window = Fget_largest_window (frames);
2423
2424 /* If we got a tall enough full-width window that can be split,
2425 split it. */
2426 if (!NILP (window)
2427 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2428 && window_height (window) >= split_height_threshold
2429 && WINDOW_FULL_WIDTH_P (XWINDOW (window)))
2430 window = Fsplit_window (window, Qnil, Qnil);
2431 else
2432 {
2433 Lisp_Object upper, lower, other;
2434
2435 window = Fget_lru_window (frames);
2436 /* If the LRU window is selected, and big enough,
2437 and can be split, split it. */
2438 if (!NILP (window)
2439 && ! FRAME_NO_SPLIT_P (XFRAME (XWINDOW (window)->frame))
2440 && (EQ (window, selected_window)
2441 || EQ (XWINDOW (window)->parent, Qnil))
2442 && window_height (window) >= window_min_height << 1)
2443 window = Fsplit_window (window, Qnil, Qnil);
2444 /* If Fget_lru_window returned nil, try other approaches. */
2445
2446 /* Try visible frames first. */
2447 if (NILP (window))
2448 window = Fget_buffer_window (buffer, Qvisible);
2449 if (NILP (window))
2450 window = Fget_largest_window (Qvisible);
2451 /* If that didn't work, try iconified frames. */
2452 if (NILP (window))
2453 window = Fget_buffer_window (buffer, make_number (0));
2454 if (NILP (window))
2455 window = Fget_largest_window (make_number (0));
2456 /* Try invisible frames. */
2457 if (NILP (window))
2458 window = Fget_buffer_window (buffer, Qt);
2459 if (NILP (window))
2460 window = Fget_largest_window (Qt);
2461 /* As a last resort, make a new frame. */
2462 if (NILP (window))
2463 window = Fframe_selected_window (call0 (Vpop_up_frame_function));
2464 /* If window appears above or below another,
2465 even out their heights. */
2466 other = upper = lower = Qnil;
2467 if (!NILP (XWINDOW (window)->prev))
2468 other = upper = XWINDOW (window)->prev, lower = window;
2469 if (!NILP (XWINDOW (window)->next))
2470 other = lower = XWINDOW (window)->next, upper = window;
2471 if (!NILP (other)
2472 /* Check that OTHER and WINDOW are vertically arrayed. */
2473 && !EQ (XWINDOW (other)->top, XWINDOW (window)->top)
2474 && (XFASTINT (XWINDOW (other)->height)
2475 > XFASTINT (XWINDOW (window)->height)))
2476 {
2477 int total = (XFASTINT (XWINDOW (other)->height)
2478 + XFASTINT (XWINDOW (window)->height));
2479 Lisp_Object old_selected_window;
2480 old_selected_window = selected_window;
2481
2482 selected_window = upper;
2483 change_window_height ((total / 2
2484 - XFASTINT (XWINDOW (upper)->height)),
2485 0);
2486 selected_window = old_selected_window;
2487 }
2488 }
2489 }
2490 else
2491 window = Fget_lru_window (Qnil);
2492
2493 Fset_window_buffer (window, buffer);
2494 return display_buffer_1 (window);
2495 }
2496
2497 void
2498 temp_output_buffer_show (buf)
2499 register Lisp_Object buf;
2500 {
2501 register struct buffer *old = current_buffer;
2502 register Lisp_Object window;
2503 register struct window *w;
2504
2505 XBUFFER (buf)->directory = current_buffer->directory;
2506
2507 Fset_buffer (buf);
2508 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
2509 BEGV = BEG;
2510 ZV = Z;
2511 SET_PT (BEG);
2512 XBUFFER (buf)->clip_changed = 1;
2513 set_buffer_internal (old);
2514
2515 if (!EQ (Vtemp_buffer_show_function, Qnil))
2516 call1 (Vtemp_buffer_show_function, buf);
2517 else
2518 {
2519 window = Fdisplay_buffer (buf, Qnil, Qnil);
2520
2521 if (XFRAME (XWINDOW (window)->frame) != selected_frame)
2522 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
2523 Vminibuf_scroll_window = window;
2524 w = XWINDOW (window);
2525 XSETFASTINT (w->hscroll, 0);
2526 set_marker_restricted_both (w->start, buf, 1, 1);
2527 set_marker_restricted_both (w->pointm, buf, 1, 1);
2528
2529 /* Run temp-buffer-show-hook, with the chosen window selected
2530 and it sbuffer current. */
2531 if (!NILP (Vrun_hooks))
2532 {
2533 Lisp_Object tem;
2534 tem = Fboundp (Qtemp_buffer_show_hook);
2535 if (!NILP (tem))
2536 {
2537 tem = Fsymbol_value (Qtemp_buffer_show_hook);
2538 if (!NILP (tem))
2539 {
2540 int count = specpdl_ptr - specpdl;
2541 Lisp_Object prev_window;
2542 prev_window = selected_window;
2543
2544 /* Select the window that was chosen, for running the hook. */
2545 record_unwind_protect (Fselect_window, prev_window);
2546 select_window_1 (window, 0);
2547 Fset_buffer (w->buffer);
2548 call1 (Vrun_hooks, Qtemp_buffer_show_hook);
2549 select_window_1 (prev_window, 0);
2550 unbind_to (count, Qnil);
2551 }
2552 }
2553 }
2554 }
2555 }
2556 \f
2557 static void
2558 make_dummy_parent (window)
2559 Lisp_Object window;
2560 {
2561 Lisp_Object new;
2562 register struct window *o, *p;
2563 register struct Lisp_Vector *vec;
2564 int i;
2565
2566 o = XWINDOW (window);
2567 vec = allocate_vectorlike ((EMACS_INT)VECSIZE (struct window));
2568 for (i = 0; i < VECSIZE (struct window); ++i)
2569 vec->contents[i] = ((struct Lisp_Vector *)o)->contents[i];
2570 vec->size = VECSIZE (struct window);
2571 p = (struct window *)vec;
2572 XSETWINDOW (new, p);
2573
2574 XSETFASTINT (p->sequence_number, ++sequence_number);
2575
2576 /* Put new into window structure in place of window */
2577 replace_window (window, new);
2578
2579 o->next = Qnil;
2580 o->prev = Qnil;
2581 o->vchild = Qnil;
2582 o->hchild = Qnil;
2583 o->parent = new;
2584
2585 p->start = Qnil;
2586 p->pointm = Qnil;
2587 p->buffer = Qnil;
2588 }
2589
2590 DEFUN ("split-window", Fsplit_window, Ssplit_window, 0, 3, "",
2591 "Split WINDOW, putting SIZE lines in the first of the pair.\n\
2592 WINDOW defaults to selected one and SIZE to half its size.\n\
2593 If optional third arg HORFLAG is non-nil, split side by side\n\
2594 and put SIZE columns in the first of the pair. In that case,\n\
2595 SIZE includes that window's scroll bar, or the divider column to its right.")
2596 (window, size, horflag)
2597 Lisp_Object window, size, horflag;
2598 {
2599 register Lisp_Object new;
2600 register struct window *o, *p;
2601 FRAME_PTR fo;
2602 register int size_int;
2603
2604 if (NILP (window))
2605 window = selected_window;
2606 else
2607 CHECK_LIVE_WINDOW (window, 0);
2608
2609 o = XWINDOW (window);
2610 fo = XFRAME (WINDOW_FRAME (o));
2611
2612 if (NILP (size))
2613 {
2614 if (!NILP (horflag))
2615 /* Calculate the size of the left-hand window, by dividing
2616 the usable space in columns by two.
2617 We round up, since the left-hand window may include
2618 a dividing line, while the right-hand may not. */
2619 size_int = (XFASTINT (o->width) + 1) >> 1;
2620 else
2621 size_int = XFASTINT (o->height) >> 1;
2622 }
2623 else
2624 {
2625 CHECK_NUMBER (size, 1);
2626 size_int = XINT (size);
2627 }
2628
2629 if (MINI_WINDOW_P (o))
2630 error ("Attempt to split minibuffer window");
2631
2632 check_min_window_sizes ();
2633
2634 if (NILP (horflag))
2635 {
2636 if (size_int < window_min_height)
2637 error ("Window height %d too small (after splitting)", size_int);
2638 if (size_int + window_min_height > XFASTINT (o->height))
2639 error ("Window height %d too small (after splitting)",
2640 XFASTINT (o->height) - size_int);
2641 if (NILP (o->parent)
2642 || NILP (XWINDOW (o->parent)->vchild))
2643 {
2644 make_dummy_parent (window);
2645 new = o->parent;
2646 XWINDOW (new)->vchild = window;
2647 }
2648 }
2649 else
2650 {
2651 if (size_int < window_min_width)
2652 error ("Window width %d too small (after splitting)", size_int);
2653
2654 if (size_int + window_min_width > XFASTINT (o->width))
2655 error ("Window width %d too small (after splitting)",
2656 XFASTINT (o->width) - size_int);
2657 if (NILP (o->parent)
2658 || NILP (XWINDOW (o->parent)->hchild))
2659 {
2660 make_dummy_parent (window);
2661 new = o->parent;
2662 XWINDOW (new)->hchild = window;
2663 }
2664 }
2665
2666 /* Now we know that window's parent is a vertical combination
2667 if we are dividing vertically, or a horizontal combination
2668 if we are making side-by-side windows */
2669
2670 windows_or_buffers_changed++;
2671 FRAME_WINDOW_SIZES_CHANGED (fo) = 1;
2672 new = make_window ();
2673 p = XWINDOW (new);
2674
2675 p->frame = o->frame;
2676 p->next = o->next;
2677 if (!NILP (p->next))
2678 XWINDOW (p->next)->prev = new;
2679 p->prev = window;
2680 o->next = new;
2681 p->parent = o->parent;
2682 p->buffer = Qt;
2683 p->window_end_valid = Qnil;
2684 bzero (&p->last_cursor, sizeof p->last_cursor);
2685
2686 /* Apportion the available frame space among the two new windows */
2687
2688 if (!NILP (horflag))
2689 {
2690 p->height = o->height;
2691 p->top = o->top;
2692 XSETFASTINT (p->width, XFASTINT (o->width) - size_int);
2693 XSETFASTINT (o->width, size_int);
2694 XSETFASTINT (p->left, XFASTINT (o->left) + size_int);
2695 }
2696 else
2697 {
2698 p->left = o->left;
2699 p->width = o->width;
2700 XSETFASTINT (p->height, XFASTINT (o->height) - size_int);
2701 XSETFASTINT (o->height, size_int);
2702 XSETFASTINT (p->top, XFASTINT (o->top) + size_int);
2703 }
2704
2705 /* Adjust glyph matrices. */
2706 adjust_glyphs (fo);
2707 Fset_window_buffer (new, o->buffer);
2708 return new;
2709 }
2710 \f
2711 DEFUN ("enlarge-window", Fenlarge_window, Senlarge_window, 1, 2, "p",
2712 "Make current window ARG lines bigger.\n\
2713 From program, optional second arg non-nil means grow sideways ARG columns.")
2714 (arg, side)
2715 register Lisp_Object arg, side;
2716 {
2717 CHECK_NUMBER (arg, 0);
2718 change_window_height (XINT (arg), !NILP (side));
2719
2720 if (! NILP (Vwindow_configuration_change_hook))
2721 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2722
2723 return Qnil;
2724 }
2725
2726 DEFUN ("shrink-window", Fshrink_window, Sshrink_window, 1, 2, "p",
2727 "Make current window ARG lines smaller.\n\
2728 From program, optional second arg non-nil means shrink sideways arg columns.")
2729 (arg, side)
2730 register Lisp_Object arg, side;
2731 {
2732 CHECK_NUMBER (arg, 0);
2733 change_window_height (-XINT (arg), !NILP (side));
2734
2735 if (! NILP (Vwindow_configuration_change_hook))
2736 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
2737
2738 return Qnil;
2739 }
2740
2741 int
2742 window_height (window)
2743 Lisp_Object window;
2744 {
2745 register struct window *p = XWINDOW (window);
2746 return XFASTINT (p->height);
2747 }
2748
2749 int
2750 window_width (window)
2751 Lisp_Object window;
2752 {
2753 register struct window *p = XWINDOW (window);
2754 return XFASTINT (p->width);
2755 }
2756
2757 #define MINSIZE(w) \
2758 (widthflag \
2759 ? window_min_width \
2760 : (MINI_WINDOW_P (XWINDOW (w)) ? 1 : window_min_height))
2761
2762 #define CURBEG(w) \
2763 *(widthflag ? (int *) &(XWINDOW (w)->left) : (int *) &(XWINDOW (w)->top))
2764
2765 #define CURSIZE(w) \
2766 *(widthflag ? (int *) &(XWINDOW (w)->width) : (int *) &(XWINDOW (w)->height))
2767
2768 /* Unlike set_window_height, this function
2769 also changes the heights of the siblings so as to
2770 keep everything consistent. */
2771
2772 void
2773 change_window_height (delta, widthflag)
2774 register int delta;
2775 int widthflag;
2776 {
2777 register Lisp_Object parent;
2778 Lisp_Object window;
2779 register struct window *p;
2780 int *sizep;
2781 int (*sizefun) P_ ((Lisp_Object))
2782 = widthflag ? window_width : window_height;
2783 register void (*setsizefun) P_ ((Lisp_Object, int, int))
2784 = (widthflag ? set_window_width : set_window_height);
2785 int maximum;
2786 Lisp_Object next, prev;
2787
2788 check_min_window_sizes ();
2789
2790 window = selected_window;
2791 while (1)
2792 {
2793 p = XWINDOW (window);
2794 parent = p->parent;
2795 if (NILP (parent))
2796 {
2797 if (widthflag)
2798 error ("No other window to side of this one");
2799 break;
2800 }
2801 if (widthflag ? !NILP (XWINDOW (parent)->hchild)
2802 : !NILP (XWINDOW (parent)->vchild))
2803 break;
2804 window = parent;
2805 }
2806
2807 sizep = &CURSIZE (window);
2808
2809 {
2810 register int maxdelta;
2811
2812 maxdelta = (!NILP (parent) ? (*sizefun) (parent) - *sizep
2813 : !NILP (p->next) ? (*sizefun) (p->next) - MINSIZE (p->next)
2814 : !NILP (p->prev) ? (*sizefun) (p->prev) - MINSIZE (p->prev)
2815 /* This is a frame with only one window, a minibuffer-only
2816 or a minibufferless frame. */
2817 : (delta = 0));
2818
2819 if (delta > maxdelta)
2820 /* This case traps trying to make the minibuffer
2821 the full frame, or make the only window aside from the
2822 minibuffer the full frame. */
2823 delta = maxdelta;
2824 }
2825
2826 if (*sizep + delta < MINSIZE (window))
2827 {
2828 delete_window (window);
2829 return;
2830 }
2831
2832 if (delta == 0)
2833 return;
2834
2835 /* Find the total we can get from other siblings. */
2836 maximum = 0;
2837 for (next = p->next; ! NILP (next); next = XWINDOW (next)->next)
2838 maximum += (*sizefun) (next) - MINSIZE (next);
2839 for (prev = p->prev; ! NILP (prev); prev = XWINDOW (prev)->prev)
2840 maximum += (*sizefun) (prev) - MINSIZE (prev);
2841
2842 /* If we can get it all from them, do so. */
2843 if (delta <= maximum)
2844 {
2845 Lisp_Object first_unaffected;
2846 Lisp_Object first_affected;
2847
2848 next = p->next;
2849 prev = p->prev;
2850 first_affected = window;
2851 /* Look at one sibling at a time,
2852 moving away from this window in both directions alternately,
2853 and take as much as we can get without deleting that sibling. */
2854 while (delta != 0)
2855 {
2856 if (delta == 0)
2857 break;
2858 if (! NILP (next))
2859 {
2860 int this_one = (*sizefun) (next) - MINSIZE (next);
2861 if (this_one > delta)
2862 this_one = delta;
2863
2864 (*setsizefun) (next, (*sizefun) (next) - this_one, 0);
2865 (*setsizefun) (window, *sizep + this_one, 0);
2866
2867 delta -= this_one;
2868 next = XWINDOW (next)->next;
2869 }
2870 if (delta == 0)
2871 break;
2872 if (! NILP (prev))
2873 {
2874 int this_one = (*sizefun) (prev) - MINSIZE (prev);
2875 if (this_one > delta)
2876 this_one = delta;
2877
2878 first_affected = prev;
2879
2880 (*setsizefun) (prev, (*sizefun) (prev) - this_one, 0);
2881 (*setsizefun) (window, *sizep + this_one, 0);
2882
2883 delta -= this_one;
2884 prev = XWINDOW (prev)->prev;
2885 }
2886 }
2887
2888 /* Now recalculate the edge positions of all the windows affected,
2889 based on the new sizes. */
2890 first_unaffected = next;
2891 prev = first_affected;
2892 for (next = XWINDOW (prev)->next; ! EQ (next, first_unaffected);
2893 prev = next, next = XWINDOW (next)->next)
2894 {
2895 CURBEG (next) = CURBEG (prev) + (*sizefun) (prev);
2896 /* This does not change size of NEXT,
2897 but it propagates the new top edge to its children */
2898 (*setsizefun) (next, (*sizefun) (next), 0);
2899 }
2900 }
2901 else
2902 {
2903 register int delta1;
2904 register int opht = (*sizefun) (parent);
2905
2906 /* If trying to grow this window to or beyond size of the parent,
2907 make delta1 so big that, on shrinking back down,
2908 all the siblings end up with less than one line and are deleted. */
2909 if (opht <= *sizep + delta)
2910 delta1 = opht * opht * 2;
2911 /* Otherwise, make delta1 just right so that if we add delta1
2912 lines to this window and to the parent, and then shrink
2913 the parent back to its original size, the new proportional
2914 size of this window will increase by delta. */
2915 else
2916 delta1 = (delta * opht * 100) / ((opht - *sizep - delta) * 100);
2917
2918 /* Add delta1 lines or columns to this window, and to the parent,
2919 keeping things consistent while not affecting siblings. */
2920 CURSIZE (parent) = opht + delta1;
2921 (*setsizefun) (window, *sizep + delta1, 0);
2922
2923 /* Squeeze out delta1 lines or columns from our parent,
2924 shriking this window and siblings proportionately.
2925 This brings parent back to correct size.
2926 Delta1 was calculated so this makes this window the desired size,
2927 taking it all out of the siblings. */
2928 (*setsizefun) (parent, opht, 0);
2929 }
2930
2931 XSETFASTINT (p->last_modified, 0);
2932 XSETFASTINT (p->last_overlay_modified, 0);
2933
2934 /* Adjust glyph matrices. */
2935 adjust_glyphs (XFRAME (WINDOW_FRAME (XWINDOW (window))));
2936 }
2937 #undef MINSIZE
2938 #undef CURBEG
2939 #undef CURSIZE
2940
2941
2942 /* Mark window cursors off for all windows in the window tree rooted
2943 at W by setting their phys_cursor_on_p flag to zero. Called from
2944 xterm.c, e.g. when a frame is cleared and thereby all cursors on
2945 the frame are cleared. */
2946
2947 void
2948 mark_window_cursors_off (w)
2949 struct window *w;
2950 {
2951 while (w)
2952 {
2953 if (!NILP (w->hchild))
2954 mark_window_cursors_off (XWINDOW (w->hchild));
2955 else if (!NILP (w->vchild))
2956 mark_window_cursors_off (XWINDOW (w->vchild));
2957 else
2958 w->phys_cursor_on_p = 0;
2959
2960 w = NILP (w->next) ? 0 : XWINDOW (w->next);
2961 }
2962 }
2963
2964
2965 /* Return number of lines of text (not counting mode line) in W. */
2966
2967 int
2968 window_internal_height (w)
2969 struct window *w;
2970 {
2971 int ht = XFASTINT (w->height);
2972
2973 if (MINI_WINDOW_P (w))
2974 return ht;
2975
2976 if (!NILP (w->parent) || !NILP (w->vchild) || !NILP (w->hchild)
2977 || !NILP (w->next) || !NILP (w->prev)
2978 || FRAME_WANTS_MODELINE_P (XFRAME (WINDOW_FRAME (w))))
2979 return ht - 1;
2980
2981 return ht;
2982 }
2983
2984
2985 /* Return the number of columns in W.
2986 Don't count columns occupied by scroll bars or the vertical bar
2987 separating W from the sibling to its right. */
2988
2989 int
2990 window_internal_width (w)
2991 struct window *w;
2992 {
2993 struct frame *f = XFRAME (WINDOW_FRAME (w));
2994 int width = XINT (w->width);
2995
2996 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2997 /* Scroll bars occupy a few columns. */
2998 width -= FRAME_SCROLL_BAR_COLS (f);
2999 else if (!WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
3000 /* The column of `|' characters separating side-by-side windows
3001 occupies one column only. */
3002 width -= 1;
3003
3004 /* On window-systems, areas to the left and right of the window
3005 are used to display bitmaps there. */
3006 if (FRAME_WINDOW_P (f))
3007 width -= 2 * FRAME_FLAGS_AREA_COLS (f);
3008
3009 return width;
3010 }
3011
3012 \f
3013 /************************************************************************
3014 Window Scrolling
3015 ***********************************************************************/
3016
3017 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
3018 one screen-full, which is defined as the height of the window minus
3019 next_screen_context_lines. If WHOLE is zero, scroll up N lines
3020 instead. Negative values of N mean scroll down. NOERROR non-zero
3021 means don't signal an error if we try to move over BEGV or ZV,
3022 respectively. */
3023
3024 static void
3025 window_scroll (window, n, whole, noerror)
3026 Lisp_Object window;
3027 int n;
3028 int whole;
3029 int noerror;
3030 {
3031 /* If we must, use the pixel-based version which is much slower than
3032 the line-based one but can handle varying line heights. */
3033 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
3034 window_scroll_pixel_based (window, n, whole, noerror);
3035 else
3036 window_scroll_line_based (window, n, whole, noerror);
3037 }
3038
3039
3040 /* Implementation of window_scroll that works based on pixel line
3041 heights. See the comment of window_scroll for parameter
3042 descriptions. */
3043
3044 static void
3045 window_scroll_pixel_based (window, n, whole, noerror)
3046 Lisp_Object window;
3047 int n;
3048 int whole;
3049 int noerror;
3050 {
3051 struct it it;
3052 struct window *w = XWINDOW (window);
3053 struct text_pos start;
3054 Lisp_Object tem;
3055 int this_scroll_margin;
3056 int preserve_y;
3057
3058 SET_TEXT_POS_FROM_MARKER (start, w->start);
3059
3060 /* If PT is not visible in WINDOW, move back one half of
3061 the screen. */
3062 XSETFASTINT (tem, PT);
3063 tem = Fpos_visible_in_window_p (tem, window);
3064 if (NILP (tem))
3065 {
3066 /* Move backward half the height of the window. Performance note:
3067 vmotion used here is about 10% faster, but would give wrong
3068 results for variable height lines. */
3069 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
3070 it.current_y = it.last_visible_y;
3071 move_it_vertically (&it, -it.last_visible_y / 2);
3072
3073 /* The function move_iterator_vertically may move over more than
3074 the specified y-distance. If it->w is small, e.g. a
3075 mini-buffer window, we may end up in front of the window's
3076 display area. This is the case when Start displaying at the
3077 start of the line containing PT in this case. */
3078 if (it.current_y <= 0)
3079 {
3080 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
3081 move_it_vertically (&it, 0);
3082 it.current_y = 0;
3083 }
3084
3085 start = it.current.pos;
3086 }
3087
3088 /* If scroll_preserve_screen_position is non-zero, we try to set
3089 point in the same window line as it is now, so get that line. */
3090 if (!NILP (Vscroll_preserve_screen_position))
3091 {
3092 start_display (&it, w, start);
3093 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
3094 preserve_y = it.current_y;
3095 }
3096 else
3097 preserve_y = -1;
3098
3099 /* Move iterator it from start the specified distance forward or
3100 backward. The result is the new window start. */
3101 start_display (&it, w, start);
3102 if (whole)
3103 {
3104 int screen_full = (it.last_visible_y
3105 - next_screen_context_lines * CANON_Y_UNIT (it.f));
3106 int direction = n < 0 ? -1 : 1;
3107 move_it_vertically (&it, direction * screen_full);
3108 }
3109 else
3110 move_it_by_lines (&it, n, 1);
3111
3112 /* End if we end up at ZV or BEGV. */
3113 if ((n > 0 && IT_CHARPOS (it) == ZV)
3114 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
3115 {
3116 if (noerror)
3117 return;
3118 else if (IT_CHARPOS (it) == ZV)
3119 Fsignal (Qend_of_buffer, Qnil);
3120 else
3121 Fsignal (Qbeginning_of_buffer, Qnil);
3122 }
3123
3124 /* Set the window start, and set up the window for redisplay. */
3125 set_marker_restricted (w->start, make_number (IT_CHARPOS (it)), w->buffer);
3126 w->start_at_line_beg = Fbolp ();
3127 w->update_mode_line = Qt;
3128 XSETFASTINT (w->last_modified, 0);
3129 XSETFASTINT (w->last_overlay_modified, 0);
3130 /* Set force_start so that redisplay_window will run the
3131 window-scroll-functions. */
3132 w->force_start = Qt;
3133
3134 it.current_y = it.vpos = 0;
3135
3136 /* Preserve the screen position if we must. */
3137 if (preserve_y >= 0)
3138 {
3139 move_it_to (&it, -1, -1, preserve_y, -1, MOVE_TO_Y);
3140 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3141 }
3142 else
3143 {
3144 /* Move PT out of scroll margins. */
3145 this_scroll_margin = max (0, scroll_margin);
3146 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->height) / 4);
3147 this_scroll_margin *= CANON_Y_UNIT (it.f);
3148
3149 if (n > 0)
3150 {
3151 /* We moved the window start towards ZV, so PT may be now
3152 in the scroll margin at the top. */
3153 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
3154 while (it.current_y < this_scroll_margin)
3155 move_it_by_lines (&it, 1, 1);
3156 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3157 }
3158 else if (n < 0)
3159 {
3160 /* We moved the window start towards BEGV, so PT may be now
3161 in the scroll margin at the bottom. */
3162 move_it_to (&it, PT, -1,
3163 it.last_visible_y - this_scroll_margin - 1, -1,
3164 MOVE_TO_POS | MOVE_TO_Y);
3165
3166 /* Don't put point on a partially visible line at the end. */
3167 if (it.current_y + it.max_ascent + it.max_descent
3168 > it.last_visible_y)
3169 move_it_by_lines (&it, -1, 0);
3170
3171 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
3172 }
3173 }
3174 }
3175
3176
3177 /* Implementation of window_scroll that works based on screen lines.
3178 See the comment of window_scroll for parameter descriptions. */
3179
3180 static void
3181 window_scroll_line_based (window, n, whole, noerror)
3182 Lisp_Object window;
3183 int n;
3184 int whole;
3185 int noerror;
3186 {
3187 register struct window *w = XWINDOW (window);
3188 register int opoint = PT, opoint_byte = PT_BYTE;
3189 register int pos, pos_byte;
3190 register int ht = window_internal_height (w);
3191 register Lisp_Object tem;
3192 int lose;
3193 Lisp_Object bolp;
3194 int startpos;
3195 struct position posit;
3196 int original_vpos;
3197
3198 startpos = marker_position (w->start);
3199
3200 posit = *compute_motion (startpos, 0, 0, 0,
3201 PT, ht, 0,
3202 window_internal_width (w), XINT (w->hscroll),
3203 0, w);
3204 original_vpos = posit.vpos;
3205
3206 XSETFASTINT (tem, PT);
3207 tem = Fpos_visible_in_window_p (tem, window);
3208
3209 if (NILP (tem))
3210 {
3211 Fvertical_motion (make_number (- (ht / 2)), window);
3212 startpos = PT;
3213 }
3214
3215 SET_PT (startpos);
3216 lose = n < 0 && PT == BEGV;
3217 Fvertical_motion (make_number (n), window);
3218 pos = PT;
3219 pos_byte = PT_BYTE;
3220 bolp = Fbolp ();
3221 SET_PT_BOTH (opoint, opoint_byte);
3222
3223 if (lose)
3224 {
3225 if (noerror)
3226 return;
3227 else
3228 Fsignal (Qbeginning_of_buffer, Qnil);
3229 }
3230
3231 if (pos < ZV)
3232 {
3233 int this_scroll_margin = scroll_margin;
3234
3235 /* Don't use a scroll margin that is negative or too large. */
3236 if (this_scroll_margin < 0)
3237 this_scroll_margin = 0;
3238
3239 if (XINT (w->height) < 4 * scroll_margin)
3240 this_scroll_margin = XINT (w->height) / 4;
3241
3242 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
3243 w->start_at_line_beg = bolp;
3244 w->update_mode_line = Qt;
3245 XSETFASTINT (w->last_modified, 0);
3246 XSETFASTINT (w->last_overlay_modified, 0);
3247 /* Set force_start so that redisplay_window will run
3248 the window-scroll-functions. */
3249 w->force_start = Qt;
3250
3251 if (whole && !NILP (Vscroll_preserve_screen_position))
3252 {
3253 SET_PT_BOTH (pos, pos_byte);
3254 Fvertical_motion (make_number (original_vpos), window);
3255 }
3256 /* If we scrolled forward, put point enough lines down
3257 that it is outside the scroll margin. */
3258 else if (n > 0)
3259 {
3260 int top_margin;
3261
3262 if (this_scroll_margin > 0)
3263 {
3264 SET_PT_BOTH (pos, pos_byte);
3265 Fvertical_motion (make_number (this_scroll_margin), window);
3266 top_margin = PT;
3267 }
3268 else
3269 top_margin = pos;
3270
3271 if (top_margin <= opoint)
3272 SET_PT_BOTH (opoint, opoint_byte);
3273 else if (!NILP (Vscroll_preserve_screen_position))
3274 {
3275 SET_PT_BOTH (pos, pos_byte);
3276 Fvertical_motion (make_number (original_vpos), window);
3277 }
3278 else
3279 SET_PT (top_margin);
3280 }
3281 else if (n < 0)
3282 {
3283 int bottom_margin;
3284
3285 /* If we scrolled backward, put point near the end of the window
3286 but not within the scroll margin. */
3287 SET_PT_BOTH (pos, pos_byte);
3288 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
3289 if (XFASTINT (tem) == ht - this_scroll_margin)
3290 bottom_margin = PT;
3291 else
3292 bottom_margin = PT + 1;
3293
3294 if (bottom_margin > opoint)
3295 SET_PT_BOTH (opoint, opoint_byte);
3296 else
3297 {
3298 if (!NILP (Vscroll_preserve_screen_position))
3299 {
3300 SET_PT_BOTH (pos, pos_byte);
3301 Fvertical_motion (make_number (original_vpos), window);
3302 }
3303 else
3304 Fvertical_motion (make_number (-1), window);
3305 }
3306 }
3307 }
3308 else
3309 {
3310 if (noerror)
3311 return;
3312 else
3313 Fsignal (Qend_of_buffer, Qnil);
3314 }
3315 }
3316
3317
3318 /* Scroll selected_window up or down. If N is nil, scroll a
3319 screen-full which is defined as the height of the window minus
3320 next_screen_context_lines. If N is the symbol `-', scroll.
3321 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
3322 up. This is the guts of Fscroll_up and Fscroll_down. */
3323
3324 static void
3325 scroll_command (n, direction)
3326 Lisp_Object n;
3327 int direction;
3328 {
3329 register int defalt;
3330 int count = specpdl_ptr - specpdl;
3331
3332 xassert (abs (direction) == 1);
3333
3334 /* If selected window's buffer isn't current, make it current for
3335 the moment. But don't screw up if window_scroll gets an error. */
3336 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
3337 {
3338 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3339 Fset_buffer (XWINDOW (selected_window)->buffer);
3340
3341 /* Make redisplay consider other windows than just selected_window. */
3342 ++windows_or_buffers_changed;
3343 }
3344
3345 defalt = (window_internal_height (XWINDOW (selected_window))
3346 - next_screen_context_lines);
3347 defalt = direction * (defalt < 1 ? 1 : defalt);
3348
3349 if (NILP (n))
3350 window_scroll (selected_window, defalt, 1, 0);
3351 else if (EQ (n, Qminus))
3352 window_scroll (selected_window, - defalt, 1, 0);
3353 else
3354 {
3355 n = Fprefix_numeric_value (n);
3356 window_scroll (selected_window, XINT (n) * direction, 0, 0);
3357 }
3358
3359 unbind_to (count, Qnil);
3360 }
3361
3362 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "P",
3363 "Scroll text of current window upward ARG lines; or near full screen if no ARG.\n\
3364 A near full screen is `next-screen-context-lines' less than a full screen.\n\
3365 Negative ARG means scroll downward.\n\
3366 If ARG is the atom `-', scroll downward by nearly full screen.\n\
3367 When calling from a program, supply as argument a number, nil, or `-'.")
3368 (arg)
3369 Lisp_Object arg;
3370 {
3371 scroll_command (arg, 1);
3372 return Qnil;
3373 }
3374
3375 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "P",
3376 "Scroll text of current window down ARG lines; or near full screen if no ARG.\n\
3377 A near full screen is `next-screen-context-lines' less than a full screen.\n\
3378 Negative ARG means scroll upward.\n\
3379 If ARG is the atom `-', scroll upward by nearly full screen.\n\
3380 When calling from a program, supply as argument a number, nil, or `-'.")
3381 (arg)
3382 Lisp_Object arg;
3383 {
3384 scroll_command (arg, -1);
3385 return Qnil;
3386 }
3387 \f
3388 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
3389 "Return the other window for \"other window scroll\" commands.\n\
3390 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
3391 specifies the window.\n\
3392 If `other-window-scroll-buffer' is non-nil, a window\n\
3393 showing that buffer is used.")
3394 ()
3395 {
3396 Lisp_Object window;
3397
3398 if (MINI_WINDOW_P (XWINDOW (selected_window))
3399 && !NILP (Vminibuf_scroll_window))
3400 window = Vminibuf_scroll_window;
3401 /* If buffer is specified, scroll that buffer. */
3402 else if (!NILP (Vother_window_scroll_buffer))
3403 {
3404 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
3405 if (NILP (window))
3406 window = Fdisplay_buffer (Vother_window_scroll_buffer, Qt, Qnil);
3407 }
3408 else
3409 {
3410 /* Nothing specified; look for a neighboring window on the same
3411 frame. */
3412 window = Fnext_window (selected_window, Qnil, Qnil);
3413
3414 if (EQ (window, selected_window))
3415 /* That didn't get us anywhere; look for a window on another
3416 visible frame. */
3417 do
3418 window = Fnext_window (window, Qnil, Qt);
3419 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
3420 && ! EQ (window, selected_window));
3421 }
3422
3423 CHECK_LIVE_WINDOW (window, 0);
3424
3425 if (EQ (window, selected_window))
3426 error ("There is no other window");
3427
3428 return window;
3429 }
3430
3431 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
3432 "Scroll next window upward ARG lines; or near full screen if no ARG.\n\
3433 A near full screen is `next-screen-context-lines' less than a full screen.\n\
3434 The next window is the one below the current one; or the one at the top\n\
3435 if the current one is at the bottom. Negative ARG means scroll downward.\n\
3436 If ARG is the atom `-', scroll downward by nearly full screen.\n\
3437 When calling from a program, supply as argument a number, nil, or `-'.\n\
3438 \n\
3439 If in the minibuffer, `minibuffer-scroll-window' if non-nil\n\
3440 specifies the window to scroll.\n\
3441 If `other-window-scroll-buffer' is non-nil, scroll the window\n\
3442 showing that buffer, popping the buffer up if necessary.")
3443 (arg)
3444 register Lisp_Object arg;
3445 {
3446 register Lisp_Object window;
3447 register int defalt;
3448 register struct window *w;
3449 register int count = specpdl_ptr - specpdl;
3450
3451 window = Fother_window_for_scrolling ();
3452
3453 w = XWINDOW (window);
3454 defalt = window_internal_height (w) - next_screen_context_lines;
3455 if (defalt < 1) defalt = 1;
3456
3457 /* Don't screw up if window_scroll gets an error. */
3458 record_unwind_protect (save_excursion_restore, save_excursion_save ());
3459 ++windows_or_buffers_changed;
3460
3461 Fset_buffer (w->buffer);
3462 SET_PT (marker_position (w->pointm));
3463
3464 if (NILP (arg))
3465 window_scroll (window, defalt, 1, 1);
3466 else if (EQ (arg, Qminus))
3467 window_scroll (window, -defalt, 1, 1);
3468 else
3469 {
3470 if (CONSP (arg))
3471 arg = Fcar (arg);
3472 CHECK_NUMBER (arg, 0);
3473 window_scroll (window, XINT (arg), 0, 1);
3474 }
3475
3476 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
3477 unbind_to (count, Qnil);
3478
3479 return Qnil;
3480 }
3481 \f
3482 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 1, "P",
3483 "Scroll selected window display ARG columns left.\n\
3484 Default for ARG is window width minus 2.")
3485 (arg)
3486 register Lisp_Object arg;
3487 {
3488
3489 if (NILP (arg))
3490 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3491 else
3492 arg = Fprefix_numeric_value (arg);
3493
3494 return
3495 Fset_window_hscroll (selected_window,
3496 make_number (XINT (XWINDOW (selected_window)->hscroll)
3497 + XINT (arg)));
3498 }
3499
3500 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 1, "P",
3501 "Scroll selected window display ARG columns right.\n\
3502 Default for ARG is window width minus 2.")
3503 (arg)
3504 register Lisp_Object arg;
3505 {
3506 if (NILP (arg))
3507 XSETFASTINT (arg, window_internal_width (XWINDOW (selected_window)) - 2);
3508 else
3509 arg = Fprefix_numeric_value (arg);
3510
3511 return
3512 Fset_window_hscroll (selected_window,
3513 make_number (XINT (XWINDOW (selected_window)->hscroll)
3514 - XINT (arg)));
3515 }
3516
3517 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
3518 "Center point in window and redisplay frame. With ARG, put point on line ARG.\n\
3519 The desired position of point is always relative to the current window.\n\
3520 Just C-u as prefix means put point in the center of the window.\n\
3521 If ARG is omitted or nil, erases the entire frame and then\n\
3522 redraws with point in the center of the current window.")
3523 (arg)
3524 register Lisp_Object arg;
3525 {
3526 register struct window *w = XWINDOW (selected_window);
3527 register int ht = window_internal_height (w);
3528 struct position pos;
3529 struct buffer *buf = XBUFFER (w->buffer);
3530 struct buffer *obuf = current_buffer;
3531
3532 if (NILP (arg))
3533 {
3534 extern int frame_garbaged;
3535
3536 SET_FRAME_GARBAGED (XFRAME (WINDOW_FRAME (w)));
3537 XSETFASTINT (arg, ht / 2);
3538 }
3539 else if (CONSP (arg)) /* Just C-u. */
3540 {
3541 XSETFASTINT (arg, ht / 2);
3542 }
3543 else
3544 {
3545 arg = Fprefix_numeric_value (arg);
3546 CHECK_NUMBER (arg, 0);
3547 }
3548
3549 if (XINT (arg) < 0)
3550 XSETINT (arg, XINT (arg) + ht);
3551
3552 set_buffer_internal (buf);
3553 pos = *vmotion (PT, - XINT (arg), w);
3554
3555 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
3556 w->start_at_line_beg = ((pos.bytepos == BEGV_BYTE
3557 || FETCH_BYTE (pos.bytepos - 1) == '\n')
3558 ? Qt : Qnil);
3559 w->force_start = Qt;
3560 set_buffer_internal (obuf);
3561
3562 return Qnil;
3563 }
3564 \f
3565 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
3566 1, 1, "P",
3567 "Position point relative to window.\n\
3568 With no argument, position point at center of window.\n\
3569 An argument specifies vertical position within the window;\n\
3570 zero means top of window, negative means relative to bottom of window.")
3571 (arg)
3572 register Lisp_Object arg;
3573 {
3574 register struct window *w = XWINDOW (selected_window);
3575 register int height = window_internal_height (w);
3576 register int start;
3577 Lisp_Object window;
3578
3579 if (NILP (arg))
3580 XSETFASTINT (arg, height / 2);
3581 else
3582 {
3583 arg = Fprefix_numeric_value (arg);
3584 if (XINT (arg) < 0)
3585 XSETINT (arg, XINT (arg) + height);
3586 }
3587
3588 start = marker_position (w->start);
3589 XSETWINDOW (window, w);
3590 if (start < BEGV || start > ZV)
3591 {
3592 Fvertical_motion (make_number (- (height / 2)), window);
3593 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
3594 w->start_at_line_beg = Fbolp ();
3595 w->force_start = Qt;
3596 }
3597 else
3598 Fgoto_char (w->start);
3599
3600 return Fvertical_motion (arg, window);
3601 }
3602
3603
3604 \f
3605 /***********************************************************************
3606 Window Configuration
3607 ***********************************************************************/
3608
3609 struct save_window_data
3610 {
3611 EMACS_INT size_from_Lisp_Vector_struct;
3612 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3613 Lisp_Object frame_width, frame_height, frame_menu_bar_lines;
3614 Lisp_Object frame_toolbar_lines;
3615 Lisp_Object selected_frame;
3616 Lisp_Object current_window;
3617 Lisp_Object current_buffer;
3618 Lisp_Object minibuf_scroll_window;
3619 Lisp_Object root_window;
3620 Lisp_Object focus_frame;
3621 /* Record the values of window-min-width and window-min-height
3622 so that window sizes remain consistent with them. */
3623 Lisp_Object min_width, min_height;
3624 /* A vector, each of whose elements is a struct saved_window
3625 for one window. */
3626 Lisp_Object saved_windows;
3627 };
3628
3629 /* This is saved as a Lisp_Vector */
3630 struct saved_window
3631 {
3632 /* these first two must agree with struct Lisp_Vector in lisp.h */
3633 EMACS_INT size_from_Lisp_Vector_struct;
3634 struct Lisp_Vector *next_from_Lisp_Vector_struct;
3635
3636 Lisp_Object window;
3637 Lisp_Object buffer, start, pointm, mark;
3638 Lisp_Object left, top, width, height, hscroll;
3639 Lisp_Object parent, prev;
3640 Lisp_Object start_at_line_beg;
3641 Lisp_Object display_table;
3642 };
3643 #define SAVED_WINDOW_VECTOR_SIZE 14 /* Arg to Fmake_vector */
3644
3645 #define SAVED_WINDOW_N(swv,n) \
3646 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
3647
3648 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
3649 "Return t if OBJECT is a window-configuration object.")
3650 (object)
3651 Lisp_Object object;
3652 {
3653 if (WINDOW_CONFIGURATIONP (object))
3654 return Qt;
3655 return Qnil;
3656 }
3657
3658 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
3659 "Return the frame that CONFIG, a window-configuration object, is about.")
3660 (config)
3661 Lisp_Object config;
3662 {
3663 register struct save_window_data *data;
3664 struct Lisp_Vector *saved_windows;
3665
3666 if (! WINDOW_CONFIGURATIONP (config))
3667 wrong_type_argument (Qwindow_configuration_p, config);
3668
3669 data = (struct save_window_data *) XVECTOR (config);
3670 saved_windows = XVECTOR (data->saved_windows);
3671 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3672 }
3673
3674 DEFUN ("set-window-configuration", Fset_window_configuration,
3675 Sset_window_configuration, 1, 1, 0,
3676 "Set the configuration of windows and buffers as specified by CONFIGURATION.\n\
3677 CONFIGURATION must be a value previously returned\n\
3678 by `current-window-configuration' (which see).\n\
3679 If CONFIGURATION was made from a frame that is now deleted,\n\
3680 only frame-independent values can be restored. In this case,\n\
3681 the return value is nil. Otherwise the value is t.")
3682 (configuration)
3683 Lisp_Object configuration;
3684 {
3685 register struct save_window_data *data;
3686 struct Lisp_Vector *saved_windows;
3687 Lisp_Object new_current_buffer;
3688 Lisp_Object frame;
3689 FRAME_PTR f;
3690 int old_point = -1;
3691
3692 while (!WINDOW_CONFIGURATIONP (configuration))
3693 wrong_type_argument (Qwindow_configuration_p, configuration);
3694
3695 data = (struct save_window_data *) XVECTOR (configuration);
3696 saved_windows = XVECTOR (data->saved_windows);
3697
3698 new_current_buffer = data->current_buffer;
3699 if (NILP (XBUFFER (new_current_buffer)->name))
3700 new_current_buffer = Qnil;
3701 else
3702 {
3703 if (XBUFFER (new_current_buffer) == current_buffer)
3704 old_point = PT;
3705
3706 }
3707
3708 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
3709 f = XFRAME (frame);
3710
3711 /* If f is a dead frame, don't bother rebuilding its window tree.
3712 However, there is other stuff we should still try to do below. */
3713 if (FRAME_LIVE_P (f))
3714 {
3715 register struct window *w;
3716 register struct saved_window *p;
3717 struct window *root_window;
3718 struct window **leaf_windows;
3719 int n_leaf_windows;
3720 int k, i;
3721
3722 /* If the frame has been resized since this window configuration was
3723 made, we change the frame to the size specified in the
3724 configuration, restore the configuration, and then resize it
3725 back. We keep track of the prevailing height in these variables. */
3726 int previous_frame_height = FRAME_HEIGHT (f);
3727 int previous_frame_width = FRAME_WIDTH (f);
3728 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
3729 int previous_frame_toolbar_lines = FRAME_TOOLBAR_LINES (f);
3730
3731 /* The mouse highlighting code could get screwed up
3732 if it runs during this. */
3733 BLOCK_INPUT;
3734
3735 if (XFASTINT (data->frame_height) != previous_frame_height
3736 || XFASTINT (data->frame_width) != previous_frame_width)
3737 change_frame_size (f, XFASTINT (data->frame_height),
3738 XFASTINT (data->frame_width), 0, 0);
3739 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3740 if (XFASTINT (data->frame_menu_bar_lines)
3741 != previous_frame_menu_bar_lines)
3742 x_set_menu_bar_lines (f, data->frame_menu_bar_lines, make_number (0));
3743 if (XFASTINT (data->frame_toolbar_lines)
3744 != previous_frame_toolbar_lines)
3745 x_set_toolbar_lines (f, data->frame_toolbar_lines, make_number (0));
3746 #endif
3747
3748 if (! NILP (XWINDOW (selected_window)->buffer))
3749 {
3750 w = XWINDOW (selected_window);
3751 set_marker_both (w->pointm,
3752 w->buffer,
3753 BUF_PT (XBUFFER (w->buffer)),
3754 BUF_PT_BYTE (XBUFFER (w->buffer)));
3755 }
3756
3757 windows_or_buffers_changed++;
3758 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3759
3760 /* Problem: Freeing all matrices and later allocating them again
3761 is a serious redisplay flickering problem. What we would
3762 really like to do is to free only those matrices not reused
3763 below. */
3764 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
3765 leaf_windows
3766 = (struct window **) alloca (count_windows (root_window)
3767 * sizeof (struct window *));
3768 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
3769
3770 /* Temporarily avoid any problems with windows that are smaller
3771 than they are supposed to be. */
3772 window_min_height = 1;
3773 window_min_width = 1;
3774
3775 /* Kludge Alert!
3776 Mark all windows now on frame as "deleted".
3777 Restoring the new configuration "undeletes" any that are in it.
3778
3779 Save their current buffers in their height fields, since we may
3780 need it later, if a buffer saved in the configuration is now
3781 dead. */
3782 delete_all_subwindows (XWINDOW (FRAME_ROOT_WINDOW (f)));
3783
3784 for (k = 0; k < saved_windows->size; k++)
3785 {
3786 p = SAVED_WINDOW_N (saved_windows, k);
3787 w = XWINDOW (p->window);
3788 w->next = Qnil;
3789
3790 if (!NILP (p->parent))
3791 w->parent = SAVED_WINDOW_N (saved_windows,
3792 XFASTINT (p->parent))->window;
3793 else
3794 w->parent = Qnil;
3795
3796 if (!NILP (p->prev))
3797 {
3798 w->prev = SAVED_WINDOW_N (saved_windows,
3799 XFASTINT (p->prev))->window;
3800 XWINDOW (w->prev)->next = p->window;
3801 }
3802 else
3803 {
3804 w->prev = Qnil;
3805 if (!NILP (w->parent))
3806 {
3807 if (EQ (p->width, XWINDOW (w->parent)->width))
3808 {
3809 XWINDOW (w->parent)->vchild = p->window;
3810 XWINDOW (w->parent)->hchild = Qnil;
3811 }
3812 else
3813 {
3814 XWINDOW (w->parent)->hchild = p->window;
3815 XWINDOW (w->parent)->vchild = Qnil;
3816 }
3817 }
3818 }
3819
3820 /* If we squirreled away the buffer in the window's height,
3821 restore it now. */
3822 if (BUFFERP (w->height))
3823 w->buffer = w->height;
3824 w->left = p->left;
3825 w->top = p->top;
3826 w->width = p->width;
3827 w->height = p->height;
3828 w->hscroll = p->hscroll;
3829 w->display_table = p->display_table;
3830 XSETFASTINT (w->last_modified, 0);
3831 XSETFASTINT (w->last_overlay_modified, 0);
3832
3833 /* Reinstall the saved buffer and pointers into it. */
3834 if (NILP (p->buffer))
3835 w->buffer = p->buffer;
3836 else
3837 {
3838 if (!NILP (XBUFFER (p->buffer)->name))
3839 /* If saved buffer is alive, install it. */
3840 {
3841 w->buffer = p->buffer;
3842 w->start_at_line_beg = p->start_at_line_beg;
3843 set_marker_restricted (w->start, p->start, w->buffer);
3844 set_marker_restricted (w->pointm, p->pointm, w->buffer);
3845 Fset_marker (XBUFFER (w->buffer)->mark,
3846 p->mark, w->buffer);
3847
3848 /* As documented in Fcurrent_window_configuration, don't
3849 save the location of point in the buffer which was current
3850 when the window configuration was recorded. */
3851 if (!EQ (p->buffer, new_current_buffer)
3852 && XBUFFER (p->buffer) == current_buffer)
3853 Fgoto_char (w->pointm);
3854 }
3855 else if (NILP (w->buffer) || NILP (XBUFFER (w->buffer)->name))
3856 /* Else unless window has a live buffer, get one. */
3857 {
3858 w->buffer = Fcdr (Fcar (Vbuffer_alist));
3859 /* This will set the markers to beginning of visible
3860 range. */
3861 set_marker_restricted (w->start, make_number (0), w->buffer);
3862 set_marker_restricted (w->pointm, make_number (0),w->buffer);
3863 w->start_at_line_beg = Qt;
3864 }
3865 else
3866 /* Keeping window's old buffer; make sure the markers
3867 are real. */
3868 {
3869 /* Set window markers at start of visible range. */
3870 if (XMARKER (w->start)->buffer == 0)
3871 set_marker_restricted (w->start, make_number (0),
3872 w->buffer);
3873 if (XMARKER (w->pointm)->buffer == 0)
3874 set_marker_restricted_both (w->pointm, w->buffer,
3875 BUF_PT (XBUFFER (w->buffer)),
3876 BUF_PT_BYTE (XBUFFER (w->buffer)));
3877 w->start_at_line_beg = Qt;
3878 }
3879 }
3880 }
3881
3882 FRAME_ROOT_WINDOW (f) = data->root_window;
3883 Fselect_window (data->current_window);
3884 XBUFFER (XWINDOW (selected_window)->buffer)->last_selected_window
3885 = selected_window;
3886
3887 if (NILP (data->focus_frame)
3888 || (FRAMEP (data->focus_frame)
3889 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
3890 Fredirect_frame_focus (frame, data->focus_frame);
3891
3892 #if 0 /* I don't understand why this is needed, and it causes problems
3893 when the frame's old selected window has been deleted. */
3894 if (f != selected_frame && FRAME_WINDOW_P (f))
3895 do_switch_frame (WINDOW_FRAME (XWINDOW (data->root_window)),
3896 Qnil, 0);
3897 #endif
3898
3899 /* Set the screen height to the value it had before this function. */
3900 if (previous_frame_height != FRAME_HEIGHT (f)
3901 || previous_frame_width != FRAME_WIDTH (f))
3902 change_frame_size (f, previous_frame_height, previous_frame_width,
3903 0, 0);
3904 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
3905 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
3906 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
3907 make_number (0));
3908 if (previous_frame_toolbar_lines != FRAME_TOOLBAR_LINES (f))
3909 x_set_toolbar_lines (f, make_number (previous_frame_toolbar_lines),
3910 make_number (0));
3911 #endif
3912
3913 /* Now, free glyph matrices in windows that were not reused. */
3914 for (i = 0; i < n_leaf_windows; ++i)
3915 if (NILP (leaf_windows[i]->buffer))
3916 {
3917 /* Assert it's not reused as a combination. */
3918 xassert (NILP (leaf_windows[i]->hchild)
3919 && NILP (leaf_windows[i]->vchild));
3920 free_window_matrices (leaf_windows[i]);
3921 SET_FRAME_GARBAGED (f);
3922 }
3923
3924 adjust_glyphs (f);
3925
3926 UNBLOCK_INPUT;
3927
3928 /* Fselect_window will have made f the selected frame, so we
3929 reselect the proper frame here. Fhandle_switch_frame will change the
3930 selected window too, but that doesn't make the call to
3931 Fselect_window above totally superfluous; it still sets f's
3932 selected window. */
3933 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
3934 do_switch_frame (data->selected_frame, Qnil, 0);
3935
3936 if (! NILP (Vwindow_configuration_change_hook)
3937 && ! NILP (Vrun_hooks))
3938 call1 (Vrun_hooks, Qwindow_configuration_change_hook);
3939 }
3940
3941 if (!NILP (new_current_buffer))
3942 {
3943 Fset_buffer (new_current_buffer);
3944
3945 /* If the buffer that is current now is the same
3946 that was current before setting the window configuration,
3947 don't alter its PT. */
3948 if (old_point >= 0)
3949 SET_PT (old_point);
3950 }
3951
3952 /* Restore the minimum heights recorded in the configuration. */
3953 window_min_height = XINT (data->min_height);
3954 window_min_width = XINT (data->min_width);
3955
3956 Vminibuf_scroll_window = data->minibuf_scroll_window;
3957
3958 return (FRAME_LIVE_P (f) ? Qt : Qnil);
3959 }
3960
3961 /* Mark all windows now on frame as deleted
3962 by setting their buffers to nil. */
3963
3964 void
3965 delete_all_subwindows (w)
3966 register struct window *w;
3967 {
3968 if (!NILP (w->next))
3969 delete_all_subwindows (XWINDOW (w->next));
3970 if (!NILP (w->vchild))
3971 delete_all_subwindows (XWINDOW (w->vchild));
3972 if (!NILP (w->hchild))
3973 delete_all_subwindows (XWINDOW (w->hchild));
3974
3975 w->height = w->buffer; /* See Fset_window_configuration for excuse. */
3976
3977 if (!NILP (w->buffer))
3978 unshow_buffer (w);
3979
3980 /* We set all three of these fields to nil, to make sure that we can
3981 distinguish this dead window from any live window. Live leaf
3982 windows will have buffer set, and combination windows will have
3983 vchild or hchild set. */
3984 w->buffer = Qnil;
3985 w->vchild = Qnil;
3986 w->hchild = Qnil;
3987 }
3988 \f
3989 static int
3990 count_windows (window)
3991 register struct window *window;
3992 {
3993 register int count = 1;
3994 if (!NILP (window->next))
3995 count += count_windows (XWINDOW (window->next));
3996 if (!NILP (window->vchild))
3997 count += count_windows (XWINDOW (window->vchild));
3998 if (!NILP (window->hchild))
3999 count += count_windows (XWINDOW (window->hchild));
4000 return count;
4001 }
4002
4003
4004 /* Fill vector FLAT with leaf windows under W, starting at index I.
4005 Value is last index + 1. */
4006
4007 static int
4008 get_leaf_windows (w, flat, i)
4009 struct window *w;
4010 struct window **flat;
4011 int i;
4012 {
4013 while (w)
4014 {
4015 if (!NILP (w->hchild))
4016 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
4017 else if (!NILP (w->vchild))
4018 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
4019 else
4020 flat[i++] = w;
4021
4022 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4023 }
4024
4025 return i;
4026 }
4027
4028
4029 /* Return a pointer to the glyph W's physical cursor is on. Value is
4030 null if W's current matrix is invalid, so that no meaningfull glyph
4031 can be returned. */
4032
4033 struct glyph *
4034 get_phys_cursor_glyph (w)
4035 struct window *w;
4036 {
4037 struct glyph_row *row;
4038 struct glyph *glyph;
4039
4040 if (w->phys_cursor.vpos >= 0
4041 && w->phys_cursor.vpos < w->current_matrix->nrows
4042 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
4043 row->enabled_p)
4044 && row->used[TEXT_AREA] > w->phys_cursor.hpos)
4045 glyph = row->glyphs[TEXT_AREA] + w->phys_cursor.hpos;
4046 else
4047 glyph = NULL;
4048
4049 return glyph;
4050 }
4051
4052
4053 static int
4054 save_window_save (window, vector, i)
4055 Lisp_Object window;
4056 struct Lisp_Vector *vector;
4057 int i;
4058 {
4059 register struct saved_window *p;
4060 register struct window *w;
4061 register Lisp_Object tem;
4062
4063 for (;!NILP (window); window = w->next)
4064 {
4065 p = SAVED_WINDOW_N (vector, i);
4066 w = XWINDOW (window);
4067
4068 XSETFASTINT (w->temslot, i++);
4069 p->window = window;
4070 p->buffer = w->buffer;
4071 p->left = w->left;
4072 p->top = w->top;
4073 p->width = w->width;
4074 p->height = w->height;
4075 p->hscroll = w->hscroll;
4076 p->display_table = w->display_table;
4077 if (!NILP (w->buffer))
4078 {
4079 /* Save w's value of point in the window configuration.
4080 If w is the selected window, then get the value of point
4081 from the buffer; pointm is garbage in the selected window. */
4082 if (EQ (window, selected_window))
4083 {
4084 p->pointm = Fmake_marker ();
4085 set_marker_both (p->pointm, w->buffer,
4086 BUF_PT (XBUFFER (w->buffer)),
4087 BUF_PT_BYTE (XBUFFER (w->buffer)));
4088 }
4089 else
4090 p->pointm = Fcopy_marker (w->pointm, Qnil);
4091
4092 p->start = Fcopy_marker (w->start, Qnil);
4093 p->start_at_line_beg = w->start_at_line_beg;
4094
4095 tem = XBUFFER (w->buffer)->mark;
4096 p->mark = Fcopy_marker (tem, Qnil);
4097 }
4098 else
4099 {
4100 p->pointm = Qnil;
4101 p->start = Qnil;
4102 p->mark = Qnil;
4103 p->start_at_line_beg = Qnil;
4104 }
4105
4106 if (NILP (w->parent))
4107 p->parent = Qnil;
4108 else
4109 p->parent = XWINDOW (w->parent)->temslot;
4110
4111 if (NILP (w->prev))
4112 p->prev = Qnil;
4113 else
4114 p->prev = XWINDOW (w->prev)->temslot;
4115
4116 if (!NILP (w->vchild))
4117 i = save_window_save (w->vchild, vector, i);
4118 if (!NILP (w->hchild))
4119 i = save_window_save (w->hchild, vector, i);
4120 }
4121
4122 return i;
4123 }
4124
4125 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
4126 Scurrent_window_configuration, 0, 1, 0,
4127 "Return an object representing the current window configuration of FRAME.\n\
4128 If FRAME is nil or omitted, use the selected frame.\n\
4129 This describes the number of windows, their sizes and current buffers,\n\
4130 and for each displayed buffer, where display starts, and the positions of\n\
4131 point and mark. An exception is made for point in the current buffer:\n\
4132 its value is -not- saved.\n\
4133 This also records the currently selected frame, and FRAME's focus\n\
4134 redirection (see `redirect-frame-focus').")
4135 (frame)
4136 Lisp_Object frame;
4137 {
4138 register Lisp_Object tem;
4139 register int n_windows;
4140 register struct save_window_data *data;
4141 register struct Lisp_Vector *vec;
4142 register int i;
4143 FRAME_PTR f;
4144
4145 if (NILP (frame))
4146 f = selected_frame;
4147 else
4148 {
4149 CHECK_LIVE_FRAME (frame, 0);
4150 f = XFRAME (frame);
4151 }
4152
4153 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
4154 vec = allocate_vectorlike (VECSIZE (struct save_window_data));
4155 for (i = 0; i < VECSIZE (struct save_window_data); i++)
4156 vec->contents[i] = Qnil;
4157 vec->size = VECSIZE (struct save_window_data);
4158 data = (struct save_window_data *)vec;
4159
4160 XSETFASTINT (data->frame_width, FRAME_WIDTH (f));
4161 XSETFASTINT (data->frame_height, FRAME_HEIGHT (f));
4162 XSETFASTINT (data->frame_menu_bar_lines, FRAME_MENU_BAR_LINES (f));
4163 XSETFASTINT (data->frame_toolbar_lines, FRAME_TOOLBAR_LINES (f));
4164 XSETFRAME (data->selected_frame, selected_frame);
4165 data->current_window = FRAME_SELECTED_WINDOW (f);
4166 XSETBUFFER (data->current_buffer, current_buffer);
4167 data->minibuf_scroll_window = Vminibuf_scroll_window;
4168 data->root_window = FRAME_ROOT_WINDOW (f);
4169 data->focus_frame = FRAME_FOCUS_FRAME (f);
4170 XSETINT (data->min_height, window_min_height);
4171 XSETINT (data->min_width, window_min_width);
4172 tem = Fmake_vector (make_number (n_windows), Qnil);
4173 data->saved_windows = tem;
4174 for (i = 0; i < n_windows; i++)
4175 XVECTOR (tem)->contents[i]
4176 = Fmake_vector (make_number (SAVED_WINDOW_VECTOR_SIZE), Qnil);
4177 save_window_save (FRAME_ROOT_WINDOW (f),
4178 XVECTOR (tem), 0);
4179 XSETWINDOW_CONFIGURATION (tem, data);
4180 return (tem);
4181 }
4182
4183 DEFUN ("save-window-excursion", Fsave_window_excursion, Ssave_window_excursion,
4184 0, UNEVALLED, 0,
4185 "Execute body, preserving window sizes and contents.\n\
4186 Restore which buffer appears in which window, where display starts,\n\
4187 and the value of point and mark for each window.\n\
4188 Also restore which buffer is current.\n\
4189 But do not preserve point in the current buffer.\n\
4190 Does not restore the value of point in current buffer.")
4191 (args)
4192 Lisp_Object args;
4193 {
4194 register Lisp_Object val;
4195 register int count = specpdl_ptr - specpdl;
4196
4197 record_unwind_protect (Fset_window_configuration,
4198 Fcurrent_window_configuration (Qnil));
4199 val = Fprogn (args);
4200 return unbind_to (count, val);
4201 }
4202
4203 \f
4204 /***********************************************************************
4205 Marginal Areas
4206 ***********************************************************************/
4207
4208 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
4209 2, 3, "",
4210 "Set width of marginal areas of window WINDOW.\n\
4211 If window is nil or omitted, set margins of the currently selected window.\n\
4212 First parameter LEFT-WIDTH specifies the number of character\n\
4213 cells to reserve for the left marginal area. Second parameter\n\
4214 RIGHT-WIDTH does the same for the right marginal area.\n\
4215 A nil width parameter means no margin.")
4216 (left, right, window)
4217 Lisp_Object window, left, right;
4218 {
4219 struct window *w = decode_window (window);
4220 struct frame *f = XFRAME (w->frame);
4221
4222 if (!NILP (left))
4223 CHECK_NUMBER_OR_FLOAT (left, 0);
4224 if (!NILP (right))
4225 CHECK_NUMBER_OR_FLOAT (right, 0);
4226
4227 /* Check widths < 0 and translate a zero width to nil.
4228 Margins that are too wide have to be checked elsewhere. */
4229 if ((INTEGERP (left) && XINT (left) < 0)
4230 || (FLOATP (left) && XFLOAT (left)->data <= 0))
4231 XSETFASTINT (left, 0);
4232 if (INTEGERP (left) && XFASTINT (left) == 0)
4233 left = Qnil;
4234
4235 if ((INTEGERP (right) && XINT (right) < 0)
4236 || (FLOATP (right) && XFLOAT (right)->data <= 0))
4237 XSETFASTINT (right, 0);
4238 if (INTEGERP (right) && XFASTINT (right) == 0)
4239 right = Qnil;
4240
4241 w->left_margin_width = left;
4242 w->right_margin_width = right;
4243
4244 ++windows_or_buffers_changed;
4245 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
4246 return Qnil;
4247 }
4248
4249
4250 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
4251 0, 1, 0,
4252 "Get width of marginal areas of window WINDOW.\n\
4253 If WINDOW is omitted or nil, use the currently selected window.\n\
4254 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).\n\
4255 If a marginal area does not exist, its width will be returned\n\
4256 as nil.")
4257 (window)
4258 Lisp_Object window;
4259 {
4260 struct window *w = decode_window (window);
4261 return Fcons (w->left_margin_width, w->right_margin_width);
4262 }
4263
4264
4265 \f
4266 /***********************************************************************
4267 Smooth scrolling
4268 ***********************************************************************/
4269
4270 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 1, 0,
4271 "Return the amount by which WINDOW is scrolled vertically.\n\
4272 Use the selected window if WINDOW is nil or omitted.\n\
4273 Value is a multiple of the canonical character height of WINDOW.")
4274 (window)
4275 Lisp_Object window;
4276 {
4277 struct frame *f;
4278 struct window *w;
4279
4280 if (NILP (window))
4281 window = selected_window;
4282 w = XWINDOW (window);
4283 f = XFRAME (w->frame);
4284
4285 if (FRAME_WINDOW_P (f))
4286 return CANON_Y_FROM_PIXEL_Y (f, w->vscroll);
4287 else
4288 return make_number (0);
4289 }
4290
4291
4292 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
4293 1, 2, 0,
4294 "Set amount by WINDOW should be scrolled vertically to VSCROLL.\n\
4295 WINDOW nil or omitted means use the selected window. VSCROLL is a\n\
4296 multiple of the canonical character height of WINDOW.")
4297 (vscroll, window)
4298 Lisp_Object vscroll, window;
4299 {
4300 struct window *w;
4301 struct frame *f;
4302
4303 CHECK_NUMBER_OR_FLOAT (vscroll, 0);
4304
4305 if (NILP (window))
4306 window = selected_window;
4307 w = XWINDOW (window);
4308 f = XFRAME (w->frame);
4309
4310 if (FRAME_WINDOW_P (f))
4311 {
4312 int old_dy = w->vscroll;
4313 w->vscroll = min (0, CANON_Y_UNIT (f) * XFLOATINT (vscroll));
4314
4315 /* Adjust glyph matrix of the frame if the virtual display
4316 area becomes larger than before. */
4317 if (w->vscroll < 0 && w->vscroll < old_dy)
4318 adjust_glyphs (f);
4319
4320 /* Prevent redisplay shortcuts. */
4321 XBUFFER (w->buffer)->clip_changed = 1;
4322 }
4323
4324 return Qnil;
4325 }
4326
4327
4328 \f
4329 /***********************************************************************
4330 Initialization
4331 ***********************************************************************/
4332
4333 /* Return 1 if window configurations C1 and C2
4334 describe the same state of affairs. This is used by Fequal. */
4335
4336 int
4337 compare_window_configurations (c1, c2, ignore_positions)
4338 Lisp_Object c1, c2;
4339 int ignore_positions;
4340 {
4341 register struct save_window_data *d1, *d2;
4342 struct Lisp_Vector *sw1, *sw2;
4343 int i;
4344
4345 d1 = (struct save_window_data *) XVECTOR (c1);
4346 d2 = (struct save_window_data *) XVECTOR (c2);
4347 sw1 = XVECTOR (d1->saved_windows);
4348 sw2 = XVECTOR (d2->saved_windows);
4349
4350 if (! EQ (d1->frame_width, d2->frame_width))
4351 return 0;
4352 if (! EQ (d1->frame_height, d2->frame_height))
4353 return 0;
4354 if (! EQ (d1->frame_menu_bar_lines, d2->frame_menu_bar_lines))
4355 return 0;
4356 if (! EQ (d1->selected_frame, d2->selected_frame))
4357 return 0;
4358 /* Don't compare the current_window field directly.
4359 Instead see w1_is_current and w2_is_current, below. */
4360 if (! EQ (d1->current_buffer, d2->current_buffer))
4361 return 0;
4362 if (! ignore_positions)
4363 if (! EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window))
4364 return 0;
4365 /* Don't compare the root_window field.
4366 We don't require the two configurations
4367 to use the same window object,
4368 and the two root windows must be equivalent
4369 if everything else compares equal. */
4370 if (! EQ (d1->focus_frame, d2->focus_frame))
4371 return 0;
4372 if (! EQ (d1->min_width, d2->min_width))
4373 return 0;
4374 if (! EQ (d1->min_height, d2->min_height))
4375 return 0;
4376
4377 /* Verify that the two confis have the same number of windows. */
4378 if (sw1->size != sw2->size)
4379 return 0;
4380
4381 for (i = 0; i < sw1->size; i++)
4382 {
4383 struct saved_window *p1, *p2;
4384 int w1_is_current, w2_is_current;
4385
4386 p1 = SAVED_WINDOW_N (sw1, i);
4387 p2 = SAVED_WINDOW_N (sw2, i);
4388
4389 /* Verify that the current windows in the two
4390 configurations correspond to each other. */
4391 w1_is_current = EQ (d1->current_window, p1->window);
4392 w2_is_current = EQ (d2->current_window, p2->window);
4393
4394 if (w1_is_current != w2_is_current)
4395 return 0;
4396
4397 /* Verify that the corresponding windows do match. */
4398 if (! EQ (p1->buffer, p2->buffer))
4399 return 0;
4400 if (! EQ (p1->left, p2->left))
4401 return 0;
4402 if (! EQ (p1->top, p2->top))
4403 return 0;
4404 if (! EQ (p1->width, p2->width))
4405 return 0;
4406 if (! EQ (p1->height, p2->height))
4407 return 0;
4408 if (! EQ (p1->display_table, p2->display_table))
4409 return 0;
4410 if (! EQ (p1->parent, p2->parent))
4411 return 0;
4412 if (! EQ (p1->prev, p2->prev))
4413 return 0;
4414 if (! ignore_positions)
4415 {
4416 if (! EQ (p1->hscroll, p2->hscroll))
4417 return 0;
4418 if (! EQ (p1->start_at_line_beg, p2->start_at_line_beg))
4419 return 0;
4420 if (NILP (Fequal (p1->start, p2->start)))
4421 return 0;
4422 if (NILP (Fequal (p1->pointm, p2->pointm)))
4423 return 0;
4424 if (NILP (Fequal (p1->mark, p2->mark)))
4425 return 0;
4426 }
4427 }
4428
4429 return 1;
4430 }
4431
4432 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
4433 Scompare_window_configurations, 2, 2, 0,
4434 "Compare two window configurations as regards the structure of windows.\n\
4435 This function ignores details such as the values of point and mark\n\
4436 and scrolling positions.")
4437 (x, y)
4438 Lisp_Object x, y;
4439 {
4440 if (compare_window_configurations (x, y, 1))
4441 return Qt;
4442 return Qnil;
4443 }
4444 \f
4445 void
4446 init_window_once ()
4447 {
4448 selected_frame = make_terminal_frame ();
4449 XSETFRAME (Vterminal_frame, selected_frame);
4450 minibuf_window = selected_frame->minibuffer_window;
4451 selected_window = selected_frame->selected_window;
4452 last_nonminibuf_frame = selected_frame;
4453
4454 window_initialized = 1;
4455 }
4456
4457 void
4458 syms_of_window ()
4459 {
4460 staticpro (&Qwindow_configuration_change_hook);
4461 Qwindow_configuration_change_hook
4462 = intern ("window-configuration-change-hook");
4463
4464 Qwindowp = intern ("windowp");
4465 staticpro (&Qwindowp);
4466
4467 Qwindow_configuration_p = intern ("window-configuration-p");
4468 staticpro (&Qwindow_configuration_p);
4469
4470 Qwindow_live_p = intern ("window-live-p");
4471 staticpro (&Qwindow_live_p);
4472
4473 Qtemp_buffer_show_hook = intern ("temp-buffer-show-hook");
4474 staticpro (&Qtemp_buffer_show_hook);
4475
4476 DEFVAR_LISP ("temp-buffer-show-function", &Vtemp_buffer_show_function,
4477 "Non-nil means call as function to display a help buffer.\n\
4478 The function is called with one argument, the buffer to be displayed.\n\
4479 Used by `with-output-to-temp-buffer'.\n\
4480 If this function is used, then it must do the entire job of showing\n\
4481 the buffer; `temp-buffer-show-hook' is not run unless this function runs it.");
4482 Vtemp_buffer_show_function = Qnil;
4483
4484 DEFVAR_LISP ("display-buffer-function", &Vdisplay_buffer_function,
4485 "If non-nil, function to call to handle `display-buffer'.\n\
4486 It will receive two args, the buffer and a flag which if non-nil means\n\
4487 that the currently selected window is not acceptable.\n\
4488 Commands such as `switch-to-buffer-other-window' and `find-file-other-window'\n\
4489 work using this function.");
4490 Vdisplay_buffer_function = Qnil;
4491
4492 DEFVAR_LISP ("minibuffer-scroll-window", &Vminibuf_scroll_window,
4493 "Non-nil means it is the window that C-M-v in minibuffer should scroll.");
4494 Vminibuf_scroll_window = Qnil;
4495
4496 DEFVAR_LISP ("other-window-scroll-buffer", &Vother_window_scroll_buffer,
4497 "If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window.");
4498 Vother_window_scroll_buffer = Qnil;
4499
4500 DEFVAR_BOOL ("pop-up-frames", &pop_up_frames,
4501 "*Non-nil means `display-buffer' should make a separate frame.");
4502 pop_up_frames = 0;
4503
4504 DEFVAR_LISP ("pop-up-frame-function", &Vpop_up_frame_function,
4505 "Function to call to handle automatic new frame creation.\n\
4506 It is called with no arguments and should return a newly created frame.\n\
4507 \n\
4508 A typical value might be `(lambda () (new-frame pop-up-frame-alist))'\n\
4509 where `pop-up-frame-alist' would hold the default frame parameters.");
4510 Vpop_up_frame_function = Qnil;
4511
4512 DEFVAR_LISP ("special-display-buffer-names", &Vspecial_display_buffer_names,
4513 "*List of buffer names that should have their own special frames.\n\
4514 Displaying a buffer whose name is in this list makes a special frame for it\n\
4515 using `special-display-function'. See also `special-display-regexps'.\n\
4516 \n\
4517 An element of the list can be a list instead of just a string.\n\
4518 There are two ways to use a list as an element:\n\
4519 (BUFFER FRAME-PARAMETERS...) (BUFFER FUNCTION OTHER-ARGS...)\n\
4520 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
4521 In the latter case, FUNCTION is called with BUFFER as the first argument,\n\
4522 followed by OTHER-ARGS--it can display BUFFER in any way it likes.\n\
4523 All this is done by the function found in `special-display-function'.\n\
4524 \n\
4525 If this variable appears \"not to work\", because you add a name to it\n\
4526 but that buffer still appears in the selected window, look at the\n\
4527 values of `same-window-buffer-names' and `same-window-regexps'.\n\
4528 Those variables take precedence over this one.");
4529 Vspecial_display_buffer_names = Qnil;
4530
4531 DEFVAR_LISP ("special-display-regexps", &Vspecial_display_regexps,
4532 "*List of regexps saying which buffers should have their own special frames.\n\
4533 If a buffer name matches one of these regexps, it gets its own frame.\n\
4534 Displaying a buffer whose name is in this list makes a special frame for it\n\
4535 using `special-display-function'.\n\
4536 \n\
4537 An element of the list can be a list instead of just a string.\n\
4538 There are two ways to use a list as an element:\n\
4539 (REGEXP FRAME-PARAMETERS...) (REGEXP FUNCTION OTHER-ARGS...)\n\
4540 In the first case, FRAME-PARAMETERS are used to create the frame.\n\
4541 In the latter case, FUNCTION is called with the buffer as first argument,\n\
4542 followed by OTHER-ARGS--it can display the buffer in any way it likes.\n\
4543 All this is done by the function found in `special-display-function'.\n\
4544 \n\
4545 If this variable appears \"not to work\", because you add a regexp to it\n\
4546 but the matching buffers still appear in the selected window, look at the\n\
4547 values of `same-window-buffer-names' and `same-window-regexps'.\n\
4548 Those variables take precedence over this one.");
4549 Vspecial_display_regexps = Qnil;
4550
4551 DEFVAR_LISP ("special-display-function", &Vspecial_display_function,
4552 "Function to call to make a new frame for a special buffer.\n\
4553 It is called with two arguments, the buffer and optional buffer specific\n\
4554 data, and should return a window displaying that buffer.\n\
4555 The default value makes a separate frame for the buffer,\n\
4556 using `special-display-frame-alist' to specify the frame parameters.\n\
4557 \n\
4558 A buffer is special if its is listed in `special-display-buffer-names'\n\
4559 or matches a regexp in `special-display-regexps'.");
4560 Vspecial_display_function = Qnil;
4561
4562 DEFVAR_LISP ("same-window-buffer-names", &Vsame_window_buffer_names,
4563 "*List of buffer names that should appear in the selected window.\n\
4564 Displaying one of these buffers using `display-buffer' or `pop-to-buffer'\n\
4565 switches to it in the selected window, rather than making it appear\n\
4566 in some other window.\n\
4567 \n\
4568 An element of the list can be a cons cell instead of just a string.\n\
4569 Then the car must be a string, which specifies the buffer name.\n\
4570 This is for compatibility with `special-display-buffer-names';\n\
4571 the cdr of the cons cell is ignored.\n\
4572 \n\
4573 See also `same-window-regexps'.");
4574 Vsame_window_buffer_names = Qnil;
4575
4576 DEFVAR_LISP ("same-window-regexps", &Vsame_window_regexps,
4577 "*List of regexps saying which buffers should appear in the selected window.\n\
4578 If a buffer name matches one of these regexps, then displaying it\n\
4579 using `display-buffer' or `pop-to-buffer' switches to it\n\
4580 in the selected window, rather than making it appear in some other window.\n\
4581 \n\
4582 An element of the list can be a cons cell instead of just a string.\n\
4583 Then the car must be a string, which specifies the buffer name.\n\
4584 This is for compatibility with `special-display-buffer-names';\n\
4585 the cdr of the cons cell is ignored.\n\
4586 \n\
4587 See also `same-window-buffer-names'.");
4588 Vsame_window_regexps = Qnil;
4589
4590 DEFVAR_BOOL ("pop-up-windows", &pop_up_windows,
4591 "*Non-nil means display-buffer should make new windows.");
4592 pop_up_windows = 1;
4593
4594 DEFVAR_INT ("next-screen-context-lines", &next_screen_context_lines,
4595 "*Number of lines of continuity when scrolling by screenfuls.");
4596 next_screen_context_lines = 2;
4597
4598 DEFVAR_INT ("split-height-threshold", &split_height_threshold,
4599 "*display-buffer would prefer to split the largest window if this large.\n\
4600 If there is only one window, it is split regardless of this value.");
4601 split_height_threshold = 500;
4602
4603 DEFVAR_INT ("window-min-height", &window_min_height,
4604 "*Delete any window less than this tall (including its mode line).");
4605 window_min_height = 4;
4606
4607 DEFVAR_INT ("window-min-width", &window_min_width,
4608 "*Delete any window less than this wide.");
4609 window_min_width = 10;
4610
4611 DEFVAR_LISP ("scroll-preserve-screen-position",
4612 &Vscroll_preserve_screen_position,
4613 "*Nonzero means scroll commands move point to keep its screen line unchanged.");
4614 Vscroll_preserve_screen_position = Qnil;
4615
4616 DEFVAR_LISP ("window-configuration-change-hook",
4617 &Vwindow_configuration_change_hook,
4618 "Functions to call when window configuration changes.\n\
4619 The selected frame is the one whose configuration has changed.");
4620 Vwindow_configuration_change_hook = Qnil;
4621
4622 defsubr (&Sselected_window);
4623 defsubr (&Sminibuffer_window);
4624 defsubr (&Swindow_minibuffer_p);
4625 defsubr (&Swindowp);
4626 defsubr (&Swindow_live_p);
4627 defsubr (&Spos_visible_in_window_p);
4628 defsubr (&Swindow_buffer);
4629 defsubr (&Swindow_height);
4630 defsubr (&Swindow_width);
4631 defsubr (&Swindow_hscroll);
4632 defsubr (&Sset_window_hscroll);
4633 defsubr (&Swindow_redisplay_end_trigger);
4634 defsubr (&Sset_window_redisplay_end_trigger);
4635 defsubr (&Swindow_edges);
4636 defsubr (&Scoordinates_in_window_p);
4637 defsubr (&Swindow_at);
4638 defsubr (&Swindow_point);
4639 defsubr (&Swindow_start);
4640 defsubr (&Swindow_end);
4641 defsubr (&Sset_window_point);
4642 defsubr (&Sset_window_start);
4643 defsubr (&Swindow_dedicated_p);
4644 defsubr (&Sset_window_dedicated_p);
4645 defsubr (&Swindow_display_table);
4646 defsubr (&Sset_window_display_table);
4647 defsubr (&Snext_window);
4648 defsubr (&Sprevious_window);
4649 defsubr (&Sother_window);
4650 defsubr (&Sget_lru_window);
4651 defsubr (&Sget_largest_window);
4652 defsubr (&Sget_buffer_window);
4653 defsubr (&Sdelete_other_windows);
4654 defsubr (&Sdelete_windows_on);
4655 defsubr (&Sreplace_buffer_in_windows);
4656 defsubr (&Sdelete_window);
4657 defsubr (&Sset_window_buffer);
4658 defsubr (&Sselect_window);
4659 defsubr (&Sspecial_display_p);
4660 defsubr (&Ssame_window_p);
4661 defsubr (&Sdisplay_buffer);
4662 defsubr (&Ssplit_window);
4663 defsubr (&Senlarge_window);
4664 defsubr (&Sshrink_window);
4665 defsubr (&Sscroll_up);
4666 defsubr (&Sscroll_down);
4667 defsubr (&Sscroll_left);
4668 defsubr (&Sscroll_right);
4669 defsubr (&Sother_window_for_scrolling);
4670 defsubr (&Sscroll_other_window);
4671 defsubr (&Srecenter);
4672 defsubr (&Smove_to_window_line);
4673 defsubr (&Swindow_configuration_p);
4674 defsubr (&Swindow_configuration_frame);
4675 defsubr (&Sset_window_configuration);
4676 defsubr (&Scurrent_window_configuration);
4677 defsubr (&Ssave_window_excursion);
4678 defsubr (&Sset_window_margins);
4679 defsubr (&Swindow_margins);
4680 defsubr (&Swindow_vscroll);
4681 defsubr (&Sset_window_vscroll);
4682 defsubr (&Scompare_window_configurations);
4683 }
4684
4685 void
4686 keys_of_window ()
4687 {
4688 initial_define_key (control_x_map, '1', "delete-other-windows");
4689 initial_define_key (control_x_map, '2', "split-window");
4690 initial_define_key (control_x_map, '0', "delete-window");
4691 initial_define_key (control_x_map, 'o', "other-window");
4692 initial_define_key (control_x_map, '^', "enlarge-window");
4693 initial_define_key (control_x_map, '<', "scroll-left");
4694 initial_define_key (control_x_map, '>', "scroll-right");
4695
4696 initial_define_key (global_map, Ctl ('V'), "scroll-up");
4697 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
4698 initial_define_key (meta_map, 'v', "scroll-down");
4699
4700 initial_define_key (global_map, Ctl('L'), "recenter");
4701 initial_define_key (meta_map, 'r', "move-to-window-line");
4702 }