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