blob: 40a4607d0f3c52fe9ca3d252535fbaa8d06eef84 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010012static int path_is_url(char_u *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010013static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
14static void win_init(win_T *newp, win_T *oldp, int flags);
15static void win_init_some(win_T *newp, win_T *oldp);
16static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
17static void frame_setheight(frame_T *curfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static void win_exchange(long);
20static void win_rotate(int, int);
21static void win_totop(int size, int flags);
22static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
23static int last_window(void);
24static int close_last_window_tabpage(win_T *win, int free_buf, tabpage_T *prev_curtab);
25static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
26static frame_T *win_altframe(win_T *win, tabpage_T *tp);
27static tabpage_T *alt_tabpage(void);
28static win_T *frame2win(frame_T *frp);
29static int frame_has_win(frame_T *frp, win_T *wp);
30static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
31static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010032static int frame_fixed_width(frame_T *frp);
33static void frame_add_statusline(frame_T *frp);
34static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
35static void frame_add_vsep(frame_T *frp);
36static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
37static void frame_fix_width(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static int win_alloc_firstwin(win_T *oldwin);
39static void new_frame(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010040static tabpage_T *alloc_tabpage(void);
41static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
42static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
43static void frame_fix_height(win_T *wp);
44static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
Bram Moolenaarc917da42016-07-19 22:31:36 +020045static 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);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010046static void win_free(win_T *wp, tabpage_T *tp);
47static void frame_append(frame_T *after, frame_T *frp);
48static void frame_insert(frame_T *before, frame_T *frp);
49static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void win_goto_ver(int up, long count);
51static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void frame_add_height(frame_T *frp, int n);
53static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void make_snapshot_rec(frame_T *fr, frame_T **frp);
56static void clear_snapshot(tabpage_T *tp, int idx);
57static void clear_snapshot_rec(frame_T *fr);
58static int check_snapshot_rec(frame_T *sn, frame_T *fr);
59static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020063
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66#define URL_SLASH 1 /* path_is_url() has found "://" */
67#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
68
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000069#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaar4033c552017-09-16 20:54:51 +020071#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000072
73static char *m_onlyone = N_("Already only one window");
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075/*
76 * all CTRL-W window commands are handled here, called from normal_cmd().
77 */
78 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010079do_window(
80 int nchar,
81 long Prenum,
82 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 long Prenum1;
85 win_T *wp;
86#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
87 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000088 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
90#ifdef FEAT_FIND_ID
91 int type = FIND_DEFINE;
92 int len;
93#endif
94 char_u cbuf[40];
95
96 if (Prenum == 0)
97 Prenum1 = 1;
98 else
99 Prenum1 = Prenum;
100
101#ifdef FEAT_CMDWIN
102# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
103#else
104# define CHECK_CMDWIN
105#endif
106
107 switch (nchar)
108 {
109/* split current window in two parts, horizontally */
110 case 'S':
111 case Ctrl_S:
112 case 's':
113 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000115#ifdef FEAT_QUICKFIX
116 /* When splitting the quickfix window open a new buffer in it,
117 * don't replicate the quickfix buffer. */
118 if (bt_quickfix(curbuf))
119 goto newwindow;
120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_GUI
122 need_mouse_correct = TRUE;
123#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200124 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 break;
126
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127/* split current window in two parts, vertically */
128 case Ctrl_V:
129 case 'v':
130 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100132#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100137#endif
138#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100140#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200141 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144/* split current window and edit alternate file */
145 case Ctrl_HAT:
146 case '^':
147 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100149 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 do_cmdline_cmd(cbuf);
151 break;
152
153/* open new window */
154 case Ctrl_N:
155 case 'n':
156 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000158#ifdef FEAT_QUICKFIX
159newwindow:
160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000162 /* window height */
163 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 else
165 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100166#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000167 if (nchar == 'v' || nchar == Ctrl_V)
168 STRCAT(cbuf, "v");
169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 STRCAT(cbuf, "new");
171 do_cmdline_cmd(cbuf);
172 break;
173
174/* quit current window */
175 case Ctrl_Q:
176 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100178 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100179 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 break;
181
182/* close current window */
183 case Ctrl_C:
184 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100186 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100187 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 break;
189
Bram Moolenaar4033c552017-09-16 20:54:51 +0200190#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/* close preview window */
192 case Ctrl_Z:
193 case 'z':
194 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 do_cmdline_cmd((char_u *)"pclose");
197 break;
198
199/* cursor to preview window */
200 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200201 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 if (wp->w_p_pvw)
203 break;
204 if (wp == NULL)
205 EMSG(_("E441: There is no preview window"));
206 else
207 win_goto(wp);
208 break;
209#endif
210
211/* close all but current window */
212 case Ctrl_O:
213 case 'o':
214 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100216 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100217 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 break;
219
220/* cursor to next window with wrap around */
221 case Ctrl_W:
222 case 'w':
223/* cursor to previous window with wrap around */
224 case 'W':
225 CHECK_CMDWIN
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100226 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 beep_flush();
228 else
229 {
230 if (Prenum) /* go to specified window */
231 {
232 for (wp = firstwin; --Prenum > 0; )
233 {
234 if (wp->w_next == NULL)
235 break;
236 else
237 wp = wp->w_next;
238 }
239 }
240 else
241 {
242 if (nchar == 'W') /* go to previous window */
243 {
244 wp = curwin->w_prev;
245 if (wp == NULL)
246 wp = lastwin; /* wrap around */
247 }
248 else /* go to next window */
249 {
250 wp = curwin->w_next;
251 if (wp == NULL)
252 wp = firstwin; /* wrap around */
253 }
254 }
255 win_goto(wp);
256 }
257 break;
258
259/* cursor to window below */
260 case 'j':
261 case K_DOWN:
262 case Ctrl_J:
263 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 break;
266
267/* cursor to window above */
268 case 'k':
269 case K_UP:
270 case Ctrl_K:
271 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 break;
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275/* cursor to left window */
276 case 'h':
277 case K_LEFT:
278 case Ctrl_H:
279 case K_BS:
280 CHECK_CMDWIN
281 win_goto_hor(TRUE, Prenum1);
282 break;
283
284/* cursor to right window */
285 case 'l':
286 case K_RIGHT:
287 case Ctrl_L:
288 CHECK_CMDWIN
289 win_goto_hor(FALSE, Prenum1);
290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000292/* move window to new tab page */
293 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000294 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000295 MSG(_(m_onlyone));
296 else
297 {
298 tabpage_T *oldtab = curtab;
299 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000300
301 /* First create a new tab with the window, then go back to
302 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000303 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000304 if (win_new_tabpage((int)Prenum) == OK
305 && valid_tabpage(oldtab))
306 {
307 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200308 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000309 if (curwin == wp)
310 win_close(curwin, FALSE);
311 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200312 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000313 }
314 }
315 break;
316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317/* cursor to top-left window */
318 case 't':
319 case Ctrl_T:
320 win_goto(firstwin);
321 break;
322
323/* cursor to bottom-right window */
324 case 'b':
325 case Ctrl_B:
326 win_goto(lastwin);
327 break;
328
329/* cursor to last accessed (previous) window */
330 case 'p':
331 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200332 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 beep_flush();
334 else
335 win_goto(prevwin);
336 break;
337
338/* exchange current and next window */
339 case 'x':
340 case Ctrl_X:
341 CHECK_CMDWIN
342 win_exchange(Prenum);
343 break;
344
345/* rotate windows downwards */
346 case Ctrl_R:
347 case 'r':
348 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 win_rotate(FALSE, (int)Prenum1); /* downwards */
351 break;
352
353/* rotate windows upwards */
354 case 'R':
355 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 win_rotate(TRUE, (int)Prenum1); /* upwards */
358 break;
359
360/* move window to the very top/bottom/left/right */
361 case 'K':
362 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 case 'H':
364 case 'L':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 CHECK_CMDWIN
366 win_totop((int)Prenum,
367 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
368 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
369 break;
370
371/* make all windows the same height */
372 case '=':
373#ifdef FEAT_GUI
374 need_mouse_correct = TRUE;
375#endif
376 win_equal(NULL, FALSE, 'b');
377 break;
378
379/* increase current window height */
380 case '+':
381#ifdef FEAT_GUI
382 need_mouse_correct = TRUE;
383#endif
384 win_setheight(curwin->w_height + (int)Prenum1);
385 break;
386
387/* decrease current window height */
388 case '-':
389#ifdef FEAT_GUI
390 need_mouse_correct = TRUE;
391#endif
392 win_setheight(curwin->w_height - (int)Prenum1);
393 break;
394
395/* set current window height */
396 case Ctrl__:
397 case '_':
398#ifdef FEAT_GUI
399 need_mouse_correct = TRUE;
400#endif
401 win_setheight(Prenum ? (int)Prenum : 9999);
402 break;
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/* increase current window width */
405 case '>':
406#ifdef FEAT_GUI
407 need_mouse_correct = TRUE;
408#endif
409 win_setwidth(curwin->w_width + (int)Prenum1);
410 break;
411
412/* decrease current window width */
413 case '<':
414#ifdef FEAT_GUI
415 need_mouse_correct = TRUE;
416#endif
417 win_setwidth(curwin->w_width - (int)Prenum1);
418 break;
419
420/* set current window width */
421 case '|':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427
428/* jump to tag and split window if tag exists (in preview window) */
429#if defined(FEAT_QUICKFIX)
430 case '}':
431 CHECK_CMDWIN
432 if (Prenum)
433 g_do_tagpreview = Prenum;
434 else
435 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200437 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 case ']':
439 case Ctrl_RSB:
440 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200441 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (Prenum)
443 postponed_split = Prenum;
444 else
445 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200446#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200447 if (nchar != '}')
448 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaard355c502014-09-23 13:48:43 +0200451 /* Execute the command right here, required when "wincmd ]"
452 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 do_nv_ident(Ctrl_RSB, NUL);
454 break;
455
456#ifdef FEAT_SEARCHPATH
457/* edit file name under cursor in a new window */
458 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000459 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000461wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000464 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (ptr != NULL)
466 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200467 tabpage_T *oldtab = curtab;
468 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000469# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 setpcmark();
473 if (win_split(0, 0) == OK)
474 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200475 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200476 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
477 ECMD_HIDE, NULL) == FAIL)
478 {
479 /* Failed to open the file, close the window
480 * opened for it. */
481 win_close(curwin, FALSE);
482 goto_tabpage_win(oldtab, oldwin);
483 }
484 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000485 {
486 curwin->w_cursor.lnum = lnum;
487 check_cursor_lnum();
488 beginline(BL_SOL | BL_FIX);
489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 }
491 vim_free(ptr);
492 }
493 break;
494#endif
495
496#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000497/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 * new window -- webb
499 */
500 case 'i': /* Go to any match */
501 case Ctrl_I:
502 type = FIND_ANY;
503 /* FALLTHROUGH */
504 case 'd': /* Go to definition, using 'define' */
505 case Ctrl_D:
506 CHECK_CMDWIN
507 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
508 break;
509 find_pattern_in_path(ptr, 0, len, TRUE,
510 Prenum == 0 ? TRUE : FALSE, type,
511 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
512 curwin->w_set_curswant = TRUE;
513 break;
514#endif
515
Bram Moolenaar05159a02005-02-26 23:04:13 +0000516 case K_KENTER:
517 case CAR:
518#if defined(FEAT_QUICKFIX)
519 /*
520 * In a quickfix window a <CR> jumps to the error under the
521 * cursor in a new window.
522 */
523 if (bt_quickfix(curbuf))
524 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000525 sprintf((char *)cbuf, "split +%ld%s",
526 (long)curwin->w_cursor.lnum,
527 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000528 do_cmdline_cmd(cbuf);
529 }
530#endif
531 break;
532
533
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534/* CTRL-W g extended commands */
535 case 'g':
536 case Ctrl_G:
537 CHECK_CMDWIN
538#ifdef USE_ON_FLY_SCROLL
539 dont_scroll = TRUE; /* disallow scrolling here */
540#endif
541 ++no_mapping;
542 ++allow_keys; /* no mapping for xchar, but allow key codes */
543 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000544 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 --no_mapping;
547 --allow_keys;
548#ifdef FEAT_CMDL_INFO
549 (void)add_to_showcmd(xchar);
550#endif
551 switch (xchar)
552 {
553#if defined(FEAT_QUICKFIX)
554 case '}':
555 xchar = Ctrl_RSB;
556 if (Prenum)
557 g_do_tagpreview = Prenum;
558 else
559 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200561 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 case ']':
563 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200564 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (Prenum)
566 postponed_split = Prenum;
567 else
568 postponed_split = -1;
569
570 /* Execute the command right here, required when
571 * "wincmd g}" was used in a function. */
572 do_nv_ident('g', xchar);
573 break;
574
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000575#ifdef FEAT_SEARCHPATH
576 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000577 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100578 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000579 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000580 goto wingotofile;
581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 default:
583 beep_flush();
584 break;
585 }
586 break;
587
588 default: beep_flush();
589 break;
590 }
591}
592
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100593/*
594 * Figure out the address type for ":wnncmd".
595 */
596 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100597get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100598{
599 switch (*arg)
600 {
601 case 'S':
602 case Ctrl_S:
603 case 's':
604 case Ctrl_N:
605 case 'n':
606 case 'j':
607 case Ctrl_J:
608 case 'k':
609 case Ctrl_K:
610 case 'T':
611 case Ctrl_R:
612 case 'r':
613 case 'R':
614 case 'K':
615 case 'J':
616 case '+':
617 case '-':
618 case Ctrl__:
619 case '_':
620 case '|':
621 case ']':
622 case Ctrl_RSB:
623 case 'g':
624 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100625 case Ctrl_V:
626 case 'v':
627 case 'h':
628 case Ctrl_H:
629 case 'l':
630 case Ctrl_L:
631 case 'H':
632 case 'L':
633 case '>':
634 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100635#if defined(FEAT_QUICKFIX)
636 case '}':
637#endif
638#ifdef FEAT_SEARCHPATH
639 case 'f':
640 case 'F':
641 case Ctrl_F:
642#endif
643#ifdef FEAT_FIND_ID
644 case 'i':
645 case Ctrl_I:
646 case 'd':
647 case Ctrl_D:
648#endif
649 /* window size or any count */
650 eap->addr_type = ADDR_LINES;
651 break;
652
653 case Ctrl_HAT:
654 case '^':
655 /* buffer number */
656 eap->addr_type = ADDR_BUFFERS;
657 break;
658
659 case Ctrl_Q:
660 case 'q':
661 case Ctrl_C:
662 case 'c':
663 case Ctrl_O:
664 case 'o':
665 case Ctrl_W:
666 case 'w':
667 case 'W':
668 case 'x':
669 case Ctrl_X:
670 /* window number */
671 eap->addr_type = ADDR_WINDOWS;
672 break;
673
674#if defined(FEAT_QUICKFIX)
675 case Ctrl_Z:
676 case 'z':
677 case 'P':
678#endif
679 case 't':
680 case Ctrl_T:
681 case 'b':
682 case Ctrl_B:
683 case 'p':
684 case Ctrl_P:
685 case '=':
686 case CAR:
687 /* no count */
688 eap->addr_type = 0;
689 break;
690 }
691}
692
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100693 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100694cmd_with_count(
695 char *cmd,
696 char_u *bufp,
697 size_t bufsize,
698 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100699{
700 size_t len = STRLEN(cmd);
701
702 STRCPY(bufp, cmd);
703 if (Prenum > 0)
704 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
705}
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707/*
708 * split the current window, implements CTRL-W s and :split
709 *
710 * "size" is the height or width for the new window, 0 to use half of current
711 * height or width.
712 *
713 * "flags":
714 * WSP_ROOM: require enough room for new window
715 * WSP_VERT: vertical split.
716 * WSP_TOP: open window at the top-left of the shell (help window).
717 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
718 * WSP_HELP: creating the help window, keep layout snapshot
719 *
720 * return FAIL for failure, OK otherwise
721 */
722 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100723win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000725 /* When the ":tab" modifier was used open a new tab page instead. */
726 if (may_open_tabpage() == OK)
727 return OK;
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 /* Add flags from ":vertical", ":topleft" and ":botright". */
730 flags |= cmdmod.split;
731 if ((flags & WSP_TOP) && (flags & WSP_BOT))
732 {
733 EMSG(_("E442: Can't split topleft and botright at the same time"));
734 return FAIL;
735 }
736
737 /* When creating the help window make a snapshot of the window layout.
738 * Otherwise clear the snapshot, it's now invalid. */
739 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000740 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000742 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 return win_split_ins(size, flags, NULL, 0);
745}
746
747/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100748 * When "new_wp" is NULL: split the current window in two.
749 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 * top/left/right/bottom.
751 * return FAIL for failure, OK otherwise
752 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000753 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100754win_split_ins(
755 int size,
756 int flags,
757 win_T *new_wp,
758 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100760 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 win_T *oldwin;
762 int new_size = size;
763 int i;
764 int need_status = 0;
765 int do_equal = FALSE;
766 int needed;
767 int available;
768 int oldwin_height = 0;
769 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200770 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200772 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200773 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 if (flags & WSP_TOP)
776 oldwin = firstwin;
777 else if (flags & WSP_BOT)
778 oldwin = lastwin;
779 else
780 oldwin = curwin;
781
782 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100783 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100785 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
787 EMSG(_(e_noroom));
788 return FAIL;
789 }
790 need_status = STATUS_HEIGHT;
791 }
792
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000793#ifdef FEAT_GUI
794 /* May be needed for the scrollbars that are going to change. */
795 if (gui.in_use)
796 out_flush();
797#endif
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (flags & WSP_VERT)
800 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200801 int wmw1;
802 int minwidth;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
806 /*
807 * Check if we are able to split the current window and compute its
808 * width.
809 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200810 /* Current window requires at least 1 space. */
811 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
812 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200814 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200815 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200817 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200819 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200821 else if (p_ea)
822 {
823 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
824 prevfrp = oldwin->w_frame;
825 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
826 frp = frp->fr_parent)
827 {
828 if (frp->fr_layout == FR_ROW)
829 for (frp2 = frp->fr_child; frp2 != NULL;
830 frp2 = frp2->fr_next)
831 if (frp2 != prevfrp)
832 minwidth += frame_minwidth(frp2, NOWIN);
833 prevfrp = frp;
834 }
835 available = topframe->fr_width;
836 needed += minwidth;
837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200839 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200840 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
841 available = oldwin->w_frame->fr_width;
842 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200843 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100844 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
846 EMSG(_(e_noroom));
847 return FAIL;
848 }
849 if (new_size == 0)
850 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200851 if (new_size > available - minwidth - 1)
852 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200853 if (new_size < wmw1)
854 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 /* if it doesn't fit in the current window, need win_equal() */
857 if (oldwin->w_width - new_size - 1 < p_wmw)
858 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000859
860 /* We don't like to take lines for the new window from a
861 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100862 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000863 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100864 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000865
866 /* Only make all windows the same width if one of them (except oldwin)
867 * is wider than one of the split windows. */
868 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
869 && oldwin->w_frame->fr_parent != NULL)
870 {
871 frp = oldwin->w_frame->fr_parent->fr_child;
872 while (frp != NULL)
873 {
874 if (frp->fr_win != oldwin && frp->fr_win != NULL
875 && (frp->fr_win->w_width > new_size
876 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100877 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000878 {
879 do_equal = TRUE;
880 break;
881 }
882 frp = frp->fr_next;
883 }
884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {
888 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 /*
891 * Check if we are able to split the current window and compute its
892 * height.
893 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200894 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100895 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200896 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200898 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200899 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200901 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200903 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200905 else if (p_ea)
906 {
907 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
908 prevfrp = oldwin->w_frame;
909 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
910 frp = frp->fr_parent)
911 {
912 if (frp->fr_layout == FR_COL)
913 for (frp2 = frp->fr_child; frp2 != NULL;
914 frp2 = frp2->fr_next)
915 if (frp2 != prevfrp)
916 minheight += frame_minheight(frp2, NOWIN);
917 prevfrp = frp;
918 }
919 available = topframe->fr_height;
920 needed += minheight;
921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else
923 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200924 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
925 available = oldwin->w_frame->fr_height;
926 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100928 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930 EMSG(_(e_noroom));
931 return FAIL;
932 }
933 oldwin_height = oldwin->w_height;
934 if (need_status)
935 {
936 oldwin->w_status_height = STATUS_HEIGHT;
937 oldwin_height -= STATUS_HEIGHT;
938 }
939 if (new_size == 0)
940 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200941 if (new_size > available - minheight - STATUS_HEIGHT)
942 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200943 if (new_size < wmh1)
944 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 /* if it doesn't fit in the current window, need win_equal() */
947 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
948 do_equal = TRUE;
949
950 /* We don't like to take lines for the new window from a
951 * 'winfixheight' window. Take them from a window above or below
952 * instead, if possible. */
953 if (oldwin->w_p_wfh)
954 {
955 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
956 oldwin);
957 oldwin_height = oldwin->w_height;
958 if (need_status)
959 oldwin_height -= STATUS_HEIGHT;
960 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000961
962 /* Only make all windows the same height if one of them (except oldwin)
963 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100964 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000965 && oldwin->w_frame->fr_parent != NULL)
966 {
967 frp = oldwin->w_frame->fr_parent->fr_child;
968 while (frp != NULL)
969 {
970 if (frp->fr_win != oldwin && frp->fr_win != NULL
971 && (frp->fr_win->w_height > new_size
972 || frp->fr_win->w_height > oldwin_height - new_size
973 - STATUS_HEIGHT))
974 {
975 do_equal = TRUE;
976 break;
977 }
978 frp = frp->fr_next;
979 }
980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982
983 /*
984 * allocate new window structure and link it in the window list
985 */
986 if ((flags & WSP_TOP) == 0
987 && ((flags & WSP_BOT)
988 || (flags & WSP_BELOW)
989 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100990 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
992 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100993 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000994 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 else
996 win_append(oldwin, wp);
997 }
998 else
999 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001000 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001001 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 else
1003 win_append(oldwin->w_prev, wp);
1004 }
1005
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001006 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
1008 if (wp == NULL)
1009 return FAIL;
1010
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001011 new_frame(wp);
1012 if (wp->w_frame == NULL)
1013 {
1014 win_free(wp, NULL);
1015 return FAIL;
1016 }
1017
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001018 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001019 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
1021
1022 /*
1023 * Reorganise the tree of frames to insert the new window.
1024 */
1025 if (flags & (WSP_TOP | WSP_BOT))
1026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1028 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 curfrp = topframe->fr_child;
1031 if (flags & WSP_BOT)
1032 while (curfrp->fr_next != NULL)
1033 curfrp = curfrp->fr_next;
1034 }
1035 else
1036 curfrp = topframe;
1037 before = (flags & WSP_TOP);
1038 }
1039 else
1040 {
1041 curfrp = oldwin->w_frame;
1042 if (flags & WSP_BELOW)
1043 before = FALSE;
1044 else if (flags & WSP_ABOVE)
1045 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001046 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 before = !p_spr;
1048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 before = !p_sb;
1050 }
1051 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1052 {
1053 /* Need to create a new frame in the tree to make a branch. */
1054 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1055 *frp = *curfrp;
1056 curfrp->fr_layout = layout;
1057 frp->fr_parent = curfrp;
1058 frp->fr_next = NULL;
1059 frp->fr_prev = NULL;
1060 curfrp->fr_child = frp;
1061 curfrp->fr_win = NULL;
1062 curfrp = frp;
1063 if (frp->fr_win != NULL)
1064 oldwin->w_frame = frp;
1065 else
1066 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1067 frp->fr_parent = curfrp;
1068 }
1069
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001070 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001071 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001073 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 frp->fr_parent = curfrp->fr_parent;
1075
1076 /* Insert the new frame at the right place in the frame list. */
1077 if (before)
1078 frame_insert(curfrp, frp);
1079 else
1080 frame_append(curfrp, frp);
1081
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001082 /* Set w_fraction now so that the cursor keeps the same relative
1083 * vertical position. */
Bram Moolenaar3679c172017-11-22 22:22:11 +01001084 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001085 wp->w_fraction = oldwin->w_fraction;
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (flags & WSP_VERT)
1088 {
1089 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (need_status)
1092 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001093 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 oldwin->w_status_height = need_status;
1095 }
1096 if (flags & (WSP_TOP | WSP_BOT))
1097 {
1098 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001099 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001100 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001101 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 wp->w_status_height = (p_ls > 0);
1103 }
1104 else
1105 {
1106 /* height and row of new window is same as current window */
1107 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001108 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 wp->w_status_height = oldwin->w_status_height;
1110 }
1111 frp->fr_height = curfrp->fr_height;
1112
1113 /* "new_size" of the current window goes to the new window, use
1114 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001115 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (before)
1117 wp->w_vsep_width = 1;
1118 else
1119 {
1120 wp->w_vsep_width = oldwin->w_vsep_width;
1121 oldwin->w_vsep_width = 1;
1122 }
1123 if (flags & (WSP_TOP | WSP_BOT))
1124 {
1125 if (flags & WSP_BOT)
1126 frame_add_vsep(curfrp);
1127 /* Set width of neighbor frame */
1128 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001129 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1130 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001133 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (before) /* new window left of current one */
1135 {
1136 wp->w_wincol = oldwin->w_wincol;
1137 oldwin->w_wincol += new_size + 1;
1138 }
1139 else /* new window right of current one */
1140 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1141 frame_fix_width(oldwin);
1142 frame_fix_width(wp);
1143 }
1144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 if (flags & (WSP_TOP | WSP_BOT))
1148 {
1149 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001150 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 wp->w_vsep_width = 0;
1152 }
1153 else
1154 {
1155 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001156 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 wp->w_vsep_width = oldwin->w_vsep_width;
1158 }
1159 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 /* "new_size" of the current window goes to the new window, use
1162 * one row for the status line */
1163 win_new_height(wp, new_size);
1164 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001165 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001166 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001167 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001168
1169 if (!((flags & WSP_BOT) && p_ls == 0))
1170 new_fr_height -= STATUS_HEIGHT;
1171 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 else
1174 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1175 if (before) /* new window above current one */
1176 {
1177 wp->w_winrow = oldwin->w_winrow;
1178 wp->w_status_height = STATUS_HEIGHT;
1179 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1180 }
1181 else /* new window below current one */
1182 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001183 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1184 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001186 if (!(flags & WSP_BOT))
1187 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (flags & WSP_BOT)
1190 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 frame_fix_height(wp);
1192 frame_fix_height(oldwin);
1193 }
1194
1195 if (flags & (WSP_TOP | WSP_BOT))
1196 (void)win_comp_pos();
1197
1198 /*
1199 * Both windows need redrawing
1200 */
1201 redraw_win_later(wp, NOT_VALID);
1202 wp->w_redr_status = TRUE;
1203 redraw_win_later(oldwin, NOT_VALID);
1204 oldwin->w_redr_status = TRUE;
1205
1206 if (need_status)
1207 {
1208 msg_row = Rows - 1;
1209 msg_col = sc_col;
1210 msg_clr_eos_force(); /* Old command/ruler may still be there */
1211 comp_col();
1212 msg_row = Rows - 1;
1213 msg_col = 0; /* put position back at start of line */
1214 }
1215
1216 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001217 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 */
1219 if (do_equal || dir != 0)
1220 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001222 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1225 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (flags & WSP_VERT)
1227 {
1228 i = p_wiw;
1229 if (size != 0)
1230 p_wiw = size;
1231
1232# ifdef FEAT_GUI
1233 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1234 if (gui.in_use)
1235 gui_init_which_components(NULL);
1236# endif
1237 }
1238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
1240 i = p_wh;
1241 if (size != 0)
1242 p_wh = size;
1243 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001244
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001245#ifdef FEAT_JUMPLIST
1246 /* Keep same changelist position in new window. */
1247 wp->w_changelistidx = oldwin->w_changelistidx;
1248#endif
1249
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001250 /*
1251 * make the new window the current window
1252 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001253 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (flags & WSP_VERT)
1255 p_wiw = i;
1256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 p_wh = i;
1258
1259 return OK;
1260}
1261
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001262
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001263/*
1264 * Initialize window "newp" from window "oldp".
1265 * Used when splitting a window and when creating a new tab page.
1266 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001267 * WSP_NEWLOC may be specified in flags to prevent the location list from
1268 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001269 */
1270 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001271win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001272{
1273 int i;
1274
1275 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001276#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001277 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001278#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001279 oldp->w_buffer->b_nwindows++;
1280 newp->w_cursor = oldp->w_cursor;
1281 newp->w_valid = 0;
1282 newp->w_curswant = oldp->w_curswant;
1283 newp->w_set_curswant = oldp->w_set_curswant;
1284 newp->w_topline = oldp->w_topline;
1285#ifdef FEAT_DIFF
1286 newp->w_topfill = oldp->w_topfill;
1287#endif
1288 newp->w_leftcol = oldp->w_leftcol;
1289 newp->w_pcmark = oldp->w_pcmark;
1290 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1291 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001292 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001293 newp->w_fraction = oldp->w_fraction;
1294 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1295#ifdef FEAT_JUMPLIST
1296 copy_jumplist(oldp, newp);
1297#endif
1298#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001299 if (flags & WSP_NEWLOC)
1300 {
1301 /* Don't copy the location list. */
1302 newp->w_llist = NULL;
1303 newp->w_llist_ref = NULL;
1304 }
1305 else
1306 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001307#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001308 newp->w_localdir = (oldp->w_localdir == NULL)
1309 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001310
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001311 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001312 for (i = 0; i < oldp->w_tagstacklen; i++)
1313 {
1314 newp->w_tagstack[i] = oldp->w_tagstack[i];
1315 if (newp->w_tagstack[i].tagname != NULL)
1316 newp->w_tagstack[i].tagname =
1317 vim_strsave(newp->w_tagstack[i].tagname);
1318 }
1319 newp->w_tagstackidx = oldp->w_tagstackidx;
1320 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001321#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001322 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001323#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001324
1325 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001326
Bram Moolenaara971b822011-09-14 14:43:25 +02001327#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001328 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001329#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001330}
1331
1332/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001333 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001334 * Only the essential things are copied.
1335 */
1336 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001337win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001338{
1339 /* Use the same argument list. */
1340 newp->w_alist = oldp->w_alist;
1341 ++newp->w_alist->al_refcount;
1342 newp->w_arg_idx = oldp->w_arg_idx;
1343
1344 /* copy options from existing window */
1345 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001346}
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001350 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 */
1352 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001353win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 win_T *wp;
1356
1357 if (win == NULL)
1358 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001359 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (wp == win)
1361 return TRUE;
1362 return FALSE;
1363}
1364
1365/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001366 * Check if "win" is a pointer to an existing window in any tab page.
1367 */
1368 int
1369win_valid_any_tab(win_T *win)
1370{
1371 win_T *wp;
1372 tabpage_T *tp;
1373
1374 if (win == NULL)
1375 return FALSE;
1376 FOR_ALL_TABPAGES(tp)
1377 {
1378 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1379 {
1380 if (wp == win)
1381 return TRUE;
1382 }
1383 }
1384 return FALSE;
1385}
1386
1387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 * Return the number of windows.
1389 */
1390 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001391win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 win_T *wp;
1394 int count = 0;
1395
Bram Moolenaar29323592016-07-24 22:04:11 +02001396 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ++count;
1398 return count;
1399}
1400
1401/*
1402 * Make "count" windows on the screen.
1403 * Return actual number of windows on the screen.
1404 * Must be called when there is just one window, filling the whole screen
1405 * (excluding the command line).
1406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001408make_windows(
1409 int count,
1410 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 int maxcount;
1413 int todo;
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (vertical)
1416 {
1417 /* Each windows needs at least 'winminwidth' lines and a separator
1418 * column. */
1419 maxcount = (curwin->w_width + curwin->w_vsep_width
1420 - (p_wiw - p_wmw)) / (p_wmw + 1);
1421 }
1422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
1424 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001425 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1427 }
1428
1429 if (maxcount < 2)
1430 maxcount = 2;
1431 if (count > maxcount)
1432 count = maxcount;
1433
1434 /*
1435 * add status line now, otherwise first window will be too big
1436 */
1437 if (count > 1)
1438 last_status(TRUE);
1439
1440#ifdef FEAT_AUTOCMD
1441 /*
1442 * Don't execute autocommands while creating the windows. Must do that
1443 * when putting the buffers in the windows.
1444 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001445 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446#endif
1447
1448 /* todo is number of windows left to create */
1449 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 if (vertical)
1451 {
1452 if (win_split(curwin->w_width - (curwin->w_width - todo)
1453 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1454 break;
1455 }
1456 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 {
1458 if (win_split(curwin->w_height - (curwin->w_height - todo
1459 * STATUS_HEIGHT) / (todo + 1)
1460 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1461 break;
1462 }
1463
1464#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001465 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
1467
1468 /* return actual number of windows */
1469 return (count - todo);
1470}
1471
1472/*
1473 * Exchange current and next window
1474 */
1475 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001476win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478 frame_T *frp;
1479 frame_T *frp2;
1480 win_T *wp;
1481 win_T *wp2;
1482 int temp;
1483
Bram Moolenaar459ca562016-11-10 18:16:33 +01001484 if (ONE_WINDOW) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 {
1486 beep_flush();
1487 return;
1488 }
1489
1490#ifdef FEAT_GUI
1491 need_mouse_correct = TRUE;
1492#endif
1493
1494 /*
1495 * find window to exchange with
1496 */
1497 if (Prenum)
1498 {
1499 frp = curwin->w_frame->fr_parent->fr_child;
1500 while (frp != NULL && --Prenum > 0)
1501 frp = frp->fr_next;
1502 }
1503 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1504 frp = curwin->w_frame->fr_next;
1505 else /* Swap last window in row/col with previous */
1506 frp = curwin->w_frame->fr_prev;
1507
1508 /* We can only exchange a window with another window, not with a frame
1509 * containing windows. */
1510 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1511 return;
1512 wp = frp->fr_win;
1513
1514/*
1515 * 1. remove curwin from the list. Remember after which window it was in wp2
1516 * 2. insert curwin before wp in the list
1517 * if wp != wp2
1518 * 3. remove wp from the list
1519 * 4. insert wp after wp2
1520 * 5. exchange the status line height and vsep width.
1521 */
1522 wp2 = curwin->w_prev;
1523 frp2 = curwin->w_frame->fr_prev;
1524 if (wp->w_prev != curwin)
1525 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001526 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 frame_remove(curwin->w_frame);
1528 win_append(wp->w_prev, curwin);
1529 frame_insert(frp, curwin->w_frame);
1530 }
1531 if (wp != wp2)
1532 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001533 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 frame_remove(wp->w_frame);
1535 win_append(wp2, wp);
1536 if (frp2 == NULL)
1537 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1538 else
1539 frame_append(frp2, wp->w_frame);
1540 }
1541 temp = curwin->w_status_height;
1542 curwin->w_status_height = wp->w_status_height;
1543 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 temp = curwin->w_vsep_width;
1545 curwin->w_vsep_width = wp->w_vsep_width;
1546 wp->w_vsep_width = temp;
1547
1548 /* If the windows are not in the same frame, exchange the sizes to avoid
1549 * messing up the window layout. Otherwise fix the frame sizes. */
1550 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1551 {
1552 temp = curwin->w_height;
1553 curwin->w_height = wp->w_height;
1554 wp->w_height = temp;
1555 temp = curwin->w_width;
1556 curwin->w_width = wp->w_width;
1557 wp->w_width = temp;
1558 }
1559 else
1560 {
1561 frame_fix_height(curwin);
1562 frame_fix_height(wp);
1563 frame_fix_width(curwin);
1564 frame_fix_width(wp);
1565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
1567 (void)win_comp_pos(); /* recompute window positions */
1568
1569 win_enter(wp, TRUE);
1570 redraw_later(CLEAR);
1571}
1572
1573/*
1574 * rotate windows: if upwards TRUE the second window becomes the first one
1575 * if upwards FALSE the first window becomes the second one
1576 */
1577 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001578win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 win_T *wp1;
1581 win_T *wp2;
1582 frame_T *frp;
1583 int n;
1584
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001585 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 {
1587 beep_flush();
1588 return;
1589 }
1590
1591#ifdef FEAT_GUI
1592 need_mouse_correct = TRUE;
1593#endif
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 /* Check if all frames in this row/col have one window. */
1596 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1597 frp = frp->fr_next)
1598 if (frp->fr_win == NULL)
1599 {
1600 EMSG(_("E443: Cannot rotate when another window is split"));
1601 return;
1602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604 while (count--)
1605 {
1606 if (upwards) /* first window becomes last window */
1607 {
1608 /* remove first window/frame from the list */
1609 frp = curwin->w_frame->fr_parent->fr_child;
1610 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001611 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 frame_remove(frp);
1613
1614 /* find last frame and append removed window/frame after it */
1615 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1616 ;
1617 win_append(frp->fr_win, wp1);
1618 frame_append(frp, wp1->w_frame);
1619
1620 wp2 = frp->fr_win; /* previously last window */
1621 }
1622 else /* last window becomes first window */
1623 {
1624 /* find last window/frame in the list and remove it */
1625 for (frp = curwin->w_frame; frp->fr_next != NULL;
1626 frp = frp->fr_next)
1627 ;
1628 wp1 = frp->fr_win;
1629 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001630 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 frame_remove(frp);
1632
1633 /* append the removed window/frame before the first in the list */
1634 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1635 frame_insert(frp->fr_parent->fr_child, frp);
1636 }
1637
1638 /* exchange status height and vsep width of old and new last window */
1639 n = wp2->w_status_height;
1640 wp2->w_status_height = wp1->w_status_height;
1641 wp1->w_status_height = n;
1642 frame_fix_height(wp1);
1643 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 n = wp2->w_vsep_width;
1645 wp2->w_vsep_width = wp1->w_vsep_width;
1646 wp1->w_vsep_width = n;
1647 frame_fix_width(wp1);
1648 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001650 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 (void)win_comp_pos();
1652 }
1653
1654 redraw_later(CLEAR);
1655}
1656
1657/*
1658 * Move the current window to the very top/bottom/left/right of the screen.
1659 */
1660 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001661win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 int dir;
1664 int height = curwin->w_height;
1665
Bram Moolenaar459ca562016-11-10 18:16:33 +01001666 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 {
1668 beep_flush();
1669 return;
1670 }
1671
1672 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001673 (void)winframe_remove(curwin, &dir, NULL);
1674 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 last_status(FALSE); /* may need to remove last status line */
1676 (void)win_comp_pos(); /* recompute window positions */
1677
1678 /* Split a window on the desired side and put the window there. */
1679 (void)win_split_ins(size, flags, curwin, dir);
1680 if (!(flags & WSP_VERT))
1681 {
1682 win_setheight(height);
1683 if (p_ea)
1684 win_equal(curwin, TRUE, 'v');
1685 }
1686
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001687#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1689 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001690 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692}
1693
1694/*
1695 * Move window "win1" to below/right of "win2" and make "win1" the current
1696 * window. Only works within the same frame!
1697 */
1698 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001699win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 int height;
1702
1703 /* check if the arguments are reasonable */
1704 if (win1 == win2)
1705 return;
1706
1707 /* check if there is something to do */
1708 if (win2->w_next != win1)
1709 {
1710 /* may need move the status line/vertical separator of the last window
1711 * */
1712 if (win1 == lastwin)
1713 {
1714 height = win1->w_prev->w_status_height;
1715 win1->w_prev->w_status_height = win1->w_status_height;
1716 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001717 if (win1->w_prev->w_vsep_width == 1)
1718 {
1719 /* Remove the vertical separator from the last-but-one window,
1720 * add it to the last window. Adjust the frame widths. */
1721 win1->w_prev->w_vsep_width = 0;
1722 win1->w_prev->w_frame->fr_width -= 1;
1723 win1->w_vsep_width = 1;
1724 win1->w_frame->fr_width += 1;
1725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 }
1727 else if (win2 == lastwin)
1728 {
1729 height = win1->w_status_height;
1730 win1->w_status_height = win2->w_status_height;
1731 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001732 if (win1->w_vsep_width == 1)
1733 {
1734 /* Remove the vertical separator from win1, add it to the last
1735 * window, win2. Adjust the frame widths. */
1736 win2->w_vsep_width = 1;
1737 win2->w_frame->fr_width += 1;
1738 win1->w_vsep_width = 0;
1739 win1->w_frame->fr_width -= 1;
1740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001742 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 frame_remove(win1->w_frame);
1744 win_append(win2, win1);
1745 frame_append(win2->w_frame, win1->w_frame);
1746
1747 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1748 redraw_later(NOT_VALID);
1749 }
1750 win_enter(win1, FALSE);
1751}
1752
1753/*
1754 * Make all windows the same height.
1755 * 'next_curwin' will soon be the current window, make sure it has enough
1756 * rows.
1757 */
1758 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001759win_equal(
1760 win_T *next_curwin, /* pointer to current window to be or NULL */
1761 int current, /* do only frame with current window */
1762 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 'b' for both, 0 for using p_ead */
1764{
1765 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001768 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001769 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770}
1771
1772/*
1773 * Set a frame to a new position and height, spreading the available room
1774 * equally over contained frames.
1775 * The window "next_curwin" (if not NULL) should at least get the size from
1776 * 'winheight' and 'winwidth' if possible.
1777 */
1778 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001779win_equal_rec(
1780 win_T *next_curwin, /* pointer to current window to be or NULL */
1781 int current, /* do only frame with current window */
1782 frame_T *topfr, /* frame to set size off */
1783 int dir, /* 'v', 'h' or 'b', see win_equal() */
1784 int col, /* horizontal position for frame */
1785 int row, /* vertical position for frame */
1786 int width, /* new width of frame */
1787 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
1789 int n, m;
1790 int extra_sep = 0;
1791 int wincount, totwincount = 0;
1792 frame_T *fr;
1793 int next_curwin_size = 0;
1794 int room = 0;
1795 int new_size;
1796 int has_next_curwin = 0;
1797 int hnc;
1798
1799 if (topfr->fr_layout == FR_LEAF)
1800 {
1801 /* Set the width/height of this frame.
1802 * Redraw when size or position changes */
1803 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 )
1806 {
1807 topfr->fr_win->w_winrow = row;
1808 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001810 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 redraw_all_later(CLEAR);
1812 }
1813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 else if (topfr->fr_layout == FR_ROW)
1815 {
1816 topfr->fr_width = width;
1817 topfr->fr_height = height;
1818
1819 if (dir != 'v') /* equalize frame widths */
1820 {
1821 /* Compute the maximum number of windows horizontally in this
1822 * frame. */
1823 n = frame_minwidth(topfr, NOWIN);
1824 /* add one for the rightmost window, it doesn't have a separator */
1825 if (col + width == Columns)
1826 extra_sep = 1;
1827 else
1828 extra_sep = 0;
1829 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001830 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001832 /*
1833 * Compute width for "next_curwin" window and room available for
1834 * other windows.
1835 * "m" is the minimal width when counting p_wiw for "next_curwin".
1836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 m = frame_minwidth(topfr, next_curwin);
1838 room = width - m;
1839 if (room < 0)
1840 {
1841 next_curwin_size = p_wiw + room;
1842 room = 0;
1843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 else
1845 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001846 next_curwin_size = -1;
1847 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1848 {
1849 /* If 'winfixwidth' set keep the window width if
1850 * possible.
1851 * Watch out for this window being the next_curwin. */
1852 if (frame_fixed_width(fr))
1853 {
1854 n = frame_minwidth(fr, NOWIN);
1855 new_size = fr->fr_width;
1856 if (frame_has_win(fr, next_curwin))
1857 {
1858 room += p_wiw - p_wmw;
1859 next_curwin_size = 0;
1860 if (new_size < p_wiw)
1861 new_size = p_wiw;
1862 }
1863 else
1864 /* These windows don't use up room. */
1865 totwincount -= (n + (fr->fr_next == NULL
1866 ? extra_sep : 0)) / (p_wmw + 1);
1867 room -= new_size - n;
1868 if (room < 0)
1869 {
1870 new_size += room;
1871 room = 0;
1872 }
1873 fr->fr_newwidth = new_size;
1874 }
1875 }
1876 if (next_curwin_size == -1)
1877 {
1878 if (!has_next_curwin)
1879 next_curwin_size = 0;
1880 else if (totwincount > 1
1881 && (room + (totwincount - 2))
1882 / (totwincount - 1) > p_wiw)
1883 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001884 /* Can make all windows wider than 'winwidth', spread
1885 * the room equally. */
1886 next_curwin_size = (room + p_wiw
1887 + (totwincount - 1) * p_wmw
1888 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001889 room -= next_curwin_size - p_wiw;
1890 }
1891 else
1892 next_curwin_size = p_wiw;
1893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001895
1896 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 --totwincount; /* don't count curwin */
1898 }
1899
1900 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 wincount = 1;
1903 if (fr->fr_next == NULL)
1904 /* last frame gets all that remains (avoid roundoff error) */
1905 new_size = width;
1906 else if (dir == 'v')
1907 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001908 else if (frame_fixed_width(fr))
1909 {
1910 new_size = fr->fr_newwidth;
1911 wincount = 0; /* doesn't count as a sizeable window */
1912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 else
1914 {
1915 /* Compute the maximum number of windows horiz. in "fr". */
1916 n = frame_minwidth(fr, NOWIN);
1917 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1918 / (p_wmw + 1);
1919 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001920 if (has_next_curwin)
1921 hnc = frame_has_win(fr, next_curwin);
1922 else
1923 hnc = FALSE;
1924 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001926 if (totwincount == 0)
1927 new_size = room;
1928 else
1929 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001931 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 next_curwin_size -= p_wiw - (m - n);
1934 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001935 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001937 else
1938 room -= new_size;
1939 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
1941
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001942 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 * window, unless equalizing all frames. */
1944 if (!current || dir != 'v' || topfr->fr_parent != NULL
1945 || (new_size != fr->fr_width)
1946 || frame_has_win(fr, next_curwin))
1947 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001948 new_size, height);
1949 col += new_size;
1950 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 totwincount -= wincount;
1952 }
1953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 else /* topfr->fr_layout == FR_COL */
1955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 topfr->fr_height = height;
1958
1959 if (dir != 'h') /* equalize frame heights */
1960 {
1961 /* Compute maximum number of windows vertically in this frame. */
1962 n = frame_minheight(topfr, NOWIN);
1963 /* add one for the bottom window if it doesn't have a statusline */
1964 if (row + height == cmdline_row && p_ls == 0)
1965 extra_sep = 1;
1966 else
1967 extra_sep = 0;
1968 totwincount = (n + extra_sep) / (p_wmh + 1);
1969 has_next_curwin = frame_has_win(topfr, next_curwin);
1970
1971 /*
1972 * Compute height for "next_curwin" window and room available for
1973 * other windows.
1974 * "m" is the minimal height when counting p_wh for "next_curwin".
1975 */
1976 m = frame_minheight(topfr, next_curwin);
1977 room = height - m;
1978 if (room < 0)
1979 {
1980 /* The room is less then 'winheight', use all space for the
1981 * current window. */
1982 next_curwin_size = p_wh + room;
1983 room = 0;
1984 }
1985 else
1986 {
1987 next_curwin_size = -1;
1988 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1989 {
1990 /* If 'winfixheight' set keep the window height if
1991 * possible.
1992 * Watch out for this window being the next_curwin. */
1993 if (frame_fixed_height(fr))
1994 {
1995 n = frame_minheight(fr, NOWIN);
1996 new_size = fr->fr_height;
1997 if (frame_has_win(fr, next_curwin))
1998 {
1999 room += p_wh - p_wmh;
2000 next_curwin_size = 0;
2001 if (new_size < p_wh)
2002 new_size = p_wh;
2003 }
2004 else
2005 /* These windows don't use up room. */
2006 totwincount -= (n + (fr->fr_next == NULL
2007 ? extra_sep : 0)) / (p_wmh + 1);
2008 room -= new_size - n;
2009 if (room < 0)
2010 {
2011 new_size += room;
2012 room = 0;
2013 }
2014 fr->fr_newheight = new_size;
2015 }
2016 }
2017 if (next_curwin_size == -1)
2018 {
2019 if (!has_next_curwin)
2020 next_curwin_size = 0;
2021 else if (totwincount > 1
2022 && (room + (totwincount - 2))
2023 / (totwincount - 1) > p_wh)
2024 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002025 /* can make all windows higher than 'winheight',
2026 * spread the room equally. */
2027 next_curwin_size = (room + p_wh
2028 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 + (totwincount - 1)) / totwincount;
2030 room -= next_curwin_size - p_wh;
2031 }
2032 else
2033 next_curwin_size = p_wh;
2034 }
2035 }
2036
2037 if (has_next_curwin)
2038 --totwincount; /* don't count curwin */
2039 }
2040
2041 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2042 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 wincount = 1;
2044 if (fr->fr_next == NULL)
2045 /* last frame gets all that remains (avoid roundoff error) */
2046 new_size = height;
2047 else if (dir == 'h')
2048 new_size = fr->fr_height;
2049 else if (frame_fixed_height(fr))
2050 {
2051 new_size = fr->fr_newheight;
2052 wincount = 0; /* doesn't count as a sizeable window */
2053 }
2054 else
2055 {
2056 /* Compute the maximum number of windows vert. in "fr". */
2057 n = frame_minheight(fr, NOWIN);
2058 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2059 / (p_wmh + 1);
2060 m = frame_minheight(fr, next_curwin);
2061 if (has_next_curwin)
2062 hnc = frame_has_win(fr, next_curwin);
2063 else
2064 hnc = FALSE;
2065 if (hnc) /* don't count next_curwin */
2066 --wincount;
2067 if (totwincount == 0)
2068 new_size = room;
2069 else
2070 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2071 / totwincount;
2072 if (hnc) /* add next_curwin size */
2073 {
2074 next_curwin_size -= p_wh - (m - n);
2075 new_size += next_curwin_size;
2076 room -= new_size - next_curwin_size;
2077 }
2078 else
2079 room -= new_size;
2080 new_size += n;
2081 }
2082 /* Skip frame that is full width when splitting or closing a
2083 * window, unless equalizing all frames. */
2084 if (!current || dir != 'h' || topfr->fr_parent != NULL
2085 || (new_size != fr->fr_height)
2086 || frame_has_win(fr, next_curwin))
2087 win_equal_rec(next_curwin, current, fr, dir, col, row,
2088 width, new_size);
2089 row += new_size;
2090 height -= new_size;
2091 totwincount -= wincount;
2092 }
2093 }
2094}
2095
2096/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002097 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 */
2099 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002100close_windows(
2101 buf_T *buf,
2102 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002104 win_T *wp;
2105 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002106 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002107#ifdef FEAT_AUTOCMD
2108 int count = tabpage_index(NULL);
2109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002112
Bram Moolenaar459ca562016-11-10 18:16:33 +01002113 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002115 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2116#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002117 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002118#endif
2119 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002121 if (win_close(wp, FALSE) == FAIL)
2122 /* If closing the window fails give up, to avoid looping
2123 * forever. */
2124 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002125
2126 /* Start all over, autocommands may change the window layout. */
2127 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
2129 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002130 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002132
2133 /* Also check windows in other tab pages. */
2134 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2135 {
2136 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002137 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002138 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002139 if (wp->w_buffer == buf
2140#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002141 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002142#endif
2143 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002144 {
2145 win_close_othertab(wp, FALSE, tp);
2146
2147 /* Start all over, the tab page may be closed and
2148 * autocommands may change the window layout. */
2149 nexttp = first_tabpage;
2150 break;
2151 }
2152 }
2153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002155
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002156#ifdef FEAT_AUTOCMD
2157 if (count != tabpage_index(NULL))
2158 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
2159#endif
2160
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002161 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002162 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002163 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164}
2165
2166/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002167 * Return TRUE if the current window is the only window that exists (ignoring
2168 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002169 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002170 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002171 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002172last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002173{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002174 return (one_window() && first_tabpage->tp_next == NULL);
2175}
2176
2177/*
2178 * Return TRUE if there is only one window other than "aucmd_win" in the
2179 * current tab page.
2180 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002181 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002182one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002183{
2184#ifdef FEAT_AUTOCMD
2185 win_T *wp;
2186 int seen_one = FALSE;
2187
2188 FOR_ALL_WINDOWS(wp)
2189 {
2190 if (wp != aucmd_win)
2191 {
2192 if (seen_one)
2193 return FALSE;
2194 seen_one = TRUE;
2195 }
2196 }
2197 return TRUE;
2198#else
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002199 return ONE_WINDOW;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002200#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002201}
2202
2203/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002204 * Close the possibly last window in a tab page.
2205 * Returns TRUE when the window was closed already.
2206 */
2207 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002208close_last_window_tabpage(
2209 win_T *win,
2210 int free_buf,
2211 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002212{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002213 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002214 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002215#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002216 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002217#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002218
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002219 /*
2220 * Closing the last window in a tab page. First go to another tab
2221 * page and then close the window and the tab page. This avoids that
2222 * curwin and curtab are invalid while we are freeing memory, they may
2223 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002224 * Don't trigger autocommands yet, they may use wrong values, so do
2225 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002226 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002227 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002228 redraw_tabline = TRUE;
2229
2230 /* Safety check: Autocommands may have closed the window when jumping
2231 * to the other tab page. */
2232 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2233 {
2234 int h = tabline_height();
2235
2236 win_close_othertab(win, free_buf, prev_curtab);
2237 if (h != tabline_height())
2238 shell_new_rows();
2239 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002240 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2241 * that now. */
2242#ifdef FEAT_AUTOCMD
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002243 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002244 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002245 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2246 if (old_curbuf != curbuf)
2247 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002248#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002249 return TRUE;
2250 }
2251 return FALSE;
2252}
2253
2254/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 * If "free_buf" is TRUE related buffer may be unloaded.
2257 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002258 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002259 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002261 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002262win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 win_T *wp;
2265#ifdef FEAT_AUTOCMD
2266 int other_buffer = FALSE;
2267#endif
2268 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 int dir;
2270 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002271 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002272 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002274 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
2276 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002280#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002281 if (win->w_closing || (win->w_buffer != NULL
2282 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002283 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002284 if (win == aucmd_win)
2285 {
2286 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002287 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002288 }
2289 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2290 {
2291 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002292 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002293 }
2294#endif
2295
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002296 /* When closing the last window in a tab page first go to another tab page
2297 * and then close the window and the tab page to avoid that curwin and
2298 * curtab are invalid while we are freeing memory. */
2299 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002300 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 /* When closing the help window, try restoring a snapshot after closing
2303 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002304 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 help_window = TRUE;
2306 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002307 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309#ifdef FEAT_AUTOCMD
2310 if (win == curwin)
2311 {
2312 /*
2313 * Guess which window is going to be the new current window.
2314 * This may change because of the autocommands (sigh).
2315 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002316 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002319 * Be careful: If autocommands delete the window or cause this window
2320 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 */
2322 if (wp->w_buffer != curbuf)
2323 {
2324 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002325 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002327 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002328 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002329 win->w_closing = FALSE;
2330 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002333 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002335 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002336 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002337 win->w_closing = FALSE;
2338 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340# ifdef FEAT_EVAL
2341 /* autocmds may abort script processing */
2342 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344# endif
2345 }
2346#endif
2347
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002348#ifdef FEAT_GUI
2349 /* Avoid trouble with scrollbars that are going to be deleted in
2350 * win_free(). */
2351 if (gui.in_use)
2352 out_flush();
2353#endif
2354
Bram Moolenaara971b822011-09-14 14:43:25 +02002355#ifdef FEAT_SYN_HL
2356 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002357 if (win->w_buffer != NULL)
2358 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002359#endif
2360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 /*
2362 * Close the link to the buffer.
2363 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002364 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002365 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002366 bufref_T bufref;
2367
2368 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002369#ifdef FEAT_AUTOCMD
2370 win->w_closing = TRUE;
2371#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002372 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002373#ifdef FEAT_AUTOCMD
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002374 if (win_valid_any_tab(win))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002375 win->w_closing = FALSE;
2376#endif
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002377 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2378 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002379 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002380 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002381 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002382
Bram Moolenaar802418d2013-01-17 14:00:11 +01002383 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2384 && (last_window() || curtab != prev_curtab
2385 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002386 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002387 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002388 * curwin->w_buffer, otherwise writing viminfo may fail. */
2389 if (curwin->w_buffer == NULL)
2390 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002391 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002392 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002393
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002394 /* Autocommands may have moved to another tab page. */
2395 if (curtab != prev_curtab && win_valid_any_tab(win)
2396 && win->w_buffer == NULL)
2397 {
2398 /* Need to close the window anyway, since the buffer is NULL. */
2399 win_close_othertab(win, FALSE, prev_curtab);
2400 return FAIL;
2401 }
2402
2403 /* Autocommands may have closed the window already or closed the only
2404 * other window. */
2405 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002406 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
Bram Moolenaara971b822011-09-14 14:43:25 +02002409 /* Free the memory used for the window and get the window that received
2410 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002411 wp = win_free_mem(win, &dir, NULL);
2412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 /* Make sure curwin isn't invalid. It can cause severe trouble when
2414 * printing an error message. For win_equal() curbuf needs to be valid
2415 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002416 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 curwin = wp;
2419#ifdef FEAT_QUICKFIX
2420 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2421 {
2422 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002423 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 * finding another window to go to.
2425 */
2426 for (;;)
2427 {
2428 if (wp->w_next == NULL)
2429 wp = firstwin;
2430 else
2431 wp = wp->w_next;
2432 if (wp == curwin)
2433 break;
2434 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2435 {
2436 curwin = wp;
2437 break;
2438 }
2439 }
2440 }
2441#endif
2442 curbuf = curwin->w_buffer;
2443 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002444
2445 /* The cursor position may be invalid if the buffer changed after last
2446 * using the window. */
2447 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002449 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002450 /* If the frame of the closed window contains the new current window,
2451 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002452 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else
2454 win_comp_pos();
2455 if (close_curwin)
2456 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002457 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458#ifdef FEAT_AUTOCMD
2459 if (other_buffer)
2460 /* careful: after this wp and win may be invalid! */
2461 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2462#endif
2463 }
2464
2465 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002466 * If last window has a status line now and we don't want one,
2467 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
2469 last_status(FALSE);
2470
2471 /* After closing the help window, try restoring the window layout from
2472 * before it was opened. */
2473 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002474 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002476#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2478 if (gui.in_use && !win_hasvertsplit())
2479 gui_init_which_components(NULL);
2480#endif
2481
2482 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002483 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484}
2485
2486/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002487 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002488 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002489 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002490 * Caller must check if buffer is hidden and whether the tabline needs to be
2491 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002492 */
2493 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002494win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002495{
2496 win_T *wp;
2497 int dir;
2498 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002499 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002500
Bram Moolenaar362ce482012-06-06 19:02:45 +02002501#ifdef FEAT_AUTOCMD
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002502 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2503 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002504 if (win->w_closing || (win->w_buffer != NULL
2505 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002506 return; /* window is already being closed */
2507#endif
2508
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002509 if (win->w_buffer != NULL)
2510 /* Close the link to the buffer. */
2511 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002512
2513 /* Careful: Autocommands may have closed the tab page or made it the
2514 * current tab page. */
2515 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2516 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002517 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002518 return;
2519
2520 /* Autocommands may have closed the window already. */
2521 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2522 ;
2523 if (wp == NULL)
2524 return;
2525
Bram Moolenaarf740b292006-02-16 22:11:02 +00002526 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002527 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002528 {
2529 if (tp == first_tabpage)
2530 first_tabpage = tp->tp_next;
2531 else
2532 {
2533 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2534 ptp = ptp->tp_next)
2535 ;
2536 if (ptp == NULL)
2537 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002538 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002539 return;
2540 }
2541 ptp->tp_next = tp->tp_next;
2542 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002543 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002544 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002545
2546 /* Free the memory used for the window. */
2547 win_free_mem(win, &dir, tp);
2548
2549 if (free_tp)
2550 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002551}
2552
2553/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002554 * Free the memory used for a window.
2555 * Returns a pointer to the window that got the freed up space.
2556 */
2557 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002558win_free_mem(
2559 win_T *win,
2560 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2561 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002562{
2563 frame_T *frp;
2564 win_T *wp;
2565
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002566 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002567 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002569 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002571
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002572 /* When deleting the current window of another tab page select a new
2573 * current window. */
2574 if (tp != NULL && win == tp->tp_curwin)
2575 tp->tp_curwin = wp;
2576
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002577 return wp;
2578}
2579
2580#if defined(EXITFREE) || defined(PROTO)
2581 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002582win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002583{
2584 int dummy;
2585
Bram Moolenaarf740b292006-02-16 22:11:02 +00002586 while (first_tabpage->tp_next != NULL)
2587 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002588
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002589# ifdef FEAT_AUTOCMD
2590 if (aucmd_win != NULL)
2591 {
2592 (void)win_free_mem(aucmd_win, &dummy, NULL);
2593 aucmd_win = NULL;
2594 }
2595# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002596
2597 while (firstwin != NULL)
2598 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002599
2600 /* No window should be used after this. Set curwin to NULL to crash
2601 * instead of using freed memory. */
2602 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002603}
2604#endif
2605
2606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 * Remove a window and its frame from the tree of frames.
2608 * Returns a pointer to the window that got the freed up space.
2609 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002610 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002611winframe_remove(
2612 win_T *win,
2613 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2614 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 frame_T *frp, *frp2, *frp3;
2617 frame_T *frp_close = win->w_frame;
2618 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002621 * If there is only one window there is nothing to remove.
2622 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002623 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002624 return NULL;
2625
2626 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 * Remove the window from its frame.
2628 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002629 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 wp = frame2win(frp2);
2631
2632 /* Remove this frame from the list of frames. */
2633 frame_remove(frp_close);
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (frp_close->fr_parent->fr_layout == FR_COL)
2636 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002637 /* When 'winfixheight' is set, try to find another frame in the column
2638 * (as close to the closed frame as possible) to distribute the height
2639 * to. */
2640 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2641 {
2642 frp = frp_close->fr_prev;
2643 frp3 = frp_close->fr_next;
2644 while (frp != NULL || frp3 != NULL)
2645 {
2646 if (frp != NULL)
2647 {
2648 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2649 {
2650 frp2 = frp;
2651 wp = frp->fr_win;
2652 break;
2653 }
2654 frp = frp->fr_prev;
2655 }
2656 if (frp3 != NULL)
2657 {
2658 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2659 {
2660 frp2 = frp3;
2661 wp = frp3->fr_win;
2662 break;
2663 }
2664 frp3 = frp3->fr_next;
2665 }
2666 }
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2669 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 *dirp = 'v';
2671 }
2672 else
2673 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002674 /* When 'winfixwidth' is set, try to find another frame in the column
2675 * (as close to the closed frame as possible) to distribute the width
2676 * to. */
2677 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2678 {
2679 frp = frp_close->fr_prev;
2680 frp3 = frp_close->fr_next;
2681 while (frp != NULL || frp3 != NULL)
2682 {
2683 if (frp != NULL)
2684 {
2685 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2686 {
2687 frp2 = frp;
2688 wp = frp->fr_win;
2689 break;
2690 }
2691 frp = frp->fr_prev;
2692 }
2693 if (frp3 != NULL)
2694 {
2695 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2696 {
2697 frp2 = frp3;
2698 wp = frp3->fr_win;
2699 break;
2700 }
2701 frp3 = frp3->fr_next;
2702 }
2703 }
2704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002706 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 *dirp = 'h';
2708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 /* If rows/columns go to a window below/right its positions need to be
2711 * updated. Can only be done after the sizes have been updated. */
2712 if (frp2 == frp_close->fr_next)
2713 {
2714 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002715 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716
2717 frame_comp_pos(frp2, &row, &col);
2718 }
2719
2720 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2721 {
2722 /* There is no other frame in this list, move its info to the parent
2723 * and remove it. */
2724 frp2->fr_parent->fr_layout = frp2->fr_layout;
2725 frp2->fr_parent->fr_child = frp2->fr_child;
2726 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2727 frp->fr_parent = frp2->fr_parent;
2728 frp2->fr_parent->fr_win = frp2->fr_win;
2729 if (frp2->fr_win != NULL)
2730 frp2->fr_win->w_frame = frp2->fr_parent;
2731 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002732 if (topframe->fr_child == frp2)
2733 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 vim_free(frp2);
2735
2736 frp2 = frp->fr_parent;
2737 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2738 {
2739 /* The frame above the parent has the same layout, have to merge
2740 * the frames into this list. */
2741 if (frp2->fr_child == frp)
2742 frp2->fr_child = frp->fr_child;
2743 frp->fr_child->fr_prev = frp->fr_prev;
2744 if (frp->fr_prev != NULL)
2745 frp->fr_prev->fr_next = frp->fr_child;
2746 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2747 {
2748 frp3->fr_parent = frp2;
2749 if (frp3->fr_next == NULL)
2750 {
2751 frp3->fr_next = frp->fr_next;
2752 if (frp->fr_next != NULL)
2753 frp->fr_next->fr_prev = frp3;
2754 break;
2755 }
2756 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002757 if (topframe->fr_child == frp)
2758 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 vim_free(frp);
2760 }
2761 }
2762
2763 return wp;
2764}
2765
2766/*
2767 * Find out which frame is going to get the freed up space when "win" is
2768 * closed.
2769 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2770 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2771 * This makes opening a window and closing it immediately keep the same window
2772 * layout.
2773 */
2774 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002775win_altframe(
2776 win_T *win,
2777 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778{
2779 frame_T *frp;
2780 int b;
2781
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002782 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002783 /* Last window in this tab page, will go to next tab page. */
2784 return alt_tabpage()->tp_curwin->w_frame;
2785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 frp = win->w_frame;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002787 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 b = p_spr;
2789 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 b = p_sb;
2791 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2792 return frp->fr_next;
2793 return frp->fr_prev;
2794}
2795
2796/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002797 * Return the tabpage that will be used if the current one is closed.
2798 */
2799 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002800alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002801{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002802 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002803
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002804 /* Use the next tab page if possible. */
2805 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002806 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002807
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002808 /* Find the last but one tab page. */
2809 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2810 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002811 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002812}
2813
2814/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 * Find the left-upper window in frame "frp".
2816 */
2817 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002818frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819{
2820 while (frp->fr_win == NULL)
2821 frp = frp->fr_child;
2822 return frp->fr_win;
2823}
2824
2825/*
2826 * Return TRUE if frame "frp" contains window "wp".
2827 */
2828 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002829frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831 frame_T *p;
2832
2833 if (frp->fr_layout == FR_LEAF)
2834 return frp->fr_win == wp;
2835
2836 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2837 if (frame_has_win(p, wp))
2838 return TRUE;
2839 return FALSE;
2840}
2841
2842/*
2843 * Set a new height for a frame. Recursively sets the height for contained
2844 * frames and windows. Caller must take care of positions.
2845 */
2846 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002847frame_new_height(
2848 frame_T *topfrp,
2849 int height,
2850 int topfirst, /* resize topmost contained frame first */
2851 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 may cause the height not to be set */
2853{
2854 frame_T *frp;
2855 int extra_lines;
2856 int h;
2857
2858 if (topfrp->fr_win != NULL)
2859 {
2860 /* Simple case: just one window. */
2861 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02002862 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01002863 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 else if (topfrp->fr_layout == FR_ROW)
2866 {
2867 do
2868 {
2869 /* All frames in this row get the same new height. */
2870 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2871 {
2872 frame_new_height(frp, height, topfirst, wfh);
2873 if (frp->fr_height > height)
2874 {
2875 /* Could not fit the windows, make the whole row higher. */
2876 height = frp->fr_height;
2877 break;
2878 }
2879 }
2880 }
2881 while (frp != NULL);
2882 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002883 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 {
2885 /* Complicated case: Resize a column of frames. Resize the bottom
2886 * frame first, frames above that when needed. */
2887
2888 frp = topfrp->fr_child;
2889 if (wfh)
2890 /* Advance past frames with one window with 'wfh' set. */
2891 while (frame_fixed_height(frp))
2892 {
2893 frp = frp->fr_next;
2894 if (frp == NULL)
2895 return; /* no frame without 'wfh', give up */
2896 }
2897 if (!topfirst)
2898 {
2899 /* Find the bottom frame of this column */
2900 while (frp->fr_next != NULL)
2901 frp = frp->fr_next;
2902 if (wfh)
2903 /* Advance back for frames with one window with 'wfh' set. */
2904 while (frame_fixed_height(frp))
2905 frp = frp->fr_prev;
2906 }
2907
2908 extra_lines = height - topfrp->fr_height;
2909 if (extra_lines < 0)
2910 {
2911 /* reduce height of contained frames, bottom or top frame first */
2912 while (frp != NULL)
2913 {
2914 h = frame_minheight(frp, NULL);
2915 if (frp->fr_height + extra_lines < h)
2916 {
2917 extra_lines += frp->fr_height - h;
2918 frame_new_height(frp, h, topfirst, wfh);
2919 }
2920 else
2921 {
2922 frame_new_height(frp, frp->fr_height + extra_lines,
2923 topfirst, wfh);
2924 break;
2925 }
2926 if (topfirst)
2927 {
2928 do
2929 frp = frp->fr_next;
2930 while (wfh && frp != NULL && frame_fixed_height(frp));
2931 }
2932 else
2933 {
2934 do
2935 frp = frp->fr_prev;
2936 while (wfh && frp != NULL && frame_fixed_height(frp));
2937 }
2938 /* Increase "height" if we could not reduce enough frames. */
2939 if (frp == NULL)
2940 height -= extra_lines;
2941 }
2942 }
2943 else if (extra_lines > 0)
2944 {
2945 /* increase height of bottom or top frame */
2946 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2947 }
2948 }
2949 topfrp->fr_height = height;
2950}
2951
2952/*
2953 * Return TRUE if height of frame "frp" should not be changed because of
2954 * the 'winfixheight' option.
2955 */
2956 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002957frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
2959 /* frame with one window: fixed height if 'winfixheight' set. */
2960 if (frp->fr_win != NULL)
2961 return frp->fr_win->w_p_wfh;
2962
2963 if (frp->fr_layout == FR_ROW)
2964 {
2965 /* The frame is fixed height if one of the frames in the row is fixed
2966 * height. */
2967 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2968 if (frame_fixed_height(frp))
2969 return TRUE;
2970 return FALSE;
2971 }
2972
2973 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2974 * frames in the row are fixed height. */
2975 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2976 if (!frame_fixed_height(frp))
2977 return FALSE;
2978 return TRUE;
2979}
2980
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002982 * Return TRUE if width of frame "frp" should not be changed because of
2983 * the 'winfixwidth' option.
2984 */
2985 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002986frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002987{
2988 /* frame with one window: fixed width if 'winfixwidth' set. */
2989 if (frp->fr_win != NULL)
2990 return frp->fr_win->w_p_wfw;
2991
2992 if (frp->fr_layout == FR_COL)
2993 {
2994 /* The frame is fixed width if one of the frames in the row is fixed
2995 * width. */
2996 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2997 if (frame_fixed_width(frp))
2998 return TRUE;
2999 return FALSE;
3000 }
3001
3002 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3003 * frames in the row are fixed width. */
3004 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3005 if (!frame_fixed_width(frp))
3006 return FALSE;
3007 return TRUE;
3008}
3009
3010/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 * Add a status line to windows at the bottom of "frp".
3012 * Note: Does not check if there is room!
3013 */
3014 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003015frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 win_T *wp;
3018
3019 if (frp->fr_layout == FR_LEAF)
3020 {
3021 wp = frp->fr_win;
3022 if (wp->w_status_height == 0)
3023 {
3024 if (wp->w_height > 0) /* don't make it negative */
3025 --wp->w_height;
3026 wp->w_status_height = STATUS_HEIGHT;
3027 }
3028 }
3029 else if (frp->fr_layout == FR_ROW)
3030 {
3031 /* Handle all the frames in the row. */
3032 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3033 frame_add_statusline(frp);
3034 }
3035 else /* frp->fr_layout == FR_COL */
3036 {
3037 /* Only need to handle the last frame in the column. */
3038 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3039 ;
3040 frame_add_statusline(frp);
3041 }
3042}
3043
3044/*
3045 * Set width of a frame. Handles recursively going through contained frames.
3046 * May remove separator line for windows at the right side (for win_close()).
3047 */
3048 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003049frame_new_width(
3050 frame_T *topfrp,
3051 int width,
3052 int leftfirst, /* resize leftmost contained frame first */
3053 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003054 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
3056 frame_T *frp;
3057 int extra_cols;
3058 int w;
3059 win_T *wp;
3060
3061 if (topfrp->fr_layout == FR_LEAF)
3062 {
3063 /* Simple case: just one window. */
3064 wp = topfrp->fr_win;
3065 /* Find out if there are any windows right of this one. */
3066 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3067 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3068 break;
3069 if (frp->fr_parent == NULL)
3070 wp->w_vsep_width = 0;
3071 win_new_width(wp, width - wp->w_vsep_width);
3072 }
3073 else if (topfrp->fr_layout == FR_COL)
3074 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003075 do
3076 {
3077 /* All frames in this column get the same new width. */
3078 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3079 {
3080 frame_new_width(frp, width, leftfirst, wfw);
3081 if (frp->fr_width > width)
3082 {
3083 /* Could not fit the windows, make whole column wider. */
3084 width = frp->fr_width;
3085 break;
3086 }
3087 }
3088 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 }
3090 else /* fr_layout == FR_ROW */
3091 {
3092 /* Complicated case: Resize a row of frames. Resize the rightmost
3093 * frame first, frames left of it when needed. */
3094
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003096 if (wfw)
3097 /* Advance past frames with one window with 'wfw' set. */
3098 while (frame_fixed_width(frp))
3099 {
3100 frp = frp->fr_next;
3101 if (frp == NULL)
3102 return; /* no frame without 'wfw', give up */
3103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003105 {
3106 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 while (frp->fr_next != NULL)
3108 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003109 if (wfw)
3110 /* Advance back for frames with one window with 'wfw' set. */
3111 while (frame_fixed_width(frp))
3112 frp = frp->fr_prev;
3113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115 extra_cols = width - topfrp->fr_width;
3116 if (extra_cols < 0)
3117 {
3118 /* reduce frame width, rightmost frame first */
3119 while (frp != NULL)
3120 {
3121 w = frame_minwidth(frp, NULL);
3122 if (frp->fr_width + extra_cols < w)
3123 {
3124 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003125 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
3127 else
3128 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003129 frame_new_width(frp, frp->fr_width + extra_cols,
3130 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 break;
3132 }
3133 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003134 {
3135 do
3136 frp = frp->fr_next;
3137 while (wfw && frp != NULL && frame_fixed_width(frp));
3138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003140 {
3141 do
3142 frp = frp->fr_prev;
3143 while (wfw && frp != NULL && frame_fixed_width(frp));
3144 }
3145 /* Increase "width" if we could not reduce enough frames. */
3146 if (frp == NULL)
3147 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 }
3149 }
3150 else if (extra_cols > 0)
3151 {
3152 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003153 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
3155 }
3156 topfrp->fr_width = width;
3157}
3158
3159/*
3160 * Add the vertical separator to windows at the right side of "frp".
3161 * Note: Does not check if there is room!
3162 */
3163 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003164frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 win_T *wp;
3167
3168 if (frp->fr_layout == FR_LEAF)
3169 {
3170 wp = frp->fr_win;
3171 if (wp->w_vsep_width == 0)
3172 {
3173 if (wp->w_width > 0) /* don't make it negative */
3174 --wp->w_width;
3175 wp->w_vsep_width = 1;
3176 }
3177 }
3178 else if (frp->fr_layout == FR_COL)
3179 {
3180 /* Handle all the frames in the column. */
3181 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3182 frame_add_vsep(frp);
3183 }
3184 else /* frp->fr_layout == FR_ROW */
3185 {
3186 /* Only need to handle the last frame in the row. */
3187 frp = frp->fr_child;
3188 while (frp->fr_next != NULL)
3189 frp = frp->fr_next;
3190 frame_add_vsep(frp);
3191 }
3192}
3193
3194/*
3195 * Set frame width from the window it contains.
3196 */
3197 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003198frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199{
3200 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3201}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
3203/*
3204 * Set frame height from the window it contains.
3205 */
3206 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003207frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003209 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210}
3211
3212/*
3213 * Compute the minimal height for frame "topfrp".
3214 * Uses the 'winminheight' option.
3215 * When "next_curwin" isn't NULL, use p_wh for this window.
3216 * When "next_curwin" is NOWIN, don't use at least one line for the current
3217 * window.
3218 */
3219 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003220frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 frame_T *frp;
3223 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225
3226 if (topfrp->fr_win != NULL)
3227 {
3228 if (topfrp->fr_win == next_curwin)
3229 m = p_wh + topfrp->fr_win->w_status_height;
3230 else
3231 {
3232 /* window: minimal height of the window plus status line */
3233 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003234 if (topfrp->fr_win == curwin && next_curwin == NULL)
3235 {
3236 /* Current window is minimal one line high and WinBar is
3237 * visible. */
3238 if (p_wmh == 0)
3239 ++m;
3240 m += WINBAR_HEIGHT(curwin);
3241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 }
3243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 else if (topfrp->fr_layout == FR_ROW)
3245 {
3246 /* get the minimal height from each frame in this row */
3247 m = 0;
3248 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3249 {
3250 n = frame_minheight(frp, next_curwin);
3251 if (n > m)
3252 m = n;
3253 }
3254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 else
3256 {
3257 /* Add up the minimal heights for all frames in this column. */
3258 m = 0;
3259 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3260 m += frame_minheight(frp, next_curwin);
3261 }
3262
3263 return m;
3264}
3265
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266/*
3267 * Compute the minimal width for frame "topfrp".
3268 * When "next_curwin" isn't NULL, use p_wiw for this window.
3269 * When "next_curwin" is NOWIN, don't use at least one column for the current
3270 * window.
3271 */
3272 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003273frame_minwidth(
3274 frame_T *topfrp,
3275 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 frame_T *frp;
3278 int m, n;
3279
3280 if (topfrp->fr_win != NULL)
3281 {
3282 if (topfrp->fr_win == next_curwin)
3283 m = p_wiw + topfrp->fr_win->w_vsep_width;
3284 else
3285 {
3286 /* window: minimal width of the window plus separator column */
3287 m = p_wmw + topfrp->fr_win->w_vsep_width;
3288 /* Current window is minimal one column wide */
3289 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3290 ++m;
3291 }
3292 }
3293 else if (topfrp->fr_layout == FR_COL)
3294 {
3295 /* get the minimal width from each frame in this column */
3296 m = 0;
3297 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3298 {
3299 n = frame_minwidth(frp, next_curwin);
3300 if (n > m)
3301 m = n;
3302 }
3303 }
3304 else
3305 {
3306 /* Add up the minimal widths for all frames in this row. */
3307 m = 0;
3308 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3309 m += frame_minwidth(frp, next_curwin);
3310 }
3311
3312 return m;
3313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
3315
3316/*
3317 * Try to close all windows except current one.
3318 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3319 * used and the buffer was modified.
3320 *
3321 * Used by ":bdel" and ":only".
3322 */
3323 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003324close_others(
3325 int message,
3326 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 win_T *wp;
3329 win_T *nextwp;
3330 int r;
3331
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003332 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
3334 if (message
3335#ifdef FEAT_AUTOCMD
3336 && !autocmd_busy
3337#endif
3338 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003339 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 return;
3341 }
3342
3343 /* Be very careful here: autocommands may change the window layout. */
3344 for (wp = firstwin; win_valid(wp); wp = nextwp)
3345 {
3346 nextwp = wp->w_next;
3347 if (wp != curwin) /* don't close current window */
3348 {
3349
3350 /* Check if it's allowed to abandon this window */
3351 r = can_abandon(wp->w_buffer, forceit);
3352#ifdef FEAT_AUTOCMD
3353 if (!win_valid(wp)) /* autocommands messed wp up */
3354 {
3355 nextwp = firstwin;
3356 continue;
3357 }
3358#endif
3359 if (!r)
3360 {
3361#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3362 if (message && (p_confirm || cmdmod.confirm) && p_write)
3363 {
3364 dialog_changed(wp->w_buffer, FALSE);
3365# ifdef FEAT_AUTOCMD
3366 if (!win_valid(wp)) /* autocommands messed wp up */
3367 {
3368 nextwp = firstwin;
3369 continue;
3370 }
3371# endif
3372 }
3373 if (bufIsChanged(wp->w_buffer))
3374#endif
3375 continue;
3376 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003377 win_close(wp, !buf_hide(wp->w_buffer)
3378 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
3380 }
3381
Bram Moolenaar459ca562016-11-10 18:16:33 +01003382 if (message && !ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 EMSG(_("E445: Other window contains changes"));
3384}
3385
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003387 * Init the current window "curwin".
3388 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 */
3390 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003391curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003393 win_init_empty(curwin);
3394}
3395
3396 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003397win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003398{
3399 redraw_win_later(wp, NOT_VALID);
3400 wp->w_lines_valid = 0;
3401 wp->w_cursor.lnum = 1;
3402 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003404 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003406 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3407 wp->w_pcmark.col = 0;
3408 wp->w_prev_pcmark.lnum = 0;
3409 wp->w_prev_pcmark.col = 0;
3410 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003412 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003414 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003416 if (wp->w_p_rl)
3417 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003419 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003421#ifdef FEAT_SYN_HL
3422 wp->w_s = &wp->w_buffer->b_s;
3423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424}
3425
3426/*
3427 * Allocate the first window and put an empty buffer in it.
3428 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003429 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003431 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003432win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003434 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003435 return FAIL;
3436
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003437 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003438 if (first_tabpage == NULL)
3439 return FAIL;
3440 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003441 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003442
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003443 return OK;
3444}
3445
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003446#if defined(FEAT_AUTOCMD) || defined(PROTO)
3447/*
3448 * Init "aucmd_win". This can only be done after the first
3449 * window is fully initialized, thus it can't be in win_alloc_first().
3450 */
3451 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003452win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003453{
3454 aucmd_win = win_alloc(NULL, TRUE);
3455 if (aucmd_win != NULL)
3456 {
3457 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003458 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003459 new_frame(aucmd_win);
3460 }
3461}
3462#endif
3463
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003464/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003465 * Allocate the first window or the first window in a new tab page.
3466 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003467 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003468 * Return FAIL when something goes wrong (out of memory).
3469 */
3470 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003471win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003472{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003473 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003474 if (oldwin == NULL)
3475 {
3476 /* Very first window, need to create an empty buffer for it and
3477 * initialize from scratch. */
3478 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3479 if (curwin == NULL || curbuf == NULL)
3480 return FAIL;
3481 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003482#ifdef FEAT_SYN_HL
3483 curwin->w_s = &(curbuf->b_s);
3484#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003485 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003486 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003487 curwin_init(); /* init current window */
3488 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003489 else
3490 {
3491 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003492 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003493
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003494 /* We don't want cursor- and scroll-binding in the first window. */
3495 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003498 new_frame(curwin);
3499 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003500 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003501 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003504
3505 return OK;
3506}
3507
3508/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003509 * Create a frame for window "wp".
3510 */
3511 static void
3512new_frame(win_T *wp)
3513{
3514 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3515
3516 wp->w_frame = frp;
3517 if (frp != NULL)
3518 {
3519 frp->fr_layout = FR_LEAF;
3520 frp->fr_win = wp;
3521 }
3522}
3523
3524/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003525 * Initialize the window and frame size to the maximum.
3526 */
3527 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003528win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003529{
3530 firstwin->w_height = ROWS_AVAIL;
3531 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003532 firstwin->w_width = Columns;
3533 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534}
3535
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003536/*
3537 * Allocate a new tabpage_T and init the values.
3538 * Returns NULL when out of memory.
3539 */
3540 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003541alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003542{
3543 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003544# ifdef FEAT_GUI
3545 int i;
3546# endif
3547
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003548
3549 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003550 if (tp == NULL)
3551 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003552
Bram Moolenaar429fa852013-04-15 12:27:36 +02003553# ifdef FEAT_EVAL
3554 /* init t: variables */
3555 tp->tp_vars = dict_alloc();
3556 if (tp->tp_vars == NULL)
3557 {
3558 vim_free(tp);
3559 return NULL;
3560 }
3561 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3562# endif
3563
3564# ifdef FEAT_GUI
3565 for (i = 0; i < 3; i++)
3566 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003567# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003568# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003569 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003570# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003571 tp->tp_ch_used = p_ch;
3572
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003573 return tp;
3574}
3575
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003576 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003577free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003578{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003579 int idx;
3580
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003581# ifdef FEAT_DIFF
3582 diff_clear(tp);
3583# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003584 for (idx = 0; idx < SNAP_COUNT; ++idx)
3585 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003586#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003587 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3588 hash_init(&tp->tp_vars->dv_hashtab);
3589 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003590#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003591
3592#ifdef FEAT_PYTHON
3593 python_tabpage_free(tp);
3594#endif
3595
3596#ifdef FEAT_PYTHON3
3597 python3_tabpage_free(tp);
3598#endif
3599
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003600 vim_free(tp);
3601}
3602
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003603/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003604 * Create a new Tab page with one window.
3605 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003606 * When "after" is 0 put it just after the current Tab page.
3607 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003608 * Return FAIL or OK.
3609 */
3610 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003611win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003612{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003613 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003614 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003615 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003616
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003617 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003618 if (newtp == NULL)
3619 return FAIL;
3620
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003621 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003622 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003623 {
3624 vim_free(newtp);
3625 return FAIL;
3626 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003627 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003628
3629 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003630 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003631 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003632 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003633 if (after == 1)
3634 {
3635 /* New tab page becomes the first one. */
3636 newtp->tp_next = first_tabpage;
3637 first_tabpage = newtp;
3638 }
3639 else
3640 {
3641 if (after > 0)
3642 {
3643 /* Put new tab page before tab page "after". */
3644 n = 2;
3645 for (tp = first_tabpage; tp->tp_next != NULL
3646 && n < after; tp = tp->tp_next)
3647 ++n;
3648 }
3649 newtp->tp_next = tp->tp_next;
3650 tp->tp_next = newtp;
3651 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003652 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003653 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003654 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003655
3656 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003657 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003658
3659#if defined(FEAT_GUI)
3660 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3661 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003662 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003663#endif
3664
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003665 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003666#ifdef FEAT_AUTOCMD
Bram Moolenaarc917da42016-07-19 22:31:36 +02003667 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003668 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003669 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003670 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003671#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003672 return OK;
3673 }
3674
3675 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003676 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003677 return FAIL;
3678}
3679
3680/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003681 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3682 * like with ":split".
3683 * Returns OK if a new tab page was created, FAIL otherwise.
3684 */
3685 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003686may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003687{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003688 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003689
Bram Moolenaard326ce82007-03-11 14:48:29 +00003690 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003691 {
3692 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003693 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003694 return win_new_tabpage(n);
3695 }
3696 return FAIL;
3697}
3698
3699/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003700 * Create up to "maxcount" tabpages with empty windows.
3701 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003702 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003703 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003704make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003706 int count = maxcount;
3707 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003708
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003709 /* Limit to 'tabpagemax' tabs. */
3710 if (count > p_tpm)
3711 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003712
3713#ifdef FEAT_AUTOCMD
3714 /*
3715 * Don't execute autocommands while creating the tab pages. Must do that
3716 * when putting the buffers in the windows.
3717 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003718 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003719#endif
3720
3721 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003722 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003723 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003724
3725#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003726 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003727#endif
3728
3729 /* return actual number of tab pages */
3730 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003731}
3732
3733/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003734 * Return TRUE when "tpc" points to a valid tab page.
3735 */
3736 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003737valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003738{
3739 tabpage_T *tp;
3740
Bram Moolenaar29323592016-07-24 22:04:11 +02003741 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003742 if (tp == tpc)
3743 return TRUE;
3744 return FALSE;
3745}
3746
3747/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003748 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3749 * valid.
3750 */
3751 int
3752valid_tabpage_win(tabpage_T *tpc)
3753{
3754 tabpage_T *tp;
3755 win_T *wp;
3756
3757 FOR_ALL_TABPAGES(tp)
3758 {
3759 if (tp == tpc)
3760 {
3761 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3762 {
3763 if (win_valid_any_tab(wp))
3764 return TRUE;
3765 }
3766 return FALSE;
3767 }
3768 }
3769 /* shouldn't happen */
3770 return FALSE;
3771}
3772
3773/*
3774 * Close tabpage "tab", assuming it has no windows in it.
3775 * There must be another tabpage or this will crash.
3776 */
3777 void
3778close_tabpage(tabpage_T *tab)
3779{
3780 tabpage_T *ptp;
3781
3782 if (tab == first_tabpage)
3783 {
3784 first_tabpage = tab->tp_next;
3785 ptp = first_tabpage;
3786 }
3787 else
3788 {
3789 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3790 ptp = ptp->tp_next)
3791 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003792 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003793 ptp->tp_next = tab->tp_next;
3794 }
3795
3796 goto_tabpage_tp(ptp, FALSE, FALSE);
3797 free_tabpage(tab);
3798}
3799
3800/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003801 * Find tab page "n" (first one is 1). Returns NULL when not found.
3802 */
3803 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003804find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003805{
3806 tabpage_T *tp;
3807 int i = 1;
3808
3809 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3810 ++i;
3811 return tp;
3812}
3813
3814/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003815 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003816 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003817 */
3818 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003819tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003820{
3821 int i = 1;
3822 tabpage_T *tp;
3823
3824 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3825 ++i;
3826 return i;
3827}
3828
3829/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003830 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003831 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003832 * FAIL.
3833 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003834 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003835 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003836leave_tabpage(
3837 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003838 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003839 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003840{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003841 tabpage_T *tp = curtab;
3842
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003843 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003844#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003845 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003846 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003847 if (new_curbuf != curbuf)
3848 {
3849 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3850 if (curtab != tp)
3851 return FAIL;
3852 }
3853 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3854 if (curtab != tp)
3855 return FAIL;
3856 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003857 if (curtab != tp)
3858 return FAIL;
3859 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003860#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003861#if defined(FEAT_GUI)
3862 /* Remove the scrollbars. They may be added back later. */
3863 if (gui.in_use)
3864 gui_remove_scrollbars();
3865#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003866 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003867 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003868 tp->tp_firstwin = firstwin;
3869 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003870 tp->tp_old_Rows = Rows;
3871 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003872 firstwin = NULL;
3873 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003874 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003875}
3876
3877/*
3878 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003879 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003880 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3881 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003882 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003883 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003884enter_tabpage(
3885 tabpage_T *tp,
3886 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003887 int trigger_enter_autocmds,
3888 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003889{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003890 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003891 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003892
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003893 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003894 firstwin = tp->tp_firstwin;
3895 lastwin = tp->tp_lastwin;
3896 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003897
3898 /* We would like doing the TabEnter event first, but we don't have a
3899 * valid current window yet, which may break some commands.
3900 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003901 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003902 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003903 prevwin = next_prevwin;
3904
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003905 last_status(FALSE); /* status line may appear or disappear */
3906 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003907 must_redraw = CLEAR; /* need to redraw everything */
3908#ifdef FEAT_DIFF
3909 diff_need_scrollbind = TRUE;
3910#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003911
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003912 /* The tabpage line may have appeared or disappeared, may need to resize
3913 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003914 * frame sizes too. Use the stored value of p_ch, so that it can be
3915 * different for each tab page. */
3916 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003917 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3918#ifdef FEAT_GUI_TABLINE
3919 && !gui_use_tabline()
3920#endif
3921 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003922 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003923 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003924 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003925
3926#if defined(FEAT_GUI)
3927 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3928 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003929 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003930#endif
3931
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003932#ifdef FEAT_AUTOCMD
3933 /* Apply autocommands after updating the display, when 'rows' and
3934 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003935 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003936 {
3937 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3938 if (old_curbuf != curbuf)
3939 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3940 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003941#endif
3942
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003943 redraw_all_later(CLEAR);
3944}
3945
3946/*
3947 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003948 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003949 */
3950 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003951goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003952{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003953 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003954 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003955 int i;
3956
Bram Moolenaard68071d2006-05-02 22:08:30 +00003957 if (text_locked())
3958 {
3959 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02003960 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00003961 return;
3962 }
3963
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003964 /* If there is only one it can't work. */
3965 if (first_tabpage->tp_next == NULL)
3966 {
3967 if (n > 1)
3968 beep_flush();
3969 return;
3970 }
3971
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003972 if (n == 0)
3973 {
3974 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003975 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003976 tp = first_tabpage;
3977 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003978 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003979 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003980 else if (n < 0)
3981 {
3982 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3983 * this N times. */
3984 ttp = curtab;
3985 for (i = n; i < 0; ++i)
3986 {
3987 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3988 tp = tp->tp_next)
3989 ;
3990 ttp = tp;
3991 }
3992 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003993 else if (n == 9999)
3994 {
3995 /* Go to last tab page. */
3996 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3997 ;
3998 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003999 else
4000 {
4001 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004002 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004003 if (tp == NULL)
4004 {
4005 beep_flush();
4006 return;
4007 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004008 }
4009
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004010 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004011
4012#ifdef FEAT_GUI_TABLINE
4013 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004014 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004015#endif
4016}
4017
4018/*
4019 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004020 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4021 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004022 * Note: doesn't update the GUI tab.
4023 */
4024 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004025goto_tabpage_tp(
4026 tabpage_T *tp,
4027 int trigger_enter_autocmds,
4028 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004029{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004030 /* Don't repeat a message in another tab page. */
4031 set_keep_msg(NULL, 0);
4032
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004033 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4034 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004035 {
4036 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004037 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4038 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004039 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004040 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4041 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004042 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004046 * Enter window "wp" in tab page "tp".
4047 * Also updates the GUI tab.
4048 */
4049 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004050goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004051{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004052 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004053 if (curtab == tp && win_valid(wp))
4054 {
4055 win_enter(wp, TRUE);
4056# ifdef FEAT_GUI_TABLINE
4057 if (gui_use_tabline())
4058 gui_mch_set_curtab(tabpage_index(curtab));
4059# endif
4060 }
4061}
4062
4063/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004064 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004065 */
4066 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004067tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004068{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004069 int n = 1;
4070 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004071
4072 if (first_tabpage->tp_next == NULL)
4073 return;
4074
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004075 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4076 ++n;
4077
4078 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4079 && tp->tp_next == curtab))
4080 return;
4081
4082 tp_dst = tp;
4083
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004084 /* Remove the current tab page from the list of tab pages. */
4085 if (curtab == first_tabpage)
4086 first_tabpage = curtab->tp_next;
4087 else
4088 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004089 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004090 if (tp->tp_next == curtab)
4091 break;
4092 if (tp == NULL) /* "cannot happen" */
4093 return;
4094 tp->tp_next = curtab->tp_next;
4095 }
4096
4097 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004098 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004099 {
4100 curtab->tp_next = first_tabpage;
4101 first_tabpage = curtab;
4102 }
4103 else
4104 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004105 curtab->tp_next = tp_dst->tp_next;
4106 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004107 }
4108
4109 /* Need to redraw the tabline. Tab page contents doesn't change. */
4110 redraw_tabline = TRUE;
4111}
4112
4113
4114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * Go to another window.
4116 * When jumping to another buffer, stop Visual mode. Do this before
4117 * changing windows so we can yank the selection into the '*' register.
4118 * When jumping to another window on the same buffer, adjust its cursor
4119 * position to keep the same Visual area.
4120 */
4121 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004122win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004124#ifdef FEAT_CONCEAL
4125 win_T *owp = curwin;
4126#endif
4127
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004128 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004131 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return;
4133 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004134#ifdef FEAT_AUTOCMD
4135 if (curbuf_locked())
4136 return;
4137#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (wp->w_buffer != curbuf)
4140 reset_VIsual_and_resel();
4141 else if (VIsual_active)
4142 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
4144#ifdef FEAT_GUI
4145 need_mouse_correct = TRUE;
4146#endif
4147 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004148
4149#ifdef FEAT_CONCEAL
4150 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004151 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004152 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004153 if (curwin->w_p_cole > 0 && !msg_scrolled)
4154 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156}
4157
4158#if defined(FEAT_PERL) || defined(PROTO)
4159/*
4160 * Find window number "winnr" (counting top to bottom).
4161 */
4162 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004163win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164{
4165 win_T *wp;
4166
Bram Moolenaar29323592016-07-24 22:04:11 +02004167 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 if (--winnr == 0)
4169 break;
4170 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171}
4172#endif
4173
Bram Moolenaar4033c552017-09-16 20:54:51 +02004174#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004175/*
4176 * Find the tabpage for window "win".
4177 */
4178 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004179win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004180{
4181 win_T *wp;
4182 tabpage_T *tp;
4183
Bram Moolenaar29323592016-07-24 22:04:11 +02004184 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004185 if (wp == win)
4186 return tp;
4187 return NULL;
4188}
4189#endif
4190
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191/*
4192 * Move to window above or below "count" times.
4193 */
4194 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004195win_goto_ver(
4196 int up, /* TRUE to go to win above */
4197 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
4199 frame_T *fr;
4200 frame_T *nfr;
4201 frame_T *foundfr;
4202
4203 foundfr = curwin->w_frame;
4204 while (count--)
4205 {
4206 /*
4207 * First go upwards in the tree of frames until we find a upwards or
4208 * downwards neighbor.
4209 */
4210 fr = foundfr;
4211 for (;;)
4212 {
4213 if (fr == topframe)
4214 goto end;
4215 if (up)
4216 nfr = fr->fr_prev;
4217 else
4218 nfr = fr->fr_next;
4219 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4220 break;
4221 fr = fr->fr_parent;
4222 }
4223
4224 /*
4225 * Now go downwards to find the bottom or top frame in it.
4226 */
4227 for (;;)
4228 {
4229 if (nfr->fr_layout == FR_LEAF)
4230 {
4231 foundfr = nfr;
4232 break;
4233 }
4234 fr = nfr->fr_child;
4235 if (nfr->fr_layout == FR_ROW)
4236 {
4237 /* Find the frame at the cursor row. */
4238 while (fr->fr_next != NULL
4239 && frame2win(fr)->w_wincol + fr->fr_width
4240 <= curwin->w_wincol + curwin->w_wcol)
4241 fr = fr->fr_next;
4242 }
4243 if (nfr->fr_layout == FR_COL && up)
4244 while (fr->fr_next != NULL)
4245 fr = fr->fr_next;
4246 nfr = fr;
4247 }
4248 }
4249end:
4250 if (foundfr != NULL)
4251 win_goto(foundfr->fr_win);
4252}
4253
4254/*
4255 * Move to left or right window.
4256 */
4257 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004258win_goto_hor(
4259 int left, /* TRUE to go to left win */
4260 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 frame_T *fr;
4263 frame_T *nfr;
4264 frame_T *foundfr;
4265
4266 foundfr = curwin->w_frame;
4267 while (count--)
4268 {
4269 /*
4270 * First go upwards in the tree of frames until we find a left or
4271 * right neighbor.
4272 */
4273 fr = foundfr;
4274 for (;;)
4275 {
4276 if (fr == topframe)
4277 goto end;
4278 if (left)
4279 nfr = fr->fr_prev;
4280 else
4281 nfr = fr->fr_next;
4282 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4283 break;
4284 fr = fr->fr_parent;
4285 }
4286
4287 /*
4288 * Now go downwards to find the leftmost or rightmost frame in it.
4289 */
4290 for (;;)
4291 {
4292 if (nfr->fr_layout == FR_LEAF)
4293 {
4294 foundfr = nfr;
4295 break;
4296 }
4297 fr = nfr->fr_child;
4298 if (nfr->fr_layout == FR_COL)
4299 {
4300 /* Find the frame at the cursor row. */
4301 while (fr->fr_next != NULL
4302 && frame2win(fr)->w_winrow + fr->fr_height
4303 <= curwin->w_winrow + curwin->w_wrow)
4304 fr = fr->fr_next;
4305 }
4306 if (nfr->fr_layout == FR_ROW && left)
4307 while (fr->fr_next != NULL)
4308 fr = fr->fr_next;
4309 nfr = fr;
4310 }
4311 }
4312end:
4313 if (foundfr != NULL)
4314 win_goto(foundfr->fr_win);
4315}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317/*
4318 * Make window "wp" the current window.
4319 */
4320 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004321win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004323 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324}
4325
4326/*
4327 * Make window wp the current window.
4328 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4329 * been closed and isn't valid.
4330 */
4331 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004332win_enter_ext(
4333 win_T *wp,
4334 int undo_sync,
4335 int curwin_invalid,
Bram Moolenaarc917da42016-07-19 22:31:36 +02004336 int trigger_new_autocmds UNUSED,
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004337 int trigger_enter_autocmds UNUSED,
4338 int trigger_leave_autocmds UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339{
4340#ifdef FEAT_AUTOCMD
4341 int other_buffer = FALSE;
4342#endif
4343
4344 if (wp == curwin && !curwin_invalid) /* nothing to do */
4345 return;
4346
4347#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004348 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
4350 /*
4351 * Be careful: If autocommands delete the window, return now.
4352 */
4353 if (wp->w_buffer != curbuf)
4354 {
4355 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4356 other_buffer = TRUE;
4357 if (!win_valid(wp))
4358 return;
4359 }
4360 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4361 if (!win_valid(wp))
4362 return;
4363# ifdef FEAT_EVAL
4364 /* autocmds may abort script processing */
4365 if (aborting())
4366 return;
4367# endif
4368 }
4369#endif
4370
4371 /* sync undo before leaving the current buffer */
4372 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004373 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004374
4375 /* Might need to scroll the old window before switching, e.g., when the
4376 * cursor was moved. */
4377 update_topline();
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /* may have to copy the buffer options when 'cpo' contains 'S' */
4380 if (wp->w_buffer != curbuf)
4381 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4382 if (!curwin_invalid)
4383 {
4384 prevwin = curwin; /* remember for CTRL-W p */
4385 curwin->w_redr_status = TRUE;
4386 }
4387 curwin = wp;
4388 curbuf = wp->w_buffer;
4389 check_cursor();
4390#ifdef FEAT_VIRTUALEDIT
4391 if (!virtual_active())
4392 curwin->w_cursor.coladd = 0;
4393#endif
4394 changed_line_abv_curs(); /* assume cursor position needs updating */
4395
4396 if (curwin->w_localdir != NULL)
4397 {
4398 /* Window has a local directory: Save current directory as global
4399 * directory (unless that was done already) and change to the local
4400 * directory. */
4401 if (globaldir == NULL)
4402 {
4403 char_u cwd[MAXPATHL];
4404
4405 if (mch_dirname(cwd, MAXPATHL) == OK)
4406 globaldir = vim_strsave(cwd);
4407 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004408 if (mch_chdir((char *)curwin->w_localdir) == 0)
4409 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 else if (globaldir != NULL)
4412 {
4413 /* Window doesn't have a local directory and we are not in the global
4414 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004415 ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004416 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 shorten_fnames(TRUE);
4418 }
4419
4420#ifdef FEAT_AUTOCMD
Bram Moolenaarc917da42016-07-19 22:31:36 +02004421 if (trigger_new_autocmds)
4422 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004423 if (trigger_enter_autocmds)
4424 {
4425 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4426 if (other_buffer)
4427 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429#endif
4430
4431#ifdef FEAT_TITLE
4432 maketitle();
4433#endif
4434 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004435 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 if (restart_edit)
4437 redraw_later(VALID); /* causes status line redraw */
4438
4439 /* set window height to desired minimal value */
4440 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4441 win_setheight((int)p_wh);
4442 else if (curwin->w_height == 0)
4443 win_setheight(1);
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004446 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449#ifdef FEAT_MOUSE
4450 setmouse(); /* in case jumped to/from help buffer */
4451#endif
4452
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004453 /* Change directories when the 'acd' option is set. */
4454 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455}
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004459 * Jump to the first open window that contains buffer "buf", if one exists.
4460 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
4462 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004463buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004465 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004467 if (curwin->w_buffer == buf)
4468 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004469 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004470 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004471 if (wp->w_buffer == buf)
4472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (wp != NULL)
4474 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004475 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004477
4478/*
4479 * Jump to the first open window in any tab page that contains buffer "buf",
4480 * if one exists.
4481 * Returns a pointer to the window found, otherwise NULL.
4482 */
4483 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004484buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004485{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004486 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004487 tabpage_T *tp;
4488
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004489 if (wp != NULL)
4490 return wp;
4491
Bram Moolenaar29323592016-07-24 22:04:11 +02004492 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004493 if (tp != curtab)
4494 {
4495 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4496 if (wp->w_buffer == buf)
4497 break;
4498 if (wp != NULL)
4499 {
4500 goto_tabpage_win(tp, wp);
4501 if (curwin != wp)
4502 wp = NULL; /* something went wrong */
4503 break;
4504 }
4505 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004506 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004507}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004509static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004510
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004512 * Allocate a window structure and link it in the window list when "hidden" is
4513 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004516win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004518 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
4520 /*
4521 * allocate window structure and linesizes arrays
4522 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004523 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004524 if (new_wp == NULL)
4525 return NULL;
4526
4527 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004529 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004530 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 }
4532
Bram Moolenaar86edef62016-03-13 18:07:30 +01004533 new_wp->w_id = ++last_win_id;
4534
Bram Moolenaar429fa852013-04-15 12:27:36 +02004535#ifdef FEAT_EVAL
4536 /* init w: variables */
4537 new_wp->w_vars = dict_alloc();
4538 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004540 win_free_lsize(new_wp);
4541 vim_free(new_wp);
4542 return NULL;
4543 }
4544 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004545#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004546
4547#ifdef FEAT_AUTOCMD
4548 /* Don't execute autocommands while the window is not properly
4549 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4550 * event. */
4551 block_autocmds();
4552#endif
4553 /*
4554 * link the window in the window list
4555 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004556 if (!hidden)
4557 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004558 new_wp->w_wincol = 0;
4559 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
Bram Moolenaar429fa852013-04-15 12:27:36 +02004561 /* position the display and the cursor at the top of the file. */
4562 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004564 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004566 new_wp->w_botline = 2;
4567 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004569 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570#endif
4571
Bram Moolenaar429fa852013-04-15 12:27:36 +02004572 /* We won't calculate w_fraction until resizing the window */
4573 new_wp->w_fraction = 0;
4574 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575
4576#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004577 if (gui.in_use)
4578 {
4579 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4580 SBAR_LEFT, new_wp);
4581 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4582 SBAR_RIGHT, new_wp);
4583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#endif
4585#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004586 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004588#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004589 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004590#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004591#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004592 new_wp->w_match_head = NULL;
4593 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004594#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004595 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596}
4597
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004599 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 */
4601 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004602win_free(
4603 win_T *wp,
4604 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004607 buf_T *buf;
4608 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004610#ifdef FEAT_FOLDING
4611 clearFolding(wp);
4612#endif
4613
4614 /* reduce the reference count to the argument list. */
4615 alist_unlink(wp->w_alist);
4616
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004617#ifdef FEAT_AUTOCMD
4618 /* Don't execute autocommands while the window is halfway being deleted.
4619 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004620 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004621#endif
4622
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004623#ifdef FEAT_LUA
4624 lua_window_free(wp);
4625#endif
4626
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004627#ifdef FEAT_MZSCHEME
4628 mzscheme_window_free(wp);
4629#endif
4630
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631#ifdef FEAT_PERL
4632 perl_win_free(wp);
4633#endif
4634
4635#ifdef FEAT_PYTHON
4636 python_window_free(wp);
4637#endif
4638
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004639#ifdef FEAT_PYTHON3
4640 python3_window_free(wp);
4641#endif
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#ifdef FEAT_TCL
4644 tcl_window_free(wp);
4645#endif
4646
4647#ifdef FEAT_RUBY
4648 ruby_window_free(wp);
4649#endif
4650
4651 clear_winopt(&wp->w_onebuf_opt);
4652 clear_winopt(&wp->w_allbuf_opt);
4653
4654#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004655 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4656 hash_init(&wp->w_vars->dv_hashtab);
4657 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#endif
4659
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004660 {
4661 tabpage_T *ttp;
4662
4663 if (prevwin == wp)
4664 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004665 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004666 if (ttp->tp_prevwin == wp)
4667 ttp->tp_prevwin = NULL;
4668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 win_free_lsize(wp);
4670
4671 for (i = 0; i < wp->w_tagstacklen; ++i)
4672 vim_free(wp->w_tagstack[i].tagname);
4673
4674 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004675
Bram Moolenaarff18df02013-07-24 17:51:57 +02004676 /* Remove the window from the b_wininfo lists, it may happen that the
4677 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004678 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004679 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4680 if (wip->wi_win == wp)
4681 wip->wi_win = NULL;
4682
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004684 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#ifdef FEAT_JUMPLIST
4688 free_jumplist(wp);
4689#endif
4690
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004691#ifdef FEAT_QUICKFIX
4692 qf_free_all(wp);
4693#endif
4694
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695#ifdef FEAT_GUI
4696 if (gui.in_use)
4697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4699 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4700 }
4701#endif /* FEAT_GUI */
4702
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004703#ifdef FEAT_MENU
4704 remove_winbar(wp);
4705#endif
4706
Bram Moolenaar860cae12010-06-05 23:22:07 +02004707#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004708 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004709#endif
4710
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004711#ifdef FEAT_AUTOCMD
4712 if (wp != aucmd_win)
4713#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004714 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004715#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004716 if (autocmd_busy)
4717 {
4718 wp->w_next = au_pending_free_win;
4719 au_pending_free_win = wp;
4720 }
4721 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004722#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004723 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004724
4725#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004726 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728}
4729
4730/*
4731 * Append window "wp" in the window list after window "after".
4732 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004733 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004734win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 win_T *before;
4737
4738 if (after == NULL) /* after NULL is in front of the first */
4739 before = firstwin;
4740 else
4741 before = after->w_next;
4742
4743 wp->w_next = before;
4744 wp->w_prev = after;
4745 if (after == NULL)
4746 firstwin = wp;
4747 else
4748 after->w_next = wp;
4749 if (before == NULL)
4750 lastwin = wp;
4751 else
4752 before->w_prev = wp;
4753}
4754
4755/*
4756 * Remove a window from the window list.
4757 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004758 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004759win_remove(
4760 win_T *wp,
4761 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 if (wp->w_prev != NULL)
4764 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004765 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004766 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004767 else
4768 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004769
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (wp->w_next != NULL)
4771 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004772 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004773 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004774 else
4775 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776}
4777
4778/*
4779 * Append frame "frp" in a frame list after frame "after".
4780 */
4781 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004782frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
4784 frp->fr_next = after->fr_next;
4785 after->fr_next = frp;
4786 if (frp->fr_next != NULL)
4787 frp->fr_next->fr_prev = frp;
4788 frp->fr_prev = after;
4789}
4790
4791/*
4792 * Insert frame "frp" in a frame list before frame "before".
4793 */
4794 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004795frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
4797 frp->fr_next = before;
4798 frp->fr_prev = before->fr_prev;
4799 before->fr_prev = frp;
4800 if (frp->fr_prev != NULL)
4801 frp->fr_prev->fr_next = frp;
4802 else
4803 frp->fr_parent->fr_child = frp;
4804}
4805
4806/*
4807 * Remove a frame from a frame list.
4808 */
4809 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004810frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
4812 if (frp->fr_prev != NULL)
4813 frp->fr_prev->fr_next = frp->fr_next;
4814 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004817 /* special case: topframe->fr_child == frp */
4818 if (topframe->fr_child == frp)
4819 topframe->fr_child = frp->fr_next;
4820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (frp->fr_next != NULL)
4822 frp->fr_next->fr_prev = frp->fr_prev;
4823}
4824
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825/*
4826 * Allocate w_lines[] for window "wp".
4827 * Return FAIL for failure, OK for success.
4828 */
4829 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004830win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
4832 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004833 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if (wp->w_lines == NULL)
4835 return FAIL;
4836 return OK;
4837}
4838
4839/*
4840 * free lsize arrays for a window
4841 */
4842 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004843win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004845 /* TODO: why would wp be NULL here? */
4846 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004847 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848}
4849
4850/*
4851 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004852 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 */
4854 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004855shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004857 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 if (firstwin == NULL) /* not initialized yet */
4860 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (h < frame_minheight(topframe, NULL))
4862 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004863
4864 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 * that doesn't result in the right height, forget about that option. */
4866 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004867 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 frame_new_height(topframe, h, FALSE, FALSE);
4869
4870 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004872 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#if 0
4875 /* Disabled: don't want making the screen smaller make a window larger. */
4876 if (p_ea)
4877 win_equal(curwin, FALSE, 'v');
4878#endif
4879}
4880
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881/*
4882 * Called from win_new_shellsize() after Columns changed.
4883 */
4884 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004885shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
4887 if (firstwin == NULL) /* not initialized yet */
4888 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004889
4890 /* First try setting the widths of windows with 'winfixwidth'. If that
4891 * doesn't result in the right width, forget about that option. */
4892 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004893 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004894 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4897#if 0
4898 /* Disabled: don't want making the screen smaller make a window larger. */
4899 if (p_ea)
4900 win_equal(curwin, FALSE, 'h');
4901#endif
4902}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
4904#if defined(FEAT_CMDWIN) || defined(PROTO)
4905/*
4906 * Save the size of all windows in "gap".
4907 */
4908 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004909win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910
4911{
4912 win_T *wp;
4913
4914 ga_init2(gap, (int)sizeof(int), 1);
4915 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004916 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 ((int *)gap->ga_data)[gap->ga_len++] =
4919 wp->w_width + wp->w_vsep_width;
4920 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4921 }
4922}
4923
4924/*
4925 * Restore window sizes, but only if the number of windows is still the same.
4926 * Does not free the growarray.
4927 */
4928 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004929win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
4931 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004932 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934 if (win_count() * 2 == gap->ga_len)
4935 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004936 /* The order matters, because frames contain other frames, but it's
4937 * difficult to get right. The easy way out is to do it twice. */
4938 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004940 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004941 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02004942 {
4943 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4944 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 /* recompute the window positions */
4948 (void)win_comp_pos();
4949 }
4950}
4951#endif /* FEAT_CMDWIN */
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953/*
4954 * Update the position for all windows, using the width and height of the
4955 * frames.
4956 * Returns the row just after the last window.
4957 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004958 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004959win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004961 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 int col = 0;
4963
4964 frame_comp_pos(topframe, &row, &col);
4965 return row;
4966}
4967
4968/*
4969 * Update the position of the windows in frame "topfrp", using the width and
4970 * height of the frames.
4971 * "*row" and "*col" are the top-left position of the frame. They are updated
4972 * to the bottom-right position plus one.
4973 */
4974 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004975frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976{
4977 win_T *wp;
4978 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 int startcol;
4980 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01004981 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 wp = topfrp->fr_win;
4984 if (wp != NULL)
4985 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004986 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
4988 /* position changed, redraw */
4989 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 redraw_win_later(wp, NOT_VALID);
4992 wp->w_redr_status = TRUE;
4993 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01004994 /* WinBar will not show if the window height is zero */
4995 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
4996 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999 else
5000 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 startrow = *row;
5002 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 if (topfrp->fr_layout == FR_ROW)
5006 *row = startrow; /* all frames are at the same row */
5007 else
5008 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 frame_comp_pos(frp, row, col);
5010 }
5011 }
5012}
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014/*
5015 * Set current window height and take care of repositioning other windows to
5016 * fit around it.
5017 */
5018 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005019win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 win_setheight_win(height, curwin);
5022}
5023
5024/*
5025 * Set the window height of window "win" and take care of repositioning other
5026 * windows to fit around it.
5027 */
5028 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005029win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
5031 int row;
5032
5033 if (win == curwin)
5034 {
5035 /* Always keep current window at least one line high, even when
5036 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (height < p_wmh)
5038 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 if (height == 0)
5040 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005041 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 frame_setheight(win->w_frame, height + win->w_status_height);
5045
5046 /* recompute the window positions */
5047 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 /*
5050 * If there is extra space created between the last window and the command
5051 * line, clear it.
5052 */
5053 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5054 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5055 cmdline_row = row;
5056 msg_row = row;
5057 msg_col = 0;
5058
5059 redraw_all_later(NOT_VALID);
5060}
5061
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062/*
5063 * Set the height of a frame to "height" and take care that all frames and
5064 * windows inside it are resized. Also resize frames on the left and right if
5065 * the are in the same FR_ROW frame.
5066 *
5067 * Strategy:
5068 * If the frame is part of a FR_COL frame, try fitting the frame in that
5069 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5070 * go to containing frames to resize them and make room.
5071 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5072 * Check for the minimal height of the FR_ROW frame.
5073 * At the top level we can also use change the command line height.
5074 */
5075 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005076frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078 int room; /* total number of lines available */
5079 int take; /* number of lines taken from other windows */
5080 int room_cmdline; /* lines available from cmdline */
5081 int run;
5082 frame_T *frp;
5083 int h;
5084 int room_reserved;
5085
5086 /* If the height already is the desired value, nothing to do. */
5087 if (curfrp->fr_height == height)
5088 return;
5089
5090 if (curfrp->fr_parent == NULL)
5091 {
5092 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005093 if (height > ROWS_AVAIL)
5094 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 if (height > 0)
5096 frame_new_height(curfrp, height, FALSE, FALSE);
5097 }
5098 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5099 {
5100 /* Row of frames: Also need to resize frames left and right of this
5101 * one. First check for the minimal height of these. */
5102 h = frame_minheight(curfrp->fr_parent, NULL);
5103 if (height < h)
5104 height = h;
5105 frame_setheight(curfrp->fr_parent, height);
5106 }
5107 else
5108 {
5109 /*
5110 * Column of frames: try to change only frames in this column.
5111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 /*
5113 * Do this twice:
5114 * 1: compute room available, if it's not enough try resizing the
5115 * containing frame.
5116 * 2: compute the room available and adjust the height to it.
5117 * Try not to reduce the height of a window with 'winfixheight' set.
5118 */
5119 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 room = 0;
5122 room_reserved = 0;
5123 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5124 frp = frp->fr_next)
5125 {
5126 if (frp != curfrp
5127 && frp->fr_win != NULL
5128 && frp->fr_win->w_p_wfh)
5129 room_reserved += frp->fr_height;
5130 room += frp->fr_height;
5131 if (frp != curfrp)
5132 room -= frame_minheight(frp, NULL);
5133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 if (curfrp->fr_width != Columns)
5135 room_cmdline = 0;
5136 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005139 + VISIBLE_HEIGHT(lastwin)
5140 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 if (room_cmdline < 0)
5142 room_cmdline = 0;
5143 }
5144
5145 if (height <= room + room_cmdline)
5146 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 if (height > room + room_cmdline)
5150 height = room + room_cmdline;
5151 break;
5152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 frame_setheight(curfrp->fr_parent, height
5154 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156
5157 /*
5158 * Compute the number of lines we will take from others frames (can be
5159 * negative!).
5160 */
5161 take = height - curfrp->fr_height;
5162
5163 /* If there is not enough room, also reduce the height of a window
5164 * with 'winfixheight' set. */
5165 if (height > room + room_cmdline - room_reserved)
5166 room_reserved = room + room_cmdline - height;
5167 /* If there is only a 'winfixheight' window and making the
5168 * window smaller, need to make the other window taller. */
5169 if (take < 0 && room - curfrp->fr_height < room_reserved)
5170 room_reserved = 0;
5171
5172 if (take > 0 && room_cmdline > 0)
5173 {
5174 /* use lines from cmdline first */
5175 if (take < room_cmdline)
5176 room_cmdline = take;
5177 take -= room_cmdline;
5178 topframe->fr_height += room_cmdline;
5179 }
5180
5181 /*
5182 * set the current frame to the new height
5183 */
5184 frame_new_height(curfrp, height, FALSE, FALSE);
5185
5186 /*
5187 * First take lines from the frames after the current frame. If
5188 * that is not enough, takes lines from frames above the current
5189 * frame.
5190 */
5191 for (run = 0; run < 2; ++run)
5192 {
5193 if (run == 0)
5194 frp = curfrp->fr_next; /* 1st run: start with next window */
5195 else
5196 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5197 while (frp != NULL && take != 0)
5198 {
5199 h = frame_minheight(frp, NULL);
5200 if (room_reserved > 0
5201 && frp->fr_win != NULL
5202 && frp->fr_win->w_p_wfh)
5203 {
5204 if (room_reserved >= frp->fr_height)
5205 room_reserved -= frp->fr_height;
5206 else
5207 {
5208 if (frp->fr_height - room_reserved > take)
5209 room_reserved = frp->fr_height - take;
5210 take -= frp->fr_height - room_reserved;
5211 frame_new_height(frp, room_reserved, FALSE, FALSE);
5212 room_reserved = 0;
5213 }
5214 }
5215 else
5216 {
5217 if (frp->fr_height - take < h)
5218 {
5219 take -= frp->fr_height - h;
5220 frame_new_height(frp, h, FALSE, FALSE);
5221 }
5222 else
5223 {
5224 frame_new_height(frp, frp->fr_height - take,
5225 FALSE, FALSE);
5226 take = 0;
5227 }
5228 }
5229 if (run == 0)
5230 frp = frp->fr_next;
5231 else
5232 frp = frp->fr_prev;
5233 }
5234 }
5235 }
5236}
5237
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238/*
5239 * Set current window width and take care of repositioning other windows to
5240 * fit around it.
5241 */
5242 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005243win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244{
5245 win_setwidth_win(width, curwin);
5246}
5247
5248 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005249win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
5251 /* Always keep current window at least one column wide, even when
5252 * 'winminwidth' is zero. */
5253 if (wp == curwin)
5254 {
5255 if (width < p_wmw)
5256 width = p_wmw;
5257 if (width == 0)
5258 width = 1;
5259 }
5260
5261 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5262
5263 /* recompute the window positions */
5264 (void)win_comp_pos();
5265
5266 redraw_all_later(NOT_VALID);
5267}
5268
5269/*
5270 * Set the width of a frame to "width" and take care that all frames and
5271 * windows inside it are resized. Also resize frames above and below if the
5272 * are in the same FR_ROW frame.
5273 *
5274 * Strategy is similar to frame_setheight().
5275 */
5276 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005277frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278{
5279 int room; /* total number of lines available */
5280 int take; /* number of lines taken from other windows */
5281 int run;
5282 frame_T *frp;
5283 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005284 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285
5286 /* If the width already is the desired value, nothing to do. */
5287 if (curfrp->fr_width == width)
5288 return;
5289
5290 if (curfrp->fr_parent == NULL)
5291 /* topframe: can't change width */
5292 return;
5293
5294 if (curfrp->fr_parent->fr_layout == FR_COL)
5295 {
5296 /* Column of frames: Also need to resize frames above and below of
5297 * this one. First check for the minimal width of these. */
5298 w = frame_minwidth(curfrp->fr_parent, NULL);
5299 if (width < w)
5300 width = w;
5301 frame_setwidth(curfrp->fr_parent, width);
5302 }
5303 else
5304 {
5305 /*
5306 * Row of frames: try to change only frames in this row.
5307 *
5308 * Do this twice:
5309 * 1: compute room available, if it's not enough try resizing the
5310 * containing frame.
5311 * 2: compute the room available and adjust the width to it.
5312 */
5313 for (run = 1; run <= 2; ++run)
5314 {
5315 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005316 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5318 frp = frp->fr_next)
5319 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005320 if (frp != curfrp
5321 && frp->fr_win != NULL
5322 && frp->fr_win->w_p_wfw)
5323 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 room += frp->fr_width;
5325 if (frp != curfrp)
5326 room -= frame_minwidth(frp, NULL);
5327 }
5328
5329 if (width <= room)
5330 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005331 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
5333 if (width > room)
5334 width = room;
5335 break;
5336 }
5337 frame_setwidth(curfrp->fr_parent, width
5338 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5339 }
5340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 /*
5342 * Compute the number of lines we will take from others frames (can be
5343 * negative!).
5344 */
5345 take = width - curfrp->fr_width;
5346
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005347 /* If there is not enough room, also reduce the width of a window
5348 * with 'winfixwidth' set. */
5349 if (width > room - room_reserved)
5350 room_reserved = room - width;
5351 /* If there is only a 'winfixwidth' window and making the
5352 * window smaller, need to make the other window narrower. */
5353 if (take < 0 && room - curfrp->fr_width < room_reserved)
5354 room_reserved = 0;
5355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 /*
5357 * set the current frame to the new width
5358 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005359 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360
5361 /*
5362 * First take lines from the frames right of the current frame. If
5363 * that is not enough, takes lines from frames left of the current
5364 * frame.
5365 */
5366 for (run = 0; run < 2; ++run)
5367 {
5368 if (run == 0)
5369 frp = curfrp->fr_next; /* 1st run: start with next window */
5370 else
5371 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5372 while (frp != NULL && take != 0)
5373 {
5374 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005375 if (room_reserved > 0
5376 && frp->fr_win != NULL
5377 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005379 if (room_reserved >= frp->fr_width)
5380 room_reserved -= frp->fr_width;
5381 else
5382 {
5383 if (frp->fr_width - room_reserved > take)
5384 room_reserved = frp->fr_width - take;
5385 take -= frp->fr_width - room_reserved;
5386 frame_new_width(frp, room_reserved, FALSE, FALSE);
5387 room_reserved = 0;
5388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 }
5390 else
5391 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005392 if (frp->fr_width - take < w)
5393 {
5394 take -= frp->fr_width - w;
5395 frame_new_width(frp, w, FALSE, FALSE);
5396 }
5397 else
5398 {
5399 frame_new_width(frp, frp->fr_width - take,
5400 FALSE, FALSE);
5401 take = 0;
5402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 }
5404 if (run == 0)
5405 frp = frp->fr_next;
5406 else
5407 frp = frp->fr_prev;
5408 }
5409 }
5410 }
5411}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412
5413/*
5414 * Check 'winminheight' for a valid value.
5415 */
5416 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005417win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
5419 int room;
5420 int first = TRUE;
5421 win_T *wp;
5422
5423 /* loop until there is a 'winminheight' that is possible */
5424 while (p_wmh > 0)
5425 {
5426 /* TODO: handle vertical splits */
5427 room = -p_wh;
Bram Moolenaar29323592016-07-24 22:04:11 +02005428 FOR_ALL_WINDOWS(wp)
Bram Moolenaar415a6932017-12-05 20:31:07 +01005429 room += VISIBLE_HEIGHT(wp) - p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 if (room >= 0)
5431 break;
5432 --p_wmh;
5433 if (first)
5434 {
5435 EMSG(_(e_noroom));
5436 first = FALSE;
5437 }
5438 }
5439}
5440
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005441#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442
5443/*
5444 * Status line of dragwin is dragged "offset" lines down (negative is up).
5445 */
5446 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005447win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448{
5449 frame_T *curfr;
5450 frame_T *fr;
5451 int room;
5452 int row;
5453 int up; /* if TRUE, drag status line up, otherwise down */
5454 int n;
5455
5456 fr = dragwin->w_frame;
5457 curfr = fr;
5458 if (fr != topframe) /* more than one window */
5459 {
5460 fr = fr->fr_parent;
5461 /* When the parent frame is not a column of frames, its parent should
5462 * be. */
5463 if (fr->fr_layout != FR_COL)
5464 {
5465 curfr = fr;
5466 if (fr != topframe) /* only a row of windows, may drag statusline */
5467 fr = fr->fr_parent;
5468 }
5469 }
5470
5471 /* If this is the last frame in a column, may want to resize the parent
5472 * frame instead (go two up to skip a row of frames). */
5473 while (curfr != topframe && curfr->fr_next == NULL)
5474 {
5475 if (fr != topframe)
5476 fr = fr->fr_parent;
5477 curfr = fr;
5478 if (fr != topframe)
5479 fr = fr->fr_parent;
5480 }
5481
5482 if (offset < 0) /* drag up */
5483 {
5484 up = TRUE;
5485 offset = -offset;
5486 /* sum up the room of the current frame and above it */
5487 if (fr == curfr)
5488 {
5489 /* only one window */
5490 room = fr->fr_height - frame_minheight(fr, NULL);
5491 }
5492 else
5493 {
5494 room = 0;
5495 for (fr = fr->fr_child; ; fr = fr->fr_next)
5496 {
5497 room += fr->fr_height - frame_minheight(fr, NULL);
5498 if (fr == curfr)
5499 break;
5500 }
5501 }
5502 fr = curfr->fr_next; /* put fr at frame that grows */
5503 }
5504 else /* drag down */
5505 {
5506 up = FALSE;
5507 /*
5508 * Only dragging the last status line can reduce p_ch.
5509 */
5510 room = Rows - cmdline_row;
5511 if (curfr->fr_next == NULL)
5512 room -= 1;
5513 else
5514 room -= p_ch;
5515 if (room < 0)
5516 room = 0;
5517 /* sum up the room of frames below of the current one */
5518 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5519 room += fr->fr_height - frame_minheight(fr, NULL);
5520 fr = curfr; /* put fr at window that grows */
5521 }
5522
5523 if (room < offset) /* Not enough room */
5524 offset = room; /* Move as far as we can */
5525 if (offset <= 0)
5526 return;
5527
5528 /*
5529 * Grow frame fr by "offset" lines.
5530 * Doesn't happen when dragging the last status line up.
5531 */
5532 if (fr != NULL)
5533 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5534
5535 if (up)
5536 fr = curfr; /* current frame gets smaller */
5537 else
5538 fr = curfr->fr_next; /* next frame gets smaller */
5539
5540 /*
5541 * Now make the other frames smaller.
5542 */
5543 while (fr != NULL && offset > 0)
5544 {
5545 n = frame_minheight(fr, NULL);
5546 if (fr->fr_height - offset <= n)
5547 {
5548 offset -= fr->fr_height - n;
5549 frame_new_height(fr, n, !up, FALSE);
5550 }
5551 else
5552 {
5553 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5554 break;
5555 }
5556 if (up)
5557 fr = fr->fr_prev;
5558 else
5559 fr = fr->fr_next;
5560 }
5561 row = win_comp_pos();
5562 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5563 cmdline_row = row;
5564 p_ch = Rows - cmdline_row;
5565 if (p_ch < 1)
5566 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005567 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005568 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 showmode();
5570}
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572/*
5573 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5574 */
5575 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005576win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 frame_T *curfr;
5579 frame_T *fr;
5580 int room;
5581 int left; /* if TRUE, drag separator line left, otherwise right */
5582 int n;
5583
5584 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005585 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 return;
5587 curfr = fr;
5588 fr = fr->fr_parent;
5589 /* When the parent frame is not a row of frames, its parent should be. */
5590 if (fr->fr_layout != FR_ROW)
5591 {
5592 if (fr == topframe) /* only a column of windows (cannot happen?) */
5593 return;
5594 curfr = fr;
5595 fr = fr->fr_parent;
5596 }
5597
5598 /* If this is the last frame in a row, may want to resize a parent
5599 * frame instead. */
5600 while (curfr->fr_next == NULL)
5601 {
5602 if (fr == topframe)
5603 break;
5604 curfr = fr;
5605 fr = fr->fr_parent;
5606 if (fr != topframe)
5607 {
5608 curfr = fr;
5609 fr = fr->fr_parent;
5610 }
5611 }
5612
5613 if (offset < 0) /* drag left */
5614 {
5615 left = TRUE;
5616 offset = -offset;
5617 /* sum up the room of the current frame and left of it */
5618 room = 0;
5619 for (fr = fr->fr_child; ; fr = fr->fr_next)
5620 {
5621 room += fr->fr_width - frame_minwidth(fr, NULL);
5622 if (fr == curfr)
5623 break;
5624 }
5625 fr = curfr->fr_next; /* put fr at frame that grows */
5626 }
5627 else /* drag right */
5628 {
5629 left = FALSE;
5630 /* sum up the room of frames right of the current one */
5631 room = 0;
5632 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5633 room += fr->fr_width - frame_minwidth(fr, NULL);
5634 fr = curfr; /* put fr at window that grows */
5635 }
5636
5637 if (room < offset) /* Not enough room */
5638 offset = room; /* Move as far as we can */
5639 if (offset <= 0) /* No room at all, quit. */
5640 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005641 if (fr == NULL)
5642 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
5644 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005645 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
5647 /* shrink other frames: current and at the left or at the right */
5648 if (left)
5649 fr = curfr; /* current frame gets smaller */
5650 else
5651 fr = curfr->fr_next; /* next frame gets smaller */
5652
5653 while (fr != NULL && offset > 0)
5654 {
5655 n = frame_minwidth(fr, NULL);
5656 if (fr->fr_width - offset <= n)
5657 {
5658 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005659 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
5661 else
5662 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005663 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 break;
5665 }
5666 if (left)
5667 fr = fr->fr_prev;
5668 else
5669 fr = fr->fr_next;
5670 }
5671 (void)win_comp_pos();
5672 redraw_all_later(NOT_VALID);
5673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674#endif /* FEAT_MOUSE */
5675
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005676#define FRACTION_MULT 16384L
5677
5678/*
5679 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005680 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005681 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005682 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005683set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005684{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005685 if (wp->w_height > 1)
5686 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005687 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005688}
5689
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690/*
5691 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005692 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 * This takes care of the things inside the window, not what happens to the
5694 * window position, the frame or to other windows.
5695 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005696 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005697win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005699 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
5701 /* Don't want a negative height. Happens when splitting a tiny window.
5702 * Will equalize heights soon to fix it. */
5703 if (height < 0)
5704 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005705 if (wp->w_height == height)
5706 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005708 if (wp->w_height > 0)
5709 {
5710 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005711 /* w_wrow needs to be valid. When setting 'laststatus' this may
5712 * call win_new_height() recursively. */
5713 validate_cursor();
5714 if (wp->w_height != prev_height)
5715 return; /* Recursive call already changed the size, bail out here
5716 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005717 if (wp->w_wrow != wp->w_prev_fraction_row)
5718 set_fraction(wp);
5719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
5721 wp->w_height = height;
5722 wp->w_skipcol = 0;
5723
Bram Moolenaar955f1982017-02-05 15:10:51 +01005724 /* There is no point in adjusting the scroll position when exiting. Some
5725 * values might be invalid. */
5726 if (!exiting)
5727 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005728}
5729
5730 void
5731scroll_to_fraction(win_T *wp, int prev_height)
5732{
5733 linenr_T lnum;
5734 int sline, line_size;
5735 int height = wp->w_height;
5736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 /* Don't change w_topline when height is zero. Don't set w_topline when
5738 * 'scrollbind' is set and this isn't the current window. */
5739 if (height > 0
5740#ifdef FEAT_SCROLLBIND
5741 && (!wp->w_p_scb || wp == curwin)
5742#endif
5743 )
5744 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005745 /*
5746 * Find a value for w_topline that shows the cursor at the same
5747 * relative position in the window as before (more or less).
5748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 lnum = wp->w_cursor.lnum;
5750 if (lnum < 1) /* can happen when starting up */
5751 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005752 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5753 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5755 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005756
5757 if (sline >= 0)
5758 {
5759 /* Make sure the whole cursor line is visible, if possible. */
5760 int rows = plines_win(wp, lnum, FALSE);
5761
5762 if (sline > wp->w_height - rows)
5763 {
5764 sline = wp->w_height - rows;
5765 wp->w_wrow -= rows - line_size;
5766 }
5767 }
5768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 if (sline < 0)
5770 {
5771 /*
5772 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005773 * Make cursor line the first line in the window. If not enough
5774 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 */
5776 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005777 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005778 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005779 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005780 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005781 --wp->w_wrow;
5782 while (wp->w_wrow >= wp->w_height)
5783 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005784 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005785 + win_col_off2(wp);
5786 --wp->w_wrow;
5787 }
5788 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005789 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005791 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005793 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795#ifdef FEAT_FOLDING
5796 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5797 if (lnum == 1)
5798 {
5799 /* first line in buffer is folded */
5800 line_size = 1;
5801 --sline;
5802 break;
5803 }
5804#endif
5805 --lnum;
5806#ifdef FEAT_DIFF
5807 if (lnum == wp->w_topline)
5808 line_size = plines_win_nofill(wp, lnum, TRUE)
5809 + wp->w_topfill;
5810 else
5811#endif
5812 line_size = plines_win(wp, lnum, TRUE);
5813 sline -= line_size;
5814 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 if (sline < 0)
5817 {
5818 /*
5819 * Line we want at top would go off top of screen. Use next
5820 * line instead.
5821 */
5822#ifdef FEAT_FOLDING
5823 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5824#endif
5825 lnum++;
5826 wp->w_wrow -= line_size + sline;
5827 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005828 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 {
5830 /* First line of file reached, use that as topline. */
5831 lnum = 1;
5832 wp->w_wrow -= sline;
5833 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005834
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005835 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 }
5838
5839 if (wp == curwin)
5840 {
5841 if (p_so)
5842 update_topline();
5843 curs_columns(FALSE); /* validate w_wrow */
5844 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005845 if (prev_height > 0)
5846 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
5848 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005849 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 invalidate_botline_win(wp);
5852}
5853
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854/*
5855 * Set the width of a window.
5856 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005857 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005858win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859{
5860 wp->w_width = width;
5861 wp->w_lines_valid = 0;
5862 changed_line_abv_curs_win(wp);
5863 invalidate_botline_win(wp);
5864 if (wp == curwin)
5865 {
5866 update_topline();
5867 curs_columns(TRUE); /* validate w_wrow */
5868 }
5869 redraw_win_later(wp, NOT_VALID);
5870 wp->w_redr_status = TRUE;
5871}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872
5873 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005874win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5877 if (wp->w_p_scr == 0)
5878 wp->w_p_scr = 1;
5879}
5880
5881/*
5882 * command_height: called whenever p_ch has been changed
5883 */
5884 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005885command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 int h;
5888 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005889 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005891 /* Use the value of p_ch that we remembered. This is needed for when the
5892 * GUI starts up, we can't be sure in what order things happen. And when
5893 * p_ch was changed in another tab page. */
5894 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005895
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 /* Find bottom frame with width of screen. */
5897 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5899 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
5901 /* Avoid changing the height of a window with 'winfixheight' set. */
5902 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5903 && frp->fr_win->w_p_wfh)
5904 frp = frp->fr_prev;
5905
5906 if (starting != NO_SCREEN)
5907 {
5908 cmdline_row = Rows - p_ch;
5909
5910 if (p_ch > old_p_ch) /* p_ch got bigger */
5911 {
5912 while (p_ch > old_p_ch)
5913 {
5914 if (frp == NULL)
5915 {
5916 EMSG(_(e_noroom));
5917 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005918 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 cmdline_row = Rows - p_ch;
5920 break;
5921 }
5922 h = frp->fr_height - frame_minheight(frp, NULL);
5923 if (h > p_ch - old_p_ch)
5924 h = p_ch - old_p_ch;
5925 old_p_ch += h;
5926 frame_add_height(frp, -h);
5927 frp = frp->fr_prev;
5928 }
5929
5930 /* Recompute window positions. */
5931 (void)win_comp_pos();
5932
5933 /* clear the lines added to cmdline */
5934 if (full_screen)
5935 screen_fill((int)(cmdline_row), (int)Rows, 0,
5936 (int)Columns, ' ', ' ', 0);
5937 msg_row = cmdline_row;
5938 redraw_cmdline = TRUE;
5939 return;
5940 }
5941
5942 if (msg_row < cmdline_row)
5943 msg_row = cmdline_row;
5944 redraw_cmdline = TRUE;
5945 }
5946 frame_add_height(frp, (int)(old_p_ch - p_ch));
5947
5948 /* Recompute window positions. */
5949 if (frp != lastwin->w_frame)
5950 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951}
5952
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953/*
5954 * Resize frame "frp" to be "n" lines higher (negative for less high).
5955 * Also resize the frames it is contained in.
5956 */
5957 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005958frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5961 for (;;)
5962 {
5963 frp = frp->fr_parent;
5964 if (frp == NULL)
5965 break;
5966 frp->fr_height += n;
5967 }
5968}
5969
5970/*
5971 * Add or remove a status line for the bottom window(s), according to the
5972 * value of 'laststatus'.
5973 */
5974 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005975last_status(
5976 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978 /* Don't make a difference between horizontal or vertical split. */
5979 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01005980 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981}
5982
5983 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005984last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
5986 frame_T *fp;
5987 win_T *wp;
5988
5989 if (fr->fr_layout == FR_LEAF)
5990 {
5991 wp = fr->fr_win;
5992 if (wp->w_status_height != 0 && !statusline)
5993 {
5994 /* remove status line */
5995 win_new_height(wp, wp->w_height + 1);
5996 wp->w_status_height = 0;
5997 comp_col();
5998 }
5999 else if (wp->w_status_height == 0 && statusline)
6000 {
6001 /* Find a frame to take a line from. */
6002 fp = fr;
6003 while (fp->fr_height <= frame_minheight(fp, NULL))
6004 {
6005 if (fp == topframe)
6006 {
6007 EMSG(_(e_noroom));
6008 return;
6009 }
6010 /* In a column of frames: go to frame above. If already at
6011 * the top or in a row of frames: go to parent. */
6012 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6013 fp = fp->fr_prev;
6014 else
6015 fp = fp->fr_parent;
6016 }
6017 wp->w_status_height = 1;
6018 if (fp != fr)
6019 {
6020 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6021 frame_fix_height(wp);
6022 (void)win_comp_pos();
6023 }
6024 else
6025 win_new_height(wp, wp->w_height - 1);
6026 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006027 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 }
6029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 else if (fr->fr_layout == FR_ROW)
6031 {
6032 /* vertically split windows, set status line for each one */
6033 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6034 last_status_rec(fp, statusline);
6035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 else
6037 {
6038 /* horizontally split window, set status line for last one */
6039 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6040 ;
6041 last_status_rec(fp, statusline);
6042 }
6043}
6044
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006045/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006046 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006047 */
6048 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006049tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006050{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006051#ifdef FEAT_GUI_TABLINE
6052 /* When the GUI has the tabline then this always returns zero. */
6053 if (gui_use_tabline())
6054 return 0;
6055#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006056 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006057 {
6058 case 0: return 0;
6059 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6060 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006061 return 1;
6062}
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6065/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006066 * Get the file name at the cursor.
6067 * If Visual mode is active, use the selected text if it's in one line.
6068 * Returns the name in allocated memory, NULL for failure.
6069 */
6070 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006071grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006072{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006073 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6074
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006075 if (VIsual_active)
6076 {
6077 int len;
6078 char_u *ptr;
6079
6080 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6081 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006082 return find_file_name_in_path(ptr, len, options,
6083 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006084 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006085 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006086}
6087
6088/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 * Return the file name under or after the cursor.
6090 *
6091 * The 'path' option is searched if the file name is not absolute.
6092 * The string returned has been alloc'ed and should be freed by the caller.
6093 * NULL is returned if the file name or file is not found.
6094 *
6095 * options:
6096 * FNAME_MESS give error messages
6097 * FNAME_EXP expand to path
6098 * FNAME_HYP check for hypertext link
6099 * FNAME_INCL apply "includeexpr"
6100 */
6101 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006102file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103{
6104 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006105 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6106 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107}
6108
6109/*
6110 * Return the name of the file under or after ptr[col].
6111 * Otherwise like file_name_at_cursor().
6112 */
6113 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006114file_name_in_line(
6115 char_u *line,
6116 int col,
6117 int options,
6118 long count,
6119 char_u *rel_fname, /* file we are searching relative to */
6120 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122 char_u *ptr;
6123 int len;
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006124 int in_type = TRUE;
6125 int is_url = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
6127 /*
6128 * search forward for what could be the start of a file name
6129 */
6130 ptr = line + col;
6131 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006132 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 if (*ptr == NUL) /* nothing found */
6134 {
6135 if (options & FNAME_MESS)
6136 EMSG(_("E446: No file name under cursor"));
6137 return NULL;
6138 }
6139
6140 /*
6141 * Search backward for first char of the file name.
6142 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6143 */
6144 while (ptr > line)
6145 {
6146#ifdef FEAT_MBYTE
6147 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6148 ptr -= len + 1;
6149 else
6150#endif
6151 if (vim_isfilec(ptr[-1])
6152 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6153 --ptr;
6154 else
6155 break;
6156 }
6157
6158 /*
6159 * Search forward for the last char of the file name.
6160 * Also allow "://" when ':' is not in 'isfname'.
6161 */
6162 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006163 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006164 || ((options & FNAME_HYP) && path_is_url(ptr + len))
6165 || (is_url && vim_strchr((char_u *)"?&=", ptr[len]) != NULL))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006166 {
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006167 /* After type:// we also include ?, & and = as valid characters, so that
6168 * http://google.com?q=this&that=ok works. */
6169 if ((ptr[len] >= 'A' && ptr[len] <= 'Z') || (ptr[len] >= 'a' && ptr[len] <= 'z'))
6170 {
6171 if (in_type && path_is_url(ptr + len + 1))
6172 is_url = TRUE;
6173 }
6174 else
6175 in_type = FALSE;
6176
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006177 if (ptr[len] == '\\')
6178 /* Skip over the "\" in "\ ". */
6179 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180#ifdef FEAT_MBYTE
6181 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006182 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 else
6184#endif
6185 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187
6188 /*
6189 * If there is trailing punctuation, remove it.
6190 * But don't remove "..", could be a directory name.
6191 */
6192 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6193 && ptr[len - 2] != '.')
6194 --len;
6195
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006196 if (file_lnum != NULL)
6197 {
6198 char_u *p;
6199
6200 /* Get the number after the file name and a separator character */
6201 p = ptr + len;
6202 p = skipwhite(p);
6203 if (*p != NUL)
6204 {
6205 if (!isdigit(*p))
6206 ++p; /* skip the separator */
6207 p = skipwhite(p);
6208 if (isdigit(*p))
6209 *file_lnum = (int)getdigits(&p);
6210 }
6211 }
6212
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6214}
6215
6216# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006217static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
6219 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006220eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221{
6222 char_u *res;
6223
6224 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006225 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006226 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 set_vim_var_string(VV_FNAME, NULL, 0);
6228 return res;
6229}
6230#endif
6231
6232/*
6233 * Return the name of the file ptr[len] in 'path'.
6234 * Otherwise like file_name_at_cursor().
6235 */
6236 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006237find_file_name_in_path(
6238 char_u *ptr,
6239 int len,
6240 int options,
6241 long count,
6242 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 char_u *file_name;
6245 int c;
6246# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6247 char_u *tofree = NULL;
6248
6249 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6250 {
6251 tofree = eval_includeexpr(ptr, len);
6252 if (tofree != NULL)
6253 {
6254 ptr = tofree;
6255 len = (int)STRLEN(ptr);
6256 }
6257 }
6258# endif
6259
6260 if (options & FNAME_EXP)
6261 {
6262 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6263 TRUE, rel_fname);
6264
6265# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6266 /*
6267 * If the file could not be found in a normal way, try applying
6268 * 'includeexpr' (unless done already).
6269 */
6270 if (file_name == NULL
6271 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6272 {
6273 tofree = eval_includeexpr(ptr, len);
6274 if (tofree != NULL)
6275 {
6276 ptr = tofree;
6277 len = (int)STRLEN(ptr);
6278 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6279 TRUE, rel_fname);
6280 }
6281 }
6282# endif
6283 if (file_name == NULL && (options & FNAME_MESS))
6284 {
6285 c = ptr[len];
6286 ptr[len] = NUL;
6287 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6288 ptr[len] = c;
6289 }
6290
6291 /* Repeat finding the file "count" times. This matters when it
6292 * appears several times in the path. */
6293 while (file_name != NULL && --count > 0)
6294 {
6295 vim_free(file_name);
6296 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6297 }
6298 }
6299 else
6300 file_name = vim_strnsave(ptr, len);
6301
6302# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6303 vim_free(tofree);
6304# endif
6305
6306 return file_name;
6307}
6308#endif /* FEAT_SEARCHPATH */
6309
6310/*
6311 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6312 * Also check for ":\\", which MS Internet Explorer accepts, return
6313 * URL_BACKSLASH.
6314 */
6315 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006316path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 if (STRNCMP(p, "://", (size_t)3) == 0)
6319 return URL_SLASH;
6320 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6321 return URL_BACKSLASH;
6322 return 0;
6323}
6324
6325/*
6326 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6327 * Return URL_BACKSLASH for "name:\\".
6328 * Return zero otherwise.
6329 */
6330 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006331path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333 char_u *p;
6334
6335 for (p = fname; isalpha(*p); ++p)
6336 ;
6337 return path_is_url(p);
6338}
6339
6340/*
6341 * Return TRUE if "name" is a full (absolute) path name or URL.
6342 */
6343 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006344vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 return (path_with_url(name) != 0 || mch_isFullName(name));
6347}
6348
6349/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006350 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 *
6352 * return FAIL for failure, OK otherwise
6353 */
6354 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006355vim_FullName(
6356 char_u *fname,
6357 char_u *buf,
6358 int len,
6359 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
6361 int retval = OK;
6362 int url;
6363
6364 *buf = NUL;
6365 if (fname == NULL)
6366 return FAIL;
6367
6368 url = path_with_url(fname);
6369 if (!url)
6370 retval = mch_FullName(fname, buf, len, force);
6371 if (url || retval == FAIL)
6372 {
6373 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006374 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 }
Bram Moolenaard0573012017-10-28 21:11:06 +02006376#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 slash_adjust(buf);
6378#endif
6379 return retval;
6380}
6381
6382/*
6383 * Return the minimal number of rows that is needed on the screen to display
6384 * the current number of windows.
6385 */
6386 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006387min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006390 tabpage_T *tp;
6391 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393 if (firstwin == NULL) /* not initialized yet */
6394 return MIN_LINES;
6395
Bram Moolenaarf740b292006-02-16 22:11:02 +00006396 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006397 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006398 {
6399 n = frame_minheight(tp->tp_topframe, NULL);
6400 if (total < n)
6401 total = n;
6402 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006403 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006404 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 return total;
6406}
6407
6408/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006409 * Return TRUE if there is only one window (in the current tab page), not
6410 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006411 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 */
6413 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006414only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 int count = 0;
6417 win_T *wp;
6418
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006419 /* If there is another tab page there always is another window. */
6420 if (first_tabpage->tp_next != NULL)
6421 return FALSE;
6422
Bram Moolenaar29323592016-07-24 22:04:11 +02006423 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006424 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006425 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426# ifdef FEAT_QUICKFIX
6427 || wp->w_p_pvw
6428# endif
6429 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006430# ifdef FEAT_AUTOCMD
6431 && wp != aucmd_win
6432# endif
6433 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 ++count;
6435 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436}
6437
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438/*
6439 * Correct the cursor line number in other windows. Used after changing the
6440 * current buffer, and before applying autocommands.
6441 * When "do_curwin" is TRUE, also check current window.
6442 */
6443 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006444check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006447 tabpage_T *tp;
6448
6449 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 {
6452 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6453 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6454 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6455 wp->w_topline = curbuf->b_ml.ml_line_count;
6456 }
6457}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459/*
6460 * A snapshot of the window sizes, to restore them after closing the help
6461 * window.
6462 * Only these fields are used:
6463 * fr_layout
6464 * fr_width
6465 * fr_height
6466 * fr_next
6467 * fr_child
6468 * fr_win (only valid for the old curwin, NULL otherwise)
6469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470
6471/*
6472 * Create a snapshot of the current frame sizes.
6473 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006474 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006475make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006477 clear_snapshot(curtab, idx);
6478 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479}
6480
6481 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006482make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6485 if (*frp == NULL)
6486 return;
6487 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 (*frp)->fr_height = fr->fr_height;
6490 if (fr->fr_next != NULL)
6491 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6492 if (fr->fr_child != NULL)
6493 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6494 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6495 (*frp)->fr_win = curwin;
6496}
6497
6498/*
6499 * Remove any existing snapshot.
6500 */
6501 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006502clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006504 clear_snapshot_rec(tp->tp_snapshot[idx]);
6505 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506}
6507
6508 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006509clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 if (fr != NULL)
6512 {
6513 clear_snapshot_rec(fr->fr_next);
6514 clear_snapshot_rec(fr->fr_child);
6515 vim_free(fr);
6516 }
6517}
6518
6519/*
6520 * Restore a previously created snapshot, if there is any.
6521 * This is only done if the screen size didn't change and the window layout is
6522 * still the same.
6523 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006524 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006525restore_snapshot(
6526 int idx,
6527 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 win_T *wp;
6530
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006531 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006532 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006533 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6534 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006536 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 win_comp_pos();
6538 if (wp != NULL && close_curwin)
6539 win_goto(wp);
6540 redraw_all_later(CLEAR);
6541 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006542 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543}
6544
6545/*
6546 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006547 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 */
6549 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006550check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551{
6552 if (sn->fr_layout != fr->fr_layout
6553 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6554 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6555 || (sn->fr_next != NULL
6556 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6557 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006558 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006559 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 return FAIL;
6561 return OK;
6562}
6563
6564/*
6565 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6566 * following frames and children.
6567 * Returns a pointer to the old current window, or NULL.
6568 */
6569 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006570restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 win_T *wp = NULL;
6573 win_T *wp2;
6574
6575 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 if (fr->fr_layout == FR_LEAF)
6578 {
6579 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006580 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 wp = sn->fr_win;
6582 }
6583 if (sn->fr_next != NULL)
6584 {
6585 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6586 if (wp2 != NULL)
6587 wp = wp2;
6588 }
6589 if (sn->fr_child != NULL)
6590 {
6591 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6592 if (wp2 != NULL)
6593 wp = wp2;
6594 }
6595 return wp;
6596}
6597
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006598#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6599 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006600/*
6601 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006602 * restore_win() MUST be called to undo, also when FAIL is returned.
6603 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006604 * When "no_display" is TRUE the display won't be affected, no redraw is
6605 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006606 * Returns FAIL if switching to "win" failed.
6607 */
6608 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006609switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006610 win_T **save_curwin,
6611 tabpage_T **save_curtab,
6612 win_T *win,
6613 tabpage_T *tp,
6614 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006615{
6616# ifdef FEAT_AUTOCMD
6617 block_autocmds();
6618# endif
Bram Moolenaar105bc352013-05-17 16:03:57 +02006619 *save_curwin = curwin;
6620 if (tp != NULL)
6621 {
6622 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006623 if (no_display)
6624 {
6625 curtab->tp_firstwin = firstwin;
6626 curtab->tp_lastwin = lastwin;
6627 curtab = tp;
6628 firstwin = curtab->tp_firstwin;
6629 lastwin = curtab->tp_lastwin;
6630 }
6631 else
6632 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006633 }
6634 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006635 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006636 curwin = win;
6637 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006638 return OK;
6639}
6640
6641/*
6642 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006643 * When "no_display" is TRUE the display won't be affected, no redraw is
6644 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006645 */
6646 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006647restore_win(
6648 win_T *save_curwin UNUSED,
6649 tabpage_T *save_curtab UNUSED,
6650 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006651{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006652 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006653 {
6654 if (no_display)
6655 {
6656 curtab->tp_firstwin = firstwin;
6657 curtab->tp_lastwin = lastwin;
6658 curtab = save_curtab;
6659 firstwin = curtab->tp_firstwin;
6660 lastwin = curtab->tp_lastwin;
6661 }
6662 else
6663 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6664 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006665 if (win_valid(save_curwin))
6666 {
6667 curwin = save_curwin;
6668 curbuf = curwin->w_buffer;
6669 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006670# ifdef FEAT_AUTOCMD
6671 unblock_autocmds();
6672# endif
6673}
6674
6675/*
6676 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6677 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6678 */
6679 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006680switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006681{
6682# ifdef FEAT_AUTOCMD
6683 block_autocmds();
6684# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006685 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006686 --curbuf->b_nwindows;
6687 curbuf = buf;
6688 curwin->w_buffer = buf;
6689 ++curbuf->b_nwindows;
6690}
6691
6692/*
6693 * Restore the current buffer after using switch_buffer().
6694 */
6695 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006696restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006697{
6698# ifdef FEAT_AUTOCMD
6699 unblock_autocmds();
6700# endif
6701 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006702 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006703 {
6704 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006705 curwin->w_buffer = save_curbuf->br_buf;
6706 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006707 ++curbuf->b_nwindows;
6708 }
6709}
6710#endif
6711
Bram Moolenaar4033c552017-09-16 20:54:51 +02006712#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713/*
6714 * Return TRUE if there is any vertically split window.
6715 */
6716 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006717win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 frame_T *fr;
6720
6721 if (topframe->fr_layout == FR_ROW)
6722 return TRUE;
6723
6724 if (topframe->fr_layout == FR_COL)
6725 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6726 if (fr->fr_layout == FR_ROW)
6727 return TRUE;
6728
6729 return FALSE;
6730}
6731#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006732
6733#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6734/*
6735 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006736 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006737 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6738 * If no particular ID is desired, -1 must be specified for 'id'.
6739 * Return ID of added match, -1 on failure.
6740 */
6741 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006742match_add(
6743 win_T *wp,
6744 char_u *grp,
6745 char_u *pat,
6746 int prio,
6747 int id,
6748 list_T *pos_list,
6749 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006750{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006751 matchitem_T *cur;
6752 matchitem_T *prev;
6753 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006754 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006755 regprog_T *regprog = NULL;
6756 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006757
Bram Moolenaarb3414592014-06-17 17:48:32 +02006758 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006759 return -1;
6760 if (id < -1 || id == 0)
6761 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006762 EMSGN(_("E799: Invalid ID: %ld (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006763 return -1;
6764 }
6765 if (id != -1)
6766 {
6767 cur = wp->w_match_head;
6768 while (cur != NULL)
6769 {
6770 if (cur->id == id)
6771 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006772 EMSGN(_("E801: ID already taken: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006773 return -1;
6774 }
6775 cur = cur->next;
6776 }
6777 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006778 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006779 {
6780 EMSG2(_(e_nogroup), grp);
6781 return -1;
6782 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006783 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006784 {
6785 EMSG2(_(e_invarg2), pat);
6786 return -1;
6787 }
6788
6789 /* Find available match ID. */
6790 while (id == -1)
6791 {
6792 cur = wp->w_match_head;
6793 while (cur != NULL && cur->id != wp->w_next_match_id)
6794 cur = cur->next;
6795 if (cur == NULL)
6796 id = wp->w_next_match_id;
6797 wp->w_next_match_id++;
6798 }
6799
6800 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006801 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006802 m->id = id;
6803 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006804 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006805 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006806 m->match.regprog = regprog;
6807 m->match.rmm_ic = FALSE;
6808 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006809# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006810 m->conceal_char = 0;
6811 if (conceal_char != NULL)
6812 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006813# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006814
Bram Moolenaarb3414592014-06-17 17:48:32 +02006815 /* Set up position matches */
6816 if (pos_list != NULL)
6817 {
6818 linenr_T toplnum = 0;
6819 linenr_T botlnum = 0;
6820 listitem_T *li;
6821 int i;
6822
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006823 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006824 i++, li = li->li_next)
6825 {
6826 linenr_T lnum = 0;
6827 colnr_T col = 0;
6828 int len = 1;
6829 list_T *subl;
6830 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006831 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006832
Bram Moolenaarb3414592014-06-17 17:48:32 +02006833 if (li->li_tv.v_type == VAR_LIST)
6834 {
6835 subl = li->li_tv.vval.v_list;
6836 if (subl == NULL)
6837 goto fail;
6838 subli = subl->lv_first;
6839 if (subli == NULL)
6840 goto fail;
6841 lnum = get_tv_number_chk(&subli->li_tv, &error);
6842 if (error == TRUE)
6843 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006844 if (lnum == 0)
6845 {
6846 --i;
6847 continue;
6848 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006849 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006850 subli = subli->li_next;
6851 if (subli != NULL)
6852 {
6853 col = get_tv_number_chk(&subli->li_tv, &error);
6854 if (error == TRUE)
6855 goto fail;
6856 subli = subli->li_next;
6857 if (subli != NULL)
6858 {
6859 len = get_tv_number_chk(&subli->li_tv, &error);
6860 if (error == TRUE)
6861 goto fail;
6862 }
6863 }
6864 m->pos.pos[i].col = col;
6865 m->pos.pos[i].len = len;
6866 }
6867 else if (li->li_tv.v_type == VAR_NUMBER)
6868 {
6869 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006870 {
6871 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006872 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006873 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006874 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6875 m->pos.pos[i].col = 0;
6876 m->pos.pos[i].len = 0;
6877 }
6878 else
6879 {
6880 EMSG(_("List or number required"));
6881 goto fail;
6882 }
6883 if (toplnum == 0 || lnum < toplnum)
6884 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006885 if (botlnum == 0 || lnum >= botlnum)
6886 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006887 }
6888
6889 /* Calculate top and bottom lines for redrawing area */
6890 if (toplnum != 0)
6891 {
6892 if (wp->w_buffer->b_mod_set)
6893 {
6894 if (wp->w_buffer->b_mod_top > toplnum)
6895 wp->w_buffer->b_mod_top = toplnum;
6896 if (wp->w_buffer->b_mod_bot < botlnum)
6897 wp->w_buffer->b_mod_bot = botlnum;
6898 }
6899 else
6900 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006901 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006902 wp->w_buffer->b_mod_top = toplnum;
6903 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006904 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006905 }
6906 m->pos.toplnum = toplnum;
6907 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006908 rtype = VALID;
6909 }
6910 }
6911
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006912 /* Insert new match. The match list is in ascending order with regard to
6913 * the match priorities. */
6914 cur = wp->w_match_head;
6915 prev = cur;
6916 while (cur != NULL && prio >= cur->priority)
6917 {
6918 prev = cur;
6919 cur = cur->next;
6920 }
6921 if (cur == prev)
6922 wp->w_match_head = m;
6923 else
6924 prev->next = m;
6925 m->next = cur;
6926
Bram Moolenaarb3414592014-06-17 17:48:32 +02006927 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006928 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006929
6930fail:
6931 vim_free(m);
6932 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006933}
6934
6935/*
6936 * Delete match with ID 'id' in the match list of window 'wp'.
6937 * Print error messages if 'perr' is TRUE.
6938 */
6939 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006940match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006941{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006942 matchitem_T *cur = wp->w_match_head;
6943 matchitem_T *prev = cur;
6944 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006945
6946 if (id < 1)
6947 {
6948 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006949 EMSGN(_("E802: Invalid ID: %ld (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006950 id);
6951 return -1;
6952 }
6953 while (cur != NULL && cur->id != id)
6954 {
6955 prev = cur;
6956 cur = cur->next;
6957 }
6958 if (cur == NULL)
6959 {
6960 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006961 EMSGN(_("E803: ID not found: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006962 return -1;
6963 }
6964 if (cur == prev)
6965 wp->w_match_head = cur->next;
6966 else
6967 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006968 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006969 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006970 if (cur->pos.toplnum != 0)
6971 {
6972 if (wp->w_buffer->b_mod_set)
6973 {
6974 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6975 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6976 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6977 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6978 }
6979 else
6980 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006981 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006982 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6983 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006984 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006985 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006986 rtype = VALID;
6987 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006988 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006989 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006990 return 0;
6991}
6992
6993/*
6994 * Delete all matches in the match list of window 'wp'.
6995 */
6996 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006997clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006998{
6999 matchitem_T *m;
7000
7001 while (wp->w_match_head != NULL)
7002 {
7003 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007004 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007005 vim_free(wp->w_match_head->pattern);
7006 vim_free(wp->w_match_head);
7007 wp->w_match_head = m;
7008 }
7009 redraw_later(SOME_VALID);
7010}
7011
7012/*
7013 * Get match from ID 'id' in window 'wp'.
7014 * Return NULL if match not found.
7015 */
7016 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007017get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007018{
7019 matchitem_T *cur = wp->w_match_head;
7020
7021 while (cur != NULL && cur->id != id)
7022 cur = cur->next;
7023 return cur;
7024}
7025#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007026
7027#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7028 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007029get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007030{
7031 int i = 1;
7032 win_T *w;
7033
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007034 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007035 ++i;
7036
7037 if (w == NULL)
7038 return 0;
7039 else
7040 return i;
7041}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007042
7043 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007044get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007045{
7046 int i = 1;
7047 tabpage_T *t;
7048
7049 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7050 ++i;
7051
7052 if (t == NULL)
7053 return 0;
7054 else
7055 return i;
7056}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007057#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007058
7059/*
7060 * Return TRUE if "topfrp" and its children are at the right height.
7061 */
7062 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007063frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007064{
7065 frame_T *frp;
7066
7067 if (topfrp->fr_height != height)
7068 return FALSE;
7069
7070 if (topfrp->fr_layout == FR_ROW)
7071 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7072 if (frp->fr_height != height)
7073 return FALSE;
7074
7075 return TRUE;
7076}
7077
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007078/*
7079 * Return TRUE if "topfrp" and its children are at the right width.
7080 */
7081 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007082frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007083{
7084 frame_T *frp;
7085
7086 if (topfrp->fr_width != width)
7087 return FALSE;
7088
7089 if (topfrp->fr_layout == FR_COL)
7090 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7091 if (frp->fr_width != width)
7092 return FALSE;
7093
7094 return TRUE;
7095}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007096
Bram Moolenaar86edef62016-03-13 18:07:30 +01007097#if defined(FEAT_EVAL) || defined(PROTO)
7098 int
7099win_getid(typval_T *argvars)
7100{
7101 int winnr;
7102 win_T *wp;
7103
7104 if (argvars[0].v_type == VAR_UNKNOWN)
7105 return curwin->w_id;
7106 winnr = get_tv_number(&argvars[0]);
7107 if (winnr > 0)
7108 {
7109 if (argvars[1].v_type == VAR_UNKNOWN)
7110 wp = firstwin;
7111 else
7112 {
7113 tabpage_T *tp;
7114 int tabnr = get_tv_number(&argvars[1]);
7115
Bram Moolenaar29323592016-07-24 22:04:11 +02007116 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007117 if (--tabnr == 0)
7118 break;
7119 if (tp == NULL)
7120 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01007121 if (tp == curtab)
7122 wp = firstwin;
7123 else
7124 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01007125 }
7126 for ( ; wp != NULL; wp = wp->w_next)
7127 if (--winnr == 0)
7128 return wp->w_id;
7129 }
7130 return 0;
7131}
7132
7133 int
7134win_gotoid(typval_T *argvars)
7135{
7136 win_T *wp;
7137 tabpage_T *tp;
7138 int id = get_tv_number(&argvars[0]);
7139
Bram Moolenaar29323592016-07-24 22:04:11 +02007140 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007141 if (wp->w_id == id)
7142 {
7143 goto_tabpage_win(tp, wp);
7144 return 1;
7145 }
7146 return 0;
7147}
7148
7149 void
7150win_id2tabwin(typval_T *argvars, list_T *list)
7151{
7152 win_T *wp;
7153 tabpage_T *tp;
7154 int winnr = 1;
7155 int tabnr = 1;
7156 int id = get_tv_number(&argvars[0]);
7157
Bram Moolenaar29323592016-07-24 22:04:11 +02007158 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007159 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007160 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007161 {
7162 if (wp->w_id == id)
7163 {
7164 list_append_number(list, tabnr);
7165 list_append_number(list, winnr);
7166 return;
7167 }
7168 ++winnr;
7169 }
7170 ++tabnr;
7171 winnr = 1;
7172 }
7173 list_append_number(list, 0);
7174 list_append_number(list, 0);
7175}
7176
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007177 win_T *
7178win_id2wp(typval_T *argvars)
7179{
7180 win_T *wp;
7181 tabpage_T *tp;
7182 int id = get_tv_number(&argvars[0]);
7183
7184 FOR_ALL_TAB_WINDOWS(tp, wp)
7185 if (wp->w_id == id)
7186 return wp;
7187
7188 return NULL;
7189}
7190
Bram Moolenaar86edef62016-03-13 18:07:30 +01007191 int
7192win_id2win(typval_T *argvars)
7193{
7194 win_T *wp;
7195 int nr = 1;
7196 int id = get_tv_number(&argvars[0]);
7197
Bram Moolenaar29323592016-07-24 22:04:11 +02007198 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007199 {
7200 if (wp->w_id == id)
7201 return nr;
7202 ++nr;
7203 }
7204 return 0;
7205}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007206
7207 void
7208win_findbuf(typval_T *argvars, list_T *list)
7209{
7210 win_T *wp;
7211 tabpage_T *tp;
7212 int bufnr = get_tv_number(&argvars[0]);
7213
Bram Moolenaar29323592016-07-24 22:04:11 +02007214 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007215 if (wp->w_buffer->b_fnum == bufnr)
7216 list_append_number(list, wp->w_id);
7217}
7218
Bram Moolenaar86edef62016-03-13 18:07:30 +01007219#endif