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