blob: 337f84dee12b807696783b1bb3cc7978d094694b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010012static int path_is_url(char_u *p);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010013static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
14static void win_init(win_T *newp, win_T *oldp, int flags);
15static void win_init_some(win_T *newp, win_T *oldp);
16static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
17static void frame_setheight(frame_T *curfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static void win_exchange(long);
20static void win_rotate(int, int);
21static void win_totop(int size, int flags);
22static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
23static int last_window(void);
24static int close_last_window_tabpage(win_T *win, int free_buf, tabpage_T *prev_curtab);
25static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
26static frame_T *win_altframe(win_T *win, tabpage_T *tp);
27static tabpage_T *alt_tabpage(void);
28static win_T *frame2win(frame_T *frp);
29static int frame_has_win(frame_T *frp, win_T *wp);
30static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
31static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010032static int frame_fixed_width(frame_T *frp);
33static void frame_add_statusline(frame_T *frp);
34static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
35static void frame_add_vsep(frame_T *frp);
36static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
37static void frame_fix_width(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static int win_alloc_firstwin(win_T *oldwin);
39static void new_frame(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010040static tabpage_T *alloc_tabpage(void);
41static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
42static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
43static void frame_fix_height(win_T *wp);
44static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
Bram Moolenaarc917da42016-07-19 22:31:36 +020045static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_new_autocmds, int trigger_enter_autocmds, int trigger_leave_autocmds);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010046static void win_free(win_T *wp, tabpage_T *tp);
47static void frame_append(frame_T *after, frame_T *frp);
48static void frame_insert(frame_T *before, frame_T *frp);
49static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void win_goto_ver(int up, long count);
51static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void frame_add_height(frame_T *frp, int n);
53static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void make_snapshot_rec(frame_T *fr, frame_T **frp);
56static void clear_snapshot(tabpage_T *tp, int idx);
57static void clear_snapshot_rec(frame_T *fr);
58static int check_snapshot_rec(frame_T *sn, frame_T *fr);
59static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020063
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66#define URL_SLASH 1 /* path_is_url() has found "://" */
67#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
68
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000069#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaar4033c552017-09-16 20:54:51 +020071#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000072
73static char *m_onlyone = N_("Already only one window");
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075/*
76 * all CTRL-W window commands are handled here, called from normal_cmd().
77 */
78 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010079do_window(
80 int nchar,
81 long Prenum,
82 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 long Prenum1;
85 win_T *wp;
86#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
87 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000088 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
90#ifdef FEAT_FIND_ID
91 int type = FIND_DEFINE;
92 int len;
93#endif
94 char_u cbuf[40];
95
96 if (Prenum == 0)
97 Prenum1 = 1;
98 else
99 Prenum1 = Prenum;
100
101#ifdef FEAT_CMDWIN
102# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
103#else
104# define CHECK_CMDWIN
105#endif
106
107 switch (nchar)
108 {
109/* split current window in two parts, horizontally */
110 case 'S':
111 case Ctrl_S:
112 case 's':
113 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000115#ifdef FEAT_QUICKFIX
116 /* When splitting the quickfix window open a new buffer in it,
117 * don't replicate the quickfix buffer. */
118 if (bt_quickfix(curbuf))
119 goto newwindow;
120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_GUI
122 need_mouse_correct = TRUE;
123#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200124 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 break;
126
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127/* split current window in two parts, vertically */
128 case Ctrl_V:
129 case 'v':
130 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100132#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100137#endif
138#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100140#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200141 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144/* split current window and edit alternate file */
145 case Ctrl_HAT:
146 case '^':
147 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100149 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 do_cmdline_cmd(cbuf);
151 break;
152
153/* open new window */
154 case Ctrl_N:
155 case 'n':
156 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000158#ifdef FEAT_QUICKFIX
159newwindow:
160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000162 /* window height */
163 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 else
165 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100166#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000167 if (nchar == 'v' || nchar == Ctrl_V)
168 STRCAT(cbuf, "v");
169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 STRCAT(cbuf, "new");
171 do_cmdline_cmd(cbuf);
172 break;
173
174/* quit current window */
175 case Ctrl_Q:
176 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100178 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100179 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 break;
181
182/* close current window */
183 case Ctrl_C:
184 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100186 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100187 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 break;
189
Bram Moolenaar4033c552017-09-16 20:54:51 +0200190#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/* close preview window */
192 case Ctrl_Z:
193 case 'z':
194 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 do_cmdline_cmd((char_u *)"pclose");
197 break;
198
199/* cursor to preview window */
200 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200201 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 if (wp->w_p_pvw)
203 break;
204 if (wp == NULL)
205 EMSG(_("E441: There is no preview window"));
206 else
207 win_goto(wp);
208 break;
209#endif
210
211/* close all but current window */
212 case Ctrl_O:
213 case 'o':
214 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100216 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100217 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 break;
219
220/* cursor to next window with wrap around */
221 case Ctrl_W:
222 case 'w':
223/* cursor to previous window with wrap around */
224 case 'W':
225 CHECK_CMDWIN
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100226 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 beep_flush();
228 else
229 {
230 if (Prenum) /* go to specified window */
231 {
232 for (wp = firstwin; --Prenum > 0; )
233 {
234 if (wp->w_next == NULL)
235 break;
236 else
237 wp = wp->w_next;
238 }
239 }
240 else
241 {
242 if (nchar == 'W') /* go to previous window */
243 {
244 wp = curwin->w_prev;
245 if (wp == NULL)
246 wp = lastwin; /* wrap around */
247 }
248 else /* go to next window */
249 {
250 wp = curwin->w_next;
251 if (wp == NULL)
252 wp = firstwin; /* wrap around */
253 }
254 }
255 win_goto(wp);
256 }
257 break;
258
259/* cursor to window below */
260 case 'j':
261 case K_DOWN:
262 case Ctrl_J:
263 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 break;
266
267/* cursor to window above */
268 case 'k':
269 case K_UP:
270 case Ctrl_K:
271 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 break;
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275/* cursor to left window */
276 case 'h':
277 case K_LEFT:
278 case Ctrl_H:
279 case K_BS:
280 CHECK_CMDWIN
281 win_goto_hor(TRUE, Prenum1);
282 break;
283
284/* cursor to right window */
285 case 'l':
286 case K_RIGHT:
287 case Ctrl_L:
288 CHECK_CMDWIN
289 win_goto_hor(FALSE, Prenum1);
290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000292/* move window to new tab page */
293 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000294 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000295 MSG(_(m_onlyone));
296 else
297 {
298 tabpage_T *oldtab = curtab;
299 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000300
301 /* First create a new tab with the window, then go back to
302 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000303 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000304 if (win_new_tabpage((int)Prenum) == OK
305 && valid_tabpage(oldtab))
306 {
307 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200308 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000309 if (curwin == wp)
310 win_close(curwin, FALSE);
311 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200312 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000313 }
314 }
315 break;
316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317/* cursor to top-left window */
318 case 't':
319 case Ctrl_T:
320 win_goto(firstwin);
321 break;
322
323/* cursor to bottom-right window */
324 case 'b':
325 case Ctrl_B:
326 win_goto(lastwin);
327 break;
328
329/* cursor to last accessed (previous) window */
330 case 'p':
331 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200332 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 beep_flush();
334 else
335 win_goto(prevwin);
336 break;
337
338/* exchange current and next window */
339 case 'x':
340 case Ctrl_X:
341 CHECK_CMDWIN
342 win_exchange(Prenum);
343 break;
344
345/* rotate windows downwards */
346 case Ctrl_R:
347 case 'r':
348 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 win_rotate(FALSE, (int)Prenum1); /* downwards */
351 break;
352
353/* rotate windows upwards */
354 case 'R':
355 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 win_rotate(TRUE, (int)Prenum1); /* upwards */
358 break;
359
360/* move window to the very top/bottom/left/right */
361 case 'K':
362 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 case 'H':
364 case 'L':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 CHECK_CMDWIN
366 win_totop((int)Prenum,
367 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
368 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
369 break;
370
371/* make all windows the same height */
372 case '=':
373#ifdef FEAT_GUI
374 need_mouse_correct = TRUE;
375#endif
376 win_equal(NULL, FALSE, 'b');
377 break;
378
379/* increase current window height */
380 case '+':
381#ifdef FEAT_GUI
382 need_mouse_correct = TRUE;
383#endif
384 win_setheight(curwin->w_height + (int)Prenum1);
385 break;
386
387/* decrease current window height */
388 case '-':
389#ifdef FEAT_GUI
390 need_mouse_correct = TRUE;
391#endif
392 win_setheight(curwin->w_height - (int)Prenum1);
393 break;
394
395/* set current window height */
396 case Ctrl__:
397 case '_':
398#ifdef FEAT_GUI
399 need_mouse_correct = TRUE;
400#endif
401 win_setheight(Prenum ? (int)Prenum : 9999);
402 break;
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/* increase current window width */
405 case '>':
406#ifdef FEAT_GUI
407 need_mouse_correct = TRUE;
408#endif
409 win_setwidth(curwin->w_width + (int)Prenum1);
410 break;
411
412/* decrease current window width */
413 case '<':
414#ifdef FEAT_GUI
415 need_mouse_correct = TRUE;
416#endif
417 win_setwidth(curwin->w_width - (int)Prenum1);
418 break;
419
420/* set current window width */
421 case '|':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427
428/* jump to tag and split window if tag exists (in preview window) */
429#if defined(FEAT_QUICKFIX)
430 case '}':
431 CHECK_CMDWIN
432 if (Prenum)
433 g_do_tagpreview = Prenum;
434 else
435 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200437 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 case ']':
439 case Ctrl_RSB:
440 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200441 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (Prenum)
443 postponed_split = Prenum;
444 else
445 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200446#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200447 if (nchar != '}')
448 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaard355c502014-09-23 13:48:43 +0200451 /* Execute the command right here, required when "wincmd ]"
452 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 do_nv_ident(Ctrl_RSB, NUL);
454 break;
455
456#ifdef FEAT_SEARCHPATH
457/* edit file name under cursor in a new window */
458 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000459 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000461wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000464 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (ptr != NULL)
466 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200467 tabpage_T *oldtab = curtab;
468 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000469# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 setpcmark();
473 if (win_split(0, 0) == OK)
474 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200475 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200476 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
477 ECMD_HIDE, NULL) == FAIL)
478 {
479 /* Failed to open the file, close the window
480 * opened for it. */
481 win_close(curwin, FALSE);
482 goto_tabpage_win(oldtab, oldwin);
483 }
484 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000485 {
486 curwin->w_cursor.lnum = lnum;
487 check_cursor_lnum();
488 beginline(BL_SOL | BL_FIX);
489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 }
491 vim_free(ptr);
492 }
493 break;
494#endif
495
496#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000497/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 * new window -- webb
499 */
500 case 'i': /* Go to any match */
501 case Ctrl_I:
502 type = FIND_ANY;
503 /* FALLTHROUGH */
504 case 'd': /* Go to definition, using 'define' */
505 case Ctrl_D:
506 CHECK_CMDWIN
507 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
508 break;
509 find_pattern_in_path(ptr, 0, len, TRUE,
510 Prenum == 0 ? TRUE : FALSE, type,
511 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
512 curwin->w_set_curswant = TRUE;
513 break;
514#endif
515
Bram Moolenaar05159a02005-02-26 23:04:13 +0000516 case K_KENTER:
517 case CAR:
518#if defined(FEAT_QUICKFIX)
519 /*
520 * In a quickfix window a <CR> jumps to the error under the
521 * cursor in a new window.
522 */
523 if (bt_quickfix(curbuf))
524 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000525 sprintf((char *)cbuf, "split +%ld%s",
526 (long)curwin->w_cursor.lnum,
527 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000528 do_cmdline_cmd(cbuf);
529 }
530#endif
531 break;
532
533
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534/* CTRL-W g extended commands */
535 case 'g':
536 case Ctrl_G:
537 CHECK_CMDWIN
538#ifdef USE_ON_FLY_SCROLL
539 dont_scroll = TRUE; /* disallow scrolling here */
540#endif
541 ++no_mapping;
542 ++allow_keys; /* no mapping for xchar, but allow key codes */
543 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000544 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 --no_mapping;
547 --allow_keys;
548#ifdef FEAT_CMDL_INFO
549 (void)add_to_showcmd(xchar);
550#endif
551 switch (xchar)
552 {
553#if defined(FEAT_QUICKFIX)
554 case '}':
555 xchar = Ctrl_RSB;
556 if (Prenum)
557 g_do_tagpreview = Prenum;
558 else
559 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200561 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 case ']':
563 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200564 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (Prenum)
566 postponed_split = Prenum;
567 else
568 postponed_split = -1;
569
570 /* Execute the command right here, required when
571 * "wincmd g}" was used in a function. */
572 do_nv_ident('g', xchar);
573 break;
574
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000575#ifdef FEAT_SEARCHPATH
576 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000577 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100578 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000579 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000580 goto wingotofile;
581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 default:
583 beep_flush();
584 break;
585 }
586 break;
587
588 default: beep_flush();
589 break;
590 }
591}
592
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100593/*
594 * Figure out the address type for ":wnncmd".
595 */
596 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100597get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100598{
599 switch (*arg)
600 {
601 case 'S':
602 case Ctrl_S:
603 case 's':
604 case Ctrl_N:
605 case 'n':
606 case 'j':
607 case Ctrl_J:
608 case 'k':
609 case Ctrl_K:
610 case 'T':
611 case Ctrl_R:
612 case 'r':
613 case 'R':
614 case 'K':
615 case 'J':
616 case '+':
617 case '-':
618 case Ctrl__:
619 case '_':
620 case '|':
621 case ']':
622 case Ctrl_RSB:
623 case 'g':
624 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100625 case Ctrl_V:
626 case 'v':
627 case 'h':
628 case Ctrl_H:
629 case 'l':
630 case Ctrl_L:
631 case 'H':
632 case 'L':
633 case '>':
634 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100635#if defined(FEAT_QUICKFIX)
636 case '}':
637#endif
638#ifdef FEAT_SEARCHPATH
639 case 'f':
640 case 'F':
641 case Ctrl_F:
642#endif
643#ifdef FEAT_FIND_ID
644 case 'i':
645 case Ctrl_I:
646 case 'd':
647 case Ctrl_D:
648#endif
649 /* window size or any count */
650 eap->addr_type = ADDR_LINES;
651 break;
652
653 case Ctrl_HAT:
654 case '^':
655 /* buffer number */
656 eap->addr_type = ADDR_BUFFERS;
657 break;
658
659 case Ctrl_Q:
660 case 'q':
661 case Ctrl_C:
662 case 'c':
663 case Ctrl_O:
664 case 'o':
665 case Ctrl_W:
666 case 'w':
667 case 'W':
668 case 'x':
669 case Ctrl_X:
670 /* window number */
671 eap->addr_type = ADDR_WINDOWS;
672 break;
673
674#if defined(FEAT_QUICKFIX)
675 case Ctrl_Z:
676 case 'z':
677 case 'P':
678#endif
679 case 't':
680 case Ctrl_T:
681 case 'b':
682 case Ctrl_B:
683 case 'p':
684 case Ctrl_P:
685 case '=':
686 case CAR:
687 /* no count */
688 eap->addr_type = 0;
689 break;
690 }
691}
692
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100693 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100694cmd_with_count(
695 char *cmd,
696 char_u *bufp,
697 size_t bufsize,
698 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100699{
700 size_t len = STRLEN(cmd);
701
702 STRCPY(bufp, cmd);
703 if (Prenum > 0)
704 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
705}
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707/*
708 * split the current window, implements CTRL-W s and :split
709 *
710 * "size" is the height or width for the new window, 0 to use half of current
711 * height or width.
712 *
713 * "flags":
714 * WSP_ROOM: require enough room for new window
715 * WSP_VERT: vertical split.
716 * WSP_TOP: open window at the top-left of the shell (help window).
717 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
718 * WSP_HELP: creating the help window, keep layout snapshot
719 *
720 * return FAIL for failure, OK otherwise
721 */
722 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100723win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000725 /* When the ":tab" modifier was used open a new tab page instead. */
726 if (may_open_tabpage() == OK)
727 return OK;
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 /* Add flags from ":vertical", ":topleft" and ":botright". */
730 flags |= cmdmod.split;
731 if ((flags & WSP_TOP) && (flags & WSP_BOT))
732 {
733 EMSG(_("E442: Can't split topleft and botright at the same time"));
734 return FAIL;
735 }
736
737 /* When creating the help window make a snapshot of the window layout.
738 * Otherwise clear the snapshot, it's now invalid. */
739 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000740 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000742 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 return win_split_ins(size, flags, NULL, 0);
745}
746
747/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100748 * When "new_wp" is NULL: split the current window in two.
749 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 * top/left/right/bottom.
751 * return FAIL for failure, OK otherwise
752 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000753 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100754win_split_ins(
755 int size,
756 int flags,
757 win_T *new_wp,
758 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100760 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 win_T *oldwin;
762 int new_size = size;
763 int i;
764 int need_status = 0;
765 int do_equal = FALSE;
766 int needed;
767 int available;
768 int oldwin_height = 0;
769 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200770 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200772 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200773 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 if (flags & WSP_TOP)
776 oldwin = firstwin;
777 else if (flags & WSP_BOT)
778 oldwin = lastwin;
779 else
780 oldwin = curwin;
781
782 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100783 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100785 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
787 EMSG(_(e_noroom));
788 return FAIL;
789 }
790 need_status = STATUS_HEIGHT;
791 }
792
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000793#ifdef FEAT_GUI
794 /* May be needed for the scrollbars that are going to change. */
795 if (gui.in_use)
796 out_flush();
797#endif
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (flags & WSP_VERT)
800 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200801 int wmw1;
802 int minwidth;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
806 /*
807 * Check if we are able to split the current window and compute its
808 * width.
809 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200810 /* Current window requires at least 1 space. */
811 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
812 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200814 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200815 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200817 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200819 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200821 else if (p_ea)
822 {
823 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
824 prevfrp = oldwin->w_frame;
825 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
826 frp = frp->fr_parent)
827 {
828 if (frp->fr_layout == FR_ROW)
829 for (frp2 = frp->fr_child; frp2 != NULL;
830 frp2 = frp2->fr_next)
831 if (frp2 != prevfrp)
832 minwidth += frame_minwidth(frp2, NOWIN);
833 prevfrp = frp;
834 }
835 available = topframe->fr_width;
836 needed += minwidth;
837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200839 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200840 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
841 available = oldwin->w_frame->fr_width;
842 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200843 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100844 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
846 EMSG(_(e_noroom));
847 return FAIL;
848 }
849 if (new_size == 0)
850 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200851 if (new_size > available - minwidth - 1)
852 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200853 if (new_size < wmw1)
854 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 /* if it doesn't fit in the current window, need win_equal() */
857 if (oldwin->w_width - new_size - 1 < p_wmw)
858 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000859
860 /* We don't like to take lines for the new window from a
861 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100862 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000863 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100864 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000865
866 /* Only make all windows the same width if one of them (except oldwin)
867 * is wider than one of the split windows. */
868 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
869 && oldwin->w_frame->fr_parent != NULL)
870 {
871 frp = oldwin->w_frame->fr_parent->fr_child;
872 while (frp != NULL)
873 {
874 if (frp->fr_win != oldwin && frp->fr_win != NULL
875 && (frp->fr_win->w_width > new_size
876 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100877 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000878 {
879 do_equal = TRUE;
880 break;
881 }
882 frp = frp->fr_next;
883 }
884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {
888 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 /*
891 * Check if we are able to split the current window and compute its
892 * height.
893 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200894 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100895 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200896 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200898 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200899 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200901 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200903 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200905 else if (p_ea)
906 {
907 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
908 prevfrp = oldwin->w_frame;
909 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
910 frp = frp->fr_parent)
911 {
912 if (frp->fr_layout == FR_COL)
913 for (frp2 = frp->fr_child; frp2 != NULL;
914 frp2 = frp2->fr_next)
915 if (frp2 != prevfrp)
916 minheight += frame_minheight(frp2, NOWIN);
917 prevfrp = frp;
918 }
919 available = topframe->fr_height;
920 needed += minheight;
921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else
923 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200924 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
925 available = oldwin->w_frame->fr_height;
926 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100928 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930 EMSG(_(e_noroom));
931 return FAIL;
932 }
933 oldwin_height = oldwin->w_height;
934 if (need_status)
935 {
936 oldwin->w_status_height = STATUS_HEIGHT;
937 oldwin_height -= STATUS_HEIGHT;
938 }
939 if (new_size == 0)
940 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200941 if (new_size > available - minheight - STATUS_HEIGHT)
942 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200943 if (new_size < wmh1)
944 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 /* if it doesn't fit in the current window, need win_equal() */
947 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
948 do_equal = TRUE;
949
950 /* We don't like to take lines for the new window from a
951 * 'winfixheight' window. Take them from a window above or below
952 * instead, if possible. */
953 if (oldwin->w_p_wfh)
954 {
955 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
956 oldwin);
957 oldwin_height = oldwin->w_height;
958 if (need_status)
959 oldwin_height -= STATUS_HEIGHT;
960 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000961
962 /* Only make all windows the same height if one of them (except oldwin)
963 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100964 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000965 && oldwin->w_frame->fr_parent != NULL)
966 {
967 frp = oldwin->w_frame->fr_parent->fr_child;
968 while (frp != NULL)
969 {
970 if (frp->fr_win != oldwin && frp->fr_win != NULL
971 && (frp->fr_win->w_height > new_size
972 || frp->fr_win->w_height > oldwin_height - new_size
973 - STATUS_HEIGHT))
974 {
975 do_equal = TRUE;
976 break;
977 }
978 frp = frp->fr_next;
979 }
980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982
983 /*
984 * allocate new window structure and link it in the window list
985 */
986 if ((flags & WSP_TOP) == 0
987 && ((flags & WSP_BOT)
988 || (flags & WSP_BELOW)
989 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100990 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
992 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100993 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000994 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 else
996 win_append(oldwin, wp);
997 }
998 else
999 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001000 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001001 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 else
1003 win_append(oldwin->w_prev, wp);
1004 }
1005
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001006 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
1008 if (wp == NULL)
1009 return FAIL;
1010
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001011 new_frame(wp);
1012 if (wp->w_frame == NULL)
1013 {
1014 win_free(wp, NULL);
1015 return FAIL;
1016 }
1017
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001018 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001019 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
1021
1022 /*
1023 * Reorganise the tree of frames to insert the new window.
1024 */
1025 if (flags & (WSP_TOP | WSP_BOT))
1026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1028 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 curfrp = topframe->fr_child;
1031 if (flags & WSP_BOT)
1032 while (curfrp->fr_next != NULL)
1033 curfrp = curfrp->fr_next;
1034 }
1035 else
1036 curfrp = topframe;
1037 before = (flags & WSP_TOP);
1038 }
1039 else
1040 {
1041 curfrp = oldwin->w_frame;
1042 if (flags & WSP_BELOW)
1043 before = FALSE;
1044 else if (flags & WSP_ABOVE)
1045 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001046 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 before = !p_spr;
1048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 before = !p_sb;
1050 }
1051 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1052 {
1053 /* Need to create a new frame in the tree to make a branch. */
1054 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1055 *frp = *curfrp;
1056 curfrp->fr_layout = layout;
1057 frp->fr_parent = curfrp;
1058 frp->fr_next = NULL;
1059 frp->fr_prev = NULL;
1060 curfrp->fr_child = frp;
1061 curfrp->fr_win = NULL;
1062 curfrp = frp;
1063 if (frp->fr_win != NULL)
1064 oldwin->w_frame = frp;
1065 else
1066 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1067 frp->fr_parent = curfrp;
1068 }
1069
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001070 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001071 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001073 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 frp->fr_parent = curfrp->fr_parent;
1075
1076 /* Insert the new frame at the right place in the frame list. */
1077 if (before)
1078 frame_insert(curfrp, frp);
1079 else
1080 frame_append(curfrp, frp);
1081
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001082 /* Set w_fraction now so that the cursor keeps the same relative
1083 * vertical position. */
Bram Moolenaar3679c172017-11-22 22:22:11 +01001084 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001085 wp->w_fraction = oldwin->w_fraction;
1086
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 if (flags & WSP_VERT)
1088 {
1089 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (need_status)
1092 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001093 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 oldwin->w_status_height = need_status;
1095 }
1096 if (flags & (WSP_TOP | WSP_BOT))
1097 {
1098 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001099 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001100 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001101 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 wp->w_status_height = (p_ls > 0);
1103 }
1104 else
1105 {
1106 /* height and row of new window is same as current window */
1107 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001108 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 wp->w_status_height = oldwin->w_status_height;
1110 }
1111 frp->fr_height = curfrp->fr_height;
1112
1113 /* "new_size" of the current window goes to the new window, use
1114 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001115 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (before)
1117 wp->w_vsep_width = 1;
1118 else
1119 {
1120 wp->w_vsep_width = oldwin->w_vsep_width;
1121 oldwin->w_vsep_width = 1;
1122 }
1123 if (flags & (WSP_TOP | WSP_BOT))
1124 {
1125 if (flags & WSP_BOT)
1126 frame_add_vsep(curfrp);
1127 /* Set width of neighbor frame */
1128 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001129 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1130 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001133 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (before) /* new window left of current one */
1135 {
1136 wp->w_wincol = oldwin->w_wincol;
1137 oldwin->w_wincol += new_size + 1;
1138 }
1139 else /* new window right of current one */
1140 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1141 frame_fix_width(oldwin);
1142 frame_fix_width(wp);
1143 }
1144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 if (flags & (WSP_TOP | WSP_BOT))
1148 {
1149 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001150 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 wp->w_vsep_width = 0;
1152 }
1153 else
1154 {
1155 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001156 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 wp->w_vsep_width = oldwin->w_vsep_width;
1158 }
1159 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 /* "new_size" of the current window goes to the new window, use
1162 * one row for the status line */
1163 win_new_height(wp, new_size);
1164 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001165 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001166 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001167 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001168
1169 if (!((flags & WSP_BOT) && p_ls == 0))
1170 new_fr_height -= STATUS_HEIGHT;
1171 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 else
1174 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1175 if (before) /* new window above current one */
1176 {
1177 wp->w_winrow = oldwin->w_winrow;
1178 wp->w_status_height = STATUS_HEIGHT;
1179 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1180 }
1181 else /* new window below current one */
1182 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001183 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1184 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001186 if (!(flags & WSP_BOT))
1187 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (flags & WSP_BOT)
1190 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 frame_fix_height(wp);
1192 frame_fix_height(oldwin);
1193 }
1194
1195 if (flags & (WSP_TOP | WSP_BOT))
1196 (void)win_comp_pos();
1197
1198 /*
1199 * Both windows need redrawing
1200 */
1201 redraw_win_later(wp, NOT_VALID);
1202 wp->w_redr_status = TRUE;
1203 redraw_win_later(oldwin, NOT_VALID);
1204 oldwin->w_redr_status = TRUE;
1205
1206 if (need_status)
1207 {
1208 msg_row = Rows - 1;
1209 msg_col = sc_col;
1210 msg_clr_eos_force(); /* Old command/ruler may still be there */
1211 comp_col();
1212 msg_row = Rows - 1;
1213 msg_col = 0; /* put position back at start of line */
1214 }
1215
1216 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001217 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 */
1219 if (do_equal || dir != 0)
1220 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001222 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223
1224 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1225 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (flags & WSP_VERT)
1227 {
1228 i = p_wiw;
1229 if (size != 0)
1230 p_wiw = size;
1231
1232# ifdef FEAT_GUI
1233 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1234 if (gui.in_use)
1235 gui_init_which_components(NULL);
1236# endif
1237 }
1238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 {
1240 i = p_wh;
1241 if (size != 0)
1242 p_wh = size;
1243 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001244
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001245#ifdef FEAT_JUMPLIST
1246 /* Keep same changelist position in new window. */
1247 wp->w_changelistidx = oldwin->w_changelistidx;
1248#endif
1249
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001250 /*
1251 * make the new window the current window
1252 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001253 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 if (flags & WSP_VERT)
1255 p_wiw = i;
1256 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 p_wh = i;
1258
1259 return OK;
1260}
1261
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001262
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001263/*
1264 * Initialize window "newp" from window "oldp".
1265 * Used when splitting a window and when creating a new tab page.
1266 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001267 * WSP_NEWLOC may be specified in flags to prevent the location list from
1268 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001269 */
1270 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001271win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001272{
1273 int i;
1274
1275 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001276#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001277 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001278#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001279 oldp->w_buffer->b_nwindows++;
1280 newp->w_cursor = oldp->w_cursor;
1281 newp->w_valid = 0;
1282 newp->w_curswant = oldp->w_curswant;
1283 newp->w_set_curswant = oldp->w_set_curswant;
1284 newp->w_topline = oldp->w_topline;
1285#ifdef FEAT_DIFF
1286 newp->w_topfill = oldp->w_topfill;
1287#endif
1288 newp->w_leftcol = oldp->w_leftcol;
1289 newp->w_pcmark = oldp->w_pcmark;
1290 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1291 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001292 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001293 newp->w_fraction = oldp->w_fraction;
1294 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1295#ifdef FEAT_JUMPLIST
1296 copy_jumplist(oldp, newp);
1297#endif
1298#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001299 if (flags & WSP_NEWLOC)
1300 {
1301 /* Don't copy the location list. */
1302 newp->w_llist = NULL;
1303 newp->w_llist_ref = NULL;
1304 }
1305 else
1306 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001307#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001308 newp->w_localdir = (oldp->w_localdir == NULL)
1309 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001310
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001311 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001312 for (i = 0; i < oldp->w_tagstacklen; i++)
1313 {
1314 newp->w_tagstack[i] = oldp->w_tagstack[i];
1315 if (newp->w_tagstack[i].tagname != NULL)
1316 newp->w_tagstack[i].tagname =
1317 vim_strsave(newp->w_tagstack[i].tagname);
1318 }
1319 newp->w_tagstackidx = oldp->w_tagstackidx;
1320 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001321#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001322 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001323#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001324
1325 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001326
Bram Moolenaara971b822011-09-14 14:43:25 +02001327#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001328 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001329#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001330}
1331
1332/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001333 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001334 * Only the essential things are copied.
1335 */
1336 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001337win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001338{
1339 /* Use the same argument list. */
1340 newp->w_alist = oldp->w_alist;
1341 ++newp->w_alist->al_refcount;
1342 newp->w_arg_idx = oldp->w_arg_idx;
1343
1344 /* copy options from existing window */
1345 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001346}
1347
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001350 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 */
1352 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001353win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354{
1355 win_T *wp;
1356
1357 if (win == NULL)
1358 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001359 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 if (wp == win)
1361 return TRUE;
1362 return FALSE;
1363}
1364
1365/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001366 * Check if "win" is a pointer to an existing window in any tab page.
1367 */
1368 int
1369win_valid_any_tab(win_T *win)
1370{
1371 win_T *wp;
1372 tabpage_T *tp;
1373
1374 if (win == NULL)
1375 return FALSE;
1376 FOR_ALL_TABPAGES(tp)
1377 {
1378 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1379 {
1380 if (wp == win)
1381 return TRUE;
1382 }
1383 }
1384 return FALSE;
1385}
1386
1387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 * Return the number of windows.
1389 */
1390 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001391win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 win_T *wp;
1394 int count = 0;
1395
Bram Moolenaar29323592016-07-24 22:04:11 +02001396 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 ++count;
1398 return count;
1399}
1400
1401/*
1402 * Make "count" windows on the screen.
1403 * Return actual number of windows on the screen.
1404 * Must be called when there is just one window, filling the whole screen
1405 * (excluding the command line).
1406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001408make_windows(
1409 int count,
1410 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411{
1412 int maxcount;
1413 int todo;
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (vertical)
1416 {
1417 /* Each windows needs at least 'winminwidth' lines and a separator
1418 * column. */
1419 maxcount = (curwin->w_width + curwin->w_vsep_width
1420 - (p_wiw - p_wmw)) / (p_wmw + 1);
1421 }
1422 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 {
1424 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001425 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1427 }
1428
1429 if (maxcount < 2)
1430 maxcount = 2;
1431 if (count > maxcount)
1432 count = maxcount;
1433
1434 /*
1435 * add status line now, otherwise first window will be too big
1436 */
1437 if (count > 1)
1438 last_status(TRUE);
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /*
1441 * Don't execute autocommands while creating the windows. Must do that
1442 * when putting the buffers in the windows.
1443 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001444 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 /* todo is number of windows left to create */
1447 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (vertical)
1449 {
1450 if (win_split(curwin->w_width - (curwin->w_width - todo)
1451 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1452 break;
1453 }
1454 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 {
1456 if (win_split(curwin->w_height - (curwin->w_height - todo
1457 * STATUS_HEIGHT) / (todo + 1)
1458 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1459 break;
1460 }
1461
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001462 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
1464 /* return actual number of windows */
1465 return (count - todo);
1466}
1467
1468/*
1469 * Exchange current and next window
1470 */
1471 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001472win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
1474 frame_T *frp;
1475 frame_T *frp2;
1476 win_T *wp;
1477 win_T *wp2;
1478 int temp;
1479
Bram Moolenaar459ca562016-11-10 18:16:33 +01001480 if (ONE_WINDOW) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
1482 beep_flush();
1483 return;
1484 }
1485
1486#ifdef FEAT_GUI
1487 need_mouse_correct = TRUE;
1488#endif
1489
1490 /*
1491 * find window to exchange with
1492 */
1493 if (Prenum)
1494 {
1495 frp = curwin->w_frame->fr_parent->fr_child;
1496 while (frp != NULL && --Prenum > 0)
1497 frp = frp->fr_next;
1498 }
1499 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1500 frp = curwin->w_frame->fr_next;
1501 else /* Swap last window in row/col with previous */
1502 frp = curwin->w_frame->fr_prev;
1503
1504 /* We can only exchange a window with another window, not with a frame
1505 * containing windows. */
1506 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1507 return;
1508 wp = frp->fr_win;
1509
1510/*
1511 * 1. remove curwin from the list. Remember after which window it was in wp2
1512 * 2. insert curwin before wp in the list
1513 * if wp != wp2
1514 * 3. remove wp from the list
1515 * 4. insert wp after wp2
1516 * 5. exchange the status line height and vsep width.
1517 */
1518 wp2 = curwin->w_prev;
1519 frp2 = curwin->w_frame->fr_prev;
1520 if (wp->w_prev != curwin)
1521 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001522 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 frame_remove(curwin->w_frame);
1524 win_append(wp->w_prev, curwin);
1525 frame_insert(frp, curwin->w_frame);
1526 }
1527 if (wp != wp2)
1528 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001529 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 frame_remove(wp->w_frame);
1531 win_append(wp2, wp);
1532 if (frp2 == NULL)
1533 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1534 else
1535 frame_append(frp2, wp->w_frame);
1536 }
1537 temp = curwin->w_status_height;
1538 curwin->w_status_height = wp->w_status_height;
1539 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 temp = curwin->w_vsep_width;
1541 curwin->w_vsep_width = wp->w_vsep_width;
1542 wp->w_vsep_width = temp;
1543
1544 /* If the windows are not in the same frame, exchange the sizes to avoid
1545 * messing up the window layout. Otherwise fix the frame sizes. */
1546 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1547 {
1548 temp = curwin->w_height;
1549 curwin->w_height = wp->w_height;
1550 wp->w_height = temp;
1551 temp = curwin->w_width;
1552 curwin->w_width = wp->w_width;
1553 wp->w_width = temp;
1554 }
1555 else
1556 {
1557 frame_fix_height(curwin);
1558 frame_fix_height(wp);
1559 frame_fix_width(curwin);
1560 frame_fix_width(wp);
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
1563 (void)win_comp_pos(); /* recompute window positions */
1564
1565 win_enter(wp, TRUE);
1566 redraw_later(CLEAR);
1567}
1568
1569/*
1570 * rotate windows: if upwards TRUE the second window becomes the first one
1571 * if upwards FALSE the first window becomes the second one
1572 */
1573 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001574win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
1576 win_T *wp1;
1577 win_T *wp2;
1578 frame_T *frp;
1579 int n;
1580
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001581 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 {
1583 beep_flush();
1584 return;
1585 }
1586
1587#ifdef FEAT_GUI
1588 need_mouse_correct = TRUE;
1589#endif
1590
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 /* Check if all frames in this row/col have one window. */
1592 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1593 frp = frp->fr_next)
1594 if (frp->fr_win == NULL)
1595 {
1596 EMSG(_("E443: Cannot rotate when another window is split"));
1597 return;
1598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 while (count--)
1601 {
1602 if (upwards) /* first window becomes last window */
1603 {
1604 /* remove first window/frame from the list */
1605 frp = curwin->w_frame->fr_parent->fr_child;
1606 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001607 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 frame_remove(frp);
1609
1610 /* find last frame and append removed window/frame after it */
1611 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1612 ;
1613 win_append(frp->fr_win, wp1);
1614 frame_append(frp, wp1->w_frame);
1615
1616 wp2 = frp->fr_win; /* previously last window */
1617 }
1618 else /* last window becomes first window */
1619 {
1620 /* find last window/frame in the list and remove it */
1621 for (frp = curwin->w_frame; frp->fr_next != NULL;
1622 frp = frp->fr_next)
1623 ;
1624 wp1 = frp->fr_win;
1625 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001626 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 frame_remove(frp);
1628
1629 /* append the removed window/frame before the first in the list */
1630 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1631 frame_insert(frp->fr_parent->fr_child, frp);
1632 }
1633
1634 /* exchange status height and vsep width of old and new last window */
1635 n = wp2->w_status_height;
1636 wp2->w_status_height = wp1->w_status_height;
1637 wp1->w_status_height = n;
1638 frame_fix_height(wp1);
1639 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 n = wp2->w_vsep_width;
1641 wp2->w_vsep_width = wp1->w_vsep_width;
1642 wp1->w_vsep_width = n;
1643 frame_fix_width(wp1);
1644 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001646 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 (void)win_comp_pos();
1648 }
1649
1650 redraw_later(CLEAR);
1651}
1652
1653/*
1654 * Move the current window to the very top/bottom/left/right of the screen.
1655 */
1656 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001657win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
1659 int dir;
1660 int height = curwin->w_height;
1661
Bram Moolenaar459ca562016-11-10 18:16:33 +01001662 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 {
1664 beep_flush();
1665 return;
1666 }
1667
1668 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001669 (void)winframe_remove(curwin, &dir, NULL);
1670 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 last_status(FALSE); /* may need to remove last status line */
1672 (void)win_comp_pos(); /* recompute window positions */
1673
1674 /* Split a window on the desired side and put the window there. */
1675 (void)win_split_ins(size, flags, curwin, dir);
1676 if (!(flags & WSP_VERT))
1677 {
1678 win_setheight(height);
1679 if (p_ea)
1680 win_equal(curwin, TRUE, 'v');
1681 }
1682
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001683#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1685 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001686 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688}
1689
1690/*
1691 * Move window "win1" to below/right of "win2" and make "win1" the current
1692 * window. Only works within the same frame!
1693 */
1694 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001695win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696{
1697 int height;
1698
1699 /* check if the arguments are reasonable */
1700 if (win1 == win2)
1701 return;
1702
1703 /* check if there is something to do */
1704 if (win2->w_next != win1)
1705 {
1706 /* may need move the status line/vertical separator of the last window
1707 * */
1708 if (win1 == lastwin)
1709 {
1710 height = win1->w_prev->w_status_height;
1711 win1->w_prev->w_status_height = win1->w_status_height;
1712 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001713 if (win1->w_prev->w_vsep_width == 1)
1714 {
1715 /* Remove the vertical separator from the last-but-one window,
1716 * add it to the last window. Adjust the frame widths. */
1717 win1->w_prev->w_vsep_width = 0;
1718 win1->w_prev->w_frame->fr_width -= 1;
1719 win1->w_vsep_width = 1;
1720 win1->w_frame->fr_width += 1;
1721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
1723 else if (win2 == lastwin)
1724 {
1725 height = win1->w_status_height;
1726 win1->w_status_height = win2->w_status_height;
1727 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001728 if (win1->w_vsep_width == 1)
1729 {
1730 /* Remove the vertical separator from win1, add it to the last
1731 * window, win2. Adjust the frame widths. */
1732 win2->w_vsep_width = 1;
1733 win2->w_frame->fr_width += 1;
1734 win1->w_vsep_width = 0;
1735 win1->w_frame->fr_width -= 1;
1736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001738 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 frame_remove(win1->w_frame);
1740 win_append(win2, win1);
1741 frame_append(win2->w_frame, win1->w_frame);
1742
1743 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1744 redraw_later(NOT_VALID);
1745 }
1746 win_enter(win1, FALSE);
1747}
1748
1749/*
1750 * Make all windows the same height.
1751 * 'next_curwin' will soon be the current window, make sure it has enough
1752 * rows.
1753 */
1754 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001755win_equal(
1756 win_T *next_curwin, /* pointer to current window to be or NULL */
1757 int current, /* do only frame with current window */
1758 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 'b' for both, 0 for using p_ead */
1760{
1761 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001764 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001765 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766}
1767
1768/*
1769 * Set a frame to a new position and height, spreading the available room
1770 * equally over contained frames.
1771 * The window "next_curwin" (if not NULL) should at least get the size from
1772 * 'winheight' and 'winwidth' if possible.
1773 */
1774 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001775win_equal_rec(
1776 win_T *next_curwin, /* pointer to current window to be or NULL */
1777 int current, /* do only frame with current window */
1778 frame_T *topfr, /* frame to set size off */
1779 int dir, /* 'v', 'h' or 'b', see win_equal() */
1780 int col, /* horizontal position for frame */
1781 int row, /* vertical position for frame */
1782 int width, /* new width of frame */
1783 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
1785 int n, m;
1786 int extra_sep = 0;
1787 int wincount, totwincount = 0;
1788 frame_T *fr;
1789 int next_curwin_size = 0;
1790 int room = 0;
1791 int new_size;
1792 int has_next_curwin = 0;
1793 int hnc;
1794
1795 if (topfr->fr_layout == FR_LEAF)
1796 {
1797 /* Set the width/height of this frame.
1798 * Redraw when size or position changes */
1799 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 )
1802 {
1803 topfr->fr_win->w_winrow = row;
1804 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001806 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 redraw_all_later(CLEAR);
1808 }
1809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 else if (topfr->fr_layout == FR_ROW)
1811 {
1812 topfr->fr_width = width;
1813 topfr->fr_height = height;
1814
1815 if (dir != 'v') /* equalize frame widths */
1816 {
1817 /* Compute the maximum number of windows horizontally in this
1818 * frame. */
1819 n = frame_minwidth(topfr, NOWIN);
1820 /* add one for the rightmost window, it doesn't have a separator */
1821 if (col + width == Columns)
1822 extra_sep = 1;
1823 else
1824 extra_sep = 0;
1825 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001826 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001828 /*
1829 * Compute width for "next_curwin" window and room available for
1830 * other windows.
1831 * "m" is the minimal width when counting p_wiw for "next_curwin".
1832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 m = frame_minwidth(topfr, next_curwin);
1834 room = width - m;
1835 if (room < 0)
1836 {
1837 next_curwin_size = p_wiw + room;
1838 room = 0;
1839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 else
1841 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001842 next_curwin_size = -1;
1843 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1844 {
1845 /* If 'winfixwidth' set keep the window width if
1846 * possible.
1847 * Watch out for this window being the next_curwin. */
1848 if (frame_fixed_width(fr))
1849 {
1850 n = frame_minwidth(fr, NOWIN);
1851 new_size = fr->fr_width;
1852 if (frame_has_win(fr, next_curwin))
1853 {
1854 room += p_wiw - p_wmw;
1855 next_curwin_size = 0;
1856 if (new_size < p_wiw)
1857 new_size = p_wiw;
1858 }
1859 else
1860 /* These windows don't use up room. */
1861 totwincount -= (n + (fr->fr_next == NULL
1862 ? extra_sep : 0)) / (p_wmw + 1);
1863 room -= new_size - n;
1864 if (room < 0)
1865 {
1866 new_size += room;
1867 room = 0;
1868 }
1869 fr->fr_newwidth = new_size;
1870 }
1871 }
1872 if (next_curwin_size == -1)
1873 {
1874 if (!has_next_curwin)
1875 next_curwin_size = 0;
1876 else if (totwincount > 1
1877 && (room + (totwincount - 2))
1878 / (totwincount - 1) > p_wiw)
1879 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001880 /* Can make all windows wider than 'winwidth', spread
1881 * the room equally. */
1882 next_curwin_size = (room + p_wiw
1883 + (totwincount - 1) * p_wmw
1884 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001885 room -= next_curwin_size - p_wiw;
1886 }
1887 else
1888 next_curwin_size = p_wiw;
1889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001891
1892 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 --totwincount; /* don't count curwin */
1894 }
1895
1896 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 wincount = 1;
1899 if (fr->fr_next == NULL)
1900 /* last frame gets all that remains (avoid roundoff error) */
1901 new_size = width;
1902 else if (dir == 'v')
1903 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001904 else if (frame_fixed_width(fr))
1905 {
1906 new_size = fr->fr_newwidth;
1907 wincount = 0; /* doesn't count as a sizeable window */
1908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 else
1910 {
1911 /* Compute the maximum number of windows horiz. in "fr". */
1912 n = frame_minwidth(fr, NOWIN);
1913 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1914 / (p_wmw + 1);
1915 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001916 if (has_next_curwin)
1917 hnc = frame_has_win(fr, next_curwin);
1918 else
1919 hnc = FALSE;
1920 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001922 if (totwincount == 0)
1923 new_size = room;
1924 else
1925 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001927 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 {
1929 next_curwin_size -= p_wiw - (m - n);
1930 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001931 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001933 else
1934 room -= new_size;
1935 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001938 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 * window, unless equalizing all frames. */
1940 if (!current || dir != 'v' || topfr->fr_parent != NULL
1941 || (new_size != fr->fr_width)
1942 || frame_has_win(fr, next_curwin))
1943 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001944 new_size, height);
1945 col += new_size;
1946 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 totwincount -= wincount;
1948 }
1949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 else /* topfr->fr_layout == FR_COL */
1951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 topfr->fr_height = height;
1954
1955 if (dir != 'h') /* equalize frame heights */
1956 {
1957 /* Compute maximum number of windows vertically in this frame. */
1958 n = frame_minheight(topfr, NOWIN);
1959 /* add one for the bottom window if it doesn't have a statusline */
1960 if (row + height == cmdline_row && p_ls == 0)
1961 extra_sep = 1;
1962 else
1963 extra_sep = 0;
1964 totwincount = (n + extra_sep) / (p_wmh + 1);
1965 has_next_curwin = frame_has_win(topfr, next_curwin);
1966
1967 /*
1968 * Compute height for "next_curwin" window and room available for
1969 * other windows.
1970 * "m" is the minimal height when counting p_wh for "next_curwin".
1971 */
1972 m = frame_minheight(topfr, next_curwin);
1973 room = height - m;
1974 if (room < 0)
1975 {
1976 /* The room is less then 'winheight', use all space for the
1977 * current window. */
1978 next_curwin_size = p_wh + room;
1979 room = 0;
1980 }
1981 else
1982 {
1983 next_curwin_size = -1;
1984 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1985 {
1986 /* If 'winfixheight' set keep the window height if
1987 * possible.
1988 * Watch out for this window being the next_curwin. */
1989 if (frame_fixed_height(fr))
1990 {
1991 n = frame_minheight(fr, NOWIN);
1992 new_size = fr->fr_height;
1993 if (frame_has_win(fr, next_curwin))
1994 {
1995 room += p_wh - p_wmh;
1996 next_curwin_size = 0;
1997 if (new_size < p_wh)
1998 new_size = p_wh;
1999 }
2000 else
2001 /* These windows don't use up room. */
2002 totwincount -= (n + (fr->fr_next == NULL
2003 ? extra_sep : 0)) / (p_wmh + 1);
2004 room -= new_size - n;
2005 if (room < 0)
2006 {
2007 new_size += room;
2008 room = 0;
2009 }
2010 fr->fr_newheight = new_size;
2011 }
2012 }
2013 if (next_curwin_size == -1)
2014 {
2015 if (!has_next_curwin)
2016 next_curwin_size = 0;
2017 else if (totwincount > 1
2018 && (room + (totwincount - 2))
2019 / (totwincount - 1) > p_wh)
2020 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002021 /* can make all windows higher than 'winheight',
2022 * spread the room equally. */
2023 next_curwin_size = (room + p_wh
2024 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 + (totwincount - 1)) / totwincount;
2026 room -= next_curwin_size - p_wh;
2027 }
2028 else
2029 next_curwin_size = p_wh;
2030 }
2031 }
2032
2033 if (has_next_curwin)
2034 --totwincount; /* don't count curwin */
2035 }
2036
2037 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 wincount = 1;
2040 if (fr->fr_next == NULL)
2041 /* last frame gets all that remains (avoid roundoff error) */
2042 new_size = height;
2043 else if (dir == 'h')
2044 new_size = fr->fr_height;
2045 else if (frame_fixed_height(fr))
2046 {
2047 new_size = fr->fr_newheight;
2048 wincount = 0; /* doesn't count as a sizeable window */
2049 }
2050 else
2051 {
2052 /* Compute the maximum number of windows vert. in "fr". */
2053 n = frame_minheight(fr, NOWIN);
2054 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2055 / (p_wmh + 1);
2056 m = frame_minheight(fr, next_curwin);
2057 if (has_next_curwin)
2058 hnc = frame_has_win(fr, next_curwin);
2059 else
2060 hnc = FALSE;
2061 if (hnc) /* don't count next_curwin */
2062 --wincount;
2063 if (totwincount == 0)
2064 new_size = room;
2065 else
2066 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2067 / totwincount;
2068 if (hnc) /* add next_curwin size */
2069 {
2070 next_curwin_size -= p_wh - (m - n);
2071 new_size += next_curwin_size;
2072 room -= new_size - next_curwin_size;
2073 }
2074 else
2075 room -= new_size;
2076 new_size += n;
2077 }
2078 /* Skip frame that is full width when splitting or closing a
2079 * window, unless equalizing all frames. */
2080 if (!current || dir != 'h' || topfr->fr_parent != NULL
2081 || (new_size != fr->fr_height)
2082 || frame_has_win(fr, next_curwin))
2083 win_equal_rec(next_curwin, current, fr, dir, col, row,
2084 width, new_size);
2085 row += new_size;
2086 height -= new_size;
2087 totwincount -= wincount;
2088 }
2089 }
2090}
2091
2092/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002093 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 */
2095 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002096close_windows(
2097 buf_T *buf,
2098 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002100 win_T *wp;
2101 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002102 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002103 int count = tabpage_index(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002106
Bram Moolenaar459ca562016-11-10 18:16:33 +01002107 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002109 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002110 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002112 if (win_close(wp, FALSE) == FAIL)
2113 /* If closing the window fails give up, to avoid looping
2114 * forever. */
2115 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002116
2117 /* Start all over, autocommands may change the window layout. */
2118 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
2120 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002121 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002123
2124 /* Also check windows in other tab pages. */
2125 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2126 {
2127 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002128 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002129 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002130 if (wp->w_buffer == buf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002131 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaarf740b292006-02-16 22:11:02 +00002132 {
2133 win_close_othertab(wp, FALSE, tp);
2134
2135 /* Start all over, the tab page may be closed and
2136 * autocommands may change the window layout. */
2137 nexttp = first_tabpage;
2138 break;
2139 }
2140 }
2141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002143
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002144 if (count != tabpage_index(NULL))
2145 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002146
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002147 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002148 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002149 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150}
2151
2152/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002153 * Return TRUE if the current window is the only window that exists (ignoring
2154 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002155 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002156 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002157 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002158last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002159{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002160 return (one_window() && first_tabpage->tp_next == NULL);
2161}
2162
2163/*
2164 * Return TRUE if there is only one window other than "aucmd_win" in the
2165 * current tab page.
2166 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002167 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002168one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002169{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002170 win_T *wp;
2171 int seen_one = FALSE;
2172
2173 FOR_ALL_WINDOWS(wp)
2174 {
2175 if (wp != aucmd_win)
2176 {
2177 if (seen_one)
2178 return FALSE;
2179 seen_one = TRUE;
2180 }
2181 }
2182 return TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002183}
2184
2185/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002186 * Close the possibly last window in a tab page.
2187 * Returns TRUE when the window was closed already.
2188 */
2189 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002190close_last_window_tabpage(
2191 win_T *win,
2192 int free_buf,
2193 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002194{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002195 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002196 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002197 buf_T *old_curbuf = curbuf;
2198
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002199 /*
2200 * Closing the last window in a tab page. First go to another tab
2201 * page and then close the window and the tab page. This avoids that
2202 * curwin and curtab are invalid while we are freeing memory, they may
2203 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002204 * Don't trigger autocommands yet, they may use wrong values, so do
2205 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002206 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002207 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002208 redraw_tabline = TRUE;
2209
2210 /* Safety check: Autocommands may have closed the window when jumping
2211 * to the other tab page. */
2212 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2213 {
2214 int h = tabline_height();
2215
2216 win_close_othertab(win, free_buf, prev_curtab);
2217 if (h != tabline_height())
2218 shell_new_rows();
2219 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002220 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2221 * that now. */
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002222 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002223 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002224 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2225 if (old_curbuf != curbuf)
2226 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002227 return TRUE;
2228 }
2229 return FALSE;
2230}
2231
2232/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 * If "free_buf" is TRUE related buffer may be unloaded.
2235 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002236 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002237 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002239 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002240win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 int dir;
2246 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002247 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002248 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002250 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
2252 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002253 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002256 if (win->w_closing || (win->w_buffer != NULL
2257 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002258 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002259 if (win == aucmd_win)
2260 {
2261 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002262 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002263 }
2264 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2265 {
2266 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002267 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002268 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002269
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002270 /* When closing the last window in a tab page first go to another tab page
2271 * and then close the window and the tab page to avoid that curwin and
2272 * curtab are invalid while we are freeing memory. */
2273 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002274 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /* When closing the help window, try restoring a snapshot after closing
2277 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002278 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 help_window = TRUE;
2280 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002281 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 if (win == curwin)
2284 {
2285 /*
2286 * Guess which window is going to be the new current window.
2287 * This may change because of the autocommands (sigh).
2288 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002289 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002292 * Be careful: If autocommands delete the window or cause this window
2293 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 */
2295 if (wp->w_buffer != curbuf)
2296 {
2297 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002298 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002300 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002301 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002302 win->w_closing = FALSE;
2303 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002306 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002308 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002309 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002310 win->w_closing = FALSE;
2311 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002312 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002313#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 /* autocmds may abort script processing */
2315 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002320#ifdef FEAT_GUI
2321 /* Avoid trouble with scrollbars that are going to be deleted in
2322 * win_free(). */
2323 if (gui.in_use)
2324 out_flush();
2325#endif
2326
Bram Moolenaara971b822011-09-14 14:43:25 +02002327#ifdef FEAT_SYN_HL
2328 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002329 if (win->w_buffer != NULL)
2330 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002331#endif
2332
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 /*
2334 * Close the link to the buffer.
2335 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002336 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002337 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002338 bufref_T bufref;
2339
2340 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002341 win->w_closing = TRUE;
Bram Moolenaar8f913992012-08-29 15:50:26 +02002342 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002343 if (win_valid_any_tab(win))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002344 win->w_closing = FALSE;
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002345 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2346 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002347 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002348 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002349 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002350
Bram Moolenaar802418d2013-01-17 14:00:11 +01002351 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2352 && (last_window() || curtab != prev_curtab
2353 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002354 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002355 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002356 * curwin->w_buffer, otherwise writing viminfo may fail. */
2357 if (curwin->w_buffer == NULL)
2358 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002359 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002360 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002361
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002362 /* Autocommands may have moved to another tab page. */
2363 if (curtab != prev_curtab && win_valid_any_tab(win)
2364 && win->w_buffer == NULL)
2365 {
2366 /* Need to close the window anyway, since the buffer is NULL. */
2367 win_close_othertab(win, FALSE, prev_curtab);
2368 return FAIL;
2369 }
2370
2371 /* Autocommands may have closed the window already or closed the only
2372 * other window. */
2373 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002374 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002375 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
Bram Moolenaara971b822011-09-14 14:43:25 +02002377 /* Free the memory used for the window and get the window that received
2378 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002379 wp = win_free_mem(win, &dir, NULL);
2380
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 /* Make sure curwin isn't invalid. It can cause severe trouble when
2382 * printing an error message. For win_equal() curbuf needs to be valid
2383 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002384 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
2386 curwin = wp;
2387#ifdef FEAT_QUICKFIX
2388 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2389 {
2390 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002391 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 * finding another window to go to.
2393 */
2394 for (;;)
2395 {
2396 if (wp->w_next == NULL)
2397 wp = firstwin;
2398 else
2399 wp = wp->w_next;
2400 if (wp == curwin)
2401 break;
2402 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2403 {
2404 curwin = wp;
2405 break;
2406 }
2407 }
2408 }
2409#endif
2410 curbuf = curwin->w_buffer;
2411 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002412
2413 /* The cursor position may be invalid if the buffer changed after last
2414 * using the window. */
2415 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002417 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002418 /* If the frame of the closed window contains the new current window,
2419 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002420 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 else
2422 win_comp_pos();
2423 if (close_curwin)
2424 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002425 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (other_buffer)
2427 /* careful: after this wp and win may be invalid! */
2428 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 }
2430
2431 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002432 * If last window has a status line now and we don't want one,
2433 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 */
2435 last_status(FALSE);
2436
2437 /* After closing the help window, try restoring the window layout from
2438 * before it was opened. */
2439 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002440 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002442#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2444 if (gui.in_use && !win_hasvertsplit())
2445 gui_init_which_components(NULL);
2446#endif
2447
2448 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002449 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450}
2451
2452/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002453 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002454 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002455 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002456 * Caller must check if buffer is hidden and whether the tabline needs to be
2457 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002458 */
2459 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002460win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002461{
2462 win_T *wp;
2463 int dir;
2464 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002465 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002466
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002467 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2468 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002469 if (win->w_closing || (win->w_buffer != NULL
2470 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002471 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002472
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002473 if (win->w_buffer != NULL)
2474 /* Close the link to the buffer. */
2475 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002476
2477 /* Careful: Autocommands may have closed the tab page or made it the
2478 * current tab page. */
2479 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2480 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002481 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002482 return;
2483
2484 /* Autocommands may have closed the window already. */
2485 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2486 ;
2487 if (wp == NULL)
2488 return;
2489
Bram Moolenaarf740b292006-02-16 22:11:02 +00002490 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002491 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002492 {
2493 if (tp == first_tabpage)
2494 first_tabpage = tp->tp_next;
2495 else
2496 {
2497 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2498 ptp = ptp->tp_next)
2499 ;
2500 if (ptp == NULL)
2501 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002502 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002503 return;
2504 }
2505 ptp->tp_next = tp->tp_next;
2506 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002507 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002508 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002509
2510 /* Free the memory used for the window. */
2511 win_free_mem(win, &dir, tp);
2512
2513 if (free_tp)
2514 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002515}
2516
2517/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002518 * Free the memory used for a window.
2519 * Returns a pointer to the window that got the freed up space.
2520 */
2521 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002522win_free_mem(
2523 win_T *win,
2524 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2525 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002526{
2527 frame_T *frp;
2528 win_T *wp;
2529
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002530 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002531 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002532 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002533 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002534 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002535
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002536 /* When deleting the current window of another tab page select a new
2537 * current window. */
2538 if (tp != NULL && win == tp->tp_curwin)
2539 tp->tp_curwin = wp;
2540
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002541 return wp;
2542}
2543
2544#if defined(EXITFREE) || defined(PROTO)
2545 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002546win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002547{
2548 int dummy;
2549
Bram Moolenaarf740b292006-02-16 22:11:02 +00002550 while (first_tabpage->tp_next != NULL)
2551 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002552
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002553 if (aucmd_win != NULL)
2554 {
2555 (void)win_free_mem(aucmd_win, &dummy, NULL);
2556 aucmd_win = NULL;
2557 }
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002558
2559 while (firstwin != NULL)
2560 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002561
2562 /* No window should be used after this. Set curwin to NULL to crash
2563 * instead of using freed memory. */
2564 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002565}
2566#endif
2567
2568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 * Remove a window and its frame from the tree of frames.
2570 * Returns a pointer to the window that got the freed up space.
2571 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002572 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002573winframe_remove(
2574 win_T *win,
2575 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2576 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577{
2578 frame_T *frp, *frp2, *frp3;
2579 frame_T *frp_close = win->w_frame;
2580 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581
2582 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002583 * If there is only one window there is nothing to remove.
2584 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002585 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002586 return NULL;
2587
2588 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 * Remove the window from its frame.
2590 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002591 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 wp = frame2win(frp2);
2593
2594 /* Remove this frame from the list of frames. */
2595 frame_remove(frp_close);
2596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (frp_close->fr_parent->fr_layout == FR_COL)
2598 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002599 /* When 'winfixheight' is set, try to find another frame in the column
2600 * (as close to the closed frame as possible) to distribute the height
2601 * to. */
2602 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2603 {
2604 frp = frp_close->fr_prev;
2605 frp3 = frp_close->fr_next;
2606 while (frp != NULL || frp3 != NULL)
2607 {
2608 if (frp != NULL)
2609 {
2610 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2611 {
2612 frp2 = frp;
2613 wp = frp->fr_win;
2614 break;
2615 }
2616 frp = frp->fr_prev;
2617 }
2618 if (frp3 != NULL)
2619 {
2620 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2621 {
2622 frp2 = frp3;
2623 wp = frp3->fr_win;
2624 break;
2625 }
2626 frp3 = frp3->fr_next;
2627 }
2628 }
2629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2631 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 *dirp = 'v';
2633 }
2634 else
2635 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002636 /* When 'winfixwidth' is set, try to find another frame in the column
2637 * (as close to the closed frame as possible) to distribute the width
2638 * to. */
2639 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2640 {
2641 frp = frp_close->fr_prev;
2642 frp3 = frp_close->fr_next;
2643 while (frp != NULL || frp3 != NULL)
2644 {
2645 if (frp != NULL)
2646 {
2647 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2648 {
2649 frp2 = frp;
2650 wp = frp->fr_win;
2651 break;
2652 }
2653 frp = frp->fr_prev;
2654 }
2655 if (frp3 != NULL)
2656 {
2657 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2658 {
2659 frp2 = frp3;
2660 wp = frp3->fr_win;
2661 break;
2662 }
2663 frp3 = frp3->fr_next;
2664 }
2665 }
2666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002668 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 *dirp = 'h';
2670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 /* If rows/columns go to a window below/right its positions need to be
2673 * updated. Can only be done after the sizes have been updated. */
2674 if (frp2 == frp_close->fr_next)
2675 {
2676 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002677 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 frame_comp_pos(frp2, &row, &col);
2680 }
2681
2682 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2683 {
2684 /* There is no other frame in this list, move its info to the parent
2685 * and remove it. */
2686 frp2->fr_parent->fr_layout = frp2->fr_layout;
2687 frp2->fr_parent->fr_child = frp2->fr_child;
2688 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2689 frp->fr_parent = frp2->fr_parent;
2690 frp2->fr_parent->fr_win = frp2->fr_win;
2691 if (frp2->fr_win != NULL)
2692 frp2->fr_win->w_frame = frp2->fr_parent;
2693 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002694 if (topframe->fr_child == frp2)
2695 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 vim_free(frp2);
2697
2698 frp2 = frp->fr_parent;
2699 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2700 {
2701 /* The frame above the parent has the same layout, have to merge
2702 * the frames into this list. */
2703 if (frp2->fr_child == frp)
2704 frp2->fr_child = frp->fr_child;
2705 frp->fr_child->fr_prev = frp->fr_prev;
2706 if (frp->fr_prev != NULL)
2707 frp->fr_prev->fr_next = frp->fr_child;
2708 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2709 {
2710 frp3->fr_parent = frp2;
2711 if (frp3->fr_next == NULL)
2712 {
2713 frp3->fr_next = frp->fr_next;
2714 if (frp->fr_next != NULL)
2715 frp->fr_next->fr_prev = frp3;
2716 break;
2717 }
2718 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002719 if (topframe->fr_child == frp)
2720 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 vim_free(frp);
2722 }
2723 }
2724
2725 return wp;
2726}
2727
2728/*
2729 * Find out which frame is going to get the freed up space when "win" is
2730 * closed.
2731 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2732 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2733 * This makes opening a window and closing it immediately keep the same window
2734 * layout.
2735 */
2736 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002737win_altframe(
2738 win_T *win,
2739 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740{
2741 frame_T *frp;
2742 int b;
2743
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002744 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002745 /* Last window in this tab page, will go to next tab page. */
2746 return alt_tabpage()->tp_curwin->w_frame;
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 frp = win->w_frame;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002749 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 b = p_spr;
2751 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 b = p_sb;
2753 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2754 return frp->fr_next;
2755 return frp->fr_prev;
2756}
2757
2758/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002759 * Return the tabpage that will be used if the current one is closed.
2760 */
2761 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002762alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002763{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002764 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002765
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002766 /* Use the next tab page if possible. */
2767 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002768 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002769
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002770 /* Find the last but one tab page. */
2771 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2772 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002773 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002774}
2775
2776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 * Find the left-upper window in frame "frp".
2778 */
2779 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002780frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781{
2782 while (frp->fr_win == NULL)
2783 frp = frp->fr_child;
2784 return frp->fr_win;
2785}
2786
2787/*
2788 * Return TRUE if frame "frp" contains window "wp".
2789 */
2790 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002791frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792{
2793 frame_T *p;
2794
2795 if (frp->fr_layout == FR_LEAF)
2796 return frp->fr_win == wp;
2797
2798 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2799 if (frame_has_win(p, wp))
2800 return TRUE;
2801 return FALSE;
2802}
2803
2804/*
2805 * Set a new height for a frame. Recursively sets the height for contained
2806 * frames and windows. Caller must take care of positions.
2807 */
2808 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002809frame_new_height(
2810 frame_T *topfrp,
2811 int height,
2812 int topfirst, /* resize topmost contained frame first */
2813 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 may cause the height not to be set */
2815{
2816 frame_T *frp;
2817 int extra_lines;
2818 int h;
2819
2820 if (topfrp->fr_win != NULL)
2821 {
2822 /* Simple case: just one window. */
2823 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02002824 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01002825 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 else if (topfrp->fr_layout == FR_ROW)
2828 {
2829 do
2830 {
2831 /* All frames in this row get the same new height. */
2832 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2833 {
2834 frame_new_height(frp, height, topfirst, wfh);
2835 if (frp->fr_height > height)
2836 {
2837 /* Could not fit the windows, make the whole row higher. */
2838 height = frp->fr_height;
2839 break;
2840 }
2841 }
2842 }
2843 while (frp != NULL);
2844 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002845 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
2847 /* Complicated case: Resize a column of frames. Resize the bottom
2848 * frame first, frames above that when needed. */
2849
2850 frp = topfrp->fr_child;
2851 if (wfh)
2852 /* Advance past frames with one window with 'wfh' set. */
2853 while (frame_fixed_height(frp))
2854 {
2855 frp = frp->fr_next;
2856 if (frp == NULL)
2857 return; /* no frame without 'wfh', give up */
2858 }
2859 if (!topfirst)
2860 {
2861 /* Find the bottom frame of this column */
2862 while (frp->fr_next != NULL)
2863 frp = frp->fr_next;
2864 if (wfh)
2865 /* Advance back for frames with one window with 'wfh' set. */
2866 while (frame_fixed_height(frp))
2867 frp = frp->fr_prev;
2868 }
2869
2870 extra_lines = height - topfrp->fr_height;
2871 if (extra_lines < 0)
2872 {
2873 /* reduce height of contained frames, bottom or top frame first */
2874 while (frp != NULL)
2875 {
2876 h = frame_minheight(frp, NULL);
2877 if (frp->fr_height + extra_lines < h)
2878 {
2879 extra_lines += frp->fr_height - h;
2880 frame_new_height(frp, h, topfirst, wfh);
2881 }
2882 else
2883 {
2884 frame_new_height(frp, frp->fr_height + extra_lines,
2885 topfirst, wfh);
2886 break;
2887 }
2888 if (topfirst)
2889 {
2890 do
2891 frp = frp->fr_next;
2892 while (wfh && frp != NULL && frame_fixed_height(frp));
2893 }
2894 else
2895 {
2896 do
2897 frp = frp->fr_prev;
2898 while (wfh && frp != NULL && frame_fixed_height(frp));
2899 }
2900 /* Increase "height" if we could not reduce enough frames. */
2901 if (frp == NULL)
2902 height -= extra_lines;
2903 }
2904 }
2905 else if (extra_lines > 0)
2906 {
2907 /* increase height of bottom or top frame */
2908 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2909 }
2910 }
2911 topfrp->fr_height = height;
2912}
2913
2914/*
2915 * Return TRUE if height of frame "frp" should not be changed because of
2916 * the 'winfixheight' option.
2917 */
2918 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002919frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920{
2921 /* frame with one window: fixed height if 'winfixheight' set. */
2922 if (frp->fr_win != NULL)
2923 return frp->fr_win->w_p_wfh;
2924
2925 if (frp->fr_layout == FR_ROW)
2926 {
2927 /* The frame is fixed height if one of the frames in the row is fixed
2928 * height. */
2929 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2930 if (frame_fixed_height(frp))
2931 return TRUE;
2932 return FALSE;
2933 }
2934
2935 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2936 * frames in the row are fixed height. */
2937 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2938 if (!frame_fixed_height(frp))
2939 return FALSE;
2940 return TRUE;
2941}
2942
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002944 * Return TRUE if width of frame "frp" should not be changed because of
2945 * the 'winfixwidth' option.
2946 */
2947 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002948frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002949{
2950 /* frame with one window: fixed width if 'winfixwidth' set. */
2951 if (frp->fr_win != NULL)
2952 return frp->fr_win->w_p_wfw;
2953
2954 if (frp->fr_layout == FR_COL)
2955 {
2956 /* The frame is fixed width if one of the frames in the row is fixed
2957 * width. */
2958 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2959 if (frame_fixed_width(frp))
2960 return TRUE;
2961 return FALSE;
2962 }
2963
2964 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2965 * frames in the row are fixed width. */
2966 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2967 if (!frame_fixed_width(frp))
2968 return FALSE;
2969 return TRUE;
2970}
2971
2972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 * Add a status line to windows at the bottom of "frp".
2974 * Note: Does not check if there is room!
2975 */
2976 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002977frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
2979 win_T *wp;
2980
2981 if (frp->fr_layout == FR_LEAF)
2982 {
2983 wp = frp->fr_win;
2984 if (wp->w_status_height == 0)
2985 {
2986 if (wp->w_height > 0) /* don't make it negative */
2987 --wp->w_height;
2988 wp->w_status_height = STATUS_HEIGHT;
2989 }
2990 }
2991 else if (frp->fr_layout == FR_ROW)
2992 {
2993 /* Handle all the frames in the row. */
2994 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2995 frame_add_statusline(frp);
2996 }
2997 else /* frp->fr_layout == FR_COL */
2998 {
2999 /* Only need to handle the last frame in the column. */
3000 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3001 ;
3002 frame_add_statusline(frp);
3003 }
3004}
3005
3006/*
3007 * Set width of a frame. Handles recursively going through contained frames.
3008 * May remove separator line for windows at the right side (for win_close()).
3009 */
3010 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003011frame_new_width(
3012 frame_T *topfrp,
3013 int width,
3014 int leftfirst, /* resize leftmost contained frame first */
3015 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003016 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 frame_T *frp;
3019 int extra_cols;
3020 int w;
3021 win_T *wp;
3022
3023 if (topfrp->fr_layout == FR_LEAF)
3024 {
3025 /* Simple case: just one window. */
3026 wp = topfrp->fr_win;
3027 /* Find out if there are any windows right of this one. */
3028 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3029 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3030 break;
3031 if (frp->fr_parent == NULL)
3032 wp->w_vsep_width = 0;
3033 win_new_width(wp, width - wp->w_vsep_width);
3034 }
3035 else if (topfrp->fr_layout == FR_COL)
3036 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003037 do
3038 {
3039 /* All frames in this column get the same new width. */
3040 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3041 {
3042 frame_new_width(frp, width, leftfirst, wfw);
3043 if (frp->fr_width > width)
3044 {
3045 /* Could not fit the windows, make whole column wider. */
3046 width = frp->fr_width;
3047 break;
3048 }
3049 }
3050 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 }
3052 else /* fr_layout == FR_ROW */
3053 {
3054 /* Complicated case: Resize a row of frames. Resize the rightmost
3055 * frame first, frames left of it when needed. */
3056
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003058 if (wfw)
3059 /* Advance past frames with one window with 'wfw' set. */
3060 while (frame_fixed_width(frp))
3061 {
3062 frp = frp->fr_next;
3063 if (frp == NULL)
3064 return; /* no frame without 'wfw', give up */
3065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003067 {
3068 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 while (frp->fr_next != NULL)
3070 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003071 if (wfw)
3072 /* Advance back for frames with one window with 'wfw' set. */
3073 while (frame_fixed_width(frp))
3074 frp = frp->fr_prev;
3075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076
3077 extra_cols = width - topfrp->fr_width;
3078 if (extra_cols < 0)
3079 {
3080 /* reduce frame width, rightmost frame first */
3081 while (frp != NULL)
3082 {
3083 w = frame_minwidth(frp, NULL);
3084 if (frp->fr_width + extra_cols < w)
3085 {
3086 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003087 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
3089 else
3090 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003091 frame_new_width(frp, frp->fr_width + extra_cols,
3092 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 break;
3094 }
3095 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003096 {
3097 do
3098 frp = frp->fr_next;
3099 while (wfw && frp != NULL && frame_fixed_width(frp));
3100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003102 {
3103 do
3104 frp = frp->fr_prev;
3105 while (wfw && frp != NULL && frame_fixed_width(frp));
3106 }
3107 /* Increase "width" if we could not reduce enough frames. */
3108 if (frp == NULL)
3109 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 }
3111 }
3112 else if (extra_cols > 0)
3113 {
3114 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003115 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 }
3117 }
3118 topfrp->fr_width = width;
3119}
3120
3121/*
3122 * Add the vertical separator to windows at the right side of "frp".
3123 * Note: Does not check if there is room!
3124 */
3125 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003126frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 win_T *wp;
3129
3130 if (frp->fr_layout == FR_LEAF)
3131 {
3132 wp = frp->fr_win;
3133 if (wp->w_vsep_width == 0)
3134 {
3135 if (wp->w_width > 0) /* don't make it negative */
3136 --wp->w_width;
3137 wp->w_vsep_width = 1;
3138 }
3139 }
3140 else if (frp->fr_layout == FR_COL)
3141 {
3142 /* Handle all the frames in the column. */
3143 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3144 frame_add_vsep(frp);
3145 }
3146 else /* frp->fr_layout == FR_ROW */
3147 {
3148 /* Only need to handle the last frame in the row. */
3149 frp = frp->fr_child;
3150 while (frp->fr_next != NULL)
3151 frp = frp->fr_next;
3152 frame_add_vsep(frp);
3153 }
3154}
3155
3156/*
3157 * Set frame width from the window it contains.
3158 */
3159 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003160frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165/*
3166 * Set frame height from the window it contains.
3167 */
3168 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003169frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003171 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172}
3173
3174/*
3175 * Compute the minimal height for frame "topfrp".
3176 * Uses the 'winminheight' option.
3177 * When "next_curwin" isn't NULL, use p_wh for this window.
3178 * When "next_curwin" is NOWIN, don't use at least one line for the current
3179 * window.
3180 */
3181 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003182frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 frame_T *frp;
3185 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
3188 if (topfrp->fr_win != NULL)
3189 {
3190 if (topfrp->fr_win == next_curwin)
3191 m = p_wh + topfrp->fr_win->w_status_height;
3192 else
3193 {
3194 /* window: minimal height of the window plus status line */
3195 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003196 if (topfrp->fr_win == curwin && next_curwin == NULL)
3197 {
3198 /* Current window is minimal one line high and WinBar is
3199 * visible. */
3200 if (p_wmh == 0)
3201 ++m;
3202 m += WINBAR_HEIGHT(curwin);
3203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 else if (topfrp->fr_layout == FR_ROW)
3207 {
3208 /* get the minimal height from each frame in this row */
3209 m = 0;
3210 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3211 {
3212 n = frame_minheight(frp, next_curwin);
3213 if (n > m)
3214 m = n;
3215 }
3216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 else
3218 {
3219 /* Add up the minimal heights for all frames in this column. */
3220 m = 0;
3221 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3222 m += frame_minheight(frp, next_curwin);
3223 }
3224
3225 return m;
3226}
3227
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228/*
3229 * Compute the minimal width for frame "topfrp".
3230 * When "next_curwin" isn't NULL, use p_wiw for this window.
3231 * When "next_curwin" is NOWIN, don't use at least one column for the current
3232 * window.
3233 */
3234 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003235frame_minwidth(
3236 frame_T *topfrp,
3237 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 frame_T *frp;
3240 int m, n;
3241
3242 if (topfrp->fr_win != NULL)
3243 {
3244 if (topfrp->fr_win == next_curwin)
3245 m = p_wiw + topfrp->fr_win->w_vsep_width;
3246 else
3247 {
3248 /* window: minimal width of the window plus separator column */
3249 m = p_wmw + topfrp->fr_win->w_vsep_width;
3250 /* Current window is minimal one column wide */
3251 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3252 ++m;
3253 }
3254 }
3255 else if (topfrp->fr_layout == FR_COL)
3256 {
3257 /* get the minimal width from each frame in this column */
3258 m = 0;
3259 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3260 {
3261 n = frame_minwidth(frp, next_curwin);
3262 if (n > m)
3263 m = n;
3264 }
3265 }
3266 else
3267 {
3268 /* Add up the minimal widths for all frames in this row. */
3269 m = 0;
3270 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3271 m += frame_minwidth(frp, next_curwin);
3272 }
3273
3274 return m;
3275}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
3277
3278/*
3279 * Try to close all windows except current one.
3280 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3281 * used and the buffer was modified.
3282 *
3283 * Used by ":bdel" and ":only".
3284 */
3285 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003286close_others(
3287 int message,
3288 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 win_T *wp;
3291 win_T *nextwp;
3292 int r;
3293
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003294 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003296 if (message && !autocmd_busy)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003297 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 return;
3299 }
3300
3301 /* Be very careful here: autocommands may change the window layout. */
3302 for (wp = firstwin; win_valid(wp); wp = nextwp)
3303 {
3304 nextwp = wp->w_next;
3305 if (wp != curwin) /* don't close current window */
3306 {
3307
3308 /* Check if it's allowed to abandon this window */
3309 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (!win_valid(wp)) /* autocommands messed wp up */
3311 {
3312 nextwp = firstwin;
3313 continue;
3314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 if (!r)
3316 {
3317#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3318 if (message && (p_confirm || cmdmod.confirm) && p_write)
3319 {
3320 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 if (!win_valid(wp)) /* autocommands messed wp up */
3322 {
3323 nextwp = firstwin;
3324 continue;
3325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 }
3327 if (bufIsChanged(wp->w_buffer))
3328#endif
3329 continue;
3330 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003331 win_close(wp, !buf_hide(wp->w_buffer)
3332 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 }
3335
Bram Moolenaar459ca562016-11-10 18:16:33 +01003336 if (message && !ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 EMSG(_("E445: Other window contains changes"));
3338}
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003341 * Init the current window "curwin".
3342 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 */
3344 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003345curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003347 win_init_empty(curwin);
3348}
3349
3350 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003351win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003352{
3353 redraw_win_later(wp, NOT_VALID);
3354 wp->w_lines_valid = 0;
3355 wp->w_cursor.lnum = 1;
3356 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003358 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003360 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3361 wp->w_pcmark.col = 0;
3362 wp->w_prev_pcmark.lnum = 0;
3363 wp->w_prev_pcmark.col = 0;
3364 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003366 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003368 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003370 if (wp->w_p_rl)
3371 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003373 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003375#ifdef FEAT_SYN_HL
3376 wp->w_s = &wp->w_buffer->b_s;
3377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378}
3379
3380/*
3381 * Allocate the first window and put an empty buffer in it.
3382 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003383 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003385 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003386win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003388 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003389 return FAIL;
3390
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003391 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003392 if (first_tabpage == NULL)
3393 return FAIL;
3394 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003395 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003396
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003397 return OK;
3398}
3399
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003400/*
3401 * Init "aucmd_win". This can only be done after the first
3402 * window is fully initialized, thus it can't be in win_alloc_first().
3403 */
3404 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003405win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003406{
3407 aucmd_win = win_alloc(NULL, TRUE);
3408 if (aucmd_win != NULL)
3409 {
3410 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003411 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003412 new_frame(aucmd_win);
3413 }
3414}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003415
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003416/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003417 * Allocate the first window or the first window in a new tab page.
3418 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003419 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420 * Return FAIL when something goes wrong (out of memory).
3421 */
3422 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003423win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003424{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003425 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003426 if (oldwin == NULL)
3427 {
3428 /* Very first window, need to create an empty buffer for it and
3429 * initialize from scratch. */
3430 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3431 if (curwin == NULL || curbuf == NULL)
3432 return FAIL;
3433 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003434#ifdef FEAT_SYN_HL
3435 curwin->w_s = &(curbuf->b_s);
3436#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003437 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003438 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003439 curwin_init(); /* init current window */
3440 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003441 else
3442 {
3443 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003444 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003445
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003446 /* We don't want cursor- and scroll-binding in the first window. */
3447 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003450 new_frame(curwin);
3451 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003452 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003453 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003456
3457 return OK;
3458}
3459
3460/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003461 * Create a frame for window "wp".
3462 */
3463 static void
3464new_frame(win_T *wp)
3465{
3466 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3467
3468 wp->w_frame = frp;
3469 if (frp != NULL)
3470 {
3471 frp->fr_layout = FR_LEAF;
3472 frp->fr_win = wp;
3473 }
3474}
3475
3476/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003477 * Initialize the window and frame size to the maximum.
3478 */
3479 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003480win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003481{
3482 firstwin->w_height = ROWS_AVAIL;
3483 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003484 firstwin->w_width = Columns;
3485 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486}
3487
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003488/*
3489 * Allocate a new tabpage_T and init the values.
3490 * Returns NULL when out of memory.
3491 */
3492 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003493alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003494{
3495 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003496# ifdef FEAT_GUI
3497 int i;
3498# endif
3499
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003500
3501 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003502 if (tp == NULL)
3503 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003504
Bram Moolenaar429fa852013-04-15 12:27:36 +02003505# ifdef FEAT_EVAL
3506 /* init t: variables */
3507 tp->tp_vars = dict_alloc();
3508 if (tp->tp_vars == NULL)
3509 {
3510 vim_free(tp);
3511 return NULL;
3512 }
3513 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3514# endif
3515
3516# ifdef FEAT_GUI
3517 for (i = 0; i < 3; i++)
3518 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003519# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003520# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003521 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003522# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003523 tp->tp_ch_used = p_ch;
3524
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003525 return tp;
3526}
3527
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003528 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003529free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003530{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003531 int idx;
3532
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003533# ifdef FEAT_DIFF
3534 diff_clear(tp);
3535# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003536 for (idx = 0; idx < SNAP_COUNT; ++idx)
3537 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003538#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003539 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3540 hash_init(&tp->tp_vars->dv_hashtab);
3541 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003542#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003543
3544#ifdef FEAT_PYTHON
3545 python_tabpage_free(tp);
3546#endif
3547
3548#ifdef FEAT_PYTHON3
3549 python3_tabpage_free(tp);
3550#endif
3551
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003552 vim_free(tp);
3553}
3554
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003555/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003556 * Create a new Tab page with one window.
3557 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003558 * When "after" is 0 put it just after the current Tab page.
3559 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003560 * Return FAIL or OK.
3561 */
3562 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003563win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003564{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003565 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003566 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003567 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003568
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003569 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003570 if (newtp == NULL)
3571 return FAIL;
3572
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003573 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003574 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003575 {
3576 vim_free(newtp);
3577 return FAIL;
3578 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003579 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003580
3581 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003582 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003583 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003584 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003585 if (after == 1)
3586 {
3587 /* New tab page becomes the first one. */
3588 newtp->tp_next = first_tabpage;
3589 first_tabpage = newtp;
3590 }
3591 else
3592 {
3593 if (after > 0)
3594 {
3595 /* Put new tab page before tab page "after". */
3596 n = 2;
3597 for (tp = first_tabpage; tp->tp_next != NULL
3598 && n < after; tp = tp->tp_next)
3599 ++n;
3600 }
3601 newtp->tp_next = tp->tp_next;
3602 tp->tp_next = newtp;
3603 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003604 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003605 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003606 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003607
3608 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003609 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003610
3611#if defined(FEAT_GUI)
3612 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3613 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003614 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003615#endif
3616
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003617 redraw_all_later(CLEAR);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003618 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003619 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003620 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003621 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003622 return OK;
3623 }
3624
3625 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003626 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003627 return FAIL;
3628}
3629
3630/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003631 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3632 * like with ":split".
3633 * Returns OK if a new tab page was created, FAIL otherwise.
3634 */
3635 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003636may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003637{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003638 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003639
Bram Moolenaard326ce82007-03-11 14:48:29 +00003640 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003641 {
3642 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003643 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003644 return win_new_tabpage(n);
3645 }
3646 return FAIL;
3647}
3648
3649/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003650 * Create up to "maxcount" tabpages with empty windows.
3651 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003652 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003653 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003654make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003655{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003656 int count = maxcount;
3657 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003658
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003659 /* Limit to 'tabpagemax' tabs. */
3660 if (count > p_tpm)
3661 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003662
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003663 /*
3664 * Don't execute autocommands while creating the tab pages. Must do that
3665 * when putting the buffers in the windows.
3666 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003667 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003668
3669 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003670 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003672
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003673 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003674
3675 /* return actual number of tab pages */
3676 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003677}
3678
3679/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003680 * Return TRUE when "tpc" points to a valid tab page.
3681 */
3682 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003683valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003684{
3685 tabpage_T *tp;
3686
Bram Moolenaar29323592016-07-24 22:04:11 +02003687 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003688 if (tp == tpc)
3689 return TRUE;
3690 return FALSE;
3691}
3692
3693/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003694 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3695 * valid.
3696 */
3697 int
3698valid_tabpage_win(tabpage_T *tpc)
3699{
3700 tabpage_T *tp;
3701 win_T *wp;
3702
3703 FOR_ALL_TABPAGES(tp)
3704 {
3705 if (tp == tpc)
3706 {
3707 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3708 {
3709 if (win_valid_any_tab(wp))
3710 return TRUE;
3711 }
3712 return FALSE;
3713 }
3714 }
3715 /* shouldn't happen */
3716 return FALSE;
3717}
3718
3719/*
3720 * Close tabpage "tab", assuming it has no windows in it.
3721 * There must be another tabpage or this will crash.
3722 */
3723 void
3724close_tabpage(tabpage_T *tab)
3725{
3726 tabpage_T *ptp;
3727
3728 if (tab == first_tabpage)
3729 {
3730 first_tabpage = tab->tp_next;
3731 ptp = first_tabpage;
3732 }
3733 else
3734 {
3735 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3736 ptp = ptp->tp_next)
3737 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003738 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003739 ptp->tp_next = tab->tp_next;
3740 }
3741
3742 goto_tabpage_tp(ptp, FALSE, FALSE);
3743 free_tabpage(tab);
3744}
3745
3746/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003747 * Find tab page "n" (first one is 1). Returns NULL when not found.
3748 */
3749 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003750find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003751{
3752 tabpage_T *tp;
3753 int i = 1;
3754
3755 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3756 ++i;
3757 return tp;
3758}
3759
3760/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003761 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003762 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003763 */
3764 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003765tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003766{
3767 int i = 1;
3768 tabpage_T *tp;
3769
3770 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3771 ++i;
3772 return i;
3773}
3774
3775/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003776 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003777 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003778 * FAIL.
3779 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003780 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003781 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003782leave_tabpage(
3783 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003784 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003785 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003786{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003787 tabpage_T *tp = curtab;
3788
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003789 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003790 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003791 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003792 if (new_curbuf != curbuf)
3793 {
3794 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3795 if (curtab != tp)
3796 return FAIL;
3797 }
3798 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3799 if (curtab != tp)
3800 return FAIL;
3801 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003802 if (curtab != tp)
3803 return FAIL;
3804 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003805#if defined(FEAT_GUI)
3806 /* Remove the scrollbars. They may be added back later. */
3807 if (gui.in_use)
3808 gui_remove_scrollbars();
3809#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003811 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003812 tp->tp_firstwin = firstwin;
3813 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003814 tp->tp_old_Rows = Rows;
3815 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003816 firstwin = NULL;
3817 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003818 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003819}
3820
3821/*
3822 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003823 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003824 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3825 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003826 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003827 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003828enter_tabpage(
3829 tabpage_T *tp,
3830 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003831 int trigger_enter_autocmds,
3832 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003833{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003834 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003835 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003836
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003837 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003838 firstwin = tp->tp_firstwin;
3839 lastwin = tp->tp_lastwin;
3840 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003841
3842 /* We would like doing the TabEnter event first, but we don't have a
3843 * valid current window yet, which may break some commands.
3844 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003845 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003846 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003847 prevwin = next_prevwin;
3848
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003849 last_status(FALSE); /* status line may appear or disappear */
3850 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003851 must_redraw = CLEAR; /* need to redraw everything */
3852#ifdef FEAT_DIFF
3853 diff_need_scrollbind = TRUE;
3854#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003855
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003856 /* The tabpage line may have appeared or disappeared, may need to resize
3857 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003858 * frame sizes too. Use the stored value of p_ch, so that it can be
3859 * different for each tab page. */
3860 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003861 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3862#ifdef FEAT_GUI_TABLINE
3863 && !gui_use_tabline()
3864#endif
3865 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003866 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003867 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003868 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003869
3870#if defined(FEAT_GUI)
3871 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3872 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003873 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003874#endif
3875
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003876 /* Apply autocommands after updating the display, when 'rows' and
3877 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003878 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003879 {
3880 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3881 if (old_curbuf != curbuf)
3882 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3883 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003884
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003885 redraw_all_later(CLEAR);
3886}
3887
3888/*
3889 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003890 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003891 */
3892 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003893goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003894{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003895 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003896 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003897 int i;
3898
Bram Moolenaard68071d2006-05-02 22:08:30 +00003899 if (text_locked())
3900 {
3901 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02003902 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00003903 return;
3904 }
3905
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003906 /* If there is only one it can't work. */
3907 if (first_tabpage->tp_next == NULL)
3908 {
3909 if (n > 1)
3910 beep_flush();
3911 return;
3912 }
3913
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003914 if (n == 0)
3915 {
3916 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003917 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003918 tp = first_tabpage;
3919 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003920 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003921 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003922 else if (n < 0)
3923 {
3924 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3925 * this N times. */
3926 ttp = curtab;
3927 for (i = n; i < 0; ++i)
3928 {
3929 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3930 tp = tp->tp_next)
3931 ;
3932 ttp = tp;
3933 }
3934 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003935 else if (n == 9999)
3936 {
3937 /* Go to last tab page. */
3938 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3939 ;
3940 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003941 else
3942 {
3943 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003944 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003945 if (tp == NULL)
3946 {
3947 beep_flush();
3948 return;
3949 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003950 }
3951
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003952 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003953
3954#ifdef FEAT_GUI_TABLINE
3955 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003956 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003957#endif
3958}
3959
3960/*
3961 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003962 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3963 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003964 * Note: doesn't update the GUI tab.
3965 */
3966 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003967goto_tabpage_tp(
3968 tabpage_T *tp,
3969 int trigger_enter_autocmds,
3970 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003971{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003972 /* Don't repeat a message in another tab page. */
3973 set_keep_msg(NULL, 0);
3974
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003975 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3976 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003977 {
3978 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003979 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3980 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003981 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003982 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3983 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003984 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
3987/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003988 * Enter window "wp" in tab page "tp".
3989 * Also updates the GUI tab.
3990 */
3991 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003992goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003993{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003994 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003995 if (curtab == tp && win_valid(wp))
3996 {
3997 win_enter(wp, TRUE);
3998# ifdef FEAT_GUI_TABLINE
3999 if (gui_use_tabline())
4000 gui_mch_set_curtab(tabpage_index(curtab));
4001# endif
4002 }
4003}
4004
4005/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004006 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004007 */
4008 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004009tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004010{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004011 int n = 1;
4012 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004013
4014 if (first_tabpage->tp_next == NULL)
4015 return;
4016
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004017 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4018 ++n;
4019
4020 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4021 && tp->tp_next == curtab))
4022 return;
4023
4024 tp_dst = tp;
4025
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004026 /* Remove the current tab page from the list of tab pages. */
4027 if (curtab == first_tabpage)
4028 first_tabpage = curtab->tp_next;
4029 else
4030 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004031 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004032 if (tp->tp_next == curtab)
4033 break;
4034 if (tp == NULL) /* "cannot happen" */
4035 return;
4036 tp->tp_next = curtab->tp_next;
4037 }
4038
4039 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004040 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004041 {
4042 curtab->tp_next = first_tabpage;
4043 first_tabpage = curtab;
4044 }
4045 else
4046 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004047 curtab->tp_next = tp_dst->tp_next;
4048 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004049 }
4050
4051 /* Need to redraw the tabline. Tab page contents doesn't change. */
4052 redraw_tabline = TRUE;
4053}
4054
4055
4056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 * Go to another window.
4058 * When jumping to another buffer, stop Visual mode. Do this before
4059 * changing windows so we can yank the selection into the '*' register.
4060 * When jumping to another window on the same buffer, adjust its cursor
4061 * position to keep the same Visual area.
4062 */
4063 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004064win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004066#ifdef FEAT_CONCEAL
4067 win_T *owp = curwin;
4068#endif
4069
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004070 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
4072 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004073 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 return;
4075 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004076 if (curbuf_locked())
4077 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004078
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 if (wp->w_buffer != curbuf)
4080 reset_VIsual_and_resel();
4081 else if (VIsual_active)
4082 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083
4084#ifdef FEAT_GUI
4085 need_mouse_correct = TRUE;
4086#endif
4087 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004088
4089#ifdef FEAT_CONCEAL
4090 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004091 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004092 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004093 if (curwin->w_p_cole > 0 && !msg_scrolled)
4094 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096}
4097
4098#if defined(FEAT_PERL) || defined(PROTO)
4099/*
4100 * Find window number "winnr" (counting top to bottom).
4101 */
4102 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004103win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
4105 win_T *wp;
4106
Bram Moolenaar29323592016-07-24 22:04:11 +02004107 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (--winnr == 0)
4109 break;
4110 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111}
4112#endif
4113
Bram Moolenaar4033c552017-09-16 20:54:51 +02004114#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004115/*
4116 * Find the tabpage for window "win".
4117 */
4118 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004119win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004120{
4121 win_T *wp;
4122 tabpage_T *tp;
4123
Bram Moolenaar29323592016-07-24 22:04:11 +02004124 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004125 if (wp == win)
4126 return tp;
4127 return NULL;
4128}
4129#endif
4130
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131/*
4132 * Move to window above or below "count" times.
4133 */
4134 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004135win_goto_ver(
4136 int up, /* TRUE to go to win above */
4137 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
4139 frame_T *fr;
4140 frame_T *nfr;
4141 frame_T *foundfr;
4142
4143 foundfr = curwin->w_frame;
4144 while (count--)
4145 {
4146 /*
4147 * First go upwards in the tree of frames until we find a upwards or
4148 * downwards neighbor.
4149 */
4150 fr = foundfr;
4151 for (;;)
4152 {
4153 if (fr == topframe)
4154 goto end;
4155 if (up)
4156 nfr = fr->fr_prev;
4157 else
4158 nfr = fr->fr_next;
4159 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4160 break;
4161 fr = fr->fr_parent;
4162 }
4163
4164 /*
4165 * Now go downwards to find the bottom or top frame in it.
4166 */
4167 for (;;)
4168 {
4169 if (nfr->fr_layout == FR_LEAF)
4170 {
4171 foundfr = nfr;
4172 break;
4173 }
4174 fr = nfr->fr_child;
4175 if (nfr->fr_layout == FR_ROW)
4176 {
4177 /* Find the frame at the cursor row. */
4178 while (fr->fr_next != NULL
4179 && frame2win(fr)->w_wincol + fr->fr_width
4180 <= curwin->w_wincol + curwin->w_wcol)
4181 fr = fr->fr_next;
4182 }
4183 if (nfr->fr_layout == FR_COL && up)
4184 while (fr->fr_next != NULL)
4185 fr = fr->fr_next;
4186 nfr = fr;
4187 }
4188 }
4189end:
4190 if (foundfr != NULL)
4191 win_goto(foundfr->fr_win);
4192}
4193
4194/*
4195 * Move to left or right window.
4196 */
4197 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004198win_goto_hor(
4199 int left, /* TRUE to go to left win */
4200 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
4202 frame_T *fr;
4203 frame_T *nfr;
4204 frame_T *foundfr;
4205
4206 foundfr = curwin->w_frame;
4207 while (count--)
4208 {
4209 /*
4210 * First go upwards in the tree of frames until we find a left or
4211 * right neighbor.
4212 */
4213 fr = foundfr;
4214 for (;;)
4215 {
4216 if (fr == topframe)
4217 goto end;
4218 if (left)
4219 nfr = fr->fr_prev;
4220 else
4221 nfr = fr->fr_next;
4222 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4223 break;
4224 fr = fr->fr_parent;
4225 }
4226
4227 /*
4228 * Now go downwards to find the leftmost or rightmost frame in it.
4229 */
4230 for (;;)
4231 {
4232 if (nfr->fr_layout == FR_LEAF)
4233 {
4234 foundfr = nfr;
4235 break;
4236 }
4237 fr = nfr->fr_child;
4238 if (nfr->fr_layout == FR_COL)
4239 {
4240 /* Find the frame at the cursor row. */
4241 while (fr->fr_next != NULL
4242 && frame2win(fr)->w_winrow + fr->fr_height
4243 <= curwin->w_winrow + curwin->w_wrow)
4244 fr = fr->fr_next;
4245 }
4246 if (nfr->fr_layout == FR_ROW && left)
4247 while (fr->fr_next != NULL)
4248 fr = fr->fr_next;
4249 nfr = fr;
4250 }
4251 }
4252end:
4253 if (foundfr != NULL)
4254 win_goto(foundfr->fr_win);
4255}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257/*
4258 * Make window "wp" the current window.
4259 */
4260 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004261win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004263 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264}
4265
4266/*
4267 * Make window wp the current window.
4268 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4269 * been closed and isn't valid.
4270 */
4271 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004272win_enter_ext(
4273 win_T *wp,
4274 int undo_sync,
4275 int curwin_invalid,
Bram Moolenaarc917da42016-07-19 22:31:36 +02004276 int trigger_new_autocmds UNUSED,
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004277 int trigger_enter_autocmds UNUSED,
4278 int trigger_leave_autocmds UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 if (wp == curwin && !curwin_invalid) /* nothing to do */
4283 return;
4284
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004285 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 {
4287 /*
4288 * Be careful: If autocommands delete the window, return now.
4289 */
4290 if (wp->w_buffer != curbuf)
4291 {
4292 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4293 other_buffer = TRUE;
4294 if (!win_valid(wp))
4295 return;
4296 }
4297 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4298 if (!win_valid(wp))
4299 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004300#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 /* autocmds may abort script processing */
4302 if (aborting())
4303 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306
4307 /* sync undo before leaving the current buffer */
4308 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004309 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004310
4311 /* Might need to scroll the old window before switching, e.g., when the
4312 * cursor was moved. */
4313 update_topline();
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 /* may have to copy the buffer options when 'cpo' contains 'S' */
4316 if (wp->w_buffer != curbuf)
4317 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4318 if (!curwin_invalid)
4319 {
4320 prevwin = curwin; /* remember for CTRL-W p */
4321 curwin->w_redr_status = TRUE;
4322 }
4323 curwin = wp;
4324 curbuf = wp->w_buffer;
4325 check_cursor();
4326#ifdef FEAT_VIRTUALEDIT
4327 if (!virtual_active())
4328 curwin->w_cursor.coladd = 0;
4329#endif
4330 changed_line_abv_curs(); /* assume cursor position needs updating */
4331
4332 if (curwin->w_localdir != NULL)
4333 {
4334 /* Window has a local directory: Save current directory as global
4335 * directory (unless that was done already) and change to the local
4336 * directory. */
4337 if (globaldir == NULL)
4338 {
4339 char_u cwd[MAXPATHL];
4340
4341 if (mch_dirname(cwd, MAXPATHL) == OK)
4342 globaldir = vim_strsave(cwd);
4343 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004344 if (mch_chdir((char *)curwin->w_localdir) == 0)
4345 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 else if (globaldir != NULL)
4348 {
4349 /* Window doesn't have a local directory and we are not in the global
4350 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004351 ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004352 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 shorten_fnames(TRUE);
4354 }
4355
Bram Moolenaarc917da42016-07-19 22:31:36 +02004356 if (trigger_new_autocmds)
4357 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004358 if (trigger_enter_autocmds)
4359 {
4360 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4361 if (other_buffer)
4362 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
4365#ifdef FEAT_TITLE
4366 maketitle();
4367#endif
4368 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004369 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (restart_edit)
4371 redraw_later(VALID); /* causes status line redraw */
4372
4373 /* set window height to desired minimal value */
4374 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4375 win_setheight((int)p_wh);
4376 else if (curwin->w_height == 0)
4377 win_setheight(1);
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004380 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383#ifdef FEAT_MOUSE
4384 setmouse(); /* in case jumped to/from help buffer */
4385#endif
4386
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004387 /* Change directories when the 'acd' option is set. */
4388 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389}
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004393 * Jump to the first open window that contains buffer "buf", if one exists.
4394 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 */
4396 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004397buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004399 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004401 if (curwin->w_buffer == buf)
4402 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004403 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004404 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004405 if (wp->w_buffer == buf)
4406 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if (wp != NULL)
4408 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004409 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004411
4412/*
4413 * Jump to the first open window in any tab page that contains buffer "buf",
4414 * if one exists.
4415 * Returns a pointer to the window found, otherwise NULL.
4416 */
4417 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004418buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004419{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004420 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004421 tabpage_T *tp;
4422
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004423 if (wp != NULL)
4424 return wp;
4425
Bram Moolenaar29323592016-07-24 22:04:11 +02004426 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004427 if (tp != curtab)
4428 {
4429 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4430 if (wp->w_buffer == buf)
4431 break;
4432 if (wp != NULL)
4433 {
4434 goto_tabpage_win(tp, wp);
4435 if (curwin != wp)
4436 wp = NULL; /* something went wrong */
4437 break;
4438 }
4439 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004440 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004441}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004443static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004446 * Allocate a window structure and link it in the window list when "hidden" is
4447 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004450win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004452 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 /*
4455 * allocate window structure and linesizes arrays
4456 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004457 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004458 if (new_wp == NULL)
4459 return NULL;
4460
4461 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004463 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004464 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466
Bram Moolenaar86edef62016-03-13 18:07:30 +01004467 new_wp->w_id = ++last_win_id;
4468
Bram Moolenaar429fa852013-04-15 12:27:36 +02004469#ifdef FEAT_EVAL
4470 /* init w: variables */
4471 new_wp->w_vars = dict_alloc();
4472 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004474 win_free_lsize(new_wp);
4475 vim_free(new_wp);
4476 return NULL;
4477 }
4478 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004479#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004480
Bram Moolenaar429fa852013-04-15 12:27:36 +02004481 /* Don't execute autocommands while the window is not properly
4482 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4483 * event. */
4484 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004485
Bram Moolenaar429fa852013-04-15 12:27:36 +02004486 /*
4487 * link the window in the window list
4488 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004489 if (!hidden)
4490 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004491 new_wp->w_wincol = 0;
4492 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar429fa852013-04-15 12:27:36 +02004494 /* position the display and the cursor at the top of the file. */
4495 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004497 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004499 new_wp->w_botline = 2;
4500 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004501 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
Bram Moolenaar429fa852013-04-15 12:27:36 +02004503 /* We won't calculate w_fraction until resizing the window */
4504 new_wp->w_fraction = 0;
4505 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004508 if (gui.in_use)
4509 {
4510 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4511 SBAR_LEFT, new_wp);
4512 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4513 SBAR_RIGHT, new_wp);
4514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515#endif
4516#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004517 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004519 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004520#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004521 new_wp->w_match_head = NULL;
4522 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004523#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004524 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525}
4526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004528 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 */
4530 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004531win_free(
4532 win_T *wp,
4533 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534{
4535 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004536 buf_T *buf;
4537 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004539#ifdef FEAT_FOLDING
4540 clearFolding(wp);
4541#endif
4542
4543 /* reduce the reference count to the argument list. */
4544 alist_unlink(wp->w_alist);
4545
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004546 /* Don't execute autocommands while the window is halfway being deleted.
4547 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004548 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004549
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004550#ifdef FEAT_LUA
4551 lua_window_free(wp);
4552#endif
4553
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004554#ifdef FEAT_MZSCHEME
4555 mzscheme_window_free(wp);
4556#endif
4557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558#ifdef FEAT_PERL
4559 perl_win_free(wp);
4560#endif
4561
4562#ifdef FEAT_PYTHON
4563 python_window_free(wp);
4564#endif
4565
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004566#ifdef FEAT_PYTHON3
4567 python3_window_free(wp);
4568#endif
4569
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570#ifdef FEAT_TCL
4571 tcl_window_free(wp);
4572#endif
4573
4574#ifdef FEAT_RUBY
4575 ruby_window_free(wp);
4576#endif
4577
4578 clear_winopt(&wp->w_onebuf_opt);
4579 clear_winopt(&wp->w_allbuf_opt);
4580
4581#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004582 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4583 hash_init(&wp->w_vars->dv_hashtab);
4584 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585#endif
4586
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004587 {
4588 tabpage_T *ttp;
4589
4590 if (prevwin == wp)
4591 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004592 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004593 if (ttp->tp_prevwin == wp)
4594 ttp->tp_prevwin = NULL;
4595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 win_free_lsize(wp);
4597
4598 for (i = 0; i < wp->w_tagstacklen; ++i)
4599 vim_free(wp->w_tagstack[i].tagname);
4600
4601 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004602
Bram Moolenaarff18df02013-07-24 17:51:57 +02004603 /* Remove the window from the b_wininfo lists, it may happen that the
4604 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004605 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004606 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4607 if (wip->wi_win == wp)
4608 wip->wi_win = NULL;
4609
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004611 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#ifdef FEAT_JUMPLIST
4615 free_jumplist(wp);
4616#endif
4617
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004618#ifdef FEAT_QUICKFIX
4619 qf_free_all(wp);
4620#endif
4621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622#ifdef FEAT_GUI
4623 if (gui.in_use)
4624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4626 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4627 }
4628#endif /* FEAT_GUI */
4629
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004630#ifdef FEAT_MENU
4631 remove_winbar(wp);
4632#endif
4633
Bram Moolenaar860cae12010-06-05 23:22:07 +02004634#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004635 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004636#endif
4637
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004638 if (wp != aucmd_win)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004639 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004640 if (autocmd_busy)
4641 {
4642 wp->w_next = au_pending_free_win;
4643 au_pending_free_win = wp;
4644 }
4645 else
4646 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004647
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004648 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649}
4650
4651/*
4652 * Append window "wp" in the window list after window "after".
4653 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004654 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004655win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
4657 win_T *before;
4658
4659 if (after == NULL) /* after NULL is in front of the first */
4660 before = firstwin;
4661 else
4662 before = after->w_next;
4663
4664 wp->w_next = before;
4665 wp->w_prev = after;
4666 if (after == NULL)
4667 firstwin = wp;
4668 else
4669 after->w_next = wp;
4670 if (before == NULL)
4671 lastwin = wp;
4672 else
4673 before->w_prev = wp;
4674}
4675
4676/*
4677 * Remove a window from the window list.
4678 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004679 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004680win_remove(
4681 win_T *wp,
4682 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
4684 if (wp->w_prev != NULL)
4685 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004686 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004687 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004688 else
4689 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 if (wp->w_next != NULL)
4692 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004693 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004694 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004695 else
4696 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697}
4698
4699/*
4700 * Append frame "frp" in a frame list after frame "after".
4701 */
4702 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004703frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
4705 frp->fr_next = after->fr_next;
4706 after->fr_next = frp;
4707 if (frp->fr_next != NULL)
4708 frp->fr_next->fr_prev = frp;
4709 frp->fr_prev = after;
4710}
4711
4712/*
4713 * Insert frame "frp" in a frame list before frame "before".
4714 */
4715 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004716frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
4718 frp->fr_next = before;
4719 frp->fr_prev = before->fr_prev;
4720 before->fr_prev = frp;
4721 if (frp->fr_prev != NULL)
4722 frp->fr_prev->fr_next = frp;
4723 else
4724 frp->fr_parent->fr_child = frp;
4725}
4726
4727/*
4728 * Remove a frame from a frame list.
4729 */
4730 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004731frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
4733 if (frp->fr_prev != NULL)
4734 frp->fr_prev->fr_next = frp->fr_next;
4735 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004736 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004738 /* special case: topframe->fr_child == frp */
4739 if (topframe->fr_child == frp)
4740 topframe->fr_child = frp->fr_next;
4741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 if (frp->fr_next != NULL)
4743 frp->fr_next->fr_prev = frp->fr_prev;
4744}
4745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746/*
4747 * Allocate w_lines[] for window "wp".
4748 * Return FAIL for failure, OK for success.
4749 */
4750 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004751win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004754 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 if (wp->w_lines == NULL)
4756 return FAIL;
4757 return OK;
4758}
4759
4760/*
4761 * free lsize arrays for a window
4762 */
4763 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004764win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004766 /* TODO: why would wp be NULL here? */
4767 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004768 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769}
4770
4771/*
4772 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004773 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 */
4775 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004776shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004778 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779
4780 if (firstwin == NULL) /* not initialized yet */
4781 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (h < frame_minheight(topframe, NULL))
4783 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004784
4785 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 * that doesn't result in the right height, forget about that option. */
4787 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004788 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 frame_new_height(topframe, h, FALSE, FALSE);
4790
4791 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004793 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004794
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795#if 0
4796 /* Disabled: don't want making the screen smaller make a window larger. */
4797 if (p_ea)
4798 win_equal(curwin, FALSE, 'v');
4799#endif
4800}
4801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802/*
4803 * Called from win_new_shellsize() after Columns changed.
4804 */
4805 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004806shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807{
4808 if (firstwin == NULL) /* not initialized yet */
4809 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004810
4811 /* First try setting the widths of windows with 'winfixwidth'. If that
4812 * doesn't result in the right width, forget about that option. */
4813 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004814 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004815 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4818#if 0
4819 /* Disabled: don't want making the screen smaller make a window larger. */
4820 if (p_ea)
4821 win_equal(curwin, FALSE, 'h');
4822#endif
4823}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
4825#if defined(FEAT_CMDWIN) || defined(PROTO)
4826/*
4827 * Save the size of all windows in "gap".
4828 */
4829 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004830win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832{
4833 win_T *wp;
4834
4835 ga_init2(gap, (int)sizeof(int), 1);
4836 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004837 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 ((int *)gap->ga_data)[gap->ga_len++] =
4840 wp->w_width + wp->w_vsep_width;
4841 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4842 }
4843}
4844
4845/*
4846 * Restore window sizes, but only if the number of windows is still the same.
4847 * Does not free the growarray.
4848 */
4849 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004850win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851{
4852 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004853 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 if (win_count() * 2 == gap->ga_len)
4856 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004857 /* The order matters, because frames contain other frames, but it's
4858 * difficult to get right. The easy way out is to do it twice. */
4859 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004861 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004862 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02004863 {
4864 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4865 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 /* recompute the window positions */
4869 (void)win_comp_pos();
4870 }
4871}
4872#endif /* FEAT_CMDWIN */
4873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874/*
4875 * Update the position for all windows, using the width and height of the
4876 * frames.
4877 * Returns the row just after the last window.
4878 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004879 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004880win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004882 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 int col = 0;
4884
4885 frame_comp_pos(topframe, &row, &col);
4886 return row;
4887}
4888
4889/*
4890 * Update the position of the windows in frame "topfrp", using the width and
4891 * height of the frames.
4892 * "*row" and "*col" are the top-left position of the frame. They are updated
4893 * to the bottom-right position plus one.
4894 */
4895 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004896frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
4898 win_T *wp;
4899 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 int startcol;
4901 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01004902 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
4904 wp = topfrp->fr_win;
4905 if (wp != NULL)
4906 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004907 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 {
4909 /* position changed, redraw */
4910 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 redraw_win_later(wp, NOT_VALID);
4913 wp->w_redr_status = TRUE;
4914 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01004915 /* WinBar will not show if the window height is zero */
4916 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
4917 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920 else
4921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 startrow = *row;
4923 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 if (topfrp->fr_layout == FR_ROW)
4927 *row = startrow; /* all frames are at the same row */
4928 else
4929 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 frame_comp_pos(frp, row, col);
4931 }
4932 }
4933}
4934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935/*
4936 * Set current window height and take care of repositioning other windows to
4937 * fit around it.
4938 */
4939 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004940win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942 win_setheight_win(height, curwin);
4943}
4944
4945/*
4946 * Set the window height of window "win" and take care of repositioning other
4947 * windows to fit around it.
4948 */
4949 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004950win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 int row;
4953
4954 if (win == curwin)
4955 {
4956 /* Always keep current window at least one line high, even when
4957 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if (height < p_wmh)
4959 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 if (height == 0)
4961 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01004962 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 frame_setheight(win->w_frame, height + win->w_status_height);
4966
4967 /* recompute the window positions */
4968 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970 /*
4971 * If there is extra space created between the last window and the command
4972 * line, clear it.
4973 */
4974 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4975 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4976 cmdline_row = row;
4977 msg_row = row;
4978 msg_col = 0;
4979
4980 redraw_all_later(NOT_VALID);
4981}
4982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983/*
4984 * Set the height of a frame to "height" and take care that all frames and
4985 * windows inside it are resized. Also resize frames on the left and right if
4986 * the are in the same FR_ROW frame.
4987 *
4988 * Strategy:
4989 * If the frame is part of a FR_COL frame, try fitting the frame in that
4990 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4991 * go to containing frames to resize them and make room.
4992 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4993 * Check for the minimal height of the FR_ROW frame.
4994 * At the top level we can also use change the command line height.
4995 */
4996 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004997frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
4999 int room; /* total number of lines available */
5000 int take; /* number of lines taken from other windows */
5001 int room_cmdline; /* lines available from cmdline */
5002 int run;
5003 frame_T *frp;
5004 int h;
5005 int room_reserved;
5006
5007 /* If the height already is the desired value, nothing to do. */
5008 if (curfrp->fr_height == height)
5009 return;
5010
5011 if (curfrp->fr_parent == NULL)
5012 {
5013 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005014 if (height > ROWS_AVAIL)
5015 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 if (height > 0)
5017 frame_new_height(curfrp, height, FALSE, FALSE);
5018 }
5019 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5020 {
5021 /* Row of frames: Also need to resize frames left and right of this
5022 * one. First check for the minimal height of these. */
5023 h = frame_minheight(curfrp->fr_parent, NULL);
5024 if (height < h)
5025 height = h;
5026 frame_setheight(curfrp->fr_parent, height);
5027 }
5028 else
5029 {
5030 /*
5031 * Column of frames: try to change only frames in this column.
5032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 /*
5034 * Do this twice:
5035 * 1: compute room available, if it's not enough try resizing the
5036 * containing frame.
5037 * 2: compute the room available and adjust the height to it.
5038 * Try not to reduce the height of a window with 'winfixheight' set.
5039 */
5040 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
5042 room = 0;
5043 room_reserved = 0;
5044 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5045 frp = frp->fr_next)
5046 {
5047 if (frp != curfrp
5048 && frp->fr_win != NULL
5049 && frp->fr_win->w_p_wfh)
5050 room_reserved += frp->fr_height;
5051 room += frp->fr_height;
5052 if (frp != curfrp)
5053 room -= frame_minheight(frp, NULL);
5054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (curfrp->fr_width != Columns)
5056 room_cmdline = 0;
5057 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005060 + VISIBLE_HEIGHT(lastwin)
5061 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (room_cmdline < 0)
5063 room_cmdline = 0;
5064 }
5065
5066 if (height <= room + room_cmdline)
5067 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 if (height > room + room_cmdline)
5071 height = room + room_cmdline;
5072 break;
5073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 frame_setheight(curfrp->fr_parent, height
5075 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077
5078 /*
5079 * Compute the number of lines we will take from others frames (can be
5080 * negative!).
5081 */
5082 take = height - curfrp->fr_height;
5083
5084 /* If there is not enough room, also reduce the height of a window
5085 * with 'winfixheight' set. */
5086 if (height > room + room_cmdline - room_reserved)
5087 room_reserved = room + room_cmdline - height;
5088 /* If there is only a 'winfixheight' window and making the
5089 * window smaller, need to make the other window taller. */
5090 if (take < 0 && room - curfrp->fr_height < room_reserved)
5091 room_reserved = 0;
5092
5093 if (take > 0 && room_cmdline > 0)
5094 {
5095 /* use lines from cmdline first */
5096 if (take < room_cmdline)
5097 room_cmdline = take;
5098 take -= room_cmdline;
5099 topframe->fr_height += room_cmdline;
5100 }
5101
5102 /*
5103 * set the current frame to the new height
5104 */
5105 frame_new_height(curfrp, height, FALSE, FALSE);
5106
5107 /*
5108 * First take lines from the frames after the current frame. If
5109 * that is not enough, takes lines from frames above the current
5110 * frame.
5111 */
5112 for (run = 0; run < 2; ++run)
5113 {
5114 if (run == 0)
5115 frp = curfrp->fr_next; /* 1st run: start with next window */
5116 else
5117 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5118 while (frp != NULL && take != 0)
5119 {
5120 h = frame_minheight(frp, NULL);
5121 if (room_reserved > 0
5122 && frp->fr_win != NULL
5123 && frp->fr_win->w_p_wfh)
5124 {
5125 if (room_reserved >= frp->fr_height)
5126 room_reserved -= frp->fr_height;
5127 else
5128 {
5129 if (frp->fr_height - room_reserved > take)
5130 room_reserved = frp->fr_height - take;
5131 take -= frp->fr_height - room_reserved;
5132 frame_new_height(frp, room_reserved, FALSE, FALSE);
5133 room_reserved = 0;
5134 }
5135 }
5136 else
5137 {
5138 if (frp->fr_height - take < h)
5139 {
5140 take -= frp->fr_height - h;
5141 frame_new_height(frp, h, FALSE, FALSE);
5142 }
5143 else
5144 {
5145 frame_new_height(frp, frp->fr_height - take,
5146 FALSE, FALSE);
5147 take = 0;
5148 }
5149 }
5150 if (run == 0)
5151 frp = frp->fr_next;
5152 else
5153 frp = frp->fr_prev;
5154 }
5155 }
5156 }
5157}
5158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159/*
5160 * Set current window width and take care of repositioning other windows to
5161 * fit around it.
5162 */
5163 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005164win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165{
5166 win_setwidth_win(width, curwin);
5167}
5168
5169 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005170win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 /* Always keep current window at least one column wide, even when
5173 * 'winminwidth' is zero. */
5174 if (wp == curwin)
5175 {
5176 if (width < p_wmw)
5177 width = p_wmw;
5178 if (width == 0)
5179 width = 1;
5180 }
5181
5182 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5183
5184 /* recompute the window positions */
5185 (void)win_comp_pos();
5186
5187 redraw_all_later(NOT_VALID);
5188}
5189
5190/*
5191 * Set the width of a frame to "width" and take care that all frames and
5192 * windows inside it are resized. Also resize frames above and below if the
5193 * are in the same FR_ROW frame.
5194 *
5195 * Strategy is similar to frame_setheight().
5196 */
5197 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005198frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199{
5200 int room; /* total number of lines available */
5201 int take; /* number of lines taken from other windows */
5202 int run;
5203 frame_T *frp;
5204 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005205 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207 /* If the width already is the desired value, nothing to do. */
5208 if (curfrp->fr_width == width)
5209 return;
5210
5211 if (curfrp->fr_parent == NULL)
5212 /* topframe: can't change width */
5213 return;
5214
5215 if (curfrp->fr_parent->fr_layout == FR_COL)
5216 {
5217 /* Column of frames: Also need to resize frames above and below of
5218 * this one. First check for the minimal width of these. */
5219 w = frame_minwidth(curfrp->fr_parent, NULL);
5220 if (width < w)
5221 width = w;
5222 frame_setwidth(curfrp->fr_parent, width);
5223 }
5224 else
5225 {
5226 /*
5227 * Row of frames: try to change only frames in this row.
5228 *
5229 * Do this twice:
5230 * 1: compute room available, if it's not enough try resizing the
5231 * containing frame.
5232 * 2: compute the room available and adjust the width to it.
5233 */
5234 for (run = 1; run <= 2; ++run)
5235 {
5236 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005237 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5239 frp = frp->fr_next)
5240 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005241 if (frp != curfrp
5242 && frp->fr_win != NULL
5243 && frp->fr_win->w_p_wfw)
5244 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 room += frp->fr_width;
5246 if (frp != curfrp)
5247 room -= frame_minwidth(frp, NULL);
5248 }
5249
5250 if (width <= room)
5251 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005252 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
5254 if (width > room)
5255 width = room;
5256 break;
5257 }
5258 frame_setwidth(curfrp->fr_parent, width
5259 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5260 }
5261
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 /*
5263 * Compute the number of lines we will take from others frames (can be
5264 * negative!).
5265 */
5266 take = width - curfrp->fr_width;
5267
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005268 /* If there is not enough room, also reduce the width of a window
5269 * with 'winfixwidth' set. */
5270 if (width > room - room_reserved)
5271 room_reserved = room - width;
5272 /* If there is only a 'winfixwidth' window and making the
5273 * window smaller, need to make the other window narrower. */
5274 if (take < 0 && room - curfrp->fr_width < room_reserved)
5275 room_reserved = 0;
5276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 /*
5278 * set the current frame to the new width
5279 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005280 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281
5282 /*
5283 * First take lines from the frames right of the current frame. If
5284 * that is not enough, takes lines from frames left of the current
5285 * frame.
5286 */
5287 for (run = 0; run < 2; ++run)
5288 {
5289 if (run == 0)
5290 frp = curfrp->fr_next; /* 1st run: start with next window */
5291 else
5292 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5293 while (frp != NULL && take != 0)
5294 {
5295 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005296 if (room_reserved > 0
5297 && frp->fr_win != NULL
5298 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005300 if (room_reserved >= frp->fr_width)
5301 room_reserved -= frp->fr_width;
5302 else
5303 {
5304 if (frp->fr_width - room_reserved > take)
5305 room_reserved = frp->fr_width - take;
5306 take -= frp->fr_width - room_reserved;
5307 frame_new_width(frp, room_reserved, FALSE, FALSE);
5308 room_reserved = 0;
5309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 }
5311 else
5312 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005313 if (frp->fr_width - take < w)
5314 {
5315 take -= frp->fr_width - w;
5316 frame_new_width(frp, w, FALSE, FALSE);
5317 }
5318 else
5319 {
5320 frame_new_width(frp, frp->fr_width - take,
5321 FALSE, FALSE);
5322 take = 0;
5323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 }
5325 if (run == 0)
5326 frp = frp->fr_next;
5327 else
5328 frp = frp->fr_prev;
5329 }
5330 }
5331 }
5332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
5334/*
5335 * Check 'winminheight' for a valid value.
5336 */
5337 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005338win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
5340 int room;
5341 int first = TRUE;
5342 win_T *wp;
5343
5344 /* loop until there is a 'winminheight' that is possible */
5345 while (p_wmh > 0)
5346 {
5347 /* TODO: handle vertical splits */
5348 room = -p_wh;
Bram Moolenaar29323592016-07-24 22:04:11 +02005349 FOR_ALL_WINDOWS(wp)
Bram Moolenaar415a6932017-12-05 20:31:07 +01005350 room += VISIBLE_HEIGHT(wp) - p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 if (room >= 0)
5352 break;
5353 --p_wmh;
5354 if (first)
5355 {
5356 EMSG(_(e_noroom));
5357 first = FALSE;
5358 }
5359 }
5360}
5361
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005362#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364/*
5365 * Status line of dragwin is dragged "offset" lines down (negative is up).
5366 */
5367 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005368win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 frame_T *curfr;
5371 frame_T *fr;
5372 int room;
5373 int row;
5374 int up; /* if TRUE, drag status line up, otherwise down */
5375 int n;
5376
5377 fr = dragwin->w_frame;
5378 curfr = fr;
5379 if (fr != topframe) /* more than one window */
5380 {
5381 fr = fr->fr_parent;
5382 /* When the parent frame is not a column of frames, its parent should
5383 * be. */
5384 if (fr->fr_layout != FR_COL)
5385 {
5386 curfr = fr;
5387 if (fr != topframe) /* only a row of windows, may drag statusline */
5388 fr = fr->fr_parent;
5389 }
5390 }
5391
5392 /* If this is the last frame in a column, may want to resize the parent
5393 * frame instead (go two up to skip a row of frames). */
5394 while (curfr != topframe && curfr->fr_next == NULL)
5395 {
5396 if (fr != topframe)
5397 fr = fr->fr_parent;
5398 curfr = fr;
5399 if (fr != topframe)
5400 fr = fr->fr_parent;
5401 }
5402
5403 if (offset < 0) /* drag up */
5404 {
5405 up = TRUE;
5406 offset = -offset;
5407 /* sum up the room of the current frame and above it */
5408 if (fr == curfr)
5409 {
5410 /* only one window */
5411 room = fr->fr_height - frame_minheight(fr, NULL);
5412 }
5413 else
5414 {
5415 room = 0;
5416 for (fr = fr->fr_child; ; fr = fr->fr_next)
5417 {
5418 room += fr->fr_height - frame_minheight(fr, NULL);
5419 if (fr == curfr)
5420 break;
5421 }
5422 }
5423 fr = curfr->fr_next; /* put fr at frame that grows */
5424 }
5425 else /* drag down */
5426 {
5427 up = FALSE;
5428 /*
5429 * Only dragging the last status line can reduce p_ch.
5430 */
5431 room = Rows - cmdline_row;
5432 if (curfr->fr_next == NULL)
5433 room -= 1;
5434 else
5435 room -= p_ch;
5436 if (room < 0)
5437 room = 0;
5438 /* sum up the room of frames below of the current one */
5439 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5440 room += fr->fr_height - frame_minheight(fr, NULL);
5441 fr = curfr; /* put fr at window that grows */
5442 }
5443
5444 if (room < offset) /* Not enough room */
5445 offset = room; /* Move as far as we can */
5446 if (offset <= 0)
5447 return;
5448
5449 /*
5450 * Grow frame fr by "offset" lines.
5451 * Doesn't happen when dragging the last status line up.
5452 */
5453 if (fr != NULL)
5454 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5455
5456 if (up)
5457 fr = curfr; /* current frame gets smaller */
5458 else
5459 fr = curfr->fr_next; /* next frame gets smaller */
5460
5461 /*
5462 * Now make the other frames smaller.
5463 */
5464 while (fr != NULL && offset > 0)
5465 {
5466 n = frame_minheight(fr, NULL);
5467 if (fr->fr_height - offset <= n)
5468 {
5469 offset -= fr->fr_height - n;
5470 frame_new_height(fr, n, !up, FALSE);
5471 }
5472 else
5473 {
5474 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5475 break;
5476 }
5477 if (up)
5478 fr = fr->fr_prev;
5479 else
5480 fr = fr->fr_next;
5481 }
5482 row = win_comp_pos();
5483 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5484 cmdline_row = row;
5485 p_ch = Rows - cmdline_row;
5486 if (p_ch < 1)
5487 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005488 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005489 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 showmode();
5491}
5492
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493/*
5494 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5495 */
5496 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005497win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
5499 frame_T *curfr;
5500 frame_T *fr;
5501 int room;
5502 int left; /* if TRUE, drag separator line left, otherwise right */
5503 int n;
5504
5505 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005506 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 return;
5508 curfr = fr;
5509 fr = fr->fr_parent;
5510 /* When the parent frame is not a row of frames, its parent should be. */
5511 if (fr->fr_layout != FR_ROW)
5512 {
5513 if (fr == topframe) /* only a column of windows (cannot happen?) */
5514 return;
5515 curfr = fr;
5516 fr = fr->fr_parent;
5517 }
5518
5519 /* If this is the last frame in a row, may want to resize a parent
5520 * frame instead. */
5521 while (curfr->fr_next == NULL)
5522 {
5523 if (fr == topframe)
5524 break;
5525 curfr = fr;
5526 fr = fr->fr_parent;
5527 if (fr != topframe)
5528 {
5529 curfr = fr;
5530 fr = fr->fr_parent;
5531 }
5532 }
5533
5534 if (offset < 0) /* drag left */
5535 {
5536 left = TRUE;
5537 offset = -offset;
5538 /* sum up the room of the current frame and left of it */
5539 room = 0;
5540 for (fr = fr->fr_child; ; fr = fr->fr_next)
5541 {
5542 room += fr->fr_width - frame_minwidth(fr, NULL);
5543 if (fr == curfr)
5544 break;
5545 }
5546 fr = curfr->fr_next; /* put fr at frame that grows */
5547 }
5548 else /* drag right */
5549 {
5550 left = FALSE;
5551 /* sum up the room of frames right of the current one */
5552 room = 0;
5553 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5554 room += fr->fr_width - frame_minwidth(fr, NULL);
5555 fr = curfr; /* put fr at window that grows */
5556 }
5557
5558 if (room < offset) /* Not enough room */
5559 offset = room; /* Move as far as we can */
5560 if (offset <= 0) /* No room at all, quit. */
5561 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005562 if (fr == NULL)
5563 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
5565 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005566 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
5568 /* shrink other frames: current and at the left or at the right */
5569 if (left)
5570 fr = curfr; /* current frame gets smaller */
5571 else
5572 fr = curfr->fr_next; /* next frame gets smaller */
5573
5574 while (fr != NULL && offset > 0)
5575 {
5576 n = frame_minwidth(fr, NULL);
5577 if (fr->fr_width - offset <= n)
5578 {
5579 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005580 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582 else
5583 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005584 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 break;
5586 }
5587 if (left)
5588 fr = fr->fr_prev;
5589 else
5590 fr = fr->fr_next;
5591 }
5592 (void)win_comp_pos();
5593 redraw_all_later(NOT_VALID);
5594}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595#endif /* FEAT_MOUSE */
5596
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005597#define FRACTION_MULT 16384L
5598
5599/*
5600 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005601 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005602 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005603 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005604set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005605{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005606 if (wp->w_height > 1)
5607 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005608 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005609}
5610
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611/*
5612 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005613 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 * This takes care of the things inside the window, not what happens to the
5615 * window position, the frame or to other windows.
5616 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005617 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005618win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005620 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622 /* Don't want a negative height. Happens when splitting a tiny window.
5623 * Will equalize heights soon to fix it. */
5624 if (height < 0)
5625 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005626 if (wp->w_height == height)
5627 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005629 if (wp->w_height > 0)
5630 {
5631 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005632 /* w_wrow needs to be valid. When setting 'laststatus' this may
5633 * call win_new_height() recursively. */
5634 validate_cursor();
5635 if (wp->w_height != prev_height)
5636 return; /* Recursive call already changed the size, bail out here
5637 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005638 if (wp->w_wrow != wp->w_prev_fraction_row)
5639 set_fraction(wp);
5640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
5642 wp->w_height = height;
5643 wp->w_skipcol = 0;
5644
Bram Moolenaar955f1982017-02-05 15:10:51 +01005645 /* There is no point in adjusting the scroll position when exiting. Some
5646 * values might be invalid. */
5647 if (!exiting)
5648 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005649}
5650
5651 void
5652scroll_to_fraction(win_T *wp, int prev_height)
5653{
5654 linenr_T lnum;
5655 int sline, line_size;
5656 int height = wp->w_height;
5657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 /* Don't change w_topline when height is zero. Don't set w_topline when
5659 * 'scrollbind' is set and this isn't the current window. */
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005660 if (height > 0 && (!wp->w_p_scb || wp == curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005662 /*
5663 * Find a value for w_topline that shows the cursor at the same
5664 * relative position in the window as before (more or less).
5665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 lnum = wp->w_cursor.lnum;
5667 if (lnum < 1) /* can happen when starting up */
5668 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005669 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5670 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5672 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005673
5674 if (sline >= 0)
5675 {
5676 /* Make sure the whole cursor line is visible, if possible. */
5677 int rows = plines_win(wp, lnum, FALSE);
5678
5679 if (sline > wp->w_height - rows)
5680 {
5681 sline = wp->w_height - rows;
5682 wp->w_wrow -= rows - line_size;
5683 }
5684 }
5685
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 if (sline < 0)
5687 {
5688 /*
5689 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005690 * Make cursor line the first line in the window. If not enough
5691 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 */
5693 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005694 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005695 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005696 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005697 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005698 --wp->w_wrow;
5699 while (wp->w_wrow >= wp->w_height)
5700 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005701 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005702 + win_col_off2(wp);
5703 --wp->w_wrow;
5704 }
5705 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005706 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005708 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005710 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 {
5712#ifdef FEAT_FOLDING
5713 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5714 if (lnum == 1)
5715 {
5716 /* first line in buffer is folded */
5717 line_size = 1;
5718 --sline;
5719 break;
5720 }
5721#endif
5722 --lnum;
5723#ifdef FEAT_DIFF
5724 if (lnum == wp->w_topline)
5725 line_size = plines_win_nofill(wp, lnum, TRUE)
5726 + wp->w_topfill;
5727 else
5728#endif
5729 line_size = plines_win(wp, lnum, TRUE);
5730 sline -= line_size;
5731 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005732
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 if (sline < 0)
5734 {
5735 /*
5736 * Line we want at top would go off top of screen. Use next
5737 * line instead.
5738 */
5739#ifdef FEAT_FOLDING
5740 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5741#endif
5742 lnum++;
5743 wp->w_wrow -= line_size + sline;
5744 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005745 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 {
5747 /* First line of file reached, use that as topline. */
5748 lnum = 1;
5749 wp->w_wrow -= sline;
5750 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005751
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005752 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755
5756 if (wp == curwin)
5757 {
5758 if (p_so)
5759 update_topline();
5760 curs_columns(FALSE); /* validate w_wrow */
5761 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005762 if (prev_height > 0)
5763 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764
5765 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005766 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 invalidate_botline_win(wp);
5769}
5770
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771/*
5772 * Set the width of a window.
5773 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005774 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005775win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 wp->w_width = width;
5778 wp->w_lines_valid = 0;
5779 changed_line_abv_curs_win(wp);
5780 invalidate_botline_win(wp);
5781 if (wp == curwin)
5782 {
5783 update_topline();
5784 curs_columns(TRUE); /* validate w_wrow */
5785 }
5786 redraw_win_later(wp, NOT_VALID);
5787 wp->w_redr_status = TRUE;
5788}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789
5790 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005791win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5794 if (wp->w_p_scr == 0)
5795 wp->w_p_scr = 1;
5796}
5797
5798/*
5799 * command_height: called whenever p_ch has been changed
5800 */
5801 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005802command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 int h;
5805 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005806 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005808 /* Use the value of p_ch that we remembered. This is needed for when the
5809 * GUI starts up, we can't be sure in what order things happen. And when
5810 * p_ch was changed in another tab page. */
5811 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 /* Find bottom frame with width of screen. */
5814 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5816 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
5818 /* Avoid changing the height of a window with 'winfixheight' set. */
5819 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5820 && frp->fr_win->w_p_wfh)
5821 frp = frp->fr_prev;
5822
5823 if (starting != NO_SCREEN)
5824 {
5825 cmdline_row = Rows - p_ch;
5826
5827 if (p_ch > old_p_ch) /* p_ch got bigger */
5828 {
5829 while (p_ch > old_p_ch)
5830 {
5831 if (frp == NULL)
5832 {
5833 EMSG(_(e_noroom));
5834 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005835 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 cmdline_row = Rows - p_ch;
5837 break;
5838 }
5839 h = frp->fr_height - frame_minheight(frp, NULL);
5840 if (h > p_ch - old_p_ch)
5841 h = p_ch - old_p_ch;
5842 old_p_ch += h;
5843 frame_add_height(frp, -h);
5844 frp = frp->fr_prev;
5845 }
5846
5847 /* Recompute window positions. */
5848 (void)win_comp_pos();
5849
5850 /* clear the lines added to cmdline */
5851 if (full_screen)
5852 screen_fill((int)(cmdline_row), (int)Rows, 0,
5853 (int)Columns, ' ', ' ', 0);
5854 msg_row = cmdline_row;
5855 redraw_cmdline = TRUE;
5856 return;
5857 }
5858
5859 if (msg_row < cmdline_row)
5860 msg_row = cmdline_row;
5861 redraw_cmdline = TRUE;
5862 }
5863 frame_add_height(frp, (int)(old_p_ch - p_ch));
5864
5865 /* Recompute window positions. */
5866 if (frp != lastwin->w_frame)
5867 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868}
5869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870/*
5871 * Resize frame "frp" to be "n" lines higher (negative for less high).
5872 * Also resize the frames it is contained in.
5873 */
5874 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005875frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5878 for (;;)
5879 {
5880 frp = frp->fr_parent;
5881 if (frp == NULL)
5882 break;
5883 frp->fr_height += n;
5884 }
5885}
5886
5887/*
5888 * Add or remove a status line for the bottom window(s), according to the
5889 * value of 'laststatus'.
5890 */
5891 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005892last_status(
5893 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 /* Don't make a difference between horizontal or vertical split. */
5896 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01005897 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898}
5899
5900 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005901last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902{
5903 frame_T *fp;
5904 win_T *wp;
5905
5906 if (fr->fr_layout == FR_LEAF)
5907 {
5908 wp = fr->fr_win;
5909 if (wp->w_status_height != 0 && !statusline)
5910 {
5911 /* remove status line */
5912 win_new_height(wp, wp->w_height + 1);
5913 wp->w_status_height = 0;
5914 comp_col();
5915 }
5916 else if (wp->w_status_height == 0 && statusline)
5917 {
5918 /* Find a frame to take a line from. */
5919 fp = fr;
5920 while (fp->fr_height <= frame_minheight(fp, NULL))
5921 {
5922 if (fp == topframe)
5923 {
5924 EMSG(_(e_noroom));
5925 return;
5926 }
5927 /* In a column of frames: go to frame above. If already at
5928 * the top or in a row of frames: go to parent. */
5929 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5930 fp = fp->fr_prev;
5931 else
5932 fp = fp->fr_parent;
5933 }
5934 wp->w_status_height = 1;
5935 if (fp != fr)
5936 {
5937 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5938 frame_fix_height(wp);
5939 (void)win_comp_pos();
5940 }
5941 else
5942 win_new_height(wp, wp->w_height - 1);
5943 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005944 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 }
5946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 else if (fr->fr_layout == FR_ROW)
5948 {
5949 /* vertically split windows, set status line for each one */
5950 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5951 last_status_rec(fp, statusline);
5952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 else
5954 {
5955 /* horizontally split window, set status line for last one */
5956 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5957 ;
5958 last_status_rec(fp, statusline);
5959 }
5960}
5961
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005962/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005963 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005964 */
5965 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005966tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005967{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005968#ifdef FEAT_GUI_TABLINE
5969 /* When the GUI has the tabline then this always returns zero. */
5970 if (gui_use_tabline())
5971 return 0;
5972#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005973 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005974 {
5975 case 0: return 0;
5976 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5977 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005978 return 1;
5979}
5980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5982/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005983 * Get the file name at the cursor.
5984 * If Visual mode is active, use the selected text if it's in one line.
5985 * Returns the name in allocated memory, NULL for failure.
5986 */
5987 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005988grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005989{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005990 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
5991
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005992 if (VIsual_active)
5993 {
5994 int len;
5995 char_u *ptr;
5996
5997 if (get_visual_text(NULL, &ptr, &len) == FAIL)
5998 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005999 return find_file_name_in_path(ptr, len, options,
6000 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006001 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006002 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006003}
6004
6005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 * Return the file name under or after the cursor.
6007 *
6008 * The 'path' option is searched if the file name is not absolute.
6009 * The string returned has been alloc'ed and should be freed by the caller.
6010 * NULL is returned if the file name or file is not found.
6011 *
6012 * options:
6013 * FNAME_MESS give error messages
6014 * FNAME_EXP expand to path
6015 * FNAME_HYP check for hypertext link
6016 * FNAME_INCL apply "includeexpr"
6017 */
6018 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006019file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
6021 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006022 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6023 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024}
6025
6026/*
6027 * Return the name of the file under or after ptr[col].
6028 * Otherwise like file_name_at_cursor().
6029 */
6030 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006031file_name_in_line(
6032 char_u *line,
6033 int col,
6034 int options,
6035 long count,
6036 char_u *rel_fname, /* file we are searching relative to */
6037 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
6039 char_u *ptr;
6040 int len;
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006041 int in_type = TRUE;
6042 int is_url = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043
6044 /*
6045 * search forward for what could be the start of a file name
6046 */
6047 ptr = line + col;
6048 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006049 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (*ptr == NUL) /* nothing found */
6051 {
6052 if (options & FNAME_MESS)
6053 EMSG(_("E446: No file name under cursor"));
6054 return NULL;
6055 }
6056
6057 /*
6058 * Search backward for first char of the file name.
6059 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6060 */
6061 while (ptr > line)
6062 {
6063#ifdef FEAT_MBYTE
6064 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6065 ptr -= len + 1;
6066 else
6067#endif
6068 if (vim_isfilec(ptr[-1])
6069 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6070 --ptr;
6071 else
6072 break;
6073 }
6074
6075 /*
6076 * Search forward for the last char of the file name.
6077 * Also allow "://" when ':' is not in 'isfname'.
6078 */
6079 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006080 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006081 || ((options & FNAME_HYP) && path_is_url(ptr + len))
6082 || (is_url && vim_strchr((char_u *)"?&=", ptr[len]) != NULL))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006083 {
Bram Moolenaar9e3dfc62017-12-25 14:29:18 +01006084 /* After type:// we also include ?, & and = as valid characters, so that
6085 * http://google.com?q=this&that=ok works. */
6086 if ((ptr[len] >= 'A' && ptr[len] <= 'Z') || (ptr[len] >= 'a' && ptr[len] <= 'z'))
6087 {
6088 if (in_type && path_is_url(ptr + len + 1))
6089 is_url = TRUE;
6090 }
6091 else
6092 in_type = FALSE;
6093
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006094 if (ptr[len] == '\\')
6095 /* Skip over the "\" in "\ ". */
6096 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097#ifdef FEAT_MBYTE
6098 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006099 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 else
6101#endif
6102 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104
6105 /*
6106 * If there is trailing punctuation, remove it.
6107 * But don't remove "..", could be a directory name.
6108 */
6109 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6110 && ptr[len - 2] != '.')
6111 --len;
6112
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006113 if (file_lnum != NULL)
6114 {
6115 char_u *p;
6116
6117 /* Get the number after the file name and a separator character */
6118 p = ptr + len;
6119 p = skipwhite(p);
6120 if (*p != NUL)
6121 {
6122 if (!isdigit(*p))
6123 ++p; /* skip the separator */
6124 p = skipwhite(p);
6125 if (isdigit(*p))
6126 *file_lnum = (int)getdigits(&p);
6127 }
6128 }
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6131}
6132
6133# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006134static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
6136 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006137eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 char_u *res;
6140
6141 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006142 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006143 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 set_vim_var_string(VV_FNAME, NULL, 0);
6145 return res;
6146}
6147#endif
6148
6149/*
6150 * Return the name of the file ptr[len] in 'path'.
6151 * Otherwise like file_name_at_cursor().
6152 */
6153 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006154find_file_name_in_path(
6155 char_u *ptr,
6156 int len,
6157 int options,
6158 long count,
6159 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160{
6161 char_u *file_name;
6162 int c;
6163# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6164 char_u *tofree = NULL;
6165
6166 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6167 {
6168 tofree = eval_includeexpr(ptr, len);
6169 if (tofree != NULL)
6170 {
6171 ptr = tofree;
6172 len = (int)STRLEN(ptr);
6173 }
6174 }
6175# endif
6176
6177 if (options & FNAME_EXP)
6178 {
6179 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6180 TRUE, rel_fname);
6181
6182# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6183 /*
6184 * If the file could not be found in a normal way, try applying
6185 * 'includeexpr' (unless done already).
6186 */
6187 if (file_name == NULL
6188 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6189 {
6190 tofree = eval_includeexpr(ptr, len);
6191 if (tofree != NULL)
6192 {
6193 ptr = tofree;
6194 len = (int)STRLEN(ptr);
6195 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6196 TRUE, rel_fname);
6197 }
6198 }
6199# endif
6200 if (file_name == NULL && (options & FNAME_MESS))
6201 {
6202 c = ptr[len];
6203 ptr[len] = NUL;
6204 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6205 ptr[len] = c;
6206 }
6207
6208 /* Repeat finding the file "count" times. This matters when it
6209 * appears several times in the path. */
6210 while (file_name != NULL && --count > 0)
6211 {
6212 vim_free(file_name);
6213 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6214 }
6215 }
6216 else
6217 file_name = vim_strnsave(ptr, len);
6218
6219# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6220 vim_free(tofree);
6221# endif
6222
6223 return file_name;
6224}
6225#endif /* FEAT_SEARCHPATH */
6226
6227/*
6228 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6229 * Also check for ":\\", which MS Internet Explorer accepts, return
6230 * URL_BACKSLASH.
6231 */
6232 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006233path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
6235 if (STRNCMP(p, "://", (size_t)3) == 0)
6236 return URL_SLASH;
6237 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6238 return URL_BACKSLASH;
6239 return 0;
6240}
6241
6242/*
6243 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6244 * Return URL_BACKSLASH for "name:\\".
6245 * Return zero otherwise.
6246 */
6247 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006248path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 char_u *p;
6251
6252 for (p = fname; isalpha(*p); ++p)
6253 ;
6254 return path_is_url(p);
6255}
6256
6257/*
6258 * Return TRUE if "name" is a full (absolute) path name or URL.
6259 */
6260 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006261vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 return (path_with_url(name) != 0 || mch_isFullName(name));
6264}
6265
6266/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006267 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 *
6269 * return FAIL for failure, OK otherwise
6270 */
6271 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006272vim_FullName(
6273 char_u *fname,
6274 char_u *buf,
6275 int len,
6276 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 int retval = OK;
6279 int url;
6280
6281 *buf = NUL;
6282 if (fname == NULL)
6283 return FAIL;
6284
6285 url = path_with_url(fname);
6286 if (!url)
6287 retval = mch_FullName(fname, buf, len, force);
6288 if (url || retval == FAIL)
6289 {
6290 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006291 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 }
Bram Moolenaard0573012017-10-28 21:11:06 +02006293#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 slash_adjust(buf);
6295#endif
6296 return retval;
6297}
6298
6299/*
6300 * Return the minimal number of rows that is needed on the screen to display
6301 * the current number of windows.
6302 */
6303 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006304min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006307 tabpage_T *tp;
6308 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310 if (firstwin == NULL) /* not initialized yet */
6311 return MIN_LINES;
6312
Bram Moolenaarf740b292006-02-16 22:11:02 +00006313 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006314 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006315 {
6316 n = frame_minheight(tp->tp_topframe, NULL);
6317 if (total < n)
6318 total = n;
6319 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006320 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006321 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 return total;
6323}
6324
6325/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006326 * Return TRUE if there is only one window (in the current tab page), not
6327 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006328 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 */
6330 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006331only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 int count = 0;
6334 win_T *wp;
6335
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006336 /* If there is another tab page there always is another window. */
6337 if (first_tabpage->tp_next != NULL)
6338 return FALSE;
6339
Bram Moolenaar29323592016-07-24 22:04:11 +02006340 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006341 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006342 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343# ifdef FEAT_QUICKFIX
6344 || wp->w_p_pvw
6345# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006346 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 ++count;
6348 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349}
6350
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351/*
6352 * Correct the cursor line number in other windows. Used after changing the
6353 * current buffer, and before applying autocommands.
6354 * When "do_curwin" is TRUE, also check current window.
6355 */
6356 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006357check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006360 tabpage_T *tp;
6361
6362 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 {
6365 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6366 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6367 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6368 wp->w_topline = curbuf->b_ml.ml_line_count;
6369 }
6370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372/*
6373 * A snapshot of the window sizes, to restore them after closing the help
6374 * window.
6375 * Only these fields are used:
6376 * fr_layout
6377 * fr_width
6378 * fr_height
6379 * fr_next
6380 * fr_child
6381 * fr_win (only valid for the old curwin, NULL otherwise)
6382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384/*
6385 * Create a snapshot of the current frame sizes.
6386 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006387 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006388make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006390 clear_snapshot(curtab, idx);
6391 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392}
6393
6394 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006395make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6398 if (*frp == NULL)
6399 return;
6400 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 (*frp)->fr_height = fr->fr_height;
6403 if (fr->fr_next != NULL)
6404 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6405 if (fr->fr_child != NULL)
6406 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6407 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6408 (*frp)->fr_win = curwin;
6409}
6410
6411/*
6412 * Remove any existing snapshot.
6413 */
6414 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006415clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006417 clear_snapshot_rec(tp->tp_snapshot[idx]);
6418 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419}
6420
6421 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006422clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
6424 if (fr != NULL)
6425 {
6426 clear_snapshot_rec(fr->fr_next);
6427 clear_snapshot_rec(fr->fr_child);
6428 vim_free(fr);
6429 }
6430}
6431
6432/*
6433 * Restore a previously created snapshot, if there is any.
6434 * This is only done if the screen size didn't change and the window layout is
6435 * still the same.
6436 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006437 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006438restore_snapshot(
6439 int idx,
6440 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 win_T *wp;
6443
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006444 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006445 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006446 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6447 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006449 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 win_comp_pos();
6451 if (wp != NULL && close_curwin)
6452 win_goto(wp);
6453 redraw_all_later(CLEAR);
6454 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006455 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456}
6457
6458/*
6459 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006460 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 */
6462 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006463check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464{
6465 if (sn->fr_layout != fr->fr_layout
6466 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6467 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6468 || (sn->fr_next != NULL
6469 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6470 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006471 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006472 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 return FAIL;
6474 return OK;
6475}
6476
6477/*
6478 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6479 * following frames and children.
6480 * Returns a pointer to the old current window, or NULL.
6481 */
6482 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006483restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
6485 win_T *wp = NULL;
6486 win_T *wp2;
6487
6488 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (fr->fr_layout == FR_LEAF)
6491 {
6492 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006493 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 wp = sn->fr_win;
6495 }
6496 if (sn->fr_next != NULL)
6497 {
6498 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6499 if (wp2 != NULL)
6500 wp = wp2;
6501 }
6502 if (sn->fr_child != NULL)
6503 {
6504 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6505 if (wp2 != NULL)
6506 wp = wp2;
6507 }
6508 return wp;
6509}
6510
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006511#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6512 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006513/*
6514 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006515 * restore_win() MUST be called to undo, also when FAIL is returned.
6516 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006517 * When "no_display" is TRUE the display won't be affected, no redraw is
6518 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006519 * Returns FAIL if switching to "win" failed.
6520 */
6521 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006522switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006523 win_T **save_curwin,
6524 tabpage_T **save_curtab,
6525 win_T *win,
6526 tabpage_T *tp,
6527 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006528{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006529 block_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006530 *save_curwin = curwin;
6531 if (tp != NULL)
6532 {
6533 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006534 if (no_display)
6535 {
6536 curtab->tp_firstwin = firstwin;
6537 curtab->tp_lastwin = lastwin;
6538 curtab = tp;
6539 firstwin = curtab->tp_firstwin;
6540 lastwin = curtab->tp_lastwin;
6541 }
6542 else
6543 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006544 }
6545 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006546 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006547 curwin = win;
6548 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006549 return OK;
6550}
6551
6552/*
6553 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006554 * When "no_display" is TRUE the display won't be affected, no redraw is
6555 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006556 */
6557 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006558restore_win(
6559 win_T *save_curwin UNUSED,
6560 tabpage_T *save_curtab UNUSED,
6561 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006562{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006563 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006564 {
6565 if (no_display)
6566 {
6567 curtab->tp_firstwin = firstwin;
6568 curtab->tp_lastwin = lastwin;
6569 curtab = save_curtab;
6570 firstwin = curtab->tp_firstwin;
6571 lastwin = curtab->tp_lastwin;
6572 }
6573 else
6574 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6575 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006576 if (win_valid(save_curwin))
6577 {
6578 curwin = save_curwin;
6579 curbuf = curwin->w_buffer;
6580 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006581 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006582}
6583
6584/*
6585 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6586 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6587 */
6588 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006589switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006590{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006591 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006592 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006593 --curbuf->b_nwindows;
6594 curbuf = buf;
6595 curwin->w_buffer = buf;
6596 ++curbuf->b_nwindows;
6597}
6598
6599/*
6600 * Restore the current buffer after using switch_buffer().
6601 */
6602 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006603restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006604{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006605 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006606 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006607 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006608 {
6609 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006610 curwin->w_buffer = save_curbuf->br_buf;
6611 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006612 ++curbuf->b_nwindows;
6613 }
6614}
6615#endif
6616
Bram Moolenaar4033c552017-09-16 20:54:51 +02006617#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618/*
6619 * Return TRUE if there is any vertically split window.
6620 */
6621 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006622win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 frame_T *fr;
6625
6626 if (topframe->fr_layout == FR_ROW)
6627 return TRUE;
6628
6629 if (topframe->fr_layout == FR_COL)
6630 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6631 if (fr->fr_layout == FR_ROW)
6632 return TRUE;
6633
6634 return FALSE;
6635}
6636#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006637
6638#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6639/*
6640 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006641 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006642 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6643 * If no particular ID is desired, -1 must be specified for 'id'.
6644 * Return ID of added match, -1 on failure.
6645 */
6646 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006647match_add(
6648 win_T *wp,
6649 char_u *grp,
6650 char_u *pat,
6651 int prio,
6652 int id,
6653 list_T *pos_list,
6654 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006655{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006656 matchitem_T *cur;
6657 matchitem_T *prev;
6658 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006659 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006660 regprog_T *regprog = NULL;
6661 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006662
Bram Moolenaarb3414592014-06-17 17:48:32 +02006663 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006664 return -1;
6665 if (id < -1 || id == 0)
6666 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006667 EMSGN(_("E799: Invalid ID: %ld (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006668 return -1;
6669 }
6670 if (id != -1)
6671 {
6672 cur = wp->w_match_head;
6673 while (cur != NULL)
6674 {
6675 if (cur->id == id)
6676 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006677 EMSGN(_("E801: ID already taken: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006678 return -1;
6679 }
6680 cur = cur->next;
6681 }
6682 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006683 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006684 {
6685 EMSG2(_(e_nogroup), grp);
6686 return -1;
6687 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006688 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006689 {
6690 EMSG2(_(e_invarg2), pat);
6691 return -1;
6692 }
6693
6694 /* Find available match ID. */
6695 while (id == -1)
6696 {
6697 cur = wp->w_match_head;
6698 while (cur != NULL && cur->id != wp->w_next_match_id)
6699 cur = cur->next;
6700 if (cur == NULL)
6701 id = wp->w_next_match_id;
6702 wp->w_next_match_id++;
6703 }
6704
6705 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006706 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006707 m->id = id;
6708 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006709 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006710 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006711 m->match.regprog = regprog;
6712 m->match.rmm_ic = FALSE;
6713 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006714# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006715 m->conceal_char = 0;
6716 if (conceal_char != NULL)
6717 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006718# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006719
Bram Moolenaarb3414592014-06-17 17:48:32 +02006720 /* Set up position matches */
6721 if (pos_list != NULL)
6722 {
6723 linenr_T toplnum = 0;
6724 linenr_T botlnum = 0;
6725 listitem_T *li;
6726 int i;
6727
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006728 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006729 i++, li = li->li_next)
6730 {
6731 linenr_T lnum = 0;
6732 colnr_T col = 0;
6733 int len = 1;
6734 list_T *subl;
6735 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006736 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006737
Bram Moolenaarb3414592014-06-17 17:48:32 +02006738 if (li->li_tv.v_type == VAR_LIST)
6739 {
6740 subl = li->li_tv.vval.v_list;
6741 if (subl == NULL)
6742 goto fail;
6743 subli = subl->lv_first;
6744 if (subli == NULL)
6745 goto fail;
6746 lnum = get_tv_number_chk(&subli->li_tv, &error);
6747 if (error == TRUE)
6748 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006749 if (lnum == 0)
6750 {
6751 --i;
6752 continue;
6753 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006754 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006755 subli = subli->li_next;
6756 if (subli != NULL)
6757 {
6758 col = get_tv_number_chk(&subli->li_tv, &error);
6759 if (error == TRUE)
6760 goto fail;
6761 subli = subli->li_next;
6762 if (subli != NULL)
6763 {
6764 len = get_tv_number_chk(&subli->li_tv, &error);
6765 if (error == TRUE)
6766 goto fail;
6767 }
6768 }
6769 m->pos.pos[i].col = col;
6770 m->pos.pos[i].len = len;
6771 }
6772 else if (li->li_tv.v_type == VAR_NUMBER)
6773 {
6774 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006775 {
6776 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006777 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006778 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006779 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6780 m->pos.pos[i].col = 0;
6781 m->pos.pos[i].len = 0;
6782 }
6783 else
6784 {
6785 EMSG(_("List or number required"));
6786 goto fail;
6787 }
6788 if (toplnum == 0 || lnum < toplnum)
6789 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006790 if (botlnum == 0 || lnum >= botlnum)
6791 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006792 }
6793
6794 /* Calculate top and bottom lines for redrawing area */
6795 if (toplnum != 0)
6796 {
6797 if (wp->w_buffer->b_mod_set)
6798 {
6799 if (wp->w_buffer->b_mod_top > toplnum)
6800 wp->w_buffer->b_mod_top = toplnum;
6801 if (wp->w_buffer->b_mod_bot < botlnum)
6802 wp->w_buffer->b_mod_bot = botlnum;
6803 }
6804 else
6805 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006806 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006807 wp->w_buffer->b_mod_top = toplnum;
6808 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006809 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006810 }
6811 m->pos.toplnum = toplnum;
6812 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006813 rtype = VALID;
6814 }
6815 }
6816
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006817 /* Insert new match. The match list is in ascending order with regard to
6818 * the match priorities. */
6819 cur = wp->w_match_head;
6820 prev = cur;
6821 while (cur != NULL && prio >= cur->priority)
6822 {
6823 prev = cur;
6824 cur = cur->next;
6825 }
6826 if (cur == prev)
6827 wp->w_match_head = m;
6828 else
6829 prev->next = m;
6830 m->next = cur;
6831
Bram Moolenaarb3414592014-06-17 17:48:32 +02006832 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006833 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006834
6835fail:
6836 vim_free(m);
6837 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006838}
6839
6840/*
6841 * Delete match with ID 'id' in the match list of window 'wp'.
6842 * Print error messages if 'perr' is TRUE.
6843 */
6844 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006845match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006846{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006847 matchitem_T *cur = wp->w_match_head;
6848 matchitem_T *prev = cur;
6849 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006850
6851 if (id < 1)
6852 {
6853 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006854 EMSGN(_("E802: Invalid ID: %ld (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006855 id);
6856 return -1;
6857 }
6858 while (cur != NULL && cur->id != id)
6859 {
6860 prev = cur;
6861 cur = cur->next;
6862 }
6863 if (cur == NULL)
6864 {
6865 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006866 EMSGN(_("E803: ID not found: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006867 return -1;
6868 }
6869 if (cur == prev)
6870 wp->w_match_head = cur->next;
6871 else
6872 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006873 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006874 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006875 if (cur->pos.toplnum != 0)
6876 {
6877 if (wp->w_buffer->b_mod_set)
6878 {
6879 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6880 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6881 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6882 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6883 }
6884 else
6885 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006886 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006887 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6888 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006889 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006890 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006891 rtype = VALID;
6892 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006893 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006894 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006895 return 0;
6896}
6897
6898/*
6899 * Delete all matches in the match list of window 'wp'.
6900 */
6901 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006902clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006903{
6904 matchitem_T *m;
6905
6906 while (wp->w_match_head != NULL)
6907 {
6908 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006909 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006910 vim_free(wp->w_match_head->pattern);
6911 vim_free(wp->w_match_head);
6912 wp->w_match_head = m;
6913 }
6914 redraw_later(SOME_VALID);
6915}
6916
6917/*
6918 * Get match from ID 'id' in window 'wp'.
6919 * Return NULL if match not found.
6920 */
6921 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006922get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006923{
6924 matchitem_T *cur = wp->w_match_head;
6925
6926 while (cur != NULL && cur->id != id)
6927 cur = cur->next;
6928 return cur;
6929}
6930#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006931
6932#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6933 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006934get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006935{
6936 int i = 1;
6937 win_T *w;
6938
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006939 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006940 ++i;
6941
6942 if (w == NULL)
6943 return 0;
6944 else
6945 return i;
6946}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006947
6948 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006949get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006950{
6951 int i = 1;
6952 tabpage_T *t;
6953
6954 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6955 ++i;
6956
6957 if (t == NULL)
6958 return 0;
6959 else
6960 return i;
6961}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006962#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006963
6964/*
6965 * Return TRUE if "topfrp" and its children are at the right height.
6966 */
6967 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006968frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006969{
6970 frame_T *frp;
6971
6972 if (topfrp->fr_height != height)
6973 return FALSE;
6974
6975 if (topfrp->fr_layout == FR_ROW)
6976 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6977 if (frp->fr_height != height)
6978 return FALSE;
6979
6980 return TRUE;
6981}
6982
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006983/*
6984 * Return TRUE if "topfrp" and its children are at the right width.
6985 */
6986 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006987frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006988{
6989 frame_T *frp;
6990
6991 if (topfrp->fr_width != width)
6992 return FALSE;
6993
6994 if (topfrp->fr_layout == FR_COL)
6995 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
6996 if (frp->fr_width != width)
6997 return FALSE;
6998
6999 return TRUE;
7000}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007001
Bram Moolenaar86edef62016-03-13 18:07:30 +01007002#if defined(FEAT_EVAL) || defined(PROTO)
7003 int
7004win_getid(typval_T *argvars)
7005{
7006 int winnr;
7007 win_T *wp;
7008
7009 if (argvars[0].v_type == VAR_UNKNOWN)
7010 return curwin->w_id;
7011 winnr = get_tv_number(&argvars[0]);
7012 if (winnr > 0)
7013 {
7014 if (argvars[1].v_type == VAR_UNKNOWN)
7015 wp = firstwin;
7016 else
7017 {
7018 tabpage_T *tp;
7019 int tabnr = get_tv_number(&argvars[1]);
7020
Bram Moolenaar29323592016-07-24 22:04:11 +02007021 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007022 if (--tabnr == 0)
7023 break;
7024 if (tp == NULL)
7025 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01007026 if (tp == curtab)
7027 wp = firstwin;
7028 else
7029 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01007030 }
7031 for ( ; wp != NULL; wp = wp->w_next)
7032 if (--winnr == 0)
7033 return wp->w_id;
7034 }
7035 return 0;
7036}
7037
7038 int
7039win_gotoid(typval_T *argvars)
7040{
7041 win_T *wp;
7042 tabpage_T *tp;
7043 int id = get_tv_number(&argvars[0]);
7044
Bram Moolenaar29323592016-07-24 22:04:11 +02007045 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007046 if (wp->w_id == id)
7047 {
7048 goto_tabpage_win(tp, wp);
7049 return 1;
7050 }
7051 return 0;
7052}
7053
7054 void
7055win_id2tabwin(typval_T *argvars, list_T *list)
7056{
7057 win_T *wp;
7058 tabpage_T *tp;
7059 int winnr = 1;
7060 int tabnr = 1;
7061 int id = get_tv_number(&argvars[0]);
7062
Bram Moolenaar29323592016-07-24 22:04:11 +02007063 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007064 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007065 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007066 {
7067 if (wp->w_id == id)
7068 {
7069 list_append_number(list, tabnr);
7070 list_append_number(list, winnr);
7071 return;
7072 }
7073 ++winnr;
7074 }
7075 ++tabnr;
7076 winnr = 1;
7077 }
7078 list_append_number(list, 0);
7079 list_append_number(list, 0);
7080}
7081
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007082 win_T *
7083win_id2wp(typval_T *argvars)
7084{
7085 win_T *wp;
7086 tabpage_T *tp;
7087 int id = get_tv_number(&argvars[0]);
7088
7089 FOR_ALL_TAB_WINDOWS(tp, wp)
7090 if (wp->w_id == id)
7091 return wp;
7092
7093 return NULL;
7094}
7095
Bram Moolenaar86edef62016-03-13 18:07:30 +01007096 int
7097win_id2win(typval_T *argvars)
7098{
7099 win_T *wp;
7100 int nr = 1;
7101 int id = get_tv_number(&argvars[0]);
7102
Bram Moolenaar29323592016-07-24 22:04:11 +02007103 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007104 {
7105 if (wp->w_id == id)
7106 return nr;
7107 ++nr;
7108 }
7109 return 0;
7110}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007111
7112 void
7113win_findbuf(typval_T *argvars, list_T *list)
7114{
7115 win_T *wp;
7116 tabpage_T *tp;
7117 int bufnr = get_tv_number(&argvars[0]);
7118
Bram Moolenaar29323592016-07-24 22:04:11 +02007119 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007120 if (wp->w_buffer->b_fnum == bufnr)
7121 list_append_number(list, wp->w_id);
7122}
7123
Bram Moolenaar86edef62016-03-13 18:07:30 +01007124#endif