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