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