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