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