blob: c6a6d2b84fe800009dcf66622893569801502370 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read a list of people who contributed.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10#include "vim.h"
11
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010012static int path_is_url(char_u *p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010014static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
15static void win_init(win_T *newp, win_T *oldp, int flags);
16static void win_init_some(win_T *newp, win_T *oldp);
17static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
18static void frame_setheight(frame_T *curfrp, int height);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef FEAT_VERTSPLIT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010020static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010022static void win_exchange(long);
23static void win_rotate(int, int);
24static void win_totop(int size, int flags);
25static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
26static int last_window(void);
27static int close_last_window_tabpage(win_T *win, int free_buf, tabpage_T *prev_curtab);
28static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
29static frame_T *win_altframe(win_T *win, tabpage_T *tp);
30static tabpage_T *alt_tabpage(void);
31static win_T *frame2win(frame_T *frp);
32static int frame_has_win(frame_T *frp, win_T *wp);
33static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
34static int frame_fixed_height(frame_T *frp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000035#ifdef FEAT_VERTSPLIT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010036static int frame_fixed_width(frame_T *frp);
37static void frame_add_statusline(frame_T *frp);
38static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
39static void frame_add_vsep(frame_T *frp);
40static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
41static void frame_fix_width(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000043#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010044static int win_alloc_firstwin(win_T *oldwin);
45static void new_frame(win_T *wp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000046#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static tabpage_T *alloc_tabpage(void);
48static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
49static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
50static void frame_fix_height(win_T *wp);
51static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
52static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_enter_autocmds, int trigger_leave_autocmds);
53static void win_free(win_T *wp, tabpage_T *tp);
54static void frame_append(frame_T *after, frame_T *frp);
55static void frame_insert(frame_T *before, frame_T *frp);
56static void frame_remove(frame_T *frp);
Bram Moolenaarf0327f62013-06-28 20:16:55 +020057# ifdef FEAT_VERTSPLIT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void win_goto_ver(int up, long count);
59static void win_goto_hor(int left, long count);
Bram Moolenaarf0327f62013-06-28 20:16:55 +020060# endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static void frame_add_height(frame_T *frp, int n);
62static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static void make_snapshot_rec(frame_T *fr, frame_T **frp);
65static void clear_snapshot(tabpage_T *tp, int idx);
66static void clear_snapshot_rec(frame_T *fr);
67static int check_snapshot_rec(frame_T *sn, frame_T *fr);
68static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010070static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020071#ifdef FEAT_VERTSPLIT
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010072static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020073#endif
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000076
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010077static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
79#define URL_SLASH 1 /* path_is_url() has found "://" */
80#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
81
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000082#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083
Bram Moolenaar05159a02005-02-26 23:04:13 +000084#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000085# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000086#else
87# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000088#endif
89
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000091
92static char *m_onlyone = N_("Already only one window");
93
Bram Moolenaar071d4272004-06-13 20:20:40 +000094/*
95 * all CTRL-W window commands are handled here, called from normal_cmd().
96 */
97 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010098do_window(
99 int nchar,
100 long Prenum,
101 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102{
103 long Prenum1;
104 win_T *wp;
105#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
106 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000107 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_FIND_ID
110 int type = FIND_DEFINE;
111 int len;
112#endif
113 char_u cbuf[40];
114
115 if (Prenum == 0)
116 Prenum1 = 1;
117 else
118 Prenum1 = Prenum;
119
120#ifdef FEAT_CMDWIN
121# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
122#else
123# define CHECK_CMDWIN
124#endif
125
126 switch (nchar)
127 {
128/* split current window in two parts, horizontally */
129 case 'S':
130 case Ctrl_S:
131 case 's':
132 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000134#ifdef FEAT_QUICKFIX
135 /* When splitting the quickfix window open a new buffer in it,
136 * don't replicate the quickfix buffer. */
137 if (bt_quickfix(curbuf))
138 goto newwindow;
139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#ifdef FEAT_GUI
141 need_mouse_correct = TRUE;
142#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200143 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 break;
145
146#ifdef FEAT_VERTSPLIT
147/* split current window in two parts, vertically */
148 case Ctrl_V:
149 case 'v':
150 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000152# ifdef FEAT_QUICKFIX
153 /* When splitting the quickfix window open a new buffer in it,
154 * don't replicate the quickfix buffer. */
155 if (bt_quickfix(curbuf))
156 goto newwindow;
157# endif
158# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 need_mouse_correct = TRUE;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000160# endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200161 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 break;
163#endif
164
165/* split current window and edit alternate file */
166 case Ctrl_HAT:
167 case '^':
168 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100170 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 do_cmdline_cmd(cbuf);
172 break;
173
174/* open new window */
175 case Ctrl_N:
176 case 'n':
177 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000179#ifdef FEAT_QUICKFIX
180newwindow:
181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000183 /* window height */
184 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 else
186 cbuf[0] = NUL;
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000187#if defined(FEAT_VERTSPLIT) && defined(FEAT_QUICKFIX)
188 if (nchar == 'v' || nchar == Ctrl_V)
189 STRCAT(cbuf, "v");
190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 STRCAT(cbuf, "new");
192 do_cmdline_cmd(cbuf);
193 break;
194
195/* quit current window */
196 case Ctrl_Q:
197 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100199 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100200 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 break;
202
203/* close current window */
204 case Ctrl_C:
205 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100207 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100208 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 break;
210
211#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
212/* close preview window */
213 case Ctrl_Z:
214 case 'z':
215 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 do_cmdline_cmd((char_u *)"pclose");
218 break;
219
220/* cursor to preview window */
221 case 'P':
222 for (wp = firstwin; wp != NULL; wp = wp->w_next)
223 if (wp->w_p_pvw)
224 break;
225 if (wp == NULL)
226 EMSG(_("E441: There is no preview window"));
227 else
228 win_goto(wp);
229 break;
230#endif
231
232/* close all but current window */
233 case Ctrl_O:
234 case 'o':
235 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100237 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100238 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 break;
240
241/* cursor to next window with wrap around */
242 case Ctrl_W:
243 case 'w':
244/* cursor to previous window with wrap around */
245 case 'W':
246 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000247 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 beep_flush();
249 else
250 {
251 if (Prenum) /* go to specified window */
252 {
253 for (wp = firstwin; --Prenum > 0; )
254 {
255 if (wp->w_next == NULL)
256 break;
257 else
258 wp = wp->w_next;
259 }
260 }
261 else
262 {
263 if (nchar == 'W') /* go to previous window */
264 {
265 wp = curwin->w_prev;
266 if (wp == NULL)
267 wp = lastwin; /* wrap around */
268 }
269 else /* go to next window */
270 {
271 wp = curwin->w_next;
272 if (wp == NULL)
273 wp = firstwin; /* wrap around */
274 }
275 }
276 win_goto(wp);
277 }
278 break;
279
280/* cursor to window below */
281 case 'j':
282 case K_DOWN:
283 case Ctrl_J:
284 CHECK_CMDWIN
285#ifdef FEAT_VERTSPLIT
286 win_goto_ver(FALSE, Prenum1);
287#else
288 for (wp = curwin; wp->w_next != NULL && Prenum1-- > 0;
289 wp = wp->w_next)
290 ;
291 win_goto(wp);
292#endif
293 break;
294
295/* cursor to window above */
296 case 'k':
297 case K_UP:
298 case Ctrl_K:
299 CHECK_CMDWIN
300#ifdef FEAT_VERTSPLIT
301 win_goto_ver(TRUE, Prenum1);
302#else
303 for (wp = curwin; wp->w_prev != NULL && Prenum1-- > 0;
304 wp = wp->w_prev)
305 ;
306 win_goto(wp);
307#endif
308 break;
309
310#ifdef FEAT_VERTSPLIT
311/* cursor to left window */
312 case 'h':
313 case K_LEFT:
314 case Ctrl_H:
315 case K_BS:
316 CHECK_CMDWIN
317 win_goto_hor(TRUE, Prenum1);
318 break;
319
320/* cursor to right window */
321 case 'l':
322 case K_RIGHT:
323 case Ctrl_L:
324 CHECK_CMDWIN
325 win_goto_hor(FALSE, Prenum1);
326 break;
327#endif
328
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000329/* move window to new tab page */
330 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000331 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000332 MSG(_(m_onlyone));
333 else
334 {
335 tabpage_T *oldtab = curtab;
336 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000337
338 /* First create a new tab with the window, then go back to
339 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000340 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341 if (win_new_tabpage((int)Prenum) == OK
342 && valid_tabpage(oldtab))
343 {
344 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200345 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000346 if (curwin == wp)
347 win_close(curwin, FALSE);
348 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200349 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000350 }
351 }
352 break;
353
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354/* cursor to top-left window */
355 case 't':
356 case Ctrl_T:
357 win_goto(firstwin);
358 break;
359
360/* cursor to bottom-right window */
361 case 'b':
362 case Ctrl_B:
363 win_goto(lastwin);
364 break;
365
366/* cursor to last accessed (previous) window */
367 case 'p':
368 case Ctrl_P:
369 if (prevwin == NULL)
370 beep_flush();
371 else
372 win_goto(prevwin);
373 break;
374
375/* exchange current and next window */
376 case 'x':
377 case Ctrl_X:
378 CHECK_CMDWIN
379 win_exchange(Prenum);
380 break;
381
382/* rotate windows downwards */
383 case Ctrl_R:
384 case 'r':
385 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 win_rotate(FALSE, (int)Prenum1); /* downwards */
388 break;
389
390/* rotate windows upwards */
391 case 'R':
392 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 win_rotate(TRUE, (int)Prenum1); /* upwards */
395 break;
396
397/* move window to the very top/bottom/left/right */
398 case 'K':
399 case 'J':
400#ifdef FEAT_VERTSPLIT
401 case 'H':
402 case 'L':
403#endif
404 CHECK_CMDWIN
405 win_totop((int)Prenum,
406 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
407 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
408 break;
409
410/* make all windows the same height */
411 case '=':
412#ifdef FEAT_GUI
413 need_mouse_correct = TRUE;
414#endif
415 win_equal(NULL, FALSE, 'b');
416 break;
417
418/* increase current window height */
419 case '+':
420#ifdef FEAT_GUI
421 need_mouse_correct = TRUE;
422#endif
423 win_setheight(curwin->w_height + (int)Prenum1);
424 break;
425
426/* decrease current window height */
427 case '-':
428#ifdef FEAT_GUI
429 need_mouse_correct = TRUE;
430#endif
431 win_setheight(curwin->w_height - (int)Prenum1);
432 break;
433
434/* set current window height */
435 case Ctrl__:
436 case '_':
437#ifdef FEAT_GUI
438 need_mouse_correct = TRUE;
439#endif
440 win_setheight(Prenum ? (int)Prenum : 9999);
441 break;
442
443#ifdef FEAT_VERTSPLIT
444/* increase current window width */
445 case '>':
446#ifdef FEAT_GUI
447 need_mouse_correct = TRUE;
448#endif
449 win_setwidth(curwin->w_width + (int)Prenum1);
450 break;
451
452/* decrease current window width */
453 case '<':
454#ifdef FEAT_GUI
455 need_mouse_correct = TRUE;
456#endif
457 win_setwidth(curwin->w_width - (int)Prenum1);
458 break;
459
460/* set current window width */
461 case '|':
462#ifdef FEAT_GUI
463 need_mouse_correct = TRUE;
464#endif
465 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
466 break;
467#endif
468
469/* jump to tag and split window if tag exists (in preview window) */
470#if defined(FEAT_QUICKFIX)
471 case '}':
472 CHECK_CMDWIN
473 if (Prenum)
474 g_do_tagpreview = Prenum;
475 else
476 g_do_tagpreview = p_pvh;
477 /*FALLTHROUGH*/
478#endif
479 case ']':
480 case Ctrl_RSB:
481 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200482 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 if (Prenum)
484 postponed_split = Prenum;
485 else
486 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200487#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200488 if (nchar != '}')
489 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
Bram Moolenaard355c502014-09-23 13:48:43 +0200492 /* Execute the command right here, required when "wincmd ]"
493 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 do_nv_ident(Ctrl_RSB, NUL);
495 break;
496
497#ifdef FEAT_SEARCHPATH
498/* edit file name under cursor in a new window */
499 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000500 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000502wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000504
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000505 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 if (ptr != NULL)
507 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000508# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 setpcmark();
512 if (win_split(0, 0) == OK)
513 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200514 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000515 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
516 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000517 if (nchar == 'F' && lnum >= 0)
518 {
519 curwin->w_cursor.lnum = lnum;
520 check_cursor_lnum();
521 beginline(BL_SOL | BL_FIX);
522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 }
524 vim_free(ptr);
525 }
526 break;
527#endif
528
529#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000530/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 * new window -- webb
532 */
533 case 'i': /* Go to any match */
534 case Ctrl_I:
535 type = FIND_ANY;
536 /* FALLTHROUGH */
537 case 'd': /* Go to definition, using 'define' */
538 case Ctrl_D:
539 CHECK_CMDWIN
540 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
541 break;
542 find_pattern_in_path(ptr, 0, len, TRUE,
543 Prenum == 0 ? TRUE : FALSE, type,
544 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
545 curwin->w_set_curswant = TRUE;
546 break;
547#endif
548
Bram Moolenaar05159a02005-02-26 23:04:13 +0000549 case K_KENTER:
550 case CAR:
551#if defined(FEAT_QUICKFIX)
552 /*
553 * In a quickfix window a <CR> jumps to the error under the
554 * cursor in a new window.
555 */
556 if (bt_quickfix(curbuf))
557 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000558 sprintf((char *)cbuf, "split +%ld%s",
559 (long)curwin->w_cursor.lnum,
560 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000561 do_cmdline_cmd(cbuf);
562 }
563#endif
564 break;
565
566
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567/* CTRL-W g extended commands */
568 case 'g':
569 case Ctrl_G:
570 CHECK_CMDWIN
571#ifdef USE_ON_FLY_SCROLL
572 dont_scroll = TRUE; /* disallow scrolling here */
573#endif
574 ++no_mapping;
575 ++allow_keys; /* no mapping for xchar, but allow key codes */
576 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000577 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 --no_mapping;
580 --allow_keys;
581#ifdef FEAT_CMDL_INFO
582 (void)add_to_showcmd(xchar);
583#endif
584 switch (xchar)
585 {
586#if defined(FEAT_QUICKFIX)
587 case '}':
588 xchar = Ctrl_RSB;
589 if (Prenum)
590 g_do_tagpreview = Prenum;
591 else
592 g_do_tagpreview = p_pvh;
593 /*FALLTHROUGH*/
594#endif
595 case ']':
596 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200597 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 if (Prenum)
599 postponed_split = Prenum;
600 else
601 postponed_split = -1;
602
603 /* Execute the command right here, required when
604 * "wincmd g}" was used in a function. */
605 do_nv_ident('g', xchar);
606 break;
607
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000608#ifdef FEAT_SEARCHPATH
609 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000610 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100611 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000612 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000613 goto wingotofile;
614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 default:
616 beep_flush();
617 break;
618 }
619 break;
620
621 default: beep_flush();
622 break;
623 }
624}
625
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100626/*
627 * Figure out the address type for ":wnncmd".
628 */
629 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100630get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100631{
632 switch (*arg)
633 {
634 case 'S':
635 case Ctrl_S:
636 case 's':
637 case Ctrl_N:
638 case 'n':
639 case 'j':
640 case Ctrl_J:
641 case 'k':
642 case Ctrl_K:
643 case 'T':
644 case Ctrl_R:
645 case 'r':
646 case 'R':
647 case 'K':
648 case 'J':
649 case '+':
650 case '-':
651 case Ctrl__:
652 case '_':
653 case '|':
654 case ']':
655 case Ctrl_RSB:
656 case 'g':
657 case Ctrl_G:
658#ifdef FEAT_VERTSPLIT
659 case Ctrl_V:
660 case 'v':
661 case 'h':
662 case Ctrl_H:
663 case 'l':
664 case Ctrl_L:
665 case 'H':
666 case 'L':
667 case '>':
668 case '<':
669#endif
670#if defined(FEAT_QUICKFIX)
671 case '}':
672#endif
673#ifdef FEAT_SEARCHPATH
674 case 'f':
675 case 'F':
676 case Ctrl_F:
677#endif
678#ifdef FEAT_FIND_ID
679 case 'i':
680 case Ctrl_I:
681 case 'd':
682 case Ctrl_D:
683#endif
684 /* window size or any count */
685 eap->addr_type = ADDR_LINES;
686 break;
687
688 case Ctrl_HAT:
689 case '^':
690 /* buffer number */
691 eap->addr_type = ADDR_BUFFERS;
692 break;
693
694 case Ctrl_Q:
695 case 'q':
696 case Ctrl_C:
697 case 'c':
698 case Ctrl_O:
699 case 'o':
700 case Ctrl_W:
701 case 'w':
702 case 'W':
703 case 'x':
704 case Ctrl_X:
705 /* window number */
706 eap->addr_type = ADDR_WINDOWS;
707 break;
708
709#if defined(FEAT_QUICKFIX)
710 case Ctrl_Z:
711 case 'z':
712 case 'P':
713#endif
714 case 't':
715 case Ctrl_T:
716 case 'b':
717 case Ctrl_B:
718 case 'p':
719 case Ctrl_P:
720 case '=':
721 case CAR:
722 /* no count */
723 eap->addr_type = 0;
724 break;
725 }
726}
727
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100728 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100729cmd_with_count(
730 char *cmd,
731 char_u *bufp,
732 size_t bufsize,
733 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100734{
735 size_t len = STRLEN(cmd);
736
737 STRCPY(bufp, cmd);
738 if (Prenum > 0)
739 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
740}
741
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742/*
743 * split the current window, implements CTRL-W s and :split
744 *
745 * "size" is the height or width for the new window, 0 to use half of current
746 * height or width.
747 *
748 * "flags":
749 * WSP_ROOM: require enough room for new window
750 * WSP_VERT: vertical split.
751 * WSP_TOP: open window at the top-left of the shell (help window).
752 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
753 * WSP_HELP: creating the help window, keep layout snapshot
754 *
755 * return FAIL for failure, OK otherwise
756 */
757 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100758win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000760 /* When the ":tab" modifier was used open a new tab page instead. */
761 if (may_open_tabpage() == OK)
762 return OK;
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 /* Add flags from ":vertical", ":topleft" and ":botright". */
765 flags |= cmdmod.split;
766 if ((flags & WSP_TOP) && (flags & WSP_BOT))
767 {
768 EMSG(_("E442: Can't split topleft and botright at the same time"));
769 return FAIL;
770 }
771
772 /* When creating the help window make a snapshot of the window layout.
773 * Otherwise clear the snapshot, it's now invalid. */
774 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000775 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000777 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
779 return win_split_ins(size, flags, NULL, 0);
780}
781
782/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100783 * When "new_wp" is NULL: split the current window in two.
784 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 * top/left/right/bottom.
786 * return FAIL for failure, OK otherwise
787 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000788 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100789win_split_ins(
790 int size,
791 int flags,
792 win_T *new_wp,
793 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100795 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 win_T *oldwin;
797 int new_size = size;
798 int i;
799 int need_status = 0;
800 int do_equal = FALSE;
801 int needed;
802 int available;
803 int oldwin_height = 0;
804 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200805 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200807 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200808 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 if (flags & WSP_TOP)
811 oldwin = firstwin;
812 else if (flags & WSP_BOT)
813 oldwin = lastwin;
814 else
815 oldwin = curwin;
816
817 /* add a status line when p_ls == 1 and splitting the first window */
818 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
819 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100820 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 {
822 EMSG(_(e_noroom));
823 return FAIL;
824 }
825 need_status = STATUS_HEIGHT;
826 }
827
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000828#ifdef FEAT_GUI
829 /* May be needed for the scrollbars that are going to change. */
830 if (gui.in_use)
831 out_flush();
832#endif
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834#ifdef FEAT_VERTSPLIT
835 if (flags & WSP_VERT)
836 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200837 int wmw1;
838 int minwidth;
839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842 /*
843 * Check if we are able to split the current window and compute its
844 * width.
845 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200846 /* Current window requires at least 1 space. */
847 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
848 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200850 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200851 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200853 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200855 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200857 else if (p_ea)
858 {
859 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
860 prevfrp = oldwin->w_frame;
861 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
862 frp = frp->fr_parent)
863 {
864 if (frp->fr_layout == FR_ROW)
865 for (frp2 = frp->fr_child; frp2 != NULL;
866 frp2 = frp2->fr_next)
867 if (frp2 != prevfrp)
868 minwidth += frame_minwidth(frp2, NOWIN);
869 prevfrp = frp;
870 }
871 available = topframe->fr_width;
872 needed += minwidth;
873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200875 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200876 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
877 available = oldwin->w_frame->fr_width;
878 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200879 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100880 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 {
882 EMSG(_(e_noroom));
883 return FAIL;
884 }
885 if (new_size == 0)
886 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200887 if (new_size > available - minwidth - 1)
888 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200889 if (new_size < wmw1)
890 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892 /* if it doesn't fit in the current window, need win_equal() */
893 if (oldwin->w_width - new_size - 1 < p_wmw)
894 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000895
896 /* We don't like to take lines for the new window from a
897 * 'winfixwidth' window. Take them from a window to the left or right
898 * instead, if possible. */
899 if (oldwin->w_p_wfw)
900 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000901
902 /* Only make all windows the same width if one of them (except oldwin)
903 * is wider than one of the split windows. */
904 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
905 && oldwin->w_frame->fr_parent != NULL)
906 {
907 frp = oldwin->w_frame->fr_parent->fr_child;
908 while (frp != NULL)
909 {
910 if (frp->fr_win != oldwin && frp->fr_win != NULL
911 && (frp->fr_win->w_width > new_size
912 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100913 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000914 {
915 do_equal = TRUE;
916 break;
917 }
918 frp = frp->fr_next;
919 }
920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922 else
923#endif
924 {
925 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
927 /*
928 * Check if we are able to split the current window and compute its
929 * height.
930 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200931 /* Current window requires at least 1 space. */
932 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
933 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200935 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200936 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200938 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200940 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200942 else if (p_ea)
943 {
944 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
945 prevfrp = oldwin->w_frame;
946 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
947 frp = frp->fr_parent)
948 {
949 if (frp->fr_layout == FR_COL)
950 for (frp2 = frp->fr_child; frp2 != NULL;
951 frp2 = frp2->fr_next)
952 if (frp2 != prevfrp)
953 minheight += frame_minheight(frp2, NOWIN);
954 prevfrp = frp;
955 }
956 available = topframe->fr_height;
957 needed += minheight;
958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 else
960 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200961 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
962 available = oldwin->w_frame->fr_height;
963 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100965 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 EMSG(_(e_noroom));
968 return FAIL;
969 }
970 oldwin_height = oldwin->w_height;
971 if (need_status)
972 {
973 oldwin->w_status_height = STATUS_HEIGHT;
974 oldwin_height -= STATUS_HEIGHT;
975 }
976 if (new_size == 0)
977 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200978 if (new_size > available - minheight - STATUS_HEIGHT)
979 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200980 if (new_size < wmh1)
981 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 /* if it doesn't fit in the current window, need win_equal() */
984 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
985 do_equal = TRUE;
986
987 /* We don't like to take lines for the new window from a
988 * 'winfixheight' window. Take them from a window above or below
989 * instead, if possible. */
990 if (oldwin->w_p_wfh)
991 {
992 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
993 oldwin);
994 oldwin_height = oldwin->w_height;
995 if (need_status)
996 oldwin_height -= STATUS_HEIGHT;
997 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000998
999 /* Only make all windows the same height if one of them (except oldwin)
1000 * is higher than one of the split windows. */
1001 if (!do_equal && p_ea && size == 0
1002#ifdef FEAT_VERTSPLIT
1003 && *p_ead != 'h'
1004#endif
1005 && oldwin->w_frame->fr_parent != NULL)
1006 {
1007 frp = oldwin->w_frame->fr_parent->fr_child;
1008 while (frp != NULL)
1009 {
1010 if (frp->fr_win != oldwin && frp->fr_win != NULL
1011 && (frp->fr_win->w_height > new_size
1012 || frp->fr_win->w_height > oldwin_height - new_size
1013 - STATUS_HEIGHT))
1014 {
1015 do_equal = TRUE;
1016 break;
1017 }
1018 frp = frp->fr_next;
1019 }
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022
1023 /*
1024 * allocate new window structure and link it in the window list
1025 */
1026 if ((flags & WSP_TOP) == 0
1027 && ((flags & WSP_BOT)
1028 || (flags & WSP_BELOW)
1029 || (!(flags & WSP_ABOVE)
1030 && (
1031#ifdef FEAT_VERTSPLIT
1032 (flags & WSP_VERT) ? p_spr :
1033#endif
1034 p_sb))))
1035 {
1036 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001037 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001038 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 else
1040 win_append(oldwin, wp);
1041 }
1042 else
1043 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001044 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001045 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 else
1047 win_append(oldwin->w_prev, wp);
1048 }
1049
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001050 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 {
1052 if (wp == NULL)
1053 return FAIL;
1054
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001055 new_frame(wp);
1056 if (wp->w_frame == NULL)
1057 {
1058 win_free(wp, NULL);
1059 return FAIL;
1060 }
1061
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001062 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001063 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 }
1065
1066 /*
1067 * Reorganise the tree of frames to insert the new window.
1068 */
1069 if (flags & (WSP_TOP | WSP_BOT))
1070 {
1071#ifdef FEAT_VERTSPLIT
1072 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1073 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
1074#else
1075 if (topframe->fr_layout == FR_COL)
1076#endif
1077 {
1078 curfrp = topframe->fr_child;
1079 if (flags & WSP_BOT)
1080 while (curfrp->fr_next != NULL)
1081 curfrp = curfrp->fr_next;
1082 }
1083 else
1084 curfrp = topframe;
1085 before = (flags & WSP_TOP);
1086 }
1087 else
1088 {
1089 curfrp = oldwin->w_frame;
1090 if (flags & WSP_BELOW)
1091 before = FALSE;
1092 else if (flags & WSP_ABOVE)
1093 before = TRUE;
1094 else
1095#ifdef FEAT_VERTSPLIT
1096 if (flags & WSP_VERT)
1097 before = !p_spr;
1098 else
1099#endif
1100 before = !p_sb;
1101 }
1102 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1103 {
1104 /* Need to create a new frame in the tree to make a branch. */
1105 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1106 *frp = *curfrp;
1107 curfrp->fr_layout = layout;
1108 frp->fr_parent = curfrp;
1109 frp->fr_next = NULL;
1110 frp->fr_prev = NULL;
1111 curfrp->fr_child = frp;
1112 curfrp->fr_win = NULL;
1113 curfrp = frp;
1114 if (frp->fr_win != NULL)
1115 oldwin->w_frame = frp;
1116 else
1117 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1118 frp->fr_parent = curfrp;
1119 }
1120
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001121 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001122 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001124 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 frp->fr_parent = curfrp->fr_parent;
1126
1127 /* Insert the new frame at the right place in the frame list. */
1128 if (before)
1129 frame_insert(curfrp, frp);
1130 else
1131 frame_append(curfrp, frp);
1132
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001133 /* Set w_fraction now so that the cursor keeps the same relative
1134 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001135 if (oldwin->w_height > 0)
1136 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001137 wp->w_fraction = oldwin->w_fraction;
1138
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#ifdef FEAT_VERTSPLIT
1140 if (flags & WSP_VERT)
1141 {
1142 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001143
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (need_status)
1145 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001146 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 oldwin->w_status_height = need_status;
1148 }
1149 if (flags & (WSP_TOP | WSP_BOT))
1150 {
1151 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001152 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001153 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wp->w_status_height = (p_ls > 0);
1155 }
1156 else
1157 {
1158 /* height and row of new window is same as current window */
1159 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001160 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 wp->w_status_height = oldwin->w_status_height;
1162 }
1163 frp->fr_height = curfrp->fr_height;
1164
1165 /* "new_size" of the current window goes to the new window, use
1166 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001167 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 if (before)
1169 wp->w_vsep_width = 1;
1170 else
1171 {
1172 wp->w_vsep_width = oldwin->w_vsep_width;
1173 oldwin->w_vsep_width = 1;
1174 }
1175 if (flags & (WSP_TOP | WSP_BOT))
1176 {
1177 if (flags & WSP_BOT)
1178 frame_add_vsep(curfrp);
1179 /* Set width of neighbor frame */
1180 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001181 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1182 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 }
1184 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001185 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 if (before) /* new window left of current one */
1187 {
1188 wp->w_wincol = oldwin->w_wincol;
1189 oldwin->w_wincol += new_size + 1;
1190 }
1191 else /* new window right of current one */
1192 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1193 frame_fix_width(oldwin);
1194 frame_fix_width(wp);
1195 }
1196 else
1197#endif
1198 {
1199 /* width and column of new window is same as current window */
1200#ifdef FEAT_VERTSPLIT
1201 if (flags & (WSP_TOP | WSP_BOT))
1202 {
1203 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001204 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 wp->w_vsep_width = 0;
1206 }
1207 else
1208 {
1209 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001210 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 wp->w_vsep_width = oldwin->w_vsep_width;
1212 }
1213 frp->fr_width = curfrp->fr_width;
1214#endif
1215
1216 /* "new_size" of the current window goes to the new window, use
1217 * one row for the status line */
1218 win_new_height(wp, new_size);
1219 if (flags & (WSP_TOP | WSP_BOT))
1220 frame_new_height(curfrp, curfrp->fr_height
1221 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1222 else
1223 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1224 if (before) /* new window above current one */
1225 {
1226 wp->w_winrow = oldwin->w_winrow;
1227 wp->w_status_height = STATUS_HEIGHT;
1228 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1229 }
1230 else /* new window below current one */
1231 {
1232 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1233 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar5b14f892015-04-21 15:43:05 +02001234 /* Don't set the status_height for oldwin yet, this might break
1235 * frame_fix_height(oldwin), therefore will be set below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 }
1237#ifdef FEAT_VERTSPLIT
1238 if (flags & WSP_BOT)
1239 frame_add_statusline(curfrp);
1240#endif
1241 frame_fix_height(wp);
1242 frame_fix_height(oldwin);
Bram Moolenaar5b14f892015-04-21 15:43:05 +02001243
1244 if (!before)
1245 /* new window above current one, set the status_height after
1246 * frame_fix_height(oldwin) */
1247 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249
1250 if (flags & (WSP_TOP | WSP_BOT))
1251 (void)win_comp_pos();
1252
1253 /*
1254 * Both windows need redrawing
1255 */
1256 redraw_win_later(wp, NOT_VALID);
1257 wp->w_redr_status = TRUE;
1258 redraw_win_later(oldwin, NOT_VALID);
1259 oldwin->w_redr_status = TRUE;
1260
1261 if (need_status)
1262 {
1263 msg_row = Rows - 1;
1264 msg_col = sc_col;
1265 msg_clr_eos_force(); /* Old command/ruler may still be there */
1266 comp_col();
1267 msg_row = Rows - 1;
1268 msg_col = 0; /* put position back at start of line */
1269 }
1270
1271 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001272 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 */
1274 if (do_equal || dir != 0)
1275 win_equal(wp, TRUE,
1276#ifdef FEAT_VERTSPLIT
1277 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
1278 : dir == 'h' ? 'b' :
1279#endif
1280 'v');
1281
1282 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1283 * size was given. */
1284#ifdef FEAT_VERTSPLIT
1285 if (flags & WSP_VERT)
1286 {
1287 i = p_wiw;
1288 if (size != 0)
1289 p_wiw = size;
1290
1291# ifdef FEAT_GUI
1292 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1293 if (gui.in_use)
1294 gui_init_which_components(NULL);
1295# endif
1296 }
1297 else
1298#endif
1299 {
1300 i = p_wh;
1301 if (size != 0)
1302 p_wh = size;
1303 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001304
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001305#ifdef FEAT_JUMPLIST
1306 /* Keep same changelist position in new window. */
1307 wp->w_changelistidx = oldwin->w_changelistidx;
1308#endif
1309
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001310 /*
1311 * make the new window the current window
1312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 win_enter(wp, FALSE);
1314#ifdef FEAT_VERTSPLIT
1315 if (flags & WSP_VERT)
1316 p_wiw = i;
1317 else
1318#endif
1319 p_wh = i;
1320
1321 return OK;
1322}
1323
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001324
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001325/*
1326 * Initialize window "newp" from window "oldp".
1327 * Used when splitting a window and when creating a new tab page.
1328 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001329 * WSP_NEWLOC may be specified in flags to prevent the location list from
1330 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001331 */
1332 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001333win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001334{
1335 int i;
1336
1337 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001338#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001339 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001340#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001341 oldp->w_buffer->b_nwindows++;
1342 newp->w_cursor = oldp->w_cursor;
1343 newp->w_valid = 0;
1344 newp->w_curswant = oldp->w_curswant;
1345 newp->w_set_curswant = oldp->w_set_curswant;
1346 newp->w_topline = oldp->w_topline;
1347#ifdef FEAT_DIFF
1348 newp->w_topfill = oldp->w_topfill;
1349#endif
1350 newp->w_leftcol = oldp->w_leftcol;
1351 newp->w_pcmark = oldp->w_pcmark;
1352 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1353 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001354 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001355 newp->w_fraction = oldp->w_fraction;
1356 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1357#ifdef FEAT_JUMPLIST
1358 copy_jumplist(oldp, newp);
1359#endif
1360#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001361 if (flags & WSP_NEWLOC)
1362 {
1363 /* Don't copy the location list. */
1364 newp->w_llist = NULL;
1365 newp->w_llist_ref = NULL;
1366 }
1367 else
1368 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001369#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001370 newp->w_localdir = (oldp->w_localdir == NULL)
1371 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001372
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001373 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001374 for (i = 0; i < oldp->w_tagstacklen; i++)
1375 {
1376 newp->w_tagstack[i] = oldp->w_tagstack[i];
1377 if (newp->w_tagstack[i].tagname != NULL)
1378 newp->w_tagstack[i].tagname =
1379 vim_strsave(newp->w_tagstack[i].tagname);
1380 }
1381 newp->w_tagstackidx = oldp->w_tagstackidx;
1382 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001383#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001384 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001385#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001386
1387 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001388
Bram Moolenaara971b822011-09-14 14:43:25 +02001389#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001390 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001391#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001392}
1393
1394/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001395 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001396 * Only the essential things are copied.
1397 */
1398 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001399win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001400{
1401 /* Use the same argument list. */
1402 newp->w_alist = oldp->w_alist;
1403 ++newp->w_alist->al_refcount;
1404 newp->w_arg_idx = oldp->w_arg_idx;
1405
1406 /* copy options from existing window */
1407 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001408}
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#endif /* FEAT_WINDOWS */
1411
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412#if defined(FEAT_WINDOWS) || defined(PROTO)
1413/*
1414 * Check if "win" is a pointer to an existing window.
1415 */
1416 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001417win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418{
1419 win_T *wp;
1420
1421 if (win == NULL)
1422 return FALSE;
1423 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1424 if (wp == win)
1425 return TRUE;
1426 return FALSE;
1427}
1428
1429/*
1430 * Return the number of windows.
1431 */
1432 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001433win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 win_T *wp;
1436 int count = 0;
1437
1438 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1439 ++count;
1440 return count;
1441}
1442
1443/*
1444 * Make "count" windows on the screen.
1445 * Return actual number of windows on the screen.
1446 * Must be called when there is just one window, filling the whole screen
1447 * (excluding the command line).
1448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001450make_windows(
1451 int count,
1452 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 int maxcount;
1455 int todo;
1456
1457#ifdef FEAT_VERTSPLIT
1458 if (vertical)
1459 {
1460 /* Each windows needs at least 'winminwidth' lines and a separator
1461 * column. */
1462 maxcount = (curwin->w_width + curwin->w_vsep_width
1463 - (p_wiw - p_wmw)) / (p_wmw + 1);
1464 }
1465 else
1466#endif
1467 {
1468 /* Each window needs at least 'winminheight' lines and a status line. */
1469 maxcount = (curwin->w_height + curwin->w_status_height
1470 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1471 }
1472
1473 if (maxcount < 2)
1474 maxcount = 2;
1475 if (count > maxcount)
1476 count = maxcount;
1477
1478 /*
1479 * add status line now, otherwise first window will be too big
1480 */
1481 if (count > 1)
1482 last_status(TRUE);
1483
1484#ifdef FEAT_AUTOCMD
1485 /*
1486 * Don't execute autocommands while creating the windows. Must do that
1487 * when putting the buffers in the windows.
1488 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001489 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491
1492 /* todo is number of windows left to create */
1493 for (todo = count - 1; todo > 0; --todo)
1494#ifdef FEAT_VERTSPLIT
1495 if (vertical)
1496 {
1497 if (win_split(curwin->w_width - (curwin->w_width - todo)
1498 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1499 break;
1500 }
1501 else
1502#endif
1503 {
1504 if (win_split(curwin->w_height - (curwin->w_height - todo
1505 * STATUS_HEIGHT) / (todo + 1)
1506 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1507 break;
1508 }
1509
1510#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001511 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512#endif
1513
1514 /* return actual number of windows */
1515 return (count - todo);
1516}
1517
1518/*
1519 * Exchange current and next window
1520 */
1521 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001522win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523{
1524 frame_T *frp;
1525 frame_T *frp2;
1526 win_T *wp;
1527 win_T *wp2;
1528 int temp;
1529
1530 if (lastwin == firstwin) /* just one window */
1531 {
1532 beep_flush();
1533 return;
1534 }
1535
1536#ifdef FEAT_GUI
1537 need_mouse_correct = TRUE;
1538#endif
1539
1540 /*
1541 * find window to exchange with
1542 */
1543 if (Prenum)
1544 {
1545 frp = curwin->w_frame->fr_parent->fr_child;
1546 while (frp != NULL && --Prenum > 0)
1547 frp = frp->fr_next;
1548 }
1549 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1550 frp = curwin->w_frame->fr_next;
1551 else /* Swap last window in row/col with previous */
1552 frp = curwin->w_frame->fr_prev;
1553
1554 /* We can only exchange a window with another window, not with a frame
1555 * containing windows. */
1556 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1557 return;
1558 wp = frp->fr_win;
1559
1560/*
1561 * 1. remove curwin from the list. Remember after which window it was in wp2
1562 * 2. insert curwin before wp in the list
1563 * if wp != wp2
1564 * 3. remove wp from the list
1565 * 4. insert wp after wp2
1566 * 5. exchange the status line height and vsep width.
1567 */
1568 wp2 = curwin->w_prev;
1569 frp2 = curwin->w_frame->fr_prev;
1570 if (wp->w_prev != curwin)
1571 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001572 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 frame_remove(curwin->w_frame);
1574 win_append(wp->w_prev, curwin);
1575 frame_insert(frp, curwin->w_frame);
1576 }
1577 if (wp != wp2)
1578 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001579 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 frame_remove(wp->w_frame);
1581 win_append(wp2, wp);
1582 if (frp2 == NULL)
1583 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1584 else
1585 frame_append(frp2, wp->w_frame);
1586 }
1587 temp = curwin->w_status_height;
1588 curwin->w_status_height = wp->w_status_height;
1589 wp->w_status_height = temp;
1590#ifdef FEAT_VERTSPLIT
1591 temp = curwin->w_vsep_width;
1592 curwin->w_vsep_width = wp->w_vsep_width;
1593 wp->w_vsep_width = temp;
1594
1595 /* If the windows are not in the same frame, exchange the sizes to avoid
1596 * messing up the window layout. Otherwise fix the frame sizes. */
1597 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1598 {
1599 temp = curwin->w_height;
1600 curwin->w_height = wp->w_height;
1601 wp->w_height = temp;
1602 temp = curwin->w_width;
1603 curwin->w_width = wp->w_width;
1604 wp->w_width = temp;
1605 }
1606 else
1607 {
1608 frame_fix_height(curwin);
1609 frame_fix_height(wp);
1610 frame_fix_width(curwin);
1611 frame_fix_width(wp);
1612 }
1613#endif
1614
1615 (void)win_comp_pos(); /* recompute window positions */
1616
1617 win_enter(wp, TRUE);
1618 redraw_later(CLEAR);
1619}
1620
1621/*
1622 * rotate windows: if upwards TRUE the second window becomes the first one
1623 * if upwards FALSE the first window becomes the second one
1624 */
1625 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001626win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627{
1628 win_T *wp1;
1629 win_T *wp2;
1630 frame_T *frp;
1631 int n;
1632
1633 if (firstwin == lastwin) /* nothing to do */
1634 {
1635 beep_flush();
1636 return;
1637 }
1638
1639#ifdef FEAT_GUI
1640 need_mouse_correct = TRUE;
1641#endif
1642
1643#ifdef FEAT_VERTSPLIT
1644 /* Check if all frames in this row/col have one window. */
1645 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1646 frp = frp->fr_next)
1647 if (frp->fr_win == NULL)
1648 {
1649 EMSG(_("E443: Cannot rotate when another window is split"));
1650 return;
1651 }
1652#endif
1653
1654 while (count--)
1655 {
1656 if (upwards) /* first window becomes last window */
1657 {
1658 /* remove first window/frame from the list */
1659 frp = curwin->w_frame->fr_parent->fr_child;
1660 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001661 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 frame_remove(frp);
1663
1664 /* find last frame and append removed window/frame after it */
1665 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1666 ;
1667 win_append(frp->fr_win, wp1);
1668 frame_append(frp, wp1->w_frame);
1669
1670 wp2 = frp->fr_win; /* previously last window */
1671 }
1672 else /* last window becomes first window */
1673 {
1674 /* find last window/frame in the list and remove it */
1675 for (frp = curwin->w_frame; frp->fr_next != NULL;
1676 frp = frp->fr_next)
1677 ;
1678 wp1 = frp->fr_win;
1679 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001680 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 frame_remove(frp);
1682
1683 /* append the removed window/frame before the first in the list */
1684 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1685 frame_insert(frp->fr_parent->fr_child, frp);
1686 }
1687
1688 /* exchange status height and vsep width of old and new last window */
1689 n = wp2->w_status_height;
1690 wp2->w_status_height = wp1->w_status_height;
1691 wp1->w_status_height = n;
1692 frame_fix_height(wp1);
1693 frame_fix_height(wp2);
1694#ifdef FEAT_VERTSPLIT
1695 n = wp2->w_vsep_width;
1696 wp2->w_vsep_width = wp1->w_vsep_width;
1697 wp1->w_vsep_width = n;
1698 frame_fix_width(wp1);
1699 frame_fix_width(wp2);
1700#endif
1701
1702 /* recompute w_winrow and w_wincol for all windows */
1703 (void)win_comp_pos();
1704 }
1705
1706 redraw_later(CLEAR);
1707}
1708
1709/*
1710 * Move the current window to the very top/bottom/left/right of the screen.
1711 */
1712 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001713win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714{
1715 int dir;
1716 int height = curwin->w_height;
1717
1718 if (lastwin == firstwin)
1719 {
1720 beep_flush();
1721 return;
1722 }
1723
1724 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001725 (void)winframe_remove(curwin, &dir, NULL);
1726 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 last_status(FALSE); /* may need to remove last status line */
1728 (void)win_comp_pos(); /* recompute window positions */
1729
1730 /* Split a window on the desired side and put the window there. */
1731 (void)win_split_ins(size, flags, curwin, dir);
1732 if (!(flags & WSP_VERT))
1733 {
1734 win_setheight(height);
1735 if (p_ea)
1736 win_equal(curwin, TRUE, 'v');
1737 }
1738
1739#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
1740 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1741 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001742 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744}
1745
1746/*
1747 * Move window "win1" to below/right of "win2" and make "win1" the current
1748 * window. Only works within the same frame!
1749 */
1750 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001751win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752{
1753 int height;
1754
1755 /* check if the arguments are reasonable */
1756 if (win1 == win2)
1757 return;
1758
1759 /* check if there is something to do */
1760 if (win2->w_next != win1)
1761 {
1762 /* may need move the status line/vertical separator of the last window
1763 * */
1764 if (win1 == lastwin)
1765 {
1766 height = win1->w_prev->w_status_height;
1767 win1->w_prev->w_status_height = win1->w_status_height;
1768 win1->w_status_height = height;
1769#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001770 if (win1->w_prev->w_vsep_width == 1)
1771 {
1772 /* Remove the vertical separator from the last-but-one window,
1773 * add it to the last window. Adjust the frame widths. */
1774 win1->w_prev->w_vsep_width = 0;
1775 win1->w_prev->w_frame->fr_width -= 1;
1776 win1->w_vsep_width = 1;
1777 win1->w_frame->fr_width += 1;
1778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
1780 }
1781 else if (win2 == lastwin)
1782 {
1783 height = win1->w_status_height;
1784 win1->w_status_height = win2->w_status_height;
1785 win2->w_status_height = height;
1786#ifdef FEAT_VERTSPLIT
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001787 if (win1->w_vsep_width == 1)
1788 {
1789 /* Remove the vertical separator from win1, add it to the last
1790 * window, win2. Adjust the frame widths. */
1791 win2->w_vsep_width = 1;
1792 win2->w_frame->fr_width += 1;
1793 win1->w_vsep_width = 0;
1794 win1->w_frame->fr_width -= 1;
1795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#endif
1797 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001798 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 frame_remove(win1->w_frame);
1800 win_append(win2, win1);
1801 frame_append(win2->w_frame, win1->w_frame);
1802
1803 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1804 redraw_later(NOT_VALID);
1805 }
1806 win_enter(win1, FALSE);
1807}
1808
1809/*
1810 * Make all windows the same height.
1811 * 'next_curwin' will soon be the current window, make sure it has enough
1812 * rows.
1813 */
1814 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001815win_equal(
1816 win_T *next_curwin, /* pointer to current window to be or NULL */
1817 int current, /* do only frame with current window */
1818 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 'b' for both, 0 for using p_ead */
1820{
1821 if (dir == 0)
1822#ifdef FEAT_VERTSPLIT
1823 dir = *p_ead;
1824#else
1825 dir = 'b';
1826#endif
1827 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001828 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001829 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830}
1831
1832/*
1833 * Set a frame to a new position and height, spreading the available room
1834 * equally over contained frames.
1835 * The window "next_curwin" (if not NULL) should at least get the size from
1836 * 'winheight' and 'winwidth' if possible.
1837 */
1838 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001839win_equal_rec(
1840 win_T *next_curwin, /* pointer to current window to be or NULL */
1841 int current, /* do only frame with current window */
1842 frame_T *topfr, /* frame to set size off */
1843 int dir, /* 'v', 'h' or 'b', see win_equal() */
1844 int col, /* horizontal position for frame */
1845 int row, /* vertical position for frame */
1846 int width, /* new width of frame */
1847 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
1849 int n, m;
1850 int extra_sep = 0;
1851 int wincount, totwincount = 0;
1852 frame_T *fr;
1853 int next_curwin_size = 0;
1854 int room = 0;
1855 int new_size;
1856 int has_next_curwin = 0;
1857 int hnc;
1858
1859 if (topfr->fr_layout == FR_LEAF)
1860 {
1861 /* Set the width/height of this frame.
1862 * Redraw when size or position changes */
1863 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
1864#ifdef FEAT_VERTSPLIT
1865 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
1866#endif
1867 )
1868 {
1869 topfr->fr_win->w_winrow = row;
1870 frame_new_height(topfr, height, FALSE, FALSE);
1871#ifdef FEAT_VERTSPLIT
1872 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001873 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874#endif
1875 redraw_all_later(CLEAR);
1876 }
1877 }
1878#ifdef FEAT_VERTSPLIT
1879 else if (topfr->fr_layout == FR_ROW)
1880 {
1881 topfr->fr_width = width;
1882 topfr->fr_height = height;
1883
1884 if (dir != 'v') /* equalize frame widths */
1885 {
1886 /* Compute the maximum number of windows horizontally in this
1887 * frame. */
1888 n = frame_minwidth(topfr, NOWIN);
1889 /* add one for the rightmost window, it doesn't have a separator */
1890 if (col + width == Columns)
1891 extra_sep = 1;
1892 else
1893 extra_sep = 0;
1894 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001895 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001897 /*
1898 * Compute width for "next_curwin" window and room available for
1899 * other windows.
1900 * "m" is the minimal width when counting p_wiw for "next_curwin".
1901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 m = frame_minwidth(topfr, next_curwin);
1903 room = width - m;
1904 if (room < 0)
1905 {
1906 next_curwin_size = p_wiw + room;
1907 room = 0;
1908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 else
1910 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001911 next_curwin_size = -1;
1912 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1913 {
1914 /* If 'winfixwidth' set keep the window width if
1915 * possible.
1916 * Watch out for this window being the next_curwin. */
1917 if (frame_fixed_width(fr))
1918 {
1919 n = frame_minwidth(fr, NOWIN);
1920 new_size = fr->fr_width;
1921 if (frame_has_win(fr, next_curwin))
1922 {
1923 room += p_wiw - p_wmw;
1924 next_curwin_size = 0;
1925 if (new_size < p_wiw)
1926 new_size = p_wiw;
1927 }
1928 else
1929 /* These windows don't use up room. */
1930 totwincount -= (n + (fr->fr_next == NULL
1931 ? extra_sep : 0)) / (p_wmw + 1);
1932 room -= new_size - n;
1933 if (room < 0)
1934 {
1935 new_size += room;
1936 room = 0;
1937 }
1938 fr->fr_newwidth = new_size;
1939 }
1940 }
1941 if (next_curwin_size == -1)
1942 {
1943 if (!has_next_curwin)
1944 next_curwin_size = 0;
1945 else if (totwincount > 1
1946 && (room + (totwincount - 2))
1947 / (totwincount - 1) > p_wiw)
1948 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001949 /* Can make all windows wider than 'winwidth', spread
1950 * the room equally. */
1951 next_curwin_size = (room + p_wiw
1952 + (totwincount - 1) * p_wmw
1953 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001954 room -= next_curwin_size - p_wiw;
1955 }
1956 else
1957 next_curwin_size = p_wiw;
1958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001960
1961 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 --totwincount; /* don't count curwin */
1963 }
1964
1965 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1966 {
1967 n = m = 0;
1968 wincount = 1;
1969 if (fr->fr_next == NULL)
1970 /* last frame gets all that remains (avoid roundoff error) */
1971 new_size = width;
1972 else if (dir == 'v')
1973 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001974 else if (frame_fixed_width(fr))
1975 {
1976 new_size = fr->fr_newwidth;
1977 wincount = 0; /* doesn't count as a sizeable window */
1978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 else
1980 {
1981 /* Compute the maximum number of windows horiz. in "fr". */
1982 n = frame_minwidth(fr, NOWIN);
1983 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1984 / (p_wmw + 1);
1985 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001986 if (has_next_curwin)
1987 hnc = frame_has_win(fr, next_curwin);
1988 else
1989 hnc = FALSE;
1990 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001992 if (totwincount == 0)
1993 new_size = room;
1994 else
1995 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001997 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
1999 next_curwin_size -= p_wiw - (m - n);
2000 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002001 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002003 else
2004 room -= new_size;
2005 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 }
2007
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002008 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 * window, unless equalizing all frames. */
2010 if (!current || dir != 'v' || topfr->fr_parent != NULL
2011 || (new_size != fr->fr_width)
2012 || frame_has_win(fr, next_curwin))
2013 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002014 new_size, height);
2015 col += new_size;
2016 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 totwincount -= wincount;
2018 }
2019 }
2020#endif
2021 else /* topfr->fr_layout == FR_COL */
2022 {
2023#ifdef FEAT_VERTSPLIT
2024 topfr->fr_width = width;
2025#endif
2026 topfr->fr_height = height;
2027
2028 if (dir != 'h') /* equalize frame heights */
2029 {
2030 /* Compute maximum number of windows vertically in this frame. */
2031 n = frame_minheight(topfr, NOWIN);
2032 /* add one for the bottom window if it doesn't have a statusline */
2033 if (row + height == cmdline_row && p_ls == 0)
2034 extra_sep = 1;
2035 else
2036 extra_sep = 0;
2037 totwincount = (n + extra_sep) / (p_wmh + 1);
2038 has_next_curwin = frame_has_win(topfr, next_curwin);
2039
2040 /*
2041 * Compute height for "next_curwin" window and room available for
2042 * other windows.
2043 * "m" is the minimal height when counting p_wh for "next_curwin".
2044 */
2045 m = frame_minheight(topfr, next_curwin);
2046 room = height - m;
2047 if (room < 0)
2048 {
2049 /* The room is less then 'winheight', use all space for the
2050 * current window. */
2051 next_curwin_size = p_wh + room;
2052 room = 0;
2053 }
2054 else
2055 {
2056 next_curwin_size = -1;
2057 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2058 {
2059 /* If 'winfixheight' set keep the window height if
2060 * possible.
2061 * Watch out for this window being the next_curwin. */
2062 if (frame_fixed_height(fr))
2063 {
2064 n = frame_minheight(fr, NOWIN);
2065 new_size = fr->fr_height;
2066 if (frame_has_win(fr, next_curwin))
2067 {
2068 room += p_wh - p_wmh;
2069 next_curwin_size = 0;
2070 if (new_size < p_wh)
2071 new_size = p_wh;
2072 }
2073 else
2074 /* These windows don't use up room. */
2075 totwincount -= (n + (fr->fr_next == NULL
2076 ? extra_sep : 0)) / (p_wmh + 1);
2077 room -= new_size - n;
2078 if (room < 0)
2079 {
2080 new_size += room;
2081 room = 0;
2082 }
2083 fr->fr_newheight = new_size;
2084 }
2085 }
2086 if (next_curwin_size == -1)
2087 {
2088 if (!has_next_curwin)
2089 next_curwin_size = 0;
2090 else if (totwincount > 1
2091 && (room + (totwincount - 2))
2092 / (totwincount - 1) > p_wh)
2093 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002094 /* can make all windows higher than 'winheight',
2095 * spread the room equally. */
2096 next_curwin_size = (room + p_wh
2097 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 + (totwincount - 1)) / totwincount;
2099 room -= next_curwin_size - p_wh;
2100 }
2101 else
2102 next_curwin_size = p_wh;
2103 }
2104 }
2105
2106 if (has_next_curwin)
2107 --totwincount; /* don't count curwin */
2108 }
2109
2110 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2111 {
2112 n = m = 0;
2113 wincount = 1;
2114 if (fr->fr_next == NULL)
2115 /* last frame gets all that remains (avoid roundoff error) */
2116 new_size = height;
2117 else if (dir == 'h')
2118 new_size = fr->fr_height;
2119 else if (frame_fixed_height(fr))
2120 {
2121 new_size = fr->fr_newheight;
2122 wincount = 0; /* doesn't count as a sizeable window */
2123 }
2124 else
2125 {
2126 /* Compute the maximum number of windows vert. in "fr". */
2127 n = frame_minheight(fr, NOWIN);
2128 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2129 / (p_wmh + 1);
2130 m = frame_minheight(fr, next_curwin);
2131 if (has_next_curwin)
2132 hnc = frame_has_win(fr, next_curwin);
2133 else
2134 hnc = FALSE;
2135 if (hnc) /* don't count next_curwin */
2136 --wincount;
2137 if (totwincount == 0)
2138 new_size = room;
2139 else
2140 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2141 / totwincount;
2142 if (hnc) /* add next_curwin size */
2143 {
2144 next_curwin_size -= p_wh - (m - n);
2145 new_size += next_curwin_size;
2146 room -= new_size - next_curwin_size;
2147 }
2148 else
2149 room -= new_size;
2150 new_size += n;
2151 }
2152 /* Skip frame that is full width when splitting or closing a
2153 * window, unless equalizing all frames. */
2154 if (!current || dir != 'h' || topfr->fr_parent != NULL
2155 || (new_size != fr->fr_height)
2156 || frame_has_win(fr, next_curwin))
2157 win_equal_rec(next_curwin, current, fr, dir, col, row,
2158 width, new_size);
2159 row += new_size;
2160 height -= new_size;
2161 totwincount -= wincount;
2162 }
2163 }
2164}
2165
2166/*
2167 * close all windows for buffer 'buf'
2168 */
2169 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002170close_windows(
2171 buf_T *buf,
2172 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002174 win_T *wp;
2175 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002176 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002179
2180 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002182 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2183#ifdef FEAT_AUTOCMD
2184 && !(wp->w_closing || wp->w_buffer->b_closing)
2185#endif
2186 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002188 win_close(wp, FALSE);
2189
2190 /* Start all over, autocommands may change the window layout. */
2191 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 }
2193 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002194 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002196
2197 /* Also check windows in other tab pages. */
2198 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2199 {
2200 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002201 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002202 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002203 if (wp->w_buffer == buf
2204#ifdef FEAT_AUTOCMD
2205 && !(wp->w_closing || wp->w_buffer->b_closing)
2206#endif
2207 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002208 {
2209 win_close_othertab(wp, FALSE, tp);
2210
2211 /* Start all over, the tab page may be closed and
2212 * autocommands may change the window layout. */
2213 nexttp = first_tabpage;
2214 break;
2215 }
2216 }
2217
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002219
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002220 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002221 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002222 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223}
2224
2225/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002226 * Return TRUE if the current window is the only window that exists (ignoring
2227 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002228 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002229 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002230 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002231last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002232{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002233 return (one_window() && first_tabpage->tp_next == NULL);
2234}
2235
2236/*
2237 * Return TRUE if there is only one window other than "aucmd_win" in the
2238 * current tab page.
2239 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002240 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002241one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002242{
2243#ifdef FEAT_AUTOCMD
2244 win_T *wp;
2245 int seen_one = FALSE;
2246
2247 FOR_ALL_WINDOWS(wp)
2248 {
2249 if (wp != aucmd_win)
2250 {
2251 if (seen_one)
2252 return FALSE;
2253 seen_one = TRUE;
2254 }
2255 }
2256 return TRUE;
2257#else
2258 return firstwin == lastwin;
2259#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002260}
2261
2262/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002263 * Close the possibly last window in a tab page.
2264 * Returns TRUE when the window was closed already.
2265 */
2266 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002267close_last_window_tabpage(
2268 win_T *win,
2269 int free_buf,
2270 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002271{
2272 if (firstwin == lastwin)
2273 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002274#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002275 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002276#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002277
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002278 /*
2279 * Closing the last window in a tab page. First go to another tab
2280 * page and then close the window and the tab page. This avoids that
2281 * curwin and curtab are invalid while we are freeing memory, they may
2282 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002283 * Don't trigger autocommands yet, they may use wrong values, so do
2284 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002285 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002286 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002287 redraw_tabline = TRUE;
2288
2289 /* Safety check: Autocommands may have closed the window when jumping
2290 * to the other tab page. */
2291 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2292 {
2293 int h = tabline_height();
2294
2295 win_close_othertab(win, free_buf, prev_curtab);
2296 if (h != tabline_height())
2297 shell_new_rows();
2298 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002299 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2300 * that now. */
2301#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002302 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002303 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2304 if (old_curbuf != curbuf)
2305 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002306#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002307 return TRUE;
2308 }
2309 return FALSE;
2310}
2311
2312/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002313 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 * If "free_buf" is TRUE related buffer may be unloaded.
2315 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002316 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002317 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002319 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002320win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
2322 win_T *wp;
2323#ifdef FEAT_AUTOCMD
2324 int other_buffer = FALSE;
2325#endif
2326 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int dir;
2328 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002329 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002331 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002337#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002338 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002339 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002340 if (win == aucmd_win)
2341 {
2342 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002343 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002344 }
2345 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2346 {
2347 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002348 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002349 }
2350#endif
2351
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002352 /* When closing the last window in a tab page first go to another tab page
2353 * and then close the window and the tab page to avoid that curwin and
2354 * curtab are invalid while we are freeing memory. */
2355 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002356 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002357
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 /* When closing the help window, try restoring a snapshot after closing
2359 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002360 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 help_window = TRUE;
2362 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002363 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365#ifdef FEAT_AUTOCMD
2366 if (win == curwin)
2367 {
2368 /*
2369 * Guess which window is going to be the new current window.
2370 * This may change because of the autocommands (sigh).
2371 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002372 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
2374 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002375 * Be careful: If autocommands delete the window or cause this window
2376 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 */
2378 if (wp->w_buffer != curbuf)
2379 {
2380 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002381 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002383 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002384 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002385 win->w_closing = FALSE;
2386 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002387 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002389 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002391 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002392 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002393 win->w_closing = FALSE;
2394 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002395 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396# ifdef FEAT_EVAL
2397 /* autocmds may abort script processing */
2398 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002399 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400# endif
2401 }
2402#endif
2403
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002404#ifdef FEAT_GUI
2405 /* Avoid trouble with scrollbars that are going to be deleted in
2406 * win_free(). */
2407 if (gui.in_use)
2408 out_flush();
2409#endif
2410
Bram Moolenaara971b822011-09-14 14:43:25 +02002411#ifdef FEAT_SYN_HL
2412 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002413 if (win->w_buffer != NULL)
2414 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002415#endif
2416
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 /*
2418 * Close the link to the buffer.
2419 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002420 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002421 {
2422#ifdef FEAT_AUTOCMD
2423 win->w_closing = TRUE;
2424#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002425 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002426#ifdef FEAT_AUTOCMD
2427 if (win_valid(win))
2428 win->w_closing = FALSE;
2429#endif
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002430 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2431 * "wipe". */
2432 if (!buf_valid(curbuf))
2433 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002434 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002435
Bram Moolenaar802418d2013-01-17 14:00:11 +01002436 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2437 && (last_window() || curtab != prev_curtab
2438 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002439 {
2440 /* Autocommands have close all windows, quit now. Restore
2441 * curwin->w_buffer, otherwise writing viminfo may fail. */
2442 if (curwin->w_buffer == NULL)
2443 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002444 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002445 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002446
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002448 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002449 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002450 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002451 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
Bram Moolenaara971b822011-09-14 14:43:25 +02002453 /* Free the memory used for the window and get the window that received
2454 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002455 wp = win_free_mem(win, &dir, NULL);
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 /* Make sure curwin isn't invalid. It can cause severe trouble when
2458 * printing an error message. For win_equal() curbuf needs to be valid
2459 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002460 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462 curwin = wp;
2463#ifdef FEAT_QUICKFIX
2464 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2465 {
2466 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002467 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 * finding another window to go to.
2469 */
2470 for (;;)
2471 {
2472 if (wp->w_next == NULL)
2473 wp = firstwin;
2474 else
2475 wp = wp->w_next;
2476 if (wp == curwin)
2477 break;
2478 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2479 {
2480 curwin = wp;
2481 break;
2482 }
2483 }
2484 }
2485#endif
2486 curbuf = curwin->w_buffer;
2487 close_curwin = TRUE;
2488 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002489 if (p_ea
2490#ifdef FEAT_VERTSPLIT
2491 && (*p_ead == 'b' || *p_ead == dir)
2492#endif
2493 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 win_equal(curwin, TRUE,
2495#ifdef FEAT_VERTSPLIT
2496 dir
2497#else
2498 0
2499#endif
2500 );
2501 else
2502 win_comp_pos();
2503 if (close_curwin)
2504 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002505 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506#ifdef FEAT_AUTOCMD
2507 if (other_buffer)
2508 /* careful: after this wp and win may be invalid! */
2509 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2510#endif
2511 }
2512
2513 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002514 * If last window has a status line now and we don't want one,
2515 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 */
2517 last_status(FALSE);
2518
2519 /* After closing the help window, try restoring the window layout from
2520 * before it was opened. */
2521 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002522 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
2524#if defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)
2525 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2526 if (gui.in_use && !win_hasvertsplit())
2527 gui_init_which_components(NULL);
2528#endif
2529
2530 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002531 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532}
2533
2534/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002535 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002536 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002537 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002538 * Caller must check if buffer is hidden and whether the tabline needs to be
2539 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002540 */
2541 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002542win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002543{
2544 win_T *wp;
2545 int dir;
2546 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002547 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002548
Bram Moolenaar362ce482012-06-06 19:02:45 +02002549#ifdef FEAT_AUTOCMD
2550 if (win->w_closing || win->w_buffer->b_closing)
2551 return; /* window is already being closed */
2552#endif
2553
Bram Moolenaarf740b292006-02-16 22:11:02 +00002554 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002555 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002556
2557 /* Careful: Autocommands may have closed the tab page or made it the
2558 * current tab page. */
2559 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2560 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002561 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002562 return;
2563
2564 /* Autocommands may have closed the window already. */
2565 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2566 ;
2567 if (wp == NULL)
2568 return;
2569
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002571 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002572 {
2573 if (tp == first_tabpage)
2574 first_tabpage = tp->tp_next;
2575 else
2576 {
2577 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2578 ptp = ptp->tp_next)
2579 ;
2580 if (ptp == NULL)
2581 {
2582 EMSG2(_(e_intern2), "win_close_othertab()");
2583 return;
2584 }
2585 ptp->tp_next = tp->tp_next;
2586 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002587 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002588 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002589
2590 /* Free the memory used for the window. */
2591 win_free_mem(win, &dir, tp);
2592
2593 if (free_tp)
2594 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002595}
2596
2597/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002598 * Free the memory used for a window.
2599 * Returns a pointer to the window that got the freed up space.
2600 */
2601 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002602win_free_mem(
2603 win_T *win,
2604 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2605 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002606{
2607 frame_T *frp;
2608 win_T *wp;
2609
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002610 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002611 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002612 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002613 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002614 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002615
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002616 /* When deleting the current window of another tab page select a new
2617 * current window. */
2618 if (tp != NULL && win == tp->tp_curwin)
2619 tp->tp_curwin = wp;
2620
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002621 return wp;
2622}
2623
2624#if defined(EXITFREE) || defined(PROTO)
2625 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002626win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002627{
2628 int dummy;
2629
Bram Moolenaarf740b292006-02-16 22:11:02 +00002630# ifdef FEAT_WINDOWS
2631 while (first_tabpage->tp_next != NULL)
2632 tabpage_close(TRUE);
2633# endif
2634
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002635# ifdef FEAT_AUTOCMD
2636 if (aucmd_win != NULL)
2637 {
2638 (void)win_free_mem(aucmd_win, &dummy, NULL);
2639 aucmd_win = NULL;
2640 }
2641# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002642
2643 while (firstwin != NULL)
2644 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002645
2646 /* No window should be used after this. Set curwin to NULL to crash
2647 * instead of using freed memory. */
2648 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002649}
2650#endif
2651
2652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 * Remove a window and its frame from the tree of frames.
2654 * Returns a pointer to the window that got the freed up space.
2655 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002656 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002657winframe_remove(
2658 win_T *win,
2659 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2660 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662 frame_T *frp, *frp2, *frp3;
2663 frame_T *frp_close = win->w_frame;
2664 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002667 * If there is only one window there is nothing to remove.
2668 */
2669 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2670 return NULL;
2671
2672 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 * Remove the window from its frame.
2674 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002675 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 wp = frame2win(frp2);
2677
2678 /* Remove this frame from the list of frames. */
2679 frame_remove(frp_close);
2680
2681#ifdef FEAT_VERTSPLIT
2682 if (frp_close->fr_parent->fr_layout == FR_COL)
2683 {
2684#endif
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002685 /* When 'winfixheight' is set, try to find another frame in the column
2686 * (as close to the closed frame as possible) to distribute the height
2687 * to. */
2688 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2689 {
2690 frp = frp_close->fr_prev;
2691 frp3 = frp_close->fr_next;
2692 while (frp != NULL || frp3 != NULL)
2693 {
2694 if (frp != NULL)
2695 {
2696 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2697 {
2698 frp2 = frp;
2699 wp = frp->fr_win;
2700 break;
2701 }
2702 frp = frp->fr_prev;
2703 }
2704 if (frp3 != NULL)
2705 {
2706 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2707 {
2708 frp2 = frp3;
2709 wp = frp3->fr_win;
2710 break;
2711 }
2712 frp3 = frp3->fr_next;
2713 }
2714 }
2715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2717 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718#ifdef FEAT_VERTSPLIT
2719 *dirp = 'v';
2720 }
2721 else
2722 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002723 /* When 'winfixwidth' is set, try to find another frame in the column
2724 * (as close to the closed frame as possible) to distribute the width
2725 * to. */
2726 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2727 {
2728 frp = frp_close->fr_prev;
2729 frp3 = frp_close->fr_next;
2730 while (frp != NULL || frp3 != NULL)
2731 {
2732 if (frp != NULL)
2733 {
2734 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2735 {
2736 frp2 = frp;
2737 wp = frp->fr_win;
2738 break;
2739 }
2740 frp = frp->fr_prev;
2741 }
2742 if (frp3 != NULL)
2743 {
2744 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2745 {
2746 frp2 = frp3;
2747 wp = frp3->fr_win;
2748 break;
2749 }
2750 frp3 = frp3->fr_next;
2751 }
2752 }
2753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002755 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 *dirp = 'h';
2757 }
2758#endif
2759
2760 /* If rows/columns go to a window below/right its positions need to be
2761 * updated. Can only be done after the sizes have been updated. */
2762 if (frp2 == frp_close->fr_next)
2763 {
2764 int row = win->w_winrow;
2765 int col = W_WINCOL(win);
2766
2767 frame_comp_pos(frp2, &row, &col);
2768 }
2769
2770 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2771 {
2772 /* There is no other frame in this list, move its info to the parent
2773 * and remove it. */
2774 frp2->fr_parent->fr_layout = frp2->fr_layout;
2775 frp2->fr_parent->fr_child = frp2->fr_child;
2776 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2777 frp->fr_parent = frp2->fr_parent;
2778 frp2->fr_parent->fr_win = frp2->fr_win;
2779 if (frp2->fr_win != NULL)
2780 frp2->fr_win->w_frame = frp2->fr_parent;
2781 frp = frp2->fr_parent;
2782 vim_free(frp2);
2783
2784 frp2 = frp->fr_parent;
2785 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2786 {
2787 /* The frame above the parent has the same layout, have to merge
2788 * the frames into this list. */
2789 if (frp2->fr_child == frp)
2790 frp2->fr_child = frp->fr_child;
2791 frp->fr_child->fr_prev = frp->fr_prev;
2792 if (frp->fr_prev != NULL)
2793 frp->fr_prev->fr_next = frp->fr_child;
2794 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2795 {
2796 frp3->fr_parent = frp2;
2797 if (frp3->fr_next == NULL)
2798 {
2799 frp3->fr_next = frp->fr_next;
2800 if (frp->fr_next != NULL)
2801 frp->fr_next->fr_prev = frp3;
2802 break;
2803 }
2804 }
2805 vim_free(frp);
2806 }
2807 }
2808
2809 return wp;
2810}
2811
2812/*
2813 * Find out which frame is going to get the freed up space when "win" is
2814 * closed.
2815 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2816 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2817 * This makes opening a window and closing it immediately keep the same window
2818 * layout.
2819 */
2820 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002821win_altframe(
2822 win_T *win,
2823 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824{
2825 frame_T *frp;
2826 int b;
2827
Bram Moolenaarf740b292006-02-16 22:11:02 +00002828 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002829 /* Last window in this tab page, will go to next tab page. */
2830 return alt_tabpage()->tp_curwin->w_frame;
2831
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 frp = win->w_frame;
2833#ifdef FEAT_VERTSPLIT
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002834 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 b = p_spr;
2836 else
2837#endif
2838 b = p_sb;
2839 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2840 return frp->fr_next;
2841 return frp->fr_prev;
2842}
2843
2844/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002845 * Return the tabpage that will be used if the current one is closed.
2846 */
2847 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002848alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002849{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002850 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002851
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002852 /* Use the next tab page if possible. */
2853 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002854 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002855
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002856 /* Find the last but one tab page. */
2857 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2858 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002859 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002860}
2861
2862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 * Find the left-upper window in frame "frp".
2864 */
2865 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002866frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 while (frp->fr_win == NULL)
2869 frp = frp->fr_child;
2870 return frp->fr_win;
2871}
2872
2873/*
2874 * Return TRUE if frame "frp" contains window "wp".
2875 */
2876 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002877frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 frame_T *p;
2880
2881 if (frp->fr_layout == FR_LEAF)
2882 return frp->fr_win == wp;
2883
2884 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2885 if (frame_has_win(p, wp))
2886 return TRUE;
2887 return FALSE;
2888}
2889
2890/*
2891 * Set a new height for a frame. Recursively sets the height for contained
2892 * frames and windows. Caller must take care of positions.
2893 */
2894 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002895frame_new_height(
2896 frame_T *topfrp,
2897 int height,
2898 int topfirst, /* resize topmost contained frame first */
2899 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 may cause the height not to be set */
2901{
2902 frame_T *frp;
2903 int extra_lines;
2904 int h;
2905
2906 if (topfrp->fr_win != NULL)
2907 {
2908 /* Simple case: just one window. */
2909 win_new_height(topfrp->fr_win,
2910 height - topfrp->fr_win->w_status_height);
2911 }
2912#ifdef FEAT_VERTSPLIT
2913 else if (topfrp->fr_layout == FR_ROW)
2914 {
2915 do
2916 {
2917 /* All frames in this row get the same new height. */
2918 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2919 {
2920 frame_new_height(frp, height, topfirst, wfh);
2921 if (frp->fr_height > height)
2922 {
2923 /* Could not fit the windows, make the whole row higher. */
2924 height = frp->fr_height;
2925 break;
2926 }
2927 }
2928 }
2929 while (frp != NULL);
2930 }
2931#endif
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002932 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 {
2934 /* Complicated case: Resize a column of frames. Resize the bottom
2935 * frame first, frames above that when needed. */
2936
2937 frp = topfrp->fr_child;
2938 if (wfh)
2939 /* Advance past frames with one window with 'wfh' set. */
2940 while (frame_fixed_height(frp))
2941 {
2942 frp = frp->fr_next;
2943 if (frp == NULL)
2944 return; /* no frame without 'wfh', give up */
2945 }
2946 if (!topfirst)
2947 {
2948 /* Find the bottom frame of this column */
2949 while (frp->fr_next != NULL)
2950 frp = frp->fr_next;
2951 if (wfh)
2952 /* Advance back for frames with one window with 'wfh' set. */
2953 while (frame_fixed_height(frp))
2954 frp = frp->fr_prev;
2955 }
2956
2957 extra_lines = height - topfrp->fr_height;
2958 if (extra_lines < 0)
2959 {
2960 /* reduce height of contained frames, bottom or top frame first */
2961 while (frp != NULL)
2962 {
2963 h = frame_minheight(frp, NULL);
2964 if (frp->fr_height + extra_lines < h)
2965 {
2966 extra_lines += frp->fr_height - h;
2967 frame_new_height(frp, h, topfirst, wfh);
2968 }
2969 else
2970 {
2971 frame_new_height(frp, frp->fr_height + extra_lines,
2972 topfirst, wfh);
2973 break;
2974 }
2975 if (topfirst)
2976 {
2977 do
2978 frp = frp->fr_next;
2979 while (wfh && frp != NULL && frame_fixed_height(frp));
2980 }
2981 else
2982 {
2983 do
2984 frp = frp->fr_prev;
2985 while (wfh && frp != NULL && frame_fixed_height(frp));
2986 }
2987 /* Increase "height" if we could not reduce enough frames. */
2988 if (frp == NULL)
2989 height -= extra_lines;
2990 }
2991 }
2992 else if (extra_lines > 0)
2993 {
2994 /* increase height of bottom or top frame */
2995 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2996 }
2997 }
2998 topfrp->fr_height = height;
2999}
3000
3001/*
3002 * Return TRUE if height of frame "frp" should not be changed because of
3003 * the 'winfixheight' option.
3004 */
3005 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003006frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007{
3008 /* frame with one window: fixed height if 'winfixheight' set. */
3009 if (frp->fr_win != NULL)
3010 return frp->fr_win->w_p_wfh;
3011
3012 if (frp->fr_layout == FR_ROW)
3013 {
3014 /* The frame is fixed height if one of the frames in the row is fixed
3015 * height. */
3016 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3017 if (frame_fixed_height(frp))
3018 return TRUE;
3019 return FALSE;
3020 }
3021
3022 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3023 * frames in the row are fixed height. */
3024 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3025 if (!frame_fixed_height(frp))
3026 return FALSE;
3027 return TRUE;
3028}
3029
3030#ifdef FEAT_VERTSPLIT
3031/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003032 * Return TRUE if width of frame "frp" should not be changed because of
3033 * the 'winfixwidth' option.
3034 */
3035 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003036frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003037{
3038 /* frame with one window: fixed width if 'winfixwidth' set. */
3039 if (frp->fr_win != NULL)
3040 return frp->fr_win->w_p_wfw;
3041
3042 if (frp->fr_layout == FR_COL)
3043 {
3044 /* The frame is fixed width if one of the frames in the row is fixed
3045 * width. */
3046 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3047 if (frame_fixed_width(frp))
3048 return TRUE;
3049 return FALSE;
3050 }
3051
3052 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3053 * frames in the row are fixed width. */
3054 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3055 if (!frame_fixed_width(frp))
3056 return FALSE;
3057 return TRUE;
3058}
3059
3060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 * Add a status line to windows at the bottom of "frp".
3062 * Note: Does not check if there is room!
3063 */
3064 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003065frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 win_T *wp;
3068
3069 if (frp->fr_layout == FR_LEAF)
3070 {
3071 wp = frp->fr_win;
3072 if (wp->w_status_height == 0)
3073 {
3074 if (wp->w_height > 0) /* don't make it negative */
3075 --wp->w_height;
3076 wp->w_status_height = STATUS_HEIGHT;
3077 }
3078 }
3079 else if (frp->fr_layout == FR_ROW)
3080 {
3081 /* Handle all the frames in the row. */
3082 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3083 frame_add_statusline(frp);
3084 }
3085 else /* frp->fr_layout == FR_COL */
3086 {
3087 /* Only need to handle the last frame in the column. */
3088 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3089 ;
3090 frame_add_statusline(frp);
3091 }
3092}
3093
3094/*
3095 * Set width of a frame. Handles recursively going through contained frames.
3096 * May remove separator line for windows at the right side (for win_close()).
3097 */
3098 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003099frame_new_width(
3100 frame_T *topfrp,
3101 int width,
3102 int leftfirst, /* resize leftmost contained frame first */
3103 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003104 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 frame_T *frp;
3107 int extra_cols;
3108 int w;
3109 win_T *wp;
3110
3111 if (topfrp->fr_layout == FR_LEAF)
3112 {
3113 /* Simple case: just one window. */
3114 wp = topfrp->fr_win;
3115 /* Find out if there are any windows right of this one. */
3116 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3117 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3118 break;
3119 if (frp->fr_parent == NULL)
3120 wp->w_vsep_width = 0;
3121 win_new_width(wp, width - wp->w_vsep_width);
3122 }
3123 else if (topfrp->fr_layout == FR_COL)
3124 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003125 do
3126 {
3127 /* All frames in this column get the same new width. */
3128 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3129 {
3130 frame_new_width(frp, width, leftfirst, wfw);
3131 if (frp->fr_width > width)
3132 {
3133 /* Could not fit the windows, make whole column wider. */
3134 width = frp->fr_width;
3135 break;
3136 }
3137 }
3138 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
3140 else /* fr_layout == FR_ROW */
3141 {
3142 /* Complicated case: Resize a row of frames. Resize the rightmost
3143 * frame first, frames left of it when needed. */
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003146 if (wfw)
3147 /* Advance past frames with one window with 'wfw' set. */
3148 while (frame_fixed_width(frp))
3149 {
3150 frp = frp->fr_next;
3151 if (frp == NULL)
3152 return; /* no frame without 'wfw', give up */
3153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003155 {
3156 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 while (frp->fr_next != NULL)
3158 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003159 if (wfw)
3160 /* Advance back for frames with one window with 'wfw' set. */
3161 while (frame_fixed_width(frp))
3162 frp = frp->fr_prev;
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
3165 extra_cols = width - topfrp->fr_width;
3166 if (extra_cols < 0)
3167 {
3168 /* reduce frame width, rightmost frame first */
3169 while (frp != NULL)
3170 {
3171 w = frame_minwidth(frp, NULL);
3172 if (frp->fr_width + extra_cols < w)
3173 {
3174 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003175 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 }
3177 else
3178 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003179 frame_new_width(frp, frp->fr_width + extra_cols,
3180 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 break;
3182 }
3183 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003184 {
3185 do
3186 frp = frp->fr_next;
3187 while (wfw && frp != NULL && frame_fixed_width(frp));
3188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003190 {
3191 do
3192 frp = frp->fr_prev;
3193 while (wfw && frp != NULL && frame_fixed_width(frp));
3194 }
3195 /* Increase "width" if we could not reduce enough frames. */
3196 if (frp == NULL)
3197 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
3199 }
3200 else if (extra_cols > 0)
3201 {
3202 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003203 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 }
3206 topfrp->fr_width = width;
3207}
3208
3209/*
3210 * Add the vertical separator to windows at the right side of "frp".
3211 * Note: Does not check if there is room!
3212 */
3213 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003214frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 win_T *wp;
3217
3218 if (frp->fr_layout == FR_LEAF)
3219 {
3220 wp = frp->fr_win;
3221 if (wp->w_vsep_width == 0)
3222 {
3223 if (wp->w_width > 0) /* don't make it negative */
3224 --wp->w_width;
3225 wp->w_vsep_width = 1;
3226 }
3227 }
3228 else if (frp->fr_layout == FR_COL)
3229 {
3230 /* Handle all the frames in the column. */
3231 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3232 frame_add_vsep(frp);
3233 }
3234 else /* frp->fr_layout == FR_ROW */
3235 {
3236 /* Only need to handle the last frame in the row. */
3237 frp = frp->fr_child;
3238 while (frp->fr_next != NULL)
3239 frp = frp->fr_next;
3240 frame_add_vsep(frp);
3241 }
3242}
3243
3244/*
3245 * Set frame width from the window it contains.
3246 */
3247 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003248frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249{
3250 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3251}
3252#endif
3253
3254/*
3255 * Set frame height from the window it contains.
3256 */
3257 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003258frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259{
3260 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3261}
3262
3263/*
3264 * Compute the minimal height for frame "topfrp".
3265 * Uses the 'winminheight' option.
3266 * When "next_curwin" isn't NULL, use p_wh for this window.
3267 * When "next_curwin" is NOWIN, don't use at least one line for the current
3268 * window.
3269 */
3270 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003271frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 frame_T *frp;
3274 int m;
3275#ifdef FEAT_VERTSPLIT
3276 int n;
3277#endif
3278
3279 if (topfrp->fr_win != NULL)
3280 {
3281 if (topfrp->fr_win == next_curwin)
3282 m = p_wh + topfrp->fr_win->w_status_height;
3283 else
3284 {
3285 /* window: minimal height of the window plus status line */
3286 m = p_wmh + topfrp->fr_win->w_status_height;
3287 /* Current window is minimal one line high */
3288 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3289 ++m;
3290 }
3291 }
3292#ifdef FEAT_VERTSPLIT
3293 else if (topfrp->fr_layout == FR_ROW)
3294 {
3295 /* get the minimal height from each frame in this row */
3296 m = 0;
3297 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3298 {
3299 n = frame_minheight(frp, next_curwin);
3300 if (n > m)
3301 m = n;
3302 }
3303 }
3304#endif
3305 else
3306 {
3307 /* Add up the minimal heights for all frames in this column. */
3308 m = 0;
3309 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3310 m += frame_minheight(frp, next_curwin);
3311 }
3312
3313 return m;
3314}
3315
3316#ifdef FEAT_VERTSPLIT
3317/*
3318 * Compute the minimal width for frame "topfrp".
3319 * When "next_curwin" isn't NULL, use p_wiw for this window.
3320 * When "next_curwin" is NOWIN, don't use at least one column for the current
3321 * window.
3322 */
3323 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003324frame_minwidth(
3325 frame_T *topfrp,
3326 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 frame_T *frp;
3329 int m, n;
3330
3331 if (topfrp->fr_win != NULL)
3332 {
3333 if (topfrp->fr_win == next_curwin)
3334 m = p_wiw + topfrp->fr_win->w_vsep_width;
3335 else
3336 {
3337 /* window: minimal width of the window plus separator column */
3338 m = p_wmw + topfrp->fr_win->w_vsep_width;
3339 /* Current window is minimal one column wide */
3340 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3341 ++m;
3342 }
3343 }
3344 else if (topfrp->fr_layout == FR_COL)
3345 {
3346 /* get the minimal width from each frame in this column */
3347 m = 0;
3348 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3349 {
3350 n = frame_minwidth(frp, next_curwin);
3351 if (n > m)
3352 m = n;
3353 }
3354 }
3355 else
3356 {
3357 /* Add up the minimal widths for all frames in this row. */
3358 m = 0;
3359 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3360 m += frame_minwidth(frp, next_curwin);
3361 }
3362
3363 return m;
3364}
3365#endif
3366
3367
3368/*
3369 * Try to close all windows except current one.
3370 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3371 * used and the buffer was modified.
3372 *
3373 * Used by ":bdel" and ":only".
3374 */
3375 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003376close_others(
3377 int message,
3378 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 win_T *wp;
3381 win_T *nextwp;
3382 int r;
3383
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003384 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 {
3386 if (message
3387#ifdef FEAT_AUTOCMD
3388 && !autocmd_busy
3389#endif
3390 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003391 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 return;
3393 }
3394
3395 /* Be very careful here: autocommands may change the window layout. */
3396 for (wp = firstwin; win_valid(wp); wp = nextwp)
3397 {
3398 nextwp = wp->w_next;
3399 if (wp != curwin) /* don't close current window */
3400 {
3401
3402 /* Check if it's allowed to abandon this window */
3403 r = can_abandon(wp->w_buffer, forceit);
3404#ifdef FEAT_AUTOCMD
3405 if (!win_valid(wp)) /* autocommands messed wp up */
3406 {
3407 nextwp = firstwin;
3408 continue;
3409 }
3410#endif
3411 if (!r)
3412 {
3413#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3414 if (message && (p_confirm || cmdmod.confirm) && p_write)
3415 {
3416 dialog_changed(wp->w_buffer, FALSE);
3417# ifdef FEAT_AUTOCMD
3418 if (!win_valid(wp)) /* autocommands messed wp up */
3419 {
3420 nextwp = firstwin;
3421 continue;
3422 }
3423# endif
3424 }
3425 if (bufIsChanged(wp->w_buffer))
3426#endif
3427 continue;
3428 }
3429 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3430 }
3431 }
3432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003433 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 EMSG(_("E445: Other window contains changes"));
3435}
3436
3437#endif /* FEAT_WINDOWS */
3438
3439/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003440 * Init the current window "curwin".
3441 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 */
3443 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003444curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003446 win_init_empty(curwin);
3447}
3448
3449 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003450win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003451{
3452 redraw_win_later(wp, NOT_VALID);
3453 wp->w_lines_valid = 0;
3454 wp->w_cursor.lnum = 1;
3455 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003457 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003459 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3460 wp->w_pcmark.col = 0;
3461 wp->w_prev_pcmark.lnum = 0;
3462 wp->w_prev_pcmark.col = 0;
3463 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003465 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003467 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003469 if (wp->w_p_rl)
3470 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003472 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003474#ifdef FEAT_SYN_HL
3475 wp->w_s = &wp->w_buffer->b_s;
3476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477}
3478
3479/*
3480 * Allocate the first window and put an empty buffer in it.
3481 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003482 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003484 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003485win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003487 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003488 return FAIL;
3489
3490#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003491 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003492 if (first_tabpage == NULL)
3493 return FAIL;
3494 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003495 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003496#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003497
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003498 return OK;
3499}
3500
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003501#if defined(FEAT_AUTOCMD) || defined(PROTO)
3502/*
3503 * Init "aucmd_win". This can only be done after the first
3504 * window is fully initialized, thus it can't be in win_alloc_first().
3505 */
3506 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003507win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003508{
3509 aucmd_win = win_alloc(NULL, TRUE);
3510 if (aucmd_win != NULL)
3511 {
3512 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003513 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003514 new_frame(aucmd_win);
3515 }
3516}
3517#endif
3518
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003519/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003520 * Allocate the first window or the first window in a new tab page.
3521 * When "oldwin" is NULL create an empty buffer for it.
3522 * When "oldwin" is not NULL copy info from it to the new window (only with
3523 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003524 * Return FAIL when something goes wrong (out of memory).
3525 */
3526 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003527win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003528{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003529 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003530 if (oldwin == NULL)
3531 {
3532 /* Very first window, need to create an empty buffer for it and
3533 * initialize from scratch. */
3534 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3535 if (curwin == NULL || curbuf == NULL)
3536 return FAIL;
3537 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003538#ifdef FEAT_SYN_HL
3539 curwin->w_s = &(curbuf->b_s);
3540#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003541 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003543 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003545 curwin_init(); /* init current window */
3546 }
3547#ifdef FEAT_WINDOWS
3548 else
3549 {
3550 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003551 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003552
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003553 /* We don't want cursor- and scroll-binding in the first window. */
3554 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003555 }
3556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003558 new_frame(curwin);
3559 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003560 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003561 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#ifdef FEAT_VERTSPLIT
3563 topframe->fr_width = Columns;
3564#endif
3565 topframe->fr_height = Rows - p_ch;
3566 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003567
3568 return OK;
3569}
3570
3571/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003572 * Create a frame for window "wp".
3573 */
3574 static void
3575new_frame(win_T *wp)
3576{
3577 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3578
3579 wp->w_frame = frp;
3580 if (frp != NULL)
3581 {
3582 frp->fr_layout = FR_LEAF;
3583 frp->fr_win = wp;
3584 }
3585}
3586
3587/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003588 * Initialize the window and frame size to the maximum.
3589 */
3590 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003591win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592{
3593 firstwin->w_height = ROWS_AVAIL;
3594 topframe->fr_height = ROWS_AVAIL;
3595#ifdef FEAT_VERTSPLIT
3596 firstwin->w_width = Columns;
3597 topframe->fr_width = Columns;
3598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599}
3600
3601#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003602
3603/*
3604 * Allocate a new tabpage_T and init the values.
3605 * Returns NULL when out of memory.
3606 */
3607 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003608alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003609{
3610 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003611# ifdef FEAT_GUI
3612 int i;
3613# endif
3614
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003615
3616 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003617 if (tp == NULL)
3618 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003619
Bram Moolenaar429fa852013-04-15 12:27:36 +02003620# ifdef FEAT_EVAL
3621 /* init t: variables */
3622 tp->tp_vars = dict_alloc();
3623 if (tp->tp_vars == NULL)
3624 {
3625 vim_free(tp);
3626 return NULL;
3627 }
3628 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3629# endif
3630
3631# ifdef FEAT_GUI
3632 for (i = 0; i < 3; i++)
3633 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003634# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003635# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003636 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003637# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003638 tp->tp_ch_used = p_ch;
3639
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003640 return tp;
3641}
3642
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003643 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003644free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003645{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003646 int idx;
3647
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003648# ifdef FEAT_DIFF
3649 diff_clear(tp);
3650# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003651 for (idx = 0; idx < SNAP_COUNT; ++idx)
3652 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003653#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003654 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3655 hash_init(&tp->tp_vars->dv_hashtab);
3656 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003657#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003658
3659#ifdef FEAT_PYTHON
3660 python_tabpage_free(tp);
3661#endif
3662
3663#ifdef FEAT_PYTHON3
3664 python3_tabpage_free(tp);
3665#endif
3666
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003667 vim_free(tp);
3668}
3669
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003670/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003671 * Create a new Tab page with one window.
3672 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003673 * When "after" is 0 put it just after the current Tab page.
3674 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003675 * Return FAIL or OK.
3676 */
3677 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003678win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003679{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003680 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003681 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003682 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003683
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003684 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003685 if (newtp == NULL)
3686 return FAIL;
3687
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003688 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003689 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003690 {
3691 vim_free(newtp);
3692 return FAIL;
3693 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003694 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003695
3696 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003697 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003698 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003699 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003700 if (after == 1)
3701 {
3702 /* New tab page becomes the first one. */
3703 newtp->tp_next = first_tabpage;
3704 first_tabpage = newtp;
3705 }
3706 else
3707 {
3708 if (after > 0)
3709 {
3710 /* Put new tab page before tab page "after". */
3711 n = 2;
3712 for (tp = first_tabpage; tp->tp_next != NULL
3713 && n < after; tp = tp->tp_next)
3714 ++n;
3715 }
3716 newtp->tp_next = tp->tp_next;
3717 tp->tp_next = newtp;
3718 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003719 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003720 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003721 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003722
3723 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003724 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003725
3726#if defined(FEAT_GUI)
3727 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3728 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003729 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003730#endif
3731
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003732 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003733#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003734 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003735 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003736#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003737 return OK;
3738 }
3739
3740 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003741 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003742 return FAIL;
3743}
3744
3745/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003746 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3747 * like with ":split".
3748 * Returns OK if a new tab page was created, FAIL otherwise.
3749 */
3750 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003751may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003752{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003753 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003754
Bram Moolenaard326ce82007-03-11 14:48:29 +00003755 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003756 {
3757 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003758 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003759 return win_new_tabpage(n);
3760 }
3761 return FAIL;
3762}
3763
3764/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003765 * Create up to "maxcount" tabpages with empty windows.
3766 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003767 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003768 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003769make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003770{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003771 int count = maxcount;
3772 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003773
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003774 /* Limit to 'tabpagemax' tabs. */
3775 if (count > p_tpm)
3776 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003777
3778#ifdef FEAT_AUTOCMD
3779 /*
3780 * Don't execute autocommands while creating the tab pages. Must do that
3781 * when putting the buffers in the windows.
3782 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003783 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003784#endif
3785
3786 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003787 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003788 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003789
3790#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003791 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003792#endif
3793
3794 /* return actual number of tab pages */
3795 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003796}
3797
3798/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003799 * Return TRUE when "tpc" points to a valid tab page.
3800 */
3801 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003802valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003803{
3804 tabpage_T *tp;
3805
3806 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3807 if (tp == tpc)
3808 return TRUE;
3809 return FALSE;
3810}
3811
3812/*
3813 * Find tab page "n" (first one is 1). Returns NULL when not found.
3814 */
3815 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003816find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003817{
3818 tabpage_T *tp;
3819 int i = 1;
3820
3821 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3822 ++i;
3823 return tp;
3824}
3825
3826/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003827 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003828 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003829 */
3830 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003831tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003832{
3833 int i = 1;
3834 tabpage_T *tp;
3835
3836 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3837 ++i;
3838 return i;
3839}
3840
3841/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003842 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003843 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003844 * FAIL.
3845 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003846 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003847 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003848leave_tabpage(
3849 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003850 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003851 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003852{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003853 tabpage_T *tp = curtab;
3854
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003855 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003856#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003857 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003858 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003859 if (new_curbuf != curbuf)
3860 {
3861 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3862 if (curtab != tp)
3863 return FAIL;
3864 }
3865 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3866 if (curtab != tp)
3867 return FAIL;
3868 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003869 if (curtab != tp)
3870 return FAIL;
3871 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003872#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003873#if defined(FEAT_GUI)
3874 /* Remove the scrollbars. They may be added back later. */
3875 if (gui.in_use)
3876 gui_remove_scrollbars();
3877#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003878 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003879 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003880 tp->tp_firstwin = firstwin;
3881 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003882 tp->tp_old_Rows = Rows;
3883 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003884 firstwin = NULL;
3885 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003886 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003887}
3888
3889/*
3890 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003891 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003892 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3893 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003894 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003895 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003896enter_tabpage(
3897 tabpage_T *tp,
3898 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003899 int trigger_enter_autocmds,
3900 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003901{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003902 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003903 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003904
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003905 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003906 firstwin = tp->tp_firstwin;
3907 lastwin = tp->tp_lastwin;
3908 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003909
3910 /* We would like doing the TabEnter event first, but we don't have a
3911 * valid current window yet, which may break some commands.
3912 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003913 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3914 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003915 prevwin = next_prevwin;
3916
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003917 last_status(FALSE); /* status line may appear or disappear */
3918 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003919 must_redraw = CLEAR; /* need to redraw everything */
3920#ifdef FEAT_DIFF
3921 diff_need_scrollbind = TRUE;
3922#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003923
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003924 /* The tabpage line may have appeared or disappeared, may need to resize
3925 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003926 * frame sizes too. Use the stored value of p_ch, so that it can be
3927 * different for each tab page. */
3928 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003929 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3930#ifdef FEAT_GUI_TABLINE
3931 && !gui_use_tabline()
3932#endif
3933 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003934 shell_new_rows();
3935#ifdef FEAT_VERTSPLIT
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003936 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003937 shell_new_columns(); /* update window widths */
3938#endif
3939
3940#if defined(FEAT_GUI)
3941 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3942 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003943 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003944#endif
3945
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003946#ifdef FEAT_AUTOCMD
3947 /* Apply autocommands after updating the display, when 'rows' and
3948 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003949 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003950 {
3951 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3952 if (old_curbuf != curbuf)
3953 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3954 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003955#endif
3956
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003957 redraw_all_later(CLEAR);
3958}
3959
3960/*
3961 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003962 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003963 */
3964 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003965goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003966{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003967 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003968 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003969 int i;
3970
Bram Moolenaard68071d2006-05-02 22:08:30 +00003971 if (text_locked())
3972 {
3973 /* Not allowed when editing the command line. */
3974#ifdef FEAT_CMDWIN
3975 if (cmdwin_type != 0)
3976 EMSG(_(e_cmdwin));
3977 else
3978#endif
3979 EMSG(_(e_secure));
3980 return;
3981 }
3982
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003983 /* If there is only one it can't work. */
3984 if (first_tabpage->tp_next == NULL)
3985 {
3986 if (n > 1)
3987 beep_flush();
3988 return;
3989 }
3990
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003991 if (n == 0)
3992 {
3993 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003994 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003995 tp = first_tabpage;
3996 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003997 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003998 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003999 else if (n < 0)
4000 {
4001 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4002 * this N times. */
4003 ttp = curtab;
4004 for (i = n; i < 0; ++i)
4005 {
4006 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4007 tp = tp->tp_next)
4008 ;
4009 ttp = tp;
4010 }
4011 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004012 else if (n == 9999)
4013 {
4014 /* Go to last tab page. */
4015 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4016 ;
4017 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004018 else
4019 {
4020 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004021 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004022 if (tp == NULL)
4023 {
4024 beep_flush();
4025 return;
4026 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004027 }
4028
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004029 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004030
4031#ifdef FEAT_GUI_TABLINE
4032 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004033 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004034#endif
4035}
4036
4037/*
4038 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004039 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4040 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004041 * Note: doesn't update the GUI tab.
4042 */
4043 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004044goto_tabpage_tp(
4045 tabpage_T *tp,
4046 int trigger_enter_autocmds,
4047 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004048{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004049 /* Don't repeat a message in another tab page. */
4050 set_keep_msg(NULL, 0);
4051
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004052 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4053 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004054 {
4055 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004056 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4057 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004058 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004059 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4060 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004061 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004065 * Enter window "wp" in tab page "tp".
4066 * Also updates the GUI tab.
4067 */
4068 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004069goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004070{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004071 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004072 if (curtab == tp && win_valid(wp))
4073 {
4074 win_enter(wp, TRUE);
4075# ifdef FEAT_GUI_TABLINE
4076 if (gui_use_tabline())
4077 gui_mch_set_curtab(tabpage_index(curtab));
4078# endif
4079 }
4080}
4081
4082/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004083 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004084 */
4085 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004086tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004087{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004088 int n = 1;
4089 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004090
4091 if (first_tabpage->tp_next == NULL)
4092 return;
4093
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004094 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4095 ++n;
4096
4097 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4098 && tp->tp_next == curtab))
4099 return;
4100
4101 tp_dst = tp;
4102
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004103 /* Remove the current tab page from the list of tab pages. */
4104 if (curtab == first_tabpage)
4105 first_tabpage = curtab->tp_next;
4106 else
4107 {
4108 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4109 if (tp->tp_next == curtab)
4110 break;
4111 if (tp == NULL) /* "cannot happen" */
4112 return;
4113 tp->tp_next = curtab->tp_next;
4114 }
4115
4116 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004117 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004118 {
4119 curtab->tp_next = first_tabpage;
4120 first_tabpage = curtab;
4121 }
4122 else
4123 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004124 curtab->tp_next = tp_dst->tp_next;
4125 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004126 }
4127
4128 /* Need to redraw the tabline. Tab page contents doesn't change. */
4129 redraw_tabline = TRUE;
4130}
4131
4132
4133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 * Go to another window.
4135 * When jumping to another buffer, stop Visual mode. Do this before
4136 * changing windows so we can yank the selection into the '*' register.
4137 * When jumping to another window on the same buffer, adjust its cursor
4138 * position to keep the same Visual area.
4139 */
4140 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004141win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004143#ifdef FEAT_CONCEAL
4144 win_T *owp = curwin;
4145#endif
4146
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004147 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 {
4149 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004150 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 return;
4152 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004153#ifdef FEAT_AUTOCMD
4154 if (curbuf_locked())
4155 return;
4156#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004157
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 if (wp->w_buffer != curbuf)
4159 reset_VIsual_and_resel();
4160 else if (VIsual_active)
4161 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
4163#ifdef FEAT_GUI
4164 need_mouse_correct = TRUE;
4165#endif
4166 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004167
4168#ifdef FEAT_CONCEAL
4169 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004170 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004171 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004172 if (curwin->w_p_cole > 0 && !msg_scrolled)
4173 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175}
4176
4177#if defined(FEAT_PERL) || defined(PROTO)
4178/*
4179 * Find window number "winnr" (counting top to bottom).
4180 */
4181 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004182win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
4184 win_T *wp;
4185
4186# ifdef FEAT_WINDOWS
4187 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4188 if (--winnr == 0)
4189 break;
4190 return wp;
4191# else
4192 return curwin;
4193# endif
4194}
4195#endif
4196
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004197#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4198 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004199/*
4200 * Find the tabpage for window "win".
4201 */
4202 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004203win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004204{
4205 win_T *wp;
4206 tabpage_T *tp;
4207
4208 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004209 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4210 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004211 if (wp == win)
4212 return tp;
4213 return NULL;
4214}
4215#endif
4216
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217#ifdef FEAT_VERTSPLIT
4218/*
4219 * Move to window above or below "count" times.
4220 */
4221 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004222win_goto_ver(
4223 int up, /* TRUE to go to win above */
4224 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225{
4226 frame_T *fr;
4227 frame_T *nfr;
4228 frame_T *foundfr;
4229
4230 foundfr = curwin->w_frame;
4231 while (count--)
4232 {
4233 /*
4234 * First go upwards in the tree of frames until we find a upwards or
4235 * downwards neighbor.
4236 */
4237 fr = foundfr;
4238 for (;;)
4239 {
4240 if (fr == topframe)
4241 goto end;
4242 if (up)
4243 nfr = fr->fr_prev;
4244 else
4245 nfr = fr->fr_next;
4246 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4247 break;
4248 fr = fr->fr_parent;
4249 }
4250
4251 /*
4252 * Now go downwards to find the bottom or top frame in it.
4253 */
4254 for (;;)
4255 {
4256 if (nfr->fr_layout == FR_LEAF)
4257 {
4258 foundfr = nfr;
4259 break;
4260 }
4261 fr = nfr->fr_child;
4262 if (nfr->fr_layout == FR_ROW)
4263 {
4264 /* Find the frame at the cursor row. */
4265 while (fr->fr_next != NULL
4266 && frame2win(fr)->w_wincol + fr->fr_width
4267 <= curwin->w_wincol + curwin->w_wcol)
4268 fr = fr->fr_next;
4269 }
4270 if (nfr->fr_layout == FR_COL && up)
4271 while (fr->fr_next != NULL)
4272 fr = fr->fr_next;
4273 nfr = fr;
4274 }
4275 }
4276end:
4277 if (foundfr != NULL)
4278 win_goto(foundfr->fr_win);
4279}
4280
4281/*
4282 * Move to left or right window.
4283 */
4284 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004285win_goto_hor(
4286 int left, /* TRUE to go to left win */
4287 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288{
4289 frame_T *fr;
4290 frame_T *nfr;
4291 frame_T *foundfr;
4292
4293 foundfr = curwin->w_frame;
4294 while (count--)
4295 {
4296 /*
4297 * First go upwards in the tree of frames until we find a left or
4298 * right neighbor.
4299 */
4300 fr = foundfr;
4301 for (;;)
4302 {
4303 if (fr == topframe)
4304 goto end;
4305 if (left)
4306 nfr = fr->fr_prev;
4307 else
4308 nfr = fr->fr_next;
4309 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4310 break;
4311 fr = fr->fr_parent;
4312 }
4313
4314 /*
4315 * Now go downwards to find the leftmost or rightmost frame in it.
4316 */
4317 for (;;)
4318 {
4319 if (nfr->fr_layout == FR_LEAF)
4320 {
4321 foundfr = nfr;
4322 break;
4323 }
4324 fr = nfr->fr_child;
4325 if (nfr->fr_layout == FR_COL)
4326 {
4327 /* Find the frame at the cursor row. */
4328 while (fr->fr_next != NULL
4329 && frame2win(fr)->w_winrow + fr->fr_height
4330 <= curwin->w_winrow + curwin->w_wrow)
4331 fr = fr->fr_next;
4332 }
4333 if (nfr->fr_layout == FR_ROW && left)
4334 while (fr->fr_next != NULL)
4335 fr = fr->fr_next;
4336 nfr = fr;
4337 }
4338 }
4339end:
4340 if (foundfr != NULL)
4341 win_goto(foundfr->fr_win);
4342}
4343#endif
4344
4345/*
4346 * Make window "wp" the current window.
4347 */
4348 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004349win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004351 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352}
4353
4354/*
4355 * Make window wp the current window.
4356 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4357 * been closed and isn't valid.
4358 */
4359 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004360win_enter_ext(
4361 win_T *wp,
4362 int undo_sync,
4363 int curwin_invalid,
4364 int trigger_enter_autocmds UNUSED,
4365 int trigger_leave_autocmds UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366{
4367#ifdef FEAT_AUTOCMD
4368 int other_buffer = FALSE;
4369#endif
4370
4371 if (wp == curwin && !curwin_invalid) /* nothing to do */
4372 return;
4373
4374#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004375 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 {
4377 /*
4378 * Be careful: If autocommands delete the window, return now.
4379 */
4380 if (wp->w_buffer != curbuf)
4381 {
4382 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4383 other_buffer = TRUE;
4384 if (!win_valid(wp))
4385 return;
4386 }
4387 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4388 if (!win_valid(wp))
4389 return;
4390# ifdef FEAT_EVAL
4391 /* autocmds may abort script processing */
4392 if (aborting())
4393 return;
4394# endif
4395 }
4396#endif
4397
4398 /* sync undo before leaving the current buffer */
4399 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004400 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004401
4402 /* Might need to scroll the old window before switching, e.g., when the
4403 * cursor was moved. */
4404 update_topline();
4405
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 /* may have to copy the buffer options when 'cpo' contains 'S' */
4407 if (wp->w_buffer != curbuf)
4408 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4409 if (!curwin_invalid)
4410 {
4411 prevwin = curwin; /* remember for CTRL-W p */
4412 curwin->w_redr_status = TRUE;
4413 }
4414 curwin = wp;
4415 curbuf = wp->w_buffer;
4416 check_cursor();
4417#ifdef FEAT_VIRTUALEDIT
4418 if (!virtual_active())
4419 curwin->w_cursor.coladd = 0;
4420#endif
4421 changed_line_abv_curs(); /* assume cursor position needs updating */
4422
4423 if (curwin->w_localdir != NULL)
4424 {
4425 /* Window has a local directory: Save current directory as global
4426 * directory (unless that was done already) and change to the local
4427 * directory. */
4428 if (globaldir == NULL)
4429 {
4430 char_u cwd[MAXPATHL];
4431
4432 if (mch_dirname(cwd, MAXPATHL) == OK)
4433 globaldir = vim_strsave(cwd);
4434 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004435 if (mch_chdir((char *)curwin->w_localdir) == 0)
4436 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 }
4438 else if (globaldir != NULL)
4439 {
4440 /* Window doesn't have a local directory and we are not in the global
4441 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004442 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 vim_free(globaldir);
4444 globaldir = NULL;
4445 shorten_fnames(TRUE);
4446 }
4447
4448#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004449 if (trigger_enter_autocmds)
4450 {
4451 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4452 if (other_buffer)
4453 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455#endif
4456
4457#ifdef FEAT_TITLE
4458 maketitle();
4459#endif
4460 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004461 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (restart_edit)
4463 redraw_later(VALID); /* causes status line redraw */
4464
4465 /* set window height to desired minimal value */
4466 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4467 win_setheight((int)p_wh);
4468 else if (curwin->w_height == 0)
4469 win_setheight(1);
4470
4471#ifdef FEAT_VERTSPLIT
4472 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004473 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 win_setwidth((int)p_wiw);
4475#endif
4476
4477#ifdef FEAT_MOUSE
4478 setmouse(); /* in case jumped to/from help buffer */
4479#endif
4480
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004481 /* Change directories when the 'acd' option is set. */
4482 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483}
4484
4485#endif /* FEAT_WINDOWS */
4486
4487#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4488/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004489 * Jump to the first open window that contains buffer "buf", if one exists.
4490 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 */
4492 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004493buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004495 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004497 if (curwin->w_buffer == buf)
4498 wp = curwin;
4499# ifdef FEAT_WINDOWS
4500 else
4501 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4502 if (wp->w_buffer == buf)
4503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (wp != NULL)
4505 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004507 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004509
4510/*
4511 * Jump to the first open window in any tab page that contains buffer "buf",
4512 * if one exists.
4513 * Returns a pointer to the window found, otherwise NULL.
4514 */
4515 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004516buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004517{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004518 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004519# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004520 tabpage_T *tp;
4521
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004522 if (wp != NULL)
4523 return wp;
4524
4525 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4526 if (tp != curtab)
4527 {
4528 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4529 if (wp->w_buffer == buf)
4530 break;
4531 if (wp != NULL)
4532 {
4533 goto_tabpage_win(tp, wp);
4534 if (curwin != wp)
4535 wp = NULL; /* something went wrong */
4536 break;
4537 }
4538 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004539# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004540 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004541}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542#endif
4543
Bram Moolenaar86edef62016-03-13 18:07:30 +01004544static int last_win_id = 0;
4545
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004547 * Allocate a window structure and link it in the window list when "hidden" is
4548 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004551win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004553 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555 /*
4556 * allocate window structure and linesizes arrays
4557 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004558 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004559 if (new_wp == NULL)
4560 return NULL;
4561
4562 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004564 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004565 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567
Bram Moolenaar86edef62016-03-13 18:07:30 +01004568 new_wp->w_id = ++last_win_id;
4569
Bram Moolenaar429fa852013-04-15 12:27:36 +02004570#ifdef FEAT_EVAL
4571 /* init w: variables */
4572 new_wp->w_vars = dict_alloc();
4573 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004575 win_free_lsize(new_wp);
4576 vim_free(new_wp);
4577 return NULL;
4578 }
4579 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004580#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004581
4582#ifdef FEAT_AUTOCMD
4583 /* Don't execute autocommands while the window is not properly
4584 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4585 * event. */
4586 block_autocmds();
4587#endif
4588 /*
4589 * link the window in the window list
4590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004592 if (!hidden)
4593 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#endif
4595#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004596 new_wp->w_wincol = 0;
4597 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#endif
4599
Bram Moolenaar429fa852013-04-15 12:27:36 +02004600 /* position the display and the cursor at the top of the file. */
4601 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004603 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004605 new_wp->w_botline = 2;
4606 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004608 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609#endif
4610
Bram Moolenaar429fa852013-04-15 12:27:36 +02004611 /* We won't calculate w_fraction until resizing the window */
4612 new_wp->w_fraction = 0;
4613 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614
4615#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004616 if (gui.in_use)
4617 {
4618 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4619 SBAR_LEFT, new_wp);
4620 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4621 SBAR_RIGHT, new_wp);
4622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623#endif
4624#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004625 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004627#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004628 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004629#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004630#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004631 new_wp->w_match_head = NULL;
4632 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004633#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004634 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635}
4636
4637#if defined(FEAT_WINDOWS) || defined(PROTO)
4638
4639/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004640 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 */
4642 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004643win_free(
4644 win_T *wp,
4645 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
4647 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004648 buf_T *buf;
4649 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004651#ifdef FEAT_FOLDING
4652 clearFolding(wp);
4653#endif
4654
4655 /* reduce the reference count to the argument list. */
4656 alist_unlink(wp->w_alist);
4657
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004658#ifdef FEAT_AUTOCMD
4659 /* Don't execute autocommands while the window is halfway being deleted.
4660 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004661 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004662#endif
4663
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004664#ifdef FEAT_LUA
4665 lua_window_free(wp);
4666#endif
4667
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004668#ifdef FEAT_MZSCHEME
4669 mzscheme_window_free(wp);
4670#endif
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#ifdef FEAT_PERL
4673 perl_win_free(wp);
4674#endif
4675
4676#ifdef FEAT_PYTHON
4677 python_window_free(wp);
4678#endif
4679
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004680#ifdef FEAT_PYTHON3
4681 python3_window_free(wp);
4682#endif
4683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684#ifdef FEAT_TCL
4685 tcl_window_free(wp);
4686#endif
4687
4688#ifdef FEAT_RUBY
4689 ruby_window_free(wp);
4690#endif
4691
4692 clear_winopt(&wp->w_onebuf_opt);
4693 clear_winopt(&wp->w_allbuf_opt);
4694
4695#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004696 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4697 hash_init(&wp->w_vars->dv_hashtab);
4698 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#endif
4700
4701 if (prevwin == wp)
4702 prevwin = NULL;
4703 win_free_lsize(wp);
4704
4705 for (i = 0; i < wp->w_tagstacklen; ++i)
4706 vim_free(wp->w_tagstack[i].tagname);
4707
4708 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004709
Bram Moolenaarff18df02013-07-24 17:51:57 +02004710 /* Remove the window from the b_wininfo lists, it may happen that the
4711 * freed memory is re-used for another window. */
4712 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4713 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4714 if (wip->wi_win == wp)
4715 wip->wi_win = NULL;
4716
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004718 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#ifdef FEAT_JUMPLIST
4722 free_jumplist(wp);
4723#endif
4724
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004725#ifdef FEAT_QUICKFIX
4726 qf_free_all(wp);
4727#endif
4728
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729#ifdef FEAT_GUI
4730 if (gui.in_use)
4731 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4733 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4734 }
4735#endif /* FEAT_GUI */
4736
Bram Moolenaar860cae12010-06-05 23:22:07 +02004737#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004738 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004739#endif
4740
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004741#ifdef FEAT_AUTOCMD
4742 if (wp != aucmd_win)
4743#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004744 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004745#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004746 if (autocmd_busy)
4747 {
4748 wp->w_next = au_pending_free_win;
4749 au_pending_free_win = wp;
4750 }
4751 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004752#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004753 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004754
4755#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004756 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758}
4759
4760/*
4761 * Append window "wp" in the window list after window "after".
4762 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004763 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004764win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
4766 win_T *before;
4767
4768 if (after == NULL) /* after NULL is in front of the first */
4769 before = firstwin;
4770 else
4771 before = after->w_next;
4772
4773 wp->w_next = before;
4774 wp->w_prev = after;
4775 if (after == NULL)
4776 firstwin = wp;
4777 else
4778 after->w_next = wp;
4779 if (before == NULL)
4780 lastwin = wp;
4781 else
4782 before->w_prev = wp;
4783}
4784
4785/*
4786 * Remove a window from the window list.
4787 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004788 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004789win_remove(
4790 win_T *wp,
4791 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 if (wp->w_prev != NULL)
4794 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004795 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004797 else
4798 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 if (wp->w_next != NULL)
4800 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004801 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004803 else
4804 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805}
4806
4807/*
4808 * Append frame "frp" in a frame list after frame "after".
4809 */
4810 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004811frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
4813 frp->fr_next = after->fr_next;
4814 after->fr_next = frp;
4815 if (frp->fr_next != NULL)
4816 frp->fr_next->fr_prev = frp;
4817 frp->fr_prev = after;
4818}
4819
4820/*
4821 * Insert frame "frp" in a frame list before frame "before".
4822 */
4823 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004824frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
4826 frp->fr_next = before;
4827 frp->fr_prev = before->fr_prev;
4828 before->fr_prev = frp;
4829 if (frp->fr_prev != NULL)
4830 frp->fr_prev->fr_next = frp;
4831 else
4832 frp->fr_parent->fr_child = frp;
4833}
4834
4835/*
4836 * Remove a frame from a frame list.
4837 */
4838 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004839frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
4841 if (frp->fr_prev != NULL)
4842 frp->fr_prev->fr_next = frp->fr_next;
4843 else
4844 frp->fr_parent->fr_child = frp->fr_next;
4845 if (frp->fr_next != NULL)
4846 frp->fr_next->fr_prev = frp->fr_prev;
4847}
4848
4849#endif /* FEAT_WINDOWS */
4850
4851/*
4852 * Allocate w_lines[] for window "wp".
4853 * Return FAIL for failure, OK for success.
4854 */
4855 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004856win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
4858 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004859 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (wp->w_lines == NULL)
4861 return FAIL;
4862 return OK;
4863}
4864
4865/*
4866 * free lsize arrays for a window
4867 */
4868 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004869win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004871 /* TODO: why would wp be NULL here? */
4872 if (wp != NULL)
4873 {
4874 vim_free(wp->w_lines);
4875 wp->w_lines = NULL;
4876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877}
4878
4879/*
4880 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004881 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 */
4883 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004884shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004886 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
4888 if (firstwin == NULL) /* not initialized yet */
4889 return;
4890#ifdef FEAT_WINDOWS
4891 if (h < frame_minheight(topframe, NULL))
4892 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004893
4894 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 * that doesn't result in the right height, forget about that option. */
4896 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004897 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 frame_new_height(topframe, h, FALSE, FALSE);
4899
4900 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4901#else
4902 if (h < 1)
4903 h = 1;
4904 win_new_height(firstwin, h);
4905#endif
4906 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004907#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004908 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004909#endif
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#if 0
4912 /* Disabled: don't want making the screen smaller make a window larger. */
4913 if (p_ea)
4914 win_equal(curwin, FALSE, 'v');
4915#endif
4916}
4917
4918#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4919/*
4920 * Called from win_new_shellsize() after Columns changed.
4921 */
4922 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004923shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924{
4925 if (firstwin == NULL) /* not initialized yet */
4926 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004927
4928 /* First try setting the widths of windows with 'winfixwidth'. If that
4929 * doesn't result in the right width, forget about that option. */
4930 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004931 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004932 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4935#if 0
4936 /* Disabled: don't want making the screen smaller make a window larger. */
4937 if (p_ea)
4938 win_equal(curwin, FALSE, 'h');
4939#endif
4940}
4941#endif
4942
4943#if defined(FEAT_CMDWIN) || defined(PROTO)
4944/*
4945 * Save the size of all windows in "gap".
4946 */
4947 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004948win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950{
4951 win_T *wp;
4952
4953 ga_init2(gap, (int)sizeof(int), 1);
4954 if (ga_grow(gap, win_count() * 2) == OK)
4955 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4956 {
4957 ((int *)gap->ga_data)[gap->ga_len++] =
4958 wp->w_width + wp->w_vsep_width;
4959 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4960 }
4961}
4962
4963/*
4964 * Restore window sizes, but only if the number of windows is still the same.
4965 * Does not free the growarray.
4966 */
4967 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004968win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969{
4970 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004971 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973 if (win_count() * 2 == gap->ga_len)
4974 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004975 /* The order matters, because frames contain other frames, but it's
4976 * difficult to get right. The easy way out is to do it twice. */
4977 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004979 i = 0;
4980 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4981 {
4982 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4983 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986 /* recompute the window positions */
4987 (void)win_comp_pos();
4988 }
4989}
4990#endif /* FEAT_CMDWIN */
4991
4992#if defined(FEAT_WINDOWS) || defined(PROTO)
4993/*
4994 * Update the position for all windows, using the width and height of the
4995 * frames.
4996 * Returns the row just after the last window.
4997 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004998 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004999win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005001 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 int col = 0;
5003
5004 frame_comp_pos(topframe, &row, &col);
5005 return row;
5006}
5007
5008/*
5009 * Update the position of the windows in frame "topfrp", using the width and
5010 * height of the frames.
5011 * "*row" and "*col" are the top-left position of the frame. They are updated
5012 * to the bottom-right position plus one.
5013 */
5014 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005015frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016{
5017 win_T *wp;
5018 frame_T *frp;
5019#ifdef FEAT_VERTSPLIT
5020 int startcol;
5021 int startrow;
5022#endif
5023
5024 wp = topfrp->fr_win;
5025 if (wp != NULL)
5026 {
5027 if (wp->w_winrow != *row
5028#ifdef FEAT_VERTSPLIT
5029 || wp->w_wincol != *col
5030#endif
5031 )
5032 {
5033 /* position changed, redraw */
5034 wp->w_winrow = *row;
5035#ifdef FEAT_VERTSPLIT
5036 wp->w_wincol = *col;
5037#endif
5038 redraw_win_later(wp, NOT_VALID);
5039 wp->w_redr_status = TRUE;
5040 }
5041 *row += wp->w_height + wp->w_status_height;
5042#ifdef FEAT_VERTSPLIT
5043 *col += wp->w_width + wp->w_vsep_width;
5044#endif
5045 }
5046 else
5047 {
5048#ifdef FEAT_VERTSPLIT
5049 startrow = *row;
5050 startcol = *col;
5051#endif
5052 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5053 {
5054#ifdef FEAT_VERTSPLIT
5055 if (topfrp->fr_layout == FR_ROW)
5056 *row = startrow; /* all frames are at the same row */
5057 else
5058 *col = startcol; /* all frames are at the same col */
5059#endif
5060 frame_comp_pos(frp, row, col);
5061 }
5062 }
5063}
5064
5065#endif /* FEAT_WINDOWS */
5066
5067/*
5068 * Set current window height and take care of repositioning other windows to
5069 * fit around it.
5070 */
5071 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005072win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073{
5074 win_setheight_win(height, curwin);
5075}
5076
5077/*
5078 * Set the window height of window "win" and take care of repositioning other
5079 * windows to fit around it.
5080 */
5081 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005082win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
5084 int row;
5085
5086 if (win == curwin)
5087 {
5088 /* Always keep current window at least one line high, even when
5089 * 'winminheight' is zero. */
5090#ifdef FEAT_WINDOWS
5091 if (height < p_wmh)
5092 height = p_wmh;
5093#endif
5094 if (height == 0)
5095 height = 1;
5096 }
5097
5098#ifdef FEAT_WINDOWS
5099 frame_setheight(win->w_frame, height + win->w_status_height);
5100
5101 /* recompute the window positions */
5102 row = win_comp_pos();
5103#else
5104 if (height > topframe->fr_height)
5105 height = topframe->fr_height;
5106 win->w_height = height;
5107 row = height;
5108#endif
5109
5110 /*
5111 * If there is extra space created between the last window and the command
5112 * line, clear it.
5113 */
5114 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5115 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5116 cmdline_row = row;
5117 msg_row = row;
5118 msg_col = 0;
5119
5120 redraw_all_later(NOT_VALID);
5121}
5122
5123#if defined(FEAT_WINDOWS) || defined(PROTO)
5124
5125/*
5126 * Set the height of a frame to "height" and take care that all frames and
5127 * windows inside it are resized. Also resize frames on the left and right if
5128 * the are in the same FR_ROW frame.
5129 *
5130 * Strategy:
5131 * If the frame is part of a FR_COL frame, try fitting the frame in that
5132 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5133 * go to containing frames to resize them and make room.
5134 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5135 * Check for the minimal height of the FR_ROW frame.
5136 * At the top level we can also use change the command line height.
5137 */
5138 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005139frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140{
5141 int room; /* total number of lines available */
5142 int take; /* number of lines taken from other windows */
5143 int room_cmdline; /* lines available from cmdline */
5144 int run;
5145 frame_T *frp;
5146 int h;
5147 int room_reserved;
5148
5149 /* If the height already is the desired value, nothing to do. */
5150 if (curfrp->fr_height == height)
5151 return;
5152
5153 if (curfrp->fr_parent == NULL)
5154 {
5155 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005156 if (height > ROWS_AVAIL)
5157 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (height > 0)
5159 frame_new_height(curfrp, height, FALSE, FALSE);
5160 }
5161 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5162 {
5163 /* Row of frames: Also need to resize frames left and right of this
5164 * one. First check for the minimal height of these. */
5165 h = frame_minheight(curfrp->fr_parent, NULL);
5166 if (height < h)
5167 height = h;
5168 frame_setheight(curfrp->fr_parent, height);
5169 }
5170 else
5171 {
5172 /*
5173 * Column of frames: try to change only frames in this column.
5174 */
5175#ifdef FEAT_VERTSPLIT
5176 /*
5177 * Do this twice:
5178 * 1: compute room available, if it's not enough try resizing the
5179 * containing frame.
5180 * 2: compute the room available and adjust the height to it.
5181 * Try not to reduce the height of a window with 'winfixheight' set.
5182 */
5183 for (run = 1; run <= 2; ++run)
5184#else
5185 for (;;)
5186#endif
5187 {
5188 room = 0;
5189 room_reserved = 0;
5190 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5191 frp = frp->fr_next)
5192 {
5193 if (frp != curfrp
5194 && frp->fr_win != NULL
5195 && frp->fr_win->w_p_wfh)
5196 room_reserved += frp->fr_height;
5197 room += frp->fr_height;
5198 if (frp != curfrp)
5199 room -= frame_minheight(frp, NULL);
5200 }
5201#ifdef FEAT_VERTSPLIT
5202 if (curfrp->fr_width != Columns)
5203 room_cmdline = 0;
5204 else
5205#endif
5206 {
5207 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5208 + lastwin->w_height + lastwin->w_status_height);
5209 if (room_cmdline < 0)
5210 room_cmdline = 0;
5211 }
5212
5213 if (height <= room + room_cmdline)
5214 break;
5215#ifdef FEAT_VERTSPLIT
5216 if (run == 2 || curfrp->fr_width == Columns)
5217#endif
5218 {
5219 if (height > room + room_cmdline)
5220 height = room + room_cmdline;
5221 break;
5222 }
5223#ifdef FEAT_VERTSPLIT
5224 frame_setheight(curfrp->fr_parent, height
5225 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5226#endif
5227 /*NOTREACHED*/
5228 }
5229
5230 /*
5231 * Compute the number of lines we will take from others frames (can be
5232 * negative!).
5233 */
5234 take = height - curfrp->fr_height;
5235
5236 /* If there is not enough room, also reduce the height of a window
5237 * with 'winfixheight' set. */
5238 if (height > room + room_cmdline - room_reserved)
5239 room_reserved = room + room_cmdline - height;
5240 /* If there is only a 'winfixheight' window and making the
5241 * window smaller, need to make the other window taller. */
5242 if (take < 0 && room - curfrp->fr_height < room_reserved)
5243 room_reserved = 0;
5244
5245 if (take > 0 && room_cmdline > 0)
5246 {
5247 /* use lines from cmdline first */
5248 if (take < room_cmdline)
5249 room_cmdline = take;
5250 take -= room_cmdline;
5251 topframe->fr_height += room_cmdline;
5252 }
5253
5254 /*
5255 * set the current frame to the new height
5256 */
5257 frame_new_height(curfrp, height, FALSE, FALSE);
5258
5259 /*
5260 * First take lines from the frames after the current frame. If
5261 * that is not enough, takes lines from frames above the current
5262 * frame.
5263 */
5264 for (run = 0; run < 2; ++run)
5265 {
5266 if (run == 0)
5267 frp = curfrp->fr_next; /* 1st run: start with next window */
5268 else
5269 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5270 while (frp != NULL && take != 0)
5271 {
5272 h = frame_minheight(frp, NULL);
5273 if (room_reserved > 0
5274 && frp->fr_win != NULL
5275 && frp->fr_win->w_p_wfh)
5276 {
5277 if (room_reserved >= frp->fr_height)
5278 room_reserved -= frp->fr_height;
5279 else
5280 {
5281 if (frp->fr_height - room_reserved > take)
5282 room_reserved = frp->fr_height - take;
5283 take -= frp->fr_height - room_reserved;
5284 frame_new_height(frp, room_reserved, FALSE, FALSE);
5285 room_reserved = 0;
5286 }
5287 }
5288 else
5289 {
5290 if (frp->fr_height - take < h)
5291 {
5292 take -= frp->fr_height - h;
5293 frame_new_height(frp, h, FALSE, FALSE);
5294 }
5295 else
5296 {
5297 frame_new_height(frp, frp->fr_height - take,
5298 FALSE, FALSE);
5299 take = 0;
5300 }
5301 }
5302 if (run == 0)
5303 frp = frp->fr_next;
5304 else
5305 frp = frp->fr_prev;
5306 }
5307 }
5308 }
5309}
5310
5311#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5312/*
5313 * Set current window width and take care of repositioning other windows to
5314 * fit around it.
5315 */
5316 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005317win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
5319 win_setwidth_win(width, curwin);
5320}
5321
5322 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005323win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324{
5325 /* Always keep current window at least one column wide, even when
5326 * 'winminwidth' is zero. */
5327 if (wp == curwin)
5328 {
5329 if (width < p_wmw)
5330 width = p_wmw;
5331 if (width == 0)
5332 width = 1;
5333 }
5334
5335 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5336
5337 /* recompute the window positions */
5338 (void)win_comp_pos();
5339
5340 redraw_all_later(NOT_VALID);
5341}
5342
5343/*
5344 * Set the width of a frame to "width" and take care that all frames and
5345 * windows inside it are resized. Also resize frames above and below if the
5346 * are in the same FR_ROW frame.
5347 *
5348 * Strategy is similar to frame_setheight().
5349 */
5350 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005351frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
5353 int room; /* total number of lines available */
5354 int take; /* number of lines taken from other windows */
5355 int run;
5356 frame_T *frp;
5357 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005358 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359
5360 /* If the width already is the desired value, nothing to do. */
5361 if (curfrp->fr_width == width)
5362 return;
5363
5364 if (curfrp->fr_parent == NULL)
5365 /* topframe: can't change width */
5366 return;
5367
5368 if (curfrp->fr_parent->fr_layout == FR_COL)
5369 {
5370 /* Column of frames: Also need to resize frames above and below of
5371 * this one. First check for the minimal width of these. */
5372 w = frame_minwidth(curfrp->fr_parent, NULL);
5373 if (width < w)
5374 width = w;
5375 frame_setwidth(curfrp->fr_parent, width);
5376 }
5377 else
5378 {
5379 /*
5380 * Row of frames: try to change only frames in this row.
5381 *
5382 * Do this twice:
5383 * 1: compute room available, if it's not enough try resizing the
5384 * containing frame.
5385 * 2: compute the room available and adjust the width to it.
5386 */
5387 for (run = 1; run <= 2; ++run)
5388 {
5389 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005390 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5392 frp = frp->fr_next)
5393 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005394 if (frp != curfrp
5395 && frp->fr_win != NULL
5396 && frp->fr_win->w_p_wfw)
5397 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 room += frp->fr_width;
5399 if (frp != curfrp)
5400 room -= frame_minwidth(frp, NULL);
5401 }
5402
5403 if (width <= room)
5404 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005405 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 if (width > room)
5408 width = room;
5409 break;
5410 }
5411 frame_setwidth(curfrp->fr_parent, width
5412 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5413 }
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 /*
5416 * Compute the number of lines we will take from others frames (can be
5417 * negative!).
5418 */
5419 take = width - curfrp->fr_width;
5420
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005421 /* If there is not enough room, also reduce the width of a window
5422 * with 'winfixwidth' set. */
5423 if (width > room - room_reserved)
5424 room_reserved = room - width;
5425 /* If there is only a 'winfixwidth' window and making the
5426 * window smaller, need to make the other window narrower. */
5427 if (take < 0 && room - curfrp->fr_width < room_reserved)
5428 room_reserved = 0;
5429
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 /*
5431 * set the current frame to the new width
5432 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005433 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
5435 /*
5436 * First take lines from the frames right of the current frame. If
5437 * that is not enough, takes lines from frames left of the current
5438 * frame.
5439 */
5440 for (run = 0; run < 2; ++run)
5441 {
5442 if (run == 0)
5443 frp = curfrp->fr_next; /* 1st run: start with next window */
5444 else
5445 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5446 while (frp != NULL && take != 0)
5447 {
5448 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005449 if (room_reserved > 0
5450 && frp->fr_win != NULL
5451 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005453 if (room_reserved >= frp->fr_width)
5454 room_reserved -= frp->fr_width;
5455 else
5456 {
5457 if (frp->fr_width - room_reserved > take)
5458 room_reserved = frp->fr_width - take;
5459 take -= frp->fr_width - room_reserved;
5460 frame_new_width(frp, room_reserved, FALSE, FALSE);
5461 room_reserved = 0;
5462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 }
5464 else
5465 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005466 if (frp->fr_width - take < w)
5467 {
5468 take -= frp->fr_width - w;
5469 frame_new_width(frp, w, FALSE, FALSE);
5470 }
5471 else
5472 {
5473 frame_new_width(frp, frp->fr_width - take,
5474 FALSE, FALSE);
5475 take = 0;
5476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 if (run == 0)
5479 frp = frp->fr_next;
5480 else
5481 frp = frp->fr_prev;
5482 }
5483 }
5484 }
5485}
5486#endif /* FEAT_VERTSPLIT */
5487
5488/*
5489 * Check 'winminheight' for a valid value.
5490 */
5491 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005492win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 int room;
5495 int first = TRUE;
5496 win_T *wp;
5497
5498 /* loop until there is a 'winminheight' that is possible */
5499 while (p_wmh > 0)
5500 {
5501 /* TODO: handle vertical splits */
5502 room = -p_wh;
5503 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5504 room += wp->w_height - p_wmh;
5505 if (room >= 0)
5506 break;
5507 --p_wmh;
5508 if (first)
5509 {
5510 EMSG(_(e_noroom));
5511 first = FALSE;
5512 }
5513 }
5514}
5515
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005516#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518/*
5519 * Status line of dragwin is dragged "offset" lines down (negative is up).
5520 */
5521 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005522win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523{
5524 frame_T *curfr;
5525 frame_T *fr;
5526 int room;
5527 int row;
5528 int up; /* if TRUE, drag status line up, otherwise down */
5529 int n;
5530
5531 fr = dragwin->w_frame;
5532 curfr = fr;
5533 if (fr != topframe) /* more than one window */
5534 {
5535 fr = fr->fr_parent;
5536 /* When the parent frame is not a column of frames, its parent should
5537 * be. */
5538 if (fr->fr_layout != FR_COL)
5539 {
5540 curfr = fr;
5541 if (fr != topframe) /* only a row of windows, may drag statusline */
5542 fr = fr->fr_parent;
5543 }
5544 }
5545
5546 /* If this is the last frame in a column, may want to resize the parent
5547 * frame instead (go two up to skip a row of frames). */
5548 while (curfr != topframe && curfr->fr_next == NULL)
5549 {
5550 if (fr != topframe)
5551 fr = fr->fr_parent;
5552 curfr = fr;
5553 if (fr != topframe)
5554 fr = fr->fr_parent;
5555 }
5556
5557 if (offset < 0) /* drag up */
5558 {
5559 up = TRUE;
5560 offset = -offset;
5561 /* sum up the room of the current frame and above it */
5562 if (fr == curfr)
5563 {
5564 /* only one window */
5565 room = fr->fr_height - frame_minheight(fr, NULL);
5566 }
5567 else
5568 {
5569 room = 0;
5570 for (fr = fr->fr_child; ; fr = fr->fr_next)
5571 {
5572 room += fr->fr_height - frame_minheight(fr, NULL);
5573 if (fr == curfr)
5574 break;
5575 }
5576 }
5577 fr = curfr->fr_next; /* put fr at frame that grows */
5578 }
5579 else /* drag down */
5580 {
5581 up = FALSE;
5582 /*
5583 * Only dragging the last status line can reduce p_ch.
5584 */
5585 room = Rows - cmdline_row;
5586 if (curfr->fr_next == NULL)
5587 room -= 1;
5588 else
5589 room -= p_ch;
5590 if (room < 0)
5591 room = 0;
5592 /* sum up the room of frames below of the current one */
5593 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5594 room += fr->fr_height - frame_minheight(fr, NULL);
5595 fr = curfr; /* put fr at window that grows */
5596 }
5597
5598 if (room < offset) /* Not enough room */
5599 offset = room; /* Move as far as we can */
5600 if (offset <= 0)
5601 return;
5602
5603 /*
5604 * Grow frame fr by "offset" lines.
5605 * Doesn't happen when dragging the last status line up.
5606 */
5607 if (fr != NULL)
5608 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5609
5610 if (up)
5611 fr = curfr; /* current frame gets smaller */
5612 else
5613 fr = curfr->fr_next; /* next frame gets smaller */
5614
5615 /*
5616 * Now make the other frames smaller.
5617 */
5618 while (fr != NULL && offset > 0)
5619 {
5620 n = frame_minheight(fr, NULL);
5621 if (fr->fr_height - offset <= n)
5622 {
5623 offset -= fr->fr_height - n;
5624 frame_new_height(fr, n, !up, FALSE);
5625 }
5626 else
5627 {
5628 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5629 break;
5630 }
5631 if (up)
5632 fr = fr->fr_prev;
5633 else
5634 fr = fr->fr_next;
5635 }
5636 row = win_comp_pos();
5637 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5638 cmdline_row = row;
5639 p_ch = Rows - cmdline_row;
5640 if (p_ch < 1)
5641 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005642 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005643 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 showmode();
5645}
5646
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005647# if defined(FEAT_VERTSPLIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648/*
5649 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5650 */
5651 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005652win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 frame_T *curfr;
5655 frame_T *fr;
5656 int room;
5657 int left; /* if TRUE, drag separator line left, otherwise right */
5658 int n;
5659
5660 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005661 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 return;
5663 curfr = fr;
5664 fr = fr->fr_parent;
5665 /* When the parent frame is not a row of frames, its parent should be. */
5666 if (fr->fr_layout != FR_ROW)
5667 {
5668 if (fr == topframe) /* only a column of windows (cannot happen?) */
5669 return;
5670 curfr = fr;
5671 fr = fr->fr_parent;
5672 }
5673
5674 /* If this is the last frame in a row, may want to resize a parent
5675 * frame instead. */
5676 while (curfr->fr_next == NULL)
5677 {
5678 if (fr == topframe)
5679 break;
5680 curfr = fr;
5681 fr = fr->fr_parent;
5682 if (fr != topframe)
5683 {
5684 curfr = fr;
5685 fr = fr->fr_parent;
5686 }
5687 }
5688
5689 if (offset < 0) /* drag left */
5690 {
5691 left = TRUE;
5692 offset = -offset;
5693 /* sum up the room of the current frame and left of it */
5694 room = 0;
5695 for (fr = fr->fr_child; ; fr = fr->fr_next)
5696 {
5697 room += fr->fr_width - frame_minwidth(fr, NULL);
5698 if (fr == curfr)
5699 break;
5700 }
5701 fr = curfr->fr_next; /* put fr at frame that grows */
5702 }
5703 else /* drag right */
5704 {
5705 left = FALSE;
5706 /* sum up the room of frames right of the current one */
5707 room = 0;
5708 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5709 room += fr->fr_width - frame_minwidth(fr, NULL);
5710 fr = curfr; /* put fr at window that grows */
5711 }
5712
5713 if (room < offset) /* Not enough room */
5714 offset = room; /* Move as far as we can */
5715 if (offset <= 0) /* No room at all, quit. */
5716 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005717 if (fr == NULL)
5718 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005721 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
5723 /* shrink other frames: current and at the left or at the right */
5724 if (left)
5725 fr = curfr; /* current frame gets smaller */
5726 else
5727 fr = curfr->fr_next; /* next frame gets smaller */
5728
5729 while (fr != NULL && offset > 0)
5730 {
5731 n = frame_minwidth(fr, NULL);
5732 if (fr->fr_width - offset <= n)
5733 {
5734 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005735 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737 else
5738 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005739 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 break;
5741 }
5742 if (left)
5743 fr = fr->fr_prev;
5744 else
5745 fr = fr->fr_next;
5746 }
5747 (void)win_comp_pos();
5748 redraw_all_later(NOT_VALID);
5749}
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005750# endif /* FEAT_VERTSPLIT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#endif /* FEAT_MOUSE */
5752
5753#endif /* FEAT_WINDOWS */
5754
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005755#define FRACTION_MULT 16384L
5756
5757/*
5758 * Set wp->w_fraction for the current w_wrow and w_height.
5759 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005760 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005761set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005762{
5763 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005764 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005765}
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767/*
5768 * Set the height of a window.
5769 * This takes care of the things inside the window, not what happens to the
5770 * window position, the frame or to other windows.
5771 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005772 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005773win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 linenr_T lnum;
5776 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005777 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 /* Don't want a negative height. Happens when splitting a tiny window.
5780 * Will equalize heights soon to fix it. */
5781 if (height < 0)
5782 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005783 if (wp->w_height == height)
5784 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005786 if (wp->w_height > 0)
5787 {
5788 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005789 /* w_wrow needs to be valid. When setting 'laststatus' this may
5790 * call win_new_height() recursively. */
5791 validate_cursor();
5792 if (wp->w_height != prev_height)
5793 return; /* Recursive call already changed the size, bail out here
5794 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005795 if (wp->w_wrow != wp->w_prev_fraction_row)
5796 set_fraction(wp);
5797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
5799 wp->w_height = height;
5800 wp->w_skipcol = 0;
5801
5802 /* Don't change w_topline when height is zero. Don't set w_topline when
5803 * 'scrollbind' is set and this isn't the current window. */
5804 if (height > 0
5805#ifdef FEAT_SCROLLBIND
5806 && (!wp->w_p_scb || wp == curwin)
5807#endif
5808 )
5809 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005810 /*
5811 * Find a value for w_topline that shows the cursor at the same
5812 * relative position in the window as before (more or less).
5813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 lnum = wp->w_cursor.lnum;
5815 if (lnum < 1) /* can happen when starting up */
5816 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005817 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5818 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5820 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005821
5822 if (sline >= 0)
5823 {
5824 /* Make sure the whole cursor line is visible, if possible. */
5825 int rows = plines_win(wp, lnum, FALSE);
5826
5827 if (sline > wp->w_height - rows)
5828 {
5829 sline = wp->w_height - rows;
5830 wp->w_wrow -= rows - line_size;
5831 }
5832 }
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if (sline < 0)
5835 {
5836 /*
5837 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005838 * Make cursor line the first line in the window. If not enough
5839 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 */
5841 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005842 if (wp->w_wrow >= wp->w_height
5843 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5844 {
5845 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5846 --wp->w_wrow;
5847 while (wp->w_wrow >= wp->w_height)
5848 {
5849 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5850 + win_col_off2(wp);
5851 --wp->w_wrow;
5852 }
5853 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005854 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005856 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005858 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 {
5860#ifdef FEAT_FOLDING
5861 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5862 if (lnum == 1)
5863 {
5864 /* first line in buffer is folded */
5865 line_size = 1;
5866 --sline;
5867 break;
5868 }
5869#endif
5870 --lnum;
5871#ifdef FEAT_DIFF
5872 if (lnum == wp->w_topline)
5873 line_size = plines_win_nofill(wp, lnum, TRUE)
5874 + wp->w_topfill;
5875 else
5876#endif
5877 line_size = plines_win(wp, lnum, TRUE);
5878 sline -= line_size;
5879 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005880
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 if (sline < 0)
5882 {
5883 /*
5884 * Line we want at top would go off top of screen. Use next
5885 * line instead.
5886 */
5887#ifdef FEAT_FOLDING
5888 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5889#endif
5890 lnum++;
5891 wp->w_wrow -= line_size + sline;
5892 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005893 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 {
5895 /* First line of file reached, use that as topline. */
5896 lnum = 1;
5897 wp->w_wrow -= sline;
5898 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005899
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005900 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 }
5903
5904 if (wp == curwin)
5905 {
5906 if (p_so)
5907 update_topline();
5908 curs_columns(FALSE); /* validate w_wrow */
5909 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005910 if (prev_height > 0)
5911 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912
5913 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005914 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915#ifdef FEAT_WINDOWS
5916 wp->w_redr_status = TRUE;
5917#endif
5918 invalidate_botline_win(wp);
5919}
5920
5921#ifdef FEAT_VERTSPLIT
5922/*
5923 * Set the width of a window.
5924 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005925 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005926win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927{
5928 wp->w_width = width;
5929 wp->w_lines_valid = 0;
5930 changed_line_abv_curs_win(wp);
5931 invalidate_botline_win(wp);
5932 if (wp == curwin)
5933 {
5934 update_topline();
5935 curs_columns(TRUE); /* validate w_wrow */
5936 }
5937 redraw_win_later(wp, NOT_VALID);
5938 wp->w_redr_status = TRUE;
5939}
5940#endif
5941
5942 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005943win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5946 if (wp->w_p_scr == 0)
5947 wp->w_p_scr = 1;
5948}
5949
5950/*
5951 * command_height: called whenever p_ch has been changed
5952 */
5953 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005954command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956#ifdef FEAT_WINDOWS
5957 int h;
5958 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005959 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005961 /* Use the value of p_ch that we remembered. This is needed for when the
5962 * GUI starts up, we can't be sure in what order things happen. And when
5963 * p_ch was changed in another tab page. */
5964 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005965
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 /* Find bottom frame with width of screen. */
5967 frp = lastwin->w_frame;
5968# ifdef FEAT_VERTSPLIT
5969 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5970 frp = frp->fr_parent;
5971# endif
5972
5973 /* Avoid changing the height of a window with 'winfixheight' set. */
5974 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5975 && frp->fr_win->w_p_wfh)
5976 frp = frp->fr_prev;
5977
5978 if (starting != NO_SCREEN)
5979 {
5980 cmdline_row = Rows - p_ch;
5981
5982 if (p_ch > old_p_ch) /* p_ch got bigger */
5983 {
5984 while (p_ch > old_p_ch)
5985 {
5986 if (frp == NULL)
5987 {
5988 EMSG(_(e_noroom));
5989 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005990 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 cmdline_row = Rows - p_ch;
5992 break;
5993 }
5994 h = frp->fr_height - frame_minheight(frp, NULL);
5995 if (h > p_ch - old_p_ch)
5996 h = p_ch - old_p_ch;
5997 old_p_ch += h;
5998 frame_add_height(frp, -h);
5999 frp = frp->fr_prev;
6000 }
6001
6002 /* Recompute window positions. */
6003 (void)win_comp_pos();
6004
6005 /* clear the lines added to cmdline */
6006 if (full_screen)
6007 screen_fill((int)(cmdline_row), (int)Rows, 0,
6008 (int)Columns, ' ', ' ', 0);
6009 msg_row = cmdline_row;
6010 redraw_cmdline = TRUE;
6011 return;
6012 }
6013
6014 if (msg_row < cmdline_row)
6015 msg_row = cmdline_row;
6016 redraw_cmdline = TRUE;
6017 }
6018 frame_add_height(frp, (int)(old_p_ch - p_ch));
6019
6020 /* Recompute window positions. */
6021 if (frp != lastwin->w_frame)
6022 (void)win_comp_pos();
6023#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006025 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026#endif
6027}
6028
6029#if defined(FEAT_WINDOWS) || defined(PROTO)
6030/*
6031 * Resize frame "frp" to be "n" lines higher (negative for less high).
6032 * Also resize the frames it is contained in.
6033 */
6034 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006035frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036{
6037 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6038 for (;;)
6039 {
6040 frp = frp->fr_parent;
6041 if (frp == NULL)
6042 break;
6043 frp->fr_height += n;
6044 }
6045}
6046
6047/*
6048 * Add or remove a status line for the bottom window(s), according to the
6049 * value of 'laststatus'.
6050 */
6051 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006052last_status(
6053 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 /* Don't make a difference between horizontal or vertical split. */
6056 last_status_rec(topframe, (p_ls == 2
6057 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6058}
6059
6060 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006061last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
6063 frame_T *fp;
6064 win_T *wp;
6065
6066 if (fr->fr_layout == FR_LEAF)
6067 {
6068 wp = fr->fr_win;
6069 if (wp->w_status_height != 0 && !statusline)
6070 {
6071 /* remove status line */
6072 win_new_height(wp, wp->w_height + 1);
6073 wp->w_status_height = 0;
6074 comp_col();
6075 }
6076 else if (wp->w_status_height == 0 && statusline)
6077 {
6078 /* Find a frame to take a line from. */
6079 fp = fr;
6080 while (fp->fr_height <= frame_minheight(fp, NULL))
6081 {
6082 if (fp == topframe)
6083 {
6084 EMSG(_(e_noroom));
6085 return;
6086 }
6087 /* In a column of frames: go to frame above. If already at
6088 * the top or in a row of frames: go to parent. */
6089 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6090 fp = fp->fr_prev;
6091 else
6092 fp = fp->fr_parent;
6093 }
6094 wp->w_status_height = 1;
6095 if (fp != fr)
6096 {
6097 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6098 frame_fix_height(wp);
6099 (void)win_comp_pos();
6100 }
6101 else
6102 win_new_height(wp, wp->w_height - 1);
6103 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006104 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 }
6106 }
6107#ifdef FEAT_VERTSPLIT
6108 else if (fr->fr_layout == FR_ROW)
6109 {
6110 /* vertically split windows, set status line for each one */
6111 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6112 last_status_rec(fp, statusline);
6113 }
6114#endif
6115 else
6116 {
6117 /* horizontally split window, set status line for last one */
6118 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6119 ;
6120 last_status_rec(fp, statusline);
6121 }
6122}
6123
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006124/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006125 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006126 */
6127 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006128tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006129{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006130#ifdef FEAT_GUI_TABLINE
6131 /* When the GUI has the tabline then this always returns zero. */
6132 if (gui_use_tabline())
6133 return 0;
6134#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006135 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006136 {
6137 case 0: return 0;
6138 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6139 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006140 return 1;
6141}
6142
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143#endif /* FEAT_WINDOWS */
6144
6145#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6146/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006147 * Get the file name at the cursor.
6148 * If Visual mode is active, use the selected text if it's in one line.
6149 * Returns the name in allocated memory, NULL for failure.
6150 */
6151 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006152grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006153{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006154 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6155
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006156 if (VIsual_active)
6157 {
6158 int len;
6159 char_u *ptr;
6160
6161 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6162 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006163 return find_file_name_in_path(ptr, len, options,
6164 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006165 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006166 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006167
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006168}
6169
6170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 * Return the file name under or after the cursor.
6172 *
6173 * The 'path' option is searched if the file name is not absolute.
6174 * The string returned has been alloc'ed and should be freed by the caller.
6175 * NULL is returned if the file name or file is not found.
6176 *
6177 * options:
6178 * FNAME_MESS give error messages
6179 * FNAME_EXP expand to path
6180 * FNAME_HYP check for hypertext link
6181 * FNAME_INCL apply "includeexpr"
6182 */
6183 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006184file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
6186 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006187 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6188 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189}
6190
6191/*
6192 * Return the name of the file under or after ptr[col].
6193 * Otherwise like file_name_at_cursor().
6194 */
6195 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006196file_name_in_line(
6197 char_u *line,
6198 int col,
6199 int options,
6200 long count,
6201 char_u *rel_fname, /* file we are searching relative to */
6202 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 char_u *ptr;
6205 int len;
6206
6207 /*
6208 * search forward for what could be the start of a file name
6209 */
6210 ptr = line + col;
6211 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006212 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 if (*ptr == NUL) /* nothing found */
6214 {
6215 if (options & FNAME_MESS)
6216 EMSG(_("E446: No file name under cursor"));
6217 return NULL;
6218 }
6219
6220 /*
6221 * Search backward for first char of the file name.
6222 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6223 */
6224 while (ptr > line)
6225 {
6226#ifdef FEAT_MBYTE
6227 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6228 ptr -= len + 1;
6229 else
6230#endif
6231 if (vim_isfilec(ptr[-1])
6232 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6233 --ptr;
6234 else
6235 break;
6236 }
6237
6238 /*
6239 * Search forward for the last char of the file name.
6240 * Also allow "://" when ':' is not in 'isfname'.
6241 */
6242 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006243 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006245 {
6246 if (ptr[len] == '\\')
6247 /* Skip over the "\" in "\ ". */
6248 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249#ifdef FEAT_MBYTE
6250 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006251 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 else
6253#endif
6254 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256
6257 /*
6258 * If there is trailing punctuation, remove it.
6259 * But don't remove "..", could be a directory name.
6260 */
6261 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6262 && ptr[len - 2] != '.')
6263 --len;
6264
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006265 if (file_lnum != NULL)
6266 {
6267 char_u *p;
6268
6269 /* Get the number after the file name and a separator character */
6270 p = ptr + len;
6271 p = skipwhite(p);
6272 if (*p != NUL)
6273 {
6274 if (!isdigit(*p))
6275 ++p; /* skip the separator */
6276 p = skipwhite(p);
6277 if (isdigit(*p))
6278 *file_lnum = (int)getdigits(&p);
6279 }
6280 }
6281
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6283}
6284
6285# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006286static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287
6288 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006289eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 char_u *res;
6292
6293 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006294 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006295 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 set_vim_var_string(VV_FNAME, NULL, 0);
6297 return res;
6298}
6299#endif
6300
6301/*
6302 * Return the name of the file ptr[len] in 'path'.
6303 * Otherwise like file_name_at_cursor().
6304 */
6305 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006306find_file_name_in_path(
6307 char_u *ptr,
6308 int len,
6309 int options,
6310 long count,
6311 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312{
6313 char_u *file_name;
6314 int c;
6315# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6316 char_u *tofree = NULL;
6317
6318 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6319 {
6320 tofree = eval_includeexpr(ptr, len);
6321 if (tofree != NULL)
6322 {
6323 ptr = tofree;
6324 len = (int)STRLEN(ptr);
6325 }
6326 }
6327# endif
6328
6329 if (options & FNAME_EXP)
6330 {
6331 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6332 TRUE, rel_fname);
6333
6334# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6335 /*
6336 * If the file could not be found in a normal way, try applying
6337 * 'includeexpr' (unless done already).
6338 */
6339 if (file_name == NULL
6340 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6341 {
6342 tofree = eval_includeexpr(ptr, len);
6343 if (tofree != NULL)
6344 {
6345 ptr = tofree;
6346 len = (int)STRLEN(ptr);
6347 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6348 TRUE, rel_fname);
6349 }
6350 }
6351# endif
6352 if (file_name == NULL && (options & FNAME_MESS))
6353 {
6354 c = ptr[len];
6355 ptr[len] = NUL;
6356 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6357 ptr[len] = c;
6358 }
6359
6360 /* Repeat finding the file "count" times. This matters when it
6361 * appears several times in the path. */
6362 while (file_name != NULL && --count > 0)
6363 {
6364 vim_free(file_name);
6365 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6366 }
6367 }
6368 else
6369 file_name = vim_strnsave(ptr, len);
6370
6371# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6372 vim_free(tofree);
6373# endif
6374
6375 return file_name;
6376}
6377#endif /* FEAT_SEARCHPATH */
6378
6379/*
6380 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6381 * Also check for ":\\", which MS Internet Explorer accepts, return
6382 * URL_BACKSLASH.
6383 */
6384 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006385path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 if (STRNCMP(p, "://", (size_t)3) == 0)
6388 return URL_SLASH;
6389 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6390 return URL_BACKSLASH;
6391 return 0;
6392}
6393
6394/*
6395 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6396 * Return URL_BACKSLASH for "name:\\".
6397 * Return zero otherwise.
6398 */
6399 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006400path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
6402 char_u *p;
6403
6404 for (p = fname; isalpha(*p); ++p)
6405 ;
6406 return path_is_url(p);
6407}
6408
6409/*
6410 * Return TRUE if "name" is a full (absolute) path name or URL.
6411 */
6412 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006413vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 return (path_with_url(name) != 0 || mch_isFullName(name));
6416}
6417
6418/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006419 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 *
6421 * return FAIL for failure, OK otherwise
6422 */
6423 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006424vim_FullName(
6425 char_u *fname,
6426 char_u *buf,
6427 int len,
6428 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429{
6430 int retval = OK;
6431 int url;
6432
6433 *buf = NUL;
6434 if (fname == NULL)
6435 return FAIL;
6436
6437 url = path_with_url(fname);
6438 if (!url)
6439 retval = mch_FullName(fname, buf, len, force);
6440 if (url || retval == FAIL)
6441 {
6442 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006443 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01006445#if defined(MACOS_CLASSIC) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 slash_adjust(buf);
6447#endif
6448 return retval;
6449}
6450
6451/*
6452 * Return the minimal number of rows that is needed on the screen to display
6453 * the current number of windows.
6454 */
6455 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006456min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006459#ifdef FEAT_WINDOWS
6460 tabpage_T *tp;
6461 int n;
6462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 if (firstwin == NULL) /* not initialized yet */
6465 return MIN_LINES;
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006468 total = 0;
6469 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6470 {
6471 n = frame_minheight(tp->tp_topframe, NULL);
6472 if (total < n)
6473 total = n;
6474 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006475 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006477 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006479 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 return total;
6481}
6482
6483/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006484 * Return TRUE if there is only one window (in the current tab page), not
6485 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006486 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 */
6488 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006489only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491#ifdef FEAT_WINDOWS
6492 int count = 0;
6493 win_T *wp;
6494
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495 /* If there is another tab page there always is another window. */
6496 if (first_tabpage->tp_next != NULL)
6497 return FALSE;
6498
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006500 if (wp->w_buffer != NULL
6501 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502# ifdef FEAT_QUICKFIX
6503 || wp->w_p_pvw
6504# endif
6505 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006506# ifdef FEAT_AUTOCMD
6507 && wp != aucmd_win
6508# endif
6509 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 ++count;
6511 return (count <= 1);
6512#else
6513 return TRUE;
6514#endif
6515}
6516
6517#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6518/*
6519 * Correct the cursor line number in other windows. Used after changing the
6520 * current buffer, and before applying autocommands.
6521 * When "do_curwin" is TRUE, also check current window.
6522 */
6523 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006524check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525{
6526 win_T *wp;
6527
6528#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006529 tabpage_T *tp;
6530
6531 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6533#else
6534 wp = curwin;
6535 if (do_curwin)
6536#endif
6537 {
6538 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6539 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6540 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6541 wp->w_topline = curbuf->b_ml.ml_line_count;
6542 }
6543}
6544#endif
6545
6546#if defined(FEAT_WINDOWS) || defined(PROTO)
6547
6548/*
6549 * A snapshot of the window sizes, to restore them after closing the help
6550 * window.
6551 * Only these fields are used:
6552 * fr_layout
6553 * fr_width
6554 * fr_height
6555 * fr_next
6556 * fr_child
6557 * fr_win (only valid for the old curwin, NULL otherwise)
6558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559
6560/*
6561 * Create a snapshot of the current frame sizes.
6562 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006563 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006564make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006566 clear_snapshot(curtab, idx);
6567 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568}
6569
6570 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006571make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6574 if (*frp == NULL)
6575 return;
6576 (*frp)->fr_layout = fr->fr_layout;
6577# ifdef FEAT_VERTSPLIT
6578 (*frp)->fr_width = fr->fr_width;
6579# endif
6580 (*frp)->fr_height = fr->fr_height;
6581 if (fr->fr_next != NULL)
6582 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6583 if (fr->fr_child != NULL)
6584 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6585 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6586 (*frp)->fr_win = curwin;
6587}
6588
6589/*
6590 * Remove any existing snapshot.
6591 */
6592 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006593clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006595 clear_snapshot_rec(tp->tp_snapshot[idx]);
6596 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597}
6598
6599 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006600clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 if (fr != NULL)
6603 {
6604 clear_snapshot_rec(fr->fr_next);
6605 clear_snapshot_rec(fr->fr_child);
6606 vim_free(fr);
6607 }
6608}
6609
6610/*
6611 * Restore a previously created snapshot, if there is any.
6612 * This is only done if the screen size didn't change and the window layout is
6613 * still the same.
6614 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006615 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006616restore_snapshot(
6617 int idx,
6618 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 win_T *wp;
6621
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006622 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006624 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006626 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6627 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006629 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 win_comp_pos();
6631 if (wp != NULL && close_curwin)
6632 win_goto(wp);
6633 redraw_all_later(CLEAR);
6634 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006635 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636}
6637
6638/*
6639 * Check if frames "sn" and "fr" have the same layout, same following frames
6640 * and same children.
6641 */
6642 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006643check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
6645 if (sn->fr_layout != fr->fr_layout
6646 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6647 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6648 || (sn->fr_next != NULL
6649 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6650 || (sn->fr_child != NULL
6651 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6652 return FAIL;
6653 return OK;
6654}
6655
6656/*
6657 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6658 * following frames and children.
6659 * Returns a pointer to the old current window, or NULL.
6660 */
6661 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006662restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
6664 win_T *wp = NULL;
6665 win_T *wp2;
6666
6667 fr->fr_height = sn->fr_height;
6668# ifdef FEAT_VERTSPLIT
6669 fr->fr_width = sn->fr_width;
6670# endif
6671 if (fr->fr_layout == FR_LEAF)
6672 {
6673 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6674# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006675 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676# endif
6677 wp = sn->fr_win;
6678 }
6679 if (sn->fr_next != NULL)
6680 {
6681 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6682 if (wp2 != NULL)
6683 wp = wp2;
6684 }
6685 if (sn->fr_child != NULL)
6686 {
6687 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6688 if (wp2 != NULL)
6689 wp = wp2;
6690 }
6691 return wp;
6692}
6693
6694#endif
6695
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006696#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6697 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006698/*
6699 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006700 * restore_win() MUST be called to undo, also when FAIL is returned.
6701 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006702 * When "no_display" is TRUE the display won't be affected, no redraw is
6703 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006704 * Returns FAIL if switching to "win" failed.
6705 */
6706 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006707switch_win(
6708 win_T **save_curwin UNUSED,
6709 tabpage_T **save_curtab UNUSED,
6710 win_T *win UNUSED,
6711 tabpage_T *tp UNUSED,
6712 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006713{
6714# ifdef FEAT_AUTOCMD
6715 block_autocmds();
6716# endif
6717# ifdef FEAT_WINDOWS
6718 *save_curwin = curwin;
6719 if (tp != NULL)
6720 {
6721 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006722 if (no_display)
6723 {
6724 curtab->tp_firstwin = firstwin;
6725 curtab->tp_lastwin = lastwin;
6726 curtab = tp;
6727 firstwin = curtab->tp_firstwin;
6728 lastwin = curtab->tp_lastwin;
6729 }
6730 else
6731 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006732 }
6733 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006734 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006735 curwin = win;
6736 curbuf = curwin->w_buffer;
6737# endif
6738 return OK;
6739}
6740
6741/*
6742 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006743 * When "no_display" is TRUE the display won't be affected, no redraw is
6744 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006745 */
6746 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006747restore_win(
6748 win_T *save_curwin UNUSED,
6749 tabpage_T *save_curtab UNUSED,
6750 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006751{
6752# ifdef FEAT_WINDOWS
6753 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006754 {
6755 if (no_display)
6756 {
6757 curtab->tp_firstwin = firstwin;
6758 curtab->tp_lastwin = lastwin;
6759 curtab = save_curtab;
6760 firstwin = curtab->tp_firstwin;
6761 lastwin = curtab->tp_lastwin;
6762 }
6763 else
6764 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6765 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006766 if (win_valid(save_curwin))
6767 {
6768 curwin = save_curwin;
6769 curbuf = curwin->w_buffer;
6770 }
6771# endif
6772# ifdef FEAT_AUTOCMD
6773 unblock_autocmds();
6774# endif
6775}
6776
6777/*
6778 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6779 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6780 */
6781 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006782switch_buffer(buf_T **save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006783{
6784# ifdef FEAT_AUTOCMD
6785 block_autocmds();
6786# endif
6787 *save_curbuf = curbuf;
6788 --curbuf->b_nwindows;
6789 curbuf = buf;
6790 curwin->w_buffer = buf;
6791 ++curbuf->b_nwindows;
6792}
6793
6794/*
6795 * Restore the current buffer after using switch_buffer().
6796 */
6797 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006798restore_buffer(buf_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006799{
6800# ifdef FEAT_AUTOCMD
6801 unblock_autocmds();
6802# endif
6803 /* Check for valid buffer, just in case. */
6804 if (buf_valid(save_curbuf))
6805 {
6806 --curbuf->b_nwindows;
6807 curwin->w_buffer = save_curbuf;
6808 curbuf = save_curbuf;
6809 ++curbuf->b_nwindows;
6810 }
6811}
6812#endif
6813
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6815/*
6816 * Return TRUE if there is any vertically split window.
6817 */
6818 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006819win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820{
6821 frame_T *fr;
6822
6823 if (topframe->fr_layout == FR_ROW)
6824 return TRUE;
6825
6826 if (topframe->fr_layout == FR_COL)
6827 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6828 if (fr->fr_layout == FR_ROW)
6829 return TRUE;
6830
6831 return FALSE;
6832}
6833#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006834
6835#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6836/*
6837 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006838 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006839 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6840 * If no particular ID is desired, -1 must be specified for 'id'.
6841 * Return ID of added match, -1 on failure.
6842 */
6843 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006844match_add(
6845 win_T *wp,
6846 char_u *grp,
6847 char_u *pat,
6848 int prio,
6849 int id,
6850 list_T *pos_list,
6851 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006852{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006853 matchitem_T *cur;
6854 matchitem_T *prev;
6855 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006856 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006857 regprog_T *regprog = NULL;
6858 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006859
Bram Moolenaarb3414592014-06-17 17:48:32 +02006860 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006861 return -1;
6862 if (id < -1 || id == 0)
6863 {
6864 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6865 return -1;
6866 }
6867 if (id != -1)
6868 {
6869 cur = wp->w_match_head;
6870 while (cur != NULL)
6871 {
6872 if (cur->id == id)
6873 {
6874 EMSGN("E801: ID already taken: %ld", id);
6875 return -1;
6876 }
6877 cur = cur->next;
6878 }
6879 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006880 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006881 {
6882 EMSG2(_(e_nogroup), grp);
6883 return -1;
6884 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006885 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006886 {
6887 EMSG2(_(e_invarg2), pat);
6888 return -1;
6889 }
6890
6891 /* Find available match ID. */
6892 while (id == -1)
6893 {
6894 cur = wp->w_match_head;
6895 while (cur != NULL && cur->id != wp->w_next_match_id)
6896 cur = cur->next;
6897 if (cur == NULL)
6898 id = wp->w_next_match_id;
6899 wp->w_next_match_id++;
6900 }
6901
6902 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006903 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006904 m->id = id;
6905 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006906 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006907 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006908 m->match.regprog = regprog;
6909 m->match.rmm_ic = FALSE;
6910 m->match.rmm_maxcol = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02006911#ifdef FEAT_CONCEAL
6912 m->conceal_char = 0;
6913 if (conceal_char != NULL)
6914 m->conceal_char = (*mb_ptr2char)(conceal_char);
6915#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006916
Bram Moolenaarb3414592014-06-17 17:48:32 +02006917 /* Set up position matches */
6918 if (pos_list != NULL)
6919 {
6920 linenr_T toplnum = 0;
6921 linenr_T botlnum = 0;
6922 listitem_T *li;
6923 int i;
6924
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006925 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006926 i++, li = li->li_next)
6927 {
6928 linenr_T lnum = 0;
6929 colnr_T col = 0;
6930 int len = 1;
6931 list_T *subl;
6932 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006933 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006934
Bram Moolenaarb3414592014-06-17 17:48:32 +02006935 if (li->li_tv.v_type == VAR_LIST)
6936 {
6937 subl = li->li_tv.vval.v_list;
6938 if (subl == NULL)
6939 goto fail;
6940 subli = subl->lv_first;
6941 if (subli == NULL)
6942 goto fail;
6943 lnum = get_tv_number_chk(&subli->li_tv, &error);
6944 if (error == TRUE)
6945 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006946 if (lnum == 0)
6947 {
6948 --i;
6949 continue;
6950 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006951 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006952 subli = subli->li_next;
6953 if (subli != NULL)
6954 {
6955 col = get_tv_number_chk(&subli->li_tv, &error);
6956 if (error == TRUE)
6957 goto fail;
6958 subli = subli->li_next;
6959 if (subli != NULL)
6960 {
6961 len = get_tv_number_chk(&subli->li_tv, &error);
6962 if (error == TRUE)
6963 goto fail;
6964 }
6965 }
6966 m->pos.pos[i].col = col;
6967 m->pos.pos[i].len = len;
6968 }
6969 else if (li->li_tv.v_type == VAR_NUMBER)
6970 {
6971 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006972 {
6973 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006974 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006975 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006976 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6977 m->pos.pos[i].col = 0;
6978 m->pos.pos[i].len = 0;
6979 }
6980 else
6981 {
6982 EMSG(_("List or number required"));
6983 goto fail;
6984 }
6985 if (toplnum == 0 || lnum < toplnum)
6986 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006987 if (botlnum == 0 || lnum >= botlnum)
6988 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006989 }
6990
6991 /* Calculate top and bottom lines for redrawing area */
6992 if (toplnum != 0)
6993 {
6994 if (wp->w_buffer->b_mod_set)
6995 {
6996 if (wp->w_buffer->b_mod_top > toplnum)
6997 wp->w_buffer->b_mod_top = toplnum;
6998 if (wp->w_buffer->b_mod_bot < botlnum)
6999 wp->w_buffer->b_mod_bot = botlnum;
7000 }
7001 else
7002 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007003 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007004 wp->w_buffer->b_mod_top = toplnum;
7005 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007006 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007007 }
7008 m->pos.toplnum = toplnum;
7009 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007010 rtype = VALID;
7011 }
7012 }
7013
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007014 /* Insert new match. The match list is in ascending order with regard to
7015 * the match priorities. */
7016 cur = wp->w_match_head;
7017 prev = cur;
7018 while (cur != NULL && prio >= cur->priority)
7019 {
7020 prev = cur;
7021 cur = cur->next;
7022 }
7023 if (cur == prev)
7024 wp->w_match_head = m;
7025 else
7026 prev->next = m;
7027 m->next = cur;
7028
Bram Moolenaarb3414592014-06-17 17:48:32 +02007029 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007030 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007031
7032fail:
7033 vim_free(m);
7034 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007035}
7036
7037/*
7038 * Delete match with ID 'id' in the match list of window 'wp'.
7039 * Print error messages if 'perr' is TRUE.
7040 */
7041 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007042match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007043{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007044 matchitem_T *cur = wp->w_match_head;
7045 matchitem_T *prev = cur;
7046 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007047
7048 if (id < 1)
7049 {
7050 if (perr == TRUE)
7051 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7052 id);
7053 return -1;
7054 }
7055 while (cur != NULL && cur->id != id)
7056 {
7057 prev = cur;
7058 cur = cur->next;
7059 }
7060 if (cur == NULL)
7061 {
7062 if (perr == TRUE)
7063 EMSGN("E803: ID not found: %ld", id);
7064 return -1;
7065 }
7066 if (cur == prev)
7067 wp->w_match_head = cur->next;
7068 else
7069 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007070 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007071 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007072 if (cur->pos.toplnum != 0)
7073 {
7074 if (wp->w_buffer->b_mod_set)
7075 {
7076 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7077 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7078 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7079 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7080 }
7081 else
7082 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007083 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007084 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7085 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007086 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007087 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007088 rtype = VALID;
7089 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007090 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007091 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007092 return 0;
7093}
7094
7095/*
7096 * Delete all matches in the match list of window 'wp'.
7097 */
7098 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007099clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007100{
7101 matchitem_T *m;
7102
7103 while (wp->w_match_head != NULL)
7104 {
7105 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007106 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007107 vim_free(wp->w_match_head->pattern);
7108 vim_free(wp->w_match_head);
7109 wp->w_match_head = m;
7110 }
7111 redraw_later(SOME_VALID);
7112}
7113
7114/*
7115 * Get match from ID 'id' in window 'wp'.
7116 * Return NULL if match not found.
7117 */
7118 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007119get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007120{
7121 matchitem_T *cur = wp->w_match_head;
7122
7123 while (cur != NULL && cur->id != id)
7124 cur = cur->next;
7125 return cur;
7126}
7127#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007128
7129#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7130 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007131get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007132{
7133 int i = 1;
7134 win_T *w;
7135
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007136 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007137 ++i;
7138
7139 if (w == NULL)
7140 return 0;
7141 else
7142 return i;
7143}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007144
7145 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007146get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007147{
7148 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007149# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007150 tabpage_T *t;
7151
7152 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7153 ++i;
7154
7155 if (t == NULL)
7156 return 0;
7157 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007158# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007159 return i;
7160}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007161#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007162
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007163#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007164/*
7165 * Return TRUE if "topfrp" and its children are at the right height.
7166 */
7167 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007168frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007169{
7170 frame_T *frp;
7171
7172 if (topfrp->fr_height != height)
7173 return FALSE;
7174
7175 if (topfrp->fr_layout == FR_ROW)
7176 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7177 if (frp->fr_height != height)
7178 return FALSE;
7179
7180 return TRUE;
7181}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007182#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007183
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007184#if defined(FEAT_VERTSPLIT) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007185/*
7186 * Return TRUE if "topfrp" and its children are at the right width.
7187 */
7188 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007189frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007190{
7191 frame_T *frp;
7192
7193 if (topfrp->fr_width != width)
7194 return FALSE;
7195
7196 if (topfrp->fr_layout == FR_COL)
7197 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7198 if (frp->fr_width != width)
7199 return FALSE;
7200
7201 return TRUE;
7202}
7203#endif
7204
Bram Moolenaar86edef62016-03-13 18:07:30 +01007205#if defined(FEAT_EVAL) || defined(PROTO)
7206 int
7207win_getid(typval_T *argvars)
7208{
7209 int winnr;
7210 win_T *wp;
7211
7212 if (argvars[0].v_type == VAR_UNKNOWN)
7213 return curwin->w_id;
7214 winnr = get_tv_number(&argvars[0]);
7215 if (winnr > 0)
7216 {
7217 if (argvars[1].v_type == VAR_UNKNOWN)
7218 wp = firstwin;
7219 else
7220 {
7221 tabpage_T *tp;
7222 int tabnr = get_tv_number(&argvars[1]);
7223
7224 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7225 if (--tabnr == 0)
7226 break;
7227 if (tp == NULL)
7228 return -1;
7229 wp = tp->tp_firstwin;
7230 }
7231 for ( ; wp != NULL; wp = wp->w_next)
7232 if (--winnr == 0)
7233 return wp->w_id;
7234 }
7235 return 0;
7236}
7237
7238 int
7239win_gotoid(typval_T *argvars)
7240{
7241 win_T *wp;
7242 tabpage_T *tp;
7243 int id = get_tv_number(&argvars[0]);
7244
7245 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7246 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7247 wp != NULL; wp = wp->w_next)
7248 if (wp->w_id == id)
7249 {
7250 goto_tabpage_win(tp, wp);
7251 return 1;
7252 }
7253 return 0;
7254}
7255
7256 void
7257win_id2tabwin(typval_T *argvars, list_T *list)
7258{
7259 win_T *wp;
7260 tabpage_T *tp;
7261 int winnr = 1;
7262 int tabnr = 1;
7263 int id = get_tv_number(&argvars[0]);
7264
7265 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7266 {
7267 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7268 wp != NULL; wp = wp->w_next)
7269 {
7270 if (wp->w_id == id)
7271 {
7272 list_append_number(list, tabnr);
7273 list_append_number(list, winnr);
7274 return;
7275 }
7276 ++winnr;
7277 }
7278 ++tabnr;
7279 winnr = 1;
7280 }
7281 list_append_number(list, 0);
7282 list_append_number(list, 0);
7283}
7284
7285 int
7286win_id2win(typval_T *argvars)
7287{
7288 win_T *wp;
7289 int nr = 1;
7290 int id = get_tv_number(&argvars[0]);
7291
7292 for (wp = firstwin; wp != NULL; wp = wp->w_next)
7293 {
7294 if (wp->w_id == id)
7295 return nr;
7296 ++nr;
7297 }
7298 return 0;
7299}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007300
7301 void
7302win_findbuf(typval_T *argvars, list_T *list)
7303{
7304 win_T *wp;
7305 tabpage_T *tp;
7306 int bufnr = get_tv_number(&argvars[0]);
7307
7308 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7309 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7310 wp != NULL; wp = wp->w_next)
7311 if (wp->w_buffer->b_fnum == bufnr)
7312 list_append_number(list, wp->w_id);
7313}
7314
Bram Moolenaar86edef62016-03-13 18:07:30 +01007315#endif