blob: d590c57ed421847c205f2e40b8ed3f4c697310cf [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 Moolenaarbaaa7e92016-01-29 22:47:03 +010019static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010020static void win_exchange(long);
21static void win_rotate(int, int);
22static void win_totop(int size, int flags);
23static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
24static int last_window(void);
25static int close_last_window_tabpage(win_T *win, int free_buf, tabpage_T *prev_curtab);
26static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
27static frame_T *win_altframe(win_T *win, tabpage_T *tp);
28static tabpage_T *alt_tabpage(void);
29static win_T *frame2win(frame_T *frp);
30static int frame_has_win(frame_T *frp, win_T *wp);
31static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
32static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010033static int frame_fixed_width(frame_T *frp);
34static void frame_add_statusline(frame_T *frp);
35static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
36static void frame_add_vsep(frame_T *frp);
37static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
38static void frame_fix_width(win_T *wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010040static int win_alloc_firstwin(win_T *oldwin);
41static void new_frame(win_T *wp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000042#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010043static tabpage_T *alloc_tabpage(void);
44static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
45static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
46static void frame_fix_height(win_T *wp);
47static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
48static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_enter_autocmds, int trigger_leave_autocmds);
49static void win_free(win_T *wp, tabpage_T *tp);
50static void frame_append(frame_T *after, frame_T *frp);
51static void frame_insert(frame_T *before, frame_T *frp);
52static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010053static void win_goto_ver(int up, long count);
54static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void frame_add_height(frame_T *frp, int n);
56static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static void make_snapshot_rec(frame_T *fr, frame_T **frp);
59static void clear_snapshot(tabpage_T *tp, int idx);
60static void clear_snapshot_rec(frame_T *fr);
61static int check_snapshot_rec(frame_T *sn, frame_T *fr);
62static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000063
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010065static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020066
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif /* FEAT_WINDOWS */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000068
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010069static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
71#define URL_SLASH 1 /* path_is_url() has found "://" */
72#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
73
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000074#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaar05159a02005-02-26 23:04:13 +000076#ifdef FEAT_WINDOWS
Bram Moolenaar32466aa2006-02-24 23:53:04 +000077# define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000078#else
79# define ROWS_AVAIL (Rows - p_ch)
Bram Moolenaar05159a02005-02-26 23:04:13 +000080#endif
81
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000083
84static char *m_onlyone = N_("Already only one window");
85
Bram Moolenaar071d4272004-06-13 20:20:40 +000086/*
87 * all CTRL-W window commands are handled here, called from normal_cmd().
88 */
89 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010090do_window(
91 int nchar,
92 long Prenum,
93 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000094{
95 long Prenum1;
96 win_T *wp;
97#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
98 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000099 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#endif
101#ifdef FEAT_FIND_ID
102 int type = FIND_DEFINE;
103 int len;
104#endif
105 char_u cbuf[40];
106
107 if (Prenum == 0)
108 Prenum1 = 1;
109 else
110 Prenum1 = Prenum;
111
112#ifdef FEAT_CMDWIN
113# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
114#else
115# define CHECK_CMDWIN
116#endif
117
118 switch (nchar)
119 {
120/* split current window in two parts, horizontally */
121 case 'S':
122 case Ctrl_S:
123 case 's':
124 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000126#ifdef FEAT_QUICKFIX
127 /* When splitting the quickfix window open a new buffer in it,
128 * don't replicate the quickfix buffer. */
129 if (bt_quickfix(curbuf))
130 goto newwindow;
131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132#ifdef FEAT_GUI
133 need_mouse_correct = TRUE;
134#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200135 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 break;
137
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138/* split current window in two parts, vertically */
139 case Ctrl_V:
140 case 'v':
141 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100143#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000144 /* When splitting the quickfix window open a new buffer in it,
145 * don't replicate the quickfix buffer. */
146 if (bt_quickfix(curbuf))
147 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100148#endif
149#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100151#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200152 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154
155/* split current window and edit alternate file */
156 case Ctrl_HAT:
157 case '^':
158 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100160 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 do_cmdline_cmd(cbuf);
162 break;
163
164/* open new window */
165 case Ctrl_N:
166 case 'n':
167 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000169#ifdef FEAT_QUICKFIX
170newwindow:
171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000173 /* window height */
174 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 else
176 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100177#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000178 if (nchar == 'v' || nchar == Ctrl_V)
179 STRCAT(cbuf, "v");
180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 STRCAT(cbuf, "new");
182 do_cmdline_cmd(cbuf);
183 break;
184
185/* quit current window */
186 case Ctrl_Q:
187 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100189 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100190 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 break;
192
193/* close current window */
194 case Ctrl_C:
195 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100197 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100198 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 break;
200
201#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
202/* close preview window */
203 case Ctrl_Z:
204 case 'z':
205 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 do_cmdline_cmd((char_u *)"pclose");
208 break;
209
210/* cursor to preview window */
211 case 'P':
212 for (wp = firstwin; wp != NULL; wp = wp->w_next)
213 if (wp->w_p_pvw)
214 break;
215 if (wp == NULL)
216 EMSG(_("E441: There is no preview window"));
217 else
218 win_goto(wp);
219 break;
220#endif
221
222/* close all but current window */
223 case Ctrl_O:
224 case 'o':
225 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100227 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100228 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 break;
230
231/* cursor to next window with wrap around */
232 case Ctrl_W:
233 case 'w':
234/* cursor to previous window with wrap around */
235 case 'W':
236 CHECK_CMDWIN
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000237 if (firstwin == lastwin && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 beep_flush();
239 else
240 {
241 if (Prenum) /* go to specified window */
242 {
243 for (wp = firstwin; --Prenum > 0; )
244 {
245 if (wp->w_next == NULL)
246 break;
247 else
248 wp = wp->w_next;
249 }
250 }
251 else
252 {
253 if (nchar == 'W') /* go to previous window */
254 {
255 wp = curwin->w_prev;
256 if (wp == NULL)
257 wp = lastwin; /* wrap around */
258 }
259 else /* go to next window */
260 {
261 wp = curwin->w_next;
262 if (wp == NULL)
263 wp = firstwin; /* wrap around */
264 }
265 }
266 win_goto(wp);
267 }
268 break;
269
270/* cursor to window below */
271 case 'j':
272 case K_DOWN:
273 case Ctrl_J:
274 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 break;
277
278/* cursor to window above */
279 case 'k':
280 case K_UP:
281 case Ctrl_K:
282 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 break;
285
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286/* cursor to left window */
287 case 'h':
288 case K_LEFT:
289 case Ctrl_H:
290 case K_BS:
291 CHECK_CMDWIN
292 win_goto_hor(TRUE, Prenum1);
293 break;
294
295/* cursor to right window */
296 case 'l':
297 case K_RIGHT:
298 case Ctrl_L:
299 CHECK_CMDWIN
300 win_goto_hor(FALSE, Prenum1);
301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000303/* move window to new tab page */
304 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000305 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000306 MSG(_(m_onlyone));
307 else
308 {
309 tabpage_T *oldtab = curtab;
310 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000311
312 /* First create a new tab with the window, then go back to
313 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000314 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000315 if (win_new_tabpage((int)Prenum) == OK
316 && valid_tabpage(oldtab))
317 {
318 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200319 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000320 if (curwin == wp)
321 win_close(curwin, FALSE);
322 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200323 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000324 }
325 }
326 break;
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328/* cursor to top-left window */
329 case 't':
330 case Ctrl_T:
331 win_goto(firstwin);
332 break;
333
334/* cursor to bottom-right window */
335 case 'b':
336 case Ctrl_B:
337 win_goto(lastwin);
338 break;
339
340/* cursor to last accessed (previous) window */
341 case 'p':
342 case Ctrl_P:
343 if (prevwin == NULL)
344 beep_flush();
345 else
346 win_goto(prevwin);
347 break;
348
349/* exchange current and next window */
350 case 'x':
351 case Ctrl_X:
352 CHECK_CMDWIN
353 win_exchange(Prenum);
354 break;
355
356/* rotate windows downwards */
357 case Ctrl_R:
358 case 'r':
359 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 win_rotate(FALSE, (int)Prenum1); /* downwards */
362 break;
363
364/* rotate windows upwards */
365 case 'R':
366 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 win_rotate(TRUE, (int)Prenum1); /* upwards */
369 break;
370
371/* move window to the very top/bottom/left/right */
372 case 'K':
373 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 case 'H':
375 case 'L':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 CHECK_CMDWIN
377 win_totop((int)Prenum,
378 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
379 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
380 break;
381
382/* make all windows the same height */
383 case '=':
384#ifdef FEAT_GUI
385 need_mouse_correct = TRUE;
386#endif
387 win_equal(NULL, FALSE, 'b');
388 break;
389
390/* increase current window height */
391 case '+':
392#ifdef FEAT_GUI
393 need_mouse_correct = TRUE;
394#endif
395 win_setheight(curwin->w_height + (int)Prenum1);
396 break;
397
398/* decrease current window height */
399 case '-':
400#ifdef FEAT_GUI
401 need_mouse_correct = TRUE;
402#endif
403 win_setheight(curwin->w_height - (int)Prenum1);
404 break;
405
406/* set current window height */
407 case Ctrl__:
408 case '_':
409#ifdef FEAT_GUI
410 need_mouse_correct = TRUE;
411#endif
412 win_setheight(Prenum ? (int)Prenum : 9999);
413 break;
414
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415/* increase current window width */
416 case '>':
417#ifdef FEAT_GUI
418 need_mouse_correct = TRUE;
419#endif
420 win_setwidth(curwin->w_width + (int)Prenum1);
421 break;
422
423/* decrease current window width */
424 case '<':
425#ifdef FEAT_GUI
426 need_mouse_correct = TRUE;
427#endif
428 win_setwidth(curwin->w_width - (int)Prenum1);
429 break;
430
431/* set current window width */
432 case '|':
433#ifdef FEAT_GUI
434 need_mouse_correct = TRUE;
435#endif
436 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
439/* jump to tag and split window if tag exists (in preview window) */
440#if defined(FEAT_QUICKFIX)
441 case '}':
442 CHECK_CMDWIN
443 if (Prenum)
444 g_do_tagpreview = Prenum;
445 else
446 g_do_tagpreview = p_pvh;
447 /*FALLTHROUGH*/
448#endif
449 case ']':
450 case Ctrl_RSB:
451 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200452 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (Prenum)
454 postponed_split = Prenum;
455 else
456 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200457#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200458 if (nchar != '}')
459 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
Bram Moolenaard355c502014-09-23 13:48:43 +0200462 /* Execute the command right here, required when "wincmd ]"
463 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 do_nv_ident(Ctrl_RSB, NUL);
465 break;
466
467#ifdef FEAT_SEARCHPATH
468/* edit file name under cursor in a new window */
469 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000470 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000472wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000474
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000475 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (ptr != NULL)
477 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000478# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 setpcmark();
482 if (win_split(0, 0) == OK)
483 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200484 RESET_BINDING(curwin);
Bram Moolenaar701f7af2008-11-15 13:12:07 +0000485 (void)do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
486 ECMD_HIDE, NULL);
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000487 if (nchar == 'F' && lnum >= 0)
488 {
489 curwin->w_cursor.lnum = lnum;
490 check_cursor_lnum();
491 beginline(BL_SOL | BL_FIX);
492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 }
494 vim_free(ptr);
495 }
496 break;
497#endif
498
499#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000500/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 * new window -- webb
502 */
503 case 'i': /* Go to any match */
504 case Ctrl_I:
505 type = FIND_ANY;
506 /* FALLTHROUGH */
507 case 'd': /* Go to definition, using 'define' */
508 case Ctrl_D:
509 CHECK_CMDWIN
510 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
511 break;
512 find_pattern_in_path(ptr, 0, len, TRUE,
513 Prenum == 0 ? TRUE : FALSE, type,
514 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
515 curwin->w_set_curswant = TRUE;
516 break;
517#endif
518
Bram Moolenaar05159a02005-02-26 23:04:13 +0000519 case K_KENTER:
520 case CAR:
521#if defined(FEAT_QUICKFIX)
522 /*
523 * In a quickfix window a <CR> jumps to the error under the
524 * cursor in a new window.
525 */
526 if (bt_quickfix(curbuf))
527 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000528 sprintf((char *)cbuf, "split +%ld%s",
529 (long)curwin->w_cursor.lnum,
530 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000531 do_cmdline_cmd(cbuf);
532 }
533#endif
534 break;
535
536
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537/* CTRL-W g extended commands */
538 case 'g':
539 case Ctrl_G:
540 CHECK_CMDWIN
541#ifdef USE_ON_FLY_SCROLL
542 dont_scroll = TRUE; /* disallow scrolling here */
543#endif
544 ++no_mapping;
545 ++allow_keys; /* no mapping for xchar, but allow key codes */
546 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000547 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 --no_mapping;
550 --allow_keys;
551#ifdef FEAT_CMDL_INFO
552 (void)add_to_showcmd(xchar);
553#endif
554 switch (xchar)
555 {
556#if defined(FEAT_QUICKFIX)
557 case '}':
558 xchar = Ctrl_RSB;
559 if (Prenum)
560 g_do_tagpreview = Prenum;
561 else
562 g_do_tagpreview = p_pvh;
563 /*FALLTHROUGH*/
564#endif
565 case ']':
566 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200567 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 if (Prenum)
569 postponed_split = Prenum;
570 else
571 postponed_split = -1;
572
573 /* Execute the command right here, required when
574 * "wincmd g}" was used in a function. */
575 do_nv_ident('g', xchar);
576 break;
577
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000578#ifdef FEAT_SEARCHPATH
579 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000580 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100581 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000582 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000583 goto wingotofile;
584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 default:
586 beep_flush();
587 break;
588 }
589 break;
590
591 default: beep_flush();
592 break;
593 }
594}
595
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100596/*
597 * Figure out the address type for ":wnncmd".
598 */
599 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100600get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100601{
602 switch (*arg)
603 {
604 case 'S':
605 case Ctrl_S:
606 case 's':
607 case Ctrl_N:
608 case 'n':
609 case 'j':
610 case Ctrl_J:
611 case 'k':
612 case Ctrl_K:
613 case 'T':
614 case Ctrl_R:
615 case 'r':
616 case 'R':
617 case 'K':
618 case 'J':
619 case '+':
620 case '-':
621 case Ctrl__:
622 case '_':
623 case '|':
624 case ']':
625 case Ctrl_RSB:
626 case 'g':
627 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100628 case Ctrl_V:
629 case 'v':
630 case 'h':
631 case Ctrl_H:
632 case 'l':
633 case Ctrl_L:
634 case 'H':
635 case 'L':
636 case '>':
637 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100638#if defined(FEAT_QUICKFIX)
639 case '}':
640#endif
641#ifdef FEAT_SEARCHPATH
642 case 'f':
643 case 'F':
644 case Ctrl_F:
645#endif
646#ifdef FEAT_FIND_ID
647 case 'i':
648 case Ctrl_I:
649 case 'd':
650 case Ctrl_D:
651#endif
652 /* window size or any count */
653 eap->addr_type = ADDR_LINES;
654 break;
655
656 case Ctrl_HAT:
657 case '^':
658 /* buffer number */
659 eap->addr_type = ADDR_BUFFERS;
660 break;
661
662 case Ctrl_Q:
663 case 'q':
664 case Ctrl_C:
665 case 'c':
666 case Ctrl_O:
667 case 'o':
668 case Ctrl_W:
669 case 'w':
670 case 'W':
671 case 'x':
672 case Ctrl_X:
673 /* window number */
674 eap->addr_type = ADDR_WINDOWS;
675 break;
676
677#if defined(FEAT_QUICKFIX)
678 case Ctrl_Z:
679 case 'z':
680 case 'P':
681#endif
682 case 't':
683 case Ctrl_T:
684 case 'b':
685 case Ctrl_B:
686 case 'p':
687 case Ctrl_P:
688 case '=':
689 case CAR:
690 /* no count */
691 eap->addr_type = 0;
692 break;
693 }
694}
695
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100696 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100697cmd_with_count(
698 char *cmd,
699 char_u *bufp,
700 size_t bufsize,
701 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100702{
703 size_t len = STRLEN(cmd);
704
705 STRCPY(bufp, cmd);
706 if (Prenum > 0)
707 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
708}
709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710/*
711 * split the current window, implements CTRL-W s and :split
712 *
713 * "size" is the height or width for the new window, 0 to use half of current
714 * height or width.
715 *
716 * "flags":
717 * WSP_ROOM: require enough room for new window
718 * WSP_VERT: vertical split.
719 * WSP_TOP: open window at the top-left of the shell (help window).
720 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
721 * WSP_HELP: creating the help window, keep layout snapshot
722 *
723 * return FAIL for failure, OK otherwise
724 */
725 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100726win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000728 /* When the ":tab" modifier was used open a new tab page instead. */
729 if (may_open_tabpage() == OK)
730 return OK;
731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 /* Add flags from ":vertical", ":topleft" and ":botright". */
733 flags |= cmdmod.split;
734 if ((flags & WSP_TOP) && (flags & WSP_BOT))
735 {
736 EMSG(_("E442: Can't split topleft and botright at the same time"));
737 return FAIL;
738 }
739
740 /* When creating the help window make a snapshot of the window layout.
741 * Otherwise clear the snapshot, it's now invalid. */
742 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000743 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000745 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
747 return win_split_ins(size, flags, NULL, 0);
748}
749
750/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100751 * When "new_wp" is NULL: split the current window in two.
752 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 * top/left/right/bottom.
754 * return FAIL for failure, OK otherwise
755 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000756 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100757win_split_ins(
758 int size,
759 int flags,
760 win_T *new_wp,
761 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100763 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 win_T *oldwin;
765 int new_size = size;
766 int i;
767 int need_status = 0;
768 int do_equal = FALSE;
769 int needed;
770 int available;
771 int oldwin_height = 0;
772 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200773 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200775 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200776 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777
778 if (flags & WSP_TOP)
779 oldwin = firstwin;
780 else if (flags & WSP_BOT)
781 oldwin = lastwin;
782 else
783 oldwin = curwin;
784
785 /* add a status line when p_ls == 1 and splitting the first window */
786 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
787 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100788 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
790 EMSG(_(e_noroom));
791 return FAIL;
792 }
793 need_status = STATUS_HEIGHT;
794 }
795
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000796#ifdef FEAT_GUI
797 /* May be needed for the scrollbars that are going to change. */
798 if (gui.in_use)
799 out_flush();
800#endif
801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (flags & WSP_VERT)
803 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200804 int wmw1;
805 int minwidth;
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 /*
810 * Check if we are able to split the current window and compute its
811 * width.
812 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200813 /* Current window requires at least 1 space. */
814 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
815 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200817 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200818 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200820 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200822 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200824 else if (p_ea)
825 {
826 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
827 prevfrp = oldwin->w_frame;
828 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
829 frp = frp->fr_parent)
830 {
831 if (frp->fr_layout == FR_ROW)
832 for (frp2 = frp->fr_child; frp2 != NULL;
833 frp2 = frp2->fr_next)
834 if (frp2 != prevfrp)
835 minwidth += frame_minwidth(frp2, NOWIN);
836 prevfrp = frp;
837 }
838 available = topframe->fr_width;
839 needed += minwidth;
840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200842 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200843 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
844 available = oldwin->w_frame->fr_width;
845 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200846 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100847 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 {
849 EMSG(_(e_noroom));
850 return FAIL;
851 }
852 if (new_size == 0)
853 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200854 if (new_size > available - minwidth - 1)
855 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200856 if (new_size < wmw1)
857 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 /* if it doesn't fit in the current window, need win_equal() */
860 if (oldwin->w_width - new_size - 1 < p_wmw)
861 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000862
863 /* We don't like to take lines for the new window from a
864 * 'winfixwidth' window. Take them from a window to the left or right
865 * instead, if possible. */
866 if (oldwin->w_p_wfw)
867 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000868
869 /* Only make all windows the same width if one of them (except oldwin)
870 * is wider than one of the split windows. */
871 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
872 && oldwin->w_frame->fr_parent != NULL)
873 {
874 frp = oldwin->w_frame->fr_parent->fr_child;
875 while (frp != NULL)
876 {
877 if (frp->fr_win != oldwin && frp->fr_win != NULL
878 && (frp->fr_win->w_width > new_size
879 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100880 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000881 {
882 do_equal = TRUE;
883 break;
884 }
885 frp = frp->fr_next;
886 }
887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
891 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893 /*
894 * Check if we are able to split the current window and compute its
895 * height.
896 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200897 /* Current window requires at least 1 space. */
898 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
899 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200901 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200902 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200904 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200906 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200908 else if (p_ea)
909 {
910 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
911 prevfrp = oldwin->w_frame;
912 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
913 frp = frp->fr_parent)
914 {
915 if (frp->fr_layout == FR_COL)
916 for (frp2 = frp->fr_child; frp2 != NULL;
917 frp2 = frp2->fr_next)
918 if (frp2 != prevfrp)
919 minheight += frame_minheight(frp2, NOWIN);
920 prevfrp = frp;
921 }
922 available = topframe->fr_height;
923 needed += minheight;
924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 else
926 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200927 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
928 available = oldwin->w_frame->fr_height;
929 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100931 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 {
933 EMSG(_(e_noroom));
934 return FAIL;
935 }
936 oldwin_height = oldwin->w_height;
937 if (need_status)
938 {
939 oldwin->w_status_height = STATUS_HEIGHT;
940 oldwin_height -= STATUS_HEIGHT;
941 }
942 if (new_size == 0)
943 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200944 if (new_size > available - minheight - STATUS_HEIGHT)
945 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200946 if (new_size < wmh1)
947 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
949 /* if it doesn't fit in the current window, need win_equal() */
950 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
951 do_equal = TRUE;
952
953 /* We don't like to take lines for the new window from a
954 * 'winfixheight' window. Take them from a window above or below
955 * instead, if possible. */
956 if (oldwin->w_p_wfh)
957 {
958 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
959 oldwin);
960 oldwin_height = oldwin->w_height;
961 if (need_status)
962 oldwin_height -= STATUS_HEIGHT;
963 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000964
965 /* Only make all windows the same height if one of them (except oldwin)
966 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100967 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000968 && oldwin->w_frame->fr_parent != NULL)
969 {
970 frp = oldwin->w_frame->fr_parent->fr_child;
971 while (frp != NULL)
972 {
973 if (frp->fr_win != oldwin && frp->fr_win != NULL
974 && (frp->fr_win->w_height > new_size
975 || frp->fr_win->w_height > oldwin_height - new_size
976 - STATUS_HEIGHT))
977 {
978 do_equal = TRUE;
979 break;
980 }
981 frp = frp->fr_next;
982 }
983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 }
985
986 /*
987 * allocate new window structure and link it in the window list
988 */
989 if ((flags & WSP_TOP) == 0
990 && ((flags & WSP_BOT)
991 || (flags & WSP_BELOW)
992 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100993 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
995 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100996 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000997 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 else
999 win_append(oldwin, wp);
1000 }
1001 else
1002 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001003 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001004 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 else
1006 win_append(oldwin->w_prev, wp);
1007 }
1008
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001009 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
1011 if (wp == NULL)
1012 return FAIL;
1013
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001014 new_frame(wp);
1015 if (wp->w_frame == NULL)
1016 {
1017 win_free(wp, NULL);
1018 return FAIL;
1019 }
1020
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001021 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001022 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024
1025 /*
1026 * Reorganise the tree of frames to insert the new window.
1027 */
1028 if (flags & (WSP_TOP | WSP_BOT))
1029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1031 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 {
1033 curfrp = topframe->fr_child;
1034 if (flags & WSP_BOT)
1035 while (curfrp->fr_next != NULL)
1036 curfrp = curfrp->fr_next;
1037 }
1038 else
1039 curfrp = topframe;
1040 before = (flags & WSP_TOP);
1041 }
1042 else
1043 {
1044 curfrp = oldwin->w_frame;
1045 if (flags & WSP_BELOW)
1046 before = FALSE;
1047 else if (flags & WSP_ABOVE)
1048 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001049 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 before = !p_spr;
1051 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 before = !p_sb;
1053 }
1054 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1055 {
1056 /* Need to create a new frame in the tree to make a branch. */
1057 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1058 *frp = *curfrp;
1059 curfrp->fr_layout = layout;
1060 frp->fr_parent = curfrp;
1061 frp->fr_next = NULL;
1062 frp->fr_prev = NULL;
1063 curfrp->fr_child = frp;
1064 curfrp->fr_win = NULL;
1065 curfrp = frp;
1066 if (frp->fr_win != NULL)
1067 oldwin->w_frame = frp;
1068 else
1069 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1070 frp->fr_parent = curfrp;
1071 }
1072
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001073 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001074 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001076 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 frp->fr_parent = curfrp->fr_parent;
1078
1079 /* Insert the new frame at the right place in the frame list. */
1080 if (before)
1081 frame_insert(curfrp, frp);
1082 else
1083 frame_append(curfrp, frp);
1084
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001085 /* Set w_fraction now so that the cursor keeps the same relative
1086 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001087 if (oldwin->w_height > 0)
1088 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001089 wp->w_fraction = oldwin->w_fraction;
1090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 if (flags & WSP_VERT)
1092 {
1093 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (need_status)
1096 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001097 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 oldwin->w_status_height = need_status;
1099 }
1100 if (flags & (WSP_TOP | WSP_BOT))
1101 {
1102 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001103 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001104 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 wp->w_status_height = (p_ls > 0);
1106 }
1107 else
1108 {
1109 /* height and row of new window is same as current window */
1110 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001111 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 wp->w_status_height = oldwin->w_status_height;
1113 }
1114 frp->fr_height = curfrp->fr_height;
1115
1116 /* "new_size" of the current window goes to the new window, use
1117 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001118 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 if (before)
1120 wp->w_vsep_width = 1;
1121 else
1122 {
1123 wp->w_vsep_width = oldwin->w_vsep_width;
1124 oldwin->w_vsep_width = 1;
1125 }
1126 if (flags & (WSP_TOP | WSP_BOT))
1127 {
1128 if (flags & WSP_BOT)
1129 frame_add_vsep(curfrp);
1130 /* Set width of neighbor frame */
1131 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001132 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1133 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 }
1135 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001136 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 if (before) /* new window left of current one */
1138 {
1139 wp->w_wincol = oldwin->w_wincol;
1140 oldwin->w_wincol += new_size + 1;
1141 }
1142 else /* new window right of current one */
1143 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1144 frame_fix_width(oldwin);
1145 frame_fix_width(wp);
1146 }
1147 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (flags & (WSP_TOP | WSP_BOT))
1151 {
1152 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001153 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 wp->w_vsep_width = 0;
1155 }
1156 else
1157 {
1158 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001159 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 wp->w_vsep_width = oldwin->w_vsep_width;
1161 }
1162 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 /* "new_size" of the current window goes to the new window, use
1165 * one row for the status line */
1166 win_new_height(wp, new_size);
1167 if (flags & (WSP_TOP | WSP_BOT))
1168 frame_new_height(curfrp, curfrp->fr_height
1169 - (new_size + STATUS_HEIGHT), flags & WSP_TOP, FALSE);
1170 else
1171 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1172 if (before) /* new window above current one */
1173 {
1174 wp->w_winrow = oldwin->w_winrow;
1175 wp->w_status_height = STATUS_HEIGHT;
1176 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1177 }
1178 else /* new window below current one */
1179 {
1180 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1181 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar5b14f892015-04-21 15:43:05 +02001182 /* Don't set the status_height for oldwin yet, this might break
1183 * frame_fix_height(oldwin), therefore will be set below. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (flags & WSP_BOT)
1186 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 frame_fix_height(wp);
1188 frame_fix_height(oldwin);
Bram Moolenaar5b14f892015-04-21 15:43:05 +02001189
1190 if (!before)
1191 /* new window above current one, set the status_height after
1192 * frame_fix_height(oldwin) */
1193 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 }
1195
1196 if (flags & (WSP_TOP | WSP_BOT))
1197 (void)win_comp_pos();
1198
1199 /*
1200 * Both windows need redrawing
1201 */
1202 redraw_win_later(wp, NOT_VALID);
1203 wp->w_redr_status = TRUE;
1204 redraw_win_later(oldwin, NOT_VALID);
1205 oldwin->w_redr_status = TRUE;
1206
1207 if (need_status)
1208 {
1209 msg_row = Rows - 1;
1210 msg_col = sc_col;
1211 msg_clr_eos_force(); /* Old command/ruler may still be there */
1212 comp_col();
1213 msg_row = Rows - 1;
1214 msg_col = 0; /* put position back at start of line */
1215 }
1216
1217 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001218 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 */
1220 if (do_equal || dir != 0)
1221 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001223 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
1225 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1226 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 if (flags & WSP_VERT)
1228 {
1229 i = p_wiw;
1230 if (size != 0)
1231 p_wiw = size;
1232
1233# ifdef FEAT_GUI
1234 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1235 if (gui.in_use)
1236 gui_init_which_components(NULL);
1237# endif
1238 }
1239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
1241 i = p_wh;
1242 if (size != 0)
1243 p_wh = size;
1244 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001245
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001246#ifdef FEAT_JUMPLIST
1247 /* Keep same changelist position in new window. */
1248 wp->w_changelistidx = oldwin->w_changelistidx;
1249#endif
1250
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001251 /*
1252 * make the new window the current window
1253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 if (flags & WSP_VERT)
1256 p_wiw = i;
1257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 p_wh = i;
1259
1260 return OK;
1261}
1262
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001263
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001264/*
1265 * Initialize window "newp" from window "oldp".
1266 * Used when splitting a window and when creating a new tab page.
1267 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001268 * WSP_NEWLOC may be specified in flags to prevent the location list from
1269 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001270 */
1271 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001272win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001273{
1274 int i;
1275
1276 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001277#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001278 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001279#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001280 oldp->w_buffer->b_nwindows++;
1281 newp->w_cursor = oldp->w_cursor;
1282 newp->w_valid = 0;
1283 newp->w_curswant = oldp->w_curswant;
1284 newp->w_set_curswant = oldp->w_set_curswant;
1285 newp->w_topline = oldp->w_topline;
1286#ifdef FEAT_DIFF
1287 newp->w_topfill = oldp->w_topfill;
1288#endif
1289 newp->w_leftcol = oldp->w_leftcol;
1290 newp->w_pcmark = oldp->w_pcmark;
1291 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1292 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001293 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001294 newp->w_fraction = oldp->w_fraction;
1295 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1296#ifdef FEAT_JUMPLIST
1297 copy_jumplist(oldp, newp);
1298#endif
1299#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001300 if (flags & WSP_NEWLOC)
1301 {
1302 /* Don't copy the location list. */
1303 newp->w_llist = NULL;
1304 newp->w_llist_ref = NULL;
1305 }
1306 else
1307 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001308#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001309 newp->w_localdir = (oldp->w_localdir == NULL)
1310 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001311
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001312 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001313 for (i = 0; i < oldp->w_tagstacklen; i++)
1314 {
1315 newp->w_tagstack[i] = oldp->w_tagstack[i];
1316 if (newp->w_tagstack[i].tagname != NULL)
1317 newp->w_tagstack[i].tagname =
1318 vim_strsave(newp->w_tagstack[i].tagname);
1319 }
1320 newp->w_tagstackidx = oldp->w_tagstackidx;
1321 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001322#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001323 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001324#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001325
1326 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001327
Bram Moolenaara971b822011-09-14 14:43:25 +02001328#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001329 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001330#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001331}
1332
1333/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001334 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001335 * Only the essential things are copied.
1336 */
1337 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001338win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001339{
1340 /* Use the same argument list. */
1341 newp->w_alist = oldp->w_alist;
1342 ++newp->w_alist->al_refcount;
1343 newp->w_arg_idx = oldp->w_arg_idx;
1344
1345 /* copy options from existing window */
1346 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001347}
1348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349#endif /* FEAT_WINDOWS */
1350
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#if defined(FEAT_WINDOWS) || defined(PROTO)
1352/*
1353 * Check if "win" is a pointer to an existing window.
1354 */
1355 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001356win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358 win_T *wp;
1359
1360 if (win == NULL)
1361 return FALSE;
1362 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1363 if (wp == win)
1364 return TRUE;
1365 return FALSE;
1366}
1367
1368/*
1369 * Return the number of windows.
1370 */
1371 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001372win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
1374 win_T *wp;
1375 int count = 0;
1376
1377 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1378 ++count;
1379 return count;
1380}
1381
1382/*
1383 * Make "count" windows on the screen.
1384 * Return actual number of windows on the screen.
1385 * Must be called when there is just one window, filling the whole screen
1386 * (excluding the command line).
1387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001389make_windows(
1390 int count,
1391 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392{
1393 int maxcount;
1394 int todo;
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (vertical)
1397 {
1398 /* Each windows needs at least 'winminwidth' lines and a separator
1399 * column. */
1400 maxcount = (curwin->w_width + curwin->w_vsep_width
1401 - (p_wiw - p_wmw)) / (p_wmw + 1);
1402 }
1403 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 {
1405 /* Each window needs at least 'winminheight' lines and a status line. */
1406 maxcount = (curwin->w_height + curwin->w_status_height
1407 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1408 }
1409
1410 if (maxcount < 2)
1411 maxcount = 2;
1412 if (count > maxcount)
1413 count = maxcount;
1414
1415 /*
1416 * add status line now, otherwise first window will be too big
1417 */
1418 if (count > 1)
1419 last_status(TRUE);
1420
1421#ifdef FEAT_AUTOCMD
1422 /*
1423 * Don't execute autocommands while creating the windows. Must do that
1424 * when putting the buffers in the windows.
1425 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001426 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427#endif
1428
1429 /* todo is number of windows left to create */
1430 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (vertical)
1432 {
1433 if (win_split(curwin->w_width - (curwin->w_width - todo)
1434 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1435 break;
1436 }
1437 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
1439 if (win_split(curwin->w_height - (curwin->w_height - todo
1440 * STATUS_HEIGHT) / (todo + 1)
1441 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1442 break;
1443 }
1444
1445#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001446 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447#endif
1448
1449 /* return actual number of windows */
1450 return (count - todo);
1451}
1452
1453/*
1454 * Exchange current and next window
1455 */
1456 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001457win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 frame_T *frp;
1460 frame_T *frp2;
1461 win_T *wp;
1462 win_T *wp2;
1463 int temp;
1464
1465 if (lastwin == firstwin) /* just one window */
1466 {
1467 beep_flush();
1468 return;
1469 }
1470
1471#ifdef FEAT_GUI
1472 need_mouse_correct = TRUE;
1473#endif
1474
1475 /*
1476 * find window to exchange with
1477 */
1478 if (Prenum)
1479 {
1480 frp = curwin->w_frame->fr_parent->fr_child;
1481 while (frp != NULL && --Prenum > 0)
1482 frp = frp->fr_next;
1483 }
1484 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1485 frp = curwin->w_frame->fr_next;
1486 else /* Swap last window in row/col with previous */
1487 frp = curwin->w_frame->fr_prev;
1488
1489 /* We can only exchange a window with another window, not with a frame
1490 * containing windows. */
1491 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1492 return;
1493 wp = frp->fr_win;
1494
1495/*
1496 * 1. remove curwin from the list. Remember after which window it was in wp2
1497 * 2. insert curwin before wp in the list
1498 * if wp != wp2
1499 * 3. remove wp from the list
1500 * 4. insert wp after wp2
1501 * 5. exchange the status line height and vsep width.
1502 */
1503 wp2 = curwin->w_prev;
1504 frp2 = curwin->w_frame->fr_prev;
1505 if (wp->w_prev != curwin)
1506 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001507 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 frame_remove(curwin->w_frame);
1509 win_append(wp->w_prev, curwin);
1510 frame_insert(frp, curwin->w_frame);
1511 }
1512 if (wp != wp2)
1513 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001514 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 frame_remove(wp->w_frame);
1516 win_append(wp2, wp);
1517 if (frp2 == NULL)
1518 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1519 else
1520 frame_append(frp2, wp->w_frame);
1521 }
1522 temp = curwin->w_status_height;
1523 curwin->w_status_height = wp->w_status_height;
1524 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 temp = curwin->w_vsep_width;
1526 curwin->w_vsep_width = wp->w_vsep_width;
1527 wp->w_vsep_width = temp;
1528
1529 /* If the windows are not in the same frame, exchange the sizes to avoid
1530 * messing up the window layout. Otherwise fix the frame sizes. */
1531 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1532 {
1533 temp = curwin->w_height;
1534 curwin->w_height = wp->w_height;
1535 wp->w_height = temp;
1536 temp = curwin->w_width;
1537 curwin->w_width = wp->w_width;
1538 wp->w_width = temp;
1539 }
1540 else
1541 {
1542 frame_fix_height(curwin);
1543 frame_fix_height(wp);
1544 frame_fix_width(curwin);
1545 frame_fix_width(wp);
1546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547
1548 (void)win_comp_pos(); /* recompute window positions */
1549
1550 win_enter(wp, TRUE);
1551 redraw_later(CLEAR);
1552}
1553
1554/*
1555 * rotate windows: if upwards TRUE the second window becomes the first one
1556 * if upwards FALSE the first window becomes the second one
1557 */
1558 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001559win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560{
1561 win_T *wp1;
1562 win_T *wp2;
1563 frame_T *frp;
1564 int n;
1565
1566 if (firstwin == lastwin) /* nothing to do */
1567 {
1568 beep_flush();
1569 return;
1570 }
1571
1572#ifdef FEAT_GUI
1573 need_mouse_correct = TRUE;
1574#endif
1575
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 /* Check if all frames in this row/col have one window. */
1577 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1578 frp = frp->fr_next)
1579 if (frp->fr_win == NULL)
1580 {
1581 EMSG(_("E443: Cannot rotate when another window is split"));
1582 return;
1583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584
1585 while (count--)
1586 {
1587 if (upwards) /* first window becomes last window */
1588 {
1589 /* remove first window/frame from the list */
1590 frp = curwin->w_frame->fr_parent->fr_child;
1591 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001592 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 frame_remove(frp);
1594
1595 /* find last frame and append removed window/frame after it */
1596 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1597 ;
1598 win_append(frp->fr_win, wp1);
1599 frame_append(frp, wp1->w_frame);
1600
1601 wp2 = frp->fr_win; /* previously last window */
1602 }
1603 else /* last window becomes first window */
1604 {
1605 /* find last window/frame in the list and remove it */
1606 for (frp = curwin->w_frame; frp->fr_next != NULL;
1607 frp = frp->fr_next)
1608 ;
1609 wp1 = frp->fr_win;
1610 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001611 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 frame_remove(frp);
1613
1614 /* append the removed window/frame before the first in the list */
1615 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1616 frame_insert(frp->fr_parent->fr_child, frp);
1617 }
1618
1619 /* exchange status height and vsep width of old and new last window */
1620 n = wp2->w_status_height;
1621 wp2->w_status_height = wp1->w_status_height;
1622 wp1->w_status_height = n;
1623 frame_fix_height(wp1);
1624 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 n = wp2->w_vsep_width;
1626 wp2->w_vsep_width = wp1->w_vsep_width;
1627 wp1->w_vsep_width = n;
1628 frame_fix_width(wp1);
1629 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001631 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 (void)win_comp_pos();
1633 }
1634
1635 redraw_later(CLEAR);
1636}
1637
1638/*
1639 * Move the current window to the very top/bottom/left/right of the screen.
1640 */
1641 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001642win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 int dir;
1645 int height = curwin->w_height;
1646
1647 if (lastwin == firstwin)
1648 {
1649 beep_flush();
1650 return;
1651 }
1652
1653 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001654 (void)winframe_remove(curwin, &dir, NULL);
1655 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 last_status(FALSE); /* may need to remove last status line */
1657 (void)win_comp_pos(); /* recompute window positions */
1658
1659 /* Split a window on the desired side and put the window there. */
1660 (void)win_split_ins(size, flags, curwin, dir);
1661 if (!(flags & WSP_VERT))
1662 {
1663 win_setheight(height);
1664 if (p_ea)
1665 win_equal(curwin, TRUE, 'v');
1666 }
1667
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001668#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1670 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001671 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673}
1674
1675/*
1676 * Move window "win1" to below/right of "win2" and make "win1" the current
1677 * window. Only works within the same frame!
1678 */
1679 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001680win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 int height;
1683
1684 /* check if the arguments are reasonable */
1685 if (win1 == win2)
1686 return;
1687
1688 /* check if there is something to do */
1689 if (win2->w_next != win1)
1690 {
1691 /* may need move the status line/vertical separator of the last window
1692 * */
1693 if (win1 == lastwin)
1694 {
1695 height = win1->w_prev->w_status_height;
1696 win1->w_prev->w_status_height = win1->w_status_height;
1697 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001698 if (win1->w_prev->w_vsep_width == 1)
1699 {
1700 /* Remove the vertical separator from the last-but-one window,
1701 * add it to the last window. Adjust the frame widths. */
1702 win1->w_prev->w_vsep_width = 0;
1703 win1->w_prev->w_frame->fr_width -= 1;
1704 win1->w_vsep_width = 1;
1705 win1->w_frame->fr_width += 1;
1706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708 else if (win2 == lastwin)
1709 {
1710 height = win1->w_status_height;
1711 win1->w_status_height = win2->w_status_height;
1712 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001713 if (win1->w_vsep_width == 1)
1714 {
1715 /* Remove the vertical separator from win1, add it to the last
1716 * window, win2. Adjust the frame widths. */
1717 win2->w_vsep_width = 1;
1718 win2->w_frame->fr_width += 1;
1719 win1->w_vsep_width = 0;
1720 win1->w_frame->fr_width -= 1;
1721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001723 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 frame_remove(win1->w_frame);
1725 win_append(win2, win1);
1726 frame_append(win2->w_frame, win1->w_frame);
1727
1728 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1729 redraw_later(NOT_VALID);
1730 }
1731 win_enter(win1, FALSE);
1732}
1733
1734/*
1735 * Make all windows the same height.
1736 * 'next_curwin' will soon be the current window, make sure it has enough
1737 * rows.
1738 */
1739 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001740win_equal(
1741 win_T *next_curwin, /* pointer to current window to be or NULL */
1742 int current, /* do only frame with current window */
1743 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 'b' for both, 0 for using p_ead */
1745{
1746 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001749 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001750 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751}
1752
1753/*
1754 * Set a frame to a new position and height, spreading the available room
1755 * equally over contained frames.
1756 * The window "next_curwin" (if not NULL) should at least get the size from
1757 * 'winheight' and 'winwidth' if possible.
1758 */
1759 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001760win_equal_rec(
1761 win_T *next_curwin, /* pointer to current window to be or NULL */
1762 int current, /* do only frame with current window */
1763 frame_T *topfr, /* frame to set size off */
1764 int dir, /* 'v', 'h' or 'b', see win_equal() */
1765 int col, /* horizontal position for frame */
1766 int row, /* vertical position for frame */
1767 int width, /* new width of frame */
1768 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 int n, m;
1771 int extra_sep = 0;
1772 int wincount, totwincount = 0;
1773 frame_T *fr;
1774 int next_curwin_size = 0;
1775 int room = 0;
1776 int new_size;
1777 int has_next_curwin = 0;
1778 int hnc;
1779
1780 if (topfr->fr_layout == FR_LEAF)
1781 {
1782 /* Set the width/height of this frame.
1783 * Redraw when size or position changes */
1784 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 )
1787 {
1788 topfr->fr_win->w_winrow = row;
1789 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001791 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 redraw_all_later(CLEAR);
1793 }
1794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 else if (topfr->fr_layout == FR_ROW)
1796 {
1797 topfr->fr_width = width;
1798 topfr->fr_height = height;
1799
1800 if (dir != 'v') /* equalize frame widths */
1801 {
1802 /* Compute the maximum number of windows horizontally in this
1803 * frame. */
1804 n = frame_minwidth(topfr, NOWIN);
1805 /* add one for the rightmost window, it doesn't have a separator */
1806 if (col + width == Columns)
1807 extra_sep = 1;
1808 else
1809 extra_sep = 0;
1810 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001811 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001813 /*
1814 * Compute width for "next_curwin" window and room available for
1815 * other windows.
1816 * "m" is the minimal width when counting p_wiw for "next_curwin".
1817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 m = frame_minwidth(topfr, next_curwin);
1819 room = width - m;
1820 if (room < 0)
1821 {
1822 next_curwin_size = p_wiw + room;
1823 room = 0;
1824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 else
1826 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001827 next_curwin_size = -1;
1828 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1829 {
1830 /* If 'winfixwidth' set keep the window width if
1831 * possible.
1832 * Watch out for this window being the next_curwin. */
1833 if (frame_fixed_width(fr))
1834 {
1835 n = frame_minwidth(fr, NOWIN);
1836 new_size = fr->fr_width;
1837 if (frame_has_win(fr, next_curwin))
1838 {
1839 room += p_wiw - p_wmw;
1840 next_curwin_size = 0;
1841 if (new_size < p_wiw)
1842 new_size = p_wiw;
1843 }
1844 else
1845 /* These windows don't use up room. */
1846 totwincount -= (n + (fr->fr_next == NULL
1847 ? extra_sep : 0)) / (p_wmw + 1);
1848 room -= new_size - n;
1849 if (room < 0)
1850 {
1851 new_size += room;
1852 room = 0;
1853 }
1854 fr->fr_newwidth = new_size;
1855 }
1856 }
1857 if (next_curwin_size == -1)
1858 {
1859 if (!has_next_curwin)
1860 next_curwin_size = 0;
1861 else if (totwincount > 1
1862 && (room + (totwincount - 2))
1863 / (totwincount - 1) > p_wiw)
1864 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001865 /* Can make all windows wider than 'winwidth', spread
1866 * the room equally. */
1867 next_curwin_size = (room + p_wiw
1868 + (totwincount - 1) * p_wmw
1869 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001870 room -= next_curwin_size - p_wiw;
1871 }
1872 else
1873 next_curwin_size = p_wiw;
1874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001876
1877 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 --totwincount; /* don't count curwin */
1879 }
1880
1881 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1882 {
1883 n = m = 0;
1884 wincount = 1;
1885 if (fr->fr_next == NULL)
1886 /* last frame gets all that remains (avoid roundoff error) */
1887 new_size = width;
1888 else if (dir == 'v')
1889 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001890 else if (frame_fixed_width(fr))
1891 {
1892 new_size = fr->fr_newwidth;
1893 wincount = 0; /* doesn't count as a sizeable window */
1894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 else
1896 {
1897 /* Compute the maximum number of windows horiz. in "fr". */
1898 n = frame_minwidth(fr, NOWIN);
1899 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1900 / (p_wmw + 1);
1901 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001902 if (has_next_curwin)
1903 hnc = frame_has_win(fr, next_curwin);
1904 else
1905 hnc = FALSE;
1906 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001908 if (totwincount == 0)
1909 new_size = room;
1910 else
1911 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001913 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
1915 next_curwin_size -= p_wiw - (m - n);
1916 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001917 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001919 else
1920 room -= new_size;
1921 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 }
1923
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001924 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 * window, unless equalizing all frames. */
1926 if (!current || dir != 'v' || topfr->fr_parent != NULL
1927 || (new_size != fr->fr_width)
1928 || frame_has_win(fr, next_curwin))
1929 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001930 new_size, height);
1931 col += new_size;
1932 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 totwincount -= wincount;
1934 }
1935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 else /* topfr->fr_layout == FR_COL */
1937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 topfr->fr_height = height;
1940
1941 if (dir != 'h') /* equalize frame heights */
1942 {
1943 /* Compute maximum number of windows vertically in this frame. */
1944 n = frame_minheight(topfr, NOWIN);
1945 /* add one for the bottom window if it doesn't have a statusline */
1946 if (row + height == cmdline_row && p_ls == 0)
1947 extra_sep = 1;
1948 else
1949 extra_sep = 0;
1950 totwincount = (n + extra_sep) / (p_wmh + 1);
1951 has_next_curwin = frame_has_win(topfr, next_curwin);
1952
1953 /*
1954 * Compute height for "next_curwin" window and room available for
1955 * other windows.
1956 * "m" is the minimal height when counting p_wh for "next_curwin".
1957 */
1958 m = frame_minheight(topfr, next_curwin);
1959 room = height - m;
1960 if (room < 0)
1961 {
1962 /* The room is less then 'winheight', use all space for the
1963 * current window. */
1964 next_curwin_size = p_wh + room;
1965 room = 0;
1966 }
1967 else
1968 {
1969 next_curwin_size = -1;
1970 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1971 {
1972 /* If 'winfixheight' set keep the window height if
1973 * possible.
1974 * Watch out for this window being the next_curwin. */
1975 if (frame_fixed_height(fr))
1976 {
1977 n = frame_minheight(fr, NOWIN);
1978 new_size = fr->fr_height;
1979 if (frame_has_win(fr, next_curwin))
1980 {
1981 room += p_wh - p_wmh;
1982 next_curwin_size = 0;
1983 if (new_size < p_wh)
1984 new_size = p_wh;
1985 }
1986 else
1987 /* These windows don't use up room. */
1988 totwincount -= (n + (fr->fr_next == NULL
1989 ? extra_sep : 0)) / (p_wmh + 1);
1990 room -= new_size - n;
1991 if (room < 0)
1992 {
1993 new_size += room;
1994 room = 0;
1995 }
1996 fr->fr_newheight = new_size;
1997 }
1998 }
1999 if (next_curwin_size == -1)
2000 {
2001 if (!has_next_curwin)
2002 next_curwin_size = 0;
2003 else if (totwincount > 1
2004 && (room + (totwincount - 2))
2005 / (totwincount - 1) > p_wh)
2006 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002007 /* can make all windows higher than 'winheight',
2008 * spread the room equally. */
2009 next_curwin_size = (room + p_wh
2010 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 + (totwincount - 1)) / totwincount;
2012 room -= next_curwin_size - p_wh;
2013 }
2014 else
2015 next_curwin_size = p_wh;
2016 }
2017 }
2018
2019 if (has_next_curwin)
2020 --totwincount; /* don't count curwin */
2021 }
2022
2023 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2024 {
2025 n = m = 0;
2026 wincount = 1;
2027 if (fr->fr_next == NULL)
2028 /* last frame gets all that remains (avoid roundoff error) */
2029 new_size = height;
2030 else if (dir == 'h')
2031 new_size = fr->fr_height;
2032 else if (frame_fixed_height(fr))
2033 {
2034 new_size = fr->fr_newheight;
2035 wincount = 0; /* doesn't count as a sizeable window */
2036 }
2037 else
2038 {
2039 /* Compute the maximum number of windows vert. in "fr". */
2040 n = frame_minheight(fr, NOWIN);
2041 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2042 / (p_wmh + 1);
2043 m = frame_minheight(fr, next_curwin);
2044 if (has_next_curwin)
2045 hnc = frame_has_win(fr, next_curwin);
2046 else
2047 hnc = FALSE;
2048 if (hnc) /* don't count next_curwin */
2049 --wincount;
2050 if (totwincount == 0)
2051 new_size = room;
2052 else
2053 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2054 / totwincount;
2055 if (hnc) /* add next_curwin size */
2056 {
2057 next_curwin_size -= p_wh - (m - n);
2058 new_size += next_curwin_size;
2059 room -= new_size - next_curwin_size;
2060 }
2061 else
2062 room -= new_size;
2063 new_size += n;
2064 }
2065 /* Skip frame that is full width when splitting or closing a
2066 * window, unless equalizing all frames. */
2067 if (!current || dir != 'h' || topfr->fr_parent != NULL
2068 || (new_size != fr->fr_height)
2069 || frame_has_win(fr, next_curwin))
2070 win_equal_rec(next_curwin, current, fr, dir, col, row,
2071 width, new_size);
2072 row += new_size;
2073 height -= new_size;
2074 totwincount -= wincount;
2075 }
2076 }
2077}
2078
2079/*
2080 * close all windows for buffer 'buf'
2081 */
2082 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002083close_windows(
2084 buf_T *buf,
2085 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002087 win_T *wp;
2088 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002089 int h = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090
2091 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002092
2093 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002095 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2096#ifdef FEAT_AUTOCMD
2097 && !(wp->w_closing || wp->w_buffer->b_closing)
2098#endif
2099 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002101 win_close(wp, FALSE);
2102
2103 /* Start all over, autocommands may change the window layout. */
2104 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002107 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002109
2110 /* Also check windows in other tab pages. */
2111 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2112 {
2113 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002114 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002115 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002116 if (wp->w_buffer == buf
2117#ifdef FEAT_AUTOCMD
2118 && !(wp->w_closing || wp->w_buffer->b_closing)
2119#endif
2120 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002121 {
2122 win_close_othertab(wp, FALSE, tp);
2123
2124 /* Start all over, the tab page may be closed and
2125 * autocommands may change the window layout. */
2126 nexttp = first_tabpage;
2127 break;
2128 }
2129 }
2130
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002132
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002133 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002134 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002135 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136}
2137
2138/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002139 * Return TRUE if the current window is the only window that exists (ignoring
2140 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002141 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002142 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002143 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002144last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002145{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002146 return (one_window() && first_tabpage->tp_next == NULL);
2147}
2148
2149/*
2150 * Return TRUE if there is only one window other than "aucmd_win" in the
2151 * current tab page.
2152 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002153 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002154one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002155{
2156#ifdef FEAT_AUTOCMD
2157 win_T *wp;
2158 int seen_one = FALSE;
2159
2160 FOR_ALL_WINDOWS(wp)
2161 {
2162 if (wp != aucmd_win)
2163 {
2164 if (seen_one)
2165 return FALSE;
2166 seen_one = TRUE;
2167 }
2168 }
2169 return TRUE;
2170#else
2171 return firstwin == lastwin;
2172#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002173}
2174
2175/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002176 * Close the possibly last window in a tab page.
2177 * Returns TRUE when the window was closed already.
2178 */
2179 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002180close_last_window_tabpage(
2181 win_T *win,
2182 int free_buf,
2183 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002184{
2185 if (firstwin == lastwin)
2186 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002187#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002188 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002189#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002190
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002191 /*
2192 * Closing the last window in a tab page. First go to another tab
2193 * page and then close the window and the tab page. This avoids that
2194 * curwin and curtab are invalid while we are freeing memory, they may
2195 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002196 * Don't trigger autocommands yet, they may use wrong values, so do
2197 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002198 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002199 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002200 redraw_tabline = TRUE;
2201
2202 /* Safety check: Autocommands may have closed the window when jumping
2203 * to the other tab page. */
2204 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2205 {
2206 int h = tabline_height();
2207
2208 win_close_othertab(win, free_buf, prev_curtab);
2209 if (h != tabline_height())
2210 shell_new_rows();
2211 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002212 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2213 * that now. */
2214#ifdef FEAT_AUTOCMD
Bram Moolenaara8596c42012-06-13 14:28:20 +02002215 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002216 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2217 if (old_curbuf != curbuf)
2218 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002219#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002220 return TRUE;
2221 }
2222 return FALSE;
2223}
2224
2225/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002226 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 * If "free_buf" is TRUE related buffer may be unloaded.
2228 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002229 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002230 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002232 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002233win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234{
2235 win_T *wp;
2236#ifdef FEAT_AUTOCMD
2237 int other_buffer = FALSE;
2238#endif
2239 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 int dir;
2241 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002242 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002244 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
2246 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002247 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002250#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002251 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002252 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002253 if (win == aucmd_win)
2254 {
2255 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002256 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002257 }
2258 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2259 {
2260 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002261 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002262 }
2263#endif
2264
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002265 /* When closing the last window in a tab page first go to another tab page
2266 * and then close the window and the tab page to avoid that curwin and
2267 * curtab are invalid while we are freeing memory. */
2268 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002269 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 /* When closing the help window, try restoring a snapshot after closing
2272 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002273 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 help_window = TRUE;
2275 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002276 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278#ifdef FEAT_AUTOCMD
2279 if (win == curwin)
2280 {
2281 /*
2282 * Guess which window is going to be the new current window.
2283 * This may change because of the autocommands (sigh).
2284 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002285 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002288 * Be careful: If autocommands delete the window or cause this window
2289 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 */
2291 if (wp->w_buffer != curbuf)
2292 {
2293 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002294 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002296 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002297 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002298 win->w_closing = FALSE;
2299 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002300 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002302 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002304 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002305 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002306 win->w_closing = FALSE;
2307 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002308 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309# ifdef FEAT_EVAL
2310 /* autocmds may abort script processing */
2311 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002312 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313# endif
2314 }
2315#endif
2316
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002317#ifdef FEAT_GUI
2318 /* Avoid trouble with scrollbars that are going to be deleted in
2319 * win_free(). */
2320 if (gui.in_use)
2321 out_flush();
2322#endif
2323
Bram Moolenaara971b822011-09-14 14:43:25 +02002324#ifdef FEAT_SYN_HL
2325 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002326 if (win->w_buffer != NULL)
2327 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002328#endif
2329
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 /*
2331 * Close the link to the buffer.
2332 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002333 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002334 {
2335#ifdef FEAT_AUTOCMD
2336 win->w_closing = TRUE;
2337#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002338 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002339#ifdef FEAT_AUTOCMD
2340 if (win_valid(win))
2341 win->w_closing = FALSE;
2342#endif
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002343 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2344 * "wipe". */
2345 if (!buf_valid(curbuf))
2346 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002347 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002348
Bram Moolenaar802418d2013-01-17 14:00:11 +01002349 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2350 && (last_window() || curtab != prev_curtab
2351 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002352 {
2353 /* Autocommands have close all windows, quit now. Restore
2354 * curwin->w_buffer, otherwise writing viminfo may fail. */
2355 if (curwin->w_buffer == NULL)
2356 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002357 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002358 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002361 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002362 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002363 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaara971b822011-09-14 14:43:25 +02002366 /* Free the memory used for the window and get the window that received
2367 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002368 wp = win_free_mem(win, &dir, NULL);
2369
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 /* Make sure curwin isn't invalid. It can cause severe trouble when
2371 * printing an error message. For win_equal() curbuf needs to be valid
2372 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002373 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
2375 curwin = wp;
2376#ifdef FEAT_QUICKFIX
2377 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2378 {
2379 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002380 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 * finding another window to go to.
2382 */
2383 for (;;)
2384 {
2385 if (wp->w_next == NULL)
2386 wp = firstwin;
2387 else
2388 wp = wp->w_next;
2389 if (wp == curwin)
2390 break;
2391 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2392 {
2393 curwin = wp;
2394 break;
2395 }
2396 }
2397 }
2398#endif
2399 curbuf = curwin->w_buffer;
2400 close_curwin = TRUE;
2401 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002402 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
2403 win_equal(curwin, TRUE, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 else
2405 win_comp_pos();
2406 if (close_curwin)
2407 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002408 win_enter_ext(wp, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#ifdef FEAT_AUTOCMD
2410 if (other_buffer)
2411 /* careful: after this wp and win may be invalid! */
2412 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2413#endif
2414 }
2415
2416 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002417 * If last window has a status line now and we don't want one,
2418 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 */
2420 last_status(FALSE);
2421
2422 /* After closing the help window, try restoring the window layout from
2423 * before it was opened. */
2424 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002425 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002427#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2429 if (gui.in_use && !win_hasvertsplit())
2430 gui_init_which_components(NULL);
2431#endif
2432
2433 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002434 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435}
2436
2437/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002438 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002439 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002440 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002441 * Caller must check if buffer is hidden and whether the tabline needs to be
2442 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002443 */
2444 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002445win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002446{
2447 win_T *wp;
2448 int dir;
2449 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002450 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002451
Bram Moolenaar362ce482012-06-06 19:02:45 +02002452#ifdef FEAT_AUTOCMD
2453 if (win->w_closing || win->w_buffer->b_closing)
2454 return; /* window is already being closed */
2455#endif
2456
Bram Moolenaarf740b292006-02-16 22:11:02 +00002457 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002458 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002459
2460 /* Careful: Autocommands may have closed the tab page or made it the
2461 * current tab page. */
2462 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2463 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002464 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002465 return;
2466
2467 /* Autocommands may have closed the window already. */
2468 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2469 ;
2470 if (wp == NULL)
2471 return;
2472
Bram Moolenaarf740b292006-02-16 22:11:02 +00002473 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002474 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002475 {
2476 if (tp == first_tabpage)
2477 first_tabpage = tp->tp_next;
2478 else
2479 {
2480 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2481 ptp = ptp->tp_next)
2482 ;
2483 if (ptp == NULL)
2484 {
2485 EMSG2(_(e_intern2), "win_close_othertab()");
2486 return;
2487 }
2488 ptp->tp_next = tp->tp_next;
2489 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002490 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002491 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002492
2493 /* Free the memory used for the window. */
2494 win_free_mem(win, &dir, tp);
2495
2496 if (free_tp)
2497 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002498}
2499
2500/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002501 * Free the memory used for a window.
2502 * Returns a pointer to the window that got the freed up space.
2503 */
2504 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002505win_free_mem(
2506 win_T *win,
2507 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2508 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002509{
2510 frame_T *frp;
2511 win_T *wp;
2512
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002513 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002514 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002515 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002516 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002517 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002518
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002519 /* When deleting the current window of another tab page select a new
2520 * current window. */
2521 if (tp != NULL && win == tp->tp_curwin)
2522 tp->tp_curwin = wp;
2523
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002524 return wp;
2525}
2526
2527#if defined(EXITFREE) || defined(PROTO)
2528 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002529win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002530{
2531 int dummy;
2532
Bram Moolenaarf740b292006-02-16 22:11:02 +00002533# ifdef FEAT_WINDOWS
2534 while (first_tabpage->tp_next != NULL)
2535 tabpage_close(TRUE);
2536# endif
2537
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002538# ifdef FEAT_AUTOCMD
2539 if (aucmd_win != NULL)
2540 {
2541 (void)win_free_mem(aucmd_win, &dummy, NULL);
2542 aucmd_win = NULL;
2543 }
2544# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002545
2546 while (firstwin != NULL)
2547 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002548
2549 /* No window should be used after this. Set curwin to NULL to crash
2550 * instead of using freed memory. */
2551 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002552}
2553#endif
2554
2555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 * Remove a window and its frame from the tree of frames.
2557 * Returns a pointer to the window that got the freed up space.
2558 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002559 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002560winframe_remove(
2561 win_T *win,
2562 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2563 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
2565 frame_T *frp, *frp2, *frp3;
2566 frame_T *frp_close = win->w_frame;
2567 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
2569 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 * If there is only one window there is nothing to remove.
2571 */
2572 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2573 return NULL;
2574
2575 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 * Remove the window from its frame.
2577 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002578 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 wp = frame2win(frp2);
2580
2581 /* Remove this frame from the list of frames. */
2582 frame_remove(frp_close);
2583
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 if (frp_close->fr_parent->fr_layout == FR_COL)
2585 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002586 /* When 'winfixheight' is set, try to find another frame in the column
2587 * (as close to the closed frame as possible) to distribute the height
2588 * to. */
2589 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2590 {
2591 frp = frp_close->fr_prev;
2592 frp3 = frp_close->fr_next;
2593 while (frp != NULL || frp3 != NULL)
2594 {
2595 if (frp != NULL)
2596 {
2597 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2598 {
2599 frp2 = frp;
2600 wp = frp->fr_win;
2601 break;
2602 }
2603 frp = frp->fr_prev;
2604 }
2605 if (frp3 != NULL)
2606 {
2607 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2608 {
2609 frp2 = frp3;
2610 wp = frp3->fr_win;
2611 break;
2612 }
2613 frp3 = frp3->fr_next;
2614 }
2615 }
2616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2618 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 *dirp = 'v';
2620 }
2621 else
2622 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002623 /* When 'winfixwidth' is set, try to find another frame in the column
2624 * (as close to the closed frame as possible) to distribute the width
2625 * to. */
2626 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2627 {
2628 frp = frp_close->fr_prev;
2629 frp3 = frp_close->fr_next;
2630 while (frp != NULL || frp3 != NULL)
2631 {
2632 if (frp != NULL)
2633 {
2634 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2635 {
2636 frp2 = frp;
2637 wp = frp->fr_win;
2638 break;
2639 }
2640 frp = frp->fr_prev;
2641 }
2642 if (frp3 != NULL)
2643 {
2644 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2645 {
2646 frp2 = frp3;
2647 wp = frp3->fr_win;
2648 break;
2649 }
2650 frp3 = frp3->fr_next;
2651 }
2652 }
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002655 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 *dirp = 'h';
2657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 /* If rows/columns go to a window below/right its positions need to be
2660 * updated. Can only be done after the sizes have been updated. */
2661 if (frp2 == frp_close->fr_next)
2662 {
2663 int row = win->w_winrow;
2664 int col = W_WINCOL(win);
2665
2666 frame_comp_pos(frp2, &row, &col);
2667 }
2668
2669 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2670 {
2671 /* There is no other frame in this list, move its info to the parent
2672 * and remove it. */
2673 frp2->fr_parent->fr_layout = frp2->fr_layout;
2674 frp2->fr_parent->fr_child = frp2->fr_child;
2675 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2676 frp->fr_parent = frp2->fr_parent;
2677 frp2->fr_parent->fr_win = frp2->fr_win;
2678 if (frp2->fr_win != NULL)
2679 frp2->fr_win->w_frame = frp2->fr_parent;
2680 frp = frp2->fr_parent;
2681 vim_free(frp2);
2682
2683 frp2 = frp->fr_parent;
2684 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2685 {
2686 /* The frame above the parent has the same layout, have to merge
2687 * the frames into this list. */
2688 if (frp2->fr_child == frp)
2689 frp2->fr_child = frp->fr_child;
2690 frp->fr_child->fr_prev = frp->fr_prev;
2691 if (frp->fr_prev != NULL)
2692 frp->fr_prev->fr_next = frp->fr_child;
2693 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2694 {
2695 frp3->fr_parent = frp2;
2696 if (frp3->fr_next == NULL)
2697 {
2698 frp3->fr_next = frp->fr_next;
2699 if (frp->fr_next != NULL)
2700 frp->fr_next->fr_prev = frp3;
2701 break;
2702 }
2703 }
2704 vim_free(frp);
2705 }
2706 }
2707
2708 return wp;
2709}
2710
2711/*
2712 * Find out which frame is going to get the freed up space when "win" is
2713 * closed.
2714 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2715 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2716 * This makes opening a window and closing it immediately keep the same window
2717 * layout.
2718 */
2719 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002720win_altframe(
2721 win_T *win,
2722 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 frame_T *frp;
2725 int b;
2726
Bram Moolenaarf740b292006-02-16 22:11:02 +00002727 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002728 /* Last window in this tab page, will go to next tab page. */
2729 return alt_tabpage()->tp_curwin->w_frame;
2730
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 frp = win->w_frame;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002732 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 b = p_spr;
2734 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 b = p_sb;
2736 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2737 return frp->fr_next;
2738 return frp->fr_prev;
2739}
2740
2741/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002742 * Return the tabpage that will be used if the current one is closed.
2743 */
2744 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002745alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002746{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002747 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002748
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002749 /* Use the next tab page if possible. */
2750 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002751 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002752
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002753 /* Find the last but one tab page. */
2754 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2755 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002756 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002757}
2758
2759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 * Find the left-upper window in frame "frp".
2761 */
2762 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002763frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 while (frp->fr_win == NULL)
2766 frp = frp->fr_child;
2767 return frp->fr_win;
2768}
2769
2770/*
2771 * Return TRUE if frame "frp" contains window "wp".
2772 */
2773 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002774frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775{
2776 frame_T *p;
2777
2778 if (frp->fr_layout == FR_LEAF)
2779 return frp->fr_win == wp;
2780
2781 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2782 if (frame_has_win(p, wp))
2783 return TRUE;
2784 return FALSE;
2785}
2786
2787/*
2788 * Set a new height for a frame. Recursively sets the height for contained
2789 * frames and windows. Caller must take care of positions.
2790 */
2791 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002792frame_new_height(
2793 frame_T *topfrp,
2794 int height,
2795 int topfirst, /* resize topmost contained frame first */
2796 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 may cause the height not to be set */
2798{
2799 frame_T *frp;
2800 int extra_lines;
2801 int h;
2802
2803 if (topfrp->fr_win != NULL)
2804 {
2805 /* Simple case: just one window. */
2806 win_new_height(topfrp->fr_win,
2807 height - topfrp->fr_win->w_status_height);
2808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 else if (topfrp->fr_layout == FR_ROW)
2810 {
2811 do
2812 {
2813 /* All frames in this row get the same new height. */
2814 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2815 {
2816 frame_new_height(frp, height, topfirst, wfh);
2817 if (frp->fr_height > height)
2818 {
2819 /* Could not fit the windows, make the whole row higher. */
2820 height = frp->fr_height;
2821 break;
2822 }
2823 }
2824 }
2825 while (frp != NULL);
2826 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002827 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
2829 /* Complicated case: Resize a column of frames. Resize the bottom
2830 * frame first, frames above that when needed. */
2831
2832 frp = topfrp->fr_child;
2833 if (wfh)
2834 /* Advance past frames with one window with 'wfh' set. */
2835 while (frame_fixed_height(frp))
2836 {
2837 frp = frp->fr_next;
2838 if (frp == NULL)
2839 return; /* no frame without 'wfh', give up */
2840 }
2841 if (!topfirst)
2842 {
2843 /* Find the bottom frame of this column */
2844 while (frp->fr_next != NULL)
2845 frp = frp->fr_next;
2846 if (wfh)
2847 /* Advance back for frames with one window with 'wfh' set. */
2848 while (frame_fixed_height(frp))
2849 frp = frp->fr_prev;
2850 }
2851
2852 extra_lines = height - topfrp->fr_height;
2853 if (extra_lines < 0)
2854 {
2855 /* reduce height of contained frames, bottom or top frame first */
2856 while (frp != NULL)
2857 {
2858 h = frame_minheight(frp, NULL);
2859 if (frp->fr_height + extra_lines < h)
2860 {
2861 extra_lines += frp->fr_height - h;
2862 frame_new_height(frp, h, topfirst, wfh);
2863 }
2864 else
2865 {
2866 frame_new_height(frp, frp->fr_height + extra_lines,
2867 topfirst, wfh);
2868 break;
2869 }
2870 if (topfirst)
2871 {
2872 do
2873 frp = frp->fr_next;
2874 while (wfh && frp != NULL && frame_fixed_height(frp));
2875 }
2876 else
2877 {
2878 do
2879 frp = frp->fr_prev;
2880 while (wfh && frp != NULL && frame_fixed_height(frp));
2881 }
2882 /* Increase "height" if we could not reduce enough frames. */
2883 if (frp == NULL)
2884 height -= extra_lines;
2885 }
2886 }
2887 else if (extra_lines > 0)
2888 {
2889 /* increase height of bottom or top frame */
2890 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2891 }
2892 }
2893 topfrp->fr_height = height;
2894}
2895
2896/*
2897 * Return TRUE if height of frame "frp" should not be changed because of
2898 * the 'winfixheight' option.
2899 */
2900 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002901frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 /* frame with one window: fixed height if 'winfixheight' set. */
2904 if (frp->fr_win != NULL)
2905 return frp->fr_win->w_p_wfh;
2906
2907 if (frp->fr_layout == FR_ROW)
2908 {
2909 /* The frame is fixed height if one of the frames in the row is fixed
2910 * height. */
2911 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2912 if (frame_fixed_height(frp))
2913 return TRUE;
2914 return FALSE;
2915 }
2916
2917 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2918 * frames in the row are fixed height. */
2919 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2920 if (!frame_fixed_height(frp))
2921 return FALSE;
2922 return TRUE;
2923}
2924
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002926 * Return TRUE if width of frame "frp" should not be changed because of
2927 * the 'winfixwidth' option.
2928 */
2929 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002930frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002931{
2932 /* frame with one window: fixed width if 'winfixwidth' set. */
2933 if (frp->fr_win != NULL)
2934 return frp->fr_win->w_p_wfw;
2935
2936 if (frp->fr_layout == FR_COL)
2937 {
2938 /* The frame is fixed width if one of the frames in the row is fixed
2939 * width. */
2940 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2941 if (frame_fixed_width(frp))
2942 return TRUE;
2943 return FALSE;
2944 }
2945
2946 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2947 * frames in the row are fixed width. */
2948 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2949 if (!frame_fixed_width(frp))
2950 return FALSE;
2951 return TRUE;
2952}
2953
2954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 * Add a status line to windows at the bottom of "frp".
2956 * Note: Does not check if there is room!
2957 */
2958 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002959frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960{
2961 win_T *wp;
2962
2963 if (frp->fr_layout == FR_LEAF)
2964 {
2965 wp = frp->fr_win;
2966 if (wp->w_status_height == 0)
2967 {
2968 if (wp->w_height > 0) /* don't make it negative */
2969 --wp->w_height;
2970 wp->w_status_height = STATUS_HEIGHT;
2971 }
2972 }
2973 else if (frp->fr_layout == FR_ROW)
2974 {
2975 /* Handle all the frames in the row. */
2976 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2977 frame_add_statusline(frp);
2978 }
2979 else /* frp->fr_layout == FR_COL */
2980 {
2981 /* Only need to handle the last frame in the column. */
2982 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
2983 ;
2984 frame_add_statusline(frp);
2985 }
2986}
2987
2988/*
2989 * Set width of a frame. Handles recursively going through contained frames.
2990 * May remove separator line for windows at the right side (for win_close()).
2991 */
2992 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002993frame_new_width(
2994 frame_T *topfrp,
2995 int width,
2996 int leftfirst, /* resize leftmost contained frame first */
2997 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002998 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 frame_T *frp;
3001 int extra_cols;
3002 int w;
3003 win_T *wp;
3004
3005 if (topfrp->fr_layout == FR_LEAF)
3006 {
3007 /* Simple case: just one window. */
3008 wp = topfrp->fr_win;
3009 /* Find out if there are any windows right of this one. */
3010 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3011 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3012 break;
3013 if (frp->fr_parent == NULL)
3014 wp->w_vsep_width = 0;
3015 win_new_width(wp, width - wp->w_vsep_width);
3016 }
3017 else if (topfrp->fr_layout == FR_COL)
3018 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003019 do
3020 {
3021 /* All frames in this column get the same new width. */
3022 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3023 {
3024 frame_new_width(frp, width, leftfirst, wfw);
3025 if (frp->fr_width > width)
3026 {
3027 /* Could not fit the windows, make whole column wider. */
3028 width = frp->fr_width;
3029 break;
3030 }
3031 }
3032 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
3034 else /* fr_layout == FR_ROW */
3035 {
3036 /* Complicated case: Resize a row of frames. Resize the rightmost
3037 * frame first, frames left of it when needed. */
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003040 if (wfw)
3041 /* Advance past frames with one window with 'wfw' set. */
3042 while (frame_fixed_width(frp))
3043 {
3044 frp = frp->fr_next;
3045 if (frp == NULL)
3046 return; /* no frame without 'wfw', give up */
3047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003049 {
3050 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 while (frp->fr_next != NULL)
3052 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003053 if (wfw)
3054 /* Advance back for frames with one window with 'wfw' set. */
3055 while (frame_fixed_width(frp))
3056 frp = frp->fr_prev;
3057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 extra_cols = width - topfrp->fr_width;
3060 if (extra_cols < 0)
3061 {
3062 /* reduce frame width, rightmost frame first */
3063 while (frp != NULL)
3064 {
3065 w = frame_minwidth(frp, NULL);
3066 if (frp->fr_width + extra_cols < w)
3067 {
3068 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003069 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 }
3071 else
3072 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003073 frame_new_width(frp, frp->fr_width + extra_cols,
3074 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 break;
3076 }
3077 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003078 {
3079 do
3080 frp = frp->fr_next;
3081 while (wfw && frp != NULL && frame_fixed_width(frp));
3082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003084 {
3085 do
3086 frp = frp->fr_prev;
3087 while (wfw && frp != NULL && frame_fixed_width(frp));
3088 }
3089 /* Increase "width" if we could not reduce enough frames. */
3090 if (frp == NULL)
3091 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 }
3093 }
3094 else if (extra_cols > 0)
3095 {
3096 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003097 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
3099 }
3100 topfrp->fr_width = width;
3101}
3102
3103/*
3104 * Add the vertical separator to windows at the right side of "frp".
3105 * Note: Does not check if there is room!
3106 */
3107 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003108frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109{
3110 win_T *wp;
3111
3112 if (frp->fr_layout == FR_LEAF)
3113 {
3114 wp = frp->fr_win;
3115 if (wp->w_vsep_width == 0)
3116 {
3117 if (wp->w_width > 0) /* don't make it negative */
3118 --wp->w_width;
3119 wp->w_vsep_width = 1;
3120 }
3121 }
3122 else if (frp->fr_layout == FR_COL)
3123 {
3124 /* Handle all the frames in the column. */
3125 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3126 frame_add_vsep(frp);
3127 }
3128 else /* frp->fr_layout == FR_ROW */
3129 {
3130 /* Only need to handle the last frame in the row. */
3131 frp = frp->fr_child;
3132 while (frp->fr_next != NULL)
3133 frp = frp->fr_next;
3134 frame_add_vsep(frp);
3135 }
3136}
3137
3138/*
3139 * Set frame width from the window it contains.
3140 */
3141 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003142frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
3144 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3145}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147/*
3148 * Set frame height from the window it contains.
3149 */
3150 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003151frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3154}
3155
3156/*
3157 * Compute the minimal height for frame "topfrp".
3158 * Uses the 'winminheight' option.
3159 * When "next_curwin" isn't NULL, use p_wh for this window.
3160 * When "next_curwin" is NOWIN, don't use at least one line for the current
3161 * window.
3162 */
3163 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003164frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 frame_T *frp;
3167 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 if (topfrp->fr_win != NULL)
3171 {
3172 if (topfrp->fr_win == next_curwin)
3173 m = p_wh + topfrp->fr_win->w_status_height;
3174 else
3175 {
3176 /* window: minimal height of the window plus status line */
3177 m = p_wmh + topfrp->fr_win->w_status_height;
3178 /* Current window is minimal one line high */
3179 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3180 ++m;
3181 }
3182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 else if (topfrp->fr_layout == FR_ROW)
3184 {
3185 /* get the minimal height from each frame in this row */
3186 m = 0;
3187 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3188 {
3189 n = frame_minheight(frp, next_curwin);
3190 if (n > m)
3191 m = n;
3192 }
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
3195 {
3196 /* Add up the minimal heights for all frames in this column. */
3197 m = 0;
3198 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3199 m += frame_minheight(frp, next_curwin);
3200 }
3201
3202 return m;
3203}
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205/*
3206 * Compute the minimal width for frame "topfrp".
3207 * When "next_curwin" isn't NULL, use p_wiw for this window.
3208 * When "next_curwin" is NOWIN, don't use at least one column for the current
3209 * window.
3210 */
3211 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003212frame_minwidth(
3213 frame_T *topfrp,
3214 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 frame_T *frp;
3217 int m, n;
3218
3219 if (topfrp->fr_win != NULL)
3220 {
3221 if (topfrp->fr_win == next_curwin)
3222 m = p_wiw + topfrp->fr_win->w_vsep_width;
3223 else
3224 {
3225 /* window: minimal width of the window plus separator column */
3226 m = p_wmw + topfrp->fr_win->w_vsep_width;
3227 /* Current window is minimal one column wide */
3228 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3229 ++m;
3230 }
3231 }
3232 else if (topfrp->fr_layout == FR_COL)
3233 {
3234 /* get the minimal width from each frame in this column */
3235 m = 0;
3236 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3237 {
3238 n = frame_minwidth(frp, next_curwin);
3239 if (n > m)
3240 m = n;
3241 }
3242 }
3243 else
3244 {
3245 /* Add up the minimal widths for all frames in this row. */
3246 m = 0;
3247 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3248 m += frame_minwidth(frp, next_curwin);
3249 }
3250
3251 return m;
3252}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
3254
3255/*
3256 * Try to close all windows except current one.
3257 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3258 * used and the buffer was modified.
3259 *
3260 * Used by ":bdel" and ":only".
3261 */
3262 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003263close_others(
3264 int message,
3265 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
3267 win_T *wp;
3268 win_T *nextwp;
3269 int r;
3270
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003271 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 {
3273 if (message
3274#ifdef FEAT_AUTOCMD
3275 && !autocmd_busy
3276#endif
3277 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003278 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 return;
3280 }
3281
3282 /* Be very careful here: autocommands may change the window layout. */
3283 for (wp = firstwin; win_valid(wp); wp = nextwp)
3284 {
3285 nextwp = wp->w_next;
3286 if (wp != curwin) /* don't close current window */
3287 {
3288
3289 /* Check if it's allowed to abandon this window */
3290 r = can_abandon(wp->w_buffer, forceit);
3291#ifdef FEAT_AUTOCMD
3292 if (!win_valid(wp)) /* autocommands messed wp up */
3293 {
3294 nextwp = firstwin;
3295 continue;
3296 }
3297#endif
3298 if (!r)
3299 {
3300#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3301 if (message && (p_confirm || cmdmod.confirm) && p_write)
3302 {
3303 dialog_changed(wp->w_buffer, FALSE);
3304# ifdef FEAT_AUTOCMD
3305 if (!win_valid(wp)) /* autocommands messed wp up */
3306 {
3307 nextwp = firstwin;
3308 continue;
3309 }
3310# endif
3311 }
3312 if (bufIsChanged(wp->w_buffer))
3313#endif
3314 continue;
3315 }
3316 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3317 }
3318 }
3319
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003320 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 EMSG(_("E445: Other window contains changes"));
3322}
3323
3324#endif /* FEAT_WINDOWS */
3325
3326/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003327 * Init the current window "curwin".
3328 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 */
3330 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003331curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003333 win_init_empty(curwin);
3334}
3335
3336 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003337win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003338{
3339 redraw_win_later(wp, NOT_VALID);
3340 wp->w_lines_valid = 0;
3341 wp->w_cursor.lnum = 1;
3342 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003344 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003346 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3347 wp->w_pcmark.col = 0;
3348 wp->w_prev_pcmark.lnum = 0;
3349 wp->w_prev_pcmark.col = 0;
3350 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003352 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003354 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003356 if (wp->w_p_rl)
3357 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003359 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003361#ifdef FEAT_SYN_HL
3362 wp->w_s = &wp->w_buffer->b_s;
3363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364}
3365
3366/*
3367 * Allocate the first window and put an empty buffer in it.
3368 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003369 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003371 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003372win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003374 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375 return FAIL;
3376
3377#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003378 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003379 if (first_tabpage == NULL)
3380 return FAIL;
3381 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003382 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003383#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003384
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003385 return OK;
3386}
3387
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003388#if defined(FEAT_AUTOCMD) || defined(PROTO)
3389/*
3390 * Init "aucmd_win". This can only be done after the first
3391 * window is fully initialized, thus it can't be in win_alloc_first().
3392 */
3393 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003394win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003395{
3396 aucmd_win = win_alloc(NULL, TRUE);
3397 if (aucmd_win != NULL)
3398 {
3399 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003400 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003401 new_frame(aucmd_win);
3402 }
3403}
3404#endif
3405
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003406/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003407 * Allocate the first window or the first window in a new tab page.
3408 * When "oldwin" is NULL create an empty buffer for it.
3409 * When "oldwin" is not NULL copy info from it to the new window (only with
3410 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003411 * Return FAIL when something goes wrong (out of memory).
3412 */
3413 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003414win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003415{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003416 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003417 if (oldwin == NULL)
3418 {
3419 /* Very first window, need to create an empty buffer for it and
3420 * initialize from scratch. */
3421 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3422 if (curwin == NULL || curbuf == NULL)
3423 return FAIL;
3424 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003425#ifdef FEAT_SYN_HL
3426 curwin->w_s = &(curbuf->b_s);
3427#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003428 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003430 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003432 curwin_init(); /* init current window */
3433 }
3434#ifdef FEAT_WINDOWS
3435 else
3436 {
3437 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003438 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003439
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003440 /* We don't want cursor- and scroll-binding in the first window. */
3441 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003442 }
3443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003445 new_frame(curwin);
3446 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003447 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003448 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 topframe->fr_height = Rows - p_ch;
3451 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003452
3453 return OK;
3454}
3455
3456/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003457 * Create a frame for window "wp".
3458 */
3459 static void
3460new_frame(win_T *wp)
3461{
3462 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3463
3464 wp->w_frame = frp;
3465 if (frp != NULL)
3466 {
3467 frp->fr_layout = FR_LEAF;
3468 frp->fr_win = wp;
3469 }
3470}
3471
3472/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003473 * Initialize the window and frame size to the maximum.
3474 */
3475 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003476win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003477{
3478 firstwin->w_height = ROWS_AVAIL;
3479 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003480 firstwin->w_width = Columns;
3481 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482}
3483
3484#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003485
3486/*
3487 * Allocate a new tabpage_T and init the values.
3488 * Returns NULL when out of memory.
3489 */
3490 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003491alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003492{
3493 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003494# ifdef FEAT_GUI
3495 int i;
3496# endif
3497
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003498
3499 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003500 if (tp == NULL)
3501 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003502
Bram Moolenaar429fa852013-04-15 12:27:36 +02003503# ifdef FEAT_EVAL
3504 /* init t: variables */
3505 tp->tp_vars = dict_alloc();
3506 if (tp->tp_vars == NULL)
3507 {
3508 vim_free(tp);
3509 return NULL;
3510 }
3511 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3512# endif
3513
3514# ifdef FEAT_GUI
3515 for (i = 0; i < 3; i++)
3516 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003517# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003518# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003519 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003520# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003521 tp->tp_ch_used = p_ch;
3522
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003523 return tp;
3524}
3525
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003526 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003527free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003528{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003529 int idx;
3530
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003531# ifdef FEAT_DIFF
3532 diff_clear(tp);
3533# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003534 for (idx = 0; idx < SNAP_COUNT; ++idx)
3535 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003536#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003537 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3538 hash_init(&tp->tp_vars->dv_hashtab);
3539 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003540#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003541
3542#ifdef FEAT_PYTHON
3543 python_tabpage_free(tp);
3544#endif
3545
3546#ifdef FEAT_PYTHON3
3547 python3_tabpage_free(tp);
3548#endif
3549
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003550 vim_free(tp);
3551}
3552
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003553/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003554 * Create a new Tab page with one window.
3555 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003556 * When "after" is 0 put it just after the current Tab page.
3557 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003558 * Return FAIL or OK.
3559 */
3560 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003561win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003562{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003563 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003564 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003565 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003566
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003567 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003568 if (newtp == NULL)
3569 return FAIL;
3570
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003571 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003572 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003573 {
3574 vim_free(newtp);
3575 return FAIL;
3576 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003577 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003578
3579 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003580 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003581 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003583 if (after == 1)
3584 {
3585 /* New tab page becomes the first one. */
3586 newtp->tp_next = first_tabpage;
3587 first_tabpage = newtp;
3588 }
3589 else
3590 {
3591 if (after > 0)
3592 {
3593 /* Put new tab page before tab page "after". */
3594 n = 2;
3595 for (tp = first_tabpage; tp->tp_next != NULL
3596 && n < after; tp = tp->tp_next)
3597 ++n;
3598 }
3599 newtp->tp_next = tp->tp_next;
3600 tp->tp_next = newtp;
3601 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003602 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003603 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003604 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003605
3606 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003607 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003608
3609#if defined(FEAT_GUI)
3610 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3611 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003612 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003613#endif
3614
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003615 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003616#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003617 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003618 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003619#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003620 return OK;
3621 }
3622
3623 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003624 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003625 return FAIL;
3626}
3627
3628/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003629 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3630 * like with ":split".
3631 * Returns OK if a new tab page was created, FAIL otherwise.
3632 */
3633 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003634may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003635{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003636 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003637
Bram Moolenaard326ce82007-03-11 14:48:29 +00003638 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003639 {
3640 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003641 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003642 return win_new_tabpage(n);
3643 }
3644 return FAIL;
3645}
3646
3647/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003648 * Create up to "maxcount" tabpages with empty windows.
3649 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003650 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003651 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003652make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003653{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003654 int count = maxcount;
3655 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003656
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003657 /* Limit to 'tabpagemax' tabs. */
3658 if (count > p_tpm)
3659 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003660
3661#ifdef FEAT_AUTOCMD
3662 /*
3663 * Don't execute autocommands while creating the tab pages. Must do that
3664 * when putting the buffers in the windows.
3665 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003666 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003667#endif
3668
3669 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003670 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003672
3673#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003674 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003675#endif
3676
3677 /* return actual number of tab pages */
3678 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003679}
3680
3681/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003682 * Return TRUE when "tpc" points to a valid tab page.
3683 */
3684 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003685valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003686{
3687 tabpage_T *tp;
3688
3689 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3690 if (tp == tpc)
3691 return TRUE;
3692 return FALSE;
3693}
3694
3695/*
3696 * Find tab page "n" (first one is 1). Returns NULL when not found.
3697 */
3698 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003699find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003700{
3701 tabpage_T *tp;
3702 int i = 1;
3703
3704 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3705 ++i;
3706 return tp;
3707}
3708
3709/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003710 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003711 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003712 */
3713 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003714tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003715{
3716 int i = 1;
3717 tabpage_T *tp;
3718
3719 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3720 ++i;
3721 return i;
3722}
3723
3724/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003725 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003726 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003727 * FAIL.
3728 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003729 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003730 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003731leave_tabpage(
3732 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003733 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003734 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003735{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003736 tabpage_T *tp = curtab;
3737
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003738 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003739#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003740 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003741 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003742 if (new_curbuf != curbuf)
3743 {
3744 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3745 if (curtab != tp)
3746 return FAIL;
3747 }
3748 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3749 if (curtab != tp)
3750 return FAIL;
3751 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003752 if (curtab != tp)
3753 return FAIL;
3754 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003755#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003756#if defined(FEAT_GUI)
3757 /* Remove the scrollbars. They may be added back later. */
3758 if (gui.in_use)
3759 gui_remove_scrollbars();
3760#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003761 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003762 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763 tp->tp_firstwin = firstwin;
3764 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003765 tp->tp_old_Rows = Rows;
3766 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003767 firstwin = NULL;
3768 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003769 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003770}
3771
3772/*
3773 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003774 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003775 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3776 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003777 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003778 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003779enter_tabpage(
3780 tabpage_T *tp,
3781 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003782 int trigger_enter_autocmds,
3783 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003784{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003785 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003786 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003787
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003788 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003789 firstwin = tp->tp_firstwin;
3790 lastwin = tp->tp_lastwin;
3791 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003792
3793 /* We would like doing the TabEnter event first, but we don't have a
3794 * valid current window yet, which may break some commands.
3795 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003796 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3797 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003798 prevwin = next_prevwin;
3799
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003800 last_status(FALSE); /* status line may appear or disappear */
3801 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003802 must_redraw = CLEAR; /* need to redraw everything */
3803#ifdef FEAT_DIFF
3804 diff_need_scrollbind = TRUE;
3805#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003806
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003807 /* The tabpage line may have appeared or disappeared, may need to resize
3808 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003809 * frame sizes too. Use the stored value of p_ch, so that it can be
3810 * different for each tab page. */
3811 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003812 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3813#ifdef FEAT_GUI_TABLINE
3814 && !gui_use_tabline()
3815#endif
3816 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003817 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003818 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003819 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003820
3821#if defined(FEAT_GUI)
3822 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3823 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003824 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003825#endif
3826
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003827#ifdef FEAT_AUTOCMD
3828 /* Apply autocommands after updating the display, when 'rows' and
3829 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003830 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003831 {
3832 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3833 if (old_curbuf != curbuf)
3834 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3835 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003836#endif
3837
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003838 redraw_all_later(CLEAR);
3839}
3840
3841/*
3842 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003843 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003844 */
3845 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003846goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003847{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003848 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003849 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003850 int i;
3851
Bram Moolenaard68071d2006-05-02 22:08:30 +00003852 if (text_locked())
3853 {
3854 /* Not allowed when editing the command line. */
3855#ifdef FEAT_CMDWIN
3856 if (cmdwin_type != 0)
3857 EMSG(_(e_cmdwin));
3858 else
3859#endif
3860 EMSG(_(e_secure));
3861 return;
3862 }
3863
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003864 /* If there is only one it can't work. */
3865 if (first_tabpage->tp_next == NULL)
3866 {
3867 if (n > 1)
3868 beep_flush();
3869 return;
3870 }
3871
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003872 if (n == 0)
3873 {
3874 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003875 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003876 tp = first_tabpage;
3877 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003878 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003879 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003880 else if (n < 0)
3881 {
3882 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3883 * this N times. */
3884 ttp = curtab;
3885 for (i = n; i < 0; ++i)
3886 {
3887 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3888 tp = tp->tp_next)
3889 ;
3890 ttp = tp;
3891 }
3892 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003893 else if (n == 9999)
3894 {
3895 /* Go to last tab page. */
3896 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3897 ;
3898 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003899 else
3900 {
3901 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003902 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003903 if (tp == NULL)
3904 {
3905 beep_flush();
3906 return;
3907 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003908 }
3909
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003910 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003911
3912#ifdef FEAT_GUI_TABLINE
3913 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003914 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003915#endif
3916}
3917
3918/*
3919 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003920 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3921 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003922 * Note: doesn't update the GUI tab.
3923 */
3924 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003925goto_tabpage_tp(
3926 tabpage_T *tp,
3927 int trigger_enter_autocmds,
3928 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003929{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003930 /* Don't repeat a message in another tab page. */
3931 set_keep_msg(NULL, 0);
3932
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003933 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3934 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003935 {
3936 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003937 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3938 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003939 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003940 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3941 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003942 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003943}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
3945/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003946 * Enter window "wp" in tab page "tp".
3947 * Also updates the GUI tab.
3948 */
3949 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003950goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003951{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003952 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003953 if (curtab == tp && win_valid(wp))
3954 {
3955 win_enter(wp, TRUE);
3956# ifdef FEAT_GUI_TABLINE
3957 if (gui_use_tabline())
3958 gui_mch_set_curtab(tabpage_index(curtab));
3959# endif
3960 }
3961}
3962
3963/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003964 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003965 */
3966 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003967tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003968{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003969 int n = 1;
3970 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003971
3972 if (first_tabpage->tp_next == NULL)
3973 return;
3974
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003975 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
3976 ++n;
3977
3978 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
3979 && tp->tp_next == curtab))
3980 return;
3981
3982 tp_dst = tp;
3983
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003984 /* Remove the current tab page from the list of tab pages. */
3985 if (curtab == first_tabpage)
3986 first_tabpage = curtab->tp_next;
3987 else
3988 {
3989 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3990 if (tp->tp_next == curtab)
3991 break;
3992 if (tp == NULL) /* "cannot happen" */
3993 return;
3994 tp->tp_next = curtab->tp_next;
3995 }
3996
3997 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003998 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003999 {
4000 curtab->tp_next = first_tabpage;
4001 first_tabpage = curtab;
4002 }
4003 else
4004 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004005 curtab->tp_next = tp_dst->tp_next;
4006 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004007 }
4008
4009 /* Need to redraw the tabline. Tab page contents doesn't change. */
4010 redraw_tabline = TRUE;
4011}
4012
4013
4014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 * Go to another window.
4016 * When jumping to another buffer, stop Visual mode. Do this before
4017 * changing windows so we can yank the selection into the '*' register.
4018 * When jumping to another window on the same buffer, adjust its cursor
4019 * position to keep the same Visual area.
4020 */
4021 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004022win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004024#ifdef FEAT_CONCEAL
4025 win_T *owp = curwin;
4026#endif
4027
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004028 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
4030 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004031 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 return;
4033 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004034#ifdef FEAT_AUTOCMD
4035 if (curbuf_locked())
4036 return;
4037#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (wp->w_buffer != curbuf)
4040 reset_VIsual_and_resel();
4041 else if (VIsual_active)
4042 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044#ifdef FEAT_GUI
4045 need_mouse_correct = TRUE;
4046#endif
4047 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004048
4049#ifdef FEAT_CONCEAL
4050 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004051 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004052 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004053 if (curwin->w_p_cole > 0 && !msg_scrolled)
4054 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056}
4057
4058#if defined(FEAT_PERL) || defined(PROTO)
4059/*
4060 * Find window number "winnr" (counting top to bottom).
4061 */
4062 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004063win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064{
4065 win_T *wp;
4066
4067# ifdef FEAT_WINDOWS
4068 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4069 if (--winnr == 0)
4070 break;
4071 return wp;
4072# else
4073 return curwin;
4074# endif
4075}
4076#endif
4077
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004078#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4079 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004080/*
4081 * Find the tabpage for window "win".
4082 */
4083 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004084win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004085{
4086 win_T *wp;
4087 tabpage_T *tp;
4088
4089 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004090 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4091 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004092 if (wp == win)
4093 return tp;
4094 return NULL;
4095}
4096#endif
4097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098/*
4099 * Move to window above or below "count" times.
4100 */
4101 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004102win_goto_ver(
4103 int up, /* TRUE to go to win above */
4104 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
4106 frame_T *fr;
4107 frame_T *nfr;
4108 frame_T *foundfr;
4109
4110 foundfr = curwin->w_frame;
4111 while (count--)
4112 {
4113 /*
4114 * First go upwards in the tree of frames until we find a upwards or
4115 * downwards neighbor.
4116 */
4117 fr = foundfr;
4118 for (;;)
4119 {
4120 if (fr == topframe)
4121 goto end;
4122 if (up)
4123 nfr = fr->fr_prev;
4124 else
4125 nfr = fr->fr_next;
4126 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4127 break;
4128 fr = fr->fr_parent;
4129 }
4130
4131 /*
4132 * Now go downwards to find the bottom or top frame in it.
4133 */
4134 for (;;)
4135 {
4136 if (nfr->fr_layout == FR_LEAF)
4137 {
4138 foundfr = nfr;
4139 break;
4140 }
4141 fr = nfr->fr_child;
4142 if (nfr->fr_layout == FR_ROW)
4143 {
4144 /* Find the frame at the cursor row. */
4145 while (fr->fr_next != NULL
4146 && frame2win(fr)->w_wincol + fr->fr_width
4147 <= curwin->w_wincol + curwin->w_wcol)
4148 fr = fr->fr_next;
4149 }
4150 if (nfr->fr_layout == FR_COL && up)
4151 while (fr->fr_next != NULL)
4152 fr = fr->fr_next;
4153 nfr = fr;
4154 }
4155 }
4156end:
4157 if (foundfr != NULL)
4158 win_goto(foundfr->fr_win);
4159}
4160
4161/*
4162 * Move to left or right window.
4163 */
4164 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004165win_goto_hor(
4166 int left, /* TRUE to go to left win */
4167 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 frame_T *fr;
4170 frame_T *nfr;
4171 frame_T *foundfr;
4172
4173 foundfr = curwin->w_frame;
4174 while (count--)
4175 {
4176 /*
4177 * First go upwards in the tree of frames until we find a left or
4178 * right neighbor.
4179 */
4180 fr = foundfr;
4181 for (;;)
4182 {
4183 if (fr == topframe)
4184 goto end;
4185 if (left)
4186 nfr = fr->fr_prev;
4187 else
4188 nfr = fr->fr_next;
4189 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4190 break;
4191 fr = fr->fr_parent;
4192 }
4193
4194 /*
4195 * Now go downwards to find the leftmost or rightmost frame in it.
4196 */
4197 for (;;)
4198 {
4199 if (nfr->fr_layout == FR_LEAF)
4200 {
4201 foundfr = nfr;
4202 break;
4203 }
4204 fr = nfr->fr_child;
4205 if (nfr->fr_layout == FR_COL)
4206 {
4207 /* Find the frame at the cursor row. */
4208 while (fr->fr_next != NULL
4209 && frame2win(fr)->w_winrow + fr->fr_height
4210 <= curwin->w_winrow + curwin->w_wrow)
4211 fr = fr->fr_next;
4212 }
4213 if (nfr->fr_layout == FR_ROW && left)
4214 while (fr->fr_next != NULL)
4215 fr = fr->fr_next;
4216 nfr = fr;
4217 }
4218 }
4219end:
4220 if (foundfr != NULL)
4221 win_goto(foundfr->fr_win);
4222}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224/*
4225 * Make window "wp" the current window.
4226 */
4227 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004228win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004230 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231}
4232
4233/*
4234 * Make window wp the current window.
4235 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4236 * been closed and isn't valid.
4237 */
4238 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004239win_enter_ext(
4240 win_T *wp,
4241 int undo_sync,
4242 int curwin_invalid,
4243 int trigger_enter_autocmds UNUSED,
4244 int trigger_leave_autocmds UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246#ifdef FEAT_AUTOCMD
4247 int other_buffer = FALSE;
4248#endif
4249
4250 if (wp == curwin && !curwin_invalid) /* nothing to do */
4251 return;
4252
4253#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004254 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 {
4256 /*
4257 * Be careful: If autocommands delete the window, return now.
4258 */
4259 if (wp->w_buffer != curbuf)
4260 {
4261 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4262 other_buffer = TRUE;
4263 if (!win_valid(wp))
4264 return;
4265 }
4266 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4267 if (!win_valid(wp))
4268 return;
4269# ifdef FEAT_EVAL
4270 /* autocmds may abort script processing */
4271 if (aborting())
4272 return;
4273# endif
4274 }
4275#endif
4276
4277 /* sync undo before leaving the current buffer */
4278 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004279 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004280
4281 /* Might need to scroll the old window before switching, e.g., when the
4282 * cursor was moved. */
4283 update_topline();
4284
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 /* may have to copy the buffer options when 'cpo' contains 'S' */
4286 if (wp->w_buffer != curbuf)
4287 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4288 if (!curwin_invalid)
4289 {
4290 prevwin = curwin; /* remember for CTRL-W p */
4291 curwin->w_redr_status = TRUE;
4292 }
4293 curwin = wp;
4294 curbuf = wp->w_buffer;
4295 check_cursor();
4296#ifdef FEAT_VIRTUALEDIT
4297 if (!virtual_active())
4298 curwin->w_cursor.coladd = 0;
4299#endif
4300 changed_line_abv_curs(); /* assume cursor position needs updating */
4301
4302 if (curwin->w_localdir != NULL)
4303 {
4304 /* Window has a local directory: Save current directory as global
4305 * directory (unless that was done already) and change to the local
4306 * directory. */
4307 if (globaldir == NULL)
4308 {
4309 char_u cwd[MAXPATHL];
4310
4311 if (mch_dirname(cwd, MAXPATHL) == OK)
4312 globaldir = vim_strsave(cwd);
4313 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004314 if (mch_chdir((char *)curwin->w_localdir) == 0)
4315 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 }
4317 else if (globaldir != NULL)
4318 {
4319 /* Window doesn't have a local directory and we are not in the global
4320 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004321 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 vim_free(globaldir);
4323 globaldir = NULL;
4324 shorten_fnames(TRUE);
4325 }
4326
4327#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004328 if (trigger_enter_autocmds)
4329 {
4330 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4331 if (other_buffer)
4332 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334#endif
4335
4336#ifdef FEAT_TITLE
4337 maketitle();
4338#endif
4339 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004340 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 if (restart_edit)
4342 redraw_later(VALID); /* causes status line redraw */
4343
4344 /* set window height to desired minimal value */
4345 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4346 win_setheight((int)p_wh);
4347 else if (curwin->w_height == 0)
4348 win_setheight(1);
4349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004351 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353
4354#ifdef FEAT_MOUSE
4355 setmouse(); /* in case jumped to/from help buffer */
4356#endif
4357
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004358 /* Change directories when the 'acd' option is set. */
4359 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360}
4361
4362#endif /* FEAT_WINDOWS */
4363
4364#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4365/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004366 * Jump to the first open window that contains buffer "buf", if one exists.
4367 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 */
4369 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004370buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004372 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004374 if (curwin->w_buffer == buf)
4375 wp = curwin;
4376# ifdef FEAT_WINDOWS
4377 else
4378 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4379 if (wp->w_buffer == buf)
4380 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 if (wp != NULL)
4382 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004384 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004386
4387/*
4388 * Jump to the first open window in any tab page that contains buffer "buf",
4389 * if one exists.
4390 * Returns a pointer to the window found, otherwise NULL.
4391 */
4392 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004393buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004394{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004395 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004396# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004397 tabpage_T *tp;
4398
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004399 if (wp != NULL)
4400 return wp;
4401
4402 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4403 if (tp != curtab)
4404 {
4405 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4406 if (wp->w_buffer == buf)
4407 break;
4408 if (wp != NULL)
4409 {
4410 goto_tabpage_win(tp, wp);
4411 if (curwin != wp)
4412 wp = NULL; /* something went wrong */
4413 break;
4414 }
4415 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004416# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004417 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004418}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#endif
4420
Bram Moolenaar86edef62016-03-13 18:07:30 +01004421static int last_win_id = 0;
4422
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004424 * Allocate a window structure and link it in the window list when "hidden" is
4425 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004428win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004430 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 /*
4433 * allocate window structure and linesizes arrays
4434 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004435 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004436 if (new_wp == NULL)
4437 return NULL;
4438
4439 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004441 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004442 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 }
4444
Bram Moolenaar86edef62016-03-13 18:07:30 +01004445 new_wp->w_id = ++last_win_id;
4446
Bram Moolenaar429fa852013-04-15 12:27:36 +02004447#ifdef FEAT_EVAL
4448 /* init w: variables */
4449 new_wp->w_vars = dict_alloc();
4450 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004452 win_free_lsize(new_wp);
4453 vim_free(new_wp);
4454 return NULL;
4455 }
4456 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004457#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004458
4459#ifdef FEAT_AUTOCMD
4460 /* Don't execute autocommands while the window is not properly
4461 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4462 * event. */
4463 block_autocmds();
4464#endif
4465 /*
4466 * link the window in the window list
4467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004469 if (!hidden)
4470 win_append(after, new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004472 new_wp->w_wincol = 0;
4473 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
Bram Moolenaar429fa852013-04-15 12:27:36 +02004475 /* position the display and the cursor at the top of the file. */
4476 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004478 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004480 new_wp->w_botline = 2;
4481 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004483 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484#endif
4485
Bram Moolenaar429fa852013-04-15 12:27:36 +02004486 /* We won't calculate w_fraction until resizing the window */
4487 new_wp->w_fraction = 0;
4488 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004491 if (gui.in_use)
4492 {
4493 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4494 SBAR_LEFT, new_wp);
4495 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4496 SBAR_RIGHT, new_wp);
4497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#endif
4499#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004500 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004502#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004503 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004504#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004505#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004506 new_wp->w_match_head = NULL;
4507 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004508#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004509 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510}
4511
4512#if defined(FEAT_WINDOWS) || defined(PROTO)
4513
4514/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004515 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 */
4517 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004518win_free(
4519 win_T *wp,
4520 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521{
4522 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004523 buf_T *buf;
4524 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004526#ifdef FEAT_FOLDING
4527 clearFolding(wp);
4528#endif
4529
4530 /* reduce the reference count to the argument list. */
4531 alist_unlink(wp->w_alist);
4532
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004533#ifdef FEAT_AUTOCMD
4534 /* Don't execute autocommands while the window is halfway being deleted.
4535 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004536 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004537#endif
4538
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004539#ifdef FEAT_LUA
4540 lua_window_free(wp);
4541#endif
4542
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004543#ifdef FEAT_MZSCHEME
4544 mzscheme_window_free(wp);
4545#endif
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547#ifdef FEAT_PERL
4548 perl_win_free(wp);
4549#endif
4550
4551#ifdef FEAT_PYTHON
4552 python_window_free(wp);
4553#endif
4554
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004555#ifdef FEAT_PYTHON3
4556 python3_window_free(wp);
4557#endif
4558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559#ifdef FEAT_TCL
4560 tcl_window_free(wp);
4561#endif
4562
4563#ifdef FEAT_RUBY
4564 ruby_window_free(wp);
4565#endif
4566
4567 clear_winopt(&wp->w_onebuf_opt);
4568 clear_winopt(&wp->w_allbuf_opt);
4569
4570#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004571 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4572 hash_init(&wp->w_vars->dv_hashtab);
4573 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574#endif
4575
4576 if (prevwin == wp)
4577 prevwin = NULL;
4578 win_free_lsize(wp);
4579
4580 for (i = 0; i < wp->w_tagstacklen; ++i)
4581 vim_free(wp->w_tagstack[i].tagname);
4582
4583 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004584
Bram Moolenaarff18df02013-07-24 17:51:57 +02004585 /* Remove the window from the b_wininfo lists, it may happen that the
4586 * freed memory is re-used for another window. */
4587 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4588 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4589 if (wip->wi_win == wp)
4590 wip->wi_win = NULL;
4591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004593 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004595
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#ifdef FEAT_JUMPLIST
4597 free_jumplist(wp);
4598#endif
4599
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004600#ifdef FEAT_QUICKFIX
4601 qf_free_all(wp);
4602#endif
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604#ifdef FEAT_GUI
4605 if (gui.in_use)
4606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4608 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4609 }
4610#endif /* FEAT_GUI */
4611
Bram Moolenaar860cae12010-06-05 23:22:07 +02004612#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004613 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004614#endif
4615
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004616#ifdef FEAT_AUTOCMD
4617 if (wp != aucmd_win)
4618#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004619 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004620#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004621 if (autocmd_busy)
4622 {
4623 wp->w_next = au_pending_free_win;
4624 au_pending_free_win = wp;
4625 }
4626 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004627#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004628 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004629
4630#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004631 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633}
4634
4635/*
4636 * Append window "wp" in the window list after window "after".
4637 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004638 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004639win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 win_T *before;
4642
4643 if (after == NULL) /* after NULL is in front of the first */
4644 before = firstwin;
4645 else
4646 before = after->w_next;
4647
4648 wp->w_next = before;
4649 wp->w_prev = after;
4650 if (after == NULL)
4651 firstwin = wp;
4652 else
4653 after->w_next = wp;
4654 if (before == NULL)
4655 lastwin = wp;
4656 else
4657 before->w_prev = wp;
4658}
4659
4660/*
4661 * Remove a window from the window list.
4662 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004663 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004664win_remove(
4665 win_T *wp,
4666 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
4668 if (wp->w_prev != NULL)
4669 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004670 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004672 else
4673 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 if (wp->w_next != NULL)
4675 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004676 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004678 else
4679 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680}
4681
4682/*
4683 * Append frame "frp" in a frame list after frame "after".
4684 */
4685 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004686frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 frp->fr_next = after->fr_next;
4689 after->fr_next = frp;
4690 if (frp->fr_next != NULL)
4691 frp->fr_next->fr_prev = frp;
4692 frp->fr_prev = after;
4693}
4694
4695/*
4696 * Insert frame "frp" in a frame list before frame "before".
4697 */
4698 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004699frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700{
4701 frp->fr_next = before;
4702 frp->fr_prev = before->fr_prev;
4703 before->fr_prev = frp;
4704 if (frp->fr_prev != NULL)
4705 frp->fr_prev->fr_next = frp;
4706 else
4707 frp->fr_parent->fr_child = frp;
4708}
4709
4710/*
4711 * Remove a frame from a frame list.
4712 */
4713 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004714frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 if (frp->fr_prev != NULL)
4717 frp->fr_prev->fr_next = frp->fr_next;
4718 else
4719 frp->fr_parent->fr_child = frp->fr_next;
4720 if (frp->fr_next != NULL)
4721 frp->fr_next->fr_prev = frp->fr_prev;
4722}
4723
4724#endif /* FEAT_WINDOWS */
4725
4726/*
4727 * Allocate w_lines[] for window "wp".
4728 * Return FAIL for failure, OK for success.
4729 */
4730 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004731win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
4733 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004734 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 if (wp->w_lines == NULL)
4736 return FAIL;
4737 return OK;
4738}
4739
4740/*
4741 * free lsize arrays for a window
4742 */
4743 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004744win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004746 /* TODO: why would wp be NULL here? */
4747 if (wp != NULL)
4748 {
4749 vim_free(wp->w_lines);
4750 wp->w_lines = NULL;
4751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752}
4753
4754/*
4755 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004756 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 */
4758 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004759shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004761 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 if (firstwin == NULL) /* not initialized yet */
4764 return;
4765#ifdef FEAT_WINDOWS
4766 if (h < frame_minheight(topframe, NULL))
4767 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004768
4769 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 * that doesn't result in the right height, forget about that option. */
4771 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004772 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 frame_new_height(topframe, h, FALSE, FALSE);
4774
4775 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4776#else
4777 if (h < 1)
4778 h = 1;
4779 win_new_height(firstwin, h);
4780#endif
4781 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004782#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004783 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004784#endif
4785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786#if 0
4787 /* Disabled: don't want making the screen smaller make a window larger. */
4788 if (p_ea)
4789 win_equal(curwin, FALSE, 'v');
4790#endif
4791}
4792
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004793#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794/*
4795 * Called from win_new_shellsize() after Columns changed.
4796 */
4797 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004798shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800 if (firstwin == NULL) /* not initialized yet */
4801 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004802
4803 /* First try setting the widths of windows with 'winfixwidth'. If that
4804 * doesn't result in the right width, forget about that option. */
4805 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004806 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004807 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4810#if 0
4811 /* Disabled: don't want making the screen smaller make a window larger. */
4812 if (p_ea)
4813 win_equal(curwin, FALSE, 'h');
4814#endif
4815}
4816#endif
4817
4818#if defined(FEAT_CMDWIN) || defined(PROTO)
4819/*
4820 * Save the size of all windows in "gap".
4821 */
4822 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004823win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
4825{
4826 win_T *wp;
4827
4828 ga_init2(gap, (int)sizeof(int), 1);
4829 if (ga_grow(gap, win_count() * 2) == OK)
4830 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4831 {
4832 ((int *)gap->ga_data)[gap->ga_len++] =
4833 wp->w_width + wp->w_vsep_width;
4834 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4835 }
4836}
4837
4838/*
4839 * Restore window sizes, but only if the number of windows is still the same.
4840 * Does not free the growarray.
4841 */
4842 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004843win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844{
4845 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004846 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847
4848 if (win_count() * 2 == gap->ga_len)
4849 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004850 /* The order matters, because frames contain other frames, but it's
4851 * difficult to get right. The easy way out is to do it twice. */
4852 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004854 i = 0;
4855 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4856 {
4857 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4858 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 }
4861 /* recompute the window positions */
4862 (void)win_comp_pos();
4863 }
4864}
4865#endif /* FEAT_CMDWIN */
4866
4867#if defined(FEAT_WINDOWS) || defined(PROTO)
4868/*
4869 * Update the position for all windows, using the width and height of the
4870 * frames.
4871 * Returns the row just after the last window.
4872 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004873 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004874win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004876 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 int col = 0;
4878
4879 frame_comp_pos(topframe, &row, &col);
4880 return row;
4881}
4882
4883/*
4884 * Update the position of the windows in frame "topfrp", using the width and
4885 * height of the frames.
4886 * "*row" and "*col" are the top-left position of the frame. They are updated
4887 * to the bottom-right position plus one.
4888 */
4889 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004890frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
4892 win_T *wp;
4893 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 int startcol;
4895 int startrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
4897 wp = topfrp->fr_win;
4898 if (wp != NULL)
4899 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004900 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
4902 /* position changed, redraw */
4903 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 redraw_win_later(wp, NOT_VALID);
4906 wp->w_redr_status = TRUE;
4907 }
4908 *row += wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911 else
4912 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 startrow = *row;
4914 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 if (topfrp->fr_layout == FR_ROW)
4918 *row = startrow; /* all frames are at the same row */
4919 else
4920 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 frame_comp_pos(frp, row, col);
4922 }
4923 }
4924}
4925
4926#endif /* FEAT_WINDOWS */
4927
4928/*
4929 * Set current window height and take care of repositioning other windows to
4930 * fit around it.
4931 */
4932 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004933win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934{
4935 win_setheight_win(height, curwin);
4936}
4937
4938/*
4939 * Set the window height of window "win" and take care of repositioning other
4940 * windows to fit around it.
4941 */
4942 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004943win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
4945 int row;
4946
4947 if (win == curwin)
4948 {
4949 /* Always keep current window at least one line high, even when
4950 * 'winminheight' is zero. */
4951#ifdef FEAT_WINDOWS
4952 if (height < p_wmh)
4953 height = p_wmh;
4954#endif
4955 if (height == 0)
4956 height = 1;
4957 }
4958
4959#ifdef FEAT_WINDOWS
4960 frame_setheight(win->w_frame, height + win->w_status_height);
4961
4962 /* recompute the window positions */
4963 row = win_comp_pos();
4964#else
4965 if (height > topframe->fr_height)
4966 height = topframe->fr_height;
4967 win->w_height = height;
4968 row = height;
4969#endif
4970
4971 /*
4972 * If there is extra space created between the last window and the command
4973 * line, clear it.
4974 */
4975 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4976 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4977 cmdline_row = row;
4978 msg_row = row;
4979 msg_col = 0;
4980
4981 redraw_all_later(NOT_VALID);
4982}
4983
4984#if defined(FEAT_WINDOWS) || defined(PROTO)
4985
4986/*
4987 * Set the height of a frame to "height" and take care that all frames and
4988 * windows inside it are resized. Also resize frames on the left and right if
4989 * the are in the same FR_ROW frame.
4990 *
4991 * Strategy:
4992 * If the frame is part of a FR_COL frame, try fitting the frame in that
4993 * frame. If that doesn't work (the FR_COL frame is too small), recursively
4994 * go to containing frames to resize them and make room.
4995 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
4996 * Check for the minimal height of the FR_ROW frame.
4997 * At the top level we can also use change the command line height.
4998 */
4999 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005000frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001{
5002 int room; /* total number of lines available */
5003 int take; /* number of lines taken from other windows */
5004 int room_cmdline; /* lines available from cmdline */
5005 int run;
5006 frame_T *frp;
5007 int h;
5008 int room_reserved;
5009
5010 /* If the height already is the desired value, nothing to do. */
5011 if (curfrp->fr_height == height)
5012 return;
5013
5014 if (curfrp->fr_parent == NULL)
5015 {
5016 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005017 if (height > ROWS_AVAIL)
5018 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (height > 0)
5020 frame_new_height(curfrp, height, FALSE, FALSE);
5021 }
5022 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5023 {
5024 /* Row of frames: Also need to resize frames left and right of this
5025 * one. First check for the minimal height of these. */
5026 h = frame_minheight(curfrp->fr_parent, NULL);
5027 if (height < h)
5028 height = h;
5029 frame_setheight(curfrp->fr_parent, height);
5030 }
5031 else
5032 {
5033 /*
5034 * Column of frames: try to change only frames in this column.
5035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 /*
5037 * Do this twice:
5038 * 1: compute room available, if it's not enough try resizing the
5039 * containing frame.
5040 * 2: compute the room available and adjust the height to it.
5041 * Try not to reduce the height of a window with 'winfixheight' set.
5042 */
5043 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 room = 0;
5046 room_reserved = 0;
5047 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5048 frp = frp->fr_next)
5049 {
5050 if (frp != curfrp
5051 && frp->fr_win != NULL
5052 && frp->fr_win->w_p_wfh)
5053 room_reserved += frp->fr_height;
5054 room += frp->fr_height;
5055 if (frp != curfrp)
5056 room -= frame_minheight(frp, NULL);
5057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 if (curfrp->fr_width != Columns)
5059 room_cmdline = 0;
5060 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5063 + lastwin->w_height + lastwin->w_status_height);
5064 if (room_cmdline < 0)
5065 room_cmdline = 0;
5066 }
5067
5068 if (height <= room + room_cmdline)
5069 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
5072 if (height > room + room_cmdline)
5073 height = room + room_cmdline;
5074 break;
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 frame_setheight(curfrp->fr_parent, height
5077 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 }
5079
5080 /*
5081 * Compute the number of lines we will take from others frames (can be
5082 * negative!).
5083 */
5084 take = height - curfrp->fr_height;
5085
5086 /* If there is not enough room, also reduce the height of a window
5087 * with 'winfixheight' set. */
5088 if (height > room + room_cmdline - room_reserved)
5089 room_reserved = room + room_cmdline - height;
5090 /* If there is only a 'winfixheight' window and making the
5091 * window smaller, need to make the other window taller. */
5092 if (take < 0 && room - curfrp->fr_height < room_reserved)
5093 room_reserved = 0;
5094
5095 if (take > 0 && room_cmdline > 0)
5096 {
5097 /* use lines from cmdline first */
5098 if (take < room_cmdline)
5099 room_cmdline = take;
5100 take -= room_cmdline;
5101 topframe->fr_height += room_cmdline;
5102 }
5103
5104 /*
5105 * set the current frame to the new height
5106 */
5107 frame_new_height(curfrp, height, FALSE, FALSE);
5108
5109 /*
5110 * First take lines from the frames after the current frame. If
5111 * that is not enough, takes lines from frames above the current
5112 * frame.
5113 */
5114 for (run = 0; run < 2; ++run)
5115 {
5116 if (run == 0)
5117 frp = curfrp->fr_next; /* 1st run: start with next window */
5118 else
5119 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5120 while (frp != NULL && take != 0)
5121 {
5122 h = frame_minheight(frp, NULL);
5123 if (room_reserved > 0
5124 && frp->fr_win != NULL
5125 && frp->fr_win->w_p_wfh)
5126 {
5127 if (room_reserved >= frp->fr_height)
5128 room_reserved -= frp->fr_height;
5129 else
5130 {
5131 if (frp->fr_height - room_reserved > take)
5132 room_reserved = frp->fr_height - take;
5133 take -= frp->fr_height - room_reserved;
5134 frame_new_height(frp, room_reserved, FALSE, FALSE);
5135 room_reserved = 0;
5136 }
5137 }
5138 else
5139 {
5140 if (frp->fr_height - take < h)
5141 {
5142 take -= frp->fr_height - h;
5143 frame_new_height(frp, h, FALSE, FALSE);
5144 }
5145 else
5146 {
5147 frame_new_height(frp, frp->fr_height - take,
5148 FALSE, FALSE);
5149 take = 0;
5150 }
5151 }
5152 if (run == 0)
5153 frp = frp->fr_next;
5154 else
5155 frp = frp->fr_prev;
5156 }
5157 }
5158 }
5159}
5160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161/*
5162 * Set current window width and take care of repositioning other windows to
5163 * fit around it.
5164 */
5165 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005166win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 win_setwidth_win(width, curwin);
5169}
5170
5171 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005172win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
5174 /* Always keep current window at least one column wide, even when
5175 * 'winminwidth' is zero. */
5176 if (wp == curwin)
5177 {
5178 if (width < p_wmw)
5179 width = p_wmw;
5180 if (width == 0)
5181 width = 1;
5182 }
5183
5184 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5185
5186 /* recompute the window positions */
5187 (void)win_comp_pos();
5188
5189 redraw_all_later(NOT_VALID);
5190}
5191
5192/*
5193 * Set the width of a frame to "width" and take care that all frames and
5194 * windows inside it are resized. Also resize frames above and below if the
5195 * are in the same FR_ROW frame.
5196 *
5197 * Strategy is similar to frame_setheight().
5198 */
5199 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005200frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201{
5202 int room; /* total number of lines available */
5203 int take; /* number of lines taken from other windows */
5204 int run;
5205 frame_T *frp;
5206 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005207 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209 /* If the width already is the desired value, nothing to do. */
5210 if (curfrp->fr_width == width)
5211 return;
5212
5213 if (curfrp->fr_parent == NULL)
5214 /* topframe: can't change width */
5215 return;
5216
5217 if (curfrp->fr_parent->fr_layout == FR_COL)
5218 {
5219 /* Column of frames: Also need to resize frames above and below of
5220 * this one. First check for the minimal width of these. */
5221 w = frame_minwidth(curfrp->fr_parent, NULL);
5222 if (width < w)
5223 width = w;
5224 frame_setwidth(curfrp->fr_parent, width);
5225 }
5226 else
5227 {
5228 /*
5229 * Row of frames: try to change only frames in this row.
5230 *
5231 * Do this twice:
5232 * 1: compute room available, if it's not enough try resizing the
5233 * containing frame.
5234 * 2: compute the room available and adjust the width to it.
5235 */
5236 for (run = 1; run <= 2; ++run)
5237 {
5238 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005239 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5241 frp = frp->fr_next)
5242 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005243 if (frp != curfrp
5244 && frp->fr_win != NULL
5245 && frp->fr_win->w_p_wfw)
5246 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 room += frp->fr_width;
5248 if (frp != curfrp)
5249 room -= frame_minwidth(frp, NULL);
5250 }
5251
5252 if (width <= room)
5253 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005254 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
5256 if (width > room)
5257 width = room;
5258 break;
5259 }
5260 frame_setwidth(curfrp->fr_parent, width
5261 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5262 }
5263
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 /*
5265 * Compute the number of lines we will take from others frames (can be
5266 * negative!).
5267 */
5268 take = width - curfrp->fr_width;
5269
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005270 /* If there is not enough room, also reduce the width of a window
5271 * with 'winfixwidth' set. */
5272 if (width > room - room_reserved)
5273 room_reserved = room - width;
5274 /* If there is only a 'winfixwidth' window and making the
5275 * window smaller, need to make the other window narrower. */
5276 if (take < 0 && room - curfrp->fr_width < room_reserved)
5277 room_reserved = 0;
5278
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 /*
5280 * set the current frame to the new width
5281 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005282 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
5284 /*
5285 * First take lines from the frames right of the current frame. If
5286 * that is not enough, takes lines from frames left of the current
5287 * frame.
5288 */
5289 for (run = 0; run < 2; ++run)
5290 {
5291 if (run == 0)
5292 frp = curfrp->fr_next; /* 1st run: start with next window */
5293 else
5294 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5295 while (frp != NULL && take != 0)
5296 {
5297 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005298 if (room_reserved > 0
5299 && frp->fr_win != NULL
5300 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005302 if (room_reserved >= frp->fr_width)
5303 room_reserved -= frp->fr_width;
5304 else
5305 {
5306 if (frp->fr_width - room_reserved > take)
5307 room_reserved = frp->fr_width - take;
5308 take -= frp->fr_width - room_reserved;
5309 frame_new_width(frp, room_reserved, FALSE, FALSE);
5310 room_reserved = 0;
5311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313 else
5314 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005315 if (frp->fr_width - take < w)
5316 {
5317 take -= frp->fr_width - w;
5318 frame_new_width(frp, w, FALSE, FALSE);
5319 }
5320 else
5321 {
5322 frame_new_width(frp, frp->fr_width - take,
5323 FALSE, FALSE);
5324 take = 0;
5325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
5327 if (run == 0)
5328 frp = frp->fr_next;
5329 else
5330 frp = frp->fr_prev;
5331 }
5332 }
5333 }
5334}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
5336/*
5337 * Check 'winminheight' for a valid value.
5338 */
5339 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005340win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 int room;
5343 int first = TRUE;
5344 win_T *wp;
5345
5346 /* loop until there is a 'winminheight' that is possible */
5347 while (p_wmh > 0)
5348 {
5349 /* TODO: handle vertical splits */
5350 room = -p_wh;
5351 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5352 room += wp->w_height - p_wmh;
5353 if (room >= 0)
5354 break;
5355 --p_wmh;
5356 if (first)
5357 {
5358 EMSG(_(e_noroom));
5359 first = FALSE;
5360 }
5361 }
5362}
5363
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005364#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366/*
5367 * Status line of dragwin is dragged "offset" lines down (negative is up).
5368 */
5369 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005370win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371{
5372 frame_T *curfr;
5373 frame_T *fr;
5374 int room;
5375 int row;
5376 int up; /* if TRUE, drag status line up, otherwise down */
5377 int n;
5378
5379 fr = dragwin->w_frame;
5380 curfr = fr;
5381 if (fr != topframe) /* more than one window */
5382 {
5383 fr = fr->fr_parent;
5384 /* When the parent frame is not a column of frames, its parent should
5385 * be. */
5386 if (fr->fr_layout != FR_COL)
5387 {
5388 curfr = fr;
5389 if (fr != topframe) /* only a row of windows, may drag statusline */
5390 fr = fr->fr_parent;
5391 }
5392 }
5393
5394 /* If this is the last frame in a column, may want to resize the parent
5395 * frame instead (go two up to skip a row of frames). */
5396 while (curfr != topframe && curfr->fr_next == NULL)
5397 {
5398 if (fr != topframe)
5399 fr = fr->fr_parent;
5400 curfr = fr;
5401 if (fr != topframe)
5402 fr = fr->fr_parent;
5403 }
5404
5405 if (offset < 0) /* drag up */
5406 {
5407 up = TRUE;
5408 offset = -offset;
5409 /* sum up the room of the current frame and above it */
5410 if (fr == curfr)
5411 {
5412 /* only one window */
5413 room = fr->fr_height - frame_minheight(fr, NULL);
5414 }
5415 else
5416 {
5417 room = 0;
5418 for (fr = fr->fr_child; ; fr = fr->fr_next)
5419 {
5420 room += fr->fr_height - frame_minheight(fr, NULL);
5421 if (fr == curfr)
5422 break;
5423 }
5424 }
5425 fr = curfr->fr_next; /* put fr at frame that grows */
5426 }
5427 else /* drag down */
5428 {
5429 up = FALSE;
5430 /*
5431 * Only dragging the last status line can reduce p_ch.
5432 */
5433 room = Rows - cmdline_row;
5434 if (curfr->fr_next == NULL)
5435 room -= 1;
5436 else
5437 room -= p_ch;
5438 if (room < 0)
5439 room = 0;
5440 /* sum up the room of frames below of the current one */
5441 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5442 room += fr->fr_height - frame_minheight(fr, NULL);
5443 fr = curfr; /* put fr at window that grows */
5444 }
5445
5446 if (room < offset) /* Not enough room */
5447 offset = room; /* Move as far as we can */
5448 if (offset <= 0)
5449 return;
5450
5451 /*
5452 * Grow frame fr by "offset" lines.
5453 * Doesn't happen when dragging the last status line up.
5454 */
5455 if (fr != NULL)
5456 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5457
5458 if (up)
5459 fr = curfr; /* current frame gets smaller */
5460 else
5461 fr = curfr->fr_next; /* next frame gets smaller */
5462
5463 /*
5464 * Now make the other frames smaller.
5465 */
5466 while (fr != NULL && offset > 0)
5467 {
5468 n = frame_minheight(fr, NULL);
5469 if (fr->fr_height - offset <= n)
5470 {
5471 offset -= fr->fr_height - n;
5472 frame_new_height(fr, n, !up, FALSE);
5473 }
5474 else
5475 {
5476 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5477 break;
5478 }
5479 if (up)
5480 fr = fr->fr_prev;
5481 else
5482 fr = fr->fr_next;
5483 }
5484 row = win_comp_pos();
5485 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5486 cmdline_row = row;
5487 p_ch = Rows - cmdline_row;
5488 if (p_ch < 1)
5489 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005490 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005491 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 showmode();
5493}
5494
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495/*
5496 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5497 */
5498 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005499win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501 frame_T *curfr;
5502 frame_T *fr;
5503 int room;
5504 int left; /* if TRUE, drag separator line left, otherwise right */
5505 int n;
5506
5507 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005508 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 return;
5510 curfr = fr;
5511 fr = fr->fr_parent;
5512 /* When the parent frame is not a row of frames, its parent should be. */
5513 if (fr->fr_layout != FR_ROW)
5514 {
5515 if (fr == topframe) /* only a column of windows (cannot happen?) */
5516 return;
5517 curfr = fr;
5518 fr = fr->fr_parent;
5519 }
5520
5521 /* If this is the last frame in a row, may want to resize a parent
5522 * frame instead. */
5523 while (curfr->fr_next == NULL)
5524 {
5525 if (fr == topframe)
5526 break;
5527 curfr = fr;
5528 fr = fr->fr_parent;
5529 if (fr != topframe)
5530 {
5531 curfr = fr;
5532 fr = fr->fr_parent;
5533 }
5534 }
5535
5536 if (offset < 0) /* drag left */
5537 {
5538 left = TRUE;
5539 offset = -offset;
5540 /* sum up the room of the current frame and left of it */
5541 room = 0;
5542 for (fr = fr->fr_child; ; fr = fr->fr_next)
5543 {
5544 room += fr->fr_width - frame_minwidth(fr, NULL);
5545 if (fr == curfr)
5546 break;
5547 }
5548 fr = curfr->fr_next; /* put fr at frame that grows */
5549 }
5550 else /* drag right */
5551 {
5552 left = FALSE;
5553 /* sum up the room of frames right of the current one */
5554 room = 0;
5555 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5556 room += fr->fr_width - frame_minwidth(fr, NULL);
5557 fr = curfr; /* put fr at window that grows */
5558 }
5559
5560 if (room < offset) /* Not enough room */
5561 offset = room; /* Move as far as we can */
5562 if (offset <= 0) /* No room at all, quit. */
5563 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005564 if (fr == NULL)
5565 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005568 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
5570 /* shrink other frames: current and at the left or at the right */
5571 if (left)
5572 fr = curfr; /* current frame gets smaller */
5573 else
5574 fr = curfr->fr_next; /* next frame gets smaller */
5575
5576 while (fr != NULL && offset > 0)
5577 {
5578 n = frame_minwidth(fr, NULL);
5579 if (fr->fr_width - offset <= n)
5580 {
5581 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005582 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 }
5584 else
5585 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005586 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 break;
5588 }
5589 if (left)
5590 fr = fr->fr_prev;
5591 else
5592 fr = fr->fr_next;
5593 }
5594 (void)win_comp_pos();
5595 redraw_all_later(NOT_VALID);
5596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597#endif /* FEAT_MOUSE */
5598
5599#endif /* FEAT_WINDOWS */
5600
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005601#define FRACTION_MULT 16384L
5602
5603/*
5604 * Set wp->w_fraction for the current w_wrow and w_height.
5605 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005606 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005607set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005608{
5609 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005610 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005611}
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613/*
5614 * Set the height of a window.
5615 * This takes care of the things inside the window, not what happens to the
5616 * window position, the frame or to other windows.
5617 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005618 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005619win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
5621 linenr_T lnum;
5622 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005623 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
5625 /* Don't want a negative height. Happens when splitting a tiny window.
5626 * Will equalize heights soon to fix it. */
5627 if (height < 0)
5628 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005629 if (wp->w_height == height)
5630 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005632 if (wp->w_height > 0)
5633 {
5634 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005635 /* w_wrow needs to be valid. When setting 'laststatus' this may
5636 * call win_new_height() recursively. */
5637 validate_cursor();
5638 if (wp->w_height != prev_height)
5639 return; /* Recursive call already changed the size, bail out here
5640 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005641 if (wp->w_wrow != wp->w_prev_fraction_row)
5642 set_fraction(wp);
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
5645 wp->w_height = height;
5646 wp->w_skipcol = 0;
5647
5648 /* Don't change w_topline when height is zero. Don't set w_topline when
5649 * 'scrollbind' is set and this isn't the current window. */
5650 if (height > 0
5651#ifdef FEAT_SCROLLBIND
5652 && (!wp->w_p_scb || wp == curwin)
5653#endif
5654 )
5655 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005656 /*
5657 * Find a value for w_topline that shows the cursor at the same
5658 * relative position in the window as before (more or less).
5659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 lnum = wp->w_cursor.lnum;
5661 if (lnum < 1) /* can happen when starting up */
5662 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005663 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5664 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5666 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005667
5668 if (sline >= 0)
5669 {
5670 /* Make sure the whole cursor line is visible, if possible. */
5671 int rows = plines_win(wp, lnum, FALSE);
5672
5673 if (sline > wp->w_height - rows)
5674 {
5675 sline = wp->w_height - rows;
5676 wp->w_wrow -= rows - line_size;
5677 }
5678 }
5679
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 if (sline < 0)
5681 {
5682 /*
5683 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005684 * Make cursor line the first line in the window. If not enough
5685 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 */
5687 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005688 if (wp->w_wrow >= wp->w_height
5689 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5690 {
5691 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5692 --wp->w_wrow;
5693 while (wp->w_wrow >= wp->w_height)
5694 {
5695 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5696 + win_col_off2(wp);
5697 --wp->w_wrow;
5698 }
5699 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005700 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005702 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005704 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
5706#ifdef FEAT_FOLDING
5707 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5708 if (lnum == 1)
5709 {
5710 /* first line in buffer is folded */
5711 line_size = 1;
5712 --sline;
5713 break;
5714 }
5715#endif
5716 --lnum;
5717#ifdef FEAT_DIFF
5718 if (lnum == wp->w_topline)
5719 line_size = plines_win_nofill(wp, lnum, TRUE)
5720 + wp->w_topfill;
5721 else
5722#endif
5723 line_size = plines_win(wp, lnum, TRUE);
5724 sline -= line_size;
5725 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 if (sline < 0)
5728 {
5729 /*
5730 * Line we want at top would go off top of screen. Use next
5731 * line instead.
5732 */
5733#ifdef FEAT_FOLDING
5734 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5735#endif
5736 lnum++;
5737 wp->w_wrow -= line_size + sline;
5738 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005739 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 {
5741 /* First line of file reached, use that as topline. */
5742 lnum = 1;
5743 wp->w_wrow -= sline;
5744 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005745
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005746 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749
5750 if (wp == curwin)
5751 {
5752 if (p_so)
5753 update_topline();
5754 curs_columns(FALSE); /* validate w_wrow */
5755 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005756 if (prev_height > 0)
5757 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
5759 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005760 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761#ifdef FEAT_WINDOWS
5762 wp->w_redr_status = TRUE;
5763#endif
5764 invalidate_botline_win(wp);
5765}
5766
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005767#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768/*
5769 * Set the width of a window.
5770 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005771 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005772win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
5774 wp->w_width = width;
5775 wp->w_lines_valid = 0;
5776 changed_line_abv_curs_win(wp);
5777 invalidate_botline_win(wp);
5778 if (wp == curwin)
5779 {
5780 update_topline();
5781 curs_columns(TRUE); /* validate w_wrow */
5782 }
5783 redraw_win_later(wp, NOT_VALID);
5784 wp->w_redr_status = TRUE;
5785}
5786#endif
5787
5788 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005789win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5792 if (wp->w_p_scr == 0)
5793 wp->w_p_scr = 1;
5794}
5795
5796/*
5797 * command_height: called whenever p_ch has been changed
5798 */
5799 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005800command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801{
5802#ifdef FEAT_WINDOWS
5803 int h;
5804 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005805 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005807 /* Use the value of p_ch that we remembered. This is needed for when the
5808 * GUI starts up, we can't be sure in what order things happen. And when
5809 * p_ch was changed in another tab page. */
5810 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 /* Find bottom frame with width of screen. */
5813 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5815 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
5817 /* Avoid changing the height of a window with 'winfixheight' set. */
5818 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5819 && frp->fr_win->w_p_wfh)
5820 frp = frp->fr_prev;
5821
5822 if (starting != NO_SCREEN)
5823 {
5824 cmdline_row = Rows - p_ch;
5825
5826 if (p_ch > old_p_ch) /* p_ch got bigger */
5827 {
5828 while (p_ch > old_p_ch)
5829 {
5830 if (frp == NULL)
5831 {
5832 EMSG(_(e_noroom));
5833 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005834 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 cmdline_row = Rows - p_ch;
5836 break;
5837 }
5838 h = frp->fr_height - frame_minheight(frp, NULL);
5839 if (h > p_ch - old_p_ch)
5840 h = p_ch - old_p_ch;
5841 old_p_ch += h;
5842 frame_add_height(frp, -h);
5843 frp = frp->fr_prev;
5844 }
5845
5846 /* Recompute window positions. */
5847 (void)win_comp_pos();
5848
5849 /* clear the lines added to cmdline */
5850 if (full_screen)
5851 screen_fill((int)(cmdline_row), (int)Rows, 0,
5852 (int)Columns, ' ', ' ', 0);
5853 msg_row = cmdline_row;
5854 redraw_cmdline = TRUE;
5855 return;
5856 }
5857
5858 if (msg_row < cmdline_row)
5859 msg_row = cmdline_row;
5860 redraw_cmdline = TRUE;
5861 }
5862 frame_add_height(frp, (int)(old_p_ch - p_ch));
5863
5864 /* Recompute window positions. */
5865 if (frp != lastwin->w_frame)
5866 (void)win_comp_pos();
5867#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005869 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870#endif
5871}
5872
5873#if defined(FEAT_WINDOWS) || defined(PROTO)
5874/*
5875 * Resize frame "frp" to be "n" lines higher (negative for less high).
5876 * Also resize the frames it is contained in.
5877 */
5878 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005879frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5882 for (;;)
5883 {
5884 frp = frp->fr_parent;
5885 if (frp == NULL)
5886 break;
5887 frp->fr_height += n;
5888 }
5889}
5890
5891/*
5892 * Add or remove a status line for the bottom window(s), according to the
5893 * value of 'laststatus'.
5894 */
5895 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005896last_status(
5897 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 /* Don't make a difference between horizontal or vertical split. */
5900 last_status_rec(topframe, (p_ls == 2
5901 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5902}
5903
5904 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005905last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 frame_T *fp;
5908 win_T *wp;
5909
5910 if (fr->fr_layout == FR_LEAF)
5911 {
5912 wp = fr->fr_win;
5913 if (wp->w_status_height != 0 && !statusline)
5914 {
5915 /* remove status line */
5916 win_new_height(wp, wp->w_height + 1);
5917 wp->w_status_height = 0;
5918 comp_col();
5919 }
5920 else if (wp->w_status_height == 0 && statusline)
5921 {
5922 /* Find a frame to take a line from. */
5923 fp = fr;
5924 while (fp->fr_height <= frame_minheight(fp, NULL))
5925 {
5926 if (fp == topframe)
5927 {
5928 EMSG(_(e_noroom));
5929 return;
5930 }
5931 /* In a column of frames: go to frame above. If already at
5932 * the top or in a row of frames: go to parent. */
5933 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5934 fp = fp->fr_prev;
5935 else
5936 fp = fp->fr_parent;
5937 }
5938 wp->w_status_height = 1;
5939 if (fp != fr)
5940 {
5941 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5942 frame_fix_height(wp);
5943 (void)win_comp_pos();
5944 }
5945 else
5946 win_new_height(wp, wp->w_height - 1);
5947 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005948 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 }
5950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 else if (fr->fr_layout == FR_ROW)
5952 {
5953 /* vertically split windows, set status line for each one */
5954 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5955 last_status_rec(fp, statusline);
5956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 else
5958 {
5959 /* horizontally split window, set status line for last one */
5960 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5961 ;
5962 last_status_rec(fp, statusline);
5963 }
5964}
5965
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005966/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005967 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005968 */
5969 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005970tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005971{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005972#ifdef FEAT_GUI_TABLINE
5973 /* When the GUI has the tabline then this always returns zero. */
5974 if (gui_use_tabline())
5975 return 0;
5976#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005977 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005978 {
5979 case 0: return 0;
5980 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5981 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005982 return 1;
5983}
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985#endif /* FEAT_WINDOWS */
5986
5987#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5988/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005989 * Get the file name at the cursor.
5990 * If Visual mode is active, use the selected text if it's in one line.
5991 * Returns the name in allocated memory, NULL for failure.
5992 */
5993 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005994grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005995{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01005996 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
5997
Bram Moolenaard857f0e2005-06-21 22:37:39 +00005998 if (VIsual_active)
5999 {
6000 int len;
6001 char_u *ptr;
6002
6003 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6004 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006005 return find_file_name_in_path(ptr, len, options,
6006 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006007 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006008 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006009
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006010}
6011
6012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 * Return the file name under or after the cursor.
6014 *
6015 * The 'path' option is searched if the file name is not absolute.
6016 * The string returned has been alloc'ed and should be freed by the caller.
6017 * NULL is returned if the file name or file is not found.
6018 *
6019 * options:
6020 * FNAME_MESS give error messages
6021 * FNAME_EXP expand to path
6022 * FNAME_HYP check for hypertext link
6023 * FNAME_INCL apply "includeexpr"
6024 */
6025 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006026file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027{
6028 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006029 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6030 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031}
6032
6033/*
6034 * Return the name of the file under or after ptr[col].
6035 * Otherwise like file_name_at_cursor().
6036 */
6037 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006038file_name_in_line(
6039 char_u *line,
6040 int col,
6041 int options,
6042 long count,
6043 char_u *rel_fname, /* file we are searching relative to */
6044 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 char_u *ptr;
6047 int len;
6048
6049 /*
6050 * search forward for what could be the start of a file name
6051 */
6052 ptr = line + col;
6053 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006054 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 if (*ptr == NUL) /* nothing found */
6056 {
6057 if (options & FNAME_MESS)
6058 EMSG(_("E446: No file name under cursor"));
6059 return NULL;
6060 }
6061
6062 /*
6063 * Search backward for first char of the file name.
6064 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6065 */
6066 while (ptr > line)
6067 {
6068#ifdef FEAT_MBYTE
6069 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6070 ptr -= len + 1;
6071 else
6072#endif
6073 if (vim_isfilec(ptr[-1])
6074 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6075 --ptr;
6076 else
6077 break;
6078 }
6079
6080 /*
6081 * Search forward for the last char of the file name.
6082 * Also allow "://" when ':' is not in 'isfname'.
6083 */
6084 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006085 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006087 {
6088 if (ptr[len] == '\\')
6089 /* Skip over the "\" in "\ ". */
6090 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091#ifdef FEAT_MBYTE
6092 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006093 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 else
6095#endif
6096 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098
6099 /*
6100 * If there is trailing punctuation, remove it.
6101 * But don't remove "..", could be a directory name.
6102 */
6103 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6104 && ptr[len - 2] != '.')
6105 --len;
6106
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006107 if (file_lnum != NULL)
6108 {
6109 char_u *p;
6110
6111 /* Get the number after the file name and a separator character */
6112 p = ptr + len;
6113 p = skipwhite(p);
6114 if (*p != NUL)
6115 {
6116 if (!isdigit(*p))
6117 ++p; /* skip the separator */
6118 p = skipwhite(p);
6119 if (isdigit(*p))
6120 *file_lnum = (int)getdigits(&p);
6121 }
6122 }
6123
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6125}
6126
6127# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006128static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129
6130 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006131eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133 char_u *res;
6134
6135 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006136 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006137 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 set_vim_var_string(VV_FNAME, NULL, 0);
6139 return res;
6140}
6141#endif
6142
6143/*
6144 * Return the name of the file ptr[len] in 'path'.
6145 * Otherwise like file_name_at_cursor().
6146 */
6147 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006148find_file_name_in_path(
6149 char_u *ptr,
6150 int len,
6151 int options,
6152 long count,
6153 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 char_u *file_name;
6156 int c;
6157# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6158 char_u *tofree = NULL;
6159
6160 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6161 {
6162 tofree = eval_includeexpr(ptr, len);
6163 if (tofree != NULL)
6164 {
6165 ptr = tofree;
6166 len = (int)STRLEN(ptr);
6167 }
6168 }
6169# endif
6170
6171 if (options & FNAME_EXP)
6172 {
6173 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6174 TRUE, rel_fname);
6175
6176# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6177 /*
6178 * If the file could not be found in a normal way, try applying
6179 * 'includeexpr' (unless done already).
6180 */
6181 if (file_name == NULL
6182 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6183 {
6184 tofree = eval_includeexpr(ptr, len);
6185 if (tofree != NULL)
6186 {
6187 ptr = tofree;
6188 len = (int)STRLEN(ptr);
6189 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6190 TRUE, rel_fname);
6191 }
6192 }
6193# endif
6194 if (file_name == NULL && (options & FNAME_MESS))
6195 {
6196 c = ptr[len];
6197 ptr[len] = NUL;
6198 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6199 ptr[len] = c;
6200 }
6201
6202 /* Repeat finding the file "count" times. This matters when it
6203 * appears several times in the path. */
6204 while (file_name != NULL && --count > 0)
6205 {
6206 vim_free(file_name);
6207 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6208 }
6209 }
6210 else
6211 file_name = vim_strnsave(ptr, len);
6212
6213# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6214 vim_free(tofree);
6215# endif
6216
6217 return file_name;
6218}
6219#endif /* FEAT_SEARCHPATH */
6220
6221/*
6222 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6223 * Also check for ":\\", which MS Internet Explorer accepts, return
6224 * URL_BACKSLASH.
6225 */
6226 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006227path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 if (STRNCMP(p, "://", (size_t)3) == 0)
6230 return URL_SLASH;
6231 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6232 return URL_BACKSLASH;
6233 return 0;
6234}
6235
6236/*
6237 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6238 * Return URL_BACKSLASH for "name:\\".
6239 * Return zero otherwise.
6240 */
6241 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006242path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 char_u *p;
6245
6246 for (p = fname; isalpha(*p); ++p)
6247 ;
6248 return path_is_url(p);
6249}
6250
6251/*
6252 * Return TRUE if "name" is a full (absolute) path name or URL.
6253 */
6254 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006255vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 return (path_with_url(name) != 0 || mch_isFullName(name));
6258}
6259
6260/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006261 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 *
6263 * return FAIL for failure, OK otherwise
6264 */
6265 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006266vim_FullName(
6267 char_u *fname,
6268 char_u *buf,
6269 int len,
6270 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 int retval = OK;
6273 int url;
6274
6275 *buf = NUL;
6276 if (fname == NULL)
6277 return FAIL;
6278
6279 url = path_with_url(fname);
6280 if (!url)
6281 retval = mch_FullName(fname, buf, len, force);
6282 if (url || retval == FAIL)
6283 {
6284 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006285 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01006287#if defined(MACOS_CLASSIC) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 slash_adjust(buf);
6289#endif
6290 return retval;
6291}
6292
6293/*
6294 * Return the minimal number of rows that is needed on the screen to display
6295 * the current number of windows.
6296 */
6297 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006298min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006301#ifdef FEAT_WINDOWS
6302 tabpage_T *tp;
6303 int n;
6304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
6306 if (firstwin == NULL) /* not initialized yet */
6307 return MIN_LINES;
6308
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006310 total = 0;
6311 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6312 {
6313 n = frame_minheight(tp->tp_topframe, NULL);
6314 if (total < n)
6315 total = n;
6316 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006317 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006319 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006321 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 return total;
6323}
6324
6325/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006326 * Return TRUE if there is only one window (in the current tab page), not
6327 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006328 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 */
6330 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006331only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332{
6333#ifdef FEAT_WINDOWS
6334 int count = 0;
6335 win_T *wp;
6336
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006337 /* If there is another tab page there always is another window. */
6338 if (first_tabpage->tp_next != NULL)
6339 return FALSE;
6340
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006342 if (wp->w_buffer != NULL
6343 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344# ifdef FEAT_QUICKFIX
6345 || wp->w_p_pvw
6346# endif
6347 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006348# ifdef FEAT_AUTOCMD
6349 && wp != aucmd_win
6350# endif
6351 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 ++count;
6353 return (count <= 1);
6354#else
6355 return TRUE;
6356#endif
6357}
6358
6359#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6360/*
6361 * Correct the cursor line number in other windows. Used after changing the
6362 * current buffer, and before applying autocommands.
6363 * When "do_curwin" is TRUE, also check current window.
6364 */
6365 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006366check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 win_T *wp;
6369
6370#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006371 tabpage_T *tp;
6372
6373 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6375#else
6376 wp = curwin;
6377 if (do_curwin)
6378#endif
6379 {
6380 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6381 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6382 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6383 wp->w_topline = curbuf->b_ml.ml_line_count;
6384 }
6385}
6386#endif
6387
6388#if defined(FEAT_WINDOWS) || defined(PROTO)
6389
6390/*
6391 * A snapshot of the window sizes, to restore them after closing the help
6392 * window.
6393 * Only these fields are used:
6394 * fr_layout
6395 * fr_width
6396 * fr_height
6397 * fr_next
6398 * fr_child
6399 * fr_win (only valid for the old curwin, NULL otherwise)
6400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401
6402/*
6403 * Create a snapshot of the current frame sizes.
6404 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006405 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006406make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006408 clear_snapshot(curtab, idx);
6409 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410}
6411
6412 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006413make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414{
6415 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6416 if (*frp == NULL)
6417 return;
6418 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 (*frp)->fr_height = fr->fr_height;
6421 if (fr->fr_next != NULL)
6422 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6423 if (fr->fr_child != NULL)
6424 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6425 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6426 (*frp)->fr_win = curwin;
6427}
6428
6429/*
6430 * Remove any existing snapshot.
6431 */
6432 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006433clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006435 clear_snapshot_rec(tp->tp_snapshot[idx]);
6436 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437}
6438
6439 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006440clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 if (fr != NULL)
6443 {
6444 clear_snapshot_rec(fr->fr_next);
6445 clear_snapshot_rec(fr->fr_child);
6446 vim_free(fr);
6447 }
6448}
6449
6450/*
6451 * Restore a previously created snapshot, if there is any.
6452 * This is only done if the screen size didn't change and the window layout is
6453 * still the same.
6454 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006455 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006456restore_snapshot(
6457 int idx,
6458 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 win_T *wp;
6461
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006462 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006463 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006464 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6465 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006467 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 win_comp_pos();
6469 if (wp != NULL && close_curwin)
6470 win_goto(wp);
6471 redraw_all_later(CLEAR);
6472 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006473 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474}
6475
6476/*
6477 * Check if frames "sn" and "fr" have the same layout, same following frames
6478 * and same children.
6479 */
6480 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006481check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
6483 if (sn->fr_layout != fr->fr_layout
6484 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6485 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6486 || (sn->fr_next != NULL
6487 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6488 || (sn->fr_child != NULL
6489 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6490 return FAIL;
6491 return OK;
6492}
6493
6494/*
6495 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6496 * following frames and children.
6497 * Returns a pointer to the old current window, or NULL.
6498 */
6499 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006500restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501{
6502 win_T *wp = NULL;
6503 win_T *wp2;
6504
6505 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 if (fr->fr_layout == FR_LEAF)
6508 {
6509 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006510 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 wp = sn->fr_win;
6512 }
6513 if (sn->fr_next != NULL)
6514 {
6515 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6516 if (wp2 != NULL)
6517 wp = wp2;
6518 }
6519 if (sn->fr_child != NULL)
6520 {
6521 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6522 if (wp2 != NULL)
6523 wp = wp2;
6524 }
6525 return wp;
6526}
6527
6528#endif
6529
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006530#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6531 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006532/*
6533 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006534 * restore_win() MUST be called to undo, also when FAIL is returned.
6535 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006536 * When "no_display" is TRUE the display won't be affected, no redraw is
6537 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006538 * Returns FAIL if switching to "win" failed.
6539 */
6540 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006541switch_win(
6542 win_T **save_curwin UNUSED,
6543 tabpage_T **save_curtab UNUSED,
6544 win_T *win UNUSED,
6545 tabpage_T *tp UNUSED,
6546 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006547{
6548# ifdef FEAT_AUTOCMD
6549 block_autocmds();
6550# endif
6551# ifdef FEAT_WINDOWS
6552 *save_curwin = curwin;
6553 if (tp != NULL)
6554 {
6555 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006556 if (no_display)
6557 {
6558 curtab->tp_firstwin = firstwin;
6559 curtab->tp_lastwin = lastwin;
6560 curtab = tp;
6561 firstwin = curtab->tp_firstwin;
6562 lastwin = curtab->tp_lastwin;
6563 }
6564 else
6565 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006566 }
6567 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006568 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006569 curwin = win;
6570 curbuf = curwin->w_buffer;
6571# endif
6572 return OK;
6573}
6574
6575/*
6576 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006577 * When "no_display" is TRUE the display won't be affected, no redraw is
6578 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006579 */
6580 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006581restore_win(
6582 win_T *save_curwin UNUSED,
6583 tabpage_T *save_curtab UNUSED,
6584 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006585{
6586# ifdef FEAT_WINDOWS
6587 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006588 {
6589 if (no_display)
6590 {
6591 curtab->tp_firstwin = firstwin;
6592 curtab->tp_lastwin = lastwin;
6593 curtab = save_curtab;
6594 firstwin = curtab->tp_firstwin;
6595 lastwin = curtab->tp_lastwin;
6596 }
6597 else
6598 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6599 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006600 if (win_valid(save_curwin))
6601 {
6602 curwin = save_curwin;
6603 curbuf = curwin->w_buffer;
6604 }
6605# endif
6606# ifdef FEAT_AUTOCMD
6607 unblock_autocmds();
6608# endif
6609}
6610
6611/*
6612 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6613 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6614 */
6615 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006616switch_buffer(buf_T **save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006617{
6618# ifdef FEAT_AUTOCMD
6619 block_autocmds();
6620# endif
6621 *save_curbuf = curbuf;
6622 --curbuf->b_nwindows;
6623 curbuf = buf;
6624 curwin->w_buffer = buf;
6625 ++curbuf->b_nwindows;
6626}
6627
6628/*
6629 * Restore the current buffer after using switch_buffer().
6630 */
6631 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006632restore_buffer(buf_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006633{
6634# ifdef FEAT_AUTOCMD
6635 unblock_autocmds();
6636# endif
6637 /* Check for valid buffer, just in case. */
6638 if (buf_valid(save_curbuf))
6639 {
6640 --curbuf->b_nwindows;
6641 curwin->w_buffer = save_curbuf;
6642 curbuf = save_curbuf;
6643 ++curbuf->b_nwindows;
6644 }
6645}
6646#endif
6647
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006648#if (defined(FEAT_GUI) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649/*
6650 * Return TRUE if there is any vertically split window.
6651 */
6652 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006653win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
6655 frame_T *fr;
6656
6657 if (topframe->fr_layout == FR_ROW)
6658 return TRUE;
6659
6660 if (topframe->fr_layout == FR_COL)
6661 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6662 if (fr->fr_layout == FR_ROW)
6663 return TRUE;
6664
6665 return FALSE;
6666}
6667#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006668
6669#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6670/*
6671 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006672 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006673 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6674 * If no particular ID is desired, -1 must be specified for 'id'.
6675 * Return ID of added match, -1 on failure.
6676 */
6677 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006678match_add(
6679 win_T *wp,
6680 char_u *grp,
6681 char_u *pat,
6682 int prio,
6683 int id,
6684 list_T *pos_list,
6685 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006686{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006687 matchitem_T *cur;
6688 matchitem_T *prev;
6689 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006690 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006691 regprog_T *regprog = NULL;
6692 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006693
Bram Moolenaarb3414592014-06-17 17:48:32 +02006694 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006695 return -1;
6696 if (id < -1 || id == 0)
6697 {
6698 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6699 return -1;
6700 }
6701 if (id != -1)
6702 {
6703 cur = wp->w_match_head;
6704 while (cur != NULL)
6705 {
6706 if (cur->id == id)
6707 {
6708 EMSGN("E801: ID already taken: %ld", id);
6709 return -1;
6710 }
6711 cur = cur->next;
6712 }
6713 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006714 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006715 {
6716 EMSG2(_(e_nogroup), grp);
6717 return -1;
6718 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006719 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006720 {
6721 EMSG2(_(e_invarg2), pat);
6722 return -1;
6723 }
6724
6725 /* Find available match ID. */
6726 while (id == -1)
6727 {
6728 cur = wp->w_match_head;
6729 while (cur != NULL && cur->id != wp->w_next_match_id)
6730 cur = cur->next;
6731 if (cur == NULL)
6732 id = wp->w_next_match_id;
6733 wp->w_next_match_id++;
6734 }
6735
6736 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006737 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006738 m->id = id;
6739 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006740 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006741 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006742 m->match.regprog = regprog;
6743 m->match.rmm_ic = FALSE;
6744 m->match.rmm_maxcol = 0;
Bram Moolenaar6561d522015-07-21 15:48:27 +02006745#ifdef FEAT_CONCEAL
6746 m->conceal_char = 0;
6747 if (conceal_char != NULL)
6748 m->conceal_char = (*mb_ptr2char)(conceal_char);
6749#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006750
Bram Moolenaarb3414592014-06-17 17:48:32 +02006751 /* Set up position matches */
6752 if (pos_list != NULL)
6753 {
6754 linenr_T toplnum = 0;
6755 linenr_T botlnum = 0;
6756 listitem_T *li;
6757 int i;
6758
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006759 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006760 i++, li = li->li_next)
6761 {
6762 linenr_T lnum = 0;
6763 colnr_T col = 0;
6764 int len = 1;
6765 list_T *subl;
6766 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006767 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006768
Bram Moolenaarb3414592014-06-17 17:48:32 +02006769 if (li->li_tv.v_type == VAR_LIST)
6770 {
6771 subl = li->li_tv.vval.v_list;
6772 if (subl == NULL)
6773 goto fail;
6774 subli = subl->lv_first;
6775 if (subli == NULL)
6776 goto fail;
6777 lnum = get_tv_number_chk(&subli->li_tv, &error);
6778 if (error == TRUE)
6779 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006780 if (lnum == 0)
6781 {
6782 --i;
6783 continue;
6784 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006785 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006786 subli = subli->li_next;
6787 if (subli != NULL)
6788 {
6789 col = get_tv_number_chk(&subli->li_tv, &error);
6790 if (error == TRUE)
6791 goto fail;
6792 subli = subli->li_next;
6793 if (subli != NULL)
6794 {
6795 len = get_tv_number_chk(&subli->li_tv, &error);
6796 if (error == TRUE)
6797 goto fail;
6798 }
6799 }
6800 m->pos.pos[i].col = col;
6801 m->pos.pos[i].len = len;
6802 }
6803 else if (li->li_tv.v_type == VAR_NUMBER)
6804 {
6805 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006806 {
6807 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006808 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006809 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006810 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6811 m->pos.pos[i].col = 0;
6812 m->pos.pos[i].len = 0;
6813 }
6814 else
6815 {
6816 EMSG(_("List or number required"));
6817 goto fail;
6818 }
6819 if (toplnum == 0 || lnum < toplnum)
6820 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006821 if (botlnum == 0 || lnum >= botlnum)
6822 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006823 }
6824
6825 /* Calculate top and bottom lines for redrawing area */
6826 if (toplnum != 0)
6827 {
6828 if (wp->w_buffer->b_mod_set)
6829 {
6830 if (wp->w_buffer->b_mod_top > toplnum)
6831 wp->w_buffer->b_mod_top = toplnum;
6832 if (wp->w_buffer->b_mod_bot < botlnum)
6833 wp->w_buffer->b_mod_bot = botlnum;
6834 }
6835 else
6836 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006837 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006838 wp->w_buffer->b_mod_top = toplnum;
6839 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006840 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006841 }
6842 m->pos.toplnum = toplnum;
6843 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006844 rtype = VALID;
6845 }
6846 }
6847
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006848 /* Insert new match. The match list is in ascending order with regard to
6849 * the match priorities. */
6850 cur = wp->w_match_head;
6851 prev = cur;
6852 while (cur != NULL && prio >= cur->priority)
6853 {
6854 prev = cur;
6855 cur = cur->next;
6856 }
6857 if (cur == prev)
6858 wp->w_match_head = m;
6859 else
6860 prev->next = m;
6861 m->next = cur;
6862
Bram Moolenaarb3414592014-06-17 17:48:32 +02006863 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006864 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006865
6866fail:
6867 vim_free(m);
6868 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006869}
6870
6871/*
6872 * Delete match with ID 'id' in the match list of window 'wp'.
6873 * Print error messages if 'perr' is TRUE.
6874 */
6875 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006876match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006877{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006878 matchitem_T *cur = wp->w_match_head;
6879 matchitem_T *prev = cur;
6880 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006881
6882 if (id < 1)
6883 {
6884 if (perr == TRUE)
6885 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6886 id);
6887 return -1;
6888 }
6889 while (cur != NULL && cur->id != id)
6890 {
6891 prev = cur;
6892 cur = cur->next;
6893 }
6894 if (cur == NULL)
6895 {
6896 if (perr == TRUE)
6897 EMSGN("E803: ID not found: %ld", id);
6898 return -1;
6899 }
6900 if (cur == prev)
6901 wp->w_match_head = cur->next;
6902 else
6903 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006904 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006905 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006906 if (cur->pos.toplnum != 0)
6907 {
6908 if (wp->w_buffer->b_mod_set)
6909 {
6910 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6911 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6912 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6913 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6914 }
6915 else
6916 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006917 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006918 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6919 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006920 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006921 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006922 rtype = VALID;
6923 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006924 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006925 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006926 return 0;
6927}
6928
6929/*
6930 * Delete all matches in the match list of window 'wp'.
6931 */
6932 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006933clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006934{
6935 matchitem_T *m;
6936
6937 while (wp->w_match_head != NULL)
6938 {
6939 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006940 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006941 vim_free(wp->w_match_head->pattern);
6942 vim_free(wp->w_match_head);
6943 wp->w_match_head = m;
6944 }
6945 redraw_later(SOME_VALID);
6946}
6947
6948/*
6949 * Get match from ID 'id' in window 'wp'.
6950 * Return NULL if match not found.
6951 */
6952 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006953get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006954{
6955 matchitem_T *cur = wp->w_match_head;
6956
6957 while (cur != NULL && cur->id != id)
6958 cur = cur->next;
6959 return cur;
6960}
6961#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006962
6963#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6964 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006965get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006966{
6967 int i = 1;
6968 win_T *w;
6969
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006970 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006971 ++i;
6972
6973 if (w == NULL)
6974 return 0;
6975 else
6976 return i;
6977}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006978
6979 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006980get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006981{
6982 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006983# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006984 tabpage_T *t;
6985
6986 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6987 ++i;
6988
6989 if (t == NULL)
6990 return 0;
6991 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006992# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006993 return i;
6994}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006995#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006996
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01006997#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006998/*
6999 * Return TRUE if "topfrp" and its children are at the right height.
7000 */
7001 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007002frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007003{
7004 frame_T *frp;
7005
7006 if (topfrp->fr_height != height)
7007 return FALSE;
7008
7009 if (topfrp->fr_layout == FR_ROW)
7010 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7011 if (frp->fr_height != height)
7012 return FALSE;
7013
7014 return TRUE;
7015}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007016#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007017
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007018#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007019/*
7020 * Return TRUE if "topfrp" and its children are at the right width.
7021 */
7022 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007023frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007024{
7025 frame_T *frp;
7026
7027 if (topfrp->fr_width != width)
7028 return FALSE;
7029
7030 if (topfrp->fr_layout == FR_COL)
7031 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7032 if (frp->fr_width != width)
7033 return FALSE;
7034
7035 return TRUE;
7036}
7037#endif
7038
Bram Moolenaar86edef62016-03-13 18:07:30 +01007039#if defined(FEAT_EVAL) || defined(PROTO)
7040 int
7041win_getid(typval_T *argvars)
7042{
7043 int winnr;
7044 win_T *wp;
7045
7046 if (argvars[0].v_type == VAR_UNKNOWN)
7047 return curwin->w_id;
7048 winnr = get_tv_number(&argvars[0]);
7049 if (winnr > 0)
7050 {
7051 if (argvars[1].v_type == VAR_UNKNOWN)
7052 wp = firstwin;
7053 else
7054 {
7055 tabpage_T *tp;
7056 int tabnr = get_tv_number(&argvars[1]);
7057
7058 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7059 if (--tabnr == 0)
7060 break;
7061 if (tp == NULL)
7062 return -1;
7063 wp = tp->tp_firstwin;
7064 }
7065 for ( ; wp != NULL; wp = wp->w_next)
7066 if (--winnr == 0)
7067 return wp->w_id;
7068 }
7069 return 0;
7070}
7071
7072 int
7073win_gotoid(typval_T *argvars)
7074{
7075 win_T *wp;
7076 tabpage_T *tp;
7077 int id = get_tv_number(&argvars[0]);
7078
7079 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7080 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7081 wp != NULL; wp = wp->w_next)
7082 if (wp->w_id == id)
7083 {
7084 goto_tabpage_win(tp, wp);
7085 return 1;
7086 }
7087 return 0;
7088}
7089
7090 void
7091win_id2tabwin(typval_T *argvars, list_T *list)
7092{
7093 win_T *wp;
7094 tabpage_T *tp;
7095 int winnr = 1;
7096 int tabnr = 1;
7097 int id = get_tv_number(&argvars[0]);
7098
7099 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7100 {
7101 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7102 wp != NULL; wp = wp->w_next)
7103 {
7104 if (wp->w_id == id)
7105 {
7106 list_append_number(list, tabnr);
7107 list_append_number(list, winnr);
7108 return;
7109 }
7110 ++winnr;
7111 }
7112 ++tabnr;
7113 winnr = 1;
7114 }
7115 list_append_number(list, 0);
7116 list_append_number(list, 0);
7117}
7118
7119 int
7120win_id2win(typval_T *argvars)
7121{
7122 win_T *wp;
7123 int nr = 1;
7124 int id = get_tv_number(&argvars[0]);
7125
7126 for (wp = firstwin; wp != NULL; wp = wp->w_next)
7127 {
7128 if (wp->w_id == id)
7129 return nr;
7130 ++nr;
7131 }
7132 return 0;
7133}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007134
7135 void
7136win_findbuf(typval_T *argvars, list_T *list)
7137{
7138 win_T *wp;
7139 tabpage_T *tp;
7140 int bufnr = get_tv_number(&argvars[0]);
7141
7142 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7143 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7144 wp != NULL; wp = wp->w_next)
7145 if (wp->w_buffer->b_fnum == bufnr)
7146 list_append_number(list, wp->w_id);
7147}
7148
Bram Moolenaar86edef62016-03-13 18:07:30 +01007149#endif