Generalize run-time debugging checks.
[bpt/emacs.git] / src / window.c
1 /* Window creation, deletion and examination for GNU Emacs.
2 Does not include redisplay.
3 Copyright (C) 1985-1987, 1993-1998, 2000-2012
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 3 of the License, or
11 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <setjmp.h>
24
25 #include "lisp.h"
26 #include "character.h"
27 #include "buffer.h"
28 #include "keyboard.h"
29 #include "keymap.h"
30 #include "frame.h"
31 #include "window.h"
32 #include "commands.h"
33 #include "indent.h"
34 #include "termchar.h"
35 #include "disptab.h"
36 #include "dispextern.h"
37 #include "blockinput.h"
38 #include "intervals.h"
39 #include "termhooks.h" /* For FRAME_TERMINAL. */
40
41 #ifdef HAVE_X_WINDOWS
42 #include "xterm.h"
43 #endif /* HAVE_X_WINDOWS */
44 #ifdef WINDOWSNT
45 #include "w32term.h"
46 #endif
47 #ifdef MSDOS
48 #include "msdos.h"
49 #endif
50 #ifdef HAVE_NS
51 #include "nsterm.h"
52 #endif
53
54 Lisp_Object Qwindowp, Qwindow_live_p;
55 static Lisp_Object Qwindow_configuration_p, Qrecord_window_buffer;
56 static Lisp_Object Qwindow_deletable_p, Qdelete_window, Qdisplay_buffer;
57 static Lisp_Object Qreplace_buffer_in_windows, Qget_mru_window;
58 static Lisp_Object Qwindow_resize_root_window, Qwindow_resize_root_window_vertically;
59 static Lisp_Object Qscroll_up, Qscroll_down, Qscroll_command;
60 static Lisp_Object Qsafe, Qabove, Qbelow;
61 static Lisp_Object Qauto_buffer_name, Qclone_of;
62
63 static int displayed_window_lines (struct window *);
64 static struct window *decode_window (Lisp_Object);
65 static int count_windows (struct window *);
66 static int get_leaf_windows (struct window *, struct window **, int);
67 static void window_scroll (Lisp_Object, EMACS_INT, int, int);
68 static void window_scroll_pixel_based (Lisp_Object, int, int, int);
69 static void window_scroll_line_based (Lisp_Object, int, int, int);
70 static int freeze_window_start (struct window *, void *);
71 static Lisp_Object window_list (void);
72 static int add_window_to_list (struct window *, void *);
73 static int candidate_window_p (Lisp_Object, Lisp_Object, Lisp_Object,
74 Lisp_Object);
75 static Lisp_Object next_window (Lisp_Object, Lisp_Object,
76 Lisp_Object, int);
77 static void decode_next_window_args (Lisp_Object *, Lisp_Object *,
78 Lisp_Object *);
79 static void foreach_window (struct frame *,
80 int (* fn) (struct window *, void *),
81 void *);
82 static int foreach_window_1 (struct window *,
83 int (* fn) (struct window *, void *),
84 void *);
85 static Lisp_Object window_list_1 (Lisp_Object, Lisp_Object, Lisp_Object);
86 static int window_resize_check (struct window *, int);
87 static void window_resize_apply (struct window *, int);
88 static Lisp_Object select_window (Lisp_Object, Lisp_Object, int);
89
90 /* This is the window in which the terminal's cursor should
91 be left when nothing is being done with it. This must
92 always be a leaf window, and its buffer is selected by
93 the top level editing loop at the end of each command.
94
95 This value is always the same as
96 FRAME_SELECTED_WINDOW (selected_frame). */
97 Lisp_Object selected_window;
98
99 /* A list of all windows for use by next_window and Fwindow_list.
100 Functions creating or deleting windows should invalidate this cache
101 by setting it to nil. */
102 Lisp_Object Vwindow_list;
103
104 /* The mini-buffer window of the selected frame.
105 Note that you cannot test for mini-bufferness of an arbitrary window
106 by comparing against this; but you can test for mini-bufferness of
107 the selected window. */
108 Lisp_Object minibuf_window;
109
110 /* Non-nil means it is the window whose mode line should be
111 shown as the selected window when the minibuffer is selected. */
112 Lisp_Object minibuf_selected_window;
113
114 /* Hook run at end of temp_output_buffer_show. */
115 static Lisp_Object Qtemp_buffer_show_hook;
116
117 /* Incremented for each window created. */
118 static int sequence_number;
119
120 /* Nonzero after init_window_once has finished. */
121 static int window_initialized;
122
123 /* Hook to run when window config changes. */
124 static Lisp_Object Qwindow_configuration_change_hook;
125
126 /* Used by the function window_scroll_pixel_based */
127 static int window_scroll_pixel_based_preserve_x;
128 static int window_scroll_pixel_based_preserve_y;
129
130 /* Same for window_scroll_line_based. */
131 static EMACS_INT window_scroll_preserve_hpos;
132 static EMACS_INT window_scroll_preserve_vpos;
133 \f
134 static struct window *
135 decode_window (register Lisp_Object window)
136 {
137 if (NILP (window))
138 return XWINDOW (selected_window);
139
140 CHECK_LIVE_WINDOW (window);
141 return XWINDOW (window);
142 }
143
144 static struct window *
145 decode_any_window (register Lisp_Object window)
146 {
147 if (NILP (window))
148 return XWINDOW (selected_window);
149
150 CHECK_WINDOW (window);
151 return XWINDOW (window);
152 }
153
154 DEFUN ("windowp", Fwindowp, Swindowp, 1, 1, 0,
155 doc: /* Return t if OBJECT is a window and nil otherwise. */)
156 (Lisp_Object object)
157 {
158 return WINDOWP (object) ? Qt : Qnil;
159 }
160
161 DEFUN ("window-live-p", Fwindow_live_p, Swindow_live_p, 1, 1, 0,
162 doc: /* Return t if OBJECT is a live window and nil otherwise.
163 A live window is a window that displays a buffer.
164 Internal windows and deleted windows are not live. */)
165 (Lisp_Object object)
166 {
167 return WINDOW_LIVE_P (object) ? Qt : Qnil;
168 }
169 \f
170 /* Frames and windows. */
171 DEFUN ("window-frame", Fwindow_frame, Swindow_frame, 1, 1, 0,
172 doc: /* Return the frame that window WINDOW is on.
173 If WINDOW is omitted or nil, it defaults to the selected window. */)
174 (Lisp_Object window)
175 {
176 return decode_any_window (window)->frame;
177 }
178
179 DEFUN ("frame-root-window", Fframe_root_window, Sframe_root_window, 0, 1, 0,
180 doc: /* Return the root window of FRAME-OR-WINDOW.
181 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
182 With a frame argument, return that frame's root window.
183 With a window argument, return the root window of that window's frame. */)
184 (Lisp_Object frame_or_window)
185 {
186 Lisp_Object window;
187
188 if (NILP (frame_or_window))
189 window = SELECTED_FRAME ()->root_window;
190 else if (WINDOWP (frame_or_window))
191 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
192 else
193 {
194 CHECK_LIVE_FRAME (frame_or_window);
195 window = XFRAME (frame_or_window)->root_window;
196 }
197
198 return window;
199 }
200
201 DEFUN ("minibuffer-window", Fminibuffer_window, Sminibuffer_window, 0, 1, 0,
202 doc: /* Return the minibuffer window for frame FRAME.
203 If FRAME is omitted or nil, it defaults to the selected frame. */)
204 (Lisp_Object frame)
205 {
206 if (NILP (frame))
207 frame = selected_frame;
208 CHECK_LIVE_FRAME (frame);
209 return FRAME_MINIBUF_WINDOW (XFRAME (frame));
210 }
211
212 DEFUN ("window-minibuffer-p", Fwindow_minibuffer_p,
213 Swindow_minibuffer_p, 0, 1, 0,
214 doc: /* Return non-nil if WINDOW is a minibuffer window.
215 If WINDOW is omitted or nil, it defaults to the selected window. */)
216 (Lisp_Object window)
217 {
218 return MINI_WINDOW_P (decode_any_window (window)) ? Qt : Qnil;
219 }
220
221 /* Don't move this to window.el - this must be a safe routine. */
222 DEFUN ("frame-first-window", Fframe_first_window, Sframe_first_window, 0, 1, 0,
223 doc: /* Return the topmost, leftmost live window on FRAME-OR-WINDOW.
224 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
225 Else if FRAME-OR-WINDOW denotes any window, return the first window of
226 that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
227 the first window of that frame. */)
228 (Lisp_Object frame_or_window)
229 {
230 Lisp_Object window;
231
232 if (NILP (frame_or_window))
233 window = SELECTED_FRAME ()->root_window;
234 else if (WINDOWP (frame_or_window))
235 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->root_window;
236 else
237 {
238 CHECK_LIVE_FRAME (frame_or_window);
239 window = XFRAME (frame_or_window)->root_window;
240 }
241
242 while (NILP (XWINDOW (window)->buffer))
243 {
244 if (! NILP (XWINDOW (window)->hchild))
245 window = XWINDOW (window)->hchild;
246 else if (! NILP (XWINDOW (window)->vchild))
247 window = XWINDOW (window)->vchild;
248 else
249 abort ();
250 }
251
252 return window;
253 }
254
255 DEFUN ("frame-selected-window", Fframe_selected_window,
256 Sframe_selected_window, 0, 1, 0,
257 doc: /* Return the selected window of FRAME-OR-WINDOW.
258 If omitted, FRAME-OR-WINDOW defaults to the currently selected frame.
259 Else if FRAME-OR-WINDOW denotes any window, return the selected window
260 of that window's frame. If FRAME-OR-WINDOW denotes a live frame, return
261 the selected window of that frame. */)
262 (Lisp_Object frame_or_window)
263 {
264 Lisp_Object window;
265
266 if (NILP (frame_or_window))
267 window = SELECTED_FRAME ()->selected_window;
268 else if (WINDOWP (frame_or_window))
269 window = XFRAME (WINDOW_FRAME (XWINDOW (frame_or_window)))->selected_window;
270 else
271 {
272 CHECK_LIVE_FRAME (frame_or_window);
273 window = XFRAME (frame_or_window)->selected_window;
274 }
275
276 return window;
277 }
278
279 DEFUN ("set-frame-selected-window", Fset_frame_selected_window,
280 Sset_frame_selected_window, 2, 3, 0,
281 doc: /* Set selected window of FRAME to WINDOW.
282 FRAME must be a live frame and defaults to the selected one. If FRAME
283 is the selected frame, this makes WINDOW the selected window. Optional
284 argument NORECORD non-nil means to neither change the order of recently
285 selected windows nor the buffer list. WINDOW must denote a live window.
286 Return WINDOW. */)
287 (Lisp_Object frame, Lisp_Object window, Lisp_Object norecord)
288 {
289 if (NILP (frame))
290 frame = selected_frame;
291
292 CHECK_LIVE_FRAME (frame);
293 CHECK_LIVE_WINDOW (window);
294
295 if (! EQ (frame, WINDOW_FRAME (XWINDOW (window))))
296 error ("In `set-frame-selected-window', WINDOW is not on FRAME");
297
298 if (EQ (frame, selected_frame))
299 return Fselect_window (window, norecord);
300 else
301 return XFRAME (frame)->selected_window = window;
302 }
303
304 DEFUN ("selected-window", Fselected_window, Sselected_window, 0, 0, 0,
305 doc: /* Return the selected window.
306 The selected window is the window in which the standard cursor for
307 selected windows appears and to which many commands apply. */)
308 (void)
309 {
310 return selected_window;
311 }
312
313 int window_select_count;
314
315 /* If select_window is called with inhibit_point_swap non-zero it will
316 not store point of the old selected window's buffer back into that
317 window's pointm slot. This is needed by Fset_window_configuration to
318 avoid that the display routine is called with selected_window set to
319 Qnil causing a subsequent crash. */
320 static Lisp_Object
321 select_window (Lisp_Object window, Lisp_Object norecord, int inhibit_point_swap)
322 {
323 register struct window *w;
324 register struct window *ow;
325 struct frame *sf;
326
327 CHECK_LIVE_WINDOW (window);
328
329 w = XWINDOW (window);
330 w->frozen_window_start_p = 0;
331
332 if (NILP (norecord))
333 {
334 ++window_select_count;
335 XSETFASTINT (w->use_time, window_select_count);
336 record_buffer (w->buffer);
337 }
338
339 if (EQ (window, selected_window) && !inhibit_point_swap)
340 return window;
341
342 sf = SELECTED_FRAME ();
343 if (XFRAME (WINDOW_FRAME (w)) != sf)
344 {
345 XFRAME (WINDOW_FRAME (w))->selected_window = window;
346 /* Use this rather than Fhandle_switch_frame
347 so that FRAME_FOCUS_FRAME is moved appropriately as we
348 move around in the state where a minibuffer in a separate
349 frame is active. */
350 Fselect_frame (WINDOW_FRAME (w), norecord);
351 /* Fselect_frame called us back so we've done all the work already. */
352 eassert (EQ (window, selected_window));
353 return window;
354 }
355 else
356 sf->selected_window = window;
357
358 /* Store the current buffer's actual point into the
359 old selected window. It belongs to that window,
360 and when the window is not selected, must be in the window. */
361 if (!inhibit_point_swap)
362 {
363 ow = XWINDOW (selected_window);
364 if (! NILP (ow->buffer))
365 set_marker_both (ow->pointm, ow->buffer,
366 BUF_PT (XBUFFER (ow->buffer)),
367 BUF_PT_BYTE (XBUFFER (ow->buffer)));
368 }
369
370 selected_window = window;
371
372 Fset_buffer (w->buffer);
373
374 BVAR (XBUFFER (w->buffer), last_selected_window) = window;
375
376 /* Go to the point recorded in the window.
377 This is important when the buffer is in more
378 than one window. It also matters when
379 redisplay_window has altered point after scrolling,
380 because it makes the change only in the window. */
381 {
382 register ptrdiff_t new_point = marker_position (w->pointm);
383 if (new_point < BEGV)
384 SET_PT (BEGV);
385 else if (new_point > ZV)
386 SET_PT (ZV);
387 else
388 SET_PT (new_point);
389 }
390
391 windows_or_buffers_changed++;
392 return window;
393 }
394
395 DEFUN ("select-window", Fselect_window, Sselect_window, 1, 2, 0,
396 doc: /* Select WINDOW. Most editing will apply to WINDOW's buffer.
397 Also make WINDOW's buffer current and make WINDOW the frame's selected
398 window. Return WINDOW.
399
400 Optional second arg NORECORD non-nil means do not put this buffer at the
401 front of the buffer list and do not make this window the most recently
402 selected one.
403
404 Note that the main editor command loop sets the current buffer to the
405 buffer of the selected window before each command. */)
406 (register Lisp_Object window, Lisp_Object norecord)
407 {
408 return select_window (window, norecord, 0);
409 }
410 \f
411 DEFUN ("window-buffer", Fwindow_buffer, Swindow_buffer, 0, 1, 0,
412 doc: /* Return the buffer displayed in window WINDOW.
413 If WINDOW is omitted or nil, it defaults to the selected window.
414 Return nil for an internal window or a deleted window. */)
415 (Lisp_Object window)
416 {
417 return decode_any_window (window)->buffer;
418 }
419
420 DEFUN ("window-parent", Fwindow_parent, Swindow_parent, 0, 1, 0,
421 doc: /* Return the parent window of window WINDOW.
422 If WINDOW is omitted or nil, it defaults to the selected window.
423 Return nil for a window with no parent (e.g. a root window). */)
424 (Lisp_Object window)
425 {
426 return decode_any_window (window)->parent;
427 }
428
429 DEFUN ("window-top-child", Fwindow_top_child, Swindow_top_child, 1, 1, 0,
430 doc: /* Return the topmost child window of window WINDOW.
431 Return nil if WINDOW is a live window (live windows have no children).
432 Return nil if WINDOW is an internal window whose children form a
433 horizontal combination. */)
434 (Lisp_Object window)
435 {
436 CHECK_WINDOW (window);
437 return decode_any_window (window)->vchild;
438 }
439
440 DEFUN ("window-left-child", Fwindow_left_child, Swindow_left_child, 1, 1, 0,
441 doc: /* Return the leftmost child window of window WINDOW.
442 Return nil if WINDOW is a live window (live windows have no children).
443 Return nil if WINDOW is an internal window whose children form a
444 vertical combination. */)
445 (Lisp_Object window)
446 {
447 CHECK_WINDOW (window);
448 return decode_any_window (window)->hchild;
449 }
450
451 DEFUN ("window-next-sibling", Fwindow_next_sibling, Swindow_next_sibling, 0, 1, 0,
452 doc: /* Return the next sibling window of window WINDOW.
453 If WINDOW is omitted or nil, it defaults to the selected window.
454 Return nil if WINDOW has no next sibling. */)
455 (Lisp_Object window)
456 {
457 return decode_any_window (window)->next;
458 }
459
460 DEFUN ("window-prev-sibling", Fwindow_prev_sibling, Swindow_prev_sibling, 0, 1, 0,
461 doc: /* Return the previous sibling window of window WINDOW.
462 If WINDOW is omitted or nil, it defaults to the selected window.
463 Return nil if WINDOW has no previous sibling. */)
464 (Lisp_Object window)
465 {
466 return decode_any_window (window)->prev;
467 }
468
469 DEFUN ("window-combination-limit", Fwindow_combination_limit, Swindow_combination_limit, 1, 1, 0,
470 doc: /* Return combination limit of window WINDOW.
471 If the return value is nil, child windows of WINDOW can be recombined with
472 WINDOW's siblings. A return value of t means that child windows of
473 WINDOW are never \(re-)combined with WINDOW's siblings. */)
474 (Lisp_Object window)
475 {
476 return decode_any_window (window)->combination_limit;
477 }
478
479 DEFUN ("set-window-combination-limit", Fset_window_combination_limit, Sset_window_combination_limit, 2, 2, 0,
480 doc: /* Set combination limit of window WINDOW to LIMIT; return LIMIT.
481 If LIMIT is nil, child windows of WINDOW can be recombined with
482 WINDOW's siblings. LIMIT t means that child windows of WINDOW are
483 never \(re-)combined with WINDOW's siblings. Other values are reserved
484 for future use. */)
485 (Lisp_Object window, Lisp_Object limit)
486 {
487 register struct window *w = decode_any_window (window);
488
489 w->combination_limit = limit;
490
491 return w->combination_limit;
492 }
493
494 DEFUN ("window-use-time", Fwindow_use_time, Swindow_use_time, 0, 1, 0,
495 doc: /* Return the use time of window WINDOW.
496 If WINDOW is omitted or nil, it defaults to the selected window.
497 The window with the highest use time is the most recently selected
498 one. The window with the lowest use time is the least recently
499 selected one. */)
500 (Lisp_Object window)
501 {
502 return decode_window (window)->use_time;
503 }
504 \f
505 DEFUN ("window-total-height", Fwindow_total_height, Swindow_total_height, 0, 1, 0,
506 doc: /* Return the total height, in lines, of window WINDOW.
507 If WINDOW is omitted or nil, it defaults to the selected window.
508
509 The return value includes the mode line and header line, if any.
510 If WINDOW is an internal window, the total height is the height
511 of the screen areas spanned by its children.
512
513 On a graphical display, this total height is reported as an
514 integer multiple of the default character height. */)
515 (Lisp_Object window)
516 {
517 return decode_any_window (window)->total_lines;
518 }
519
520 DEFUN ("window-total-width", Fwindow_total_width, Swindow_total_width, 0, 1, 0,
521 doc: /* Return the total width, in columns, of window WINDOW.
522 If WINDOW is omitted or nil, it defaults to the selected window.
523
524 The return value includes any vertical dividers or scroll bars
525 belonging to WINDOW. If WINDOW is an internal window, the total width
526 is the width of the screen areas spanned by its children.
527
528 On a graphical display, this total width is reported as an
529 integer multiple of the default character width. */)
530 (Lisp_Object window)
531 {
532 return decode_any_window (window)->total_cols;
533 }
534
535 DEFUN ("window-new-total", Fwindow_new_total, Swindow_new_total, 0, 1, 0,
536 doc: /* Return the new total size of window WINDOW.
537 If WINDOW is omitted or nil, it defaults to the selected window. */)
538 (Lisp_Object window)
539 {
540 return decode_any_window (window)->new_total;
541 }
542
543 DEFUN ("window-normal-size", Fwindow_normal_size, Swindow_normal_size, 0, 2, 0,
544 doc: /* Return the normal height of window WINDOW.
545 If WINDOW is omitted or nil, it defaults to the selected window.
546 If HORIZONTAL is non-nil, return the normal width of WINDOW. */)
547 (Lisp_Object window, Lisp_Object horizontal)
548 {
549 if (NILP (horizontal))
550 return decode_any_window (window)->normal_lines;
551 else
552 return decode_any_window (window)->normal_cols;
553 }
554
555 DEFUN ("window-new-normal", Fwindow_new_normal, Swindow_new_normal, 0, 1, 0,
556 doc: /* Return new normal size of window WINDOW.
557 If WINDOW is omitted or nil, it defaults to the selected window. */)
558 (Lisp_Object window)
559 {
560 return decode_any_window (window)->new_normal;
561 }
562
563 DEFUN ("window-left-column", Fwindow_left_column, Swindow_left_column, 0, 1, 0,
564 doc: /* Return left column of window WINDOW.
565 This is the distance, in columns, between the left edge of WINDOW and
566 the left edge of the frame's window area. For instance, the return
567 value is 0 if there is no window to the left of WINDOW.
568
569 If WINDOW is omitted or nil, it defaults to the selected window. */)
570 (Lisp_Object window)
571 {
572 return decode_any_window (window)->left_col;
573 }
574
575 DEFUN ("window-top-line", Fwindow_top_line, Swindow_top_line, 0, 1, 0,
576 doc: /* Return top line of window WINDOW.
577 This is the distance, in lines, between the top of WINDOW and the top
578 of the frame's window area. For instance, the return value is 0 if
579 there is no window above WINDOW.
580
581 If WINDOW is omitted or nil, it defaults to the selected window. */)
582 (Lisp_Object window)
583 {
584 return decode_any_window (window)->top_line;
585 }
586
587 /* Return the number of lines of W's body. Don't count any mode or
588 header line of W. */
589
590 static int
591 window_body_lines (struct window *w)
592 {
593 int height = XFASTINT (w->total_lines);
594
595 if (!MINI_WINDOW_P (w))
596 {
597 if (WINDOW_WANTS_MODELINE_P (w))
598 --height;
599 if (WINDOW_WANTS_HEADER_LINE_P (w))
600 --height;
601 }
602
603 return height;
604 }
605
606 /* Return the number of columns of W's body. Don't count columns
607 occupied by the scroll bar or the vertical bar separating W from its
608 right sibling. On window-systems don't count fringes or display
609 margins either. */
610
611 int
612 window_body_cols (struct window *w)
613 {
614 struct frame *f = XFRAME (WINDOW_FRAME (w));
615 int width = XINT (w->total_cols);
616
617 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
618 /* Scroll bars occupy a few columns. */
619 width -= WINDOW_CONFIG_SCROLL_BAR_COLS (w);
620 else if (!FRAME_WINDOW_P (f)
621 && !WINDOW_RIGHTMOST_P (w) && !WINDOW_FULL_WIDTH_P (w))
622 /* The column of `|' characters separating side-by-side windows
623 occupies one column only. */
624 width -= 1;
625
626 if (FRAME_WINDOW_P (f))
627 /* On window-systems, fringes and display margins cannot be
628 used for normal text. */
629 width -= (WINDOW_FRINGE_COLS (w)
630 + WINDOW_LEFT_MARGIN_COLS (w)
631 + WINDOW_RIGHT_MARGIN_COLS (w));
632
633 return width;
634 }
635
636 DEFUN ("window-body-height", Fwindow_body_height, Swindow_body_height, 0, 1, 0,
637 doc: /* Return the height, in lines, of WINDOW's text area.
638 If WINDOW is omitted or nil, it defaults to the selected window.
639 Signal an error if the window is not live.
640
641 The returned height does not include the mode line or header line.
642 On a graphical display, the height is expressed as an integer multiple
643 of the default character height. If a line at the bottom of the text
644 area is only partially visible, that counts as a whole line; to
645 exclude partially-visible lines, use `window-text-height'. */)
646 (Lisp_Object window)
647 {
648 struct window *w = decode_window (window);
649 return make_number (window_body_lines (w));
650 }
651
652 DEFUN ("window-body-width", Fwindow_body_width, Swindow_body_width, 0, 1, 0,
653 doc: /* Return the width, in columns, of WINDOW's text area.
654 If WINDOW is omitted or nil, it defaults to the selected window.
655 Signal an error if the window is not live.
656
657 The return value does not include any vertical dividers, fringe or
658 marginal areas, or scroll bars. On a graphical display, the width is
659 expressed as an integer multiple of the default character width. */)
660 (Lisp_Object window)
661 {
662 struct window *w = decode_window (window);
663 return make_number (window_body_cols (w));
664 }
665
666 DEFUN ("window-hscroll", Fwindow_hscroll, Swindow_hscroll, 0, 1, 0,
667 doc: /* Return the number of columns by which WINDOW is scrolled from left margin.
668 WINDOW must be a live window and defaults to the selected one. */)
669 (Lisp_Object window)
670 {
671 return decode_window (window)->hscroll;
672 }
673
674 DEFUN ("set-window-hscroll", Fset_window_hscroll, Sset_window_hscroll, 2, 2, 0,
675 doc: /* Set number of columns WINDOW is scrolled from left margin to NCOL.
676 If WINDOW is nil, the selected window is used.
677 Return NCOL. NCOL should be zero or positive.
678
679 Note that if `automatic-hscrolling' is non-nil, you cannot scroll the
680 window so that the location of point moves off-window. */)
681 (Lisp_Object window, Lisp_Object ncol)
682 {
683 struct window *w = decode_window (window);
684 ptrdiff_t hscroll;
685
686 CHECK_NUMBER (ncol);
687 hscroll = clip_to_bounds (0, XINT (ncol), PTRDIFF_MAX);
688
689 /* Prevent redisplay shortcuts when changing the hscroll. */
690 if (XINT (w->hscroll) != hscroll)
691 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
692
693 w->hscroll = make_number (hscroll);
694 return ncol;
695 }
696
697 DEFUN ("window-redisplay-end-trigger", Fwindow_redisplay_end_trigger,
698 Swindow_redisplay_end_trigger, 0, 1, 0,
699 doc: /* Return WINDOW's redisplay end trigger value.
700 WINDOW defaults to the selected window.
701 See `set-window-redisplay-end-trigger' for more information. */)
702 (Lisp_Object window)
703 {
704 return decode_window (window)->redisplay_end_trigger;
705 }
706
707 DEFUN ("set-window-redisplay-end-trigger", Fset_window_redisplay_end_trigger,
708 Sset_window_redisplay_end_trigger, 2, 2, 0,
709 doc: /* Set WINDOW's redisplay end trigger value to VALUE.
710 VALUE should be a buffer position (typically a marker) or nil.
711 If it is a buffer position, then if redisplay in WINDOW reaches a position
712 beyond VALUE, the functions in `redisplay-end-trigger-functions' are called
713 with two arguments: WINDOW, and the end trigger value.
714 Afterwards the end-trigger value is reset to nil. */)
715 (register Lisp_Object window, Lisp_Object value)
716 {
717 register struct window *w;
718
719 w = decode_window (window);
720 w->redisplay_end_trigger = value;
721 return value;
722 }
723
724 DEFUN ("window-edges", Fwindow_edges, Swindow_edges, 0, 1, 0,
725 doc: /* Return a list of the edge coordinates of WINDOW.
726 The list has the form (LEFT TOP RIGHT BOTTOM).
727 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
728 all relative to 0, 0 at top left corner of frame.
729
730 RIGHT is one more than the rightmost column occupied by WINDOW.
731 BOTTOM is one more than the bottommost row occupied by WINDOW.
732 The edges include the space used by WINDOW's scroll bar, display
733 margins, fringes, header line, and/or mode line. For the edges of
734 just the text area, use `window-inside-edges'. */)
735 (Lisp_Object window)
736 {
737 register struct window *w = decode_any_window (window);
738
739 return Fcons (make_number (WINDOW_LEFT_EDGE_COL (w)),
740 Fcons (make_number (WINDOW_TOP_EDGE_LINE (w)),
741 Fcons (make_number (WINDOW_RIGHT_EDGE_COL (w)),
742 Fcons (make_number (WINDOW_BOTTOM_EDGE_LINE (w)),
743 Qnil))));
744 }
745
746 DEFUN ("window-pixel-edges", Fwindow_pixel_edges, Swindow_pixel_edges, 0, 1, 0,
747 doc: /* Return a list of the edge pixel coordinates of WINDOW.
748 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
749 the top left corner of the frame.
750
751 RIGHT is one more than the rightmost x position occupied by WINDOW.
752 BOTTOM is one more than the bottommost y position occupied by WINDOW.
753 The pixel edges include the space used by WINDOW's scroll bar, display
754 margins, fringes, header line, and/or mode line. For the pixel edges
755 of just the text area, use `window-inside-pixel-edges'. */)
756 (Lisp_Object window)
757 {
758 register struct window *w = decode_any_window (window);
759
760 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w)),
761 Fcons (make_number (WINDOW_TOP_EDGE_Y (w)),
762 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w)),
763 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w)),
764 Qnil))));
765 }
766
767 static void
768 calc_absolute_offset (struct window *w, int *add_x, int *add_y)
769 {
770 struct frame *f = XFRAME (w->frame);
771 *add_y = f->top_pos;
772 #ifdef FRAME_MENUBAR_HEIGHT
773 *add_y += FRAME_MENUBAR_HEIGHT (f);
774 #endif
775 #ifdef FRAME_TOOLBAR_TOP_HEIGHT
776 *add_y += FRAME_TOOLBAR_TOP_HEIGHT (f);
777 #elif FRAME_TOOLBAR_HEIGHT
778 *add_y += FRAME_TOOLBAR_HEIGHT (f);
779 #endif
780 #ifdef FRAME_NS_TITLEBAR_HEIGHT
781 *add_y += FRAME_NS_TITLEBAR_HEIGHT (f);
782 #endif
783 *add_x = f->left_pos;
784 #ifdef FRAME_TOOLBAR_LEFT_WIDTH
785 *add_x += FRAME_TOOLBAR_LEFT_WIDTH (f);
786 #endif
787 }
788
789 DEFUN ("window-absolute-pixel-edges", Fwindow_absolute_pixel_edges,
790 Swindow_absolute_pixel_edges, 0, 1, 0,
791 doc: /* Return a list of the edge pixel coordinates of WINDOW.
792 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to 0, 0 at
793 the top left corner of the display.
794
795 RIGHT is one more than the rightmost x position occupied by WINDOW.
796 BOTTOM is one more than the bottommost y position occupied by WINDOW.
797 The pixel edges include the space used by WINDOW's scroll bar, display
798 margins, fringes, header line, and/or mode line. For the pixel edges
799 of just the text area, use `window-inside-absolute-pixel-edges'. */)
800 (Lisp_Object window)
801 {
802 register struct window *w = decode_any_window (window);
803 int add_x, add_y;
804 calc_absolute_offset (w, &add_x, &add_y);
805
806 return Fcons (make_number (WINDOW_LEFT_EDGE_X (w) + add_x),
807 Fcons (make_number (WINDOW_TOP_EDGE_Y (w) + add_y),
808 Fcons (make_number (WINDOW_RIGHT_EDGE_X (w) + add_x),
809 Fcons (make_number (WINDOW_BOTTOM_EDGE_Y (w) + add_y),
810 Qnil))));
811 }
812
813 DEFUN ("window-inside-edges", Fwindow_inside_edges, Swindow_inside_edges, 0, 1, 0,
814 doc: /* Return a list of the edge coordinates of WINDOW.
815 The list has the form (LEFT TOP RIGHT BOTTOM).
816 TOP and BOTTOM count by lines, and LEFT and RIGHT count by columns,
817 all relative to 0, 0 at top left corner of frame.
818
819 RIGHT is one more than the rightmost column of WINDOW's text area.
820 BOTTOM is one more than the bottommost row of WINDOW's text area.
821 The inside edges do not include the space used by the WINDOW's scroll
822 bar, display margins, fringes, header line, and/or mode line. */)
823 (Lisp_Object window)
824 {
825 register struct window *w = decode_window (window);
826
827 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_COL (w)
828 + WINDOW_LEFT_MARGIN_COLS (w)
829 + WINDOW_LEFT_FRINGE_COLS (w)),
830 make_number (WINDOW_TOP_EDGE_LINE (w)
831 + WINDOW_HEADER_LINE_LINES (w)),
832 make_number (WINDOW_BOX_RIGHT_EDGE_COL (w)
833 - WINDOW_RIGHT_MARGIN_COLS (w)
834 - WINDOW_RIGHT_FRINGE_COLS (w)),
835 make_number (WINDOW_BOTTOM_EDGE_LINE (w)
836 - WINDOW_MODE_LINE_LINES (w)));
837 }
838
839 DEFUN ("window-inside-pixel-edges", Fwindow_inside_pixel_edges, Swindow_inside_pixel_edges, 0, 1, 0,
840 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
841 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to (0,0)
842 at the top left corner of the frame's window area.
843
844 RIGHT is one more than the rightmost x position of WINDOW's text area.
845 BOTTOM is one more than the bottommost y position of WINDOW's text area.
846 The inside edges do not include the space used by WINDOW's scroll bar,
847 display margins, fringes, header line, and/or mode line. */)
848 (Lisp_Object window)
849 {
850 register struct window *w = decode_window (window);
851
852 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
853 + WINDOW_LEFT_MARGIN_WIDTH (w)
854 + WINDOW_LEFT_FRINGE_WIDTH (w)),
855 make_number (WINDOW_TOP_EDGE_Y (w)
856 + WINDOW_HEADER_LINE_HEIGHT (w)),
857 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
858 - WINDOW_RIGHT_MARGIN_WIDTH (w)
859 - WINDOW_RIGHT_FRINGE_WIDTH (w)),
860 make_number (WINDOW_BOTTOM_EDGE_Y (w)
861 - WINDOW_MODE_LINE_HEIGHT (w)));
862 }
863
864 DEFUN ("window-inside-absolute-pixel-edges",
865 Fwindow_inside_absolute_pixel_edges,
866 Swindow_inside_absolute_pixel_edges, 0, 1, 0,
867 doc: /* Return a list of the edge pixel coordinates of WINDOW's text area.
868 The list has the form (LEFT TOP RIGHT BOTTOM), all relative to (0,0)
869 at the top left corner of the frame's window area.
870
871 RIGHT is one more than the rightmost x position of WINDOW's text area.
872 BOTTOM is one more than the bottommost y position of WINDOW's text area.
873 The inside edges do not include the space used by WINDOW's scroll bar,
874 display margins, fringes, header line, and/or mode line. */)
875 (Lisp_Object window)
876 {
877 register struct window *w = decode_window (window);
878 int add_x, add_y;
879 calc_absolute_offset (w, &add_x, &add_y);
880
881 return list4 (make_number (WINDOW_BOX_LEFT_EDGE_X (w)
882 + WINDOW_LEFT_MARGIN_WIDTH (w)
883 + WINDOW_LEFT_FRINGE_WIDTH (w) + add_x),
884 make_number (WINDOW_TOP_EDGE_Y (w)
885 + WINDOW_HEADER_LINE_HEIGHT (w) + add_y),
886 make_number (WINDOW_BOX_RIGHT_EDGE_X (w)
887 - WINDOW_RIGHT_MARGIN_WIDTH (w)
888 - WINDOW_RIGHT_FRINGE_WIDTH (w) + add_x),
889 make_number (WINDOW_BOTTOM_EDGE_Y (w)
890 - WINDOW_MODE_LINE_HEIGHT (w) + add_y));
891 }
892
893 /* Test if the character at column X, row Y is within window W.
894 If it is not, return ON_NOTHING;
895 if it is in the window's text area, return ON_TEXT;
896 if it is on the window's modeline, return ON_MODE_LINE;
897 if it is on the border between the window and its right sibling,
898 return ON_VERTICAL_BORDER.
899 if it is on a scroll bar, return ON_SCROLL_BAR.
900 if it is on the window's top line, return ON_HEADER_LINE;
901 if it is in left or right fringe of the window,
902 return ON_LEFT_FRINGE or ON_RIGHT_FRINGE;
903 if it is in the marginal area to the left/right of the window,
904 return ON_LEFT_MARGIN or ON_RIGHT_MARGIN.
905
906 X and Y are frame relative pixel coordinates. */
907
908 static enum window_part
909 coordinates_in_window (register struct window *w, int x, int y)
910 {
911 struct frame *f = XFRAME (WINDOW_FRAME (w));
912 enum window_part part;
913 int ux = FRAME_COLUMN_WIDTH (f);
914 int left_x = WINDOW_LEFT_EDGE_X (w);
915 int right_x = WINDOW_RIGHT_EDGE_X (w);
916 int top_y = WINDOW_TOP_EDGE_Y (w);
917 int bottom_y = WINDOW_BOTTOM_EDGE_Y (w);
918 /* The width of the area where the vertical line can be dragged.
919 (Between mode lines for instance. */
920 int grabbable_width = ux;
921 int lmargin_width, rmargin_width, text_left, text_right;
922
923 /* Outside any interesting row or column? */
924 if (y < top_y || y >= bottom_y || x < left_x || x >= right_x)
925 return ON_NOTHING;
926
927 /* On the mode line or header line? */
928 if ((WINDOW_WANTS_MODELINE_P (w)
929 && y >= bottom_y - CURRENT_MODE_LINE_HEIGHT (w)
930 && (part = ON_MODE_LINE))
931 || (WINDOW_WANTS_HEADER_LINE_P (w)
932 && y < top_y + CURRENT_HEADER_LINE_HEIGHT (w)
933 && (part = ON_HEADER_LINE)))
934 {
935 /* If it's under/over the scroll bar portion of the mode/header
936 line, say it's on the vertical line. That's to be able to
937 resize windows horizontally in case we're using toolkit scroll
938 bars. Note: If scrollbars are on the left, the window that
939 must be eventually resized is that on the left of WINDOW. */
940 if ((WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
941 && !WINDOW_LEFTMOST_P (w)
942 && eabs (x - left_x) < grabbable_width)
943 || (!WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
944 && !WINDOW_RIGHTMOST_P (w)
945 && eabs (x - right_x) < grabbable_width))
946 return ON_VERTICAL_BORDER;
947 else
948 return part;
949 }
950
951 /* In what's below, we subtract 1 when computing right_x because we
952 want the rightmost pixel, which is given by left_pixel+width-1. */
953 if (w->pseudo_window_p)
954 {
955 left_x = 0;
956 right_x = WINDOW_TOTAL_WIDTH (w) - 1;
957 }
958 else
959 {
960 left_x = WINDOW_BOX_LEFT_EDGE_X (w);
961 right_x = WINDOW_BOX_RIGHT_EDGE_X (w) - 1;
962 }
963
964 /* Outside any interesting column? */
965 if (x < left_x || x > right_x)
966 return ON_SCROLL_BAR;
967
968 lmargin_width = window_box_width (w, LEFT_MARGIN_AREA);
969 rmargin_width = window_box_width (w, RIGHT_MARGIN_AREA);
970
971 text_left = window_box_left (w, TEXT_AREA);
972 text_right = text_left + window_box_width (w, TEXT_AREA);
973
974 if (FRAME_WINDOW_P (f))
975 {
976 if (!w->pseudo_window_p
977 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
978 && !WINDOW_RIGHTMOST_P (w)
979 && (eabs (x - right_x) < grabbable_width))
980 return ON_VERTICAL_BORDER;
981 }
982 /* Need to say "x > right_x" rather than >=, since on character
983 terminals, the vertical line's x coordinate is right_x. */
984 else if (!w->pseudo_window_p
985 && !WINDOW_RIGHTMOST_P (w)
986 && x > right_x - ux)
987 return ON_VERTICAL_BORDER;
988
989 if (x < text_left)
990 {
991 if (lmargin_width > 0
992 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
993 ? (x >= left_x + WINDOW_LEFT_FRINGE_WIDTH (w))
994 : (x < left_x + lmargin_width)))
995 return ON_LEFT_MARGIN;
996
997 return ON_LEFT_FRINGE;
998 }
999
1000 if (x >= text_right)
1001 {
1002 if (rmargin_width > 0
1003 && (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1004 ? (x < right_x - WINDOW_RIGHT_FRINGE_WIDTH (w))
1005 : (x >= right_x - rmargin_width)))
1006 return ON_RIGHT_MARGIN;
1007
1008 return ON_RIGHT_FRINGE;
1009 }
1010
1011 /* Everything special ruled out - must be on text area */
1012 return ON_TEXT;
1013 }
1014
1015 /* Take X is the frame-relative pixel x-coordinate, and return the
1016 x-coordinate relative to part PART of window W. */
1017 int
1018 window_relative_x_coord (struct window *w, enum window_part part, int x)
1019 {
1020 int left_x = (w->pseudo_window_p) ? 0 : WINDOW_BOX_LEFT_EDGE_X (w);
1021
1022 switch (part)
1023 {
1024 case ON_TEXT:
1025 return x - window_box_left (w, TEXT_AREA);
1026
1027 case ON_LEFT_FRINGE:
1028 return x - left_x;
1029
1030 case ON_RIGHT_FRINGE:
1031 return x - left_x - WINDOW_LEFT_FRINGE_WIDTH (w);
1032
1033 case ON_LEFT_MARGIN:
1034 return (x - left_x
1035 - ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1036 ? WINDOW_LEFT_FRINGE_WIDTH (w) : 0));
1037
1038 case ON_RIGHT_MARGIN:
1039 return (x + 1
1040 - ((w->pseudo_window_p)
1041 ? WINDOW_TOTAL_WIDTH (w)
1042 : WINDOW_BOX_RIGHT_EDGE_X (w))
1043 + window_box_width (w, RIGHT_MARGIN_AREA)
1044 + ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1045 ? WINDOW_RIGHT_FRINGE_WIDTH (w) : 0));
1046 }
1047
1048 /* ON_SCROLL_BAR, ON_NOTHING, and ON_VERTICAL_BORDER: */
1049 return 0;
1050 }
1051
1052
1053 DEFUN ("coordinates-in-window-p", Fcoordinates_in_window_p,
1054 Scoordinates_in_window_p, 2, 2, 0,
1055 doc: /* Return non-nil if COORDINATES are in WINDOW.
1056 WINDOW must be a live window.
1057 COORDINATES is a cons of the form (X . Y), X and Y being distances
1058 measured in characters from the upper-left corner of the frame.
1059 \(0 . 0) denotes the character in the upper left corner of the
1060 frame.
1061 If COORDINATES are in the text portion of WINDOW,
1062 the coordinates relative to the window are returned.
1063 If they are in the mode line of WINDOW, `mode-line' is returned.
1064 If they are in the top mode line of WINDOW, `header-line' is returned.
1065 If they are in the left fringe of WINDOW, `left-fringe' is returned.
1066 If they are in the right fringe of WINDOW, `right-fringe' is returned.
1067 If they are on the border between WINDOW and its right sibling,
1068 `vertical-line' is returned.
1069 If they are in the windows's left or right marginal areas, `left-margin'\n\
1070 or `right-margin' is returned. */)
1071 (register Lisp_Object coordinates, Lisp_Object window)
1072 {
1073 struct window *w;
1074 struct frame *f;
1075 int x, y;
1076 Lisp_Object lx, ly;
1077
1078 CHECK_LIVE_WINDOW (window);
1079 w = XWINDOW (window);
1080 f = XFRAME (w->frame);
1081 CHECK_CONS (coordinates);
1082 lx = Fcar (coordinates);
1083 ly = Fcdr (coordinates);
1084 CHECK_NUMBER_OR_FLOAT (lx);
1085 CHECK_NUMBER_OR_FLOAT (ly);
1086 x = FRAME_PIXEL_X_FROM_CANON_X (f, lx) + FRAME_INTERNAL_BORDER_WIDTH (f);
1087 y = FRAME_PIXEL_Y_FROM_CANON_Y (f, ly) + FRAME_INTERNAL_BORDER_WIDTH (f);
1088
1089 switch (coordinates_in_window (w, x, y))
1090 {
1091 case ON_NOTHING:
1092 return Qnil;
1093
1094 case ON_TEXT:
1095 /* Convert X and Y to window relative pixel coordinates, and
1096 return the canonical char units. */
1097 x -= window_box_left (w, TEXT_AREA);
1098 y -= WINDOW_TOP_EDGE_Y (w);
1099 return Fcons (FRAME_CANON_X_FROM_PIXEL_X (f, x),
1100 FRAME_CANON_Y_FROM_PIXEL_Y (f, y));
1101
1102 case ON_MODE_LINE:
1103 return Qmode_line;
1104
1105 case ON_VERTICAL_BORDER:
1106 return Qvertical_line;
1107
1108 case ON_HEADER_LINE:
1109 return Qheader_line;
1110
1111 case ON_LEFT_FRINGE:
1112 return Qleft_fringe;
1113
1114 case ON_RIGHT_FRINGE:
1115 return Qright_fringe;
1116
1117 case ON_LEFT_MARGIN:
1118 return Qleft_margin;
1119
1120 case ON_RIGHT_MARGIN:
1121 return Qright_margin;
1122
1123 case ON_SCROLL_BAR:
1124 /* Historically we are supposed to return nil in this case. */
1125 return Qnil;
1126
1127 default:
1128 abort ();
1129 }
1130 }
1131
1132
1133 /* Callback for foreach_window, used in window_from_coordinates.
1134 Check if window W contains coordinates specified by USER_DATA which
1135 is actually a pointer to a struct check_window_data CW.
1136
1137 Check if window W contains coordinates *CW->x and *CW->y. If it
1138 does, return W in *CW->window, as Lisp_Object, and return in
1139 *CW->part the part of the window under coordinates *X,*Y. Return
1140 zero from this function to stop iterating over windows. */
1141
1142 struct check_window_data
1143 {
1144 Lisp_Object *window;
1145 int x, y;
1146 enum window_part *part;
1147 };
1148
1149 static int
1150 check_window_containing (struct window *w, void *user_data)
1151 {
1152 struct check_window_data *cw = (struct check_window_data *) user_data;
1153 enum window_part found;
1154 int continue_p = 1;
1155
1156 found = coordinates_in_window (w, cw->x, cw->y);
1157 if (found != ON_NOTHING)
1158 {
1159 *cw->part = found;
1160 XSETWINDOW (*cw->window, w);
1161 continue_p = 0;
1162 }
1163
1164 return continue_p;
1165 }
1166
1167
1168 /* Find the window containing frame-relative pixel position X/Y and
1169 return it as a Lisp_Object.
1170
1171 If X, Y is on one of the window's special `window_part' elements,
1172 set *PART to the id of that element.
1173
1174 If there is no window under X, Y return nil and leave *PART
1175 unmodified. TOOL_BAR_P non-zero means detect tool-bar windows.
1176
1177 This function was previously implemented with a loop cycling over
1178 windows with Fnext_window, and starting with the frame's selected
1179 window. It turned out that this doesn't work with an
1180 implementation of next_window using Vwindow_list, because
1181 FRAME_SELECTED_WINDOW (F) is not always contained in the window
1182 tree of F when this function is called asynchronously from
1183 note_mouse_highlight. The original loop didn't terminate in this
1184 case. */
1185
1186 Lisp_Object
1187 window_from_coordinates (struct frame *f, int x, int y,
1188 enum window_part *part, int tool_bar_p)
1189 {
1190 Lisp_Object window;
1191 struct check_window_data cw;
1192 enum window_part dummy;
1193
1194 if (part == 0)
1195 part = &dummy;
1196
1197 window = Qnil;
1198 cw.window = &window, cw.x = x, cw.y = y; cw.part = part;
1199 foreach_window (f, check_window_containing, &cw);
1200
1201 /* If not found above, see if it's in the tool bar window, if a tool
1202 bar exists. */
1203 if (NILP (window)
1204 && tool_bar_p
1205 && WINDOWP (f->tool_bar_window)
1206 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0
1207 && (coordinates_in_window (XWINDOW (f->tool_bar_window), x, y)
1208 != ON_NOTHING))
1209 {
1210 *part = ON_TEXT;
1211 window = f->tool_bar_window;
1212 }
1213
1214 return window;
1215 }
1216
1217 DEFUN ("window-at", Fwindow_at, Swindow_at, 2, 3, 0,
1218 doc: /* Return window containing coordinates X and Y on FRAME.
1219 FRAME must be a live frame and defaults to the selected one.
1220 The top left corner of the frame is considered to be row 0,
1221 column 0. */)
1222 (Lisp_Object x, Lisp_Object y, Lisp_Object frame)
1223 {
1224 struct frame *f;
1225
1226 if (NILP (frame))
1227 frame = selected_frame;
1228 CHECK_LIVE_FRAME (frame);
1229 f = XFRAME (frame);
1230
1231 /* Check that arguments are integers or floats. */
1232 CHECK_NUMBER_OR_FLOAT (x);
1233 CHECK_NUMBER_OR_FLOAT (y);
1234
1235 return window_from_coordinates (f,
1236 (FRAME_PIXEL_X_FROM_CANON_X (f, x)
1237 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1238 (FRAME_PIXEL_Y_FROM_CANON_Y (f, y)
1239 + FRAME_INTERNAL_BORDER_WIDTH (f)),
1240 0, 0);
1241 }
1242
1243 DEFUN ("window-point", Fwindow_point, Swindow_point, 0, 1, 0,
1244 doc: /* Return current value of point in WINDOW.
1245 WINDOW must be a live window and defaults to the selected one.
1246
1247 For a nonselected window, this is the value point would have
1248 if that window were selected.
1249
1250 Note that, when WINDOW is the selected window and its buffer
1251 is also currently selected, the value returned is the same as (point).
1252 It would be more strictly correct to return the `top-level' value
1253 of point, outside of any save-excursion forms.
1254 But that is hard to define. */)
1255 (Lisp_Object window)
1256 {
1257 register struct window *w = decode_window (window);
1258
1259 if (w == XWINDOW (selected_window)
1260 && current_buffer == XBUFFER (w->buffer))
1261 return Fpoint ();
1262 return Fmarker_position (w->pointm);
1263 }
1264
1265 DEFUN ("window-start", Fwindow_start, Swindow_start, 0, 1, 0,
1266 doc: /* Return position at which display currently starts in WINDOW.
1267 WINDOW must be a live window and defaults to the selected one.
1268 This is updated by redisplay or by calling `set-window-start'. */)
1269 (Lisp_Object window)
1270 {
1271 return Fmarker_position (decode_window (window)->start);
1272 }
1273
1274 /* This is text temporarily removed from the doc string below.
1275
1276 This function returns nil if the position is not currently known.
1277 That happens when redisplay is preempted and doesn't finish.
1278 If in that case you want to compute where the end of the window would
1279 have been if redisplay had finished, do this:
1280 (save-excursion
1281 (goto-char (window-start window))
1282 (vertical-motion (1- (window-height window)) window)
1283 (point))") */
1284
1285 DEFUN ("window-end", Fwindow_end, Swindow_end, 0, 2, 0,
1286 doc: /* Return position at which display currently ends in WINDOW.
1287 WINDOW must be a live window and defaults to the selected one.
1288 This is updated by redisplay, when it runs to completion.
1289 Simply changing the buffer text or setting `window-start'
1290 does not update this value.
1291 Return nil if there is no recorded value. (This can happen if the
1292 last redisplay of WINDOW was preempted, and did not finish.)
1293 If UPDATE is non-nil, compute the up-to-date position
1294 if it isn't already recorded. */)
1295 (Lisp_Object window, Lisp_Object update)
1296 {
1297 Lisp_Object value;
1298 struct window *w = decode_window (window);
1299 Lisp_Object buf;
1300 struct buffer *b;
1301
1302 buf = w->buffer;
1303 CHECK_BUFFER (buf);
1304 b = XBUFFER (buf);
1305
1306 #if 0 /* This change broke some things. We should make it later. */
1307 /* If we don't know the end position, return nil.
1308 The user can compute it with vertical-motion if he wants to.
1309 It would be nicer to do it automatically,
1310 but that's so slow that it would probably bother people. */
1311 if (NILP (w->window_end_valid))
1312 return Qnil;
1313 #endif
1314
1315 if (! NILP (update)
1316 && ! (! NILP (w->window_end_valid)
1317 && XFASTINT (w->last_modified) >= BUF_MODIFF (b)
1318 && XFASTINT (w->last_overlay_modified) >= BUF_OVERLAY_MODIFF (b))
1319 && !noninteractive)
1320 {
1321 struct text_pos startp;
1322 struct it it;
1323 struct buffer *old_buffer = NULL;
1324 void *itdata = NULL;
1325
1326 /* Cannot use Fvertical_motion because that function doesn't
1327 cope with variable-height lines. */
1328 if (b != current_buffer)
1329 {
1330 old_buffer = current_buffer;
1331 set_buffer_internal (b);
1332 }
1333
1334 /* In case W->start is out of the range, use something
1335 reasonable. This situation occurred when loading a file with
1336 `-l' containing a call to `rmail' with subsequent other
1337 commands. At the end, W->start happened to be BEG, while
1338 rmail had already narrowed the buffer. */
1339 if (XMARKER (w->start)->charpos < BEGV)
1340 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
1341 else if (XMARKER (w->start)->charpos > ZV)
1342 SET_TEXT_POS (startp, ZV, ZV_BYTE);
1343 else
1344 SET_TEXT_POS_FROM_MARKER (startp, w->start);
1345
1346 itdata = bidi_shelve_cache ();
1347 start_display (&it, w, startp);
1348 move_it_vertically (&it, window_box_height (w));
1349 if (it.current_y < it.last_visible_y)
1350 move_it_past_eol (&it);
1351 value = make_number (IT_CHARPOS (it));
1352 bidi_unshelve_cache (itdata, 0);
1353
1354 if (old_buffer)
1355 set_buffer_internal (old_buffer);
1356 }
1357 else
1358 XSETINT (value, BUF_Z (b) - XFASTINT (w->window_end_pos));
1359
1360 return value;
1361 }
1362
1363 DEFUN ("set-window-point", Fset_window_point, Sset_window_point, 2, 2, 0,
1364 doc: /* Make point value in WINDOW be at position POS in WINDOW's buffer.
1365 Return POS. */)
1366 (Lisp_Object window, Lisp_Object pos)
1367 {
1368 register struct window *w = decode_window (window);
1369
1370 CHECK_NUMBER_COERCE_MARKER (pos);
1371 if (w == XWINDOW (selected_window)
1372 && XBUFFER (w->buffer) == current_buffer)
1373 Fgoto_char (pos);
1374 else
1375 set_marker_restricted (w->pointm, pos, w->buffer);
1376
1377 /* We have to make sure that redisplay updates the window to show
1378 the new value of point. */
1379 if (!EQ (window, selected_window))
1380 ++windows_or_buffers_changed;
1381
1382 return pos;
1383 }
1384
1385 DEFUN ("set-window-start", Fset_window_start, Sset_window_start, 2, 3, 0,
1386 doc: /* Make display in WINDOW start at position POS in WINDOW's buffer.
1387 If WINDOW is nil, the selected window is used. Return POS.
1388 Optional third arg NOFORCE non-nil inhibits next redisplay from
1389 overriding motion of point in order to display at this exact start. */)
1390 (Lisp_Object window, Lisp_Object pos, Lisp_Object noforce)
1391 {
1392 register struct window *w = decode_window (window);
1393
1394 CHECK_NUMBER_COERCE_MARKER (pos);
1395 set_marker_restricted (w->start, pos, w->buffer);
1396 /* this is not right, but much easier than doing what is right. */
1397 w->start_at_line_beg = 0;
1398 if (NILP (noforce))
1399 w->force_start = 1;
1400 w->update_mode_line = 1;
1401 XSETFASTINT (w->last_modified, 0);
1402 XSETFASTINT (w->last_overlay_modified, 0);
1403 if (!EQ (window, selected_window))
1404 windows_or_buffers_changed++;
1405
1406 return pos;
1407 }
1408
1409 DEFUN ("pos-visible-in-window-p", Fpos_visible_in_window_p,
1410 Spos_visible_in_window_p, 0, 3, 0,
1411 doc: /* Return non-nil if position POS is currently on the frame in WINDOW.
1412 Return nil if that position is scrolled vertically out of view.
1413 If a character is only partially visible, nil is returned, unless the
1414 optional argument PARTIALLY is non-nil.
1415 If POS is only out of view because of horizontal scrolling, return non-nil.
1416 If POS is t, it specifies the position of the last visible glyph in WINDOW.
1417 POS defaults to point in WINDOW; WINDOW defaults to the selected window.
1418
1419 If POS is visible, return t if PARTIALLY is nil; if PARTIALLY is non-nil,
1420 return value is a list of 2 or 6 elements (X Y [RTOP RBOT ROWH VPOS]),
1421 where X and Y are the pixel coordinates relative to the top left corner
1422 of the window. The remaining elements are omitted if the character after
1423 POS is fully visible; otherwise, RTOP and RBOT are the number of pixels
1424 off-window at the top and bottom of the row, ROWH is the height of the
1425 display row, and VPOS is the row number (0-based) containing POS. */)
1426 (Lisp_Object pos, Lisp_Object window, Lisp_Object partially)
1427 {
1428 register struct window *w;
1429 register EMACS_INT posint;
1430 register struct buffer *buf;
1431 struct text_pos top;
1432 Lisp_Object in_window = Qnil;
1433 int rtop, rbot, rowh, vpos, fully_p = 1;
1434 int x, y;
1435
1436 w = decode_window (window);
1437 buf = XBUFFER (w->buffer);
1438 SET_TEXT_POS_FROM_MARKER (top, w->start);
1439
1440 if (EQ (pos, Qt))
1441 posint = -1;
1442 else if (!NILP (pos))
1443 {
1444 CHECK_NUMBER_COERCE_MARKER (pos);
1445 posint = XINT (pos);
1446 }
1447 else if (w == XWINDOW (selected_window))
1448 posint = PT;
1449 else
1450 posint = XMARKER (w->pointm)->charpos;
1451
1452 /* If position is above window start or outside buffer boundaries,
1453 or if window start is out of range, position is not visible. */
1454 if ((EQ (pos, Qt)
1455 || (posint >= CHARPOS (top) && posint <= BUF_ZV (buf)))
1456 && CHARPOS (top) >= BUF_BEGV (buf)
1457 && CHARPOS (top) <= BUF_ZV (buf)
1458 && pos_visible_p (w, posint, &x, &y, &rtop, &rbot, &rowh, &vpos)
1459 && (fully_p = !rtop && !rbot, (!NILP (partially) || fully_p)))
1460 in_window = Qt;
1461
1462 if (!NILP (in_window) && !NILP (partially))
1463 {
1464 Lisp_Object part = Qnil;
1465 if (!fully_p)
1466 part = list4 (make_number (rtop), make_number (rbot),
1467 make_number (rowh), make_number (vpos));
1468 in_window = Fcons (make_number (x),
1469 Fcons (make_number (y), part));
1470 }
1471
1472 return in_window;
1473 }
1474
1475 DEFUN ("window-line-height", Fwindow_line_height,
1476 Swindow_line_height, 0, 2, 0,
1477 doc: /* Return height in pixels of text line LINE in window WINDOW.
1478 WINDOW defaults to the selected window.
1479
1480 Return height of current line if LINE is omitted or nil. Return height of
1481 header or mode line if LINE is `header-line' or `mode-line'.
1482 Otherwise, LINE is a text line number starting from 0. A negative number
1483 counts from the end of the window.
1484
1485 Value is a list (HEIGHT VPOS YPOS OFFBOT), where HEIGHT is the height
1486 in pixels of the visible part of the line, VPOS and YPOS are the
1487 vertical position in lines and pixels of the line, relative to the top
1488 of the first text line, and OFFBOT is the number of off-window pixels at
1489 the bottom of the text line. If there are off-window pixels at the top
1490 of the (first) text line, YPOS is negative.
1491
1492 Return nil if window display is not up-to-date. In that case, use
1493 `pos-visible-in-window-p' to obtain the information. */)
1494 (Lisp_Object line, Lisp_Object window)
1495 {
1496 register struct window *w;
1497 register struct buffer *b;
1498 struct glyph_row *row, *end_row;
1499 int max_y, crop, i;
1500 EMACS_INT n;
1501
1502 w = decode_window (window);
1503
1504 if (noninteractive || w->pseudo_window_p)
1505 return Qnil;
1506
1507 CHECK_BUFFER (w->buffer);
1508 b = XBUFFER (w->buffer);
1509
1510 /* Fail if current matrix is not up-to-date. */
1511 if (NILP (w->window_end_valid)
1512 || current_buffer->clip_changed
1513 || current_buffer->prevent_redisplay_optimizations_p
1514 || XFASTINT (w->last_modified) < BUF_MODIFF (b)
1515 || XFASTINT (w->last_overlay_modified) < BUF_OVERLAY_MODIFF (b))
1516 return Qnil;
1517
1518 if (NILP (line))
1519 {
1520 i = w->cursor.vpos;
1521 if (i < 0 || i >= w->current_matrix->nrows
1522 || (row = MATRIX_ROW (w->current_matrix, i), !row->enabled_p))
1523 return Qnil;
1524 max_y = window_text_bottom_y (w);
1525 goto found_row;
1526 }
1527
1528 if (EQ (line, Qheader_line))
1529 {
1530 if (!WINDOW_WANTS_HEADER_LINE_P (w))
1531 return Qnil;
1532 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1533 if (!row->enabled_p)
1534 return Qnil;
1535 return list4 (make_number (row->height),
1536 make_number (0), make_number (0),
1537 make_number (0));
1538 }
1539
1540 if (EQ (line, Qmode_line))
1541 {
1542 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1543 if (!row->enabled_p)
1544 return Qnil;
1545 return list4 (make_number (row->height),
1546 make_number (0), /* not accurate */
1547 make_number (WINDOW_HEADER_LINE_HEIGHT (w)
1548 + window_text_bottom_y (w)),
1549 make_number (0));
1550 }
1551
1552 CHECK_NUMBER (line);
1553 n = XINT (line);
1554
1555 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1556 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1557 max_y = window_text_bottom_y (w);
1558 i = 0;
1559
1560 while ((n < 0 || i < n)
1561 && row <= end_row && row->enabled_p
1562 && row->y + row->height < max_y)
1563 row++, i++;
1564
1565 if (row > end_row || !row->enabled_p)
1566 return Qnil;
1567
1568 if (++n < 0)
1569 {
1570 if (-n > i)
1571 return Qnil;
1572 row += n;
1573 i += n;
1574 }
1575
1576 found_row:
1577 crop = max (0, (row->y + row->height) - max_y);
1578 return list4 (make_number (row->height + min (0, row->y) - crop),
1579 make_number (i),
1580 make_number (row->y),
1581 make_number (crop));
1582 }
1583
1584 DEFUN ("window-dedicated-p", Fwindow_dedicated_p, Swindow_dedicated_p,
1585 0, 1, 0,
1586 doc: /* Return non-nil when WINDOW is dedicated to its buffer.
1587 More precisely, return the value assigned by the last call of
1588 `set-window-dedicated-p' for WINDOW. Return nil if that function was
1589 never called with WINDOW as its argument, or the value set by that
1590 function was internally reset since its last call. WINDOW defaults to
1591 the selected window.
1592
1593 When a window is dedicated to its buffer, `display-buffer' will refrain
1594 from displaying another buffer in it. `get-lru-window' and
1595 `get-largest-window' treat dedicated windows specially.
1596 `delete-windows-on', `replace-buffer-in-windows', `quit-window' and
1597 `kill-buffer' can delete a dedicated window and the containing frame.
1598
1599 Functions like `set-window-buffer' may change the buffer displayed by a
1600 window, unless that window is "strongly" dedicated to its buffer, that
1601 is the value returned by `window-dedicated-p' is t. */)
1602 (Lisp_Object window)
1603 {
1604 return decode_window (window)->dedicated;
1605 }
1606
1607 DEFUN ("set-window-dedicated-p", Fset_window_dedicated_p,
1608 Sset_window_dedicated_p, 2, 2, 0,
1609 doc: /* Mark WINDOW as dedicated according to FLAG.
1610 WINDOW must be a live window and defaults to the selected one. FLAG
1611 non-nil means mark WINDOW as dedicated to its buffer. FLAG nil means
1612 mark WINDOW as non-dedicated. Return FLAG.
1613
1614 When a window is dedicated to its buffer, `display-buffer' will refrain
1615 from displaying another buffer in it. `get-lru-window' and
1616 `get-largest-window' treat dedicated windows specially.
1617 `delete-windows-on', `replace-buffer-in-windows', `quit-window',
1618 `quit-restore-window' and `kill-buffer' can delete a dedicated window
1619 and the containing frame.
1620
1621 As a special case, if FLAG is t, mark WINDOW as "strongly" dedicated to
1622 its buffer. Functions like `set-window-buffer' may change the buffer
1623 displayed by a window, unless that window is strongly dedicated to its
1624 buffer. If and when `set-window-buffer' displays another buffer in a
1625 window, it also makes sure that the window is no more dedicated. */)
1626 (Lisp_Object window, Lisp_Object flag)
1627 {
1628 register struct window *w = decode_window (window);
1629
1630 w->dedicated = flag;
1631 return w->dedicated;
1632 }
1633
1634 DEFUN ("window-prev-buffers", Fwindow_prev_buffers, Swindow_prev_buffers,
1635 0, 1, 0,
1636 doc: /* Return buffers previously shown in WINDOW.
1637 WINDOW must be a live window and defaults to the selected one.
1638
1639 The return value is a list of elements (BUFFER WINDOW-START POS),
1640 where BUFFER is a buffer, WINDOW-START is the start position of the
1641 window for that buffer, and POS is a window-specific point value. */)
1642 (Lisp_Object window)
1643 {
1644 return decode_window (window)->prev_buffers;
1645 }
1646
1647 DEFUN ("set-window-prev-buffers", Fset_window_prev_buffers,
1648 Sset_window_prev_buffers, 2, 2, 0,
1649 doc: /* Set WINDOW's previous buffers to PREV-BUFFERS.
1650 WINDOW must be a live window and defaults to the selected one.
1651
1652 PREV-BUFFERS should be a list of elements (BUFFER WINDOW-START POS),
1653 where BUFFER is a buffer, WINDOW-START is the start position of the
1654 window for that buffer, and POS is a window-specific point value. */)
1655 (Lisp_Object window, Lisp_Object prev_buffers)
1656 {
1657 return decode_window (window)->prev_buffers = prev_buffers;
1658 }
1659
1660 DEFUN ("window-next-buffers", Fwindow_next_buffers, Swindow_next_buffers,
1661 0, 1, 0,
1662 doc: /* Return list of buffers recently re-shown in WINDOW.
1663 WINDOW must be a live window and defaults to the selected one. */)
1664 (Lisp_Object window)
1665 {
1666 return decode_window (window)->next_buffers;
1667 }
1668
1669 DEFUN ("set-window-next-buffers", Fset_window_next_buffers,
1670 Sset_window_next_buffers, 2, 2, 0,
1671 doc: /* Set WINDOW's next buffers to NEXT-BUFFERS.
1672 WINDOW must be a live window and defaults to the selected one.
1673 NEXT-BUFFERS should be a list of buffers. */)
1674 (Lisp_Object window, Lisp_Object next_buffers)
1675 {
1676 return decode_window (window)->next_buffers = next_buffers;
1677 }
1678
1679 DEFUN ("window-parameters", Fwindow_parameters, Swindow_parameters,
1680 0, 1, 0,
1681 doc: /* Return the parameters of WINDOW and their values.
1682 WINDOW defaults to the selected window. The return value is a list of
1683 elements of the form (PARAMETER . VALUE). */)
1684 (Lisp_Object window)
1685 {
1686 return Fcopy_alist (decode_any_window (window)->window_parameters);
1687 }
1688
1689 DEFUN ("window-parameter", Fwindow_parameter, Swindow_parameter,
1690 2, 2, 0,
1691 doc: /* Return WINDOW's value for PARAMETER.
1692 WINDOW defaults to the selected window. */)
1693 (Lisp_Object window, Lisp_Object parameter)
1694 {
1695 Lisp_Object result;
1696
1697 result = Fassq (parameter, decode_any_window (window)->window_parameters);
1698 return CDR_SAFE (result);
1699 }
1700
1701 DEFUN ("set-window-parameter", Fset_window_parameter,
1702 Sset_window_parameter, 3, 3, 0,
1703 doc: /* Set WINDOW's value of PARAMETER to VALUE.
1704 WINDOW defaults to the selected window. Return VALUE. */)
1705 (Lisp_Object window, Lisp_Object parameter, Lisp_Object value)
1706 {
1707 register struct window *w = decode_any_window (window);
1708 Lisp_Object old_alist_elt;
1709
1710 old_alist_elt = Fassq (parameter, w->window_parameters);
1711 if (NILP (old_alist_elt))
1712 w->window_parameters = Fcons (Fcons (parameter, value), w->window_parameters);
1713 else
1714 Fsetcdr (old_alist_elt, value);
1715 return value;
1716 }
1717
1718 DEFUN ("window-display-table", Fwindow_display_table, Swindow_display_table,
1719 0, 1, 0,
1720 doc: /* Return the display-table that WINDOW is using.
1721 WINDOW defaults to the selected window. */)
1722 (Lisp_Object window)
1723 {
1724 return decode_window (window)->display_table;
1725 }
1726
1727 /* Get the display table for use on window W. This is either W's
1728 display table or W's buffer's display table. Ignore the specified
1729 tables if they are not valid; if no valid table is specified,
1730 return 0. */
1731
1732 struct Lisp_Char_Table *
1733 window_display_table (struct window *w)
1734 {
1735 struct Lisp_Char_Table *dp = NULL;
1736
1737 if (DISP_TABLE_P (w->display_table))
1738 dp = XCHAR_TABLE (w->display_table);
1739 else if (BUFFERP (w->buffer))
1740 {
1741 struct buffer *b = XBUFFER (w->buffer);
1742
1743 if (DISP_TABLE_P (BVAR (b, display_table)))
1744 dp = XCHAR_TABLE (BVAR (b, display_table));
1745 else if (DISP_TABLE_P (Vstandard_display_table))
1746 dp = XCHAR_TABLE (Vstandard_display_table);
1747 }
1748
1749 return dp;
1750 }
1751
1752 DEFUN ("set-window-display-table", Fset_window_display_table, Sset_window_display_table, 2, 2, 0,
1753 doc: /* Set WINDOW's display-table to TABLE. */)
1754 (register Lisp_Object window, Lisp_Object table)
1755 {
1756 register struct window *w;
1757
1758 w = decode_window (window);
1759 w->display_table = table;
1760 return table;
1761 }
1762 \f
1763 /* Record info on buffer window W is displaying
1764 when it is about to cease to display that buffer. */
1765 static void
1766 unshow_buffer (register struct window *w)
1767 {
1768 Lisp_Object buf;
1769 struct buffer *b;
1770
1771 buf = w->buffer;
1772 b = XBUFFER (buf);
1773 if (b != XMARKER (w->pointm)->buffer)
1774 abort ();
1775
1776 #if 0
1777 if (w == XWINDOW (selected_window)
1778 || ! EQ (buf, XWINDOW (selected_window)->buffer))
1779 /* Do this except when the selected window's buffer
1780 is being removed from some other window. */
1781 #endif
1782 /* last_window_start records the start position that this buffer
1783 had in the last window to be disconnected from it.
1784 Now that this statement is unconditional,
1785 it is possible for the buffer to be displayed in the
1786 selected window, while last_window_start reflects another
1787 window which was recently showing the same buffer.
1788 Some people might say that might be a good thing. Let's see. */
1789 b->last_window_start = marker_position (w->start);
1790
1791 /* Point in the selected window's buffer
1792 is actually stored in that buffer, and the window's pointm isn't used.
1793 So don't clobber point in that buffer. */
1794 if (! EQ (buf, XWINDOW (selected_window)->buffer)
1795 /* This line helps to fix Horsley's testbug.el bug. */
1796 && !(WINDOWP (BVAR (b, last_selected_window))
1797 && w != XWINDOW (BVAR (b, last_selected_window))
1798 && EQ (buf, XWINDOW (BVAR (b, last_selected_window))->buffer)))
1799 temp_set_point_both (b,
1800 clip_to_bounds (BUF_BEGV (b),
1801 XMARKER (w->pointm)->charpos,
1802 BUF_ZV (b)),
1803 clip_to_bounds (BUF_BEGV_BYTE (b),
1804 marker_byte_position (w->pointm),
1805 BUF_ZV_BYTE (b)));
1806
1807 if (WINDOWP (BVAR (b, last_selected_window))
1808 && w == XWINDOW (BVAR (b, last_selected_window)))
1809 BVAR (b, last_selected_window) = Qnil;
1810 }
1811
1812 /* Put NEW into the window structure in place of OLD. SETFLAG zero
1813 means change window structure only. Otherwise store geometry and
1814 other settings as well. */
1815 static void
1816 replace_window (Lisp_Object old, Lisp_Object new, int setflag)
1817 {
1818 register Lisp_Object tem;
1819 register struct window *o = XWINDOW (old), *n = XWINDOW (new);
1820
1821 /* If OLD is its frame's root window, then NEW is the new
1822 root window for that frame. */
1823 if (EQ (old, FRAME_ROOT_WINDOW (XFRAME (o->frame))))
1824 FRAME_ROOT_WINDOW (XFRAME (o->frame)) = new;
1825
1826 if (setflag)
1827 {
1828 n->left_col = o->left_col;
1829 n->top_line = o->top_line;
1830 n->total_cols = o->total_cols;
1831 n->total_lines = o->total_lines;
1832 n->normal_cols = o->normal_cols;
1833 o->normal_cols = make_float (1.0);
1834 n->normal_lines = o->normal_lines;
1835 o->normal_lines = make_float (1.0);
1836 n->desired_matrix = n->current_matrix = 0;
1837 n->vscroll = 0;
1838 memset (&n->cursor, 0, sizeof (n->cursor));
1839 memset (&n->last_cursor, 0, sizeof (n->last_cursor));
1840 memset (&n->phys_cursor, 0, sizeof (n->phys_cursor));
1841 n->phys_cursor_type = -1;
1842 n->phys_cursor_width = -1;
1843 n->must_be_updated_p = 0;
1844 n->pseudo_window_p = 0;
1845 XSETFASTINT (n->window_end_vpos, 0);
1846 XSETFASTINT (n->window_end_pos, 0);
1847 n->window_end_valid = Qnil;
1848 n->frozen_window_start_p = 0;
1849 }
1850
1851 n->next = tem = o->next;
1852 if (!NILP (tem))
1853 XWINDOW (tem)->prev = new;
1854
1855 n->prev = tem = o->prev;
1856 if (!NILP (tem))
1857 XWINDOW (tem)->next = new;
1858
1859 n->parent = tem = o->parent;
1860 if (!NILP (tem))
1861 {
1862 if (EQ (XWINDOW (tem)->vchild, old))
1863 XWINDOW (tem)->vchild = new;
1864 if (EQ (XWINDOW (tem)->hchild, old))
1865 XWINDOW (tem)->hchild = new;
1866 }
1867 }
1868
1869 /* If window WINDOW and its parent window are iso-combined, merge
1870 WINDOW's children into those of its parent window and mark WINDOW as
1871 deleted. */
1872
1873 static void
1874 recombine_windows (Lisp_Object window)
1875 {
1876 struct window *w, *p, *c;
1877 Lisp_Object parent, child;
1878 int horflag;
1879
1880 w = XWINDOW (window);
1881 parent = w->parent;
1882 if (!NILP (parent) && NILP (w->combination_limit))
1883 {
1884 p = XWINDOW (parent);
1885 if (((!NILP (p->vchild) && !NILP (w->vchild))
1886 || (!NILP (p->hchild) && !NILP (w->hchild))))
1887 /* WINDOW and PARENT are both either a vertical or a horizontal
1888 combination. */
1889 {
1890 horflag = NILP (w->vchild);
1891 child = horflag ? w->hchild : w->vchild;
1892 c = XWINDOW (child);
1893
1894 /* Splice WINDOW's children into its parent's children and
1895 assign new normal sizes. */
1896 if (NILP (w->prev))
1897 if (horflag)
1898 p->hchild = child;
1899 else
1900 p->vchild = child;
1901 else
1902 {
1903 c->prev = w->prev;
1904 XWINDOW (w->prev)->next = child;
1905 }
1906
1907 while (c)
1908 {
1909 c->parent = parent;
1910
1911 if (horflag)
1912 c->normal_cols
1913 = make_float (XFLOATINT (c->total_cols)
1914 / XFLOATINT (p->total_cols));
1915 else
1916 c->normal_lines
1917 = make_float (XFLOATINT (c->total_lines)
1918 / XFLOATINT (p->total_lines));
1919
1920 if (NILP (c->next))
1921 {
1922 if (!NILP (w->next))
1923 {
1924 c->next = w->next;
1925 XWINDOW (c->next)->prev = child;
1926 }
1927
1928 c = 0;
1929 }
1930 else
1931 {
1932 child = c->next;
1933 c = XWINDOW (child);
1934 }
1935 }
1936
1937 /* WINDOW can be deleted now. */
1938 w->vchild = w->hchild = Qnil;
1939 }
1940 }
1941 }
1942
1943 /* If WINDOW can be deleted, delete it. */
1944 static void
1945 delete_deletable_window (Lisp_Object window)
1946 {
1947 if (!NILP (call1 (Qwindow_deletable_p, window)))
1948 call1 (Qdelete_window, window);
1949 }
1950 \f
1951 /***********************************************************************
1952 Window List
1953 ***********************************************************************/
1954
1955 /* Add window W to *USER_DATA. USER_DATA is actually a Lisp_Object
1956 pointer. This is a callback function for foreach_window, used in
1957 the window_list function. */
1958
1959 static int
1960 add_window_to_list (struct window *w, void *user_data)
1961 {
1962 Lisp_Object *list = (Lisp_Object *) user_data;
1963 Lisp_Object window;
1964 XSETWINDOW (window, w);
1965 *list = Fcons (window, *list);
1966 return 1;
1967 }
1968
1969
1970 /* Return a list of all windows, for use by next_window. If
1971 Vwindow_list is a list, return that list. Otherwise, build a new
1972 list, cache it in Vwindow_list, and return that. */
1973
1974 static Lisp_Object
1975 window_list (void)
1976 {
1977 if (!CONSP (Vwindow_list))
1978 {
1979 Lisp_Object tail;
1980
1981 Vwindow_list = Qnil;
1982 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1983 {
1984 Lisp_Object args[2];
1985
1986 /* We are visiting windows in canonical order, and add
1987 new windows at the front of args[1], which means we
1988 have to reverse this list at the end. */
1989 args[1] = Qnil;
1990 foreach_window (XFRAME (XCAR (tail)), add_window_to_list, &args[1]);
1991 args[0] = Vwindow_list;
1992 args[1] = Fnreverse (args[1]);
1993 Vwindow_list = Fnconc (2, args);
1994 }
1995 }
1996
1997 return Vwindow_list;
1998 }
1999
2000
2001 /* Value is non-zero if WINDOW satisfies the constraints given by
2002 OWINDOW, MINIBUF and ALL_FRAMES.
2003
2004 MINIBUF t means WINDOW may be minibuffer windows.
2005 `lambda' means WINDOW may not be a minibuffer window.
2006 a window means a specific minibuffer window
2007
2008 ALL_FRAMES t means search all frames,
2009 nil means search just current frame,
2010 `visible' means search just visible frames on the
2011 current terminal,
2012 0 means search visible and iconified frames on the
2013 current terminal,
2014 a window means search the frame that window belongs to,
2015 a frame means consider windows on that frame, only. */
2016
2017 static int
2018 candidate_window_p (Lisp_Object window, Lisp_Object owindow, Lisp_Object minibuf, Lisp_Object all_frames)
2019 {
2020 struct window *w = XWINDOW (window);
2021 struct frame *f = XFRAME (w->frame);
2022 int candidate_p = 1;
2023
2024 if (!BUFFERP (w->buffer))
2025 candidate_p = 0;
2026 else if (MINI_WINDOW_P (w)
2027 && (EQ (minibuf, Qlambda)
2028 || (WINDOWP (minibuf) && !EQ (minibuf, window))))
2029 {
2030 /* If MINIBUF is `lambda' don't consider any mini-windows.
2031 If it is a window, consider only that one. */
2032 candidate_p = 0;
2033 }
2034 else if (EQ (all_frames, Qt))
2035 candidate_p = 1;
2036 else if (NILP (all_frames))
2037 {
2038 eassert (WINDOWP (owindow));
2039 candidate_p = EQ (w->frame, XWINDOW (owindow)->frame);
2040 }
2041 else if (EQ (all_frames, Qvisible))
2042 {
2043 FRAME_SAMPLE_VISIBILITY (f);
2044 candidate_p = FRAME_VISIBLE_P (f)
2045 && (FRAME_TERMINAL (XFRAME (w->frame))
2046 == FRAME_TERMINAL (XFRAME (selected_frame)));
2047
2048 }
2049 else if (INTEGERP (all_frames) && XINT (all_frames) == 0)
2050 {
2051 FRAME_SAMPLE_VISIBILITY (f);
2052 candidate_p = (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)
2053 #ifdef HAVE_X_WINDOWS
2054 /* Yuck!! If we've just created the frame and the
2055 window-manager requested the user to place it
2056 manually, the window may still not be considered
2057 `visible'. I'd argue it should be at least
2058 something like `iconified', but don't know how to do
2059 that yet. --Stef */
2060 || (FRAME_X_P (f) && f->output_data.x->asked_for_visible
2061 && !f->output_data.x->has_been_visible)
2062 #endif
2063 )
2064 && (FRAME_TERMINAL (XFRAME (w->frame))
2065 == FRAME_TERMINAL (XFRAME (selected_frame)));
2066 }
2067 else if (WINDOWP (all_frames))
2068 candidate_p = (EQ (FRAME_MINIBUF_WINDOW (f), all_frames)
2069 || EQ (XWINDOW (all_frames)->frame, w->frame)
2070 || EQ (XWINDOW (all_frames)->frame, FRAME_FOCUS_FRAME (f)));
2071 else if (FRAMEP (all_frames))
2072 candidate_p = EQ (all_frames, w->frame);
2073
2074 return candidate_p;
2075 }
2076
2077
2078 /* Decode arguments as allowed by Fnext_window, Fprevious_window, and
2079 Fwindow_list. See candidate_window_p for the meaning of WINDOW,
2080 MINIBUF, and ALL_FRAMES. */
2081
2082 static void
2083 decode_next_window_args (Lisp_Object *window, Lisp_Object *minibuf, Lisp_Object *all_frames)
2084 {
2085 if (NILP (*window))
2086 *window = selected_window;
2087 else
2088 CHECK_LIVE_WINDOW (*window);
2089
2090 /* MINIBUF nil may or may not include minibuffers. Decide if it
2091 does. */
2092 if (NILP (*minibuf))
2093 *minibuf = minibuf_level ? minibuf_window : Qlambda;
2094 else if (!EQ (*minibuf, Qt))
2095 *minibuf = Qlambda;
2096
2097 /* Now *MINIBUF can be t => count all minibuffer windows, `lambda'
2098 => count none of them, or a specific minibuffer window (the
2099 active one) to count. */
2100
2101 /* ALL_FRAMES nil doesn't specify which frames to include. */
2102 if (NILP (*all_frames))
2103 *all_frames = (!EQ (*minibuf, Qlambda)
2104 ? FRAME_MINIBUF_WINDOW (XFRAME (XWINDOW (*window)->frame))
2105 : Qnil);
2106 else if (EQ (*all_frames, Qvisible))
2107 ;
2108 else if (EQ (*all_frames, make_number (0)))
2109 ;
2110 else if (FRAMEP (*all_frames))
2111 ;
2112 else if (!EQ (*all_frames, Qt))
2113 *all_frames = Qnil;
2114 }
2115
2116
2117 /* Return the next or previous window of WINDOW in cyclic ordering
2118 of windows. NEXT_P non-zero means return the next window. See the
2119 documentation string of next-window for the meaning of MINIBUF and
2120 ALL_FRAMES. */
2121
2122 static Lisp_Object
2123 next_window (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames, int next_p)
2124 {
2125 decode_next_window_args (&window, &minibuf, &all_frames);
2126
2127 /* If ALL_FRAMES is a frame, and WINDOW isn't on that frame, just
2128 return the first window on the frame. */
2129 if (FRAMEP (all_frames)
2130 && !EQ (all_frames, XWINDOW (window)->frame))
2131 return Fframe_first_window (all_frames);
2132
2133 if (next_p)
2134 {
2135 Lisp_Object list;
2136
2137 /* Find WINDOW in the list of all windows. */
2138 list = Fmemq (window, window_list ());
2139
2140 /* Scan forward from WINDOW to the end of the window list. */
2141 if (CONSP (list))
2142 for (list = XCDR (list); CONSP (list); list = XCDR (list))
2143 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2144 break;
2145
2146 /* Scan from the start of the window list up to WINDOW. */
2147 if (!CONSP (list))
2148 for (list = Vwindow_list;
2149 CONSP (list) && !EQ (XCAR (list), window);
2150 list = XCDR (list))
2151 if (candidate_window_p (XCAR (list), window, minibuf, all_frames))
2152 break;
2153
2154 if (CONSP (list))
2155 window = XCAR (list);
2156 }
2157 else
2158 {
2159 Lisp_Object candidate, list;
2160
2161 /* Scan through the list of windows for candidates. If there are
2162 candidate windows in front of WINDOW, the last one of these
2163 is the one we want. If there are candidates following WINDOW
2164 in the list, again the last one of these is the one we want. */
2165 candidate = Qnil;
2166 for (list = window_list (); CONSP (list); list = XCDR (list))
2167 {
2168 if (EQ (XCAR (list), window))
2169 {
2170 if (WINDOWP (candidate))
2171 break;
2172 }
2173 else if (candidate_window_p (XCAR (list), window, minibuf,
2174 all_frames))
2175 candidate = XCAR (list);
2176 }
2177
2178 if (WINDOWP (candidate))
2179 window = candidate;
2180 }
2181
2182 return window;
2183 }
2184
2185
2186 DEFUN ("next-window", Fnext_window, Snext_window, 0, 3, 0,
2187 doc: /* Return live window after WINDOW in the cyclic ordering of windows.
2188 WINDOW must be a live window and defaults to the selected one. The
2189 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2190 consider.
2191
2192 MINIBUF nil or omitted means consider the minibuffer window only if the
2193 minibuffer is active. MINIBUF t means consider the minibuffer window
2194 even if the minibuffer is not active. Any other value means do not
2195 consider the minibuffer window even if the minibuffer is active.
2196
2197 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2198 plus the minibuffer window if specified by the MINIBUF argument. If the
2199 minibuffer counts, consider all windows on all frames that share that
2200 minibuffer too. The following non-nil values of ALL-FRAMES have special
2201 meanings:
2202
2203 - t means consider all windows on all existing frames.
2204
2205 - `visible' means consider all windows on all visible frames.
2206
2207 - 0 (the number zero) means consider all windows on all visible and
2208 iconified frames.
2209
2210 - A frame means consider all windows on that frame only.
2211
2212 Anything else means consider all windows on WINDOW's frame and no
2213 others.
2214
2215 If you use consistent values for MINIBUF and ALL-FRAMES, you can use
2216 `next-window' to iterate through the entire cycle of acceptable
2217 windows, eventually ending up back at the window you started with.
2218 `previous-window' traverses the same cycle, in the reverse order. */)
2219 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2220 {
2221 return next_window (window, minibuf, all_frames, 1);
2222 }
2223
2224
2225 DEFUN ("previous-window", Fprevious_window, Sprevious_window, 0, 3, 0,
2226 doc: /* Return live window before WINDOW in the cyclic ordering of windows.
2227 WINDOW must be a live window and defaults to the selected one. The
2228 optional arguments MINIBUF and ALL-FRAMES specify the set of windows to
2229 consider.
2230
2231 MINIBUF nil or omitted means consider the minibuffer window only if the
2232 minibuffer is active. MINIBUF t means consider the minibuffer window
2233 even if the minibuffer is not active. Any other value means do not
2234 consider the minibuffer window even if the minibuffer is active.
2235
2236 ALL-FRAMES nil or omitted means consider all windows on WINDOW's frame,
2237 plus the minibuffer window if specified by the MINIBUF argument. If the
2238 minibuffer counts, consider all windows on all frames that share that
2239 minibuffer too. The following non-nil values of ALL-FRAMES have special
2240 meanings:
2241
2242 - t means consider all windows on all existing frames.
2243
2244 - `visible' means consider all windows on all visible frames.
2245
2246 - 0 (the number zero) means consider all windows on all visible and
2247 iconified frames.
2248
2249 - A frame means consider all windows on that frame only.
2250
2251 Anything else means consider all windows on WINDOW's frame and no
2252 others.
2253
2254 If you use consistent values for MINIBUF and ALL-FRAMES, you can
2255 use `previous-window' to iterate through the entire cycle of
2256 acceptable windows, eventually ending up back at the window you
2257 started with. `next-window' traverses the same cycle, in the
2258 reverse order. */)
2259 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2260 {
2261 return next_window (window, minibuf, all_frames, 0);
2262 }
2263
2264
2265 /* Return a list of windows in cyclic ordering. Arguments are like
2266 for `next-window'. */
2267
2268 static Lisp_Object
2269 window_list_1 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2270 {
2271 Lisp_Object tail, list, rest;
2272
2273 decode_next_window_args (&window, &minibuf, &all_frames);
2274 list = Qnil;
2275
2276 for (tail = window_list (); CONSP (tail); tail = XCDR (tail))
2277 if (candidate_window_p (XCAR (tail), window, minibuf, all_frames))
2278 list = Fcons (XCAR (tail), list);
2279
2280 /* Rotate the list to start with WINDOW. */
2281 list = Fnreverse (list);
2282 rest = Fmemq (window, list);
2283 if (!NILP (rest) && !EQ (rest, list))
2284 {
2285 for (tail = list; !EQ (XCDR (tail), rest); tail = XCDR (tail))
2286 ;
2287 XSETCDR (tail, Qnil);
2288 list = nconc2 (rest, list);
2289 }
2290 return list;
2291 }
2292
2293
2294 DEFUN ("window-list", Fwindow_list, Swindow_list, 0, 3, 0,
2295 doc: /* Return a list of windows on FRAME, starting with WINDOW.
2296 FRAME nil or omitted means use the selected frame.
2297 WINDOW nil or omitted means use the window selected within FRAME.
2298 MINIBUF t means include the minibuffer window, even if it isn't active.
2299 MINIBUF nil or omitted means include the minibuffer window only
2300 if it's active.
2301 MINIBUF neither nil nor t means never include the minibuffer window. */)
2302 (Lisp_Object frame, Lisp_Object minibuf, Lisp_Object window)
2303 {
2304 if (NILP (window))
2305 window = FRAMEP (frame) ? XFRAME (frame)->selected_window : selected_window;
2306 CHECK_WINDOW (window);
2307 if (NILP (frame))
2308 frame = selected_frame;
2309
2310 if (!EQ (frame, XWINDOW (window)->frame))
2311 error ("Window is on a different frame");
2312
2313 return window_list_1 (window, minibuf, frame);
2314 }
2315
2316
2317 DEFUN ("window-list-1", Fwindow_list_1, Swindow_list_1, 0, 3, 0,
2318 doc: /* Return a list of all live windows.
2319 WINDOW specifies the first window to list and defaults to the selected
2320 window.
2321
2322 Optional argument MINIBUF nil or omitted means consider the minibuffer
2323 window only if the minibuffer is active. MINIBUF t means consider the
2324 minibuffer window even if the minibuffer is not active. Any other value
2325 means do not consider the minibuffer window even if the minibuffer is
2326 active.
2327
2328 Optional argument ALL-FRAMES nil or omitted means consider all windows
2329 on WINDOW's frame, plus the minibuffer window if specified by the
2330 MINIBUF argument. If the minibuffer counts, consider all windows on all
2331 frames that share that minibuffer too. The following non-nil values of
2332 ALL-FRAMES have special meanings:
2333
2334 - t means consider all windows on all existing frames.
2335
2336 - `visible' means consider all windows on all visible frames.
2337
2338 - 0 (the number zero) means consider all windows on all visible and
2339 iconified frames.
2340
2341 - A frame means consider all windows on that frame only.
2342
2343 Anything else means consider all windows on WINDOW's frame and no
2344 others.
2345
2346 If WINDOW is not on the list of windows returned, some other window will
2347 be listed first but no error is signaled. */)
2348 (Lisp_Object window, Lisp_Object minibuf, Lisp_Object all_frames)
2349 {
2350 return window_list_1 (window, minibuf, all_frames);
2351 }
2352 \f
2353 /* Look at all windows, performing an operation specified by TYPE
2354 with argument OBJ.
2355 If FRAMES is Qt, look at all frames;
2356 Qnil, look at just the selected frame;
2357 Qvisible, look at visible frames;
2358 a frame, just look at windows on that frame.
2359 If MINI is non-zero, perform the operation on minibuffer windows too. */
2360
2361 enum window_loop
2362 {
2363 WINDOW_LOOP_UNUSED,
2364 GET_BUFFER_WINDOW, /* Arg is buffer */
2365 REPLACE_BUFFER_IN_WINDOWS_SAFELY, /* Arg is buffer */
2366 REDISPLAY_BUFFER_WINDOWS, /* Arg is buffer */
2367 CHECK_ALL_WINDOWS
2368 };
2369
2370 static Lisp_Object
2371 window_loop (enum window_loop type, Lisp_Object obj, int mini, Lisp_Object frames)
2372 {
2373 Lisp_Object window, windows, best_window, frame_arg;
2374 int frame_best_window_flag = 0;
2375 struct frame *f;
2376 struct gcpro gcpro1;
2377
2378 /* If we're only looping through windows on a particular frame,
2379 frame points to that frame. If we're looping through windows
2380 on all frames, frame is 0. */
2381 if (FRAMEP (frames))
2382 f = XFRAME (frames);
2383 else if (NILP (frames))
2384 f = SELECTED_FRAME ();
2385 else
2386 f = NULL;
2387
2388 if (f)
2389 frame_arg = Qlambda;
2390 else if (EQ (frames, make_number (0)))
2391 frame_arg = frames;
2392 else if (EQ (frames, Qvisible))
2393 frame_arg = frames;
2394 else
2395 frame_arg = Qt;
2396
2397 /* frame_arg is Qlambda to stick to one frame,
2398 Qvisible to consider all visible frames,
2399 or Qt otherwise. */
2400
2401 /* Pick a window to start with. */
2402 if (WINDOWP (obj))
2403 window = obj;
2404 else if (f)
2405 window = FRAME_SELECTED_WINDOW (f);
2406 else
2407 window = FRAME_SELECTED_WINDOW (SELECTED_FRAME ());
2408
2409 windows = window_list_1 (window, mini ? Qt : Qnil, frame_arg);
2410 GCPRO1 (windows);
2411 best_window = Qnil;
2412
2413 for (; CONSP (windows); windows = XCDR (windows))
2414 {
2415 struct window *w;
2416
2417 window = XCAR (windows);
2418 w = XWINDOW (window);
2419
2420 /* Note that we do not pay attention here to whether the frame
2421 is visible, since Fwindow_list skips non-visible frames if
2422 that is desired, under the control of frame_arg. */
2423 if (!MINI_WINDOW_P (w)
2424 /* For REPLACE_BUFFER_IN_WINDOWS_SAFELY, we must always
2425 consider all windows. */
2426 || type == REPLACE_BUFFER_IN_WINDOWS_SAFELY
2427 || (mini && minibuf_level > 0))
2428 switch (type)
2429 {
2430 case GET_BUFFER_WINDOW:
2431 if (EQ (w->buffer, obj)
2432 /* Don't find any minibuffer window except the one that
2433 is currently in use. */
2434 && (MINI_WINDOW_P (w) ? EQ (window, minibuf_window) : 1))
2435 {
2436 if (EQ (window, selected_window))
2437 /* Preferably return the selected window. */
2438 RETURN_UNGCPRO (window);
2439 else if (EQ (XWINDOW (window)->frame, selected_frame)
2440 && !frame_best_window_flag)
2441 /* Prefer windows on the current frame (but don't
2442 choose another one if we have one already). */
2443 {
2444 best_window = window;
2445 frame_best_window_flag = 1;
2446 }
2447 else if (NILP (best_window))
2448 best_window = window;
2449 }
2450 break;
2451
2452 case REPLACE_BUFFER_IN_WINDOWS_SAFELY:
2453 /* We could simply check whether the buffer shown by window
2454 is live, and show another buffer in case it isn't. */
2455 if (EQ (w->buffer, obj))
2456 {
2457 /* Undedicate WINDOW. */
2458 w->dedicated = Qnil;
2459 /* Make WINDOW show the buffer returned by
2460 other_buffer_safely, don't run any hooks. */
2461 set_window_buffer
2462 (window, other_buffer_safely (w->buffer), 0, 0);
2463 /* If WINDOW is the selected window, make its buffer
2464 current. But do so only if the window shows the
2465 current buffer (Bug#6454). */
2466 if (EQ (window, selected_window)
2467 && XBUFFER (w->buffer) == current_buffer)
2468 Fset_buffer (w->buffer);
2469 }
2470 break;
2471
2472 case REDISPLAY_BUFFER_WINDOWS:
2473 if (EQ (w->buffer, obj))
2474 {
2475 mark_window_display_accurate (window, 0);
2476 w->update_mode_line = 1;
2477 XBUFFER (obj)->prevent_redisplay_optimizations_p = 1;
2478 ++update_mode_lines;
2479 best_window = window;
2480 }
2481 break;
2482
2483 /* Check for a window that has a killed buffer. */
2484 case CHECK_ALL_WINDOWS:
2485 if (! NILP (w->buffer)
2486 && NILP (BVAR (XBUFFER (w->buffer), name)))
2487 abort ();
2488 break;
2489
2490 case WINDOW_LOOP_UNUSED:
2491 break;
2492 }
2493 }
2494
2495 UNGCPRO;
2496 return best_window;
2497 }
2498
2499 /* Used for debugging. Abort if any window has a dead buffer. */
2500
2501 extern void check_all_windows (void) EXTERNALLY_VISIBLE;
2502 void
2503 check_all_windows (void)
2504 {
2505 window_loop (CHECK_ALL_WINDOWS, Qnil, 1, Qt);
2506 }
2507
2508 DEFUN ("get-buffer-window", Fget_buffer_window, Sget_buffer_window, 0, 2, 0,
2509 doc: /* Return a window currently displaying BUFFER-OR-NAME, or nil if none.
2510 BUFFER-OR-NAME may be a buffer or a buffer name and defaults to
2511 the current buffer.
2512
2513 The optional argument ALL-FRAMES specifies the frames to consider:
2514
2515 - t means consider all windows on all existing frames.
2516
2517 - `visible' means consider all windows on all visible frames.
2518
2519 - 0 (the number zero) means consider all windows on all visible
2520 and iconified frames.
2521
2522 - A frame means consider all windows on that frame only.
2523
2524 Any other value of ALL-FRAMES means consider all windows on the
2525 selected frame and no others. */)
2526 (Lisp_Object buffer_or_name, Lisp_Object all_frames)
2527 {
2528 Lisp_Object buffer;
2529
2530 if (NILP (buffer_or_name))
2531 buffer = Fcurrent_buffer ();
2532 else
2533 buffer = Fget_buffer (buffer_or_name);
2534
2535 if (BUFFERP (buffer))
2536 return window_loop (GET_BUFFER_WINDOW, buffer, 1, all_frames);
2537 else
2538 return Qnil;
2539 }
2540
2541 static Lisp_Object
2542 resize_root_window (Lisp_Object window, Lisp_Object delta, Lisp_Object horizontal, Lisp_Object ignore)
2543 {
2544 return call4 (Qwindow_resize_root_window, window, delta, horizontal, ignore);
2545 }
2546
2547
2548 DEFUN ("delete-other-windows-internal", Fdelete_other_windows_internal,
2549 Sdelete_other_windows_internal, 0, 2, "",
2550 doc: /* Make WINDOW fill its frame.
2551 Only the frame WINDOW is on is affected. WINDOW may be any window and
2552 defaults to the selected one.
2553
2554 Optional argument ROOT, if non-nil, must specify an internal window such
2555 that WINDOW is in its window subtree. If this is the case, replace ROOT
2556 by WINDOW and leave alone any windows not part of ROOT's subtree.
2557
2558 When WINDOW is live try to reduce display jumps by keeping the text
2559 previously visible in WINDOW in the same place on the frame. Doing this
2560 depends on the value of (window-start WINDOW), so if calling this
2561 function in a program gives strange scrolling, make sure the
2562 window-start value is reasonable when this function is called. */)
2563 (Lisp_Object window, Lisp_Object root)
2564 {
2565 struct window *w, *r, *s;
2566 struct frame *f;
2567 Lisp_Object sibling, pwindow, swindow IF_LINT (= Qnil), delta;
2568 ptrdiff_t startpos IF_LINT (= 0);
2569 int top IF_LINT (= 0), new_top, resize_failed;
2570
2571 w = decode_any_window (window);
2572 XSETWINDOW (window, w);
2573 f = XFRAME (w->frame);
2574
2575 if (NILP (root))
2576 /* ROOT is the frame's root window. */
2577 {
2578 root = FRAME_ROOT_WINDOW (f);
2579 r = XWINDOW (root);
2580 }
2581 else
2582 /* ROOT must be an ancestor of WINDOW. */
2583 {
2584 r = decode_any_window (root);
2585 pwindow = XWINDOW (window)->parent;
2586 while (!NILP (pwindow))
2587 if (EQ (pwindow, root))
2588 break;
2589 else
2590 pwindow = XWINDOW (pwindow)->parent;
2591 if (!EQ (pwindow, root))
2592 error ("Specified root is not an ancestor of specified window");
2593 }
2594
2595 if (EQ (window, root))
2596 /* A noop. */
2597 return Qnil;
2598 /* I don't understand the "top > 0" part below. If we deal with a
2599 standalone minibuffer it would have been caught by the preceding
2600 test. */
2601 else if (MINI_WINDOW_P (w)) /* && top > 0) */
2602 error ("Can't expand minibuffer to full frame");
2603
2604 if (!NILP (w->buffer))
2605 {
2606 startpos = marker_position (w->start);
2607 top = WINDOW_TOP_EDGE_LINE (w)
2608 - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2609 /* Make sure WINDOW is the frame's selected window. */
2610 if (!EQ (window, FRAME_SELECTED_WINDOW (f)))
2611 {
2612 if (EQ (selected_frame, w->frame))
2613 Fselect_window (window, Qnil);
2614 else
2615 FRAME_SELECTED_WINDOW (f) = window;
2616 }
2617 }
2618 else
2619 {
2620 /* See if the frame's selected window is a part of the window
2621 subtree rooted at WINDOW, by finding all the selected window's
2622 parents and comparing each one with WINDOW. If it isn't we
2623 need a new selected window for this frame. */
2624 swindow = FRAME_SELECTED_WINDOW (f);
2625 while (1)
2626 {
2627 pwindow = swindow;
2628 while (!NILP (pwindow) && !EQ (window, pwindow))
2629 pwindow = XWINDOW (pwindow)->parent;
2630
2631 if (EQ (window, pwindow))
2632 /* If WINDOW is an ancestor of SWINDOW, then SWINDOW is ok
2633 as the new selected window. */
2634 break;
2635 else
2636 /* Else try the previous window of SWINDOW. */
2637 swindow = Fprevious_window (swindow, Qlambda, Qnil);
2638 }
2639
2640 if (!EQ (swindow, FRAME_SELECTED_WINDOW (f)))
2641 {
2642 if (EQ (selected_frame, w->frame))
2643 Fselect_window (swindow, Qnil);
2644 else
2645 FRAME_SELECTED_WINDOW (f) = swindow;
2646 }
2647 }
2648
2649 BLOCK_INPUT;
2650 if (!FRAME_INITIAL_P (f))
2651 {
2652 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
2653
2654 /* We are going to free the glyph matrices of WINDOW, and with
2655 that we might lose any information about glyph rows that have
2656 some of their glyphs highlighted in mouse face. (These rows
2657 are marked with a non-zero mouse_face_p flag.) If WINDOW
2658 indeed has some glyphs highlighted in mouse face, signal to
2659 frame's up-to-date hook that mouse highlight was overwritten,
2660 so that it will arrange for redisplaying the highlight. */
2661 if (EQ (hlinfo->mouse_face_window, window))
2662 {
2663 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
2664 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
2665 hlinfo->mouse_face_window = Qnil;
2666 }
2667 }
2668 free_window_matrices (r);
2669
2670 windows_or_buffers_changed++;
2671 Vwindow_list = Qnil;
2672 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
2673 resize_failed = 0;
2674
2675 if (NILP (w->buffer))
2676 {
2677 /* Resize child windows vertically. */
2678 XSETINT (delta, XINT (r->total_lines) - XINT (w->total_lines));
2679 w->top_line = r->top_line;
2680 resize_root_window (window, delta, Qnil, Qnil);
2681 if (window_resize_check (w, 0))
2682 window_resize_apply (w, 0);
2683 else
2684 {
2685 resize_root_window (window, delta, Qnil, Qt);
2686 if (window_resize_check (w, 0))
2687 window_resize_apply (w, 0);
2688 else
2689 resize_failed = 1;
2690 }
2691
2692 /* Resize child windows horizontally. */
2693 if (!resize_failed)
2694 {
2695 w->left_col = r->left_col;
2696 XSETINT (delta, XINT (r->total_cols) - XINT (w->total_cols));
2697 w->left_col = r->left_col;
2698 resize_root_window (window, delta, Qt, Qnil);
2699 if (window_resize_check (w, 1))
2700 window_resize_apply (w, 1);
2701 else
2702 {
2703 resize_root_window (window, delta, Qt, Qt);
2704 if (window_resize_check (w, 1))
2705 window_resize_apply (w, 1);
2706 else
2707 resize_failed = 1;
2708 }
2709 }
2710
2711 if (resize_failed)
2712 /* Play safe, if we still can ... */
2713 {
2714 window = swindow;
2715 w = XWINDOW (window);
2716 }
2717 }
2718
2719 /* Cleanly unlink WINDOW from window-tree. */
2720 if (!NILP (w->prev))
2721 /* Get SIBLING above (on the left of) WINDOW. */
2722 {
2723 sibling = w->prev;
2724 s = XWINDOW (sibling);
2725 s->next = w->next;
2726 if (!NILP (s->next))
2727 XWINDOW (s->next)->prev = sibling;
2728 }
2729 else
2730 /* Get SIBLING below (on the right of) WINDOW. */
2731 {
2732 sibling = w->next;
2733 s = XWINDOW (sibling);
2734 s->prev = Qnil;
2735 if (!NILP (XWINDOW (w->parent)->vchild))
2736 XWINDOW (w->parent)->vchild = sibling;
2737 else
2738 XWINDOW (w->parent)->hchild = sibling;
2739 }
2740
2741 /* Delete ROOT and all child windows of ROOT. */
2742 if (!NILP (r->vchild))
2743 {
2744 delete_all_child_windows (r->vchild);
2745 r->vchild = Qnil;
2746 }
2747 else if (!NILP (r->hchild))
2748 {
2749 delete_all_child_windows (r->hchild);
2750 r->hchild = Qnil;
2751 }
2752
2753 replace_window (root, window, 1);
2754
2755 /* This must become SWINDOW anyway ....... */
2756 if (!NILP (w->buffer) && !resize_failed)
2757 {
2758 /* Try to minimize scrolling, by setting the window start to the
2759 point will cause the text at the old window start to be at the
2760 same place on the frame. But don't try to do this if the
2761 window start is outside the visible portion (as might happen
2762 when the display is not current, due to typeahead). */
2763 new_top = WINDOW_TOP_EDGE_LINE (w) - FRAME_TOP_MARGIN (XFRAME (WINDOW_FRAME (w)));
2764 if (new_top != top
2765 && startpos >= BUF_BEGV (XBUFFER (w->buffer))
2766 && startpos <= BUF_ZV (XBUFFER (w->buffer)))
2767 {
2768 struct position pos;
2769 struct buffer *obuf = current_buffer;
2770
2771 Fset_buffer (w->buffer);
2772 /* This computation used to temporarily move point, but that
2773 can have unwanted side effects due to text properties. */
2774 pos = *vmotion (startpos, -top, w);
2775
2776 set_marker_both (w->start, w->buffer, pos.bufpos, pos.bytepos);
2777 w->window_end_valid = Qnil;
2778 w->start_at_line_beg = (pos.bytepos == BEGV_BYTE
2779 || FETCH_BYTE (pos.bytepos - 1) == '\n');
2780 /* We need to do this, so that the window-scroll-functions
2781 get called. */
2782 w->optional_new_start = 1;
2783
2784 set_buffer_internal (obuf);
2785 }
2786 }
2787
2788 adjust_glyphs (f);
2789 UNBLOCK_INPUT;
2790
2791 run_window_configuration_change_hook (f);
2792
2793 return Qnil;
2794 }
2795
2796
2797 void
2798 replace_buffer_in_windows (Lisp_Object buffer)
2799 {
2800 call1 (Qreplace_buffer_in_windows, buffer);
2801 }
2802
2803
2804 /* Safely replace BUFFER with some other buffer in all windows of all
2805 frames, even those on other keyboards. */
2806
2807 void
2808 replace_buffer_in_windows_safely (Lisp_Object buffer)
2809 {
2810 Lisp_Object tail, frame;
2811
2812 /* A single call to window_loop won't do the job because it only
2813 considers frames on the current keyboard. So loop manually over
2814 frames, and handle each one. */
2815 FOR_EACH_FRAME (tail, frame)
2816 window_loop (REPLACE_BUFFER_IN_WINDOWS_SAFELY, buffer, 1, frame);
2817 }
2818 \f
2819 /* If *ROWS or *COLS are too small a size for FRAME, set them to the
2820 minimum allowable size. */
2821
2822 void
2823 check_frame_size (FRAME_PTR frame, int *rows, int *cols)
2824 {
2825 /* For height, we have to see:
2826 how many windows the frame has at minimum (one or two),
2827 and whether it has a menu bar or other special stuff at the top. */
2828 int min_height
2829 = ((FRAME_MINIBUF_ONLY_P (frame) || ! FRAME_HAS_MINIBUF_P (frame))
2830 ? MIN_SAFE_WINDOW_HEIGHT
2831 : 2 * MIN_SAFE_WINDOW_HEIGHT);
2832
2833 if (FRAME_TOP_MARGIN (frame) > 0)
2834 min_height += FRAME_TOP_MARGIN (frame);
2835
2836 if (*rows < min_height)
2837 *rows = min_height;
2838 if (*cols < MIN_SAFE_WINDOW_WIDTH)
2839 *cols = MIN_SAFE_WINDOW_WIDTH;
2840 }
2841
2842 /* Adjust the margins of window W if text area is too small.
2843 Return 1 if window width is ok after adjustment; 0 if window
2844 is still too narrow. */
2845
2846 static int
2847 adjust_window_margins (struct window *w)
2848 {
2849 int box_cols = (WINDOW_TOTAL_COLS (w)
2850 - WINDOW_FRINGE_COLS (w)
2851 - WINDOW_SCROLL_BAR_COLS (w));
2852 int margin_cols = (WINDOW_LEFT_MARGIN_COLS (w)
2853 + WINDOW_RIGHT_MARGIN_COLS (w));
2854
2855 if (box_cols - margin_cols >= MIN_SAFE_WINDOW_WIDTH)
2856 return 1;
2857
2858 if (margin_cols < 0 || box_cols < MIN_SAFE_WINDOW_WIDTH)
2859 return 0;
2860
2861 /* Window's text area is too narrow, but reducing the window
2862 margins will fix that. */
2863 margin_cols = box_cols - MIN_SAFE_WINDOW_WIDTH;
2864 if (WINDOW_RIGHT_MARGIN_COLS (w) > 0)
2865 {
2866 if (WINDOW_LEFT_MARGIN_COLS (w) > 0)
2867 w->left_margin_cols = w->right_margin_cols
2868 = make_number (margin_cols/2);
2869 else
2870 w->right_margin_cols = make_number (margin_cols);
2871 }
2872 else
2873 w->left_margin_cols = make_number (margin_cols);
2874 return 1;
2875 }
2876 \f
2877 static Lisp_Object Fset_window_margins (Lisp_Object, Lisp_Object, Lisp_Object);
2878 static Lisp_Object Fset_window_fringes (Lisp_Object, Lisp_Object, Lisp_Object,
2879 Lisp_Object);
2880 static Lisp_Object Fset_window_scroll_bars (Lisp_Object, Lisp_Object,
2881 Lisp_Object, Lisp_Object);
2882 static Lisp_Object Fset_window_vscroll (Lisp_Object, Lisp_Object, Lisp_Object);
2883
2884 /* The following three routines are needed for running a window's
2885 configuration change hook. */
2886 static void
2887 run_funs (Lisp_Object funs)
2888 {
2889 for (; CONSP (funs); funs = XCDR (funs))
2890 if (!EQ (XCAR (funs), Qt))
2891 call0 (XCAR (funs));
2892 }
2893
2894 static Lisp_Object
2895 select_window_norecord (Lisp_Object window)
2896 {
2897 return WINDOW_LIVE_P (window)
2898 ? Fselect_window (window, Qt) : selected_window;
2899 }
2900
2901 static Lisp_Object
2902 select_frame_norecord (Lisp_Object frame)
2903 {
2904 return FRAME_LIVE_P (XFRAME (frame))
2905 ? Fselect_frame (frame, Qt) : selected_frame;
2906 }
2907
2908 void
2909 run_window_configuration_change_hook (struct frame *f)
2910 {
2911 ptrdiff_t count = SPECPDL_INDEX ();
2912 Lisp_Object frame, global_wcch
2913 = Fdefault_value (Qwindow_configuration_change_hook);
2914 XSETFRAME (frame, f);
2915
2916 if (NILP (Vrun_hooks) || !NILP (inhibit_lisp_code))
2917 return;
2918
2919 /* Use the right buffer. Matters when running the local hooks. */
2920 if (current_buffer != XBUFFER (Fwindow_buffer (Qnil)))
2921 {
2922 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
2923 Fset_buffer (Fwindow_buffer (Qnil));
2924 }
2925
2926 if (SELECTED_FRAME () != f)
2927 {
2928 record_unwind_protect (select_frame_norecord, Fselected_frame ());
2929 select_frame_norecord (frame);
2930 }
2931
2932 /* Look for buffer-local values. */
2933 {
2934 Lisp_Object windows = Fwindow_list (frame, Qlambda, Qnil);
2935 for (; CONSP (windows); windows = XCDR (windows))
2936 {
2937 Lisp_Object window = XCAR (windows);
2938 Lisp_Object buffer = Fwindow_buffer (window);
2939 if (!NILP (Flocal_variable_p (Qwindow_configuration_change_hook,
2940 buffer)))
2941 {
2942 ptrdiff_t inner_count = SPECPDL_INDEX ();
2943 record_unwind_protect (select_window_norecord, Fselected_window ());
2944 select_window_norecord (window);
2945 run_funs (Fbuffer_local_value (Qwindow_configuration_change_hook,
2946 buffer));
2947 unbind_to (inner_count, Qnil);
2948 }
2949 }
2950 }
2951
2952 run_funs (global_wcch);
2953 unbind_to (count, Qnil);
2954 }
2955
2956 DEFUN ("run-window-configuration-change-hook", Frun_window_configuration_change_hook,
2957 Srun_window_configuration_change_hook, 1, 1, 0,
2958 doc: /* Run `window-configuration-change-hook' for FRAME. */)
2959 (Lisp_Object frame)
2960 {
2961 CHECK_LIVE_FRAME (frame);
2962 run_window_configuration_change_hook (XFRAME (frame));
2963 return Qnil;
2964 }
2965
2966 /* Make WINDOW display BUFFER as its contents. RUN_HOOKS_P non-zero
2967 means it's allowed to run hooks. See make_frame for a case where
2968 it's not allowed. KEEP_MARGINS_P non-zero means that the current
2969 margins, fringes, and scroll-bar settings of the window are not
2970 reset from the buffer's local settings. */
2971
2972 void
2973 set_window_buffer (Lisp_Object window, Lisp_Object buffer, int run_hooks_p, int keep_margins_p)
2974 {
2975 struct window *w = XWINDOW (window);
2976 struct buffer *b = XBUFFER (buffer);
2977 ptrdiff_t count = SPECPDL_INDEX ();
2978 int samebuf = EQ (buffer, w->buffer);
2979
2980 w->buffer = buffer;
2981
2982 if (EQ (window, selected_window))
2983 BVAR (b, last_selected_window) = window;
2984
2985 /* Let redisplay errors through. */
2986 b->display_error_modiff = 0;
2987
2988 /* Update time stamps of buffer display. */
2989 if (INTEGERP (BVAR (b, display_count)))
2990 XSETINT (BVAR (b, display_count), XINT (BVAR (b, display_count)) + 1);
2991 BVAR (b, display_time) = Fcurrent_time ();
2992
2993 XSETFASTINT (w->window_end_pos, 0);
2994 XSETFASTINT (w->window_end_vpos, 0);
2995 memset (&w->last_cursor, 0, sizeof w->last_cursor);
2996 w->window_end_valid = Qnil;
2997 if (!(keep_margins_p && samebuf))
2998 { /* If we're not actually changing the buffer, don't reset hscroll and
2999 vscroll. This case happens for example when called from
3000 change_frame_size_1, where we use a dummy call to
3001 Fset_window_buffer on the frame's selected window (and no other)
3002 just in order to run window-configuration-change-hook.
3003 Resetting hscroll and vscroll here is problematic for things like
3004 image-mode and doc-view-mode since it resets the image's position
3005 whenever we resize the frame. */
3006 w->hscroll = w->min_hscroll = make_number (0);
3007 w->vscroll = 0;
3008 set_marker_both (w->pointm, buffer, BUF_PT (b), BUF_PT_BYTE (b));
3009 set_marker_restricted (w->start,
3010 make_number (b->last_window_start),
3011 buffer);
3012 w->start_at_line_beg = 0;
3013 w->force_start = 0;
3014 XSETFASTINT (w->last_modified, 0);
3015 XSETFASTINT (w->last_overlay_modified, 0);
3016 }
3017 /* Maybe we could move this into the `if' but it's not obviously safe and
3018 I doubt it's worth the trouble. */
3019 windows_or_buffers_changed++;
3020
3021 /* We must select BUFFER for running the window-scroll-functions. */
3022 /* We can't check ! NILP (Vwindow_scroll_functions) here
3023 because that might itself be a local variable. */
3024 if (window_initialized)
3025 {
3026 record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
3027 Fset_buffer (buffer);
3028 }
3029
3030 XMARKER (w->pointm)->insertion_type = !NILP (Vwindow_point_insertion_type);
3031
3032 if (!keep_margins_p)
3033 {
3034 /* Set left and right marginal area width etc. from buffer. */
3035
3036 /* This may call adjust_window_margins three times, so
3037 temporarily disable window margins. */
3038 Lisp_Object save_left = w->left_margin_cols;
3039 Lisp_Object save_right = w->right_margin_cols;
3040
3041 w->left_margin_cols = w->right_margin_cols = Qnil;
3042
3043 Fset_window_fringes (window,
3044 BVAR (b, left_fringe_width), BVAR (b, right_fringe_width),
3045 BVAR (b, fringes_outside_margins));
3046
3047 Fset_window_scroll_bars (window,
3048 BVAR (b, scroll_bar_width),
3049 BVAR (b, vertical_scroll_bar_type), Qnil);
3050
3051 w->left_margin_cols = save_left;
3052 w->right_margin_cols = save_right;
3053
3054 Fset_window_margins (window,
3055 BVAR (b, left_margin_cols), BVAR (b, right_margin_cols));
3056 }
3057
3058 if (run_hooks_p)
3059 {
3060 if (! NILP (Vwindow_scroll_functions))
3061 run_hook_with_args_2 (Qwindow_scroll_functions, window,
3062 Fmarker_position (w->start));
3063 run_window_configuration_change_hook (XFRAME (WINDOW_FRAME (w)));
3064 }
3065
3066 unbind_to (count, Qnil);
3067 }
3068
3069 DEFUN ("set-window-buffer", Fset_window_buffer, Sset_window_buffer, 2, 3, 0,
3070 doc: /* Make WINDOW display BUFFER-OR-NAME as its contents.
3071 WINDOW has to be a live window and defaults to the selected one.
3072 BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
3073
3074 Optional third argument KEEP-MARGINS non-nil means that WINDOW's current
3075 display margins, fringe widths, and scroll bar settings are preserved;
3076 the default is to reset these from the local settings for BUFFER-OR-NAME
3077 or the frame defaults. Return nil.
3078
3079 This function throws an error when WINDOW is strongly dedicated to its
3080 buffer (that is `window-dedicated-p' returns t for WINDOW) and does not
3081 already display BUFFER-OR-NAME.
3082
3083 This function runs `window-scroll-functions' before running
3084 `window-configuration-change-hook'. */)
3085 (register Lisp_Object window, Lisp_Object buffer_or_name, Lisp_Object keep_margins)
3086 {
3087 register Lisp_Object tem, buffer;
3088 register struct window *w = decode_window (window);
3089
3090 XSETWINDOW (window, w);
3091 buffer = Fget_buffer (buffer_or_name);
3092 CHECK_BUFFER (buffer);
3093 if (NILP (BVAR (XBUFFER (buffer), name)))
3094 error ("Attempt to display deleted buffer");
3095
3096 tem = w->buffer;
3097 if (NILP (tem))
3098 error ("Window is deleted");
3099 else if (!EQ (tem, Qt))
3100 /* w->buffer is t when the window is first being set up. */
3101 {
3102 if (!EQ (tem, buffer))
3103 {
3104 if (EQ (w->dedicated, Qt))
3105 /* WINDOW is strongly dedicated to its buffer, signal an
3106 error. */
3107 error ("Window is dedicated to `%s'", SDATA (BVAR (XBUFFER (tem), name)));
3108 else
3109 /* WINDOW is weakly dedicated to its buffer, reset
3110 dedication. */
3111 w->dedicated = Qnil;
3112
3113 call1 (Qrecord_window_buffer, window);
3114 }
3115
3116 unshow_buffer (w);
3117 }
3118
3119 set_window_buffer (window, buffer, 1, !NILP (keep_margins));
3120
3121 return Qnil;
3122 }
3123 \f
3124 static Lisp_Object
3125 display_buffer (Lisp_Object buffer, Lisp_Object not_this_window_p, Lisp_Object override_frame)
3126 {
3127 return call3 (Qdisplay_buffer, buffer, not_this_window_p, override_frame);
3128 }
3129
3130 DEFUN ("force-window-update", Fforce_window_update, Sforce_window_update,
3131 0, 1, 0,
3132 doc: /* Force all windows to be updated on next redisplay.
3133 If optional arg OBJECT is a window, force redisplay of that window only.
3134 If OBJECT is a buffer or buffer name, force redisplay of all windows
3135 displaying that buffer. */)
3136 (Lisp_Object object)
3137 {
3138 if (NILP (object))
3139 {
3140 windows_or_buffers_changed++;
3141 update_mode_lines++;
3142 return Qt;
3143 }
3144
3145 if (WINDOWP (object))
3146 {
3147 struct window *w = XWINDOW (object);
3148 mark_window_display_accurate (object, 0);
3149 w->update_mode_line = 1;
3150 if (BUFFERP (w->buffer))
3151 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
3152 ++update_mode_lines;
3153 return Qt;
3154 }
3155
3156 if (STRINGP (object))
3157 object = Fget_buffer (object);
3158 if (BUFFERP (object) && !NILP (BVAR (XBUFFER (object), name)))
3159 {
3160 /* Walk all windows looking for buffer, and force update
3161 of each of those windows. */
3162
3163 object = window_loop (REDISPLAY_BUFFER_WINDOWS, object, 0, Qvisible);
3164 return NILP (object) ? Qnil : Qt;
3165 }
3166
3167 /* If nothing suitable was found, just return.
3168 We could signal an error, but this feature will typically be used
3169 asynchronously in timers or process sentinels, so we don't. */
3170 return Qnil;
3171 }
3172
3173
3174 void
3175 temp_output_buffer_show (register Lisp_Object buf)
3176 {
3177 register struct buffer *old = current_buffer;
3178 register Lisp_Object window;
3179 register struct window *w;
3180
3181 BVAR (XBUFFER (buf), directory) = BVAR (current_buffer, directory);
3182
3183 Fset_buffer (buf);
3184 BUF_SAVE_MODIFF (XBUFFER (buf)) = MODIFF;
3185 BEGV = BEG;
3186 ZV = Z;
3187 SET_PT (BEG);
3188 set_buffer_internal (old);
3189
3190 if (!NILP (Vtemp_buffer_show_function))
3191 call1 (Vtemp_buffer_show_function, buf);
3192 else
3193 {
3194 window = display_buffer (buf, Qnil, Qnil);
3195
3196 if (!EQ (XWINDOW (window)->frame, selected_frame))
3197 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (window)));
3198 Vminibuf_scroll_window = window;
3199 w = XWINDOW (window);
3200 XSETFASTINT (w->hscroll, 0);
3201 XSETFASTINT (w->min_hscroll, 0);
3202 set_marker_restricted_both (w->start, buf, BEG, BEG);
3203 set_marker_restricted_both (w->pointm, buf, BEG, BEG);
3204
3205 /* Run temp-buffer-show-hook, with the chosen window selected
3206 and its buffer current. */
3207 {
3208 ptrdiff_t count = SPECPDL_INDEX ();
3209 Lisp_Object prev_window, prev_buffer;
3210 prev_window = selected_window;
3211 XSETBUFFER (prev_buffer, old);
3212
3213 /* Select the window that was chosen, for running the hook.
3214 Note: Both Fselect_window and select_window_norecord may
3215 set-buffer to the buffer displayed in the window,
3216 so we need to save the current buffer. --stef */
3217 record_unwind_protect (Fset_buffer, prev_buffer);
3218 record_unwind_protect (select_window_norecord, prev_window);
3219 Fselect_window (window, Qt);
3220 Fset_buffer (w->buffer);
3221 Frun_hooks (1, &Qtemp_buffer_show_hook);
3222 unbind_to (count, Qnil);
3223 }
3224 }
3225 }
3226
3227 DEFUN ("internal-temp-output-buffer-show",
3228 Ftemp_output_buffer_show, Stemp_output_buffer_show,
3229 1, 1, 0,
3230 doc: /* Internal function for `with-output-to-temp-buffer'. */)
3231 (Lisp_Object buf)
3232 {
3233 temp_output_buffer_show (buf);
3234 return Qnil;
3235 }
3236 \f
3237 /* Make new window, have it replace WINDOW in window-tree, and make
3238 WINDOW its only vertical child (HORFLAG 1 means make WINDOW its only
3239 horizontal child). */
3240 static void
3241 make_parent_window (Lisp_Object window, int horflag)
3242 {
3243 Lisp_Object parent;
3244 register struct window *o, *p;
3245
3246 o = XWINDOW (window);
3247 p = allocate_window ();
3248 memcpy ((char *) p + sizeof (struct vectorlike_header),
3249 (char *) o + sizeof (struct vectorlike_header),
3250 sizeof (Lisp_Object) * VECSIZE (struct window));
3251 XSETWINDOW (parent, p);
3252
3253 ++sequence_number;
3254 XSETFASTINT (p->sequence_number, sequence_number);
3255
3256 replace_window (window, parent, 1);
3257
3258 o->next = Qnil;
3259 o->prev = Qnil;
3260 o->parent = parent;
3261
3262 p->hchild = horflag ? window : Qnil;
3263 p->vchild = horflag ? Qnil : window;
3264 p->start = Qnil;
3265 p->pointm = Qnil;
3266 p->buffer = Qnil;
3267 p->combination_limit = Qnil;
3268 p->window_parameters = Qnil;
3269 }
3270
3271 /* Make new window from scratch. */
3272 Lisp_Object
3273 make_window (void)
3274 {
3275 Lisp_Object window;
3276 register struct window *w;
3277
3278 w = allocate_window ();
3279 /* Initialize Lisp data. Note that allocate_window initializes all
3280 Lisp data to nil, so do it only for slots which should not be nil. */
3281 XSETFASTINT (w->left_col, 0);
3282 XSETFASTINT (w->top_line, 0);
3283 XSETFASTINT (w->total_lines, 0);
3284 XSETFASTINT (w->total_cols, 0);
3285 w->normal_lines = make_float (1.0);
3286 w->normal_cols = make_float (1.0);
3287 XSETFASTINT (w->new_total, 0);
3288 XSETFASTINT (w->new_normal, 0);
3289 w->start = Fmake_marker ();
3290 w->pointm = Fmake_marker ();
3291 XSETFASTINT (w->hscroll, 0);
3292 XSETFASTINT (w->min_hscroll, 0);
3293 XSETFASTINT (w->use_time, 0);
3294 ++sequence_number;
3295 XSETFASTINT (w->sequence_number, sequence_number);
3296 XSETFASTINT (w->last_point, 0);
3297 w->vertical_scroll_bar_type = Qt;
3298 XSETFASTINT (w->window_end_pos, 0);
3299 XSETFASTINT (w->window_end_vpos, 0);
3300
3301 /* Initialize non-Lisp data. Note that allocate_window zeroes out all
3302 non-Lisp data, so do it only for slots which should not be zero. */
3303 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3304 w->phys_cursor_type = -1;
3305 w->phys_cursor_width = -1;
3306
3307 /* Reset window_list. */
3308 Vwindow_list = Qnil;
3309 /* Return window. */
3310 XSETWINDOW (window, w);
3311 return window;
3312 }
3313 \f
3314 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3315 doc: /* Set new total size of WINDOW to SIZE.
3316 Return SIZE.
3317
3318 Optional argument ADD non-nil means add SIZE to the new total size of
3319 WINDOW and return the sum.
3320
3321 Note: This function does not operate on any child windows of WINDOW. */)
3322 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3323 {
3324 struct window *w = decode_any_window (window);
3325
3326 CHECK_NUMBER (size);
3327 if (NILP (add))
3328 XSETINT (w->new_total, XINT (size));
3329 else
3330 XSETINT (w->new_total, XINT (w->new_total) + XINT (size));
3331
3332 return w->new_total;
3333 }
3334
3335 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3336 doc: /* Set new normal size of WINDOW to SIZE.
3337 Return SIZE.
3338
3339 Note: This function does not operate on any child windows of WINDOW. */)
3340 (Lisp_Object window, Lisp_Object size)
3341 {
3342 struct window *w = decode_any_window (window);
3343
3344 w->new_normal = size;
3345 return w->new_normal;
3346 }
3347
3348 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3349 non-zero) to w->new_total would result in correct heights (widths)
3350 for window W and recursively all child windows of W.
3351
3352 Note: This function does not check any of `window-fixed-size-p',
3353 `window-min-height' or `window-min-width'. It does check that window
3354 sizes do not drop below one line (two columns). */
3355 static int
3356 window_resize_check (struct window *w, int horflag)
3357 {
3358 struct window *c;
3359
3360 if (!NILP (w->vchild))
3361 /* W is a vertical combination. */
3362 {
3363 c = XWINDOW (w->vchild);
3364 if (horflag)
3365 /* All child windows of W must have the same width as W. */
3366 {
3367 while (c)
3368 {
3369 if ((XINT (c->new_total) != XINT (w->new_total))
3370 || !window_resize_check (c, horflag))
3371 return 0;
3372 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3373 }
3374 return 1;
3375 }
3376 else
3377 /* The sum of the heights of the child windows of W must equal
3378 W's height. */
3379 {
3380 int sum_of_sizes = 0;
3381 while (c)
3382 {
3383 if (!window_resize_check (c, horflag))
3384 return 0;
3385 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3386 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3387 }
3388 return (sum_of_sizes == XINT (w->new_total));
3389 }
3390 }
3391 else if (!NILP (w->hchild))
3392 /* W is a horizontal combination. */
3393 {
3394 c = XWINDOW (w->hchild);
3395 if (horflag)
3396 /* The sum of the widths of the child windows of W must equal W's
3397 width. */
3398 {
3399 int sum_of_sizes = 0;
3400 while (c)
3401 {
3402 if (!window_resize_check (c, horflag))
3403 return 0;
3404 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3405 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3406 }
3407 return (sum_of_sizes == XINT (w->new_total));
3408 }
3409 else
3410 /* All child windows of W must have the same height as W. */
3411 {
3412 while (c)
3413 {
3414 if ((XINT (c->new_total) != XINT (w->new_total))
3415 || !window_resize_check (c, horflag))
3416 return 0;
3417 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3418 }
3419 return 1;
3420 }
3421 }
3422 else
3423 /* A leaf window. Make sure it's not too small. The following
3424 hardcodes the values of `window-safe-min-width' (2) and
3425 `window-safe-min-height' (1) which are defined in window.el. */
3426 return XINT (w->new_total) >= (horflag ? 2 : 1);
3427 }
3428
3429 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3430 w->new_total for window W and recursively all child windows of W.
3431 Also calculate and assign the new vertical (horizontal) start
3432 positions of each of these windows.
3433
3434 This function does not perform any error checks. Make sure you have
3435 run window_resize_check on W before applying this function. */
3436 static void
3437 window_resize_apply (struct window *w, int horflag)
3438 {
3439 struct window *c;
3440 int pos;
3441
3442 /* Note: Assigning new_normal requires that the new total size of the
3443 parent window has been set *before*. */
3444 if (horflag)
3445 {
3446 w->total_cols = w->new_total;
3447 if (NUMBERP (w->new_normal))
3448 w->normal_cols = w->new_normal;
3449
3450 pos = XINT (w->left_col);
3451 }
3452 else
3453 {
3454 w->total_lines = w->new_total;
3455 if (NUMBERP (w->new_normal))
3456 w->normal_lines = w->new_normal;
3457
3458 pos = XINT (w->top_line);
3459 }
3460
3461 if (!NILP (w->vchild))
3462 /* W is a vertical combination. */
3463 {
3464 c = XWINDOW (w->vchild);
3465 while (c)
3466 {
3467 if (horflag)
3468 XSETFASTINT (c->left_col, pos);
3469 else
3470 XSETFASTINT (c->top_line, pos);
3471 window_resize_apply (c, horflag);
3472 if (!horflag)
3473 pos = pos + XINT (c->total_lines);
3474 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3475 }
3476 }
3477 else if (!NILP (w->hchild))
3478 /* W is a horizontal combination. */
3479 {
3480 c = XWINDOW (w->hchild);
3481 while (c)
3482 {
3483 if (horflag)
3484 XSETFASTINT (c->left_col, pos);
3485 else
3486 XSETFASTINT (c->top_line, pos);
3487 window_resize_apply (c, horflag);
3488 if (horflag)
3489 pos = pos + XINT (c->total_cols);
3490 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3491 }
3492 }
3493
3494 /* Clear out some redisplay caches. */
3495 XSETFASTINT (w->last_modified, 0);
3496 XSETFASTINT (w->last_overlay_modified, 0);
3497 }
3498
3499
3500 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 1, 2, 0,
3501 doc: /* Apply requested size values for window-tree of FRAME.
3502 Optional argument HORIZONTAL omitted or nil means apply requested height
3503 values. HORIZONTAL non-nil means apply requested width values.
3504
3505 This function checks whether the requested values sum up to a valid
3506 window layout, recursively assigns the new sizes of all child windows
3507 and calculates and assigns the new start positions of these windows.
3508
3509 Note: This function does not check any of `window-fixed-size-p',
3510 `window-min-height' or `window-min-width'. All these checks have to
3511 be applied on the Elisp level. */)
3512 (Lisp_Object frame, Lisp_Object horizontal)
3513 {
3514 struct frame *f;
3515 struct window *r;
3516 int horflag = !NILP (horizontal);
3517
3518 if (NILP (frame))
3519 frame = selected_frame;
3520 CHECK_LIVE_FRAME (frame);
3521
3522 f = XFRAME (frame);
3523 r = XWINDOW (FRAME_ROOT_WINDOW (f));
3524
3525 if (!window_resize_check (r, horflag)
3526 || ! EQ (r->new_total, (horflag ? r->total_cols : r->total_lines)))
3527 return Qnil;
3528
3529 BLOCK_INPUT;
3530 window_resize_apply (r, horflag);
3531
3532 windows_or_buffers_changed++;
3533 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3534
3535 adjust_glyphs (f);
3536 UNBLOCK_INPUT;
3537
3538 run_window_configuration_change_hook (f);
3539
3540 return Qt;
3541 }
3542
3543
3544 /* Resize frame F's windows when number of lines of F is set to SIZE.
3545 HORFLAG 1 means resize windows when number of columns of F is set to
3546 SIZE.
3547
3548 This function can delete all windows but the selected one in order to
3549 satisfy the request. The result will be meaningful if and only if
3550 F's windows have meaningful sizes when you call this. */
3551 void
3552 resize_frame_windows (struct frame *f, int size, int horflag)
3553 {
3554 Lisp_Object root = f->root_window;
3555 struct window *r = XWINDOW (root);
3556 Lisp_Object mini = f->minibuffer_window;
3557 struct window *m;
3558 /* new_size is the new size of the frame's root window. */
3559 int new_size = (horflag
3560 ? size
3561 : (size
3562 - FRAME_TOP_MARGIN (f)
3563 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3564 ? 1 : 0)));
3565
3566 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
3567 if (NILP (r->vchild) && NILP (r->hchild))
3568 /* For a leaf root window just set the size. */
3569 if (horflag)
3570 XSETFASTINT (r->total_cols, new_size);
3571 else
3572 XSETFASTINT (r->total_lines, new_size);
3573 else
3574 {
3575 /* old_size is the old size of the frame's root window. */
3576 int old_size = XFASTINT (horflag ? r->total_cols : r->total_lines);
3577 Lisp_Object delta;
3578
3579 XSETINT (delta, new_size - old_size);
3580 /* Try a "normal" resize first. */
3581 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3582 if (window_resize_check (r, horflag) && new_size == XINT (r->new_total))
3583 window_resize_apply (r, horflag);
3584 else
3585 {
3586 /* Try with "reasonable" minimum sizes next. */
3587 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3588 if (window_resize_check (r, horflag)
3589 && new_size == XINT (r->new_total))
3590 window_resize_apply (r, horflag);
3591 else
3592 {
3593 /* Finally, try with "safe" minimum sizes. */
3594 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3595 if (window_resize_check (r, horflag)
3596 && new_size == XINT (r->new_total))
3597 window_resize_apply (r, horflag);
3598 else
3599 {
3600 /* We lost. Delete all windows but the frame's
3601 selected one. */
3602 root = f->selected_window;
3603 Fdelete_other_windows_internal (root, Qnil);
3604 if (horflag)
3605 XSETFASTINT (XWINDOW (root)->total_cols, new_size);
3606 else
3607 XSETFASTINT (XWINDOW (root)->total_lines, new_size);
3608 }
3609 }
3610 }
3611 }
3612
3613 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3614 {
3615 m = XWINDOW (mini);
3616 if (horflag)
3617 XSETFASTINT (m->total_cols, size);
3618 else
3619 {
3620 /* Are we sure we always want 1 line here? */
3621 XSETFASTINT (m->total_lines, 1);
3622 XSETFASTINT (m->top_line, XINT (r->top_line) + XINT (r->total_lines));
3623 }
3624 }
3625 }
3626
3627
3628 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3629 doc: /* Split window OLD.
3630 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3631 new window. In any case TOTAL-SIZE must be a positive integer.
3632
3633 Third argument SIDE nil (or `below') specifies that the new window shall
3634 be located below WINDOW. SIDE `above' means the new window shall be
3635 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3636 lines of the new window including space reserved for the mode and/or
3637 header line.
3638
3639 SIDE t (or `right') specifies that the new window shall be located on
3640 the right side of WINDOW. SIDE `left' means the new window shall be
3641 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3642 number of columns of the new window including space reserved for fringes
3643 and the scrollbar or a divider column.
3644
3645 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3646 according to the SIDE argument.
3647
3648 The new total and normal sizes of all involved windows must have been
3649 set correctly. See the code of `split-window' for how this is done. */)
3650 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3651 {
3652 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3653 parent window or an internal window we have to install as OLD's new
3654 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3655 provided OLD is a leaf window, or to the frame's selected window.
3656 NEW (*n) is the new window created with some parameters taken from
3657 REFERENCE (*r). */
3658 register Lisp_Object new, frame, reference;
3659 register struct window *o, *p, *n, *r;
3660 struct frame *f;
3661 int horflag
3662 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3663 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3664 int combination_limit = 0;
3665
3666 CHECK_WINDOW (old);
3667 o = XWINDOW (old);
3668 frame = WINDOW_FRAME (o);
3669 f = XFRAME (frame);
3670
3671 CHECK_NUMBER (total_size);
3672
3673 /* Set combination_limit to 1 if we have to make a new parent window.
3674 We do that if either `window-combination-limit' is t, or OLD has no
3675 parent, or OLD is ortho-combined. */
3676 combination_limit =
3677 !NILP (Vwindow_combination_limit)
3678 || NILP (o->parent)
3679 || NILP (horflag
3680 ? (XWINDOW (o->parent)->hchild)
3681 : (XWINDOW (o->parent)->vchild));
3682
3683 /* We need a live reference window to initialize some parameters. */
3684 if (WINDOW_LIVE_P (old))
3685 /* OLD is live, use it as reference window. */
3686 reference = old;
3687 else
3688 /* Use the frame's selected window as reference window. */
3689 reference = FRAME_SELECTED_WINDOW (f);
3690 r = XWINDOW (reference);
3691
3692 /* The following bugs are caught by `split-window'. */
3693 if (MINI_WINDOW_P (o))
3694 error ("Attempt to split minibuffer window");
3695 else if (XINT (total_size) < (horflag ? 2 : 1))
3696 error ("Size of new window too small (after split)");
3697 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3698 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3699 proportionally. */
3700 {
3701 p = XWINDOW (o->parent);
3702 /* Temporarily pretend we split the parent window. */
3703 XSETINT (p->new_total,
3704 XINT (horflag ? p->total_cols : p->total_lines)
3705 - XINT (total_size));
3706 if (!window_resize_check (p, horflag))
3707 error ("Window sizes don't fit");
3708 else
3709 /* Undo the temporary pretension. */
3710 p->new_total = horflag ? p->total_cols : p->total_lines;
3711 }
3712 else
3713 {
3714 if (!window_resize_check (o, horflag))
3715 error ("Resizing old window failed");
3716 else if (XINT (total_size) + XINT (o->new_total)
3717 != XINT (horflag ? o->total_cols : o->total_lines))
3718 error ("Sum of sizes of old and new window don't fit");
3719 }
3720
3721 /* This is our point of no return. */
3722 if (combination_limit)
3723 {
3724 /* Save the old value of o->normal_cols/lines. It gets corrupted
3725 by make_parent_window and we need it below for assigning it to
3726 p->new_normal. */
3727 Lisp_Object new_normal = horflag ? o->normal_cols : o->normal_lines;
3728
3729 make_parent_window (old, horflag);
3730 p = XWINDOW (o->parent);
3731 /* Store value of `window-combination-limit' in new parent's
3732 combination_limit slot. */
3733 p->combination_limit = Vwindow_combination_limit;
3734 /* These get applied below. */
3735 p->new_total = horflag ? o->total_cols : o->total_lines;
3736 p->new_normal = new_normal;
3737 }
3738 else
3739 p = XWINDOW (o->parent);
3740
3741 windows_or_buffers_changed++;
3742 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3743 new = make_window ();
3744 n = XWINDOW (new);
3745 n->frame = frame;
3746 n->parent = o->parent;
3747 n->vchild = n->hchild = Qnil;
3748
3749 if (EQ (side, Qabove) || EQ (side, Qleft))
3750 {
3751 n->prev = o->prev;
3752 if (NILP (n->prev))
3753 if (horflag)
3754 p->hchild = new;
3755 else
3756 p->vchild = new;
3757 else
3758 XWINDOW (n->prev)->next = new;
3759 n->next = old;
3760 o->prev = new;
3761 }
3762 else
3763 {
3764 n->next = o->next;
3765 if (!NILP (n->next))
3766 XWINDOW (n->next)->prev = new;
3767 n->prev = old;
3768 o->next = new;
3769 }
3770
3771 n->buffer = Qt;
3772 n->window_end_valid = Qnil;
3773 memset (&n->last_cursor, 0, sizeof n->last_cursor);
3774
3775 /* Get special geometry settings from reference window. */
3776 n->left_margin_cols = r->left_margin_cols;
3777 n->right_margin_cols = r->right_margin_cols;
3778 n->left_fringe_width = r->left_fringe_width;
3779 n->right_fringe_width = r->right_fringe_width;
3780 n->fringes_outside_margins = r->fringes_outside_margins;
3781 n->scroll_bar_width = r->scroll_bar_width;
3782 n->vertical_scroll_bar_type = r->vertical_scroll_bar_type;
3783
3784 /* Directly assign orthogonal coordinates and sizes. */
3785 if (horflag)
3786 {
3787 n->top_line = o->top_line;
3788 n->total_lines = o->total_lines;
3789 }
3790 else
3791 {
3792 n->left_col = o->left_col;
3793 n->total_cols = o->total_cols;
3794 }
3795
3796 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3797 get them ready here. */
3798 n->new_total = total_size;
3799 n->new_normal = normal_size;
3800
3801 BLOCK_INPUT;
3802 window_resize_apply (p, horflag);
3803 adjust_glyphs (f);
3804 /* Set buffer of NEW to buffer of reference window. Don't run
3805 any hooks. */
3806 set_window_buffer (new, r->buffer, 0, 1);
3807 UNBLOCK_INPUT;
3808
3809 /* Maybe we should run the scroll functions in Elisp (which already
3810 runs the configuration change hook). */
3811 if (! NILP (Vwindow_scroll_functions))
3812 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3813 Fmarker_position (n->start));
3814 /* Return NEW. */
3815 return new;
3816 }
3817
3818
3819 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3820 doc: /* Remove WINDOW from its frame.
3821 WINDOW defaults to the selected window. Return nil.
3822 Signal an error when WINDOW is the only window on its frame. */)
3823 (register Lisp_Object window)
3824 {
3825 register Lisp_Object parent, sibling, frame, root;
3826 struct window *w, *p, *s, *r;
3827 struct frame *f;
3828 int horflag;
3829 int before_sibling = 0;
3830
3831 w = decode_any_window (window);
3832 XSETWINDOW (window, w);
3833 if (NILP (w->buffer) && NILP (w->hchild) && NILP (w->vchild))
3834 /* It's a no-op to delete an already deleted window. */
3835 return Qnil;
3836
3837 parent = w->parent;
3838 if (NILP (parent))
3839 /* Never delete a minibuffer or frame root window. */
3840 error ("Attempt to delete minibuffer or sole ordinary window");
3841 else if (NILP (w->prev) && NILP (w->next))
3842 /* Rather bow out here, this case should be handled on the Elisp
3843 level. */
3844 error ("Attempt to delete sole window of parent");
3845
3846 p = XWINDOW (parent);
3847 horflag = NILP (p->vchild);
3848
3849 frame = WINDOW_FRAME (w);
3850 f = XFRAME (frame);
3851
3852 root = FRAME_ROOT_WINDOW (f);
3853 r = XWINDOW (root);
3854
3855 /* Unlink WINDOW from window tree. */
3856 if (NILP (w->prev))
3857 /* Get SIBLING below (on the right of) WINDOW. */
3858 {
3859 /* before_sibling 1 means WINDOW is the first child of its
3860 parent and thus before the sibling. */
3861 before_sibling = 1;
3862 sibling = w->next;
3863 s = XWINDOW (sibling);
3864 s->prev = Qnil;
3865 if (horflag)
3866 p->hchild = sibling;
3867 else
3868 p->vchild = sibling;
3869 }
3870 else
3871 /* Get SIBLING above (on the left of) WINDOW. */
3872 {
3873 sibling = w->prev;
3874 s = XWINDOW (sibling);
3875 s->next = w->next;
3876 if (!NILP (s->next))
3877 XWINDOW (s->next)->prev = sibling;
3878 }
3879
3880 if (window_resize_check (r, horflag)
3881 && EQ (r->new_total, (horflag ? r->total_cols : r->total_lines)))
3882 /* We can delete WINDOW now. */
3883 {
3884
3885 /* Block input. */
3886 BLOCK_INPUT;
3887 window_resize_apply (p, horflag);
3888
3889 /* If this window is referred to by the dpyinfo's mouse
3890 highlight, invalidate that slot to be safe (Bug#9904). */
3891 if (!FRAME_INITIAL_P (f))
3892 {
3893 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3894
3895 if (EQ (hlinfo->mouse_face_window, window))
3896 hlinfo->mouse_face_window = Qnil;
3897 }
3898
3899 windows_or_buffers_changed++;
3900 Vwindow_list = Qnil;
3901 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3902
3903 w->next = Qnil; /* Don't delete w->next too. */
3904 free_window_matrices (w);
3905
3906 if (!NILP (w->vchild))
3907 {
3908 delete_all_child_windows (w->vchild);
3909 w->vchild = Qnil;
3910 }
3911 else if (!NILP (w->hchild))
3912 {
3913 delete_all_child_windows (w->hchild);
3914 w->hchild = Qnil;
3915 }
3916 else if (!NILP (w->buffer))
3917 {
3918 unshow_buffer (w);
3919 unchain_marker (XMARKER (w->pointm));
3920 unchain_marker (XMARKER (w->start));
3921 w->buffer = Qnil;
3922 }
3923
3924 if (NILP (s->prev) && NILP (s->next))
3925 /* A matrjoshka where SIBLING has become the only child of
3926 PARENT. */
3927 {
3928 /* Put SIBLING into PARENT's place. */
3929 replace_window (parent, sibling, 0);
3930 /* Have SIBLING inherit the following three slot values from
3931 PARENT (the combination_limit slot is not inherited). */
3932 s->normal_cols = p->normal_cols;
3933 s->normal_lines = p->normal_lines;
3934 /* Mark PARENT as deleted. */
3935 p->vchild = p->hchild = Qnil;
3936 /* Try to merge SIBLING into its new parent. */
3937 recombine_windows (sibling);
3938 }
3939
3940 adjust_glyphs (f);
3941
3942 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
3943 /* We deleted the frame's selected window. */
3944 {
3945 /* Use the frame's first window as fallback ... */
3946 Lisp_Object new_selected_window = Fframe_first_window (frame);
3947 /* ... but preferably use its most recently used window. */
3948 Lisp_Object mru_window;
3949
3950 /* `get-mru-window' might fail for some reason so play it safe
3951 - promote the first window _without recording it_ first. */
3952 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
3953 Fselect_window (new_selected_window, Qt);
3954 else
3955 FRAME_SELECTED_WINDOW (f) = new_selected_window;
3956
3957 UNBLOCK_INPUT;
3958
3959 /* Now look whether `get-mru-window' gets us something. */
3960 mru_window = call1 (Qget_mru_window, frame);
3961 if (WINDOW_LIVE_P (mru_window)
3962 && EQ (XWINDOW (mru_window)->frame, frame))
3963 new_selected_window = mru_window;
3964
3965 /* If all ended up well, we now promote the mru window. */
3966 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
3967 Fselect_window (new_selected_window, Qnil);
3968 else
3969 FRAME_SELECTED_WINDOW (f) = new_selected_window;
3970 }
3971 else
3972 UNBLOCK_INPUT;
3973
3974 /* Must be run by the caller:
3975 run_window_configuration_change_hook (f); */
3976 }
3977 else
3978 /* We failed: Relink WINDOW into window tree. */
3979 {
3980 if (before_sibling)
3981 {
3982 s->prev = window;
3983 if (horflag)
3984 p->hchild = window;
3985 else
3986 p->vchild = window;
3987 }
3988 else
3989 {
3990 s->next = window;
3991 if (!NILP (w->next))
3992 XWINDOW (w->next)->prev = window;
3993 }
3994 error ("Deletion failed");
3995 }
3996
3997 return Qnil;
3998 }
3999 \f
4000 /***********************************************************************
4001 Resizing Mini-Windows
4002 ***********************************************************************/
4003
4004 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4005 can. */
4006 void
4007 grow_mini_window (struct window *w, int delta)
4008 {
4009 struct frame *f = XFRAME (w->frame);
4010 struct window *r;
4011 Lisp_Object root, value;
4012
4013 eassert (MINI_WINDOW_P (w));
4014 eassert (delta >= 0);
4015
4016 root = FRAME_ROOT_WINDOW (f);
4017 r = XWINDOW (root);
4018 value = call2 (Qwindow_resize_root_window_vertically,
4019 root, make_number (- delta));
4020 if (INTEGERP (value) && window_resize_check (r, 0))
4021 {
4022 BLOCK_INPUT;
4023 window_resize_apply (r, 0);
4024
4025 /* Grow the mini-window. */
4026 XSETFASTINT (w->top_line, XFASTINT (r->top_line) + XFASTINT (r->total_lines));
4027 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - XINT (value));
4028 XSETFASTINT (w->last_modified, 0);
4029 XSETFASTINT (w->last_overlay_modified, 0);
4030
4031 adjust_glyphs (f);
4032 UNBLOCK_INPUT;
4033 }
4034 }
4035
4036
4037 /* Shrink mini-window W. */
4038 void
4039 shrink_mini_window (struct window *w)
4040 {
4041 struct frame *f = XFRAME (w->frame);
4042 struct window *r;
4043 Lisp_Object root, value;
4044 EMACS_INT size;
4045
4046 eassert (MINI_WINDOW_P (w));
4047
4048 size = XINT (w->total_lines);
4049 if (size > 1)
4050 {
4051 root = FRAME_ROOT_WINDOW (f);
4052 r = XWINDOW (root);
4053 value = call2 (Qwindow_resize_root_window_vertically,
4054 root, make_number (size - 1));
4055 if (INTEGERP (value) && window_resize_check (r, 0))
4056 {
4057 BLOCK_INPUT;
4058 window_resize_apply (r, 0);
4059
4060 /* Shrink the mini-window. */
4061 XSETFASTINT (w->top_line, XFASTINT (r->top_line) + XFASTINT (r->total_lines));
4062 XSETFASTINT (w->total_lines, 1);
4063
4064 XSETFASTINT (w->last_modified, 0);
4065 XSETFASTINT (w->last_overlay_modified, 0);
4066
4067 adjust_glyphs (f);
4068 UNBLOCK_INPUT;
4069 }
4070 /* If the above failed for whatever strange reason we must make a
4071 one window frame here. The same routine will be needed when
4072 shrinking the frame (and probably when making the initial
4073 *scratch* window). For the moment leave things as they are. */
4074 }
4075 }
4076
4077 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4078 doc: /* Resize minibuffer window WINDOW. */)
4079 (Lisp_Object window)
4080 {
4081 struct window *w = XWINDOW (window);
4082 struct window *r;
4083 struct frame *f;
4084 int height;
4085
4086 CHECK_WINDOW (window);
4087 f = XFRAME (w->frame);
4088
4089 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4090 error ("Not a valid minibuffer window");
4091 else if (FRAME_MINIBUF_ONLY_P (f))
4092 error ("Cannot resize a minibuffer-only frame");
4093
4094 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4095 height = XINT (r->total_lines) + XINT (w->total_lines);
4096 if (window_resize_check (r, 0)
4097 && XINT (w->new_total) > 0
4098 && height == XINT (r->new_total) + XINT (w->new_total))
4099 {
4100 BLOCK_INPUT;
4101 window_resize_apply (r, 0);
4102
4103 w->total_lines = w->new_total;
4104 XSETFASTINT (w->top_line, XINT (r->top_line) + XINT (r->total_lines));
4105
4106 windows_or_buffers_changed++;
4107 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4108 adjust_glyphs (f);
4109 UNBLOCK_INPUT;
4110
4111 run_window_configuration_change_hook (f);
4112 return Qt;
4113 }
4114 else error ("Failed to resize minibuffer window");
4115 }
4116 \f
4117 /* Mark window cursors off for all windows in the window tree rooted
4118 at W by setting their phys_cursor_on_p flag to zero. Called from
4119 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4120 the frame are cleared. */
4121
4122 void
4123 mark_window_cursors_off (struct window *w)
4124 {
4125 while (w)
4126 {
4127 if (!NILP (w->hchild))
4128 mark_window_cursors_off (XWINDOW (w->hchild));
4129 else if (!NILP (w->vchild))
4130 mark_window_cursors_off (XWINDOW (w->vchild));
4131 else
4132 w->phys_cursor_on_p = 0;
4133
4134 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4135 }
4136 }
4137
4138
4139 /* Return number of lines of text (not counting mode lines) in W. */
4140
4141 int
4142 window_internal_height (struct window *w)
4143 {
4144 int ht = XFASTINT (w->total_lines);
4145
4146 if (!MINI_WINDOW_P (w))
4147 {
4148 if (!NILP (w->parent)
4149 || !NILP (w->vchild)
4150 || !NILP (w->hchild)
4151 || !NILP (w->next)
4152 || !NILP (w->prev)
4153 || WINDOW_WANTS_MODELINE_P (w))
4154 --ht;
4155
4156 if (WINDOW_WANTS_HEADER_LINE_P (w))
4157 --ht;
4158 }
4159
4160 return ht;
4161 }
4162 \f
4163 /************************************************************************
4164 Window Scrolling
4165 ***********************************************************************/
4166
4167 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4168 N screen-fulls, which is defined as the height of the window minus
4169 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4170 instead. Negative values of N mean scroll down. NOERROR non-zero
4171 means don't signal an error if we try to move over BEGV or ZV,
4172 respectively. */
4173
4174 static void
4175 window_scroll (Lisp_Object window, EMACS_INT n, int whole, int noerror)
4176 {
4177 immediate_quit = 1;
4178 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4179
4180 /* If we must, use the pixel-based version which is much slower than
4181 the line-based one but can handle varying line heights. */
4182 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4183 window_scroll_pixel_based (window, n, whole, noerror);
4184 else
4185 window_scroll_line_based (window, n, whole, noerror);
4186
4187 immediate_quit = 0;
4188 }
4189
4190
4191 /* Implementation of window_scroll that works based on pixel line
4192 heights. See the comment of window_scroll for parameter
4193 descriptions. */
4194
4195 static void
4196 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4197 {
4198 struct it it;
4199 struct window *w = XWINDOW (window);
4200 struct text_pos start;
4201 int this_scroll_margin;
4202 /* True if we fiddled the window vscroll field without really scrolling. */
4203 int vscrolled = 0;
4204 int x, y, rtop, rbot, rowh, vpos;
4205 void *itdata = NULL;
4206
4207 SET_TEXT_POS_FROM_MARKER (start, w->start);
4208 /* Scrolling a minibuffer window via scroll bar when the echo area
4209 shows long text sometimes resets the minibuffer contents behind
4210 our backs. */
4211 if (CHARPOS (start) > ZV)
4212 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4213
4214 /* If PT is not visible in WINDOW, move back one half of
4215 the screen. Allow PT to be partially visible, otherwise
4216 something like (scroll-down 1) with PT in the line before
4217 the partially visible one would recenter. */
4218
4219 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4220 {
4221 itdata = bidi_shelve_cache ();
4222 /* Move backward half the height of the window. Performance note:
4223 vmotion used here is about 10% faster, but would give wrong
4224 results for variable height lines. */
4225 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4226 it.current_y = it.last_visible_y;
4227 move_it_vertically_backward (&it, window_box_height (w) / 2);
4228
4229 /* The function move_iterator_vertically may move over more than
4230 the specified y-distance. If it->w is small, e.g. a
4231 mini-buffer window, we may end up in front of the window's
4232 display area. This is the case when Start displaying at the
4233 start of the line containing PT in this case. */
4234 if (it.current_y <= 0)
4235 {
4236 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4237 move_it_vertically_backward (&it, 0);
4238 it.current_y = 0;
4239 }
4240
4241 start = it.current.pos;
4242 bidi_unshelve_cache (itdata, 0);
4243 }
4244 else if (auto_window_vscroll_p)
4245 {
4246 if (rtop || rbot) /* partially visible */
4247 {
4248 int px;
4249 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4250 if (whole)
4251 dy = max ((window_box_height (w)
4252 - next_screen_context_lines * dy),
4253 dy);
4254 dy *= n;
4255
4256 if (n < 0)
4257 {
4258 /* Only vscroll backwards if already vscrolled forwards. */
4259 if (w->vscroll < 0 && rtop > 0)
4260 {
4261 px = max (0, -w->vscroll - min (rtop, -dy));
4262 Fset_window_vscroll (window, make_number (px), Qt);
4263 return;
4264 }
4265 }
4266 if (n > 0)
4267 {
4268 /* Do vscroll if already vscrolled or only display line. */
4269 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4270 {
4271 px = max (0, -w->vscroll + min (rbot, dy));
4272 Fset_window_vscroll (window, make_number (px), Qt);
4273 return;
4274 }
4275
4276 /* Maybe modify window start instead of scrolling. */
4277 if (rbot > 0 || w->vscroll < 0)
4278 {
4279 ptrdiff_t spos;
4280
4281 Fset_window_vscroll (window, make_number (0), Qt);
4282 /* If there are other text lines above the current row,
4283 move window start to current row. Else to next row. */
4284 if (rbot > 0)
4285 spos = XINT (Fline_beginning_position (Qnil));
4286 else
4287 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4288 set_marker_restricted (w->start, make_number (spos),
4289 w->buffer);
4290 w->start_at_line_beg = 1;
4291 w->update_mode_line = 1;
4292 XSETFASTINT (w->last_modified, 0);
4293 XSETFASTINT (w->last_overlay_modified, 0);
4294 /* Set force_start so that redisplay_window will run the
4295 window-scroll-functions. */
4296 w->force_start = 1;
4297 return;
4298 }
4299 }
4300 }
4301 /* Cancel previous vscroll. */
4302 Fset_window_vscroll (window, make_number (0), Qt);
4303 }
4304
4305 itdata = bidi_shelve_cache ();
4306 /* If scroll_preserve_screen_position is non-nil, we try to set
4307 point in the same window line as it is now, so get that line. */
4308 if (!NILP (Vscroll_preserve_screen_position))
4309 {
4310 /* We preserve the goal pixel coordinate across consecutive
4311 calls to scroll-up, scroll-down and other commands that
4312 have the `scroll-command' property. This avoids the
4313 possibility of point becoming "stuck" on a tall line when
4314 scrolling by one line. */
4315 if (window_scroll_pixel_based_preserve_y < 0
4316 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4317 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4318 {
4319 start_display (&it, w, start);
4320 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4321 window_scroll_pixel_based_preserve_y = it.current_y;
4322 window_scroll_pixel_based_preserve_x = it.current_x;
4323 }
4324 }
4325 else
4326 window_scroll_pixel_based_preserve_y
4327 = window_scroll_pixel_based_preserve_x = -1;
4328
4329 /* Move iterator it from start the specified distance forward or
4330 backward. The result is the new window start. */
4331 start_display (&it, w, start);
4332 if (whole)
4333 {
4334 ptrdiff_t start_pos = IT_CHARPOS (it);
4335 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4336 dy = max ((window_box_height (w)
4337 - next_screen_context_lines * dy),
4338 dy) * n;
4339
4340 /* Note that move_it_vertically always moves the iterator to the
4341 start of a line. So, if the last line doesn't have a newline,
4342 we would end up at the start of the line ending at ZV. */
4343 if (dy <= 0)
4344 {
4345 move_it_vertically_backward (&it, -dy);
4346 /* Ensure we actually do move, e.g. in case we are currently
4347 looking at an image that is taller that the window height. */
4348 while (start_pos == IT_CHARPOS (it)
4349 && start_pos > BEGV)
4350 move_it_by_lines (&it, -1);
4351 }
4352 else if (dy > 0)
4353 {
4354 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4355 MOVE_TO_POS | MOVE_TO_Y);
4356 /* Ensure we actually do move, e.g. in case we are currently
4357 looking at an image that is taller that the window height. */
4358 while (start_pos == IT_CHARPOS (it)
4359 && start_pos < ZV)
4360 move_it_by_lines (&it, 1);
4361 }
4362 }
4363 else
4364 move_it_by_lines (&it, n);
4365
4366 /* We failed if we find ZV is already on the screen (scrolling up,
4367 means there's nothing past the end), or if we can't start any
4368 earlier (scrolling down, means there's nothing past the top). */
4369 if ((n > 0 && IT_CHARPOS (it) == ZV)
4370 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4371 {
4372 if (IT_CHARPOS (it) == ZV)
4373 {
4374 if (it.current_y < it.last_visible_y
4375 && (it.current_y + it.max_ascent + it.max_descent
4376 > it.last_visible_y))
4377 {
4378 /* The last line was only partially visible, make it fully
4379 visible. */
4380 w->vscroll = (it.last_visible_y
4381 - it.current_y + it.max_ascent + it.max_descent);
4382 adjust_glyphs (it.f);
4383 }
4384 else
4385 {
4386 bidi_unshelve_cache (itdata, 0);
4387 if (noerror)
4388 return;
4389 else if (n < 0) /* could happen with empty buffers */
4390 xsignal0 (Qbeginning_of_buffer);
4391 else
4392 xsignal0 (Qend_of_buffer);
4393 }
4394 }
4395 else
4396 {
4397 if (w->vscroll != 0)
4398 /* The first line was only partially visible, make it fully
4399 visible. */
4400 w->vscroll = 0;
4401 else
4402 {
4403 bidi_unshelve_cache (itdata, 0);
4404 if (noerror)
4405 return;
4406 else
4407 xsignal0 (Qbeginning_of_buffer);
4408 }
4409 }
4410
4411 /* If control gets here, then we vscrolled. */
4412
4413 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4414
4415 /* Don't try to change the window start below. */
4416 vscrolled = 1;
4417 }
4418
4419 if (! vscrolled)
4420 {
4421 ptrdiff_t pos = IT_CHARPOS (it);
4422 ptrdiff_t bytepos;
4423
4424 /* If in the middle of a multi-glyph character move forward to
4425 the next character. */
4426 if (in_display_vector_p (&it))
4427 {
4428 ++pos;
4429 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4430 }
4431
4432 /* Set the window start, and set up the window for redisplay. */
4433 set_marker_restricted (w->start, make_number (pos),
4434 w->buffer);
4435 bytepos = XMARKER (w->start)->bytepos;
4436 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4437 w->update_mode_line = 1;
4438 XSETFASTINT (w->last_modified, 0);
4439 XSETFASTINT (w->last_overlay_modified, 0);
4440 /* Set force_start so that redisplay_window will run the
4441 window-scroll-functions. */
4442 w->force_start = 1;
4443 }
4444
4445 /* The rest of this function uses current_y in a nonstandard way,
4446 not including the height of the header line if any. */
4447 it.current_y = it.vpos = 0;
4448
4449 /* Move PT out of scroll margins.
4450 This code wants current_y to be zero at the window start position
4451 even if there is a header line. */
4452 this_scroll_margin = max (0, scroll_margin);
4453 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4454 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4455
4456 if (n > 0)
4457 {
4458 /* We moved the window start towards ZV, so PT may be now
4459 in the scroll margin at the top. */
4460 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4461 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4462 && (NILP (Vscroll_preserve_screen_position)
4463 || EQ (Vscroll_preserve_screen_position, Qt)))
4464 /* We found PT at a legitimate height. Leave it alone. */
4465 ;
4466 else if (window_scroll_pixel_based_preserve_y >= 0)
4467 {
4468 /* If we have a header line, take account of it.
4469 This is necessary because we set it.current_y to 0, above. */
4470 move_it_to (&it, -1,
4471 window_scroll_pixel_based_preserve_x,
4472 window_scroll_pixel_based_preserve_y
4473 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4474 -1, MOVE_TO_Y | MOVE_TO_X);
4475 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4476 }
4477 else
4478 {
4479 while (it.current_y < this_scroll_margin)
4480 {
4481 int prev = it.current_y;
4482 move_it_by_lines (&it, 1);
4483 if (prev == it.current_y)
4484 break;
4485 }
4486 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4487 }
4488 }
4489 else if (n < 0)
4490 {
4491 ptrdiff_t charpos, bytepos;
4492 int partial_p;
4493
4494 /* Save our position, for the
4495 window_scroll_pixel_based_preserve_y case. */
4496 charpos = IT_CHARPOS (it);
4497 bytepos = IT_BYTEPOS (it);
4498
4499 /* We moved the window start towards BEGV, so PT may be now
4500 in the scroll margin at the bottom. */
4501 move_it_to (&it, PT, -1,
4502 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4503 - this_scroll_margin - 1),
4504 -1,
4505 MOVE_TO_POS | MOVE_TO_Y);
4506
4507 /* Save our position, in case it's correct. */
4508 charpos = IT_CHARPOS (it);
4509 bytepos = IT_BYTEPOS (it);
4510
4511 /* See if point is on a partially visible line at the end. */
4512 if (it.what == IT_EOB)
4513 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4514 else
4515 {
4516 move_it_by_lines (&it, 1);
4517 partial_p = it.current_y > it.last_visible_y;
4518 }
4519
4520 if (charpos == PT && !partial_p
4521 && (NILP (Vscroll_preserve_screen_position)
4522 || EQ (Vscroll_preserve_screen_position, Qt)))
4523 /* We found PT before we found the display margin, so PT is ok. */
4524 ;
4525 else if (window_scroll_pixel_based_preserve_y >= 0)
4526 {
4527 SET_TEXT_POS_FROM_MARKER (start, w->start);
4528 start_display (&it, w, start);
4529 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4530 here because we called start_display again and did not
4531 alter it.current_y this time. */
4532 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4533 window_scroll_pixel_based_preserve_y, -1,
4534 MOVE_TO_Y | MOVE_TO_X);
4535 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4536 }
4537 else
4538 {
4539 if (partial_p)
4540 /* The last line was only partially visible, so back up two
4541 lines to make sure we're on a fully visible line. */
4542 {
4543 move_it_by_lines (&it, -2);
4544 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4545 }
4546 else
4547 /* No, the position we saved is OK, so use it. */
4548 SET_PT_BOTH (charpos, bytepos);
4549 }
4550 }
4551 bidi_unshelve_cache (itdata, 0);
4552 }
4553
4554
4555 /* Implementation of window_scroll that works based on screen lines.
4556 See the comment of window_scroll for parameter descriptions. */
4557
4558 static void
4559 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
4560 {
4561 register struct window *w = XWINDOW (window);
4562 /* Fvertical_motion enters redisplay, which can trigger
4563 fontification, which in turn can modify buffer text (e.g., if the
4564 fontification functions replace escape sequences with faces, as
4565 in `grep-mode-font-lock-keywords'). So we use a marker to record
4566 the old point position, to prevent crashes in SET_PT_BOTH. */
4567 Lisp_Object opoint_marker = Fpoint_marker ();
4568 register ptrdiff_t pos, pos_byte;
4569 register int ht = window_internal_height (w);
4570 register Lisp_Object tem;
4571 int lose;
4572 Lisp_Object bolp;
4573 ptrdiff_t startpos;
4574 Lisp_Object original_pos = Qnil;
4575
4576 /* If scrolling screen-fulls, compute the number of lines to
4577 scroll from the window's height. */
4578 if (whole)
4579 n *= max (1, ht - next_screen_context_lines);
4580
4581 startpos = marker_position (w->start);
4582
4583 if (!NILP (Vscroll_preserve_screen_position))
4584 {
4585 if (window_scroll_preserve_vpos <= 0
4586 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4587 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4588 {
4589 struct position posit
4590 = *compute_motion (startpos, 0, 0, 0,
4591 PT, ht, 0,
4592 -1, XINT (w->hscroll),
4593 0, w);
4594 window_scroll_preserve_vpos = posit.vpos;
4595 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
4596 }
4597
4598 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4599 make_number (window_scroll_preserve_vpos));
4600 }
4601
4602 XSETFASTINT (tem, PT);
4603 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4604
4605 if (NILP (tem))
4606 {
4607 Fvertical_motion (make_number (- (ht / 2)), window);
4608 startpos = PT;
4609 }
4610
4611 SET_PT (startpos);
4612 lose = n < 0 && PT == BEGV;
4613 Fvertical_motion (make_number (n), window);
4614 pos = PT;
4615 pos_byte = PT_BYTE;
4616 bolp = Fbolp ();
4617 SET_PT_BOTH (marker_position (opoint_marker),
4618 marker_byte_position (opoint_marker));
4619
4620 if (lose)
4621 {
4622 if (noerror)
4623 return;
4624 else
4625 xsignal0 (Qbeginning_of_buffer);
4626 }
4627
4628 if (pos < ZV)
4629 {
4630 /* Don't use a scroll margin that is negative or too large. */
4631 int this_scroll_margin =
4632 max (0, min (scroll_margin, XINT (w->total_lines) / 4));
4633
4634 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4635 w->start_at_line_beg = !NILP (bolp);
4636 w->update_mode_line = 1;
4637 XSETFASTINT (w->last_modified, 0);
4638 XSETFASTINT (w->last_overlay_modified, 0);
4639 /* Set force_start so that redisplay_window will run
4640 the window-scroll-functions. */
4641 w->force_start = 1;
4642
4643 if (!NILP (Vscroll_preserve_screen_position)
4644 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4645 {
4646 SET_PT_BOTH (pos, pos_byte);
4647 Fvertical_motion (original_pos, window);
4648 }
4649 /* If we scrolled forward, put point enough lines down
4650 that it is outside the scroll margin. */
4651 else if (n > 0)
4652 {
4653 int top_margin;
4654
4655 if (this_scroll_margin > 0)
4656 {
4657 SET_PT_BOTH (pos, pos_byte);
4658 Fvertical_motion (make_number (this_scroll_margin), window);
4659 top_margin = PT;
4660 }
4661 else
4662 top_margin = pos;
4663
4664 if (top_margin <= marker_position (opoint_marker))
4665 SET_PT_BOTH (marker_position (opoint_marker),
4666 marker_byte_position (opoint_marker));
4667 else if (!NILP (Vscroll_preserve_screen_position))
4668 {
4669 SET_PT_BOTH (pos, pos_byte);
4670 Fvertical_motion (original_pos, window);
4671 }
4672 else
4673 SET_PT (top_margin);
4674 }
4675 else if (n < 0)
4676 {
4677 int bottom_margin;
4678
4679 /* If we scrolled backward, put point near the end of the window
4680 but not within the scroll margin. */
4681 SET_PT_BOTH (pos, pos_byte);
4682 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4683 if (XFASTINT (tem) == ht - this_scroll_margin)
4684 bottom_margin = PT;
4685 else
4686 bottom_margin = PT + 1;
4687
4688 if (bottom_margin > marker_position (opoint_marker))
4689 SET_PT_BOTH (marker_position (opoint_marker),
4690 marker_byte_position (opoint_marker));
4691 else
4692 {
4693 if (!NILP (Vscroll_preserve_screen_position))
4694 {
4695 SET_PT_BOTH (pos, pos_byte);
4696 Fvertical_motion (original_pos, window);
4697 }
4698 else
4699 Fvertical_motion (make_number (-1), window);
4700 }
4701 }
4702 }
4703 else
4704 {
4705 if (noerror)
4706 return;
4707 else
4708 xsignal0 (Qend_of_buffer);
4709 }
4710 }
4711
4712
4713 /* Scroll selected_window up or down. If N is nil, scroll a
4714 screen-full which is defined as the height of the window minus
4715 next_screen_context_lines. If N is the symbol `-', scroll.
4716 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4717 up. This is the guts of Fscroll_up and Fscroll_down. */
4718
4719 static void
4720 scroll_command (Lisp_Object n, int direction)
4721 {
4722 ptrdiff_t count = SPECPDL_INDEX ();
4723
4724 eassert (eabs (direction) == 1);
4725
4726 /* If selected window's buffer isn't current, make it current for
4727 the moment. But don't screw up if window_scroll gets an error. */
4728 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4729 {
4730 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4731 Fset_buffer (XWINDOW (selected_window)->buffer);
4732
4733 /* Make redisplay consider other windows than just selected_window. */
4734 ++windows_or_buffers_changed;
4735 }
4736
4737 if (NILP (n))
4738 window_scroll (selected_window, direction, 1, 0);
4739 else if (EQ (n, Qminus))
4740 window_scroll (selected_window, -direction, 1, 0);
4741 else
4742 {
4743 n = Fprefix_numeric_value (n);
4744 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4745 }
4746
4747 unbind_to (count, Qnil);
4748 }
4749
4750 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4751 doc: /* Scroll text of selected window upward ARG lines.
4752 If ARG is omitted or nil, scroll upward by a near full screen.
4753 A near full screen is `next-screen-context-lines' less than a full screen.
4754 Negative ARG means scroll downward.
4755 If ARG is the atom `-', scroll downward by nearly full screen.
4756 When calling from a program, supply as argument a number, nil, or `-'. */)
4757 (Lisp_Object arg)
4758 {
4759 scroll_command (arg, 1);
4760 return Qnil;
4761 }
4762
4763 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4764 doc: /* Scroll text of selected window down ARG lines.
4765 If ARG is omitted or nil, scroll down by a near full screen.
4766 A near full screen is `next-screen-context-lines' less than a full screen.
4767 Negative ARG means scroll upward.
4768 If ARG is the atom `-', scroll upward by nearly full screen.
4769 When calling from a program, supply as argument a number, nil, or `-'. */)
4770 (Lisp_Object arg)
4771 {
4772 scroll_command (arg, -1);
4773 return Qnil;
4774 }
4775 \f
4776 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4777 doc: /* Return the other window for \"other window scroll\" commands.
4778 If `other-window-scroll-buffer' is non-nil, a window
4779 showing that buffer is used.
4780 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4781 specifies the window. This takes precedence over
4782 `other-window-scroll-buffer'. */)
4783 (void)
4784 {
4785 Lisp_Object window;
4786
4787 if (MINI_WINDOW_P (XWINDOW (selected_window))
4788 && !NILP (Vminibuf_scroll_window))
4789 window = Vminibuf_scroll_window;
4790 /* If buffer is specified, scroll that buffer. */
4791 else if (!NILP (Vother_window_scroll_buffer))
4792 {
4793 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4794 if (NILP (window))
4795 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4796 }
4797 else
4798 {
4799 /* Nothing specified; look for a neighboring window on the same
4800 frame. */
4801 window = Fnext_window (selected_window, Qnil, Qnil);
4802
4803 if (EQ (window, selected_window))
4804 /* That didn't get us anywhere; look for a window on another
4805 visible frame. */
4806 do
4807 window = Fnext_window (window, Qnil, Qt);
4808 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4809 && ! EQ (window, selected_window));
4810 }
4811
4812 CHECK_LIVE_WINDOW (window);
4813
4814 if (EQ (window, selected_window))
4815 error ("There is no other window");
4816
4817 return window;
4818 }
4819
4820 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4821 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4822 A near full screen is `next-screen-context-lines' less than a full screen.
4823 The next window is the one below the current one; or the one at the top
4824 if the current one is at the bottom. Negative ARG means scroll downward.
4825 If ARG is the atom `-', scroll downward by nearly full screen.
4826 When calling from a program, supply as argument a number, nil, or `-'.
4827
4828 If `other-window-scroll-buffer' is non-nil, scroll the window
4829 showing that buffer, popping the buffer up if necessary.
4830 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4831 specifies the window to scroll. This takes precedence over
4832 `other-window-scroll-buffer'. */)
4833 (Lisp_Object arg)
4834 {
4835 Lisp_Object window;
4836 struct window *w;
4837 ptrdiff_t count = SPECPDL_INDEX ();
4838
4839 window = Fother_window_for_scrolling ();
4840 w = XWINDOW (window);
4841
4842 /* Don't screw up if window_scroll gets an error. */
4843 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4844 ++windows_or_buffers_changed;
4845
4846 Fset_buffer (w->buffer);
4847 SET_PT (marker_position (w->pointm));
4848
4849 if (NILP (arg))
4850 window_scroll (window, 1, 1, 1);
4851 else if (EQ (arg, Qminus))
4852 window_scroll (window, -1, 1, 1);
4853 else
4854 {
4855 if (CONSP (arg))
4856 arg = Fcar (arg);
4857 CHECK_NUMBER (arg);
4858 window_scroll (window, XINT (arg), 0, 1);
4859 }
4860
4861 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4862 unbind_to (count, Qnil);
4863
4864 return Qnil;
4865 }
4866 \f
4867 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
4868 doc: /* Scroll selected window display ARG columns left.
4869 Default for ARG is window width minus 2.
4870 Value is the total amount of leftward horizontal scrolling in
4871 effect after the change.
4872 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4873 lower bound for automatic scrolling, i.e. automatic scrolling
4874 will not scroll a window to a column less than the value returned
4875 by this function. This happens in an interactive call. */)
4876 (register Lisp_Object arg, Lisp_Object set_minimum)
4877 {
4878 Lisp_Object result;
4879 EMACS_INT hscroll;
4880 struct window *w = XWINDOW (selected_window);
4881
4882 if (NILP (arg))
4883 XSETFASTINT (arg, window_body_cols (w) - 2);
4884 else
4885 arg = Fprefix_numeric_value (arg);
4886
4887 hscroll = XINT (w->hscroll) + XINT (arg);
4888 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4889
4890 if (!NILP (set_minimum))
4891 w->min_hscroll = w->hscroll;
4892
4893 return result;
4894 }
4895
4896 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
4897 doc: /* Scroll selected window display ARG columns right.
4898 Default for ARG is window width minus 2.
4899 Value is the total amount of leftward horizontal scrolling in
4900 effect after the change.
4901 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4902 lower bound for automatic scrolling, i.e. automatic scrolling
4903 will not scroll a window to a column less than the value returned
4904 by this function. This happens in an interactive call. */)
4905 (register Lisp_Object arg, Lisp_Object set_minimum)
4906 {
4907 Lisp_Object result;
4908 EMACS_INT hscroll;
4909 struct window *w = XWINDOW (selected_window);
4910
4911 if (NILP (arg))
4912 XSETFASTINT (arg, window_body_cols (w) - 2);
4913 else
4914 arg = Fprefix_numeric_value (arg);
4915
4916 hscroll = XINT (w->hscroll) - XINT (arg);
4917 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4918
4919 if (!NILP (set_minimum))
4920 w->min_hscroll = w->hscroll;
4921
4922 return result;
4923 }
4924
4925 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
4926 doc: /* Return the window which was selected when entering the minibuffer.
4927 Returns nil, if selected window is not a minibuffer window. */)
4928 (void)
4929 {
4930 if (minibuf_level > 0
4931 && MINI_WINDOW_P (XWINDOW (selected_window))
4932 && WINDOW_LIVE_P (minibuf_selected_window))
4933 return minibuf_selected_window;
4934
4935 return Qnil;
4936 }
4937
4938 /* Value is the number of lines actually displayed in window W,
4939 as opposed to its height. */
4940
4941 static int
4942 displayed_window_lines (struct window *w)
4943 {
4944 struct it it;
4945 struct text_pos start;
4946 int height = window_box_height (w);
4947 struct buffer *old_buffer;
4948 int bottom_y;
4949 void *itdata = NULL;
4950
4951 if (XBUFFER (w->buffer) != current_buffer)
4952 {
4953 old_buffer = current_buffer;
4954 set_buffer_internal (XBUFFER (w->buffer));
4955 }
4956 else
4957 old_buffer = NULL;
4958
4959 /* In case W->start is out of the accessible range, do something
4960 reasonable. This happens in Info mode when Info-scroll-down
4961 calls (recenter -1) while W->start is 1. */
4962 if (XMARKER (w->start)->charpos < BEGV)
4963 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4964 else if (XMARKER (w->start)->charpos > ZV)
4965 SET_TEXT_POS (start, ZV, ZV_BYTE);
4966 else
4967 SET_TEXT_POS_FROM_MARKER (start, w->start);
4968
4969 itdata = bidi_shelve_cache ();
4970 start_display (&it, w, start);
4971 move_it_vertically (&it, height);
4972 bottom_y = line_bottom_y (&it);
4973 bidi_unshelve_cache (itdata, 0);
4974
4975 /* rms: On a non-window display,
4976 the value of it.vpos at the bottom of the screen
4977 seems to be 1 larger than window_box_height (w).
4978 This kludge fixes a bug whereby (move-to-window-line -1)
4979 when ZV is on the last screen line
4980 moves to the previous screen line instead of the last one. */
4981 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
4982 height++;
4983
4984 /* Add in empty lines at the bottom of the window. */
4985 if (bottom_y < height)
4986 {
4987 int uy = FRAME_LINE_HEIGHT (it.f);
4988 it.vpos += (height - bottom_y + uy - 1) / uy;
4989 }
4990
4991 if (old_buffer)
4992 set_buffer_internal (old_buffer);
4993
4994 return it.vpos;
4995 }
4996
4997
4998 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
4999 doc: /* Center point in selected window and maybe redisplay frame.
5000 With prefix argument ARG, recenter putting point on screen line ARG
5001 relative to the selected window. If ARG is negative, it counts up from the
5002 bottom of the window. (ARG should be less than the height of the window.)
5003
5004 If ARG is omitted or nil, then recenter with point on the middle line of
5005 the selected window; if the variable `recenter-redisplay' is non-nil,
5006 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5007 is set to `grow-only', this resets the tool-bar's height to the minimum
5008 height needed); if `recenter-redisplay' has the special value `tty',
5009 then only tty frames are redrawn.
5010
5011 Just C-u as prefix means put point in the center of the window
5012 and redisplay normally--don't erase and redraw the frame. */)
5013 (register Lisp_Object arg)
5014 {
5015 struct window *w = XWINDOW (selected_window);
5016 struct buffer *buf = XBUFFER (w->buffer);
5017 struct buffer *obuf = current_buffer;
5018 int center_p = 0;
5019 ptrdiff_t charpos, bytepos;
5020 EMACS_INT iarg IF_LINT (= 0);
5021 int this_scroll_margin;
5022
5023 /* If redisplay is suppressed due to an error, try again. */
5024 obuf->display_error_modiff = 0;
5025
5026 if (NILP (arg))
5027 {
5028 if (!NILP (Vrecenter_redisplay)
5029 && (!EQ (Vrecenter_redisplay, Qtty)
5030 || !NILP (Ftty_type (selected_frame))))
5031 {
5032 ptrdiff_t i;
5033
5034 /* Invalidate pixel data calculated for all compositions. */
5035 for (i = 0; i < n_compositions; i++)
5036 composition_table[i]->font = NULL;
5037
5038 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5039
5040 Fredraw_frame (WINDOW_FRAME (w));
5041 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5042 }
5043
5044 center_p = 1;
5045 }
5046 else if (CONSP (arg)) /* Just C-u. */
5047 center_p = 1;
5048 else
5049 {
5050 arg = Fprefix_numeric_value (arg);
5051 CHECK_NUMBER (arg);
5052 iarg = XINT (arg);
5053 }
5054
5055 set_buffer_internal (buf);
5056
5057 /* Do this after making BUF current
5058 in case scroll_margin is buffer-local. */
5059 this_scroll_margin =
5060 max (0, min (scroll_margin, XFASTINT (w->total_lines) / 4));
5061
5062 /* Handle centering on a graphical frame specially. Such frames can
5063 have variable-height lines and centering point on the basis of
5064 line counts would lead to strange effects. */
5065 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5066 {
5067 if (center_p)
5068 {
5069 struct it it;
5070 struct text_pos pt;
5071 void *itdata = bidi_shelve_cache ();
5072
5073 SET_TEXT_POS (pt, PT, PT_BYTE);
5074 start_display (&it, w, pt);
5075 move_it_vertically_backward (&it, window_box_height (w) / 2);
5076 charpos = IT_CHARPOS (it);
5077 bytepos = IT_BYTEPOS (it);
5078 bidi_unshelve_cache (itdata, 0);
5079 }
5080 else if (iarg < 0)
5081 {
5082 struct it it;
5083 struct text_pos pt;
5084 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5085 int extra_line_spacing;
5086 int h = window_box_height (w);
5087 void *itdata = bidi_shelve_cache ();
5088
5089 iarg = - max (-iarg, this_scroll_margin);
5090
5091 SET_TEXT_POS (pt, PT, PT_BYTE);
5092 start_display (&it, w, pt);
5093
5094 /* Be sure we have the exact height of the full line containing PT. */
5095 move_it_by_lines (&it, 0);
5096
5097 /* The amount of pixels we have to move back is the window
5098 height minus what's displayed in the line containing PT,
5099 and the lines below. */
5100 it.current_y = 0;
5101 it.vpos = 0;
5102 move_it_by_lines (&it, nlines);
5103
5104 if (it.vpos == nlines)
5105 h -= it.current_y;
5106 else
5107 {
5108 /* Last line has no newline */
5109 h -= line_bottom_y (&it);
5110 it.vpos++;
5111 }
5112
5113 /* Don't reserve space for extra line spacing of last line. */
5114 extra_line_spacing = it.max_extra_line_spacing;
5115
5116 /* If we can't move down NLINES lines because we hit
5117 the end of the buffer, count in some empty lines. */
5118 if (it.vpos < nlines)
5119 {
5120 nlines -= it.vpos;
5121 extra_line_spacing = it.extra_line_spacing;
5122 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5123 }
5124 if (h <= 0)
5125 {
5126 bidi_unshelve_cache (itdata, 0);
5127 return Qnil;
5128 }
5129
5130 /* Now find the new top line (starting position) of the window. */
5131 start_display (&it, w, pt);
5132 it.current_y = 0;
5133 move_it_vertically_backward (&it, h);
5134
5135 /* If extra line spacing is present, we may move too far
5136 back. This causes the last line to be only partially
5137 visible (which triggers redisplay to recenter that line
5138 in the middle), so move forward.
5139 But ignore extra line spacing on last line, as it is not
5140 considered to be part of the visible height of the line.
5141 */
5142 h += extra_line_spacing;
5143 while (-it.current_y > h)
5144 move_it_by_lines (&it, 1);
5145
5146 charpos = IT_CHARPOS (it);
5147 bytepos = IT_BYTEPOS (it);
5148
5149 bidi_unshelve_cache (itdata, 0);
5150 }
5151 else
5152 {
5153 struct position pos;
5154
5155 iarg = max (iarg, this_scroll_margin);
5156
5157 pos = *vmotion (PT, -iarg, w);
5158 charpos = pos.bufpos;
5159 bytepos = pos.bytepos;
5160 }
5161 }
5162 else
5163 {
5164 struct position pos;
5165 int ht = window_internal_height (w);
5166
5167 if (center_p)
5168 iarg = ht / 2;
5169 else if (iarg < 0)
5170 iarg += ht;
5171
5172 /* Don't let it get into the margin at either top or bottom. */
5173 iarg = max (iarg, this_scroll_margin);
5174 iarg = min (iarg, ht - this_scroll_margin - 1);
5175
5176 pos = *vmotion (PT, - iarg, w);
5177 charpos = pos.bufpos;
5178 bytepos = pos.bytepos;
5179 }
5180
5181 /* Set the new window start. */
5182 set_marker_both (w->start, w->buffer, charpos, bytepos);
5183 w->window_end_valid = Qnil;
5184
5185 w->optional_new_start = 1;
5186
5187 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5188 FETCH_BYTE (bytepos - 1) == '\n');
5189
5190 set_buffer_internal (obuf);
5191 return Qnil;
5192 }
5193
5194 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5195 0, 1, 0,
5196 doc: /* Return the height in lines of the text display area of WINDOW.
5197 If WINDOW is omitted or nil, it defaults to the selected window.
5198
5199 The returned height does not include the mode line, any header line,
5200 nor any partial-height lines at the bottom of the text area. */)
5201 (Lisp_Object window)
5202 {
5203 struct window *w = decode_window (window);
5204 int pixel_height = window_box_height (w);
5205 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5206 return make_number (line_height);
5207 }
5208
5209
5210 \f
5211 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5212 1, 1, "P",
5213 doc: /* Position point relative to window.
5214 ARG nil means position point at center of window.
5215 Else, ARG specifies vertical position within the window;
5216 zero means top of window, negative means relative to bottom of window. */)
5217 (Lisp_Object arg)
5218 {
5219 struct window *w = XWINDOW (selected_window);
5220 int lines, start;
5221 Lisp_Object window;
5222 #if 0
5223 int this_scroll_margin;
5224 #endif
5225
5226 if (!(BUFFERP (w->buffer)
5227 && XBUFFER (w->buffer) == current_buffer))
5228 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5229 when passed below to set_marker_both. */
5230 error ("move-to-window-line called from unrelated buffer");
5231
5232 window = selected_window;
5233 start = marker_position (w->start);
5234 if (start < BEGV || start > ZV)
5235 {
5236 int height = window_internal_height (w);
5237 Fvertical_motion (make_number (- (height / 2)), window);
5238 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5239 w->start_at_line_beg = !NILP (Fbolp ());
5240 w->force_start = 1;
5241 }
5242 else
5243 Fgoto_char (w->start);
5244
5245 lines = displayed_window_lines (w);
5246
5247 #if 0
5248 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5249 #endif
5250
5251 if (NILP (arg))
5252 XSETFASTINT (arg, lines / 2);
5253 else
5254 {
5255 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5256
5257 if (iarg < 0)
5258 iarg = iarg + lines;
5259
5260 #if 0 /* This code would prevent move-to-window-line from moving point
5261 to a place inside the scroll margins (which would cause the
5262 next redisplay to scroll). I wrote this code, but then concluded
5263 it is probably better not to install it. However, it is here
5264 inside #if 0 so as not to lose it. -- rms. */
5265
5266 /* Don't let it get into the margin at either top or bottom. */
5267 iarg = max (iarg, this_scroll_margin);
5268 iarg = min (iarg, lines - this_scroll_margin - 1);
5269 #endif
5270
5271 arg = make_number (iarg);
5272 }
5273
5274 /* Skip past a partially visible first line. */
5275 if (w->vscroll)
5276 XSETINT (arg, XINT (arg) + 1);
5277
5278 return Fvertical_motion (arg, window);
5279 }
5280
5281
5282 \f
5283 /***********************************************************************
5284 Window Configuration
5285 ***********************************************************************/
5286
5287 struct save_window_data
5288 {
5289 struct vectorlike_header header;
5290 Lisp_Object selected_frame;
5291 Lisp_Object current_window;
5292 Lisp_Object current_buffer;
5293 Lisp_Object minibuf_scroll_window;
5294 Lisp_Object minibuf_selected_window;
5295 Lisp_Object root_window;
5296 Lisp_Object focus_frame;
5297 /* A vector, each of whose elements is a struct saved_window
5298 for one window. */
5299 Lisp_Object saved_windows;
5300
5301 /* All fields above are traced by the GC.
5302 From `fame-cols' down, the fields are ignored by the GC. */
5303
5304 int frame_cols, frame_lines, frame_menu_bar_lines;
5305 int frame_tool_bar_lines;
5306 };
5307
5308 /* This is saved as a Lisp_Vector */
5309 struct saved_window
5310 {
5311 struct vectorlike_header header;
5312
5313 Lisp_Object window, buffer, start, pointm, mark;
5314 Lisp_Object left_col, top_line, total_cols, total_lines;
5315 Lisp_Object normal_cols, normal_lines;
5316 Lisp_Object hscroll, min_hscroll;
5317 Lisp_Object parent, prev;
5318 Lisp_Object start_at_line_beg;
5319 Lisp_Object display_table;
5320 Lisp_Object left_margin_cols, right_margin_cols;
5321 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5322 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5323 Lisp_Object combination_limit, window_parameters;
5324 };
5325
5326 #define SAVED_WINDOW_N(swv,n) \
5327 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5328
5329 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5330 doc: /* Return t if OBJECT is a window-configuration object. */)
5331 (Lisp_Object object)
5332 {
5333 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5334 }
5335
5336 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5337 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5338 (Lisp_Object config)
5339 {
5340 register struct save_window_data *data;
5341 struct Lisp_Vector *saved_windows;
5342
5343 CHECK_WINDOW_CONFIGURATION (config);
5344
5345 data = (struct save_window_data *) XVECTOR (config);
5346 saved_windows = XVECTOR (data->saved_windows);
5347 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5348 }
5349
5350 DEFUN ("set-window-configuration", Fset_window_configuration,
5351 Sset_window_configuration, 1, 1, 0,
5352 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5353 CONFIGURATION must be a value previously returned
5354 by `current-window-configuration' (which see).
5355 If CONFIGURATION was made from a frame that is now deleted,
5356 only frame-independent values can be restored. In this case,
5357 the return value is nil. Otherwise the value is t. */)
5358 (Lisp_Object configuration)
5359 {
5360 register struct save_window_data *data;
5361 struct Lisp_Vector *saved_windows;
5362 Lisp_Object new_current_buffer;
5363 Lisp_Object frame;
5364 Lisp_Object auto_buffer_name;
5365 FRAME_PTR f;
5366 ptrdiff_t old_point = -1;
5367
5368 CHECK_WINDOW_CONFIGURATION (configuration);
5369
5370 data = (struct save_window_data *) XVECTOR (configuration);
5371 saved_windows = XVECTOR (data->saved_windows);
5372
5373 new_current_buffer = data->current_buffer;
5374 if (NILP (BVAR (XBUFFER (new_current_buffer), name)))
5375 new_current_buffer = Qnil;
5376 else
5377 {
5378 if (XBUFFER (new_current_buffer) == current_buffer)
5379 /* The code further down "preserves point" by saving here PT in
5380 old_point and then setting it later back into PT. When the
5381 current-selected-window and the final-selected-window both show
5382 the current buffer, this suffers from the problem that the
5383 current PT is the window-point of the current-selected-window,
5384 while the final PT is the point of the final-selected-window, so
5385 this copy from one PT to the other would end up moving the
5386 window-point of the final-selected-window to the window-point of
5387 the current-selected-window. So we have to be careful which
5388 point of the current-buffer we copy into old_point. */
5389 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5390 && WINDOWP (selected_window)
5391 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5392 && !EQ (selected_window, data->current_window))
5393 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5394 else
5395 old_point = PT;
5396 else
5397 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5398 point in new_current_buffer as of the last time this buffer was
5399 used. This can be non-deterministic since it can be changed by
5400 things like jit-lock by mere temporary selection of some random
5401 window that happens to show this buffer.
5402 So if possible we want this arbitrary choice of "which point" to
5403 be the one from the to-be-selected-window so as to prevent this
5404 window's cursor from being copied from another window. */
5405 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5406 /* If current_window = selected_window, its point is in BUF_PT. */
5407 && !EQ (selected_window, data->current_window))
5408 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5409 else
5410 old_point = BUF_PT (XBUFFER (new_current_buffer));
5411 }
5412
5413 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5414 f = XFRAME (frame);
5415
5416 /* If f is a dead frame, don't bother rebuilding its window tree.
5417 However, there is other stuff we should still try to do below. */
5418 if (FRAME_LIVE_P (f))
5419 {
5420 Lisp_Object window;
5421 Lisp_Object dead_windows = Qnil;
5422 register Lisp_Object tem, par, pers;
5423 register struct window *w;
5424 register struct saved_window *p;
5425 struct window *root_window;
5426 struct window **leaf_windows;
5427 int n_leaf_windows;
5428 ptrdiff_t k;
5429 int i, n;
5430
5431 /* If the frame has been resized since this window configuration was
5432 made, we change the frame to the size specified in the
5433 configuration, restore the configuration, and then resize it
5434 back. We keep track of the prevailing height in these variables. */
5435 int previous_frame_lines = FRAME_LINES (f);
5436 int previous_frame_cols = FRAME_COLS (f);
5437 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5438 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5439
5440 /* The mouse highlighting code could get screwed up
5441 if it runs during this. */
5442 BLOCK_INPUT;
5443
5444 if (data->frame_lines != previous_frame_lines
5445 || data->frame_cols != previous_frame_cols)
5446 change_frame_size (f, data->frame_lines,
5447 data->frame_cols, 0, 0, 0);
5448 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5449 if (data->frame_menu_bar_lines
5450 != previous_frame_menu_bar_lines)
5451 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5452 make_number (0));
5453 #ifdef HAVE_WINDOW_SYSTEM
5454 if (data->frame_tool_bar_lines
5455 != previous_frame_tool_bar_lines)
5456 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5457 make_number (0));
5458 #endif
5459 #endif
5460
5461 /* "Swap out" point from the selected window's buffer
5462 into the window itself. (Normally the pointm of the selected
5463 window holds garbage.) We do this now, before
5464 restoring the window contents, and prevent it from
5465 being done later on when we select a new window. */
5466 if (! NILP (XWINDOW (selected_window)->buffer))
5467 {
5468 w = XWINDOW (selected_window);
5469 set_marker_both (w->pointm,
5470 w->buffer,
5471 BUF_PT (XBUFFER (w->buffer)),
5472 BUF_PT_BYTE (XBUFFER (w->buffer)));
5473 }
5474
5475 windows_or_buffers_changed++;
5476 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5477
5478 /* Problem: Freeing all matrices and later allocating them again
5479 is a serious redisplay flickering problem. What we would
5480 really like to do is to free only those matrices not reused
5481 below. */
5482 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5483 leaf_windows
5484 = (struct window **) alloca (count_windows (root_window)
5485 * sizeof (struct window *));
5486 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5487
5488 /* Kludge Alert!
5489 Mark all windows now on frame as "deleted".
5490 Restoring the new configuration "undeletes" any that are in it.
5491
5492 Save their current buffers in their height fields, since we may
5493 need it later, if a buffer saved in the configuration is now
5494 dead. */
5495 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5496
5497 for (k = 0; k < saved_windows->header.size; k++)
5498 {
5499 p = SAVED_WINDOW_N (saved_windows, k);
5500 window = p->window;
5501 w = XWINDOW (window);
5502 w->next = Qnil;
5503
5504 if (!NILP (p->parent))
5505 w->parent = SAVED_WINDOW_N (saved_windows,
5506 XFASTINT (p->parent))->window;
5507 else
5508 w->parent = Qnil;
5509
5510 if (!NILP (p->prev))
5511 {
5512 w->prev = SAVED_WINDOW_N (saved_windows,
5513 XFASTINT (p->prev))->window;
5514 XWINDOW (w->prev)->next = p->window;
5515 }
5516 else
5517 {
5518 w->prev = Qnil;
5519 if (!NILP (w->parent))
5520 {
5521 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5522 {
5523 XWINDOW (w->parent)->vchild = p->window;
5524 XWINDOW (w->parent)->hchild = Qnil;
5525 }
5526 else
5527 {
5528 XWINDOW (w->parent)->hchild = p->window;
5529 XWINDOW (w->parent)->vchild = Qnil;
5530 }
5531 }
5532 }
5533
5534 /* If we squirreled away the buffer in the window's height,
5535 restore it now. */
5536 if (BUFFERP (w->total_lines))
5537 w->buffer = w->total_lines;
5538 w->left_col = p->left_col;
5539 w->top_line = p->top_line;
5540 w->total_cols = p->total_cols;
5541 w->total_lines = p->total_lines;
5542 w->normal_cols = p->normal_cols;
5543 w->normal_lines = p->normal_lines;
5544 w->hscroll = p->hscroll;
5545 w->min_hscroll = p->min_hscroll;
5546 w->display_table = p->display_table;
5547 w->left_margin_cols = p->left_margin_cols;
5548 w->right_margin_cols = p->right_margin_cols;
5549 w->left_fringe_width = p->left_fringe_width;
5550 w->right_fringe_width = p->right_fringe_width;
5551 w->fringes_outside_margins = p->fringes_outside_margins;
5552 w->scroll_bar_width = p->scroll_bar_width;
5553 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
5554 w->dedicated = p->dedicated;
5555 w->combination_limit = p->combination_limit;
5556 /* Restore any window parameters that have been saved.
5557 Parameters that have not been saved are left alone. */
5558 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5559 {
5560 pers = XCAR (tem);
5561 if (CONSP (pers))
5562 {
5563 if (NILP (XCDR (pers)))
5564 {
5565 par = Fassq (XCAR (pers), w->window_parameters);
5566 if (CONSP (par) && !NILP (XCDR (par)))
5567 /* Reset a parameter to nil if and only if it
5568 has a non-nil association. Don't make new
5569 associations. */
5570 Fsetcdr (par, Qnil);
5571 }
5572 else
5573 /* Always restore a non-nil value. */
5574 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5575 }
5576 }
5577
5578 XSETFASTINT (w->last_modified, 0);
5579 XSETFASTINT (w->last_overlay_modified, 0);
5580
5581 /* Reinstall the saved buffer and pointers into it. */
5582 if (NILP (p->buffer))
5583 /* An internal window. */
5584 w->buffer = p->buffer;
5585 else if (!NILP (BVAR (XBUFFER (p->buffer), name)))
5586 /* If saved buffer is alive, install it. */
5587 {
5588 w->buffer = p->buffer;
5589 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5590 set_marker_restricted (w->start, p->start, w->buffer);
5591 set_marker_restricted (w->pointm, p->pointm, w->buffer);
5592 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
5593 p->mark, w->buffer);
5594
5595 /* As documented in Fcurrent_window_configuration, don't
5596 restore the location of point in the buffer which was
5597 current when the window configuration was recorded. */
5598 if (!EQ (p->buffer, new_current_buffer)
5599 && XBUFFER (p->buffer) == current_buffer)
5600 Fgoto_char (w->pointm);
5601 }
5602 else if (!NILP (w->buffer) && !NILP (BVAR (XBUFFER (w->buffer), name)))
5603 /* Keep window's old buffer; make sure the markers are
5604 real. */
5605 {
5606 /* Set window markers at start of visible range. */
5607 if (XMARKER (w->start)->buffer == 0)
5608 set_marker_restricted (w->start, make_number (0),
5609 w->buffer);
5610 if (XMARKER (w->pointm)->buffer == 0)
5611 set_marker_restricted_both (w->pointm, w->buffer,
5612 BUF_PT (XBUFFER (w->buffer)),
5613 BUF_PT_BYTE (XBUFFER (w->buffer)));
5614 w->start_at_line_beg = 1;
5615 }
5616 else if (STRINGP (auto_buffer_name =
5617 Fwindow_parameter (window, Qauto_buffer_name))
5618 && SCHARS (auto_buffer_name) != 0
5619 && !NILP (w->buffer = Fget_buffer_create (auto_buffer_name)))
5620 {
5621 set_marker_restricted (w->start, make_number (0), w->buffer);
5622 set_marker_restricted (w->pointm, make_number (0), w->buffer);
5623 w->start_at_line_beg = 1;
5624 }
5625 else
5626 /* Window has no live buffer, get one. */
5627 {
5628 /* Get the buffer via other_buffer_safely in order to
5629 avoid showing an unimportant buffer and, if necessary, to
5630 recreate *scratch* in the course (part of Juanma's bs-show
5631 scenario from March 2011). */
5632 w->buffer = other_buffer_safely (Fcurrent_buffer ());
5633 /* This will set the markers to beginning of visible
5634 range. */
5635 set_marker_restricted (w->start, make_number (0), w->buffer);
5636 set_marker_restricted (w->pointm, make_number (0), w->buffer);
5637 w->start_at_line_beg = 1;
5638 if (!NILP (w->dedicated))
5639 /* Record this window as dead. */
5640 dead_windows = Fcons (window, dead_windows);
5641 /* Make sure window is no more dedicated. */
5642 w->dedicated = Qnil;
5643 }
5644 }
5645
5646 FRAME_ROOT_WINDOW (f) = data->root_window;
5647 /* Arrange *not* to restore point in the buffer that was
5648 current when the window configuration was saved. */
5649 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5650 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5651 make_number (old_point),
5652 XWINDOW (data->current_window)->buffer);
5653
5654 /* In the following call to `select-window', prevent "swapping out
5655 point" in the old selected window using the buffer that has
5656 been restored into it. We already swapped out that point from
5657 that window's old buffer. */
5658 select_window (data->current_window, Qnil, 1);
5659 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
5660 = selected_window;
5661
5662 if (NILP (data->focus_frame)
5663 || (FRAMEP (data->focus_frame)
5664 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5665 Fredirect_frame_focus (frame, data->focus_frame);
5666
5667 /* Set the screen height to the value it had before this function. */
5668 if (previous_frame_lines != FRAME_LINES (f)
5669 || previous_frame_cols != FRAME_COLS (f))
5670 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5671 0, 0, 0);
5672 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5673 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5674 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5675 make_number (0));
5676 #ifdef HAVE_WINDOW_SYSTEM
5677 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5678 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5679 make_number (0));
5680 #endif
5681 #endif
5682
5683 /* Now, free glyph matrices in windows that were not reused. */
5684 for (i = n = 0; i < n_leaf_windows; ++i)
5685 {
5686 if (NILP (leaf_windows[i]->buffer))
5687 {
5688 /* Assert it's not reused as a combination. */
5689 eassert (NILP (leaf_windows[i]->hchild)
5690 && NILP (leaf_windows[i]->vchild));
5691 free_window_matrices (leaf_windows[i]);
5692 }
5693 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5694 ++n;
5695 }
5696
5697 adjust_glyphs (f);
5698 UNBLOCK_INPUT;
5699
5700 /* Scan dead buffer windows. */
5701 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5702 {
5703 window = XCAR (dead_windows);
5704 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5705 delete_deletable_window (window);
5706 }
5707
5708 /* Fselect_window will have made f the selected frame, so we
5709 reselect the proper frame here. Fhandle_switch_frame will change the
5710 selected window too, but that doesn't make the call to
5711 Fselect_window above totally superfluous; it still sets f's
5712 selected window. */
5713 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5714 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5715
5716 run_window_configuration_change_hook (f);
5717 }
5718
5719 if (!NILP (new_current_buffer))
5720 Fset_buffer (new_current_buffer);
5721
5722 Vminibuf_scroll_window = data->minibuf_scroll_window;
5723 minibuf_selected_window = data->minibuf_selected_window;
5724
5725 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5726 }
5727
5728
5729 /* Recursively delete all child windows reachable via the next, vchild,
5730 and hchild slots of WINDOW. */
5731 void
5732 delete_all_child_windows (Lisp_Object window)
5733 {
5734 register struct window *w;
5735
5736 w = XWINDOW (window);
5737
5738 if (!NILP (w->next))
5739 /* Delete WINDOW's siblings (we traverse postorderly). */
5740 delete_all_child_windows (w->next);
5741
5742 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
5743
5744 if (!NILP (w->vchild))
5745 {
5746 delete_all_child_windows (w->vchild);
5747 w->vchild = Qnil;
5748 }
5749 else if (!NILP (w->hchild))
5750 {
5751 delete_all_child_windows (w->hchild);
5752 w->hchild = Qnil;
5753 }
5754 else if (!NILP (w->buffer))
5755 {
5756 unshow_buffer (w);
5757 unchain_marker (XMARKER (w->pointm));
5758 unchain_marker (XMARKER (w->start));
5759 w->buffer = Qnil;
5760 }
5761
5762 Vwindow_list = Qnil;
5763 }
5764 \f
5765 static int
5766 count_windows (register struct window *window)
5767 {
5768 register int count = 1;
5769 if (!NILP (window->next))
5770 count += count_windows (XWINDOW (window->next));
5771 if (!NILP (window->vchild))
5772 count += count_windows (XWINDOW (window->vchild));
5773 if (!NILP (window->hchild))
5774 count += count_windows (XWINDOW (window->hchild));
5775 return count;
5776 }
5777
5778
5779 /* Fill vector FLAT with leaf windows under W, starting at index I.
5780 Value is last index + 1. */
5781 static int
5782 get_leaf_windows (struct window *w, struct window **flat, int i)
5783 {
5784 while (w)
5785 {
5786 if (!NILP (w->hchild))
5787 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5788 else if (!NILP (w->vchild))
5789 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5790 else
5791 flat[i++] = w;
5792
5793 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5794 }
5795
5796 return i;
5797 }
5798
5799
5800 /* Return a pointer to the glyph W's physical cursor is on. Value is
5801 null if W's current matrix is invalid, so that no meaningful glyph
5802 can be returned. */
5803 struct glyph *
5804 get_phys_cursor_glyph (struct window *w)
5805 {
5806 struct glyph_row *row;
5807 struct glyph *glyph;
5808 int hpos = w->phys_cursor.hpos;
5809
5810 if (!(w->phys_cursor.vpos >= 0
5811 && w->phys_cursor.vpos < w->current_matrix->nrows))
5812 return NULL;
5813
5814 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5815 if (!row->enabled_p)
5816 return NULL;
5817
5818 if (XINT (w->hscroll))
5819 {
5820 /* When the window is hscrolled, cursor hpos can legitimately be
5821 out of bounds, but we draw the cursor at the corresponding
5822 window margin in that case. */
5823 if (!row->reversed_p && hpos < 0)
5824 hpos = 0;
5825 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
5826 hpos = row->used[TEXT_AREA] - 1;
5827 }
5828
5829 if (row->used[TEXT_AREA] > hpos
5830 && 0 <= hpos)
5831 glyph = row->glyphs[TEXT_AREA] + hpos;
5832 else
5833 glyph = NULL;
5834
5835 return glyph;
5836 }
5837
5838
5839 static int
5840 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
5841 {
5842 register struct saved_window *p;
5843 register struct window *w;
5844 register Lisp_Object tem, pers, par;
5845
5846 for (;!NILP (window); window = w->next)
5847 {
5848 p = SAVED_WINDOW_N (vector, i);
5849 w = XWINDOW (window);
5850
5851 XSETFASTINT (w->temslot, i); i++;
5852 p->window = window;
5853 p->buffer = w->buffer;
5854 p->left_col = w->left_col;
5855 p->top_line = w->top_line;
5856 p->total_cols = w->total_cols;
5857 p->total_lines = w->total_lines;
5858 p->normal_cols = w->normal_cols;
5859 p->normal_lines = w->normal_lines;
5860 p->hscroll = w->hscroll;
5861 p->min_hscroll = w->min_hscroll;
5862 p->display_table = w->display_table;
5863 p->left_margin_cols = w->left_margin_cols;
5864 p->right_margin_cols = w->right_margin_cols;
5865 p->left_fringe_width = w->left_fringe_width;
5866 p->right_fringe_width = w->right_fringe_width;
5867 p->fringes_outside_margins = w->fringes_outside_margins;
5868 p->scroll_bar_width = w->scroll_bar_width;
5869 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5870 p->dedicated = w->dedicated;
5871 p->combination_limit = w->combination_limit;
5872 p->window_parameters = Qnil;
5873
5874 if (!NILP (Vwindow_persistent_parameters))
5875 {
5876 /* Run cycle detection on Vwindow_persistent_parameters. */
5877 Lisp_Object tortoise, hare;
5878
5879 hare = tortoise = Vwindow_persistent_parameters;
5880 while (CONSP (hare))
5881 {
5882 hare = XCDR (hare);
5883 if (!CONSP (hare))
5884 break;
5885
5886 hare = XCDR (hare);
5887 tortoise = XCDR (tortoise);
5888
5889 if (EQ (hare, tortoise))
5890 /* Reset Vwindow_persistent_parameters to Qnil. */
5891 {
5892 Vwindow_persistent_parameters = Qnil;
5893 break;
5894 }
5895 }
5896
5897 for (tem = Vwindow_persistent_parameters; CONSP (tem);
5898 tem = XCDR (tem))
5899 {
5900 pers = XCAR (tem);
5901 /* Save values for persistent window parameters. */
5902 if (CONSP (pers) && !NILP (XCDR (pers)))
5903 {
5904 par = Fassq (XCAR (pers), w->window_parameters);
5905 if (NILP (par))
5906 /* If the window has no value for the parameter,
5907 make one. */
5908 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
5909 p->window_parameters);
5910 else
5911 /* If the window has a value for the parameter,
5912 save it. */
5913 p->window_parameters = Fcons (Fcons (XCAR (par),
5914 XCDR (par)),
5915 p->window_parameters);
5916 }
5917 }
5918 }
5919
5920 if (!NILP (w->buffer))
5921 {
5922 /* Save w's value of point in the window configuration. If w
5923 is the selected window, then get the value of point from
5924 the buffer; pointm is garbage in the selected window. */
5925 if (EQ (window, selected_window))
5926 {
5927 p->pointm = Fmake_marker ();
5928 set_marker_both (p->pointm, w->buffer,
5929 BUF_PT (XBUFFER (w->buffer)),
5930 BUF_PT_BYTE (XBUFFER (w->buffer)));
5931 }
5932 else
5933 p->pointm = Fcopy_marker (w->pointm, Qnil);
5934 XMARKER (p->pointm)->insertion_type
5935 = !NILP (Vwindow_point_insertion_type);
5936
5937 p->start = Fcopy_marker (w->start, Qnil);
5938 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
5939
5940 tem = BVAR (XBUFFER (w->buffer), mark);
5941 p->mark = Fcopy_marker (tem, Qnil);
5942 }
5943 else
5944 {
5945 p->pointm = Qnil;
5946 p->start = Qnil;
5947 p->mark = Qnil;
5948 p->start_at_line_beg = Qnil;
5949 }
5950
5951 if (NILP (w->parent))
5952 p->parent = Qnil;
5953 else
5954 p->parent = XWINDOW (w->parent)->temslot;
5955
5956 if (NILP (w->prev))
5957 p->prev = Qnil;
5958 else
5959 p->prev = XWINDOW (w->prev)->temslot;
5960
5961 if (!NILP (w->vchild))
5962 i = save_window_save (w->vchild, vector, i);
5963 if (!NILP (w->hchild))
5964 i = save_window_save (w->hchild, vector, i);
5965 }
5966
5967 return i;
5968 }
5969
5970 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
5971 Scurrent_window_configuration, 0, 1, 0,
5972 doc: /* Return an object representing the current window configuration of FRAME.
5973 If FRAME is nil or omitted, use the selected frame.
5974 This describes the number of windows, their sizes and current buffers,
5975 and for each displayed buffer, where display starts, and the positions of
5976 point and mark. An exception is made for point in the current buffer:
5977 its value is -not- saved.
5978 This also records the currently selected frame, and FRAME's focus
5979 redirection (see `redirect-frame-focus'). The variable
5980 `window-persistent-parameters' specifies which window parameters are
5981 saved by this function. */)
5982 (Lisp_Object frame)
5983 {
5984 register Lisp_Object tem;
5985 register int n_windows;
5986 register struct save_window_data *data;
5987 register int i;
5988 FRAME_PTR f;
5989
5990 if (NILP (frame))
5991 frame = selected_frame;
5992 CHECK_LIVE_FRAME (frame);
5993 f = XFRAME (frame);
5994
5995 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
5996 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
5997 PVEC_WINDOW_CONFIGURATION);
5998
5999 data->frame_cols = FRAME_COLS (f);
6000 data->frame_lines = FRAME_LINES (f);
6001 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6002 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6003 data->selected_frame = selected_frame;
6004 data->current_window = FRAME_SELECTED_WINDOW (f);
6005 XSETBUFFER (data->current_buffer, current_buffer);
6006 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6007 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6008 data->root_window = FRAME_ROOT_WINDOW (f);
6009 data->focus_frame = FRAME_FOCUS_FRAME (f);
6010 tem = Fmake_vector (make_number (n_windows), Qnil);
6011 data->saved_windows = tem;
6012 for (i = 0; i < n_windows; i++)
6013 ASET (tem, i,
6014 Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil));
6015 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6016 XSETWINDOW_CONFIGURATION (tem, data);
6017 return (tem);
6018 }
6019 \f
6020 /***********************************************************************
6021 Marginal Areas
6022 ***********************************************************************/
6023
6024 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6025 2, 3, 0,
6026 doc: /* Set width of marginal areas of window WINDOW.
6027 If WINDOW is nil, set margins of the currently selected window.
6028 Second arg LEFT-WIDTH specifies the number of character cells to
6029 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6030 does the same for the right marginal area. A nil width parameter
6031 means no margin. */)
6032 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6033 {
6034 struct window *w = decode_window (window);
6035
6036 /* Translate negative or zero widths to nil.
6037 Margins that are too wide have to be checked elsewhere. */
6038
6039 if (!NILP (left_width))
6040 {
6041 CHECK_NUMBER (left_width);
6042 if (XINT (left_width) <= 0)
6043 left_width = Qnil;
6044 }
6045
6046 if (!NILP (right_width))
6047 {
6048 CHECK_NUMBER (right_width);
6049 if (XINT (right_width) <= 0)
6050 right_width = Qnil;
6051 }
6052
6053 if (!EQ (w->left_margin_cols, left_width)
6054 || !EQ (w->right_margin_cols, right_width))
6055 {
6056 w->left_margin_cols = left_width;
6057 w->right_margin_cols = right_width;
6058
6059 adjust_window_margins (w);
6060
6061 ++windows_or_buffers_changed;
6062 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6063 }
6064
6065 return Qnil;
6066 }
6067
6068
6069 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6070 0, 1, 0,
6071 doc: /* Get width of marginal areas of window WINDOW.
6072 If WINDOW is omitted or nil, it defaults to the selected window.
6073 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6074 If a marginal area does not exist, its width will be returned
6075 as nil. */)
6076 (Lisp_Object window)
6077 {
6078 struct window *w = decode_window (window);
6079 return Fcons (w->left_margin_cols, w->right_margin_cols);
6080 }
6081
6082
6083 \f
6084 /***********************************************************************
6085 Fringes
6086 ***********************************************************************/
6087
6088 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6089 2, 4, 0,
6090 doc: /* Set the fringe widths of window WINDOW.
6091 If WINDOW is nil, set the fringe widths of the currently selected
6092 window.
6093 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6094 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6095 fringe width. If a fringe width arg is nil, that means to use the
6096 frame's default fringe width. Default fringe widths can be set with
6097 the command `set-fringe-style'.
6098 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6099 outside of the display margins. By default, fringes are drawn between
6100 display marginal areas and the text area. */)
6101 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6102 {
6103 struct window *w = decode_window (window);
6104
6105 if (!NILP (left_width))
6106 CHECK_NATNUM (left_width);
6107 if (!NILP (right_width))
6108 CHECK_NATNUM (right_width);
6109
6110 /* Do nothing on a tty. */
6111 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6112 && (!EQ (w->left_fringe_width, left_width)
6113 || !EQ (w->right_fringe_width, right_width)
6114 || !EQ (w->fringes_outside_margins, outside_margins)))
6115 {
6116 w->left_fringe_width = left_width;
6117 w->right_fringe_width = right_width;
6118 w->fringes_outside_margins = outside_margins;
6119
6120 adjust_window_margins (w);
6121
6122 clear_glyph_matrix (w->current_matrix);
6123 w->window_end_valid = Qnil;
6124
6125 ++windows_or_buffers_changed;
6126 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6127 }
6128
6129 return Qnil;
6130 }
6131
6132
6133 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6134 0, 1, 0,
6135 doc: /* Get width of fringes of window WINDOW.
6136 If WINDOW is omitted or nil, it defaults to the selected window.
6137 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6138 (Lisp_Object window)
6139 {
6140 struct window *w = decode_window (window);
6141
6142 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6143 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6144 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6145 ? Qt : Qnil), Qnil)));
6146 }
6147
6148
6149 \f
6150 /***********************************************************************
6151 Scroll bars
6152 ***********************************************************************/
6153
6154 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6155 Sset_window_scroll_bars, 2, 4, 0,
6156 doc: /* Set width and type of scroll bars of window WINDOW.
6157 If window is nil, set scroll bars of the currently selected window.
6158 Second parameter WIDTH specifies the pixel width for the scroll bar;
6159 this is automatically adjusted to a multiple of the frame column width.
6160 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6161 bar: left, right, or nil.
6162 If WIDTH is nil, use the frame's scroll-bar width.
6163 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6164 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6165 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6166 {
6167 struct window *w = decode_window (window);
6168
6169 if (!NILP (width))
6170 {
6171 CHECK_RANGED_INTEGER (0, width, INT_MAX);
6172
6173 if (XINT (width) == 0)
6174 vertical_type = Qnil;
6175 }
6176
6177 if (!(NILP (vertical_type)
6178 || EQ (vertical_type, Qleft)
6179 || EQ (vertical_type, Qright)
6180 || EQ (vertical_type, Qt)))
6181 error ("Invalid type of vertical scroll bar");
6182
6183 if (!EQ (w->scroll_bar_width, width)
6184 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6185 {
6186 w->scroll_bar_width = width;
6187 w->vertical_scroll_bar_type = vertical_type;
6188
6189 adjust_window_margins (w);
6190
6191 clear_glyph_matrix (w->current_matrix);
6192 w->window_end_valid = Qnil;
6193
6194 ++windows_or_buffers_changed;
6195 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6196 }
6197
6198 return Qnil;
6199 }
6200
6201
6202 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6203 0, 1, 0,
6204 doc: /* Get width and type of scroll bars of window WINDOW.
6205 If WINDOW is omitted or nil, it defaults to the selected window.
6206 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6207 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6208 value. */)
6209 (Lisp_Object window)
6210 {
6211 struct window *w = decode_window (window);
6212 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6213 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6214 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6215 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6216 Fcons (w->vertical_scroll_bar_type,
6217 Fcons (Qnil, Qnil))));
6218 }
6219
6220
6221 \f
6222 /***********************************************************************
6223 Smooth scrolling
6224 ***********************************************************************/
6225
6226 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6227 doc: /* Return the amount by which WINDOW is scrolled vertically.
6228 If WINDOW is omitted or nil, it defaults to the selected window.
6229 Normally, value is a multiple of the canonical character height of WINDOW;
6230 optional second arg PIXELS-P means value is measured in pixels. */)
6231 (Lisp_Object window, Lisp_Object pixels_p)
6232 {
6233 Lisp_Object result;
6234 struct frame *f;
6235 struct window *w;
6236
6237 if (NILP (window))
6238 window = selected_window;
6239 else
6240 CHECK_WINDOW (window);
6241 w = XWINDOW (window);
6242 f = XFRAME (w->frame);
6243
6244 if (FRAME_WINDOW_P (f))
6245 result = (NILP (pixels_p)
6246 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6247 : make_number (-w->vscroll));
6248 else
6249 result = make_number (0);
6250 return result;
6251 }
6252
6253
6254 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6255 2, 3, 0,
6256 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6257 WINDOW nil means use the selected window. Normally, VSCROLL is a
6258 non-negative multiple of the canonical character height of WINDOW;
6259 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6260 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6261 corresponds to an integral number of pixels. The return value is the
6262 result of this rounding.
6263 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6264 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6265 {
6266 struct window *w;
6267 struct frame *f;
6268
6269 if (NILP (window))
6270 window = selected_window;
6271 else
6272 CHECK_WINDOW (window);
6273 CHECK_NUMBER_OR_FLOAT (vscroll);
6274
6275 w = XWINDOW (window);
6276 f = XFRAME (w->frame);
6277
6278 if (FRAME_WINDOW_P (f))
6279 {
6280 int old_dy = w->vscroll;
6281
6282 w->vscroll = - (NILP (pixels_p)
6283 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6284 : XFLOATINT (vscroll));
6285 w->vscroll = min (w->vscroll, 0);
6286
6287 if (w->vscroll != old_dy)
6288 {
6289 /* Adjust glyph matrix of the frame if the virtual display
6290 area becomes larger than before. */
6291 if (w->vscroll < 0 && w->vscroll < old_dy)
6292 adjust_glyphs (f);
6293
6294 /* Prevent redisplay shortcuts. */
6295 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6296 }
6297 }
6298
6299 return Fwindow_vscroll (window, pixels_p);
6300 }
6301
6302 \f
6303 /* Call FN for all leaf windows on frame F. FN is called with the
6304 first argument being a pointer to the leaf window, and with
6305 additional argument USER_DATA. Stops when FN returns 0. */
6306
6307 static void
6308 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6309 void *user_data)
6310 {
6311 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6312 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6313 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6314 }
6315
6316
6317 /* Helper function for foreach_window. Call FN for all leaf windows
6318 reachable from W. FN is called with the first argument being a
6319 pointer to the leaf window, and with additional argument USER_DATA.
6320 Stop when FN returns 0. Value is 0 if stopped by FN. */
6321
6322 static int
6323 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6324 {
6325 int cont;
6326
6327 for (cont = 1; w && cont;)
6328 {
6329 if (!NILP (w->hchild))
6330 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6331 else if (!NILP (w->vchild))
6332 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6333 else
6334 cont = fn (w, user_data);
6335
6336 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6337 }
6338
6339 return cont;
6340 }
6341
6342
6343 /* Freeze or unfreeze the window start of W unless it is a
6344 mini-window or the selected window. FREEZE_P non-null means freeze
6345 the window start. */
6346
6347 static int
6348 freeze_window_start (struct window *w, void *freeze_p)
6349 {
6350 if (MINI_WINDOW_P (w)
6351 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6352 && (w == XWINDOW (selected_window)
6353 || (MINI_WINDOW_P (XWINDOW (selected_window))
6354 && ! NILP (Vminibuf_scroll_window)
6355 && w == XWINDOW (Vminibuf_scroll_window)))))
6356 freeze_p = NULL;
6357
6358 w->frozen_window_start_p = freeze_p != NULL;
6359 return 1;
6360 }
6361
6362
6363 /* Freeze or unfreeze the window starts of all leaf windows on frame
6364 F, except the selected window and a mini-window. FREEZE_P non-zero
6365 means freeze the window start. */
6366
6367 void
6368 freeze_window_starts (struct frame *f, int freeze_p)
6369 {
6370 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6371 }
6372
6373 \f
6374 /***********************************************************************
6375 Initialization
6376 ***********************************************************************/
6377
6378 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6379 describe the same state of affairs. This is used by Fequal.
6380
6381 ignore_positions non-zero means ignore non-matching scroll positions
6382 and the like.
6383
6384 This ignores a couple of things like the dedication status of
6385 window, combination_limit and the like. This might have to be
6386 fixed. */
6387
6388 int
6389 compare_window_configurations (Lisp_Object configuration1, Lisp_Object configuration2, int ignore_positions)
6390 {
6391 register struct save_window_data *d1, *d2;
6392 struct Lisp_Vector *sws1, *sws2;
6393 ptrdiff_t i;
6394
6395 CHECK_WINDOW_CONFIGURATION (configuration1);
6396 CHECK_WINDOW_CONFIGURATION (configuration2);
6397
6398 d1 = (struct save_window_data *) XVECTOR (configuration1);
6399 d2 = (struct save_window_data *) XVECTOR (configuration2);
6400 sws1 = XVECTOR (d1->saved_windows);
6401 sws2 = XVECTOR (d2->saved_windows);
6402
6403 /* Frame settings must match. */
6404 if (d1->frame_cols != d2->frame_cols
6405 || d1->frame_lines != d2->frame_lines
6406 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6407 || !EQ (d1->selected_frame, d2->selected_frame)
6408 || !EQ (d1->current_buffer, d2->current_buffer)
6409 || (!ignore_positions
6410 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6411 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6412 || !EQ (d1->focus_frame, d2->focus_frame)
6413 /* Verify that the two configurations have the same number of windows. */
6414 || sws1->header.size != sws2->header.size)
6415 return 0;
6416
6417 for (i = 0; i < sws1->header.size; i++)
6418 {
6419 struct saved_window *sw1, *sw2;
6420
6421 sw1 = SAVED_WINDOW_N (sws1, i);
6422 sw2 = SAVED_WINDOW_N (sws2, i);
6423
6424 if (
6425 /* The "current" windows in the two configurations must
6426 correspond to each other. */
6427 EQ (d1->current_window, sw1->window)
6428 != EQ (d2->current_window, sw2->window)
6429 /* Windows' buffers must match. */
6430 || !EQ (sw1->buffer, sw2->buffer)
6431 || !EQ (sw1->left_col, sw2->left_col)
6432 || !EQ (sw1->top_line, sw2->top_line)
6433 || !EQ (sw1->total_cols, sw2->total_cols)
6434 || !EQ (sw1->total_lines, sw2->total_lines)
6435 || !EQ (sw1->display_table, sw2->display_table)
6436 /* The next two disjuncts check the window structure for
6437 equality. */
6438 || !EQ (sw1->parent, sw2->parent)
6439 || !EQ (sw1->prev, sw2->prev)
6440 || (!ignore_positions
6441 && (!EQ (sw1->hscroll, sw2->hscroll)
6442 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6443 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6444 || NILP (Fequal (sw1->start, sw2->start))
6445 || NILP (Fequal (sw1->pointm, sw2->pointm))
6446 || NILP (Fequal (sw1->mark, sw2->mark))))
6447 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6448 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6449 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6450 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6451 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6452 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6453 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6454 return 0;
6455 }
6456
6457 return 1;
6458 }
6459
6460 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6461 Scompare_window_configurations, 2, 2, 0,
6462 doc: /* Compare two window configurations as regards the structure of windows.
6463 This function ignores details such as the values of point and mark
6464 and scrolling positions. */)
6465 (Lisp_Object x, Lisp_Object y)
6466 {
6467 if (compare_window_configurations (x, y, 1))
6468 return Qt;
6469 return Qnil;
6470 }
6471 \f
6472 void
6473 init_window_once (void)
6474 {
6475 struct frame *f = make_initial_frame ();
6476 XSETFRAME (selected_frame, f);
6477 Vterminal_frame = selected_frame;
6478 minibuf_window = f->minibuffer_window;
6479 selected_window = f->selected_window;
6480 last_nonminibuf_frame = f;
6481
6482 window_initialized = 1;
6483 }
6484
6485 void
6486 init_window (void)
6487 {
6488 Vwindow_list = Qnil;
6489 }
6490
6491 void
6492 syms_of_window (void)
6493 {
6494 DEFSYM (Qscroll_up, "scroll-up");
6495 DEFSYM (Qscroll_down, "scroll-down");
6496 DEFSYM (Qscroll_command, "scroll-command");
6497
6498 Fput (Qscroll_up, Qscroll_command, Qt);
6499 Fput (Qscroll_down, Qscroll_command, Qt);
6500
6501 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6502 DEFSYM (Qwindowp, "windowp");
6503 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6504 DEFSYM (Qwindow_live_p, "window-live-p");
6505 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6506 DEFSYM (Qdelete_window, "delete-window");
6507 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6508 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6509 DEFSYM (Qsafe, "safe");
6510 DEFSYM (Qdisplay_buffer, "display-buffer");
6511 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6512 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6513 DEFSYM (Qget_mru_window, "get-mru-window");
6514 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6515 DEFSYM (Qabove, "above");
6516 DEFSYM (Qbelow, "below");
6517 DEFSYM (Qauto_buffer_name, "auto-buffer-name");
6518 DEFSYM (Qclone_of, "clone-of");
6519
6520 staticpro (&Vwindow_list);
6521
6522 minibuf_selected_window = Qnil;
6523 staticpro (&minibuf_selected_window);
6524
6525 window_scroll_pixel_based_preserve_x = -1;
6526 window_scroll_pixel_based_preserve_y = -1;
6527 window_scroll_preserve_hpos = -1;
6528 window_scroll_preserve_vpos = -1;
6529
6530 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6531 doc: /* Non-nil means call as function to display a help buffer.
6532 The function is called with one argument, the buffer to be displayed.
6533 Used by `with-output-to-temp-buffer'.
6534 If this function is used, then it must do the entire job of showing
6535 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6536 Vtemp_buffer_show_function = Qnil;
6537
6538 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6539 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6540 Vminibuf_scroll_window = Qnil;
6541
6542 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6543 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6544 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6545 is displayed in the `mode-line' face. */);
6546 mode_line_in_non_selected_windows = 1;
6547
6548 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6549 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6550 Vother_window_scroll_buffer = Qnil;
6551
6552 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6553 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6554 auto_window_vscroll_p = 1;
6555
6556 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6557 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6558 next_screen_context_lines = 2;
6559
6560 DEFVAR_LISP ("scroll-preserve-screen-position",
6561 Vscroll_preserve_screen_position,
6562 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6563 A value of nil means point does not keep its screen position except
6564 at the scroll margin or window boundary respectively.
6565 A value of t means point keeps its screen position if the scroll
6566 command moved it vertically out of the window, e.g. when scrolling
6567 by full screens.
6568 Any other value means point always keeps its screen position.
6569 Scroll commands should have the `scroll-command' property
6570 on their symbols to be controlled by this variable. */);
6571 Vscroll_preserve_screen_position = Qnil;
6572
6573 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6574 doc: /* Type of marker to use for `window-point'. */);
6575 Vwindow_point_insertion_type = Qnil;
6576
6577 DEFVAR_LISP ("window-configuration-change-hook",
6578 Vwindow_configuration_change_hook,
6579 doc: /* Functions to call when window configuration changes.
6580 The buffer-local part is run once per window, with the relevant window
6581 selected; while the global part is run only once for the modified frame,
6582 with the relevant frame selected. */);
6583 Vwindow_configuration_change_hook = Qnil;
6584
6585 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6586 doc: /* Non-nil means `recenter' redraws entire frame.
6587 If this option is non-nil, then the `recenter' command with a nil
6588 argument will redraw the entire frame; the special value `tty' causes
6589 the frame to be redrawn only if it is a tty frame. */);
6590 Vrecenter_redisplay = Qtty;
6591
6592 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6593 doc: /* If t, resize window combinations proportionally.
6594 If this variable is nil, splitting a window gets the entire screen space
6595 for displaying the new window from the window to split. Deleting and
6596 resizing a window preferably resizes one adjacent window only.
6597
6598 If this variable is t, splitting a window tries to get the space
6599 proportionally from all windows in the same combination. This also
6600 allows to split a window that is otherwise too small or of fixed size.
6601 Resizing and deleting a window proportionally resize all windows in the
6602 same combination.
6603
6604 Other values are reserved for future use.
6605
6606 This variable takes no effect if `window-combination-limit' is non-nil. */);
6607 Vwindow_combination_resize = Qnil;
6608
6609 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6610 doc: /* If t, splitting a window makes a new parent window.
6611 If this variable is nil, splitting a window will create a new parent
6612 window only if the window has no parent window or the window shall
6613 become a combination orthogonal to the one it is part of.
6614
6615 If this variable is t, splitting a window always creates a new parent
6616 window. If all splits behave this way, each frame's window tree is a
6617 binary tree and every window but the frame's root window has exactly one
6618 sibling.
6619
6620 Other values are reserved for future use.
6621
6622 The value of this variable is also assigned to the combination limit of
6623 the new parent window. The combination limit of a window can be
6624 retrieved via the function `window-combination-limit' and altered by the
6625 function `set-window-combination-limit'. */);
6626 Vwindow_combination_limit = Qnil;
6627
6628 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6629 doc: /* Alist of persistent window parameters.
6630 This alist specifies which window parameters shall get saved by
6631 `current-window-configuration' and `window-state-get' and subsequently
6632 restored to their previous values by `set-window-configuration' and
6633 `window-state-put'.
6634
6635 The car of each entry of this alist is the symbol specifying the
6636 parameter. The cdr is one of the following:
6637
6638 nil means the parameter is neither saved by `window-state-get' nor by
6639 `current-window-configuration'.
6640
6641 t means the parameter is saved by `current-window-configuration' and,
6642 provided its WRITABLE argument is nil, by `window-state-get'.
6643
6644 The symbol `writable' means the parameter is saved unconditionally by
6645 both `current-window-configuration' and `window-state-get'. Do not use
6646 this value for parameters without read syntax (like windows or frames).
6647
6648 Parameters not saved by `current-window-configuration' or
6649 `window-state-get' are left alone by `set-window-configuration'
6650 respectively are not installed by `window-state-put'. */);
6651 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6652
6653 defsubr (&Sselected_window);
6654 defsubr (&Sminibuffer_window);
6655 defsubr (&Swindow_minibuffer_p);
6656 defsubr (&Swindowp);
6657 defsubr (&Swindow_live_p);
6658 defsubr (&Swindow_frame);
6659 defsubr (&Sframe_root_window);
6660 defsubr (&Sframe_first_window);
6661 defsubr (&Sframe_selected_window);
6662 defsubr (&Sset_frame_selected_window);
6663 defsubr (&Spos_visible_in_window_p);
6664 defsubr (&Swindow_line_height);
6665 defsubr (&Swindow_buffer);
6666 defsubr (&Swindow_parent);
6667 defsubr (&Swindow_top_child);
6668 defsubr (&Swindow_left_child);
6669 defsubr (&Swindow_next_sibling);
6670 defsubr (&Swindow_prev_sibling);
6671 defsubr (&Swindow_combination_limit);
6672 defsubr (&Sset_window_combination_limit);
6673 defsubr (&Swindow_use_time);
6674 defsubr (&Swindow_top_line);
6675 defsubr (&Swindow_left_column);
6676 defsubr (&Swindow_total_height);
6677 defsubr (&Swindow_total_width);
6678 defsubr (&Swindow_normal_size);
6679 defsubr (&Swindow_new_total);
6680 defsubr (&Swindow_new_normal);
6681 defsubr (&Sset_window_new_total);
6682 defsubr (&Sset_window_new_normal);
6683 defsubr (&Swindow_resize_apply);
6684 defsubr (&Swindow_body_height);
6685 defsubr (&Swindow_body_width);
6686 defsubr (&Swindow_hscroll);
6687 defsubr (&Sset_window_hscroll);
6688 defsubr (&Swindow_redisplay_end_trigger);
6689 defsubr (&Sset_window_redisplay_end_trigger);
6690 defsubr (&Swindow_edges);
6691 defsubr (&Swindow_pixel_edges);
6692 defsubr (&Swindow_absolute_pixel_edges);
6693 defsubr (&Swindow_inside_edges);
6694 defsubr (&Swindow_inside_pixel_edges);
6695 defsubr (&Swindow_inside_absolute_pixel_edges);
6696 defsubr (&Scoordinates_in_window_p);
6697 defsubr (&Swindow_at);
6698 defsubr (&Swindow_point);
6699 defsubr (&Swindow_start);
6700 defsubr (&Swindow_end);
6701 defsubr (&Sset_window_point);
6702 defsubr (&Sset_window_start);
6703 defsubr (&Swindow_dedicated_p);
6704 defsubr (&Sset_window_dedicated_p);
6705 defsubr (&Swindow_display_table);
6706 defsubr (&Sset_window_display_table);
6707 defsubr (&Snext_window);
6708 defsubr (&Sprevious_window);
6709 defsubr (&Sget_buffer_window);
6710 defsubr (&Sdelete_other_windows_internal);
6711 defsubr (&Sdelete_window_internal);
6712 defsubr (&Sresize_mini_window_internal);
6713 defsubr (&Sset_window_buffer);
6714 defsubr (&Srun_window_configuration_change_hook);
6715 defsubr (&Sselect_window);
6716 defsubr (&Sforce_window_update);
6717 defsubr (&Stemp_output_buffer_show);
6718 defsubr (&Ssplit_window_internal);
6719 defsubr (&Sscroll_up);
6720 defsubr (&Sscroll_down);
6721 defsubr (&Sscroll_left);
6722 defsubr (&Sscroll_right);
6723 defsubr (&Sother_window_for_scrolling);
6724 defsubr (&Sscroll_other_window);
6725 defsubr (&Sminibuffer_selected_window);
6726 defsubr (&Srecenter);
6727 defsubr (&Swindow_text_height);
6728 defsubr (&Smove_to_window_line);
6729 defsubr (&Swindow_configuration_p);
6730 defsubr (&Swindow_configuration_frame);
6731 defsubr (&Sset_window_configuration);
6732 defsubr (&Scurrent_window_configuration);
6733 defsubr (&Sset_window_margins);
6734 defsubr (&Swindow_margins);
6735 defsubr (&Sset_window_fringes);
6736 defsubr (&Swindow_fringes);
6737 defsubr (&Sset_window_scroll_bars);
6738 defsubr (&Swindow_scroll_bars);
6739 defsubr (&Swindow_vscroll);
6740 defsubr (&Sset_window_vscroll);
6741 defsubr (&Scompare_window_configurations);
6742 defsubr (&Swindow_list);
6743 defsubr (&Swindow_list_1);
6744 defsubr (&Swindow_prev_buffers);
6745 defsubr (&Sset_window_prev_buffers);
6746 defsubr (&Swindow_next_buffers);
6747 defsubr (&Sset_window_next_buffers);
6748 defsubr (&Swindow_parameters);
6749 defsubr (&Swindow_parameter);
6750 defsubr (&Sset_window_parameter);
6751 }
6752
6753 void
6754 keys_of_window (void)
6755 {
6756 initial_define_key (control_x_map, '<', "scroll-left");
6757 initial_define_key (control_x_map, '>', "scroll-right");
6758
6759 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6760 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6761 initial_define_key (meta_map, 'v', "scroll-down-command");
6762 }