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