Merge changes from emacs-24; up to 2012-04-26T02:03:19Z!ueno@unixuser.org
[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 xassert (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 int i;
3246
3247 o = XWINDOW (window);
3248 p = allocate_window ();
3249 for (i = 0; i < VECSIZE (struct window); ++i)
3250 ((struct Lisp_Vector *) p)->contents[i]
3251 = ((struct Lisp_Vector *) o)->contents[i];
3252 XSETWINDOW (parent, p);
3253
3254 ++sequence_number;
3255 XSETFASTINT (p->sequence_number, sequence_number);
3256
3257 replace_window (window, parent, 1);
3258
3259 o->next = Qnil;
3260 o->prev = Qnil;
3261 o->parent = parent;
3262
3263 p->hchild = horflag ? window : Qnil;
3264 p->vchild = horflag ? Qnil : window;
3265 p->start = Qnil;
3266 p->pointm = Qnil;
3267 p->buffer = Qnil;
3268 p->combination_limit = Qnil;
3269 p->window_parameters = Qnil;
3270 }
3271
3272 /* Make new window from scratch. */
3273 Lisp_Object
3274 make_window (void)
3275 {
3276 Lisp_Object window;
3277 register struct window *w;
3278
3279 w = allocate_window ();
3280 /* Initialize all Lisp data. */
3281 w->frame = Qnil;
3282 w->mini = 0;
3283 w->next = w->prev = w->hchild = w->vchild = w->parent = Qnil;
3284 XSETFASTINT (w->left_col, 0);
3285 XSETFASTINT (w->top_line, 0);
3286 XSETFASTINT (w->total_lines, 0);
3287 XSETFASTINT (w->total_cols, 0);
3288 w->normal_lines = make_float (1.0);
3289 w->normal_cols = make_float (1.0);
3290 XSETFASTINT (w->new_total, 0);
3291 XSETFASTINT (w->new_normal, 0);
3292 w->buffer = Qnil;
3293 w->start = Fmake_marker ();
3294 w->pointm = Fmake_marker ();
3295 w->force_start = w->optional_new_start = 0;
3296 XSETFASTINT (w->hscroll, 0);
3297 XSETFASTINT (w->min_hscroll, 0);
3298 XSETFASTINT (w->use_time, 0);
3299 ++sequence_number;
3300 XSETFASTINT (w->sequence_number, sequence_number);
3301 w->temslot = w->last_modified = w->last_overlay_modified = Qnil;
3302 XSETFASTINT (w->last_point, 0);
3303 w->last_had_star = 0;
3304 w->vertical_scroll_bar = Qnil;
3305 w->left_margin_cols = w->right_margin_cols = Qnil;
3306 w->left_fringe_width = w->right_fringe_width = Qnil;
3307 w->fringes_outside_margins = Qnil;
3308 w->scroll_bar_width = Qnil;
3309 w->vertical_scroll_bar_type = Qt;
3310 XSETFASTINT (w->window_end_pos, 0);
3311 XSETFASTINT (w->window_end_vpos, 0);
3312 w->window_end_valid = w->display_table = Qnil;
3313 w->update_mode_line = w->start_at_line_beg = 0;
3314 w->dedicated = Qnil;
3315 w->base_line_number = w->base_line_pos = w->region_showing = Qnil;
3316 w->column_number_displayed = w->redisplay_end_trigger = Qnil;
3317 w->combination_limit = w->window_parameters = Qnil;
3318 w->prev_buffers = w->next_buffers = Qnil;
3319 /* Initialize non-Lisp data. */
3320 w->desired_matrix = w->current_matrix = 0;
3321 w->nrows_scale_factor = w->ncols_scale_factor = 1;
3322 memset (&w->cursor, 0, sizeof (w->cursor));
3323 memset (&w->last_cursor, 0, sizeof (w->last_cursor));
3324 memset (&w->phys_cursor, 0, sizeof (w->phys_cursor));
3325 w->phys_cursor_type = -1;
3326 w->phys_cursor_width = -1;
3327 w->phys_cursor_on_p = 0;
3328 w->last_cursor_off_p = w->cursor_off_p = 0;
3329 w->must_be_updated_p = 0;
3330 w->pseudo_window_p = 0;
3331 w->frozen_window_start_p = 0;
3332 w->vscroll = 0;
3333 /* Reset window_list. */
3334 Vwindow_list = Qnil;
3335 /* Return window. */
3336 XSETWINDOW (window, w);
3337 return window;
3338 }
3339 \f
3340 DEFUN ("set-window-new-total", Fset_window_new_total, Sset_window_new_total, 2, 3, 0,
3341 doc: /* Set new total size of WINDOW to SIZE.
3342 Return SIZE.
3343
3344 Optional argument ADD non-nil means add SIZE to the new total size of
3345 WINDOW and return the sum.
3346
3347 Note: This function does not operate on any child windows of WINDOW. */)
3348 (Lisp_Object window, Lisp_Object size, Lisp_Object add)
3349 {
3350 struct window *w = decode_any_window (window);
3351
3352 CHECK_NUMBER (size);
3353 if (NILP (add))
3354 XSETINT (w->new_total, XINT (size));
3355 else
3356 XSETINT (w->new_total, XINT (w->new_total) + XINT (size));
3357
3358 return w->new_total;
3359 }
3360
3361 DEFUN ("set-window-new-normal", Fset_window_new_normal, Sset_window_new_normal, 1, 2, 0,
3362 doc: /* Set new normal size of WINDOW to SIZE.
3363 Return SIZE.
3364
3365 Note: This function does not operate on any child windows of WINDOW. */)
3366 (Lisp_Object window, Lisp_Object size)
3367 {
3368 struct window *w = decode_any_window (window);
3369
3370 w->new_normal = size;
3371 return w->new_normal;
3372 }
3373
3374 /* Return 1 if setting w->total_lines (w->total_cols if HORFLAG is
3375 non-zero) to w->new_total would result in correct heights (widths)
3376 for window W and recursively all child windows of W.
3377
3378 Note: This function does not check any of `window-fixed-size-p',
3379 `window-min-height' or `window-min-width'. It does check that window
3380 sizes do not drop below one line (two columns). */
3381 static int
3382 window_resize_check (struct window *w, int horflag)
3383 {
3384 struct window *c;
3385
3386 if (!NILP (w->vchild))
3387 /* W is a vertical combination. */
3388 {
3389 c = XWINDOW (w->vchild);
3390 if (horflag)
3391 /* All child windows of W must have the same width as W. */
3392 {
3393 while (c)
3394 {
3395 if ((XINT (c->new_total) != XINT (w->new_total))
3396 || !window_resize_check (c, horflag))
3397 return 0;
3398 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3399 }
3400 return 1;
3401 }
3402 else
3403 /* The sum of the heights of the child windows of W must equal
3404 W's height. */
3405 {
3406 int sum_of_sizes = 0;
3407 while (c)
3408 {
3409 if (!window_resize_check (c, horflag))
3410 return 0;
3411 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3412 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3413 }
3414 return (sum_of_sizes == XINT (w->new_total));
3415 }
3416 }
3417 else if (!NILP (w->hchild))
3418 /* W is a horizontal combination. */
3419 {
3420 c = XWINDOW (w->hchild);
3421 if (horflag)
3422 /* The sum of the widths of the child windows of W must equal W's
3423 width. */
3424 {
3425 int sum_of_sizes = 0;
3426 while (c)
3427 {
3428 if (!window_resize_check (c, horflag))
3429 return 0;
3430 sum_of_sizes = sum_of_sizes + XINT (c->new_total);
3431 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3432 }
3433 return (sum_of_sizes == XINT (w->new_total));
3434 }
3435 else
3436 /* All child windows of W must have the same height as W. */
3437 {
3438 while (c)
3439 {
3440 if ((XINT (c->new_total) != XINT (w->new_total))
3441 || !window_resize_check (c, horflag))
3442 return 0;
3443 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3444 }
3445 return 1;
3446 }
3447 }
3448 else
3449 /* A leaf window. Make sure it's not too small. The following
3450 hardcodes the values of `window-safe-min-width' (2) and
3451 `window-safe-min-height' (1) which are defined in window.el. */
3452 return XINT (w->new_total) >= (horflag ? 2 : 1);
3453 }
3454
3455 /* Set w->total_lines (w->total_cols if HORIZONTAL is non-zero) to
3456 w->new_total for window W and recursively all child windows of W.
3457 Also calculate and assign the new vertical (horizontal) start
3458 positions of each of these windows.
3459
3460 This function does not perform any error checks. Make sure you have
3461 run window_resize_check on W before applying this function. */
3462 static void
3463 window_resize_apply (struct window *w, int horflag)
3464 {
3465 struct window *c;
3466 int pos;
3467
3468 /* Note: Assigning new_normal requires that the new total size of the
3469 parent window has been set *before*. */
3470 if (horflag)
3471 {
3472 w->total_cols = w->new_total;
3473 if (NUMBERP (w->new_normal))
3474 w->normal_cols = w->new_normal;
3475
3476 pos = XINT (w->left_col);
3477 }
3478 else
3479 {
3480 w->total_lines = w->new_total;
3481 if (NUMBERP (w->new_normal))
3482 w->normal_lines = w->new_normal;
3483
3484 pos = XINT (w->top_line);
3485 }
3486
3487 if (!NILP (w->vchild))
3488 /* W is a vertical combination. */
3489 {
3490 c = XWINDOW (w->vchild);
3491 while (c)
3492 {
3493 if (horflag)
3494 XSETFASTINT (c->left_col, pos);
3495 else
3496 XSETFASTINT (c->top_line, pos);
3497 window_resize_apply (c, horflag);
3498 if (!horflag)
3499 pos = pos + XINT (c->total_lines);
3500 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3501 }
3502 }
3503 else if (!NILP (w->hchild))
3504 /* W is a horizontal combination. */
3505 {
3506 c = XWINDOW (w->hchild);
3507 while (c)
3508 {
3509 if (horflag)
3510 XSETFASTINT (c->left_col, pos);
3511 else
3512 XSETFASTINT (c->top_line, pos);
3513 window_resize_apply (c, horflag);
3514 if (horflag)
3515 pos = pos + XINT (c->total_cols);
3516 c = NILP (c->next) ? 0 : XWINDOW (c->next);
3517 }
3518 }
3519
3520 /* Clear out some redisplay caches. */
3521 XSETFASTINT (w->last_modified, 0);
3522 XSETFASTINT (w->last_overlay_modified, 0);
3523 }
3524
3525
3526 DEFUN ("window-resize-apply", Fwindow_resize_apply, Swindow_resize_apply, 1, 2, 0,
3527 doc: /* Apply requested size values for window-tree of FRAME.
3528 Optional argument HORIZONTAL omitted or nil means apply requested height
3529 values. HORIZONTAL non-nil means apply requested width values.
3530
3531 This function checks whether the requested values sum up to a valid
3532 window layout, recursively assigns the new sizes of all child windows
3533 and calculates and assigns the new start positions of these windows.
3534
3535 Note: This function does not check any of `window-fixed-size-p',
3536 `window-min-height' or `window-min-width'. All these checks have to
3537 be applied on the Elisp level. */)
3538 (Lisp_Object frame, Lisp_Object horizontal)
3539 {
3540 struct frame *f;
3541 struct window *r;
3542 int horflag = !NILP (horizontal);
3543
3544 if (NILP (frame))
3545 frame = selected_frame;
3546 CHECK_LIVE_FRAME (frame);
3547
3548 f = XFRAME (frame);
3549 r = XWINDOW (FRAME_ROOT_WINDOW (f));
3550
3551 if (!window_resize_check (r, horflag)
3552 || ! EQ (r->new_total, (horflag ? r->total_cols : r->total_lines)))
3553 return Qnil;
3554
3555 BLOCK_INPUT;
3556 window_resize_apply (r, horflag);
3557
3558 windows_or_buffers_changed++;
3559 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3560
3561 adjust_glyphs (f);
3562 UNBLOCK_INPUT;
3563
3564 run_window_configuration_change_hook (f);
3565
3566 return Qt;
3567 }
3568
3569
3570 /* Resize frame F's windows when number of lines of F is set to SIZE.
3571 HORFLAG 1 means resize windows when number of columns of F is set to
3572 SIZE.
3573
3574 This function can delete all windows but the selected one in order to
3575 satisfy the request. The result will be meaningful if and only if
3576 F's windows have meaningful sizes when you call this. */
3577 void
3578 resize_frame_windows (struct frame *f, int size, int horflag)
3579 {
3580 Lisp_Object root = f->root_window;
3581 struct window *r = XWINDOW (root);
3582 Lisp_Object mini = f->minibuffer_window;
3583 struct window *m;
3584 /* new_size is the new size of the frame's root window. */
3585 int new_size = (horflag
3586 ? size
3587 : (size
3588 - FRAME_TOP_MARGIN (f)
3589 - ((FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3590 ? 1 : 0)));
3591
3592 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
3593 if (NILP (r->vchild) && NILP (r->hchild))
3594 /* For a leaf root window just set the size. */
3595 if (horflag)
3596 XSETFASTINT (r->total_cols, new_size);
3597 else
3598 XSETFASTINT (r->total_lines, new_size);
3599 else
3600 {
3601 /* old_size is the old size of the frame's root window. */
3602 int old_size = XFASTINT (horflag ? r->total_cols : r->total_lines);
3603 Lisp_Object delta;
3604
3605 XSETINT (delta, new_size - old_size);
3606 /* Try a "normal" resize first. */
3607 resize_root_window (root, delta, horflag ? Qt : Qnil, Qnil);
3608 if (window_resize_check (r, horflag) && new_size == XINT (r->new_total))
3609 window_resize_apply (r, horflag);
3610 else
3611 {
3612 /* Try with "reasonable" minimum sizes next. */
3613 resize_root_window (root, delta, horflag ? Qt : Qnil, Qt);
3614 if (window_resize_check (r, horflag)
3615 && new_size == XINT (r->new_total))
3616 window_resize_apply (r, horflag);
3617 else
3618 {
3619 /* Finally, try with "safe" minimum sizes. */
3620 resize_root_window (root, delta, horflag ? Qt : Qnil, Qsafe);
3621 if (window_resize_check (r, horflag)
3622 && new_size == XINT (r->new_total))
3623 window_resize_apply (r, horflag);
3624 else
3625 {
3626 /* We lost. Delete all windows but the frame's
3627 selected one. */
3628 root = f->selected_window;
3629 Fdelete_other_windows_internal (root, Qnil);
3630 if (horflag)
3631 XSETFASTINT (XWINDOW (root)->total_cols, new_size);
3632 else
3633 XSETFASTINT (XWINDOW (root)->total_lines, new_size);
3634 }
3635 }
3636 }
3637 }
3638
3639 if (FRAME_HAS_MINIBUF_P (f) && !FRAME_MINIBUF_ONLY_P (f))
3640 {
3641 m = XWINDOW (mini);
3642 if (horflag)
3643 XSETFASTINT (m->total_cols, size);
3644 else
3645 {
3646 /* Are we sure we always want 1 line here? */
3647 XSETFASTINT (m->total_lines, 1);
3648 XSETFASTINT (m->top_line, XINT (r->top_line) + XINT (r->total_lines));
3649 }
3650 }
3651 }
3652
3653
3654 DEFUN ("split-window-internal", Fsplit_window_internal, Ssplit_window_internal, 4, 4, 0,
3655 doc: /* Split window OLD.
3656 Second argument TOTAL-SIZE specifies the number of lines or columns of the
3657 new window. In any case TOTAL-SIZE must be a positive integer.
3658
3659 Third argument SIDE nil (or `below') specifies that the new window shall
3660 be located below WINDOW. SIDE `above' means the new window shall be
3661 located above WINDOW. In both cases TOTAL-SIZE specifies the number of
3662 lines of the new window including space reserved for the mode and/or
3663 header line.
3664
3665 SIDE t (or `right') specifies that the new window shall be located on
3666 the right side of WINDOW. SIDE `left' means the new window shall be
3667 located on the left of WINDOW. In both cases TOTAL-SIZE specifies the
3668 number of columns of the new window including space reserved for fringes
3669 and the scrollbar or a divider column.
3670
3671 Fourth argument NORMAL-SIZE specifies the normal size of the new window
3672 according to the SIDE argument.
3673
3674 The new total and normal sizes of all involved windows must have been
3675 set correctly. See the code of `split-window' for how this is done. */)
3676 (Lisp_Object old, Lisp_Object total_size, Lisp_Object side, Lisp_Object normal_size)
3677 {
3678 /* OLD (*o) is the window we have to split. (*p) is either OLD's
3679 parent window or an internal window we have to install as OLD's new
3680 parent. REFERENCE (*r) must denote a live window, or is set to OLD
3681 provided OLD is a leaf window, or to the frame's selected window.
3682 NEW (*n) is the new window created with some parameters taken from
3683 REFERENCE (*r). */
3684 register Lisp_Object new, frame, reference;
3685 register struct window *o, *p, *n, *r;
3686 struct frame *f;
3687 int horflag
3688 /* HORFLAG is 1 when we split side-by-side, 0 otherwise. */
3689 = EQ (side, Qt) || EQ (side, Qleft) || EQ (side, Qright);
3690 int combination_limit = 0;
3691
3692 CHECK_WINDOW (old);
3693 o = XWINDOW (old);
3694 frame = WINDOW_FRAME (o);
3695 f = XFRAME (frame);
3696
3697 CHECK_NUMBER (total_size);
3698
3699 /* Set combination_limit to 1 if we have to make a new parent window.
3700 We do that if either `window-combination-limit' is t, or OLD has no
3701 parent, or OLD is ortho-combined. */
3702 combination_limit =
3703 !NILP (Vwindow_combination_limit)
3704 || NILP (o->parent)
3705 || NILP (horflag
3706 ? (XWINDOW (o->parent)->hchild)
3707 : (XWINDOW (o->parent)->vchild));
3708
3709 /* We need a live reference window to initialize some parameters. */
3710 if (WINDOW_LIVE_P (old))
3711 /* OLD is live, use it as reference window. */
3712 reference = old;
3713 else
3714 /* Use the frame's selected window as reference window. */
3715 reference = FRAME_SELECTED_WINDOW (f);
3716 r = XWINDOW (reference);
3717
3718 /* The following bugs are caught by `split-window'. */
3719 if (MINI_WINDOW_P (o))
3720 error ("Attempt to split minibuffer window");
3721 else if (XINT (total_size) < (horflag ? 2 : 1))
3722 error ("Size of new window too small (after split)");
3723 else if (!combination_limit && !NILP (Vwindow_combination_resize))
3724 /* `window-combination-resize' non-nil means try to resize OLD's siblings
3725 proportionally. */
3726 {
3727 p = XWINDOW (o->parent);
3728 /* Temporarily pretend we split the parent window. */
3729 XSETINT (p->new_total,
3730 XINT (horflag ? p->total_cols : p->total_lines)
3731 - XINT (total_size));
3732 if (!window_resize_check (p, horflag))
3733 error ("Window sizes don't fit");
3734 else
3735 /* Undo the temporary pretension. */
3736 p->new_total = horflag ? p->total_cols : p->total_lines;
3737 }
3738 else
3739 {
3740 if (!window_resize_check (o, horflag))
3741 error ("Resizing old window failed");
3742 else if (XINT (total_size) + XINT (o->new_total)
3743 != XINT (horflag ? o->total_cols : o->total_lines))
3744 error ("Sum of sizes of old and new window don't fit");
3745 }
3746
3747 /* This is our point of no return. */
3748 if (combination_limit)
3749 {
3750 /* Save the old value of o->normal_cols/lines. It gets corrupted
3751 by make_parent_window and we need it below for assigning it to
3752 p->new_normal. */
3753 Lisp_Object new_normal = horflag ? o->normal_cols : o->normal_lines;
3754
3755 make_parent_window (old, horflag);
3756 p = XWINDOW (o->parent);
3757 /* Store value of `window-combination-limit' in new parent's
3758 combination_limit slot. */
3759 p->combination_limit = Vwindow_combination_limit;
3760 /* These get applied below. */
3761 p->new_total = horflag ? o->total_cols : o->total_lines;
3762 p->new_normal = new_normal;
3763 }
3764 else
3765 p = XWINDOW (o->parent);
3766
3767 windows_or_buffers_changed++;
3768 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3769 new = make_window ();
3770 n = XWINDOW (new);
3771 n->frame = frame;
3772 n->parent = o->parent;
3773 n->vchild = n->hchild = Qnil;
3774
3775 if (EQ (side, Qabove) || EQ (side, Qleft))
3776 {
3777 n->prev = o->prev;
3778 if (NILP (n->prev))
3779 if (horflag)
3780 p->hchild = new;
3781 else
3782 p->vchild = new;
3783 else
3784 XWINDOW (n->prev)->next = new;
3785 n->next = old;
3786 o->prev = new;
3787 }
3788 else
3789 {
3790 n->next = o->next;
3791 if (!NILP (n->next))
3792 XWINDOW (n->next)->prev = new;
3793 n->prev = old;
3794 o->next = new;
3795 }
3796
3797 n->buffer = Qt;
3798 n->window_end_valid = Qnil;
3799 memset (&n->last_cursor, 0, sizeof n->last_cursor);
3800
3801 /* Get special geometry settings from reference window. */
3802 n->left_margin_cols = r->left_margin_cols;
3803 n->right_margin_cols = r->right_margin_cols;
3804 n->left_fringe_width = r->left_fringe_width;
3805 n->right_fringe_width = r->right_fringe_width;
3806 n->fringes_outside_margins = r->fringes_outside_margins;
3807 n->scroll_bar_width = r->scroll_bar_width;
3808 n->vertical_scroll_bar_type = r->vertical_scroll_bar_type;
3809
3810 /* Directly assign orthogonal coordinates and sizes. */
3811 if (horflag)
3812 {
3813 n->top_line = o->top_line;
3814 n->total_lines = o->total_lines;
3815 }
3816 else
3817 {
3818 n->left_col = o->left_col;
3819 n->total_cols = o->total_cols;
3820 }
3821
3822 /* Iso-coordinates and sizes are assigned by window_resize_apply,
3823 get them ready here. */
3824 n->new_total = total_size;
3825 n->new_normal = normal_size;
3826
3827 BLOCK_INPUT;
3828 window_resize_apply (p, horflag);
3829 adjust_glyphs (f);
3830 /* Set buffer of NEW to buffer of reference window. Don't run
3831 any hooks. */
3832 set_window_buffer (new, r->buffer, 0, 1);
3833 UNBLOCK_INPUT;
3834
3835 /* Maybe we should run the scroll functions in Elisp (which already
3836 runs the configuration change hook). */
3837 if (! NILP (Vwindow_scroll_functions))
3838 run_hook_with_args_2 (Qwindow_scroll_functions, new,
3839 Fmarker_position (n->start));
3840 /* Return NEW. */
3841 return new;
3842 }
3843
3844
3845 DEFUN ("delete-window-internal", Fdelete_window_internal, Sdelete_window_internal, 1, 1, 0,
3846 doc: /* Remove WINDOW from its frame.
3847 WINDOW defaults to the selected window. Return nil.
3848 Signal an error when WINDOW is the only window on its frame. */)
3849 (register Lisp_Object window)
3850 {
3851 register Lisp_Object parent, sibling, frame, root;
3852 struct window *w, *p, *s, *r;
3853 struct frame *f;
3854 int horflag;
3855 int before_sibling = 0;
3856
3857 w = decode_any_window (window);
3858 XSETWINDOW (window, w);
3859 if (NILP (w->buffer) && NILP (w->hchild) && NILP (w->vchild))
3860 /* It's a no-op to delete an already deleted window. */
3861 return Qnil;
3862
3863 parent = w->parent;
3864 if (NILP (parent))
3865 /* Never delete a minibuffer or frame root window. */
3866 error ("Attempt to delete minibuffer or sole ordinary window");
3867 else if (NILP (w->prev) && NILP (w->next))
3868 /* Rather bow out here, this case should be handled on the Elisp
3869 level. */
3870 error ("Attempt to delete sole window of parent");
3871
3872 p = XWINDOW (parent);
3873 horflag = NILP (p->vchild);
3874
3875 frame = WINDOW_FRAME (w);
3876 f = XFRAME (frame);
3877
3878 root = FRAME_ROOT_WINDOW (f);
3879 r = XWINDOW (root);
3880
3881 /* Unlink WINDOW from window tree. */
3882 if (NILP (w->prev))
3883 /* Get SIBLING below (on the right of) WINDOW. */
3884 {
3885 /* before_sibling 1 means WINDOW is the first child of its
3886 parent and thus before the sibling. */
3887 before_sibling = 1;
3888 sibling = w->next;
3889 s = XWINDOW (sibling);
3890 s->prev = Qnil;
3891 if (horflag)
3892 p->hchild = sibling;
3893 else
3894 p->vchild = sibling;
3895 }
3896 else
3897 /* Get SIBLING above (on the left of) WINDOW. */
3898 {
3899 sibling = w->prev;
3900 s = XWINDOW (sibling);
3901 s->next = w->next;
3902 if (!NILP (s->next))
3903 XWINDOW (s->next)->prev = sibling;
3904 }
3905
3906 if (window_resize_check (r, horflag)
3907 && EQ (r->new_total, (horflag ? r->total_cols : r->total_lines)))
3908 /* We can delete WINDOW now. */
3909 {
3910
3911 /* Block input. */
3912 BLOCK_INPUT;
3913 window_resize_apply (p, horflag);
3914
3915 /* If this window is referred to by the dpyinfo's mouse
3916 highlight, invalidate that slot to be safe (Bug#9904). */
3917 if (!FRAME_INITIAL_P (f))
3918 {
3919 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
3920
3921 if (EQ (hlinfo->mouse_face_window, window))
3922 hlinfo->mouse_face_window = Qnil;
3923 }
3924
3925 windows_or_buffers_changed++;
3926 Vwindow_list = Qnil;
3927 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
3928
3929 w->next = Qnil; /* Don't delete w->next too. */
3930 free_window_matrices (w);
3931
3932 if (!NILP (w->vchild))
3933 {
3934 delete_all_child_windows (w->vchild);
3935 w->vchild = Qnil;
3936 }
3937 else if (!NILP (w->hchild))
3938 {
3939 delete_all_child_windows (w->hchild);
3940 w->hchild = Qnil;
3941 }
3942 else if (!NILP (w->buffer))
3943 {
3944 unshow_buffer (w);
3945 unchain_marker (XMARKER (w->pointm));
3946 unchain_marker (XMARKER (w->start));
3947 w->buffer = Qnil;
3948 }
3949
3950 if (NILP (s->prev) && NILP (s->next))
3951 /* A matrjoshka where SIBLING has become the only child of
3952 PARENT. */
3953 {
3954 /* Put SIBLING into PARENT's place. */
3955 replace_window (parent, sibling, 0);
3956 /* Have SIBLING inherit the following three slot values from
3957 PARENT (the combination_limit slot is not inherited). */
3958 s->normal_cols = p->normal_cols;
3959 s->normal_lines = p->normal_lines;
3960 /* Mark PARENT as deleted. */
3961 p->vchild = p->hchild = Qnil;
3962 /* Try to merge SIBLING into its new parent. */
3963 recombine_windows (sibling);
3964 }
3965
3966 adjust_glyphs (f);
3967
3968 if (!WINDOW_LIVE_P (FRAME_SELECTED_WINDOW (f)))
3969 /* We deleted the frame's selected window. */
3970 {
3971 /* Use the frame's first window as fallback ... */
3972 Lisp_Object new_selected_window = Fframe_first_window (frame);
3973 /* ... but preferably use its most recently used window. */
3974 Lisp_Object mru_window;
3975
3976 /* `get-mru-window' might fail for some reason so play it safe
3977 - promote the first window _without recording it_ first. */
3978 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
3979 Fselect_window (new_selected_window, Qt);
3980 else
3981 FRAME_SELECTED_WINDOW (f) = new_selected_window;
3982
3983 UNBLOCK_INPUT;
3984
3985 /* Now look whether `get-mru-window' gets us something. */
3986 mru_window = call1 (Qget_mru_window, frame);
3987 if (WINDOW_LIVE_P (mru_window)
3988 && EQ (XWINDOW (mru_window)->frame, frame))
3989 new_selected_window = mru_window;
3990
3991 /* If all ended up well, we now promote the mru window. */
3992 if (EQ (FRAME_SELECTED_WINDOW (f), selected_window))
3993 Fselect_window (new_selected_window, Qnil);
3994 else
3995 FRAME_SELECTED_WINDOW (f) = new_selected_window;
3996 }
3997 else
3998 UNBLOCK_INPUT;
3999
4000 /* Must be run by the caller:
4001 run_window_configuration_change_hook (f); */
4002 }
4003 else
4004 /* We failed: Relink WINDOW into window tree. */
4005 {
4006 if (before_sibling)
4007 {
4008 s->prev = window;
4009 if (horflag)
4010 p->hchild = window;
4011 else
4012 p->vchild = window;
4013 }
4014 else
4015 {
4016 s->next = window;
4017 if (!NILP (w->next))
4018 XWINDOW (w->next)->prev = window;
4019 }
4020 error ("Deletion failed");
4021 }
4022
4023 return Qnil;
4024 }
4025 \f
4026 /***********************************************************************
4027 Resizing Mini-Windows
4028 ***********************************************************************/
4029
4030 /* Grow mini-window W by DELTA lines, DELTA >= 0, or as much as we
4031 can. */
4032 void
4033 grow_mini_window (struct window *w, int delta)
4034 {
4035 struct frame *f = XFRAME (w->frame);
4036 struct window *r;
4037 Lisp_Object root, value;
4038
4039 xassert (MINI_WINDOW_P (w));
4040 xassert (delta >= 0);
4041
4042 root = FRAME_ROOT_WINDOW (f);
4043 r = XWINDOW (root);
4044 value = call2 (Qwindow_resize_root_window_vertically,
4045 root, make_number (- delta));
4046 if (INTEGERP (value) && window_resize_check (r, 0))
4047 {
4048 BLOCK_INPUT;
4049 window_resize_apply (r, 0);
4050
4051 /* Grow the mini-window. */
4052 XSETFASTINT (w->top_line, XFASTINT (r->top_line) + XFASTINT (r->total_lines));
4053 XSETFASTINT (w->total_lines, XFASTINT (w->total_lines) - XINT (value));
4054 XSETFASTINT (w->last_modified, 0);
4055 XSETFASTINT (w->last_overlay_modified, 0);
4056
4057 adjust_glyphs (f);
4058 UNBLOCK_INPUT;
4059 }
4060 }
4061
4062
4063 /* Shrink mini-window W. */
4064 void
4065 shrink_mini_window (struct window *w)
4066 {
4067 struct frame *f = XFRAME (w->frame);
4068 struct window *r;
4069 Lisp_Object root, value;
4070 EMACS_INT size;
4071
4072 xassert (MINI_WINDOW_P (w));
4073
4074 size = XINT (w->total_lines);
4075 if (size > 1)
4076 {
4077 root = FRAME_ROOT_WINDOW (f);
4078 r = XWINDOW (root);
4079 value = call2 (Qwindow_resize_root_window_vertically,
4080 root, make_number (size - 1));
4081 if (INTEGERP (value) && window_resize_check (r, 0))
4082 {
4083 BLOCK_INPUT;
4084 window_resize_apply (r, 0);
4085
4086 /* Shrink the mini-window. */
4087 XSETFASTINT (w->top_line, XFASTINT (r->top_line) + XFASTINT (r->total_lines));
4088 XSETFASTINT (w->total_lines, 1);
4089
4090 XSETFASTINT (w->last_modified, 0);
4091 XSETFASTINT (w->last_overlay_modified, 0);
4092
4093 adjust_glyphs (f);
4094 UNBLOCK_INPUT;
4095 }
4096 /* If the above failed for whatever strange reason we must make a
4097 one window frame here. The same routine will be needed when
4098 shrinking the frame (and probably when making the initial
4099 *scratch* window). For the moment leave things as they are. */
4100 }
4101 }
4102
4103 DEFUN ("resize-mini-window-internal", Fresize_mini_window_internal, Sresize_mini_window_internal, 1, 1, 0,
4104 doc: /* Resize minibuffer window WINDOW. */)
4105 (Lisp_Object window)
4106 {
4107 struct window *w = XWINDOW (window);
4108 struct window *r;
4109 struct frame *f;
4110 int height;
4111
4112 CHECK_WINDOW (window);
4113 f = XFRAME (w->frame);
4114
4115 if (!EQ (FRAME_MINIBUF_WINDOW (XFRAME (w->frame)), window))
4116 error ("Not a valid minibuffer window");
4117 else if (FRAME_MINIBUF_ONLY_P (f))
4118 error ("Cannot resize a minibuffer-only frame");
4119
4120 r = XWINDOW (FRAME_ROOT_WINDOW (f));
4121 height = XINT (r->total_lines) + XINT (w->total_lines);
4122 if (window_resize_check (r, 0)
4123 && XINT (w->new_total) > 0
4124 && height == XINT (r->new_total) + XINT (w->new_total))
4125 {
4126 BLOCK_INPUT;
4127 window_resize_apply (r, 0);
4128
4129 w->total_lines = w->new_total;
4130 XSETFASTINT (w->top_line, XINT (r->top_line) + XINT (r->total_lines));
4131
4132 windows_or_buffers_changed++;
4133 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
4134 adjust_glyphs (f);
4135 UNBLOCK_INPUT;
4136
4137 run_window_configuration_change_hook (f);
4138 return Qt;
4139 }
4140 else error ("Failed to resize minibuffer window");
4141 }
4142 \f
4143 /* Mark window cursors off for all windows in the window tree rooted
4144 at W by setting their phys_cursor_on_p flag to zero. Called from
4145 xterm.c, e.g. when a frame is cleared and thereby all cursors on
4146 the frame are cleared. */
4147
4148 void
4149 mark_window_cursors_off (struct window *w)
4150 {
4151 while (w)
4152 {
4153 if (!NILP (w->hchild))
4154 mark_window_cursors_off (XWINDOW (w->hchild));
4155 else if (!NILP (w->vchild))
4156 mark_window_cursors_off (XWINDOW (w->vchild));
4157 else
4158 w->phys_cursor_on_p = 0;
4159
4160 w = NILP (w->next) ? 0 : XWINDOW (w->next);
4161 }
4162 }
4163
4164
4165 /* Return number of lines of text (not counting mode lines) in W. */
4166
4167 int
4168 window_internal_height (struct window *w)
4169 {
4170 int ht = XFASTINT (w->total_lines);
4171
4172 if (!MINI_WINDOW_P (w))
4173 {
4174 if (!NILP (w->parent)
4175 || !NILP (w->vchild)
4176 || !NILP (w->hchild)
4177 || !NILP (w->next)
4178 || !NILP (w->prev)
4179 || WINDOW_WANTS_MODELINE_P (w))
4180 --ht;
4181
4182 if (WINDOW_WANTS_HEADER_LINE_P (w))
4183 --ht;
4184 }
4185
4186 return ht;
4187 }
4188 \f
4189 /************************************************************************
4190 Window Scrolling
4191 ***********************************************************************/
4192
4193 /* Scroll contents of window WINDOW up. If WHOLE is non-zero, scroll
4194 N screen-fulls, which is defined as the height of the window minus
4195 next_screen_context_lines. If WHOLE is zero, scroll up N lines
4196 instead. Negative values of N mean scroll down. NOERROR non-zero
4197 means don't signal an error if we try to move over BEGV or ZV,
4198 respectively. */
4199
4200 static void
4201 window_scroll (Lisp_Object window, EMACS_INT n, int whole, int noerror)
4202 {
4203 immediate_quit = 1;
4204 n = clip_to_bounds (INT_MIN, n, INT_MAX);
4205
4206 /* If we must, use the pixel-based version which is much slower than
4207 the line-based one but can handle varying line heights. */
4208 if (FRAME_WINDOW_P (XFRAME (XWINDOW (window)->frame)))
4209 window_scroll_pixel_based (window, n, whole, noerror);
4210 else
4211 window_scroll_line_based (window, n, whole, noerror);
4212
4213 immediate_quit = 0;
4214 }
4215
4216
4217 /* Implementation of window_scroll that works based on pixel line
4218 heights. See the comment of window_scroll for parameter
4219 descriptions. */
4220
4221 static void
4222 window_scroll_pixel_based (Lisp_Object window, int n, int whole, int noerror)
4223 {
4224 struct it it;
4225 struct window *w = XWINDOW (window);
4226 struct text_pos start;
4227 int this_scroll_margin;
4228 /* True if we fiddled the window vscroll field without really scrolling. */
4229 int vscrolled = 0;
4230 int x, y, rtop, rbot, rowh, vpos;
4231 void *itdata = NULL;
4232
4233 SET_TEXT_POS_FROM_MARKER (start, w->start);
4234 /* Scrolling a minibuffer window via scroll bar when the echo area
4235 shows long text sometimes resets the minibuffer contents behind
4236 our backs. */
4237 if (CHARPOS (start) > ZV)
4238 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4239
4240 /* If PT is not visible in WINDOW, move back one half of
4241 the screen. Allow PT to be partially visible, otherwise
4242 something like (scroll-down 1) with PT in the line before
4243 the partially visible one would recenter. */
4244
4245 if (!pos_visible_p (w, PT, &x, &y, &rtop, &rbot, &rowh, &vpos))
4246 {
4247 itdata = bidi_shelve_cache ();
4248 /* Move backward half the height of the window. Performance note:
4249 vmotion used here is about 10% faster, but would give wrong
4250 results for variable height lines. */
4251 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4252 it.current_y = it.last_visible_y;
4253 move_it_vertically_backward (&it, window_box_height (w) / 2);
4254
4255 /* The function move_iterator_vertically may move over more than
4256 the specified y-distance. If it->w is small, e.g. a
4257 mini-buffer window, we may end up in front of the window's
4258 display area. This is the case when Start displaying at the
4259 start of the line containing PT in this case. */
4260 if (it.current_y <= 0)
4261 {
4262 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
4263 move_it_vertically_backward (&it, 0);
4264 it.current_y = 0;
4265 }
4266
4267 start = it.current.pos;
4268 bidi_unshelve_cache (itdata, 0);
4269 }
4270 else if (auto_window_vscroll_p)
4271 {
4272 if (rtop || rbot) /* partially visible */
4273 {
4274 int px;
4275 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4276 if (whole)
4277 dy = max ((window_box_height (w)
4278 - next_screen_context_lines * dy),
4279 dy);
4280 dy *= n;
4281
4282 if (n < 0)
4283 {
4284 /* Only vscroll backwards if already vscrolled forwards. */
4285 if (w->vscroll < 0 && rtop > 0)
4286 {
4287 px = max (0, -w->vscroll - min (rtop, -dy));
4288 Fset_window_vscroll (window, make_number (px), Qt);
4289 return;
4290 }
4291 }
4292 if (n > 0)
4293 {
4294 /* Do vscroll if already vscrolled or only display line. */
4295 if (rbot > 0 && (w->vscroll < 0 || vpos == 0))
4296 {
4297 px = max (0, -w->vscroll + min (rbot, dy));
4298 Fset_window_vscroll (window, make_number (px), Qt);
4299 return;
4300 }
4301
4302 /* Maybe modify window start instead of scrolling. */
4303 if (rbot > 0 || w->vscroll < 0)
4304 {
4305 ptrdiff_t spos;
4306
4307 Fset_window_vscroll (window, make_number (0), Qt);
4308 /* If there are other text lines above the current row,
4309 move window start to current row. Else to next row. */
4310 if (rbot > 0)
4311 spos = XINT (Fline_beginning_position (Qnil));
4312 else
4313 spos = min (XINT (Fline_end_position (Qnil)) + 1, ZV);
4314 set_marker_restricted (w->start, make_number (spos),
4315 w->buffer);
4316 w->start_at_line_beg = 1;
4317 w->update_mode_line = 1;
4318 XSETFASTINT (w->last_modified, 0);
4319 XSETFASTINT (w->last_overlay_modified, 0);
4320 /* Set force_start so that redisplay_window will run the
4321 window-scroll-functions. */
4322 w->force_start = 1;
4323 return;
4324 }
4325 }
4326 }
4327 /* Cancel previous vscroll. */
4328 Fset_window_vscroll (window, make_number (0), Qt);
4329 }
4330
4331 itdata = bidi_shelve_cache ();
4332 /* If scroll_preserve_screen_position is non-nil, we try to set
4333 point in the same window line as it is now, so get that line. */
4334 if (!NILP (Vscroll_preserve_screen_position))
4335 {
4336 /* We preserve the goal pixel coordinate across consecutive
4337 calls to scroll-up, scroll-down and other commands that
4338 have the `scroll-command' property. This avoids the
4339 possibility of point becoming "stuck" on a tall line when
4340 scrolling by one line. */
4341 if (window_scroll_pixel_based_preserve_y < 0
4342 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4343 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4344 {
4345 start_display (&it, w, start);
4346 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4347 window_scroll_pixel_based_preserve_y = it.current_y;
4348 window_scroll_pixel_based_preserve_x = it.current_x;
4349 }
4350 }
4351 else
4352 window_scroll_pixel_based_preserve_y
4353 = window_scroll_pixel_based_preserve_x = -1;
4354
4355 /* Move iterator it from start the specified distance forward or
4356 backward. The result is the new window start. */
4357 start_display (&it, w, start);
4358 if (whole)
4359 {
4360 ptrdiff_t start_pos = IT_CHARPOS (it);
4361 int dy = WINDOW_FRAME_LINE_HEIGHT (w);
4362 dy = max ((window_box_height (w)
4363 - next_screen_context_lines * dy),
4364 dy) * n;
4365
4366 /* Note that move_it_vertically always moves the iterator to the
4367 start of a line. So, if the last line doesn't have a newline,
4368 we would end up at the start of the line ending at ZV. */
4369 if (dy <= 0)
4370 {
4371 move_it_vertically_backward (&it, -dy);
4372 /* Ensure we actually do move, e.g. in case we are currently
4373 looking at an image that is taller that the window height. */
4374 while (start_pos == IT_CHARPOS (it)
4375 && start_pos > BEGV)
4376 move_it_by_lines (&it, -1);
4377 }
4378 else if (dy > 0)
4379 {
4380 move_it_to (&it, ZV, -1, it.current_y + dy, -1,
4381 MOVE_TO_POS | MOVE_TO_Y);
4382 /* Ensure we actually do move, e.g. in case we are currently
4383 looking at an image that is taller that the window height. */
4384 while (start_pos == IT_CHARPOS (it)
4385 && start_pos < ZV)
4386 move_it_by_lines (&it, 1);
4387 }
4388 }
4389 else
4390 move_it_by_lines (&it, n);
4391
4392 /* We failed if we find ZV is already on the screen (scrolling up,
4393 means there's nothing past the end), or if we can't start any
4394 earlier (scrolling down, means there's nothing past the top). */
4395 if ((n > 0 && IT_CHARPOS (it) == ZV)
4396 || (n < 0 && IT_CHARPOS (it) == CHARPOS (start)))
4397 {
4398 if (IT_CHARPOS (it) == ZV)
4399 {
4400 if (it.current_y < it.last_visible_y
4401 && (it.current_y + it.max_ascent + it.max_descent
4402 > it.last_visible_y))
4403 {
4404 /* The last line was only partially visible, make it fully
4405 visible. */
4406 w->vscroll = (it.last_visible_y
4407 - it.current_y + it.max_ascent + it.max_descent);
4408 adjust_glyphs (it.f);
4409 }
4410 else
4411 {
4412 bidi_unshelve_cache (itdata, 0);
4413 if (noerror)
4414 return;
4415 else if (n < 0) /* could happen with empty buffers */
4416 xsignal0 (Qbeginning_of_buffer);
4417 else
4418 xsignal0 (Qend_of_buffer);
4419 }
4420 }
4421 else
4422 {
4423 if (w->vscroll != 0)
4424 /* The first line was only partially visible, make it fully
4425 visible. */
4426 w->vscroll = 0;
4427 else
4428 {
4429 bidi_unshelve_cache (itdata, 0);
4430 if (noerror)
4431 return;
4432 else
4433 xsignal0 (Qbeginning_of_buffer);
4434 }
4435 }
4436
4437 /* If control gets here, then we vscrolled. */
4438
4439 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
4440
4441 /* Don't try to change the window start below. */
4442 vscrolled = 1;
4443 }
4444
4445 if (! vscrolled)
4446 {
4447 ptrdiff_t pos = IT_CHARPOS (it);
4448 ptrdiff_t bytepos;
4449
4450 /* If in the middle of a multi-glyph character move forward to
4451 the next character. */
4452 if (in_display_vector_p (&it))
4453 {
4454 ++pos;
4455 move_it_to (&it, pos, -1, -1, -1, MOVE_TO_POS);
4456 }
4457
4458 /* Set the window start, and set up the window for redisplay. */
4459 set_marker_restricted (w->start, make_number (pos),
4460 w->buffer);
4461 bytepos = XMARKER (w->start)->bytepos;
4462 w->start_at_line_beg = (pos == BEGV || FETCH_BYTE (bytepos - 1) == '\n');
4463 w->update_mode_line = 1;
4464 XSETFASTINT (w->last_modified, 0);
4465 XSETFASTINT (w->last_overlay_modified, 0);
4466 /* Set force_start so that redisplay_window will run the
4467 window-scroll-functions. */
4468 w->force_start = 1;
4469 }
4470
4471 /* The rest of this function uses current_y in a nonstandard way,
4472 not including the height of the header line if any. */
4473 it.current_y = it.vpos = 0;
4474
4475 /* Move PT out of scroll margins.
4476 This code wants current_y to be zero at the window start position
4477 even if there is a header line. */
4478 this_scroll_margin = max (0, scroll_margin);
4479 this_scroll_margin = min (this_scroll_margin, XFASTINT (w->total_lines) / 4);
4480 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
4481
4482 if (n > 0)
4483 {
4484 /* We moved the window start towards ZV, so PT may be now
4485 in the scroll margin at the top. */
4486 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
4487 if (IT_CHARPOS (it) == PT && it.current_y >= this_scroll_margin
4488 && (NILP (Vscroll_preserve_screen_position)
4489 || EQ (Vscroll_preserve_screen_position, Qt)))
4490 /* We found PT at a legitimate height. Leave it alone. */
4491 ;
4492 else if (window_scroll_pixel_based_preserve_y >= 0)
4493 {
4494 /* If we have a header line, take account of it.
4495 This is necessary because we set it.current_y to 0, above. */
4496 move_it_to (&it, -1,
4497 window_scroll_pixel_based_preserve_x,
4498 window_scroll_pixel_based_preserve_y
4499 - (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0 ),
4500 -1, MOVE_TO_Y | MOVE_TO_X);
4501 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4502 }
4503 else
4504 {
4505 while (it.current_y < this_scroll_margin)
4506 {
4507 int prev = it.current_y;
4508 move_it_by_lines (&it, 1);
4509 if (prev == it.current_y)
4510 break;
4511 }
4512 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4513 }
4514 }
4515 else if (n < 0)
4516 {
4517 ptrdiff_t charpos, bytepos;
4518 int partial_p;
4519
4520 /* Save our position, for the
4521 window_scroll_pixel_based_preserve_y case. */
4522 charpos = IT_CHARPOS (it);
4523 bytepos = IT_BYTEPOS (it);
4524
4525 /* We moved the window start towards BEGV, so PT may be now
4526 in the scroll margin at the bottom. */
4527 move_it_to (&it, PT, -1,
4528 (it.last_visible_y - CURRENT_HEADER_LINE_HEIGHT (w)
4529 - this_scroll_margin - 1),
4530 -1,
4531 MOVE_TO_POS | MOVE_TO_Y);
4532
4533 /* Save our position, in case it's correct. */
4534 charpos = IT_CHARPOS (it);
4535 bytepos = IT_BYTEPOS (it);
4536
4537 /* See if point is on a partially visible line at the end. */
4538 if (it.what == IT_EOB)
4539 partial_p = it.current_y + it.ascent + it.descent > it.last_visible_y;
4540 else
4541 {
4542 move_it_by_lines (&it, 1);
4543 partial_p = it.current_y > it.last_visible_y;
4544 }
4545
4546 if (charpos == PT && !partial_p
4547 && (NILP (Vscroll_preserve_screen_position)
4548 || EQ (Vscroll_preserve_screen_position, Qt)))
4549 /* We found PT before we found the display margin, so PT is ok. */
4550 ;
4551 else if (window_scroll_pixel_based_preserve_y >= 0)
4552 {
4553 SET_TEXT_POS_FROM_MARKER (start, w->start);
4554 start_display (&it, w, start);
4555 /* It would be wrong to subtract CURRENT_HEADER_LINE_HEIGHT
4556 here because we called start_display again and did not
4557 alter it.current_y this time. */
4558 move_it_to (&it, -1, window_scroll_pixel_based_preserve_x,
4559 window_scroll_pixel_based_preserve_y, -1,
4560 MOVE_TO_Y | MOVE_TO_X);
4561 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4562 }
4563 else
4564 {
4565 if (partial_p)
4566 /* The last line was only partially visible, so back up two
4567 lines to make sure we're on a fully visible line. */
4568 {
4569 move_it_by_lines (&it, -2);
4570 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
4571 }
4572 else
4573 /* No, the position we saved is OK, so use it. */
4574 SET_PT_BOTH (charpos, bytepos);
4575 }
4576 }
4577 bidi_unshelve_cache (itdata, 0);
4578 }
4579
4580
4581 /* Implementation of window_scroll that works based on screen lines.
4582 See the comment of window_scroll for parameter descriptions. */
4583
4584 static void
4585 window_scroll_line_based (Lisp_Object window, int n, int whole, int noerror)
4586 {
4587 register struct window *w = XWINDOW (window);
4588 /* Fvertical_motion enters redisplay, which can trigger
4589 fontification, which in turn can modify buffer text (e.g., if the
4590 fontification functions replace escape sequences with faces, as
4591 in `grep-mode-font-lock-keywords'). So we use a marker to record
4592 the old point position, to prevent crashes in SET_PT_BOTH. */
4593 Lisp_Object opoint_marker = Fpoint_marker ();
4594 register ptrdiff_t pos, pos_byte;
4595 register int ht = window_internal_height (w);
4596 register Lisp_Object tem;
4597 int lose;
4598 Lisp_Object bolp;
4599 ptrdiff_t startpos;
4600 Lisp_Object original_pos = Qnil;
4601
4602 /* If scrolling screen-fulls, compute the number of lines to
4603 scroll from the window's height. */
4604 if (whole)
4605 n *= max (1, ht - next_screen_context_lines);
4606
4607 startpos = marker_position (w->start);
4608
4609 if (!NILP (Vscroll_preserve_screen_position))
4610 {
4611 if (window_scroll_preserve_vpos <= 0
4612 || !SYMBOLP (KVAR (current_kboard, Vlast_command))
4613 || NILP (Fget (KVAR (current_kboard, Vlast_command), Qscroll_command)))
4614 {
4615 struct position posit
4616 = *compute_motion (startpos, 0, 0, 0,
4617 PT, ht, 0,
4618 -1, XINT (w->hscroll),
4619 0, w);
4620 window_scroll_preserve_vpos = posit.vpos;
4621 window_scroll_preserve_hpos = posit.hpos + XINT (w->hscroll);
4622 }
4623
4624 original_pos = Fcons (make_number (window_scroll_preserve_hpos),
4625 make_number (window_scroll_preserve_vpos));
4626 }
4627
4628 XSETFASTINT (tem, PT);
4629 tem = Fpos_visible_in_window_p (tem, window, Qnil);
4630
4631 if (NILP (tem))
4632 {
4633 Fvertical_motion (make_number (- (ht / 2)), window);
4634 startpos = PT;
4635 }
4636
4637 SET_PT (startpos);
4638 lose = n < 0 && PT == BEGV;
4639 Fvertical_motion (make_number (n), window);
4640 pos = PT;
4641 pos_byte = PT_BYTE;
4642 bolp = Fbolp ();
4643 SET_PT_BOTH (marker_position (opoint_marker),
4644 marker_byte_position (opoint_marker));
4645
4646 if (lose)
4647 {
4648 if (noerror)
4649 return;
4650 else
4651 xsignal0 (Qbeginning_of_buffer);
4652 }
4653
4654 if (pos < ZV)
4655 {
4656 /* Don't use a scroll margin that is negative or too large. */
4657 int this_scroll_margin =
4658 max (0, min (scroll_margin, XINT (w->total_lines) / 4));
4659
4660 set_marker_restricted_both (w->start, w->buffer, pos, pos_byte);
4661 w->start_at_line_beg = !NILP (bolp);
4662 w->update_mode_line = 1;
4663 XSETFASTINT (w->last_modified, 0);
4664 XSETFASTINT (w->last_overlay_modified, 0);
4665 /* Set force_start so that redisplay_window will run
4666 the window-scroll-functions. */
4667 w->force_start = 1;
4668
4669 if (!NILP (Vscroll_preserve_screen_position)
4670 && (whole || !EQ (Vscroll_preserve_screen_position, Qt)))
4671 {
4672 SET_PT_BOTH (pos, pos_byte);
4673 Fvertical_motion (original_pos, window);
4674 }
4675 /* If we scrolled forward, put point enough lines down
4676 that it is outside the scroll margin. */
4677 else if (n > 0)
4678 {
4679 int top_margin;
4680
4681 if (this_scroll_margin > 0)
4682 {
4683 SET_PT_BOTH (pos, pos_byte);
4684 Fvertical_motion (make_number (this_scroll_margin), window);
4685 top_margin = PT;
4686 }
4687 else
4688 top_margin = pos;
4689
4690 if (top_margin <= marker_position (opoint_marker))
4691 SET_PT_BOTH (marker_position (opoint_marker),
4692 marker_byte_position (opoint_marker));
4693 else if (!NILP (Vscroll_preserve_screen_position))
4694 {
4695 SET_PT_BOTH (pos, pos_byte);
4696 Fvertical_motion (original_pos, window);
4697 }
4698 else
4699 SET_PT (top_margin);
4700 }
4701 else if (n < 0)
4702 {
4703 int bottom_margin;
4704
4705 /* If we scrolled backward, put point near the end of the window
4706 but not within the scroll margin. */
4707 SET_PT_BOTH (pos, pos_byte);
4708 tem = Fvertical_motion (make_number (ht - this_scroll_margin), window);
4709 if (XFASTINT (tem) == ht - this_scroll_margin)
4710 bottom_margin = PT;
4711 else
4712 bottom_margin = PT + 1;
4713
4714 if (bottom_margin > marker_position (opoint_marker))
4715 SET_PT_BOTH (marker_position (opoint_marker),
4716 marker_byte_position (opoint_marker));
4717 else
4718 {
4719 if (!NILP (Vscroll_preserve_screen_position))
4720 {
4721 SET_PT_BOTH (pos, pos_byte);
4722 Fvertical_motion (original_pos, window);
4723 }
4724 else
4725 Fvertical_motion (make_number (-1), window);
4726 }
4727 }
4728 }
4729 else
4730 {
4731 if (noerror)
4732 return;
4733 else
4734 xsignal0 (Qend_of_buffer);
4735 }
4736 }
4737
4738
4739 /* Scroll selected_window up or down. If N is nil, scroll a
4740 screen-full which is defined as the height of the window minus
4741 next_screen_context_lines. If N is the symbol `-', scroll.
4742 DIRECTION may be 1 meaning to scroll down, or -1 meaning to scroll
4743 up. This is the guts of Fscroll_up and Fscroll_down. */
4744
4745 static void
4746 scroll_command (Lisp_Object n, int direction)
4747 {
4748 ptrdiff_t count = SPECPDL_INDEX ();
4749
4750 xassert (eabs (direction) == 1);
4751
4752 /* If selected window's buffer isn't current, make it current for
4753 the moment. But don't screw up if window_scroll gets an error. */
4754 if (XBUFFER (XWINDOW (selected_window)->buffer) != current_buffer)
4755 {
4756 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4757 Fset_buffer (XWINDOW (selected_window)->buffer);
4758
4759 /* Make redisplay consider other windows than just selected_window. */
4760 ++windows_or_buffers_changed;
4761 }
4762
4763 if (NILP (n))
4764 window_scroll (selected_window, direction, 1, 0);
4765 else if (EQ (n, Qminus))
4766 window_scroll (selected_window, -direction, 1, 0);
4767 else
4768 {
4769 n = Fprefix_numeric_value (n);
4770 window_scroll (selected_window, XINT (n) * direction, 0, 0);
4771 }
4772
4773 unbind_to (count, Qnil);
4774 }
4775
4776 DEFUN ("scroll-up", Fscroll_up, Sscroll_up, 0, 1, "^P",
4777 doc: /* Scroll text of selected window upward ARG lines.
4778 If ARG is omitted or nil, scroll upward by a near full screen.
4779 A near full screen is `next-screen-context-lines' less than a full screen.
4780 Negative ARG means scroll downward.
4781 If ARG is the atom `-', scroll downward by nearly full screen.
4782 When calling from a program, supply as argument a number, nil, or `-'. */)
4783 (Lisp_Object arg)
4784 {
4785 scroll_command (arg, 1);
4786 return Qnil;
4787 }
4788
4789 DEFUN ("scroll-down", Fscroll_down, Sscroll_down, 0, 1, "^P",
4790 doc: /* Scroll text of selected window down ARG lines.
4791 If ARG is omitted or nil, scroll down by a near full screen.
4792 A near full screen is `next-screen-context-lines' less than a full screen.
4793 Negative ARG means scroll upward.
4794 If ARG is the atom `-', scroll upward by nearly full screen.
4795 When calling from a program, supply as argument a number, nil, or `-'. */)
4796 (Lisp_Object arg)
4797 {
4798 scroll_command (arg, -1);
4799 return Qnil;
4800 }
4801 \f
4802 DEFUN ("other-window-for-scrolling", Fother_window_for_scrolling, Sother_window_for_scrolling, 0, 0, 0,
4803 doc: /* Return the other window for \"other window scroll\" commands.
4804 If `other-window-scroll-buffer' is non-nil, a window
4805 showing that buffer is used.
4806 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4807 specifies the window. This takes precedence over
4808 `other-window-scroll-buffer'. */)
4809 (void)
4810 {
4811 Lisp_Object window;
4812
4813 if (MINI_WINDOW_P (XWINDOW (selected_window))
4814 && !NILP (Vminibuf_scroll_window))
4815 window = Vminibuf_scroll_window;
4816 /* If buffer is specified, scroll that buffer. */
4817 else if (!NILP (Vother_window_scroll_buffer))
4818 {
4819 window = Fget_buffer_window (Vother_window_scroll_buffer, Qnil);
4820 if (NILP (window))
4821 window = display_buffer (Vother_window_scroll_buffer, Qt, Qnil);
4822 }
4823 else
4824 {
4825 /* Nothing specified; look for a neighboring window on the same
4826 frame. */
4827 window = Fnext_window (selected_window, Qnil, Qnil);
4828
4829 if (EQ (window, selected_window))
4830 /* That didn't get us anywhere; look for a window on another
4831 visible frame. */
4832 do
4833 window = Fnext_window (window, Qnil, Qt);
4834 while (! FRAME_VISIBLE_P (XFRAME (WINDOW_FRAME (XWINDOW (window))))
4835 && ! EQ (window, selected_window));
4836 }
4837
4838 CHECK_LIVE_WINDOW (window);
4839
4840 if (EQ (window, selected_window))
4841 error ("There is no other window");
4842
4843 return window;
4844 }
4845
4846 DEFUN ("scroll-other-window", Fscroll_other_window, Sscroll_other_window, 0, 1, "P",
4847 doc: /* Scroll next window upward ARG lines; or near full screen if no ARG.
4848 A near full screen is `next-screen-context-lines' less than a full screen.
4849 The next window is the one below the current one; or the one at the top
4850 if the current one is at the bottom. Negative ARG means scroll downward.
4851 If ARG is the atom `-', scroll downward by nearly full screen.
4852 When calling from a program, supply as argument a number, nil, or `-'.
4853
4854 If `other-window-scroll-buffer' is non-nil, scroll the window
4855 showing that buffer, popping the buffer up if necessary.
4856 If in the minibuffer, `minibuffer-scroll-window' if non-nil
4857 specifies the window to scroll. This takes precedence over
4858 `other-window-scroll-buffer'. */)
4859 (Lisp_Object arg)
4860 {
4861 Lisp_Object window;
4862 struct window *w;
4863 ptrdiff_t count = SPECPDL_INDEX ();
4864
4865 window = Fother_window_for_scrolling ();
4866 w = XWINDOW (window);
4867
4868 /* Don't screw up if window_scroll gets an error. */
4869 record_unwind_protect (save_excursion_restore, save_excursion_save ());
4870 ++windows_or_buffers_changed;
4871
4872 Fset_buffer (w->buffer);
4873 SET_PT (marker_position (w->pointm));
4874
4875 if (NILP (arg))
4876 window_scroll (window, 1, 1, 1);
4877 else if (EQ (arg, Qminus))
4878 window_scroll (window, -1, 1, 1);
4879 else
4880 {
4881 if (CONSP (arg))
4882 arg = Fcar (arg);
4883 CHECK_NUMBER (arg);
4884 window_scroll (window, XINT (arg), 0, 1);
4885 }
4886
4887 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
4888 unbind_to (count, Qnil);
4889
4890 return Qnil;
4891 }
4892 \f
4893 DEFUN ("scroll-left", Fscroll_left, Sscroll_left, 0, 2, "^P\np",
4894 doc: /* Scroll selected window display ARG columns left.
4895 Default for ARG is window width minus 2.
4896 Value is the total amount of leftward horizontal scrolling in
4897 effect after the change.
4898 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4899 lower bound for automatic scrolling, i.e. automatic scrolling
4900 will not scroll a window to a column less than the value returned
4901 by this function. This happens in an interactive call. */)
4902 (register Lisp_Object arg, Lisp_Object set_minimum)
4903 {
4904 Lisp_Object result;
4905 EMACS_INT hscroll;
4906 struct window *w = XWINDOW (selected_window);
4907
4908 if (NILP (arg))
4909 XSETFASTINT (arg, window_body_cols (w) - 2);
4910 else
4911 arg = Fprefix_numeric_value (arg);
4912
4913 hscroll = XINT (w->hscroll) + XINT (arg);
4914 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4915
4916 if (!NILP (set_minimum))
4917 w->min_hscroll = w->hscroll;
4918
4919 return result;
4920 }
4921
4922 DEFUN ("scroll-right", Fscroll_right, Sscroll_right, 0, 2, "^P\np",
4923 doc: /* Scroll selected window display ARG columns right.
4924 Default for ARG is window width minus 2.
4925 Value is the total amount of leftward horizontal scrolling in
4926 effect after the change.
4927 If SET-MINIMUM is non-nil, the new scroll amount becomes the
4928 lower bound for automatic scrolling, i.e. automatic scrolling
4929 will not scroll a window to a column less than the value returned
4930 by this function. This happens in an interactive call. */)
4931 (register Lisp_Object arg, Lisp_Object set_minimum)
4932 {
4933 Lisp_Object result;
4934 EMACS_INT hscroll;
4935 struct window *w = XWINDOW (selected_window);
4936
4937 if (NILP (arg))
4938 XSETFASTINT (arg, window_body_cols (w) - 2);
4939 else
4940 arg = Fprefix_numeric_value (arg);
4941
4942 hscroll = XINT (w->hscroll) - XINT (arg);
4943 result = Fset_window_hscroll (selected_window, make_number (hscroll));
4944
4945 if (!NILP (set_minimum))
4946 w->min_hscroll = w->hscroll;
4947
4948 return result;
4949 }
4950
4951 DEFUN ("minibuffer-selected-window", Fminibuffer_selected_window, Sminibuffer_selected_window, 0, 0, 0,
4952 doc: /* Return the window which was selected when entering the minibuffer.
4953 Returns nil, if selected window is not a minibuffer window. */)
4954 (void)
4955 {
4956 if (minibuf_level > 0
4957 && MINI_WINDOW_P (XWINDOW (selected_window))
4958 && WINDOW_LIVE_P (minibuf_selected_window))
4959 return minibuf_selected_window;
4960
4961 return Qnil;
4962 }
4963
4964 /* Value is the number of lines actually displayed in window W,
4965 as opposed to its height. */
4966
4967 static int
4968 displayed_window_lines (struct window *w)
4969 {
4970 struct it it;
4971 struct text_pos start;
4972 int height = window_box_height (w);
4973 struct buffer *old_buffer;
4974 int bottom_y;
4975 void *itdata = NULL;
4976
4977 if (XBUFFER (w->buffer) != current_buffer)
4978 {
4979 old_buffer = current_buffer;
4980 set_buffer_internal (XBUFFER (w->buffer));
4981 }
4982 else
4983 old_buffer = NULL;
4984
4985 /* In case W->start is out of the accessible range, do something
4986 reasonable. This happens in Info mode when Info-scroll-down
4987 calls (recenter -1) while W->start is 1. */
4988 if (XMARKER (w->start)->charpos < BEGV)
4989 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
4990 else if (XMARKER (w->start)->charpos > ZV)
4991 SET_TEXT_POS (start, ZV, ZV_BYTE);
4992 else
4993 SET_TEXT_POS_FROM_MARKER (start, w->start);
4994
4995 itdata = bidi_shelve_cache ();
4996 start_display (&it, w, start);
4997 move_it_vertically (&it, height);
4998 bottom_y = line_bottom_y (&it);
4999 bidi_unshelve_cache (itdata, 0);
5000
5001 /* rms: On a non-window display,
5002 the value of it.vpos at the bottom of the screen
5003 seems to be 1 larger than window_box_height (w).
5004 This kludge fixes a bug whereby (move-to-window-line -1)
5005 when ZV is on the last screen line
5006 moves to the previous screen line instead of the last one. */
5007 if (! FRAME_WINDOW_P (XFRAME (w->frame)))
5008 height++;
5009
5010 /* Add in empty lines at the bottom of the window. */
5011 if (bottom_y < height)
5012 {
5013 int uy = FRAME_LINE_HEIGHT (it.f);
5014 it.vpos += (height - bottom_y + uy - 1) / uy;
5015 }
5016
5017 if (old_buffer)
5018 set_buffer_internal (old_buffer);
5019
5020 return it.vpos;
5021 }
5022
5023
5024 DEFUN ("recenter", Frecenter, Srecenter, 0, 1, "P",
5025 doc: /* Center point in selected window and maybe redisplay frame.
5026 With prefix argument ARG, recenter putting point on screen line ARG
5027 relative to the selected window. If ARG is negative, it counts up from the
5028 bottom of the window. (ARG should be less than the height of the window.)
5029
5030 If ARG is omitted or nil, then recenter with point on the middle line of
5031 the selected window; if the variable `recenter-redisplay' is non-nil,
5032 also erase the entire frame and redraw it (when `auto-resize-tool-bars'
5033 is set to `grow-only', this resets the tool-bar's height to the minimum
5034 height needed); if `recenter-redisplay' has the special value `tty',
5035 then only tty frames are redrawn.
5036
5037 Just C-u as prefix means put point in the center of the window
5038 and redisplay normally--don't erase and redraw the frame. */)
5039 (register Lisp_Object arg)
5040 {
5041 struct window *w = XWINDOW (selected_window);
5042 struct buffer *buf = XBUFFER (w->buffer);
5043 struct buffer *obuf = current_buffer;
5044 int center_p = 0;
5045 ptrdiff_t charpos, bytepos;
5046 EMACS_INT iarg IF_LINT (= 0);
5047 int this_scroll_margin;
5048
5049 /* If redisplay is suppressed due to an error, try again. */
5050 obuf->display_error_modiff = 0;
5051
5052 if (NILP (arg))
5053 {
5054 if (!NILP (Vrecenter_redisplay)
5055 && (!EQ (Vrecenter_redisplay, Qtty)
5056 || !NILP (Ftty_type (selected_frame))))
5057 {
5058 ptrdiff_t i;
5059
5060 /* Invalidate pixel data calculated for all compositions. */
5061 for (i = 0; i < n_compositions; i++)
5062 composition_table[i]->font = NULL;
5063
5064 WINDOW_XFRAME (w)->minimize_tool_bar_window_p = 1;
5065
5066 Fredraw_frame (WINDOW_FRAME (w));
5067 SET_FRAME_GARBAGED (WINDOW_XFRAME (w));
5068 }
5069
5070 center_p = 1;
5071 }
5072 else if (CONSP (arg)) /* Just C-u. */
5073 center_p = 1;
5074 else
5075 {
5076 arg = Fprefix_numeric_value (arg);
5077 CHECK_NUMBER (arg);
5078 iarg = XINT (arg);
5079 }
5080
5081 set_buffer_internal (buf);
5082
5083 /* Do this after making BUF current
5084 in case scroll_margin is buffer-local. */
5085 this_scroll_margin =
5086 max (0, min (scroll_margin, XFASTINT (w->total_lines) / 4));
5087
5088 /* Handle centering on a graphical frame specially. Such frames can
5089 have variable-height lines and centering point on the basis of
5090 line counts would lead to strange effects. */
5091 if (FRAME_WINDOW_P (XFRAME (w->frame)))
5092 {
5093 if (center_p)
5094 {
5095 struct it it;
5096 struct text_pos pt;
5097 void *itdata = bidi_shelve_cache ();
5098
5099 SET_TEXT_POS (pt, PT, PT_BYTE);
5100 start_display (&it, w, pt);
5101 move_it_vertically_backward (&it, window_box_height (w) / 2);
5102 charpos = IT_CHARPOS (it);
5103 bytepos = IT_BYTEPOS (it);
5104 bidi_unshelve_cache (itdata, 0);
5105 }
5106 else if (iarg < 0)
5107 {
5108 struct it it;
5109 struct text_pos pt;
5110 ptrdiff_t nlines = min (PTRDIFF_MAX, -iarg);
5111 int extra_line_spacing;
5112 int h = window_box_height (w);
5113 void *itdata = bidi_shelve_cache ();
5114
5115 iarg = - max (-iarg, this_scroll_margin);
5116
5117 SET_TEXT_POS (pt, PT, PT_BYTE);
5118 start_display (&it, w, pt);
5119
5120 /* Be sure we have the exact height of the full line containing PT. */
5121 move_it_by_lines (&it, 0);
5122
5123 /* The amount of pixels we have to move back is the window
5124 height minus what's displayed in the line containing PT,
5125 and the lines below. */
5126 it.current_y = 0;
5127 it.vpos = 0;
5128 move_it_by_lines (&it, nlines);
5129
5130 if (it.vpos == nlines)
5131 h -= it.current_y;
5132 else
5133 {
5134 /* Last line has no newline */
5135 h -= line_bottom_y (&it);
5136 it.vpos++;
5137 }
5138
5139 /* Don't reserve space for extra line spacing of last line. */
5140 extra_line_spacing = it.max_extra_line_spacing;
5141
5142 /* If we can't move down NLINES lines because we hit
5143 the end of the buffer, count in some empty lines. */
5144 if (it.vpos < nlines)
5145 {
5146 nlines -= it.vpos;
5147 extra_line_spacing = it.extra_line_spacing;
5148 h -= nlines * (FRAME_LINE_HEIGHT (it.f) + extra_line_spacing);
5149 }
5150 if (h <= 0)
5151 {
5152 bidi_unshelve_cache (itdata, 0);
5153 return Qnil;
5154 }
5155
5156 /* Now find the new top line (starting position) of the window. */
5157 start_display (&it, w, pt);
5158 it.current_y = 0;
5159 move_it_vertically_backward (&it, h);
5160
5161 /* If extra line spacing is present, we may move too far
5162 back. This causes the last line to be only partially
5163 visible (which triggers redisplay to recenter that line
5164 in the middle), so move forward.
5165 But ignore extra line spacing on last line, as it is not
5166 considered to be part of the visible height of the line.
5167 */
5168 h += extra_line_spacing;
5169 while (-it.current_y > h)
5170 move_it_by_lines (&it, 1);
5171
5172 charpos = IT_CHARPOS (it);
5173 bytepos = IT_BYTEPOS (it);
5174
5175 bidi_unshelve_cache (itdata, 0);
5176 }
5177 else
5178 {
5179 struct position pos;
5180
5181 iarg = max (iarg, this_scroll_margin);
5182
5183 pos = *vmotion (PT, -iarg, w);
5184 charpos = pos.bufpos;
5185 bytepos = pos.bytepos;
5186 }
5187 }
5188 else
5189 {
5190 struct position pos;
5191 int ht = window_internal_height (w);
5192
5193 if (center_p)
5194 iarg = ht / 2;
5195 else if (iarg < 0)
5196 iarg += ht;
5197
5198 /* Don't let it get into the margin at either top or bottom. */
5199 iarg = max (iarg, this_scroll_margin);
5200 iarg = min (iarg, ht - this_scroll_margin - 1);
5201
5202 pos = *vmotion (PT, - iarg, w);
5203 charpos = pos.bufpos;
5204 bytepos = pos.bytepos;
5205 }
5206
5207 /* Set the new window start. */
5208 set_marker_both (w->start, w->buffer, charpos, bytepos);
5209 w->window_end_valid = Qnil;
5210
5211 w->optional_new_start = 1;
5212
5213 w->start_at_line_beg = (bytepos == BEGV_BYTE ||
5214 FETCH_BYTE (bytepos - 1) == '\n');
5215
5216 set_buffer_internal (obuf);
5217 return Qnil;
5218 }
5219
5220 DEFUN ("window-text-height", Fwindow_text_height, Swindow_text_height,
5221 0, 1, 0,
5222 doc: /* Return the height in lines of the text display area of WINDOW.
5223 If WINDOW is omitted or nil, it defaults to the selected window.
5224
5225 The returned height does not include the mode line, any header line,
5226 nor any partial-height lines at the bottom of the text area. */)
5227 (Lisp_Object window)
5228 {
5229 struct window *w = decode_window (window);
5230 int pixel_height = window_box_height (w);
5231 int line_height = pixel_height / FRAME_LINE_HEIGHT (XFRAME (w->frame));
5232 return make_number (line_height);
5233 }
5234
5235
5236 \f
5237 DEFUN ("move-to-window-line", Fmove_to_window_line, Smove_to_window_line,
5238 1, 1, "P",
5239 doc: /* Position point relative to window.
5240 ARG nil means position point at center of window.
5241 Else, ARG specifies vertical position within the window;
5242 zero means top of window, negative means relative to bottom of window. */)
5243 (Lisp_Object arg)
5244 {
5245 struct window *w = XWINDOW (selected_window);
5246 int lines, start;
5247 Lisp_Object window;
5248 #if 0
5249 int this_scroll_margin;
5250 #endif
5251
5252 if (!(BUFFERP (w->buffer)
5253 && XBUFFER (w->buffer) == current_buffer))
5254 /* This test is needed to make sure PT/PT_BYTE make sense in w->buffer
5255 when passed below to set_marker_both. */
5256 error ("move-to-window-line called from unrelated buffer");
5257
5258 window = selected_window;
5259 start = marker_position (w->start);
5260 if (start < BEGV || start > ZV)
5261 {
5262 int height = window_internal_height (w);
5263 Fvertical_motion (make_number (- (height / 2)), window);
5264 set_marker_both (w->start, w->buffer, PT, PT_BYTE);
5265 w->start_at_line_beg = !NILP (Fbolp ());
5266 w->force_start = 1;
5267 }
5268 else
5269 Fgoto_char (w->start);
5270
5271 lines = displayed_window_lines (w);
5272
5273 #if 0
5274 this_scroll_margin = max (0, min (scroll_margin, lines / 4));
5275 #endif
5276
5277 if (NILP (arg))
5278 XSETFASTINT (arg, lines / 2);
5279 else
5280 {
5281 EMACS_INT iarg = XINT (Fprefix_numeric_value (arg));
5282
5283 if (iarg < 0)
5284 iarg = iarg + lines;
5285
5286 #if 0 /* This code would prevent move-to-window-line from moving point
5287 to a place inside the scroll margins (which would cause the
5288 next redisplay to scroll). I wrote this code, but then concluded
5289 it is probably better not to install it. However, it is here
5290 inside #if 0 so as not to lose it. -- rms. */
5291
5292 /* Don't let it get into the margin at either top or bottom. */
5293 iarg = max (iarg, this_scroll_margin);
5294 iarg = min (iarg, lines - this_scroll_margin - 1);
5295 #endif
5296
5297 arg = make_number (iarg);
5298 }
5299
5300 /* Skip past a partially visible first line. */
5301 if (w->vscroll)
5302 XSETINT (arg, XINT (arg) + 1);
5303
5304 return Fvertical_motion (arg, window);
5305 }
5306
5307
5308 \f
5309 /***********************************************************************
5310 Window Configuration
5311 ***********************************************************************/
5312
5313 struct save_window_data
5314 {
5315 struct vectorlike_header header;
5316 Lisp_Object selected_frame;
5317 Lisp_Object current_window;
5318 Lisp_Object current_buffer;
5319 Lisp_Object minibuf_scroll_window;
5320 Lisp_Object minibuf_selected_window;
5321 Lisp_Object root_window;
5322 Lisp_Object focus_frame;
5323 /* A vector, each of whose elements is a struct saved_window
5324 for one window. */
5325 Lisp_Object saved_windows;
5326
5327 /* All fields above are traced by the GC.
5328 From `fame-cols' down, the fields are ignored by the GC. */
5329
5330 int frame_cols, frame_lines, frame_menu_bar_lines;
5331 int frame_tool_bar_lines;
5332 };
5333
5334 /* This is saved as a Lisp_Vector */
5335 struct saved_window
5336 {
5337 struct vectorlike_header header;
5338
5339 Lisp_Object window, buffer, start, pointm, mark;
5340 Lisp_Object left_col, top_line, total_cols, total_lines;
5341 Lisp_Object normal_cols, normal_lines;
5342 Lisp_Object hscroll, min_hscroll;
5343 Lisp_Object parent, prev;
5344 Lisp_Object start_at_line_beg;
5345 Lisp_Object display_table;
5346 Lisp_Object left_margin_cols, right_margin_cols;
5347 Lisp_Object left_fringe_width, right_fringe_width, fringes_outside_margins;
5348 Lisp_Object scroll_bar_width, vertical_scroll_bar_type, dedicated;
5349 Lisp_Object combination_limit, window_parameters;
5350 };
5351
5352 #define SAVED_WINDOW_N(swv,n) \
5353 ((struct saved_window *) (XVECTOR ((swv)->contents[(n)])))
5354
5355 DEFUN ("window-configuration-p", Fwindow_configuration_p, Swindow_configuration_p, 1, 1, 0,
5356 doc: /* Return t if OBJECT is a window-configuration object. */)
5357 (Lisp_Object object)
5358 {
5359 return WINDOW_CONFIGURATIONP (object) ? Qt : Qnil;
5360 }
5361
5362 DEFUN ("window-configuration-frame", Fwindow_configuration_frame, Swindow_configuration_frame, 1, 1, 0,
5363 doc: /* Return the frame that CONFIG, a window-configuration object, is about. */)
5364 (Lisp_Object config)
5365 {
5366 register struct save_window_data *data;
5367 struct Lisp_Vector *saved_windows;
5368
5369 CHECK_WINDOW_CONFIGURATION (config);
5370
5371 data = (struct save_window_data *) XVECTOR (config);
5372 saved_windows = XVECTOR (data->saved_windows);
5373 return XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5374 }
5375
5376 DEFUN ("set-window-configuration", Fset_window_configuration,
5377 Sset_window_configuration, 1, 1, 0,
5378 doc: /* Set the configuration of windows and buffers as specified by CONFIGURATION.
5379 CONFIGURATION must be a value previously returned
5380 by `current-window-configuration' (which see).
5381 If CONFIGURATION was made from a frame that is now deleted,
5382 only frame-independent values can be restored. In this case,
5383 the return value is nil. Otherwise the value is t. */)
5384 (Lisp_Object configuration)
5385 {
5386 register struct save_window_data *data;
5387 struct Lisp_Vector *saved_windows;
5388 Lisp_Object new_current_buffer;
5389 Lisp_Object frame;
5390 Lisp_Object auto_buffer_name;
5391 FRAME_PTR f;
5392 ptrdiff_t old_point = -1;
5393
5394 CHECK_WINDOW_CONFIGURATION (configuration);
5395
5396 data = (struct save_window_data *) XVECTOR (configuration);
5397 saved_windows = XVECTOR (data->saved_windows);
5398
5399 new_current_buffer = data->current_buffer;
5400 if (NILP (BVAR (XBUFFER (new_current_buffer), name)))
5401 new_current_buffer = Qnil;
5402 else
5403 {
5404 if (XBUFFER (new_current_buffer) == current_buffer)
5405 /* The code further down "preserves point" by saving here PT in
5406 old_point and then setting it later back into PT. When the
5407 current-selected-window and the final-selected-window both show
5408 the current buffer, this suffers from the problem that the
5409 current PT is the window-point of the current-selected-window,
5410 while the final PT is the point of the final-selected-window, so
5411 this copy from one PT to the other would end up moving the
5412 window-point of the final-selected-window to the window-point of
5413 the current-selected-window. So we have to be careful which
5414 point of the current-buffer we copy into old_point. */
5415 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5416 && WINDOWP (selected_window)
5417 && EQ (XWINDOW (selected_window)->buffer, new_current_buffer)
5418 && !EQ (selected_window, data->current_window))
5419 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5420 else
5421 old_point = PT;
5422 else
5423 /* BUF_PT (XBUFFER (new_current_buffer)) gives us the position of
5424 point in new_current_buffer as of the last time this buffer was
5425 used. This can be non-deterministic since it can be changed by
5426 things like jit-lock by mere temporary selection of some random
5427 window that happens to show this buffer.
5428 So if possible we want this arbitrary choice of "which point" to
5429 be the one from the to-be-selected-window so as to prevent this
5430 window's cursor from being copied from another window. */
5431 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer)
5432 /* If current_window = selected_window, its point is in BUF_PT. */
5433 && !EQ (selected_window, data->current_window))
5434 old_point = XMARKER (XWINDOW (data->current_window)->pointm)->charpos;
5435 else
5436 old_point = BUF_PT (XBUFFER (new_current_buffer));
5437 }
5438
5439 frame = XWINDOW (SAVED_WINDOW_N (saved_windows, 0)->window)->frame;
5440 f = XFRAME (frame);
5441
5442 /* If f is a dead frame, don't bother rebuilding its window tree.
5443 However, there is other stuff we should still try to do below. */
5444 if (FRAME_LIVE_P (f))
5445 {
5446 Lisp_Object window;
5447 Lisp_Object dead_windows = Qnil;
5448 register Lisp_Object tem, par, pers;
5449 register struct window *w;
5450 register struct saved_window *p;
5451 struct window *root_window;
5452 struct window **leaf_windows;
5453 int n_leaf_windows;
5454 ptrdiff_t k;
5455 int i, n;
5456
5457 /* If the frame has been resized since this window configuration was
5458 made, we change the frame to the size specified in the
5459 configuration, restore the configuration, and then resize it
5460 back. We keep track of the prevailing height in these variables. */
5461 int previous_frame_lines = FRAME_LINES (f);
5462 int previous_frame_cols = FRAME_COLS (f);
5463 int previous_frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
5464 int previous_frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
5465
5466 /* The mouse highlighting code could get screwed up
5467 if it runs during this. */
5468 BLOCK_INPUT;
5469
5470 if (data->frame_lines != previous_frame_lines
5471 || data->frame_cols != previous_frame_cols)
5472 change_frame_size (f, data->frame_lines,
5473 data->frame_cols, 0, 0, 0);
5474 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5475 if (data->frame_menu_bar_lines
5476 != previous_frame_menu_bar_lines)
5477 x_set_menu_bar_lines (f, make_number (data->frame_menu_bar_lines),
5478 make_number (0));
5479 #ifdef HAVE_WINDOW_SYSTEM
5480 if (data->frame_tool_bar_lines
5481 != previous_frame_tool_bar_lines)
5482 x_set_tool_bar_lines (f, make_number (data->frame_tool_bar_lines),
5483 make_number (0));
5484 #endif
5485 #endif
5486
5487 /* "Swap out" point from the selected window's buffer
5488 into the window itself. (Normally the pointm of the selected
5489 window holds garbage.) We do this now, before
5490 restoring the window contents, and prevent it from
5491 being done later on when we select a new window. */
5492 if (! NILP (XWINDOW (selected_window)->buffer))
5493 {
5494 w = XWINDOW (selected_window);
5495 set_marker_both (w->pointm,
5496 w->buffer,
5497 BUF_PT (XBUFFER (w->buffer)),
5498 BUF_PT_BYTE (XBUFFER (w->buffer)));
5499 }
5500
5501 windows_or_buffers_changed++;
5502 FRAME_WINDOW_SIZES_CHANGED (f) = 1;
5503
5504 /* Problem: Freeing all matrices and later allocating them again
5505 is a serious redisplay flickering problem. What we would
5506 really like to do is to free only those matrices not reused
5507 below. */
5508 root_window = XWINDOW (FRAME_ROOT_WINDOW (f));
5509 leaf_windows
5510 = (struct window **) alloca (count_windows (root_window)
5511 * sizeof (struct window *));
5512 n_leaf_windows = get_leaf_windows (root_window, leaf_windows, 0);
5513
5514 /* Kludge Alert!
5515 Mark all windows now on frame as "deleted".
5516 Restoring the new configuration "undeletes" any that are in it.
5517
5518 Save their current buffers in their height fields, since we may
5519 need it later, if a buffer saved in the configuration is now
5520 dead. */
5521 delete_all_child_windows (FRAME_ROOT_WINDOW (f));
5522
5523 for (k = 0; k < saved_windows->header.size; k++)
5524 {
5525 p = SAVED_WINDOW_N (saved_windows, k);
5526 window = p->window;
5527 w = XWINDOW (window);
5528 w->next = Qnil;
5529
5530 if (!NILP (p->parent))
5531 w->parent = SAVED_WINDOW_N (saved_windows,
5532 XFASTINT (p->parent))->window;
5533 else
5534 w->parent = Qnil;
5535
5536 if (!NILP (p->prev))
5537 {
5538 w->prev = SAVED_WINDOW_N (saved_windows,
5539 XFASTINT (p->prev))->window;
5540 XWINDOW (w->prev)->next = p->window;
5541 }
5542 else
5543 {
5544 w->prev = Qnil;
5545 if (!NILP (w->parent))
5546 {
5547 if (EQ (p->total_cols, XWINDOW (w->parent)->total_cols))
5548 {
5549 XWINDOW (w->parent)->vchild = p->window;
5550 XWINDOW (w->parent)->hchild = Qnil;
5551 }
5552 else
5553 {
5554 XWINDOW (w->parent)->hchild = p->window;
5555 XWINDOW (w->parent)->vchild = Qnil;
5556 }
5557 }
5558 }
5559
5560 /* If we squirreled away the buffer in the window's height,
5561 restore it now. */
5562 if (BUFFERP (w->total_lines))
5563 w->buffer = w->total_lines;
5564 w->left_col = p->left_col;
5565 w->top_line = p->top_line;
5566 w->total_cols = p->total_cols;
5567 w->total_lines = p->total_lines;
5568 w->normal_cols = p->normal_cols;
5569 w->normal_lines = p->normal_lines;
5570 w->hscroll = p->hscroll;
5571 w->min_hscroll = p->min_hscroll;
5572 w->display_table = p->display_table;
5573 w->left_margin_cols = p->left_margin_cols;
5574 w->right_margin_cols = p->right_margin_cols;
5575 w->left_fringe_width = p->left_fringe_width;
5576 w->right_fringe_width = p->right_fringe_width;
5577 w->fringes_outside_margins = p->fringes_outside_margins;
5578 w->scroll_bar_width = p->scroll_bar_width;
5579 w->vertical_scroll_bar_type = p->vertical_scroll_bar_type;
5580 w->dedicated = p->dedicated;
5581 w->combination_limit = p->combination_limit;
5582 /* Restore any window parameters that have been saved.
5583 Parameters that have not been saved are left alone. */
5584 for (tem = p->window_parameters; CONSP (tem); tem = XCDR (tem))
5585 {
5586 pers = XCAR (tem);
5587 if (CONSP (pers))
5588 {
5589 if (NILP (XCDR (pers)))
5590 {
5591 par = Fassq (XCAR (pers), w->window_parameters);
5592 if (CONSP (par) && !NILP (XCDR (par)))
5593 /* Reset a parameter to nil if and only if it
5594 has a non-nil association. Don't make new
5595 associations. */
5596 Fsetcdr (par, Qnil);
5597 }
5598 else
5599 /* Always restore a non-nil value. */
5600 Fset_window_parameter (window, XCAR (pers), XCDR (pers));
5601 }
5602 }
5603
5604 XSETFASTINT (w->last_modified, 0);
5605 XSETFASTINT (w->last_overlay_modified, 0);
5606
5607 /* Reinstall the saved buffer and pointers into it. */
5608 if (NILP (p->buffer))
5609 /* An internal window. */
5610 w->buffer = p->buffer;
5611 else if (!NILP (BVAR (XBUFFER (p->buffer), name)))
5612 /* If saved buffer is alive, install it. */
5613 {
5614 w->buffer = p->buffer;
5615 w->start_at_line_beg = !NILP (p->start_at_line_beg);
5616 set_marker_restricted (w->start, p->start, w->buffer);
5617 set_marker_restricted (w->pointm, p->pointm, w->buffer);
5618 Fset_marker (BVAR (XBUFFER (w->buffer), mark),
5619 p->mark, w->buffer);
5620
5621 /* As documented in Fcurrent_window_configuration, don't
5622 restore the location of point in the buffer which was
5623 current when the window configuration was recorded. */
5624 if (!EQ (p->buffer, new_current_buffer)
5625 && XBUFFER (p->buffer) == current_buffer)
5626 Fgoto_char (w->pointm);
5627 }
5628 else if (!NILP (w->buffer) && !NILP (BVAR (XBUFFER (w->buffer), name)))
5629 /* Keep window's old buffer; make sure the markers are
5630 real. */
5631 {
5632 /* Set window markers at start of visible range. */
5633 if (XMARKER (w->start)->buffer == 0)
5634 set_marker_restricted (w->start, make_number (0),
5635 w->buffer);
5636 if (XMARKER (w->pointm)->buffer == 0)
5637 set_marker_restricted_both (w->pointm, w->buffer,
5638 BUF_PT (XBUFFER (w->buffer)),
5639 BUF_PT_BYTE (XBUFFER (w->buffer)));
5640 w->start_at_line_beg = 1;
5641 }
5642 else if (STRINGP (auto_buffer_name =
5643 Fwindow_parameter (window, Qauto_buffer_name))
5644 && SCHARS (auto_buffer_name) != 0
5645 && !NILP (w->buffer = Fget_buffer_create (auto_buffer_name)))
5646 {
5647 set_marker_restricted (w->start, make_number (0), w->buffer);
5648 set_marker_restricted (w->pointm, make_number (0), w->buffer);
5649 w->start_at_line_beg = 1;
5650 }
5651 else
5652 /* Window has no live buffer, get one. */
5653 {
5654 /* Get the buffer via other_buffer_safely in order to
5655 avoid showing an unimportant buffer and, if necessary, to
5656 recreate *scratch* in the course (part of Juanma's bs-show
5657 scenario from March 2011). */
5658 w->buffer = other_buffer_safely (Fcurrent_buffer ());
5659 /* This will set the markers to beginning of visible
5660 range. */
5661 set_marker_restricted (w->start, make_number (0), w->buffer);
5662 set_marker_restricted (w->pointm, make_number (0), w->buffer);
5663 w->start_at_line_beg = 1;
5664 if (!NILP (w->dedicated))
5665 /* Record this window as dead. */
5666 dead_windows = Fcons (window, dead_windows);
5667 /* Make sure window is no more dedicated. */
5668 w->dedicated = Qnil;
5669 }
5670 }
5671
5672 FRAME_ROOT_WINDOW (f) = data->root_window;
5673 /* Arrange *not* to restore point in the buffer that was
5674 current when the window configuration was saved. */
5675 if (EQ (XWINDOW (data->current_window)->buffer, new_current_buffer))
5676 set_marker_restricted (XWINDOW (data->current_window)->pointm,
5677 make_number (old_point),
5678 XWINDOW (data->current_window)->buffer);
5679
5680 /* In the following call to `select-window', prevent "swapping out
5681 point" in the old selected window using the buffer that has
5682 been restored into it. We already swapped out that point from
5683 that window's old buffer. */
5684 select_window (data->current_window, Qnil, 1);
5685 BVAR (XBUFFER (XWINDOW (selected_window)->buffer), last_selected_window)
5686 = selected_window;
5687
5688 if (NILP (data->focus_frame)
5689 || (FRAMEP (data->focus_frame)
5690 && FRAME_LIVE_P (XFRAME (data->focus_frame))))
5691 Fredirect_frame_focus (frame, data->focus_frame);
5692
5693 /* Set the screen height to the value it had before this function. */
5694 if (previous_frame_lines != FRAME_LINES (f)
5695 || previous_frame_cols != FRAME_COLS (f))
5696 change_frame_size (f, previous_frame_lines, previous_frame_cols,
5697 0, 0, 0);
5698 #if defined (HAVE_WINDOW_SYSTEM) || defined (MSDOS)
5699 if (previous_frame_menu_bar_lines != FRAME_MENU_BAR_LINES (f))
5700 x_set_menu_bar_lines (f, make_number (previous_frame_menu_bar_lines),
5701 make_number (0));
5702 #ifdef HAVE_WINDOW_SYSTEM
5703 if (previous_frame_tool_bar_lines != FRAME_TOOL_BAR_LINES (f))
5704 x_set_tool_bar_lines (f, make_number (previous_frame_tool_bar_lines),
5705 make_number (0));
5706 #endif
5707 #endif
5708
5709 /* Now, free glyph matrices in windows that were not reused. */
5710 for (i = n = 0; i < n_leaf_windows; ++i)
5711 {
5712 if (NILP (leaf_windows[i]->buffer))
5713 {
5714 /* Assert it's not reused as a combination. */
5715 xassert (NILP (leaf_windows[i]->hchild)
5716 && NILP (leaf_windows[i]->vchild));
5717 free_window_matrices (leaf_windows[i]);
5718 }
5719 else if (EQ (leaf_windows[i]->buffer, new_current_buffer))
5720 ++n;
5721 }
5722
5723 adjust_glyphs (f);
5724 UNBLOCK_INPUT;
5725
5726 /* Scan dead buffer windows. */
5727 for (; CONSP (dead_windows); dead_windows = XCDR (dead_windows))
5728 {
5729 window = XCAR (dead_windows);
5730 if (WINDOW_LIVE_P (window) && !EQ (window, FRAME_ROOT_WINDOW (f)))
5731 delete_deletable_window (window);
5732 }
5733
5734 /* Fselect_window will have made f the selected frame, so we
5735 reselect the proper frame here. Fhandle_switch_frame will change the
5736 selected window too, but that doesn't make the call to
5737 Fselect_window above totally superfluous; it still sets f's
5738 selected window. */
5739 if (FRAME_LIVE_P (XFRAME (data->selected_frame)))
5740 do_switch_frame (data->selected_frame, 0, 0, Qnil);
5741
5742 run_window_configuration_change_hook (f);
5743 }
5744
5745 if (!NILP (new_current_buffer))
5746 Fset_buffer (new_current_buffer);
5747
5748 Vminibuf_scroll_window = data->minibuf_scroll_window;
5749 minibuf_selected_window = data->minibuf_selected_window;
5750
5751 return (FRAME_LIVE_P (f) ? Qt : Qnil);
5752 }
5753
5754
5755 /* Recursively delete all child windows reachable via the next, vchild,
5756 and hchild slots of WINDOW. */
5757 void
5758 delete_all_child_windows (Lisp_Object window)
5759 {
5760 register struct window *w;
5761
5762 w = XWINDOW (window);
5763
5764 if (!NILP (w->next))
5765 /* Delete WINDOW's siblings (we traverse postorderly). */
5766 delete_all_child_windows (w->next);
5767
5768 w->total_lines = w->buffer; /* See Fset_window_configuration for excuse. */
5769
5770 if (!NILP (w->vchild))
5771 {
5772 delete_all_child_windows (w->vchild);
5773 w->vchild = Qnil;
5774 }
5775 else if (!NILP (w->hchild))
5776 {
5777 delete_all_child_windows (w->hchild);
5778 w->hchild = Qnil;
5779 }
5780 else if (!NILP (w->buffer))
5781 {
5782 unshow_buffer (w);
5783 unchain_marker (XMARKER (w->pointm));
5784 unchain_marker (XMARKER (w->start));
5785 w->buffer = Qnil;
5786 }
5787
5788 Vwindow_list = Qnil;
5789 }
5790 \f
5791 static int
5792 count_windows (register struct window *window)
5793 {
5794 register int count = 1;
5795 if (!NILP (window->next))
5796 count += count_windows (XWINDOW (window->next));
5797 if (!NILP (window->vchild))
5798 count += count_windows (XWINDOW (window->vchild));
5799 if (!NILP (window->hchild))
5800 count += count_windows (XWINDOW (window->hchild));
5801 return count;
5802 }
5803
5804
5805 /* Fill vector FLAT with leaf windows under W, starting at index I.
5806 Value is last index + 1. */
5807 static int
5808 get_leaf_windows (struct window *w, struct window **flat, int i)
5809 {
5810 while (w)
5811 {
5812 if (!NILP (w->hchild))
5813 i = get_leaf_windows (XWINDOW (w->hchild), flat, i);
5814 else if (!NILP (w->vchild))
5815 i = get_leaf_windows (XWINDOW (w->vchild), flat, i);
5816 else
5817 flat[i++] = w;
5818
5819 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5820 }
5821
5822 return i;
5823 }
5824
5825
5826 /* Return a pointer to the glyph W's physical cursor is on. Value is
5827 null if W's current matrix is invalid, so that no meaningful glyph
5828 can be returned. */
5829 struct glyph *
5830 get_phys_cursor_glyph (struct window *w)
5831 {
5832 struct glyph_row *row;
5833 struct glyph *glyph;
5834 int hpos = w->phys_cursor.hpos;
5835
5836 if (!(w->phys_cursor.vpos >= 0
5837 && w->phys_cursor.vpos < w->current_matrix->nrows))
5838 return NULL;
5839
5840 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
5841 if (!row->enabled_p)
5842 return NULL;
5843
5844 if (XINT (w->hscroll))
5845 {
5846 /* When the window is hscrolled, cursor hpos can legitimately be
5847 out of bounds, but we draw the cursor at the corresponding
5848 window margin in that case. */
5849 if (!row->reversed_p && hpos < 0)
5850 hpos = 0;
5851 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
5852 hpos = row->used[TEXT_AREA] - 1;
5853 }
5854
5855 if (row->used[TEXT_AREA] > hpos
5856 && 0 <= hpos)
5857 glyph = row->glyphs[TEXT_AREA] + hpos;
5858 else
5859 glyph = NULL;
5860
5861 return glyph;
5862 }
5863
5864
5865 static int
5866 save_window_save (Lisp_Object window, struct Lisp_Vector *vector, int i)
5867 {
5868 register struct saved_window *p;
5869 register struct window *w;
5870 register Lisp_Object tem, pers, par;
5871
5872 for (;!NILP (window); window = w->next)
5873 {
5874 p = SAVED_WINDOW_N (vector, i);
5875 w = XWINDOW (window);
5876
5877 XSETFASTINT (w->temslot, i); i++;
5878 p->window = window;
5879 p->buffer = w->buffer;
5880 p->left_col = w->left_col;
5881 p->top_line = w->top_line;
5882 p->total_cols = w->total_cols;
5883 p->total_lines = w->total_lines;
5884 p->normal_cols = w->normal_cols;
5885 p->normal_lines = w->normal_lines;
5886 p->hscroll = w->hscroll;
5887 p->min_hscroll = w->min_hscroll;
5888 p->display_table = w->display_table;
5889 p->left_margin_cols = w->left_margin_cols;
5890 p->right_margin_cols = w->right_margin_cols;
5891 p->left_fringe_width = w->left_fringe_width;
5892 p->right_fringe_width = w->right_fringe_width;
5893 p->fringes_outside_margins = w->fringes_outside_margins;
5894 p->scroll_bar_width = w->scroll_bar_width;
5895 p->vertical_scroll_bar_type = w->vertical_scroll_bar_type;
5896 p->dedicated = w->dedicated;
5897 p->combination_limit = w->combination_limit;
5898 p->window_parameters = Qnil;
5899
5900 if (!NILP (Vwindow_persistent_parameters))
5901 {
5902 /* Run cycle detection on Vwindow_persistent_parameters. */
5903 Lisp_Object tortoise, hare;
5904
5905 hare = tortoise = Vwindow_persistent_parameters;
5906 while (CONSP (hare))
5907 {
5908 hare = XCDR (hare);
5909 if (!CONSP (hare))
5910 break;
5911
5912 hare = XCDR (hare);
5913 tortoise = XCDR (tortoise);
5914
5915 if (EQ (hare, tortoise))
5916 /* Reset Vwindow_persistent_parameters to Qnil. */
5917 {
5918 Vwindow_persistent_parameters = Qnil;
5919 break;
5920 }
5921 }
5922
5923 for (tem = Vwindow_persistent_parameters; CONSP (tem);
5924 tem = XCDR (tem))
5925 {
5926 pers = XCAR (tem);
5927 /* Save values for persistent window parameters. */
5928 if (CONSP (pers) && !NILP (XCDR (pers)))
5929 {
5930 par = Fassq (XCAR (pers), w->window_parameters);
5931 if (NILP (par))
5932 /* If the window has no value for the parameter,
5933 make one. */
5934 p->window_parameters = Fcons (Fcons (XCAR (pers), Qnil),
5935 p->window_parameters);
5936 else
5937 /* If the window has a value for the parameter,
5938 save it. */
5939 p->window_parameters = Fcons (Fcons (XCAR (par),
5940 XCDR (par)),
5941 p->window_parameters);
5942 }
5943 }
5944 }
5945
5946 if (!NILP (w->buffer))
5947 {
5948 /* Save w's value of point in the window configuration. If w
5949 is the selected window, then get the value of point from
5950 the buffer; pointm is garbage in the selected window. */
5951 if (EQ (window, selected_window))
5952 {
5953 p->pointm = Fmake_marker ();
5954 set_marker_both (p->pointm, w->buffer,
5955 BUF_PT (XBUFFER (w->buffer)),
5956 BUF_PT_BYTE (XBUFFER (w->buffer)));
5957 }
5958 else
5959 p->pointm = Fcopy_marker (w->pointm, Qnil);
5960 XMARKER (p->pointm)->insertion_type
5961 = !NILP (Vwindow_point_insertion_type);
5962
5963 p->start = Fcopy_marker (w->start, Qnil);
5964 p->start_at_line_beg = w->start_at_line_beg ? Qt : Qnil;
5965
5966 tem = BVAR (XBUFFER (w->buffer), mark);
5967 p->mark = Fcopy_marker (tem, Qnil);
5968 }
5969 else
5970 {
5971 p->pointm = Qnil;
5972 p->start = Qnil;
5973 p->mark = Qnil;
5974 p->start_at_line_beg = Qnil;
5975 }
5976
5977 if (NILP (w->parent))
5978 p->parent = Qnil;
5979 else
5980 p->parent = XWINDOW (w->parent)->temslot;
5981
5982 if (NILP (w->prev))
5983 p->prev = Qnil;
5984 else
5985 p->prev = XWINDOW (w->prev)->temslot;
5986
5987 if (!NILP (w->vchild))
5988 i = save_window_save (w->vchild, vector, i);
5989 if (!NILP (w->hchild))
5990 i = save_window_save (w->hchild, vector, i);
5991 }
5992
5993 return i;
5994 }
5995
5996 DEFUN ("current-window-configuration", Fcurrent_window_configuration,
5997 Scurrent_window_configuration, 0, 1, 0,
5998 doc: /* Return an object representing the current window configuration of FRAME.
5999 If FRAME is nil or omitted, use the selected frame.
6000 This describes the number of windows, their sizes and current buffers,
6001 and for each displayed buffer, where display starts, and the positions of
6002 point and mark. An exception is made for point in the current buffer:
6003 its value is -not- saved.
6004 This also records the currently selected frame, and FRAME's focus
6005 redirection (see `redirect-frame-focus'). The variable
6006 `window-persistent-parameters' specifies which window parameters are
6007 saved by this function. */)
6008 (Lisp_Object frame)
6009 {
6010 register Lisp_Object tem;
6011 register int n_windows;
6012 register struct save_window_data *data;
6013 register int i;
6014 FRAME_PTR f;
6015
6016 if (NILP (frame))
6017 frame = selected_frame;
6018 CHECK_LIVE_FRAME (frame);
6019 f = XFRAME (frame);
6020
6021 n_windows = count_windows (XWINDOW (FRAME_ROOT_WINDOW (f)));
6022 data = ALLOCATE_PSEUDOVECTOR (struct save_window_data, frame_cols,
6023 PVEC_WINDOW_CONFIGURATION);
6024
6025 data->frame_cols = FRAME_COLS (f);
6026 data->frame_lines = FRAME_LINES (f);
6027 data->frame_menu_bar_lines = FRAME_MENU_BAR_LINES (f);
6028 data->frame_tool_bar_lines = FRAME_TOOL_BAR_LINES (f);
6029 data->selected_frame = selected_frame;
6030 data->current_window = FRAME_SELECTED_WINDOW (f);
6031 XSETBUFFER (data->current_buffer, current_buffer);
6032 data->minibuf_scroll_window = minibuf_level > 0 ? Vminibuf_scroll_window : Qnil;
6033 data->minibuf_selected_window = minibuf_level > 0 ? minibuf_selected_window : Qnil;
6034 data->root_window = FRAME_ROOT_WINDOW (f);
6035 data->focus_frame = FRAME_FOCUS_FRAME (f);
6036 tem = Fmake_vector (make_number (n_windows), Qnil);
6037 data->saved_windows = tem;
6038 for (i = 0; i < n_windows; i++)
6039 XVECTOR (tem)->contents[i]
6040 = Fmake_vector (make_number (VECSIZE (struct saved_window)), Qnil);
6041 save_window_save (FRAME_ROOT_WINDOW (f), XVECTOR (tem), 0);
6042 XSETWINDOW_CONFIGURATION (tem, data);
6043 return (tem);
6044 }
6045 \f
6046 /***********************************************************************
6047 Marginal Areas
6048 ***********************************************************************/
6049
6050 DEFUN ("set-window-margins", Fset_window_margins, Sset_window_margins,
6051 2, 3, 0,
6052 doc: /* Set width of marginal areas of window WINDOW.
6053 If WINDOW is nil, set margins of the currently selected window.
6054 Second arg LEFT-WIDTH specifies the number of character cells to
6055 reserve for the left marginal area. Optional third arg RIGHT-WIDTH
6056 does the same for the right marginal area. A nil width parameter
6057 means no margin. */)
6058 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width)
6059 {
6060 struct window *w = decode_window (window);
6061
6062 /* Translate negative or zero widths to nil.
6063 Margins that are too wide have to be checked elsewhere. */
6064
6065 if (!NILP (left_width))
6066 {
6067 CHECK_NUMBER (left_width);
6068 if (XINT (left_width) <= 0)
6069 left_width = Qnil;
6070 }
6071
6072 if (!NILP (right_width))
6073 {
6074 CHECK_NUMBER (right_width);
6075 if (XINT (right_width) <= 0)
6076 right_width = Qnil;
6077 }
6078
6079 if (!EQ (w->left_margin_cols, left_width)
6080 || !EQ (w->right_margin_cols, right_width))
6081 {
6082 w->left_margin_cols = left_width;
6083 w->right_margin_cols = right_width;
6084
6085 adjust_window_margins (w);
6086
6087 ++windows_or_buffers_changed;
6088 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6089 }
6090
6091 return Qnil;
6092 }
6093
6094
6095 DEFUN ("window-margins", Fwindow_margins, Swindow_margins,
6096 0, 1, 0,
6097 doc: /* Get width of marginal areas of window WINDOW.
6098 If WINDOW is omitted or nil, it defaults to the selected window.
6099 Value is a cons of the form (LEFT-WIDTH . RIGHT-WIDTH).
6100 If a marginal area does not exist, its width will be returned
6101 as nil. */)
6102 (Lisp_Object window)
6103 {
6104 struct window *w = decode_window (window);
6105 return Fcons (w->left_margin_cols, w->right_margin_cols);
6106 }
6107
6108
6109 \f
6110 /***********************************************************************
6111 Fringes
6112 ***********************************************************************/
6113
6114 DEFUN ("set-window-fringes", Fset_window_fringes, Sset_window_fringes,
6115 2, 4, 0,
6116 doc: /* Set the fringe widths of window WINDOW.
6117 If WINDOW is nil, set the fringe widths of the currently selected
6118 window.
6119 Second arg LEFT-WIDTH specifies the number of pixels to reserve for
6120 the left fringe. Optional third arg RIGHT-WIDTH specifies the right
6121 fringe width. If a fringe width arg is nil, that means to use the
6122 frame's default fringe width. Default fringe widths can be set with
6123 the command `set-fringe-style'.
6124 If optional fourth arg OUTSIDE-MARGINS is non-nil, draw the fringes
6125 outside of the display margins. By default, fringes are drawn between
6126 display marginal areas and the text area. */)
6127 (Lisp_Object window, Lisp_Object left_width, Lisp_Object right_width, Lisp_Object outside_margins)
6128 {
6129 struct window *w = decode_window (window);
6130
6131 if (!NILP (left_width))
6132 CHECK_NATNUM (left_width);
6133 if (!NILP (right_width))
6134 CHECK_NATNUM (right_width);
6135
6136 /* Do nothing on a tty. */
6137 if (FRAME_WINDOW_P (WINDOW_XFRAME (w))
6138 && (!EQ (w->left_fringe_width, left_width)
6139 || !EQ (w->right_fringe_width, right_width)
6140 || !EQ (w->fringes_outside_margins, outside_margins)))
6141 {
6142 w->left_fringe_width = left_width;
6143 w->right_fringe_width = right_width;
6144 w->fringes_outside_margins = outside_margins;
6145
6146 adjust_window_margins (w);
6147
6148 clear_glyph_matrix (w->current_matrix);
6149 w->window_end_valid = Qnil;
6150
6151 ++windows_or_buffers_changed;
6152 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6153 }
6154
6155 return Qnil;
6156 }
6157
6158
6159 DEFUN ("window-fringes", Fwindow_fringes, Swindow_fringes,
6160 0, 1, 0,
6161 doc: /* Get width of fringes of window WINDOW.
6162 If WINDOW is omitted or nil, it defaults to the selected window.
6163 Value is a list of the form (LEFT-WIDTH RIGHT-WIDTH OUTSIDE-MARGINS). */)
6164 (Lisp_Object window)
6165 {
6166 struct window *w = decode_window (window);
6167
6168 return Fcons (make_number (WINDOW_LEFT_FRINGE_WIDTH (w)),
6169 Fcons (make_number (WINDOW_RIGHT_FRINGE_WIDTH (w)),
6170 Fcons ((WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
6171 ? Qt : Qnil), Qnil)));
6172 }
6173
6174
6175 \f
6176 /***********************************************************************
6177 Scroll bars
6178 ***********************************************************************/
6179
6180 DEFUN ("set-window-scroll-bars", Fset_window_scroll_bars,
6181 Sset_window_scroll_bars, 2, 4, 0,
6182 doc: /* Set width and type of scroll bars of window WINDOW.
6183 If window is nil, set scroll bars of the currently selected window.
6184 Second parameter WIDTH specifies the pixel width for the scroll bar;
6185 this is automatically adjusted to a multiple of the frame column width.
6186 Third parameter VERTICAL-TYPE specifies the type of the vertical scroll
6187 bar: left, right, or nil.
6188 If WIDTH is nil, use the frame's scroll-bar width.
6189 If VERTICAL-TYPE is t, use the frame's scroll-bar type.
6190 Fourth parameter HORIZONTAL-TYPE is currently unused. */)
6191 (Lisp_Object window, Lisp_Object width, Lisp_Object vertical_type, Lisp_Object horizontal_type)
6192 {
6193 struct window *w = decode_window (window);
6194
6195 if (!NILP (width))
6196 {
6197 CHECK_RANGED_INTEGER (0, width, INT_MAX);
6198
6199 if (XINT (width) == 0)
6200 vertical_type = Qnil;
6201 }
6202
6203 if (!(NILP (vertical_type)
6204 || EQ (vertical_type, Qleft)
6205 || EQ (vertical_type, Qright)
6206 || EQ (vertical_type, Qt)))
6207 error ("Invalid type of vertical scroll bar");
6208
6209 if (!EQ (w->scroll_bar_width, width)
6210 || !EQ (w->vertical_scroll_bar_type, vertical_type))
6211 {
6212 w->scroll_bar_width = width;
6213 w->vertical_scroll_bar_type = vertical_type;
6214
6215 adjust_window_margins (w);
6216
6217 clear_glyph_matrix (w->current_matrix);
6218 w->window_end_valid = Qnil;
6219
6220 ++windows_or_buffers_changed;
6221 adjust_glyphs (XFRAME (WINDOW_FRAME (w)));
6222 }
6223
6224 return Qnil;
6225 }
6226
6227
6228 DEFUN ("window-scroll-bars", Fwindow_scroll_bars, Swindow_scroll_bars,
6229 0, 1, 0,
6230 doc: /* Get width and type of scroll bars of window WINDOW.
6231 If WINDOW is omitted or nil, it defaults to the selected window.
6232 Value is a list of the form (WIDTH COLS VERTICAL-TYPE HORIZONTAL-TYPE).
6233 If WIDTH is nil or TYPE is t, the window is using the frame's corresponding
6234 value. */)
6235 (Lisp_Object window)
6236 {
6237 struct window *w = decode_window (window);
6238 return Fcons (make_number ((WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6239 ? WINDOW_CONFIG_SCROLL_BAR_WIDTH (w)
6240 : WINDOW_SCROLL_BAR_AREA_WIDTH (w))),
6241 Fcons (make_number (WINDOW_SCROLL_BAR_COLS (w)),
6242 Fcons (w->vertical_scroll_bar_type,
6243 Fcons (Qnil, Qnil))));
6244 }
6245
6246
6247 \f
6248 /***********************************************************************
6249 Smooth scrolling
6250 ***********************************************************************/
6251
6252 DEFUN ("window-vscroll", Fwindow_vscroll, Swindow_vscroll, 0, 2, 0,
6253 doc: /* Return the amount by which WINDOW is scrolled vertically.
6254 If WINDOW is omitted or nil, it defaults to the selected window.
6255 Normally, value is a multiple of the canonical character height of WINDOW;
6256 optional second arg PIXELS-P means value is measured in pixels. */)
6257 (Lisp_Object window, Lisp_Object pixels_p)
6258 {
6259 Lisp_Object result;
6260 struct frame *f;
6261 struct window *w;
6262
6263 if (NILP (window))
6264 window = selected_window;
6265 else
6266 CHECK_WINDOW (window);
6267 w = XWINDOW (window);
6268 f = XFRAME (w->frame);
6269
6270 if (FRAME_WINDOW_P (f))
6271 result = (NILP (pixels_p)
6272 ? FRAME_CANON_Y_FROM_PIXEL_Y (f, -w->vscroll)
6273 : make_number (-w->vscroll));
6274 else
6275 result = make_number (0);
6276 return result;
6277 }
6278
6279
6280 DEFUN ("set-window-vscroll", Fset_window_vscroll, Sset_window_vscroll,
6281 2, 3, 0,
6282 doc: /* Set amount by which WINDOW should be scrolled vertically to VSCROLL.
6283 WINDOW nil means use the selected window. Normally, VSCROLL is a
6284 non-negative multiple of the canonical character height of WINDOW;
6285 optional third arg PIXELS-P non-nil means that VSCROLL is in pixels.
6286 If PIXELS-P is nil, VSCROLL may have to be rounded so that it
6287 corresponds to an integral number of pixels. The return value is the
6288 result of this rounding.
6289 If PIXELS-P is non-nil, the return value is VSCROLL. */)
6290 (Lisp_Object window, Lisp_Object vscroll, Lisp_Object pixels_p)
6291 {
6292 struct window *w;
6293 struct frame *f;
6294
6295 if (NILP (window))
6296 window = selected_window;
6297 else
6298 CHECK_WINDOW (window);
6299 CHECK_NUMBER_OR_FLOAT (vscroll);
6300
6301 w = XWINDOW (window);
6302 f = XFRAME (w->frame);
6303
6304 if (FRAME_WINDOW_P (f))
6305 {
6306 int old_dy = w->vscroll;
6307
6308 w->vscroll = - (NILP (pixels_p)
6309 ? FRAME_LINE_HEIGHT (f) * XFLOATINT (vscroll)
6310 : XFLOATINT (vscroll));
6311 w->vscroll = min (w->vscroll, 0);
6312
6313 if (w->vscroll != old_dy)
6314 {
6315 /* Adjust glyph matrix of the frame if the virtual display
6316 area becomes larger than before. */
6317 if (w->vscroll < 0 && w->vscroll < old_dy)
6318 adjust_glyphs (f);
6319
6320 /* Prevent redisplay shortcuts. */
6321 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
6322 }
6323 }
6324
6325 return Fwindow_vscroll (window, pixels_p);
6326 }
6327
6328 \f
6329 /* Call FN for all leaf windows on frame F. FN is called with the
6330 first argument being a pointer to the leaf window, and with
6331 additional argument USER_DATA. Stops when FN returns 0. */
6332
6333 static void
6334 foreach_window (struct frame *f, int (*fn) (struct window *, void *),
6335 void *user_data)
6336 {
6337 /* delete_frame may set FRAME_ROOT_WINDOW (f) to Qnil. */
6338 if (WINDOWP (FRAME_ROOT_WINDOW (f)))
6339 foreach_window_1 (XWINDOW (FRAME_ROOT_WINDOW (f)), fn, user_data);
6340 }
6341
6342
6343 /* Helper function for foreach_window. Call FN for all leaf windows
6344 reachable from W. FN is called with the first argument being a
6345 pointer to the leaf window, and with additional argument USER_DATA.
6346 Stop when FN returns 0. Value is 0 if stopped by FN. */
6347
6348 static int
6349 foreach_window_1 (struct window *w, int (*fn) (struct window *, void *), void *user_data)
6350 {
6351 int cont;
6352
6353 for (cont = 1; w && cont;)
6354 {
6355 if (!NILP (w->hchild))
6356 cont = foreach_window_1 (XWINDOW (w->hchild), fn, user_data);
6357 else if (!NILP (w->vchild))
6358 cont = foreach_window_1 (XWINDOW (w->vchild), fn, user_data);
6359 else
6360 cont = fn (w, user_data);
6361
6362 w = NILP (w->next) ? 0 : XWINDOW (w->next);
6363 }
6364
6365 return cont;
6366 }
6367
6368
6369 /* Freeze or unfreeze the window start of W unless it is a
6370 mini-window or the selected window. FREEZE_P non-null means freeze
6371 the window start. */
6372
6373 static int
6374 freeze_window_start (struct window *w, void *freeze_p)
6375 {
6376 if (MINI_WINDOW_P (w)
6377 || (WINDOWP (selected_window) /* Can be nil in corner cases. */
6378 && (w == XWINDOW (selected_window)
6379 || (MINI_WINDOW_P (XWINDOW (selected_window))
6380 && ! NILP (Vminibuf_scroll_window)
6381 && w == XWINDOW (Vminibuf_scroll_window)))))
6382 freeze_p = NULL;
6383
6384 w->frozen_window_start_p = freeze_p != NULL;
6385 return 1;
6386 }
6387
6388
6389 /* Freeze or unfreeze the window starts of all leaf windows on frame
6390 F, except the selected window and a mini-window. FREEZE_P non-zero
6391 means freeze the window start. */
6392
6393 void
6394 freeze_window_starts (struct frame *f, int freeze_p)
6395 {
6396 foreach_window (f, freeze_window_start, (void *) (freeze_p ? f : 0));
6397 }
6398
6399 \f
6400 /***********************************************************************
6401 Initialization
6402 ***********************************************************************/
6403
6404 /* Return 1 if window configurations CONFIGURATION1 and CONFIGURATION2
6405 describe the same state of affairs. This is used by Fequal.
6406
6407 ignore_positions non-zero means ignore non-matching scroll positions
6408 and the like.
6409
6410 This ignores a couple of things like the dedication status of
6411 window, combination_limit and the like. This might have to be
6412 fixed. */
6413
6414 int
6415 compare_window_configurations (Lisp_Object configuration1, Lisp_Object configuration2, int ignore_positions)
6416 {
6417 register struct save_window_data *d1, *d2;
6418 struct Lisp_Vector *sws1, *sws2;
6419 ptrdiff_t i;
6420
6421 CHECK_WINDOW_CONFIGURATION (configuration1);
6422 CHECK_WINDOW_CONFIGURATION (configuration2);
6423
6424 d1 = (struct save_window_data *) XVECTOR (configuration1);
6425 d2 = (struct save_window_data *) XVECTOR (configuration2);
6426 sws1 = XVECTOR (d1->saved_windows);
6427 sws2 = XVECTOR (d2->saved_windows);
6428
6429 /* Frame settings must match. */
6430 if (d1->frame_cols != d2->frame_cols
6431 || d1->frame_lines != d2->frame_lines
6432 || d1->frame_menu_bar_lines != d2->frame_menu_bar_lines
6433 || !EQ (d1->selected_frame, d2->selected_frame)
6434 || !EQ (d1->current_buffer, d2->current_buffer)
6435 || (!ignore_positions
6436 && (!EQ (d1->minibuf_scroll_window, d2->minibuf_scroll_window)
6437 || !EQ (d1->minibuf_selected_window, d2->minibuf_selected_window)))
6438 || !EQ (d1->focus_frame, d2->focus_frame)
6439 /* Verify that the two configurations have the same number of windows. */
6440 || sws1->header.size != sws2->header.size)
6441 return 0;
6442
6443 for (i = 0; i < sws1->header.size; i++)
6444 {
6445 struct saved_window *sw1, *sw2;
6446
6447 sw1 = SAVED_WINDOW_N (sws1, i);
6448 sw2 = SAVED_WINDOW_N (sws2, i);
6449
6450 if (
6451 /* The "current" windows in the two configurations must
6452 correspond to each other. */
6453 EQ (d1->current_window, sw1->window)
6454 != EQ (d2->current_window, sw2->window)
6455 /* Windows' buffers must match. */
6456 || !EQ (sw1->buffer, sw2->buffer)
6457 || !EQ (sw1->left_col, sw2->left_col)
6458 || !EQ (sw1->top_line, sw2->top_line)
6459 || !EQ (sw1->total_cols, sw2->total_cols)
6460 || !EQ (sw1->total_lines, sw2->total_lines)
6461 || !EQ (sw1->display_table, sw2->display_table)
6462 /* The next two disjuncts check the window structure for
6463 equality. */
6464 || !EQ (sw1->parent, sw2->parent)
6465 || !EQ (sw1->prev, sw2->prev)
6466 || (!ignore_positions
6467 && (!EQ (sw1->hscroll, sw2->hscroll)
6468 || !EQ (sw1->min_hscroll, sw2->min_hscroll)
6469 || !EQ (sw1->start_at_line_beg, sw2->start_at_line_beg)
6470 || NILP (Fequal (sw1->start, sw2->start))
6471 || NILP (Fequal (sw1->pointm, sw2->pointm))
6472 || NILP (Fequal (sw1->mark, sw2->mark))))
6473 || !EQ (sw1->left_margin_cols, sw2->left_margin_cols)
6474 || !EQ (sw1->right_margin_cols, sw2->right_margin_cols)
6475 || !EQ (sw1->left_fringe_width, sw2->left_fringe_width)
6476 || !EQ (sw1->right_fringe_width, sw2->right_fringe_width)
6477 || !EQ (sw1->fringes_outside_margins, sw2->fringes_outside_margins)
6478 || !EQ (sw1->scroll_bar_width, sw2->scroll_bar_width)
6479 || !EQ (sw1->vertical_scroll_bar_type, sw2->vertical_scroll_bar_type))
6480 return 0;
6481 }
6482
6483 return 1;
6484 }
6485
6486 DEFUN ("compare-window-configurations", Fcompare_window_configurations,
6487 Scompare_window_configurations, 2, 2, 0,
6488 doc: /* Compare two window configurations as regards the structure of windows.
6489 This function ignores details such as the values of point and mark
6490 and scrolling positions. */)
6491 (Lisp_Object x, Lisp_Object y)
6492 {
6493 if (compare_window_configurations (x, y, 1))
6494 return Qt;
6495 return Qnil;
6496 }
6497 \f
6498 void
6499 init_window_once (void)
6500 {
6501 struct frame *f = make_initial_frame ();
6502 XSETFRAME (selected_frame, f);
6503 Vterminal_frame = selected_frame;
6504 minibuf_window = f->minibuffer_window;
6505 selected_window = f->selected_window;
6506 last_nonminibuf_frame = f;
6507
6508 window_initialized = 1;
6509 }
6510
6511 void
6512 init_window (void)
6513 {
6514 Vwindow_list = Qnil;
6515 }
6516
6517 void
6518 syms_of_window (void)
6519 {
6520 DEFSYM (Qscroll_up, "scroll-up");
6521 DEFSYM (Qscroll_down, "scroll-down");
6522 DEFSYM (Qscroll_command, "scroll-command");
6523
6524 Fput (Qscroll_up, Qscroll_command, Qt);
6525 Fput (Qscroll_down, Qscroll_command, Qt);
6526
6527 DEFSYM (Qwindow_configuration_change_hook, "window-configuration-change-hook");
6528 DEFSYM (Qwindowp, "windowp");
6529 DEFSYM (Qwindow_configuration_p, "window-configuration-p");
6530 DEFSYM (Qwindow_live_p, "window-live-p");
6531 DEFSYM (Qwindow_deletable_p, "window-deletable-p");
6532 DEFSYM (Qdelete_window, "delete-window");
6533 DEFSYM (Qwindow_resize_root_window, "window--resize-root-window");
6534 DEFSYM (Qwindow_resize_root_window_vertically, "window--resize-root-window-vertically");
6535 DEFSYM (Qsafe, "safe");
6536 DEFSYM (Qdisplay_buffer, "display-buffer");
6537 DEFSYM (Qreplace_buffer_in_windows, "replace-buffer-in-windows");
6538 DEFSYM (Qrecord_window_buffer, "record-window-buffer");
6539 DEFSYM (Qget_mru_window, "get-mru-window");
6540 DEFSYM (Qtemp_buffer_show_hook, "temp-buffer-show-hook");
6541 DEFSYM (Qabove, "above");
6542 DEFSYM (Qbelow, "below");
6543 DEFSYM (Qauto_buffer_name, "auto-buffer-name");
6544 DEFSYM (Qclone_of, "clone-of");
6545
6546 staticpro (&Vwindow_list);
6547
6548 minibuf_selected_window = Qnil;
6549 staticpro (&minibuf_selected_window);
6550
6551 window_scroll_pixel_based_preserve_x = -1;
6552 window_scroll_pixel_based_preserve_y = -1;
6553 window_scroll_preserve_hpos = -1;
6554 window_scroll_preserve_vpos = -1;
6555
6556 DEFVAR_LISP ("temp-buffer-show-function", Vtemp_buffer_show_function,
6557 doc: /* Non-nil means call as function to display a help buffer.
6558 The function is called with one argument, the buffer to be displayed.
6559 Used by `with-output-to-temp-buffer'.
6560 If this function is used, then it must do the entire job of showing
6561 the buffer; `temp-buffer-show-hook' is not run unless this function runs it. */);
6562 Vtemp_buffer_show_function = Qnil;
6563
6564 DEFVAR_LISP ("minibuffer-scroll-window", Vminibuf_scroll_window,
6565 doc: /* Non-nil means it is the window that C-M-v in minibuffer should scroll. */);
6566 Vminibuf_scroll_window = Qnil;
6567
6568 DEFVAR_BOOL ("mode-line-in-non-selected-windows", mode_line_in_non_selected_windows,
6569 doc: /* Non-nil means to use `mode-line-inactive' face in non-selected windows.
6570 If the minibuffer is active, the `minibuffer-scroll-window' mode line
6571 is displayed in the `mode-line' face. */);
6572 mode_line_in_non_selected_windows = 1;
6573
6574 DEFVAR_LISP ("other-window-scroll-buffer", Vother_window_scroll_buffer,
6575 doc: /* If non-nil, this is a buffer and \\[scroll-other-window] should scroll its window. */);
6576 Vother_window_scroll_buffer = Qnil;
6577
6578 DEFVAR_BOOL ("auto-window-vscroll", auto_window_vscroll_p,
6579 doc: /* Non-nil means to automatically adjust `window-vscroll' to view tall lines. */);
6580 auto_window_vscroll_p = 1;
6581
6582 DEFVAR_INT ("next-screen-context-lines", next_screen_context_lines,
6583 doc: /* Number of lines of continuity when scrolling by screenfuls. */);
6584 next_screen_context_lines = 2;
6585
6586 DEFVAR_LISP ("scroll-preserve-screen-position",
6587 Vscroll_preserve_screen_position,
6588 doc: /* Controls if scroll commands move point to keep its screen position unchanged.
6589 A value of nil means point does not keep its screen position except
6590 at the scroll margin or window boundary respectively.
6591 A value of t means point keeps its screen position if the scroll
6592 command moved it vertically out of the window, e.g. when scrolling
6593 by full screens.
6594 Any other value means point always keeps its screen position.
6595 Scroll commands should have the `scroll-command' property
6596 on their symbols to be controlled by this variable. */);
6597 Vscroll_preserve_screen_position = Qnil;
6598
6599 DEFVAR_LISP ("window-point-insertion-type", Vwindow_point_insertion_type,
6600 doc: /* Type of marker to use for `window-point'. */);
6601 Vwindow_point_insertion_type = Qnil;
6602
6603 DEFVAR_LISP ("window-configuration-change-hook",
6604 Vwindow_configuration_change_hook,
6605 doc: /* Functions to call when window configuration changes.
6606 The buffer-local part is run once per window, with the relevant window
6607 selected; while the global part is run only once for the modified frame,
6608 with the relevant frame selected. */);
6609 Vwindow_configuration_change_hook = Qnil;
6610
6611 DEFVAR_LISP ("recenter-redisplay", Vrecenter_redisplay,
6612 doc: /* Non-nil means `recenter' redraws entire frame.
6613 If this option is non-nil, then the `recenter' command with a nil
6614 argument will redraw the entire frame; the special value `tty' causes
6615 the frame to be redrawn only if it is a tty frame. */);
6616 Vrecenter_redisplay = Qtty;
6617
6618 DEFVAR_LISP ("window-combination-resize", Vwindow_combination_resize,
6619 doc: /* If t, resize window combinations proportionally.
6620 If this variable is nil, splitting a window gets the entire screen space
6621 for displaying the new window from the window to split. Deleting and
6622 resizing a window preferably resizes one adjacent window only.
6623
6624 If this variable is t, splitting a window tries to get the space
6625 proportionally from all windows in the same combination. This also
6626 allows to split a window that is otherwise too small or of fixed size.
6627 Resizing and deleting a window proportionally resize all windows in the
6628 same combination.
6629
6630 Other values are reserved for future use.
6631
6632 This variable takes no effect if `window-combination-limit' is non-nil. */);
6633 Vwindow_combination_resize = Qnil;
6634
6635 DEFVAR_LISP ("window-combination-limit", Vwindow_combination_limit,
6636 doc: /* If t, splitting a window makes a new parent window.
6637 If this variable is nil, splitting a window will create a new parent
6638 window only if the window has no parent window or the window shall
6639 become a combination orthogonal to the one it is part of.
6640
6641 If this variable is t, splitting a window always creates a new parent
6642 window. If all splits behave this way, each frame's window tree is a
6643 binary tree and every window but the frame's root window has exactly one
6644 sibling.
6645
6646 Other values are reserved for future use.
6647
6648 The value of this variable is also assigned to the combination limit of
6649 the new parent window. The combination limit of a window can be
6650 retrieved via the function `window-combination-limit' and altered by the
6651 function `set-window-combination-limit'. */);
6652 Vwindow_combination_limit = Qnil;
6653
6654 DEFVAR_LISP ("window-persistent-parameters", Vwindow_persistent_parameters,
6655 doc: /* Alist of persistent window parameters.
6656 This alist specifies which window parameters shall get saved by
6657 `current-window-configuration' and `window-state-get' and subsequently
6658 restored to their previous values by `set-window-configuration' and
6659 `window-state-put'.
6660
6661 The car of each entry of this alist is the symbol specifying the
6662 parameter. The cdr is one of the following:
6663
6664 nil means the parameter is neither saved by `window-state-get' nor by
6665 `current-window-configuration'.
6666
6667 t means the parameter is saved by `current-window-configuration' and,
6668 provided its WRITABLE argument is nil, by `window-state-get'.
6669
6670 The symbol `writable' means the parameter is saved unconditionally by
6671 both `current-window-configuration' and `window-state-get'. Do not use
6672 this value for parameters without read syntax (like windows or frames).
6673
6674 Parameters not saved by `current-window-configuration' or
6675 `window-state-get' are left alone by `set-window-configuration'
6676 respectively are not installed by `window-state-put'. */);
6677 Vwindow_persistent_parameters = list1 (Fcons (Qclone_of, Qt));
6678
6679 defsubr (&Sselected_window);
6680 defsubr (&Sminibuffer_window);
6681 defsubr (&Swindow_minibuffer_p);
6682 defsubr (&Swindowp);
6683 defsubr (&Swindow_live_p);
6684 defsubr (&Swindow_frame);
6685 defsubr (&Sframe_root_window);
6686 defsubr (&Sframe_first_window);
6687 defsubr (&Sframe_selected_window);
6688 defsubr (&Sset_frame_selected_window);
6689 defsubr (&Spos_visible_in_window_p);
6690 defsubr (&Swindow_line_height);
6691 defsubr (&Swindow_buffer);
6692 defsubr (&Swindow_parent);
6693 defsubr (&Swindow_top_child);
6694 defsubr (&Swindow_left_child);
6695 defsubr (&Swindow_next_sibling);
6696 defsubr (&Swindow_prev_sibling);
6697 defsubr (&Swindow_combination_limit);
6698 defsubr (&Sset_window_combination_limit);
6699 defsubr (&Swindow_use_time);
6700 defsubr (&Swindow_top_line);
6701 defsubr (&Swindow_left_column);
6702 defsubr (&Swindow_total_height);
6703 defsubr (&Swindow_total_width);
6704 defsubr (&Swindow_normal_size);
6705 defsubr (&Swindow_new_total);
6706 defsubr (&Swindow_new_normal);
6707 defsubr (&Sset_window_new_total);
6708 defsubr (&Sset_window_new_normal);
6709 defsubr (&Swindow_resize_apply);
6710 defsubr (&Swindow_body_height);
6711 defsubr (&Swindow_body_width);
6712 defsubr (&Swindow_hscroll);
6713 defsubr (&Sset_window_hscroll);
6714 defsubr (&Swindow_redisplay_end_trigger);
6715 defsubr (&Sset_window_redisplay_end_trigger);
6716 defsubr (&Swindow_edges);
6717 defsubr (&Swindow_pixel_edges);
6718 defsubr (&Swindow_absolute_pixel_edges);
6719 defsubr (&Swindow_inside_edges);
6720 defsubr (&Swindow_inside_pixel_edges);
6721 defsubr (&Swindow_inside_absolute_pixel_edges);
6722 defsubr (&Scoordinates_in_window_p);
6723 defsubr (&Swindow_at);
6724 defsubr (&Swindow_point);
6725 defsubr (&Swindow_start);
6726 defsubr (&Swindow_end);
6727 defsubr (&Sset_window_point);
6728 defsubr (&Sset_window_start);
6729 defsubr (&Swindow_dedicated_p);
6730 defsubr (&Sset_window_dedicated_p);
6731 defsubr (&Swindow_display_table);
6732 defsubr (&Sset_window_display_table);
6733 defsubr (&Snext_window);
6734 defsubr (&Sprevious_window);
6735 defsubr (&Sget_buffer_window);
6736 defsubr (&Sdelete_other_windows_internal);
6737 defsubr (&Sdelete_window_internal);
6738 defsubr (&Sresize_mini_window_internal);
6739 defsubr (&Sset_window_buffer);
6740 defsubr (&Srun_window_configuration_change_hook);
6741 defsubr (&Sselect_window);
6742 defsubr (&Sforce_window_update);
6743 defsubr (&Stemp_output_buffer_show);
6744 defsubr (&Ssplit_window_internal);
6745 defsubr (&Sscroll_up);
6746 defsubr (&Sscroll_down);
6747 defsubr (&Sscroll_left);
6748 defsubr (&Sscroll_right);
6749 defsubr (&Sother_window_for_scrolling);
6750 defsubr (&Sscroll_other_window);
6751 defsubr (&Sminibuffer_selected_window);
6752 defsubr (&Srecenter);
6753 defsubr (&Swindow_text_height);
6754 defsubr (&Smove_to_window_line);
6755 defsubr (&Swindow_configuration_p);
6756 defsubr (&Swindow_configuration_frame);
6757 defsubr (&Sset_window_configuration);
6758 defsubr (&Scurrent_window_configuration);
6759 defsubr (&Sset_window_margins);
6760 defsubr (&Swindow_margins);
6761 defsubr (&Sset_window_fringes);
6762 defsubr (&Swindow_fringes);
6763 defsubr (&Sset_window_scroll_bars);
6764 defsubr (&Swindow_scroll_bars);
6765 defsubr (&Swindow_vscroll);
6766 defsubr (&Sset_window_vscroll);
6767 defsubr (&Scompare_window_configurations);
6768 defsubr (&Swindow_list);
6769 defsubr (&Swindow_list_1);
6770 defsubr (&Swindow_prev_buffers);
6771 defsubr (&Sset_window_prev_buffers);
6772 defsubr (&Swindow_next_buffers);
6773 defsubr (&Sset_window_next_buffers);
6774 defsubr (&Swindow_parameters);
6775 defsubr (&Swindow_parameter);
6776 defsubr (&Sset_window_parameter);
6777 }
6778
6779 void
6780 keys_of_window (void)
6781 {
6782 initial_define_key (control_x_map, '<', "scroll-left");
6783 initial_define_key (control_x_map, '>', "scroll-right");
6784
6785 initial_define_key (global_map, Ctl ('V'), "scroll-up-command");
6786 initial_define_key (meta_map, Ctl ('V'), "scroll-other-window");
6787 initial_define_key (meta_map, 'v', "scroll-down-command");
6788 }