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