blob: 4fbe6ca58524cd36f9e72861806aea62f3ee9dc1 [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);
Bram Moolenaarc917da42016-07-19 22:31:36 +020048static void win_enter_ext(win_T *wp, int undo_sync, int no_curwin, int trigger_new_autocmds, int trigger_enter_autocmds, int trigger_leave_autocmds);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static 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 Moolenaar5d2ca042016-06-26 17:11:21 +0200478 tabpage_T *oldtab = curtab;
479 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000480# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 setpcmark();
484 if (win_split(0, 0) == OK)
485 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200486 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200487 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
488 ECMD_HIDE, NULL) == FAIL)
489 {
490 /* Failed to open the file, close the window
491 * opened for it. */
492 win_close(curwin, FALSE);
493 goto_tabpage_win(oldtab, oldwin);
494 }
495 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000496 {
497 curwin->w_cursor.lnum = lnum;
498 check_cursor_lnum();
499 beginline(BL_SOL | BL_FIX);
500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
502 vim_free(ptr);
503 }
504 break;
505#endif
506
507#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000508/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 * new window -- webb
510 */
511 case 'i': /* Go to any match */
512 case Ctrl_I:
513 type = FIND_ANY;
514 /* FALLTHROUGH */
515 case 'd': /* Go to definition, using 'define' */
516 case Ctrl_D:
517 CHECK_CMDWIN
518 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
519 break;
520 find_pattern_in_path(ptr, 0, len, TRUE,
521 Prenum == 0 ? TRUE : FALSE, type,
522 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
523 curwin->w_set_curswant = TRUE;
524 break;
525#endif
526
Bram Moolenaar05159a02005-02-26 23:04:13 +0000527 case K_KENTER:
528 case CAR:
529#if defined(FEAT_QUICKFIX)
530 /*
531 * In a quickfix window a <CR> jumps to the error under the
532 * cursor in a new window.
533 */
534 if (bt_quickfix(curbuf))
535 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000536 sprintf((char *)cbuf, "split +%ld%s",
537 (long)curwin->w_cursor.lnum,
538 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000539 do_cmdline_cmd(cbuf);
540 }
541#endif
542 break;
543
544
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545/* CTRL-W g extended commands */
546 case 'g':
547 case Ctrl_G:
548 CHECK_CMDWIN
549#ifdef USE_ON_FLY_SCROLL
550 dont_scroll = TRUE; /* disallow scrolling here */
551#endif
552 ++no_mapping;
553 ++allow_keys; /* no mapping for xchar, but allow key codes */
554 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000555 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 --no_mapping;
558 --allow_keys;
559#ifdef FEAT_CMDL_INFO
560 (void)add_to_showcmd(xchar);
561#endif
562 switch (xchar)
563 {
564#if defined(FEAT_QUICKFIX)
565 case '}':
566 xchar = Ctrl_RSB;
567 if (Prenum)
568 g_do_tagpreview = Prenum;
569 else
570 g_do_tagpreview = p_pvh;
571 /*FALLTHROUGH*/
572#endif
573 case ']':
574 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200575 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (Prenum)
577 postponed_split = Prenum;
578 else
579 postponed_split = -1;
580
581 /* Execute the command right here, required when
582 * "wincmd g}" was used in a function. */
583 do_nv_ident('g', xchar);
584 break;
585
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000586#ifdef FEAT_SEARCHPATH
587 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000588 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100589 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000590 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000591 goto wingotofile;
592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 default:
594 beep_flush();
595 break;
596 }
597 break;
598
599 default: beep_flush();
600 break;
601 }
602}
603
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100604/*
605 * Figure out the address type for ":wnncmd".
606 */
607 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100608get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100609{
610 switch (*arg)
611 {
612 case 'S':
613 case Ctrl_S:
614 case 's':
615 case Ctrl_N:
616 case 'n':
617 case 'j':
618 case Ctrl_J:
619 case 'k':
620 case Ctrl_K:
621 case 'T':
622 case Ctrl_R:
623 case 'r':
624 case 'R':
625 case 'K':
626 case 'J':
627 case '+':
628 case '-':
629 case Ctrl__:
630 case '_':
631 case '|':
632 case ']':
633 case Ctrl_RSB:
634 case 'g':
635 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100636 case Ctrl_V:
637 case 'v':
638 case 'h':
639 case Ctrl_H:
640 case 'l':
641 case Ctrl_L:
642 case 'H':
643 case 'L':
644 case '>':
645 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100646#if defined(FEAT_QUICKFIX)
647 case '}':
648#endif
649#ifdef FEAT_SEARCHPATH
650 case 'f':
651 case 'F':
652 case Ctrl_F:
653#endif
654#ifdef FEAT_FIND_ID
655 case 'i':
656 case Ctrl_I:
657 case 'd':
658 case Ctrl_D:
659#endif
660 /* window size or any count */
661 eap->addr_type = ADDR_LINES;
662 break;
663
664 case Ctrl_HAT:
665 case '^':
666 /* buffer number */
667 eap->addr_type = ADDR_BUFFERS;
668 break;
669
670 case Ctrl_Q:
671 case 'q':
672 case Ctrl_C:
673 case 'c':
674 case Ctrl_O:
675 case 'o':
676 case Ctrl_W:
677 case 'w':
678 case 'W':
679 case 'x':
680 case Ctrl_X:
681 /* window number */
682 eap->addr_type = ADDR_WINDOWS;
683 break;
684
685#if defined(FEAT_QUICKFIX)
686 case Ctrl_Z:
687 case 'z':
688 case 'P':
689#endif
690 case 't':
691 case Ctrl_T:
692 case 'b':
693 case Ctrl_B:
694 case 'p':
695 case Ctrl_P:
696 case '=':
697 case CAR:
698 /* no count */
699 eap->addr_type = 0;
700 break;
701 }
702}
703
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100704 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100705cmd_with_count(
706 char *cmd,
707 char_u *bufp,
708 size_t bufsize,
709 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100710{
711 size_t len = STRLEN(cmd);
712
713 STRCPY(bufp, cmd);
714 if (Prenum > 0)
715 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
716}
717
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718/*
719 * split the current window, implements CTRL-W s and :split
720 *
721 * "size" is the height or width for the new window, 0 to use half of current
722 * height or width.
723 *
724 * "flags":
725 * WSP_ROOM: require enough room for new window
726 * WSP_VERT: vertical split.
727 * WSP_TOP: open window at the top-left of the shell (help window).
728 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
729 * WSP_HELP: creating the help window, keep layout snapshot
730 *
731 * return FAIL for failure, OK otherwise
732 */
733 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100734win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000736 /* When the ":tab" modifier was used open a new tab page instead. */
737 if (may_open_tabpage() == OK)
738 return OK;
739
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 /* Add flags from ":vertical", ":topleft" and ":botright". */
741 flags |= cmdmod.split;
742 if ((flags & WSP_TOP) && (flags & WSP_BOT))
743 {
744 EMSG(_("E442: Can't split topleft and botright at the same time"));
745 return FAIL;
746 }
747
748 /* When creating the help window make a snapshot of the window layout.
749 * Otherwise clear the snapshot, it's now invalid. */
750 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000751 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000753 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755 return win_split_ins(size, flags, NULL, 0);
756}
757
758/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100759 * When "new_wp" is NULL: split the current window in two.
760 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * top/left/right/bottom.
762 * return FAIL for failure, OK otherwise
763 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000764 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100765win_split_ins(
766 int size,
767 int flags,
768 win_T *new_wp,
769 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100771 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 win_T *oldwin;
773 int new_size = size;
774 int i;
775 int need_status = 0;
776 int do_equal = FALSE;
777 int needed;
778 int available;
779 int oldwin_height = 0;
780 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200781 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200783 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200784 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 if (flags & WSP_TOP)
787 oldwin = firstwin;
788 else if (flags & WSP_BOT)
789 oldwin = lastwin;
790 else
791 oldwin = curwin;
792
793 /* add a status line when p_ls == 1 and splitting the first window */
794 if (lastwin == firstwin && p_ls == 1 && oldwin->w_status_height == 0)
795 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100796 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
798 EMSG(_(e_noroom));
799 return FAIL;
800 }
801 need_status = STATUS_HEIGHT;
802 }
803
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000804#ifdef FEAT_GUI
805 /* May be needed for the scrollbars that are going to change. */
806 if (gui.in_use)
807 out_flush();
808#endif
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (flags & WSP_VERT)
811 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200812 int wmw1;
813 int minwidth;
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817 /*
818 * Check if we are able to split the current window and compute its
819 * width.
820 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200821 /* Current window requires at least 1 space. */
822 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
823 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200825 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200826 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200828 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200830 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200832 else if (p_ea)
833 {
834 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
835 prevfrp = oldwin->w_frame;
836 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
837 frp = frp->fr_parent)
838 {
839 if (frp->fr_layout == FR_ROW)
840 for (frp2 = frp->fr_child; frp2 != NULL;
841 frp2 = frp2->fr_next)
842 if (frp2 != prevfrp)
843 minwidth += frame_minwidth(frp2, NOWIN);
844 prevfrp = frp;
845 }
846 available = topframe->fr_width;
847 needed += minwidth;
848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200850 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200851 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
852 available = oldwin->w_frame->fr_width;
853 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200854 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100855 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
857 EMSG(_(e_noroom));
858 return FAIL;
859 }
860 if (new_size == 0)
861 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200862 if (new_size > available - minwidth - 1)
863 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200864 if (new_size < wmw1)
865 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
867 /* if it doesn't fit in the current window, need win_equal() */
868 if (oldwin->w_width - new_size - 1 < p_wmw)
869 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000870
871 /* We don't like to take lines for the new window from a
872 * 'winfixwidth' window. Take them from a window to the left or right
873 * instead, if possible. */
874 if (oldwin->w_p_wfw)
875 win_setwidth_win(oldwin->w_width + new_size, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000876
877 /* Only make all windows the same width if one of them (except oldwin)
878 * is wider than one of the split windows. */
879 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
880 && oldwin->w_frame->fr_parent != NULL)
881 {
882 frp = oldwin->w_frame->fr_parent->fr_child;
883 while (frp != NULL)
884 {
885 if (frp->fr_win != oldwin && frp->fr_win != NULL
886 && (frp->fr_win->w_width > new_size
887 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100888 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000889 {
890 do_equal = TRUE;
891 break;
892 }
893 frp = frp->fr_next;
894 }
895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 }
897 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
899 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900
901 /*
902 * Check if we are able to split the current window and compute its
903 * height.
904 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200905 /* Current window requires at least 1 space. */
906 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
907 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200909 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200910 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200912 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200914 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200916 else if (p_ea)
917 {
918 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
919 prevfrp = oldwin->w_frame;
920 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
921 frp = frp->fr_parent)
922 {
923 if (frp->fr_layout == FR_COL)
924 for (frp2 = frp->fr_child; frp2 != NULL;
925 frp2 = frp2->fr_next)
926 if (frp2 != prevfrp)
927 minheight += frame_minheight(frp2, NOWIN);
928 prevfrp = frp;
929 }
930 available = topframe->fr_height;
931 needed += minheight;
932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 else
934 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200935 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
936 available = oldwin->w_frame->fr_height;
937 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100939 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
941 EMSG(_(e_noroom));
942 return FAIL;
943 }
944 oldwin_height = oldwin->w_height;
945 if (need_status)
946 {
947 oldwin->w_status_height = STATUS_HEIGHT;
948 oldwin_height -= STATUS_HEIGHT;
949 }
950 if (new_size == 0)
951 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200952 if (new_size > available - minheight - STATUS_HEIGHT)
953 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200954 if (new_size < wmh1)
955 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956
957 /* if it doesn't fit in the current window, need win_equal() */
958 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
959 do_equal = TRUE;
960
961 /* We don't like to take lines for the new window from a
962 * 'winfixheight' window. Take them from a window above or below
963 * instead, if possible. */
964 if (oldwin->w_p_wfh)
965 {
966 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
967 oldwin);
968 oldwin_height = oldwin->w_height;
969 if (need_status)
970 oldwin_height -= STATUS_HEIGHT;
971 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000972
973 /* Only make all windows the same height if one of them (except oldwin)
974 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100975 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000976 && oldwin->w_frame->fr_parent != NULL)
977 {
978 frp = oldwin->w_frame->fr_parent->fr_child;
979 while (frp != NULL)
980 {
981 if (frp->fr_win != oldwin && frp->fr_win != NULL
982 && (frp->fr_win->w_height > new_size
983 || frp->fr_win->w_height > oldwin_height - new_size
984 - STATUS_HEIGHT))
985 {
986 do_equal = TRUE;
987 break;
988 }
989 frp = frp->fr_next;
990 }
991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
993
994 /*
995 * allocate new window structure and link it in the window list
996 */
997 if ((flags & WSP_TOP) == 0
998 && ((flags & WSP_BOT)
999 || (flags & WSP_BELOW)
1000 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001001 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 {
1003 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001004 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001005 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 else
1007 win_append(oldwin, wp);
1008 }
1009 else
1010 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001011 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001012 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 else
1014 win_append(oldwin->w_prev, wp);
1015 }
1016
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001017 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
1019 if (wp == NULL)
1020 return FAIL;
1021
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001022 new_frame(wp);
1023 if (wp->w_frame == NULL)
1024 {
1025 win_free(wp, NULL);
1026 return FAIL;
1027 }
1028
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001029 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001030 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032
1033 /*
1034 * Reorganise the tree of frames to insert the new window.
1035 */
1036 if (flags & (WSP_TOP | WSP_BOT))
1037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1039 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
1041 curfrp = topframe->fr_child;
1042 if (flags & WSP_BOT)
1043 while (curfrp->fr_next != NULL)
1044 curfrp = curfrp->fr_next;
1045 }
1046 else
1047 curfrp = topframe;
1048 before = (flags & WSP_TOP);
1049 }
1050 else
1051 {
1052 curfrp = oldwin->w_frame;
1053 if (flags & WSP_BELOW)
1054 before = FALSE;
1055 else if (flags & WSP_ABOVE)
1056 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001057 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 before = !p_spr;
1059 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 before = !p_sb;
1061 }
1062 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1063 {
1064 /* Need to create a new frame in the tree to make a branch. */
1065 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1066 *frp = *curfrp;
1067 curfrp->fr_layout = layout;
1068 frp->fr_parent = curfrp;
1069 frp->fr_next = NULL;
1070 frp->fr_prev = NULL;
1071 curfrp->fr_child = frp;
1072 curfrp->fr_win = NULL;
1073 curfrp = frp;
1074 if (frp->fr_win != NULL)
1075 oldwin->w_frame = frp;
1076 else
1077 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1078 frp->fr_parent = curfrp;
1079 }
1080
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001081 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001082 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001084 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 frp->fr_parent = curfrp->fr_parent;
1086
1087 /* Insert the new frame at the right place in the frame list. */
1088 if (before)
1089 frame_insert(curfrp, frp);
1090 else
1091 frame_append(curfrp, frp);
1092
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001093 /* Set w_fraction now so that the cursor keeps the same relative
1094 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001095 if (oldwin->w_height > 0)
1096 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001097 wp->w_fraction = oldwin->w_fraction;
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (flags & WSP_VERT)
1100 {
1101 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001102
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (need_status)
1104 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001105 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 oldwin->w_status_height = need_status;
1107 }
1108 if (flags & (WSP_TOP | WSP_BOT))
1109 {
1110 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001111 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001112 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 wp->w_status_height = (p_ls > 0);
1114 }
1115 else
1116 {
1117 /* height and row of new window is same as current window */
1118 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001119 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 wp->w_status_height = oldwin->w_status_height;
1121 }
1122 frp->fr_height = curfrp->fr_height;
1123
1124 /* "new_size" of the current window goes to the new window, use
1125 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001126 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 if (before)
1128 wp->w_vsep_width = 1;
1129 else
1130 {
1131 wp->w_vsep_width = oldwin->w_vsep_width;
1132 oldwin->w_vsep_width = 1;
1133 }
1134 if (flags & (WSP_TOP | WSP_BOT))
1135 {
1136 if (flags & WSP_BOT)
1137 frame_add_vsep(curfrp);
1138 /* Set width of neighbor frame */
1139 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001140 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1141 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 }
1143 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001144 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 if (before) /* new window left of current one */
1146 {
1147 wp->w_wincol = oldwin->w_wincol;
1148 oldwin->w_wincol += new_size + 1;
1149 }
1150 else /* new window right of current one */
1151 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1152 frame_fix_width(oldwin);
1153 frame_fix_width(wp);
1154 }
1155 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
1157 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (flags & (WSP_TOP | WSP_BOT))
1159 {
1160 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001161 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 wp->w_vsep_width = 0;
1163 }
1164 else
1165 {
1166 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001167 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 wp->w_vsep_width = oldwin->w_vsep_width;
1169 }
1170 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171
1172 /* "new_size" of the current window goes to the new window, use
1173 * one row for the status line */
1174 win_new_height(wp, new_size);
1175 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001176 {
1177 int new_fr_height = curfrp->fr_height - new_size;
1178
1179 if (!((flags & WSP_BOT) && p_ls == 0))
1180 new_fr_height -= STATUS_HEIGHT;
1181 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 else
1184 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1185 if (before) /* new window above current one */
1186 {
1187 wp->w_winrow = oldwin->w_winrow;
1188 wp->w_status_height = STATUS_HEIGHT;
1189 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1190 }
1191 else /* new window below current one */
1192 {
1193 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1194 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001195 if (!(flags & WSP_BOT))
1196 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 if (flags & WSP_BOT)
1199 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 frame_fix_height(wp);
1201 frame_fix_height(oldwin);
1202 }
1203
1204 if (flags & (WSP_TOP | WSP_BOT))
1205 (void)win_comp_pos();
1206
1207 /*
1208 * Both windows need redrawing
1209 */
1210 redraw_win_later(wp, NOT_VALID);
1211 wp->w_redr_status = TRUE;
1212 redraw_win_later(oldwin, NOT_VALID);
1213 oldwin->w_redr_status = TRUE;
1214
1215 if (need_status)
1216 {
1217 msg_row = Rows - 1;
1218 msg_col = sc_col;
1219 msg_clr_eos_force(); /* Old command/ruler may still be there */
1220 comp_col();
1221 msg_row = Rows - 1;
1222 msg_col = 0; /* put position back at start of line */
1223 }
1224
1225 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001226 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 */
1228 if (do_equal || dir != 0)
1229 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001231 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
1233 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1234 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 if (flags & WSP_VERT)
1236 {
1237 i = p_wiw;
1238 if (size != 0)
1239 p_wiw = size;
1240
1241# ifdef FEAT_GUI
1242 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1243 if (gui.in_use)
1244 gui_init_which_components(NULL);
1245# endif
1246 }
1247 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 {
1249 i = p_wh;
1250 if (size != 0)
1251 p_wh = size;
1252 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001253
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001254#ifdef FEAT_JUMPLIST
1255 /* Keep same changelist position in new window. */
1256 wp->w_changelistidx = oldwin->w_changelistidx;
1257#endif
1258
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001259 /*
1260 * make the new window the current window
1261 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001262 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (flags & WSP_VERT)
1264 p_wiw = i;
1265 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 p_wh = i;
1267
1268 return OK;
1269}
1270
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001271
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001272/*
1273 * Initialize window "newp" from window "oldp".
1274 * Used when splitting a window and when creating a new tab page.
1275 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001276 * WSP_NEWLOC may be specified in flags to prevent the location list from
1277 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001278 */
1279 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001280win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001281{
1282 int i;
1283
1284 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001285#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001286 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001287#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001288 oldp->w_buffer->b_nwindows++;
1289 newp->w_cursor = oldp->w_cursor;
1290 newp->w_valid = 0;
1291 newp->w_curswant = oldp->w_curswant;
1292 newp->w_set_curswant = oldp->w_set_curswant;
1293 newp->w_topline = oldp->w_topline;
1294#ifdef FEAT_DIFF
1295 newp->w_topfill = oldp->w_topfill;
1296#endif
1297 newp->w_leftcol = oldp->w_leftcol;
1298 newp->w_pcmark = oldp->w_pcmark;
1299 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1300 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001301 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001302 newp->w_fraction = oldp->w_fraction;
1303 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1304#ifdef FEAT_JUMPLIST
1305 copy_jumplist(oldp, newp);
1306#endif
1307#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001308 if (flags & WSP_NEWLOC)
1309 {
1310 /* Don't copy the location list. */
1311 newp->w_llist = NULL;
1312 newp->w_llist_ref = NULL;
1313 }
1314 else
1315 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001316#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001317 newp->w_localdir = (oldp->w_localdir == NULL)
1318 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001319
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001320 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001321 for (i = 0; i < oldp->w_tagstacklen; i++)
1322 {
1323 newp->w_tagstack[i] = oldp->w_tagstack[i];
1324 if (newp->w_tagstack[i].tagname != NULL)
1325 newp->w_tagstack[i].tagname =
1326 vim_strsave(newp->w_tagstack[i].tagname);
1327 }
1328 newp->w_tagstackidx = oldp->w_tagstackidx;
1329 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001330#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001331 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001332#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001333
1334 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001335
Bram Moolenaara971b822011-09-14 14:43:25 +02001336#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001337 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001338#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001339}
1340
1341/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001342 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001343 * Only the essential things are copied.
1344 */
1345 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001346win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001347{
1348 /* Use the same argument list. */
1349 newp->w_alist = oldp->w_alist;
1350 ++newp->w_alist->al_refcount;
1351 newp->w_arg_idx = oldp->w_arg_idx;
1352
1353 /* copy options from existing window */
1354 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001355}
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357#endif /* FEAT_WINDOWS */
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#if defined(FEAT_WINDOWS) || defined(PROTO)
1360/*
1361 * Check if "win" is a pointer to an existing window.
1362 */
1363 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001364win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365{
1366 win_T *wp;
1367
1368 if (win == NULL)
1369 return FALSE;
1370 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1371 if (wp == win)
1372 return TRUE;
1373 return FALSE;
1374}
1375
1376/*
1377 * Return the number of windows.
1378 */
1379 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001380win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381{
1382 win_T *wp;
1383 int count = 0;
1384
1385 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1386 ++count;
1387 return count;
1388}
1389
1390/*
1391 * Make "count" windows on the screen.
1392 * Return actual number of windows on the screen.
1393 * Must be called when there is just one window, filling the whole screen
1394 * (excluding the command line).
1395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001397make_windows(
1398 int count,
1399 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
1401 int maxcount;
1402 int todo;
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 if (vertical)
1405 {
1406 /* Each windows needs at least 'winminwidth' lines and a separator
1407 * column. */
1408 maxcount = (curwin->w_width + curwin->w_vsep_width
1409 - (p_wiw - p_wmw)) / (p_wmw + 1);
1410 }
1411 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 {
1413 /* Each window needs at least 'winminheight' lines and a status line. */
1414 maxcount = (curwin->w_height + curwin->w_status_height
1415 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1416 }
1417
1418 if (maxcount < 2)
1419 maxcount = 2;
1420 if (count > maxcount)
1421 count = maxcount;
1422
1423 /*
1424 * add status line now, otherwise first window will be too big
1425 */
1426 if (count > 1)
1427 last_status(TRUE);
1428
1429#ifdef FEAT_AUTOCMD
1430 /*
1431 * Don't execute autocommands while creating the windows. Must do that
1432 * when putting the buffers in the windows.
1433 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001434 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435#endif
1436
1437 /* todo is number of windows left to create */
1438 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (vertical)
1440 {
1441 if (win_split(curwin->w_width - (curwin->w_width - todo)
1442 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1443 break;
1444 }
1445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 {
1447 if (win_split(curwin->w_height - (curwin->w_height - todo
1448 * STATUS_HEIGHT) / (todo + 1)
1449 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1450 break;
1451 }
1452
1453#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001454 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#endif
1456
1457 /* return actual number of windows */
1458 return (count - todo);
1459}
1460
1461/*
1462 * Exchange current and next window
1463 */
1464 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001465win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 frame_T *frp;
1468 frame_T *frp2;
1469 win_T *wp;
1470 win_T *wp2;
1471 int temp;
1472
1473 if (lastwin == firstwin) /* just one window */
1474 {
1475 beep_flush();
1476 return;
1477 }
1478
1479#ifdef FEAT_GUI
1480 need_mouse_correct = TRUE;
1481#endif
1482
1483 /*
1484 * find window to exchange with
1485 */
1486 if (Prenum)
1487 {
1488 frp = curwin->w_frame->fr_parent->fr_child;
1489 while (frp != NULL && --Prenum > 0)
1490 frp = frp->fr_next;
1491 }
1492 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1493 frp = curwin->w_frame->fr_next;
1494 else /* Swap last window in row/col with previous */
1495 frp = curwin->w_frame->fr_prev;
1496
1497 /* We can only exchange a window with another window, not with a frame
1498 * containing windows. */
1499 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1500 return;
1501 wp = frp->fr_win;
1502
1503/*
1504 * 1. remove curwin from the list. Remember after which window it was in wp2
1505 * 2. insert curwin before wp in the list
1506 * if wp != wp2
1507 * 3. remove wp from the list
1508 * 4. insert wp after wp2
1509 * 5. exchange the status line height and vsep width.
1510 */
1511 wp2 = curwin->w_prev;
1512 frp2 = curwin->w_frame->fr_prev;
1513 if (wp->w_prev != curwin)
1514 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001515 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 frame_remove(curwin->w_frame);
1517 win_append(wp->w_prev, curwin);
1518 frame_insert(frp, curwin->w_frame);
1519 }
1520 if (wp != wp2)
1521 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001522 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 frame_remove(wp->w_frame);
1524 win_append(wp2, wp);
1525 if (frp2 == NULL)
1526 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1527 else
1528 frame_append(frp2, wp->w_frame);
1529 }
1530 temp = curwin->w_status_height;
1531 curwin->w_status_height = wp->w_status_height;
1532 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 temp = curwin->w_vsep_width;
1534 curwin->w_vsep_width = wp->w_vsep_width;
1535 wp->w_vsep_width = temp;
1536
1537 /* If the windows are not in the same frame, exchange the sizes to avoid
1538 * messing up the window layout. Otherwise fix the frame sizes. */
1539 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1540 {
1541 temp = curwin->w_height;
1542 curwin->w_height = wp->w_height;
1543 wp->w_height = temp;
1544 temp = curwin->w_width;
1545 curwin->w_width = wp->w_width;
1546 wp->w_width = temp;
1547 }
1548 else
1549 {
1550 frame_fix_height(curwin);
1551 frame_fix_height(wp);
1552 frame_fix_width(curwin);
1553 frame_fix_width(wp);
1554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
1556 (void)win_comp_pos(); /* recompute window positions */
1557
1558 win_enter(wp, TRUE);
1559 redraw_later(CLEAR);
1560}
1561
1562/*
1563 * rotate windows: if upwards TRUE the second window becomes the first one
1564 * if upwards FALSE the first window becomes the second one
1565 */
1566 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001567win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
1569 win_T *wp1;
1570 win_T *wp2;
1571 frame_T *frp;
1572 int n;
1573
1574 if (firstwin == lastwin) /* nothing to do */
1575 {
1576 beep_flush();
1577 return;
1578 }
1579
1580#ifdef FEAT_GUI
1581 need_mouse_correct = TRUE;
1582#endif
1583
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 /* Check if all frames in this row/col have one window. */
1585 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1586 frp = frp->fr_next)
1587 if (frp->fr_win == NULL)
1588 {
1589 EMSG(_("E443: Cannot rotate when another window is split"));
1590 return;
1591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593 while (count--)
1594 {
1595 if (upwards) /* first window becomes last window */
1596 {
1597 /* remove first window/frame from the list */
1598 frp = curwin->w_frame->fr_parent->fr_child;
1599 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001600 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 frame_remove(frp);
1602
1603 /* find last frame and append removed window/frame after it */
1604 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1605 ;
1606 win_append(frp->fr_win, wp1);
1607 frame_append(frp, wp1->w_frame);
1608
1609 wp2 = frp->fr_win; /* previously last window */
1610 }
1611 else /* last window becomes first window */
1612 {
1613 /* find last window/frame in the list and remove it */
1614 for (frp = curwin->w_frame; frp->fr_next != NULL;
1615 frp = frp->fr_next)
1616 ;
1617 wp1 = frp->fr_win;
1618 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001619 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 frame_remove(frp);
1621
1622 /* append the removed window/frame before the first in the list */
1623 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1624 frame_insert(frp->fr_parent->fr_child, frp);
1625 }
1626
1627 /* exchange status height and vsep width of old and new last window */
1628 n = wp2->w_status_height;
1629 wp2->w_status_height = wp1->w_status_height;
1630 wp1->w_status_height = n;
1631 frame_fix_height(wp1);
1632 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 n = wp2->w_vsep_width;
1634 wp2->w_vsep_width = wp1->w_vsep_width;
1635 wp1->w_vsep_width = n;
1636 frame_fix_width(wp1);
1637 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001639 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 (void)win_comp_pos();
1641 }
1642
1643 redraw_later(CLEAR);
1644}
1645
1646/*
1647 * Move the current window to the very top/bottom/left/right of the screen.
1648 */
1649 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001650win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
1652 int dir;
1653 int height = curwin->w_height;
1654
1655 if (lastwin == firstwin)
1656 {
1657 beep_flush();
1658 return;
1659 }
1660
1661 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001662 (void)winframe_remove(curwin, &dir, NULL);
1663 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 last_status(FALSE); /* may need to remove last status line */
1665 (void)win_comp_pos(); /* recompute window positions */
1666
1667 /* Split a window on the desired side and put the window there. */
1668 (void)win_split_ins(size, flags, curwin, dir);
1669 if (!(flags & WSP_VERT))
1670 {
1671 win_setheight(height);
1672 if (p_ea)
1673 win_equal(curwin, TRUE, 'v');
1674 }
1675
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001676#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1678 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001679 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681}
1682
1683/*
1684 * Move window "win1" to below/right of "win2" and make "win1" the current
1685 * window. Only works within the same frame!
1686 */
1687 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001688win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 int height;
1691
1692 /* check if the arguments are reasonable */
1693 if (win1 == win2)
1694 return;
1695
1696 /* check if there is something to do */
1697 if (win2->w_next != win1)
1698 {
1699 /* may need move the status line/vertical separator of the last window
1700 * */
1701 if (win1 == lastwin)
1702 {
1703 height = win1->w_prev->w_status_height;
1704 win1->w_prev->w_status_height = win1->w_status_height;
1705 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001706 if (win1->w_prev->w_vsep_width == 1)
1707 {
1708 /* Remove the vertical separator from the last-but-one window,
1709 * add it to the last window. Adjust the frame widths. */
1710 win1->w_prev->w_vsep_width = 0;
1711 win1->w_prev->w_frame->fr_width -= 1;
1712 win1->w_vsep_width = 1;
1713 win1->w_frame->fr_width += 1;
1714 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 }
1716 else if (win2 == lastwin)
1717 {
1718 height = win1->w_status_height;
1719 win1->w_status_height = win2->w_status_height;
1720 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001721 if (win1->w_vsep_width == 1)
1722 {
1723 /* Remove the vertical separator from win1, add it to the last
1724 * window, win2. Adjust the frame widths. */
1725 win2->w_vsep_width = 1;
1726 win2->w_frame->fr_width += 1;
1727 win1->w_vsep_width = 0;
1728 win1->w_frame->fr_width -= 1;
1729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001731 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 frame_remove(win1->w_frame);
1733 win_append(win2, win1);
1734 frame_append(win2->w_frame, win1->w_frame);
1735
1736 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1737 redraw_later(NOT_VALID);
1738 }
1739 win_enter(win1, FALSE);
1740}
1741
1742/*
1743 * Make all windows the same height.
1744 * 'next_curwin' will soon be the current window, make sure it has enough
1745 * rows.
1746 */
1747 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001748win_equal(
1749 win_T *next_curwin, /* pointer to current window to be or NULL */
1750 int current, /* do only frame with current window */
1751 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 'b' for both, 0 for using p_ead */
1753{
1754 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001757 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001758 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759}
1760
1761/*
1762 * Set a frame to a new position and height, spreading the available room
1763 * equally over contained frames.
1764 * The window "next_curwin" (if not NULL) should at least get the size from
1765 * 'winheight' and 'winwidth' if possible.
1766 */
1767 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001768win_equal_rec(
1769 win_T *next_curwin, /* pointer to current window to be or NULL */
1770 int current, /* do only frame with current window */
1771 frame_T *topfr, /* frame to set size off */
1772 int dir, /* 'v', 'h' or 'b', see win_equal() */
1773 int col, /* horizontal position for frame */
1774 int row, /* vertical position for frame */
1775 int width, /* new width of frame */
1776 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777{
1778 int n, m;
1779 int extra_sep = 0;
1780 int wincount, totwincount = 0;
1781 frame_T *fr;
1782 int next_curwin_size = 0;
1783 int room = 0;
1784 int new_size;
1785 int has_next_curwin = 0;
1786 int hnc;
1787
1788 if (topfr->fr_layout == FR_LEAF)
1789 {
1790 /* Set the width/height of this frame.
1791 * Redraw when size or position changes */
1792 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 )
1795 {
1796 topfr->fr_win->w_winrow = row;
1797 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001799 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 redraw_all_later(CLEAR);
1801 }
1802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 else if (topfr->fr_layout == FR_ROW)
1804 {
1805 topfr->fr_width = width;
1806 topfr->fr_height = height;
1807
1808 if (dir != 'v') /* equalize frame widths */
1809 {
1810 /* Compute the maximum number of windows horizontally in this
1811 * frame. */
1812 n = frame_minwidth(topfr, NOWIN);
1813 /* add one for the rightmost window, it doesn't have a separator */
1814 if (col + width == Columns)
1815 extra_sep = 1;
1816 else
1817 extra_sep = 0;
1818 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001819 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001821 /*
1822 * Compute width for "next_curwin" window and room available for
1823 * other windows.
1824 * "m" is the minimal width when counting p_wiw for "next_curwin".
1825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 m = frame_minwidth(topfr, next_curwin);
1827 room = width - m;
1828 if (room < 0)
1829 {
1830 next_curwin_size = p_wiw + room;
1831 room = 0;
1832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 else
1834 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001835 next_curwin_size = -1;
1836 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1837 {
1838 /* If 'winfixwidth' set keep the window width if
1839 * possible.
1840 * Watch out for this window being the next_curwin. */
1841 if (frame_fixed_width(fr))
1842 {
1843 n = frame_minwidth(fr, NOWIN);
1844 new_size = fr->fr_width;
1845 if (frame_has_win(fr, next_curwin))
1846 {
1847 room += p_wiw - p_wmw;
1848 next_curwin_size = 0;
1849 if (new_size < p_wiw)
1850 new_size = p_wiw;
1851 }
1852 else
1853 /* These windows don't use up room. */
1854 totwincount -= (n + (fr->fr_next == NULL
1855 ? extra_sep : 0)) / (p_wmw + 1);
1856 room -= new_size - n;
1857 if (room < 0)
1858 {
1859 new_size += room;
1860 room = 0;
1861 }
1862 fr->fr_newwidth = new_size;
1863 }
1864 }
1865 if (next_curwin_size == -1)
1866 {
1867 if (!has_next_curwin)
1868 next_curwin_size = 0;
1869 else if (totwincount > 1
1870 && (room + (totwincount - 2))
1871 / (totwincount - 1) > p_wiw)
1872 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001873 /* Can make all windows wider than 'winwidth', spread
1874 * the room equally. */
1875 next_curwin_size = (room + p_wiw
1876 + (totwincount - 1) * p_wmw
1877 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001878 room -= next_curwin_size - p_wiw;
1879 }
1880 else
1881 next_curwin_size = p_wiw;
1882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001884
1885 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 --totwincount; /* don't count curwin */
1887 }
1888
1889 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1890 {
1891 n = m = 0;
1892 wincount = 1;
1893 if (fr->fr_next == NULL)
1894 /* last frame gets all that remains (avoid roundoff error) */
1895 new_size = width;
1896 else if (dir == 'v')
1897 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001898 else if (frame_fixed_width(fr))
1899 {
1900 new_size = fr->fr_newwidth;
1901 wincount = 0; /* doesn't count as a sizeable window */
1902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 else
1904 {
1905 /* Compute the maximum number of windows horiz. in "fr". */
1906 n = frame_minwidth(fr, NOWIN);
1907 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1908 / (p_wmw + 1);
1909 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001910 if (has_next_curwin)
1911 hnc = frame_has_win(fr, next_curwin);
1912 else
1913 hnc = FALSE;
1914 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001916 if (totwincount == 0)
1917 new_size = room;
1918 else
1919 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001921 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 next_curwin_size -= p_wiw - (m - n);
1924 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001925 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001927 else
1928 room -= new_size;
1929 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001932 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 * window, unless equalizing all frames. */
1934 if (!current || dir != 'v' || topfr->fr_parent != NULL
1935 || (new_size != fr->fr_width)
1936 || frame_has_win(fr, next_curwin))
1937 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001938 new_size, height);
1939 col += new_size;
1940 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 totwincount -= wincount;
1942 }
1943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 else /* topfr->fr_layout == FR_COL */
1945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 topfr->fr_height = height;
1948
1949 if (dir != 'h') /* equalize frame heights */
1950 {
1951 /* Compute maximum number of windows vertically in this frame. */
1952 n = frame_minheight(topfr, NOWIN);
1953 /* add one for the bottom window if it doesn't have a statusline */
1954 if (row + height == cmdline_row && p_ls == 0)
1955 extra_sep = 1;
1956 else
1957 extra_sep = 0;
1958 totwincount = (n + extra_sep) / (p_wmh + 1);
1959 has_next_curwin = frame_has_win(topfr, next_curwin);
1960
1961 /*
1962 * Compute height for "next_curwin" window and room available for
1963 * other windows.
1964 * "m" is the minimal height when counting p_wh for "next_curwin".
1965 */
1966 m = frame_minheight(topfr, next_curwin);
1967 room = height - m;
1968 if (room < 0)
1969 {
1970 /* The room is less then 'winheight', use all space for the
1971 * current window. */
1972 next_curwin_size = p_wh + room;
1973 room = 0;
1974 }
1975 else
1976 {
1977 next_curwin_size = -1;
1978 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1979 {
1980 /* If 'winfixheight' set keep the window height if
1981 * possible.
1982 * Watch out for this window being the next_curwin. */
1983 if (frame_fixed_height(fr))
1984 {
1985 n = frame_minheight(fr, NOWIN);
1986 new_size = fr->fr_height;
1987 if (frame_has_win(fr, next_curwin))
1988 {
1989 room += p_wh - p_wmh;
1990 next_curwin_size = 0;
1991 if (new_size < p_wh)
1992 new_size = p_wh;
1993 }
1994 else
1995 /* These windows don't use up room. */
1996 totwincount -= (n + (fr->fr_next == NULL
1997 ? extra_sep : 0)) / (p_wmh + 1);
1998 room -= new_size - n;
1999 if (room < 0)
2000 {
2001 new_size += room;
2002 room = 0;
2003 }
2004 fr->fr_newheight = new_size;
2005 }
2006 }
2007 if (next_curwin_size == -1)
2008 {
2009 if (!has_next_curwin)
2010 next_curwin_size = 0;
2011 else if (totwincount > 1
2012 && (room + (totwincount - 2))
2013 / (totwincount - 1) > p_wh)
2014 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002015 /* can make all windows higher than 'winheight',
2016 * spread the room equally. */
2017 next_curwin_size = (room + p_wh
2018 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 + (totwincount - 1)) / totwincount;
2020 room -= next_curwin_size - p_wh;
2021 }
2022 else
2023 next_curwin_size = p_wh;
2024 }
2025 }
2026
2027 if (has_next_curwin)
2028 --totwincount; /* don't count curwin */
2029 }
2030
2031 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2032 {
2033 n = m = 0;
2034 wincount = 1;
2035 if (fr->fr_next == NULL)
2036 /* last frame gets all that remains (avoid roundoff error) */
2037 new_size = height;
2038 else if (dir == 'h')
2039 new_size = fr->fr_height;
2040 else if (frame_fixed_height(fr))
2041 {
2042 new_size = fr->fr_newheight;
2043 wincount = 0; /* doesn't count as a sizeable window */
2044 }
2045 else
2046 {
2047 /* Compute the maximum number of windows vert. in "fr". */
2048 n = frame_minheight(fr, NOWIN);
2049 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2050 / (p_wmh + 1);
2051 m = frame_minheight(fr, next_curwin);
2052 if (has_next_curwin)
2053 hnc = frame_has_win(fr, next_curwin);
2054 else
2055 hnc = FALSE;
2056 if (hnc) /* don't count next_curwin */
2057 --wincount;
2058 if (totwincount == 0)
2059 new_size = room;
2060 else
2061 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2062 / totwincount;
2063 if (hnc) /* add next_curwin size */
2064 {
2065 next_curwin_size -= p_wh - (m - n);
2066 new_size += next_curwin_size;
2067 room -= new_size - next_curwin_size;
2068 }
2069 else
2070 room -= new_size;
2071 new_size += n;
2072 }
2073 /* Skip frame that is full width when splitting or closing a
2074 * window, unless equalizing all frames. */
2075 if (!current || dir != 'h' || topfr->fr_parent != NULL
2076 || (new_size != fr->fr_height)
2077 || frame_has_win(fr, next_curwin))
2078 win_equal_rec(next_curwin, current, fr, dir, col, row,
2079 width, new_size);
2080 row += new_size;
2081 height -= new_size;
2082 totwincount -= wincount;
2083 }
2084 }
2085}
2086
2087/*
2088 * close all windows for buffer 'buf'
2089 */
2090 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002091close_windows(
2092 buf_T *buf,
2093 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002095 win_T *wp;
2096 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002097 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002098#ifdef FEAT_AUTOCMD
2099 int count = tabpage_index(NULL);
2100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002103
2104 for (wp = firstwin; wp != NULL && lastwin != firstwin; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002106 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2107#ifdef FEAT_AUTOCMD
2108 && !(wp->w_closing || wp->w_buffer->b_closing)
2109#endif
2110 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00002112 win_close(wp, FALSE);
2113
2114 /* Start all over, autocommands may change the window layout. */
2115 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 }
2117 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002118 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002120
2121 /* Also check windows in other tab pages. */
2122 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2123 {
2124 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002125 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002126 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002127 if (wp->w_buffer == buf
2128#ifdef FEAT_AUTOCMD
2129 && !(wp->w_closing || wp->w_buffer->b_closing)
2130#endif
2131 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002132 {
2133 win_close_othertab(wp, FALSE, tp);
2134
2135 /* Start all over, the tab page may be closed and
2136 * autocommands may change the window layout. */
2137 nexttp = first_tabpage;
2138 break;
2139 }
2140 }
2141
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002143
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002144#ifdef FEAT_AUTOCMD
2145 if (count != tabpage_index(NULL))
2146 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
2147#endif
2148
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002149 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002150 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002151 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152}
2153
2154/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002155 * Return TRUE if the current window is the only window that exists (ignoring
2156 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002157 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002158 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002159 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002160last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002161{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002162 return (one_window() && first_tabpage->tp_next == NULL);
2163}
2164
2165/*
2166 * Return TRUE if there is only one window other than "aucmd_win" in the
2167 * current tab page.
2168 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002169 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002170one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002171{
2172#ifdef FEAT_AUTOCMD
2173 win_T *wp;
2174 int seen_one = FALSE;
2175
2176 FOR_ALL_WINDOWS(wp)
2177 {
2178 if (wp != aucmd_win)
2179 {
2180 if (seen_one)
2181 return FALSE;
2182 seen_one = TRUE;
2183 }
2184 }
2185 return TRUE;
2186#else
2187 return firstwin == lastwin;
2188#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002189}
2190
2191/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002192 * Close the possibly last window in a tab page.
2193 * Returns TRUE when the window was closed already.
2194 */
2195 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002196close_last_window_tabpage(
2197 win_T *win,
2198 int free_buf,
2199 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002200{
2201 if (firstwin == lastwin)
2202 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002203#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002204 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002205#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002206
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002207 /*
2208 * Closing the last window in a tab page. First go to another tab
2209 * page and then close the window and the tab page. This avoids that
2210 * curwin and curtab are invalid while we are freeing memory, they may
2211 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002212 * Don't trigger autocommands yet, they may use wrong values, so do
2213 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002214 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002215 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002216 redraw_tabline = TRUE;
2217
2218 /* Safety check: Autocommands may have closed the window when jumping
2219 * to the other tab page. */
2220 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2221 {
2222 int h = tabline_height();
2223
2224 win_close_othertab(win, free_buf, prev_curtab);
2225 if (h != tabline_height())
2226 shell_new_rows();
2227 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002228 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2229 * that now. */
2230#ifdef FEAT_AUTOCMD
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002231 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002232 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002233 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2234 if (old_curbuf != curbuf)
2235 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002236#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002237 return TRUE;
2238 }
2239 return FALSE;
2240}
2241
2242/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002243 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 * If "free_buf" is TRUE related buffer may be unloaded.
2245 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002246 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002247 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002249 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002250win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
2252 win_T *wp;
2253#ifdef FEAT_AUTOCMD
2254 int other_buffer = FALSE;
2255#endif
2256 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 int dir;
2258 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002259 tabpage_T *prev_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002261 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
2263 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
2266
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002267#ifdef FEAT_AUTOCMD
Bram Moolenaar756287d2012-07-06 16:39:47 +02002268 if (win->w_closing || (win->w_buffer != NULL && win->w_buffer->b_closing))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002269 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002270 if (win == aucmd_win)
2271 {
2272 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002273 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002274 }
2275 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2276 {
2277 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002278 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002279 }
2280#endif
2281
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002282 /* When closing the last window in a tab page first go to another tab page
2283 * and then close the window and the tab page to avoid that curwin and
2284 * curtab are invalid while we are freeing memory. */
2285 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002286 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002287
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 /* When closing the help window, try restoring a snapshot after closing
2289 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002290 if (win->w_buffer != NULL && win->w_buffer->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 help_window = TRUE;
2292 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002293 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
2295#ifdef FEAT_AUTOCMD
2296 if (win == curwin)
2297 {
2298 /*
2299 * Guess which window is going to be the new current window.
2300 * This may change because of the autocommands (sigh).
2301 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002302 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002305 * Be careful: If autocommands delete the window or cause this window
2306 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 */
2308 if (wp->w_buffer != curbuf)
2309 {
2310 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002311 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002313 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002314 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002315 win->w_closing = FALSE;
2316 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002319 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002321 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002322 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002323 win->w_closing = FALSE;
2324 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326# ifdef FEAT_EVAL
2327 /* autocmds may abort script processing */
2328 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330# endif
2331 }
2332#endif
2333
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002334#ifdef FEAT_GUI
2335 /* Avoid trouble with scrollbars that are going to be deleted in
2336 * win_free(). */
2337 if (gui.in_use)
2338 out_flush();
2339#endif
2340
Bram Moolenaara971b822011-09-14 14:43:25 +02002341#ifdef FEAT_SYN_HL
2342 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002343 if (win->w_buffer != NULL)
2344 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002345#endif
2346
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 /*
2348 * Close the link to the buffer.
2349 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002350 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002351 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002352 bufref_T bufref;
2353
2354 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002355#ifdef FEAT_AUTOCMD
2356 win->w_closing = TRUE;
2357#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002358 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002359#ifdef FEAT_AUTOCMD
2360 if (win_valid(win))
2361 win->w_closing = FALSE;
2362#endif
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002363 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2364 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002365 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002366 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002367 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002368
Bram Moolenaar802418d2013-01-17 14:00:11 +01002369 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2370 && (last_window() || curtab != prev_curtab
2371 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002372 {
2373 /* Autocommands have close all windows, quit now. Restore
2374 * curwin->w_buffer, otherwise writing viminfo may fail. */
2375 if (curwin->w_buffer == NULL)
2376 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002377 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002378 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 /* Autocommands may have closed the window already, or closed the only
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002381 * other window or moved to another tab page. */
Bram Moolenaar802418d2013-01-17 14:00:11 +01002382 else if (!win_valid(win) || last_window() || curtab != prev_curtab
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002383 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaara971b822011-09-14 14:43:25 +02002386 /* Free the memory used for the window and get the window that received
2387 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002388 wp = win_free_mem(win, &dir, NULL);
2389
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 /* Make sure curwin isn't invalid. It can cause severe trouble when
2391 * printing an error message. For win_equal() curbuf needs to be valid
2392 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002393 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 {
2395 curwin = wp;
2396#ifdef FEAT_QUICKFIX
2397 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2398 {
2399 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002400 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 * finding another window to go to.
2402 */
2403 for (;;)
2404 {
2405 if (wp->w_next == NULL)
2406 wp = firstwin;
2407 else
2408 wp = wp->w_next;
2409 if (wp == curwin)
2410 break;
2411 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2412 {
2413 curwin = wp;
2414 break;
2415 }
2416 }
2417 }
2418#endif
2419 curbuf = curwin->w_buffer;
2420 close_curwin = TRUE;
2421 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002422 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
2423 win_equal(curwin, TRUE, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 else
2425 win_comp_pos();
2426 if (close_curwin)
2427 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002428 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#ifdef FEAT_AUTOCMD
2430 if (other_buffer)
2431 /* careful: after this wp and win may be invalid! */
2432 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2433#endif
2434 }
2435
2436 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002437 * If last window has a status line now and we don't want one,
2438 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 */
2440 last_status(FALSE);
2441
2442 /* After closing the help window, try restoring the window layout from
2443 * before it was opened. */
2444 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002445 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002447#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2449 if (gui.in_use && !win_hasvertsplit())
2450 gui_init_which_components(NULL);
2451#endif
2452
2453 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002454 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455}
2456
2457/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002458 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002459 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002460 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002461 * Caller must check if buffer is hidden and whether the tabline needs to be
2462 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002463 */
2464 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002465win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002466{
2467 win_T *wp;
2468 int dir;
2469 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002470 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002471
Bram Moolenaar362ce482012-06-06 19:02:45 +02002472#ifdef FEAT_AUTOCMD
2473 if (win->w_closing || win->w_buffer->b_closing)
2474 return; /* window is already being closed */
2475#endif
2476
Bram Moolenaarf740b292006-02-16 22:11:02 +00002477 /* Close the link to the buffer. */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002478 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002479
2480 /* Careful: Autocommands may have closed the tab page or made it the
2481 * current tab page. */
2482 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2483 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002484 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002485 return;
2486
2487 /* Autocommands may have closed the window already. */
2488 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2489 ;
2490 if (wp == NULL)
2491 return;
2492
Bram Moolenaarf740b292006-02-16 22:11:02 +00002493 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002494 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002495 {
2496 if (tp == first_tabpage)
2497 first_tabpage = tp->tp_next;
2498 else
2499 {
2500 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2501 ptp = ptp->tp_next)
2502 ;
2503 if (ptp == NULL)
2504 {
2505 EMSG2(_(e_intern2), "win_close_othertab()");
2506 return;
2507 }
2508 ptp->tp_next = tp->tp_next;
2509 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002510 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002511 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002512
2513 /* Free the memory used for the window. */
2514 win_free_mem(win, &dir, tp);
2515
2516 if (free_tp)
2517 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002518}
2519
2520/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002521 * Free the memory used for a window.
2522 * Returns a pointer to the window that got the freed up space.
2523 */
2524 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002525win_free_mem(
2526 win_T *win,
2527 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2528 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002529{
2530 frame_T *frp;
2531 win_T *wp;
2532
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002533 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002534 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002535 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002536 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002537 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002538
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002539 /* When deleting the current window of another tab page select a new
2540 * current window. */
2541 if (tp != NULL && win == tp->tp_curwin)
2542 tp->tp_curwin = wp;
2543
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002544 return wp;
2545}
2546
2547#if defined(EXITFREE) || defined(PROTO)
2548 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002549win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002550{
2551 int dummy;
2552
Bram Moolenaarf740b292006-02-16 22:11:02 +00002553# ifdef FEAT_WINDOWS
2554 while (first_tabpage->tp_next != NULL)
2555 tabpage_close(TRUE);
2556# endif
2557
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002558# ifdef FEAT_AUTOCMD
2559 if (aucmd_win != NULL)
2560 {
2561 (void)win_free_mem(aucmd_win, &dummy, NULL);
2562 aucmd_win = NULL;
2563 }
2564# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002565
2566 while (firstwin != NULL)
2567 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002568
2569 /* No window should be used after this. Set curwin to NULL to crash
2570 * instead of using freed memory. */
2571 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002572}
2573#endif
2574
2575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 * Remove a window and its frame from the tree of frames.
2577 * Returns a pointer to the window that got the freed up space.
2578 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002579 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002580winframe_remove(
2581 win_T *win,
2582 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2583 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585 frame_T *frp, *frp2, *frp3;
2586 frame_T *frp_close = win->w_frame;
2587 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588
2589 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002590 * If there is only one window there is nothing to remove.
2591 */
2592 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
2593 return NULL;
2594
2595 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 * Remove the window from its frame.
2597 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002598 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 wp = frame2win(frp2);
2600
2601 /* Remove this frame from the list of frames. */
2602 frame_remove(frp_close);
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (frp_close->fr_parent->fr_layout == FR_COL)
2605 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002606 /* When 'winfixheight' is set, try to find another frame in the column
2607 * (as close to the closed frame as possible) to distribute the height
2608 * to. */
2609 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2610 {
2611 frp = frp_close->fr_prev;
2612 frp3 = frp_close->fr_next;
2613 while (frp != NULL || frp3 != NULL)
2614 {
2615 if (frp != NULL)
2616 {
2617 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2618 {
2619 frp2 = frp;
2620 wp = frp->fr_win;
2621 break;
2622 }
2623 frp = frp->fr_prev;
2624 }
2625 if (frp3 != NULL)
2626 {
2627 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2628 {
2629 frp2 = frp3;
2630 wp = frp3->fr_win;
2631 break;
2632 }
2633 frp3 = frp3->fr_next;
2634 }
2635 }
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2638 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 *dirp = 'v';
2640 }
2641 else
2642 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002643 /* When 'winfixwidth' is set, try to find another frame in the column
2644 * (as close to the closed frame as possible) to distribute the width
2645 * to. */
2646 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2647 {
2648 frp = frp_close->fr_prev;
2649 frp3 = frp_close->fr_next;
2650 while (frp != NULL || frp3 != NULL)
2651 {
2652 if (frp != NULL)
2653 {
2654 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2655 {
2656 frp2 = frp;
2657 wp = frp->fr_win;
2658 break;
2659 }
2660 frp = frp->fr_prev;
2661 }
2662 if (frp3 != NULL)
2663 {
2664 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2665 {
2666 frp2 = frp3;
2667 wp = frp3->fr_win;
2668 break;
2669 }
2670 frp3 = frp3->fr_next;
2671 }
2672 }
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002675 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 *dirp = 'h';
2677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678
2679 /* If rows/columns go to a window below/right its positions need to be
2680 * updated. Can only be done after the sizes have been updated. */
2681 if (frp2 == frp_close->fr_next)
2682 {
2683 int row = win->w_winrow;
2684 int col = W_WINCOL(win);
2685
2686 frame_comp_pos(frp2, &row, &col);
2687 }
2688
2689 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2690 {
2691 /* There is no other frame in this list, move its info to the parent
2692 * and remove it. */
2693 frp2->fr_parent->fr_layout = frp2->fr_layout;
2694 frp2->fr_parent->fr_child = frp2->fr_child;
2695 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2696 frp->fr_parent = frp2->fr_parent;
2697 frp2->fr_parent->fr_win = frp2->fr_win;
2698 if (frp2->fr_win != NULL)
2699 frp2->fr_win->w_frame = frp2->fr_parent;
2700 frp = frp2->fr_parent;
2701 vim_free(frp2);
2702
2703 frp2 = frp->fr_parent;
2704 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2705 {
2706 /* The frame above the parent has the same layout, have to merge
2707 * the frames into this list. */
2708 if (frp2->fr_child == frp)
2709 frp2->fr_child = frp->fr_child;
2710 frp->fr_child->fr_prev = frp->fr_prev;
2711 if (frp->fr_prev != NULL)
2712 frp->fr_prev->fr_next = frp->fr_child;
2713 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2714 {
2715 frp3->fr_parent = frp2;
2716 if (frp3->fr_next == NULL)
2717 {
2718 frp3->fr_next = frp->fr_next;
2719 if (frp->fr_next != NULL)
2720 frp->fr_next->fr_prev = frp3;
2721 break;
2722 }
2723 }
2724 vim_free(frp);
2725 }
2726 }
2727
2728 return wp;
2729}
2730
2731/*
2732 * Find out which frame is going to get the freed up space when "win" is
2733 * closed.
2734 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2735 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2736 * This makes opening a window and closing it immediately keep the same window
2737 * layout.
2738 */
2739 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002740win_altframe(
2741 win_T *win,
2742 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 frame_T *frp;
2745 int b;
2746
Bram Moolenaarf740b292006-02-16 22:11:02 +00002747 if (tp == NULL ? firstwin == lastwin : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002748 /* Last window in this tab page, will go to next tab page. */
2749 return alt_tabpage()->tp_curwin->w_frame;
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 frp = win->w_frame;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002752 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 b = p_spr;
2754 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 b = p_sb;
2756 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2757 return frp->fr_next;
2758 return frp->fr_prev;
2759}
2760
2761/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002762 * Return the tabpage that will be used if the current one is closed.
2763 */
2764 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002765alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002766{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002767 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002768
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002769 /* Use the next tab page if possible. */
2770 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002771 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002772
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002773 /* Find the last but one tab page. */
2774 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2775 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002776 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002777}
2778
2779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 * Find the left-upper window in frame "frp".
2781 */
2782 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002783frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784{
2785 while (frp->fr_win == NULL)
2786 frp = frp->fr_child;
2787 return frp->fr_win;
2788}
2789
2790/*
2791 * Return TRUE if frame "frp" contains window "wp".
2792 */
2793 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002794frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 frame_T *p;
2797
2798 if (frp->fr_layout == FR_LEAF)
2799 return frp->fr_win == wp;
2800
2801 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2802 if (frame_has_win(p, wp))
2803 return TRUE;
2804 return FALSE;
2805}
2806
2807/*
2808 * Set a new height for a frame. Recursively sets the height for contained
2809 * frames and windows. Caller must take care of positions.
2810 */
2811 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002812frame_new_height(
2813 frame_T *topfrp,
2814 int height,
2815 int topfirst, /* resize topmost contained frame first */
2816 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 may cause the height not to be set */
2818{
2819 frame_T *frp;
2820 int extra_lines;
2821 int h;
2822
2823 if (topfrp->fr_win != NULL)
2824 {
2825 /* Simple case: just one window. */
2826 win_new_height(topfrp->fr_win,
2827 height - topfrp->fr_win->w_status_height);
2828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 else if (topfrp->fr_layout == FR_ROW)
2830 {
2831 do
2832 {
2833 /* All frames in this row get the same new height. */
2834 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2835 {
2836 frame_new_height(frp, height, topfirst, wfh);
2837 if (frp->fr_height > height)
2838 {
2839 /* Could not fit the windows, make the whole row higher. */
2840 height = frp->fr_height;
2841 break;
2842 }
2843 }
2844 }
2845 while (frp != NULL);
2846 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002847 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
2849 /* Complicated case: Resize a column of frames. Resize the bottom
2850 * frame first, frames above that when needed. */
2851
2852 frp = topfrp->fr_child;
2853 if (wfh)
2854 /* Advance past frames with one window with 'wfh' set. */
2855 while (frame_fixed_height(frp))
2856 {
2857 frp = frp->fr_next;
2858 if (frp == NULL)
2859 return; /* no frame without 'wfh', give up */
2860 }
2861 if (!topfirst)
2862 {
2863 /* Find the bottom frame of this column */
2864 while (frp->fr_next != NULL)
2865 frp = frp->fr_next;
2866 if (wfh)
2867 /* Advance back for frames with one window with 'wfh' set. */
2868 while (frame_fixed_height(frp))
2869 frp = frp->fr_prev;
2870 }
2871
2872 extra_lines = height - topfrp->fr_height;
2873 if (extra_lines < 0)
2874 {
2875 /* reduce height of contained frames, bottom or top frame first */
2876 while (frp != NULL)
2877 {
2878 h = frame_minheight(frp, NULL);
2879 if (frp->fr_height + extra_lines < h)
2880 {
2881 extra_lines += frp->fr_height - h;
2882 frame_new_height(frp, h, topfirst, wfh);
2883 }
2884 else
2885 {
2886 frame_new_height(frp, frp->fr_height + extra_lines,
2887 topfirst, wfh);
2888 break;
2889 }
2890 if (topfirst)
2891 {
2892 do
2893 frp = frp->fr_next;
2894 while (wfh && frp != NULL && frame_fixed_height(frp));
2895 }
2896 else
2897 {
2898 do
2899 frp = frp->fr_prev;
2900 while (wfh && frp != NULL && frame_fixed_height(frp));
2901 }
2902 /* Increase "height" if we could not reduce enough frames. */
2903 if (frp == NULL)
2904 height -= extra_lines;
2905 }
2906 }
2907 else if (extra_lines > 0)
2908 {
2909 /* increase height of bottom or top frame */
2910 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2911 }
2912 }
2913 topfrp->fr_height = height;
2914}
2915
2916/*
2917 * Return TRUE if height of frame "frp" should not be changed because of
2918 * the 'winfixheight' option.
2919 */
2920 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002921frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 /* frame with one window: fixed height if 'winfixheight' set. */
2924 if (frp->fr_win != NULL)
2925 return frp->fr_win->w_p_wfh;
2926
2927 if (frp->fr_layout == FR_ROW)
2928 {
2929 /* The frame is fixed height if one of the frames in the row is fixed
2930 * height. */
2931 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2932 if (frame_fixed_height(frp))
2933 return TRUE;
2934 return FALSE;
2935 }
2936
2937 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2938 * frames in the row are fixed height. */
2939 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2940 if (!frame_fixed_height(frp))
2941 return FALSE;
2942 return TRUE;
2943}
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002946 * Return TRUE if width of frame "frp" should not be changed because of
2947 * the 'winfixwidth' option.
2948 */
2949 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002950frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002951{
2952 /* frame with one window: fixed width if 'winfixwidth' set. */
2953 if (frp->fr_win != NULL)
2954 return frp->fr_win->w_p_wfw;
2955
2956 if (frp->fr_layout == FR_COL)
2957 {
2958 /* The frame is fixed width if one of the frames in the row is fixed
2959 * width. */
2960 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2961 if (frame_fixed_width(frp))
2962 return TRUE;
2963 return FALSE;
2964 }
2965
2966 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2967 * frames in the row are fixed width. */
2968 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2969 if (!frame_fixed_width(frp))
2970 return FALSE;
2971 return TRUE;
2972}
2973
2974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 * Add a status line to windows at the bottom of "frp".
2976 * Note: Does not check if there is room!
2977 */
2978 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002979frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 win_T *wp;
2982
2983 if (frp->fr_layout == FR_LEAF)
2984 {
2985 wp = frp->fr_win;
2986 if (wp->w_status_height == 0)
2987 {
2988 if (wp->w_height > 0) /* don't make it negative */
2989 --wp->w_height;
2990 wp->w_status_height = STATUS_HEIGHT;
2991 }
2992 }
2993 else if (frp->fr_layout == FR_ROW)
2994 {
2995 /* Handle all the frames in the row. */
2996 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2997 frame_add_statusline(frp);
2998 }
2999 else /* frp->fr_layout == FR_COL */
3000 {
3001 /* Only need to handle the last frame in the column. */
3002 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3003 ;
3004 frame_add_statusline(frp);
3005 }
3006}
3007
3008/*
3009 * Set width of a frame. Handles recursively going through contained frames.
3010 * May remove separator line for windows at the right side (for win_close()).
3011 */
3012 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003013frame_new_width(
3014 frame_T *topfrp,
3015 int width,
3016 int leftfirst, /* resize leftmost contained frame first */
3017 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003018 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019{
3020 frame_T *frp;
3021 int extra_cols;
3022 int w;
3023 win_T *wp;
3024
3025 if (topfrp->fr_layout == FR_LEAF)
3026 {
3027 /* Simple case: just one window. */
3028 wp = topfrp->fr_win;
3029 /* Find out if there are any windows right of this one. */
3030 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3031 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3032 break;
3033 if (frp->fr_parent == NULL)
3034 wp->w_vsep_width = 0;
3035 win_new_width(wp, width - wp->w_vsep_width);
3036 }
3037 else if (topfrp->fr_layout == FR_COL)
3038 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003039 do
3040 {
3041 /* All frames in this column get the same new width. */
3042 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3043 {
3044 frame_new_width(frp, width, leftfirst, wfw);
3045 if (frp->fr_width > width)
3046 {
3047 /* Could not fit the windows, make whole column wider. */
3048 width = frp->fr_width;
3049 break;
3050 }
3051 }
3052 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054 else /* fr_layout == FR_ROW */
3055 {
3056 /* Complicated case: Resize a row of frames. Resize the rightmost
3057 * frame first, frames left of it when needed. */
3058
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003060 if (wfw)
3061 /* Advance past frames with one window with 'wfw' set. */
3062 while (frame_fixed_width(frp))
3063 {
3064 frp = frp->fr_next;
3065 if (frp == NULL)
3066 return; /* no frame without 'wfw', give up */
3067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003069 {
3070 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 while (frp->fr_next != NULL)
3072 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003073 if (wfw)
3074 /* Advance back for frames with one window with 'wfw' set. */
3075 while (frame_fixed_width(frp))
3076 frp = frp->fr_prev;
3077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079 extra_cols = width - topfrp->fr_width;
3080 if (extra_cols < 0)
3081 {
3082 /* reduce frame width, rightmost frame first */
3083 while (frp != NULL)
3084 {
3085 w = frame_minwidth(frp, NULL);
3086 if (frp->fr_width + extra_cols < w)
3087 {
3088 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003089 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091 else
3092 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003093 frame_new_width(frp, frp->fr_width + extra_cols,
3094 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 break;
3096 }
3097 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003098 {
3099 do
3100 frp = frp->fr_next;
3101 while (wfw && frp != NULL && frame_fixed_width(frp));
3102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003104 {
3105 do
3106 frp = frp->fr_prev;
3107 while (wfw && frp != NULL && frame_fixed_width(frp));
3108 }
3109 /* Increase "width" if we could not reduce enough frames. */
3110 if (frp == NULL)
3111 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 }
3113 }
3114 else if (extra_cols > 0)
3115 {
3116 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003117 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
3119 }
3120 topfrp->fr_width = width;
3121}
3122
3123/*
3124 * Add the vertical separator to windows at the right side of "frp".
3125 * Note: Does not check if there is room!
3126 */
3127 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003128frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129{
3130 win_T *wp;
3131
3132 if (frp->fr_layout == FR_LEAF)
3133 {
3134 wp = frp->fr_win;
3135 if (wp->w_vsep_width == 0)
3136 {
3137 if (wp->w_width > 0) /* don't make it negative */
3138 --wp->w_width;
3139 wp->w_vsep_width = 1;
3140 }
3141 }
3142 else if (frp->fr_layout == FR_COL)
3143 {
3144 /* Handle all the frames in the column. */
3145 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3146 frame_add_vsep(frp);
3147 }
3148 else /* frp->fr_layout == FR_ROW */
3149 {
3150 /* Only need to handle the last frame in the row. */
3151 frp = frp->fr_child;
3152 while (frp->fr_next != NULL)
3153 frp = frp->fr_next;
3154 frame_add_vsep(frp);
3155 }
3156}
3157
3158/*
3159 * Set frame width from the window it contains.
3160 */
3161 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003162frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163{
3164 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3165}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
3167/*
3168 * Set frame height from the window it contains.
3169 */
3170 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003171frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3174}
3175
3176/*
3177 * Compute the minimal height for frame "topfrp".
3178 * Uses the 'winminheight' option.
3179 * When "next_curwin" isn't NULL, use p_wh for this window.
3180 * When "next_curwin" is NOWIN, don't use at least one line for the current
3181 * window.
3182 */
3183 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003184frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
3186 frame_T *frp;
3187 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 if (topfrp->fr_win != NULL)
3191 {
3192 if (topfrp->fr_win == next_curwin)
3193 m = p_wh + topfrp->fr_win->w_status_height;
3194 else
3195 {
3196 /* window: minimal height of the window plus status line */
3197 m = p_wmh + topfrp->fr_win->w_status_height;
3198 /* Current window is minimal one line high */
3199 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3200 ++m;
3201 }
3202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 else if (topfrp->fr_layout == FR_ROW)
3204 {
3205 /* get the minimal height from each frame in this row */
3206 m = 0;
3207 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3208 {
3209 n = frame_minheight(frp, next_curwin);
3210 if (n > m)
3211 m = n;
3212 }
3213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 else
3215 {
3216 /* Add up the minimal heights for all frames in this column. */
3217 m = 0;
3218 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3219 m += frame_minheight(frp, next_curwin);
3220 }
3221
3222 return m;
3223}
3224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225/*
3226 * Compute the minimal width for frame "topfrp".
3227 * When "next_curwin" isn't NULL, use p_wiw for this window.
3228 * When "next_curwin" is NOWIN, don't use at least one column for the current
3229 * window.
3230 */
3231 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003232frame_minwidth(
3233 frame_T *topfrp,
3234 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235{
3236 frame_T *frp;
3237 int m, n;
3238
3239 if (topfrp->fr_win != NULL)
3240 {
3241 if (topfrp->fr_win == next_curwin)
3242 m = p_wiw + topfrp->fr_win->w_vsep_width;
3243 else
3244 {
3245 /* window: minimal width of the window plus separator column */
3246 m = p_wmw + topfrp->fr_win->w_vsep_width;
3247 /* Current window is minimal one column wide */
3248 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3249 ++m;
3250 }
3251 }
3252 else if (topfrp->fr_layout == FR_COL)
3253 {
3254 /* get the minimal width from each frame in this column */
3255 m = 0;
3256 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3257 {
3258 n = frame_minwidth(frp, next_curwin);
3259 if (n > m)
3260 m = n;
3261 }
3262 }
3263 else
3264 {
3265 /* Add up the minimal widths for all frames in this row. */
3266 m = 0;
3267 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3268 m += frame_minwidth(frp, next_curwin);
3269 }
3270
3271 return m;
3272}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
3274
3275/*
3276 * Try to close all windows except current one.
3277 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3278 * used and the buffer was modified.
3279 *
3280 * Used by ":bdel" and ":only".
3281 */
3282 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003283close_others(
3284 int message,
3285 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 win_T *wp;
3288 win_T *nextwp;
3289 int r;
3290
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003291 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 {
3293 if (message
3294#ifdef FEAT_AUTOCMD
3295 && !autocmd_busy
3296#endif
3297 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003298 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 return;
3300 }
3301
3302 /* Be very careful here: autocommands may change the window layout. */
3303 for (wp = firstwin; win_valid(wp); wp = nextwp)
3304 {
3305 nextwp = wp->w_next;
3306 if (wp != curwin) /* don't close current window */
3307 {
3308
3309 /* Check if it's allowed to abandon this window */
3310 r = can_abandon(wp->w_buffer, forceit);
3311#ifdef FEAT_AUTOCMD
3312 if (!win_valid(wp)) /* autocommands messed wp up */
3313 {
3314 nextwp = firstwin;
3315 continue;
3316 }
3317#endif
3318 if (!r)
3319 {
3320#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3321 if (message && (p_confirm || cmdmod.confirm) && p_write)
3322 {
3323 dialog_changed(wp->w_buffer, FALSE);
3324# ifdef FEAT_AUTOCMD
3325 if (!win_valid(wp)) /* autocommands messed wp up */
3326 {
3327 nextwp = firstwin;
3328 continue;
3329 }
3330# endif
3331 }
3332 if (bufIsChanged(wp->w_buffer))
3333#endif
3334 continue;
3335 }
3336 win_close(wp, !P_HID(wp->w_buffer) && !bufIsChanged(wp->w_buffer));
3337 }
3338 }
3339
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003340 if (message && lastwin != firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 EMSG(_("E445: Other window contains changes"));
3342}
3343
3344#endif /* FEAT_WINDOWS */
3345
3346/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003347 * Init the current window "curwin".
3348 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 */
3350 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003351curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003353 win_init_empty(curwin);
3354}
3355
3356 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003357win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003358{
3359 redraw_win_later(wp, NOT_VALID);
3360 wp->w_lines_valid = 0;
3361 wp->w_cursor.lnum = 1;
3362 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003364 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003366 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3367 wp->w_pcmark.col = 0;
3368 wp->w_prev_pcmark.lnum = 0;
3369 wp->w_prev_pcmark.col = 0;
3370 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003372 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003374 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003376 if (wp->w_p_rl)
3377 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003379 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003381#ifdef FEAT_SYN_HL
3382 wp->w_s = &wp->w_buffer->b_s;
3383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384}
3385
3386/*
3387 * Allocate the first window and put an empty buffer in it.
3388 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003389 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003391 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003392win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003394 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003395 return FAIL;
3396
3397#ifdef FEAT_WINDOWS
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003398 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003399 if (first_tabpage == NULL)
3400 return FAIL;
3401 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003402 curtab = first_tabpage;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003403#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003404
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003405 return OK;
3406}
3407
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003408#if defined(FEAT_AUTOCMD) || defined(PROTO)
3409/*
3410 * Init "aucmd_win". This can only be done after the first
3411 * window is fully initialized, thus it can't be in win_alloc_first().
3412 */
3413 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003414win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003415{
3416 aucmd_win = win_alloc(NULL, TRUE);
3417 if (aucmd_win != NULL)
3418 {
3419 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003420 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003421 new_frame(aucmd_win);
3422 }
3423}
3424#endif
3425
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003426/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003427 * Allocate the first window or the first window in a new tab page.
3428 * When "oldwin" is NULL create an empty buffer for it.
3429 * When "oldwin" is not NULL copy info from it to the new window (only with
3430 * FEAT_WINDOWS).
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003431 * Return FAIL when something goes wrong (out of memory).
3432 */
3433 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003434win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003435{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003436 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003437 if (oldwin == NULL)
3438 {
3439 /* Very first window, need to create an empty buffer for it and
3440 * initialize from scratch. */
3441 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3442 if (curwin == NULL || curbuf == NULL)
3443 return FAIL;
3444 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003445#ifdef FEAT_SYN_HL
3446 curwin->w_s = &(curbuf->b_s);
3447#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003448 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449#ifdef FEAT_WINDOWS
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003450 curwin->w_alist = &global_alist;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003452 curwin_init(); /* init current window */
3453 }
3454#ifdef FEAT_WINDOWS
3455 else
3456 {
3457 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003458 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003459
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003460 /* We don't want cursor- and scroll-binding in the first window. */
3461 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003462 }
3463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003465 new_frame(curwin);
3466 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003467 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003468 topframe = curwin->w_frame;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003469#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 topframe->fr_width = Columns;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 topframe->fr_height = Rows - p_ch;
3473 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003474
3475 return OK;
3476}
3477
3478/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003479 * Create a frame for window "wp".
3480 */
3481 static void
3482new_frame(win_T *wp)
3483{
3484 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3485
3486 wp->w_frame = frp;
3487 if (frp != NULL)
3488 {
3489 frp->fr_layout = FR_LEAF;
3490 frp->fr_win = wp;
3491 }
3492}
3493
3494/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495 * Initialize the window and frame size to the maximum.
3496 */
3497 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003498win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003499{
3500 firstwin->w_height = ROWS_AVAIL;
3501 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003502#ifdef FEAT_WINDOWS
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003503 firstwin->w_width = Columns;
3504 topframe->fr_width = Columns;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01003505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506}
3507
3508#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003509
3510/*
3511 * Allocate a new tabpage_T and init the values.
3512 * Returns NULL when out of memory.
3513 */
3514 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003515alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003516{
3517 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003518# ifdef FEAT_GUI
3519 int i;
3520# endif
3521
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003522
3523 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003524 if (tp == NULL)
3525 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003526
Bram Moolenaar429fa852013-04-15 12:27:36 +02003527# ifdef FEAT_EVAL
3528 /* init t: variables */
3529 tp->tp_vars = dict_alloc();
3530 if (tp->tp_vars == NULL)
3531 {
3532 vim_free(tp);
3533 return NULL;
3534 }
3535 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3536# endif
3537
3538# ifdef FEAT_GUI
3539 for (i = 0; i < 3; i++)
3540 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003541# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003542# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003543 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003544# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003545 tp->tp_ch_used = p_ch;
3546
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003547 return tp;
3548}
3549
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003550 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003551free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003552{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003553 int idx;
3554
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003555# ifdef FEAT_DIFF
3556 diff_clear(tp);
3557# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003558 for (idx = 0; idx < SNAP_COUNT; ++idx)
3559 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003560#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003561 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3562 hash_init(&tp->tp_vars->dv_hashtab);
3563 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003564#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003565
3566#ifdef FEAT_PYTHON
3567 python_tabpage_free(tp);
3568#endif
3569
3570#ifdef FEAT_PYTHON3
3571 python3_tabpage_free(tp);
3572#endif
3573
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003574 vim_free(tp);
3575}
3576
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003577/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003578 * Create a new Tab page with one window.
3579 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003580 * When "after" is 0 put it just after the current Tab page.
3581 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582 * Return FAIL or OK.
3583 */
3584 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003585win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003586{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003587 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003588 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003589 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003590
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003591 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592 if (newtp == NULL)
3593 return FAIL;
3594
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003595 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003596 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003597 {
3598 vim_free(newtp);
3599 return FAIL;
3600 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003601 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003602
3603 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003604 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003605 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003606 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003607 if (after == 1)
3608 {
3609 /* New tab page becomes the first one. */
3610 newtp->tp_next = first_tabpage;
3611 first_tabpage = newtp;
3612 }
3613 else
3614 {
3615 if (after > 0)
3616 {
3617 /* Put new tab page before tab page "after". */
3618 n = 2;
3619 for (tp = first_tabpage; tp->tp_next != NULL
3620 && n < after; tp = tp->tp_next)
3621 ++n;
3622 }
3623 newtp->tp_next = tp->tp_next;
3624 tp->tp_next = newtp;
3625 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003626 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003627 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003628 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003629
3630 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003631 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003632
3633#if defined(FEAT_GUI)
3634 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3635 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003636 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003637#endif
3638
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003639 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003640#ifdef FEAT_AUTOCMD
Bram Moolenaarc917da42016-07-19 22:31:36 +02003641 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003642 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003643 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003644 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003645#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003646 return OK;
3647 }
3648
3649 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003650 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003651 return FAIL;
3652}
3653
3654/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003655 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3656 * like with ":split".
3657 * Returns OK if a new tab page was created, FAIL otherwise.
3658 */
3659 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003660may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003661{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003662 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003663
Bram Moolenaard326ce82007-03-11 14:48:29 +00003664 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003665 {
3666 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003667 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003668 return win_new_tabpage(n);
3669 }
3670 return FAIL;
3671}
3672
3673/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003674 * Create up to "maxcount" tabpages with empty windows.
3675 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003676 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003677 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003678make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003679{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003680 int count = maxcount;
3681 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003682
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003683 /* Limit to 'tabpagemax' tabs. */
3684 if (count > p_tpm)
3685 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003686
3687#ifdef FEAT_AUTOCMD
3688 /*
3689 * Don't execute autocommands while creating the tab pages. Must do that
3690 * when putting the buffers in the windows.
3691 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003692 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003693#endif
3694
3695 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003696 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003697 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003698
3699#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003700 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003701#endif
3702
3703 /* return actual number of tab pages */
3704 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705}
3706
3707/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003708 * Return TRUE when "tpc" points to a valid tab page.
3709 */
3710 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003711valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003712{
3713 tabpage_T *tp;
3714
3715 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
3716 if (tp == tpc)
3717 return TRUE;
3718 return FALSE;
3719}
3720
3721/*
3722 * Find tab page "n" (first one is 1). Returns NULL when not found.
3723 */
3724 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003725find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003726{
3727 tabpage_T *tp;
3728 int i = 1;
3729
3730 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3731 ++i;
3732 return tp;
3733}
3734
3735/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003736 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003737 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003738 */
3739 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003740tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003741{
3742 int i = 1;
3743 tabpage_T *tp;
3744
3745 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3746 ++i;
3747 return i;
3748}
3749
3750/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003751 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003752 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003753 * FAIL.
3754 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003755 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003756 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003757leave_tabpage(
3758 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003759 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003760 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003761{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003762 tabpage_T *tp = curtab;
3763
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003764 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003765#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003766 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003767 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003768 if (new_curbuf != curbuf)
3769 {
3770 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3771 if (curtab != tp)
3772 return FAIL;
3773 }
3774 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3775 if (curtab != tp)
3776 return FAIL;
3777 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003778 if (curtab != tp)
3779 return FAIL;
3780 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003781#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003782#if defined(FEAT_GUI)
3783 /* Remove the scrollbars. They may be added back later. */
3784 if (gui.in_use)
3785 gui_remove_scrollbars();
3786#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003787 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003788 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003789 tp->tp_firstwin = firstwin;
3790 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003791 tp->tp_old_Rows = Rows;
3792 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003793 firstwin = NULL;
3794 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003795 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003796}
3797
3798/*
3799 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003800 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003801 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3802 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003803 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003804 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003805enter_tabpage(
3806 tabpage_T *tp,
3807 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003808 int trigger_enter_autocmds,
3809 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003811 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003812 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003813
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003814 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003815 firstwin = tp->tp_firstwin;
3816 lastwin = tp->tp_lastwin;
3817 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003818
3819 /* We would like doing the TabEnter event first, but we don't have a
3820 * valid current window yet, which may break some commands.
3821 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003822 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003823 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003824 prevwin = next_prevwin;
3825
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003826 last_status(FALSE); /* status line may appear or disappear */
3827 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003828 must_redraw = CLEAR; /* need to redraw everything */
3829#ifdef FEAT_DIFF
3830 diff_need_scrollbind = TRUE;
3831#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003832
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003833 /* The tabpage line may have appeared or disappeared, may need to resize
3834 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003835 * frame sizes too. Use the stored value of p_ch, so that it can be
3836 * different for each tab page. */
3837 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003838 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3839#ifdef FEAT_GUI_TABLINE
3840 && !gui_use_tabline()
3841#endif
3842 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003843 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003844 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003845 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003846
3847#if defined(FEAT_GUI)
3848 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3849 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003850 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003851#endif
3852
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003853#ifdef FEAT_AUTOCMD
3854 /* Apply autocommands after updating the display, when 'rows' and
3855 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003856 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003857 {
3858 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3859 if (old_curbuf != curbuf)
3860 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3861 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003862#endif
3863
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003864 redraw_all_later(CLEAR);
3865}
3866
3867/*
3868 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003869 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003870 */
3871 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003872goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003873{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003874 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003875 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003876 int i;
3877
Bram Moolenaard68071d2006-05-02 22:08:30 +00003878 if (text_locked())
3879 {
3880 /* Not allowed when editing the command line. */
3881#ifdef FEAT_CMDWIN
3882 if (cmdwin_type != 0)
3883 EMSG(_(e_cmdwin));
3884 else
3885#endif
3886 EMSG(_(e_secure));
3887 return;
3888 }
3889
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003890 /* If there is only one it can't work. */
3891 if (first_tabpage->tp_next == NULL)
3892 {
3893 if (n > 1)
3894 beep_flush();
3895 return;
3896 }
3897
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003898 if (n == 0)
3899 {
3900 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003901 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003902 tp = first_tabpage;
3903 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003904 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003905 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003906 else if (n < 0)
3907 {
3908 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3909 * this N times. */
3910 ttp = curtab;
3911 for (i = n; i < 0; ++i)
3912 {
3913 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3914 tp = tp->tp_next)
3915 ;
3916 ttp = tp;
3917 }
3918 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003919 else if (n == 9999)
3920 {
3921 /* Go to last tab page. */
3922 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3923 ;
3924 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003925 else
3926 {
3927 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003928 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003929 if (tp == NULL)
3930 {
3931 beep_flush();
3932 return;
3933 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003934 }
3935
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003936 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003937
3938#ifdef FEAT_GUI_TABLINE
3939 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003940 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003941#endif
3942}
3943
3944/*
3945 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003946 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3947 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003948 * Note: doesn't update the GUI tab.
3949 */
3950 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003951goto_tabpage_tp(
3952 tabpage_T *tp,
3953 int trigger_enter_autocmds,
3954 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003955{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02003956 /* Don't repeat a message in another tab page. */
3957 set_keep_msg(NULL, 0);
3958
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003959 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
3960 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003961 {
3962 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003963 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
3964 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003965 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003966 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
3967 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003968 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970
3971/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003972 * Enter window "wp" in tab page "tp".
3973 * Also updates the GUI tab.
3974 */
3975 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003976goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003977{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003978 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003979 if (curtab == tp && win_valid(wp))
3980 {
3981 win_enter(wp, TRUE);
3982# ifdef FEAT_GUI_TABLINE
3983 if (gui_use_tabline())
3984 gui_mch_set_curtab(tabpage_index(curtab));
3985# endif
3986 }
3987}
3988
3989/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003990 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003991 */
3992 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003993tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003994{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02003995 int n = 1;
3996 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003997
3998 if (first_tabpage->tp_next == NULL)
3999 return;
4000
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004001 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4002 ++n;
4003
4004 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4005 && tp->tp_next == curtab))
4006 return;
4007
4008 tp_dst = tp;
4009
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004010 /* Remove the current tab page from the list of tab pages. */
4011 if (curtab == first_tabpage)
4012 first_tabpage = curtab->tp_next;
4013 else
4014 {
4015 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4016 if (tp->tp_next == curtab)
4017 break;
4018 if (tp == NULL) /* "cannot happen" */
4019 return;
4020 tp->tp_next = curtab->tp_next;
4021 }
4022
4023 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004024 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004025 {
4026 curtab->tp_next = first_tabpage;
4027 first_tabpage = curtab;
4028 }
4029 else
4030 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004031 curtab->tp_next = tp_dst->tp_next;
4032 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004033 }
4034
4035 /* Need to redraw the tabline. Tab page contents doesn't change. */
4036 redraw_tabline = TRUE;
4037}
4038
4039
4040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 * Go to another window.
4042 * When jumping to another buffer, stop Visual mode. Do this before
4043 * changing windows so we can yank the selection into the '*' register.
4044 * When jumping to another window on the same buffer, adjust its cursor
4045 * position to keep the same Visual area.
4046 */
4047 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004048win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004050#ifdef FEAT_CONCEAL
4051 win_T *owp = curwin;
4052#endif
4053
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004054 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
4056 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004057 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 return;
4059 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_AUTOCMD
4061 if (curbuf_locked())
4062 return;
4063#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (wp->w_buffer != curbuf)
4066 reset_VIsual_and_resel();
4067 else if (VIsual_active)
4068 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070#ifdef FEAT_GUI
4071 need_mouse_correct = TRUE;
4072#endif
4073 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004074
4075#ifdef FEAT_CONCEAL
4076 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004077 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004078 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004079 if (curwin->w_p_cole > 0 && !msg_scrolled)
4080 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082}
4083
4084#if defined(FEAT_PERL) || defined(PROTO)
4085/*
4086 * Find window number "winnr" (counting top to bottom).
4087 */
4088 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004089win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090{
4091 win_T *wp;
4092
4093# ifdef FEAT_WINDOWS
4094 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4095 if (--winnr == 0)
4096 break;
4097 return wp;
4098# else
4099 return curwin;
4100# endif
4101}
4102#endif
4103
Bram Moolenaar6fa41fb2013-05-18 20:55:35 +02004104#if (defined(FEAT_WINDOWS) && (defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) \
4105 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004106/*
4107 * Find the tabpage for window "win".
4108 */
4109 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004110win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004111{
4112 win_T *wp;
4113 tabpage_T *tp;
4114
4115 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar5e6d5ca2013-07-03 14:01:56 +02004116 for (wp = (tp == curtab ? firstwin : tp->tp_firstwin);
4117 wp != NULL; wp = wp->w_next)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004118 if (wp == win)
4119 return tp;
4120 return NULL;
4121}
4122#endif
4123
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124/*
4125 * Move to window above or below "count" times.
4126 */
4127 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004128win_goto_ver(
4129 int up, /* TRUE to go to win above */
4130 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
4132 frame_T *fr;
4133 frame_T *nfr;
4134 frame_T *foundfr;
4135
4136 foundfr = curwin->w_frame;
4137 while (count--)
4138 {
4139 /*
4140 * First go upwards in the tree of frames until we find a upwards or
4141 * downwards neighbor.
4142 */
4143 fr = foundfr;
4144 for (;;)
4145 {
4146 if (fr == topframe)
4147 goto end;
4148 if (up)
4149 nfr = fr->fr_prev;
4150 else
4151 nfr = fr->fr_next;
4152 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4153 break;
4154 fr = fr->fr_parent;
4155 }
4156
4157 /*
4158 * Now go downwards to find the bottom or top frame in it.
4159 */
4160 for (;;)
4161 {
4162 if (nfr->fr_layout == FR_LEAF)
4163 {
4164 foundfr = nfr;
4165 break;
4166 }
4167 fr = nfr->fr_child;
4168 if (nfr->fr_layout == FR_ROW)
4169 {
4170 /* Find the frame at the cursor row. */
4171 while (fr->fr_next != NULL
4172 && frame2win(fr)->w_wincol + fr->fr_width
4173 <= curwin->w_wincol + curwin->w_wcol)
4174 fr = fr->fr_next;
4175 }
4176 if (nfr->fr_layout == FR_COL && up)
4177 while (fr->fr_next != NULL)
4178 fr = fr->fr_next;
4179 nfr = fr;
4180 }
4181 }
4182end:
4183 if (foundfr != NULL)
4184 win_goto(foundfr->fr_win);
4185}
4186
4187/*
4188 * Move to left or right window.
4189 */
4190 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004191win_goto_hor(
4192 int left, /* TRUE to go to left win */
4193 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194{
4195 frame_T *fr;
4196 frame_T *nfr;
4197 frame_T *foundfr;
4198
4199 foundfr = curwin->w_frame;
4200 while (count--)
4201 {
4202 /*
4203 * First go upwards in the tree of frames until we find a left or
4204 * right neighbor.
4205 */
4206 fr = foundfr;
4207 for (;;)
4208 {
4209 if (fr == topframe)
4210 goto end;
4211 if (left)
4212 nfr = fr->fr_prev;
4213 else
4214 nfr = fr->fr_next;
4215 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4216 break;
4217 fr = fr->fr_parent;
4218 }
4219
4220 /*
4221 * Now go downwards to find the leftmost or rightmost frame in it.
4222 */
4223 for (;;)
4224 {
4225 if (nfr->fr_layout == FR_LEAF)
4226 {
4227 foundfr = nfr;
4228 break;
4229 }
4230 fr = nfr->fr_child;
4231 if (nfr->fr_layout == FR_COL)
4232 {
4233 /* Find the frame at the cursor row. */
4234 while (fr->fr_next != NULL
4235 && frame2win(fr)->w_winrow + fr->fr_height
4236 <= curwin->w_winrow + curwin->w_wrow)
4237 fr = fr->fr_next;
4238 }
4239 if (nfr->fr_layout == FR_ROW && left)
4240 while (fr->fr_next != NULL)
4241 fr = fr->fr_next;
4242 nfr = fr;
4243 }
4244 }
4245end:
4246 if (foundfr != NULL)
4247 win_goto(foundfr->fr_win);
4248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250/*
4251 * Make window "wp" the current window.
4252 */
4253 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004254win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004256 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257}
4258
4259/*
4260 * Make window wp the current window.
4261 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4262 * been closed and isn't valid.
4263 */
4264 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004265win_enter_ext(
4266 win_T *wp,
4267 int undo_sync,
4268 int curwin_invalid,
Bram Moolenaarc917da42016-07-19 22:31:36 +02004269 int trigger_new_autocmds UNUSED,
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004270 int trigger_enter_autocmds UNUSED,
4271 int trigger_leave_autocmds UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
4273#ifdef FEAT_AUTOCMD
4274 int other_buffer = FALSE;
4275#endif
4276
4277 if (wp == curwin && !curwin_invalid) /* nothing to do */
4278 return;
4279
4280#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004281 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
4283 /*
4284 * Be careful: If autocommands delete the window, return now.
4285 */
4286 if (wp->w_buffer != curbuf)
4287 {
4288 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4289 other_buffer = TRUE;
4290 if (!win_valid(wp))
4291 return;
4292 }
4293 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4294 if (!win_valid(wp))
4295 return;
4296# ifdef FEAT_EVAL
4297 /* autocmds may abort script processing */
4298 if (aborting())
4299 return;
4300# endif
4301 }
4302#endif
4303
4304 /* sync undo before leaving the current buffer */
4305 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004306 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004307
4308 /* Might need to scroll the old window before switching, e.g., when the
4309 * cursor was moved. */
4310 update_topline();
4311
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 /* may have to copy the buffer options when 'cpo' contains 'S' */
4313 if (wp->w_buffer != curbuf)
4314 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4315 if (!curwin_invalid)
4316 {
4317 prevwin = curwin; /* remember for CTRL-W p */
4318 curwin->w_redr_status = TRUE;
4319 }
4320 curwin = wp;
4321 curbuf = wp->w_buffer;
4322 check_cursor();
4323#ifdef FEAT_VIRTUALEDIT
4324 if (!virtual_active())
4325 curwin->w_cursor.coladd = 0;
4326#endif
4327 changed_line_abv_curs(); /* assume cursor position needs updating */
4328
4329 if (curwin->w_localdir != NULL)
4330 {
4331 /* Window has a local directory: Save current directory as global
4332 * directory (unless that was done already) and change to the local
4333 * directory. */
4334 if (globaldir == NULL)
4335 {
4336 char_u cwd[MAXPATHL];
4337
4338 if (mch_dirname(cwd, MAXPATHL) == OK)
4339 globaldir = vim_strsave(cwd);
4340 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004341 if (mch_chdir((char *)curwin->w_localdir) == 0)
4342 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 else if (globaldir != NULL)
4345 {
4346 /* Window doesn't have a local directory and we are not in the global
4347 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004348 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 vim_free(globaldir);
4350 globaldir = NULL;
4351 shorten_fnames(TRUE);
4352 }
4353
4354#ifdef FEAT_AUTOCMD
Bram Moolenaarc917da42016-07-19 22:31:36 +02004355 if (trigger_new_autocmds)
4356 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004357 if (trigger_enter_autocmds)
4358 {
4359 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4360 if (other_buffer)
4361 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363#endif
4364
4365#ifdef FEAT_TITLE
4366 maketitle();
4367#endif
4368 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004369 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (restart_edit)
4371 redraw_later(VALID); /* causes status line redraw */
4372
4373 /* set window height to desired minimal value */
4374 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4375 win_setheight((int)p_wh);
4376 else if (curwin->w_height == 0)
4377 win_setheight(1);
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004380 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
4383#ifdef FEAT_MOUSE
4384 setmouse(); /* in case jumped to/from help buffer */
4385#endif
4386
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004387 /* Change directories when the 'acd' option is set. */
4388 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389}
4390
4391#endif /* FEAT_WINDOWS */
4392
4393#if defined(FEAT_WINDOWS) || defined(FEAT_SIGNS) || defined(PROTO)
4394/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004395 * Jump to the first open window that contains buffer "buf", if one exists.
4396 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 */
4398 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004399buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004401 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004403 if (curwin->w_buffer == buf)
4404 wp = curwin;
4405# ifdef FEAT_WINDOWS
4406 else
4407 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4408 if (wp->w_buffer == buf)
4409 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 if (wp != NULL)
4411 win_enter(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004413 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004415
4416/*
4417 * Jump to the first open window in any tab page that contains buffer "buf",
4418 * if one exists.
4419 * Returns a pointer to the window found, otherwise NULL.
4420 */
4421 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004422buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004423{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004424 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004425# ifdef FEAT_WINDOWS
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004426 tabpage_T *tp;
4427
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004428 if (wp != NULL)
4429 return wp;
4430
4431 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
4432 if (tp != curtab)
4433 {
4434 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4435 if (wp->w_buffer == buf)
4436 break;
4437 if (wp != NULL)
4438 {
4439 goto_tabpage_win(tp, wp);
4440 if (curwin != wp)
4441 wp = NULL; /* something went wrong */
4442 break;
4443 }
4444 }
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004445# endif
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004446 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004447}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448#endif
4449
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004450static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004451
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004453 * Allocate a window structure and link it in the window list when "hidden" is
4454 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004457win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004459 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
4461 /*
4462 * allocate window structure and linesizes arrays
4463 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004464 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004465 if (new_wp == NULL)
4466 return NULL;
4467
4468 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004470 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004471 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473
Bram Moolenaar86edef62016-03-13 18:07:30 +01004474 new_wp->w_id = ++last_win_id;
4475
Bram Moolenaar429fa852013-04-15 12:27:36 +02004476#ifdef FEAT_EVAL
4477 /* init w: variables */
4478 new_wp->w_vars = dict_alloc();
4479 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004481 win_free_lsize(new_wp);
4482 vim_free(new_wp);
4483 return NULL;
4484 }
4485 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004486#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004487
4488#ifdef FEAT_AUTOCMD
4489 /* Don't execute autocommands while the window is not properly
4490 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4491 * event. */
4492 block_autocmds();
4493#endif
4494 /*
4495 * link the window in the window list
4496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497#ifdef FEAT_WINDOWS
Bram Moolenaar429fa852013-04-15 12:27:36 +02004498 if (!hidden)
4499 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004500 new_wp->w_wincol = 0;
4501 new_wp->w_width = Columns;
Bram Moolenaar829c8e32016-03-19 23:07:23 +01004502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
Bram Moolenaar429fa852013-04-15 12:27:36 +02004504 /* position the display and the cursor at the top of the file. */
4505 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004507 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004509 new_wp->w_botline = 2;
4510 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004512 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513#endif
4514
Bram Moolenaar429fa852013-04-15 12:27:36 +02004515 /* We won't calculate w_fraction until resizing the window */
4516 new_wp->w_fraction = 0;
4517 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518
4519#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004520 if (gui.in_use)
4521 {
4522 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4523 SBAR_LEFT, new_wp);
4524 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4525 SBAR_RIGHT, new_wp);
4526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527#endif
4528#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004529 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004531#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004532 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004533#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004534#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004535 new_wp->w_match_head = NULL;
4536 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004537#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004538 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539}
4540
4541#if defined(FEAT_WINDOWS) || defined(PROTO)
4542
4543/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004544 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 */
4546 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004547win_free(
4548 win_T *wp,
4549 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
4551 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004552 buf_T *buf;
4553 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004555#ifdef FEAT_FOLDING
4556 clearFolding(wp);
4557#endif
4558
4559 /* reduce the reference count to the argument list. */
4560 alist_unlink(wp->w_alist);
4561
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004562#ifdef FEAT_AUTOCMD
4563 /* Don't execute autocommands while the window is halfway being deleted.
4564 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004565 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004566#endif
4567
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004568#ifdef FEAT_LUA
4569 lua_window_free(wp);
4570#endif
4571
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004572#ifdef FEAT_MZSCHEME
4573 mzscheme_window_free(wp);
4574#endif
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#ifdef FEAT_PERL
4577 perl_win_free(wp);
4578#endif
4579
4580#ifdef FEAT_PYTHON
4581 python_window_free(wp);
4582#endif
4583
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004584#ifdef FEAT_PYTHON3
4585 python3_window_free(wp);
4586#endif
4587
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588#ifdef FEAT_TCL
4589 tcl_window_free(wp);
4590#endif
4591
4592#ifdef FEAT_RUBY
4593 ruby_window_free(wp);
4594#endif
4595
4596 clear_winopt(&wp->w_onebuf_opt);
4597 clear_winopt(&wp->w_allbuf_opt);
4598
4599#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004600 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4601 hash_init(&wp->w_vars->dv_hashtab);
4602 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603#endif
4604
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004605 {
4606 tabpage_T *ttp;
4607
4608 if (prevwin == wp)
4609 prevwin = NULL;
4610 for (ttp = first_tabpage; ttp != NULL; ttp = ttp->tp_next)
4611 if (ttp->tp_prevwin == wp)
4612 ttp->tp_prevwin = NULL;
4613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 win_free_lsize(wp);
4615
4616 for (i = 0; i < wp->w_tagstacklen; ++i)
4617 vim_free(wp->w_tagstack[i].tagname);
4618
4619 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004620
Bram Moolenaarff18df02013-07-24 17:51:57 +02004621 /* Remove the window from the b_wininfo lists, it may happen that the
4622 * freed memory is re-used for another window. */
4623 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4624 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4625 if (wip->wi_win == wp)
4626 wip->wi_win = NULL;
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004629 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004631
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632#ifdef FEAT_JUMPLIST
4633 free_jumplist(wp);
4634#endif
4635
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004636#ifdef FEAT_QUICKFIX
4637 qf_free_all(wp);
4638#endif
4639
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640#ifdef FEAT_GUI
4641 if (gui.in_use)
4642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4644 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4645 }
4646#endif /* FEAT_GUI */
4647
Bram Moolenaar860cae12010-06-05 23:22:07 +02004648#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004649 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004650#endif
4651
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004652#ifdef FEAT_AUTOCMD
4653 if (wp != aucmd_win)
4654#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004655 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004656#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004657 if (autocmd_busy)
4658 {
4659 wp->w_next = au_pending_free_win;
4660 au_pending_free_win = wp;
4661 }
4662 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004663#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004664 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004665
4666#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004667 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669}
4670
4671/*
4672 * Append window "wp" in the window list after window "after".
4673 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004674 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004675win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 win_T *before;
4678
4679 if (after == NULL) /* after NULL is in front of the first */
4680 before = firstwin;
4681 else
4682 before = after->w_next;
4683
4684 wp->w_next = before;
4685 wp->w_prev = after;
4686 if (after == NULL)
4687 firstwin = wp;
4688 else
4689 after->w_next = wp;
4690 if (before == NULL)
4691 lastwin = wp;
4692 else
4693 before->w_prev = wp;
4694}
4695
4696/*
4697 * Remove a window from the window list.
4698 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004699 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004700win_remove(
4701 win_T *wp,
4702 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704 if (wp->w_prev != NULL)
4705 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004706 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004708 else
4709 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 if (wp->w_next != NULL)
4711 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004712 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004714 else
4715 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716}
4717
4718/*
4719 * Append frame "frp" in a frame list after frame "after".
4720 */
4721 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004722frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 frp->fr_next = after->fr_next;
4725 after->fr_next = frp;
4726 if (frp->fr_next != NULL)
4727 frp->fr_next->fr_prev = frp;
4728 frp->fr_prev = after;
4729}
4730
4731/*
4732 * Insert frame "frp" in a frame list before frame "before".
4733 */
4734 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004735frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
4737 frp->fr_next = before;
4738 frp->fr_prev = before->fr_prev;
4739 before->fr_prev = frp;
4740 if (frp->fr_prev != NULL)
4741 frp->fr_prev->fr_next = frp;
4742 else
4743 frp->fr_parent->fr_child = frp;
4744}
4745
4746/*
4747 * Remove a frame from a frame list.
4748 */
4749 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004750frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 if (frp->fr_prev != NULL)
4753 frp->fr_prev->fr_next = frp->fr_next;
4754 else
4755 frp->fr_parent->fr_child = frp->fr_next;
4756 if (frp->fr_next != NULL)
4757 frp->fr_next->fr_prev = frp->fr_prev;
4758}
4759
4760#endif /* FEAT_WINDOWS */
4761
4762/*
4763 * Allocate w_lines[] for window "wp".
4764 * Return FAIL for failure, OK for success.
4765 */
4766 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004767win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004770 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 if (wp->w_lines == NULL)
4772 return FAIL;
4773 return OK;
4774}
4775
4776/*
4777 * free lsize arrays for a window
4778 */
4779 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004780win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004782 /* TODO: why would wp be NULL here? */
4783 if (wp != NULL)
4784 {
4785 vim_free(wp->w_lines);
4786 wp->w_lines = NULL;
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788}
4789
4790/*
4791 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004792 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 */
4794 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004795shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004797 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799 if (firstwin == NULL) /* not initialized yet */
4800 return;
4801#ifdef FEAT_WINDOWS
4802 if (h < frame_minheight(topframe, NULL))
4803 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004804
4805 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 * that doesn't result in the right height, forget about that option. */
4807 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004808 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 frame_new_height(topframe, h, FALSE, FALSE);
4810
4811 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4812#else
4813 if (h < 1)
4814 h = 1;
4815 win_new_height(firstwin, h);
4816#endif
4817 compute_cmdrow();
Bram Moolenaar05159a02005-02-26 23:04:13 +00004818#ifdef FEAT_WINDOWS
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004819 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004820#endif
4821
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#if 0
4823 /* Disabled: don't want making the screen smaller make a window larger. */
4824 if (p_ea)
4825 win_equal(curwin, FALSE, 'v');
4826#endif
4827}
4828
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004829#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830/*
4831 * Called from win_new_shellsize() after Columns changed.
4832 */
4833 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004834shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
4836 if (firstwin == NULL) /* not initialized yet */
4837 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004838
4839 /* First try setting the widths of windows with 'winfixwidth'. If that
4840 * doesn't result in the right width, forget about that option. */
4841 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004842 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004843 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4846#if 0
4847 /* Disabled: don't want making the screen smaller make a window larger. */
4848 if (p_ea)
4849 win_equal(curwin, FALSE, 'h');
4850#endif
4851}
4852#endif
4853
4854#if defined(FEAT_CMDWIN) || defined(PROTO)
4855/*
4856 * Save the size of all windows in "gap".
4857 */
4858 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004859win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861{
4862 win_T *wp;
4863
4864 ga_init2(gap, (int)sizeof(int), 1);
4865 if (ga_grow(gap, win_count() * 2) == OK)
4866 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4867 {
4868 ((int *)gap->ga_data)[gap->ga_len++] =
4869 wp->w_width + wp->w_vsep_width;
4870 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4871 }
4872}
4873
4874/*
4875 * Restore window sizes, but only if the number of windows is still the same.
4876 * Does not free the growarray.
4877 */
4878 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004879win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004882 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 if (win_count() * 2 == gap->ga_len)
4885 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004886 /* The order matters, because frames contain other frames, but it's
4887 * difficult to get right. The easy way out is to do it twice. */
4888 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004890 i = 0;
4891 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4892 {
4893 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4894 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 /* recompute the window positions */
4898 (void)win_comp_pos();
4899 }
4900}
4901#endif /* FEAT_CMDWIN */
4902
4903#if defined(FEAT_WINDOWS) || defined(PROTO)
4904/*
4905 * Update the position for all windows, using the width and height of the
4906 * frames.
4907 * Returns the row just after the last window.
4908 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004909 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004910win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004912 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 int col = 0;
4914
4915 frame_comp_pos(topframe, &row, &col);
4916 return row;
4917}
4918
4919/*
4920 * Update the position of the windows in frame "topfrp", using the width and
4921 * height of the frames.
4922 * "*row" and "*col" are the top-left position of the frame. They are updated
4923 * to the bottom-right position plus one.
4924 */
4925 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004926frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
4928 win_T *wp;
4929 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 int startcol;
4931 int startrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932
4933 wp = topfrp->fr_win;
4934 if (wp != NULL)
4935 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004936 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 /* position changed, redraw */
4939 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 redraw_win_later(wp, NOT_VALID);
4942 wp->w_redr_status = TRUE;
4943 }
4944 *row += wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 else
4948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 startrow = *row;
4950 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 if (topfrp->fr_layout == FR_ROW)
4954 *row = startrow; /* all frames are at the same row */
4955 else
4956 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 frame_comp_pos(frp, row, col);
4958 }
4959 }
4960}
4961
4962#endif /* FEAT_WINDOWS */
4963
4964/*
4965 * Set current window height and take care of repositioning other windows to
4966 * fit around it.
4967 */
4968 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004969win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
4971 win_setheight_win(height, curwin);
4972}
4973
4974/*
4975 * Set the window height of window "win" and take care of repositioning other
4976 * windows to fit around it.
4977 */
4978 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004979win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
4981 int row;
4982
4983 if (win == curwin)
4984 {
4985 /* Always keep current window at least one line high, even when
4986 * 'winminheight' is zero. */
4987#ifdef FEAT_WINDOWS
4988 if (height < p_wmh)
4989 height = p_wmh;
4990#endif
4991 if (height == 0)
4992 height = 1;
4993 }
4994
4995#ifdef FEAT_WINDOWS
4996 frame_setheight(win->w_frame, height + win->w_status_height);
4997
4998 /* recompute the window positions */
4999 row = win_comp_pos();
5000#else
5001 if (height > topframe->fr_height)
5002 height = topframe->fr_height;
5003 win->w_height = height;
5004 row = height;
5005#endif
5006
5007 /*
5008 * If there is extra space created between the last window and the command
5009 * line, clear it.
5010 */
5011 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5012 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5013 cmdline_row = row;
5014 msg_row = row;
5015 msg_col = 0;
5016
5017 redraw_all_later(NOT_VALID);
5018}
5019
5020#if defined(FEAT_WINDOWS) || defined(PROTO)
5021
5022/*
5023 * Set the height of a frame to "height" and take care that all frames and
5024 * windows inside it are resized. Also resize frames on the left and right if
5025 * the are in the same FR_ROW frame.
5026 *
5027 * Strategy:
5028 * If the frame is part of a FR_COL frame, try fitting the frame in that
5029 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5030 * go to containing frames to resize them and make room.
5031 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5032 * Check for the minimal height of the FR_ROW frame.
5033 * At the top level we can also use change the command line height.
5034 */
5035 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005036frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
5038 int room; /* total number of lines available */
5039 int take; /* number of lines taken from other windows */
5040 int room_cmdline; /* lines available from cmdline */
5041 int run;
5042 frame_T *frp;
5043 int h;
5044 int room_reserved;
5045
5046 /* If the height already is the desired value, nothing to do. */
5047 if (curfrp->fr_height == height)
5048 return;
5049
5050 if (curfrp->fr_parent == NULL)
5051 {
5052 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005053 if (height > ROWS_AVAIL)
5054 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (height > 0)
5056 frame_new_height(curfrp, height, FALSE, FALSE);
5057 }
5058 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5059 {
5060 /* Row of frames: Also need to resize frames left and right of this
5061 * one. First check for the minimal height of these. */
5062 h = frame_minheight(curfrp->fr_parent, NULL);
5063 if (height < h)
5064 height = h;
5065 frame_setheight(curfrp->fr_parent, height);
5066 }
5067 else
5068 {
5069 /*
5070 * Column of frames: try to change only frames in this column.
5071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 /*
5073 * Do this twice:
5074 * 1: compute room available, if it's not enough try resizing the
5075 * containing frame.
5076 * 2: compute the room available and adjust the height to it.
5077 * Try not to reduce the height of a window with 'winfixheight' set.
5078 */
5079 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 {
5081 room = 0;
5082 room_reserved = 0;
5083 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5084 frp = frp->fr_next)
5085 {
5086 if (frp != curfrp
5087 && frp->fr_win != NULL
5088 && frp->fr_win->w_p_wfh)
5089 room_reserved += frp->fr_height;
5090 room += frp->fr_height;
5091 if (frp != curfrp)
5092 room -= frame_minheight(frp, NULL);
5093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (curfrp->fr_width != Columns)
5095 room_cmdline = 0;
5096 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5099 + lastwin->w_height + lastwin->w_status_height);
5100 if (room_cmdline < 0)
5101 room_cmdline = 0;
5102 }
5103
5104 if (height <= room + room_cmdline)
5105 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
5108 if (height > room + room_cmdline)
5109 height = room + room_cmdline;
5110 break;
5111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 frame_setheight(curfrp->fr_parent, height
5113 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115
5116 /*
5117 * Compute the number of lines we will take from others frames (can be
5118 * negative!).
5119 */
5120 take = height - curfrp->fr_height;
5121
5122 /* If there is not enough room, also reduce the height of a window
5123 * with 'winfixheight' set. */
5124 if (height > room + room_cmdline - room_reserved)
5125 room_reserved = room + room_cmdline - height;
5126 /* If there is only a 'winfixheight' window and making the
5127 * window smaller, need to make the other window taller. */
5128 if (take < 0 && room - curfrp->fr_height < room_reserved)
5129 room_reserved = 0;
5130
5131 if (take > 0 && room_cmdline > 0)
5132 {
5133 /* use lines from cmdline first */
5134 if (take < room_cmdline)
5135 room_cmdline = take;
5136 take -= room_cmdline;
5137 topframe->fr_height += room_cmdline;
5138 }
5139
5140 /*
5141 * set the current frame to the new height
5142 */
5143 frame_new_height(curfrp, height, FALSE, FALSE);
5144
5145 /*
5146 * First take lines from the frames after the current frame. If
5147 * that is not enough, takes lines from frames above the current
5148 * frame.
5149 */
5150 for (run = 0; run < 2; ++run)
5151 {
5152 if (run == 0)
5153 frp = curfrp->fr_next; /* 1st run: start with next window */
5154 else
5155 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5156 while (frp != NULL && take != 0)
5157 {
5158 h = frame_minheight(frp, NULL);
5159 if (room_reserved > 0
5160 && frp->fr_win != NULL
5161 && frp->fr_win->w_p_wfh)
5162 {
5163 if (room_reserved >= frp->fr_height)
5164 room_reserved -= frp->fr_height;
5165 else
5166 {
5167 if (frp->fr_height - room_reserved > take)
5168 room_reserved = frp->fr_height - take;
5169 take -= frp->fr_height - room_reserved;
5170 frame_new_height(frp, room_reserved, FALSE, FALSE);
5171 room_reserved = 0;
5172 }
5173 }
5174 else
5175 {
5176 if (frp->fr_height - take < h)
5177 {
5178 take -= frp->fr_height - h;
5179 frame_new_height(frp, h, FALSE, FALSE);
5180 }
5181 else
5182 {
5183 frame_new_height(frp, frp->fr_height - take,
5184 FALSE, FALSE);
5185 take = 0;
5186 }
5187 }
5188 if (run == 0)
5189 frp = frp->fr_next;
5190 else
5191 frp = frp->fr_prev;
5192 }
5193 }
5194 }
5195}
5196
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197/*
5198 * Set current window width and take care of repositioning other windows to
5199 * fit around it.
5200 */
5201 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005202win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
5204 win_setwidth_win(width, curwin);
5205}
5206
5207 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005208win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 /* Always keep current window at least one column wide, even when
5211 * 'winminwidth' is zero. */
5212 if (wp == curwin)
5213 {
5214 if (width < p_wmw)
5215 width = p_wmw;
5216 if (width == 0)
5217 width = 1;
5218 }
5219
5220 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5221
5222 /* recompute the window positions */
5223 (void)win_comp_pos();
5224
5225 redraw_all_later(NOT_VALID);
5226}
5227
5228/*
5229 * Set the width of a frame to "width" and take care that all frames and
5230 * windows inside it are resized. Also resize frames above and below if the
5231 * are in the same FR_ROW frame.
5232 *
5233 * Strategy is similar to frame_setheight().
5234 */
5235 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005236frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 int room; /* total number of lines available */
5239 int take; /* number of lines taken from other windows */
5240 int run;
5241 frame_T *frp;
5242 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005243 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 /* If the width already is the desired value, nothing to do. */
5246 if (curfrp->fr_width == width)
5247 return;
5248
5249 if (curfrp->fr_parent == NULL)
5250 /* topframe: can't change width */
5251 return;
5252
5253 if (curfrp->fr_parent->fr_layout == FR_COL)
5254 {
5255 /* Column of frames: Also need to resize frames above and below of
5256 * this one. First check for the minimal width of these. */
5257 w = frame_minwidth(curfrp->fr_parent, NULL);
5258 if (width < w)
5259 width = w;
5260 frame_setwidth(curfrp->fr_parent, width);
5261 }
5262 else
5263 {
5264 /*
5265 * Row of frames: try to change only frames in this row.
5266 *
5267 * Do this twice:
5268 * 1: compute room available, if it's not enough try resizing the
5269 * containing frame.
5270 * 2: compute the room available and adjust the width to it.
5271 */
5272 for (run = 1; run <= 2; ++run)
5273 {
5274 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005275 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5277 frp = frp->fr_next)
5278 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005279 if (frp != curfrp
5280 && frp->fr_win != NULL
5281 && frp->fr_win->w_p_wfw)
5282 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 room += frp->fr_width;
5284 if (frp != curfrp)
5285 room -= frame_minwidth(frp, NULL);
5286 }
5287
5288 if (width <= room)
5289 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005290 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
5292 if (width > room)
5293 width = room;
5294 break;
5295 }
5296 frame_setwidth(curfrp->fr_parent, width
5297 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5298 }
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 /*
5301 * Compute the number of lines we will take from others frames (can be
5302 * negative!).
5303 */
5304 take = width - curfrp->fr_width;
5305
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005306 /* If there is not enough room, also reduce the width of a window
5307 * with 'winfixwidth' set. */
5308 if (width > room - room_reserved)
5309 room_reserved = room - width;
5310 /* If there is only a 'winfixwidth' window and making the
5311 * window smaller, need to make the other window narrower. */
5312 if (take < 0 && room - curfrp->fr_width < room_reserved)
5313 room_reserved = 0;
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 /*
5316 * set the current frame to the new width
5317 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005318 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 /*
5321 * First take lines from the frames right of the current frame. If
5322 * that is not enough, takes lines from frames left of the current
5323 * frame.
5324 */
5325 for (run = 0; run < 2; ++run)
5326 {
5327 if (run == 0)
5328 frp = curfrp->fr_next; /* 1st run: start with next window */
5329 else
5330 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5331 while (frp != NULL && take != 0)
5332 {
5333 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005334 if (room_reserved > 0
5335 && frp->fr_win != NULL
5336 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005338 if (room_reserved >= frp->fr_width)
5339 room_reserved -= frp->fr_width;
5340 else
5341 {
5342 if (frp->fr_width - room_reserved > take)
5343 room_reserved = frp->fr_width - take;
5344 take -= frp->fr_width - room_reserved;
5345 frame_new_width(frp, room_reserved, FALSE, FALSE);
5346 room_reserved = 0;
5347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
5349 else
5350 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005351 if (frp->fr_width - take < w)
5352 {
5353 take -= frp->fr_width - w;
5354 frame_new_width(frp, w, FALSE, FALSE);
5355 }
5356 else
5357 {
5358 frame_new_width(frp, frp->fr_width - take,
5359 FALSE, FALSE);
5360 take = 0;
5361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363 if (run == 0)
5364 frp = frp->fr_next;
5365 else
5366 frp = frp->fr_prev;
5367 }
5368 }
5369 }
5370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372/*
5373 * Check 'winminheight' for a valid value.
5374 */
5375 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005376win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377{
5378 int room;
5379 int first = TRUE;
5380 win_T *wp;
5381
5382 /* loop until there is a 'winminheight' that is possible */
5383 while (p_wmh > 0)
5384 {
5385 /* TODO: handle vertical splits */
5386 room = -p_wh;
5387 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5388 room += wp->w_height - p_wmh;
5389 if (room >= 0)
5390 break;
5391 --p_wmh;
5392 if (first)
5393 {
5394 EMSG(_(e_noroom));
5395 first = FALSE;
5396 }
5397 }
5398}
5399
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005400#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401
5402/*
5403 * Status line of dragwin is dragged "offset" lines down (negative is up).
5404 */
5405 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005406win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
5408 frame_T *curfr;
5409 frame_T *fr;
5410 int room;
5411 int row;
5412 int up; /* if TRUE, drag status line up, otherwise down */
5413 int n;
5414
5415 fr = dragwin->w_frame;
5416 curfr = fr;
5417 if (fr != topframe) /* more than one window */
5418 {
5419 fr = fr->fr_parent;
5420 /* When the parent frame is not a column of frames, its parent should
5421 * be. */
5422 if (fr->fr_layout != FR_COL)
5423 {
5424 curfr = fr;
5425 if (fr != topframe) /* only a row of windows, may drag statusline */
5426 fr = fr->fr_parent;
5427 }
5428 }
5429
5430 /* If this is the last frame in a column, may want to resize the parent
5431 * frame instead (go two up to skip a row of frames). */
5432 while (curfr != topframe && curfr->fr_next == NULL)
5433 {
5434 if (fr != topframe)
5435 fr = fr->fr_parent;
5436 curfr = fr;
5437 if (fr != topframe)
5438 fr = fr->fr_parent;
5439 }
5440
5441 if (offset < 0) /* drag up */
5442 {
5443 up = TRUE;
5444 offset = -offset;
5445 /* sum up the room of the current frame and above it */
5446 if (fr == curfr)
5447 {
5448 /* only one window */
5449 room = fr->fr_height - frame_minheight(fr, NULL);
5450 }
5451 else
5452 {
5453 room = 0;
5454 for (fr = fr->fr_child; ; fr = fr->fr_next)
5455 {
5456 room += fr->fr_height - frame_minheight(fr, NULL);
5457 if (fr == curfr)
5458 break;
5459 }
5460 }
5461 fr = curfr->fr_next; /* put fr at frame that grows */
5462 }
5463 else /* drag down */
5464 {
5465 up = FALSE;
5466 /*
5467 * Only dragging the last status line can reduce p_ch.
5468 */
5469 room = Rows - cmdline_row;
5470 if (curfr->fr_next == NULL)
5471 room -= 1;
5472 else
5473 room -= p_ch;
5474 if (room < 0)
5475 room = 0;
5476 /* sum up the room of frames below of the current one */
5477 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5478 room += fr->fr_height - frame_minheight(fr, NULL);
5479 fr = curfr; /* put fr at window that grows */
5480 }
5481
5482 if (room < offset) /* Not enough room */
5483 offset = room; /* Move as far as we can */
5484 if (offset <= 0)
5485 return;
5486
5487 /*
5488 * Grow frame fr by "offset" lines.
5489 * Doesn't happen when dragging the last status line up.
5490 */
5491 if (fr != NULL)
5492 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5493
5494 if (up)
5495 fr = curfr; /* current frame gets smaller */
5496 else
5497 fr = curfr->fr_next; /* next frame gets smaller */
5498
5499 /*
5500 * Now make the other frames smaller.
5501 */
5502 while (fr != NULL && offset > 0)
5503 {
5504 n = frame_minheight(fr, NULL);
5505 if (fr->fr_height - offset <= n)
5506 {
5507 offset -= fr->fr_height - n;
5508 frame_new_height(fr, n, !up, FALSE);
5509 }
5510 else
5511 {
5512 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5513 break;
5514 }
5515 if (up)
5516 fr = fr->fr_prev;
5517 else
5518 fr = fr->fr_next;
5519 }
5520 row = win_comp_pos();
5521 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5522 cmdline_row = row;
5523 p_ch = Rows - cmdline_row;
5524 if (p_ch < 1)
5525 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005526 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005527 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 showmode();
5529}
5530
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531/*
5532 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5533 */
5534 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005535win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 frame_T *curfr;
5538 frame_T *fr;
5539 int room;
5540 int left; /* if TRUE, drag separator line left, otherwise right */
5541 int n;
5542
5543 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005544 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 return;
5546 curfr = fr;
5547 fr = fr->fr_parent;
5548 /* When the parent frame is not a row of frames, its parent should be. */
5549 if (fr->fr_layout != FR_ROW)
5550 {
5551 if (fr == topframe) /* only a column of windows (cannot happen?) */
5552 return;
5553 curfr = fr;
5554 fr = fr->fr_parent;
5555 }
5556
5557 /* If this is the last frame in a row, may want to resize a parent
5558 * frame instead. */
5559 while (curfr->fr_next == NULL)
5560 {
5561 if (fr == topframe)
5562 break;
5563 curfr = fr;
5564 fr = fr->fr_parent;
5565 if (fr != topframe)
5566 {
5567 curfr = fr;
5568 fr = fr->fr_parent;
5569 }
5570 }
5571
5572 if (offset < 0) /* drag left */
5573 {
5574 left = TRUE;
5575 offset = -offset;
5576 /* sum up the room of the current frame and left of it */
5577 room = 0;
5578 for (fr = fr->fr_child; ; fr = fr->fr_next)
5579 {
5580 room += fr->fr_width - frame_minwidth(fr, NULL);
5581 if (fr == curfr)
5582 break;
5583 }
5584 fr = curfr->fr_next; /* put fr at frame that grows */
5585 }
5586 else /* drag right */
5587 {
5588 left = FALSE;
5589 /* sum up the room of frames right of the current one */
5590 room = 0;
5591 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5592 room += fr->fr_width - frame_minwidth(fr, NULL);
5593 fr = curfr; /* put fr at window that grows */
5594 }
5595
5596 if (room < offset) /* Not enough room */
5597 offset = room; /* Move as far as we can */
5598 if (offset <= 0) /* No room at all, quit. */
5599 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005600 if (fr == NULL)
5601 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
5603 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005604 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605
5606 /* shrink other frames: current and at the left or at the right */
5607 if (left)
5608 fr = curfr; /* current frame gets smaller */
5609 else
5610 fr = curfr->fr_next; /* next frame gets smaller */
5611
5612 while (fr != NULL && offset > 0)
5613 {
5614 n = frame_minwidth(fr, NULL);
5615 if (fr->fr_width - offset <= n)
5616 {
5617 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005618 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
5620 else
5621 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005622 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 break;
5624 }
5625 if (left)
5626 fr = fr->fr_prev;
5627 else
5628 fr = fr->fr_next;
5629 }
5630 (void)win_comp_pos();
5631 redraw_all_later(NOT_VALID);
5632}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633#endif /* FEAT_MOUSE */
5634
5635#endif /* FEAT_WINDOWS */
5636
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005637#define FRACTION_MULT 16384L
5638
5639/*
5640 * Set wp->w_fraction for the current w_wrow and w_height.
5641 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005642 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005643set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005644{
5645 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005646 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005647}
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649/*
5650 * Set the height of a window.
5651 * This takes care of the things inside the window, not what happens to the
5652 * window position, the frame or to other windows.
5653 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005654 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005655win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656{
5657 linenr_T lnum;
5658 int sline, line_size;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005659 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 /* Don't want a negative height. Happens when splitting a tiny window.
5662 * Will equalize heights soon to fix it. */
5663 if (height < 0)
5664 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005665 if (wp->w_height == height)
5666 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005668 if (wp->w_height > 0)
5669 {
5670 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005671 /* w_wrow needs to be valid. When setting 'laststatus' this may
5672 * call win_new_height() recursively. */
5673 validate_cursor();
5674 if (wp->w_height != prev_height)
5675 return; /* Recursive call already changed the size, bail out here
5676 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005677 if (wp->w_wrow != wp->w_prev_fraction_row)
5678 set_fraction(wp);
5679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
5681 wp->w_height = height;
5682 wp->w_skipcol = 0;
5683
5684 /* Don't change w_topline when height is zero. Don't set w_topline when
5685 * 'scrollbind' is set and this isn't the current window. */
5686 if (height > 0
5687#ifdef FEAT_SCROLLBIND
5688 && (!wp->w_p_scb || wp == curwin)
5689#endif
5690 )
5691 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005692 /*
5693 * Find a value for w_topline that shows the cursor at the same
5694 * relative position in the window as before (more or less).
5695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 lnum = wp->w_cursor.lnum;
5697 if (lnum < 1) /* can happen when starting up */
5698 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005699 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5700 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5702 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005703
5704 if (sline >= 0)
5705 {
5706 /* Make sure the whole cursor line is visible, if possible. */
5707 int rows = plines_win(wp, lnum, FALSE);
5708
5709 if (sline > wp->w_height - rows)
5710 {
5711 sline = wp->w_height - rows;
5712 wp->w_wrow -= rows - line_size;
5713 }
5714 }
5715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (sline < 0)
5717 {
5718 /*
5719 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005720 * Make cursor line the first line in the window. If not enough
5721 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 */
5723 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005724 if (wp->w_wrow >= wp->w_height
5725 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5726 {
5727 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5728 --wp->w_wrow;
5729 while (wp->w_wrow >= wp->w_height)
5730 {
5731 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5732 + win_col_off2(wp);
5733 --wp->w_wrow;
5734 }
5735 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005736 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005738 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005740 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 {
5742#ifdef FEAT_FOLDING
5743 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5744 if (lnum == 1)
5745 {
5746 /* first line in buffer is folded */
5747 line_size = 1;
5748 --sline;
5749 break;
5750 }
5751#endif
5752 --lnum;
5753#ifdef FEAT_DIFF
5754 if (lnum == wp->w_topline)
5755 line_size = plines_win_nofill(wp, lnum, TRUE)
5756 + wp->w_topfill;
5757 else
5758#endif
5759 line_size = plines_win(wp, lnum, TRUE);
5760 sline -= line_size;
5761 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005762
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 if (sline < 0)
5764 {
5765 /*
5766 * Line we want at top would go off top of screen. Use next
5767 * line instead.
5768 */
5769#ifdef FEAT_FOLDING
5770 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5771#endif
5772 lnum++;
5773 wp->w_wrow -= line_size + sline;
5774 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005775 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
5777 /* First line of file reached, use that as topline. */
5778 lnum = 1;
5779 wp->w_wrow -= sline;
5780 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005781
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005782 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 }
5785
5786 if (wp == curwin)
5787 {
5788 if (p_so)
5789 update_topline();
5790 curs_columns(FALSE); /* validate w_wrow */
5791 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005792 if (prev_height > 0)
5793 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
5795 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005796 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797#ifdef FEAT_WINDOWS
5798 wp->w_redr_status = TRUE;
5799#endif
5800 invalidate_botline_win(wp);
5801}
5802
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005803#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804/*
5805 * Set the width of a window.
5806 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005807 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005808win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
5810 wp->w_width = width;
5811 wp->w_lines_valid = 0;
5812 changed_line_abv_curs_win(wp);
5813 invalidate_botline_win(wp);
5814 if (wp == curwin)
5815 {
5816 update_topline();
5817 curs_columns(TRUE); /* validate w_wrow */
5818 }
5819 redraw_win_later(wp, NOT_VALID);
5820 wp->w_redr_status = TRUE;
5821}
5822#endif
5823
5824 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005825win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5828 if (wp->w_p_scr == 0)
5829 wp->w_p_scr = 1;
5830}
5831
5832/*
5833 * command_height: called whenever p_ch has been changed
5834 */
5835 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005836command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
5838#ifdef FEAT_WINDOWS
5839 int h;
5840 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005841 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005843 /* Use the value of p_ch that we remembered. This is needed for when the
5844 * GUI starts up, we can't be sure in what order things happen. And when
5845 * p_ch was changed in another tab page. */
5846 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005847
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 /* Find bottom frame with width of screen. */
5849 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5851 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852
5853 /* Avoid changing the height of a window with 'winfixheight' set. */
5854 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5855 && frp->fr_win->w_p_wfh)
5856 frp = frp->fr_prev;
5857
5858 if (starting != NO_SCREEN)
5859 {
5860 cmdline_row = Rows - p_ch;
5861
5862 if (p_ch > old_p_ch) /* p_ch got bigger */
5863 {
5864 while (p_ch > old_p_ch)
5865 {
5866 if (frp == NULL)
5867 {
5868 EMSG(_(e_noroom));
5869 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005870 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 cmdline_row = Rows - p_ch;
5872 break;
5873 }
5874 h = frp->fr_height - frame_minheight(frp, NULL);
5875 if (h > p_ch - old_p_ch)
5876 h = p_ch - old_p_ch;
5877 old_p_ch += h;
5878 frame_add_height(frp, -h);
5879 frp = frp->fr_prev;
5880 }
5881
5882 /* Recompute window positions. */
5883 (void)win_comp_pos();
5884
5885 /* clear the lines added to cmdline */
5886 if (full_screen)
5887 screen_fill((int)(cmdline_row), (int)Rows, 0,
5888 (int)Columns, ' ', ' ', 0);
5889 msg_row = cmdline_row;
5890 redraw_cmdline = TRUE;
5891 return;
5892 }
5893
5894 if (msg_row < cmdline_row)
5895 msg_row = cmdline_row;
5896 redraw_cmdline = TRUE;
5897 }
5898 frame_add_height(frp, (int)(old_p_ch - p_ch));
5899
5900 /* Recompute window positions. */
5901 if (frp != lastwin->w_frame)
5902 (void)win_comp_pos();
5903#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 cmdline_row = Rows - p_ch;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005905 win_setheight(cmdline_row);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906#endif
5907}
5908
5909#if defined(FEAT_WINDOWS) || defined(PROTO)
5910/*
5911 * Resize frame "frp" to be "n" lines higher (negative for less high).
5912 * Also resize the frames it is contained in.
5913 */
5914 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005915frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
5917 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5918 for (;;)
5919 {
5920 frp = frp->fr_parent;
5921 if (frp == NULL)
5922 break;
5923 frp->fr_height += n;
5924 }
5925}
5926
5927/*
5928 * Add or remove a status line for the bottom window(s), according to the
5929 * value of 'laststatus'.
5930 */
5931 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005932last_status(
5933 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 /* Don't make a difference between horizontal or vertical split. */
5936 last_status_rec(topframe, (p_ls == 2
5937 || (p_ls == 1 && (morewin || lastwin != firstwin))));
5938}
5939
5940 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005941last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 frame_T *fp;
5944 win_T *wp;
5945
5946 if (fr->fr_layout == FR_LEAF)
5947 {
5948 wp = fr->fr_win;
5949 if (wp->w_status_height != 0 && !statusline)
5950 {
5951 /* remove status line */
5952 win_new_height(wp, wp->w_height + 1);
5953 wp->w_status_height = 0;
5954 comp_col();
5955 }
5956 else if (wp->w_status_height == 0 && statusline)
5957 {
5958 /* Find a frame to take a line from. */
5959 fp = fr;
5960 while (fp->fr_height <= frame_minheight(fp, NULL))
5961 {
5962 if (fp == topframe)
5963 {
5964 EMSG(_(e_noroom));
5965 return;
5966 }
5967 /* In a column of frames: go to frame above. If already at
5968 * the top or in a row of frames: go to parent. */
5969 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5970 fp = fp->fr_prev;
5971 else
5972 fp = fp->fr_parent;
5973 }
5974 wp->w_status_height = 1;
5975 if (fp != fr)
5976 {
5977 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5978 frame_fix_height(wp);
5979 (void)win_comp_pos();
5980 }
5981 else
5982 win_new_height(wp, wp->w_height - 1);
5983 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005984 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 }
5986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 else if (fr->fr_layout == FR_ROW)
5988 {
5989 /* vertically split windows, set status line for each one */
5990 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
5991 last_status_rec(fp, statusline);
5992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 else
5994 {
5995 /* horizontally split window, set status line for last one */
5996 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
5997 ;
5998 last_status_rec(fp, statusline);
5999 }
6000}
6001
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006002/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006003 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006004 */
6005 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006006tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006007{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006008#ifdef FEAT_GUI_TABLINE
6009 /* When the GUI has the tabline then this always returns zero. */
6010 if (gui_use_tabline())
6011 return 0;
6012#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006013 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006014 {
6015 case 0: return 0;
6016 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6017 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006018 return 1;
6019}
6020
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021#endif /* FEAT_WINDOWS */
6022
6023#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6024/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006025 * Get the file name at the cursor.
6026 * If Visual mode is active, use the selected text if it's in one line.
6027 * Returns the name in allocated memory, NULL for failure.
6028 */
6029 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006030grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006031{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006032 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6033
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006034 if (VIsual_active)
6035 {
6036 int len;
6037 char_u *ptr;
6038
6039 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6040 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006041 return find_file_name_in_path(ptr, len, options,
6042 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006043 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006044 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006045
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006046}
6047
6048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 * Return the file name under or after the cursor.
6050 *
6051 * The 'path' option is searched if the file name is not absolute.
6052 * The string returned has been alloc'ed and should be freed by the caller.
6053 * NULL is returned if the file name or file is not found.
6054 *
6055 * options:
6056 * FNAME_MESS give error messages
6057 * FNAME_EXP expand to path
6058 * FNAME_HYP check for hypertext link
6059 * FNAME_INCL apply "includeexpr"
6060 */
6061 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006062file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063{
6064 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006065 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6066 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067}
6068
6069/*
6070 * Return the name of the file under or after ptr[col].
6071 * Otherwise like file_name_at_cursor().
6072 */
6073 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006074file_name_in_line(
6075 char_u *line,
6076 int col,
6077 int options,
6078 long count,
6079 char_u *rel_fname, /* file we are searching relative to */
6080 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 char_u *ptr;
6083 int len;
6084
6085 /*
6086 * search forward for what could be the start of a file name
6087 */
6088 ptr = line + col;
6089 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar0dd492f2005-06-22 22:25:07 +00006090 mb_ptr_adv(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 if (*ptr == NUL) /* nothing found */
6092 {
6093 if (options & FNAME_MESS)
6094 EMSG(_("E446: No file name under cursor"));
6095 return NULL;
6096 }
6097
6098 /*
6099 * Search backward for first char of the file name.
6100 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6101 */
6102 while (ptr > line)
6103 {
6104#ifdef FEAT_MBYTE
6105 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6106 ptr -= len + 1;
6107 else
6108#endif
6109 if (vim_isfilec(ptr[-1])
6110 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6111 --ptr;
6112 else
6113 break;
6114 }
6115
6116 /*
6117 * Search forward for the last char of the file name.
6118 * Also allow "://" when ':' is not in 'isfname'.
6119 */
6120 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006121 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006123 {
6124 if (ptr[len] == '\\')
6125 /* Skip over the "\" in "\ ". */
6126 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127#ifdef FEAT_MBYTE
6128 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006129 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 else
6131#endif
6132 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135 /*
6136 * If there is trailing punctuation, remove it.
6137 * But don't remove "..", could be a directory name.
6138 */
6139 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6140 && ptr[len - 2] != '.')
6141 --len;
6142
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006143 if (file_lnum != NULL)
6144 {
6145 char_u *p;
6146
6147 /* Get the number after the file name and a separator character */
6148 p = ptr + len;
6149 p = skipwhite(p);
6150 if (*p != NUL)
6151 {
6152 if (!isdigit(*p))
6153 ++p; /* skip the separator */
6154 p = skipwhite(p);
6155 if (isdigit(*p))
6156 *file_lnum = (int)getdigits(&p);
6157 }
6158 }
6159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6161}
6162
6163# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006164static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006167eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
6169 char_u *res;
6170
6171 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006172 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006173 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 set_vim_var_string(VV_FNAME, NULL, 0);
6175 return res;
6176}
6177#endif
6178
6179/*
6180 * Return the name of the file ptr[len] in 'path'.
6181 * Otherwise like file_name_at_cursor().
6182 */
6183 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006184find_file_name_in_path(
6185 char_u *ptr,
6186 int len,
6187 int options,
6188 long count,
6189 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 char_u *file_name;
6192 int c;
6193# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6194 char_u *tofree = NULL;
6195
6196 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6197 {
6198 tofree = eval_includeexpr(ptr, len);
6199 if (tofree != NULL)
6200 {
6201 ptr = tofree;
6202 len = (int)STRLEN(ptr);
6203 }
6204 }
6205# endif
6206
6207 if (options & FNAME_EXP)
6208 {
6209 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6210 TRUE, rel_fname);
6211
6212# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6213 /*
6214 * If the file could not be found in a normal way, try applying
6215 * 'includeexpr' (unless done already).
6216 */
6217 if (file_name == NULL
6218 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6219 {
6220 tofree = eval_includeexpr(ptr, len);
6221 if (tofree != NULL)
6222 {
6223 ptr = tofree;
6224 len = (int)STRLEN(ptr);
6225 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6226 TRUE, rel_fname);
6227 }
6228 }
6229# endif
6230 if (file_name == NULL && (options & FNAME_MESS))
6231 {
6232 c = ptr[len];
6233 ptr[len] = NUL;
6234 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6235 ptr[len] = c;
6236 }
6237
6238 /* Repeat finding the file "count" times. This matters when it
6239 * appears several times in the path. */
6240 while (file_name != NULL && --count > 0)
6241 {
6242 vim_free(file_name);
6243 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6244 }
6245 }
6246 else
6247 file_name = vim_strnsave(ptr, len);
6248
6249# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6250 vim_free(tofree);
6251# endif
6252
6253 return file_name;
6254}
6255#endif /* FEAT_SEARCHPATH */
6256
6257/*
6258 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6259 * Also check for ":\\", which MS Internet Explorer accepts, return
6260 * URL_BACKSLASH.
6261 */
6262 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006263path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 if (STRNCMP(p, "://", (size_t)3) == 0)
6266 return URL_SLASH;
6267 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6268 return URL_BACKSLASH;
6269 return 0;
6270}
6271
6272/*
6273 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6274 * Return URL_BACKSLASH for "name:\\".
6275 * Return zero otherwise.
6276 */
6277 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006278path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 char_u *p;
6281
6282 for (p = fname; isalpha(*p); ++p)
6283 ;
6284 return path_is_url(p);
6285}
6286
6287/*
6288 * Return TRUE if "name" is a full (absolute) path name or URL.
6289 */
6290 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006291vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 return (path_with_url(name) != 0 || mch_isFullName(name));
6294}
6295
6296/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006297 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 *
6299 * return FAIL for failure, OK otherwise
6300 */
6301 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006302vim_FullName(
6303 char_u *fname,
6304 char_u *buf,
6305 int len,
6306 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 int retval = OK;
6309 int url;
6310
6311 *buf = NUL;
6312 if (fname == NULL)
6313 return FAIL;
6314
6315 url = path_with_url(fname);
6316 if (!url)
6317 retval = mch_FullName(fname, buf, len, force);
6318 if (url || retval == FAIL)
6319 {
6320 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006321 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01006323#if defined(MACOS_CLASSIC) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 slash_adjust(buf);
6325#endif
6326 return retval;
6327}
6328
6329/*
6330 * Return the minimal number of rows that is needed on the screen to display
6331 * the current number of windows.
6332 */
6333 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006334min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335{
6336 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006337#ifdef FEAT_WINDOWS
6338 tabpage_T *tp;
6339 int n;
6340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341
6342 if (firstwin == NULL) /* not initialized yet */
6343 return MIN_LINES;
6344
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006346 total = 0;
6347 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
6348 {
6349 n = frame_minheight(tp->tp_topframe, NULL);
6350 if (total < n)
6351 total = n;
6352 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006353 total += tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354#else
Bram Moolenaarf740b292006-02-16 22:11:02 +00006355 total = 1; /* at least one window should have a line! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006357 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 return total;
6359}
6360
6361/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006362 * Return TRUE if there is only one window (in the current tab page), not
6363 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006364 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 */
6366 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006367only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369#ifdef FEAT_WINDOWS
6370 int count = 0;
6371 win_T *wp;
6372
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373 /* If there is another tab page there always is another window. */
6374 if (first_tabpage->tp_next != NULL)
6375 return FALSE;
6376
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 for (wp = firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006378 if (wp->w_buffer != NULL
6379 && (!((wp->w_buffer->b_help && !curbuf->b_help)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380# ifdef FEAT_QUICKFIX
6381 || wp->w_p_pvw
6382# endif
6383 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006384# ifdef FEAT_AUTOCMD
6385 && wp != aucmd_win
6386# endif
6387 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 ++count;
6389 return (count <= 1);
6390#else
6391 return TRUE;
6392#endif
6393}
6394
6395#if defined(FEAT_WINDOWS) || defined(FEAT_AUTOCMD) || defined(PROTO)
6396/*
6397 * Correct the cursor line number in other windows. Used after changing the
6398 * current buffer, and before applying autocommands.
6399 * When "do_curwin" is TRUE, also check current window.
6400 */
6401 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006402check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
6404 win_T *wp;
6405
6406#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00006407 tabpage_T *tp;
6408
6409 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
6411#else
6412 wp = curwin;
6413 if (do_curwin)
6414#endif
6415 {
6416 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6417 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6418 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6419 wp->w_topline = curbuf->b_ml.ml_line_count;
6420 }
6421}
6422#endif
6423
6424#if defined(FEAT_WINDOWS) || defined(PROTO)
6425
6426/*
6427 * A snapshot of the window sizes, to restore them after closing the help
6428 * window.
6429 * Only these fields are used:
6430 * fr_layout
6431 * fr_width
6432 * fr_height
6433 * fr_next
6434 * fr_child
6435 * fr_win (only valid for the old curwin, NULL otherwise)
6436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
6438/*
6439 * Create a snapshot of the current frame sizes.
6440 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006441 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006442make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006444 clear_snapshot(curtab, idx);
6445 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446}
6447
6448 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006449make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6452 if (*frp == NULL)
6453 return;
6454 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 (*frp)->fr_height = fr->fr_height;
6457 if (fr->fr_next != NULL)
6458 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6459 if (fr->fr_child != NULL)
6460 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6461 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6462 (*frp)->fr_win = curwin;
6463}
6464
6465/*
6466 * Remove any existing snapshot.
6467 */
6468 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006469clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006471 clear_snapshot_rec(tp->tp_snapshot[idx]);
6472 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473}
6474
6475 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006476clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 if (fr != NULL)
6479 {
6480 clear_snapshot_rec(fr->fr_next);
6481 clear_snapshot_rec(fr->fr_child);
6482 vim_free(fr);
6483 }
6484}
6485
6486/*
6487 * Restore a previously created snapshot, if there is any.
6488 * This is only done if the screen size didn't change and the window layout is
6489 * still the same.
6490 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006491 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006492restore_snapshot(
6493 int idx,
6494 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
6496 win_T *wp;
6497
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006498 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006499 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006500 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6501 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006503 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 win_comp_pos();
6505 if (wp != NULL && close_curwin)
6506 win_goto(wp);
6507 redraw_all_later(CLEAR);
6508 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006509 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510}
6511
6512/*
6513 * Check if frames "sn" and "fr" have the same layout, same following frames
6514 * and same children.
6515 */
6516 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006517check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
6519 if (sn->fr_layout != fr->fr_layout
6520 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6521 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6522 || (sn->fr_next != NULL
6523 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6524 || (sn->fr_child != NULL
6525 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL))
6526 return FAIL;
6527 return OK;
6528}
6529
6530/*
6531 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6532 * following frames and children.
6533 * Returns a pointer to the old current window, or NULL.
6534 */
6535 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006536restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 win_T *wp = NULL;
6539 win_T *wp2;
6540
6541 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (fr->fr_layout == FR_LEAF)
6544 {
6545 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006546 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 wp = sn->fr_win;
6548 }
6549 if (sn->fr_next != NULL)
6550 {
6551 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6552 if (wp2 != NULL)
6553 wp = wp2;
6554 }
6555 if (sn->fr_child != NULL)
6556 {
6557 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6558 if (wp2 != NULL)
6559 wp = wp2;
6560 }
6561 return wp;
6562}
6563
6564#endif
6565
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006566#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6567 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006568/*
6569 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006570 * restore_win() MUST be called to undo, also when FAIL is returned.
6571 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006572 * When "no_display" is TRUE the display won't be affected, no redraw is
6573 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006574 * Returns FAIL if switching to "win" failed.
6575 */
6576 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006577switch_win(
6578 win_T **save_curwin UNUSED,
6579 tabpage_T **save_curtab UNUSED,
6580 win_T *win UNUSED,
6581 tabpage_T *tp UNUSED,
6582 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006583{
6584# ifdef FEAT_AUTOCMD
6585 block_autocmds();
6586# endif
6587# ifdef FEAT_WINDOWS
6588 *save_curwin = curwin;
6589 if (tp != NULL)
6590 {
6591 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006592 if (no_display)
6593 {
6594 curtab->tp_firstwin = firstwin;
6595 curtab->tp_lastwin = lastwin;
6596 curtab = tp;
6597 firstwin = curtab->tp_firstwin;
6598 lastwin = curtab->tp_lastwin;
6599 }
6600 else
6601 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006602 }
6603 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006604 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006605 curwin = win;
6606 curbuf = curwin->w_buffer;
6607# endif
6608 return OK;
6609}
6610
6611/*
6612 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006613 * When "no_display" is TRUE the display won't be affected, no redraw is
6614 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006615 */
6616 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006617restore_win(
6618 win_T *save_curwin UNUSED,
6619 tabpage_T *save_curtab UNUSED,
6620 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006621{
6622# ifdef FEAT_WINDOWS
6623 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006624 {
6625 if (no_display)
6626 {
6627 curtab->tp_firstwin = firstwin;
6628 curtab->tp_lastwin = lastwin;
6629 curtab = save_curtab;
6630 firstwin = curtab->tp_firstwin;
6631 lastwin = curtab->tp_lastwin;
6632 }
6633 else
6634 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6635 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006636 if (win_valid(save_curwin))
6637 {
6638 curwin = save_curwin;
6639 curbuf = curwin->w_buffer;
6640 }
6641# endif
6642# ifdef FEAT_AUTOCMD
6643 unblock_autocmds();
6644# endif
6645}
6646
6647/*
6648 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6649 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6650 */
6651 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006652switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006653{
6654# ifdef FEAT_AUTOCMD
6655 block_autocmds();
6656# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006657 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006658 --curbuf->b_nwindows;
6659 curbuf = buf;
6660 curwin->w_buffer = buf;
6661 ++curbuf->b_nwindows;
6662}
6663
6664/*
6665 * Restore the current buffer after using switch_buffer().
6666 */
6667 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006668restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006669{
6670# ifdef FEAT_AUTOCMD
6671 unblock_autocmds();
6672# endif
6673 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006674 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006675 {
6676 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006677 curwin->w_buffer = save_curbuf->br_buf;
6678 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006679 ++curbuf->b_nwindows;
6680 }
6681}
6682#endif
6683
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006684#if (defined(FEAT_GUI) && defined(FEAT_WINDOWS)) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685/*
6686 * Return TRUE if there is any vertically split window.
6687 */
6688 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006689win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 frame_T *fr;
6692
6693 if (topframe->fr_layout == FR_ROW)
6694 return TRUE;
6695
6696 if (topframe->fr_layout == FR_COL)
6697 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6698 if (fr->fr_layout == FR_ROW)
6699 return TRUE;
6700
6701 return FALSE;
6702}
6703#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006704
6705#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6706/*
6707 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006708 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006709 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6710 * If no particular ID is desired, -1 must be specified for 'id'.
6711 * Return ID of added match, -1 on failure.
6712 */
6713 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006714match_add(
6715 win_T *wp,
6716 char_u *grp,
6717 char_u *pat,
6718 int prio,
6719 int id,
6720 list_T *pos_list,
6721 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006722{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006723 matchitem_T *cur;
6724 matchitem_T *prev;
6725 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006726 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006727 regprog_T *regprog = NULL;
6728 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006729
Bram Moolenaarb3414592014-06-17 17:48:32 +02006730 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006731 return -1;
6732 if (id < -1 || id == 0)
6733 {
6734 EMSGN("E799: Invalid ID: %ld (must be greater than or equal to 1)", id);
6735 return -1;
6736 }
6737 if (id != -1)
6738 {
6739 cur = wp->w_match_head;
6740 while (cur != NULL)
6741 {
6742 if (cur->id == id)
6743 {
6744 EMSGN("E801: ID already taken: %ld", id);
6745 return -1;
6746 }
6747 cur = cur->next;
6748 }
6749 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006750 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006751 {
6752 EMSG2(_(e_nogroup), grp);
6753 return -1;
6754 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006755 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006756 {
6757 EMSG2(_(e_invarg2), pat);
6758 return -1;
6759 }
6760
6761 /* Find available match ID. */
6762 while (id == -1)
6763 {
6764 cur = wp->w_match_head;
6765 while (cur != NULL && cur->id != wp->w_next_match_id)
6766 cur = cur->next;
6767 if (cur == NULL)
6768 id = wp->w_next_match_id;
6769 wp->w_next_match_id++;
6770 }
6771
6772 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006773 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006774 m->id = id;
6775 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006776 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006777 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006778 m->match.regprog = regprog;
6779 m->match.rmm_ic = FALSE;
6780 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006781# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006782 m->conceal_char = 0;
6783 if (conceal_char != NULL)
6784 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006785# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006786
Bram Moolenaarb3414592014-06-17 17:48:32 +02006787 /* Set up position matches */
6788 if (pos_list != NULL)
6789 {
6790 linenr_T toplnum = 0;
6791 linenr_T botlnum = 0;
6792 listitem_T *li;
6793 int i;
6794
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006795 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006796 i++, li = li->li_next)
6797 {
6798 linenr_T lnum = 0;
6799 colnr_T col = 0;
6800 int len = 1;
6801 list_T *subl;
6802 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006803 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006804
Bram Moolenaarb3414592014-06-17 17:48:32 +02006805 if (li->li_tv.v_type == VAR_LIST)
6806 {
6807 subl = li->li_tv.vval.v_list;
6808 if (subl == NULL)
6809 goto fail;
6810 subli = subl->lv_first;
6811 if (subli == NULL)
6812 goto fail;
6813 lnum = get_tv_number_chk(&subli->li_tv, &error);
6814 if (error == TRUE)
6815 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006816 if (lnum == 0)
6817 {
6818 --i;
6819 continue;
6820 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006821 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006822 subli = subli->li_next;
6823 if (subli != NULL)
6824 {
6825 col = get_tv_number_chk(&subli->li_tv, &error);
6826 if (error == TRUE)
6827 goto fail;
6828 subli = subli->li_next;
6829 if (subli != NULL)
6830 {
6831 len = get_tv_number_chk(&subli->li_tv, &error);
6832 if (error == TRUE)
6833 goto fail;
6834 }
6835 }
6836 m->pos.pos[i].col = col;
6837 m->pos.pos[i].len = len;
6838 }
6839 else if (li->li_tv.v_type == VAR_NUMBER)
6840 {
6841 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006842 {
6843 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006844 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006845 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006846 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6847 m->pos.pos[i].col = 0;
6848 m->pos.pos[i].len = 0;
6849 }
6850 else
6851 {
6852 EMSG(_("List or number required"));
6853 goto fail;
6854 }
6855 if (toplnum == 0 || lnum < toplnum)
6856 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006857 if (botlnum == 0 || lnum >= botlnum)
6858 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006859 }
6860
6861 /* Calculate top and bottom lines for redrawing area */
6862 if (toplnum != 0)
6863 {
6864 if (wp->w_buffer->b_mod_set)
6865 {
6866 if (wp->w_buffer->b_mod_top > toplnum)
6867 wp->w_buffer->b_mod_top = toplnum;
6868 if (wp->w_buffer->b_mod_bot < botlnum)
6869 wp->w_buffer->b_mod_bot = botlnum;
6870 }
6871 else
6872 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006873 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006874 wp->w_buffer->b_mod_top = toplnum;
6875 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006876 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006877 }
6878 m->pos.toplnum = toplnum;
6879 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006880 rtype = VALID;
6881 }
6882 }
6883
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006884 /* Insert new match. The match list is in ascending order with regard to
6885 * the match priorities. */
6886 cur = wp->w_match_head;
6887 prev = cur;
6888 while (cur != NULL && prio >= cur->priority)
6889 {
6890 prev = cur;
6891 cur = cur->next;
6892 }
6893 if (cur == prev)
6894 wp->w_match_head = m;
6895 else
6896 prev->next = m;
6897 m->next = cur;
6898
Bram Moolenaarb3414592014-06-17 17:48:32 +02006899 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006900 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006901
6902fail:
6903 vim_free(m);
6904 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006905}
6906
6907/*
6908 * Delete match with ID 'id' in the match list of window 'wp'.
6909 * Print error messages if 'perr' is TRUE.
6910 */
6911 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006912match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006913{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006914 matchitem_T *cur = wp->w_match_head;
6915 matchitem_T *prev = cur;
6916 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006917
6918 if (id < 1)
6919 {
6920 if (perr == TRUE)
6921 EMSGN("E802: Invalid ID: %ld (must be greater than or equal to 1)",
6922 id);
6923 return -1;
6924 }
6925 while (cur != NULL && cur->id != id)
6926 {
6927 prev = cur;
6928 cur = cur->next;
6929 }
6930 if (cur == NULL)
6931 {
6932 if (perr == TRUE)
6933 EMSGN("E803: ID not found: %ld", id);
6934 return -1;
6935 }
6936 if (cur == prev)
6937 wp->w_match_head = cur->next;
6938 else
6939 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006940 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006941 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006942 if (cur->pos.toplnum != 0)
6943 {
6944 if (wp->w_buffer->b_mod_set)
6945 {
6946 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6947 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6948 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6949 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6950 }
6951 else
6952 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006953 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006954 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6955 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006956 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006957 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006958 rtype = VALID;
6959 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006960 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006961 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006962 return 0;
6963}
6964
6965/*
6966 * Delete all matches in the match list of window 'wp'.
6967 */
6968 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006969clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006970{
6971 matchitem_T *m;
6972
6973 while (wp->w_match_head != NULL)
6974 {
6975 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006976 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006977 vim_free(wp->w_match_head->pattern);
6978 vim_free(wp->w_match_head);
6979 wp->w_match_head = m;
6980 }
6981 redraw_later(SOME_VALID);
6982}
6983
6984/*
6985 * Get match from ID 'id' in window 'wp'.
6986 * Return NULL if match not found.
6987 */
6988 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006989get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006990{
6991 matchitem_T *cur = wp->w_match_head;
6992
6993 while (cur != NULL && cur->id != id)
6994 cur = cur->next;
6995 return cur;
6996}
6997#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006998
6999#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
7000 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007001get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02007002{
7003 int i = 1;
7004 win_T *w;
7005
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007006 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007007 ++i;
7008
7009 if (w == NULL)
7010 return 0;
7011 else
7012 return i;
7013}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007014
7015 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007016get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007017{
7018 int i = 1;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007019# ifdef FEAT_WINDOWS
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007020 tabpage_T *t;
7021
7022 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7023 ++i;
7024
7025 if (t == NULL)
7026 return 0;
7027 else
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007028# endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007029 return i;
7030}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007031#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007032
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01007033#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007034/*
7035 * Return TRUE if "topfrp" and its children are at the right height.
7036 */
7037 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007038frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007039{
7040 frame_T *frp;
7041
7042 if (topfrp->fr_height != height)
7043 return FALSE;
7044
7045 if (topfrp->fr_layout == FR_ROW)
7046 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7047 if (frp->fr_height != height)
7048 return FALSE;
7049
7050 return TRUE;
7051}
Bram Moolenaarf0327f62013-06-28 20:16:55 +02007052#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007053
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007054#if defined(FEAT_WINDOWS) || defined(PROTO)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007055/*
7056 * Return TRUE if "topfrp" and its children are at the right width.
7057 */
7058 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007059frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007060{
7061 frame_T *frp;
7062
7063 if (topfrp->fr_width != width)
7064 return FALSE;
7065
7066 if (topfrp->fr_layout == FR_COL)
7067 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7068 if (frp->fr_width != width)
7069 return FALSE;
7070
7071 return TRUE;
7072}
7073#endif
7074
Bram Moolenaar86edef62016-03-13 18:07:30 +01007075#if defined(FEAT_EVAL) || defined(PROTO)
7076 int
7077win_getid(typval_T *argvars)
7078{
7079 int winnr;
7080 win_T *wp;
7081
7082 if (argvars[0].v_type == VAR_UNKNOWN)
7083 return curwin->w_id;
7084 winnr = get_tv_number(&argvars[0]);
7085 if (winnr > 0)
7086 {
7087 if (argvars[1].v_type == VAR_UNKNOWN)
7088 wp = firstwin;
7089 else
7090 {
7091 tabpage_T *tp;
7092 int tabnr = get_tv_number(&argvars[1]);
7093
7094 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7095 if (--tabnr == 0)
7096 break;
7097 if (tp == NULL)
7098 return -1;
7099 wp = tp->tp_firstwin;
7100 }
7101 for ( ; wp != NULL; wp = wp->w_next)
7102 if (--winnr == 0)
7103 return wp->w_id;
7104 }
7105 return 0;
7106}
7107
7108 int
7109win_gotoid(typval_T *argvars)
7110{
7111 win_T *wp;
7112 tabpage_T *tp;
7113 int id = get_tv_number(&argvars[0]);
7114
7115 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7116 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7117 wp != NULL; wp = wp->w_next)
7118 if (wp->w_id == id)
7119 {
7120 goto_tabpage_win(tp, wp);
7121 return 1;
7122 }
7123 return 0;
7124}
7125
7126 void
7127win_id2tabwin(typval_T *argvars, list_T *list)
7128{
7129 win_T *wp;
7130 tabpage_T *tp;
7131 int winnr = 1;
7132 int tabnr = 1;
7133 int id = get_tv_number(&argvars[0]);
7134
7135 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7136 {
7137 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7138 wp != NULL; wp = wp->w_next)
7139 {
7140 if (wp->w_id == id)
7141 {
7142 list_append_number(list, tabnr);
7143 list_append_number(list, winnr);
7144 return;
7145 }
7146 ++winnr;
7147 }
7148 ++tabnr;
7149 winnr = 1;
7150 }
7151 list_append_number(list, 0);
7152 list_append_number(list, 0);
7153}
7154
7155 int
7156win_id2win(typval_T *argvars)
7157{
7158 win_T *wp;
7159 int nr = 1;
7160 int id = get_tv_number(&argvars[0]);
7161
7162 for (wp = firstwin; wp != NULL; wp = wp->w_next)
7163 {
7164 if (wp->w_id == id)
7165 return nr;
7166 ++nr;
7167 }
7168 return 0;
7169}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007170
7171 void
7172win_findbuf(typval_T *argvars, list_T *list)
7173{
7174 win_T *wp;
7175 tabpage_T *tp;
7176 int bufnr = get_tv_number(&argvars[0]);
7177
7178 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
7179 for (wp = tp == curtab ? firstwin : tp->tp_firstwin;
7180 wp != NULL; wp = wp->w_next)
7181 if (wp->w_buffer->b_fnum == bufnr)
7182 list_append_number(list, wp->w_id);
7183}
7184
Bram Moolenaar86edef62016-03-13 18:07:30 +01007185#endif