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