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