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