xref: /vim-8.2.3635/src/window.c (revision 910f66f9)
1 /* vi:set ts=8 sts=4 sw=4:
2  *
3  * VIM - Vi IMproved	by Bram Moolenaar
4  *
5  * Do ":help uganda"  in Vim to read a list of people who contributed.
6  * Do ":help credits" in Vim to see a list of people who contributed.
7  * See README.txt for an overview of the Vim source code.
8  */
9 
10 #include "vim.h"
11 
12 #ifdef HAVE_FCNTL_H
13 # include <fcntl.h>	    /* for chdir() */
14 #endif
15 
16 static int path_is_url __ARGS((char_u *p));
17 #if defined(FEAT_WINDOWS) || defined(PROTO)
18 static int win_split_ins __ARGS((int size, int flags, win_T *newwin, int dir));
19 static void win_init __ARGS((win_T *newp, win_T *oldp));
20 static void frame_comp_pos __ARGS((frame_T *topfrp, int *row, int *col));
21 static void frame_setheight __ARGS((frame_T *curfrp, int height));
22 #ifdef FEAT_VERTSPLIT
23 static void frame_setwidth __ARGS((frame_T *curfrp, int width));
24 #endif
25 static void win_exchange __ARGS((long));
26 static void win_rotate __ARGS((int, int));
27 static void win_totop __ARGS((int size, int flags));
28 static void win_equal_rec __ARGS((win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height));
29 static int last_window __ARGS((void));
30 static win_T *win_free_mem __ARGS((win_T *win, int *dirp, tabpage_T *tp));
31 static win_T *winframe_remove __ARGS((win_T *win, int *dirp, tabpage_T *tp));
32 static frame_T *win_altframe __ARGS((win_T *win, tabpage_T *tp));
33 static tabpage_T *alt_tabpage __ARGS((void));
34 static win_T *frame2win __ARGS((frame_T *frp));
35 static int frame_has_win __ARGS((frame_T *frp, win_T *wp));
36 static void frame_new_height __ARGS((frame_T *topfrp, int height, int topfirst, int wfh));
37 static int frame_fixed_height __ARGS((frame_T *frp));
38 #ifdef FEAT_VERTSPLIT
39 static int frame_fixed_width __ARGS((frame_T *frp));
40 static void frame_add_statusline __ARGS((frame_T *frp));
41 static void frame_new_width __ARGS((frame_T *topfrp, int width, int leftfirst, int wfw));
42 static void frame_add_vsep __ARGS((frame_T *frp));
43 static int frame_minwidth __ARGS((frame_T *topfrp, win_T *next_curwin));
44 static void frame_fix_width __ARGS((win_T *wp));
45 #endif
46 #endif
47 static int win_alloc_firstwin __ARGS((win_T *oldwin));
48 #if defined(FEAT_WINDOWS) || defined(PROTO)
49 static tabpage_T *alloc_tabpage __ARGS((void));
50 static void free_tabpage __ARGS((tabpage_T *tp));
51 static int leave_tabpage __ARGS((buf_T *new_curbuf));
52 static void enter_tabpage __ARGS((tabpage_T *tp, buf_T *old_curbuf));
53 static void frame_fix_height __ARGS((win_T *wp));
54 static int frame_minheight __ARGS((frame_T *topfrp, win_T *next_curwin));
55 static void win_enter_ext __ARGS((win_T *wp, int undo_sync, int no_curwin));
56 static void win_free __ARGS((win_T *wp, tabpage_T *tp));
57 static void win_append __ARGS((win_T *, win_T *));
58 static void win_remove __ARGS((win_T *, tabpage_T *tp));
59 static void frame_append __ARGS((frame_T *after, frame_T *frp));
60 static void frame_insert __ARGS((frame_T *before, frame_T *frp));
61 static void frame_remove __ARGS((frame_T *frp));
62 #ifdef FEAT_VERTSPLIT
63 static void win_new_width __ARGS((win_T *wp, int width));
64 static void win_goto_ver __ARGS((int up, long count));
65 static void win_goto_hor __ARGS((int left, long count));
66 #endif
67 static void frame_add_height __ARGS((frame_T *frp, int n));
68 static void last_status_rec __ARGS((frame_T *fr, int statusline));
69 
70 static void make_snapshot __ARGS((void));
71 static void make_snapshot_rec __ARGS((frame_T *fr, frame_T **frp));
72 static void clear_snapshot __ARGS((tabpage_T *tp));
73 static void clear_snapshot_rec __ARGS((frame_T *fr));
74 static void restore_snapshot __ARGS((int close_curwin));
75 static int check_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
76 static win_T *restore_snapshot_rec __ARGS((frame_T *sn, frame_T *fr));
77 
78 #endif /* FEAT_WINDOWS */
79 static win_T *win_alloc __ARGS((win_T *after));
80 static void win_new_height __ARGS((win_T *, int));
81 
82 #define URL_SLASH	1		/* path_is_url() has found "://" */
83 #define URL_BACKSLASH	2		/* path_is_url() has found ":\\" */
84 
85 #define NOWIN		(win_T *)-1	/* non-exisiting window */
86 
87 #ifdef FEAT_WINDOWS
88 static long p_ch_used = 1L;		/* value of 'cmdheight' when frame
89 					   size was set */
90 # define ROWS_AVAIL (Rows - p_ch - tabline_height())
91 #else
92 # define ROWS_AVAIL (Rows - p_ch)
93 #endif
94 
95 #if defined(FEAT_WINDOWS) || defined(PROTO)
96 /*
97  * all CTRL-W window commands are handled here, called from normal_cmd().
98  */
99     void
100 do_window(nchar, Prenum, xchar)
101     int		nchar;
102     long	Prenum;
103     int		xchar;	    /* extra char from ":wincmd gx" or NUL */
104 {
105     long	Prenum1;
106     win_T	*wp;
107 #if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
108     char_u	*ptr;
109     linenr_T    lnum = -1;
110 #endif
111 #ifdef FEAT_FIND_ID
112     int		type = FIND_DEFINE;
113     int		len;
114 #endif
115     char_u	cbuf[40];
116 
117     if (Prenum == 0)
118 	Prenum1 = 1;
119     else
120 	Prenum1 = Prenum;
121 
122 #ifdef FEAT_CMDWIN
123 # define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
124 #else
125 # define CHECK_CMDWIN
126 #endif
127 
128     switch (nchar)
129     {
130 /* split current window in two parts, horizontally */
131     case 'S':
132     case Ctrl_S:
133     case 's':
134 		CHECK_CMDWIN
135 #ifdef FEAT_VISUAL
136 		reset_VIsual_and_resel();	/* stop Visual mode */
137 #endif
138 #ifdef FEAT_QUICKFIX
139 		/* When splitting the quickfix window open a new buffer in it,
140 		 * don't replicate the quickfix buffer. */
141 		if (bt_quickfix(curbuf))
142 		    goto newwindow;
143 #endif
144 #ifdef FEAT_GUI
145 		need_mouse_correct = TRUE;
146 #endif
147 		win_split((int)Prenum, 0);
148 		break;
149 
150 #ifdef FEAT_VERTSPLIT
151 /* split current window in two parts, vertically */
152     case Ctrl_V:
153     case 'v':
154 		CHECK_CMDWIN
155 #ifdef FEAT_VISUAL
156 		reset_VIsual_and_resel();	/* stop Visual mode */
157 #endif
158 #ifdef FEAT_GUI
159 		need_mouse_correct = TRUE;
160 #endif
161 		win_split((int)Prenum, WSP_VERT);
162 		break;
163 #endif
164 
165 /* split current window and edit alternate file */
166     case Ctrl_HAT:
167     case '^':
168 		CHECK_CMDWIN
169 #ifdef FEAT_VISUAL
170 		reset_VIsual_and_resel();	/* stop Visual mode */
171 #endif
172 		STRCPY(cbuf, "split #");
173 		if (Prenum)
174 		    sprintf((char *)cbuf + 7, "%ld", Prenum);
175 		do_cmdline_cmd(cbuf);
176 		break;
177 
178 /* open new window */
179     case Ctrl_N:
180     case 'n':
181 		CHECK_CMDWIN
182 #ifdef FEAT_VISUAL
183 		reset_VIsual_and_resel();	/* stop Visual mode */
184 #endif
185 #ifdef FEAT_QUICKFIX
186 newwindow:
187 #endif
188 		if (Prenum)
189 		    sprintf((char *)cbuf, "%ld", Prenum); /* window height */
190 		else
191 		    cbuf[0] = NUL;
192 		STRCAT(cbuf, "new");
193 		do_cmdline_cmd(cbuf);
194 		break;
195 
196 /* quit current window */
197     case Ctrl_Q:
198     case 'q':
199 #ifdef FEAT_VISUAL
200 		reset_VIsual_and_resel();	/* stop Visual mode */
201 #endif
202 		do_cmdline_cmd((char_u *)"quit");
203 		break;
204 
205 /* close current window */
206     case Ctrl_C:
207     case 'c':
208 #ifdef FEAT_VISUAL
209 		reset_VIsual_and_resel();	/* stop Visual mode */
210 #endif
211 		do_cmdline_cmd((char_u *)"close");
212 		break;
213 
214 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
215 /* close preview window */
216     case Ctrl_Z:
217     case 'z':
218 		CHECK_CMDWIN
219 #ifdef FEAT_VISUAL
220 		reset_VIsual_and_resel();	/* stop Visual mode */
221 #endif
222 		do_cmdline_cmd((char_u *)"pclose");
223 		break;
224 
225 /* cursor to preview window */
226     case 'P':
227 		for (wp = firstwin; wp != NULL; wp = wp->w_next)
228 		    if (wp->w_p_pvw)
229 			break;
230 		if (wp == NULL)
231 		    EMSG(_("E441: There is no preview window"));
232 		else
233 		    win_goto(wp);
234 		break;
235 #endif
236 
237 /* close all but current window */
238     case Ctrl_O:
239     case 'o':
240 		CHECK_CMDWIN
241 #ifdef FEAT_VISUAL
242 		reset_VIsual_and_resel();	/* stop Visual mode */
243 #endif
244 		do_cmdline_cmd((char_u *)"only");
245 		break;
246 
247 /* cursor to next window with wrap around */
248     case Ctrl_W:
249     case 'w':
250 /* cursor to previous window with wrap around */
251     case 'W':
252 		CHECK_CMDWIN
253 		if (lastwin == firstwin && Prenum != 1)	/* just one window */
254 		    beep_flush();
255 		else
256 		{
257 		    if (Prenum)			/* go to specified window */
258 		    {
259 			for (wp = firstwin; --Prenum > 0; )
260 			{
261 			    if (wp->w_next == NULL)
262 				break;
263 			    else
264 				wp = wp->w_next;
265 			}
266 		    }
267 		    else
268 		    {
269 			if (nchar == 'W')	    /* go to previous window */
270 			{
271 			    wp = curwin->w_prev;
272 			    if (wp == NULL)
273 				wp = lastwin;	    /* wrap around */
274 			}
275 			else			    /* go to next window */
276 			{
277 			    wp = curwin->w_next;
278 			    if (wp == NULL)
279 				wp = firstwin;	    /* wrap around */
280 			}
281 		    }
282 		    win_goto(wp);
283 		}
284 		break;
285 
286 /* cursor to window below */
287     case 'j':
288     case K_DOWN:
289     case Ctrl_J:
290 		CHECK_CMDWIN
291 #ifdef FEAT_VERTSPLIT
292 		win_goto_ver(FALSE, Prenum1);
293 #else
294 		for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
295 							    wp = wp->w_next)
296 		    ;
297 		win_goto(wp);
298 #endif
299 		break;
300 
301 /* cursor to window above */
302     case 'k':
303     case K_UP:
304     case Ctrl_K:
305 		CHECK_CMDWIN
306 #ifdef FEAT_VERTSPLIT
307 		win_goto_ver(TRUE, Prenum1);
308 #else
309 		for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
310 							    wp = wp->w_prev)
311 		    ;
312 		win_goto(wp);
313 #endif
314 		break;
315 
316 #ifdef FEAT_VERTSPLIT
317 /* cursor to left window */
318     case 'h':
319     case K_LEFT:
320     case Ctrl_H:
321     case K_BS:
322 		CHECK_CMDWIN
323 		win_goto_hor(TRUE, Prenum1);
324 		break;
325 
326 /* cursor to right window */
327     case 'l':
328     case K_RIGHT:
329     case Ctrl_L:
330 		CHECK_CMDWIN
331 		win_goto_hor(FALSE, Prenum1);
332 		break;
333 #endif
334 
335 /* cursor to top-left window */
336     case 't':
337     case Ctrl_T:
338 		win_goto(firstwin);
339 		break;
340 
341 /* cursor to bottom-right window */
342     case 'b':
343     case Ctrl_B:
344 		win_goto(lastwin);
345 		break;
346 
347 /* cursor to last accessed (previous) window */
348     case 'p':
349     case Ctrl_P:
350 		if (prevwin == NULL)
351 		    beep_flush();
352 		else
353 		    win_goto(prevwin);
354 		break;
355 
356 /* exchange current and next window */
357     case 'x':
358     case Ctrl_X:
359 		CHECK_CMDWIN
360 		win_exchange(Prenum);
361 		break;
362 
363 /* rotate windows downwards */
364     case Ctrl_R:
365     case 'r':
366 		CHECK_CMDWIN
367 #ifdef FEAT_VISUAL
368 		reset_VIsual_and_resel();	/* stop Visual mode */
369 #endif
370 		win_rotate(FALSE, (int)Prenum1);    /* downwards */
371 		break;
372 
373 /* rotate windows upwards */
374     case 'R':
375 		CHECK_CMDWIN
376 #ifdef FEAT_VISUAL
377 		reset_VIsual_and_resel();	/* stop Visual mode */
378 #endif
379 		win_rotate(TRUE, (int)Prenum1);	    /* upwards */
380 		break;
381 
382 /* move window to the very top/bottom/left/right */
383     case 'K':
384     case 'J':
385 #ifdef FEAT_VERTSPLIT
386     case 'H':
387     case 'L':
388 #endif
389 		CHECK_CMDWIN
390 		win_totop((int)Prenum,
391 			((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
392 			| ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
393 		break;
394 
395 /* make all windows the same height */
396     case '=':
397 #ifdef FEAT_GUI
398 		need_mouse_correct = TRUE;
399 #endif
400 		win_equal(NULL, FALSE, 'b');
401 		break;
402 
403 /* increase current window height */
404     case '+':
405 #ifdef FEAT_GUI
406 		need_mouse_correct = TRUE;
407 #endif
408 		win_setheight(curwin->w_height + (int)Prenum1);
409 		break;
410 
411 /* decrease current window height */
412     case '-':
413 #ifdef FEAT_GUI
414 		need_mouse_correct = TRUE;
415 #endif
416 		win_setheight(curwin->w_height - (int)Prenum1);
417 		break;
418 
419 /* set current window height */
420     case Ctrl__:
421     case '_':
422 #ifdef FEAT_GUI
423 		need_mouse_correct = TRUE;
424 #endif
425 		win_setheight(Prenum ? (int)Prenum : 9999);
426 		break;
427 
428 #ifdef FEAT_VERTSPLIT
429 /* increase current window width */
430     case '>':
431 #ifdef FEAT_GUI
432 		need_mouse_correct = TRUE;
433 #endif
434 		win_setwidth(curwin->w_width + (int)Prenum1);
435 		break;
436 
437 /* decrease current window width */
438     case '<':
439 #ifdef FEAT_GUI
440 		need_mouse_correct = TRUE;
441 #endif
442 		win_setwidth(curwin->w_width - (int)Prenum1);
443 		break;
444 
445 /* set current window width */
446     case '|':
447 #ifdef FEAT_GUI
448 		need_mouse_correct = TRUE;
449 #endif
450 		win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
451 		break;
452 #endif
453 
454 /* jump to tag and split window if tag exists (in preview window) */
455 #if defined(FEAT_QUICKFIX)
456     case '}':
457 		CHECK_CMDWIN
458 		if (Prenum)
459 		    g_do_tagpreview = Prenum;
460 		else
461 		    g_do_tagpreview = p_pvh;
462 		/*FALLTHROUGH*/
463 #endif
464     case ']':
465     case Ctrl_RSB:
466 		CHECK_CMDWIN
467 #ifdef FEAT_VISUAL
468 		reset_VIsual_and_resel();	/* stop Visual mode */
469 #endif
470 		if (Prenum)
471 		    postponed_split = Prenum;
472 		else
473 		    postponed_split = -1;
474 
475 		/* Execute the command right here, required when
476 		 * "wincmd ]" was used in a function. */
477 		do_nv_ident(Ctrl_RSB, NUL);
478 		break;
479 
480 #ifdef FEAT_SEARCHPATH
481 /* edit file name under cursor in a new window */
482     case 'f':
483     case 'F':
484     case Ctrl_F:
485 		CHECK_CMDWIN
486 
487 		ptr = grab_file_name(Prenum1, &lnum);
488 		if (ptr != NULL)
489 		{
490 #ifdef FEAT_GUI
491 		    need_mouse_correct = TRUE;
492 #endif
493 		    setpcmark();
494 		    if (win_split(0, 0) == OK)
495 		    {
496 # ifdef FEAT_SCROLLBIND
497 			curwin->w_p_scb = FALSE;
498 # endif
499 			(void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE);
500 			if (nchar == 'F' && lnum >= 0)
501 			{
502 			    curwin->w_cursor.lnum = lnum;
503 			    check_cursor_lnum();
504 			    beginline(BL_SOL | BL_FIX);
505 			}
506 		    }
507 		    vim_free(ptr);
508 		}
509 		break;
510 #endif
511 
512 #ifdef FEAT_FIND_ID
513 /* Go to the first occurence of the identifier under cursor along path in a
514  * new window -- webb
515  */
516     case 'i':			    /* Go to any match */
517     case Ctrl_I:
518 		type = FIND_ANY;
519 		/* FALLTHROUGH */
520     case 'd':			    /* Go to definition, using 'define' */
521     case Ctrl_D:
522 		CHECK_CMDWIN
523 		if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
524 		    break;
525 		find_pattern_in_path(ptr, 0, len, TRUE,
526 			Prenum == 0 ? TRUE : FALSE, type,
527 			Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
528 		curwin->w_set_curswant = TRUE;
529 		break;
530 #endif
531 
532     case K_KENTER:
533     case CAR:
534 #if defined(FEAT_QUICKFIX)
535 		/*
536 		 * In a quickfix window a <CR> jumps to the error under the
537 		 * cursor in a new window.
538 		 */
539 		if (bt_quickfix(curbuf))
540 		{
541 		    sprintf((char *)cbuf, "split +%ld%s",
542 				(long)curwin->w_cursor.lnum,
543 				(curwin->w_llist_ref == NULL) ? "cc" : "ll");
544 		    do_cmdline_cmd(cbuf);
545 		}
546 #endif
547 		break;
548 
549 
550 /* CTRL-W g  extended commands */
551     case 'g':
552     case Ctrl_G:
553 		CHECK_CMDWIN
554 #ifdef USE_ON_FLY_SCROLL
555 		dont_scroll = TRUE;		/* disallow scrolling here */
556 #endif
557 		++no_mapping;
558 		++allow_keys;   /* no mapping for xchar, but allow key codes */
559 		if (xchar == NUL)
560 		    xchar = safe_vgetc();
561 #ifdef FEAT_LANGMAP
562 		LANGMAP_ADJUST(xchar, TRUE);
563 #endif
564 		--no_mapping;
565 		--allow_keys;
566 #ifdef FEAT_CMDL_INFO
567 		(void)add_to_showcmd(xchar);
568 #endif
569 		switch (xchar)
570 		{
571 #if defined(FEAT_QUICKFIX)
572 		    case '}':
573 			xchar = Ctrl_RSB;
574 			if (Prenum)
575 			    g_do_tagpreview = Prenum;
576 			else
577 			    g_do_tagpreview = p_pvh;
578 			/*FALLTHROUGH*/
579 #endif
580 		    case ']':
581 		    case Ctrl_RSB:
582 #ifdef FEAT_VISUAL
583 			reset_VIsual_and_resel();	/* stop Visual mode */
584 #endif
585 			if (Prenum)
586 			    postponed_split = Prenum;
587 			else
588 			    postponed_split = -1;
589 
590 			/* Execute the command right here, required when
591 			 * "wincmd g}" was used in a function. */
592 			do_nv_ident('g', xchar);
593 			break;
594 
595 		    default:
596 			beep_flush();
597 			break;
598 		}
599 		break;
600 
601     default:	beep_flush();
602 		break;
603     }
604 }
605 
606 /*
607  * split the current window, implements CTRL-W s and :split
608  *
609  * "size" is the height or width for the new window, 0 to use half of current
610  * height or width.
611  *
612  * "flags":
613  * WSP_ROOM: require enough room for new window
614  * WSP_VERT: vertical split.
615  * WSP_TOP:  open window at the top-left of the shell (help window).
616  * WSP_BOT:  open window at the bottom-right of the shell (quickfix window).
617  * WSP_HELP: creating the help window, keep layout snapshot
618  *
619  * return FAIL for failure, OK otherwise
620  */
621     int
622 win_split(size, flags)
623     int		size;
624     int		flags;
625 {
626     /* When the ":tab" modifier was used open a new tab page instead. */
627     if (may_open_tabpage() == OK)
628 	return OK;
629 
630     /* Add flags from ":vertical", ":topleft" and ":botright". */
631     flags |= cmdmod.split;
632     if ((flags & WSP_TOP) && (flags & WSP_BOT))
633     {
634 	EMSG(_("E442: Can't split topleft and botright at the same time"));
635 	return FAIL;
636     }
637 
638     /* When creating the help window make a snapshot of the window layout.
639      * Otherwise clear the snapshot, it's now invalid. */
640     if (flags & WSP_HELP)
641 	make_snapshot();
642     else
643 	clear_snapshot(curtab);
644 
645     return win_split_ins(size, flags, NULL, 0);
646 }
647 
648 /*
649  * When "newwin" is NULL: split the current window in two.
650  * When "newwin" is not NULL: insert this window at the far
651  * top/left/right/bottom.
652  * return FAIL for failure, OK otherwise
653  */
654     static int
655 win_split_ins(size, flags, newwin, dir)
656     int		size;
657     int		flags;
658     win_T	*newwin;
659     int		dir;
660 {
661     win_T	*wp = newwin;
662     win_T	*oldwin;
663     int		new_size = size;
664     int		i;
665     int		need_status = 0;
666     int		do_equal = FALSE;
667     int		needed;
668     int		available;
669     int		oldwin_height = 0;
670     int		layout;
671     frame_T	*frp, *curfrp;
672     int		before;
673 
674     if (flags & WSP_TOP)
675 	oldwin = firstwin;
676     else if (flags & WSP_BOT)
677 	oldwin = lastwin;
678     else
679 	oldwin = curwin;
680 
681     /* add a status line when p_ls == 1 and splitting the first window */
682     if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
683     {
684 	if (oldwin->w_height <= p_wmh && newwin == NULL)
685 	{
686 	    EMSG(_(e_noroom));
687 	    return FAIL;
688 	}
689 	need_status = STATUS_HEIGHT;
690     }
691 
692 #ifdef FEAT_VERTSPLIT
693     if (flags & WSP_VERT)
694     {
695 	layout = FR_ROW;
696 	do_equal = (p_ea && new_size == 0 && *p_ead != 'v');
697 
698 	/*
699 	 * Check if we are able to split the current window and compute its
700 	 * width.
701 	 */
702 	needed = p_wmw + 1;
703 	if (flags & WSP_ROOM)
704 	    needed += p_wiw - p_wmw;
705 	if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
706 	{
707 	    available = topframe->fr_width;
708 	    needed += frame_minwidth(topframe, NULL);
709 	}
710 	else
711 	    available = oldwin->w_width;
712 	if (available < needed && newwin == NULL)
713 	{
714 	    EMSG(_(e_noroom));
715 	    return FAIL;
716 	}
717 	if (new_size == 0)
718 	    new_size = oldwin->w_width / 2;
719 	if (new_size > oldwin->w_width - p_wmw - 1)
720 	    new_size = oldwin->w_width - p_wmw - 1;
721 	if (new_size < p_wmw)
722 	    new_size = p_wmw;
723 
724 	/* if it doesn't fit in the current window, need win_equal() */
725 	if (oldwin->w_width - new_size - 1 < p_wmw)
726 	    do_equal = TRUE;
727 
728 	/* We don't like to take lines for the new window from a
729 	 * 'winfixwidth' window.  Take them from a window to the left or right
730 	 * instead, if possible. */
731 	if (oldwin->w_p_wfw)
732 	    win_setwidth_win(oldwin->w_width + new_size, oldwin);
733     }
734     else
735 #endif
736     {
737 	layout = FR_COL;
738 	do_equal = (p_ea && new_size == 0
739 #ifdef FEAT_VERTSPLIT
740 		&& *p_ead != 'h'
741 #endif
742 		);
743 
744 	/*
745 	 * Check if we are able to split the current window and compute its
746 	 * height.
747 	 */
748 	needed = p_wmh + STATUS_HEIGHT + need_status;
749 	if (flags & WSP_ROOM)
750 	    needed += p_wh - p_wmh;
751 	if (p_ea || (flags & (WSP_BOT | WSP_TOP)))
752 	{
753 	    available = topframe->fr_height;
754 	    needed += frame_minheight(topframe, NULL);
755 	}
756 	else
757 	{
758 	    available = oldwin->w_height;
759 	    needed += p_wmh;
760 	}
761 	if (available < needed && newwin == NULL)
762 	{
763 	    EMSG(_(e_noroom));
764 	    return FAIL;
765 	}
766 	oldwin_height = oldwin->w_height;
767 	if (need_status)
768 	{
769 	    oldwin->w_status_height = STATUS_HEIGHT;
770 	    oldwin_height -= STATUS_HEIGHT;
771 	}
772 	if (new_size == 0)
773 	    new_size = oldwin_height / 2;
774 
775 	if (new_size > oldwin_height - p_wmh - STATUS_HEIGHT)
776 	    new_size = oldwin_height - p_wmh - STATUS_HEIGHT;
777 	if (new_size < p_wmh)
778 	    new_size = p_wmh;
779 
780 	/* if it doesn't fit in the current window, need win_equal() */
781 	if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
782 	    do_equal = TRUE;
783 
784 	/* We don't like to take lines for the new window from a
785 	 * 'winfixheight' window.  Take them from a window above or below
786 	 * instead, if possible. */
787 	if (oldwin->w_p_wfh)
788 	{
789 	    win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
790 								      oldwin);
791 	    oldwin_height = oldwin->w_height;
792 	    if (need_status)
793 		oldwin_height -= STATUS_HEIGHT;
794 	}
795     }
796 
797     /*
798      * allocate new window structure and link it in the window list
799      */
800     if ((flags & WSP_TOP) == 0
801 	    && ((flags & WSP_BOT)
802 		|| (flags & WSP_BELOW)
803 		|| (!(flags & WSP_ABOVE)
804 		    && (
805 #ifdef FEAT_VERTSPLIT
806 			(flags & WSP_VERT) ? p_spr :
807 #endif
808 			p_sb))))
809     {
810 	/* new window below/right of current one */
811 	if (newwin == NULL)
812 	    wp = win_alloc(oldwin);
813 	else
814 	    win_append(oldwin, wp);
815     }
816     else
817     {
818 	if (newwin == NULL)
819 	    wp = win_alloc(oldwin->w_prev);
820 	else
821 	    win_append(oldwin->w_prev, wp);
822     }
823 
824     if (newwin == NULL)
825     {
826 	if (wp == NULL)
827 	    return FAIL;
828 
829 	/* make the contents of the new window the same as the current one */
830 	win_init(wp, curwin);
831     }
832 
833     /*
834      * Reorganise the tree of frames to insert the new window.
835      */
836     if (flags & (WSP_TOP | WSP_BOT))
837     {
838 #ifdef FEAT_VERTSPLIT
839 	if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
840 	    || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
841 #else
842 	if (topframe->fr_layout == FR_COL)
843 #endif
844 	{
845 	    curfrp = topframe->fr_child;
846 	    if (flags & WSP_BOT)
847 		while (curfrp->fr_next != NULL)
848 		    curfrp = curfrp->fr_next;
849 	}
850 	else
851 	    curfrp = topframe;
852 	before = (flags & WSP_TOP);
853     }
854     else
855     {
856 	curfrp = oldwin->w_frame;
857 	if (flags & WSP_BELOW)
858 	    before = FALSE;
859 	else if (flags & WSP_ABOVE)
860 	    before = TRUE;
861 	else
862 #ifdef FEAT_VERTSPLIT
863 	if (flags & WSP_VERT)
864 	    before = !p_spr;
865 	else
866 #endif
867 	    before = !p_sb;
868     }
869     if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
870     {
871 	/* Need to create a new frame in the tree to make a branch. */
872 	frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
873 	*frp = *curfrp;
874 	curfrp->fr_layout = layout;
875 	frp->fr_parent = curfrp;
876 	frp->fr_next = NULL;
877 	frp->fr_prev = NULL;
878 	curfrp->fr_child = frp;
879 	curfrp->fr_win = NULL;
880 	curfrp = frp;
881 	if (frp->fr_win != NULL)
882 	    oldwin->w_frame = frp;
883 	else
884 	    for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
885 		frp->fr_parent = curfrp;
886     }
887 
888     if (newwin == NULL)
889     {
890 	/* Create a frame for the new window. */
891 	frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
892 	frp->fr_layout = FR_LEAF;
893 	frp->fr_win = wp;
894 	wp->w_frame = frp;
895     }
896     else
897 	frp = newwin->w_frame;
898     frp->fr_parent = curfrp->fr_parent;
899 
900     /* Insert the new frame at the right place in the frame list. */
901     if (before)
902 	frame_insert(curfrp, frp);
903     else
904 	frame_append(curfrp, frp);
905 
906 #ifdef FEAT_VERTSPLIT
907     if (flags & WSP_VERT)
908     {
909 	wp->w_p_scr = curwin->w_p_scr;
910 	if (need_status)
911 	{
912 	    --oldwin->w_height;
913 	    oldwin->w_status_height = need_status;
914 	}
915 	if (flags & (WSP_TOP | WSP_BOT))
916 	{
917 	    /* set height and row of new window to full height */
918 	    wp->w_winrow = tabline_height();
919 	    wp->w_height = curfrp->fr_height - (p_ls > 0);
920 	    wp->w_status_height = (p_ls > 0);
921 	}
922 	else
923 	{
924 	    /* height and row of new window is same as current window */
925 	    wp->w_winrow = oldwin->w_winrow;
926 	    wp->w_height = oldwin->w_height;
927 	    wp->w_status_height = oldwin->w_status_height;
928 	}
929 	frp->fr_height = curfrp->fr_height;
930 
931 	/* "new_size" of the current window goes to the new window, use
932 	 * one column for the vertical separator */
933 	wp->w_width = new_size;
934 	if (before)
935 	    wp->w_vsep_width = 1;
936 	else
937 	{
938 	    wp->w_vsep_width = oldwin->w_vsep_width;
939 	    oldwin->w_vsep_width = 1;
940 	}
941 	if (flags & (WSP_TOP | WSP_BOT))
942 	{
943 	    if (flags & WSP_BOT)
944 		frame_add_vsep(curfrp);
945 	    /* Set width of neighbor frame */
946 	    frame_new_width(curfrp, curfrp->fr_width
947 		     - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
948 								       FALSE);
949 	}
950 	else
951 	    win_new_width(oldwin, oldwin->w_width - (new_size + 1));
952 	if (before)	/* new window left of current one */
953 	{
954 	    wp->w_wincol = oldwin->w_wincol;
955 	    oldwin->w_wincol += new_size + 1;
956 	}
957 	else		/* new window right of current one */
958 	    wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
959 	frame_fix_width(oldwin);
960 	frame_fix_width(wp);
961     }
962     else
963 #endif
964     {
965 	/* width and column of new window is same as current window */
966 #ifdef FEAT_VERTSPLIT
967 	if (flags & (WSP_TOP | WSP_BOT))
968 	{
969 	    wp->w_wincol = 0;
970 	    wp->w_width = Columns;
971 	    wp->w_vsep_width = 0;
972 	}
973 	else
974 	{
975 	    wp->w_wincol = oldwin->w_wincol;
976 	    wp->w_width = oldwin->w_width;
977 	    wp->w_vsep_width = oldwin->w_vsep_width;
978 	}
979 	frp->fr_width = curfrp->fr_width;
980 #endif
981 
982 	/* "new_size" of the current window goes to the new window, use
983 	 * one row for the status line */
984 	win_new_height(wp, new_size);
985 	if (flags & (WSP_TOP | WSP_BOT))
986 	    frame_new_height(curfrp, curfrp->fr_height
987 			- (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
988 	else
989 	    win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
990 	if (before)	/* new window above current one */
991 	{
992 	    wp->w_winrow = oldwin->w_winrow;
993 	    wp->w_status_height = STATUS_HEIGHT;
994 	    oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
995 	}
996 	else		/* new window below current one */
997 	{
998 	    wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
999 	    wp->w_status_height = oldwin->w_status_height;
1000 	    oldwin->w_status_height = STATUS_HEIGHT;
1001 	}
1002 #ifdef FEAT_VERTSPLIT
1003 	if (flags & WSP_BOT)
1004 	    frame_add_statusline(curfrp);
1005 #endif
1006 	frame_fix_height(wp);
1007 	frame_fix_height(oldwin);
1008     }
1009 
1010     if (flags & (WSP_TOP | WSP_BOT))
1011 	(void)win_comp_pos();
1012 
1013     /*
1014      * Both windows need redrawing
1015      */
1016     redraw_win_later(wp, NOT_VALID);
1017     wp->w_redr_status = TRUE;
1018     redraw_win_later(oldwin, NOT_VALID);
1019     oldwin->w_redr_status = TRUE;
1020 
1021     if (need_status)
1022     {
1023 	msg_row = Rows - 1;
1024 	msg_col = sc_col;
1025 	msg_clr_eos_force();	/* Old command/ruler may still be there */
1026 	comp_col();
1027 	msg_row = Rows - 1;
1028 	msg_col = 0;	/* put position back at start of line */
1029     }
1030 
1031     /*
1032      * make the new window the current window and redraw
1033      */
1034     if (do_equal || dir != 0)
1035 	win_equal(wp, TRUE,
1036 #ifdef FEAT_VERTSPLIT
1037 		(flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1038 		: dir == 'h' ? 'b' :
1039 #endif
1040 		'v');
1041 
1042     /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1043      * size was given. */
1044 #ifdef FEAT_VERTSPLIT
1045     if (flags & WSP_VERT)
1046     {
1047 	i = p_wiw;
1048 	if (size != 0)
1049 	    p_wiw = size;
1050 
1051 # ifdef FEAT_GUI
1052 	/* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1053 	if (gui.in_use)
1054 	    gui_init_which_components(NULL);
1055 # endif
1056     }
1057     else
1058 #endif
1059     {
1060 	i = p_wh;
1061 	if (size != 0)
1062 	    p_wh = size;
1063     }
1064     win_enter(wp, FALSE);
1065 #ifdef FEAT_VERTSPLIT
1066     if (flags & WSP_VERT)
1067 	p_wiw = i;
1068     else
1069 #endif
1070 	p_wh = i;
1071 
1072     return OK;
1073 }
1074 
1075 /*
1076  * Initialize window "newp" from window "oldp".
1077  * Used when splitting a window and when creating a new tab page.
1078  * The windows will both edit the same buffer.
1079  */
1080     static void
1081 win_init(newp, oldp)
1082     win_T	*newp;
1083     win_T	*oldp;
1084 {
1085     int		i;
1086 
1087     newp->w_buffer = oldp->w_buffer;
1088     oldp->w_buffer->b_nwindows++;
1089     newp->w_cursor = oldp->w_cursor;
1090     newp->w_valid = 0;
1091     newp->w_curswant = oldp->w_curswant;
1092     newp->w_set_curswant = oldp->w_set_curswant;
1093     newp->w_topline = oldp->w_topline;
1094 #ifdef FEAT_DIFF
1095     newp->w_topfill = oldp->w_topfill;
1096 #endif
1097     newp->w_leftcol = oldp->w_leftcol;
1098     newp->w_pcmark = oldp->w_pcmark;
1099     newp->w_prev_pcmark = oldp->w_prev_pcmark;
1100     newp->w_alt_fnum = oldp->w_alt_fnum;
1101     newp->w_fraction = oldp->w_fraction;
1102     newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1103 #ifdef FEAT_JUMPLIST
1104     copy_jumplist(oldp, newp);
1105 #endif
1106 #ifdef FEAT_QUICKFIX
1107     copy_loclist(oldp, newp);
1108 #endif
1109     if (oldp->w_localdir != NULL)
1110 	newp->w_localdir = vim_strsave(oldp->w_localdir);
1111 
1112     /* Use the same argument list. */
1113     newp->w_alist = oldp->w_alist;
1114     ++newp->w_alist->al_refcount;
1115     newp->w_arg_idx = oldp->w_arg_idx;
1116 
1117     /*
1118      * copy tagstack and options from existing window
1119      */
1120     for (i = 0; i < oldp->w_tagstacklen; i++)
1121     {
1122 	newp->w_tagstack[i] = oldp->w_tagstack[i];
1123 	if (newp->w_tagstack[i].tagname != NULL)
1124 	    newp->w_tagstack[i].tagname =
1125 				   vim_strsave(newp->w_tagstack[i].tagname);
1126     }
1127     newp->w_tagstackidx = oldp->w_tagstackidx;
1128     newp->w_tagstacklen = oldp->w_tagstacklen;
1129     win_copy_options(oldp, newp);
1130 # ifdef FEAT_FOLDING
1131     copyFoldingState(oldp, newp);
1132 # endif
1133 }
1134 
1135 #endif /* FEAT_WINDOWS */
1136 
1137 #if defined(FEAT_WINDOWS) || defined(PROTO)
1138 /*
1139  * Check if "win" is a pointer to an existing window.
1140  */
1141     int
1142 win_valid(win)
1143     win_T	*win;
1144 {
1145     win_T	*wp;
1146 
1147     if (win == NULL)
1148 	return FALSE;
1149     for (wp = firstwin; wp != NULL; wp = wp->w_next)
1150 	if (wp == win)
1151 	    return TRUE;
1152     return FALSE;
1153 }
1154 
1155 /*
1156  * Return the number of windows.
1157  */
1158     int
1159 win_count()
1160 {
1161     win_T	*wp;
1162     int		count = 0;
1163 
1164     for (wp = firstwin; wp != NULL; wp = wp->w_next)
1165 	++count;
1166     return count;
1167 }
1168 
1169 /*
1170  * Make "count" windows on the screen.
1171  * Return actual number of windows on the screen.
1172  * Must be called when there is just one window, filling the whole screen
1173  * (excluding the command line).
1174  */
1175 /*ARGSUSED*/
1176     int
1177 make_windows(count, vertical)
1178     int		count;
1179     int		vertical;	/* split windows vertically if TRUE */
1180 {
1181     int		maxcount;
1182     int		todo;
1183 
1184 #ifdef FEAT_VERTSPLIT
1185     if (vertical)
1186     {
1187 	/* Each windows needs at least 'winminwidth' lines and a separator
1188 	 * column. */
1189 	maxcount = (curwin->w_width + curwin->w_vsep_width
1190 					     - (p_wiw - p_wmw)) / (p_wmw + 1);
1191     }
1192     else
1193 #endif
1194     {
1195 	/* Each window needs at least 'winminheight' lines and a status line. */
1196 	maxcount = (curwin->w_height + curwin->w_status_height
1197 				  - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1198     }
1199 
1200     if (maxcount < 2)
1201 	maxcount = 2;
1202     if (count > maxcount)
1203 	count = maxcount;
1204 
1205     /*
1206      * add status line now, otherwise first window will be too big
1207      */
1208     if (count > 1)
1209 	last_status(TRUE);
1210 
1211 #ifdef FEAT_AUTOCMD
1212     /*
1213      * Don't execute autocommands while creating the windows.  Must do that
1214      * when putting the buffers in the windows.
1215      */
1216     ++autocmd_block;
1217 #endif
1218 
1219     /* todo is number of windows left to create */
1220     for (todo = count - 1; todo > 0; --todo)
1221 #ifdef FEAT_VERTSPLIT
1222 	if (vertical)
1223 	{
1224 	    if (win_split(curwin->w_width - (curwin->w_width - todo)
1225 			/ (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1226 		break;
1227 	}
1228 	else
1229 #endif
1230 	{
1231 	    if (win_split(curwin->w_height - (curwin->w_height - todo
1232 			    * STATUS_HEIGHT) / (todo + 1)
1233 			- STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1234 		break;
1235 	}
1236 
1237 #ifdef FEAT_AUTOCMD
1238     --autocmd_block;
1239 #endif
1240 
1241     /* return actual number of windows */
1242     return (count - todo);
1243 }
1244 
1245 /*
1246  * Exchange current and next window
1247  */
1248     static void
1249 win_exchange(Prenum)
1250     long	Prenum;
1251 {
1252     frame_T	*frp;
1253     frame_T	*frp2;
1254     win_T	*wp;
1255     win_T	*wp2;
1256     int		temp;
1257 
1258     if (lastwin == firstwin)	    /* just one window */
1259     {
1260 	beep_flush();
1261 	return;
1262     }
1263 
1264 #ifdef FEAT_GUI
1265     need_mouse_correct = TRUE;
1266 #endif
1267 
1268     /*
1269      * find window to exchange with
1270      */
1271     if (Prenum)
1272     {
1273 	frp = curwin->w_frame->fr_parent->fr_child;
1274 	while (frp != NULL && --Prenum > 0)
1275 	    frp = frp->fr_next;
1276     }
1277     else if (curwin->w_frame->fr_next != NULL)	/* Swap with next */
1278 	frp = curwin->w_frame->fr_next;
1279     else    /* Swap last window in row/col with previous */
1280 	frp = curwin->w_frame->fr_prev;
1281 
1282     /* We can only exchange a window with another window, not with a frame
1283      * containing windows. */
1284     if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1285 	return;
1286     wp = frp->fr_win;
1287 
1288 /*
1289  * 1. remove curwin from the list. Remember after which window it was in wp2
1290  * 2. insert curwin before wp in the list
1291  * if wp != wp2
1292  *    3. remove wp from the list
1293  *    4. insert wp after wp2
1294  * 5. exchange the status line height and vsep width.
1295  */
1296     wp2 = curwin->w_prev;
1297     frp2 = curwin->w_frame->fr_prev;
1298     if (wp->w_prev != curwin)
1299     {
1300 	win_remove(curwin, NULL);
1301 	frame_remove(curwin->w_frame);
1302 	win_append(wp->w_prev, curwin);
1303 	frame_insert(frp, curwin->w_frame);
1304     }
1305     if (wp != wp2)
1306     {
1307 	win_remove(wp, NULL);
1308 	frame_remove(wp->w_frame);
1309 	win_append(wp2, wp);
1310 	if (frp2 == NULL)
1311 	    frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1312 	else
1313 	    frame_append(frp2, wp->w_frame);
1314     }
1315     temp = curwin->w_status_height;
1316     curwin->w_status_height = wp->w_status_height;
1317     wp->w_status_height = temp;
1318 #ifdef FEAT_VERTSPLIT
1319     temp = curwin->w_vsep_width;
1320     curwin->w_vsep_width = wp->w_vsep_width;
1321     wp->w_vsep_width = temp;
1322 
1323     /* If the windows are not in the same frame, exchange the sizes to avoid
1324      * messing up the window layout.  Otherwise fix the frame sizes. */
1325     if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1326     {
1327 	temp = curwin->w_height;
1328 	curwin->w_height = wp->w_height;
1329 	wp->w_height = temp;
1330 	temp = curwin->w_width;
1331 	curwin->w_width = wp->w_width;
1332 	wp->w_width = temp;
1333     }
1334     else
1335     {
1336 	frame_fix_height(curwin);
1337 	frame_fix_height(wp);
1338 	frame_fix_width(curwin);
1339 	frame_fix_width(wp);
1340     }
1341 #endif
1342 
1343     (void)win_comp_pos();		/* recompute window positions */
1344 
1345     win_enter(wp, TRUE);
1346     redraw_later(CLEAR);
1347 }
1348 
1349 /*
1350  * rotate windows: if upwards TRUE the second window becomes the first one
1351  *		   if upwards FALSE the first window becomes the second one
1352  */
1353     static void
1354 win_rotate(upwards, count)
1355     int		upwards;
1356     int		count;
1357 {
1358     win_T	*wp1;
1359     win_T	*wp2;
1360     frame_T	*frp;
1361     int		n;
1362 
1363     if (firstwin == lastwin)		/* nothing to do */
1364     {
1365 	beep_flush();
1366 	return;
1367     }
1368 
1369 #ifdef FEAT_GUI
1370     need_mouse_correct = TRUE;
1371 #endif
1372 
1373 #ifdef FEAT_VERTSPLIT
1374     /* Check if all frames in this row/col have one window. */
1375     for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1376 							   frp = frp->fr_next)
1377 	if (frp->fr_win == NULL)
1378 	{
1379 	    EMSG(_("E443: Cannot rotate when another window is split"));
1380 	    return;
1381 	}
1382 #endif
1383 
1384     while (count--)
1385     {
1386 	if (upwards)		/* first window becomes last window */
1387 	{
1388 	    /* remove first window/frame from the list */
1389 	    frp = curwin->w_frame->fr_parent->fr_child;
1390 	    wp1 = frp->fr_win;
1391 	    win_remove(wp1, NULL);
1392 	    frame_remove(frp);
1393 
1394 	    /* find last frame and append removed window/frame after it */
1395 	    for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1396 		;
1397 	    win_append(frp->fr_win, wp1);
1398 	    frame_append(frp, wp1->w_frame);
1399 
1400 	    wp2 = frp->fr_win;		/* previously last window */
1401 	}
1402 	else			/* last window becomes first window */
1403 	{
1404 	    /* find last window/frame in the list and remove it */
1405 	    for (frp = curwin->w_frame; frp->fr_next != NULL;
1406 							   frp = frp->fr_next)
1407 		;
1408 	    wp1 = frp->fr_win;
1409 	    wp2 = wp1->w_prev;		    /* will become last window */
1410 	    win_remove(wp1, NULL);
1411 	    frame_remove(frp);
1412 
1413 	    /* append the removed window/frame before the first in the list */
1414 	    win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1415 	    frame_insert(frp->fr_parent->fr_child, frp);
1416 	}
1417 
1418 	/* exchange status height and vsep width of old and new last window */
1419 	n = wp2->w_status_height;
1420 	wp2->w_status_height = wp1->w_status_height;
1421 	wp1->w_status_height = n;
1422 	frame_fix_height(wp1);
1423 	frame_fix_height(wp2);
1424 #ifdef FEAT_VERTSPLIT
1425 	n = wp2->w_vsep_width;
1426 	wp2->w_vsep_width = wp1->w_vsep_width;
1427 	wp1->w_vsep_width = n;
1428 	frame_fix_width(wp1);
1429 	frame_fix_width(wp2);
1430 #endif
1431 
1432 	    /* recompute w_winrow and w_wincol for all windows */
1433 	(void)win_comp_pos();
1434     }
1435 
1436     redraw_later(CLEAR);
1437 }
1438 
1439 /*
1440  * Move the current window to the very top/bottom/left/right of the screen.
1441  */
1442     static void
1443 win_totop(size, flags)
1444     int		size;
1445     int		flags;
1446 {
1447     int		dir;
1448     int		height = curwin->w_height;
1449 
1450     if (lastwin == firstwin)
1451     {
1452 	beep_flush();
1453 	return;
1454     }
1455 
1456     /* Remove the window and frame from the tree of frames. */
1457     (void)winframe_remove(curwin, &dir, NULL);
1458     win_remove(curwin, NULL);
1459     last_status(FALSE);	    /* may need to remove last status line */
1460     (void)win_comp_pos();   /* recompute window positions */
1461 
1462     /* Split a window on the desired side and put the window there. */
1463     (void)win_split_ins(size, flags, curwin, dir);
1464     if (!(flags & WSP_VERT))
1465     {
1466 	win_setheight(height);
1467 	if (p_ea)
1468 	    win_equal(curwin, TRUE, 'v');
1469     }
1470 
1471 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1472     /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1473      * scrollbars.  Have to update them anyway. */
1474     if (gui.in_use)
1475     {
1476 	out_flush();
1477 	gui_init_which_components(NULL);
1478 	gui_update_scrollbars(TRUE);
1479     }
1480     need_mouse_correct = TRUE;
1481 #endif
1482 
1483 }
1484 
1485 /*
1486  * Move window "win1" to below/right of "win2" and make "win1" the current
1487  * window.  Only works within the same frame!
1488  */
1489     void
1490 win_move_after(win1, win2)
1491     win_T	*win1, *win2;
1492 {
1493     int		height;
1494 
1495     /* check if the arguments are reasonable */
1496     if (win1 == win2)
1497 	return;
1498 
1499     /* check if there is something to do */
1500     if (win2->w_next != win1)
1501     {
1502 	/* may need move the status line/vertical separator of the last window
1503 	 * */
1504 	if (win1 == lastwin)
1505 	{
1506 	    height = win1->w_prev->w_status_height;
1507 	    win1->w_prev->w_status_height = win1->w_status_height;
1508 	    win1->w_status_height = height;
1509 #ifdef FEAT_VERTSPLIT
1510 	    win1->w_prev->w_vsep_width = 0;
1511 	    win1->w_vsep_width = 1;
1512 #endif
1513 	}
1514 	else if (win2 == lastwin)
1515 	{
1516 	    height = win1->w_status_height;
1517 	    win1->w_status_height = win2->w_status_height;
1518 	    win2->w_status_height = height;
1519 #ifdef FEAT_VERTSPLIT
1520 	    win2->w_vsep_width = 1;
1521 	    win1->w_vsep_width = 0;
1522 #endif
1523 	}
1524 	win_remove(win1, NULL);
1525 	frame_remove(win1->w_frame);
1526 	win_append(win2, win1);
1527 	frame_append(win2->w_frame, win1->w_frame);
1528 
1529 	(void)win_comp_pos();	/* recompute w_winrow for all windows */
1530 	redraw_later(NOT_VALID);
1531     }
1532     win_enter(win1, FALSE);
1533 }
1534 
1535 /*
1536  * Make all windows the same height.
1537  * 'next_curwin' will soon be the current window, make sure it has enough
1538  * rows.
1539  */
1540     void
1541 win_equal(next_curwin, current, dir)
1542     win_T	*next_curwin;	/* pointer to current window to be or NULL */
1543     int		current;	/* do only frame with current window */
1544     int		dir;		/* 'v' for vertically, 'h' for horizontally,
1545 				   'b' for both, 0 for using p_ead */
1546 {
1547     if (dir == 0)
1548 #ifdef FEAT_VERTSPLIT
1549 	dir = *p_ead;
1550 #else
1551 	dir = 'b';
1552 #endif
1553     win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
1554 		      topframe, dir, 0, tabline_height(),
1555 					   (int)Columns, topframe->fr_height);
1556 }
1557 
1558 /*
1559  * Set a frame to a new position and height, spreading the available room
1560  * equally over contained frames.
1561  * The window "next_curwin" (if not NULL) should at least get the size from
1562  * 'winheight' and 'winwidth' if possible.
1563  */
1564     static void
1565 win_equal_rec(next_curwin, current, topfr, dir, col, row, width, height)
1566     win_T	*next_curwin;	/* pointer to current window to be or NULL */
1567     int		current;	/* do only frame with current window */
1568     frame_T	*topfr;		/* frame to set size off */
1569     int		dir;		/* 'v', 'h' or 'b', see win_equal() */
1570     int		col;		/* horizontal position for frame */
1571     int		row;		/* vertical position for frame */
1572     int		width;		/* new width of frame */
1573     int		height;		/* new height of frame */
1574 {
1575     int		n, m;
1576     int		extra_sep = 0;
1577     int		wincount, totwincount = 0;
1578     frame_T	*fr;
1579     int		next_curwin_size = 0;
1580     int		room = 0;
1581     int		new_size;
1582     int		has_next_curwin = 0;
1583     int		hnc;
1584 
1585     if (topfr->fr_layout == FR_LEAF)
1586     {
1587 	/* Set the width/height of this frame.
1588 	 * Redraw when size or position changes */
1589 	if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1590 #ifdef FEAT_VERTSPLIT
1591 		|| topfr->fr_width != width || topfr->fr_win->w_wincol != col
1592 #endif
1593 	   )
1594 	{
1595 	    topfr->fr_win->w_winrow = row;
1596 	    frame_new_height(topfr, height, FALSE, FALSE);
1597 #ifdef FEAT_VERTSPLIT
1598 	    topfr->fr_win->w_wincol = col;
1599 	    frame_new_width(topfr, width, FALSE, FALSE);
1600 #endif
1601 	    redraw_all_later(CLEAR);
1602 	}
1603     }
1604 #ifdef FEAT_VERTSPLIT
1605     else if (topfr->fr_layout == FR_ROW)
1606     {
1607 	topfr->fr_width = width;
1608 	topfr->fr_height = height;
1609 
1610 	if (dir != 'v')			/* equalize frame widths */
1611 	{
1612 	    /* Compute the maximum number of windows horizontally in this
1613 	     * frame. */
1614 	    n = frame_minwidth(topfr, NOWIN);
1615 	    /* add one for the rightmost window, it doesn't have a separator */
1616 	    if (col + width == Columns)
1617 		extra_sep = 1;
1618 	    else
1619 		extra_sep = 0;
1620 	    totwincount = (n + extra_sep) / (p_wmw + 1);
1621 	    has_next_curwin = frame_has_win(topfr, next_curwin);
1622 
1623 	    /*
1624 	     * Compute width for "next_curwin" window and room available for
1625 	     * other windows.
1626 	     * "m" is the minimal width when counting p_wiw for "next_curwin".
1627 	     */
1628 	    m = frame_minwidth(topfr, next_curwin);
1629 	    room = width - m;
1630 	    if (room < 0)
1631 	    {
1632 		next_curwin_size = p_wiw + room;
1633 		room = 0;
1634 	    }
1635 	    else
1636 	    {
1637 		next_curwin_size = -1;
1638 		for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1639 		{
1640 		    /* If 'winfixwidth' set keep the window width if
1641 		     * possible.
1642 		     * Watch out for this window being the next_curwin. */
1643 		    if (frame_fixed_width(fr))
1644 		    {
1645 			n = frame_minwidth(fr, NOWIN);
1646 			new_size = fr->fr_width;
1647 			if (frame_has_win(fr, next_curwin))
1648 			{
1649 			    room += p_wiw - p_wmw;
1650 			    next_curwin_size = 0;
1651 			    if (new_size < p_wiw)
1652 				new_size = p_wiw;
1653 			}
1654 			else
1655 			    /* These windows don't use up room. */
1656 			    totwincount -= (n + (fr->fr_next == NULL
1657 					      ? extra_sep : 0)) / (p_wmw + 1);
1658 			room -= new_size - n;
1659 			if (room < 0)
1660 			{
1661 			    new_size += room;
1662 			    room = 0;
1663 			}
1664 			fr->fr_newwidth = new_size;
1665 		    }
1666 		}
1667 		if (next_curwin_size == -1)
1668 		{
1669 		    if (!has_next_curwin)
1670 			next_curwin_size = 0;
1671 		    else if (totwincount > 1
1672 			    && (room + (totwincount - 2))
1673 						  / (totwincount - 1) > p_wiw)
1674 		    {
1675 			next_curwin_size = (room + p_wiw + totwincount * p_wmw
1676 					   + (totwincount - 1)) / totwincount;
1677 			room -= next_curwin_size - p_wiw;
1678 		    }
1679 		    else
1680 			next_curwin_size = p_wiw;
1681 		}
1682 	    }
1683 
1684 	    if (has_next_curwin)
1685 		--totwincount;		/* don't count curwin */
1686 	}
1687 
1688 	for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1689 	{
1690 	    n = m = 0;
1691 	    wincount = 1;
1692 	    if (fr->fr_next == NULL)
1693 		/* last frame gets all that remains (avoid roundoff error) */
1694 		new_size = width;
1695 	    else if (dir == 'v')
1696 		new_size = fr->fr_width;
1697 	    else if (frame_fixed_width(fr))
1698 	    {
1699 		new_size = fr->fr_newwidth;
1700 		wincount = 0;	    /* doesn't count as a sizeable window */
1701 	    }
1702 	    else
1703 	    {
1704 		/* Compute the maximum number of windows horiz. in "fr". */
1705 		n = frame_minwidth(fr, NOWIN);
1706 		wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1707 								/ (p_wmw + 1);
1708 		m = frame_minwidth(fr, next_curwin);
1709 		if (has_next_curwin)
1710 		    hnc = frame_has_win(fr, next_curwin);
1711 		else
1712 		    hnc = FALSE;
1713 		if (hnc)	    /* don't count next_curwin */
1714 		    --wincount;
1715 		if (totwincount == 0)
1716 		    new_size = room;
1717 		else
1718 		    new_size = (wincount * room + ((unsigned)totwincount >> 1))
1719 								/ totwincount;
1720 		if (hnc)	    /* add next_curwin size */
1721 		{
1722 		    next_curwin_size -= p_wiw - (m - n);
1723 		    new_size += next_curwin_size;
1724 		    room -= new_size - next_curwin_size;
1725 		}
1726 		else
1727 		    room -= new_size;
1728 		new_size += n;
1729 	    }
1730 
1731 	    /* Skip frame that is full width when splitting or closing a
1732 	     * window, unless equalizing all frames. */
1733 	    if (!current || dir != 'v' || topfr->fr_parent != NULL
1734 		    || (new_size != fr->fr_width)
1735 		    || frame_has_win(fr, next_curwin))
1736 		win_equal_rec(next_curwin, current, fr, dir, col, row,
1737 							    new_size, height);
1738 	    col += new_size;
1739 	    width -= new_size;
1740 	    totwincount -= wincount;
1741 	}
1742     }
1743 #endif
1744     else /* topfr->fr_layout == FR_COL */
1745     {
1746 #ifdef FEAT_VERTSPLIT
1747 	topfr->fr_width = width;
1748 #endif
1749 	topfr->fr_height = height;
1750 
1751 	if (dir != 'h')			/* equalize frame heights */
1752 	{
1753 	    /* Compute maximum number of windows vertically in this frame. */
1754 	    n = frame_minheight(topfr, NOWIN);
1755 	    /* add one for the bottom window if it doesn't have a statusline */
1756 	    if (row + height == cmdline_row && p_ls == 0)
1757 		extra_sep = 1;
1758 	    else
1759 		extra_sep = 0;
1760 	    totwincount = (n + extra_sep) / (p_wmh + 1);
1761 	    has_next_curwin = frame_has_win(topfr, next_curwin);
1762 
1763 	    /*
1764 	     * Compute height for "next_curwin" window and room available for
1765 	     * other windows.
1766 	     * "m" is the minimal height when counting p_wh for "next_curwin".
1767 	     */
1768 	    m = frame_minheight(topfr, next_curwin);
1769 	    room = height - m;
1770 	    if (room < 0)
1771 	    {
1772 		/* The room is less then 'winheight', use all space for the
1773 		 * current window. */
1774 		next_curwin_size = p_wh + room;
1775 		room = 0;
1776 	    }
1777 	    else
1778 	    {
1779 		next_curwin_size = -1;
1780 		for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1781 		{
1782 		    /* If 'winfixheight' set keep the window height if
1783 		     * possible.
1784 		     * Watch out for this window being the next_curwin. */
1785 		    if (frame_fixed_height(fr))
1786 		    {
1787 			n = frame_minheight(fr, NOWIN);
1788 			new_size = fr->fr_height;
1789 			if (frame_has_win(fr, next_curwin))
1790 			{
1791 			    room += p_wh - p_wmh;
1792 			    next_curwin_size = 0;
1793 			    if (new_size < p_wh)
1794 				new_size = p_wh;
1795 			}
1796 			else
1797 			    /* These windows don't use up room. */
1798 			    totwincount -= (n + (fr->fr_next == NULL
1799 					      ? extra_sep : 0)) / (p_wmh + 1);
1800 			room -= new_size - n;
1801 			if (room < 0)
1802 			{
1803 			    new_size += room;
1804 			    room = 0;
1805 			}
1806 			fr->fr_newheight = new_size;
1807 		    }
1808 		}
1809 		if (next_curwin_size == -1)
1810 		{
1811 		    if (!has_next_curwin)
1812 			next_curwin_size = 0;
1813 		    else if (totwincount > 1
1814 			    && (room + (totwincount - 2))
1815 						   / (totwincount - 1) > p_wh)
1816 		    {
1817 			next_curwin_size = (room + p_wh + totwincount * p_wmh
1818 					   + (totwincount - 1)) / totwincount;
1819 			room -= next_curwin_size - p_wh;
1820 		    }
1821 		    else
1822 			next_curwin_size = p_wh;
1823 		}
1824 	    }
1825 
1826 	    if (has_next_curwin)
1827 		--totwincount;		/* don't count curwin */
1828 	}
1829 
1830 	for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1831 	{
1832 	    n = m = 0;
1833 	    wincount = 1;
1834 	    if (fr->fr_next == NULL)
1835 		/* last frame gets all that remains (avoid roundoff error) */
1836 		new_size = height;
1837 	    else if (dir == 'h')
1838 		new_size = fr->fr_height;
1839 	    else if (frame_fixed_height(fr))
1840 	    {
1841 		new_size = fr->fr_newheight;
1842 		wincount = 0;	    /* doesn't count as a sizeable window */
1843 	    }
1844 	    else
1845 	    {
1846 		/* Compute the maximum number of windows vert. in "fr". */
1847 		n = frame_minheight(fr, NOWIN);
1848 		wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1849 								/ (p_wmh + 1);
1850 		m = frame_minheight(fr, next_curwin);
1851 		if (has_next_curwin)
1852 		    hnc = frame_has_win(fr, next_curwin);
1853 		else
1854 		    hnc = FALSE;
1855 		if (hnc)	    /* don't count next_curwin */
1856 		    --wincount;
1857 		if (totwincount == 0)
1858 		    new_size = room;
1859 		else
1860 		    new_size = (wincount * room + ((unsigned)totwincount >> 1))
1861 								/ totwincount;
1862 		if (hnc)	    /* add next_curwin size */
1863 		{
1864 		    next_curwin_size -= p_wh - (m - n);
1865 		    new_size += next_curwin_size;
1866 		    room -= new_size - next_curwin_size;
1867 		}
1868 		else
1869 		    room -= new_size;
1870 		new_size += n;
1871 	    }
1872 	    /* Skip frame that is full width when splitting or closing a
1873 	     * window, unless equalizing all frames. */
1874 	    if (!current || dir != 'h' || topfr->fr_parent != NULL
1875 		    || (new_size != fr->fr_height)
1876 		    || frame_has_win(fr, next_curwin))
1877 		win_equal_rec(next_curwin, current, fr, dir, col, row,
1878 							     width, new_size);
1879 	    row += new_size;
1880 	    height -= new_size;
1881 	    totwincount -= wincount;
1882 	}
1883     }
1884 }
1885 
1886 /*
1887  * close all windows for buffer 'buf'
1888  */
1889     void
1890 close_windows(buf, keep_curwin)
1891     buf_T	*buf;
1892     int		keep_curwin;	    /* don't close "curwin" */
1893 {
1894     win_T	*wp;
1895     tabpage_T   *tp, *nexttp;
1896     int		h = tabline_height();
1897 
1898     ++RedrawingDisabled;
1899 
1900     for (wp = firstwin; wp != NULL && lastwin != firstwin; )
1901     {
1902 	if (wp->w_buffer == buf && (!keep_curwin || wp != curwin))
1903 	{
1904 	    win_close(wp, FALSE);
1905 
1906 	    /* Start all over, autocommands may change the window layout. */
1907 	    wp = firstwin;
1908 	}
1909 	else
1910 	    wp = wp->w_next;
1911     }
1912 
1913     /* Also check windows in other tab pages. */
1914     for (tp = first_tabpage; tp != NULL; tp = nexttp)
1915     {
1916 	nexttp = tp->tp_next;
1917 	if (tp != curtab)
1918 	    for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
1919 		if (wp->w_buffer == buf)
1920 		{
1921 		    win_close_othertab(wp, FALSE, tp);
1922 
1923 		    /* Start all over, the tab page may be closed and
1924 		     * autocommands may change the window layout. */
1925 		    nexttp = first_tabpage;
1926 		    break;
1927 		}
1928     }
1929 
1930     --RedrawingDisabled;
1931 
1932     if (h != tabline_height())
1933 	shell_new_rows();
1934 }
1935 
1936 /*
1937  * Return TRUE if the current window is the only window that exists.
1938  * Returns FALSE if there is a window, possibly in another tab page.
1939  */
1940     static int
1941 last_window()
1942 {
1943     return (lastwin == firstwin && first_tabpage->tp_next == NULL);
1944 }
1945 
1946 /*
1947  * Close window "win".  Only works for the current tab page.
1948  * If "free_buf" is TRUE related buffer may be unloaded.
1949  *
1950  * called by :quit, :close, :xit, :wq and findtag()
1951  */
1952     void
1953 win_close(win, free_buf)
1954     win_T	*win;
1955     int		free_buf;
1956 {
1957     win_T	*wp;
1958     buf_T	*old_curbuf = curbuf;
1959 #ifdef FEAT_AUTOCMD
1960     int		other_buffer = FALSE;
1961 #endif
1962     int		close_curwin = FALSE;
1963     int		dir;
1964     int		help_window = FALSE;
1965 
1966     if (last_window())
1967     {
1968 	EMSG(_("E444: Cannot close last window"));
1969 	return;
1970     }
1971 
1972     /* When closing the help window, try restoring a snapshot after closing
1973      * the window.  Otherwise clear the snapshot, it's now invalid. */
1974     if (win->w_buffer->b_help)
1975 	help_window = TRUE;
1976     else
1977 	clear_snapshot(curtab);
1978 
1979 #ifdef FEAT_AUTOCMD
1980     if (win == curwin)
1981     {
1982 	/*
1983 	 * Guess which window is going to be the new current window.
1984 	 * This may change because of the autocommands (sigh).
1985 	 */
1986 	wp = frame2win(win_altframe(win, NULL));
1987 
1988 	/*
1989 	 * Be careful: If autocommands delete the window, return now.
1990 	 */
1991 	if (wp->w_buffer != curbuf)
1992 	{
1993 	    other_buffer = TRUE;
1994 	    apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1995 	    if (!win_valid(win) || last_window())
1996 		return;
1997 	}
1998 	apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
1999 	if (!win_valid(win) || last_window())
2000 	    return;
2001 # ifdef FEAT_EVAL
2002 	/* autocmds may abort script processing */
2003 	if (aborting())
2004 	    return;
2005 # endif
2006     }
2007 #endif
2008 
2009     /*
2010      * Close the link to the buffer.
2011      */
2012     close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2013     /* Autocommands may have closed the window already, or closed the only
2014      * other window. */
2015     if (!win_valid(win) || last_window())
2016 	return;
2017 
2018     /* Free the memory used for the window. */
2019     wp = win_free_mem(win, &dir, NULL);
2020 
2021     /* When closing the last window in a tab page go to another tab page. */
2022     if (wp == NULL)
2023     {
2024 	tabpage_T   *ptp = NULL;
2025 	tabpage_T   *tp;
2026 	tabpage_T   *atp = alt_tabpage();
2027 
2028 	for (tp = first_tabpage; tp != curtab; tp = tp->tp_next)
2029 	    ptp = tp;
2030 	if (tp == NULL)
2031 	{
2032 	    EMSG2(_(e_intern2), "win_close()");
2033 	    return;
2034 	}
2035 	if (ptp == NULL)
2036 	    first_tabpage = tp->tp_next;
2037 	else
2038 	    ptp->tp_next = tp->tp_next;
2039 	free_tabpage(tp);
2040 
2041 	/* We don't do the window resizing stuff, let enter_tabpage() take
2042 	 * care of entering a window in another tab page. */
2043 	enter_tabpage(atp, old_curbuf);
2044 	return;
2045     }
2046 
2047     /* Make sure curwin isn't invalid.  It can cause severe trouble when
2048      * printing an error message.  For win_equal() curbuf needs to be valid
2049      * too. */
2050     else if (win == curwin)
2051     {
2052 	curwin = wp;
2053 #ifdef FEAT_QUICKFIX
2054 	if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2055 	{
2056 	    /*
2057 	     * The cursor goes to the preview or the quickfix window, try
2058 	     * finding another window to go to.
2059 	     */
2060 	    for (;;)
2061 	    {
2062 		if (wp->w_next == NULL)
2063 		    wp = firstwin;
2064 		else
2065 		    wp = wp->w_next;
2066 		if (wp == curwin)
2067 		    break;
2068 		if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2069 		{
2070 		    curwin = wp;
2071 		    break;
2072 		}
2073 	    }
2074 	}
2075 #endif
2076 	curbuf = curwin->w_buffer;
2077 	close_curwin = TRUE;
2078     }
2079     if (p_ea
2080 #ifdef FEAT_VERTSPLIT
2081 	    && (*p_ead == 'b' || *p_ead == dir)
2082 #endif
2083 	    )
2084 	win_equal(curwin, TRUE,
2085 #ifdef FEAT_VERTSPLIT
2086 		dir
2087 #else
2088 		0
2089 #endif
2090 		);
2091     else
2092 	win_comp_pos();
2093     if (close_curwin)
2094     {
2095 	win_enter_ext(wp, FALSE, TRUE);
2096 #ifdef FEAT_AUTOCMD
2097 	if (other_buffer)
2098 	    /* careful: after this wp and win may be invalid! */
2099 	    apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2100 #endif
2101     }
2102 
2103     /*
2104      * If last window has a status line now and we don't want one,
2105      * remove the status line.
2106      */
2107     last_status(FALSE);
2108 
2109     /* After closing the help window, try restoring the window layout from
2110      * before it was opened. */
2111     if (help_window)
2112 	restore_snapshot(close_curwin);
2113 
2114 #if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2115     /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2116     if (gui.in_use && !win_hasvertsplit())
2117 	gui_init_which_components(NULL);
2118 #endif
2119 
2120     redraw_all_later(NOT_VALID);
2121 }
2122 
2123 /*
2124  * Close window "win" in tab page "tp", which is not the current tab page.
2125  * This may be the last window ih that tab page and result in closing the tab,
2126  * thus "tp" may become invalid!
2127  * Called must check if buffer is hidden.
2128  */
2129     void
2130 win_close_othertab(win, free_buf, tp)
2131     win_T	*win;
2132     int		free_buf;
2133     tabpage_T	*tp;
2134 {
2135     win_T	*wp;
2136     int		dir;
2137     tabpage_T   *ptp = NULL;
2138 
2139     /* Close the link to the buffer. */
2140     close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0);
2141 
2142     /* Careful: Autocommands may have closed the tab page or made it the
2143      * current tab page.  */
2144     for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2145 	;
2146     if (ptp == NULL || tp == curtab)
2147 	return;
2148 
2149     /* Autocommands may have closed the window already. */
2150     for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2151 	;
2152     if (wp == NULL)
2153 	return;
2154 
2155     /* Free the memory used for the window. */
2156     wp = win_free_mem(win, &dir, tp);
2157 
2158     /* When closing the last window in a tab page remove the tab page. */
2159     if (wp == NULL)
2160     {
2161 	if (tp == first_tabpage)
2162 	    first_tabpage = tp->tp_next;
2163 	else
2164 	{
2165 	    for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2166 							   ptp = ptp->tp_next)
2167 		;
2168 	    if (ptp == NULL)
2169 	    {
2170 		EMSG2(_(e_intern2), "win_close_othertab()");
2171 		return;
2172 	    }
2173 	    ptp->tp_next = tp->tp_next;
2174 	}
2175 	vim_free(tp);
2176     }
2177 }
2178 
2179 /*
2180  * Free the memory used for a window.
2181  * Returns a pointer to the window that got the freed up space.
2182  */
2183     static win_T *
2184 win_free_mem(win, dirp, tp)
2185     win_T	*win;
2186     int		*dirp;		/* set to 'v' or 'h' for direction if 'ea' */
2187     tabpage_T	*tp;		/* tab page "win" is in, NULL for current */
2188 {
2189     frame_T	*frp;
2190     win_T	*wp;
2191 
2192 #ifdef FEAT_FOLDING
2193     clearFolding(win);
2194 #endif
2195 
2196     /* reduce the reference count to the argument list. */
2197     alist_unlink(win->w_alist);
2198 
2199     /* Remove the window and its frame from the tree of frames. */
2200     frp = win->w_frame;
2201     wp = winframe_remove(win, dirp, tp);
2202     vim_free(frp);
2203     win_free(win, tp);
2204 
2205     /* When deleting the current window of another tab page select a new
2206      * current window. */
2207     if (tp != NULL && win == tp->tp_curwin)
2208 	tp->tp_curwin = wp;
2209 
2210     return wp;
2211 }
2212 
2213 #if defined(EXITFREE) || defined(PROTO)
2214     void
2215 win_free_all()
2216 {
2217     int		dummy;
2218 
2219 # ifdef FEAT_WINDOWS
2220     while (first_tabpage->tp_next != NULL)
2221 	tabpage_close(TRUE);
2222 # endif
2223 
2224     while (firstwin != NULL)
2225 	(void)win_free_mem(firstwin, &dummy, NULL);
2226 }
2227 #endif
2228 
2229 /*
2230  * Remove a window and its frame from the tree of frames.
2231  * Returns a pointer to the window that got the freed up space.
2232  */
2233 /*ARGSUSED*/
2234     static win_T *
2235 winframe_remove(win, dirp, tp)
2236     win_T	*win;
2237     int		*dirp;		/* set to 'v' or 'h' for direction if 'ea' */
2238     tabpage_T	*tp;		/* tab page "win" is in, NULL for current */
2239 {
2240     frame_T	*frp, *frp2, *frp3;
2241     frame_T	*frp_close = win->w_frame;
2242     win_T	*wp;
2243     int		old_size = 0;
2244 
2245     /*
2246      * If there is only one window there is nothing to remove.
2247      */
2248     if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2249 	return NULL;
2250 
2251     /*
2252      * Remove the window from its frame.
2253      */
2254     frp2 = win_altframe(win, tp);
2255     wp = frame2win(frp2);
2256 
2257     /* Remove this frame from the list of frames. */
2258     frame_remove(frp_close);
2259 
2260 #ifdef FEAT_VERTSPLIT
2261     if (frp_close->fr_parent->fr_layout == FR_COL)
2262     {
2263 #endif
2264 	/* When 'winfixheight' is set, remember its old size and restore
2265 	 * it later (it's a simplistic solution...).  Don't do this if the
2266 	 * window will occupy the full height of the screen. */
2267 	if (frp2->fr_win != NULL
2268 		&& (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2269 		&& frp2->fr_win->w_p_wfh)
2270 	    old_size = frp2->fr_win->w_height;
2271 	frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2272 			    frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2273 	if (old_size != 0)
2274 	    win_setheight_win(old_size, frp2->fr_win);
2275 #ifdef FEAT_VERTSPLIT
2276 	*dirp = 'v';
2277     }
2278     else
2279     {
2280 	/* When 'winfixwidth' is set, remember its old size and restore
2281 	 * it later (it's a simplistic solution...).  Don't do this if the
2282 	 * window will occupy the full width of the screen. */
2283 	if (frp2->fr_win != NULL
2284 		&& (frp2->fr_next != NULL || frp2->fr_prev != NULL)
2285 		&& frp2->fr_win->w_p_wfw)
2286 	    old_size = frp2->fr_win->w_width;
2287 	frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
2288 			    frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
2289 	if (old_size != 0)
2290 	    win_setwidth_win(old_size, frp2->fr_win);
2291 	*dirp = 'h';
2292     }
2293 #endif
2294 
2295     /* If rows/columns go to a window below/right its positions need to be
2296      * updated.  Can only be done after the sizes have been updated. */
2297     if (frp2 == frp_close->fr_next)
2298     {
2299 	int row = win->w_winrow;
2300 	int col = W_WINCOL(win);
2301 
2302 	frame_comp_pos(frp2, &row, &col);
2303     }
2304 
2305     if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2306     {
2307 	/* There is no other frame in this list, move its info to the parent
2308 	 * and remove it. */
2309 	frp2->fr_parent->fr_layout = frp2->fr_layout;
2310 	frp2->fr_parent->fr_child = frp2->fr_child;
2311 	for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2312 	    frp->fr_parent = frp2->fr_parent;
2313 	frp2->fr_parent->fr_win = frp2->fr_win;
2314 	if (frp2->fr_win != NULL)
2315 	    frp2->fr_win->w_frame = frp2->fr_parent;
2316 	frp = frp2->fr_parent;
2317 	vim_free(frp2);
2318 
2319 	frp2 = frp->fr_parent;
2320 	if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2321 	{
2322 	    /* The frame above the parent has the same layout, have to merge
2323 	     * the frames into this list. */
2324 	    if (frp2->fr_child == frp)
2325 		frp2->fr_child = frp->fr_child;
2326 	    frp->fr_child->fr_prev = frp->fr_prev;
2327 	    if (frp->fr_prev != NULL)
2328 		frp->fr_prev->fr_next = frp->fr_child;
2329 	    for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2330 	    {
2331 		frp3->fr_parent = frp2;
2332 		if (frp3->fr_next == NULL)
2333 		{
2334 		    frp3->fr_next = frp->fr_next;
2335 		    if (frp->fr_next != NULL)
2336 			frp->fr_next->fr_prev = frp3;
2337 		    break;
2338 		}
2339 	    }
2340 	    vim_free(frp);
2341 	}
2342     }
2343 
2344     return wp;
2345 }
2346 
2347 /*
2348  * Find out which frame is going to get the freed up space when "win" is
2349  * closed.
2350  * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2351  * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2352  * This makes opening a window and closing it immediately keep the same window
2353  * layout.
2354  */
2355     static frame_T *
2356 win_altframe(win, tp)
2357     win_T	*win;
2358     tabpage_T	*tp;		/* tab page "win" is in, NULL for current */
2359 {
2360     frame_T	*frp;
2361     int		b;
2362 
2363     if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2364 	/* Last window in this tab page, will go to next tab page. */
2365 	return alt_tabpage()->tp_curwin->w_frame;
2366 
2367     frp = win->w_frame;
2368 #ifdef FEAT_VERTSPLIT
2369     if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2370 	b = p_spr;
2371     else
2372 #endif
2373 	b = p_sb;
2374     if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2375 	return frp->fr_next;
2376     return frp->fr_prev;
2377 }
2378 
2379 /*
2380  * Return the tabpage that will be used if the current one is closed.
2381  */
2382     static tabpage_T *
2383 alt_tabpage()
2384 {
2385     tabpage_T	*tp;
2386 
2387     /* Use the next tab page if possible. */
2388     if (curtab->tp_next != NULL)
2389 	return curtab->tp_next;
2390 
2391     /* Find the last but one tab page. */
2392     for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2393 	;
2394     return tp;
2395 }
2396 
2397 /*
2398  * Find the left-upper window in frame "frp".
2399  */
2400     static win_T *
2401 frame2win(frp)
2402     frame_T	*frp;
2403 {
2404     while (frp->fr_win == NULL)
2405 	frp = frp->fr_child;
2406     return frp->fr_win;
2407 }
2408 
2409 /*
2410  * Return TRUE if frame "frp" contains window "wp".
2411  */
2412     static int
2413 frame_has_win(frp, wp)
2414     frame_T	*frp;
2415     win_T	*wp;
2416 {
2417     frame_T	*p;
2418 
2419     if (frp->fr_layout == FR_LEAF)
2420 	return frp->fr_win == wp;
2421 
2422     for (p = frp->fr_child; p != NULL; p = p->fr_next)
2423 	if (frame_has_win(p, wp))
2424 	    return TRUE;
2425     return FALSE;
2426 }
2427 
2428 /*
2429  * Set a new height for a frame.  Recursively sets the height for contained
2430  * frames and windows.  Caller must take care of positions.
2431  */
2432     static void
2433 frame_new_height(topfrp, height, topfirst, wfh)
2434     frame_T	*topfrp;
2435     int		height;
2436     int		topfirst;	/* resize topmost contained frame first */
2437     int		wfh;		/* obey 'winfixheight' when there is a choice;
2438 				   may cause the height not to be set */
2439 {
2440     frame_T	*frp;
2441     int		extra_lines;
2442     int		h;
2443 
2444     if (topfrp->fr_win != NULL)
2445     {
2446 	/* Simple case: just one window. */
2447 	win_new_height(topfrp->fr_win,
2448 				    height - topfrp->fr_win->w_status_height);
2449     }
2450 #ifdef FEAT_VERTSPLIT
2451     else if (topfrp->fr_layout == FR_ROW)
2452     {
2453 	do
2454 	{
2455 	    /* All frames in this row get the same new height. */
2456 	    for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2457 	    {
2458 		frame_new_height(frp, height, topfirst, wfh);
2459 		if (frp->fr_height > height)
2460 		{
2461 		    /* Could not fit the windows, make the whole row higher. */
2462 		    height = frp->fr_height;
2463 		    break;
2464 		}
2465 	    }
2466 	}
2467 	while (frp != NULL);
2468     }
2469 #endif
2470     else    /* fr_layout == FR_COL */
2471     {
2472 	/* Complicated case: Resize a column of frames.  Resize the bottom
2473 	 * frame first, frames above that when needed. */
2474 
2475 	frp = topfrp->fr_child;
2476 	if (wfh)
2477 	    /* Advance past frames with one window with 'wfh' set. */
2478 	    while (frame_fixed_height(frp))
2479 	    {
2480 		frp = frp->fr_next;
2481 		if (frp == NULL)
2482 		    return;	    /* no frame without 'wfh', give up */
2483 	    }
2484 	if (!topfirst)
2485 	{
2486 	    /* Find the bottom frame of this column */
2487 	    while (frp->fr_next != NULL)
2488 		frp = frp->fr_next;
2489 	    if (wfh)
2490 		/* Advance back for frames with one window with 'wfh' set. */
2491 		while (frame_fixed_height(frp))
2492 		    frp = frp->fr_prev;
2493 	}
2494 
2495 	extra_lines = height - topfrp->fr_height;
2496 	if (extra_lines < 0)
2497 	{
2498 	    /* reduce height of contained frames, bottom or top frame first */
2499 	    while (frp != NULL)
2500 	    {
2501 		h = frame_minheight(frp, NULL);
2502 		if (frp->fr_height + extra_lines < h)
2503 		{
2504 		    extra_lines += frp->fr_height - h;
2505 		    frame_new_height(frp, h, topfirst, wfh);
2506 		}
2507 		else
2508 		{
2509 		    frame_new_height(frp, frp->fr_height + extra_lines,
2510 							       topfirst, wfh);
2511 		    break;
2512 		}
2513 		if (topfirst)
2514 		{
2515 		    do
2516 			frp = frp->fr_next;
2517 		    while (wfh && frp != NULL && frame_fixed_height(frp));
2518 		}
2519 		else
2520 		{
2521 		    do
2522 			frp = frp->fr_prev;
2523 		    while (wfh && frp != NULL && frame_fixed_height(frp));
2524 		}
2525 		/* Increase "height" if we could not reduce enough frames. */
2526 		if (frp == NULL)
2527 		    height -= extra_lines;
2528 	    }
2529 	}
2530 	else if (extra_lines > 0)
2531 	{
2532 	    /* increase height of bottom or top frame */
2533 	    frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2534 	}
2535     }
2536     topfrp->fr_height = height;
2537 }
2538 
2539 /*
2540  * Return TRUE if height of frame "frp" should not be changed because of
2541  * the 'winfixheight' option.
2542  */
2543     static int
2544 frame_fixed_height(frp)
2545     frame_T	*frp;
2546 {
2547     /* frame with one window: fixed height if 'winfixheight' set. */
2548     if (frp->fr_win != NULL)
2549 	return frp->fr_win->w_p_wfh;
2550 
2551     if (frp->fr_layout == FR_ROW)
2552     {
2553 	/* The frame is fixed height if one of the frames in the row is fixed
2554 	 * height. */
2555 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2556 	    if (frame_fixed_height(frp))
2557 		return TRUE;
2558 	return FALSE;
2559     }
2560 
2561     /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2562      * frames in the row are fixed height. */
2563     for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2564 	if (!frame_fixed_height(frp))
2565 	    return FALSE;
2566     return TRUE;
2567 }
2568 
2569 #ifdef FEAT_VERTSPLIT
2570 /*
2571  * Return TRUE if width of frame "frp" should not be changed because of
2572  * the 'winfixwidth' option.
2573  */
2574     static int
2575 frame_fixed_width(frp)
2576     frame_T	*frp;
2577 {
2578     /* frame with one window: fixed width if 'winfixwidth' set. */
2579     if (frp->fr_win != NULL)
2580 	return frp->fr_win->w_p_wfw;
2581 
2582     if (frp->fr_layout == FR_COL)
2583     {
2584 	/* The frame is fixed width if one of the frames in the row is fixed
2585 	 * width. */
2586 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2587 	    if (frame_fixed_width(frp))
2588 		return TRUE;
2589 	return FALSE;
2590     }
2591 
2592     /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2593      * frames in the row are fixed width. */
2594     for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2595 	if (!frame_fixed_width(frp))
2596 	    return FALSE;
2597     return TRUE;
2598 }
2599 
2600 /*
2601  * Add a status line to windows at the bottom of "frp".
2602  * Note: Does not check if there is room!
2603  */
2604     static void
2605 frame_add_statusline(frp)
2606     frame_T	*frp;
2607 {
2608     win_T	*wp;
2609 
2610     if (frp->fr_layout == FR_LEAF)
2611     {
2612 	wp = frp->fr_win;
2613 	if (wp->w_status_height == 0)
2614 	{
2615 	    if (wp->w_height > 0)	/* don't make it negative */
2616 		--wp->w_height;
2617 	    wp->w_status_height = STATUS_HEIGHT;
2618 	}
2619     }
2620     else if (frp->fr_layout == FR_ROW)
2621     {
2622 	/* Handle all the frames in the row. */
2623 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2624 	    frame_add_statusline(frp);
2625     }
2626     else /* frp->fr_layout == FR_COL */
2627     {
2628 	/* Only need to handle the last frame in the column. */
2629 	for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2630 	    ;
2631 	frame_add_statusline(frp);
2632     }
2633 }
2634 
2635 /*
2636  * Set width of a frame.  Handles recursively going through contained frames.
2637  * May remove separator line for windows at the right side (for win_close()).
2638  */
2639     static void
2640 frame_new_width(topfrp, width, leftfirst, wfw)
2641     frame_T	*topfrp;
2642     int		width;
2643     int		leftfirst;	/* resize leftmost contained frame first */
2644     int		wfw;		/* obey 'winfixwidth' when there is a choice;
2645 				   may cause the width not to be set */
2646 {
2647     frame_T	*frp;
2648     int		extra_cols;
2649     int		w;
2650     win_T	*wp;
2651 
2652     if (topfrp->fr_layout == FR_LEAF)
2653     {
2654 	/* Simple case: just one window. */
2655 	wp = topfrp->fr_win;
2656 	/* Find out if there are any windows right of this one. */
2657 	for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
2658 	    if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
2659 		break;
2660 	if (frp->fr_parent == NULL)
2661 	    wp->w_vsep_width = 0;
2662 	win_new_width(wp, width - wp->w_vsep_width);
2663     }
2664     else if (topfrp->fr_layout == FR_COL)
2665     {
2666 	do
2667 	{
2668 	    /* All frames in this column get the same new width. */
2669 	    for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2670 	    {
2671 		frame_new_width(frp, width, leftfirst, wfw);
2672 		if (frp->fr_width > width)
2673 		{
2674 		    /* Could not fit the windows, make whole column wider. */
2675 		    width = frp->fr_width;
2676 		    break;
2677 		}
2678 	    }
2679 	} while (frp != NULL);
2680     }
2681     else    /* fr_layout == FR_ROW */
2682     {
2683 	/* Complicated case: Resize a row of frames.  Resize the rightmost
2684 	 * frame first, frames left of it when needed. */
2685 
2686 	frp = topfrp->fr_child;
2687 	if (wfw)
2688 	    /* Advance past frames with one window with 'wfw' set. */
2689 	    while (frame_fixed_width(frp))
2690 	    {
2691 		frp = frp->fr_next;
2692 		if (frp == NULL)
2693 		    return;	    /* no frame without 'wfw', give up */
2694 	    }
2695 	if (!leftfirst)
2696 	{
2697 	    /* Find the rightmost frame of this row */
2698 	    while (frp->fr_next != NULL)
2699 		frp = frp->fr_next;
2700 	    if (wfw)
2701 		/* Advance back for frames with one window with 'wfw' set. */
2702 		while (frame_fixed_width(frp))
2703 		    frp = frp->fr_prev;
2704 	}
2705 
2706 	extra_cols = width - topfrp->fr_width;
2707 	if (extra_cols < 0)
2708 	{
2709 	    /* reduce frame width, rightmost frame first */
2710 	    while (frp != NULL)
2711 	    {
2712 		w = frame_minwidth(frp, NULL);
2713 		if (frp->fr_width + extra_cols < w)
2714 		{
2715 		    extra_cols += frp->fr_width - w;
2716 		    frame_new_width(frp, w, leftfirst, wfw);
2717 		}
2718 		else
2719 		{
2720 		    frame_new_width(frp, frp->fr_width + extra_cols,
2721 							      leftfirst, wfw);
2722 		    break;
2723 		}
2724 		if (leftfirst)
2725 		{
2726 		    do
2727 			frp = frp->fr_next;
2728 		    while (wfw && frp != NULL && frame_fixed_width(frp));
2729 		}
2730 		else
2731 		{
2732 		    do
2733 			frp = frp->fr_prev;
2734 		    while (wfw && frp != NULL && frame_fixed_width(frp));
2735 		}
2736 		/* Increase "width" if we could not reduce enough frames. */
2737 		if (frp == NULL)
2738 		    width -= extra_cols;
2739 	    }
2740 	}
2741 	else if (extra_cols > 0)
2742 	{
2743 	    /* increase width of rightmost frame */
2744 	    frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
2745 	}
2746     }
2747     topfrp->fr_width = width;
2748 }
2749 
2750 /*
2751  * Add the vertical separator to windows at the right side of "frp".
2752  * Note: Does not check if there is room!
2753  */
2754     static void
2755 frame_add_vsep(frp)
2756     frame_T	*frp;
2757 {
2758     win_T	*wp;
2759 
2760     if (frp->fr_layout == FR_LEAF)
2761     {
2762 	wp = frp->fr_win;
2763 	if (wp->w_vsep_width == 0)
2764 	{
2765 	    if (wp->w_width > 0)	/* don't make it negative */
2766 		--wp->w_width;
2767 	    wp->w_vsep_width = 1;
2768 	}
2769     }
2770     else if (frp->fr_layout == FR_COL)
2771     {
2772 	/* Handle all the frames in the column. */
2773 	for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2774 	    frame_add_vsep(frp);
2775     }
2776     else /* frp->fr_layout == FR_ROW */
2777     {
2778 	/* Only need to handle the last frame in the row. */
2779 	frp = frp->fr_child;
2780 	while (frp->fr_next != NULL)
2781 	    frp = frp->fr_next;
2782 	frame_add_vsep(frp);
2783     }
2784 }
2785 
2786 /*
2787  * Set frame width from the window it contains.
2788  */
2789     static void
2790 frame_fix_width(wp)
2791     win_T	*wp;
2792 {
2793     wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
2794 }
2795 #endif
2796 
2797 /*
2798  * Set frame height from the window it contains.
2799  */
2800     static void
2801 frame_fix_height(wp)
2802     win_T	*wp;
2803 {
2804     wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
2805 }
2806 
2807 /*
2808  * Compute the minimal height for frame "topfrp".
2809  * Uses the 'winminheight' option.
2810  * When "next_curwin" isn't NULL, use p_wh for this window.
2811  * When "next_curwin" is NOWIN, don't use at least one line for the current
2812  * window.
2813  */
2814     static int
2815 frame_minheight(topfrp, next_curwin)
2816     frame_T	*topfrp;
2817     win_T	*next_curwin;
2818 {
2819     frame_T	*frp;
2820     int		m;
2821 #ifdef FEAT_VERTSPLIT
2822     int		n;
2823 #endif
2824 
2825     if (topfrp->fr_win != NULL)
2826     {
2827 	if (topfrp->fr_win == next_curwin)
2828 	    m = p_wh + topfrp->fr_win->w_status_height;
2829 	else
2830 	{
2831 	    /* window: minimal height of the window plus status line */
2832 	    m = p_wmh + topfrp->fr_win->w_status_height;
2833 	    /* Current window is minimal one line high */
2834 	    if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2835 		++m;
2836 	}
2837     }
2838 #ifdef FEAT_VERTSPLIT
2839     else if (topfrp->fr_layout == FR_ROW)
2840     {
2841 	/* get the minimal height from each frame in this row */
2842 	m = 0;
2843 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2844 	{
2845 	    n = frame_minheight(frp, next_curwin);
2846 	    if (n > m)
2847 		m = n;
2848 	}
2849     }
2850 #endif
2851     else
2852     {
2853 	/* Add up the minimal heights for all frames in this column. */
2854 	m = 0;
2855 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2856 	    m += frame_minheight(frp, next_curwin);
2857     }
2858 
2859     return m;
2860 }
2861 
2862 #ifdef FEAT_VERTSPLIT
2863 /*
2864  * Compute the minimal width for frame "topfrp".
2865  * When "next_curwin" isn't NULL, use p_wiw for this window.
2866  * When "next_curwin" is NOWIN, don't use at least one column for the current
2867  * window.
2868  */
2869     static int
2870 frame_minwidth(topfrp, next_curwin)
2871     frame_T	*topfrp;
2872     win_T	*next_curwin;	/* use p_wh and p_wiw for next_curwin */
2873 {
2874     frame_T	*frp;
2875     int		m, n;
2876 
2877     if (topfrp->fr_win != NULL)
2878     {
2879 	if (topfrp->fr_win == next_curwin)
2880 	    m = p_wiw + topfrp->fr_win->w_vsep_width;
2881 	else
2882 	{
2883 	    /* window: minimal width of the window plus separator column */
2884 	    m = p_wmw + topfrp->fr_win->w_vsep_width;
2885 	    /* Current window is minimal one column wide */
2886 	    if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
2887 		++m;
2888 	}
2889     }
2890     else if (topfrp->fr_layout == FR_COL)
2891     {
2892 	/* get the minimal width from each frame in this column */
2893 	m = 0;
2894 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2895 	{
2896 	    n = frame_minwidth(frp, next_curwin);
2897 	    if (n > m)
2898 		m = n;
2899 	}
2900     }
2901     else
2902     {
2903 	/* Add up the minimal widths for all frames in this row. */
2904 	m = 0;
2905 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2906 	    m += frame_minwidth(frp, next_curwin);
2907     }
2908 
2909     return m;
2910 }
2911 #endif
2912 
2913 
2914 /*
2915  * Try to close all windows except current one.
2916  * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
2917  * used and the buffer was modified.
2918  *
2919  * Used by ":bdel" and ":only".
2920  */
2921     void
2922 close_others(message, forceit)
2923     int		message;
2924     int		forceit;	    /* always hide all other windows */
2925 {
2926     win_T	*wp;
2927     win_T	*nextwp;
2928     int		r;
2929 
2930     if (lastwin == firstwin)
2931     {
2932 	if (message
2933 #ifdef FEAT_AUTOCMD
2934 		    && !autocmd_busy
2935 #endif
2936 				    )
2937 	    MSG(_("Already only one window"));
2938 	return;
2939     }
2940 
2941     /* Be very careful here: autocommands may change the window layout. */
2942     for (wp = firstwin; win_valid(wp); wp = nextwp)
2943     {
2944 	nextwp = wp->w_next;
2945 	if (wp != curwin)		/* don't close current window */
2946 	{
2947 
2948 	    /* Check if it's allowed to abandon this window */
2949 	    r = can_abandon(wp->w_buffer, forceit);
2950 #ifdef FEAT_AUTOCMD
2951 	    if (!win_valid(wp))		/* autocommands messed wp up */
2952 	    {
2953 		nextwp = firstwin;
2954 		continue;
2955 	    }
2956 #endif
2957 	    if (!r)
2958 	    {
2959 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2960 		if (message && (p_confirm || cmdmod.confirm) && p_write)
2961 		{
2962 		    dialog_changed(wp->w_buffer, FALSE);
2963 # ifdef FEAT_AUTOCMD
2964 		    if (!win_valid(wp))		/* autocommands messed wp up */
2965 		    {
2966 			nextwp = firstwin;
2967 			continue;
2968 		    }
2969 # endif
2970 		}
2971 		if (bufIsChanged(wp->w_buffer))
2972 #endif
2973 		    continue;
2974 	    }
2975 	    win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
2976 	}
2977     }
2978 
2979     if (message && lastwin != firstwin)
2980 	EMSG(_("E445: Other window contains changes"));
2981 }
2982 
2983 #endif /* FEAT_WINDOWS */
2984 
2985 /*
2986  * Init the current window "curwin".
2987  * Called when a new file is being edited.
2988  */
2989     void
2990 curwin_init()
2991 {
2992     redraw_win_later(curwin, NOT_VALID);
2993     curwin->w_lines_valid = 0;
2994     curwin->w_cursor.lnum = 1;
2995     curwin->w_curswant = curwin->w_cursor.col = 0;
2996 #ifdef FEAT_VIRTUALEDIT
2997     curwin->w_cursor.coladd = 0;
2998 #endif
2999     curwin->w_pcmark.lnum = 1;	/* pcmark not cleared but set to line 1 */
3000     curwin->w_pcmark.col = 0;
3001     curwin->w_prev_pcmark.lnum = 0;
3002     curwin->w_prev_pcmark.col = 0;
3003     curwin->w_topline = 1;
3004 #ifdef FEAT_DIFF
3005     curwin->w_topfill = 0;
3006 #endif
3007     curwin->w_botline = 2;
3008 #ifdef FEAT_FKMAP
3009     if (curwin->w_p_rl)
3010 	curwin->w_farsi = W_CONV + W_R_L;
3011     else
3012 	curwin->w_farsi = W_CONV;
3013 #endif
3014 }
3015 
3016 /*
3017  * Allocate the first window and put an empty buffer in it.
3018  * Called from main().
3019  * Return FAIL when something goes wrong (out of memory).
3020  */
3021     int
3022 win_alloc_first()
3023 {
3024     if (win_alloc_firstwin(NULL) == FAIL)
3025 	return FAIL;
3026 
3027 #ifdef FEAT_WINDOWS
3028     first_tabpage = alloc_tabpage();
3029     if (first_tabpage == NULL)
3030 	return FAIL;
3031     first_tabpage->tp_topframe = topframe;
3032     curtab = first_tabpage;
3033 #endif
3034     return OK;
3035 }
3036 
3037 /*
3038  * Allocate the first window or the first window in a new tab page.
3039  * When "oldwin" is NULL create an empty buffer for it.
3040  * When "oldwin" is not NULL copy info from it to the new window (only with
3041  * FEAT_WINDOWS).
3042  * Return FAIL when something goes wrong (out of memory).
3043  */
3044     static int
3045 win_alloc_firstwin(oldwin)
3046     win_T	*oldwin;
3047 {
3048     curwin = win_alloc(NULL);
3049     if (oldwin == NULL)
3050     {
3051 	/* Very first window, need to create an empty buffer for it and
3052 	 * initialize from scratch. */
3053 	curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3054 	if (curwin == NULL || curbuf == NULL)
3055 	    return FAIL;
3056 	curwin->w_buffer = curbuf;
3057 	curbuf->b_nwindows = 1;	/* there is one window */
3058 #ifdef FEAT_WINDOWS
3059 	curwin->w_alist = &global_alist;
3060 #endif
3061 	curwin_init();		/* init current window */
3062     }
3063 #ifdef FEAT_WINDOWS
3064     else
3065     {
3066 	/* First window in new tab page, initialize it from "oldwin". */
3067 	win_init(curwin, oldwin);
3068 
3069 # ifdef FEAT_SCROLLBIND
3070 	/* We don't want scroll-binding in the first window. */
3071 	curwin->w_p_scb = FALSE;
3072 # endif
3073     }
3074 #endif
3075 
3076     topframe = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3077     if (topframe == NULL)
3078 	return FAIL;
3079     topframe->fr_layout = FR_LEAF;
3080 #ifdef FEAT_VERTSPLIT
3081     topframe->fr_width = Columns;
3082 #endif
3083     topframe->fr_height = Rows - p_ch;
3084 #ifdef FEAT_WINDOWS
3085     p_ch_used = p_ch;
3086 #endif
3087     topframe->fr_win = curwin;
3088     curwin->w_frame = topframe;
3089 
3090     return OK;
3091 }
3092 
3093 /*
3094  * Initialize the window and frame size to the maximum.
3095  */
3096     void
3097 win_init_size()
3098 {
3099     firstwin->w_height = ROWS_AVAIL;
3100     topframe->fr_height = ROWS_AVAIL;
3101 #ifdef FEAT_VERTSPLIT
3102     firstwin->w_width = Columns;
3103     topframe->fr_width = Columns;
3104 #endif
3105 }
3106 
3107 #if defined(FEAT_WINDOWS) || defined(PROTO)
3108 
3109 /*
3110  * Allocate a new tabpage_T and init the values.
3111  * Returns NULL when out of memory.
3112  */
3113     static tabpage_T *
3114 alloc_tabpage()
3115 {
3116     tabpage_T	*tp;
3117 
3118     tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
3119     if (tp != NULL)
3120     {
3121 # ifdef FEAT_GUI
3122 	int	i;
3123 
3124 	for (i = 0; i < 3; i++)
3125 	    tp->tp_prev_which_scrollbars[i] = -1;
3126 # endif
3127 # ifdef FEAT_DIFF
3128 	tp->tp_diff_invalid = TRUE;
3129 # endif
3130 #ifdef FEAT_EVAL
3131 	/* init t: variables */
3132 	init_var_dict(&tp->tp_vars, &tp->tp_winvar);
3133 #endif
3134     }
3135     return tp;
3136 }
3137 
3138     static void
3139 free_tabpage(tp)
3140     tabpage_T	*tp;
3141 {
3142 # ifdef FEAT_DIFF
3143     diff_clear(tp);
3144 # endif
3145     clear_snapshot(tp);
3146 #ifdef FEAT_EVAL
3147     vars_clear(&tp->tp_vars.dv_hashtab);	/* free all t: variables */
3148 #endif
3149     vim_free(tp);
3150 }
3151 
3152 /*
3153  * Create a new Tab page with one window.
3154  * It will edit the current buffer, like after ":split".
3155  * When "after" is 0 put it just after the current Tab page.
3156  * Otherwise put it just before tab page "after".
3157  * Return FAIL or OK.
3158  */
3159     int
3160 win_new_tabpage(after)
3161     int		after;
3162 {
3163     tabpage_T	*tp = curtab;
3164     tabpage_T	*newtp;
3165     int		n;
3166 
3167     newtp = alloc_tabpage();
3168     if (newtp == NULL)
3169 	return FAIL;
3170 
3171     /* Remember the current windows in this Tab page. */
3172     if (leave_tabpage(curbuf) == FAIL)
3173     {
3174 	vim_free(newtp);
3175 	return FAIL;
3176     }
3177     curtab = newtp;
3178 
3179     /* Create a new empty window. */
3180     if (win_alloc_firstwin(tp->tp_curwin) == OK)
3181     {
3182 	/* Make the new Tab page the new topframe. */
3183 	if (after == 1)
3184 	{
3185 	    /* New tab page becomes the first one. */
3186 	    newtp->tp_next = first_tabpage;
3187 	    first_tabpage = newtp;
3188 	}
3189 	else
3190 	{
3191 	    if (after > 0)
3192 	    {
3193 		/* Put new tab page before tab page "after". */
3194 		n = 2;
3195 		for (tp = first_tabpage; tp->tp_next != NULL
3196 					       && n < after; tp = tp->tp_next)
3197 		    ++n;
3198 	    }
3199 	    newtp->tp_next = tp->tp_next;
3200 	    tp->tp_next = newtp;
3201 	}
3202 	win_init_size();
3203 	firstwin->w_winrow = tabline_height();
3204 	win_comp_scroll(curwin);
3205 
3206 	newtp->tp_topframe = topframe;
3207 	last_status(FALSE);
3208 
3209 #if defined(FEAT_GUI)
3210 	/* When 'guioptions' includes 'L' or 'R' may have to remove or add
3211 	 * scrollbars.  Have to update them anyway. */
3212 	if (gui.in_use && starting == 0)
3213 	{
3214 	    gui_init_which_components(NULL);
3215 	    gui_update_scrollbars(TRUE);
3216 	}
3217 	need_mouse_correct = TRUE;
3218 #endif
3219 
3220 	redraw_all_later(CLEAR);
3221 #ifdef FEAT_AUTOCMD
3222 	apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3223 	apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3224 #endif
3225 	return OK;
3226     }
3227 
3228     /* Failed, get back the previous Tab page */
3229     enter_tabpage(curtab, curbuf);
3230     return FAIL;
3231 }
3232 
3233 /*
3234  * Open a new tab page if ":tab cmd" was used.  It will edit the same buffer,
3235  * like with ":split".
3236  * Returns OK if a new tab page was created, FAIL otherwise.
3237  */
3238     int
3239 may_open_tabpage()
3240 {
3241     int		n = cmdmod.tab;
3242 
3243     if (cmdmod.tab != 0)
3244     {
3245 	cmdmod.tab = 0;	    /* reset it to avoid doing it twice */
3246 	return win_new_tabpage(n);
3247     }
3248     return FAIL;
3249 }
3250 
3251 /*
3252  * Create up to "maxcount" tabpages with empty windows.
3253  * Returns the number of resulting tab pages.
3254  */
3255     int
3256 make_tabpages(maxcount)
3257     int		maxcount;
3258 {
3259     int		count = maxcount;
3260     int		todo;
3261 
3262     /* Limit to 'tabpagemax' tabs. */
3263     if (count > p_tpm)
3264 	count = p_tpm;
3265 
3266 #ifdef FEAT_AUTOCMD
3267     /*
3268      * Don't execute autocommands while creating the tab pages.  Must do that
3269      * when putting the buffers in the windows.
3270      */
3271     ++autocmd_block;
3272 #endif
3273 
3274     for (todo = count - 1; todo > 0; --todo)
3275 	if (win_new_tabpage(0) == FAIL)
3276 	    break;
3277 
3278 #ifdef FEAT_AUTOCMD
3279     --autocmd_block;
3280 #endif
3281 
3282     /* return actual number of tab pages */
3283     return (count - todo);
3284 }
3285 
3286 /*
3287  * Return TRUE when "tpc" points to a valid tab page.
3288  */
3289     int
3290 valid_tabpage(tpc)
3291     tabpage_T	*tpc;
3292 {
3293     tabpage_T	*tp;
3294 
3295     for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3296 	if (tp == tpc)
3297 	    return TRUE;
3298     return FALSE;
3299 }
3300 
3301 /*
3302  * Find tab page "n" (first one is 1).  Returns NULL when not found.
3303  */
3304     tabpage_T *
3305 find_tabpage(n)
3306     int		n;
3307 {
3308     tabpage_T	*tp;
3309     int		i = 1;
3310 
3311     for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3312 	++i;
3313     return tp;
3314 }
3315 
3316 /*
3317  * Get index of tab page "tp".  First one has index 1.
3318  * When not found returns number of tab pages plus one.
3319  */
3320     int
3321 tabpage_index(ftp)
3322     tabpage_T	*ftp;
3323 {
3324     int		i = 1;
3325     tabpage_T	*tp;
3326 
3327     for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3328 	++i;
3329     return i;
3330 }
3331 
3332 /*
3333  * Prepare for leaving the current tab page.
3334  * When autocomands change "curtab" we don't leave the tab page and return
3335  * FAIL.
3336  * Careful: When OK is returned need to get a new tab page very very soon!
3337  */
3338 /*ARGSUSED*/
3339     static int
3340 leave_tabpage(new_curbuf)
3341     buf_T	*new_curbuf;	    /* what is going to be the new curbuf,
3342 				       NULL if unknown */
3343 {
3344     tabpage_T	*tp = curtab;
3345 
3346 #ifdef FEAT_VISUAL
3347     reset_VIsual_and_resel();	/* stop Visual mode */
3348 #endif
3349 #ifdef FEAT_AUTOCMD
3350     if (new_curbuf != curbuf)
3351     {
3352 	apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3353 	if (curtab != tp)
3354 	    return FAIL;
3355     }
3356     apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3357     if (curtab != tp)
3358 	return FAIL;
3359     apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
3360     if (curtab != tp)
3361 	return FAIL;
3362 #endif
3363 #if defined(FEAT_GUI)
3364     /* Remove the scrollbars.  They may be added back later. */
3365     if (gui.in_use)
3366 	gui_remove_scrollbars();
3367 #endif
3368     tp->tp_curwin = curwin;
3369     tp->tp_prevwin = prevwin;
3370     tp->tp_firstwin = firstwin;
3371     tp->tp_lastwin = lastwin;
3372     tp->tp_old_Rows = Rows;
3373     tp->tp_old_Columns = Columns;
3374     firstwin = NULL;
3375     lastwin = NULL;
3376     return OK;
3377 }
3378 
3379 /*
3380  * Start using tab page "tp".
3381  * Only to be used after leave_tabpage() or freeing the current tab page.
3382  */
3383 /*ARGSUSED*/
3384     static void
3385 enter_tabpage(tp, old_curbuf)
3386     tabpage_T	*tp;
3387     buf_T	*old_curbuf;
3388 {
3389     int		old_off = tp->tp_firstwin->w_winrow;
3390 
3391     curtab = tp;
3392     firstwin = tp->tp_firstwin;
3393     lastwin = tp->tp_lastwin;
3394     topframe = tp->tp_topframe;
3395 #ifdef FEAT_AUTOCMD
3396     apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3397 #endif
3398 
3399     win_enter_ext(tp->tp_curwin, FALSE, TRUE);
3400     prevwin = tp->tp_prevwin;
3401 
3402 #ifdef FEAT_AUTOCMD
3403     if (old_curbuf != curbuf)
3404 	apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3405 #endif
3406 
3407     last_status(FALSE);		/* status line may appear or disappear */
3408     (void)win_comp_pos();	/* recompute w_winrow for all windows */
3409     must_redraw = CLEAR;	/* need to redraw everything */
3410 #ifdef FEAT_DIFF
3411     diff_need_scrollbind = TRUE;
3412 #endif
3413 
3414     /* The tabpage line may have appeared or disappeared, may need to resize
3415      * the frames for that.  When the Vim window was resized need to update
3416      * frame sizes too. */
3417     if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3418 #ifdef FEAT_GUI_TABLINE
3419 			    && !gui_use_tabline()
3420 #endif
3421 		))
3422 	shell_new_rows();
3423 #ifdef FEAT_VERTSPLIT
3424     if (curtab->tp_old_Columns != Columns && starting == 0)
3425 	shell_new_columns();	/* update window widths */
3426 #endif
3427 
3428 #if defined(FEAT_GUI)
3429     /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3430      * scrollbars.  Have to update them anyway. */
3431     if (gui.in_use && starting == 0)
3432     {
3433 	gui_init_which_components(NULL);
3434 	gui_update_scrollbars(TRUE);
3435     }
3436     need_mouse_correct = TRUE;
3437 #endif
3438 
3439     redraw_all_later(CLEAR);
3440 }
3441 
3442 /*
3443  * Go to tab page "n".  For ":tab N" and "Ngt".
3444  * When "n" is 9999 go to the last tab page.
3445  */
3446     void
3447 goto_tabpage(n)
3448     int	    n;
3449 {
3450     tabpage_T	*tp;
3451     tabpage_T	*ttp;
3452     int		i;
3453 
3454     /* If there is only one it can't work. */
3455     if (first_tabpage->tp_next == NULL)
3456     {
3457 	if (n > 1)
3458 	    beep_flush();
3459 	return;
3460     }
3461 
3462     if (n == 0)
3463     {
3464 	/* No count, go to next tab page, wrap around end. */
3465 	if (curtab->tp_next == NULL)
3466 	    tp = first_tabpage;
3467 	else
3468 	    tp = curtab->tp_next;
3469     }
3470     else if (n < 0)
3471     {
3472 	/* "gT": go to previous tab page, wrap around end.  "N gT" repeats
3473 	 * this N times. */
3474 	ttp = curtab;
3475 	for (i = n; i < 0; ++i)
3476 	{
3477 	    for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3478 		    tp = tp->tp_next)
3479 		;
3480 	    ttp = tp;
3481 	}
3482     }
3483     else if (n == 9999)
3484     {
3485 	/* Go to last tab page. */
3486 	for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3487 	    ;
3488     }
3489     else
3490     {
3491 	/* Go to tab page "n". */
3492 	tp = find_tabpage(n);
3493 	if (tp == NULL)
3494 	{
3495 	    beep_flush();
3496 	    return;
3497 	}
3498     }
3499 
3500     goto_tabpage_tp(tp);
3501 
3502 #ifdef FEAT_GUI_TABLINE
3503     if (gui_use_tabline())
3504 	gui_mch_set_curtab(tabpage_index(curtab));
3505 #endif
3506 }
3507 
3508 /*
3509  * Go to tabpage "tp".
3510  * Note: doesn't update the GUI tab.
3511  */
3512     void
3513 goto_tabpage_tp(tp)
3514     tabpage_T	*tp;
3515 {
3516     if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer) == OK)
3517     {
3518 	if (valid_tabpage(tp))
3519 	    enter_tabpage(tp, curbuf);
3520 	else
3521 	    enter_tabpage(curtab, curbuf);
3522     }
3523 }
3524 
3525 /*
3526  * Move the current tab page to before tab page "nr".
3527  */
3528     void
3529 tabpage_move(nr)
3530     int		nr;
3531 {
3532     int		n = nr;
3533     tabpage_T	*tp;
3534 
3535     if (first_tabpage->tp_next == NULL)
3536 	return;
3537 
3538     /* Remove the current tab page from the list of tab pages. */
3539     if (curtab == first_tabpage)
3540 	first_tabpage = curtab->tp_next;
3541     else
3542     {
3543 	for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3544 	    if (tp->tp_next == curtab)
3545 		break;
3546 	if (tp == NULL)	/* "cannot happen" */
3547 	    return;
3548 	tp->tp_next = curtab->tp_next;
3549     }
3550 
3551     /* Re-insert it at the specified position. */
3552     if (n == 0)
3553     {
3554 	curtab->tp_next = first_tabpage;
3555 	first_tabpage = curtab;
3556     }
3557     else
3558     {
3559 	for (tp = first_tabpage; tp->tp_next != NULL && n > 1; tp = tp->tp_next)
3560 	    --n;
3561 	curtab->tp_next = tp->tp_next;
3562 	tp->tp_next = curtab;
3563     }
3564 
3565     /* Need to redraw the tabline.  Tab page contents doesn't change. */
3566     redraw_tabline = TRUE;
3567 }
3568 
3569 
3570 /*
3571  * Go to another window.
3572  * When jumping to another buffer, stop Visual mode.  Do this before
3573  * changing windows so we can yank the selection into the '*' register.
3574  * When jumping to another window on the same buffer, adjust its cursor
3575  * position to keep the same Visual area.
3576  */
3577     void
3578 win_goto(wp)
3579     win_T	*wp;
3580 {
3581     if (text_locked())
3582     {
3583 	beep_flush();
3584 	text_locked_msg();
3585 	return;
3586     }
3587 #ifdef FEAT_AUTOCMD
3588     if (curbuf_locked())
3589 	return;
3590 #endif
3591 
3592 #ifdef FEAT_VISUAL
3593     if (wp->w_buffer != curbuf)
3594 	reset_VIsual_and_resel();
3595     else if (VIsual_active)
3596 	wp->w_cursor = curwin->w_cursor;
3597 #endif
3598 
3599 #ifdef FEAT_GUI
3600     need_mouse_correct = TRUE;
3601 #endif
3602     win_enter(wp, TRUE);
3603 }
3604 
3605 #if defined(FEAT_PERL) || defined(PROTO)
3606 /*
3607  * Find window number "winnr" (counting top to bottom).
3608  */
3609     win_T *
3610 win_find_nr(winnr)
3611     int		winnr;
3612 {
3613     win_T	*wp;
3614 
3615 # ifdef FEAT_WINDOWS
3616     for (wp = firstwin; wp != NULL; wp = wp->w_next)
3617 	if (--winnr == 0)
3618 	    break;
3619     return wp;
3620 # else
3621     return curwin;
3622 # endif
3623 }
3624 #endif
3625 
3626 #ifdef FEAT_VERTSPLIT
3627 /*
3628  * Move to window above or below "count" times.
3629  */
3630     static void
3631 win_goto_ver(up, count)
3632     int		up;		/* TRUE to go to win above */
3633     long	count;
3634 {
3635     frame_T	*fr;
3636     frame_T	*nfr;
3637     frame_T	*foundfr;
3638 
3639     foundfr = curwin->w_frame;
3640     while (count--)
3641     {
3642 	/*
3643 	 * First go upwards in the tree of frames until we find a upwards or
3644 	 * downwards neighbor.
3645 	 */
3646 	fr = foundfr;
3647 	for (;;)
3648 	{
3649 	    if (fr == topframe)
3650 		goto end;
3651 	    if (up)
3652 		nfr = fr->fr_prev;
3653 	    else
3654 		nfr = fr->fr_next;
3655 	    if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
3656 		break;
3657 	    fr = fr->fr_parent;
3658 	}
3659 
3660 	/*
3661 	 * Now go downwards to find the bottom or top frame in it.
3662 	 */
3663 	for (;;)
3664 	{
3665 	    if (nfr->fr_layout == FR_LEAF)
3666 	    {
3667 		foundfr = nfr;
3668 		break;
3669 	    }
3670 	    fr = nfr->fr_child;
3671 	    if (nfr->fr_layout == FR_ROW)
3672 	    {
3673 		/* Find the frame at the cursor row. */
3674 		while (fr->fr_next != NULL
3675 			&& frame2win(fr)->w_wincol + fr->fr_width
3676 					 <= curwin->w_wincol + curwin->w_wcol)
3677 		    fr = fr->fr_next;
3678 	    }
3679 	    if (nfr->fr_layout == FR_COL && up)
3680 		while (fr->fr_next != NULL)
3681 		    fr = fr->fr_next;
3682 	    nfr = fr;
3683 	}
3684     }
3685 end:
3686     if (foundfr != NULL)
3687 	win_goto(foundfr->fr_win);
3688 }
3689 
3690 /*
3691  * Move to left or right window.
3692  */
3693     static void
3694 win_goto_hor(left, count)
3695     int		left;		/* TRUE to go to left win */
3696     long	count;
3697 {
3698     frame_T	*fr;
3699     frame_T	*nfr;
3700     frame_T	*foundfr;
3701 
3702     foundfr = curwin->w_frame;
3703     while (count--)
3704     {
3705 	/*
3706 	 * First go upwards in the tree of frames until we find a left or
3707 	 * right neighbor.
3708 	 */
3709 	fr = foundfr;
3710 	for (;;)
3711 	{
3712 	    if (fr == topframe)
3713 		goto end;
3714 	    if (left)
3715 		nfr = fr->fr_prev;
3716 	    else
3717 		nfr = fr->fr_next;
3718 	    if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
3719 		break;
3720 	    fr = fr->fr_parent;
3721 	}
3722 
3723 	/*
3724 	 * Now go downwards to find the leftmost or rightmost frame in it.
3725 	 */
3726 	for (;;)
3727 	{
3728 	    if (nfr->fr_layout == FR_LEAF)
3729 	    {
3730 		foundfr = nfr;
3731 		break;
3732 	    }
3733 	    fr = nfr->fr_child;
3734 	    if (nfr->fr_layout == FR_COL)
3735 	    {
3736 		/* Find the frame at the cursor row. */
3737 		while (fr->fr_next != NULL
3738 			&& frame2win(fr)->w_winrow + fr->fr_height
3739 					 <= curwin->w_winrow + curwin->w_wrow)
3740 		    fr = fr->fr_next;
3741 	    }
3742 	    if (nfr->fr_layout == FR_ROW && left)
3743 		while (fr->fr_next != NULL)
3744 		    fr = fr->fr_next;
3745 	    nfr = fr;
3746 	}
3747     }
3748 end:
3749     if (foundfr != NULL)
3750 	win_goto(foundfr->fr_win);
3751 }
3752 #endif
3753 
3754 /*
3755  * Make window "wp" the current window.
3756  */
3757     void
3758 win_enter(wp, undo_sync)
3759     win_T	*wp;
3760     int		undo_sync;
3761 {
3762     win_enter_ext(wp, undo_sync, FALSE);
3763 }
3764 
3765 /*
3766  * Make window wp the current window.
3767  * Can be called with "curwin_invalid" TRUE, which means that curwin has just
3768  * been closed and isn't valid.
3769  */
3770     static void
3771 win_enter_ext(wp, undo_sync, curwin_invalid)
3772     win_T	*wp;
3773     int		undo_sync;
3774     int		curwin_invalid;
3775 {
3776 #ifdef FEAT_AUTOCMD
3777     int		other_buffer = FALSE;
3778 #endif
3779 
3780     if (wp == curwin && !curwin_invalid)	/* nothing to do */
3781 	return;
3782 
3783 #ifdef FEAT_AUTOCMD
3784     if (!curwin_invalid)
3785     {
3786 	/*
3787 	 * Be careful: If autocommands delete the window, return now.
3788 	 */
3789 	if (wp->w_buffer != curbuf)
3790 	{
3791 	    apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3792 	    other_buffer = TRUE;
3793 	    if (!win_valid(wp))
3794 		return;
3795 	}
3796 	apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3797 	if (!win_valid(wp))
3798 	    return;
3799 # ifdef FEAT_EVAL
3800 	/* autocmds may abort script processing */
3801 	if (aborting())
3802 	    return;
3803 # endif
3804     }
3805 #endif
3806 
3807     /* sync undo before leaving the current buffer */
3808     if (undo_sync && curbuf != wp->w_buffer)
3809 	u_sync();
3810     /* may have to copy the buffer options when 'cpo' contains 'S' */
3811     if (wp->w_buffer != curbuf)
3812 	buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
3813     if (!curwin_invalid)
3814     {
3815 	prevwin = curwin;	/* remember for CTRL-W p */
3816 	curwin->w_redr_status = TRUE;
3817     }
3818     curwin = wp;
3819     curbuf = wp->w_buffer;
3820     check_cursor();
3821 #ifdef FEAT_VIRTUALEDIT
3822     if (!virtual_active())
3823 	curwin->w_cursor.coladd = 0;
3824 #endif
3825     changed_line_abv_curs();	/* assume cursor position needs updating */
3826 
3827     if (curwin->w_localdir != NULL)
3828     {
3829 	/* Window has a local directory: Save current directory as global
3830 	 * directory (unless that was done already) and change to the local
3831 	 * directory. */
3832 	if (globaldir == NULL)
3833 	{
3834 	    char_u	cwd[MAXPATHL];
3835 
3836 	    if (mch_dirname(cwd, MAXPATHL) == OK)
3837 		globaldir = vim_strsave(cwd);
3838 	}
3839 	mch_chdir((char *)curwin->w_localdir);
3840 	shorten_fnames(TRUE);
3841     }
3842     else if (globaldir != NULL)
3843     {
3844 	/* Window doesn't have a local directory and we are not in the global
3845 	 * directory: Change to the global directory. */
3846 	mch_chdir((char *)globaldir);
3847 	vim_free(globaldir);
3848 	globaldir = NULL;
3849 	shorten_fnames(TRUE);
3850     }
3851 
3852 #ifdef FEAT_AUTOCMD
3853     apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
3854     if (other_buffer)
3855 	apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3856 #endif
3857 
3858 #ifdef FEAT_TITLE
3859     maketitle();
3860 #endif
3861     curwin->w_redr_status = TRUE;
3862     redraw_tabline = TRUE;
3863     if (restart_edit)
3864 	redraw_later(VALID);	/* causes status line redraw */
3865 
3866     /* set window height to desired minimal value */
3867     if (curwin->w_height < p_wh && !curwin->w_p_wfh)
3868 	win_setheight((int)p_wh);
3869     else if (curwin->w_height == 0)
3870 	win_setheight(1);
3871 
3872 #ifdef FEAT_VERTSPLIT
3873     /* set window width to desired minimal value */
3874     if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
3875 	win_setwidth((int)p_wiw);
3876 #endif
3877 
3878 #ifdef FEAT_MOUSE
3879     setmouse();			/* in case jumped to/from help buffer */
3880 #endif
3881 
3882 #if defined(FEAT_NETBEANS_INTG) || defined(FEAT_SUN_WORKSHOP)
3883     /* Change directories when the acd option is set on and after
3884      * switching windows. */
3885     if (p_acd && curbuf->b_ffname != NULL
3886 				     && vim_chdirfile(curbuf->b_ffname) == OK)
3887 	shorten_fnames(TRUE);
3888 #endif
3889 }
3890 
3891 #endif /* FEAT_WINDOWS */
3892 
3893 #if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
3894 /*
3895  * Jump to the first open window that contains buffer buf if one exists
3896  * TODO: Alternatively jump to last open window? Dependent from 'splitbelow'?
3897  * Returns pointer to window if it exists, otherwise NULL.
3898  */
3899     win_T *
3900 buf_jump_open_win(buf)
3901     buf_T	*buf;
3902 {
3903 # ifdef FEAT_WINDOWS
3904     win_T	*wp;
3905 
3906     for (wp = firstwin; wp; wp = wp->w_next)
3907 	if (wp->w_buffer == buf)
3908 	    break;
3909     if (wp != NULL)
3910 	win_enter(wp, FALSE);
3911     return wp;
3912 # else
3913     if (curwin->w_buffer == buf)
3914 	return curwin;
3915     return NULL;
3916 # endif
3917 }
3918 #endif
3919 
3920 /*
3921  * allocate a window structure and link it in the window list
3922  */
3923 /*ARGSUSED*/
3924     static win_T *
3925 win_alloc(after)
3926     win_T	*after;
3927 {
3928     win_T	*newwin;
3929 
3930     /*
3931      * allocate window structure and linesizes arrays
3932      */
3933     newwin = (win_T *)alloc_clear((unsigned)sizeof(win_T));
3934     if (newwin != NULL && win_alloc_lines(newwin) == FAIL)
3935     {
3936 	vim_free(newwin);
3937 	newwin = NULL;
3938     }
3939 
3940     if (newwin != NULL)
3941     {
3942 	/*
3943 	 * link the window in the window list
3944 	 */
3945 #ifdef FEAT_WINDOWS
3946 	win_append(after, newwin);
3947 #endif
3948 #ifdef FEAT_VERTSPLIT
3949 	newwin->w_wincol = 0;
3950 	newwin->w_width = Columns;
3951 #endif
3952 
3953 	/* position the display and the cursor at the top of the file. */
3954 	newwin->w_topline = 1;
3955 #ifdef FEAT_DIFF
3956 	newwin->w_topfill = 0;
3957 #endif
3958 	newwin->w_botline = 2;
3959 	newwin->w_cursor.lnum = 1;
3960 #ifdef FEAT_SCROLLBIND
3961 	newwin->w_scbind_pos = 1;
3962 #endif
3963 
3964 	/* We won't calculate w_fraction until resizing the window */
3965 	newwin->w_fraction = 0;
3966 	newwin->w_prev_fraction_row = -1;
3967 
3968 #ifdef FEAT_GUI
3969 	if (gui.in_use)
3970 	{
3971 	    out_flush();
3972 	    gui_create_scrollbar(&newwin->w_scrollbars[SBAR_LEFT],
3973 		    SBAR_LEFT, newwin);
3974 	    gui_create_scrollbar(&newwin->w_scrollbars[SBAR_RIGHT],
3975 		    SBAR_RIGHT, newwin);
3976 	}
3977 #endif
3978 #ifdef FEAT_EVAL
3979 	/* init w: variables */
3980 	init_var_dict(&newwin->w_vars, &newwin->w_winvar);
3981 #endif
3982 #ifdef FEAT_FOLDING
3983 	foldInitWin(newwin);
3984 #endif
3985     }
3986     return newwin;
3987 }
3988 
3989 #if defined(FEAT_WINDOWS) || defined(PROTO)
3990 
3991 /*
3992  * remove window 'wp' from the window list and free the structure
3993  */
3994     static void
3995 win_free(wp, tp)
3996     win_T	*wp;
3997     tabpage_T	*tp;		/* tab page "win" is in, NULL for current */
3998 {
3999     int		i;
4000 
4001 #ifdef FEAT_MZSCHEME
4002     mzscheme_window_free(wp);
4003 #endif
4004 
4005 #ifdef FEAT_PERL
4006     perl_win_free(wp);
4007 #endif
4008 
4009 #ifdef FEAT_PYTHON
4010     python_window_free(wp);
4011 #endif
4012 
4013 #ifdef FEAT_TCL
4014     tcl_window_free(wp);
4015 #endif
4016 
4017 #ifdef FEAT_RUBY
4018     ruby_window_free(wp);
4019 #endif
4020 
4021     clear_winopt(&wp->w_onebuf_opt);
4022     clear_winopt(&wp->w_allbuf_opt);
4023 
4024 #ifdef FEAT_EVAL
4025     vars_clear(&wp->w_vars.dv_hashtab);	    /* free all w: variables */
4026 #endif
4027 
4028     if (prevwin == wp)
4029 	prevwin = NULL;
4030     win_free_lsize(wp);
4031 
4032     for (i = 0; i < wp->w_tagstacklen; ++i)
4033 	vim_free(wp->w_tagstack[i].tagname);
4034 
4035     vim_free(wp->w_localdir);
4036 #ifdef FEAT_SEARCH_EXTRA
4037     vim_free(wp->w_match[0].regprog);
4038     vim_free(wp->w_match[1].regprog);
4039     vim_free(wp->w_match[2].regprog);
4040 #endif
4041 #ifdef FEAT_JUMPLIST
4042     free_jumplist(wp);
4043 #endif
4044 
4045 #ifdef FEAT_QUICKFIX
4046     qf_free_all(wp);
4047 #endif
4048 
4049 #ifdef FEAT_GUI
4050     if (gui.in_use)
4051     {
4052 	out_flush();
4053 	gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4054 	gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4055     }
4056 #endif /* FEAT_GUI */
4057 
4058     win_remove(wp, tp);
4059     vim_free(wp);
4060 }
4061 
4062 /*
4063  * Append window "wp" in the window list after window "after".
4064  */
4065     static void
4066 win_append(after, wp)
4067     win_T	*after, *wp;
4068 {
4069     win_T	*before;
4070 
4071     if (after == NULL)	    /* after NULL is in front of the first */
4072 	before = firstwin;
4073     else
4074 	before = after->w_next;
4075 
4076     wp->w_next = before;
4077     wp->w_prev = after;
4078     if (after == NULL)
4079 	firstwin = wp;
4080     else
4081 	after->w_next = wp;
4082     if (before == NULL)
4083 	lastwin = wp;
4084     else
4085 	before->w_prev = wp;
4086 }
4087 
4088 /*
4089  * Remove a window from the window list.
4090  */
4091     static void
4092 win_remove(wp, tp)
4093     win_T	*wp;
4094     tabpage_T	*tp;		/* tab page "win" is in, NULL for current */
4095 {
4096     if (wp->w_prev != NULL)
4097 	wp->w_prev->w_next = wp->w_next;
4098     else if (tp == NULL)
4099 	firstwin = wp->w_next;
4100     else
4101 	tp->tp_firstwin = wp->w_next;
4102     if (wp->w_next != NULL)
4103 	wp->w_next->w_prev = wp->w_prev;
4104     else if (tp == NULL)
4105 	lastwin = wp->w_prev;
4106     else
4107 	tp->tp_lastwin = wp->w_prev;
4108 }
4109 
4110 /*
4111  * Append frame "frp" in a frame list after frame "after".
4112  */
4113     static void
4114 frame_append(after, frp)
4115     frame_T	*after, *frp;
4116 {
4117     frp->fr_next = after->fr_next;
4118     after->fr_next = frp;
4119     if (frp->fr_next != NULL)
4120 	frp->fr_next->fr_prev = frp;
4121     frp->fr_prev = after;
4122 }
4123 
4124 /*
4125  * Insert frame "frp" in a frame list before frame "before".
4126  */
4127     static void
4128 frame_insert(before, frp)
4129     frame_T	*before, *frp;
4130 {
4131     frp->fr_next = before;
4132     frp->fr_prev = before->fr_prev;
4133     before->fr_prev = frp;
4134     if (frp->fr_prev != NULL)
4135 	frp->fr_prev->fr_next = frp;
4136     else
4137 	frp->fr_parent->fr_child = frp;
4138 }
4139 
4140 /*
4141  * Remove a frame from a frame list.
4142  */
4143     static void
4144 frame_remove(frp)
4145     frame_T	*frp;
4146 {
4147     if (frp->fr_prev != NULL)
4148 	frp->fr_prev->fr_next = frp->fr_next;
4149     else
4150 	frp->fr_parent->fr_child = frp->fr_next;
4151     if (frp->fr_next != NULL)
4152 	frp->fr_next->fr_prev = frp->fr_prev;
4153 }
4154 
4155 #endif /* FEAT_WINDOWS */
4156 
4157 /*
4158  * Allocate w_lines[] for window "wp".
4159  * Return FAIL for failure, OK for success.
4160  */
4161     int
4162 win_alloc_lines(wp)
4163     win_T	*wp;
4164 {
4165     wp->w_lines_valid = 0;
4166     wp->w_lines = (wline_T *)alloc((unsigned)(Rows * sizeof(wline_T)));
4167     if (wp->w_lines == NULL)
4168 	return FAIL;
4169     return OK;
4170 }
4171 
4172 /*
4173  * free lsize arrays for a window
4174  */
4175     void
4176 win_free_lsize(wp)
4177     win_T	*wp;
4178 {
4179     vim_free(wp->w_lines);
4180     wp->w_lines = NULL;
4181 }
4182 
4183 /*
4184  * Called from win_new_shellsize() after Rows changed.
4185  * This only does the current tab page, others must be done when made active.
4186  */
4187     void
4188 shell_new_rows()
4189 {
4190     int		h = (int)ROWS_AVAIL;
4191 
4192     if (firstwin == NULL)	/* not initialized yet */
4193 	return;
4194 #ifdef FEAT_WINDOWS
4195     if (h < frame_minheight(topframe, NULL))
4196 	h = frame_minheight(topframe, NULL);
4197 
4198     /* First try setting the heights of windows with 'winfixheight'.  If
4199      * that doesn't result in the right height, forget about that option. */
4200     frame_new_height(topframe, h, FALSE, TRUE);
4201     if (topframe->fr_height != h)
4202 	frame_new_height(topframe, h, FALSE, FALSE);
4203 
4204     (void)win_comp_pos();		/* recompute w_winrow and w_wincol */
4205 #else
4206     if (h < 1)
4207 	h = 1;
4208     win_new_height(firstwin, h);
4209 #endif
4210     compute_cmdrow();
4211 #ifdef FEAT_WINDOWS
4212     p_ch_used = p_ch;
4213 #endif
4214 
4215 #if 0
4216     /* Disabled: don't want making the screen smaller make a window larger. */
4217     if (p_ea)
4218 	win_equal(curwin, FALSE, 'v');
4219 #endif
4220 }
4221 
4222 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4223 /*
4224  * Called from win_new_shellsize() after Columns changed.
4225  */
4226     void
4227 shell_new_columns()
4228 {
4229     if (firstwin == NULL)	/* not initialized yet */
4230 	return;
4231 
4232     /* First try setting the widths of windows with 'winfixwidth'.  If that
4233      * doesn't result in the right width, forget about that option. */
4234     frame_new_width(topframe, (int)Columns, FALSE, TRUE);
4235     if (topframe->fr_width != Columns)
4236 	frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4237 
4238     (void)win_comp_pos();		/* recompute w_winrow and w_wincol */
4239 #if 0
4240     /* Disabled: don't want making the screen smaller make a window larger. */
4241     if (p_ea)
4242 	win_equal(curwin, FALSE, 'h');
4243 #endif
4244 }
4245 #endif
4246 
4247 #if defined(FEAT_CMDWIN) || defined(PROTO)
4248 /*
4249  * Save the size of all windows in "gap".
4250  */
4251     void
4252 win_size_save(gap)
4253     garray_T	*gap;
4254 
4255 {
4256     win_T	*wp;
4257 
4258     ga_init2(gap, (int)sizeof(int), 1);
4259     if (ga_grow(gap, win_count() * 2) == OK)
4260 	for (wp = firstwin; wp != NULL; wp = wp->w_next)
4261 	{
4262 	    ((int *)gap->ga_data)[gap->ga_len++] =
4263 					       wp->w_width + wp->w_vsep_width;
4264 	    ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4265 	}
4266 }
4267 
4268 /*
4269  * Restore window sizes, but only if the number of windows is still the same.
4270  * Does not free the growarray.
4271  */
4272     void
4273 win_size_restore(gap)
4274     garray_T	*gap;
4275 {
4276     win_T	*wp;
4277     int		i;
4278 
4279     if (win_count() * 2 == gap->ga_len)
4280     {
4281 	i = 0;
4282 	for (wp = firstwin; wp != NULL; wp = wp->w_next)
4283 	{
4284 	    frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4285 	    win_setheight_win(((int *)gap->ga_data)[i++], wp);
4286 	}
4287 	/* recompute the window positions */
4288 	(void)win_comp_pos();
4289     }
4290 }
4291 #endif /* FEAT_CMDWIN */
4292 
4293 #if defined(FEAT_WINDOWS) || defined(PROTO)
4294 /*
4295  * Update the position for all windows, using the width and height of the
4296  * frames.
4297  * Returns the row just after the last window.
4298  */
4299     int
4300 win_comp_pos()
4301 {
4302     int		row = tabline_height();
4303     int		col = 0;
4304 
4305     frame_comp_pos(topframe, &row, &col);
4306     return row;
4307 }
4308 
4309 /*
4310  * Update the position of the windows in frame "topfrp", using the width and
4311  * height of the frames.
4312  * "*row" and "*col" are the top-left position of the frame.  They are updated
4313  * to the bottom-right position plus one.
4314  */
4315     static void
4316 frame_comp_pos(topfrp, row, col)
4317     frame_T	*topfrp;
4318     int		*row;
4319     int		*col;
4320 {
4321     win_T	*wp;
4322     frame_T	*frp;
4323 #ifdef FEAT_VERTSPLIT
4324     int		startcol;
4325     int		startrow;
4326 #endif
4327 
4328     wp = topfrp->fr_win;
4329     if (wp != NULL)
4330     {
4331 	if (wp->w_winrow != *row
4332 #ifdef FEAT_VERTSPLIT
4333 		|| wp->w_wincol != *col
4334 #endif
4335 		)
4336 	{
4337 	    /* position changed, redraw */
4338 	    wp->w_winrow = *row;
4339 #ifdef FEAT_VERTSPLIT
4340 	    wp->w_wincol = *col;
4341 #endif
4342 	    redraw_win_later(wp, NOT_VALID);
4343 	    wp->w_redr_status = TRUE;
4344 	}
4345 	*row += wp->w_height + wp->w_status_height;
4346 #ifdef FEAT_VERTSPLIT
4347 	*col += wp->w_width + wp->w_vsep_width;
4348 #endif
4349     }
4350     else
4351     {
4352 #ifdef FEAT_VERTSPLIT
4353 	startrow = *row;
4354 	startcol = *col;
4355 #endif
4356 	for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4357 	{
4358 #ifdef FEAT_VERTSPLIT
4359 	    if (topfrp->fr_layout == FR_ROW)
4360 		*row = startrow;	/* all frames are at the same row */
4361 	    else
4362 		*col = startcol;	/* all frames are at the same col */
4363 #endif
4364 	    frame_comp_pos(frp, row, col);
4365 	}
4366     }
4367 }
4368 
4369 #endif /* FEAT_WINDOWS */
4370 
4371 /*
4372  * Set current window height and take care of repositioning other windows to
4373  * fit around it.
4374  */
4375     void
4376 win_setheight(height)
4377     int		height;
4378 {
4379     win_setheight_win(height, curwin);
4380 }
4381 
4382 /*
4383  * Set the window height of window "win" and take care of repositioning other
4384  * windows to fit around it.
4385  */
4386     void
4387 win_setheight_win(height, win)
4388     int		height;
4389     win_T	*win;
4390 {
4391     int		row;
4392 
4393     if (win == curwin)
4394     {
4395 	/* Always keep current window at least one line high, even when
4396 	 * 'winminheight' is zero. */
4397 #ifdef FEAT_WINDOWS
4398 	if (height < p_wmh)
4399 	    height = p_wmh;
4400 #endif
4401 	if (height == 0)
4402 	    height = 1;
4403     }
4404 
4405 #ifdef FEAT_WINDOWS
4406     frame_setheight(win->w_frame, height + win->w_status_height);
4407 
4408     /* recompute the window positions */
4409     row = win_comp_pos();
4410 #else
4411     if (height > topframe->fr_height)
4412 	height = topframe->fr_height;
4413     win->w_height = height;
4414     row = height;
4415 #endif
4416 
4417     /*
4418      * If there is extra space created between the last window and the command
4419      * line, clear it.
4420      */
4421     if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4422 	screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4423     cmdline_row = row;
4424     msg_row = row;
4425     msg_col = 0;
4426 
4427     redraw_all_later(NOT_VALID);
4428 }
4429 
4430 #if defined(FEAT_WINDOWS) || defined(PROTO)
4431 
4432 /*
4433  * Set the height of a frame to "height" and take care that all frames and
4434  * windows inside it are resized.  Also resize frames on the left and right if
4435  * the are in the same FR_ROW frame.
4436  *
4437  * Strategy:
4438  * If the frame is part of a FR_COL frame, try fitting the frame in that
4439  * frame.  If that doesn't work (the FR_COL frame is too small), recursively
4440  * go to containing frames to resize them and make room.
4441  * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4442  * Check for the minimal height of the FR_ROW frame.
4443  * At the top level we can also use change the command line height.
4444  */
4445     static void
4446 frame_setheight(curfrp, height)
4447     frame_T	*curfrp;
4448     int		height;
4449 {
4450     int		room;		/* total number of lines available */
4451     int		take;		/* number of lines taken from other windows */
4452     int		room_cmdline;	/* lines available from cmdline */
4453     int		run;
4454     frame_T	*frp;
4455     int		h;
4456     int		room_reserved;
4457 
4458     /* If the height already is the desired value, nothing to do. */
4459     if (curfrp->fr_height == height)
4460 	return;
4461 
4462     if (curfrp->fr_parent == NULL)
4463     {
4464 	/* topframe: can only change the command line */
4465 	if (height > ROWS_AVAIL)
4466 	    height = ROWS_AVAIL;
4467 	if (height > 0)
4468 	    frame_new_height(curfrp, height, FALSE, FALSE);
4469     }
4470     else if (curfrp->fr_parent->fr_layout == FR_ROW)
4471     {
4472 	/* Row of frames: Also need to resize frames left and right of this
4473 	 * one.  First check for the minimal height of these. */
4474 	h = frame_minheight(curfrp->fr_parent, NULL);
4475 	if (height < h)
4476 	    height = h;
4477 	frame_setheight(curfrp->fr_parent, height);
4478     }
4479     else
4480     {
4481 	/*
4482 	 * Column of frames: try to change only frames in this column.
4483 	 */
4484 #ifdef FEAT_VERTSPLIT
4485 	/*
4486 	 * Do this twice:
4487 	 * 1: compute room available, if it's not enough try resizing the
4488 	 *    containing frame.
4489 	 * 2: compute the room available and adjust the height to it.
4490 	 * Try not to reduce the height of a window with 'winfixheight' set.
4491 	 */
4492 	for (run = 1; run <= 2; ++run)
4493 #else
4494 	for (;;)
4495 #endif
4496 	{
4497 	    room = 0;
4498 	    room_reserved = 0;
4499 	    for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4500 							   frp = frp->fr_next)
4501 	    {
4502 		if (frp != curfrp
4503 			&& frp->fr_win != NULL
4504 			&& frp->fr_win->w_p_wfh)
4505 		    room_reserved += frp->fr_height;
4506 		room += frp->fr_height;
4507 		if (frp != curfrp)
4508 		    room -= frame_minheight(frp, NULL);
4509 	    }
4510 #ifdef FEAT_VERTSPLIT
4511 	    if (curfrp->fr_width != Columns)
4512 		room_cmdline = 0;
4513 	    else
4514 #endif
4515 	    {
4516 		room_cmdline = Rows - p_ch - (lastwin->w_winrow
4517 			       + lastwin->w_height + lastwin->w_status_height);
4518 		if (room_cmdline < 0)
4519 		    room_cmdline = 0;
4520 	    }
4521 
4522 	    if (height <= room + room_cmdline)
4523 		break;
4524 #ifdef FEAT_VERTSPLIT
4525 	    if (run == 2 || curfrp->fr_width == Columns)
4526 #endif
4527 	    {
4528 		if (height > room + room_cmdline)
4529 		    height = room + room_cmdline;
4530 		break;
4531 	    }
4532 #ifdef FEAT_VERTSPLIT
4533 	    frame_setheight(curfrp->fr_parent, height
4534 		+ frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
4535 #endif
4536 	    /*NOTREACHED*/
4537 	}
4538 
4539 	/*
4540 	 * Compute the number of lines we will take from others frames (can be
4541 	 * negative!).
4542 	 */
4543 	take = height - curfrp->fr_height;
4544 
4545 	/* If there is not enough room, also reduce the height of a window
4546 	 * with 'winfixheight' set. */
4547 	if (height > room + room_cmdline - room_reserved)
4548 	    room_reserved = room + room_cmdline - height;
4549 	/* If there is only a 'winfixheight' window and making the
4550 	 * window smaller, need to make the other window taller. */
4551 	if (take < 0 && room - curfrp->fr_height < room_reserved)
4552 	    room_reserved = 0;
4553 
4554 	if (take > 0 && room_cmdline > 0)
4555 	{
4556 	    /* use lines from cmdline first */
4557 	    if (take < room_cmdline)
4558 		room_cmdline = take;
4559 	    take -= room_cmdline;
4560 	    topframe->fr_height += room_cmdline;
4561 	}
4562 
4563 	/*
4564 	 * set the current frame to the new height
4565 	 */
4566 	frame_new_height(curfrp, height, FALSE, FALSE);
4567 
4568 	/*
4569 	 * First take lines from the frames after the current frame.  If
4570 	 * that is not enough, takes lines from frames above the current
4571 	 * frame.
4572 	 */
4573 	for (run = 0; run < 2; ++run)
4574 	{
4575 	    if (run == 0)
4576 		frp = curfrp->fr_next;	/* 1st run: start with next window */
4577 	    else
4578 		frp = curfrp->fr_prev;	/* 2nd run: start with prev window */
4579 	    while (frp != NULL && take != 0)
4580 	    {
4581 		h = frame_minheight(frp, NULL);
4582 		if (room_reserved > 0
4583 			&& frp->fr_win != NULL
4584 			&& frp->fr_win->w_p_wfh)
4585 		{
4586 		    if (room_reserved >= frp->fr_height)
4587 			room_reserved -= frp->fr_height;
4588 		    else
4589 		    {
4590 			if (frp->fr_height - room_reserved > take)
4591 			    room_reserved = frp->fr_height - take;
4592 			take -= frp->fr_height - room_reserved;
4593 			frame_new_height(frp, room_reserved, FALSE, FALSE);
4594 			room_reserved = 0;
4595 		    }
4596 		}
4597 		else
4598 		{
4599 		    if (frp->fr_height - take < h)
4600 		    {
4601 			take -= frp->fr_height - h;
4602 			frame_new_height(frp, h, FALSE, FALSE);
4603 		    }
4604 		    else
4605 		    {
4606 			frame_new_height(frp, frp->fr_height - take,
4607 								FALSE, FALSE);
4608 			take = 0;
4609 		    }
4610 		}
4611 		if (run == 0)
4612 		    frp = frp->fr_next;
4613 		else
4614 		    frp = frp->fr_prev;
4615 	    }
4616 	}
4617     }
4618 }
4619 
4620 #if defined(FEAT_VERTSPLIT) || defined(PROTO)
4621 /*
4622  * Set current window width and take care of repositioning other windows to
4623  * fit around it.
4624  */
4625     void
4626 win_setwidth(width)
4627     int		width;
4628 {
4629     win_setwidth_win(width, curwin);
4630 }
4631 
4632     void
4633 win_setwidth_win(width, wp)
4634     int		width;
4635     win_T	*wp;
4636 {
4637     /* Always keep current window at least one column wide, even when
4638      * 'winminwidth' is zero. */
4639     if (wp == curwin)
4640     {
4641 	if (width < p_wmw)
4642 	    width = p_wmw;
4643 	if (width == 0)
4644 	    width = 1;
4645     }
4646 
4647     frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
4648 
4649     /* recompute the window positions */
4650     (void)win_comp_pos();
4651 
4652     redraw_all_later(NOT_VALID);
4653 }
4654 
4655 /*
4656  * Set the width of a frame to "width" and take care that all frames and
4657  * windows inside it are resized.  Also resize frames above and below if the
4658  * are in the same FR_ROW frame.
4659  *
4660  * Strategy is similar to frame_setheight().
4661  */
4662     static void
4663 frame_setwidth(curfrp, width)
4664     frame_T	*curfrp;
4665     int		width;
4666 {
4667     int		room;		/* total number of lines available */
4668     int		take;		/* number of lines taken from other windows */
4669     int		run;
4670     frame_T	*frp;
4671     int		w;
4672     int		room_reserved;
4673 
4674     /* If the width already is the desired value, nothing to do. */
4675     if (curfrp->fr_width == width)
4676 	return;
4677 
4678     if (curfrp->fr_parent == NULL)
4679 	/* topframe: can't change width */
4680 	return;
4681 
4682     if (curfrp->fr_parent->fr_layout == FR_COL)
4683     {
4684 	/* Column of frames: Also need to resize frames above and below of
4685 	 * this one.  First check for the minimal width of these. */
4686 	w = frame_minwidth(curfrp->fr_parent, NULL);
4687 	if (width < w)
4688 	    width = w;
4689 	frame_setwidth(curfrp->fr_parent, width);
4690     }
4691     else
4692     {
4693 	/*
4694 	 * Row of frames: try to change only frames in this row.
4695 	 *
4696 	 * Do this twice:
4697 	 * 1: compute room available, if it's not enough try resizing the
4698 	 *    containing frame.
4699 	 * 2: compute the room available and adjust the width to it.
4700 	 */
4701 	for (run = 1; run <= 2; ++run)
4702 	{
4703 	    room = 0;
4704 	    room_reserved = 0;
4705 	    for (frp = curfrp->fr_parent->fr_child; frp != NULL;
4706 							   frp = frp->fr_next)
4707 	    {
4708 		if (frp != curfrp
4709 			&& frp->fr_win != NULL
4710 			&& frp->fr_win->w_p_wfw)
4711 		    room_reserved += frp->fr_width;
4712 		room += frp->fr_width;
4713 		if (frp != curfrp)
4714 		    room -= frame_minwidth(frp, NULL);
4715 	    }
4716 
4717 	    if (width <= room)
4718 		break;
4719 	    if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
4720 	    {
4721 		if (width > room)
4722 		    width = room;
4723 		break;
4724 	    }
4725 	    frame_setwidth(curfrp->fr_parent, width
4726 		 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
4727 	}
4728 
4729 	/*
4730 	 * Compute the number of lines we will take from others frames (can be
4731 	 * negative!).
4732 	 */
4733 	take = width - curfrp->fr_width;
4734 
4735 	/* If there is not enough room, also reduce the width of a window
4736 	 * with 'winfixwidth' set. */
4737 	if (width > room - room_reserved)
4738 	    room_reserved = room - width;
4739 	/* If there is only a 'winfixwidth' window and making the
4740 	 * window smaller, need to make the other window narrower. */
4741 	if (take < 0 && room - curfrp->fr_width < room_reserved)
4742 	    room_reserved = 0;
4743 
4744 	/*
4745 	 * set the current frame to the new width
4746 	 */
4747 	frame_new_width(curfrp, width, FALSE, FALSE);
4748 
4749 	/*
4750 	 * First take lines from the frames right of the current frame.  If
4751 	 * that is not enough, takes lines from frames left of the current
4752 	 * frame.
4753 	 */
4754 	for (run = 0; run < 2; ++run)
4755 	{
4756 	    if (run == 0)
4757 		frp = curfrp->fr_next;	/* 1st run: start with next window */
4758 	    else
4759 		frp = curfrp->fr_prev;	/* 2nd run: start with prev window */
4760 	    while (frp != NULL && take != 0)
4761 	    {
4762 		w = frame_minwidth(frp, NULL);
4763 		if (room_reserved > 0
4764 			&& frp->fr_win != NULL
4765 			&& frp->fr_win->w_p_wfw)
4766 		{
4767 		    if (room_reserved >= frp->fr_width)
4768 			room_reserved -= frp->fr_width;
4769 		    else
4770 		    {
4771 			if (frp->fr_width - room_reserved > take)
4772 			    room_reserved = frp->fr_width - take;
4773 			take -= frp->fr_width - room_reserved;
4774 			frame_new_width(frp, room_reserved, FALSE, FALSE);
4775 			room_reserved = 0;
4776 		    }
4777 		}
4778 		else
4779 		{
4780 		    if (frp->fr_width - take < w)
4781 		    {
4782 			take -= frp->fr_width - w;
4783 			frame_new_width(frp, w, FALSE, FALSE);
4784 		    }
4785 		    else
4786 		    {
4787 			frame_new_width(frp, frp->fr_width - take,
4788 								FALSE, FALSE);
4789 			take = 0;
4790 		    }
4791 		}
4792 		if (run == 0)
4793 		    frp = frp->fr_next;
4794 		else
4795 		    frp = frp->fr_prev;
4796 	    }
4797 	}
4798     }
4799 }
4800 #endif /* FEAT_VERTSPLIT */
4801 
4802 /*
4803  * Check 'winminheight' for a valid value.
4804  */
4805     void
4806 win_setminheight()
4807 {
4808     int		room;
4809     int		first = TRUE;
4810     win_T	*wp;
4811 
4812     /* loop until there is a 'winminheight' that is possible */
4813     while (p_wmh > 0)
4814     {
4815 	/* TODO: handle vertical splits */
4816 	room = -p_wh;
4817 	for (wp = firstwin; wp != NULL; wp = wp->w_next)
4818 	    room += wp->w_height - p_wmh;
4819 	if (room >= 0)
4820 	    break;
4821 	--p_wmh;
4822 	if (first)
4823 	{
4824 	    EMSG(_(e_noroom));
4825 	    first = FALSE;
4826 	}
4827     }
4828 }
4829 
4830 #ifdef FEAT_MOUSE
4831 
4832 /*
4833  * Status line of dragwin is dragged "offset" lines down (negative is up).
4834  */
4835     void
4836 win_drag_status_line(dragwin, offset)
4837     win_T	*dragwin;
4838     int		offset;
4839 {
4840     frame_T	*curfr;
4841     frame_T	*fr;
4842     int		room;
4843     int		row;
4844     int		up;	/* if TRUE, drag status line up, otherwise down */
4845     int		n;
4846 
4847     fr = dragwin->w_frame;
4848     curfr = fr;
4849     if (fr != topframe)		/* more than one window */
4850     {
4851 	fr = fr->fr_parent;
4852 	/* When the parent frame is not a column of frames, its parent should
4853 	 * be. */
4854 	if (fr->fr_layout != FR_COL)
4855 	{
4856 	    curfr = fr;
4857 	    if (fr != topframe)	/* only a row of windows, may drag statusline */
4858 		fr = fr->fr_parent;
4859 	}
4860     }
4861 
4862     /* If this is the last frame in a column, may want to resize the parent
4863      * frame instead (go two up to skip a row of frames). */
4864     while (curfr != topframe && curfr->fr_next == NULL)
4865     {
4866 	if (fr != topframe)
4867 	    fr = fr->fr_parent;
4868 	curfr = fr;
4869 	if (fr != topframe)
4870 	    fr = fr->fr_parent;
4871     }
4872 
4873     if (offset < 0) /* drag up */
4874     {
4875 	up = TRUE;
4876 	offset = -offset;
4877 	/* sum up the room of the current frame and above it */
4878 	if (fr == curfr)
4879 	{
4880 	    /* only one window */
4881 	    room = fr->fr_height - frame_minheight(fr, NULL);
4882 	}
4883 	else
4884 	{
4885 	    room = 0;
4886 	    for (fr = fr->fr_child; ; fr = fr->fr_next)
4887 	    {
4888 		room += fr->fr_height - frame_minheight(fr, NULL);
4889 		if (fr == curfr)
4890 		    break;
4891 	    }
4892 	}
4893 	fr = curfr->fr_next;		/* put fr at frame that grows */
4894     }
4895     else    /* drag down */
4896     {
4897 	up = FALSE;
4898 	/*
4899 	 * Only dragging the last status line can reduce p_ch.
4900 	 */
4901 	room = Rows - cmdline_row;
4902 	if (curfr->fr_next == NULL)
4903 	    room -= 1;
4904 	else
4905 	    room -= p_ch;
4906 	if (room < 0)
4907 	    room = 0;
4908 	/* sum up the room of frames below of the current one */
4909 	for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
4910 	    room += fr->fr_height - frame_minheight(fr, NULL);
4911 	fr = curfr;			/* put fr at window that grows */
4912     }
4913 
4914     if (room < offset)		/* Not enough room */
4915 	offset = room;		/* Move as far as we can */
4916     if (offset <= 0)
4917 	return;
4918 
4919     /*
4920      * Grow frame fr by "offset" lines.
4921      * Doesn't happen when dragging the last status line up.
4922      */
4923     if (fr != NULL)
4924 	frame_new_height(fr, fr->fr_height + offset, up, FALSE);
4925 
4926     if (up)
4927 	fr = curfr;		/* current frame gets smaller */
4928     else
4929 	fr = curfr->fr_next;	/* next frame gets smaller */
4930 
4931     /*
4932      * Now make the other frames smaller.
4933      */
4934     while (fr != NULL && offset > 0)
4935     {
4936 	n = frame_minheight(fr, NULL);
4937 	if (fr->fr_height - offset <= n)
4938 	{
4939 	    offset -= fr->fr_height - n;
4940 	    frame_new_height(fr, n, !up, FALSE);
4941 	}
4942 	else
4943 	{
4944 	    frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
4945 	    break;
4946 	}
4947 	if (up)
4948 	    fr = fr->fr_prev;
4949 	else
4950 	    fr = fr->fr_next;
4951     }
4952     row = win_comp_pos();
4953     screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4954     cmdline_row = row;
4955     p_ch = Rows - cmdline_row;
4956     if (p_ch < 1)
4957 	p_ch = 1;
4958     redraw_all_later(SOME_VALID);
4959     showmode();
4960 }
4961 
4962 #ifdef FEAT_VERTSPLIT
4963 /*
4964  * Separator line of dragwin is dragged "offset" lines right (negative is left).
4965  */
4966     void
4967 win_drag_vsep_line(dragwin, offset)
4968     win_T	*dragwin;
4969     int		offset;
4970 {
4971     frame_T	*curfr;
4972     frame_T	*fr;
4973     int		room;
4974     int		left;	/* if TRUE, drag separator line left, otherwise right */
4975     int		n;
4976 
4977     fr = dragwin->w_frame;
4978     if (fr == topframe)		/* only one window (cannot happe?) */
4979 	return;
4980     curfr = fr;
4981     fr = fr->fr_parent;
4982     /* When the parent frame is not a row of frames, its parent should be. */
4983     if (fr->fr_layout != FR_ROW)
4984     {
4985 	if (fr == topframe)	/* only a column of windows (cannot happen?) */
4986 	    return;
4987 	curfr = fr;
4988 	fr = fr->fr_parent;
4989     }
4990 
4991     /* If this is the last frame in a row, may want to resize a parent
4992      * frame instead. */
4993     while (curfr->fr_next == NULL)
4994     {
4995 	if (fr == topframe)
4996 	    break;
4997 	curfr = fr;
4998 	fr = fr->fr_parent;
4999 	if (fr != topframe)
5000 	{
5001 	    curfr = fr;
5002 	    fr = fr->fr_parent;
5003 	}
5004     }
5005 
5006     if (offset < 0) /* drag left */
5007     {
5008 	left = TRUE;
5009 	offset = -offset;
5010 	/* sum up the room of the current frame and left of it */
5011 	room = 0;
5012 	for (fr = fr->fr_child; ; fr = fr->fr_next)
5013 	{
5014 	    room += fr->fr_width - frame_minwidth(fr, NULL);
5015 	    if (fr == curfr)
5016 		break;
5017 	}
5018 	fr = curfr->fr_next;		/* put fr at frame that grows */
5019     }
5020     else    /* drag right */
5021     {
5022 	left = FALSE;
5023 	/* sum up the room of frames right of the current one */
5024 	room = 0;
5025 	for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5026 	    room += fr->fr_width - frame_minwidth(fr, NULL);
5027 	fr = curfr;			/* put fr at window that grows */
5028     }
5029 
5030     if (room < offset)		/* Not enough room */
5031 	offset = room;		/* Move as far as we can */
5032     if (offset <= 0)		/* No room at all, quit. */
5033 	return;
5034 
5035     /* grow frame fr by offset lines */
5036     frame_new_width(fr, fr->fr_width + offset, left, FALSE);
5037 
5038     /* shrink other frames: current and at the left or at the right */
5039     if (left)
5040 	fr = curfr;		/* current frame gets smaller */
5041     else
5042 	fr = curfr->fr_next;	/* next frame gets smaller */
5043 
5044     while (fr != NULL && offset > 0)
5045     {
5046 	n = frame_minwidth(fr, NULL);
5047 	if (fr->fr_width - offset <= n)
5048 	{
5049 	    offset -= fr->fr_width - n;
5050 	    frame_new_width(fr, n, !left, FALSE);
5051 	}
5052 	else
5053 	{
5054 	    frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
5055 	    break;
5056 	}
5057 	if (left)
5058 	    fr = fr->fr_prev;
5059 	else
5060 	    fr = fr->fr_next;
5061     }
5062     (void)win_comp_pos();
5063     redraw_all_later(NOT_VALID);
5064 }
5065 #endif /* FEAT_VERTSPLIT */
5066 #endif /* FEAT_MOUSE */
5067 
5068 #endif /* FEAT_WINDOWS */
5069 
5070 /*
5071  * Set the height of a window.
5072  * This takes care of the things inside the window, not what happens to the
5073  * window position, the frame or to other windows.
5074  */
5075     static void
5076 win_new_height(wp, height)
5077     win_T	*wp;
5078     int		height;
5079 {
5080     linenr_T	lnum;
5081     linenr_T	bot;
5082     int		sline, line_size;
5083     int		space;
5084     int		did_below = FALSE;
5085 #define FRACTION_MULT	16384L
5086 
5087     /* Don't want a negative height.  Happens when splitting a tiny window.
5088      * Will equalize heights soon to fix it. */
5089     if (height < 0)
5090 	height = 0;
5091 
5092     if (wp->w_wrow != wp->w_prev_fraction_row && wp->w_height > 0)
5093 	wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
5094 				    + FRACTION_MULT / 2) / (long)wp->w_height;
5095 
5096     wp->w_height = height;
5097     wp->w_skipcol = 0;
5098 
5099     /* Don't change w_topline when height is zero.  Don't set w_topline when
5100      * 'scrollbind' is set and this isn't the current window. */
5101     if (height > 0
5102 #ifdef FEAT_SCROLLBIND
5103 	    && (!wp->w_p_scb || wp == curwin)
5104 #endif
5105        )
5106     {
5107 	/*
5108 	 * Find a value for w_topline that shows the cursor at the same
5109 	 * relative position in the window as before (more or less).
5110 	 */
5111 	lnum = wp->w_cursor.lnum;
5112 	if (lnum < 1)		/* can happen when starting up */
5113 	    lnum = 1;
5114 	wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L) / FRACTION_MULT;
5115 	line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5116 	sline = wp->w_wrow - line_size;
5117 	if (sline < 0)
5118 	{
5119 	    /*
5120 	     * Cursor line would go off top of screen if w_wrow was this high.
5121 	     */
5122 	    wp->w_wrow = line_size;
5123 	}
5124 	else
5125 	{
5126 	    space = height;
5127 	    while (lnum > 1)
5128 	    {
5129 		space -= line_size;
5130 		if (space > 0 && sline <= 0 && !did_below)
5131 		{
5132 		    /* Try to use "~" lines below the text to avoid that text
5133 		     * is above the window while there are empty lines.
5134 		     * Subtract the rows below the cursor from "space" and
5135 		     * give the rest to "sline". */
5136 		    did_below = TRUE;
5137 		    bot = wp->w_cursor.lnum;
5138 		    while (space > 0)
5139 		    {
5140 			if (wp->w_buffer->b_ml.ml_line_count - bot >= space)
5141 			    space = 0;
5142 			else
5143 			{
5144 #ifdef FEAT_FOLDING
5145 			    hasFoldingWin(wp, bot, NULL, &bot, TRUE, NULL);
5146 #endif
5147 			    if (bot >= wp->w_buffer->b_ml.ml_line_count)
5148 				break;
5149 			    ++bot;
5150 			    space -= plines_win(wp, bot, TRUE);
5151 			}
5152 		    }
5153 		    if (bot == wp->w_buffer->b_ml.ml_line_count && space > 0)
5154 			sline += space;
5155 		}
5156 		if (sline <= 0)
5157 		    break;
5158 
5159 #ifdef FEAT_FOLDING
5160 		hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5161 		if (lnum == 1)
5162 		{
5163 		    /* first line in buffer is folded */
5164 		    line_size = 1;
5165 		    --sline;
5166 		    break;
5167 		}
5168 #endif
5169 		--lnum;
5170 #ifdef FEAT_DIFF
5171 		if (lnum == wp->w_topline)
5172 		    line_size = plines_win_nofill(wp, lnum, TRUE)
5173 							      + wp->w_topfill;
5174 		else
5175 #endif
5176 		    line_size = plines_win(wp, lnum, TRUE);
5177 		sline -= line_size;
5178 	    }
5179 
5180 	    if (sline < 0)
5181 	    {
5182 		/*
5183 		 * Line we want at top would go off top of screen.  Use next
5184 		 * line instead.
5185 		 */
5186 #ifdef FEAT_FOLDING
5187 		hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5188 #endif
5189 		lnum++;
5190 		wp->w_wrow -= line_size + sline;
5191 	    }
5192 	    else if (sline > 0)
5193 	    {
5194 		/* First line of file reached, use that as topline. */
5195 		lnum = 1;
5196 		wp->w_wrow -= sline;
5197 	    }
5198 	}
5199 	set_topline(wp, lnum);
5200     }
5201 
5202     if (wp == curwin)
5203     {
5204 	if (p_so)
5205 	    update_topline();
5206 	curs_columns(FALSE);	/* validate w_wrow */
5207     }
5208     wp->w_prev_fraction_row = wp->w_wrow;
5209 
5210     win_comp_scroll(wp);
5211     redraw_win_later(wp, SOME_VALID);
5212 #ifdef FEAT_WINDOWS
5213     wp->w_redr_status = TRUE;
5214 #endif
5215     invalidate_botline_win(wp);
5216 }
5217 
5218 #ifdef FEAT_VERTSPLIT
5219 /*
5220  * Set the width of a window.
5221  */
5222     static void
5223 win_new_width(wp, width)
5224     win_T	*wp;
5225     int		width;
5226 {
5227     wp->w_width = width;
5228     wp->w_lines_valid = 0;
5229     changed_line_abv_curs_win(wp);
5230     invalidate_botline_win(wp);
5231     if (wp == curwin)
5232     {
5233 	update_topline();
5234 	curs_columns(TRUE);	/* validate w_wrow */
5235     }
5236     redraw_win_later(wp, NOT_VALID);
5237     wp->w_redr_status = TRUE;
5238 }
5239 #endif
5240 
5241     void
5242 win_comp_scroll(wp)
5243     win_T	*wp;
5244 {
5245     wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5246     if (wp->w_p_scr == 0)
5247 	wp->w_p_scr = 1;
5248 }
5249 
5250 /*
5251  * command_height: called whenever p_ch has been changed
5252  */
5253     void
5254 command_height(old_p_ch)
5255     long	old_p_ch;
5256 {
5257 #ifdef FEAT_WINDOWS
5258     int		h;
5259     frame_T	*frp;
5260 
5261     /* When passed a negative value use the value of p_ch that we remembered.
5262      * This is needed for when the GUI starts up, we can't be sure in what
5263      * order things happen. */
5264     if (old_p_ch < 0)
5265 	old_p_ch = p_ch_used;
5266     p_ch_used = p_ch;
5267 
5268     /* Find bottom frame with width of screen. */
5269     frp = lastwin->w_frame;
5270 # ifdef FEAT_VERTSPLIT
5271     while (frp->fr_width != Columns && frp->fr_parent != NULL)
5272 	frp = frp->fr_parent;
5273 # endif
5274 
5275     /* Avoid changing the height of a window with 'winfixheight' set. */
5276     while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5277 						      && frp->fr_win->w_p_wfh)
5278 	frp = frp->fr_prev;
5279 
5280     if (starting != NO_SCREEN)
5281     {
5282 	cmdline_row = Rows - p_ch;
5283 
5284 	if (p_ch > old_p_ch)		    /* p_ch got bigger */
5285 	{
5286 	    while (p_ch > old_p_ch)
5287 	    {
5288 		if (frp == NULL)
5289 		{
5290 		    EMSG(_(e_noroom));
5291 		    p_ch = old_p_ch;
5292 		    cmdline_row = Rows - p_ch;
5293 		    break;
5294 		}
5295 		h = frp->fr_height - frame_minheight(frp, NULL);
5296 		if (h > p_ch - old_p_ch)
5297 		    h = p_ch - old_p_ch;
5298 		old_p_ch += h;
5299 		frame_add_height(frp, -h);
5300 		frp = frp->fr_prev;
5301 	    }
5302 
5303 	    /* Recompute window positions. */
5304 	    (void)win_comp_pos();
5305 
5306 	    /* clear the lines added to cmdline */
5307 	    if (full_screen)
5308 		screen_fill((int)(cmdline_row), (int)Rows, 0,
5309 						   (int)Columns, ' ', ' ', 0);
5310 	    msg_row = cmdline_row;
5311 	    redraw_cmdline = TRUE;
5312 	    return;
5313 	}
5314 
5315 	if (msg_row < cmdline_row)
5316 	    msg_row = cmdline_row;
5317 	redraw_cmdline = TRUE;
5318     }
5319     frame_add_height(frp, (int)(old_p_ch - p_ch));
5320 
5321     /* Recompute window positions. */
5322     if (frp != lastwin->w_frame)
5323 	(void)win_comp_pos();
5324 #else
5325     win_setheight((int)(firstwin->w_height + old_p_ch - p_ch));
5326     cmdline_row = Rows - p_ch;
5327 #endif
5328 }
5329 
5330 #if defined(FEAT_WINDOWS) || defined(PROTO)
5331 /*
5332  * Resize frame "frp" to be "n" lines higher (negative for less high).
5333  * Also resize the frames it is contained in.
5334  */
5335     static void
5336 frame_add_height(frp, n)
5337     frame_T	*frp;
5338     int		n;
5339 {
5340     frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5341     for (;;)
5342     {
5343 	frp = frp->fr_parent;
5344 	if (frp == NULL)
5345 	    break;
5346 	frp->fr_height += n;
5347     }
5348 }
5349 
5350 /*
5351  * Add or remove a status line for the bottom window(s), according to the
5352  * value of 'laststatus'.
5353  */
5354     void
5355 last_status(morewin)
5356     int		morewin;	/* pretend there are two or more windows */
5357 {
5358     /* Don't make a difference between horizontal or vertical split. */
5359     last_status_rec(topframe, (p_ls == 2
5360 			  || (p_ls == 1 && (morewin || lastwin != firstwin))));
5361 }
5362 
5363     static void
5364 last_status_rec(fr, statusline)
5365     frame_T	*fr;
5366     int		statusline;
5367 {
5368     frame_T	*fp;
5369     win_T	*wp;
5370 
5371     if (fr->fr_layout == FR_LEAF)
5372     {
5373 	wp = fr->fr_win;
5374 	if (wp->w_status_height != 0 && !statusline)
5375 	{
5376 	    /* remove status line */
5377 	    win_new_height(wp, wp->w_height + 1);
5378 	    wp->w_status_height = 0;
5379 	    comp_col();
5380 	}
5381 	else if (wp->w_status_height == 0 && statusline)
5382 	{
5383 	    /* Find a frame to take a line from. */
5384 	    fp = fr;
5385 	    while (fp->fr_height <= frame_minheight(fp, NULL))
5386 	    {
5387 		if (fp == topframe)
5388 		{
5389 		    EMSG(_(e_noroom));
5390 		    return;
5391 		}
5392 		/* In a column of frames: go to frame above.  If already at
5393 		 * the top or in a row of frames: go to parent. */
5394 		if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5395 		    fp = fp->fr_prev;
5396 		else
5397 		    fp = fp->fr_parent;
5398 	    }
5399 	    wp->w_status_height = 1;
5400 	    if (fp != fr)
5401 	    {
5402 		frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5403 		frame_fix_height(wp);
5404 		(void)win_comp_pos();
5405 	    }
5406 	    else
5407 		win_new_height(wp, wp->w_height - 1);
5408 	    comp_col();
5409 	    redraw_all_later(SOME_VALID);
5410 	}
5411     }
5412 #ifdef FEAT_VERTSPLIT
5413     else if (fr->fr_layout == FR_ROW)
5414     {
5415 	/* vertically split windows, set status line for each one */
5416 	for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5417 	    last_status_rec(fp, statusline);
5418     }
5419 #endif
5420     else
5421     {
5422 	/* horizontally split window, set status line for last one */
5423 	for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5424 	    ;
5425 	last_status_rec(fp, statusline);
5426     }
5427 }
5428 
5429 /*
5430  * Return the number of lines used by the tab page line.
5431  */
5432     int
5433 tabline_height()
5434 {
5435 #ifdef FEAT_GUI_TABLINE
5436     /* When the GUI has the tabline then this always returns zero. */
5437     if (gui_use_tabline())
5438 	return 0;
5439 #endif
5440     switch (p_stal)
5441     {
5442 	case 0: return 0;
5443 	case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5444     }
5445     return 1;
5446 }
5447 
5448 #endif /* FEAT_WINDOWS */
5449 
5450 #if defined(FEAT_SEARCHPATH) || defined(PROTO)
5451 /*
5452  * Get the file name at the cursor.
5453  * If Visual mode is active, use the selected text if it's in one line.
5454  * Returns the name in allocated memory, NULL for failure.
5455  */
5456     char_u *
5457 grab_file_name(count, file_lnum)
5458     long	count;
5459     linenr_T	*file_lnum;
5460 {
5461 # ifdef FEAT_VISUAL
5462     if (VIsual_active)
5463     {
5464 	int	len;
5465 	char_u	*ptr;
5466 
5467 	if (get_visual_text(NULL, &ptr, &len) == FAIL)
5468 	    return NULL;
5469 	return find_file_name_in_path(ptr, len,
5470 		     FNAME_MESS|FNAME_EXP|FNAME_REL, count, curbuf->b_ffname);
5471     }
5472 # endif
5473     return file_name_at_cursor(FNAME_MESS|FNAME_HYP|FNAME_EXP|FNAME_REL, count,
5474 			       file_lnum);
5475 
5476 }
5477 
5478 /*
5479  * Return the file name under or after the cursor.
5480  *
5481  * The 'path' option is searched if the file name is not absolute.
5482  * The string returned has been alloc'ed and should be freed by the caller.
5483  * NULL is returned if the file name or file is not found.
5484  *
5485  * options:
5486  * FNAME_MESS	    give error messages
5487  * FNAME_EXP	    expand to path
5488  * FNAME_HYP	    check for hypertext link
5489  * FNAME_INCL	    apply "includeexpr"
5490  */
5491     char_u *
5492 file_name_at_cursor(options, count, file_lnum)
5493     int		options;
5494     long	count;
5495     linenr_T	*file_lnum;
5496 {
5497     return file_name_in_line(ml_get_curline(),
5498 		      curwin->w_cursor.col, options, count, curbuf->b_ffname,
5499 		      file_lnum);
5500 }
5501 
5502 /*
5503  * Return the name of the file under or after ptr[col].
5504  * Otherwise like file_name_at_cursor().
5505  */
5506     char_u *
5507 file_name_in_line(line, col, options, count, rel_fname, file_lnum)
5508     char_u	*line;
5509     int		col;
5510     int		options;
5511     long	count;
5512     char_u	*rel_fname;	/* file we are searching relative to */
5513     linenr_T	*file_lnum;	/* line number after the file name */
5514 {
5515     char_u	*ptr;
5516     int		len;
5517 
5518     /*
5519      * search forward for what could be the start of a file name
5520      */
5521     ptr = line + col;
5522     while (*ptr != NUL && !vim_isfilec(*ptr))
5523 	mb_ptr_adv(ptr);
5524     if (*ptr == NUL)		/* nothing found */
5525     {
5526 	if (options & FNAME_MESS)
5527 	    EMSG(_("E446: No file name under cursor"));
5528 	return NULL;
5529     }
5530 
5531     /*
5532      * Search backward for first char of the file name.
5533      * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
5534      */
5535     while (ptr > line)
5536     {
5537 #ifdef FEAT_MBYTE
5538 	if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
5539 	    ptr -= len + 1;
5540 	else
5541 #endif
5542 	if (vim_isfilec(ptr[-1])
5543 		|| ((options & FNAME_HYP) && path_is_url(ptr - 1)))
5544 	    --ptr;
5545 	else
5546 	    break;
5547     }
5548 
5549     /*
5550      * Search forward for the last char of the file name.
5551      * Also allow "://" when ':' is not in 'isfname'.
5552      */
5553     len = 0;
5554     while (vim_isfilec(ptr[len])
5555 			 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
5556 #ifdef FEAT_MBYTE
5557 	if (has_mbyte)
5558 	    len += (*mb_ptr2len)(ptr + len);
5559 	else
5560 #endif
5561 	    ++len;
5562 
5563     /*
5564      * If there is trailing punctuation, remove it.
5565      * But don't remove "..", could be a directory name.
5566      */
5567     if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
5568 						       && ptr[len - 2] != '.')
5569 	--len;
5570 
5571     if (file_lnum != NULL)
5572     {
5573 	char_u *p;
5574 
5575 	/* Get the number after the file name and a separator character */
5576 	p = ptr + len;
5577 	p = skipwhite(p);
5578 	if (*p != NUL)
5579 	{
5580 	    if (!isdigit(*p))
5581 		++p;		    /* skip the separator */
5582 	    p = skipwhite(p);
5583 	    if (isdigit(*p))
5584 		*file_lnum = (int)getdigits(&p);
5585 	}
5586     }
5587 
5588     return find_file_name_in_path(ptr, len, options, count, rel_fname);
5589 }
5590 
5591 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5592 static char_u *eval_includeexpr __ARGS((char_u *ptr, int len));
5593 
5594     static char_u *
5595 eval_includeexpr(ptr, len)
5596     char_u	*ptr;
5597     int		len;
5598 {
5599     char_u	*res;
5600 
5601     set_vim_var_string(VV_FNAME, ptr, len);
5602     res = eval_to_string_safe(curbuf->b_p_inex, NULL,
5603 		      was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
5604     set_vim_var_string(VV_FNAME, NULL, 0);
5605     return res;
5606 }
5607 #endif
5608 
5609 /*
5610  * Return the name of the file ptr[len] in 'path'.
5611  * Otherwise like file_name_at_cursor().
5612  */
5613     char_u *
5614 find_file_name_in_path(ptr, len, options, count, rel_fname)
5615     char_u	*ptr;
5616     int		len;
5617     int		options;
5618     long	count;
5619     char_u	*rel_fname;	/* file we are searching relative to */
5620 {
5621     char_u	*file_name;
5622     int		c;
5623 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5624     char_u	*tofree = NULL;
5625 
5626     if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5627     {
5628 	tofree = eval_includeexpr(ptr, len);
5629 	if (tofree != NULL)
5630 	{
5631 	    ptr = tofree;
5632 	    len = (int)STRLEN(ptr);
5633 	}
5634     }
5635 # endif
5636 
5637     if (options & FNAME_EXP)
5638     {
5639 	file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5640 							     TRUE, rel_fname);
5641 
5642 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5643 	/*
5644 	 * If the file could not be found in a normal way, try applying
5645 	 * 'includeexpr' (unless done already).
5646 	 */
5647 	if (file_name == NULL
5648 		&& !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
5649 	{
5650 	    tofree = eval_includeexpr(ptr, len);
5651 	    if (tofree != NULL)
5652 	    {
5653 		ptr = tofree;
5654 		len = (int)STRLEN(ptr);
5655 		file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
5656 							     TRUE, rel_fname);
5657 	    }
5658 	}
5659 # endif
5660 	if (file_name == NULL && (options & FNAME_MESS))
5661 	{
5662 	    c = ptr[len];
5663 	    ptr[len] = NUL;
5664 	    EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
5665 	    ptr[len] = c;
5666 	}
5667 
5668 	/* Repeat finding the file "count" times.  This matters when it
5669 	 * appears several times in the path. */
5670 	while (file_name != NULL && --count > 0)
5671 	{
5672 	    vim_free(file_name);
5673 	    file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
5674 	}
5675     }
5676     else
5677 	file_name = vim_strnsave(ptr, len);
5678 
5679 # if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
5680     vim_free(tofree);
5681 # endif
5682 
5683     return file_name;
5684 }
5685 #endif /* FEAT_SEARCHPATH */
5686 
5687 /*
5688  * Check if the "://" of a URL is at the pointer, return URL_SLASH.
5689  * Also check for ":\\", which MS Internet Explorer accepts, return
5690  * URL_BACKSLASH.
5691  */
5692     static int
5693 path_is_url(p)
5694     char_u  *p;
5695 {
5696     if (STRNCMP(p, "://", (size_t)3) == 0)
5697 	return URL_SLASH;
5698     else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
5699 	return URL_BACKSLASH;
5700     return 0;
5701 }
5702 
5703 /*
5704  * Check if "fname" starts with "name://".  Return URL_SLASH if it does.
5705  * Return URL_BACKSLASH for "name:\\".
5706  * Return zero otherwise.
5707  */
5708     int
5709 path_with_url(fname)
5710     char_u *fname;
5711 {
5712     char_u *p;
5713 
5714     for (p = fname; isalpha(*p); ++p)
5715 	;
5716     return path_is_url(p);
5717 }
5718 
5719 /*
5720  * Return TRUE if "name" is a full (absolute) path name or URL.
5721  */
5722     int
5723 vim_isAbsName(name)
5724     char_u	*name;
5725 {
5726     return (path_with_url(name) != 0 || mch_isFullName(name));
5727 }
5728 
5729 /*
5730  * Get absolute file name into buffer "buf[len]".
5731  *
5732  * return FAIL for failure, OK otherwise
5733  */
5734     int
5735 vim_FullName(fname, buf, len, force)
5736     char_u	*fname, *buf;
5737     int		len;
5738     int		force;	    /* force expansion even when already absolute */
5739 {
5740     int		retval = OK;
5741     int		url;
5742 
5743     *buf = NUL;
5744     if (fname == NULL)
5745 	return FAIL;
5746 
5747     url = path_with_url(fname);
5748     if (!url)
5749 	retval = mch_FullName(fname, buf, len, force);
5750     if (url || retval == FAIL)
5751     {
5752 	/* something failed; use the file name (truncate when too long) */
5753 	vim_strncpy(buf, fname, len - 1);
5754     }
5755 #if defined(MACOS_CLASSIC) || defined(OS2) || defined(MSDOS) || defined(MSWIN)
5756     slash_adjust(buf);
5757 #endif
5758     return retval;
5759 }
5760 
5761 /*
5762  * Return the minimal number of rows that is needed on the screen to display
5763  * the current number of windows.
5764  */
5765     int
5766 min_rows()
5767 {
5768     int		total;
5769 #ifdef FEAT_WINDOWS
5770     tabpage_T	*tp;
5771     int		n;
5772 #endif
5773 
5774     if (firstwin == NULL)	/* not initialized yet */
5775 	return MIN_LINES;
5776 
5777 #ifdef FEAT_WINDOWS
5778     total = 0;
5779     for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
5780     {
5781 	n = frame_minheight(tp->tp_topframe, NULL);
5782 	if (total < n)
5783 	    total = n;
5784     }
5785     total += tabline_height();
5786 #else
5787     total = 1;		/* at least one window should have a line! */
5788 #endif
5789     total += 1;		/* count the room for the command line */
5790     return total;
5791 }
5792 
5793 /*
5794  * Return TRUE if there is only one window (in the current tab page), not
5795  * counting a help or preview window, unless it is the current window.
5796  */
5797     int
5798 only_one_window()
5799 {
5800 #ifdef FEAT_WINDOWS
5801     int		count = 0;
5802     win_T	*wp;
5803 
5804     /* If there is another tab page there always is another window. */
5805     if (first_tabpage->tp_next != NULL)
5806 	return FALSE;
5807 
5808     for (wp = firstwin; wp != NULL; wp = wp->w_next)
5809 	if (!((wp->w_buffer->b_help && !curbuf->b_help)
5810 # ifdef FEAT_QUICKFIX
5811 		    || wp->w_p_pvw
5812 # endif
5813 	     ) || wp == curwin)
5814 	    ++count;
5815     return (count <= 1);
5816 #else
5817     return TRUE;
5818 #endif
5819 }
5820 
5821 #if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
5822 /*
5823  * Correct the cursor line number in other windows.  Used after changing the
5824  * current buffer, and before applying autocommands.
5825  * When "do_curwin" is TRUE, also check current window.
5826  */
5827     void
5828 check_lnums(do_curwin)
5829     int		do_curwin;
5830 {
5831     win_T	*wp;
5832 
5833 #ifdef FEAT_WINDOWS
5834     tabpage_T	*tp;
5835 
5836     FOR_ALL_TAB_WINDOWS(tp, wp)
5837 	if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
5838 #else
5839     wp = curwin;
5840     if (do_curwin)
5841 #endif
5842 	{
5843 	    if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
5844 		wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5845 	    if (wp->w_topline > curbuf->b_ml.ml_line_count)
5846 		wp->w_topline = curbuf->b_ml.ml_line_count;
5847 	}
5848 }
5849 #endif
5850 
5851 #if defined(FEAT_WINDOWS) || defined(PROTO)
5852 
5853 /*
5854  * A snapshot of the window sizes, to restore them after closing the help
5855  * window.
5856  * Only these fields are used:
5857  * fr_layout
5858  * fr_width
5859  * fr_height
5860  * fr_next
5861  * fr_child
5862  * fr_win (only valid for the old curwin, NULL otherwise)
5863  */
5864 
5865 /*
5866  * Create a snapshot of the current frame sizes.
5867  */
5868     static void
5869 make_snapshot()
5870 {
5871     clear_snapshot(curtab);
5872     make_snapshot_rec(topframe, &curtab->tp_snapshot);
5873 }
5874 
5875     static void
5876 make_snapshot_rec(fr, frp)
5877     frame_T	*fr;
5878     frame_T	**frp;
5879 {
5880     *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
5881     if (*frp == NULL)
5882 	return;
5883     (*frp)->fr_layout = fr->fr_layout;
5884 # ifdef FEAT_VERTSPLIT
5885     (*frp)->fr_width = fr->fr_width;
5886 # endif
5887     (*frp)->fr_height = fr->fr_height;
5888     if (fr->fr_next != NULL)
5889 	make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
5890     if (fr->fr_child != NULL)
5891 	make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
5892     if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
5893 	(*frp)->fr_win = curwin;
5894 }
5895 
5896 /*
5897  * Remove any existing snapshot.
5898  */
5899     static void
5900 clear_snapshot(tp)
5901     tabpage_T	*tp;
5902 {
5903     clear_snapshot_rec(tp->tp_snapshot);
5904     tp->tp_snapshot = NULL;
5905 }
5906 
5907     static void
5908 clear_snapshot_rec(fr)
5909     frame_T	*fr;
5910 {
5911     if (fr != NULL)
5912     {
5913 	clear_snapshot_rec(fr->fr_next);
5914 	clear_snapshot_rec(fr->fr_child);
5915 	vim_free(fr);
5916     }
5917 }
5918 
5919 /*
5920  * Restore a previously created snapshot, if there is any.
5921  * This is only done if the screen size didn't change and the window layout is
5922  * still the same.
5923  */
5924     static void
5925 restore_snapshot(close_curwin)
5926     int		close_curwin;	    /* closing current window */
5927 {
5928     win_T	*wp;
5929 
5930     if (curtab->tp_snapshot != NULL
5931 # ifdef FEAT_VERTSPLIT
5932 	    && curtab->tp_snapshot->fr_width == topframe->fr_width
5933 # endif
5934 	    && curtab->tp_snapshot->fr_height == topframe->fr_height
5935 	    && check_snapshot_rec(curtab->tp_snapshot, topframe) == OK)
5936     {
5937 	wp = restore_snapshot_rec(curtab->tp_snapshot, topframe);
5938 	win_comp_pos();
5939 	if (wp != NULL && close_curwin)
5940 	    win_goto(wp);
5941 	redraw_all_later(CLEAR);
5942     }
5943     clear_snapshot(curtab);
5944 }
5945 
5946 /*
5947  * Check if frames "sn" and "fr" have the same layout, same following frames
5948  * and same children.
5949  */
5950     static int
5951 check_snapshot_rec(sn, fr)
5952     frame_T	*sn;
5953     frame_T	*fr;
5954 {
5955     if (sn->fr_layout != fr->fr_layout
5956 	    || (sn->fr_next == NULL) != (fr->fr_next == NULL)
5957 	    || (sn->fr_child == NULL) != (fr->fr_child == NULL)
5958 	    || (sn->fr_next != NULL
5959 		&& check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
5960 	    || (sn->fr_child != NULL
5961 		&& check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
5962 	return FAIL;
5963     return OK;
5964 }
5965 
5966 /*
5967  * Copy the size of snapshot frame "sn" to frame "fr".  Do the same for all
5968  * following frames and children.
5969  * Returns a pointer to the old current window, or NULL.
5970  */
5971     static win_T *
5972 restore_snapshot_rec(sn, fr)
5973     frame_T	*sn;
5974     frame_T	*fr;
5975 {
5976     win_T	*wp = NULL;
5977     win_T	*wp2;
5978 
5979     fr->fr_height = sn->fr_height;
5980 # ifdef FEAT_VERTSPLIT
5981     fr->fr_width = sn->fr_width;
5982 # endif
5983     if (fr->fr_layout == FR_LEAF)
5984     {
5985 	frame_new_height(fr, fr->fr_height, FALSE, FALSE);
5986 # ifdef FEAT_VERTSPLIT
5987 	frame_new_width(fr, fr->fr_width, FALSE, FALSE);
5988 # endif
5989 	wp = sn->fr_win;
5990     }
5991     if (sn->fr_next != NULL)
5992     {
5993 	wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
5994 	if (wp2 != NULL)
5995 	    wp = wp2;
5996     }
5997     if (sn->fr_child != NULL)
5998     {
5999 	wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6000 	if (wp2 != NULL)
6001 	    wp = wp2;
6002     }
6003     return wp;
6004 }
6005 
6006 #endif
6007 
6008 #if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6009 /*
6010  * Return TRUE if there is any vertically split window.
6011  */
6012     int
6013 win_hasvertsplit()
6014 {
6015     frame_T	*fr;
6016 
6017     if (topframe->fr_layout == FR_ROW)
6018 	return TRUE;
6019 
6020     if (topframe->fr_layout == FR_COL)
6021 	for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6022 	    if (fr->fr_layout == FR_ROW)
6023 		return TRUE;
6024 
6025     return FALSE;
6026 }
6027 #endif
6028