blob: 2dc255467262c2001329329deeb44ca0b8592cab [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:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200343 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 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))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001168 {
1169 int new_fr_height = curfrp->fr_height - new_size;
1170
1171 if (!((flags & WSP_BOT) && p_ls == 0))
1172 new_fr_height -= STATUS_HEIGHT;
1173 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 else
1176 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1177 if (before) /* new window above current one */
1178 {
1179 wp->w_winrow = oldwin->w_winrow;
1180 wp->w_status_height = STATUS_HEIGHT;
1181 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1182 }
1183 else /* new window below current one */
1184 {
1185 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1186 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001187 if (!(flags & WSP_BOT))
1188 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (flags & WSP_BOT)
1191 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 frame_fix_height(wp);
1193 frame_fix_height(oldwin);
1194 }
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 Moolenaar829c8e32016-03-19 23:07:23 +01003449#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 topframe->fr_width = Columns;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 topframe->fr_height = Rows - p_ch;
3453 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003454
3455 return OK;
3456}
3457
3458/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003459 * Create a frame for window "wp".
3460 */
3461 static void
3462new_frame(win_T *wp)
3463{
3464 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3465
3466 wp->w_frame = frp;
3467 if (frp != NULL)
3468 {
3469 frp->fr_layout = FR_LEAF;
3470 frp->fr_win = wp;
3471 }
3472}
3473
3474/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003475 * Initialize the window and frame size to the maximum.
3476 */
3477 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003478win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003479{
3480 firstwin->w_height = ROWS_AVAIL;
3481 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003482#ifdef FEAT_WINDOWS
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003483 firstwin->w_width = Columns;
3484 topframe->fr_width = Columns;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486}
3487
3488#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003489
3490/*
3491 * Allocate a new tabpage_T and init the values.
3492 * Returns NULL when out of memory.
3493 */
3494 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003495alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003496{
3497 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003498# ifdef FEAT_GUI
3499 int i;
3500# endif
3501
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003502
3503 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003504 if (tp == NULL)
3505 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003506
Bram Moolenaar429fa852013-04-15 12:27:36 +02003507# ifdef FEAT_EVAL
3508 /* init t: variables */
3509 tp->tp_vars = dict_alloc();
3510 if (tp->tp_vars == NULL)
3511 {
3512 vim_free(tp);
3513 return NULL;
3514 }
3515 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3516# endif
3517
3518# ifdef FEAT_GUI
3519 for (i = 0; i < 3; i++)
3520 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003521# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003522# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003523 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003524# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003525 tp->tp_ch_used = p_ch;
3526
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003527 return tp;
3528}
3529
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003530 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003531free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003532{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003533 int idx;
3534
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003535# ifdef FEAT_DIFF
3536 diff_clear(tp);
3537# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003538 for (idx = 0; idx < SNAP_COUNT; ++idx)
3539 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003540#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003541 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3542 hash_init(&tp->tp_vars->dv_hashtab);
3543 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003544#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003545
3546#ifdef FEAT_PYTHON
3547 python_tabpage_free(tp);
3548#endif
3549
3550#ifdef FEAT_PYTHON3
3551 python3_tabpage_free(tp);
3552#endif
3553
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003554 vim_free(tp);
3555}
3556
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003557/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003558 * Create a new Tab page with one window.
3559 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003560 * When "after" is 0 put it just after the current Tab page.
3561 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003562 * Return FAIL or OK.
3563 */
3564 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003565win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003566{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003567 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003568 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003569 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003570
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003571 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003572 if (newtp == NULL)
3573 return FAIL;
3574
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003575 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003576 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003577 {
3578 vim_free(newtp);
3579 return FAIL;
3580 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003581 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582
3583 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003584 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003585 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003586 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003587 if (after == 1)
3588 {
3589 /* New tab page becomes the first one. */
3590 newtp->tp_next = first_tabpage;
3591 first_tabpage = newtp;
3592 }
3593 else
3594 {
3595 if (after > 0)
3596 {
3597 /* Put new tab page before tab page "after". */
3598 n = 2;
3599 for (tp = first_tabpage; tp->tp_next != NULL
3600 && n < after; tp = tp->tp_next)
3601 ++n;
3602 }
3603 newtp->tp_next = tp->tp_next;
3604 tp->tp_next = newtp;
3605 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003606 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003607 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003608 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003609
3610 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003611 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003612
3613#if defined(FEAT_GUI)
3614 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3615 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003616 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003617#endif
3618
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003619 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003620#ifdef FEAT_AUTOCMD
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003621 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003622 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003623#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003624 return OK;
3625 }
3626
3627 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003628 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629 return FAIL;
3630}
3631
3632/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003633 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3634 * like with ":split".
3635 * Returns OK if a new tab page was created, FAIL otherwise.
3636 */
3637 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003638may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003639{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003640 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003641
Bram Moolenaard326ce82007-03-11 14:48:29 +00003642 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003643 {
3644 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003645 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003646 return win_new_tabpage(n);
3647 }
3648 return FAIL;
3649}
3650
3651/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003652 * Create up to "maxcount" tabpages with empty windows.
3653 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003654 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003655 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003656make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003657{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003658 int count = maxcount;
3659 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003660
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003661 /* Limit to 'tabpagemax' tabs. */
3662 if (count > p_tpm)
3663 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003664
3665#ifdef FEAT_AUTOCMD
3666 /*
3667 * Don't execute autocommands while creating the tab pages. Must do that
3668 * when putting the buffers in the windows.
3669 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003670 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003671#endif
3672
3673 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003674 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003675 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003676
3677#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003678 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003679#endif
3680
3681 /* return actual number of tab pages */
3682 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003683}
3684
3685/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003686 * Return TRUE when "tpc" points to a valid tab page.
3687 */
3688 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003689valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003690{
3691 tabpage_T *tp;
3692
3693 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3694 if (tp == tpc)
3695 return TRUE;
3696 return FALSE;
3697}
3698
3699/*
3700 * Find tab page "n" (first one is 1). Returns NULL when not found.
3701 */
3702 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003703find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003704{
3705 tabpage_T *tp;
3706 int i = 1;
3707
3708 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3709 ++i;
3710 return tp;
3711}
3712
3713/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003714 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003715 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003716 */
3717 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003718tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003719{
3720 int i = 1;
3721 tabpage_T *tp;
3722
3723 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3724 ++i;
3725 return i;
3726}
3727
3728/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003729 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003730 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003731 * FAIL.
3732 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003733 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003734 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003735leave_tabpage(
3736 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003737 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003738 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003739{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003740 tabpage_T *tp = curtab;
3741
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003742 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003743#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003744 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003745 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003746 if (new_curbuf != curbuf)
3747 {
3748 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3749 if (curtab != tp)
3750 return FAIL;
3751 }
3752 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3753 if (curtab != tp)
3754 return FAIL;
3755 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003756 if (curtab != tp)
3757 return FAIL;
3758 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003759#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003760#if defined(FEAT_GUI)
3761 /* Remove the scrollbars. They may be added back later. */
3762 if (gui.in_use)
3763 gui_remove_scrollbars();
3764#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003765 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003766 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003767 tp->tp_firstwin = firstwin;
3768 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003769 tp->tp_old_Rows = Rows;
3770 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003771 firstwin = NULL;
3772 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003773 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003774}
3775
3776/*
3777 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003778 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003779 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3780 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003781 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003782 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003783enter_tabpage(
3784 tabpage_T *tp,
3785 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003786 int trigger_enter_autocmds,
3787 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003788{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003789 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003790 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003791
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003792 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003793 firstwin = tp->tp_firstwin;
3794 lastwin = tp->tp_lastwin;
3795 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003796
3797 /* We would like doing the TabEnter event first, but we don't have a
3798 * valid current window yet, which may break some commands.
3799 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaard6949742013-06-16 14:18:28 +02003800 win_enter_ext(tp->tp_curwin, FALSE, TRUE,
3801 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003802 prevwin = next_prevwin;
3803
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003804 last_status(FALSE); /* status line may appear or disappear */
3805 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003806 must_redraw = CLEAR; /* need to redraw everything */
3807#ifdef FEAT_DIFF
3808 diff_need_scrollbind = TRUE;
3809#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003811 /* The tabpage line may have appeared or disappeared, may need to resize
3812 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003813 * frame sizes too. Use the stored value of p_ch, so that it can be
3814 * different for each tab page. */
3815 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003816 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3817#ifdef FEAT_GUI_TABLINE
3818 && !gui_use_tabline()
3819#endif
3820 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003821 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003822 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003823 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003824
3825#if defined(FEAT_GUI)
3826 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3827 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003828 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003829#endif
3830
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003831#ifdef FEAT_AUTOCMD
3832 /* Apply autocommands after updating the display, when 'rows' and
3833 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003834 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003835 {
3836 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3837 if (old_curbuf != curbuf)
3838 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3839 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003840#endif
3841
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003842 redraw_all_later(CLEAR);
3843}
3844
3845/*
3846 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003847 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003848 */
3849 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003850goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003851{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003852 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003853 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003854 int i;
3855
Bram Moolenaard68071d2006-05-02 22:08:30 +00003856 if (text_locked())
3857 {
3858 /* Not allowed when editing the command line. */
3859#ifdef FEAT_CMDWIN
3860 if (cmdwin_type != 0)
3861 EMSG(_(e_cmdwin));
3862 else
3863#endif
3864 EMSG(_(e_secure));
3865 return;
3866 }
3867
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003868 /* If there is only one it can't work. */
3869 if (first_tabpage->tp_next == NULL)
3870 {
3871 if (n > 1)
3872 beep_flush();
3873 return;
3874 }
3875
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003876 if (n == 0)
3877 {
3878 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003879 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003880 tp = first_tabpage;
3881 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003882 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003883 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003884 else if (n < 0)
3885 {
3886 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3887 * this N times. */
3888 ttp = curtab;
3889 for (i = n; i < 0; ++i)
3890 {
3891 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3892 tp = tp->tp_next)
3893 ;
3894 ttp = tp;
3895 }
3896 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003897 else if (n == 9999)
3898 {
3899 /* Go to last tab page. */
3900 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3901 ;
3902 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003903 else
3904 {
3905 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003906 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003907 if (tp == NULL)
3908 {
3909 beep_flush();
3910 return;
3911 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003912 }
3913
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003914 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003915
3916#ifdef FEAT_GUI_TABLINE
3917 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003918 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003919#endif
3920}
3921
3922/*
3923 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003924 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3925 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003926 * Note: doesn't update the GUI tab.
3927 */
3928 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003929goto_tabpage_tp(
3930 tabpage_T *tp,
3931 int trigger_enter_autocmds,
3932 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003933{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003934 /* Don't repeat a message in another tab page. */
3935 set_keep_msg(NULL, 0);
3936
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003937 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3938 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003939 {
3940 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003941 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3942 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003943 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003944 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3945 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003946 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003947}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
3949/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003950 * Enter window "wp" in tab page "tp".
3951 * Also updates the GUI tab.
3952 */
3953 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003954goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003955{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003956 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003957 if (curtab == tp && win_valid(wp))
3958 {
3959 win_enter(wp, TRUE);
3960# ifdef FEAT_GUI_TABLINE
3961 if (gui_use_tabline())
3962 gui_mch_set_curtab(tabpage_index(curtab));
3963# endif
3964 }
3965}
3966
3967/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003968 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003969 */
3970 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003971tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003972{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003973 int n = 1;
3974 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003975
3976 if (first_tabpage->tp_next == NULL)
3977 return;
3978
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003979 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
3980 ++n;
3981
3982 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
3983 && tp->tp_next == curtab))
3984 return;
3985
3986 tp_dst = tp;
3987
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003988 /* Remove the current tab page from the list of tab pages. */
3989 if (curtab == first_tabpage)
3990 first_tabpage = curtab->tp_next;
3991 else
3992 {
3993 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3994 if (tp->tp_next == curtab)
3995 break;
3996 if (tp == NULL) /* "cannot happen" */
3997 return;
3998 tp->tp_next = curtab->tp_next;
3999 }
4000
4001 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004002 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004003 {
4004 curtab->tp_next = first_tabpage;
4005 first_tabpage = curtab;
4006 }
4007 else
4008 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004009 curtab->tp_next = tp_dst->tp_next;
4010 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004011 }
4012
4013 /* Need to redraw the tabline. Tab page contents doesn't change. */
4014 redraw_tabline = TRUE;
4015}
4016
4017
4018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * Go to another window.
4020 * When jumping to another buffer, stop Visual mode. Do this before
4021 * changing windows so we can yank the selection into the '*' register.
4022 * When jumping to another window on the same buffer, adjust its cursor
4023 * position to keep the same Visual area.
4024 */
4025 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004026win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004028#ifdef FEAT_CONCEAL
4029 win_T *owp = curwin;
4030#endif
4031
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004032 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 {
4034 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004035 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 return;
4037 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004038#ifdef FEAT_AUTOCMD
4039 if (curbuf_locked())
4040 return;
4041#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (wp->w_buffer != curbuf)
4044 reset_VIsual_and_resel();
4045 else if (VIsual_active)
4046 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048#ifdef FEAT_GUI
4049 need_mouse_correct = TRUE;
4050#endif
4051 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004052
4053#ifdef FEAT_CONCEAL
4054 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004055 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004056 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004057 if (curwin->w_p_cole > 0 && !msg_scrolled)
4058 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004059#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060}
4061
4062#if defined(FEAT_PERL) || defined(PROTO)
4063/*
4064 * Find window number "winnr" (counting top to bottom).
4065 */
4066 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004067win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068{
4069 win_T *wp;
4070
4071# ifdef FEAT_WINDOWS
4072 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4073 if (--winnr == 0)
4074 break;
4075 return wp;
4076# else
4077 return curwin;
4078# endif
4079}
4080#endif
4081
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004082#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4083 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004084/*
4085 * Find the tabpage for window "win".
4086 */
4087 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004088win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004089{
4090 win_T *wp;
4091 tabpage_T *tp;
4092
4093 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004094 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4095 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004096 if (wp == win)
4097 return tp;
4098 return NULL;
4099}
4100#endif
4101
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102/*
4103 * Move to window above or below "count" times.
4104 */
4105 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004106win_goto_ver(
4107 int up, /* TRUE to go to win above */
4108 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109{
4110 frame_T *fr;
4111 frame_T *nfr;
4112 frame_T *foundfr;
4113
4114 foundfr = curwin->w_frame;
4115 while (count--)
4116 {
4117 /*
4118 * First go upwards in the tree of frames until we find a upwards or
4119 * downwards neighbor.
4120 */
4121 fr = foundfr;
4122 for (;;)
4123 {
4124 if (fr == topframe)
4125 goto end;
4126 if (up)
4127 nfr = fr->fr_prev;
4128 else
4129 nfr = fr->fr_next;
4130 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4131 break;
4132 fr = fr->fr_parent;
4133 }
4134
4135 /*
4136 * Now go downwards to find the bottom or top frame in it.
4137 */
4138 for (;;)
4139 {
4140 if (nfr->fr_layout == FR_LEAF)
4141 {
4142 foundfr = nfr;
4143 break;
4144 }
4145 fr = nfr->fr_child;
4146 if (nfr->fr_layout == FR_ROW)
4147 {
4148 /* Find the frame at the cursor row. */
4149 while (fr->fr_next != NULL
4150 && frame2win(fr)->w_wincol + fr->fr_width
4151 <= curwin->w_wincol + curwin->w_wcol)
4152 fr = fr->fr_next;
4153 }
4154 if (nfr->fr_layout == FR_COL && up)
4155 while (fr->fr_next != NULL)
4156 fr = fr->fr_next;
4157 nfr = fr;
4158 }
4159 }
4160end:
4161 if (foundfr != NULL)
4162 win_goto(foundfr->fr_win);
4163}
4164
4165/*
4166 * Move to left or right window.
4167 */
4168 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004169win_goto_hor(
4170 int left, /* TRUE to go to left win */
4171 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 frame_T *fr;
4174 frame_T *nfr;
4175 frame_T *foundfr;
4176
4177 foundfr = curwin->w_frame;
4178 while (count--)
4179 {
4180 /*
4181 * First go upwards in the tree of frames until we find a left or
4182 * right neighbor.
4183 */
4184 fr = foundfr;
4185 for (;;)
4186 {
4187 if (fr == topframe)
4188 goto end;
4189 if (left)
4190 nfr = fr->fr_prev;
4191 else
4192 nfr = fr->fr_next;
4193 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4194 break;
4195 fr = fr->fr_parent;
4196 }
4197
4198 /*
4199 * Now go downwards to find the leftmost or rightmost frame in it.
4200 */
4201 for (;;)
4202 {
4203 if (nfr->fr_layout == FR_LEAF)
4204 {
4205 foundfr = nfr;
4206 break;
4207 }
4208 fr = nfr->fr_child;
4209 if (nfr->fr_layout == FR_COL)
4210 {
4211 /* Find the frame at the cursor row. */
4212 while (fr->fr_next != NULL
4213 && frame2win(fr)->w_winrow + fr->fr_height
4214 <= curwin->w_winrow + curwin->w_wrow)
4215 fr = fr->fr_next;
4216 }
4217 if (nfr->fr_layout == FR_ROW && left)
4218 while (fr->fr_next != NULL)
4219 fr = fr->fr_next;
4220 nfr = fr;
4221 }
4222 }
4223end:
4224 if (foundfr != NULL)
4225 win_goto(foundfr->fr_win);
4226}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228/*
4229 * Make window "wp" the current window.
4230 */
4231 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004232win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004234 win_enter_ext(wp, undo_sync, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235}
4236
4237/*
4238 * Make window wp the current window.
4239 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4240 * been closed and isn't valid.
4241 */
4242 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004243win_enter_ext(
4244 win_T *wp,
4245 int undo_sync,
4246 int curwin_invalid,
4247 int trigger_enter_autocmds UNUSED,
4248 int trigger_leave_autocmds UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250#ifdef FEAT_AUTOCMD
4251 int other_buffer = FALSE;
4252#endif
4253
4254 if (wp == curwin && !curwin_invalid) /* nothing to do */
4255 return;
4256
4257#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004258 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
4260 /*
4261 * Be careful: If autocommands delete the window, return now.
4262 */
4263 if (wp->w_buffer != curbuf)
4264 {
4265 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4266 other_buffer = TRUE;
4267 if (!win_valid(wp))
4268 return;
4269 }
4270 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4271 if (!win_valid(wp))
4272 return;
4273# ifdef FEAT_EVAL
4274 /* autocmds may abort script processing */
4275 if (aborting())
4276 return;
4277# endif
4278 }
4279#endif
4280
4281 /* sync undo before leaving the current buffer */
4282 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004283 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004284
4285 /* Might need to scroll the old window before switching, e.g., when the
4286 * cursor was moved. */
4287 update_topline();
4288
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 /* may have to copy the buffer options when 'cpo' contains 'S' */
4290 if (wp->w_buffer != curbuf)
4291 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4292 if (!curwin_invalid)
4293 {
4294 prevwin = curwin; /* remember for CTRL-W p */
4295 curwin->w_redr_status = TRUE;
4296 }
4297 curwin = wp;
4298 curbuf = wp->w_buffer;
4299 check_cursor();
4300#ifdef FEAT_VIRTUALEDIT
4301 if (!virtual_active())
4302 curwin->w_cursor.coladd = 0;
4303#endif
4304 changed_line_abv_curs(); /* assume cursor position needs updating */
4305
4306 if (curwin->w_localdir != NULL)
4307 {
4308 /* Window has a local directory: Save current directory as global
4309 * directory (unless that was done already) and change to the local
4310 * directory. */
4311 if (globaldir == NULL)
4312 {
4313 char_u cwd[MAXPATHL];
4314
4315 if (mch_dirname(cwd, MAXPATHL) == OK)
4316 globaldir = vim_strsave(cwd);
4317 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004318 if (mch_chdir((char *)curwin->w_localdir) == 0)
4319 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321 else if (globaldir != NULL)
4322 {
4323 /* Window doesn't have a local directory and we are not in the global
4324 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004325 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 vim_free(globaldir);
4327 globaldir = NULL;
4328 shorten_fnames(TRUE);
4329 }
4330
4331#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004332 if (trigger_enter_autocmds)
4333 {
4334 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4335 if (other_buffer)
4336 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338#endif
4339
4340#ifdef FEAT_TITLE
4341 maketitle();
4342#endif
4343 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004344 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (restart_edit)
4346 redraw_later(VALID); /* causes status line redraw */
4347
4348 /* set window height to desired minimal value */
4349 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4350 win_setheight((int)p_wh);
4351 else if (curwin->w_height == 0)
4352 win_setheight(1);
4353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004355 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357
4358#ifdef FEAT_MOUSE
4359 setmouse(); /* in case jumped to/from help buffer */
4360#endif
4361
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004362 /* Change directories when the 'acd' option is set. */
4363 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364}
4365
4366#endif /* FEAT_WINDOWS */
4367
4368#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4369/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004370 * Jump to the first open window that contains buffer "buf", if one exists.
4371 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 */
4373 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004374buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004376 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004378 if (curwin->w_buffer == buf)
4379 wp = curwin;
4380# ifdef FEAT_WINDOWS
4381 else
4382 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4383 if (wp->w_buffer == buf)
4384 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 if (wp != NULL)
4386 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004388 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004390
4391/*
4392 * Jump to the first open window in any tab page that contains buffer "buf",
4393 * if one exists.
4394 * Returns a pointer to the window found, otherwise NULL.
4395 */
4396 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004397buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004398{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004399 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004400# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004401 tabpage_T *tp;
4402
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004403 if (wp != NULL)
4404 return wp;
4405
4406 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4407 if (tp != curtab)
4408 {
4409 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4410 if (wp->w_buffer == buf)
4411 break;
4412 if (wp != NULL)
4413 {
4414 goto_tabpage_win(tp, wp);
4415 if (curwin != wp)
4416 wp = NULL; /* something went wrong */
4417 break;
4418 }
4419 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004420# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004421 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004422}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423#endif
4424
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004425static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004428 * Allocate a window structure and link it in the window list when "hidden" is
4429 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004432win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004434 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 /*
4437 * allocate window structure and linesizes arrays
4438 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004439 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004440 if (new_wp == NULL)
4441 return NULL;
4442
4443 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004445 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004446 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448
Bram Moolenaar86edef62016-03-13 18:07:30 +01004449 new_wp->w_id = ++last_win_id;
4450
Bram Moolenaar429fa852013-04-15 12:27:36 +02004451#ifdef FEAT_EVAL
4452 /* init w: variables */
4453 new_wp->w_vars = dict_alloc();
4454 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004456 win_free_lsize(new_wp);
4457 vim_free(new_wp);
4458 return NULL;
4459 }
4460 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004461#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004462
4463#ifdef FEAT_AUTOCMD
4464 /* Don't execute autocommands while the window is not properly
4465 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4466 * event. */
4467 block_autocmds();
4468#endif
4469 /*
4470 * link the window in the window list
4471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004473 if (!hidden)
4474 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004475 new_wp->w_wincol = 0;
4476 new_wp->w_width = Columns;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01004477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
Bram Moolenaar429fa852013-04-15 12:27:36 +02004479 /* position the display and the cursor at the top of the file. */
4480 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004482 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004484 new_wp->w_botline = 2;
4485 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004487 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488#endif
4489
Bram Moolenaar429fa852013-04-15 12:27:36 +02004490 /* We won't calculate w_fraction until resizing the window */
4491 new_wp->w_fraction = 0;
4492 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
4494#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004495 if (gui.in_use)
4496 {
4497 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4498 SBAR_LEFT, new_wp);
4499 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4500 SBAR_RIGHT, new_wp);
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502#endif
4503#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004504 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004506#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004507 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004508#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004509#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004510 new_wp->w_match_head = NULL;
4511 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004512#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004513 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514}
4515
4516#if defined(FEAT_WINDOWS) || defined(PROTO)
4517
4518/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004519 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 */
4521 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004522win_free(
4523 win_T *wp,
4524 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004527 buf_T *buf;
4528 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004530#ifdef FEAT_FOLDING
4531 clearFolding(wp);
4532#endif
4533
4534 /* reduce the reference count to the argument list. */
4535 alist_unlink(wp->w_alist);
4536
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004537#ifdef FEAT_AUTOCMD
4538 /* Don't execute autocommands while the window is halfway being deleted.
4539 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004540 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004541#endif
4542
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004543#ifdef FEAT_LUA
4544 lua_window_free(wp);
4545#endif
4546
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004547#ifdef FEAT_MZSCHEME
4548 mzscheme_window_free(wp);
4549#endif
4550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#ifdef FEAT_PERL
4552 perl_win_free(wp);
4553#endif
4554
4555#ifdef FEAT_PYTHON
4556 python_window_free(wp);
4557#endif
4558
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004559#ifdef FEAT_PYTHON3
4560 python3_window_free(wp);
4561#endif
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563#ifdef FEAT_TCL
4564 tcl_window_free(wp);
4565#endif
4566
4567#ifdef FEAT_RUBY
4568 ruby_window_free(wp);
4569#endif
4570
4571 clear_winopt(&wp->w_onebuf_opt);
4572 clear_winopt(&wp->w_allbuf_opt);
4573
4574#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004575 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4576 hash_init(&wp->w_vars->dv_hashtab);
4577 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#endif
4579
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004580 {
4581 tabpage_T *ttp;
4582
4583 if (prevwin == wp)
4584 prevwin = NULL;
4585 for (ttp = first_tabpage; ttp != NULL; ttp = ttp->tp_next)
4586 if (ttp->tp_prevwin == wp)
4587 ttp->tp_prevwin = NULL;
4588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 win_free_lsize(wp);
4590
4591 for (i = 0; i < wp->w_tagstacklen; ++i)
4592 vim_free(wp->w_tagstack[i].tagname);
4593
4594 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004595
Bram Moolenaarff18df02013-07-24 17:51:57 +02004596 /* Remove the window from the b_wininfo lists, it may happen that the
4597 * freed memory is re-used for another window. */
4598 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4599 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4600 if (wip->wi_win == wp)
4601 wip->wi_win = NULL;
4602
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004604 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607#ifdef FEAT_JUMPLIST
4608 free_jumplist(wp);
4609#endif
4610
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004611#ifdef FEAT_QUICKFIX
4612 qf_free_all(wp);
4613#endif
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#ifdef FEAT_GUI
4616 if (gui.in_use)
4617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4619 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4620 }
4621#endif /* FEAT_GUI */
4622
Bram Moolenaar860cae12010-06-05 23:22:07 +02004623#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004624 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004625#endif
4626
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004627#ifdef FEAT_AUTOCMD
4628 if (wp != aucmd_win)
4629#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004630 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004631#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004632 if (autocmd_busy)
4633 {
4634 wp->w_next = au_pending_free_win;
4635 au_pending_free_win = wp;
4636 }
4637 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004638#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004639 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004640
4641#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004642 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644}
4645
4646/*
4647 * Append window "wp" in the window list after window "after".
4648 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004649 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004650win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651{
4652 win_T *before;
4653
4654 if (after == NULL) /* after NULL is in front of the first */
4655 before = firstwin;
4656 else
4657 before = after->w_next;
4658
4659 wp->w_next = before;
4660 wp->w_prev = after;
4661 if (after == NULL)
4662 firstwin = wp;
4663 else
4664 after->w_next = wp;
4665 if (before == NULL)
4666 lastwin = wp;
4667 else
4668 before->w_prev = wp;
4669}
4670
4671/*
4672 * Remove a window from the window list.
4673 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004674 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004675win_remove(
4676 win_T *wp,
4677 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
4679 if (wp->w_prev != NULL)
4680 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004681 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004683 else
4684 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (wp->w_next != NULL)
4686 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004687 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004689 else
4690 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691}
4692
4693/*
4694 * Append frame "frp" in a frame list after frame "after".
4695 */
4696 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004697frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
4699 frp->fr_next = after->fr_next;
4700 after->fr_next = frp;
4701 if (frp->fr_next != NULL)
4702 frp->fr_next->fr_prev = frp;
4703 frp->fr_prev = after;
4704}
4705
4706/*
4707 * Insert frame "frp" in a frame list before frame "before".
4708 */
4709 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004710frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 frp->fr_next = before;
4713 frp->fr_prev = before->fr_prev;
4714 before->fr_prev = frp;
4715 if (frp->fr_prev != NULL)
4716 frp->fr_prev->fr_next = frp;
4717 else
4718 frp->fr_parent->fr_child = frp;
4719}
4720
4721/*
4722 * Remove a frame from a frame list.
4723 */
4724 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004725frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726{
4727 if (frp->fr_prev != NULL)
4728 frp->fr_prev->fr_next = frp->fr_next;
4729 else
4730 frp->fr_parent->fr_child = frp->fr_next;
4731 if (frp->fr_next != NULL)
4732 frp->fr_next->fr_prev = frp->fr_prev;
4733}
4734
4735#endif /* FEAT_WINDOWS */
4736
4737/*
4738 * Allocate w_lines[] for window "wp".
4739 * Return FAIL for failure, OK for success.
4740 */
4741 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004742win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004745 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if (wp->w_lines == NULL)
4747 return FAIL;
4748 return OK;
4749}
4750
4751/*
4752 * free lsize arrays for a window
4753 */
4754 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004755win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004757 /* TODO: why would wp be NULL here? */
4758 if (wp != NULL)
4759 {
4760 vim_free(wp->w_lines);
4761 wp->w_lines = NULL;
4762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763}
4764
4765/*
4766 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004767 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 */
4769 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004770shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004772 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
4774 if (firstwin == NULL) /* not initialized yet */
4775 return;
4776#ifdef FEAT_WINDOWS
4777 if (h < frame_minheight(topframe, NULL))
4778 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004779
4780 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 * that doesn't result in the right height, forget about that option. */
4782 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004783 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 frame_new_height(topframe, h, FALSE, FALSE);
4785
4786 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4787#else
4788 if (h < 1)
4789 h = 1;
4790 win_new_height(firstwin, h);
4791#endif
4792 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004793#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004794 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004795#endif
4796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797#if 0
4798 /* Disabled: don't want making the screen smaller make a window larger. */
4799 if (p_ea)
4800 win_equal(curwin, FALSE, 'v');
4801#endif
4802}
4803
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004804#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805/*
4806 * Called from win_new_shellsize() after Columns changed.
4807 */
4808 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004809shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 if (firstwin == NULL) /* not initialized yet */
4812 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004813
4814 /* First try setting the widths of windows with 'winfixwidth'. If that
4815 * doesn't result in the right width, forget about that option. */
4816 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004817 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004818 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4821#if 0
4822 /* Disabled: don't want making the screen smaller make a window larger. */
4823 if (p_ea)
4824 win_equal(curwin, FALSE, 'h');
4825#endif
4826}
4827#endif
4828
4829#if defined(FEAT_CMDWIN) || defined(PROTO)
4830/*
4831 * Save the size of all windows in "gap".
4832 */
4833 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004834win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
4836{
4837 win_T *wp;
4838
4839 ga_init2(gap, (int)sizeof(int), 1);
4840 if (ga_grow(gap, win_count() * 2) == OK)
4841 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4842 {
4843 ((int *)gap->ga_data)[gap->ga_len++] =
4844 wp->w_width + wp->w_vsep_width;
4845 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4846 }
4847}
4848
4849/*
4850 * Restore window sizes, but only if the number of windows is still the same.
4851 * Does not free the growarray.
4852 */
4853 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004854win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004857 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 if (win_count() * 2 == gap->ga_len)
4860 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004861 /* The order matters, because frames contain other frames, but it's
4862 * difficult to get right. The easy way out is to do it twice. */
4863 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004865 i = 0;
4866 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4867 {
4868 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4869 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 /* recompute the window positions */
4873 (void)win_comp_pos();
4874 }
4875}
4876#endif /* FEAT_CMDWIN */
4877
4878#if defined(FEAT_WINDOWS) || defined(PROTO)
4879/*
4880 * Update the position for all windows, using the width and height of the
4881 * frames.
4882 * Returns the row just after the last window.
4883 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004884 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004885win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004887 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 int col = 0;
4889
4890 frame_comp_pos(topframe, &row, &col);
4891 return row;
4892}
4893
4894/*
4895 * Update the position of the windows in frame "topfrp", using the width and
4896 * height of the frames.
4897 * "*row" and "*col" are the top-left position of the frame. They are updated
4898 * to the bottom-right position plus one.
4899 */
4900 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004901frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
4903 win_T *wp;
4904 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 int startcol;
4906 int startrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907
4908 wp = topfrp->fr_win;
4909 if (wp != NULL)
4910 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004911 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
4913 /* position changed, redraw */
4914 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 redraw_win_later(wp, NOT_VALID);
4917 wp->w_redr_status = TRUE;
4918 }
4919 *row += wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 }
4922 else
4923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 startrow = *row;
4925 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (topfrp->fr_layout == FR_ROW)
4929 *row = startrow; /* all frames are at the same row */
4930 else
4931 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 frame_comp_pos(frp, row, col);
4933 }
4934 }
4935}
4936
4937#endif /* FEAT_WINDOWS */
4938
4939/*
4940 * Set current window height and take care of repositioning other windows to
4941 * fit around it.
4942 */
4943 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004944win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
4946 win_setheight_win(height, curwin);
4947}
4948
4949/*
4950 * Set the window height of window "win" and take care of repositioning other
4951 * windows to fit around it.
4952 */
4953 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004954win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955{
4956 int row;
4957
4958 if (win == curwin)
4959 {
4960 /* Always keep current window at least one line high, even when
4961 * 'winminheight' is zero. */
4962#ifdef FEAT_WINDOWS
4963 if (height < p_wmh)
4964 height = p_wmh;
4965#endif
4966 if (height == 0)
4967 height = 1;
4968 }
4969
4970#ifdef FEAT_WINDOWS
4971 frame_setheight(win->w_frame, height + win->w_status_height);
4972
4973 /* recompute the window positions */
4974 row = win_comp_pos();
4975#else
4976 if (height > topframe->fr_height)
4977 height = topframe->fr_height;
4978 win->w_height = height;
4979 row = height;
4980#endif
4981
4982 /*
4983 * If there is extra space created between the last window and the command
4984 * line, clear it.
4985 */
4986 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
4987 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
4988 cmdline_row = row;
4989 msg_row = row;
4990 msg_col = 0;
4991
4992 redraw_all_later(NOT_VALID);
4993}
4994
4995#if defined(FEAT_WINDOWS) || defined(PROTO)
4996
4997/*
4998 * Set the height of a frame to "height" and take care that all frames and
4999 * windows inside it are resized. Also resize frames on the left and right if
5000 * the are in the same FR_ROW frame.
5001 *
5002 * Strategy:
5003 * If the frame is part of a FR_COL frame, try fitting the frame in that
5004 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5005 * go to containing frames to resize them and make room.
5006 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5007 * Check for the minimal height of the FR_ROW frame.
5008 * At the top level we can also use change the command line height.
5009 */
5010 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005011frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 int room; /* total number of lines available */
5014 int take; /* number of lines taken from other windows */
5015 int room_cmdline; /* lines available from cmdline */
5016 int run;
5017 frame_T *frp;
5018 int h;
5019 int room_reserved;
5020
5021 /* If the height already is the desired value, nothing to do. */
5022 if (curfrp->fr_height == height)
5023 return;
5024
5025 if (curfrp->fr_parent == NULL)
5026 {
5027 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005028 if (height > ROWS_AVAIL)
5029 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 if (height > 0)
5031 frame_new_height(curfrp, height, FALSE, FALSE);
5032 }
5033 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5034 {
5035 /* Row of frames: Also need to resize frames left and right of this
5036 * one. First check for the minimal height of these. */
5037 h = frame_minheight(curfrp->fr_parent, NULL);
5038 if (height < h)
5039 height = h;
5040 frame_setheight(curfrp->fr_parent, height);
5041 }
5042 else
5043 {
5044 /*
5045 * Column of frames: try to change only frames in this column.
5046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 /*
5048 * Do this twice:
5049 * 1: compute room available, if it's not enough try resizing the
5050 * containing frame.
5051 * 2: compute the room available and adjust the height to it.
5052 * Try not to reduce the height of a window with 'winfixheight' set.
5053 */
5054 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 room = 0;
5057 room_reserved = 0;
5058 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5059 frp = frp->fr_next)
5060 {
5061 if (frp != curfrp
5062 && frp->fr_win != NULL
5063 && frp->fr_win->w_p_wfh)
5064 room_reserved += frp->fr_height;
5065 room += frp->fr_height;
5066 if (frp != curfrp)
5067 room -= frame_minheight(frp, NULL);
5068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (curfrp->fr_width != Columns)
5070 room_cmdline = 0;
5071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5074 + lastwin->w_height + lastwin->w_status_height);
5075 if (room_cmdline < 0)
5076 room_cmdline = 0;
5077 }
5078
5079 if (height <= room + room_cmdline)
5080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
5083 if (height > room + room_cmdline)
5084 height = room + room_cmdline;
5085 break;
5086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 frame_setheight(curfrp->fr_parent, height
5088 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090
5091 /*
5092 * Compute the number of lines we will take from others frames (can be
5093 * negative!).
5094 */
5095 take = height - curfrp->fr_height;
5096
5097 /* If there is not enough room, also reduce the height of a window
5098 * with 'winfixheight' set. */
5099 if (height > room + room_cmdline - room_reserved)
5100 room_reserved = room + room_cmdline - height;
5101 /* If there is only a 'winfixheight' window and making the
5102 * window smaller, need to make the other window taller. */
5103 if (take < 0 && room - curfrp->fr_height < room_reserved)
5104 room_reserved = 0;
5105
5106 if (take > 0 && room_cmdline > 0)
5107 {
5108 /* use lines from cmdline first */
5109 if (take < room_cmdline)
5110 room_cmdline = take;
5111 take -= room_cmdline;
5112 topframe->fr_height += room_cmdline;
5113 }
5114
5115 /*
5116 * set the current frame to the new height
5117 */
5118 frame_new_height(curfrp, height, FALSE, FALSE);
5119
5120 /*
5121 * First take lines from the frames after the current frame. If
5122 * that is not enough, takes lines from frames above the current
5123 * frame.
5124 */
5125 for (run = 0; run < 2; ++run)
5126 {
5127 if (run == 0)
5128 frp = curfrp->fr_next; /* 1st run: start with next window */
5129 else
5130 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5131 while (frp != NULL && take != 0)
5132 {
5133 h = frame_minheight(frp, NULL);
5134 if (room_reserved > 0
5135 && frp->fr_win != NULL
5136 && frp->fr_win->w_p_wfh)
5137 {
5138 if (room_reserved >= frp->fr_height)
5139 room_reserved -= frp->fr_height;
5140 else
5141 {
5142 if (frp->fr_height - room_reserved > take)
5143 room_reserved = frp->fr_height - take;
5144 take -= frp->fr_height - room_reserved;
5145 frame_new_height(frp, room_reserved, FALSE, FALSE);
5146 room_reserved = 0;
5147 }
5148 }
5149 else
5150 {
5151 if (frp->fr_height - take < h)
5152 {
5153 take -= frp->fr_height - h;
5154 frame_new_height(frp, h, FALSE, FALSE);
5155 }
5156 else
5157 {
5158 frame_new_height(frp, frp->fr_height - take,
5159 FALSE, FALSE);
5160 take = 0;
5161 }
5162 }
5163 if (run == 0)
5164 frp = frp->fr_next;
5165 else
5166 frp = frp->fr_prev;
5167 }
5168 }
5169 }
5170}
5171
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172/*
5173 * Set current window width and take care of repositioning other windows to
5174 * fit around it.
5175 */
5176 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005177win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178{
5179 win_setwidth_win(width, curwin);
5180}
5181
5182 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005183win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
5185 /* Always keep current window at least one column wide, even when
5186 * 'winminwidth' is zero. */
5187 if (wp == curwin)
5188 {
5189 if (width < p_wmw)
5190 width = p_wmw;
5191 if (width == 0)
5192 width = 1;
5193 }
5194
5195 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5196
5197 /* recompute the window positions */
5198 (void)win_comp_pos();
5199
5200 redraw_all_later(NOT_VALID);
5201}
5202
5203/*
5204 * Set the width of a frame to "width" and take care that all frames and
5205 * windows inside it are resized. Also resize frames above and below if the
5206 * are in the same FR_ROW frame.
5207 *
5208 * Strategy is similar to frame_setheight().
5209 */
5210 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005211frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212{
5213 int room; /* total number of lines available */
5214 int take; /* number of lines taken from other windows */
5215 int run;
5216 frame_T *frp;
5217 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005218 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
5220 /* If the width already is the desired value, nothing to do. */
5221 if (curfrp->fr_width == width)
5222 return;
5223
5224 if (curfrp->fr_parent == NULL)
5225 /* topframe: can't change width */
5226 return;
5227
5228 if (curfrp->fr_parent->fr_layout == FR_COL)
5229 {
5230 /* Column of frames: Also need to resize frames above and below of
5231 * this one. First check for the minimal width of these. */
5232 w = frame_minwidth(curfrp->fr_parent, NULL);
5233 if (width < w)
5234 width = w;
5235 frame_setwidth(curfrp->fr_parent, width);
5236 }
5237 else
5238 {
5239 /*
5240 * Row of frames: try to change only frames in this row.
5241 *
5242 * Do this twice:
5243 * 1: compute room available, if it's not enough try resizing the
5244 * containing frame.
5245 * 2: compute the room available and adjust the width to it.
5246 */
5247 for (run = 1; run <= 2; ++run)
5248 {
5249 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005250 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5252 frp = frp->fr_next)
5253 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005254 if (frp != curfrp
5255 && frp->fr_win != NULL
5256 && frp->fr_win->w_p_wfw)
5257 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 room += frp->fr_width;
5259 if (frp != curfrp)
5260 room -= frame_minwidth(frp, NULL);
5261 }
5262
5263 if (width <= room)
5264 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005265 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
5267 if (width > room)
5268 width = room;
5269 break;
5270 }
5271 frame_setwidth(curfrp->fr_parent, width
5272 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5273 }
5274
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 /*
5276 * Compute the number of lines we will take from others frames (can be
5277 * negative!).
5278 */
5279 take = width - curfrp->fr_width;
5280
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005281 /* If there is not enough room, also reduce the width of a window
5282 * with 'winfixwidth' set. */
5283 if (width > room - room_reserved)
5284 room_reserved = room - width;
5285 /* If there is only a 'winfixwidth' window and making the
5286 * window smaller, need to make the other window narrower. */
5287 if (take < 0 && room - curfrp->fr_width < room_reserved)
5288 room_reserved = 0;
5289
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 /*
5291 * set the current frame to the new width
5292 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005293 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294
5295 /*
5296 * First take lines from the frames right of the current frame. If
5297 * that is not enough, takes lines from frames left of the current
5298 * frame.
5299 */
5300 for (run = 0; run < 2; ++run)
5301 {
5302 if (run == 0)
5303 frp = curfrp->fr_next; /* 1st run: start with next window */
5304 else
5305 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5306 while (frp != NULL && take != 0)
5307 {
5308 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005309 if (room_reserved > 0
5310 && frp->fr_win != NULL
5311 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005313 if (room_reserved >= frp->fr_width)
5314 room_reserved -= frp->fr_width;
5315 else
5316 {
5317 if (frp->fr_width - room_reserved > take)
5318 room_reserved = frp->fr_width - take;
5319 take -= frp->fr_width - room_reserved;
5320 frame_new_width(frp, room_reserved, FALSE, FALSE);
5321 room_reserved = 0;
5322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324 else
5325 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005326 if (frp->fr_width - take < w)
5327 {
5328 take -= frp->fr_width - w;
5329 frame_new_width(frp, w, FALSE, FALSE);
5330 }
5331 else
5332 {
5333 frame_new_width(frp, frp->fr_width - take,
5334 FALSE, FALSE);
5335 take = 0;
5336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338 if (run == 0)
5339 frp = frp->fr_next;
5340 else
5341 frp = frp->fr_prev;
5342 }
5343 }
5344 }
5345}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347/*
5348 * Check 'winminheight' for a valid value.
5349 */
5350 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005351win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
5353 int room;
5354 int first = TRUE;
5355 win_T *wp;
5356
5357 /* loop until there is a 'winminheight' that is possible */
5358 while (p_wmh > 0)
5359 {
5360 /* TODO: handle vertical splits */
5361 room = -p_wh;
5362 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5363 room += wp->w_height - p_wmh;
5364 if (room >= 0)
5365 break;
5366 --p_wmh;
5367 if (first)
5368 {
5369 EMSG(_(e_noroom));
5370 first = FALSE;
5371 }
5372 }
5373}
5374
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005375#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377/*
5378 * Status line of dragwin is dragged "offset" lines down (negative is up).
5379 */
5380 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005381win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 frame_T *curfr;
5384 frame_T *fr;
5385 int room;
5386 int row;
5387 int up; /* if TRUE, drag status line up, otherwise down */
5388 int n;
5389
5390 fr = dragwin->w_frame;
5391 curfr = fr;
5392 if (fr != topframe) /* more than one window */
5393 {
5394 fr = fr->fr_parent;
5395 /* When the parent frame is not a column of frames, its parent should
5396 * be. */
5397 if (fr->fr_layout != FR_COL)
5398 {
5399 curfr = fr;
5400 if (fr != topframe) /* only a row of windows, may drag statusline */
5401 fr = fr->fr_parent;
5402 }
5403 }
5404
5405 /* If this is the last frame in a column, may want to resize the parent
5406 * frame instead (go two up to skip a row of frames). */
5407 while (curfr != topframe && curfr->fr_next == NULL)
5408 {
5409 if (fr != topframe)
5410 fr = fr->fr_parent;
5411 curfr = fr;
5412 if (fr != topframe)
5413 fr = fr->fr_parent;
5414 }
5415
5416 if (offset < 0) /* drag up */
5417 {
5418 up = TRUE;
5419 offset = -offset;
5420 /* sum up the room of the current frame and above it */
5421 if (fr == curfr)
5422 {
5423 /* only one window */
5424 room = fr->fr_height - frame_minheight(fr, NULL);
5425 }
5426 else
5427 {
5428 room = 0;
5429 for (fr = fr->fr_child; ; fr = fr->fr_next)
5430 {
5431 room += fr->fr_height - frame_minheight(fr, NULL);
5432 if (fr == curfr)
5433 break;
5434 }
5435 }
5436 fr = curfr->fr_next; /* put fr at frame that grows */
5437 }
5438 else /* drag down */
5439 {
5440 up = FALSE;
5441 /*
5442 * Only dragging the last status line can reduce p_ch.
5443 */
5444 room = Rows - cmdline_row;
5445 if (curfr->fr_next == NULL)
5446 room -= 1;
5447 else
5448 room -= p_ch;
5449 if (room < 0)
5450 room = 0;
5451 /* sum up the room of frames below of the current one */
5452 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5453 room += fr->fr_height - frame_minheight(fr, NULL);
5454 fr = curfr; /* put fr at window that grows */
5455 }
5456
5457 if (room < offset) /* Not enough room */
5458 offset = room; /* Move as far as we can */
5459 if (offset <= 0)
5460 return;
5461
5462 /*
5463 * Grow frame fr by "offset" lines.
5464 * Doesn't happen when dragging the last status line up.
5465 */
5466 if (fr != NULL)
5467 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5468
5469 if (up)
5470 fr = curfr; /* current frame gets smaller */
5471 else
5472 fr = curfr->fr_next; /* next frame gets smaller */
5473
5474 /*
5475 * Now make the other frames smaller.
5476 */
5477 while (fr != NULL && offset > 0)
5478 {
5479 n = frame_minheight(fr, NULL);
5480 if (fr->fr_height - offset <= n)
5481 {
5482 offset -= fr->fr_height - n;
5483 frame_new_height(fr, n, !up, FALSE);
5484 }
5485 else
5486 {
5487 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5488 break;
5489 }
5490 if (up)
5491 fr = fr->fr_prev;
5492 else
5493 fr = fr->fr_next;
5494 }
5495 row = win_comp_pos();
5496 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5497 cmdline_row = row;
5498 p_ch = Rows - cmdline_row;
5499 if (p_ch < 1)
5500 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005501 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005502 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 showmode();
5504}
5505
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506/*
5507 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5508 */
5509 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005510win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511{
5512 frame_T *curfr;
5513 frame_T *fr;
5514 int room;
5515 int left; /* if TRUE, drag separator line left, otherwise right */
5516 int n;
5517
5518 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005519 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 return;
5521 curfr = fr;
5522 fr = fr->fr_parent;
5523 /* When the parent frame is not a row of frames, its parent should be. */
5524 if (fr->fr_layout != FR_ROW)
5525 {
5526 if (fr == topframe) /* only a column of windows (cannot happen?) */
5527 return;
5528 curfr = fr;
5529 fr = fr->fr_parent;
5530 }
5531
5532 /* If this is the last frame in a row, may want to resize a parent
5533 * frame instead. */
5534 while (curfr->fr_next == NULL)
5535 {
5536 if (fr == topframe)
5537 break;
5538 curfr = fr;
5539 fr = fr->fr_parent;
5540 if (fr != topframe)
5541 {
5542 curfr = fr;
5543 fr = fr->fr_parent;
5544 }
5545 }
5546
5547 if (offset < 0) /* drag left */
5548 {
5549 left = TRUE;
5550 offset = -offset;
5551 /* sum up the room of the current frame and left of it */
5552 room = 0;
5553 for (fr = fr->fr_child; ; fr = fr->fr_next)
5554 {
5555 room += fr->fr_width - frame_minwidth(fr, NULL);
5556 if (fr == curfr)
5557 break;
5558 }
5559 fr = curfr->fr_next; /* put fr at frame that grows */
5560 }
5561 else /* drag right */
5562 {
5563 left = FALSE;
5564 /* sum up the room of frames right of the current one */
5565 room = 0;
5566 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5567 room += fr->fr_width - frame_minwidth(fr, NULL);
5568 fr = curfr; /* put fr at window that grows */
5569 }
5570
5571 if (room < offset) /* Not enough room */
5572 offset = room; /* Move as far as we can */
5573 if (offset <= 0) /* No room at all, quit. */
5574 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005575 if (fr == NULL)
5576 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577
5578 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005579 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
5581 /* shrink other frames: current and at the left or at the right */
5582 if (left)
5583 fr = curfr; /* current frame gets smaller */
5584 else
5585 fr = curfr->fr_next; /* next frame gets smaller */
5586
5587 while (fr != NULL && offset > 0)
5588 {
5589 n = frame_minwidth(fr, NULL);
5590 if (fr->fr_width - offset <= n)
5591 {
5592 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005593 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 else
5596 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005597 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 break;
5599 }
5600 if (left)
5601 fr = fr->fr_prev;
5602 else
5603 fr = fr->fr_next;
5604 }
5605 (void)win_comp_pos();
5606 redraw_all_later(NOT_VALID);
5607}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608#endif /* FEAT_MOUSE */
5609
5610#endif /* FEAT_WINDOWS */
5611
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005612#define FRACTION_MULT 16384L
5613
5614/*
5615 * Set wp->w_fraction for the current w_wrow and w_height.
5616 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005617 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005618set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005619{
5620 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005621 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005622}
5623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624/*
5625 * Set the height of a window.
5626 * This takes care of the things inside the window, not what happens to the
5627 * window position, the frame or to other windows.
5628 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005629 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005630win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 linenr_T lnum;
5633 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005634 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
5636 /* Don't want a negative height. Happens when splitting a tiny window.
5637 * Will equalize heights soon to fix it. */
5638 if (height < 0)
5639 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005640 if (wp->w_height == height)
5641 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005643 if (wp->w_height > 0)
5644 {
5645 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005646 /* w_wrow needs to be valid. When setting 'laststatus' this may
5647 * call win_new_height() recursively. */
5648 validate_cursor();
5649 if (wp->w_height != prev_height)
5650 return; /* Recursive call already changed the size, bail out here
5651 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005652 if (wp->w_wrow != wp->w_prev_fraction_row)
5653 set_fraction(wp);
5654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
5656 wp->w_height = height;
5657 wp->w_skipcol = 0;
5658
5659 /* Don't change w_topline when height is zero. Don't set w_topline when
5660 * 'scrollbind' is set and this isn't the current window. */
5661 if (height > 0
5662#ifdef FEAT_SCROLLBIND
5663 && (!wp->w_p_scb || wp == curwin)
5664#endif
5665 )
5666 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005667 /*
5668 * Find a value for w_topline that shows the cursor at the same
5669 * relative position in the window as before (more or less).
5670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 lnum = wp->w_cursor.lnum;
5672 if (lnum < 1) /* can happen when starting up */
5673 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005674 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5675 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5677 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005678
5679 if (sline >= 0)
5680 {
5681 /* Make sure the whole cursor line is visible, if possible. */
5682 int rows = plines_win(wp, lnum, FALSE);
5683
5684 if (sline > wp->w_height - rows)
5685 {
5686 sline = wp->w_height - rows;
5687 wp->w_wrow -= rows - line_size;
5688 }
5689 }
5690
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 if (sline < 0)
5692 {
5693 /*
5694 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005695 * Make cursor line the first line in the window. If not enough
5696 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 */
5698 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005699 if (wp->w_wrow >= wp->w_height
5700 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5701 {
5702 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5703 --wp->w_wrow;
5704 while (wp->w_wrow >= wp->w_height)
5705 {
5706 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5707 + win_col_off2(wp);
5708 --wp->w_wrow;
5709 }
5710 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005711 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005713 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005715 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717#ifdef FEAT_FOLDING
5718 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5719 if (lnum == 1)
5720 {
5721 /* first line in buffer is folded */
5722 line_size = 1;
5723 --sline;
5724 break;
5725 }
5726#endif
5727 --lnum;
5728#ifdef FEAT_DIFF
5729 if (lnum == wp->w_topline)
5730 line_size = plines_win_nofill(wp, lnum, TRUE)
5731 + wp->w_topfill;
5732 else
5733#endif
5734 line_size = plines_win(wp, lnum, TRUE);
5735 sline -= line_size;
5736 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005737
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 if (sline < 0)
5739 {
5740 /*
5741 * Line we want at top would go off top of screen. Use next
5742 * line instead.
5743 */
5744#ifdef FEAT_FOLDING
5745 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5746#endif
5747 lnum++;
5748 wp->w_wrow -= line_size + sline;
5749 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005750 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 {
5752 /* First line of file reached, use that as topline. */
5753 lnum = 1;
5754 wp->w_wrow -= sline;
5755 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005756
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005757 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760
5761 if (wp == curwin)
5762 {
5763 if (p_so)
5764 update_topline();
5765 curs_columns(FALSE); /* validate w_wrow */
5766 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005767 if (prev_height > 0)
5768 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005771 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772#ifdef FEAT_WINDOWS
5773 wp->w_redr_status = TRUE;
5774#endif
5775 invalidate_botline_win(wp);
5776}
5777
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005778#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779/*
5780 * Set the width of a window.
5781 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005782 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005783win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785 wp->w_width = width;
5786 wp->w_lines_valid = 0;
5787 changed_line_abv_curs_win(wp);
5788 invalidate_botline_win(wp);
5789 if (wp == curwin)
5790 {
5791 update_topline();
5792 curs_columns(TRUE); /* validate w_wrow */
5793 }
5794 redraw_win_later(wp, NOT_VALID);
5795 wp->w_redr_status = TRUE;
5796}
5797#endif
5798
5799 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005800win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801{
5802 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5803 if (wp->w_p_scr == 0)
5804 wp->w_p_scr = 1;
5805}
5806
5807/*
5808 * command_height: called whenever p_ch has been changed
5809 */
5810 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005811command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813#ifdef FEAT_WINDOWS
5814 int h;
5815 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005816 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005818 /* Use the value of p_ch that we remembered. This is needed for when the
5819 * GUI starts up, we can't be sure in what order things happen. And when
5820 * p_ch was changed in another tab page. */
5821 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 /* Find bottom frame with width of screen. */
5824 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5826 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827
5828 /* Avoid changing the height of a window with 'winfixheight' set. */
5829 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5830 && frp->fr_win->w_p_wfh)
5831 frp = frp->fr_prev;
5832
5833 if (starting != NO_SCREEN)
5834 {
5835 cmdline_row = Rows - p_ch;
5836
5837 if (p_ch > old_p_ch) /* p_ch got bigger */
5838 {
5839 while (p_ch > old_p_ch)
5840 {
5841 if (frp == NULL)
5842 {
5843 EMSG(_(e_noroom));
5844 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005845 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 cmdline_row = Rows - p_ch;
5847 break;
5848 }
5849 h = frp->fr_height - frame_minheight(frp, NULL);
5850 if (h > p_ch - old_p_ch)
5851 h = p_ch - old_p_ch;
5852 old_p_ch += h;
5853 frame_add_height(frp, -h);
5854 frp = frp->fr_prev;
5855 }
5856
5857 /* Recompute window positions. */
5858 (void)win_comp_pos();
5859
5860 /* clear the lines added to cmdline */
5861 if (full_screen)
5862 screen_fill((int)(cmdline_row), (int)Rows, 0,
5863 (int)Columns, ' ', ' ', 0);
5864 msg_row = cmdline_row;
5865 redraw_cmdline = TRUE;
5866 return;
5867 }
5868
5869 if (msg_row < cmdline_row)
5870 msg_row = cmdline_row;
5871 redraw_cmdline = TRUE;
5872 }
5873 frame_add_height(frp, (int)(old_p_ch - p_ch));
5874
5875 /* Recompute window positions. */
5876 if (frp != lastwin->w_frame)
5877 (void)win_comp_pos();
5878#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005880 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881#endif
5882}
5883
5884#if defined(FEAT_WINDOWS) || defined(PROTO)
5885/*
5886 * Resize frame "frp" to be "n" lines higher (negative for less high).
5887 * Also resize the frames it is contained in.
5888 */
5889 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005890frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5893 for (;;)
5894 {
5895 frp = frp->fr_parent;
5896 if (frp == NULL)
5897 break;
5898 frp->fr_height += n;
5899 }
5900}
5901
5902/*
5903 * Add or remove a status line for the bottom window(s), according to the
5904 * value of 'laststatus'.
5905 */
5906 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005907last_status(
5908 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 /* Don't make a difference between horizontal or vertical split. */
5911 last_status_rec(topframe, (p_ls == 2
5912 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5913}
5914
5915 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005916last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917{
5918 frame_T *fp;
5919 win_T *wp;
5920
5921 if (fr->fr_layout == FR_LEAF)
5922 {
5923 wp = fr->fr_win;
5924 if (wp->w_status_height != 0 && !statusline)
5925 {
5926 /* remove status line */
5927 win_new_height(wp, wp->w_height + 1);
5928 wp->w_status_height = 0;
5929 comp_col();
5930 }
5931 else if (wp->w_status_height == 0 && statusline)
5932 {
5933 /* Find a frame to take a line from. */
5934 fp = fr;
5935 while (fp->fr_height <= frame_minheight(fp, NULL))
5936 {
5937 if (fp == topframe)
5938 {
5939 EMSG(_(e_noroom));
5940 return;
5941 }
5942 /* In a column of frames: go to frame above. If already at
5943 * the top or in a row of frames: go to parent. */
5944 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5945 fp = fp->fr_prev;
5946 else
5947 fp = fp->fr_parent;
5948 }
5949 wp->w_status_height = 1;
5950 if (fp != fr)
5951 {
5952 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5953 frame_fix_height(wp);
5954 (void)win_comp_pos();
5955 }
5956 else
5957 win_new_height(wp, wp->w_height - 1);
5958 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005959 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 }
5961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 else if (fr->fr_layout == FR_ROW)
5963 {
5964 /* vertically split windows, set status line for each one */
5965 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5966 last_status_rec(fp, statusline);
5967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 else
5969 {
5970 /* horizontally split window, set status line for last one */
5971 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5972 ;
5973 last_status_rec(fp, statusline);
5974 }
5975}
5976
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005977/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005978 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005979 */
5980 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005981tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005982{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005983#ifdef FEAT_GUI_TABLINE
5984 /* When the GUI has the tabline then this always returns zero. */
5985 if (gui_use_tabline())
5986 return 0;
5987#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005988 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005989 {
5990 case 0: return 0;
5991 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
5992 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993 return 1;
5994}
5995
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996#endif /* FEAT_WINDOWS */
5997
5998#if defined(FEAT_SEARCHPATH) || defined(PROTO)
5999/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006000 * Get the file name at the cursor.
6001 * If Visual mode is active, use the selected text if it's in one line.
6002 * Returns the name in allocated memory, NULL for failure.
6003 */
6004 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006005grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006006{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006007 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6008
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006009 if (VIsual_active)
6010 {
6011 int len;
6012 char_u *ptr;
6013
6014 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6015 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006016 return find_file_name_in_path(ptr, len, options,
6017 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006018 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006019 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006020
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006021}
6022
6023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 * Return the file name under or after the cursor.
6025 *
6026 * The 'path' option is searched if the file name is not absolute.
6027 * The string returned has been alloc'ed and should be freed by the caller.
6028 * NULL is returned if the file name or file is not found.
6029 *
6030 * options:
6031 * FNAME_MESS give error messages
6032 * FNAME_EXP expand to path
6033 * FNAME_HYP check for hypertext link
6034 * FNAME_INCL apply "includeexpr"
6035 */
6036 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006037file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038{
6039 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006040 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6041 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042}
6043
6044/*
6045 * Return the name of the file under or after ptr[col].
6046 * Otherwise like file_name_at_cursor().
6047 */
6048 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006049file_name_in_line(
6050 char_u *line,
6051 int col,
6052 int options,
6053 long count,
6054 char_u *rel_fname, /* file we are searching relative to */
6055 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056{
6057 char_u *ptr;
6058 int len;
6059
6060 /*
6061 * search forward for what could be the start of a file name
6062 */
6063 ptr = line + col;
6064 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006065 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 if (*ptr == NUL) /* nothing found */
6067 {
6068 if (options & FNAME_MESS)
6069 EMSG(_("E446: No file name under cursor"));
6070 return NULL;
6071 }
6072
6073 /*
6074 * Search backward for first char of the file name.
6075 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6076 */
6077 while (ptr > line)
6078 {
6079#ifdef FEAT_MBYTE
6080 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6081 ptr -= len + 1;
6082 else
6083#endif
6084 if (vim_isfilec(ptr[-1])
6085 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6086 --ptr;
6087 else
6088 break;
6089 }
6090
6091 /*
6092 * Search forward for the last char of the file name.
6093 * Also allow "://" when ':' is not in 'isfname'.
6094 */
6095 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006096 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006098 {
6099 if (ptr[len] == '\\')
6100 /* Skip over the "\" in "\ ". */
6101 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102#ifdef FEAT_MBYTE
6103 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006104 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 else
6106#endif
6107 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
6110 /*
6111 * If there is trailing punctuation, remove it.
6112 * But don't remove "..", could be a directory name.
6113 */
6114 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6115 && ptr[len - 2] != '.')
6116 --len;
6117
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006118 if (file_lnum != NULL)
6119 {
6120 char_u *p;
6121
6122 /* Get the number after the file name and a separator character */
6123 p = ptr + len;
6124 p = skipwhite(p);
6125 if (*p != NUL)
6126 {
6127 if (!isdigit(*p))
6128 ++p; /* skip the separator */
6129 p = skipwhite(p);
6130 if (isdigit(*p))
6131 *file_lnum = (int)getdigits(&p);
6132 }
6133 }
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6136}
6137
6138# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006139static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006142eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143{
6144 char_u *res;
6145
6146 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006147 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006148 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 set_vim_var_string(VV_FNAME, NULL, 0);
6150 return res;
6151}
6152#endif
6153
6154/*
6155 * Return the name of the file ptr[len] in 'path'.
6156 * Otherwise like file_name_at_cursor().
6157 */
6158 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006159find_file_name_in_path(
6160 char_u *ptr,
6161 int len,
6162 int options,
6163 long count,
6164 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
6166 char_u *file_name;
6167 int c;
6168# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6169 char_u *tofree = NULL;
6170
6171 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6172 {
6173 tofree = eval_includeexpr(ptr, len);
6174 if (tofree != NULL)
6175 {
6176 ptr = tofree;
6177 len = (int)STRLEN(ptr);
6178 }
6179 }
6180# endif
6181
6182 if (options & FNAME_EXP)
6183 {
6184 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6185 TRUE, rel_fname);
6186
6187# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6188 /*
6189 * If the file could not be found in a normal way, try applying
6190 * 'includeexpr' (unless done already).
6191 */
6192 if (file_name == NULL
6193 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6194 {
6195 tofree = eval_includeexpr(ptr, len);
6196 if (tofree != NULL)
6197 {
6198 ptr = tofree;
6199 len = (int)STRLEN(ptr);
6200 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6201 TRUE, rel_fname);
6202 }
6203 }
6204# endif
6205 if (file_name == NULL && (options & FNAME_MESS))
6206 {
6207 c = ptr[len];
6208 ptr[len] = NUL;
6209 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6210 ptr[len] = c;
6211 }
6212
6213 /* Repeat finding the file "count" times. This matters when it
6214 * appears several times in the path. */
6215 while (file_name != NULL && --count > 0)
6216 {
6217 vim_free(file_name);
6218 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6219 }
6220 }
6221 else
6222 file_name = vim_strnsave(ptr, len);
6223
6224# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6225 vim_free(tofree);
6226# endif
6227
6228 return file_name;
6229}
6230#endif /* FEAT_SEARCHPATH */
6231
6232/*
6233 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6234 * Also check for ":\\", which MS Internet Explorer accepts, return
6235 * URL_BACKSLASH.
6236 */
6237 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006238path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
6240 if (STRNCMP(p, "://", (size_t)3) == 0)
6241 return URL_SLASH;
6242 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6243 return URL_BACKSLASH;
6244 return 0;
6245}
6246
6247/*
6248 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6249 * Return URL_BACKSLASH for "name:\\".
6250 * Return zero otherwise.
6251 */
6252 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006253path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254{
6255 char_u *p;
6256
6257 for (p = fname; isalpha(*p); ++p)
6258 ;
6259 return path_is_url(p);
6260}
6261
6262/*
6263 * Return TRUE if "name" is a full (absolute) path name or URL.
6264 */
6265 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006266vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267{
6268 return (path_with_url(name) != 0 || mch_isFullName(name));
6269}
6270
6271/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006272 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 *
6274 * return FAIL for failure, OK otherwise
6275 */
6276 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006277vim_FullName(
6278 char_u *fname,
6279 char_u *buf,
6280 int len,
6281 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 int retval = OK;
6284 int url;
6285
6286 *buf = NUL;
6287 if (fname == NULL)
6288 return FAIL;
6289
6290 url = path_with_url(fname);
6291 if (!url)
6292 retval = mch_FullName(fname, buf, len, force);
6293 if (url || retval == FAIL)
6294 {
6295 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006296 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01006298#if defined(MACOS_CLASSIC) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 slash_adjust(buf);
6300#endif
6301 return retval;
6302}
6303
6304/*
6305 * Return the minimal number of rows that is needed on the screen to display
6306 * the current number of windows.
6307 */
6308 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006309min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006312#ifdef FEAT_WINDOWS
6313 tabpage_T *tp;
6314 int n;
6315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316
6317 if (firstwin == NULL) /* not initialized yet */
6318 return MIN_LINES;
6319
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006321 total = 0;
6322 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6323 {
6324 n = frame_minheight(tp->tp_topframe, NULL);
6325 if (total < n)
6326 total = n;
6327 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006328 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006330 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006332 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 return total;
6334}
6335
6336/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006337 * Return TRUE if there is only one window (in the current tab page), not
6338 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006339 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 */
6341 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006342only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344#ifdef FEAT_WINDOWS
6345 int count = 0;
6346 win_T *wp;
6347
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006348 /* If there is another tab page there always is another window. */
6349 if (first_tabpage->tp_next != NULL)
6350 return FALSE;
6351
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006353 if (wp->w_buffer != NULL
6354 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355# ifdef FEAT_QUICKFIX
6356 || wp->w_p_pvw
6357# endif
6358 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006359# ifdef FEAT_AUTOCMD
6360 && wp != aucmd_win
6361# endif
6362 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 ++count;
6364 return (count <= 1);
6365#else
6366 return TRUE;
6367#endif
6368}
6369
6370#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6371/*
6372 * Correct the cursor line number in other windows. Used after changing the
6373 * current buffer, and before applying autocommands.
6374 * When "do_curwin" is TRUE, also check current window.
6375 */
6376 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006377check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 win_T *wp;
6380
6381#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006382 tabpage_T *tp;
6383
6384 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6386#else
6387 wp = curwin;
6388 if (do_curwin)
6389#endif
6390 {
6391 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6392 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6393 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6394 wp->w_topline = curbuf->b_ml.ml_line_count;
6395 }
6396}
6397#endif
6398
6399#if defined(FEAT_WINDOWS) || defined(PROTO)
6400
6401/*
6402 * A snapshot of the window sizes, to restore them after closing the help
6403 * window.
6404 * Only these fields are used:
6405 * fr_layout
6406 * fr_width
6407 * fr_height
6408 * fr_next
6409 * fr_child
6410 * fr_win (only valid for the old curwin, NULL otherwise)
6411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412
6413/*
6414 * Create a snapshot of the current frame sizes.
6415 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006416 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006417make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006419 clear_snapshot(curtab, idx);
6420 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421}
6422
6423 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006424make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6427 if (*frp == NULL)
6428 return;
6429 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 (*frp)->fr_height = fr->fr_height;
6432 if (fr->fr_next != NULL)
6433 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6434 if (fr->fr_child != NULL)
6435 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6436 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6437 (*frp)->fr_win = curwin;
6438}
6439
6440/*
6441 * Remove any existing snapshot.
6442 */
6443 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006444clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006446 clear_snapshot_rec(tp->tp_snapshot[idx]);
6447 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448}
6449
6450 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006451clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 if (fr != NULL)
6454 {
6455 clear_snapshot_rec(fr->fr_next);
6456 clear_snapshot_rec(fr->fr_child);
6457 vim_free(fr);
6458 }
6459}
6460
6461/*
6462 * Restore a previously created snapshot, if there is any.
6463 * This is only done if the screen size didn't change and the window layout is
6464 * still the same.
6465 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006466 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006467restore_snapshot(
6468 int idx,
6469 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 win_T *wp;
6472
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006473 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006474 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006475 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6476 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006478 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 win_comp_pos();
6480 if (wp != NULL && close_curwin)
6481 win_goto(wp);
6482 redraw_all_later(CLEAR);
6483 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006484 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485}
6486
6487/*
6488 * Check if frames "sn" and "fr" have the same layout, same following frames
6489 * and same children.
6490 */
6491 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006492check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 if (sn->fr_layout != fr->fr_layout
6495 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6496 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6497 || (sn->fr_next != NULL
6498 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6499 || (sn->fr_child != NULL
6500 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6501 return FAIL;
6502 return OK;
6503}
6504
6505/*
6506 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6507 * following frames and children.
6508 * Returns a pointer to the old current window, or NULL.
6509 */
6510 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006511restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512{
6513 win_T *wp = NULL;
6514 win_T *wp2;
6515
6516 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 if (fr->fr_layout == FR_LEAF)
6519 {
6520 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006521 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 wp = sn->fr_win;
6523 }
6524 if (sn->fr_next != NULL)
6525 {
6526 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6527 if (wp2 != NULL)
6528 wp = wp2;
6529 }
6530 if (sn->fr_child != NULL)
6531 {
6532 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6533 if (wp2 != NULL)
6534 wp = wp2;
6535 }
6536 return wp;
6537}
6538
6539#endif
6540
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006541#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6542 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006543/*
6544 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006545 * restore_win() MUST be called to undo, also when FAIL is returned.
6546 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006547 * When "no_display" is TRUE the display won't be affected, no redraw is
6548 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006549 * Returns FAIL if switching to "win" failed.
6550 */
6551 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006552switch_win(
6553 win_T **save_curwin UNUSED,
6554 tabpage_T **save_curtab UNUSED,
6555 win_T *win UNUSED,
6556 tabpage_T *tp UNUSED,
6557 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006558{
6559# ifdef FEAT_AUTOCMD
6560 block_autocmds();
6561# endif
6562# ifdef FEAT_WINDOWS
6563 *save_curwin = curwin;
6564 if (tp != NULL)
6565 {
6566 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006567 if (no_display)
6568 {
6569 curtab->tp_firstwin = firstwin;
6570 curtab->tp_lastwin = lastwin;
6571 curtab = tp;
6572 firstwin = curtab->tp_firstwin;
6573 lastwin = curtab->tp_lastwin;
6574 }
6575 else
6576 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006577 }
6578 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006579 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006580 curwin = win;
6581 curbuf = curwin->w_buffer;
6582# endif
6583 return OK;
6584}
6585
6586/*
6587 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006588 * When "no_display" is TRUE the display won't be affected, no redraw is
6589 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006590 */
6591 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006592restore_win(
6593 win_T *save_curwin UNUSED,
6594 tabpage_T *save_curtab UNUSED,
6595 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006596{
6597# ifdef FEAT_WINDOWS
6598 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006599 {
6600 if (no_display)
6601 {
6602 curtab->tp_firstwin = firstwin;
6603 curtab->tp_lastwin = lastwin;
6604 curtab = save_curtab;
6605 firstwin = curtab->tp_firstwin;
6606 lastwin = curtab->tp_lastwin;
6607 }
6608 else
6609 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6610 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006611 if (win_valid(save_curwin))
6612 {
6613 curwin = save_curwin;
6614 curbuf = curwin->w_buffer;
6615 }
6616# endif
6617# ifdef FEAT_AUTOCMD
6618 unblock_autocmds();
6619# endif
6620}
6621
6622/*
6623 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6624 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6625 */
6626 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006627switch_buffer(buf_T **save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006628{
6629# ifdef FEAT_AUTOCMD
6630 block_autocmds();
6631# endif
6632 *save_curbuf = curbuf;
6633 --curbuf->b_nwindows;
6634 curbuf = buf;
6635 curwin->w_buffer = buf;
6636 ++curbuf->b_nwindows;
6637}
6638
6639/*
6640 * Restore the current buffer after using switch_buffer().
6641 */
6642 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006643restore_buffer(buf_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006644{
6645# ifdef FEAT_AUTOCMD
6646 unblock_autocmds();
6647# endif
6648 /* Check for valid buffer, just in case. */
6649 if (buf_valid(save_curbuf))
6650 {
6651 --curbuf->b_nwindows;
6652 curwin->w_buffer = save_curbuf;
6653 curbuf = save_curbuf;
6654 ++curbuf->b_nwindows;
6655 }
6656}
6657#endif
6658
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006659#if (defined(FEAT_GUI) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660/*
6661 * Return TRUE if there is any vertically split window.
6662 */
6663 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006664win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 frame_T *fr;
6667
6668 if (topframe->fr_layout == FR_ROW)
6669 return TRUE;
6670
6671 if (topframe->fr_layout == FR_COL)
6672 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6673 if (fr->fr_layout == FR_ROW)
6674 return TRUE;
6675
6676 return FALSE;
6677}
6678#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006679
6680#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6681/*
6682 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006683 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006684 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6685 * If no particular ID is desired, -1 must be specified for 'id'.
6686 * Return ID of added match, -1 on failure.
6687 */
6688 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006689match_add(
6690 win_T *wp,
6691 char_u *grp,
6692 char_u *pat,
6693 int prio,
6694 int id,
6695 list_T *pos_list,
6696 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006697{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006698 matchitem_T *cur;
6699 matchitem_T *prev;
6700 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006701 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006702 regprog_T *regprog = NULL;
6703 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006704
Bram Moolenaarb3414592014-06-17 17:48:32 +02006705 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006706 return -1;
6707 if (id < -1 || id == 0)
6708 {
6709 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6710 return -1;
6711 }
6712 if (id != -1)
6713 {
6714 cur = wp->w_match_head;
6715 while (cur != NULL)
6716 {
6717 if (cur->id == id)
6718 {
6719 EMSGN("E801: ID already taken: %ld", id);
6720 return -1;
6721 }
6722 cur = cur->next;
6723 }
6724 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006725 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006726 {
6727 EMSG2(_(e_nogroup), grp);
6728 return -1;
6729 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006730 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006731 {
6732 EMSG2(_(e_invarg2), pat);
6733 return -1;
6734 }
6735
6736 /* Find available match ID. */
6737 while (id == -1)
6738 {
6739 cur = wp->w_match_head;
6740 while (cur != NULL && cur->id != wp->w_next_match_id)
6741 cur = cur->next;
6742 if (cur == NULL)
6743 id = wp->w_next_match_id;
6744 wp->w_next_match_id++;
6745 }
6746
6747 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006748 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006749 m->id = id;
6750 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006751 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006752 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006753 m->match.regprog = regprog;
6754 m->match.rmm_ic = FALSE;
6755 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006756# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006757 m->conceal_char = 0;
6758 if (conceal_char != NULL)
6759 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006760# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006761
Bram Moolenaarb3414592014-06-17 17:48:32 +02006762 /* Set up position matches */
6763 if (pos_list != NULL)
6764 {
6765 linenr_T toplnum = 0;
6766 linenr_T botlnum = 0;
6767 listitem_T *li;
6768 int i;
6769
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006770 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006771 i++, li = li->li_next)
6772 {
6773 linenr_T lnum = 0;
6774 colnr_T col = 0;
6775 int len = 1;
6776 list_T *subl;
6777 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006778 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006779
Bram Moolenaarb3414592014-06-17 17:48:32 +02006780 if (li->li_tv.v_type == VAR_LIST)
6781 {
6782 subl = li->li_tv.vval.v_list;
6783 if (subl == NULL)
6784 goto fail;
6785 subli = subl->lv_first;
6786 if (subli == NULL)
6787 goto fail;
6788 lnum = get_tv_number_chk(&subli->li_tv, &error);
6789 if (error == TRUE)
6790 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006791 if (lnum == 0)
6792 {
6793 --i;
6794 continue;
6795 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006796 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006797 subli = subli->li_next;
6798 if (subli != NULL)
6799 {
6800 col = get_tv_number_chk(&subli->li_tv, &error);
6801 if (error == TRUE)
6802 goto fail;
6803 subli = subli->li_next;
6804 if (subli != NULL)
6805 {
6806 len = get_tv_number_chk(&subli->li_tv, &error);
6807 if (error == TRUE)
6808 goto fail;
6809 }
6810 }
6811 m->pos.pos[i].col = col;
6812 m->pos.pos[i].len = len;
6813 }
6814 else if (li->li_tv.v_type == VAR_NUMBER)
6815 {
6816 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006817 {
6818 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006819 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006820 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006821 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6822 m->pos.pos[i].col = 0;
6823 m->pos.pos[i].len = 0;
6824 }
6825 else
6826 {
6827 EMSG(_("List or number required"));
6828 goto fail;
6829 }
6830 if (toplnum == 0 || lnum < toplnum)
6831 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006832 if (botlnum == 0 || lnum >= botlnum)
6833 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006834 }
6835
6836 /* Calculate top and bottom lines for redrawing area */
6837 if (toplnum != 0)
6838 {
6839 if (wp->w_buffer->b_mod_set)
6840 {
6841 if (wp->w_buffer->b_mod_top > toplnum)
6842 wp->w_buffer->b_mod_top = toplnum;
6843 if (wp->w_buffer->b_mod_bot < botlnum)
6844 wp->w_buffer->b_mod_bot = botlnum;
6845 }
6846 else
6847 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006848 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006849 wp->w_buffer->b_mod_top = toplnum;
6850 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006851 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006852 }
6853 m->pos.toplnum = toplnum;
6854 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006855 rtype = VALID;
6856 }
6857 }
6858
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006859 /* Insert new match. The match list is in ascending order with regard to
6860 * the match priorities. */
6861 cur = wp->w_match_head;
6862 prev = cur;
6863 while (cur != NULL && prio >= cur->priority)
6864 {
6865 prev = cur;
6866 cur = cur->next;
6867 }
6868 if (cur == prev)
6869 wp->w_match_head = m;
6870 else
6871 prev->next = m;
6872 m->next = cur;
6873
Bram Moolenaarb3414592014-06-17 17:48:32 +02006874 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006875 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006876
6877fail:
6878 vim_free(m);
6879 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006880}
6881
6882/*
6883 * Delete match with ID 'id' in the match list of window 'wp'.
6884 * Print error messages if 'perr' is TRUE.
6885 */
6886 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006887match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006888{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006889 matchitem_T *cur = wp->w_match_head;
6890 matchitem_T *prev = cur;
6891 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006892
6893 if (id < 1)
6894 {
6895 if (perr == TRUE)
6896 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6897 id);
6898 return -1;
6899 }
6900 while (cur != NULL && cur->id != id)
6901 {
6902 prev = cur;
6903 cur = cur->next;
6904 }
6905 if (cur == NULL)
6906 {
6907 if (perr == TRUE)
6908 EMSGN("E803: ID not found: %ld", id);
6909 return -1;
6910 }
6911 if (cur == prev)
6912 wp->w_match_head = cur->next;
6913 else
6914 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006915 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006916 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006917 if (cur->pos.toplnum != 0)
6918 {
6919 if (wp->w_buffer->b_mod_set)
6920 {
6921 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6922 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6923 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6924 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6925 }
6926 else
6927 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006928 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006929 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6930 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006931 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006932 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006933 rtype = VALID;
6934 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006935 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006936 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006937 return 0;
6938}
6939
6940/*
6941 * Delete all matches in the match list of window 'wp'.
6942 */
6943 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006944clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006945{
6946 matchitem_T *m;
6947
6948 while (wp->w_match_head != NULL)
6949 {
6950 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006951 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006952 vim_free(wp->w_match_head->pattern);
6953 vim_free(wp->w_match_head);
6954 wp->w_match_head = m;
6955 }
6956 redraw_later(SOME_VALID);
6957}
6958
6959/*
6960 * Get match from ID 'id' in window 'wp'.
6961 * Return NULL if match not found.
6962 */
6963 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006964get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006965{
6966 matchitem_T *cur = wp->w_match_head;
6967
6968 while (cur != NULL && cur->id != id)
6969 cur = cur->next;
6970 return cur;
6971}
6972#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006973
6974#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6975 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006976get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006977{
6978 int i = 1;
6979 win_T *w;
6980
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006981 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006982 ++i;
6983
6984 if (w == NULL)
6985 return 0;
6986 else
6987 return i;
6988}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006989
6990 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006991get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006992{
6993 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006994# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006995 tabpage_T *t;
6996
6997 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6998 ++i;
6999
7000 if (t == NULL)
7001 return 0;
7002 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007003# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007004 return i;
7005}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007006#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007007
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007008#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007009/*
7010 * Return TRUE if "topfrp" and its children are at the right height.
7011 */
7012 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007013frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007014{
7015 frame_T *frp;
7016
7017 if (topfrp->fr_height != height)
7018 return FALSE;
7019
7020 if (topfrp->fr_layout == FR_ROW)
7021 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7022 if (frp->fr_height != height)
7023 return FALSE;
7024
7025 return TRUE;
7026}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007027#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007028
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007029#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007030/*
7031 * Return TRUE if "topfrp" and its children are at the right width.
7032 */
7033 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007034frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007035{
7036 frame_T *frp;
7037
7038 if (topfrp->fr_width != width)
7039 return FALSE;
7040
7041 if (topfrp->fr_layout == FR_COL)
7042 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7043 if (frp->fr_width != width)
7044 return FALSE;
7045
7046 return TRUE;
7047}
7048#endif
7049
Bram Moolenaar86edef62016-03-13 18:07:30 +01007050#if defined(FEAT_EVAL) || defined(PROTO)
7051 int
7052win_getid(typval_T *argvars)
7053{
7054 int winnr;
7055 win_T *wp;
7056
7057 if (argvars[0].v_type == VAR_UNKNOWN)
7058 return curwin->w_id;
7059 winnr = get_tv_number(&argvars[0]);
7060 if (winnr > 0)
7061 {
7062 if (argvars[1].v_type == VAR_UNKNOWN)
7063 wp = firstwin;
7064 else
7065 {
7066 tabpage_T *tp;
7067 int tabnr = get_tv_number(&argvars[1]);
7068
7069 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7070 if (--tabnr == 0)
7071 break;
7072 if (tp == NULL)
7073 return -1;
7074 wp = tp->tp_firstwin;
7075 }
7076 for ( ; wp != NULL; wp = wp->w_next)
7077 if (--winnr == 0)
7078 return wp->w_id;
7079 }
7080 return 0;
7081}
7082
7083 int
7084win_gotoid(typval_T *argvars)
7085{
7086 win_T *wp;
7087 tabpage_T *tp;
7088 int id = get_tv_number(&argvars[0]);
7089
7090 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7091 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7092 wp != NULL; wp = wp->w_next)
7093 if (wp->w_id == id)
7094 {
7095 goto_tabpage_win(tp, wp);
7096 return 1;
7097 }
7098 return 0;
7099}
7100
7101 void
7102win_id2tabwin(typval_T *argvars, list_T *list)
7103{
7104 win_T *wp;
7105 tabpage_T *tp;
7106 int winnr = 1;
7107 int tabnr = 1;
7108 int id = get_tv_number(&argvars[0]);
7109
7110 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7111 {
7112 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7113 wp != NULL; wp = wp->w_next)
7114 {
7115 if (wp->w_id == id)
7116 {
7117 list_append_number(list, tabnr);
7118 list_append_number(list, winnr);
7119 return;
7120 }
7121 ++winnr;
7122 }
7123 ++tabnr;
7124 winnr = 1;
7125 }
7126 list_append_number(list, 0);
7127 list_append_number(list, 0);
7128}
7129
7130 int
7131win_id2win(typval_T *argvars)
7132{
7133 win_T *wp;
7134 int nr = 1;
7135 int id = get_tv_number(&argvars[0]);
7136
7137 for (wp = firstwin; wp != NULL; wp = wp->w_next)
7138 {
7139 if (wp->w_id == id)
7140 return nr;
7141 ++nr;
7142 }
7143 return 0;
7144}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007145
7146 void
7147win_findbuf(typval_T *argvars, list_T *list)
7148{
7149 win_T *wp;
7150 tabpage_T *tp;
7151 int bufnr = get_tv_number(&argvars[0]);
7152
7153 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7154 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7155 wp != NULL; wp = wp->w_next)
7156 if (wp->w_buffer->b_fnum == bufnr)
7157 list_append_number(list, wp->w_id);
7158}
7159
Bram Moolenaar86edef62016-03-13 18:07:30 +01007160#endif