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