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