blob: 97c5dc3c36e26ddfc9e98dbb298680c61bc5ceb4 [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 Moolenaar70b2a562012-01-10 22:26:17 +0100785 if (oldwin->w_height <= 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. */
895 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
896 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 Moolenaar13d831f2011-01-08 14:46:03 +01001084 if (oldwin->w_height > 0)
1085 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001086 wp->w_fraction = oldwin->w_fraction;
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (flags & WSP_VERT)
1089 {
1090 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (need_status)
1093 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001094 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 oldwin->w_status_height = need_status;
1096 }
1097 if (flags & (WSP_TOP | WSP_BOT))
1098 {
1099 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001100 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001101 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
1102#ifdef FEAT_MENU
1103 - wp->w_winbar_height
1104#endif
1105 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 wp->w_status_height = (p_ls > 0);
1107 }
1108 else
1109 {
1110 /* height and row of new window is same as current window */
1111 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaard326ad62017-09-18 20:31:41 +02001112 win_new_height(wp, oldwin->w_height
1113#ifdef FEAT_MENU
1114 + oldwin->w_winbar_height
1115#endif
1116 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 wp->w_status_height = oldwin->w_status_height;
1118 }
1119 frp->fr_height = curfrp->fr_height;
1120
1121 /* "new_size" of the current window goes to the new window, use
1122 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001123 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 if (before)
1125 wp->w_vsep_width = 1;
1126 else
1127 {
1128 wp->w_vsep_width = oldwin->w_vsep_width;
1129 oldwin->w_vsep_width = 1;
1130 }
1131 if (flags & (WSP_TOP | WSP_BOT))
1132 {
1133 if (flags & WSP_BOT)
1134 frame_add_vsep(curfrp);
1135 /* Set width of neighbor frame */
1136 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001137 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1138 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 }
1140 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001141 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 if (before) /* new window left of current one */
1143 {
1144 wp->w_wincol = oldwin->w_wincol;
1145 oldwin->w_wincol += new_size + 1;
1146 }
1147 else /* new window right of current one */
1148 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1149 frame_fix_width(oldwin);
1150 frame_fix_width(wp);
1151 }
1152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 if (flags & (WSP_TOP | WSP_BOT))
1156 {
1157 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001158 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 wp->w_vsep_width = 0;
1160 }
1161 else
1162 {
1163 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001164 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 wp->w_vsep_width = oldwin->w_vsep_width;
1166 }
1167 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168
1169 /* "new_size" of the current window goes to the new window, use
1170 * one row for the status line */
1171 win_new_height(wp, new_size);
1172 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001173 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001174 int new_fr_height = curfrp->fr_height - new_size
1175#ifdef FEAT_MENU
1176 + wp->w_winbar_height
1177#endif
1178 ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001179
1180 if (!((flags & WSP_BOT) && p_ls == 0))
1181 new_fr_height -= STATUS_HEIGHT;
1182 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 else
1185 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1186 if (before) /* new window above current one */
1187 {
1188 wp->w_winrow = oldwin->w_winrow;
1189 wp->w_status_height = STATUS_HEIGHT;
1190 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1191 }
1192 else /* new window below current one */
1193 {
1194 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1195 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001196 if (!(flags & WSP_BOT))
1197 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (flags & WSP_BOT)
1200 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 frame_fix_height(wp);
1202 frame_fix_height(oldwin);
1203 }
1204
1205 if (flags & (WSP_TOP | WSP_BOT))
1206 (void)win_comp_pos();
1207
1208 /*
1209 * Both windows need redrawing
1210 */
1211 redraw_win_later(wp, NOT_VALID);
1212 wp->w_redr_status = TRUE;
1213 redraw_win_later(oldwin, NOT_VALID);
1214 oldwin->w_redr_status = TRUE;
1215
1216 if (need_status)
1217 {
1218 msg_row = Rows - 1;
1219 msg_col = sc_col;
1220 msg_clr_eos_force(); /* Old command/ruler may still be there */
1221 comp_col();
1222 msg_row = Rows - 1;
1223 msg_col = 0; /* put position back at start of line */
1224 }
1225
1226 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001227 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 */
1229 if (do_equal || dir != 0)
1230 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001232 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1235 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 if (flags & WSP_VERT)
1237 {
1238 i = p_wiw;
1239 if (size != 0)
1240 p_wiw = size;
1241
1242# ifdef FEAT_GUI
1243 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1244 if (gui.in_use)
1245 gui_init_which_components(NULL);
1246# endif
1247 }
1248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 {
1250 i = p_wh;
1251 if (size != 0)
1252 p_wh = size;
1253 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001254
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001255#ifdef FEAT_JUMPLIST
1256 /* Keep same changelist position in new window. */
1257 wp->w_changelistidx = oldwin->w_changelistidx;
1258#endif
1259
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001260 /*
1261 * make the new window the current window
1262 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001263 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 if (flags & WSP_VERT)
1265 p_wiw = i;
1266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 p_wh = i;
1268
1269 return OK;
1270}
1271
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001272
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001273/*
1274 * Initialize window "newp" from window "oldp".
1275 * Used when splitting a window and when creating a new tab page.
1276 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001277 * WSP_NEWLOC may be specified in flags to prevent the location list from
1278 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001279 */
1280 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001281win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001282{
1283 int i;
1284
1285 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001286#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001287 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001288#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001289 oldp->w_buffer->b_nwindows++;
1290 newp->w_cursor = oldp->w_cursor;
1291 newp->w_valid = 0;
1292 newp->w_curswant = oldp->w_curswant;
1293 newp->w_set_curswant = oldp->w_set_curswant;
1294 newp->w_topline = oldp->w_topline;
1295#ifdef FEAT_DIFF
1296 newp->w_topfill = oldp->w_topfill;
1297#endif
1298 newp->w_leftcol = oldp->w_leftcol;
1299 newp->w_pcmark = oldp->w_pcmark;
1300 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1301 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001302 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001303 newp->w_fraction = oldp->w_fraction;
1304 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1305#ifdef FEAT_JUMPLIST
1306 copy_jumplist(oldp, newp);
1307#endif
1308#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001309 if (flags & WSP_NEWLOC)
1310 {
1311 /* Don't copy the location list. */
1312 newp->w_llist = NULL;
1313 newp->w_llist_ref = NULL;
1314 }
1315 else
1316 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001317#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001318 newp->w_localdir = (oldp->w_localdir == NULL)
1319 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001320
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001321 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001322 for (i = 0; i < oldp->w_tagstacklen; i++)
1323 {
1324 newp->w_tagstack[i] = oldp->w_tagstack[i];
1325 if (newp->w_tagstack[i].tagname != NULL)
1326 newp->w_tagstack[i].tagname =
1327 vim_strsave(newp->w_tagstack[i].tagname);
1328 }
1329 newp->w_tagstackidx = oldp->w_tagstackidx;
1330 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001331#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001332 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001333#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001334
1335 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001336
Bram Moolenaara971b822011-09-14 14:43:25 +02001337#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001338 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001339#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001340}
1341
1342/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001343 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001344 * Only the essential things are copied.
1345 */
1346 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001347win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001348{
1349 /* Use the same argument list. */
1350 newp->w_alist = oldp->w_alist;
1351 ++newp->w_alist->al_refcount;
1352 newp->w_arg_idx = oldp->w_arg_idx;
1353
1354 /* copy options from existing window */
1355 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001356}
1357
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001360 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 */
1362 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001363win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364{
1365 win_T *wp;
1366
1367 if (win == NULL)
1368 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001369 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (wp == win)
1371 return TRUE;
1372 return FALSE;
1373}
1374
1375/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001376 * Check if "win" is a pointer to an existing window in any tab page.
1377 */
1378 int
1379win_valid_any_tab(win_T *win)
1380{
1381 win_T *wp;
1382 tabpage_T *tp;
1383
1384 if (win == NULL)
1385 return FALSE;
1386 FOR_ALL_TABPAGES(tp)
1387 {
1388 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1389 {
1390 if (wp == win)
1391 return TRUE;
1392 }
1393 }
1394 return FALSE;
1395}
1396
1397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 * Return the number of windows.
1399 */
1400 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001401win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
1403 win_T *wp;
1404 int count = 0;
1405
Bram Moolenaar29323592016-07-24 22:04:11 +02001406 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 ++count;
1408 return count;
1409}
1410
1411/*
1412 * Make "count" windows on the screen.
1413 * Return actual number of windows on the screen.
1414 * Must be called when there is just one window, filling the whole screen
1415 * (excluding the command line).
1416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001418make_windows(
1419 int count,
1420 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 int maxcount;
1423 int todo;
1424
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (vertical)
1426 {
1427 /* Each windows needs at least 'winminwidth' lines and a separator
1428 * column. */
1429 maxcount = (curwin->w_width + curwin->w_vsep_width
1430 - (p_wiw - p_wmw)) / (p_wmw + 1);
1431 }
1432 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 {
1434 /* Each window needs at least 'winminheight' lines and a status line. */
1435 maxcount = (curwin->w_height + curwin->w_status_height
1436 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1437 }
1438
1439 if (maxcount < 2)
1440 maxcount = 2;
1441 if (count > maxcount)
1442 count = maxcount;
1443
1444 /*
1445 * add status line now, otherwise first window will be too big
1446 */
1447 if (count > 1)
1448 last_status(TRUE);
1449
1450#ifdef FEAT_AUTOCMD
1451 /*
1452 * Don't execute autocommands while creating the windows. Must do that
1453 * when putting the buffers in the windows.
1454 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001455 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456#endif
1457
1458 /* todo is number of windows left to create */
1459 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (vertical)
1461 {
1462 if (win_split(curwin->w_width - (curwin->w_width - todo)
1463 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1464 break;
1465 }
1466 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 {
1468 if (win_split(curwin->w_height - (curwin->w_height - todo
1469 * STATUS_HEIGHT) / (todo + 1)
1470 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1471 break;
1472 }
1473
1474#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001475 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476#endif
1477
1478 /* return actual number of windows */
1479 return (count - todo);
1480}
1481
1482/*
1483 * Exchange current and next window
1484 */
1485 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001486win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 frame_T *frp;
1489 frame_T *frp2;
1490 win_T *wp;
1491 win_T *wp2;
1492 int temp;
1493
Bram Moolenaar459ca562016-11-10 18:16:33 +01001494 if (ONE_WINDOW) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 {
1496 beep_flush();
1497 return;
1498 }
1499
1500#ifdef FEAT_GUI
1501 need_mouse_correct = TRUE;
1502#endif
1503
1504 /*
1505 * find window to exchange with
1506 */
1507 if (Prenum)
1508 {
1509 frp = curwin->w_frame->fr_parent->fr_child;
1510 while (frp != NULL && --Prenum > 0)
1511 frp = frp->fr_next;
1512 }
1513 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1514 frp = curwin->w_frame->fr_next;
1515 else /* Swap last window in row/col with previous */
1516 frp = curwin->w_frame->fr_prev;
1517
1518 /* We can only exchange a window with another window, not with a frame
1519 * containing windows. */
1520 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1521 return;
1522 wp = frp->fr_win;
1523
1524/*
1525 * 1. remove curwin from the list. Remember after which window it was in wp2
1526 * 2. insert curwin before wp in the list
1527 * if wp != wp2
1528 * 3. remove wp from the list
1529 * 4. insert wp after wp2
1530 * 5. exchange the status line height and vsep width.
1531 */
1532 wp2 = curwin->w_prev;
1533 frp2 = curwin->w_frame->fr_prev;
1534 if (wp->w_prev != curwin)
1535 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001536 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 frame_remove(curwin->w_frame);
1538 win_append(wp->w_prev, curwin);
1539 frame_insert(frp, curwin->w_frame);
1540 }
1541 if (wp != wp2)
1542 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001543 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 frame_remove(wp->w_frame);
1545 win_append(wp2, wp);
1546 if (frp2 == NULL)
1547 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1548 else
1549 frame_append(frp2, wp->w_frame);
1550 }
1551 temp = curwin->w_status_height;
1552 curwin->w_status_height = wp->w_status_height;
1553 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 temp = curwin->w_vsep_width;
1555 curwin->w_vsep_width = wp->w_vsep_width;
1556 wp->w_vsep_width = temp;
1557
1558 /* If the windows are not in the same frame, exchange the sizes to avoid
1559 * messing up the window layout. Otherwise fix the frame sizes. */
1560 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1561 {
1562 temp = curwin->w_height;
1563 curwin->w_height = wp->w_height;
1564 wp->w_height = temp;
1565 temp = curwin->w_width;
1566 curwin->w_width = wp->w_width;
1567 wp->w_width = temp;
1568 }
1569 else
1570 {
1571 frame_fix_height(curwin);
1572 frame_fix_height(wp);
1573 frame_fix_width(curwin);
1574 frame_fix_width(wp);
1575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 (void)win_comp_pos(); /* recompute window positions */
1578
1579 win_enter(wp, TRUE);
1580 redraw_later(CLEAR);
1581}
1582
1583/*
1584 * rotate windows: if upwards TRUE the second window becomes the first one
1585 * if upwards FALSE the first window becomes the second one
1586 */
1587 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001588win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589{
1590 win_T *wp1;
1591 win_T *wp2;
1592 frame_T *frp;
1593 int n;
1594
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001595 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 {
1597 beep_flush();
1598 return;
1599 }
1600
1601#ifdef FEAT_GUI
1602 need_mouse_correct = TRUE;
1603#endif
1604
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 /* Check if all frames in this row/col have one window. */
1606 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1607 frp = frp->fr_next)
1608 if (frp->fr_win == NULL)
1609 {
1610 EMSG(_("E443: Cannot rotate when another window is split"));
1611 return;
1612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614 while (count--)
1615 {
1616 if (upwards) /* first window becomes last window */
1617 {
1618 /* remove first window/frame from the list */
1619 frp = curwin->w_frame->fr_parent->fr_child;
1620 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001621 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 frame_remove(frp);
1623
1624 /* find last frame and append removed window/frame after it */
1625 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1626 ;
1627 win_append(frp->fr_win, wp1);
1628 frame_append(frp, wp1->w_frame);
1629
1630 wp2 = frp->fr_win; /* previously last window */
1631 }
1632 else /* last window becomes first window */
1633 {
1634 /* find last window/frame in the list and remove it */
1635 for (frp = curwin->w_frame; frp->fr_next != NULL;
1636 frp = frp->fr_next)
1637 ;
1638 wp1 = frp->fr_win;
1639 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001640 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 frame_remove(frp);
1642
1643 /* append the removed window/frame before the first in the list */
1644 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1645 frame_insert(frp->fr_parent->fr_child, frp);
1646 }
1647
1648 /* exchange status height and vsep width of old and new last window */
1649 n = wp2->w_status_height;
1650 wp2->w_status_height = wp1->w_status_height;
1651 wp1->w_status_height = n;
1652 frame_fix_height(wp1);
1653 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 n = wp2->w_vsep_width;
1655 wp2->w_vsep_width = wp1->w_vsep_width;
1656 wp1->w_vsep_width = n;
1657 frame_fix_width(wp1);
1658 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001660 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 (void)win_comp_pos();
1662 }
1663
1664 redraw_later(CLEAR);
1665}
1666
1667/*
1668 * Move the current window to the very top/bottom/left/right of the screen.
1669 */
1670 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001671win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 int dir;
1674 int height = curwin->w_height;
1675
Bram Moolenaar459ca562016-11-10 18:16:33 +01001676 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 {
1678 beep_flush();
1679 return;
1680 }
1681
1682 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001683 (void)winframe_remove(curwin, &dir, NULL);
1684 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 last_status(FALSE); /* may need to remove last status line */
1686 (void)win_comp_pos(); /* recompute window positions */
1687
1688 /* Split a window on the desired side and put the window there. */
1689 (void)win_split_ins(size, flags, curwin, dir);
1690 if (!(flags & WSP_VERT))
1691 {
1692 win_setheight(height);
1693 if (p_ea)
1694 win_equal(curwin, TRUE, 'v');
1695 }
1696
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001697#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1699 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001700 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702}
1703
1704/*
1705 * Move window "win1" to below/right of "win2" and make "win1" the current
1706 * window. Only works within the same frame!
1707 */
1708 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001709win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710{
1711 int height;
1712
1713 /* check if the arguments are reasonable */
1714 if (win1 == win2)
1715 return;
1716
1717 /* check if there is something to do */
1718 if (win2->w_next != win1)
1719 {
1720 /* may need move the status line/vertical separator of the last window
1721 * */
1722 if (win1 == lastwin)
1723 {
1724 height = win1->w_prev->w_status_height;
1725 win1->w_prev->w_status_height = win1->w_status_height;
1726 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001727 if (win1->w_prev->w_vsep_width == 1)
1728 {
1729 /* Remove the vertical separator from the last-but-one window,
1730 * add it to the last window. Adjust the frame widths. */
1731 win1->w_prev->w_vsep_width = 0;
1732 win1->w_prev->w_frame->fr_width -= 1;
1733 win1->w_vsep_width = 1;
1734 win1->w_frame->fr_width += 1;
1735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 }
1737 else if (win2 == lastwin)
1738 {
1739 height = win1->w_status_height;
1740 win1->w_status_height = win2->w_status_height;
1741 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001742 if (win1->w_vsep_width == 1)
1743 {
1744 /* Remove the vertical separator from win1, add it to the last
1745 * window, win2. Adjust the frame widths. */
1746 win2->w_vsep_width = 1;
1747 win2->w_frame->fr_width += 1;
1748 win1->w_vsep_width = 0;
1749 win1->w_frame->fr_width -= 1;
1750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001752 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 frame_remove(win1->w_frame);
1754 win_append(win2, win1);
1755 frame_append(win2->w_frame, win1->w_frame);
1756
1757 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1758 redraw_later(NOT_VALID);
1759 }
1760 win_enter(win1, FALSE);
1761}
1762
1763/*
1764 * Make all windows the same height.
1765 * 'next_curwin' will soon be the current window, make sure it has enough
1766 * rows.
1767 */
1768 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001769win_equal(
1770 win_T *next_curwin, /* pointer to current window to be or NULL */
1771 int current, /* do only frame with current window */
1772 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 'b' for both, 0 for using p_ead */
1774{
1775 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001778 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001779 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780}
1781
1782/*
1783 * Set a frame to a new position and height, spreading the available room
1784 * equally over contained frames.
1785 * The window "next_curwin" (if not NULL) should at least get the size from
1786 * 'winheight' and 'winwidth' if possible.
1787 */
1788 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001789win_equal_rec(
1790 win_T *next_curwin, /* pointer to current window to be or NULL */
1791 int current, /* do only frame with current window */
1792 frame_T *topfr, /* frame to set size off */
1793 int dir, /* 'v', 'h' or 'b', see win_equal() */
1794 int col, /* horizontal position for frame */
1795 int row, /* vertical position for frame */
1796 int width, /* new width of frame */
1797 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 int n, m;
1800 int extra_sep = 0;
1801 int wincount, totwincount = 0;
1802 frame_T *fr;
1803 int next_curwin_size = 0;
1804 int room = 0;
1805 int new_size;
1806 int has_next_curwin = 0;
1807 int hnc;
1808
1809 if (topfr->fr_layout == FR_LEAF)
1810 {
1811 /* Set the width/height of this frame.
1812 * Redraw when size or position changes */
1813 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 )
1816 {
1817 topfr->fr_win->w_winrow = row;
1818 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001820 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 redraw_all_later(CLEAR);
1822 }
1823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 else if (topfr->fr_layout == FR_ROW)
1825 {
1826 topfr->fr_width = width;
1827 topfr->fr_height = height;
1828
1829 if (dir != 'v') /* equalize frame widths */
1830 {
1831 /* Compute the maximum number of windows horizontally in this
1832 * frame. */
1833 n = frame_minwidth(topfr, NOWIN);
1834 /* add one for the rightmost window, it doesn't have a separator */
1835 if (col + width == Columns)
1836 extra_sep = 1;
1837 else
1838 extra_sep = 0;
1839 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001840 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001842 /*
1843 * Compute width for "next_curwin" window and room available for
1844 * other windows.
1845 * "m" is the minimal width when counting p_wiw for "next_curwin".
1846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 m = frame_minwidth(topfr, next_curwin);
1848 room = width - m;
1849 if (room < 0)
1850 {
1851 next_curwin_size = p_wiw + room;
1852 room = 0;
1853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 else
1855 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001856 next_curwin_size = -1;
1857 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1858 {
1859 /* If 'winfixwidth' set keep the window width if
1860 * possible.
1861 * Watch out for this window being the next_curwin. */
1862 if (frame_fixed_width(fr))
1863 {
1864 n = frame_minwidth(fr, NOWIN);
1865 new_size = fr->fr_width;
1866 if (frame_has_win(fr, next_curwin))
1867 {
1868 room += p_wiw - p_wmw;
1869 next_curwin_size = 0;
1870 if (new_size < p_wiw)
1871 new_size = p_wiw;
1872 }
1873 else
1874 /* These windows don't use up room. */
1875 totwincount -= (n + (fr->fr_next == NULL
1876 ? extra_sep : 0)) / (p_wmw + 1);
1877 room -= new_size - n;
1878 if (room < 0)
1879 {
1880 new_size += room;
1881 room = 0;
1882 }
1883 fr->fr_newwidth = new_size;
1884 }
1885 }
1886 if (next_curwin_size == -1)
1887 {
1888 if (!has_next_curwin)
1889 next_curwin_size = 0;
1890 else if (totwincount > 1
1891 && (room + (totwincount - 2))
1892 / (totwincount - 1) > p_wiw)
1893 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001894 /* Can make all windows wider than 'winwidth', spread
1895 * the room equally. */
1896 next_curwin_size = (room + p_wiw
1897 + (totwincount - 1) * p_wmw
1898 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001899 room -= next_curwin_size - p_wiw;
1900 }
1901 else
1902 next_curwin_size = p_wiw;
1903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001905
1906 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 --totwincount; /* don't count curwin */
1908 }
1909
1910 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1911 {
1912 n = m = 0;
1913 wincount = 1;
1914 if (fr->fr_next == NULL)
1915 /* last frame gets all that remains (avoid roundoff error) */
1916 new_size = width;
1917 else if (dir == 'v')
1918 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001919 else if (frame_fixed_width(fr))
1920 {
1921 new_size = fr->fr_newwidth;
1922 wincount = 0; /* doesn't count as a sizeable window */
1923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 else
1925 {
1926 /* Compute the maximum number of windows horiz. in "fr". */
1927 n = frame_minwidth(fr, NOWIN);
1928 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1929 / (p_wmw + 1);
1930 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001931 if (has_next_curwin)
1932 hnc = frame_has_win(fr, next_curwin);
1933 else
1934 hnc = FALSE;
1935 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001937 if (totwincount == 0)
1938 new_size = room;
1939 else
1940 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001942 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
1944 next_curwin_size -= p_wiw - (m - n);
1945 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001946 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001948 else
1949 room -= new_size;
1950 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 }
1952
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001953 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 * window, unless equalizing all frames. */
1955 if (!current || dir != 'v' || topfr->fr_parent != NULL
1956 || (new_size != fr->fr_width)
1957 || frame_has_win(fr, next_curwin))
1958 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001959 new_size, height);
1960 col += new_size;
1961 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 totwincount -= wincount;
1963 }
1964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 else /* topfr->fr_layout == FR_COL */
1966 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 topfr->fr_height = height;
1969
1970 if (dir != 'h') /* equalize frame heights */
1971 {
1972 /* Compute maximum number of windows vertically in this frame. */
1973 n = frame_minheight(topfr, NOWIN);
1974 /* add one for the bottom window if it doesn't have a statusline */
1975 if (row + height == cmdline_row && p_ls == 0)
1976 extra_sep = 1;
1977 else
1978 extra_sep = 0;
1979 totwincount = (n + extra_sep) / (p_wmh + 1);
1980 has_next_curwin = frame_has_win(topfr, next_curwin);
1981
1982 /*
1983 * Compute height for "next_curwin" window and room available for
1984 * other windows.
1985 * "m" is the minimal height when counting p_wh for "next_curwin".
1986 */
1987 m = frame_minheight(topfr, next_curwin);
1988 room = height - m;
1989 if (room < 0)
1990 {
1991 /* The room is less then 'winheight', use all space for the
1992 * current window. */
1993 next_curwin_size = p_wh + room;
1994 room = 0;
1995 }
1996 else
1997 {
1998 next_curwin_size = -1;
1999 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2000 {
2001 /* If 'winfixheight' set keep the window height if
2002 * possible.
2003 * Watch out for this window being the next_curwin. */
2004 if (frame_fixed_height(fr))
2005 {
2006 n = frame_minheight(fr, NOWIN);
2007 new_size = fr->fr_height;
2008 if (frame_has_win(fr, next_curwin))
2009 {
2010 room += p_wh - p_wmh;
2011 next_curwin_size = 0;
2012 if (new_size < p_wh)
2013 new_size = p_wh;
2014 }
2015 else
2016 /* These windows don't use up room. */
2017 totwincount -= (n + (fr->fr_next == NULL
2018 ? extra_sep : 0)) / (p_wmh + 1);
2019 room -= new_size - n;
2020 if (room < 0)
2021 {
2022 new_size += room;
2023 room = 0;
2024 }
2025 fr->fr_newheight = new_size;
2026 }
2027 }
2028 if (next_curwin_size == -1)
2029 {
2030 if (!has_next_curwin)
2031 next_curwin_size = 0;
2032 else if (totwincount > 1
2033 && (room + (totwincount - 2))
2034 / (totwincount - 1) > p_wh)
2035 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002036 /* can make all windows higher than 'winheight',
2037 * spread the room equally. */
2038 next_curwin_size = (room + p_wh
2039 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 + (totwincount - 1)) / totwincount;
2041 room -= next_curwin_size - p_wh;
2042 }
2043 else
2044 next_curwin_size = p_wh;
2045 }
2046 }
2047
2048 if (has_next_curwin)
2049 --totwincount; /* don't count curwin */
2050 }
2051
2052 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2053 {
2054 n = m = 0;
2055 wincount = 1;
2056 if (fr->fr_next == NULL)
2057 /* last frame gets all that remains (avoid roundoff error) */
2058 new_size = height;
2059 else if (dir == 'h')
2060 new_size = fr->fr_height;
2061 else if (frame_fixed_height(fr))
2062 {
2063 new_size = fr->fr_newheight;
2064 wincount = 0; /* doesn't count as a sizeable window */
2065 }
2066 else
2067 {
2068 /* Compute the maximum number of windows vert. in "fr". */
2069 n = frame_minheight(fr, NOWIN);
2070 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2071 / (p_wmh + 1);
2072 m = frame_minheight(fr, next_curwin);
2073 if (has_next_curwin)
2074 hnc = frame_has_win(fr, next_curwin);
2075 else
2076 hnc = FALSE;
2077 if (hnc) /* don't count next_curwin */
2078 --wincount;
2079 if (totwincount == 0)
2080 new_size = room;
2081 else
2082 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2083 / totwincount;
2084 if (hnc) /* add next_curwin size */
2085 {
2086 next_curwin_size -= p_wh - (m - n);
2087 new_size += next_curwin_size;
2088 room -= new_size - next_curwin_size;
2089 }
2090 else
2091 room -= new_size;
2092 new_size += n;
2093 }
2094 /* Skip frame that is full width when splitting or closing a
2095 * window, unless equalizing all frames. */
2096 if (!current || dir != 'h' || topfr->fr_parent != NULL
2097 || (new_size != fr->fr_height)
2098 || frame_has_win(fr, next_curwin))
2099 win_equal_rec(next_curwin, current, fr, dir, col, row,
2100 width, new_size);
2101 row += new_size;
2102 height -= new_size;
2103 totwincount -= wincount;
2104 }
2105 }
2106}
2107
2108/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002109 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 */
2111 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002112close_windows(
2113 buf_T *buf,
2114 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002116 win_T *wp;
2117 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002118 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002119#ifdef FEAT_AUTOCMD
2120 int count = tabpage_index(NULL);
2121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002124
Bram Moolenaar459ca562016-11-10 18:16:33 +01002125 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002127 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2128#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002129 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002130#endif
2131 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002133 if (win_close(wp, FALSE) == FAIL)
2134 /* If closing the window fails give up, to avoid looping
2135 * forever. */
2136 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002137
2138 /* Start all over, autocommands may change the window layout. */
2139 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002142 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002144
2145 /* Also check windows in other tab pages. */
2146 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2147 {
2148 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002149 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002150 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002151 if (wp->w_buffer == buf
2152#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002153 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002154#endif
2155 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002156 {
2157 win_close_othertab(wp, FALSE, tp);
2158
2159 /* Start all over, the tab page may be closed and
2160 * autocommands may change the window layout. */
2161 nexttp = first_tabpage;
2162 break;
2163 }
2164 }
2165
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002167
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002168#ifdef FEAT_AUTOCMD
2169 if (count != tabpage_index(NULL))
2170 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
2171#endif
2172
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002173 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002174 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002175 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176}
2177
2178/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002179 * Return TRUE if the current window is the only window that exists (ignoring
2180 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002181 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002182 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002183 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002184last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002185{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002186 return (one_window() && first_tabpage->tp_next == NULL);
2187}
2188
2189/*
2190 * Return TRUE if there is only one window other than "aucmd_win" in the
2191 * current tab page.
2192 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002193 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002194one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002195{
2196#ifdef FEAT_AUTOCMD
2197 win_T *wp;
2198 int seen_one = FALSE;
2199
2200 FOR_ALL_WINDOWS(wp)
2201 {
2202 if (wp != aucmd_win)
2203 {
2204 if (seen_one)
2205 return FALSE;
2206 seen_one = TRUE;
2207 }
2208 }
2209 return TRUE;
2210#else
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002211 return ONE_WINDOW;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002212#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002213}
2214
2215/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002216 * Close the possibly last window in a tab page.
2217 * Returns TRUE when the window was closed already.
2218 */
2219 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002220close_last_window_tabpage(
2221 win_T *win,
2222 int free_buf,
2223 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002224{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002225 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002226 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002227#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002228 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002229#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002230
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002231 /*
2232 * Closing the last window in a tab page. First go to another tab
2233 * page and then close the window and the tab page. This avoids that
2234 * curwin and curtab are invalid while we are freeing memory, they may
2235 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002236 * Don't trigger autocommands yet, they may use wrong values, so do
2237 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002238 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002239 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002240 redraw_tabline = TRUE;
2241
2242 /* Safety check: Autocommands may have closed the window when jumping
2243 * to the other tab page. */
2244 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2245 {
2246 int h = tabline_height();
2247
2248 win_close_othertab(win, free_buf, prev_curtab);
2249 if (h != tabline_height())
2250 shell_new_rows();
2251 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002252 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2253 * that now. */
2254#ifdef FEAT_AUTOCMD
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002255 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002256 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002257 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2258 if (old_curbuf != curbuf)
2259 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002260#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002261 return TRUE;
2262 }
2263 return FALSE;
2264}
2265
2266/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 * If "free_buf" is TRUE related buffer may be unloaded.
2269 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002270 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002271 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002273 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002274win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
2276 win_T *wp;
2277#ifdef FEAT_AUTOCMD
2278 int other_buffer = FALSE;
2279#endif
2280 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 int dir;
2282 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002283 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002284 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002286 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 {
2288 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002289 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 }
2291
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002292#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002293 if (win->w_closing || (win->w_buffer != NULL
2294 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002295 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002296 if (win == aucmd_win)
2297 {
2298 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002299 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002300 }
2301 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2302 {
2303 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002304 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002305 }
2306#endif
2307
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002308 /* When closing the last window in a tab page first go to another tab page
2309 * and then close the window and the tab page to avoid that curwin and
2310 * curtab are invalid while we are freeing memory. */
2311 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002312 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002313
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 /* When closing the help window, try restoring a snapshot after closing
2315 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002316 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 help_window = TRUE;
2318 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002319 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321#ifdef FEAT_AUTOCMD
2322 if (win == curwin)
2323 {
2324 /*
2325 * Guess which window is going to be the new current window.
2326 * This may change because of the autocommands (sigh).
2327 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002328 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
2330 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002331 * Be careful: If autocommands delete the window or cause this window
2332 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 */
2334 if (wp->w_buffer != curbuf)
2335 {
2336 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002337 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002339 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002340 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002341 win->w_closing = FALSE;
2342 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002345 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002347 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002348 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002349 win->w_closing = FALSE;
2350 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352# ifdef FEAT_EVAL
2353 /* autocmds may abort script processing */
2354 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356# endif
2357 }
2358#endif
2359
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002360#ifdef FEAT_GUI
2361 /* Avoid trouble with scrollbars that are going to be deleted in
2362 * win_free(). */
2363 if (gui.in_use)
2364 out_flush();
2365#endif
2366
Bram Moolenaara971b822011-09-14 14:43:25 +02002367#ifdef FEAT_SYN_HL
2368 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002369 if (win->w_buffer != NULL)
2370 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002371#endif
2372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 /*
2374 * Close the link to the buffer.
2375 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002376 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002377 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002378 bufref_T bufref;
2379
2380 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002381#ifdef FEAT_AUTOCMD
2382 win->w_closing = TRUE;
2383#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002384 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002385#ifdef FEAT_AUTOCMD
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002386 if (win_valid_any_tab(win))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002387 win->w_closing = FALSE;
2388#endif
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002389 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2390 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002391 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002392 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002393 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002394
Bram Moolenaar802418d2013-01-17 14:00:11 +01002395 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2396 && (last_window() || curtab != prev_curtab
2397 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002398 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002399 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002400 * curwin->w_buffer, otherwise writing viminfo may fail. */
2401 if (curwin->w_buffer == NULL)
2402 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002403 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002404 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002405
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002406 /* Autocommands may have moved to another tab page. */
2407 if (curtab != prev_curtab && win_valid_any_tab(win)
2408 && win->w_buffer == NULL)
2409 {
2410 /* Need to close the window anyway, since the buffer is NULL. */
2411 win_close_othertab(win, FALSE, prev_curtab);
2412 return FAIL;
2413 }
2414
2415 /* Autocommands may have closed the window already or closed the only
2416 * other window. */
2417 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002418 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002419 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420
Bram Moolenaara971b822011-09-14 14:43:25 +02002421 /* Free the memory used for the window and get the window that received
2422 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002423 wp = win_free_mem(win, &dir, NULL);
2424
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 /* Make sure curwin isn't invalid. It can cause severe trouble when
2426 * printing an error message. For win_equal() curbuf needs to be valid
2427 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002428 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 {
2430 curwin = wp;
2431#ifdef FEAT_QUICKFIX
2432 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2433 {
2434 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002435 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 * finding another window to go to.
2437 */
2438 for (;;)
2439 {
2440 if (wp->w_next == NULL)
2441 wp = firstwin;
2442 else
2443 wp = wp->w_next;
2444 if (wp == curwin)
2445 break;
2446 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2447 {
2448 curwin = wp;
2449 break;
2450 }
2451 }
2452 }
2453#endif
2454 curbuf = curwin->w_buffer;
2455 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002456
2457 /* The cursor position may be invalid if the buffer changed after last
2458 * using the window. */
2459 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002461 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002462 /* If the frame of the closed window contains the new current window,
2463 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002464 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 else
2466 win_comp_pos();
2467 if (close_curwin)
2468 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002469 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470#ifdef FEAT_AUTOCMD
2471 if (other_buffer)
2472 /* careful: after this wp and win may be invalid! */
2473 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2474#endif
2475 }
2476
2477 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002478 * If last window has a status line now and we don't want one,
2479 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
2481 last_status(FALSE);
2482
2483 /* After closing the help window, try restoring the window layout from
2484 * before it was opened. */
2485 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002486 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002488#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2490 if (gui.in_use && !win_hasvertsplit())
2491 gui_init_which_components(NULL);
2492#endif
2493
2494 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002495 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496}
2497
2498/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002499 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002500 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002501 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002502 * Caller must check if buffer is hidden and whether the tabline needs to be
2503 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002504 */
2505 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002506win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002507{
2508 win_T *wp;
2509 int dir;
2510 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002511 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002512
Bram Moolenaar362ce482012-06-06 19:02:45 +02002513#ifdef FEAT_AUTOCMD
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002514 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2515 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002516 if (win->w_closing || (win->w_buffer != NULL
2517 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002518 return; /* window is already being closed */
2519#endif
2520
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002521 if (win->w_buffer != NULL)
2522 /* Close the link to the buffer. */
2523 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002524
2525 /* Careful: Autocommands may have closed the tab page or made it the
2526 * current tab page. */
2527 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2528 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002529 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002530 return;
2531
2532 /* Autocommands may have closed the window already. */
2533 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2534 ;
2535 if (wp == NULL)
2536 return;
2537
Bram Moolenaarf740b292006-02-16 22:11:02 +00002538 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002539 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002540 {
2541 if (tp == first_tabpage)
2542 first_tabpage = tp->tp_next;
2543 else
2544 {
2545 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2546 ptp = ptp->tp_next)
2547 ;
2548 if (ptp == NULL)
2549 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002550 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002551 return;
2552 }
2553 ptp->tp_next = tp->tp_next;
2554 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002555 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002556 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002557
2558 /* Free the memory used for the window. */
2559 win_free_mem(win, &dir, tp);
2560
2561 if (free_tp)
2562 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002563}
2564
2565/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002566 * Free the memory used for a window.
2567 * Returns a pointer to the window that got the freed up space.
2568 */
2569 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002570win_free_mem(
2571 win_T *win,
2572 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2573 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002574{
2575 frame_T *frp;
2576 win_T *wp;
2577
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002578 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002579 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002580 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002581 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002582 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002583
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002584 /* When deleting the current window of another tab page select a new
2585 * current window. */
2586 if (tp != NULL && win == tp->tp_curwin)
2587 tp->tp_curwin = wp;
2588
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002589 return wp;
2590}
2591
2592#if defined(EXITFREE) || defined(PROTO)
2593 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002594win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002595{
2596 int dummy;
2597
Bram Moolenaarf740b292006-02-16 22:11:02 +00002598 while (first_tabpage->tp_next != NULL)
2599 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002600
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002601# ifdef FEAT_AUTOCMD
2602 if (aucmd_win != NULL)
2603 {
2604 (void)win_free_mem(aucmd_win, &dummy, NULL);
2605 aucmd_win = NULL;
2606 }
2607# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002608
2609 while (firstwin != NULL)
2610 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002611
2612 /* No window should be used after this. Set curwin to NULL to crash
2613 * instead of using freed memory. */
2614 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002615}
2616#endif
2617
2618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 * Remove a window and its frame from the tree of frames.
2620 * Returns a pointer to the window that got the freed up space.
2621 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002622 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002623winframe_remove(
2624 win_T *win,
2625 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2626 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 frame_T *frp, *frp2, *frp3;
2629 frame_T *frp_close = win->w_frame;
2630 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631
2632 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002633 * If there is only one window there is nothing to remove.
2634 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002635 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002636 return NULL;
2637
2638 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 * Remove the window from its frame.
2640 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002641 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 wp = frame2win(frp2);
2643
2644 /* Remove this frame from the list of frames. */
2645 frame_remove(frp_close);
2646
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 if (frp_close->fr_parent->fr_layout == FR_COL)
2648 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002649 /* When 'winfixheight' is set, try to find another frame in the column
2650 * (as close to the closed frame as possible) to distribute the height
2651 * to. */
2652 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2653 {
2654 frp = frp_close->fr_prev;
2655 frp3 = frp_close->fr_next;
2656 while (frp != NULL || frp3 != NULL)
2657 {
2658 if (frp != NULL)
2659 {
2660 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2661 {
2662 frp2 = frp;
2663 wp = frp->fr_win;
2664 break;
2665 }
2666 frp = frp->fr_prev;
2667 }
2668 if (frp3 != NULL)
2669 {
2670 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2671 {
2672 frp2 = frp3;
2673 wp = frp3->fr_win;
2674 break;
2675 }
2676 frp3 = frp3->fr_next;
2677 }
2678 }
2679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2681 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 *dirp = 'v';
2683 }
2684 else
2685 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002686 /* When 'winfixwidth' is set, try to find another frame in the column
2687 * (as close to the closed frame as possible) to distribute the width
2688 * to. */
2689 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2690 {
2691 frp = frp_close->fr_prev;
2692 frp3 = frp_close->fr_next;
2693 while (frp != NULL || frp3 != NULL)
2694 {
2695 if (frp != NULL)
2696 {
2697 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2698 {
2699 frp2 = frp;
2700 wp = frp->fr_win;
2701 break;
2702 }
2703 frp = frp->fr_prev;
2704 }
2705 if (frp3 != NULL)
2706 {
2707 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2708 {
2709 frp2 = frp3;
2710 wp = frp3->fr_win;
2711 break;
2712 }
2713 frp3 = frp3->fr_next;
2714 }
2715 }
2716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002718 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 *dirp = 'h';
2720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 /* If rows/columns go to a window below/right its positions need to be
2723 * updated. Can only be done after the sizes have been updated. */
2724 if (frp2 == frp_close->fr_next)
2725 {
2726 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002727 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 frame_comp_pos(frp2, &row, &col);
2730 }
2731
2732 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2733 {
2734 /* There is no other frame in this list, move its info to the parent
2735 * and remove it. */
2736 frp2->fr_parent->fr_layout = frp2->fr_layout;
2737 frp2->fr_parent->fr_child = frp2->fr_child;
2738 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2739 frp->fr_parent = frp2->fr_parent;
2740 frp2->fr_parent->fr_win = frp2->fr_win;
2741 if (frp2->fr_win != NULL)
2742 frp2->fr_win->w_frame = frp2->fr_parent;
2743 frp = frp2->fr_parent;
2744 vim_free(frp2);
2745
2746 frp2 = frp->fr_parent;
2747 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2748 {
2749 /* The frame above the parent has the same layout, have to merge
2750 * the frames into this list. */
2751 if (frp2->fr_child == frp)
2752 frp2->fr_child = frp->fr_child;
2753 frp->fr_child->fr_prev = frp->fr_prev;
2754 if (frp->fr_prev != NULL)
2755 frp->fr_prev->fr_next = frp->fr_child;
2756 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2757 {
2758 frp3->fr_parent = frp2;
2759 if (frp3->fr_next == NULL)
2760 {
2761 frp3->fr_next = frp->fr_next;
2762 if (frp->fr_next != NULL)
2763 frp->fr_next->fr_prev = frp3;
2764 break;
2765 }
2766 }
2767 vim_free(frp);
2768 }
2769 }
2770
2771 return wp;
2772}
2773
2774/*
2775 * Find out which frame is going to get the freed up space when "win" is
2776 * closed.
2777 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2778 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2779 * This makes opening a window and closing it immediately keep the same window
2780 * layout.
2781 */
2782 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002783win_altframe(
2784 win_T *win,
2785 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
2787 frame_T *frp;
2788 int b;
2789
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002790 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002791 /* Last window in this tab page, will go to next tab page. */
2792 return alt_tabpage()->tp_curwin->w_frame;
2793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 frp = win->w_frame;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002795 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 b = p_spr;
2797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 b = p_sb;
2799 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2800 return frp->fr_next;
2801 return frp->fr_prev;
2802}
2803
2804/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002805 * Return the tabpage that will be used if the current one is closed.
2806 */
2807 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002808alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002809{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002810 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002811
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002812 /* Use the next tab page if possible. */
2813 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002814 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002815
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002816 /* Find the last but one tab page. */
2817 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2818 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002819 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002820}
2821
2822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 * Find the left-upper window in frame "frp".
2824 */
2825 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002826frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 while (frp->fr_win == NULL)
2829 frp = frp->fr_child;
2830 return frp->fr_win;
2831}
2832
2833/*
2834 * Return TRUE if frame "frp" contains window "wp".
2835 */
2836 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002837frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
2839 frame_T *p;
2840
2841 if (frp->fr_layout == FR_LEAF)
2842 return frp->fr_win == wp;
2843
2844 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2845 if (frame_has_win(p, wp))
2846 return TRUE;
2847 return FALSE;
2848}
2849
2850/*
2851 * Set a new height for a frame. Recursively sets the height for contained
2852 * frames and windows. Caller must take care of positions.
2853 */
2854 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002855frame_new_height(
2856 frame_T *topfrp,
2857 int height,
2858 int topfirst, /* resize topmost contained frame first */
2859 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 may cause the height not to be set */
2861{
2862 frame_T *frp;
2863 int extra_lines;
2864 int h;
2865
2866 if (topfrp->fr_win != NULL)
2867 {
2868 /* Simple case: just one window. */
2869 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02002870 height - topfrp->fr_win->w_status_height
2871#ifdef FEAT_MENU
2872 - topfrp->fr_win->w_winbar_height
2873#endif
2874 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 else if (topfrp->fr_layout == FR_ROW)
2877 {
2878 do
2879 {
2880 /* All frames in this row get the same new height. */
2881 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2882 {
2883 frame_new_height(frp, height, topfirst, wfh);
2884 if (frp->fr_height > height)
2885 {
2886 /* Could not fit the windows, make the whole row higher. */
2887 height = frp->fr_height;
2888 break;
2889 }
2890 }
2891 }
2892 while (frp != NULL);
2893 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002894 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
2896 /* Complicated case: Resize a column of frames. Resize the bottom
2897 * frame first, frames above that when needed. */
2898
2899 frp = topfrp->fr_child;
2900 if (wfh)
2901 /* Advance past frames with one window with 'wfh' set. */
2902 while (frame_fixed_height(frp))
2903 {
2904 frp = frp->fr_next;
2905 if (frp == NULL)
2906 return; /* no frame without 'wfh', give up */
2907 }
2908 if (!topfirst)
2909 {
2910 /* Find the bottom frame of this column */
2911 while (frp->fr_next != NULL)
2912 frp = frp->fr_next;
2913 if (wfh)
2914 /* Advance back for frames with one window with 'wfh' set. */
2915 while (frame_fixed_height(frp))
2916 frp = frp->fr_prev;
2917 }
2918
2919 extra_lines = height - topfrp->fr_height;
2920 if (extra_lines < 0)
2921 {
2922 /* reduce height of contained frames, bottom or top frame first */
2923 while (frp != NULL)
2924 {
2925 h = frame_minheight(frp, NULL);
2926 if (frp->fr_height + extra_lines < h)
2927 {
2928 extra_lines += frp->fr_height - h;
2929 frame_new_height(frp, h, topfirst, wfh);
2930 }
2931 else
2932 {
2933 frame_new_height(frp, frp->fr_height + extra_lines,
2934 topfirst, wfh);
2935 break;
2936 }
2937 if (topfirst)
2938 {
2939 do
2940 frp = frp->fr_next;
2941 while (wfh && frp != NULL && frame_fixed_height(frp));
2942 }
2943 else
2944 {
2945 do
2946 frp = frp->fr_prev;
2947 while (wfh && frp != NULL && frame_fixed_height(frp));
2948 }
2949 /* Increase "height" if we could not reduce enough frames. */
2950 if (frp == NULL)
2951 height -= extra_lines;
2952 }
2953 }
2954 else if (extra_lines > 0)
2955 {
2956 /* increase height of bottom or top frame */
2957 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2958 }
2959 }
2960 topfrp->fr_height = height;
2961}
2962
2963/*
2964 * Return TRUE if height of frame "frp" should not be changed because of
2965 * the 'winfixheight' option.
2966 */
2967 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002968frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969{
2970 /* frame with one window: fixed height if 'winfixheight' set. */
2971 if (frp->fr_win != NULL)
2972 return frp->fr_win->w_p_wfh;
2973
2974 if (frp->fr_layout == FR_ROW)
2975 {
2976 /* The frame is fixed height if one of the frames in the row is fixed
2977 * height. */
2978 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2979 if (frame_fixed_height(frp))
2980 return TRUE;
2981 return FALSE;
2982 }
2983
2984 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2985 * frames in the row are fixed height. */
2986 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2987 if (!frame_fixed_height(frp))
2988 return FALSE;
2989 return TRUE;
2990}
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002993 * Return TRUE if width of frame "frp" should not be changed because of
2994 * the 'winfixwidth' option.
2995 */
2996 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002997frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002998{
2999 /* frame with one window: fixed width if 'winfixwidth' set. */
3000 if (frp->fr_win != NULL)
3001 return frp->fr_win->w_p_wfw;
3002
3003 if (frp->fr_layout == FR_COL)
3004 {
3005 /* The frame is fixed width if one of the frames in the row is fixed
3006 * width. */
3007 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3008 if (frame_fixed_width(frp))
3009 return TRUE;
3010 return FALSE;
3011 }
3012
3013 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3014 * frames in the row are fixed width. */
3015 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3016 if (!frame_fixed_width(frp))
3017 return FALSE;
3018 return TRUE;
3019}
3020
3021/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 * Add a status line to windows at the bottom of "frp".
3023 * Note: Does not check if there is room!
3024 */
3025 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003026frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 win_T *wp;
3029
3030 if (frp->fr_layout == FR_LEAF)
3031 {
3032 wp = frp->fr_win;
3033 if (wp->w_status_height == 0)
3034 {
3035 if (wp->w_height > 0) /* don't make it negative */
3036 --wp->w_height;
3037 wp->w_status_height = STATUS_HEIGHT;
3038 }
3039 }
3040 else if (frp->fr_layout == FR_ROW)
3041 {
3042 /* Handle all the frames in the row. */
3043 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3044 frame_add_statusline(frp);
3045 }
3046 else /* frp->fr_layout == FR_COL */
3047 {
3048 /* Only need to handle the last frame in the column. */
3049 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3050 ;
3051 frame_add_statusline(frp);
3052 }
3053}
3054
3055/*
3056 * Set width of a frame. Handles recursively going through contained frames.
3057 * May remove separator line for windows at the right side (for win_close()).
3058 */
3059 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003060frame_new_width(
3061 frame_T *topfrp,
3062 int width,
3063 int leftfirst, /* resize leftmost contained frame first */
3064 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003065 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 frame_T *frp;
3068 int extra_cols;
3069 int w;
3070 win_T *wp;
3071
3072 if (topfrp->fr_layout == FR_LEAF)
3073 {
3074 /* Simple case: just one window. */
3075 wp = topfrp->fr_win;
3076 /* Find out if there are any windows right of this one. */
3077 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3078 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3079 break;
3080 if (frp->fr_parent == NULL)
3081 wp->w_vsep_width = 0;
3082 win_new_width(wp, width - wp->w_vsep_width);
3083 }
3084 else if (topfrp->fr_layout == FR_COL)
3085 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003086 do
3087 {
3088 /* All frames in this column get the same new width. */
3089 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3090 {
3091 frame_new_width(frp, width, leftfirst, wfw);
3092 if (frp->fr_width > width)
3093 {
3094 /* Could not fit the windows, make whole column wider. */
3095 width = frp->fr_width;
3096 break;
3097 }
3098 }
3099 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101 else /* fr_layout == FR_ROW */
3102 {
3103 /* Complicated case: Resize a row of frames. Resize the rightmost
3104 * frame first, frames left of it when needed. */
3105
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003107 if (wfw)
3108 /* Advance past frames with one window with 'wfw' set. */
3109 while (frame_fixed_width(frp))
3110 {
3111 frp = frp->fr_next;
3112 if (frp == NULL)
3113 return; /* no frame without 'wfw', give up */
3114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003116 {
3117 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 while (frp->fr_next != NULL)
3119 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003120 if (wfw)
3121 /* Advance back for frames with one window with 'wfw' set. */
3122 while (frame_fixed_width(frp))
3123 frp = frp->fr_prev;
3124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126 extra_cols = width - topfrp->fr_width;
3127 if (extra_cols < 0)
3128 {
3129 /* reduce frame width, rightmost frame first */
3130 while (frp != NULL)
3131 {
3132 w = frame_minwidth(frp, NULL);
3133 if (frp->fr_width + extra_cols < w)
3134 {
3135 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003136 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
3138 else
3139 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003140 frame_new_width(frp, frp->fr_width + extra_cols,
3141 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 break;
3143 }
3144 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003145 {
3146 do
3147 frp = frp->fr_next;
3148 while (wfw && frp != NULL && frame_fixed_width(frp));
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003151 {
3152 do
3153 frp = frp->fr_prev;
3154 while (wfw && frp != NULL && frame_fixed_width(frp));
3155 }
3156 /* Increase "width" if we could not reduce enough frames. */
3157 if (frp == NULL)
3158 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
3160 }
3161 else if (extra_cols > 0)
3162 {
3163 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003164 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 }
3166 }
3167 topfrp->fr_width = width;
3168}
3169
3170/*
3171 * Add the vertical separator to windows at the right side of "frp".
3172 * Note: Does not check if there is room!
3173 */
3174 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003175frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 win_T *wp;
3178
3179 if (frp->fr_layout == FR_LEAF)
3180 {
3181 wp = frp->fr_win;
3182 if (wp->w_vsep_width == 0)
3183 {
3184 if (wp->w_width > 0) /* don't make it negative */
3185 --wp->w_width;
3186 wp->w_vsep_width = 1;
3187 }
3188 }
3189 else if (frp->fr_layout == FR_COL)
3190 {
3191 /* Handle all the frames in the column. */
3192 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3193 frame_add_vsep(frp);
3194 }
3195 else /* frp->fr_layout == FR_ROW */
3196 {
3197 /* Only need to handle the last frame in the row. */
3198 frp = frp->fr_child;
3199 while (frp->fr_next != NULL)
3200 frp = frp->fr_next;
3201 frame_add_vsep(frp);
3202 }
3203}
3204
3205/*
3206 * Set frame width from the window it contains.
3207 */
3208 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003209frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3212}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213
3214/*
3215 * Set frame height from the window it contains.
3216 */
3217 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003218frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
Bram Moolenaard326ad62017-09-18 20:31:41 +02003220 wp->w_frame->fr_height = wp->w_height + wp->w_status_height
3221#ifdef FEAT_MENU
3222 + wp->w_winbar_height
3223#endif
3224 ;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225}
3226
3227/*
3228 * Compute the minimal height for frame "topfrp".
3229 * Uses the 'winminheight' option.
3230 * When "next_curwin" isn't NULL, use p_wh for this window.
3231 * When "next_curwin" is NOWIN, don't use at least one line for the current
3232 * window.
3233 */
3234 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003235frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 frame_T *frp;
3238 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
3241 if (topfrp->fr_win != NULL)
3242 {
3243 if (topfrp->fr_win == next_curwin)
3244 m = p_wh + topfrp->fr_win->w_status_height;
3245 else
3246 {
3247 /* window: minimal height of the window plus status line */
3248 m = p_wmh + topfrp->fr_win->w_status_height;
3249 /* Current window is minimal one line high */
3250 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3251 ++m;
3252 }
3253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 else if (topfrp->fr_layout == FR_ROW)
3255 {
3256 /* get the minimal height from each frame in this row */
3257 m = 0;
3258 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3259 {
3260 n = frame_minheight(frp, next_curwin);
3261 if (n > m)
3262 m = n;
3263 }
3264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 else
3266 {
3267 /* Add up the minimal heights for all frames in this column. */
3268 m = 0;
3269 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3270 m += frame_minheight(frp, next_curwin);
3271 }
3272
3273 return m;
3274}
3275
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276/*
3277 * Compute the minimal width for frame "topfrp".
3278 * When "next_curwin" isn't NULL, use p_wiw for this window.
3279 * When "next_curwin" is NOWIN, don't use at least one column for the current
3280 * window.
3281 */
3282 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003283frame_minwidth(
3284 frame_T *topfrp,
3285 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 frame_T *frp;
3288 int m, n;
3289
3290 if (topfrp->fr_win != NULL)
3291 {
3292 if (topfrp->fr_win == next_curwin)
3293 m = p_wiw + topfrp->fr_win->w_vsep_width;
3294 else
3295 {
3296 /* window: minimal width of the window plus separator column */
3297 m = p_wmw + topfrp->fr_win->w_vsep_width;
3298 /* Current window is minimal one column wide */
3299 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3300 ++m;
3301 }
3302 }
3303 else if (topfrp->fr_layout == FR_COL)
3304 {
3305 /* get the minimal width from each frame in this column */
3306 m = 0;
3307 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3308 {
3309 n = frame_minwidth(frp, next_curwin);
3310 if (n > m)
3311 m = n;
3312 }
3313 }
3314 else
3315 {
3316 /* Add up the minimal widths for all frames in this row. */
3317 m = 0;
3318 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3319 m += frame_minwidth(frp, next_curwin);
3320 }
3321
3322 return m;
3323}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324
3325
3326/*
3327 * Try to close all windows except current one.
3328 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3329 * used and the buffer was modified.
3330 *
3331 * Used by ":bdel" and ":only".
3332 */
3333 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003334close_others(
3335 int message,
3336 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337{
3338 win_T *wp;
3339 win_T *nextwp;
3340 int r;
3341
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003342 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
3344 if (message
3345#ifdef FEAT_AUTOCMD
3346 && !autocmd_busy
3347#endif
3348 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003349 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 return;
3351 }
3352
3353 /* Be very careful here: autocommands may change the window layout. */
3354 for (wp = firstwin; win_valid(wp); wp = nextwp)
3355 {
3356 nextwp = wp->w_next;
3357 if (wp != curwin) /* don't close current window */
3358 {
3359
3360 /* Check if it's allowed to abandon this window */
3361 r = can_abandon(wp->w_buffer, forceit);
3362#ifdef FEAT_AUTOCMD
3363 if (!win_valid(wp)) /* autocommands messed wp up */
3364 {
3365 nextwp = firstwin;
3366 continue;
3367 }
3368#endif
3369 if (!r)
3370 {
3371#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3372 if (message && (p_confirm || cmdmod.confirm) && p_write)
3373 {
3374 dialog_changed(wp->w_buffer, FALSE);
3375# ifdef FEAT_AUTOCMD
3376 if (!win_valid(wp)) /* autocommands messed wp up */
3377 {
3378 nextwp = firstwin;
3379 continue;
3380 }
3381# endif
3382 }
3383 if (bufIsChanged(wp->w_buffer))
3384#endif
3385 continue;
3386 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003387 win_close(wp, !buf_hide(wp->w_buffer)
3388 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390 }
3391
Bram Moolenaar459ca562016-11-10 18:16:33 +01003392 if (message && !ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 EMSG(_("E445: Other window contains changes"));
3394}
3395
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003397 * Init the current window "curwin".
3398 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 */
3400 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003401curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003403 win_init_empty(curwin);
3404}
3405
3406 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003407win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003408{
3409 redraw_win_later(wp, NOT_VALID);
3410 wp->w_lines_valid = 0;
3411 wp->w_cursor.lnum = 1;
3412 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003414 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003416 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3417 wp->w_pcmark.col = 0;
3418 wp->w_prev_pcmark.lnum = 0;
3419 wp->w_prev_pcmark.col = 0;
3420 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003422 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003424 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003426 if (wp->w_p_rl)
3427 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003429 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003431#ifdef FEAT_SYN_HL
3432 wp->w_s = &wp->w_buffer->b_s;
3433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434}
3435
3436/*
3437 * Allocate the first window and put an empty buffer in it.
3438 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003439 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003441 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003442win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003444 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003445 return FAIL;
3446
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003447 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003448 if (first_tabpage == NULL)
3449 return FAIL;
3450 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003451 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003452
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003453 return OK;
3454}
3455
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003456#if defined(FEAT_AUTOCMD) || defined(PROTO)
3457/*
3458 * Init "aucmd_win". This can only be done after the first
3459 * window is fully initialized, thus it can't be in win_alloc_first().
3460 */
3461 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003462win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003463{
3464 aucmd_win = win_alloc(NULL, TRUE);
3465 if (aucmd_win != NULL)
3466 {
3467 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003468 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003469 new_frame(aucmd_win);
3470 }
3471}
3472#endif
3473
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003474/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003475 * Allocate the first window or the first window in a new tab page.
3476 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003477 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003478 * Return FAIL when something goes wrong (out of memory).
3479 */
3480 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003481win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003482{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003483 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003484 if (oldwin == NULL)
3485 {
3486 /* Very first window, need to create an empty buffer for it and
3487 * initialize from scratch. */
3488 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3489 if (curwin == NULL || curbuf == NULL)
3490 return FAIL;
3491 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003492#ifdef FEAT_SYN_HL
3493 curwin->w_s = &(curbuf->b_s);
3494#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003495 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003496 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003497 curwin_init(); /* init current window */
3498 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003499 else
3500 {
3501 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003502 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003503
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003504 /* We don't want cursor- and scroll-binding in the first window. */
3505 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003508 new_frame(curwin);
3509 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003510 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003511 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 topframe->fr_height = Rows - p_ch;
3514 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003515
3516 return OK;
3517}
3518
3519/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003520 * Create a frame for window "wp".
3521 */
3522 static void
3523new_frame(win_T *wp)
3524{
3525 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3526
3527 wp->w_frame = frp;
3528 if (frp != NULL)
3529 {
3530 frp->fr_layout = FR_LEAF;
3531 frp->fr_win = wp;
3532 }
3533}
3534
3535/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003536 * Initialize the window and frame size to the maximum.
3537 */
3538 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003539win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003540{
3541 firstwin->w_height = ROWS_AVAIL;
3542 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003543 firstwin->w_width = Columns;
3544 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545}
3546
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003547/*
3548 * Allocate a new tabpage_T and init the values.
3549 * Returns NULL when out of memory.
3550 */
3551 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003552alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003553{
3554 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003555# ifdef FEAT_GUI
3556 int i;
3557# endif
3558
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003559
3560 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003561 if (tp == NULL)
3562 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003563
Bram Moolenaar429fa852013-04-15 12:27:36 +02003564# ifdef FEAT_EVAL
3565 /* init t: variables */
3566 tp->tp_vars = dict_alloc();
3567 if (tp->tp_vars == NULL)
3568 {
3569 vim_free(tp);
3570 return NULL;
3571 }
3572 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3573# endif
3574
3575# ifdef FEAT_GUI
3576 for (i = 0; i < 3; i++)
3577 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003578# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003579# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003580 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003581# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003582 tp->tp_ch_used = p_ch;
3583
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003584 return tp;
3585}
3586
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003587 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003588free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003589{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003590 int idx;
3591
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003592# ifdef FEAT_DIFF
3593 diff_clear(tp);
3594# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003595 for (idx = 0; idx < SNAP_COUNT; ++idx)
3596 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003597#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003598 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3599 hash_init(&tp->tp_vars->dv_hashtab);
3600 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003601#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003602
3603#ifdef FEAT_PYTHON
3604 python_tabpage_free(tp);
3605#endif
3606
3607#ifdef FEAT_PYTHON3
3608 python3_tabpage_free(tp);
3609#endif
3610
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003611 vim_free(tp);
3612}
3613
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003614/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003615 * Create a new Tab page with one window.
3616 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003617 * When "after" is 0 put it just after the current Tab page.
3618 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003619 * Return FAIL or OK.
3620 */
3621 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003622win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003623{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003624 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003625 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003626 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003627
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003628 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629 if (newtp == NULL)
3630 return FAIL;
3631
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003632 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003633 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003634 {
3635 vim_free(newtp);
3636 return FAIL;
3637 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003638 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639
3640 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003641 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003642 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003644 if (after == 1)
3645 {
3646 /* New tab page becomes the first one. */
3647 newtp->tp_next = first_tabpage;
3648 first_tabpage = newtp;
3649 }
3650 else
3651 {
3652 if (after > 0)
3653 {
3654 /* Put new tab page before tab page "after". */
3655 n = 2;
3656 for (tp = first_tabpage; tp->tp_next != NULL
3657 && n < after; tp = tp->tp_next)
3658 ++n;
3659 }
3660 newtp->tp_next = tp->tp_next;
3661 tp->tp_next = newtp;
3662 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003663 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003664 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003665 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003666
3667 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003668 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003669
3670#if defined(FEAT_GUI)
3671 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3672 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003673 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003674#endif
3675
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003676 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003677#ifdef FEAT_AUTOCMD
Bram Moolenaarc917da42016-07-19 22:31:36 +02003678 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003679 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003680 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003681 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003682#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003683 return OK;
3684 }
3685
3686 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003687 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003688 return FAIL;
3689}
3690
3691/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003692 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3693 * like with ":split".
3694 * Returns OK if a new tab page was created, FAIL otherwise.
3695 */
3696 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003697may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003698{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003699 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003700
Bram Moolenaard326ce82007-03-11 14:48:29 +00003701 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003702 {
3703 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003704 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003705 return win_new_tabpage(n);
3706 }
3707 return FAIL;
3708}
3709
3710/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003711 * Create up to "maxcount" tabpages with empty windows.
3712 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003713 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003714 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003715make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003716{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003717 int count = maxcount;
3718 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003719
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003720 /* Limit to 'tabpagemax' tabs. */
3721 if (count > p_tpm)
3722 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003723
3724#ifdef FEAT_AUTOCMD
3725 /*
3726 * Don't execute autocommands while creating the tab pages. Must do that
3727 * when putting the buffers in the windows.
3728 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003729 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003730#endif
3731
3732 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003733 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003734 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003735
3736#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003737 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003738#endif
3739
3740 /* return actual number of tab pages */
3741 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003742}
3743
3744/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003745 * Return TRUE when "tpc" points to a valid tab page.
3746 */
3747 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003748valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003749{
3750 tabpage_T *tp;
3751
Bram Moolenaar29323592016-07-24 22:04:11 +02003752 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003753 if (tp == tpc)
3754 return TRUE;
3755 return FALSE;
3756}
3757
3758/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003759 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3760 * valid.
3761 */
3762 int
3763valid_tabpage_win(tabpage_T *tpc)
3764{
3765 tabpage_T *tp;
3766 win_T *wp;
3767
3768 FOR_ALL_TABPAGES(tp)
3769 {
3770 if (tp == tpc)
3771 {
3772 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3773 {
3774 if (win_valid_any_tab(wp))
3775 return TRUE;
3776 }
3777 return FALSE;
3778 }
3779 }
3780 /* shouldn't happen */
3781 return FALSE;
3782}
3783
3784/*
3785 * Close tabpage "tab", assuming it has no windows in it.
3786 * There must be another tabpage or this will crash.
3787 */
3788 void
3789close_tabpage(tabpage_T *tab)
3790{
3791 tabpage_T *ptp;
3792
3793 if (tab == first_tabpage)
3794 {
3795 first_tabpage = tab->tp_next;
3796 ptp = first_tabpage;
3797 }
3798 else
3799 {
3800 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3801 ptp = ptp->tp_next)
3802 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003803 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003804 ptp->tp_next = tab->tp_next;
3805 }
3806
3807 goto_tabpage_tp(ptp, FALSE, FALSE);
3808 free_tabpage(tab);
3809}
3810
3811/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003812 * Find tab page "n" (first one is 1). Returns NULL when not found.
3813 */
3814 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003815find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003816{
3817 tabpage_T *tp;
3818 int i = 1;
3819
3820 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3821 ++i;
3822 return tp;
3823}
3824
3825/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003826 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003827 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003828 */
3829 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003830tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003831{
3832 int i = 1;
3833 tabpage_T *tp;
3834
3835 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3836 ++i;
3837 return i;
3838}
3839
3840/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003841 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003842 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003843 * FAIL.
3844 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003845 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003846 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003847leave_tabpage(
3848 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003849 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003850 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003851{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003852 tabpage_T *tp = curtab;
3853
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003854 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003855#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003856 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003857 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003858 if (new_curbuf != curbuf)
3859 {
3860 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3861 if (curtab != tp)
3862 return FAIL;
3863 }
3864 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3865 if (curtab != tp)
3866 return FAIL;
3867 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003868 if (curtab != tp)
3869 return FAIL;
3870 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003871#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003872#if defined(FEAT_GUI)
3873 /* Remove the scrollbars. They may be added back later. */
3874 if (gui.in_use)
3875 gui_remove_scrollbars();
3876#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003877 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003878 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003879 tp->tp_firstwin = firstwin;
3880 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003881 tp->tp_old_Rows = Rows;
3882 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003883 firstwin = NULL;
3884 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003885 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003886}
3887
3888/*
3889 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003890 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003891 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3892 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003893 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003894 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003895enter_tabpage(
3896 tabpage_T *tp,
3897 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003898 int trigger_enter_autocmds,
3899 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003900{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003901 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003902 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003903
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003904 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003905 firstwin = tp->tp_firstwin;
3906 lastwin = tp->tp_lastwin;
3907 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003908
3909 /* We would like doing the TabEnter event first, but we don't have a
3910 * valid current window yet, which may break some commands.
3911 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003912 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003913 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003914 prevwin = next_prevwin;
3915
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003916 last_status(FALSE); /* status line may appear or disappear */
3917 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003918 must_redraw = CLEAR; /* need to redraw everything */
3919#ifdef FEAT_DIFF
3920 diff_need_scrollbind = TRUE;
3921#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003922
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003923 /* The tabpage line may have appeared or disappeared, may need to resize
3924 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003925 * frame sizes too. Use the stored value of p_ch, so that it can be
3926 * different for each tab page. */
3927 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003928 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3929#ifdef FEAT_GUI_TABLINE
3930 && !gui_use_tabline()
3931#endif
3932 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003933 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003934 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003935 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003936
3937#if defined(FEAT_GUI)
3938 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3939 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003940 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003941#endif
3942
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003943#ifdef FEAT_AUTOCMD
3944 /* Apply autocommands after updating the display, when 'rows' and
3945 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003946 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003947 {
3948 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3949 if (old_curbuf != curbuf)
3950 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3951 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003952#endif
3953
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003954 redraw_all_later(CLEAR);
3955}
3956
3957/*
3958 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003959 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003960 */
3961 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003962goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003963{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003964 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003965 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003966 int i;
3967
Bram Moolenaard68071d2006-05-02 22:08:30 +00003968 if (text_locked())
3969 {
3970 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02003971 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00003972 return;
3973 }
3974
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003975 /* If there is only one it can't work. */
3976 if (first_tabpage->tp_next == NULL)
3977 {
3978 if (n > 1)
3979 beep_flush();
3980 return;
3981 }
3982
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003983 if (n == 0)
3984 {
3985 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003986 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003987 tp = first_tabpage;
3988 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003989 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003990 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003991 else if (n < 0)
3992 {
3993 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3994 * this N times. */
3995 ttp = curtab;
3996 for (i = n; i < 0; ++i)
3997 {
3998 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3999 tp = tp->tp_next)
4000 ;
4001 ttp = tp;
4002 }
4003 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004004 else if (n == 9999)
4005 {
4006 /* Go to last tab page. */
4007 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4008 ;
4009 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004010 else
4011 {
4012 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004013 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004014 if (tp == NULL)
4015 {
4016 beep_flush();
4017 return;
4018 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004019 }
4020
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004021 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004022
4023#ifdef FEAT_GUI_TABLINE
4024 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004025 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004026#endif
4027}
4028
4029/*
4030 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004031 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4032 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004033 * Note: doesn't update the GUI tab.
4034 */
4035 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004036goto_tabpage_tp(
4037 tabpage_T *tp,
4038 int trigger_enter_autocmds,
4039 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004040{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004041 /* Don't repeat a message in another tab page. */
4042 set_keep_msg(NULL, 0);
4043
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004044 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4045 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004046 {
4047 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004048 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4049 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004050 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004051 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4052 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004053 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055
4056/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004057 * Enter window "wp" in tab page "tp".
4058 * Also updates the GUI tab.
4059 */
4060 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004061goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004062{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004063 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004064 if (curtab == tp && win_valid(wp))
4065 {
4066 win_enter(wp, TRUE);
4067# ifdef FEAT_GUI_TABLINE
4068 if (gui_use_tabline())
4069 gui_mch_set_curtab(tabpage_index(curtab));
4070# endif
4071 }
4072}
4073
4074/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004075 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004076 */
4077 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004078tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004079{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004080 int n = 1;
4081 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004082
4083 if (first_tabpage->tp_next == NULL)
4084 return;
4085
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004086 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4087 ++n;
4088
4089 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4090 && tp->tp_next == curtab))
4091 return;
4092
4093 tp_dst = tp;
4094
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004095 /* Remove the current tab page from the list of tab pages. */
4096 if (curtab == first_tabpage)
4097 first_tabpage = curtab->tp_next;
4098 else
4099 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004100 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004101 if (tp->tp_next == curtab)
4102 break;
4103 if (tp == NULL) /* "cannot happen" */
4104 return;
4105 tp->tp_next = curtab->tp_next;
4106 }
4107
4108 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004109 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004110 {
4111 curtab->tp_next = first_tabpage;
4112 first_tabpage = curtab;
4113 }
4114 else
4115 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004116 curtab->tp_next = tp_dst->tp_next;
4117 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004118 }
4119
4120 /* Need to redraw the tabline. Tab page contents doesn't change. */
4121 redraw_tabline = TRUE;
4122}
4123
4124
4125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 * Go to another window.
4127 * When jumping to another buffer, stop Visual mode. Do this before
4128 * changing windows so we can yank the selection into the '*' register.
4129 * When jumping to another window on the same buffer, adjust its cursor
4130 * position to keep the same Visual area.
4131 */
4132 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004133win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004135#ifdef FEAT_CONCEAL
4136 win_T *owp = curwin;
4137#endif
4138
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004139 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
4141 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004142 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 return;
4144 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004145#ifdef FEAT_AUTOCMD
4146 if (curbuf_locked())
4147 return;
4148#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004149
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 if (wp->w_buffer != curbuf)
4151 reset_VIsual_and_resel();
4152 else if (VIsual_active)
4153 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155#ifdef FEAT_GUI
4156 need_mouse_correct = TRUE;
4157#endif
4158 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004159
4160#ifdef FEAT_CONCEAL
4161 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004162 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004163 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004164 if (curwin->w_p_cole > 0 && !msg_scrolled)
4165 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167}
4168
4169#if defined(FEAT_PERL) || defined(PROTO)
4170/*
4171 * Find window number "winnr" (counting top to bottom).
4172 */
4173 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004174win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 win_T *wp;
4177
Bram Moolenaar29323592016-07-24 22:04:11 +02004178 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 if (--winnr == 0)
4180 break;
4181 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182}
4183#endif
4184
Bram Moolenaar4033c552017-09-16 20:54:51 +02004185#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004186/*
4187 * Find the tabpage for window "win".
4188 */
4189 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004190win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004191{
4192 win_T *wp;
4193 tabpage_T *tp;
4194
Bram Moolenaar29323592016-07-24 22:04:11 +02004195 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004196 if (wp == win)
4197 return tp;
4198 return NULL;
4199}
4200#endif
4201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202/*
4203 * Move to window above or below "count" times.
4204 */
4205 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004206win_goto_ver(
4207 int up, /* TRUE to go to win above */
4208 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 frame_T *fr;
4211 frame_T *nfr;
4212 frame_T *foundfr;
4213
4214 foundfr = curwin->w_frame;
4215 while (count--)
4216 {
4217 /*
4218 * First go upwards in the tree of frames until we find a upwards or
4219 * downwards neighbor.
4220 */
4221 fr = foundfr;
4222 for (;;)
4223 {
4224 if (fr == topframe)
4225 goto end;
4226 if (up)
4227 nfr = fr->fr_prev;
4228 else
4229 nfr = fr->fr_next;
4230 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4231 break;
4232 fr = fr->fr_parent;
4233 }
4234
4235 /*
4236 * Now go downwards to find the bottom or top frame in it.
4237 */
4238 for (;;)
4239 {
4240 if (nfr->fr_layout == FR_LEAF)
4241 {
4242 foundfr = nfr;
4243 break;
4244 }
4245 fr = nfr->fr_child;
4246 if (nfr->fr_layout == FR_ROW)
4247 {
4248 /* Find the frame at the cursor row. */
4249 while (fr->fr_next != NULL
4250 && frame2win(fr)->w_wincol + fr->fr_width
4251 <= curwin->w_wincol + curwin->w_wcol)
4252 fr = fr->fr_next;
4253 }
4254 if (nfr->fr_layout == FR_COL && up)
4255 while (fr->fr_next != NULL)
4256 fr = fr->fr_next;
4257 nfr = fr;
4258 }
4259 }
4260end:
4261 if (foundfr != NULL)
4262 win_goto(foundfr->fr_win);
4263}
4264
4265/*
4266 * Move to left or right window.
4267 */
4268 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004269win_goto_hor(
4270 int left, /* TRUE to go to left win */
4271 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
4273 frame_T *fr;
4274 frame_T *nfr;
4275 frame_T *foundfr;
4276
4277 foundfr = curwin->w_frame;
4278 while (count--)
4279 {
4280 /*
4281 * First go upwards in the tree of frames until we find a left or
4282 * right neighbor.
4283 */
4284 fr = foundfr;
4285 for (;;)
4286 {
4287 if (fr == topframe)
4288 goto end;
4289 if (left)
4290 nfr = fr->fr_prev;
4291 else
4292 nfr = fr->fr_next;
4293 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4294 break;
4295 fr = fr->fr_parent;
4296 }
4297
4298 /*
4299 * Now go downwards to find the leftmost or rightmost frame in it.
4300 */
4301 for (;;)
4302 {
4303 if (nfr->fr_layout == FR_LEAF)
4304 {
4305 foundfr = nfr;
4306 break;
4307 }
4308 fr = nfr->fr_child;
4309 if (nfr->fr_layout == FR_COL)
4310 {
4311 /* Find the frame at the cursor row. */
4312 while (fr->fr_next != NULL
4313 && frame2win(fr)->w_winrow + fr->fr_height
4314 <= curwin->w_winrow + curwin->w_wrow)
4315 fr = fr->fr_next;
4316 }
4317 if (nfr->fr_layout == FR_ROW && left)
4318 while (fr->fr_next != NULL)
4319 fr = fr->fr_next;
4320 nfr = fr;
4321 }
4322 }
4323end:
4324 if (foundfr != NULL)
4325 win_goto(foundfr->fr_win);
4326}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328/*
4329 * Make window "wp" the current window.
4330 */
4331 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004332win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004334 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335}
4336
4337/*
4338 * Make window wp the current window.
4339 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4340 * been closed and isn't valid.
4341 */
4342 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004343win_enter_ext(
4344 win_T *wp,
4345 int undo_sync,
4346 int curwin_invalid,
Bram Moolenaarc917da42016-07-19 22:31:36 +02004347 int trigger_new_autocmds UNUSED,
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004348 int trigger_enter_autocmds UNUSED,
4349 int trigger_leave_autocmds UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
4351#ifdef FEAT_AUTOCMD
4352 int other_buffer = FALSE;
4353#endif
4354
4355 if (wp == curwin && !curwin_invalid) /* nothing to do */
4356 return;
4357
4358#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004359 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 {
4361 /*
4362 * Be careful: If autocommands delete the window, return now.
4363 */
4364 if (wp->w_buffer != curbuf)
4365 {
4366 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4367 other_buffer = TRUE;
4368 if (!win_valid(wp))
4369 return;
4370 }
4371 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4372 if (!win_valid(wp))
4373 return;
4374# ifdef FEAT_EVAL
4375 /* autocmds may abort script processing */
4376 if (aborting())
4377 return;
4378# endif
4379 }
4380#endif
4381
4382 /* sync undo before leaving the current buffer */
4383 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004384 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004385
4386 /* Might need to scroll the old window before switching, e.g., when the
4387 * cursor was moved. */
4388 update_topline();
4389
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 /* may have to copy the buffer options when 'cpo' contains 'S' */
4391 if (wp->w_buffer != curbuf)
4392 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4393 if (!curwin_invalid)
4394 {
4395 prevwin = curwin; /* remember for CTRL-W p */
4396 curwin->w_redr_status = TRUE;
4397 }
4398 curwin = wp;
4399 curbuf = wp->w_buffer;
4400 check_cursor();
4401#ifdef FEAT_VIRTUALEDIT
4402 if (!virtual_active())
4403 curwin->w_cursor.coladd = 0;
4404#endif
4405 changed_line_abv_curs(); /* assume cursor position needs updating */
4406
4407 if (curwin->w_localdir != NULL)
4408 {
4409 /* Window has a local directory: Save current directory as global
4410 * directory (unless that was done already) and change to the local
4411 * directory. */
4412 if (globaldir == NULL)
4413 {
4414 char_u cwd[MAXPATHL];
4415
4416 if (mch_dirname(cwd, MAXPATHL) == OK)
4417 globaldir = vim_strsave(cwd);
4418 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004419 if (mch_chdir((char *)curwin->w_localdir) == 0)
4420 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
4422 else if (globaldir != NULL)
4423 {
4424 /* Window doesn't have a local directory and we are not in the global
4425 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004426 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 vim_free(globaldir);
4428 globaldir = NULL;
4429 shorten_fnames(TRUE);
4430 }
4431
4432#ifdef FEAT_AUTOCMD
Bram Moolenaarc917da42016-07-19 22:31:36 +02004433 if (trigger_new_autocmds)
4434 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004435 if (trigger_enter_autocmds)
4436 {
4437 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4438 if (other_buffer)
4439 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441#endif
4442
4443#ifdef FEAT_TITLE
4444 maketitle();
4445#endif
4446 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004447 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 if (restart_edit)
4449 redraw_later(VALID); /* causes status line redraw */
4450
4451 /* set window height to desired minimal value */
4452 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4453 win_setheight((int)p_wh);
4454 else if (curwin->w_height == 0)
4455 win_setheight(1);
4456
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004458 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461#ifdef FEAT_MOUSE
4462 setmouse(); /* in case jumped to/from help buffer */
4463#endif
4464
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004465 /* Change directories when the 'acd' option is set. */
4466 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467}
4468
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004471 * Jump to the first open window that contains buffer "buf", if one exists.
4472 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 */
4474 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004475buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004477 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004479 if (curwin->w_buffer == buf)
4480 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004481 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004482 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004483 if (wp->w_buffer == buf)
4484 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 if (wp != NULL)
4486 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004487 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004489
4490/*
4491 * Jump to the first open window in any tab page that contains buffer "buf",
4492 * if one exists.
4493 * Returns a pointer to the window found, otherwise NULL.
4494 */
4495 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004496buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004497{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004498 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004499 tabpage_T *tp;
4500
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004501 if (wp != NULL)
4502 return wp;
4503
Bram Moolenaar29323592016-07-24 22:04:11 +02004504 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004505 if (tp != curtab)
4506 {
4507 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4508 if (wp->w_buffer == buf)
4509 break;
4510 if (wp != NULL)
4511 {
4512 goto_tabpage_win(tp, wp);
4513 if (curwin != wp)
4514 wp = NULL; /* something went wrong */
4515 break;
4516 }
4517 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004518 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004519}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004521static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004524 * Allocate a window structure and link it in the window list when "hidden" is
4525 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004528win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004530 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532 /*
4533 * allocate window structure and linesizes arrays
4534 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004535 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004536 if (new_wp == NULL)
4537 return NULL;
4538
4539 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004541 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004542 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544
Bram Moolenaar86edef62016-03-13 18:07:30 +01004545 new_wp->w_id = ++last_win_id;
4546
Bram Moolenaar429fa852013-04-15 12:27:36 +02004547#ifdef FEAT_EVAL
4548 /* init w: variables */
4549 new_wp->w_vars = dict_alloc();
4550 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004552 win_free_lsize(new_wp);
4553 vim_free(new_wp);
4554 return NULL;
4555 }
4556 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004557#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004558
4559#ifdef FEAT_AUTOCMD
4560 /* Don't execute autocommands while the window is not properly
4561 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4562 * event. */
4563 block_autocmds();
4564#endif
4565 /*
4566 * link the window in the window list
4567 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004568 if (!hidden)
4569 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004570 new_wp->w_wincol = 0;
4571 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
Bram Moolenaar429fa852013-04-15 12:27:36 +02004573 /* position the display and the cursor at the top of the file. */
4574 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004576 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004578 new_wp->w_botline = 2;
4579 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004581 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582#endif
4583
Bram Moolenaar429fa852013-04-15 12:27:36 +02004584 /* We won't calculate w_fraction until resizing the window */
4585 new_wp->w_fraction = 0;
4586 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004589 if (gui.in_use)
4590 {
4591 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4592 SBAR_LEFT, new_wp);
4593 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4594 SBAR_RIGHT, new_wp);
4595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#endif
4597#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004598 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004600#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004601 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004602#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004603#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004604 new_wp->w_match_head = NULL;
4605 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004606#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004607 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608}
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004611 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 */
4613 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004614win_free(
4615 win_T *wp,
4616 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004619 buf_T *buf;
4620 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004622#ifdef FEAT_FOLDING
4623 clearFolding(wp);
4624#endif
4625
4626 /* reduce the reference count to the argument list. */
4627 alist_unlink(wp->w_alist);
4628
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004629#ifdef FEAT_AUTOCMD
4630 /* Don't execute autocommands while the window is halfway being deleted.
4631 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004632 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004633#endif
4634
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004635#ifdef FEAT_LUA
4636 lua_window_free(wp);
4637#endif
4638
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004639#ifdef FEAT_MZSCHEME
4640 mzscheme_window_free(wp);
4641#endif
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#ifdef FEAT_PERL
4644 perl_win_free(wp);
4645#endif
4646
4647#ifdef FEAT_PYTHON
4648 python_window_free(wp);
4649#endif
4650
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004651#ifdef FEAT_PYTHON3
4652 python3_window_free(wp);
4653#endif
4654
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655#ifdef FEAT_TCL
4656 tcl_window_free(wp);
4657#endif
4658
4659#ifdef FEAT_RUBY
4660 ruby_window_free(wp);
4661#endif
4662
4663 clear_winopt(&wp->w_onebuf_opt);
4664 clear_winopt(&wp->w_allbuf_opt);
4665
4666#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004667 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4668 hash_init(&wp->w_vars->dv_hashtab);
4669 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670#endif
4671
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004672 {
4673 tabpage_T *ttp;
4674
4675 if (prevwin == wp)
4676 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004677 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004678 if (ttp->tp_prevwin == wp)
4679 ttp->tp_prevwin = NULL;
4680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 win_free_lsize(wp);
4682
4683 for (i = 0; i < wp->w_tagstacklen; ++i)
4684 vim_free(wp->w_tagstack[i].tagname);
4685
4686 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004687
Bram Moolenaarff18df02013-07-24 17:51:57 +02004688 /* Remove the window from the b_wininfo lists, it may happen that the
4689 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004690 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004691 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4692 if (wip->wi_win == wp)
4693 wip->wi_win = NULL;
4694
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004696 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#ifdef FEAT_JUMPLIST
4700 free_jumplist(wp);
4701#endif
4702
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004703#ifdef FEAT_QUICKFIX
4704 qf_free_all(wp);
4705#endif
4706
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707#ifdef FEAT_GUI
4708 if (gui.in_use)
4709 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4711 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4712 }
4713#endif /* FEAT_GUI */
4714
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004715#ifdef FEAT_MENU
4716 remove_winbar(wp);
4717#endif
4718
Bram Moolenaar860cae12010-06-05 23:22:07 +02004719#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004720 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004721#endif
4722
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004723#ifdef FEAT_AUTOCMD
4724 if (wp != aucmd_win)
4725#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004726 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004727#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004728 if (autocmd_busy)
4729 {
4730 wp->w_next = au_pending_free_win;
4731 au_pending_free_win = wp;
4732 }
4733 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004734#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004735 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004736
4737#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004738 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740}
4741
4742/*
4743 * Append window "wp" in the window list after window "after".
4744 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004745 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004746win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 win_T *before;
4749
4750 if (after == NULL) /* after NULL is in front of the first */
4751 before = firstwin;
4752 else
4753 before = after->w_next;
4754
4755 wp->w_next = before;
4756 wp->w_prev = after;
4757 if (after == NULL)
4758 firstwin = wp;
4759 else
4760 after->w_next = wp;
4761 if (before == NULL)
4762 lastwin = wp;
4763 else
4764 before->w_prev = wp;
4765}
4766
4767/*
4768 * Remove a window from the window list.
4769 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004770 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004771win_remove(
4772 win_T *wp,
4773 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 if (wp->w_prev != NULL)
4776 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004777 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004778 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004779 else
4780 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004781
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (wp->w_next != NULL)
4783 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004784 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004785 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004786 else
4787 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788}
4789
4790/*
4791 * Append frame "frp" in a frame list after frame "after".
4792 */
4793 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004794frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
4796 frp->fr_next = after->fr_next;
4797 after->fr_next = frp;
4798 if (frp->fr_next != NULL)
4799 frp->fr_next->fr_prev = frp;
4800 frp->fr_prev = after;
4801}
4802
4803/*
4804 * Insert frame "frp" in a frame list before frame "before".
4805 */
4806 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004807frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
4809 frp->fr_next = before;
4810 frp->fr_prev = before->fr_prev;
4811 before->fr_prev = frp;
4812 if (frp->fr_prev != NULL)
4813 frp->fr_prev->fr_next = frp;
4814 else
4815 frp->fr_parent->fr_child = frp;
4816}
4817
4818/*
4819 * Remove a frame from a frame list.
4820 */
4821 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004822frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823{
4824 if (frp->fr_prev != NULL)
4825 frp->fr_prev->fr_next = frp->fr_next;
4826 else
4827 frp->fr_parent->fr_child = frp->fr_next;
4828 if (frp->fr_next != NULL)
4829 frp->fr_next->fr_prev = frp->fr_prev;
4830}
4831
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832/*
4833 * Allocate w_lines[] for window "wp".
4834 * Return FAIL for failure, OK for success.
4835 */
4836 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004837win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838{
4839 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004840 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if (wp->w_lines == NULL)
4842 return FAIL;
4843 return OK;
4844}
4845
4846/*
4847 * free lsize arrays for a window
4848 */
4849 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004850win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004852 /* TODO: why would wp be NULL here? */
4853 if (wp != NULL)
4854 {
4855 vim_free(wp->w_lines);
4856 wp->w_lines = NULL;
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858}
4859
4860/*
4861 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004862 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 */
4864 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004865shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004867 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 if (firstwin == NULL) /* not initialized yet */
4870 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (h < frame_minheight(topframe, NULL))
4872 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004873
4874 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 * that doesn't result in the right height, forget about that option. */
4876 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004877 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 frame_new_height(topframe, h, FALSE, FALSE);
4879
4880 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004882 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#if 0
4885 /* Disabled: don't want making the screen smaller make a window larger. */
4886 if (p_ea)
4887 win_equal(curwin, FALSE, 'v');
4888#endif
4889}
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891/*
4892 * Called from win_new_shellsize() after Columns changed.
4893 */
4894 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004895shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 if (firstwin == NULL) /* not initialized yet */
4898 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004899
4900 /* First try setting the widths of windows with 'winfixwidth'. If that
4901 * doesn't result in the right width, forget about that option. */
4902 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004903 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004904 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4907#if 0
4908 /* Disabled: don't want making the screen smaller make a window larger. */
4909 if (p_ea)
4910 win_equal(curwin, FALSE, 'h');
4911#endif
4912}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914#if defined(FEAT_CMDWIN) || defined(PROTO)
4915/*
4916 * Save the size of all windows in "gap".
4917 */
4918 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004919win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921{
4922 win_T *wp;
4923
4924 ga_init2(gap, (int)sizeof(int), 1);
4925 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004926 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
4928 ((int *)gap->ga_data)[gap->ga_len++] =
4929 wp->w_width + wp->w_vsep_width;
4930 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4931 }
4932}
4933
4934/*
4935 * Restore window sizes, but only if the number of windows is still the same.
4936 * Does not free the growarray.
4937 */
4938 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004939win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
4941 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004942 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944 if (win_count() * 2 == gap->ga_len)
4945 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004946 /* The order matters, because frames contain other frames, but it's
4947 * difficult to get right. The easy way out is to do it twice. */
4948 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004950 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004951 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02004952 {
4953 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4954 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 /* recompute the window positions */
4958 (void)win_comp_pos();
4959 }
4960}
4961#endif /* FEAT_CMDWIN */
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * Update the position for all windows, using the width and height of the
4965 * frames.
4966 * Returns the row just after the last window.
4967 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004968 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004969win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004971 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 int col = 0;
4973
4974 frame_comp_pos(topframe, &row, &col);
4975 return row;
4976}
4977
4978/*
4979 * Update the position of the windows in frame "topfrp", using the width and
4980 * height of the frames.
4981 * "*row" and "*col" are the top-left position of the frame. They are updated
4982 * to the bottom-right position plus one.
4983 */
4984 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004985frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987 win_T *wp;
4988 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 int startcol;
4990 int startrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
4992 wp = topfrp->fr_win;
4993 if (wp != NULL)
4994 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004995 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
4997 /* position changed, redraw */
4998 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 redraw_win_later(wp, NOT_VALID);
5001 wp->w_redr_status = TRUE;
5002 }
5003 *row += wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 else
5007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 startrow = *row;
5009 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (topfrp->fr_layout == FR_ROW)
5013 *row = startrow; /* all frames are at the same row */
5014 else
5015 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 frame_comp_pos(frp, row, col);
5017 }
5018 }
5019}
5020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021/*
5022 * Set current window height and take care of repositioning other windows to
5023 * fit around it.
5024 */
5025 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005026win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028 win_setheight_win(height, curwin);
5029}
5030
5031/*
5032 * Set the window height of window "win" and take care of repositioning other
5033 * windows to fit around it.
5034 */
5035 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005036win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
5038 int row;
5039
5040 if (win == curwin)
5041 {
5042 /* Always keep current window at least one line high, even when
5043 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (height < p_wmh)
5045 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if (height == 0)
5047 height = 1;
5048 }
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 frame_setheight(win->w_frame, height + win->w_status_height);
5051
5052 /* recompute the window positions */
5053 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 /*
5056 * If there is extra space created between the last window and the command
5057 * line, clear it.
5058 */
5059 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5060 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5061 cmdline_row = row;
5062 msg_row = row;
5063 msg_col = 0;
5064
5065 redraw_all_later(NOT_VALID);
5066}
5067
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068/*
5069 * Set the height of a frame to "height" and take care that all frames and
5070 * windows inside it are resized. Also resize frames on the left and right if
5071 * the are in the same FR_ROW frame.
5072 *
5073 * Strategy:
5074 * If the frame is part of a FR_COL frame, try fitting the frame in that
5075 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5076 * go to containing frames to resize them and make room.
5077 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5078 * Check for the minimal height of the FR_ROW frame.
5079 * At the top level we can also use change the command line height.
5080 */
5081 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005082frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
5084 int room; /* total number of lines available */
5085 int take; /* number of lines taken from other windows */
5086 int room_cmdline; /* lines available from cmdline */
5087 int run;
5088 frame_T *frp;
5089 int h;
5090 int room_reserved;
5091
5092 /* If the height already is the desired value, nothing to do. */
5093 if (curfrp->fr_height == height)
5094 return;
5095
5096 if (curfrp->fr_parent == NULL)
5097 {
5098 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005099 if (height > ROWS_AVAIL)
5100 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (height > 0)
5102 frame_new_height(curfrp, height, FALSE, FALSE);
5103 }
5104 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5105 {
5106 /* Row of frames: Also need to resize frames left and right of this
5107 * one. First check for the minimal height of these. */
5108 h = frame_minheight(curfrp->fr_parent, NULL);
5109 if (height < h)
5110 height = h;
5111 frame_setheight(curfrp->fr_parent, height);
5112 }
5113 else
5114 {
5115 /*
5116 * Column of frames: try to change only frames in this column.
5117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 /*
5119 * Do this twice:
5120 * 1: compute room available, if it's not enough try resizing the
5121 * containing frame.
5122 * 2: compute the room available and adjust the height to it.
5123 * Try not to reduce the height of a window with 'winfixheight' set.
5124 */
5125 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 room = 0;
5128 room_reserved = 0;
5129 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5130 frp = frp->fr_next)
5131 {
5132 if (frp != curfrp
5133 && frp->fr_win != NULL
5134 && frp->fr_win->w_p_wfh)
5135 room_reserved += frp->fr_height;
5136 room += frp->fr_height;
5137 if (frp != curfrp)
5138 room -= frame_minheight(frp, NULL);
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 if (curfrp->fr_width != Columns)
5141 room_cmdline = 0;
5142 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
5144 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5145 + lastwin->w_height + lastwin->w_status_height);
5146 if (room_cmdline < 0)
5147 room_cmdline = 0;
5148 }
5149
5150 if (height <= room + room_cmdline)
5151 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
5154 if (height > room + room_cmdline)
5155 height = room + room_cmdline;
5156 break;
5157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 frame_setheight(curfrp->fr_parent, height
5159 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 }
5161
5162 /*
5163 * Compute the number of lines we will take from others frames (can be
5164 * negative!).
5165 */
5166 take = height - curfrp->fr_height;
5167
5168 /* If there is not enough room, also reduce the height of a window
5169 * with 'winfixheight' set. */
5170 if (height > room + room_cmdline - room_reserved)
5171 room_reserved = room + room_cmdline - height;
5172 /* If there is only a 'winfixheight' window and making the
5173 * window smaller, need to make the other window taller. */
5174 if (take < 0 && room - curfrp->fr_height < room_reserved)
5175 room_reserved = 0;
5176
5177 if (take > 0 && room_cmdline > 0)
5178 {
5179 /* use lines from cmdline first */
5180 if (take < room_cmdline)
5181 room_cmdline = take;
5182 take -= room_cmdline;
5183 topframe->fr_height += room_cmdline;
5184 }
5185
5186 /*
5187 * set the current frame to the new height
5188 */
5189 frame_new_height(curfrp, height, FALSE, FALSE);
5190
5191 /*
5192 * First take lines from the frames after the current frame. If
5193 * that is not enough, takes lines from frames above the current
5194 * frame.
5195 */
5196 for (run = 0; run < 2; ++run)
5197 {
5198 if (run == 0)
5199 frp = curfrp->fr_next; /* 1st run: start with next window */
5200 else
5201 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5202 while (frp != NULL && take != 0)
5203 {
5204 h = frame_minheight(frp, NULL);
5205 if (room_reserved > 0
5206 && frp->fr_win != NULL
5207 && frp->fr_win->w_p_wfh)
5208 {
5209 if (room_reserved >= frp->fr_height)
5210 room_reserved -= frp->fr_height;
5211 else
5212 {
5213 if (frp->fr_height - room_reserved > take)
5214 room_reserved = frp->fr_height - take;
5215 take -= frp->fr_height - room_reserved;
5216 frame_new_height(frp, room_reserved, FALSE, FALSE);
5217 room_reserved = 0;
5218 }
5219 }
5220 else
5221 {
5222 if (frp->fr_height - take < h)
5223 {
5224 take -= frp->fr_height - h;
5225 frame_new_height(frp, h, FALSE, FALSE);
5226 }
5227 else
5228 {
5229 frame_new_height(frp, frp->fr_height - take,
5230 FALSE, FALSE);
5231 take = 0;
5232 }
5233 }
5234 if (run == 0)
5235 frp = frp->fr_next;
5236 else
5237 frp = frp->fr_prev;
5238 }
5239 }
5240 }
5241}
5242
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243/*
5244 * Set current window width and take care of repositioning other windows to
5245 * fit around it.
5246 */
5247 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005248win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249{
5250 win_setwidth_win(width, curwin);
5251}
5252
5253 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005254win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
5256 /* Always keep current window at least one column wide, even when
5257 * 'winminwidth' is zero. */
5258 if (wp == curwin)
5259 {
5260 if (width < p_wmw)
5261 width = p_wmw;
5262 if (width == 0)
5263 width = 1;
5264 }
5265
5266 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5267
5268 /* recompute the window positions */
5269 (void)win_comp_pos();
5270
5271 redraw_all_later(NOT_VALID);
5272}
5273
5274/*
5275 * Set the width of a frame to "width" and take care that all frames and
5276 * windows inside it are resized. Also resize frames above and below if the
5277 * are in the same FR_ROW frame.
5278 *
5279 * Strategy is similar to frame_setheight().
5280 */
5281 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005282frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283{
5284 int room; /* total number of lines available */
5285 int take; /* number of lines taken from other windows */
5286 int run;
5287 frame_T *frp;
5288 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005289 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 /* If the width already is the desired value, nothing to do. */
5292 if (curfrp->fr_width == width)
5293 return;
5294
5295 if (curfrp->fr_parent == NULL)
5296 /* topframe: can't change width */
5297 return;
5298
5299 if (curfrp->fr_parent->fr_layout == FR_COL)
5300 {
5301 /* Column of frames: Also need to resize frames above and below of
5302 * this one. First check for the minimal width of these. */
5303 w = frame_minwidth(curfrp->fr_parent, NULL);
5304 if (width < w)
5305 width = w;
5306 frame_setwidth(curfrp->fr_parent, width);
5307 }
5308 else
5309 {
5310 /*
5311 * Row of frames: try to change only frames in this row.
5312 *
5313 * Do this twice:
5314 * 1: compute room available, if it's not enough try resizing the
5315 * containing frame.
5316 * 2: compute the room available and adjust the width to it.
5317 */
5318 for (run = 1; run <= 2; ++run)
5319 {
5320 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005321 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5323 frp = frp->fr_next)
5324 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005325 if (frp != curfrp
5326 && frp->fr_win != NULL
5327 && frp->fr_win->w_p_wfw)
5328 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 room += frp->fr_width;
5330 if (frp != curfrp)
5331 room -= frame_minwidth(frp, NULL);
5332 }
5333
5334 if (width <= room)
5335 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005336 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
5338 if (width > room)
5339 width = room;
5340 break;
5341 }
5342 frame_setwidth(curfrp->fr_parent, width
5343 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5344 }
5345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 /*
5347 * Compute the number of lines we will take from others frames (can be
5348 * negative!).
5349 */
5350 take = width - curfrp->fr_width;
5351
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005352 /* If there is not enough room, also reduce the width of a window
5353 * with 'winfixwidth' set. */
5354 if (width > room - room_reserved)
5355 room_reserved = room - width;
5356 /* If there is only a 'winfixwidth' window and making the
5357 * window smaller, need to make the other window narrower. */
5358 if (take < 0 && room - curfrp->fr_width < room_reserved)
5359 room_reserved = 0;
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 /*
5362 * set the current frame to the new width
5363 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005364 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366 /*
5367 * First take lines from the frames right of the current frame. If
5368 * that is not enough, takes lines from frames left of the current
5369 * frame.
5370 */
5371 for (run = 0; run < 2; ++run)
5372 {
5373 if (run == 0)
5374 frp = curfrp->fr_next; /* 1st run: start with next window */
5375 else
5376 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5377 while (frp != NULL && take != 0)
5378 {
5379 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005380 if (room_reserved > 0
5381 && frp->fr_win != NULL
5382 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005384 if (room_reserved >= frp->fr_width)
5385 room_reserved -= frp->fr_width;
5386 else
5387 {
5388 if (frp->fr_width - room_reserved > take)
5389 room_reserved = frp->fr_width - take;
5390 take -= frp->fr_width - room_reserved;
5391 frame_new_width(frp, room_reserved, FALSE, FALSE);
5392 room_reserved = 0;
5393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
5395 else
5396 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005397 if (frp->fr_width - take < w)
5398 {
5399 take -= frp->fr_width - w;
5400 frame_new_width(frp, w, FALSE, FALSE);
5401 }
5402 else
5403 {
5404 frame_new_width(frp, frp->fr_width - take,
5405 FALSE, FALSE);
5406 take = 0;
5407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
5409 if (run == 0)
5410 frp = frp->fr_next;
5411 else
5412 frp = frp->fr_prev;
5413 }
5414 }
5415 }
5416}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417
5418/*
5419 * Check 'winminheight' for a valid value.
5420 */
5421 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005422win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 int room;
5425 int first = TRUE;
5426 win_T *wp;
5427
5428 /* loop until there is a 'winminheight' that is possible */
5429 while (p_wmh > 0)
5430 {
5431 /* TODO: handle vertical splits */
5432 room = -p_wh;
Bram Moolenaar29323592016-07-24 22:04:11 +02005433 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 room += wp->w_height - p_wmh;
5435 if (room >= 0)
5436 break;
5437 --p_wmh;
5438 if (first)
5439 {
5440 EMSG(_(e_noroom));
5441 first = FALSE;
5442 }
5443 }
5444}
5445
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005446#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447
5448/*
5449 * Status line of dragwin is dragged "offset" lines down (negative is up).
5450 */
5451 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005452win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
5454 frame_T *curfr;
5455 frame_T *fr;
5456 int room;
5457 int row;
5458 int up; /* if TRUE, drag status line up, otherwise down */
5459 int n;
5460
5461 fr = dragwin->w_frame;
5462 curfr = fr;
5463 if (fr != topframe) /* more than one window */
5464 {
5465 fr = fr->fr_parent;
5466 /* When the parent frame is not a column of frames, its parent should
5467 * be. */
5468 if (fr->fr_layout != FR_COL)
5469 {
5470 curfr = fr;
5471 if (fr != topframe) /* only a row of windows, may drag statusline */
5472 fr = fr->fr_parent;
5473 }
5474 }
5475
5476 /* If this is the last frame in a column, may want to resize the parent
5477 * frame instead (go two up to skip a row of frames). */
5478 while (curfr != topframe && curfr->fr_next == NULL)
5479 {
5480 if (fr != topframe)
5481 fr = fr->fr_parent;
5482 curfr = fr;
5483 if (fr != topframe)
5484 fr = fr->fr_parent;
5485 }
5486
5487 if (offset < 0) /* drag up */
5488 {
5489 up = TRUE;
5490 offset = -offset;
5491 /* sum up the room of the current frame and above it */
5492 if (fr == curfr)
5493 {
5494 /* only one window */
5495 room = fr->fr_height - frame_minheight(fr, NULL);
5496 }
5497 else
5498 {
5499 room = 0;
5500 for (fr = fr->fr_child; ; fr = fr->fr_next)
5501 {
5502 room += fr->fr_height - frame_minheight(fr, NULL);
5503 if (fr == curfr)
5504 break;
5505 }
5506 }
5507 fr = curfr->fr_next; /* put fr at frame that grows */
5508 }
5509 else /* drag down */
5510 {
5511 up = FALSE;
5512 /*
5513 * Only dragging the last status line can reduce p_ch.
5514 */
5515 room = Rows - cmdline_row;
5516 if (curfr->fr_next == NULL)
5517 room -= 1;
5518 else
5519 room -= p_ch;
5520 if (room < 0)
5521 room = 0;
5522 /* sum up the room of frames below of the current one */
5523 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5524 room += fr->fr_height - frame_minheight(fr, NULL);
5525 fr = curfr; /* put fr at window that grows */
5526 }
5527
5528 if (room < offset) /* Not enough room */
5529 offset = room; /* Move as far as we can */
5530 if (offset <= 0)
5531 return;
5532
5533 /*
5534 * Grow frame fr by "offset" lines.
5535 * Doesn't happen when dragging the last status line up.
5536 */
5537 if (fr != NULL)
5538 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5539
5540 if (up)
5541 fr = curfr; /* current frame gets smaller */
5542 else
5543 fr = curfr->fr_next; /* next frame gets smaller */
5544
5545 /*
5546 * Now make the other frames smaller.
5547 */
5548 while (fr != NULL && offset > 0)
5549 {
5550 n = frame_minheight(fr, NULL);
5551 if (fr->fr_height - offset <= n)
5552 {
5553 offset -= fr->fr_height - n;
5554 frame_new_height(fr, n, !up, FALSE);
5555 }
5556 else
5557 {
5558 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5559 break;
5560 }
5561 if (up)
5562 fr = fr->fr_prev;
5563 else
5564 fr = fr->fr_next;
5565 }
5566 row = win_comp_pos();
5567 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5568 cmdline_row = row;
5569 p_ch = Rows - cmdline_row;
5570 if (p_ch < 1)
5571 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005572 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005573 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 showmode();
5575}
5576
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577/*
5578 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5579 */
5580 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005581win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
5583 frame_T *curfr;
5584 frame_T *fr;
5585 int room;
5586 int left; /* if TRUE, drag separator line left, otherwise right */
5587 int n;
5588
5589 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005590 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 return;
5592 curfr = fr;
5593 fr = fr->fr_parent;
5594 /* When the parent frame is not a row of frames, its parent should be. */
5595 if (fr->fr_layout != FR_ROW)
5596 {
5597 if (fr == topframe) /* only a column of windows (cannot happen?) */
5598 return;
5599 curfr = fr;
5600 fr = fr->fr_parent;
5601 }
5602
5603 /* If this is the last frame in a row, may want to resize a parent
5604 * frame instead. */
5605 while (curfr->fr_next == NULL)
5606 {
5607 if (fr == topframe)
5608 break;
5609 curfr = fr;
5610 fr = fr->fr_parent;
5611 if (fr != topframe)
5612 {
5613 curfr = fr;
5614 fr = fr->fr_parent;
5615 }
5616 }
5617
5618 if (offset < 0) /* drag left */
5619 {
5620 left = TRUE;
5621 offset = -offset;
5622 /* sum up the room of the current frame and left of it */
5623 room = 0;
5624 for (fr = fr->fr_child; ; fr = fr->fr_next)
5625 {
5626 room += fr->fr_width - frame_minwidth(fr, NULL);
5627 if (fr == curfr)
5628 break;
5629 }
5630 fr = curfr->fr_next; /* put fr at frame that grows */
5631 }
5632 else /* drag right */
5633 {
5634 left = FALSE;
5635 /* sum up the room of frames right of the current one */
5636 room = 0;
5637 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5638 room += fr->fr_width - frame_minwidth(fr, NULL);
5639 fr = curfr; /* put fr at window that grows */
5640 }
5641
5642 if (room < offset) /* Not enough room */
5643 offset = room; /* Move as far as we can */
5644 if (offset <= 0) /* No room at all, quit. */
5645 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005646 if (fr == NULL)
5647 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
5649 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005650 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
5652 /* shrink other frames: current and at the left or at the right */
5653 if (left)
5654 fr = curfr; /* current frame gets smaller */
5655 else
5656 fr = curfr->fr_next; /* next frame gets smaller */
5657
5658 while (fr != NULL && offset > 0)
5659 {
5660 n = frame_minwidth(fr, NULL);
5661 if (fr->fr_width - offset <= n)
5662 {
5663 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005664 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
5666 else
5667 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005668 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 break;
5670 }
5671 if (left)
5672 fr = fr->fr_prev;
5673 else
5674 fr = fr->fr_next;
5675 }
5676 (void)win_comp_pos();
5677 redraw_all_later(NOT_VALID);
5678}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679#endif /* FEAT_MOUSE */
5680
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005681#define FRACTION_MULT 16384L
5682
5683/*
5684 * Set wp->w_fraction for the current w_wrow and w_height.
5685 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005686 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005687set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005688{
5689 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005690 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005691}
5692
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693/*
5694 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005695 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 * This takes care of the things inside the window, not what happens to the
5697 * window position, the frame or to other windows.
5698 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005699 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005700win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005702 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703
5704 /* Don't want a negative height. Happens when splitting a tiny window.
5705 * Will equalize heights soon to fix it. */
5706 if (height < 0)
5707 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005708 if (wp->w_height == height)
5709 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005711 if (wp->w_height > 0)
5712 {
5713 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005714 /* w_wrow needs to be valid. When setting 'laststatus' this may
5715 * call win_new_height() recursively. */
5716 validate_cursor();
5717 if (wp->w_height != prev_height)
5718 return; /* Recursive call already changed the size, bail out here
5719 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005720 if (wp->w_wrow != wp->w_prev_fraction_row)
5721 set_fraction(wp);
5722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724 wp->w_height = height;
5725 wp->w_skipcol = 0;
5726
Bram Moolenaar955f1982017-02-05 15:10:51 +01005727 /* There is no point in adjusting the scroll position when exiting. Some
5728 * values might be invalid. */
5729 if (!exiting)
5730 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005731}
5732
5733 void
5734scroll_to_fraction(win_T *wp, int prev_height)
5735{
5736 linenr_T lnum;
5737 int sline, line_size;
5738 int height = wp->w_height;
5739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 /* Don't change w_topline when height is zero. Don't set w_topline when
5741 * 'scrollbind' is set and this isn't the current window. */
5742 if (height > 0
5743#ifdef FEAT_SCROLLBIND
5744 && (!wp->w_p_scb || wp == curwin)
5745#endif
5746 )
5747 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005748 /*
5749 * Find a value for w_topline that shows the cursor at the same
5750 * relative position in the window as before (more or less).
5751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 lnum = wp->w_cursor.lnum;
5753 if (lnum < 1) /* can happen when starting up */
5754 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005755 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5756 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5758 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005759
5760 if (sline >= 0)
5761 {
5762 /* Make sure the whole cursor line is visible, if possible. */
5763 int rows = plines_win(wp, lnum, FALSE);
5764
5765 if (sline > wp->w_height - rows)
5766 {
5767 sline = wp->w_height - rows;
5768 wp->w_wrow -= rows - line_size;
5769 }
5770 }
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 if (sline < 0)
5773 {
5774 /*
5775 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005776 * Make cursor line the first line in the window. If not enough
5777 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 */
5779 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005780 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005781 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005782 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005783 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005784 --wp->w_wrow;
5785 while (wp->w_wrow >= wp->w_height)
5786 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005787 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005788 + win_col_off2(wp);
5789 --wp->w_wrow;
5790 }
5791 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005792 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005794 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005796 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 {
5798#ifdef FEAT_FOLDING
5799 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5800 if (lnum == 1)
5801 {
5802 /* first line in buffer is folded */
5803 line_size = 1;
5804 --sline;
5805 break;
5806 }
5807#endif
5808 --lnum;
5809#ifdef FEAT_DIFF
5810 if (lnum == wp->w_topline)
5811 line_size = plines_win_nofill(wp, lnum, TRUE)
5812 + wp->w_topfill;
5813 else
5814#endif
5815 line_size = plines_win(wp, lnum, TRUE);
5816 sline -= line_size;
5817 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 if (sline < 0)
5820 {
5821 /*
5822 * Line we want at top would go off top of screen. Use next
5823 * line instead.
5824 */
5825#ifdef FEAT_FOLDING
5826 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5827#endif
5828 lnum++;
5829 wp->w_wrow -= line_size + sline;
5830 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005831 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
5833 /* First line of file reached, use that as topline. */
5834 lnum = 1;
5835 wp->w_wrow -= sline;
5836 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005837
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005838 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 }
5841
5842 if (wp == curwin)
5843 {
5844 if (p_so)
5845 update_topline();
5846 curs_columns(FALSE); /* validate w_wrow */
5847 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005848 if (prev_height > 0)
5849 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
5851 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005852 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 invalidate_botline_win(wp);
5855}
5856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857/*
5858 * Set the width of a window.
5859 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005860 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005861win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 wp->w_width = width;
5864 wp->w_lines_valid = 0;
5865 changed_line_abv_curs_win(wp);
5866 invalidate_botline_win(wp);
5867 if (wp == curwin)
5868 {
5869 update_topline();
5870 curs_columns(TRUE); /* validate w_wrow */
5871 }
5872 redraw_win_later(wp, NOT_VALID);
5873 wp->w_redr_status = TRUE;
5874}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
5876 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005877win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5880 if (wp->w_p_scr == 0)
5881 wp->w_p_scr = 1;
5882}
5883
5884/*
5885 * command_height: called whenever p_ch has been changed
5886 */
5887 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005888command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 int h;
5891 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005892 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005894 /* Use the value of p_ch that we remembered. This is needed for when the
5895 * GUI starts up, we can't be sure in what order things happen. And when
5896 * p_ch was changed in another tab page. */
5897 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 /* Find bottom frame with width of screen. */
5900 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5902 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904 /* Avoid changing the height of a window with 'winfixheight' set. */
5905 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5906 && frp->fr_win->w_p_wfh)
5907 frp = frp->fr_prev;
5908
5909 if (starting != NO_SCREEN)
5910 {
5911 cmdline_row = Rows - p_ch;
5912
5913 if (p_ch > old_p_ch) /* p_ch got bigger */
5914 {
5915 while (p_ch > old_p_ch)
5916 {
5917 if (frp == NULL)
5918 {
5919 EMSG(_(e_noroom));
5920 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005921 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 cmdline_row = Rows - p_ch;
5923 break;
5924 }
5925 h = frp->fr_height - frame_minheight(frp, NULL);
5926 if (h > p_ch - old_p_ch)
5927 h = p_ch - old_p_ch;
5928 old_p_ch += h;
5929 frame_add_height(frp, -h);
5930 frp = frp->fr_prev;
5931 }
5932
5933 /* Recompute window positions. */
5934 (void)win_comp_pos();
5935
5936 /* clear the lines added to cmdline */
5937 if (full_screen)
5938 screen_fill((int)(cmdline_row), (int)Rows, 0,
5939 (int)Columns, ' ', ' ', 0);
5940 msg_row = cmdline_row;
5941 redraw_cmdline = TRUE;
5942 return;
5943 }
5944
5945 if (msg_row < cmdline_row)
5946 msg_row = cmdline_row;
5947 redraw_cmdline = TRUE;
5948 }
5949 frame_add_height(frp, (int)(old_p_ch - p_ch));
5950
5951 /* Recompute window positions. */
5952 if (frp != lastwin->w_frame)
5953 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954}
5955
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956/*
5957 * Resize frame "frp" to be "n" lines higher (negative for less high).
5958 * Also resize the frames it is contained in.
5959 */
5960 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005961frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5964 for (;;)
5965 {
5966 frp = frp->fr_parent;
5967 if (frp == NULL)
5968 break;
5969 frp->fr_height += n;
5970 }
5971}
5972
5973/*
5974 * Add or remove a status line for the bottom window(s), according to the
5975 * value of 'laststatus'.
5976 */
5977 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005978last_status(
5979 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 /* Don't make a difference between horizontal or vertical split. */
5982 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01005983 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984}
5985
5986 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005987last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 frame_T *fp;
5990 win_T *wp;
5991
5992 if (fr->fr_layout == FR_LEAF)
5993 {
5994 wp = fr->fr_win;
5995 if (wp->w_status_height != 0 && !statusline)
5996 {
5997 /* remove status line */
5998 win_new_height(wp, wp->w_height + 1);
5999 wp->w_status_height = 0;
6000 comp_col();
6001 }
6002 else if (wp->w_status_height == 0 && statusline)
6003 {
6004 /* Find a frame to take a line from. */
6005 fp = fr;
6006 while (fp->fr_height <= frame_minheight(fp, NULL))
6007 {
6008 if (fp == topframe)
6009 {
6010 EMSG(_(e_noroom));
6011 return;
6012 }
6013 /* In a column of frames: go to frame above. If already at
6014 * the top or in a row of frames: go to parent. */
6015 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6016 fp = fp->fr_prev;
6017 else
6018 fp = fp->fr_parent;
6019 }
6020 wp->w_status_height = 1;
6021 if (fp != fr)
6022 {
6023 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6024 frame_fix_height(wp);
6025 (void)win_comp_pos();
6026 }
6027 else
6028 win_new_height(wp, wp->w_height - 1);
6029 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006030 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 else if (fr->fr_layout == FR_ROW)
6034 {
6035 /* vertically split windows, set status line for each one */
6036 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6037 last_status_rec(fp, statusline);
6038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 else
6040 {
6041 /* horizontally split window, set status line for last one */
6042 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6043 ;
6044 last_status_rec(fp, statusline);
6045 }
6046}
6047
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006048/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006049 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006050 */
6051 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006052tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006053{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006054#ifdef FEAT_GUI_TABLINE
6055 /* When the GUI has the tabline then this always returns zero. */
6056 if (gui_use_tabline())
6057 return 0;
6058#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006059 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006060 {
6061 case 0: return 0;
6062 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6063 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006064 return 1;
6065}
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6068/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006069 * Get the file name at the cursor.
6070 * If Visual mode is active, use the selected text if it's in one line.
6071 * Returns the name in allocated memory, NULL for failure.
6072 */
6073 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006074grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006075{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006076 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6077
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006078 if (VIsual_active)
6079 {
6080 int len;
6081 char_u *ptr;
6082
6083 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6084 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006085 return find_file_name_in_path(ptr, len, options,
6086 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006087 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006088 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006089
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006090}
6091
6092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 * Return the file name under or after the cursor.
6094 *
6095 * The 'path' option is searched if the file name is not absolute.
6096 * The string returned has been alloc'ed and should be freed by the caller.
6097 * NULL is returned if the file name or file is not found.
6098 *
6099 * options:
6100 * FNAME_MESS give error messages
6101 * FNAME_EXP expand to path
6102 * FNAME_HYP check for hypertext link
6103 * FNAME_INCL apply "includeexpr"
6104 */
6105 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006106file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
6108 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006109 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6110 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111}
6112
6113/*
6114 * Return the name of the file under or after ptr[col].
6115 * Otherwise like file_name_at_cursor().
6116 */
6117 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006118file_name_in_line(
6119 char_u *line,
6120 int col,
6121 int options,
6122 long count,
6123 char_u *rel_fname, /* file we are searching relative to */
6124 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
6126 char_u *ptr;
6127 int len;
6128
6129 /*
6130 * search forward for what could be the start of a file name
6131 */
6132 ptr = line + col;
6133 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006134 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (*ptr == NUL) /* nothing found */
6136 {
6137 if (options & FNAME_MESS)
6138 EMSG(_("E446: No file name under cursor"));
6139 return NULL;
6140 }
6141
6142 /*
6143 * Search backward for first char of the file name.
6144 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6145 */
6146 while (ptr > line)
6147 {
6148#ifdef FEAT_MBYTE
6149 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6150 ptr -= len + 1;
6151 else
6152#endif
6153 if (vim_isfilec(ptr[-1])
6154 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6155 --ptr;
6156 else
6157 break;
6158 }
6159
6160 /*
6161 * Search forward for the last char of the file name.
6162 * Also allow "://" when ':' is not in 'isfname'.
6163 */
6164 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006165 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006167 {
6168 if (ptr[len] == '\\')
6169 /* Skip over the "\" in "\ ". */
6170 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171#ifdef FEAT_MBYTE
6172 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006173 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 else
6175#endif
6176 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
6179 /*
6180 * If there is trailing punctuation, remove it.
6181 * But don't remove "..", could be a directory name.
6182 */
6183 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6184 && ptr[len - 2] != '.')
6185 --len;
6186
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006187 if (file_lnum != NULL)
6188 {
6189 char_u *p;
6190
6191 /* Get the number after the file name and a separator character */
6192 p = ptr + len;
6193 p = skipwhite(p);
6194 if (*p != NUL)
6195 {
6196 if (!isdigit(*p))
6197 ++p; /* skip the separator */
6198 p = skipwhite(p);
6199 if (isdigit(*p))
6200 *file_lnum = (int)getdigits(&p);
6201 }
6202 }
6203
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6205}
6206
6207# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006208static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
6210 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006211eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 char_u *res;
6214
6215 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006216 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006217 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 set_vim_var_string(VV_FNAME, NULL, 0);
6219 return res;
6220}
6221#endif
6222
6223/*
6224 * Return the name of the file ptr[len] in 'path'.
6225 * Otherwise like file_name_at_cursor().
6226 */
6227 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006228find_file_name_in_path(
6229 char_u *ptr,
6230 int len,
6231 int options,
6232 long count,
6233 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
6235 char_u *file_name;
6236 int c;
6237# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6238 char_u *tofree = NULL;
6239
6240 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6241 {
6242 tofree = eval_includeexpr(ptr, len);
6243 if (tofree != NULL)
6244 {
6245 ptr = tofree;
6246 len = (int)STRLEN(ptr);
6247 }
6248 }
6249# endif
6250
6251 if (options & FNAME_EXP)
6252 {
6253 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6254 TRUE, rel_fname);
6255
6256# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6257 /*
6258 * If the file could not be found in a normal way, try applying
6259 * 'includeexpr' (unless done already).
6260 */
6261 if (file_name == NULL
6262 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6263 {
6264 tofree = eval_includeexpr(ptr, len);
6265 if (tofree != NULL)
6266 {
6267 ptr = tofree;
6268 len = (int)STRLEN(ptr);
6269 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6270 TRUE, rel_fname);
6271 }
6272 }
6273# endif
6274 if (file_name == NULL && (options & FNAME_MESS))
6275 {
6276 c = ptr[len];
6277 ptr[len] = NUL;
6278 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6279 ptr[len] = c;
6280 }
6281
6282 /* Repeat finding the file "count" times. This matters when it
6283 * appears several times in the path. */
6284 while (file_name != NULL && --count > 0)
6285 {
6286 vim_free(file_name);
6287 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6288 }
6289 }
6290 else
6291 file_name = vim_strnsave(ptr, len);
6292
6293# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6294 vim_free(tofree);
6295# endif
6296
6297 return file_name;
6298}
6299#endif /* FEAT_SEARCHPATH */
6300
6301/*
6302 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6303 * Also check for ":\\", which MS Internet Explorer accepts, return
6304 * URL_BACKSLASH.
6305 */
6306 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006307path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 if (STRNCMP(p, "://", (size_t)3) == 0)
6310 return URL_SLASH;
6311 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6312 return URL_BACKSLASH;
6313 return 0;
6314}
6315
6316/*
6317 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6318 * Return URL_BACKSLASH for "name:\\".
6319 * Return zero otherwise.
6320 */
6321 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006322path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323{
6324 char_u *p;
6325
6326 for (p = fname; isalpha(*p); ++p)
6327 ;
6328 return path_is_url(p);
6329}
6330
6331/*
6332 * Return TRUE if "name" is a full (absolute) path name or URL.
6333 */
6334 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006335vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 return (path_with_url(name) != 0 || mch_isFullName(name));
6338}
6339
6340/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006341 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 *
6343 * return FAIL for failure, OK otherwise
6344 */
6345 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006346vim_FullName(
6347 char_u *fname,
6348 char_u *buf,
6349 int len,
6350 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 int retval = OK;
6353 int url;
6354
6355 *buf = NUL;
6356 if (fname == NULL)
6357 return FAIL;
6358
6359 url = path_with_url(fname);
6360 if (!url)
6361 retval = mch_FullName(fname, buf, len, force);
6362 if (url || retval == FAIL)
6363 {
6364 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006365 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 }
Bram Moolenaard0573012017-10-28 21:11:06 +02006367#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 slash_adjust(buf);
6369#endif
6370 return retval;
6371}
6372
6373/*
6374 * Return the minimal number of rows that is needed on the screen to display
6375 * the current number of windows.
6376 */
6377 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006378min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006381 tabpage_T *tp;
6382 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384 if (firstwin == NULL) /* not initialized yet */
6385 return MIN_LINES;
6386
Bram Moolenaarf740b292006-02-16 22:11:02 +00006387 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006388 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006389 {
6390 n = frame_minheight(tp->tp_topframe, NULL);
6391 if (total < n)
6392 total = n;
6393 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006394 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006395 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 return total;
6397}
6398
6399/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006400 * Return TRUE if there is only one window (in the current tab page), not
6401 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006402 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 */
6404 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006405only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 int count = 0;
6408 win_T *wp;
6409
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006410 /* If there is another tab page there always is another window. */
6411 if (first_tabpage->tp_next != NULL)
6412 return FALSE;
6413
Bram Moolenaar29323592016-07-24 22:04:11 +02006414 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006415 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006416 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417# ifdef FEAT_QUICKFIX
6418 || wp->w_p_pvw
6419# endif
6420 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006421# ifdef FEAT_AUTOCMD
6422 && wp != aucmd_win
6423# endif
6424 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 ++count;
6426 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427}
6428
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429/*
6430 * Correct the cursor line number in other windows. Used after changing the
6431 * current buffer, and before applying autocommands.
6432 * When "do_curwin" is TRUE, also check current window.
6433 */
6434 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006435check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006438 tabpage_T *tp;
6439
6440 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
6443 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6444 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6445 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6446 wp->w_topline = curbuf->b_ml.ml_line_count;
6447 }
6448}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449
6450/*
6451 * A snapshot of the window sizes, to restore them after closing the help
6452 * window.
6453 * Only these fields are used:
6454 * fr_layout
6455 * fr_width
6456 * fr_height
6457 * fr_next
6458 * fr_child
6459 * fr_win (only valid for the old curwin, NULL otherwise)
6460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462/*
6463 * Create a snapshot of the current frame sizes.
6464 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006465 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006466make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006468 clear_snapshot(curtab, idx);
6469 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470}
6471
6472 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006473make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6476 if (*frp == NULL)
6477 return;
6478 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 (*frp)->fr_height = fr->fr_height;
6481 if (fr->fr_next != NULL)
6482 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6483 if (fr->fr_child != NULL)
6484 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6485 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6486 (*frp)->fr_win = curwin;
6487}
6488
6489/*
6490 * Remove any existing snapshot.
6491 */
6492 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006493clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006495 clear_snapshot_rec(tp->tp_snapshot[idx]);
6496 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497}
6498
6499 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006500clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 if (fr != NULL)
6503 {
6504 clear_snapshot_rec(fr->fr_next);
6505 clear_snapshot_rec(fr->fr_child);
6506 vim_free(fr);
6507 }
6508}
6509
6510/*
6511 * Restore a previously created snapshot, if there is any.
6512 * This is only done if the screen size didn't change and the window layout is
6513 * still the same.
6514 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006515 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006516restore_snapshot(
6517 int idx,
6518 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 win_T *wp;
6521
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006522 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006523 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006524 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6525 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006527 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 win_comp_pos();
6529 if (wp != NULL && close_curwin)
6530 win_goto(wp);
6531 redraw_all_later(CLEAR);
6532 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006533 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534}
6535
6536/*
6537 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006538 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 */
6540 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006541check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542{
6543 if (sn->fr_layout != fr->fr_layout
6544 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6545 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6546 || (sn->fr_next != NULL
6547 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6548 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006549 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006550 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 return FAIL;
6552 return OK;
6553}
6554
6555/*
6556 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6557 * following frames and children.
6558 * Returns a pointer to the old current window, or NULL.
6559 */
6560 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006561restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562{
6563 win_T *wp = NULL;
6564 win_T *wp2;
6565
6566 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 if (fr->fr_layout == FR_LEAF)
6569 {
6570 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006571 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 wp = sn->fr_win;
6573 }
6574 if (sn->fr_next != NULL)
6575 {
6576 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6577 if (wp2 != NULL)
6578 wp = wp2;
6579 }
6580 if (sn->fr_child != NULL)
6581 {
6582 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6583 if (wp2 != NULL)
6584 wp = wp2;
6585 }
6586 return wp;
6587}
6588
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006589#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6590 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006591/*
6592 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006593 * restore_win() MUST be called to undo, also when FAIL is returned.
6594 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006595 * When "no_display" is TRUE the display won't be affected, no redraw is
6596 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006597 * Returns FAIL if switching to "win" failed.
6598 */
6599 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006600switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006601 win_T **save_curwin,
6602 tabpage_T **save_curtab,
6603 win_T *win,
6604 tabpage_T *tp,
6605 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006606{
6607# ifdef FEAT_AUTOCMD
6608 block_autocmds();
6609# endif
Bram Moolenaar105bc352013-05-17 16:03:57 +02006610 *save_curwin = curwin;
6611 if (tp != NULL)
6612 {
6613 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006614 if (no_display)
6615 {
6616 curtab->tp_firstwin = firstwin;
6617 curtab->tp_lastwin = lastwin;
6618 curtab = tp;
6619 firstwin = curtab->tp_firstwin;
6620 lastwin = curtab->tp_lastwin;
6621 }
6622 else
6623 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006624 }
6625 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006626 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006627 curwin = win;
6628 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006629 return OK;
6630}
6631
6632/*
6633 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006634 * When "no_display" is TRUE the display won't be affected, no redraw is
6635 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006636 */
6637 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006638restore_win(
6639 win_T *save_curwin UNUSED,
6640 tabpage_T *save_curtab UNUSED,
6641 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006642{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006643 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006644 {
6645 if (no_display)
6646 {
6647 curtab->tp_firstwin = firstwin;
6648 curtab->tp_lastwin = lastwin;
6649 curtab = save_curtab;
6650 firstwin = curtab->tp_firstwin;
6651 lastwin = curtab->tp_lastwin;
6652 }
6653 else
6654 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6655 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006656 if (win_valid(save_curwin))
6657 {
6658 curwin = save_curwin;
6659 curbuf = curwin->w_buffer;
6660 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006661# ifdef FEAT_AUTOCMD
6662 unblock_autocmds();
6663# endif
6664}
6665
6666/*
6667 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6668 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6669 */
6670 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006671switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006672{
6673# ifdef FEAT_AUTOCMD
6674 block_autocmds();
6675# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006676 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006677 --curbuf->b_nwindows;
6678 curbuf = buf;
6679 curwin->w_buffer = buf;
6680 ++curbuf->b_nwindows;
6681}
6682
6683/*
6684 * Restore the current buffer after using switch_buffer().
6685 */
6686 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006687restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006688{
6689# ifdef FEAT_AUTOCMD
6690 unblock_autocmds();
6691# endif
6692 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006693 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006694 {
6695 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006696 curwin->w_buffer = save_curbuf->br_buf;
6697 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006698 ++curbuf->b_nwindows;
6699 }
6700}
6701#endif
6702
Bram Moolenaar4033c552017-09-16 20:54:51 +02006703#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704/*
6705 * Return TRUE if there is any vertically split window.
6706 */
6707 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006708win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709{
6710 frame_T *fr;
6711
6712 if (topframe->fr_layout == FR_ROW)
6713 return TRUE;
6714
6715 if (topframe->fr_layout == FR_COL)
6716 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6717 if (fr->fr_layout == FR_ROW)
6718 return TRUE;
6719
6720 return FALSE;
6721}
6722#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006723
6724#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6725/*
6726 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006727 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006728 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6729 * If no particular ID is desired, -1 must be specified for 'id'.
6730 * Return ID of added match, -1 on failure.
6731 */
6732 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006733match_add(
6734 win_T *wp,
6735 char_u *grp,
6736 char_u *pat,
6737 int prio,
6738 int id,
6739 list_T *pos_list,
6740 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006741{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006742 matchitem_T *cur;
6743 matchitem_T *prev;
6744 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006745 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006746 regprog_T *regprog = NULL;
6747 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006748
Bram Moolenaarb3414592014-06-17 17:48:32 +02006749 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006750 return -1;
6751 if (id < -1 || id == 0)
6752 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006753 EMSGN(_("E799: Invalid ID: %ld (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006754 return -1;
6755 }
6756 if (id != -1)
6757 {
6758 cur = wp->w_match_head;
6759 while (cur != NULL)
6760 {
6761 if (cur->id == id)
6762 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006763 EMSGN(_("E801: ID already taken: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006764 return -1;
6765 }
6766 cur = cur->next;
6767 }
6768 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006769 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006770 {
6771 EMSG2(_(e_nogroup), grp);
6772 return -1;
6773 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006774 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006775 {
6776 EMSG2(_(e_invarg2), pat);
6777 return -1;
6778 }
6779
6780 /* Find available match ID. */
6781 while (id == -1)
6782 {
6783 cur = wp->w_match_head;
6784 while (cur != NULL && cur->id != wp->w_next_match_id)
6785 cur = cur->next;
6786 if (cur == NULL)
6787 id = wp->w_next_match_id;
6788 wp->w_next_match_id++;
6789 }
6790
6791 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006792 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006793 m->id = id;
6794 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006795 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006796 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006797 m->match.regprog = regprog;
6798 m->match.rmm_ic = FALSE;
6799 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006800# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006801 m->conceal_char = 0;
6802 if (conceal_char != NULL)
6803 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006804# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006805
Bram Moolenaarb3414592014-06-17 17:48:32 +02006806 /* Set up position matches */
6807 if (pos_list != NULL)
6808 {
6809 linenr_T toplnum = 0;
6810 linenr_T botlnum = 0;
6811 listitem_T *li;
6812 int i;
6813
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006814 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006815 i++, li = li->li_next)
6816 {
6817 linenr_T lnum = 0;
6818 colnr_T col = 0;
6819 int len = 1;
6820 list_T *subl;
6821 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006822 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006823
Bram Moolenaarb3414592014-06-17 17:48:32 +02006824 if (li->li_tv.v_type == VAR_LIST)
6825 {
6826 subl = li->li_tv.vval.v_list;
6827 if (subl == NULL)
6828 goto fail;
6829 subli = subl->lv_first;
6830 if (subli == NULL)
6831 goto fail;
6832 lnum = get_tv_number_chk(&subli->li_tv, &error);
6833 if (error == TRUE)
6834 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006835 if (lnum == 0)
6836 {
6837 --i;
6838 continue;
6839 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006840 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006841 subli = subli->li_next;
6842 if (subli != NULL)
6843 {
6844 col = get_tv_number_chk(&subli->li_tv, &error);
6845 if (error == TRUE)
6846 goto fail;
6847 subli = subli->li_next;
6848 if (subli != NULL)
6849 {
6850 len = get_tv_number_chk(&subli->li_tv, &error);
6851 if (error == TRUE)
6852 goto fail;
6853 }
6854 }
6855 m->pos.pos[i].col = col;
6856 m->pos.pos[i].len = len;
6857 }
6858 else if (li->li_tv.v_type == VAR_NUMBER)
6859 {
6860 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006861 {
6862 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006863 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006864 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006865 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6866 m->pos.pos[i].col = 0;
6867 m->pos.pos[i].len = 0;
6868 }
6869 else
6870 {
6871 EMSG(_("List or number required"));
6872 goto fail;
6873 }
6874 if (toplnum == 0 || lnum < toplnum)
6875 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006876 if (botlnum == 0 || lnum >= botlnum)
6877 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006878 }
6879
6880 /* Calculate top and bottom lines for redrawing area */
6881 if (toplnum != 0)
6882 {
6883 if (wp->w_buffer->b_mod_set)
6884 {
6885 if (wp->w_buffer->b_mod_top > toplnum)
6886 wp->w_buffer->b_mod_top = toplnum;
6887 if (wp->w_buffer->b_mod_bot < botlnum)
6888 wp->w_buffer->b_mod_bot = botlnum;
6889 }
6890 else
6891 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006892 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006893 wp->w_buffer->b_mod_top = toplnum;
6894 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006895 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006896 }
6897 m->pos.toplnum = toplnum;
6898 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006899 rtype = VALID;
6900 }
6901 }
6902
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006903 /* Insert new match. The match list is in ascending order with regard to
6904 * the match priorities. */
6905 cur = wp->w_match_head;
6906 prev = cur;
6907 while (cur != NULL && prio >= cur->priority)
6908 {
6909 prev = cur;
6910 cur = cur->next;
6911 }
6912 if (cur == prev)
6913 wp->w_match_head = m;
6914 else
6915 prev->next = m;
6916 m->next = cur;
6917
Bram Moolenaarb3414592014-06-17 17:48:32 +02006918 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006919 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006920
6921fail:
6922 vim_free(m);
6923 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006924}
6925
6926/*
6927 * Delete match with ID 'id' in the match list of window 'wp'.
6928 * Print error messages if 'perr' is TRUE.
6929 */
6930 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006931match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006932{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006933 matchitem_T *cur = wp->w_match_head;
6934 matchitem_T *prev = cur;
6935 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006936
6937 if (id < 1)
6938 {
6939 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006940 EMSGN(_("E802: Invalid ID: %ld (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006941 id);
6942 return -1;
6943 }
6944 while (cur != NULL && cur->id != id)
6945 {
6946 prev = cur;
6947 cur = cur->next;
6948 }
6949 if (cur == NULL)
6950 {
6951 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006952 EMSGN(_("E803: ID not found: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006953 return -1;
6954 }
6955 if (cur == prev)
6956 wp->w_match_head = cur->next;
6957 else
6958 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006959 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006960 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006961 if (cur->pos.toplnum != 0)
6962 {
6963 if (wp->w_buffer->b_mod_set)
6964 {
6965 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6966 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6967 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6968 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6969 }
6970 else
6971 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006972 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006973 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6974 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006975 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006976 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006977 rtype = VALID;
6978 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006979 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006980 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006981 return 0;
6982}
6983
6984/*
6985 * Delete all matches in the match list of window 'wp'.
6986 */
6987 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006988clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006989{
6990 matchitem_T *m;
6991
6992 while (wp->w_match_head != NULL)
6993 {
6994 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006995 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006996 vim_free(wp->w_match_head->pattern);
6997 vim_free(wp->w_match_head);
6998 wp->w_match_head = m;
6999 }
7000 redraw_later(SOME_VALID);
7001}
7002
7003/*
7004 * Get match from ID 'id' in window 'wp'.
7005 * Return NULL if match not found.
7006 */
7007 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007008get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007009{
7010 matchitem_T *cur = wp->w_match_head;
7011
7012 while (cur != NULL && cur->id != id)
7013 cur = cur->next;
7014 return cur;
7015}
7016#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007017
7018#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7019 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007020get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007021{
7022 int i = 1;
7023 win_T *w;
7024
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007025 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007026 ++i;
7027
7028 if (w == NULL)
7029 return 0;
7030 else
7031 return i;
7032}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007033
7034 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007035get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007036{
7037 int i = 1;
7038 tabpage_T *t;
7039
7040 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7041 ++i;
7042
7043 if (t == NULL)
7044 return 0;
7045 else
7046 return i;
7047}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007048#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007049
7050/*
7051 * Return TRUE if "topfrp" and its children are at the right height.
7052 */
7053 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007054frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007055{
7056 frame_T *frp;
7057
7058 if (topfrp->fr_height != height)
7059 return FALSE;
7060
7061 if (topfrp->fr_layout == FR_ROW)
7062 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7063 if (frp->fr_height != height)
7064 return FALSE;
7065
7066 return TRUE;
7067}
7068
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007069/*
7070 * Return TRUE if "topfrp" and its children are at the right width.
7071 */
7072 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007073frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007074{
7075 frame_T *frp;
7076
7077 if (topfrp->fr_width != width)
7078 return FALSE;
7079
7080 if (topfrp->fr_layout == FR_COL)
7081 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7082 if (frp->fr_width != width)
7083 return FALSE;
7084
7085 return TRUE;
7086}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007087
Bram Moolenaar86edef62016-03-13 18:07:30 +01007088#if defined(FEAT_EVAL) || defined(PROTO)
7089 int
7090win_getid(typval_T *argvars)
7091{
7092 int winnr;
7093 win_T *wp;
7094
7095 if (argvars[0].v_type == VAR_UNKNOWN)
7096 return curwin->w_id;
7097 winnr = get_tv_number(&argvars[0]);
7098 if (winnr > 0)
7099 {
7100 if (argvars[1].v_type == VAR_UNKNOWN)
7101 wp = firstwin;
7102 else
7103 {
7104 tabpage_T *tp;
7105 int tabnr = get_tv_number(&argvars[1]);
7106
Bram Moolenaar29323592016-07-24 22:04:11 +02007107 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007108 if (--tabnr == 0)
7109 break;
7110 if (tp == NULL)
7111 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01007112 if (tp == curtab)
7113 wp = firstwin;
7114 else
7115 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01007116 }
7117 for ( ; wp != NULL; wp = wp->w_next)
7118 if (--winnr == 0)
7119 return wp->w_id;
7120 }
7121 return 0;
7122}
7123
7124 int
7125win_gotoid(typval_T *argvars)
7126{
7127 win_T *wp;
7128 tabpage_T *tp;
7129 int id = get_tv_number(&argvars[0]);
7130
Bram Moolenaar29323592016-07-24 22:04:11 +02007131 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007132 if (wp->w_id == id)
7133 {
7134 goto_tabpage_win(tp, wp);
7135 return 1;
7136 }
7137 return 0;
7138}
7139
7140 void
7141win_id2tabwin(typval_T *argvars, list_T *list)
7142{
7143 win_T *wp;
7144 tabpage_T *tp;
7145 int winnr = 1;
7146 int tabnr = 1;
7147 int id = get_tv_number(&argvars[0]);
7148
Bram Moolenaar29323592016-07-24 22:04:11 +02007149 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007150 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007151 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007152 {
7153 if (wp->w_id == id)
7154 {
7155 list_append_number(list, tabnr);
7156 list_append_number(list, winnr);
7157 return;
7158 }
7159 ++winnr;
7160 }
7161 ++tabnr;
7162 winnr = 1;
7163 }
7164 list_append_number(list, 0);
7165 list_append_number(list, 0);
7166}
7167
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007168 win_T *
7169win_id2wp(typval_T *argvars)
7170{
7171 win_T *wp;
7172 tabpage_T *tp;
7173 int id = get_tv_number(&argvars[0]);
7174
7175 FOR_ALL_TAB_WINDOWS(tp, wp)
7176 if (wp->w_id == id)
7177 return wp;
7178
7179 return NULL;
7180}
7181
Bram Moolenaar86edef62016-03-13 18:07:30 +01007182 int
7183win_id2win(typval_T *argvars)
7184{
7185 win_T *wp;
7186 int nr = 1;
7187 int id = get_tv_number(&argvars[0]);
7188
Bram Moolenaar29323592016-07-24 22:04:11 +02007189 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007190 {
7191 if (wp->w_id == id)
7192 return nr;
7193 ++nr;
7194 }
7195 return 0;
7196}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007197
7198 void
7199win_findbuf(typval_T *argvars, list_T *list)
7200{
7201 win_T *wp;
7202 tabpage_T *tp;
7203 int bufnr = get_tv_number(&argvars[0]);
7204
Bram Moolenaar29323592016-07-24 22:04:11 +02007205 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007206 if (wp->w_buffer->b_fnum == bufnr)
7207 list_append_number(list, wp->w_id);
7208}
7209
Bram Moolenaar86edef62016-03-13 18:07:30 +01007210#endif