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