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