blob: 7dfbe5a991e2e0c375b251d52d9e6c4701e86198 [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
4544/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004545 * Allocate a window structure and link it in the window list when "hidden" is
4546 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004549win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004551 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 /*
4554 * allocate window structure and linesizes arrays
4555 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004556 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004557 if (new_wp == NULL)
4558 return NULL;
4559
4560 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004562 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004563 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565
Bram Moolenaar429fa852013-04-15 12:27:36 +02004566#ifdef FEAT_EVAL
4567 /* init w: variables */
4568 new_wp->w_vars = dict_alloc();
4569 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004571 win_free_lsize(new_wp);
4572 vim_free(new_wp);
4573 return NULL;
4574 }
4575 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004576#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004577
4578#ifdef FEAT_AUTOCMD
4579 /* Don't execute autocommands while the window is not properly
4580 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4581 * event. */
4582 block_autocmds();
4583#endif
4584 /*
4585 * link the window in the window list
4586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004588 if (!hidden)
4589 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590#endif
4591#ifdef FEAT_VERTSPLIT
Bram Moolenaar429fa852013-04-15 12:27:36 +02004592 new_wp->w_wincol = 0;
4593 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#endif
4595
Bram Moolenaar429fa852013-04-15 12:27:36 +02004596 /* position the display and the cursor at the top of the file. */
4597 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004599 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004601 new_wp->w_botline = 2;
4602 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004604 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#endif
4606
Bram Moolenaar429fa852013-04-15 12:27:36 +02004607 /* We won't calculate w_fraction until resizing the window */
4608 new_wp->w_fraction = 0;
4609 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
4611#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004612 if (gui.in_use)
4613 {
4614 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4615 SBAR_LEFT, new_wp);
4616 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4617 SBAR_RIGHT, new_wp);
4618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619#endif
4620#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004621 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004623#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004624 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004625#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004626#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004627 new_wp->w_match_head = NULL;
4628 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004629#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004630 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631}
4632
4633#if defined(FEAT_WINDOWS) || defined(PROTO)
4634
4635/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004636 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 */
4638 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004639win_free(
4640 win_T *wp,
4641 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004644 buf_T *buf;
4645 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004647#ifdef FEAT_FOLDING
4648 clearFolding(wp);
4649#endif
4650
4651 /* reduce the reference count to the argument list. */
4652 alist_unlink(wp->w_alist);
4653
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004654#ifdef FEAT_AUTOCMD
4655 /* Don't execute autocommands while the window is halfway being deleted.
4656 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004657 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004658#endif
4659
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004660#ifdef FEAT_LUA
4661 lua_window_free(wp);
4662#endif
4663
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004664#ifdef FEAT_MZSCHEME
4665 mzscheme_window_free(wp);
4666#endif
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668#ifdef FEAT_PERL
4669 perl_win_free(wp);
4670#endif
4671
4672#ifdef FEAT_PYTHON
4673 python_window_free(wp);
4674#endif
4675
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004676#ifdef FEAT_PYTHON3
4677 python3_window_free(wp);
4678#endif
4679
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680#ifdef FEAT_TCL
4681 tcl_window_free(wp);
4682#endif
4683
4684#ifdef FEAT_RUBY
4685 ruby_window_free(wp);
4686#endif
4687
4688 clear_winopt(&wp->w_onebuf_opt);
4689 clear_winopt(&wp->w_allbuf_opt);
4690
4691#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004692 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4693 hash_init(&wp->w_vars->dv_hashtab);
4694 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695#endif
4696
4697 if (prevwin == wp)
4698 prevwin = NULL;
4699 win_free_lsize(wp);
4700
4701 for (i = 0; i < wp->w_tagstacklen; ++i)
4702 vim_free(wp->w_tagstack[i].tagname);
4703
4704 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004705
Bram Moolenaarff18df02013-07-24 17:51:57 +02004706 /* Remove the window from the b_wininfo lists, it may happen that the
4707 * freed memory is re-used for another window. */
4708 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4709 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4710 if (wip->wi_win == wp)
4711 wip->wi_win = NULL;
4712
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004714 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004716
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_JUMPLIST
4718 free_jumplist(wp);
4719#endif
4720
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004721#ifdef FEAT_QUICKFIX
4722 qf_free_all(wp);
4723#endif
4724
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725#ifdef FEAT_GUI
4726 if (gui.in_use)
4727 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4729 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4730 }
4731#endif /* FEAT_GUI */
4732
Bram Moolenaar860cae12010-06-05 23:22:07 +02004733#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004734 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004735#endif
4736
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004737#ifdef FEAT_AUTOCMD
4738 if (wp != aucmd_win)
4739#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004740 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004741#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004742 if (autocmd_busy)
4743 {
4744 wp->w_next = au_pending_free_win;
4745 au_pending_free_win = wp;
4746 }
4747 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004748#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004749 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004750
4751#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004752 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754}
4755
4756/*
4757 * Append window "wp" in the window list after window "after".
4758 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004759 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004760win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
4762 win_T *before;
4763
4764 if (after == NULL) /* after NULL is in front of the first */
4765 before = firstwin;
4766 else
4767 before = after->w_next;
4768
4769 wp->w_next = before;
4770 wp->w_prev = after;
4771 if (after == NULL)
4772 firstwin = wp;
4773 else
4774 after->w_next = wp;
4775 if (before == NULL)
4776 lastwin = wp;
4777 else
4778 before->w_prev = wp;
4779}
4780
4781/*
4782 * Remove a window from the window list.
4783 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004784 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004785win_remove(
4786 win_T *wp,
4787 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788{
4789 if (wp->w_prev != NULL)
4790 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004791 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004793 else
4794 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 if (wp->w_next != NULL)
4796 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004797 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004799 else
4800 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801}
4802
4803/*
4804 * Append frame "frp" in a frame list after frame "after".
4805 */
4806 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004807frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808{
4809 frp->fr_next = after->fr_next;
4810 after->fr_next = frp;
4811 if (frp->fr_next != NULL)
4812 frp->fr_next->fr_prev = frp;
4813 frp->fr_prev = after;
4814}
4815
4816/*
4817 * Insert frame "frp" in a frame list before frame "before".
4818 */
4819 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004820frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821{
4822 frp->fr_next = before;
4823 frp->fr_prev = before->fr_prev;
4824 before->fr_prev = frp;
4825 if (frp->fr_prev != NULL)
4826 frp->fr_prev->fr_next = frp;
4827 else
4828 frp->fr_parent->fr_child = frp;
4829}
4830
4831/*
4832 * Remove a frame from a frame list.
4833 */
4834 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004835frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836{
4837 if (frp->fr_prev != NULL)
4838 frp->fr_prev->fr_next = frp->fr_next;
4839 else
4840 frp->fr_parent->fr_child = frp->fr_next;
4841 if (frp->fr_next != NULL)
4842 frp->fr_next->fr_prev = frp->fr_prev;
4843}
4844
4845#endif /* FEAT_WINDOWS */
4846
4847/*
4848 * Allocate w_lines[] for window "wp".
4849 * Return FAIL for failure, OK for success.
4850 */
4851 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004852win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853{
4854 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004855 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 if (wp->w_lines == NULL)
4857 return FAIL;
4858 return OK;
4859}
4860
4861/*
4862 * free lsize arrays for a window
4863 */
4864 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004865win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004867 /* TODO: why would wp be NULL here? */
4868 if (wp != NULL)
4869 {
4870 vim_free(wp->w_lines);
4871 wp->w_lines = NULL;
4872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873}
4874
4875/*
4876 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004877 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 */
4879 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004880shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004882 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 if (firstwin == NULL) /* not initialized yet */
4885 return;
4886#ifdef FEAT_WINDOWS
4887 if (h < frame_minheight(topframe, NULL))
4888 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004889
4890 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 * that doesn't result in the right height, forget about that option. */
4892 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004893 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 frame_new_height(topframe, h, FALSE, FALSE);
4895
4896 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4897#else
4898 if (h < 1)
4899 h = 1;
4900 win_new_height(firstwin, h);
4901#endif
4902 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004903#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004904 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004905#endif
4906
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#if 0
4908 /* Disabled: don't want making the screen smaller make a window larger. */
4909 if (p_ea)
4910 win_equal(curwin, FALSE, 'v');
4911#endif
4912}
4913
4914#if defined(FEAT_VERTSPLIT) || defined(PROTO)
4915/*
4916 * Called from win_new_shellsize() after Columns changed.
4917 */
4918 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004919shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920{
4921 if (firstwin == NULL) /* not initialized yet */
4922 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004923
4924 /* First try setting the widths of windows with 'winfixwidth'. If that
4925 * doesn't result in the right width, forget about that option. */
4926 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004927 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004928 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4929
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4931#if 0
4932 /* Disabled: don't want making the screen smaller make a window larger. */
4933 if (p_ea)
4934 win_equal(curwin, FALSE, 'h');
4935#endif
4936}
4937#endif
4938
4939#if defined(FEAT_CMDWIN) || defined(PROTO)
4940/*
4941 * Save the size of all windows in "gap".
4942 */
4943 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004944win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
4946{
4947 win_T *wp;
4948
4949 ga_init2(gap, (int)sizeof(int), 1);
4950 if (ga_grow(gap, win_count() * 2) == OK)
4951 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4952 {
4953 ((int *)gap->ga_data)[gap->ga_len++] =
4954 wp->w_width + wp->w_vsep_width;
4955 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4956 }
4957}
4958
4959/*
4960 * Restore window sizes, but only if the number of windows is still the same.
4961 * Does not free the growarray.
4962 */
4963 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004964win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
4966 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004967 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 if (win_count() * 2 == gap->ga_len)
4970 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004971 /* The order matters, because frames contain other frames, but it's
4972 * difficult to get right. The easy way out is to do it twice. */
4973 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004975 i = 0;
4976 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4977 {
4978 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4979 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982 /* recompute the window positions */
4983 (void)win_comp_pos();
4984 }
4985}
4986#endif /* FEAT_CMDWIN */
4987
4988#if defined(FEAT_WINDOWS) || defined(PROTO)
4989/*
4990 * Update the position for all windows, using the width and height of the
4991 * frames.
4992 * Returns the row just after the last window.
4993 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004994 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004995win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004997 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 int col = 0;
4999
5000 frame_comp_pos(topframe, &row, &col);
5001 return row;
5002}
5003
5004/*
5005 * Update the position of the windows in frame "topfrp", using the width and
5006 * height of the frames.
5007 * "*row" and "*col" are the top-left position of the frame. They are updated
5008 * to the bottom-right position plus one.
5009 */
5010 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005011frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 win_T *wp;
5014 frame_T *frp;
5015#ifdef FEAT_VERTSPLIT
5016 int startcol;
5017 int startrow;
5018#endif
5019
5020 wp = topfrp->fr_win;
5021 if (wp != NULL)
5022 {
5023 if (wp->w_winrow != *row
5024#ifdef FEAT_VERTSPLIT
5025 || wp->w_wincol != *col
5026#endif
5027 )
5028 {
5029 /* position changed, redraw */
5030 wp->w_winrow = *row;
5031#ifdef FEAT_VERTSPLIT
5032 wp->w_wincol = *col;
5033#endif
5034 redraw_win_later(wp, NOT_VALID);
5035 wp->w_redr_status = TRUE;
5036 }
5037 *row += wp->w_height + wp->w_status_height;
5038#ifdef FEAT_VERTSPLIT
5039 *col += wp->w_width + wp->w_vsep_width;
5040#endif
5041 }
5042 else
5043 {
5044#ifdef FEAT_VERTSPLIT
5045 startrow = *row;
5046 startcol = *col;
5047#endif
5048 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
5049 {
5050#ifdef FEAT_VERTSPLIT
5051 if (topfrp->fr_layout == FR_ROW)
5052 *row = startrow; /* all frames are at the same row */
5053 else
5054 *col = startcol; /* all frames are at the same col */
5055#endif
5056 frame_comp_pos(frp, row, col);
5057 }
5058 }
5059}
5060
5061#endif /* FEAT_WINDOWS */
5062
5063/*
5064 * Set current window height and take care of repositioning other windows to
5065 * fit around it.
5066 */
5067 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005068win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
5070 win_setheight_win(height, curwin);
5071}
5072
5073/*
5074 * Set the window height of window "win" and take care of repositioning other
5075 * windows to fit around it.
5076 */
5077 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005078win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
5080 int row;
5081
5082 if (win == curwin)
5083 {
5084 /* Always keep current window at least one line high, even when
5085 * 'winminheight' is zero. */
5086#ifdef FEAT_WINDOWS
5087 if (height < p_wmh)
5088 height = p_wmh;
5089#endif
5090 if (height == 0)
5091 height = 1;
5092 }
5093
5094#ifdef FEAT_WINDOWS
5095 frame_setheight(win->w_frame, height + win->w_status_height);
5096
5097 /* recompute the window positions */
5098 row = win_comp_pos();
5099#else
5100 if (height > topframe->fr_height)
5101 height = topframe->fr_height;
5102 win->w_height = height;
5103 row = height;
5104#endif
5105
5106 /*
5107 * If there is extra space created between the last window and the command
5108 * line, clear it.
5109 */
5110 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5111 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5112 cmdline_row = row;
5113 msg_row = row;
5114 msg_col = 0;
5115
5116 redraw_all_later(NOT_VALID);
5117}
5118
5119#if defined(FEAT_WINDOWS) || defined(PROTO)
5120
5121/*
5122 * Set the height of a frame to "height" and take care that all frames and
5123 * windows inside it are resized. Also resize frames on the left and right if
5124 * the are in the same FR_ROW frame.
5125 *
5126 * Strategy:
5127 * If the frame is part of a FR_COL frame, try fitting the frame in that
5128 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5129 * go to containing frames to resize them and make room.
5130 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5131 * Check for the minimal height of the FR_ROW frame.
5132 * At the top level we can also use change the command line height.
5133 */
5134 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005135frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136{
5137 int room; /* total number of lines available */
5138 int take; /* number of lines taken from other windows */
5139 int room_cmdline; /* lines available from cmdline */
5140 int run;
5141 frame_T *frp;
5142 int h;
5143 int room_reserved;
5144
5145 /* If the height already is the desired value, nothing to do. */
5146 if (curfrp->fr_height == height)
5147 return;
5148
5149 if (curfrp->fr_parent == NULL)
5150 {
5151 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005152 if (height > ROWS_AVAIL)
5153 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 if (height > 0)
5155 frame_new_height(curfrp, height, FALSE, FALSE);
5156 }
5157 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5158 {
5159 /* Row of frames: Also need to resize frames left and right of this
5160 * one. First check for the minimal height of these. */
5161 h = frame_minheight(curfrp->fr_parent, NULL);
5162 if (height < h)
5163 height = h;
5164 frame_setheight(curfrp->fr_parent, height);
5165 }
5166 else
5167 {
5168 /*
5169 * Column of frames: try to change only frames in this column.
5170 */
5171#ifdef FEAT_VERTSPLIT
5172 /*
5173 * Do this twice:
5174 * 1: compute room available, if it's not enough try resizing the
5175 * containing frame.
5176 * 2: compute the room available and adjust the height to it.
5177 * Try not to reduce the height of a window with 'winfixheight' set.
5178 */
5179 for (run = 1; run <= 2; ++run)
5180#else
5181 for (;;)
5182#endif
5183 {
5184 room = 0;
5185 room_reserved = 0;
5186 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5187 frp = frp->fr_next)
5188 {
5189 if (frp != curfrp
5190 && frp->fr_win != NULL
5191 && frp->fr_win->w_p_wfh)
5192 room_reserved += frp->fr_height;
5193 room += frp->fr_height;
5194 if (frp != curfrp)
5195 room -= frame_minheight(frp, NULL);
5196 }
5197#ifdef FEAT_VERTSPLIT
5198 if (curfrp->fr_width != Columns)
5199 room_cmdline = 0;
5200 else
5201#endif
5202 {
5203 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5204 + lastwin->w_height + lastwin->w_status_height);
5205 if (room_cmdline < 0)
5206 room_cmdline = 0;
5207 }
5208
5209 if (height <= room + room_cmdline)
5210 break;
5211#ifdef FEAT_VERTSPLIT
5212 if (run == 2 || curfrp->fr_width == Columns)
5213#endif
5214 {
5215 if (height > room + room_cmdline)
5216 height = room + room_cmdline;
5217 break;
5218 }
5219#ifdef FEAT_VERTSPLIT
5220 frame_setheight(curfrp->fr_parent, height
5221 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
5222#endif
5223 /*NOTREACHED*/
5224 }
5225
5226 /*
5227 * Compute the number of lines we will take from others frames (can be
5228 * negative!).
5229 */
5230 take = height - curfrp->fr_height;
5231
5232 /* If there is not enough room, also reduce the height of a window
5233 * with 'winfixheight' set. */
5234 if (height > room + room_cmdline - room_reserved)
5235 room_reserved = room + room_cmdline - height;
5236 /* If there is only a 'winfixheight' window and making the
5237 * window smaller, need to make the other window taller. */
5238 if (take < 0 && room - curfrp->fr_height < room_reserved)
5239 room_reserved = 0;
5240
5241 if (take > 0 && room_cmdline > 0)
5242 {
5243 /* use lines from cmdline first */
5244 if (take < room_cmdline)
5245 room_cmdline = take;
5246 take -= room_cmdline;
5247 topframe->fr_height += room_cmdline;
5248 }
5249
5250 /*
5251 * set the current frame to the new height
5252 */
5253 frame_new_height(curfrp, height, FALSE, FALSE);
5254
5255 /*
5256 * First take lines from the frames after the current frame. If
5257 * that is not enough, takes lines from frames above the current
5258 * frame.
5259 */
5260 for (run = 0; run < 2; ++run)
5261 {
5262 if (run == 0)
5263 frp = curfrp->fr_next; /* 1st run: start with next window */
5264 else
5265 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5266 while (frp != NULL && take != 0)
5267 {
5268 h = frame_minheight(frp, NULL);
5269 if (room_reserved > 0
5270 && frp->fr_win != NULL
5271 && frp->fr_win->w_p_wfh)
5272 {
5273 if (room_reserved >= frp->fr_height)
5274 room_reserved -= frp->fr_height;
5275 else
5276 {
5277 if (frp->fr_height - room_reserved > take)
5278 room_reserved = frp->fr_height - take;
5279 take -= frp->fr_height - room_reserved;
5280 frame_new_height(frp, room_reserved, FALSE, FALSE);
5281 room_reserved = 0;
5282 }
5283 }
5284 else
5285 {
5286 if (frp->fr_height - take < h)
5287 {
5288 take -= frp->fr_height - h;
5289 frame_new_height(frp, h, FALSE, FALSE);
5290 }
5291 else
5292 {
5293 frame_new_height(frp, frp->fr_height - take,
5294 FALSE, FALSE);
5295 take = 0;
5296 }
5297 }
5298 if (run == 0)
5299 frp = frp->fr_next;
5300 else
5301 frp = frp->fr_prev;
5302 }
5303 }
5304 }
5305}
5306
5307#if defined(FEAT_VERTSPLIT) || defined(PROTO)
5308/*
5309 * Set current window width and take care of repositioning other windows to
5310 * fit around it.
5311 */
5312 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005313win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 win_setwidth_win(width, curwin);
5316}
5317
5318 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005319win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320{
5321 /* Always keep current window at least one column wide, even when
5322 * 'winminwidth' is zero. */
5323 if (wp == curwin)
5324 {
5325 if (width < p_wmw)
5326 width = p_wmw;
5327 if (width == 0)
5328 width = 1;
5329 }
5330
5331 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5332
5333 /* recompute the window positions */
5334 (void)win_comp_pos();
5335
5336 redraw_all_later(NOT_VALID);
5337}
5338
5339/*
5340 * Set the width of a frame to "width" and take care that all frames and
5341 * windows inside it are resized. Also resize frames above and below if the
5342 * are in the same FR_ROW frame.
5343 *
5344 * Strategy is similar to frame_setheight().
5345 */
5346 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005347frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348{
5349 int room; /* total number of lines available */
5350 int take; /* number of lines taken from other windows */
5351 int run;
5352 frame_T *frp;
5353 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005354 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
5356 /* If the width already is the desired value, nothing to do. */
5357 if (curfrp->fr_width == width)
5358 return;
5359
5360 if (curfrp->fr_parent == NULL)
5361 /* topframe: can't change width */
5362 return;
5363
5364 if (curfrp->fr_parent->fr_layout == FR_COL)
5365 {
5366 /* Column of frames: Also need to resize frames above and below of
5367 * this one. First check for the minimal width of these. */
5368 w = frame_minwidth(curfrp->fr_parent, NULL);
5369 if (width < w)
5370 width = w;
5371 frame_setwidth(curfrp->fr_parent, width);
5372 }
5373 else
5374 {
5375 /*
5376 * Row of frames: try to change only frames in this row.
5377 *
5378 * Do this twice:
5379 * 1: compute room available, if it's not enough try resizing the
5380 * containing frame.
5381 * 2: compute the room available and adjust the width to it.
5382 */
5383 for (run = 1; run <= 2; ++run)
5384 {
5385 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005386 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5388 frp = frp->fr_next)
5389 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005390 if (frp != curfrp
5391 && frp->fr_win != NULL
5392 && frp->fr_win->w_p_wfw)
5393 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 room += frp->fr_width;
5395 if (frp != curfrp)
5396 room -= frame_minwidth(frp, NULL);
5397 }
5398
5399 if (width <= room)
5400 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005401 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
5403 if (width > room)
5404 width = room;
5405 break;
5406 }
5407 frame_setwidth(curfrp->fr_parent, width
5408 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5409 }
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 /*
5412 * Compute the number of lines we will take from others frames (can be
5413 * negative!).
5414 */
5415 take = width - curfrp->fr_width;
5416
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005417 /* If there is not enough room, also reduce the width of a window
5418 * with 'winfixwidth' set. */
5419 if (width > room - room_reserved)
5420 room_reserved = room - width;
5421 /* If there is only a 'winfixwidth' window and making the
5422 * window smaller, need to make the other window narrower. */
5423 if (take < 0 && room - curfrp->fr_width < room_reserved)
5424 room_reserved = 0;
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 /*
5427 * set the current frame to the new width
5428 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005429 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430
5431 /*
5432 * First take lines from the frames right of the current frame. If
5433 * that is not enough, takes lines from frames left of the current
5434 * frame.
5435 */
5436 for (run = 0; run < 2; ++run)
5437 {
5438 if (run == 0)
5439 frp = curfrp->fr_next; /* 1st run: start with next window */
5440 else
5441 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5442 while (frp != NULL && take != 0)
5443 {
5444 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005445 if (room_reserved > 0
5446 && frp->fr_win != NULL
5447 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005449 if (room_reserved >= frp->fr_width)
5450 room_reserved -= frp->fr_width;
5451 else
5452 {
5453 if (frp->fr_width - room_reserved > take)
5454 room_reserved = frp->fr_width - take;
5455 take -= frp->fr_width - room_reserved;
5456 frame_new_width(frp, room_reserved, FALSE, FALSE);
5457 room_reserved = 0;
5458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460 else
5461 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005462 if (frp->fr_width - take < w)
5463 {
5464 take -= frp->fr_width - w;
5465 frame_new_width(frp, w, FALSE, FALSE);
5466 }
5467 else
5468 {
5469 frame_new_width(frp, frp->fr_width - take,
5470 FALSE, FALSE);
5471 take = 0;
5472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 }
5474 if (run == 0)
5475 frp = frp->fr_next;
5476 else
5477 frp = frp->fr_prev;
5478 }
5479 }
5480 }
5481}
5482#endif /* FEAT_VERTSPLIT */
5483
5484/*
5485 * Check 'winminheight' for a valid value.
5486 */
5487 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005488win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 int room;
5491 int first = TRUE;
5492 win_T *wp;
5493
5494 /* loop until there is a 'winminheight' that is possible */
5495 while (p_wmh > 0)
5496 {
5497 /* TODO: handle vertical splits */
5498 room = -p_wh;
5499 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5500 room += wp->w_height - p_wmh;
5501 if (room >= 0)
5502 break;
5503 --p_wmh;
5504 if (first)
5505 {
5506 EMSG(_(e_noroom));
5507 first = FALSE;
5508 }
5509 }
5510}
5511
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005512#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514/*
5515 * Status line of dragwin is dragged "offset" lines down (negative is up).
5516 */
5517 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005518win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
5520 frame_T *curfr;
5521 frame_T *fr;
5522 int room;
5523 int row;
5524 int up; /* if TRUE, drag status line up, otherwise down */
5525 int n;
5526
5527 fr = dragwin->w_frame;
5528 curfr = fr;
5529 if (fr != topframe) /* more than one window */
5530 {
5531 fr = fr->fr_parent;
5532 /* When the parent frame is not a column of frames, its parent should
5533 * be. */
5534 if (fr->fr_layout != FR_COL)
5535 {
5536 curfr = fr;
5537 if (fr != topframe) /* only a row of windows, may drag statusline */
5538 fr = fr->fr_parent;
5539 }
5540 }
5541
5542 /* If this is the last frame in a column, may want to resize the parent
5543 * frame instead (go two up to skip a row of frames). */
5544 while (curfr != topframe && curfr->fr_next == NULL)
5545 {
5546 if (fr != topframe)
5547 fr = fr->fr_parent;
5548 curfr = fr;
5549 if (fr != topframe)
5550 fr = fr->fr_parent;
5551 }
5552
5553 if (offset < 0) /* drag up */
5554 {
5555 up = TRUE;
5556 offset = -offset;
5557 /* sum up the room of the current frame and above it */
5558 if (fr == curfr)
5559 {
5560 /* only one window */
5561 room = fr->fr_height - frame_minheight(fr, NULL);
5562 }
5563 else
5564 {
5565 room = 0;
5566 for (fr = fr->fr_child; ; fr = fr->fr_next)
5567 {
5568 room += fr->fr_height - frame_minheight(fr, NULL);
5569 if (fr == curfr)
5570 break;
5571 }
5572 }
5573 fr = curfr->fr_next; /* put fr at frame that grows */
5574 }
5575 else /* drag down */
5576 {
5577 up = FALSE;
5578 /*
5579 * Only dragging the last status line can reduce p_ch.
5580 */
5581 room = Rows - cmdline_row;
5582 if (curfr->fr_next == NULL)
5583 room -= 1;
5584 else
5585 room -= p_ch;
5586 if (room < 0)
5587 room = 0;
5588 /* sum up the room of frames below of the current one */
5589 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5590 room += fr->fr_height - frame_minheight(fr, NULL);
5591 fr = curfr; /* put fr at window that grows */
5592 }
5593
5594 if (room < offset) /* Not enough room */
5595 offset = room; /* Move as far as we can */
5596 if (offset <= 0)
5597 return;
5598
5599 /*
5600 * Grow frame fr by "offset" lines.
5601 * Doesn't happen when dragging the last status line up.
5602 */
5603 if (fr != NULL)
5604 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5605
5606 if (up)
5607 fr = curfr; /* current frame gets smaller */
5608 else
5609 fr = curfr->fr_next; /* next frame gets smaller */
5610
5611 /*
5612 * Now make the other frames smaller.
5613 */
5614 while (fr != NULL && offset > 0)
5615 {
5616 n = frame_minheight(fr, NULL);
5617 if (fr->fr_height - offset <= n)
5618 {
5619 offset -= fr->fr_height - n;
5620 frame_new_height(fr, n, !up, FALSE);
5621 }
5622 else
5623 {
5624 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5625 break;
5626 }
5627 if (up)
5628 fr = fr->fr_prev;
5629 else
5630 fr = fr->fr_next;
5631 }
5632 row = win_comp_pos();
5633 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5634 cmdline_row = row;
5635 p_ch = Rows - cmdline_row;
5636 if (p_ch < 1)
5637 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005638 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005639 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 showmode();
5641}
5642
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005643# if defined(FEAT_VERTSPLIT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644/*
5645 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5646 */
5647 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005648win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 frame_T *curfr;
5651 frame_T *fr;
5652 int room;
5653 int left; /* if TRUE, drag separator line left, otherwise right */
5654 int n;
5655
5656 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005657 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 return;
5659 curfr = fr;
5660 fr = fr->fr_parent;
5661 /* When the parent frame is not a row of frames, its parent should be. */
5662 if (fr->fr_layout != FR_ROW)
5663 {
5664 if (fr == topframe) /* only a column of windows (cannot happen?) */
5665 return;
5666 curfr = fr;
5667 fr = fr->fr_parent;
5668 }
5669
5670 /* If this is the last frame in a row, may want to resize a parent
5671 * frame instead. */
5672 while (curfr->fr_next == NULL)
5673 {
5674 if (fr == topframe)
5675 break;
5676 curfr = fr;
5677 fr = fr->fr_parent;
5678 if (fr != topframe)
5679 {
5680 curfr = fr;
5681 fr = fr->fr_parent;
5682 }
5683 }
5684
5685 if (offset < 0) /* drag left */
5686 {
5687 left = TRUE;
5688 offset = -offset;
5689 /* sum up the room of the current frame and left of it */
5690 room = 0;
5691 for (fr = fr->fr_child; ; fr = fr->fr_next)
5692 {
5693 room += fr->fr_width - frame_minwidth(fr, NULL);
5694 if (fr == curfr)
5695 break;
5696 }
5697 fr = curfr->fr_next; /* put fr at frame that grows */
5698 }
5699 else /* drag right */
5700 {
5701 left = FALSE;
5702 /* sum up the room of frames right of the current one */
5703 room = 0;
5704 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5705 room += fr->fr_width - frame_minwidth(fr, NULL);
5706 fr = curfr; /* put fr at window that grows */
5707 }
5708
5709 if (room < offset) /* Not enough room */
5710 offset = room; /* Move as far as we can */
5711 if (offset <= 0) /* No room at all, quit. */
5712 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005713 if (fr == NULL)
5714 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715
5716 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005717 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718
5719 /* shrink other frames: current and at the left or at the right */
5720 if (left)
5721 fr = curfr; /* current frame gets smaller */
5722 else
5723 fr = curfr->fr_next; /* next frame gets smaller */
5724
5725 while (fr != NULL && offset > 0)
5726 {
5727 n = frame_minwidth(fr, NULL);
5728 if (fr->fr_width - offset <= n)
5729 {
5730 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005731 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else
5734 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005735 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 break;
5737 }
5738 if (left)
5739 fr = fr->fr_prev;
5740 else
5741 fr = fr->fr_next;
5742 }
5743 (void)win_comp_pos();
5744 redraw_all_later(NOT_VALID);
5745}
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005746# endif /* FEAT_VERTSPLIT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747#endif /* FEAT_MOUSE */
5748
5749#endif /* FEAT_WINDOWS */
5750
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005751#define FRACTION_MULT 16384L
5752
5753/*
5754 * Set wp->w_fraction for the current w_wrow and w_height.
5755 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005756 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005757set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005758{
5759 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005760 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005761}
5762
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763/*
5764 * Set the height of a window.
5765 * This takes care of the things inside the window, not what happens to the
5766 * window position, the frame or to other windows.
5767 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005768 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005769win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
5771 linenr_T lnum;
5772 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005773 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774
5775 /* Don't want a negative height. Happens when splitting a tiny window.
5776 * Will equalize heights soon to fix it. */
5777 if (height < 0)
5778 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005779 if (wp->w_height == height)
5780 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005782 if (wp->w_height > 0)
5783 {
5784 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005785 /* w_wrow needs to be valid. When setting 'laststatus' this may
5786 * call win_new_height() recursively. */
5787 validate_cursor();
5788 if (wp->w_height != prev_height)
5789 return; /* Recursive call already changed the size, bail out here
5790 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005791 if (wp->w_wrow != wp->w_prev_fraction_row)
5792 set_fraction(wp);
5793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 wp->w_height = height;
5796 wp->w_skipcol = 0;
5797
5798 /* Don't change w_topline when height is zero. Don't set w_topline when
5799 * 'scrollbind' is set and this isn't the current window. */
5800 if (height > 0
5801#ifdef FEAT_SCROLLBIND
5802 && (!wp->w_p_scb || wp == curwin)
5803#endif
5804 )
5805 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005806 /*
5807 * Find a value for w_topline that shows the cursor at the same
5808 * relative position in the window as before (more or less).
5809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 lnum = wp->w_cursor.lnum;
5811 if (lnum < 1) /* can happen when starting up */
5812 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005813 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5814 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5816 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005817
5818 if (sline >= 0)
5819 {
5820 /* Make sure the whole cursor line is visible, if possible. */
5821 int rows = plines_win(wp, lnum, FALSE);
5822
5823 if (sline > wp->w_height - rows)
5824 {
5825 sline = wp->w_height - rows;
5826 wp->w_wrow -= rows - line_size;
5827 }
5828 }
5829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (sline < 0)
5831 {
5832 /*
5833 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005834 * Make cursor line the first line in the window. If not enough
5835 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 */
5837 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005838 if (wp->w_wrow >= wp->w_height
5839 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5840 {
5841 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5842 --wp->w_wrow;
5843 while (wp->w_wrow >= wp->w_height)
5844 {
5845 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5846 + win_col_off2(wp);
5847 --wp->w_wrow;
5848 }
5849 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005850 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005852 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005854 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 {
5856#ifdef FEAT_FOLDING
5857 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5858 if (lnum == 1)
5859 {
5860 /* first line in buffer is folded */
5861 line_size = 1;
5862 --sline;
5863 break;
5864 }
5865#endif
5866 --lnum;
5867#ifdef FEAT_DIFF
5868 if (lnum == wp->w_topline)
5869 line_size = plines_win_nofill(wp, lnum, TRUE)
5870 + wp->w_topfill;
5871 else
5872#endif
5873 line_size = plines_win(wp, lnum, TRUE);
5874 sline -= line_size;
5875 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 if (sline < 0)
5878 {
5879 /*
5880 * Line we want at top would go off top of screen. Use next
5881 * line instead.
5882 */
5883#ifdef FEAT_FOLDING
5884 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5885#endif
5886 lnum++;
5887 wp->w_wrow -= line_size + sline;
5888 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005889 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
5891 /* First line of file reached, use that as topline. */
5892 lnum = 1;
5893 wp->w_wrow -= sline;
5894 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005895
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005896 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 }
5899
5900 if (wp == curwin)
5901 {
5902 if (p_so)
5903 update_topline();
5904 curs_columns(FALSE); /* validate w_wrow */
5905 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005906 if (prev_height > 0)
5907 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908
5909 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005910 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911#ifdef FEAT_WINDOWS
5912 wp->w_redr_status = TRUE;
5913#endif
5914 invalidate_botline_win(wp);
5915}
5916
5917#ifdef FEAT_VERTSPLIT
5918/*
5919 * Set the width of a window.
5920 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005921 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005922win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 wp->w_width = width;
5925 wp->w_lines_valid = 0;
5926 changed_line_abv_curs_win(wp);
5927 invalidate_botline_win(wp);
5928 if (wp == curwin)
5929 {
5930 update_topline();
5931 curs_columns(TRUE); /* validate w_wrow */
5932 }
5933 redraw_win_later(wp, NOT_VALID);
5934 wp->w_redr_status = TRUE;
5935}
5936#endif
5937
5938 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005939win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5942 if (wp->w_p_scr == 0)
5943 wp->w_p_scr = 1;
5944}
5945
5946/*
5947 * command_height: called whenever p_ch has been changed
5948 */
5949 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005950command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
5952#ifdef FEAT_WINDOWS
5953 int h;
5954 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005955 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005957 /* Use the value of p_ch that we remembered. This is needed for when the
5958 * GUI starts up, we can't be sure in what order things happen. And when
5959 * p_ch was changed in another tab page. */
5960 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005961
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 /* Find bottom frame with width of screen. */
5963 frp = lastwin->w_frame;
5964# ifdef FEAT_VERTSPLIT
5965 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5966 frp = frp->fr_parent;
5967# endif
5968
5969 /* Avoid changing the height of a window with 'winfixheight' set. */
5970 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5971 && frp->fr_win->w_p_wfh)
5972 frp = frp->fr_prev;
5973
5974 if (starting != NO_SCREEN)
5975 {
5976 cmdline_row = Rows - p_ch;
5977
5978 if (p_ch > old_p_ch) /* p_ch got bigger */
5979 {
5980 while (p_ch > old_p_ch)
5981 {
5982 if (frp == NULL)
5983 {
5984 EMSG(_(e_noroom));
5985 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005986 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 cmdline_row = Rows - p_ch;
5988 break;
5989 }
5990 h = frp->fr_height - frame_minheight(frp, NULL);
5991 if (h > p_ch - old_p_ch)
5992 h = p_ch - old_p_ch;
5993 old_p_ch += h;
5994 frame_add_height(frp, -h);
5995 frp = frp->fr_prev;
5996 }
5997
5998 /* Recompute window positions. */
5999 (void)win_comp_pos();
6000
6001 /* clear the lines added to cmdline */
6002 if (full_screen)
6003 screen_fill((int)(cmdline_row), (int)Rows, 0,
6004 (int)Columns, ' ', ' ', 0);
6005 msg_row = cmdline_row;
6006 redraw_cmdline = TRUE;
6007 return;
6008 }
6009
6010 if (msg_row < cmdline_row)
6011 msg_row = cmdline_row;
6012 redraw_cmdline = TRUE;
6013 }
6014 frame_add_height(frp, (int)(old_p_ch - p_ch));
6015
6016 /* Recompute window positions. */
6017 if (frp != lastwin->w_frame)
6018 (void)win_comp_pos();
6019#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006021 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022#endif
6023}
6024
6025#if defined(FEAT_WINDOWS) || defined(PROTO)
6026/*
6027 * Resize frame "frp" to be "n" lines higher (negative for less high).
6028 * Also resize the frames it is contained in.
6029 */
6030 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006031frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032{
6033 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6034 for (;;)
6035 {
6036 frp = frp->fr_parent;
6037 if (frp == NULL)
6038 break;
6039 frp->fr_height += n;
6040 }
6041}
6042
6043/*
6044 * Add or remove a status line for the bottom window(s), according to the
6045 * value of 'laststatus'.
6046 */
6047 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006048last_status(
6049 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 /* Don't make a difference between horizontal or vertical split. */
6052 last_status_rec(topframe, (p_ls == 2
6053 || (p_ls == 1 && (morewin || lastwin != firstwin))));
6054}
6055
6056 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006057last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 frame_T *fp;
6060 win_T *wp;
6061
6062 if (fr->fr_layout == FR_LEAF)
6063 {
6064 wp = fr->fr_win;
6065 if (wp->w_status_height != 0 && !statusline)
6066 {
6067 /* remove status line */
6068 win_new_height(wp, wp->w_height + 1);
6069 wp->w_status_height = 0;
6070 comp_col();
6071 }
6072 else if (wp->w_status_height == 0 && statusline)
6073 {
6074 /* Find a frame to take a line from. */
6075 fp = fr;
6076 while (fp->fr_height <= frame_minheight(fp, NULL))
6077 {
6078 if (fp == topframe)
6079 {
6080 EMSG(_(e_noroom));
6081 return;
6082 }
6083 /* In a column of frames: go to frame above. If already at
6084 * the top or in a row of frames: go to parent. */
6085 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6086 fp = fp->fr_prev;
6087 else
6088 fp = fp->fr_parent;
6089 }
6090 wp->w_status_height = 1;
6091 if (fp != fr)
6092 {
6093 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6094 frame_fix_height(wp);
6095 (void)win_comp_pos();
6096 }
6097 else
6098 win_new_height(wp, wp->w_height - 1);
6099 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006100 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 }
6102 }
6103#ifdef FEAT_VERTSPLIT
6104 else if (fr->fr_layout == FR_ROW)
6105 {
6106 /* vertically split windows, set status line for each one */
6107 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6108 last_status_rec(fp, statusline);
6109 }
6110#endif
6111 else
6112 {
6113 /* horizontally split window, set status line for last one */
6114 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6115 ;
6116 last_status_rec(fp, statusline);
6117 }
6118}
6119
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006120/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006121 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006122 */
6123 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006124tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006125{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006126#ifdef FEAT_GUI_TABLINE
6127 /* When the GUI has the tabline then this always returns zero. */
6128 if (gui_use_tabline())
6129 return 0;
6130#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006131 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006132 {
6133 case 0: return 0;
6134 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6135 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006136 return 1;
6137}
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139#endif /* FEAT_WINDOWS */
6140
6141#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6142/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006143 * Get the file name at the cursor.
6144 * If Visual mode is active, use the selected text if it's in one line.
6145 * Returns the name in allocated memory, NULL for failure.
6146 */
6147 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006148grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006149{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006150 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6151
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006152 if (VIsual_active)
6153 {
6154 int len;
6155 char_u *ptr;
6156
6157 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6158 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006159 return find_file_name_in_path(ptr, len, options,
6160 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006161 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006162 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006163
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006164}
6165
6166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 * Return the file name under or after the cursor.
6168 *
6169 * The 'path' option is searched if the file name is not absolute.
6170 * The string returned has been alloc'ed and should be freed by the caller.
6171 * NULL is returned if the file name or file is not found.
6172 *
6173 * options:
6174 * FNAME_MESS give error messages
6175 * FNAME_EXP expand to path
6176 * FNAME_HYP check for hypertext link
6177 * FNAME_INCL apply "includeexpr"
6178 */
6179 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006180file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181{
6182 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006183 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6184 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185}
6186
6187/*
6188 * Return the name of the file under or after ptr[col].
6189 * Otherwise like file_name_at_cursor().
6190 */
6191 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006192file_name_in_line(
6193 char_u *line,
6194 int col,
6195 int options,
6196 long count,
6197 char_u *rel_fname, /* file we are searching relative to */
6198 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 char_u *ptr;
6201 int len;
6202
6203 /*
6204 * search forward for what could be the start of a file name
6205 */
6206 ptr = line + col;
6207 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006208 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (*ptr == NUL) /* nothing found */
6210 {
6211 if (options & FNAME_MESS)
6212 EMSG(_("E446: No file name under cursor"));
6213 return NULL;
6214 }
6215
6216 /*
6217 * Search backward for first char of the file name.
6218 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6219 */
6220 while (ptr > line)
6221 {
6222#ifdef FEAT_MBYTE
6223 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6224 ptr -= len + 1;
6225 else
6226#endif
6227 if (vim_isfilec(ptr[-1])
6228 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6229 --ptr;
6230 else
6231 break;
6232 }
6233
6234 /*
6235 * Search forward for the last char of the file name.
6236 * Also allow "://" when ':' is not in 'isfname'.
6237 */
6238 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006239 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006241 {
6242 if (ptr[len] == '\\')
6243 /* Skip over the "\" in "\ ". */
6244 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245#ifdef FEAT_MBYTE
6246 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006247 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 else
6249#endif
6250 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
6253 /*
6254 * If there is trailing punctuation, remove it.
6255 * But don't remove "..", could be a directory name.
6256 */
6257 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6258 && ptr[len - 2] != '.')
6259 --len;
6260
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006261 if (file_lnum != NULL)
6262 {
6263 char_u *p;
6264
6265 /* Get the number after the file name and a separator character */
6266 p = ptr + len;
6267 p = skipwhite(p);
6268 if (*p != NUL)
6269 {
6270 if (!isdigit(*p))
6271 ++p; /* skip the separator */
6272 p = skipwhite(p);
6273 if (isdigit(*p))
6274 *file_lnum = (int)getdigits(&p);
6275 }
6276 }
6277
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6279}
6280
6281# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006282static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
6284 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006285eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
6287 char_u *res;
6288
6289 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006290 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006291 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 set_vim_var_string(VV_FNAME, NULL, 0);
6293 return res;
6294}
6295#endif
6296
6297/*
6298 * Return the name of the file ptr[len] in 'path'.
6299 * Otherwise like file_name_at_cursor().
6300 */
6301 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006302find_file_name_in_path(
6303 char_u *ptr,
6304 int len,
6305 int options,
6306 long count,
6307 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 char_u *file_name;
6310 int c;
6311# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6312 char_u *tofree = NULL;
6313
6314 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6315 {
6316 tofree = eval_includeexpr(ptr, len);
6317 if (tofree != NULL)
6318 {
6319 ptr = tofree;
6320 len = (int)STRLEN(ptr);
6321 }
6322 }
6323# endif
6324
6325 if (options & FNAME_EXP)
6326 {
6327 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6328 TRUE, rel_fname);
6329
6330# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6331 /*
6332 * If the file could not be found in a normal way, try applying
6333 * 'includeexpr' (unless done already).
6334 */
6335 if (file_name == NULL
6336 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6337 {
6338 tofree = eval_includeexpr(ptr, len);
6339 if (tofree != NULL)
6340 {
6341 ptr = tofree;
6342 len = (int)STRLEN(ptr);
6343 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6344 TRUE, rel_fname);
6345 }
6346 }
6347# endif
6348 if (file_name == NULL && (options & FNAME_MESS))
6349 {
6350 c = ptr[len];
6351 ptr[len] = NUL;
6352 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6353 ptr[len] = c;
6354 }
6355
6356 /* Repeat finding the file "count" times. This matters when it
6357 * appears several times in the path. */
6358 while (file_name != NULL && --count > 0)
6359 {
6360 vim_free(file_name);
6361 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6362 }
6363 }
6364 else
6365 file_name = vim_strnsave(ptr, len);
6366
6367# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6368 vim_free(tofree);
6369# endif
6370
6371 return file_name;
6372}
6373#endif /* FEAT_SEARCHPATH */
6374
6375/*
6376 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6377 * Also check for ":\\", which MS Internet Explorer accepts, return
6378 * URL_BACKSLASH.
6379 */
6380 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006381path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 if (STRNCMP(p, "://", (size_t)3) == 0)
6384 return URL_SLASH;
6385 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6386 return URL_BACKSLASH;
6387 return 0;
6388}
6389
6390/*
6391 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6392 * Return URL_BACKSLASH for "name:\\".
6393 * Return zero otherwise.
6394 */
6395 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006396path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397{
6398 char_u *p;
6399
6400 for (p = fname; isalpha(*p); ++p)
6401 ;
6402 return path_is_url(p);
6403}
6404
6405/*
6406 * Return TRUE if "name" is a full (absolute) path name or URL.
6407 */
6408 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006409vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
6411 return (path_with_url(name) != 0 || mch_isFullName(name));
6412}
6413
6414/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006415 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 *
6417 * return FAIL for failure, OK otherwise
6418 */
6419 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006420vim_FullName(
6421 char_u *fname,
6422 char_u *buf,
6423 int len,
6424 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 int retval = OK;
6427 int url;
6428
6429 *buf = NUL;
6430 if (fname == NULL)
6431 return FAIL;
6432
6433 url = path_with_url(fname);
6434 if (!url)
6435 retval = mch_FullName(fname, buf, len, force);
6436 if (url || retval == FAIL)
6437 {
6438 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006439 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01006441#if defined(MACOS_CLASSIC) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 slash_adjust(buf);
6443#endif
6444 return retval;
6445}
6446
6447/*
6448 * Return the minimal number of rows that is needed on the screen to display
6449 * the current number of windows.
6450 */
6451 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006452min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006455#ifdef FEAT_WINDOWS
6456 tabpage_T *tp;
6457 int n;
6458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
6460 if (firstwin == NULL) /* not initialized yet */
6461 return MIN_LINES;
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006464 total = 0;
6465 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6466 {
6467 n = frame_minheight(tp->tp_topframe, NULL);
6468 if (total < n)
6469 total = n;
6470 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006471 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006473 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006475 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 return total;
6477}
6478
6479/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006480 * Return TRUE if there is only one window (in the current tab page), not
6481 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006482 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 */
6484 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006485only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487#ifdef FEAT_WINDOWS
6488 int count = 0;
6489 win_T *wp;
6490
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006491 /* If there is another tab page there always is another window. */
6492 if (first_tabpage->tp_next != NULL)
6493 return FALSE;
6494
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006496 if (wp->w_buffer != NULL
6497 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498# ifdef FEAT_QUICKFIX
6499 || wp->w_p_pvw
6500# endif
6501 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006502# ifdef FEAT_AUTOCMD
6503 && wp != aucmd_win
6504# endif
6505 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 ++count;
6507 return (count <= 1);
6508#else
6509 return TRUE;
6510#endif
6511}
6512
6513#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6514/*
6515 * Correct the cursor line number in other windows. Used after changing the
6516 * current buffer, and before applying autocommands.
6517 * When "do_curwin" is TRUE, also check current window.
6518 */
6519 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006520check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 win_T *wp;
6523
6524#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006525 tabpage_T *tp;
6526
6527 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6529#else
6530 wp = curwin;
6531 if (do_curwin)
6532#endif
6533 {
6534 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6535 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6536 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6537 wp->w_topline = curbuf->b_ml.ml_line_count;
6538 }
6539}
6540#endif
6541
6542#if defined(FEAT_WINDOWS) || defined(PROTO)
6543
6544/*
6545 * A snapshot of the window sizes, to restore them after closing the help
6546 * window.
6547 * Only these fields are used:
6548 * fr_layout
6549 * fr_width
6550 * fr_height
6551 * fr_next
6552 * fr_child
6553 * fr_win (only valid for the old curwin, NULL otherwise)
6554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555
6556/*
6557 * Create a snapshot of the current frame sizes.
6558 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006559 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006560make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006562 clear_snapshot(curtab, idx);
6563 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564}
6565
6566 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006567make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568{
6569 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6570 if (*frp == NULL)
6571 return;
6572 (*frp)->fr_layout = fr->fr_layout;
6573# ifdef FEAT_VERTSPLIT
6574 (*frp)->fr_width = fr->fr_width;
6575# endif
6576 (*frp)->fr_height = fr->fr_height;
6577 if (fr->fr_next != NULL)
6578 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6579 if (fr->fr_child != NULL)
6580 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6581 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6582 (*frp)->fr_win = curwin;
6583}
6584
6585/*
6586 * Remove any existing snapshot.
6587 */
6588 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006589clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006591 clear_snapshot_rec(tp->tp_snapshot[idx]);
6592 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593}
6594
6595 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006596clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597{
6598 if (fr != NULL)
6599 {
6600 clear_snapshot_rec(fr->fr_next);
6601 clear_snapshot_rec(fr->fr_child);
6602 vim_free(fr);
6603 }
6604}
6605
6606/*
6607 * Restore a previously created snapshot, if there is any.
6608 * This is only done if the screen size didn't change and the window layout is
6609 * still the same.
6610 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006611 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006612restore_snapshot(
6613 int idx,
6614 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 win_T *wp;
6617
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006618 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619# ifdef FEAT_VERTSPLIT
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006620 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006622 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6623 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006625 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626 win_comp_pos();
6627 if (wp != NULL && close_curwin)
6628 win_goto(wp);
6629 redraw_all_later(CLEAR);
6630 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006631 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632}
6633
6634/*
6635 * Check if frames "sn" and "fr" have the same layout, same following frames
6636 * and same children.
6637 */
6638 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006639check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
6641 if (sn->fr_layout != fr->fr_layout
6642 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6643 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6644 || (sn->fr_next != NULL
6645 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6646 || (sn->fr_child != NULL
6647 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6648 return FAIL;
6649 return OK;
6650}
6651
6652/*
6653 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6654 * following frames and children.
6655 * Returns a pointer to the old current window, or NULL.
6656 */
6657 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006658restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 win_T *wp = NULL;
6661 win_T *wp2;
6662
6663 fr->fr_height = sn->fr_height;
6664# ifdef FEAT_VERTSPLIT
6665 fr->fr_width = sn->fr_width;
6666# endif
6667 if (fr->fr_layout == FR_LEAF)
6668 {
6669 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
6670# ifdef FEAT_VERTSPLIT
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006671 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672# endif
6673 wp = sn->fr_win;
6674 }
6675 if (sn->fr_next != NULL)
6676 {
6677 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6678 if (wp2 != NULL)
6679 wp = wp2;
6680 }
6681 if (sn->fr_child != NULL)
6682 {
6683 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6684 if (wp2 != NULL)
6685 wp = wp2;
6686 }
6687 return wp;
6688}
6689
6690#endif
6691
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006692#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6693 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006694/*
6695 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006696 * restore_win() MUST be called to undo, also when FAIL is returned.
6697 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006698 * When "no_display" is TRUE the display won't be affected, no redraw is
6699 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006700 * Returns FAIL if switching to "win" failed.
6701 */
6702 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006703switch_win(
6704 win_T **save_curwin UNUSED,
6705 tabpage_T **save_curtab UNUSED,
6706 win_T *win UNUSED,
6707 tabpage_T *tp UNUSED,
6708 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006709{
6710# ifdef FEAT_AUTOCMD
6711 block_autocmds();
6712# endif
6713# ifdef FEAT_WINDOWS
6714 *save_curwin = curwin;
6715 if (tp != NULL)
6716 {
6717 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006718 if (no_display)
6719 {
6720 curtab->tp_firstwin = firstwin;
6721 curtab->tp_lastwin = lastwin;
6722 curtab = tp;
6723 firstwin = curtab->tp_firstwin;
6724 lastwin = curtab->tp_lastwin;
6725 }
6726 else
6727 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006728 }
6729 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006730 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006731 curwin = win;
6732 curbuf = curwin->w_buffer;
6733# endif
6734 return OK;
6735}
6736
6737/*
6738 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006739 * When "no_display" is TRUE the display won't be affected, no redraw is
6740 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006741 */
6742 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006743restore_win(
6744 win_T *save_curwin UNUSED,
6745 tabpage_T *save_curtab UNUSED,
6746 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006747{
6748# ifdef FEAT_WINDOWS
6749 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006750 {
6751 if (no_display)
6752 {
6753 curtab->tp_firstwin = firstwin;
6754 curtab->tp_lastwin = lastwin;
6755 curtab = save_curtab;
6756 firstwin = curtab->tp_firstwin;
6757 lastwin = curtab->tp_lastwin;
6758 }
6759 else
6760 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6761 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006762 if (win_valid(save_curwin))
6763 {
6764 curwin = save_curwin;
6765 curbuf = curwin->w_buffer;
6766 }
6767# endif
6768# ifdef FEAT_AUTOCMD
6769 unblock_autocmds();
6770# endif
6771}
6772
6773/*
6774 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6775 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6776 */
6777 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006778switch_buffer(buf_T **save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006779{
6780# ifdef FEAT_AUTOCMD
6781 block_autocmds();
6782# endif
6783 *save_curbuf = curbuf;
6784 --curbuf->b_nwindows;
6785 curbuf = buf;
6786 curwin->w_buffer = buf;
6787 ++curbuf->b_nwindows;
6788}
6789
6790/*
6791 * Restore the current buffer after using switch_buffer().
6792 */
6793 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006794restore_buffer(buf_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006795{
6796# ifdef FEAT_AUTOCMD
6797 unblock_autocmds();
6798# endif
6799 /* Check for valid buffer, just in case. */
6800 if (buf_valid(save_curbuf))
6801 {
6802 --curbuf->b_nwindows;
6803 curwin->w_buffer = save_curbuf;
6804 curbuf = save_curbuf;
6805 ++curbuf->b_nwindows;
6806 }
6807}
6808#endif
6809
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810#if (defined(FEAT_GUI) && defined(FEAT_VERTSPLIT)) || defined(PROTO)
6811/*
6812 * Return TRUE if there is any vertically split window.
6813 */
6814 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006815win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816{
6817 frame_T *fr;
6818
6819 if (topframe->fr_layout == FR_ROW)
6820 return TRUE;
6821
6822 if (topframe->fr_layout == FR_COL)
6823 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6824 if (fr->fr_layout == FR_ROW)
6825 return TRUE;
6826
6827 return FALSE;
6828}
6829#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006830
6831#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6832/*
6833 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006834 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006835 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6836 * If no particular ID is desired, -1 must be specified for 'id'.
6837 * Return ID of added match, -1 on failure.
6838 */
6839 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006840match_add(
6841 win_T *wp,
6842 char_u *grp,
6843 char_u *pat,
6844 int prio,
6845 int id,
6846 list_T *pos_list,
6847 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006848{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006849 matchitem_T *cur;
6850 matchitem_T *prev;
6851 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006852 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006853 regprog_T *regprog = NULL;
6854 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006855
Bram Moolenaarb3414592014-06-17 17:48:32 +02006856 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006857 return -1;
6858 if (id < -1 || id == 0)
6859 {
6860 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6861 return -1;
6862 }
6863 if (id != -1)
6864 {
6865 cur = wp->w_match_head;
6866 while (cur != NULL)
6867 {
6868 if (cur->id == id)
6869 {
6870 EMSGN("E801: ID already taken: %ld", id);
6871 return -1;
6872 }
6873 cur = cur->next;
6874 }
6875 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006876 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006877 {
6878 EMSG2(_(e_nogroup), grp);
6879 return -1;
6880 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006881 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006882 {
6883 EMSG2(_(e_invarg2), pat);
6884 return -1;
6885 }
6886
6887 /* Find available match ID. */
6888 while (id == -1)
6889 {
6890 cur = wp->w_match_head;
6891 while (cur != NULL && cur->id != wp->w_next_match_id)
6892 cur = cur->next;
6893 if (cur == NULL)
6894 id = wp->w_next_match_id;
6895 wp->w_next_match_id++;
6896 }
6897
6898 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006899 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006900 m->id = id;
6901 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006902 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006903 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006904 m->match.regprog = regprog;
6905 m->match.rmm_ic = FALSE;
6906 m->match.rmm_maxcol = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02006907#ifdef FEAT_CONCEAL
6908 m->conceal_char = 0;
6909 if (conceal_char != NULL)
6910 m->conceal_char = (*mb_ptr2char)(conceal_char);
6911#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006912
Bram Moolenaarb3414592014-06-17 17:48:32 +02006913 /* Set up position matches */
6914 if (pos_list != NULL)
6915 {
6916 linenr_T toplnum = 0;
6917 linenr_T botlnum = 0;
6918 listitem_T *li;
6919 int i;
6920
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006921 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006922 i++, li = li->li_next)
6923 {
6924 linenr_T lnum = 0;
6925 colnr_T col = 0;
6926 int len = 1;
6927 list_T *subl;
6928 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006929 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006930
Bram Moolenaarb3414592014-06-17 17:48:32 +02006931 if (li->li_tv.v_type == VAR_LIST)
6932 {
6933 subl = li->li_tv.vval.v_list;
6934 if (subl == NULL)
6935 goto fail;
6936 subli = subl->lv_first;
6937 if (subli == NULL)
6938 goto fail;
6939 lnum = get_tv_number_chk(&subli->li_tv, &error);
6940 if (error == TRUE)
6941 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006942 if (lnum == 0)
6943 {
6944 --i;
6945 continue;
6946 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006947 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006948 subli = subli->li_next;
6949 if (subli != NULL)
6950 {
6951 col = get_tv_number_chk(&subli->li_tv, &error);
6952 if (error == TRUE)
6953 goto fail;
6954 subli = subli->li_next;
6955 if (subli != NULL)
6956 {
6957 len = get_tv_number_chk(&subli->li_tv, &error);
6958 if (error == TRUE)
6959 goto fail;
6960 }
6961 }
6962 m->pos.pos[i].col = col;
6963 m->pos.pos[i].len = len;
6964 }
6965 else if (li->li_tv.v_type == VAR_NUMBER)
6966 {
6967 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006968 {
6969 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006970 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006971 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006972 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6973 m->pos.pos[i].col = 0;
6974 m->pos.pos[i].len = 0;
6975 }
6976 else
6977 {
6978 EMSG(_("List or number required"));
6979 goto fail;
6980 }
6981 if (toplnum == 0 || lnum < toplnum)
6982 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006983 if (botlnum == 0 || lnum >= botlnum)
6984 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006985 }
6986
6987 /* Calculate top and bottom lines for redrawing area */
6988 if (toplnum != 0)
6989 {
6990 if (wp->w_buffer->b_mod_set)
6991 {
6992 if (wp->w_buffer->b_mod_top > toplnum)
6993 wp->w_buffer->b_mod_top = toplnum;
6994 if (wp->w_buffer->b_mod_bot < botlnum)
6995 wp->w_buffer->b_mod_bot = botlnum;
6996 }
6997 else
6998 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006999 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007000 wp->w_buffer->b_mod_top = toplnum;
7001 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007002 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007003 }
7004 m->pos.toplnum = toplnum;
7005 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007006 rtype = VALID;
7007 }
7008 }
7009
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007010 /* Insert new match. The match list is in ascending order with regard to
7011 * the match priorities. */
7012 cur = wp->w_match_head;
7013 prev = cur;
7014 while (cur != NULL && prio >= cur->priority)
7015 {
7016 prev = cur;
7017 cur = cur->next;
7018 }
7019 if (cur == prev)
7020 wp->w_match_head = m;
7021 else
7022 prev->next = m;
7023 m->next = cur;
7024
Bram Moolenaarb3414592014-06-17 17:48:32 +02007025 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007026 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007027
7028fail:
7029 vim_free(m);
7030 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007031}
7032
7033/*
7034 * Delete match with ID 'id' in the match list of window 'wp'.
7035 * Print error messages if 'perr' is TRUE.
7036 */
7037 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007038match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007039{
Bram Moolenaarb3414592014-06-17 17:48:32 +02007040 matchitem_T *cur = wp->w_match_head;
7041 matchitem_T *prev = cur;
7042 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007043
7044 if (id < 1)
7045 {
7046 if (perr == TRUE)
7047 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
7048 id);
7049 return -1;
7050 }
7051 while (cur != NULL && cur->id != id)
7052 {
7053 prev = cur;
7054 cur = cur->next;
7055 }
7056 if (cur == NULL)
7057 {
7058 if (perr == TRUE)
7059 EMSGN("E803: ID not found: %ld", id);
7060 return -1;
7061 }
7062 if (cur == prev)
7063 wp->w_match_head = cur->next;
7064 else
7065 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007066 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007067 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007068 if (cur->pos.toplnum != 0)
7069 {
7070 if (wp->w_buffer->b_mod_set)
7071 {
7072 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
7073 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7074 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
7075 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
7076 }
7077 else
7078 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02007079 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007080 wp->w_buffer->b_mod_top = cur->pos.toplnum;
7081 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02007082 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02007083 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02007084 rtype = VALID;
7085 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007086 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02007087 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007088 return 0;
7089}
7090
7091/*
7092 * Delete all matches in the match list of window 'wp'.
7093 */
7094 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007095clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007096{
7097 matchitem_T *m;
7098
7099 while (wp->w_match_head != NULL)
7100 {
7101 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02007102 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007103 vim_free(wp->w_match_head->pattern);
7104 vim_free(wp->w_match_head);
7105 wp->w_match_head = m;
7106 }
7107 redraw_later(SOME_VALID);
7108}
7109
7110/*
7111 * Get match from ID 'id' in window 'wp'.
7112 * Return NULL if match not found.
7113 */
7114 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007115get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007116{
7117 matchitem_T *cur = wp->w_match_head;
7118
7119 while (cur != NULL && cur->id != id)
7120 cur = cur->next;
7121 return cur;
7122}
7123#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02007124
7125#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7126 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007127get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007128{
7129 int i = 1;
7130 win_T *w;
7131
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007132 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007133 ++i;
7134
7135 if (w == NULL)
7136 return 0;
7137 else
7138 return i;
7139}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007140
7141 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007142get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007143{
7144 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007145# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007146 tabpage_T *t;
7147
7148 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7149 ++i;
7150
7151 if (t == NULL)
7152 return 0;
7153 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007154# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007155 return i;
7156}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007157#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007158
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007159#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007160/*
7161 * Return TRUE if "topfrp" and its children are at the right height.
7162 */
7163 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007164frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007165{
7166 frame_T *frp;
7167
7168 if (topfrp->fr_height != height)
7169 return FALSE;
7170
7171 if (topfrp->fr_layout == FR_ROW)
7172 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7173 if (frp->fr_height != height)
7174 return FALSE;
7175
7176 return TRUE;
7177}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007178#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007179
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007180#if defined(FEAT_VERTSPLIT) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007181/*
7182 * Return TRUE if "topfrp" and its children are at the right width.
7183 */
7184 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007185frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007186{
7187 frame_T *frp;
7188
7189 if (topfrp->fr_width != width)
7190 return FALSE;
7191
7192 if (topfrp->fr_layout == FR_COL)
7193 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7194 if (frp->fr_width != width)
7195 return FALSE;
7196
7197 return TRUE;
7198}
7199#endif
7200