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