blob: 9b470f6c3bb5344676fd5b0c8b78fe9609e835f6 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaarbaaa7e92016-01-29 22:47:03 +010013static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
14static void win_init(win_T *newp, win_T *oldp, int flags);
15static void win_init_some(win_T *newp, win_T *oldp);
16static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
17static void frame_setheight(frame_T *curfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010019static void win_exchange(long);
20static void win_rotate(int, int);
21static void win_totop(int size, int flags);
22static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
23static int last_window(void);
24static int close_last_window_tabpage(win_T *win, int free_buf, tabpage_T *prev_curtab);
25static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
26static frame_T *win_altframe(win_T *win, tabpage_T *tp);
27static tabpage_T *alt_tabpage(void);
28static win_T *frame2win(frame_T *frp);
29static int frame_has_win(frame_T *frp, win_T *wp);
30static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
31static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010032static int frame_fixed_width(frame_T *frp);
33static void frame_add_statusline(frame_T *frp);
34static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
35static void frame_add_vsep(frame_T *frp);
36static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
37static void frame_fix_width(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010038static int win_alloc_firstwin(win_T *oldwin);
39static void new_frame(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010040static tabpage_T *alloc_tabpage(void);
41static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
42static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
43static void frame_fix_height(win_T *wp);
44static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
Bram Moolenaarc917da42016-07-19 22:31:36 +020045static 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 +010046static void win_free(win_T *wp, tabpage_T *tp);
47static void frame_append(frame_T *after, frame_T *frp);
48static void frame_insert(frame_T *before, frame_T *frp);
49static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void win_goto_ver(int up, long count);
51static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void frame_add_height(frame_T *frp, int n);
53static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void make_snapshot_rec(frame_T *fr, frame_T **frp);
56static void clear_snapshot(tabpage_T *tp, int idx);
57static void clear_snapshot_rec(frame_T *fr);
58static int check_snapshot_rec(frame_T *sn, frame_T *fr);
59static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020063
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66#define URL_SLASH 1 /* path_is_url() has found "://" */
67#define URL_BACKSLASH 2 /* path_is_url() has found ":\\" */
68
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000069#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaar4033c552017-09-16 20:54:51 +020071#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000072
73static char *m_onlyone = N_("Already only one window");
74
Bram Moolenaar071d4272004-06-13 20:20:40 +000075/*
76 * all CTRL-W window commands are handled here, called from normal_cmd().
77 */
78 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010079do_window(
80 int nchar,
81 long Prenum,
82 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 long Prenum1;
85 win_T *wp;
86#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
87 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000088 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000089#endif
90#ifdef FEAT_FIND_ID
91 int type = FIND_DEFINE;
92 int len;
93#endif
94 char_u cbuf[40];
95
96 if (Prenum == 0)
97 Prenum1 = 1;
98 else
99 Prenum1 = Prenum;
100
101#ifdef FEAT_CMDWIN
102# define CHECK_CMDWIN if (cmdwin_type != 0) { EMSG(_(e_cmdwin)); break; }
103#else
104# define CHECK_CMDWIN
105#endif
106
107 switch (nchar)
108 {
109/* split current window in two parts, horizontally */
110 case 'S':
111 case Ctrl_S:
112 case 's':
113 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000115#ifdef FEAT_QUICKFIX
116 /* When splitting the quickfix window open a new buffer in it,
117 * don't replicate the quickfix buffer. */
118 if (bt_quickfix(curbuf))
119 goto newwindow;
120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_GUI
122 need_mouse_correct = TRUE;
123#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200124 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 break;
126
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127/* split current window in two parts, vertically */
128 case Ctrl_V:
129 case 'v':
130 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100132#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000133 /* When splitting the quickfix window open a new buffer in it,
134 * don't replicate the quickfix buffer. */
135 if (bt_quickfix(curbuf))
136 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100137#endif
138#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100140#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200141 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143
144/* split current window and edit alternate file */
145 case Ctrl_HAT:
146 case '^':
147 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100149 cmd_with_count("split #", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 do_cmdline_cmd(cbuf);
151 break;
152
153/* open new window */
154 case Ctrl_N:
155 case 'n':
156 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000158#ifdef FEAT_QUICKFIX
159newwindow:
160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000162 /* window height */
163 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 else
165 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100166#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000167 if (nchar == 'v' || nchar == Ctrl_V)
168 STRCAT(cbuf, "v");
169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 STRCAT(cbuf, "new");
171 do_cmdline_cmd(cbuf);
172 break;
173
174/* quit current window */
175 case Ctrl_Q:
176 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100178 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100179 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 break;
181
182/* close current window */
183 case Ctrl_C:
184 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100186 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100187 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 break;
189
Bram Moolenaar4033c552017-09-16 20:54:51 +0200190#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191/* close preview window */
192 case Ctrl_Z:
193 case 'z':
194 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 do_cmdline_cmd((char_u *)"pclose");
197 break;
198
199/* cursor to preview window */
200 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200201 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 if (wp->w_p_pvw)
203 break;
204 if (wp == NULL)
205 EMSG(_("E441: There is no preview window"));
206 else
207 win_goto(wp);
208 break;
209#endif
210
211/* close all but current window */
212 case Ctrl_O:
213 case 'o':
214 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100216 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100217 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218 break;
219
220/* cursor to next window with wrap around */
221 case Ctrl_W:
222 case 'w':
223/* cursor to previous window with wrap around */
224 case 'W':
225 CHECK_CMDWIN
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100226 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 beep_flush();
228 else
229 {
230 if (Prenum) /* go to specified window */
231 {
232 for (wp = firstwin; --Prenum > 0; )
233 {
234 if (wp->w_next == NULL)
235 break;
236 else
237 wp = wp->w_next;
238 }
239 }
240 else
241 {
242 if (nchar == 'W') /* go to previous window */
243 {
244 wp = curwin->w_prev;
245 if (wp == NULL)
246 wp = lastwin; /* wrap around */
247 }
248 else /* go to next window */
249 {
250 wp = curwin->w_next;
251 if (wp == NULL)
252 wp = firstwin; /* wrap around */
253 }
254 }
255 win_goto(wp);
256 }
257 break;
258
259/* cursor to window below */
260 case 'j':
261 case K_DOWN:
262 case Ctrl_J:
263 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 break;
266
267/* cursor to window above */
268 case 'k':
269 case K_UP:
270 case Ctrl_K:
271 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 break;
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275/* cursor to left window */
276 case 'h':
277 case K_LEFT:
278 case Ctrl_H:
279 case K_BS:
280 CHECK_CMDWIN
281 win_goto_hor(TRUE, Prenum1);
282 break;
283
284/* cursor to right window */
285 case 'l':
286 case K_RIGHT:
287 case Ctrl_L:
288 CHECK_CMDWIN
289 win_goto_hor(FALSE, Prenum1);
290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000292/* move window to new tab page */
293 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000294 if (one_window())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000295 MSG(_(m_onlyone));
296 else
297 {
298 tabpage_T *oldtab = curtab;
299 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000300
301 /* First create a new tab with the window, then go back to
302 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000303 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000304 if (win_new_tabpage((int)Prenum) == OK
305 && valid_tabpage(oldtab))
306 {
307 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200308 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000309 if (curwin == wp)
310 win_close(curwin, FALSE);
311 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200312 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000313 }
314 }
315 break;
316
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317/* cursor to top-left window */
318 case 't':
319 case Ctrl_T:
320 win_goto(firstwin);
321 break;
322
323/* cursor to bottom-right window */
324 case 'b':
325 case Ctrl_B:
326 win_goto(lastwin);
327 break;
328
329/* cursor to last accessed (previous) window */
330 case 'p':
331 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200332 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 beep_flush();
334 else
335 win_goto(prevwin);
336 break;
337
338/* exchange current and next window */
339 case 'x':
340 case Ctrl_X:
341 CHECK_CMDWIN
342 win_exchange(Prenum);
343 break;
344
345/* rotate windows downwards */
346 case Ctrl_R:
347 case 'r':
348 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 win_rotate(FALSE, (int)Prenum1); /* downwards */
351 break;
352
353/* rotate windows upwards */
354 case 'R':
355 CHECK_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 win_rotate(TRUE, (int)Prenum1); /* upwards */
358 break;
359
360/* move window to the very top/bottom/left/right */
361 case 'K':
362 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363 case 'H':
364 case 'L':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 CHECK_CMDWIN
366 win_totop((int)Prenum,
367 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
368 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
369 break;
370
371/* make all windows the same height */
372 case '=':
373#ifdef FEAT_GUI
374 need_mouse_correct = TRUE;
375#endif
376 win_equal(NULL, FALSE, 'b');
377 break;
378
379/* increase current window height */
380 case '+':
381#ifdef FEAT_GUI
382 need_mouse_correct = TRUE;
383#endif
384 win_setheight(curwin->w_height + (int)Prenum1);
385 break;
386
387/* decrease current window height */
388 case '-':
389#ifdef FEAT_GUI
390 need_mouse_correct = TRUE;
391#endif
392 win_setheight(curwin->w_height - (int)Prenum1);
393 break;
394
395/* set current window height */
396 case Ctrl__:
397 case '_':
398#ifdef FEAT_GUI
399 need_mouse_correct = TRUE;
400#endif
401 win_setheight(Prenum ? (int)Prenum : 9999);
402 break;
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/* increase current window width */
405 case '>':
406#ifdef FEAT_GUI
407 need_mouse_correct = TRUE;
408#endif
409 win_setwidth(curwin->w_width + (int)Prenum1);
410 break;
411
412/* decrease current window width */
413 case '<':
414#ifdef FEAT_GUI
415 need_mouse_correct = TRUE;
416#endif
417 win_setwidth(curwin->w_width - (int)Prenum1);
418 break;
419
420/* set current window width */
421 case '|':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
426 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427
428/* jump to tag and split window if tag exists (in preview window) */
429#if defined(FEAT_QUICKFIX)
430 case '}':
431 CHECK_CMDWIN
432 if (Prenum)
433 g_do_tagpreview = Prenum;
434 else
435 g_do_tagpreview = p_pvh;
436 /*FALLTHROUGH*/
437#endif
438 case ']':
439 case Ctrl_RSB:
440 CHECK_CMDWIN
Bram Moolenaard355c502014-09-23 13:48:43 +0200441 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (Prenum)
443 postponed_split = Prenum;
444 else
445 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200446#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200447 if (nchar != '}')
448 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
Bram Moolenaard355c502014-09-23 13:48:43 +0200451 /* Execute the command right here, required when "wincmd ]"
452 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 do_nv_ident(Ctrl_RSB, NUL);
454 break;
455
456#ifdef FEAT_SEARCHPATH
457/* edit file name under cursor in a new window */
458 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000459 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000461wingotofile:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 CHECK_CMDWIN
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000463
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000464 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 if (ptr != NULL)
466 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200467 tabpage_T *oldtab = curtab;
468 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000469# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 setpcmark();
473 if (win_split(0, 0) == OK)
474 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200475 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200476 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
477 ECMD_HIDE, NULL) == FAIL)
478 {
479 /* Failed to open the file, close the window
480 * opened for it. */
481 win_close(curwin, FALSE);
482 goto_tabpage_win(oldtab, oldwin);
483 }
484 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000485 {
486 curwin->w_cursor.lnum = lnum;
487 check_cursor_lnum();
488 beginline(BL_SOL | BL_FIX);
489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 }
491 vim_free(ptr);
492 }
493 break;
494#endif
495
496#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000497/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 * new window -- webb
499 */
500 case 'i': /* Go to any match */
501 case Ctrl_I:
502 type = FIND_ANY;
503 /* FALLTHROUGH */
504 case 'd': /* Go to definition, using 'define' */
505 case Ctrl_D:
506 CHECK_CMDWIN
507 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
508 break;
509 find_pattern_in_path(ptr, 0, len, TRUE,
510 Prenum == 0 ? TRUE : FALSE, type,
511 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
512 curwin->w_set_curswant = TRUE;
513 break;
514#endif
515
Bram Moolenaar05159a02005-02-26 23:04:13 +0000516 case K_KENTER:
517 case CAR:
518#if defined(FEAT_QUICKFIX)
519 /*
520 * In a quickfix window a <CR> jumps to the error under the
521 * cursor in a new window.
522 */
523 if (bt_quickfix(curbuf))
524 {
Bram Moolenaar28c258f2006-01-25 22:02:51 +0000525 sprintf((char *)cbuf, "split +%ld%s",
526 (long)curwin->w_cursor.lnum,
527 (curwin->w_llist_ref == NULL) ? "cc" : "ll");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000528 do_cmdline_cmd(cbuf);
529 }
530#endif
531 break;
532
533
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534/* CTRL-W g extended commands */
535 case 'g':
536 case Ctrl_G:
537 CHECK_CMDWIN
538#ifdef USE_ON_FLY_SCROLL
539 dont_scroll = TRUE; /* disallow scrolling here */
540#endif
541 ++no_mapping;
542 ++allow_keys; /* no mapping for xchar, but allow key codes */
543 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000544 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 --no_mapping;
547 --allow_keys;
548#ifdef FEAT_CMDL_INFO
549 (void)add_to_showcmd(xchar);
550#endif
551 switch (xchar)
552 {
553#if defined(FEAT_QUICKFIX)
554 case '}':
555 xchar = Ctrl_RSB;
556 if (Prenum)
557 g_do_tagpreview = Prenum;
558 else
559 g_do_tagpreview = p_pvh;
560 /*FALLTHROUGH*/
561#endif
562 case ']':
563 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200564 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (Prenum)
566 postponed_split = Prenum;
567 else
568 postponed_split = -1;
569
570 /* Execute the command right here, required when
571 * "wincmd g}" was used in a function. */
572 do_nv_ident('g', xchar);
573 break;
574
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000575#ifdef FEAT_SEARCHPATH
576 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000577 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100578 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000579 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000580 goto wingotofile;
581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 default:
583 beep_flush();
584 break;
585 }
586 break;
587
588 default: beep_flush();
589 break;
590 }
591}
592
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100593/*
594 * Figure out the address type for ":wnncmd".
595 */
596 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100597get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100598{
599 switch (*arg)
600 {
601 case 'S':
602 case Ctrl_S:
603 case 's':
604 case Ctrl_N:
605 case 'n':
606 case 'j':
607 case Ctrl_J:
608 case 'k':
609 case Ctrl_K:
610 case 'T':
611 case Ctrl_R:
612 case 'r':
613 case 'R':
614 case 'K':
615 case 'J':
616 case '+':
617 case '-':
618 case Ctrl__:
619 case '_':
620 case '|':
621 case ']':
622 case Ctrl_RSB:
623 case 'g':
624 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100625 case Ctrl_V:
626 case 'v':
627 case 'h':
628 case Ctrl_H:
629 case 'l':
630 case Ctrl_L:
631 case 'H':
632 case 'L':
633 case '>':
634 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100635#if defined(FEAT_QUICKFIX)
636 case '}':
637#endif
638#ifdef FEAT_SEARCHPATH
639 case 'f':
640 case 'F':
641 case Ctrl_F:
642#endif
643#ifdef FEAT_FIND_ID
644 case 'i':
645 case Ctrl_I:
646 case 'd':
647 case Ctrl_D:
648#endif
649 /* window size or any count */
650 eap->addr_type = ADDR_LINES;
651 break;
652
653 case Ctrl_HAT:
654 case '^':
655 /* buffer number */
656 eap->addr_type = ADDR_BUFFERS;
657 break;
658
659 case Ctrl_Q:
660 case 'q':
661 case Ctrl_C:
662 case 'c':
663 case Ctrl_O:
664 case 'o':
665 case Ctrl_W:
666 case 'w':
667 case 'W':
668 case 'x':
669 case Ctrl_X:
670 /* window number */
671 eap->addr_type = ADDR_WINDOWS;
672 break;
673
674#if defined(FEAT_QUICKFIX)
675 case Ctrl_Z:
676 case 'z':
677 case 'P':
678#endif
679 case 't':
680 case Ctrl_T:
681 case 'b':
682 case Ctrl_B:
683 case 'p':
684 case Ctrl_P:
685 case '=':
686 case CAR:
687 /* no count */
688 eap->addr_type = 0;
689 break;
690 }
691}
692
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100693 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100694cmd_with_count(
695 char *cmd,
696 char_u *bufp,
697 size_t bufsize,
698 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100699{
700 size_t len = STRLEN(cmd);
701
702 STRCPY(bufp, cmd);
703 if (Prenum > 0)
704 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
705}
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707/*
708 * split the current window, implements CTRL-W s and :split
709 *
710 * "size" is the height or width for the new window, 0 to use half of current
711 * height or width.
712 *
713 * "flags":
714 * WSP_ROOM: require enough room for new window
715 * WSP_VERT: vertical split.
716 * WSP_TOP: open window at the top-left of the shell (help window).
717 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
718 * WSP_HELP: creating the help window, keep layout snapshot
719 *
720 * return FAIL for failure, OK otherwise
721 */
722 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100723win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000725 /* When the ":tab" modifier was used open a new tab page instead. */
726 if (may_open_tabpage() == OK)
727 return OK;
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 /* Add flags from ":vertical", ":topleft" and ":botright". */
730 flags |= cmdmod.split;
731 if ((flags & WSP_TOP) && (flags & WSP_BOT))
732 {
733 EMSG(_("E442: Can't split topleft and botright at the same time"));
734 return FAIL;
735 }
736
737 /* When creating the help window make a snapshot of the window layout.
738 * Otherwise clear the snapshot, it's now invalid. */
739 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000740 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000742 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743
744 return win_split_ins(size, flags, NULL, 0);
745}
746
747/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100748 * When "new_wp" is NULL: split the current window in two.
749 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 * top/left/right/bottom.
751 * return FAIL for failure, OK otherwise
752 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000753 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100754win_split_ins(
755 int size,
756 int flags,
757 win_T *new_wp,
758 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100760 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 win_T *oldwin;
762 int new_size = size;
763 int i;
764 int need_status = 0;
765 int do_equal = FALSE;
766 int needed;
767 int available;
768 int oldwin_height = 0;
769 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200770 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200772 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200773 int wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
775 if (flags & WSP_TOP)
776 oldwin = firstwin;
777 else if (flags & WSP_BOT)
778 oldwin = lastwin;
779 else
780 oldwin = curwin;
781
782 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100783 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100785 if (oldwin->w_height <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
787 EMSG(_(e_noroom));
788 return FAIL;
789 }
790 need_status = STATUS_HEIGHT;
791 }
792
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000793#ifdef FEAT_GUI
794 /* May be needed for the scrollbars that are going to change. */
795 if (gui.in_use)
796 out_flush();
797#endif
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 if (flags & WSP_VERT)
800 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200801 int wmw1;
802 int minwidth;
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805
806 /*
807 * Check if we are able to split the current window and compute its
808 * width.
809 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200810 /* Current window requires at least 1 space. */
811 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
812 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200814 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200815 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200817 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200819 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200821 else if (p_ea)
822 {
823 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
824 prevfrp = oldwin->w_frame;
825 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
826 frp = frp->fr_parent)
827 {
828 if (frp->fr_layout == FR_ROW)
829 for (frp2 = frp->fr_child; frp2 != NULL;
830 frp2 = frp2->fr_next)
831 if (frp2 != prevfrp)
832 minwidth += frame_minwidth(frp2, NOWIN);
833 prevfrp = frp;
834 }
835 available = topframe->fr_width;
836 needed += minwidth;
837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200839 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200840 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
841 available = oldwin->w_frame->fr_width;
842 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200843 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100844 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
846 EMSG(_(e_noroom));
847 return FAIL;
848 }
849 if (new_size == 0)
850 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200851 if (new_size > available - minwidth - 1)
852 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200853 if (new_size < wmw1)
854 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855
856 /* if it doesn't fit in the current window, need win_equal() */
857 if (oldwin->w_width - new_size - 1 < p_wmw)
858 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000859
860 /* We don't like to take lines for the new window from a
861 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100862 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000863 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100864 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000865
866 /* Only make all windows the same width if one of them (except oldwin)
867 * is wider than one of the split windows. */
868 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
869 && oldwin->w_frame->fr_parent != NULL)
870 {
871 frp = oldwin->w_frame->fr_parent->fr_child;
872 while (frp != NULL)
873 {
874 if (frp->fr_win != oldwin && frp->fr_win != NULL
875 && (frp->fr_win->w_width > new_size
876 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100877 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000878 {
879 do_equal = TRUE;
880 break;
881 }
882 frp = frp->fr_next;
883 }
884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {
888 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 /*
891 * Check if we are able to split the current window and compute its
892 * height.
893 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200894 /* Current window requires at least 1 space. */
895 wmh1 = (p_wmh == 0 ? 1 : p_wmh);
896 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200898 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200899 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200901 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200903 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200905 else if (p_ea)
906 {
907 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
908 prevfrp = oldwin->w_frame;
909 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
910 frp = frp->fr_parent)
911 {
912 if (frp->fr_layout == FR_COL)
913 for (frp2 = frp->fr_child; frp2 != NULL;
914 frp2 = frp2->fr_next)
915 if (frp2 != prevfrp)
916 minheight += frame_minheight(frp2, NOWIN);
917 prevfrp = frp;
918 }
919 available = topframe->fr_height;
920 needed += minheight;
921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 else
923 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200924 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
925 available = oldwin->w_frame->fr_height;
926 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100928 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
930 EMSG(_(e_noroom));
931 return FAIL;
932 }
933 oldwin_height = oldwin->w_height;
934 if (need_status)
935 {
936 oldwin->w_status_height = STATUS_HEIGHT;
937 oldwin_height -= STATUS_HEIGHT;
938 }
939 if (new_size == 0)
940 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200941 if (new_size > available - minheight - STATUS_HEIGHT)
942 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200943 if (new_size < wmh1)
944 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 /* if it doesn't fit in the current window, need win_equal() */
947 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
948 do_equal = TRUE;
949
950 /* We don't like to take lines for the new window from a
951 * 'winfixheight' window. Take them from a window above or below
952 * instead, if possible. */
953 if (oldwin->w_p_wfh)
954 {
955 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
956 oldwin);
957 oldwin_height = oldwin->w_height;
958 if (need_status)
959 oldwin_height -= STATUS_HEIGHT;
960 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000961
962 /* Only make all windows the same height if one of them (except oldwin)
963 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100964 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000965 && oldwin->w_frame->fr_parent != NULL)
966 {
967 frp = oldwin->w_frame->fr_parent->fr_child;
968 while (frp != NULL)
969 {
970 if (frp->fr_win != oldwin && frp->fr_win != NULL
971 && (frp->fr_win->w_height > new_size
972 || frp->fr_win->w_height > oldwin_height - new_size
973 - STATUS_HEIGHT))
974 {
975 do_equal = TRUE;
976 break;
977 }
978 frp = frp->fr_next;
979 }
980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 }
982
983 /*
984 * allocate new window structure and link it in the window list
985 */
986 if ((flags & WSP_TOP) == 0
987 && ((flags & WSP_BOT)
988 || (flags & WSP_BELOW)
989 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100990 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 {
992 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100993 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000994 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 else
996 win_append(oldwin, wp);
997 }
998 else
999 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001000 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001001 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 else
1003 win_append(oldwin->w_prev, wp);
1004 }
1005
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001006 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 {
1008 if (wp == NULL)
1009 return FAIL;
1010
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001011 new_frame(wp);
1012 if (wp->w_frame == NULL)
1013 {
1014 win_free(wp, NULL);
1015 return FAIL;
1016 }
1017
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001018 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001019 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 }
1021
1022 /*
1023 * Reorganise the tree of frames to insert the new window.
1024 */
1025 if (flags & (WSP_TOP | WSP_BOT))
1026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1028 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 curfrp = topframe->fr_child;
1031 if (flags & WSP_BOT)
1032 while (curfrp->fr_next != NULL)
1033 curfrp = curfrp->fr_next;
1034 }
1035 else
1036 curfrp = topframe;
1037 before = (flags & WSP_TOP);
1038 }
1039 else
1040 {
1041 curfrp = oldwin->w_frame;
1042 if (flags & WSP_BELOW)
1043 before = FALSE;
1044 else if (flags & WSP_ABOVE)
1045 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001046 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 before = !p_spr;
1048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 before = !p_sb;
1050 }
1051 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1052 {
1053 /* Need to create a new frame in the tree to make a branch. */
1054 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1055 *frp = *curfrp;
1056 curfrp->fr_layout = layout;
1057 frp->fr_parent = curfrp;
1058 frp->fr_next = NULL;
1059 frp->fr_prev = NULL;
1060 curfrp->fr_child = frp;
1061 curfrp->fr_win = NULL;
1062 curfrp = frp;
1063 if (frp->fr_win != NULL)
1064 oldwin->w_frame = frp;
1065 else
1066 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
1067 frp->fr_parent = curfrp;
1068 }
1069
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001070 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001071 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001073 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 frp->fr_parent = curfrp->fr_parent;
1075
1076 /* Insert the new frame at the right place in the frame list. */
1077 if (before)
1078 frame_insert(curfrp, frp);
1079 else
1080 frame_append(curfrp, frp);
1081
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001082 /* Set w_fraction now so that the cursor keeps the same relative
1083 * vertical position. */
Bram Moolenaar13d831f2011-01-08 14:46:03 +01001084 if (oldwin->w_height > 0)
1085 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001086 wp->w_fraction = oldwin->w_fraction;
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 if (flags & WSP_VERT)
1089 {
1090 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 if (need_status)
1093 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001094 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 oldwin->w_status_height = need_status;
1096 }
1097 if (flags & (WSP_TOP | WSP_BOT))
1098 {
1099 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001100 wp->w_winrow = tabline_height();
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001101 win_new_height(wp, curfrp->fr_height - (p_ls > 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 wp->w_status_height = (p_ls > 0);
1103 }
1104 else
1105 {
1106 /* height and row of new window is same as current window */
1107 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001108 win_new_height(wp, oldwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 wp->w_status_height = oldwin->w_status_height;
1110 }
1111 frp->fr_height = curfrp->fr_height;
1112
1113 /* "new_size" of the current window goes to the new window, use
1114 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001115 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 if (before)
1117 wp->w_vsep_width = 1;
1118 else
1119 {
1120 wp->w_vsep_width = oldwin->w_vsep_width;
1121 oldwin->w_vsep_width = 1;
1122 }
1123 if (flags & (WSP_TOP | WSP_BOT))
1124 {
1125 if (flags & WSP_BOT)
1126 frame_add_vsep(curfrp);
1127 /* Set width of neighbor frame */
1128 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001129 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1130 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 }
1132 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001133 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (before) /* new window left of current one */
1135 {
1136 wp->w_wincol = oldwin->w_wincol;
1137 oldwin->w_wincol += new_size + 1;
1138 }
1139 else /* new window right of current one */
1140 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1141 frame_fix_width(oldwin);
1142 frame_fix_width(wp);
1143 }
1144 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 if (flags & (WSP_TOP | WSP_BOT))
1148 {
1149 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001150 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 wp->w_vsep_width = 0;
1152 }
1153 else
1154 {
1155 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001156 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 wp->w_vsep_width = oldwin->w_vsep_width;
1158 }
1159 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
1161 /* "new_size" of the current window goes to the new window, use
1162 * one row for the status line */
1163 win_new_height(wp, new_size);
1164 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001165 {
1166 int new_fr_height = curfrp->fr_height - new_size;
1167
1168 if (!((flags & WSP_BOT) && p_ls == 0))
1169 new_fr_height -= STATUS_HEIGHT;
1170 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 else
1173 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1174 if (before) /* new window above current one */
1175 {
1176 wp->w_winrow = oldwin->w_winrow;
1177 wp->w_status_height = STATUS_HEIGHT;
1178 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1179 }
1180 else /* new window below current one */
1181 {
1182 wp->w_winrow = oldwin->w_winrow + oldwin->w_height + STATUS_HEIGHT;
1183 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001184 if (!(flags & WSP_BOT))
1185 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 if (flags & WSP_BOT)
1188 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 frame_fix_height(wp);
1190 frame_fix_height(oldwin);
1191 }
1192
1193 if (flags & (WSP_TOP | WSP_BOT))
1194 (void)win_comp_pos();
1195
1196 /*
1197 * Both windows need redrawing
1198 */
1199 redraw_win_later(wp, NOT_VALID);
1200 wp->w_redr_status = TRUE;
1201 redraw_win_later(oldwin, NOT_VALID);
1202 oldwin->w_redr_status = TRUE;
1203
1204 if (need_status)
1205 {
1206 msg_row = Rows - 1;
1207 msg_col = sc_col;
1208 msg_clr_eos_force(); /* Old command/ruler may still be there */
1209 comp_col();
1210 msg_row = Rows - 1;
1211 msg_col = 0; /* put position back at start of line */
1212 }
1213
1214 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001215 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 */
1217 if (do_equal || dir != 0)
1218 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001220 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
1222 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1223 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (flags & WSP_VERT)
1225 {
1226 i = p_wiw;
1227 if (size != 0)
1228 p_wiw = size;
1229
1230# ifdef FEAT_GUI
1231 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1232 if (gui.in_use)
1233 gui_init_which_components(NULL);
1234# endif
1235 }
1236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 {
1238 i = p_wh;
1239 if (size != 0)
1240 p_wh = size;
1241 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001242
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001243#ifdef FEAT_JUMPLIST
1244 /* Keep same changelist position in new window. */
1245 wp->w_changelistidx = oldwin->w_changelistidx;
1246#endif
1247
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001248 /*
1249 * make the new window the current window
1250 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001251 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 if (flags & WSP_VERT)
1253 p_wiw = i;
1254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 p_wh = i;
1256
1257 return OK;
1258}
1259
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001260
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001261/*
1262 * Initialize window "newp" from window "oldp".
1263 * Used when splitting a window and when creating a new tab page.
1264 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001265 * WSP_NEWLOC may be specified in flags to prevent the location list from
1266 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001267 */
1268 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001269win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001270{
1271 int i;
1272
1273 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001274#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001275 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001276#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001277 oldp->w_buffer->b_nwindows++;
1278 newp->w_cursor = oldp->w_cursor;
1279 newp->w_valid = 0;
1280 newp->w_curswant = oldp->w_curswant;
1281 newp->w_set_curswant = oldp->w_set_curswant;
1282 newp->w_topline = oldp->w_topline;
1283#ifdef FEAT_DIFF
1284 newp->w_topfill = oldp->w_topfill;
1285#endif
1286 newp->w_leftcol = oldp->w_leftcol;
1287 newp->w_pcmark = oldp->w_pcmark;
1288 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1289 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001290 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001291 newp->w_fraction = oldp->w_fraction;
1292 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1293#ifdef FEAT_JUMPLIST
1294 copy_jumplist(oldp, newp);
1295#endif
1296#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001297 if (flags & WSP_NEWLOC)
1298 {
1299 /* Don't copy the location list. */
1300 newp->w_llist = NULL;
1301 newp->w_llist_ref = NULL;
1302 }
1303 else
1304 copy_loclist(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001305#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001306 newp->w_localdir = (oldp->w_localdir == NULL)
1307 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001308
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001309 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001310 for (i = 0; i < oldp->w_tagstacklen; i++)
1311 {
1312 newp->w_tagstack[i] = oldp->w_tagstack[i];
1313 if (newp->w_tagstack[i].tagname != NULL)
1314 newp->w_tagstack[i].tagname =
1315 vim_strsave(newp->w_tagstack[i].tagname);
1316 }
1317 newp->w_tagstackidx = oldp->w_tagstackidx;
1318 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001319#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001320 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001321#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001322
1323 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001324
Bram Moolenaara971b822011-09-14 14:43:25 +02001325#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001326 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001327#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001328}
1329
1330/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001331 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001332 * Only the essential things are copied.
1333 */
1334 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001335win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001336{
1337 /* Use the same argument list. */
1338 newp->w_alist = oldp->w_alist;
1339 ++newp->w_alist->al_refcount;
1340 newp->w_arg_idx = oldp->w_arg_idx;
1341
1342 /* copy options from existing window */
1343 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001344}
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001348 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 */
1350 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001351win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352{
1353 win_T *wp;
1354
1355 if (win == NULL)
1356 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001357 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (wp == win)
1359 return TRUE;
1360 return FALSE;
1361}
1362
1363/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001364 * Check if "win" is a pointer to an existing window in any tab page.
1365 */
1366 int
1367win_valid_any_tab(win_T *win)
1368{
1369 win_T *wp;
1370 tabpage_T *tp;
1371
1372 if (win == NULL)
1373 return FALSE;
1374 FOR_ALL_TABPAGES(tp)
1375 {
1376 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1377 {
1378 if (wp == win)
1379 return TRUE;
1380 }
1381 }
1382 return FALSE;
1383}
1384
1385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 * Return the number of windows.
1387 */
1388 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001389win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390{
1391 win_T *wp;
1392 int count = 0;
1393
Bram Moolenaar29323592016-07-24 22:04:11 +02001394 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 ++count;
1396 return count;
1397}
1398
1399/*
1400 * Make "count" windows on the screen.
1401 * Return actual number of windows on the screen.
1402 * Must be called when there is just one window, filling the whole screen
1403 * (excluding the command line).
1404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001406make_windows(
1407 int count,
1408 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 int maxcount;
1411 int todo;
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (vertical)
1414 {
1415 /* Each windows needs at least 'winminwidth' lines and a separator
1416 * column. */
1417 maxcount = (curwin->w_width + curwin->w_vsep_width
1418 - (p_wiw - p_wmw)) / (p_wmw + 1);
1419 }
1420 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 {
1422 /* Each window needs at least 'winminheight' lines and a status line. */
1423 maxcount = (curwin->w_height + curwin->w_status_height
1424 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1425 }
1426
1427 if (maxcount < 2)
1428 maxcount = 2;
1429 if (count > maxcount)
1430 count = maxcount;
1431
1432 /*
1433 * add status line now, otherwise first window will be too big
1434 */
1435 if (count > 1)
1436 last_status(TRUE);
1437
1438#ifdef FEAT_AUTOCMD
1439 /*
1440 * Don't execute autocommands while creating the windows. Must do that
1441 * when putting the buffers in the windows.
1442 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001443 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444#endif
1445
1446 /* todo is number of windows left to create */
1447 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (vertical)
1449 {
1450 if (win_split(curwin->w_width - (curwin->w_width - todo)
1451 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1452 break;
1453 }
1454 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 {
1456 if (win_split(curwin->w_height - (curwin->w_height - todo
1457 * STATUS_HEIGHT) / (todo + 1)
1458 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1459 break;
1460 }
1461
1462#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001463 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464#endif
1465
1466 /* return actual number of windows */
1467 return (count - todo);
1468}
1469
1470/*
1471 * Exchange current and next window
1472 */
1473 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001474win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475{
1476 frame_T *frp;
1477 frame_T *frp2;
1478 win_T *wp;
1479 win_T *wp2;
1480 int temp;
1481
Bram Moolenaar459ca562016-11-10 18:16:33 +01001482 if (ONE_WINDOW) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 beep_flush();
1485 return;
1486 }
1487
1488#ifdef FEAT_GUI
1489 need_mouse_correct = TRUE;
1490#endif
1491
1492 /*
1493 * find window to exchange with
1494 */
1495 if (Prenum)
1496 {
1497 frp = curwin->w_frame->fr_parent->fr_child;
1498 while (frp != NULL && --Prenum > 0)
1499 frp = frp->fr_next;
1500 }
1501 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1502 frp = curwin->w_frame->fr_next;
1503 else /* Swap last window in row/col with previous */
1504 frp = curwin->w_frame->fr_prev;
1505
1506 /* We can only exchange a window with another window, not with a frame
1507 * containing windows. */
1508 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1509 return;
1510 wp = frp->fr_win;
1511
1512/*
1513 * 1. remove curwin from the list. Remember after which window it was in wp2
1514 * 2. insert curwin before wp in the list
1515 * if wp != wp2
1516 * 3. remove wp from the list
1517 * 4. insert wp after wp2
1518 * 5. exchange the status line height and vsep width.
1519 */
1520 wp2 = curwin->w_prev;
1521 frp2 = curwin->w_frame->fr_prev;
1522 if (wp->w_prev != curwin)
1523 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001524 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 frame_remove(curwin->w_frame);
1526 win_append(wp->w_prev, curwin);
1527 frame_insert(frp, curwin->w_frame);
1528 }
1529 if (wp != wp2)
1530 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001531 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 frame_remove(wp->w_frame);
1533 win_append(wp2, wp);
1534 if (frp2 == NULL)
1535 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1536 else
1537 frame_append(frp2, wp->w_frame);
1538 }
1539 temp = curwin->w_status_height;
1540 curwin->w_status_height = wp->w_status_height;
1541 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 temp = curwin->w_vsep_width;
1543 curwin->w_vsep_width = wp->w_vsep_width;
1544 wp->w_vsep_width = temp;
1545
1546 /* If the windows are not in the same frame, exchange the sizes to avoid
1547 * messing up the window layout. Otherwise fix the frame sizes. */
1548 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1549 {
1550 temp = curwin->w_height;
1551 curwin->w_height = wp->w_height;
1552 wp->w_height = temp;
1553 temp = curwin->w_width;
1554 curwin->w_width = wp->w_width;
1555 wp->w_width = temp;
1556 }
1557 else
1558 {
1559 frame_fix_height(curwin);
1560 frame_fix_height(wp);
1561 frame_fix_width(curwin);
1562 frame_fix_width(wp);
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
1565 (void)win_comp_pos(); /* recompute window positions */
1566
1567 win_enter(wp, TRUE);
1568 redraw_later(CLEAR);
1569}
1570
1571/*
1572 * rotate windows: if upwards TRUE the second window becomes the first one
1573 * if upwards FALSE the first window becomes the second one
1574 */
1575 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001576win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577{
1578 win_T *wp1;
1579 win_T *wp2;
1580 frame_T *frp;
1581 int n;
1582
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001583 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 beep_flush();
1586 return;
1587 }
1588
1589#ifdef FEAT_GUI
1590 need_mouse_correct = TRUE;
1591#endif
1592
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 /* Check if all frames in this row/col have one window. */
1594 for (frp = curwin->w_frame->fr_parent->fr_child; frp != NULL;
1595 frp = frp->fr_next)
1596 if (frp->fr_win == NULL)
1597 {
1598 EMSG(_("E443: Cannot rotate when another window is split"));
1599 return;
1600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601
1602 while (count--)
1603 {
1604 if (upwards) /* first window becomes last window */
1605 {
1606 /* remove first window/frame from the list */
1607 frp = curwin->w_frame->fr_parent->fr_child;
1608 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001609 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 frame_remove(frp);
1611
1612 /* find last frame and append removed window/frame after it */
1613 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1614 ;
1615 win_append(frp->fr_win, wp1);
1616 frame_append(frp, wp1->w_frame);
1617
1618 wp2 = frp->fr_win; /* previously last window */
1619 }
1620 else /* last window becomes first window */
1621 {
1622 /* find last window/frame in the list and remove it */
1623 for (frp = curwin->w_frame; frp->fr_next != NULL;
1624 frp = frp->fr_next)
1625 ;
1626 wp1 = frp->fr_win;
1627 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001628 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 frame_remove(frp);
1630
1631 /* append the removed window/frame before the first in the list */
1632 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1633 frame_insert(frp->fr_parent->fr_child, frp);
1634 }
1635
1636 /* exchange status height and vsep width of old and new last window */
1637 n = wp2->w_status_height;
1638 wp2->w_status_height = wp1->w_status_height;
1639 wp1->w_status_height = n;
1640 frame_fix_height(wp1);
1641 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 n = wp2->w_vsep_width;
1643 wp2->w_vsep_width = wp1->w_vsep_width;
1644 wp1->w_vsep_width = n;
1645 frame_fix_width(wp1);
1646 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001648 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 (void)win_comp_pos();
1650 }
1651
1652 redraw_later(CLEAR);
1653}
1654
1655/*
1656 * Move the current window to the very top/bottom/left/right of the screen.
1657 */
1658 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001659win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660{
1661 int dir;
1662 int height = curwin->w_height;
1663
Bram Moolenaar459ca562016-11-10 18:16:33 +01001664 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 {
1666 beep_flush();
1667 return;
1668 }
1669
1670 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001671 (void)winframe_remove(curwin, &dir, NULL);
1672 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 last_status(FALSE); /* may need to remove last status line */
1674 (void)win_comp_pos(); /* recompute window positions */
1675
1676 /* Split a window on the desired side and put the window there. */
1677 (void)win_split_ins(size, flags, curwin, dir);
1678 if (!(flags & WSP_VERT))
1679 {
1680 win_setheight(height);
1681 if (p_ea)
1682 win_equal(curwin, TRUE, 'v');
1683 }
1684
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001685#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1687 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001688 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690}
1691
1692/*
1693 * Move window "win1" to below/right of "win2" and make "win1" the current
1694 * window. Only works within the same frame!
1695 */
1696 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001697win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698{
1699 int height;
1700
1701 /* check if the arguments are reasonable */
1702 if (win1 == win2)
1703 return;
1704
1705 /* check if there is something to do */
1706 if (win2->w_next != win1)
1707 {
1708 /* may need move the status line/vertical separator of the last window
1709 * */
1710 if (win1 == lastwin)
1711 {
1712 height = win1->w_prev->w_status_height;
1713 win1->w_prev->w_status_height = win1->w_status_height;
1714 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001715 if (win1->w_prev->w_vsep_width == 1)
1716 {
1717 /* Remove the vertical separator from the last-but-one window,
1718 * add it to the last window. Adjust the frame widths. */
1719 win1->w_prev->w_vsep_width = 0;
1720 win1->w_prev->w_frame->fr_width -= 1;
1721 win1->w_vsep_width = 1;
1722 win1->w_frame->fr_width += 1;
1723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 }
1725 else if (win2 == lastwin)
1726 {
1727 height = win1->w_status_height;
1728 win1->w_status_height = win2->w_status_height;
1729 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001730 if (win1->w_vsep_width == 1)
1731 {
1732 /* Remove the vertical separator from win1, add it to the last
1733 * window, win2. Adjust the frame widths. */
1734 win2->w_vsep_width = 1;
1735 win2->w_frame->fr_width += 1;
1736 win1->w_vsep_width = 0;
1737 win1->w_frame->fr_width -= 1;
1738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001740 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 frame_remove(win1->w_frame);
1742 win_append(win2, win1);
1743 frame_append(win2->w_frame, win1->w_frame);
1744
1745 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1746 redraw_later(NOT_VALID);
1747 }
1748 win_enter(win1, FALSE);
1749}
1750
1751/*
1752 * Make all windows the same height.
1753 * 'next_curwin' will soon be the current window, make sure it has enough
1754 * rows.
1755 */
1756 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001757win_equal(
1758 win_T *next_curwin, /* pointer to current window to be or NULL */
1759 int current, /* do only frame with current window */
1760 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 'b' for both, 0 for using p_ead */
1762{
1763 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001766 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001767 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768}
1769
1770/*
1771 * Set a frame to a new position and height, spreading the available room
1772 * equally over contained frames.
1773 * The window "next_curwin" (if not NULL) should at least get the size from
1774 * 'winheight' and 'winwidth' if possible.
1775 */
1776 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001777win_equal_rec(
1778 win_T *next_curwin, /* pointer to current window to be or NULL */
1779 int current, /* do only frame with current window */
1780 frame_T *topfr, /* frame to set size off */
1781 int dir, /* 'v', 'h' or 'b', see win_equal() */
1782 int col, /* horizontal position for frame */
1783 int row, /* vertical position for frame */
1784 int width, /* new width of frame */
1785 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 int n, m;
1788 int extra_sep = 0;
1789 int wincount, totwincount = 0;
1790 frame_T *fr;
1791 int next_curwin_size = 0;
1792 int room = 0;
1793 int new_size;
1794 int has_next_curwin = 0;
1795 int hnc;
1796
1797 if (topfr->fr_layout == FR_LEAF)
1798 {
1799 /* Set the width/height of this frame.
1800 * Redraw when size or position changes */
1801 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 )
1804 {
1805 topfr->fr_win->w_winrow = row;
1806 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001808 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 redraw_all_later(CLEAR);
1810 }
1811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 else if (topfr->fr_layout == FR_ROW)
1813 {
1814 topfr->fr_width = width;
1815 topfr->fr_height = height;
1816
1817 if (dir != 'v') /* equalize frame widths */
1818 {
1819 /* Compute the maximum number of windows horizontally in this
1820 * frame. */
1821 n = frame_minwidth(topfr, NOWIN);
1822 /* add one for the rightmost window, it doesn't have a separator */
1823 if (col + width == Columns)
1824 extra_sep = 1;
1825 else
1826 extra_sep = 0;
1827 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001828 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001830 /*
1831 * Compute width for "next_curwin" window and room available for
1832 * other windows.
1833 * "m" is the minimal width when counting p_wiw for "next_curwin".
1834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 m = frame_minwidth(topfr, next_curwin);
1836 room = width - m;
1837 if (room < 0)
1838 {
1839 next_curwin_size = p_wiw + room;
1840 room = 0;
1841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 else
1843 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001844 next_curwin_size = -1;
1845 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1846 {
1847 /* If 'winfixwidth' set keep the window width if
1848 * possible.
1849 * Watch out for this window being the next_curwin. */
1850 if (frame_fixed_width(fr))
1851 {
1852 n = frame_minwidth(fr, NOWIN);
1853 new_size = fr->fr_width;
1854 if (frame_has_win(fr, next_curwin))
1855 {
1856 room += p_wiw - p_wmw;
1857 next_curwin_size = 0;
1858 if (new_size < p_wiw)
1859 new_size = p_wiw;
1860 }
1861 else
1862 /* These windows don't use up room. */
1863 totwincount -= (n + (fr->fr_next == NULL
1864 ? extra_sep : 0)) / (p_wmw + 1);
1865 room -= new_size - n;
1866 if (room < 0)
1867 {
1868 new_size += room;
1869 room = 0;
1870 }
1871 fr->fr_newwidth = new_size;
1872 }
1873 }
1874 if (next_curwin_size == -1)
1875 {
1876 if (!has_next_curwin)
1877 next_curwin_size = 0;
1878 else if (totwincount > 1
1879 && (room + (totwincount - 2))
1880 / (totwincount - 1) > p_wiw)
1881 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001882 /* Can make all windows wider than 'winwidth', spread
1883 * the room equally. */
1884 next_curwin_size = (room + p_wiw
1885 + (totwincount - 1) * p_wmw
1886 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001887 room -= next_curwin_size - p_wiw;
1888 }
1889 else
1890 next_curwin_size = p_wiw;
1891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001893
1894 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 --totwincount; /* don't count curwin */
1896 }
1897
1898 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1899 {
1900 n = m = 0;
1901 wincount = 1;
1902 if (fr->fr_next == NULL)
1903 /* last frame gets all that remains (avoid roundoff error) */
1904 new_size = width;
1905 else if (dir == 'v')
1906 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001907 else if (frame_fixed_width(fr))
1908 {
1909 new_size = fr->fr_newwidth;
1910 wincount = 0; /* doesn't count as a sizeable window */
1911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 else
1913 {
1914 /* Compute the maximum number of windows horiz. in "fr". */
1915 n = frame_minwidth(fr, NOWIN);
1916 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1917 / (p_wmw + 1);
1918 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001919 if (has_next_curwin)
1920 hnc = frame_has_win(fr, next_curwin);
1921 else
1922 hnc = FALSE;
1923 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001925 if (totwincount == 0)
1926 new_size = room;
1927 else
1928 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001930 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 {
1932 next_curwin_size -= p_wiw - (m - n);
1933 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001934 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001936 else
1937 room -= new_size;
1938 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001941 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 * window, unless equalizing all frames. */
1943 if (!current || dir != 'v' || topfr->fr_parent != NULL
1944 || (new_size != fr->fr_width)
1945 || frame_has_win(fr, next_curwin))
1946 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001947 new_size, height);
1948 col += new_size;
1949 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 totwincount -= wincount;
1951 }
1952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 else /* topfr->fr_layout == FR_COL */
1954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 topfr->fr_height = height;
1957
1958 if (dir != 'h') /* equalize frame heights */
1959 {
1960 /* Compute maximum number of windows vertically in this frame. */
1961 n = frame_minheight(topfr, NOWIN);
1962 /* add one for the bottom window if it doesn't have a statusline */
1963 if (row + height == cmdline_row && p_ls == 0)
1964 extra_sep = 1;
1965 else
1966 extra_sep = 0;
1967 totwincount = (n + extra_sep) / (p_wmh + 1);
1968 has_next_curwin = frame_has_win(topfr, next_curwin);
1969
1970 /*
1971 * Compute height for "next_curwin" window and room available for
1972 * other windows.
1973 * "m" is the minimal height when counting p_wh for "next_curwin".
1974 */
1975 m = frame_minheight(topfr, next_curwin);
1976 room = height - m;
1977 if (room < 0)
1978 {
1979 /* The room is less then 'winheight', use all space for the
1980 * current window. */
1981 next_curwin_size = p_wh + room;
1982 room = 0;
1983 }
1984 else
1985 {
1986 next_curwin_size = -1;
1987 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
1988 {
1989 /* If 'winfixheight' set keep the window height if
1990 * possible.
1991 * Watch out for this window being the next_curwin. */
1992 if (frame_fixed_height(fr))
1993 {
1994 n = frame_minheight(fr, NOWIN);
1995 new_size = fr->fr_height;
1996 if (frame_has_win(fr, next_curwin))
1997 {
1998 room += p_wh - p_wmh;
1999 next_curwin_size = 0;
2000 if (new_size < p_wh)
2001 new_size = p_wh;
2002 }
2003 else
2004 /* These windows don't use up room. */
2005 totwincount -= (n + (fr->fr_next == NULL
2006 ? extra_sep : 0)) / (p_wmh + 1);
2007 room -= new_size - n;
2008 if (room < 0)
2009 {
2010 new_size += room;
2011 room = 0;
2012 }
2013 fr->fr_newheight = new_size;
2014 }
2015 }
2016 if (next_curwin_size == -1)
2017 {
2018 if (!has_next_curwin)
2019 next_curwin_size = 0;
2020 else if (totwincount > 1
2021 && (room + (totwincount - 2))
2022 / (totwincount - 1) > p_wh)
2023 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002024 /* can make all windows higher than 'winheight',
2025 * spread the room equally. */
2026 next_curwin_size = (room + p_wh
2027 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 + (totwincount - 1)) / totwincount;
2029 room -= next_curwin_size - p_wh;
2030 }
2031 else
2032 next_curwin_size = p_wh;
2033 }
2034 }
2035
2036 if (has_next_curwin)
2037 --totwincount; /* don't count curwin */
2038 }
2039
2040 for (fr = topfr->fr_child; fr != NULL; fr = fr->fr_next)
2041 {
2042 n = m = 0;
2043 wincount = 1;
2044 if (fr->fr_next == NULL)
2045 /* last frame gets all that remains (avoid roundoff error) */
2046 new_size = height;
2047 else if (dir == 'h')
2048 new_size = fr->fr_height;
2049 else if (frame_fixed_height(fr))
2050 {
2051 new_size = fr->fr_newheight;
2052 wincount = 0; /* doesn't count as a sizeable window */
2053 }
2054 else
2055 {
2056 /* Compute the maximum number of windows vert. in "fr". */
2057 n = frame_minheight(fr, NOWIN);
2058 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2059 / (p_wmh + 1);
2060 m = frame_minheight(fr, next_curwin);
2061 if (has_next_curwin)
2062 hnc = frame_has_win(fr, next_curwin);
2063 else
2064 hnc = FALSE;
2065 if (hnc) /* don't count next_curwin */
2066 --wincount;
2067 if (totwincount == 0)
2068 new_size = room;
2069 else
2070 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2071 / totwincount;
2072 if (hnc) /* add next_curwin size */
2073 {
2074 next_curwin_size -= p_wh - (m - n);
2075 new_size += next_curwin_size;
2076 room -= new_size - next_curwin_size;
2077 }
2078 else
2079 room -= new_size;
2080 new_size += n;
2081 }
2082 /* Skip frame that is full width when splitting or closing a
2083 * window, unless equalizing all frames. */
2084 if (!current || dir != 'h' || topfr->fr_parent != NULL
2085 || (new_size != fr->fr_height)
2086 || frame_has_win(fr, next_curwin))
2087 win_equal_rec(next_curwin, current, fr, dir, col, row,
2088 width, new_size);
2089 row += new_size;
2090 height -= new_size;
2091 totwincount -= wincount;
2092 }
2093 }
2094}
2095
2096/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002097 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 */
2099 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002100close_windows(
2101 buf_T *buf,
2102 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002104 win_T *wp;
2105 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002106 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002107#ifdef FEAT_AUTOCMD
2108 int count = tabpage_index(NULL);
2109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002112
Bram Moolenaar459ca562016-11-10 18:16:33 +01002113 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002115 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
2116#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002117 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002118#endif
2119 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002121 if (win_close(wp, FALSE) == FAIL)
2122 /* If closing the window fails give up, to avoid looping
2123 * forever. */
2124 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002125
2126 /* Start all over, autocommands may change the window layout. */
2127 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 }
2129 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002130 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002132
2133 /* Also check windows in other tab pages. */
2134 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2135 {
2136 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002137 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002138 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002139 if (wp->w_buffer == buf
2140#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002141 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002142#endif
2143 )
Bram Moolenaarf740b292006-02-16 22:11:02 +00002144 {
2145 win_close_othertab(wp, FALSE, tp);
2146
2147 /* Start all over, the tab page may be closed and
2148 * autocommands may change the window layout. */
2149 nexttp = first_tabpage;
2150 break;
2151 }
2152 }
2153
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002155
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002156#ifdef FEAT_AUTOCMD
2157 if (count != tabpage_index(NULL))
2158 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
2159#endif
2160
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002161 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002162 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002163 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164}
2165
2166/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002167 * Return TRUE if the current window is the only window that exists (ignoring
2168 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002169 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002170 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002171 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002172last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002173{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002174 return (one_window() && first_tabpage->tp_next == NULL);
2175}
2176
2177/*
2178 * Return TRUE if there is only one window other than "aucmd_win" in the
2179 * current tab page.
2180 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002181 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002182one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002183{
2184#ifdef FEAT_AUTOCMD
2185 win_T *wp;
2186 int seen_one = FALSE;
2187
2188 FOR_ALL_WINDOWS(wp)
2189 {
2190 if (wp != aucmd_win)
2191 {
2192 if (seen_one)
2193 return FALSE;
2194 seen_one = TRUE;
2195 }
2196 }
2197 return TRUE;
2198#else
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002199 return ONE_WINDOW;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002200#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002201}
2202
2203/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002204 * Close the possibly last window in a tab page.
2205 * Returns TRUE when the window was closed already.
2206 */
2207 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002208close_last_window_tabpage(
2209 win_T *win,
2210 int free_buf,
2211 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002212{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002213 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002214 {
Bram Moolenaar07729b22013-05-15 23:13:10 +02002215#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002216 buf_T *old_curbuf = curbuf;
Bram Moolenaar07729b22013-05-15 23:13:10 +02002217#endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002218
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002219 /*
2220 * Closing the last window in a tab page. First go to another tab
2221 * page and then close the window and the tab page. This avoids that
2222 * curwin and curtab are invalid while we are freeing memory, they may
2223 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002224 * Don't trigger autocommands yet, they may use wrong values, so do
2225 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002226 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002227 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002228 redraw_tabline = TRUE;
2229
2230 /* Safety check: Autocommands may have closed the window when jumping
2231 * to the other tab page. */
2232 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2233 {
2234 int h = tabline_height();
2235
2236 win_close_othertab(win, free_buf, prev_curtab);
2237 if (h != tabline_height())
2238 shell_new_rows();
2239 }
Bram Moolenaara8596c42012-06-13 14:28:20 +02002240 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2241 * that now. */
2242#ifdef FEAT_AUTOCMD
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002243 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002244 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002245 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2246 if (old_curbuf != curbuf)
2247 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002248#endif
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002249 return TRUE;
2250 }
2251 return FALSE;
2252}
2253
2254/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 * If "free_buf" is TRUE related buffer may be unloaded.
2257 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002258 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002259 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002261 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002262win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 win_T *wp;
2265#ifdef FEAT_AUTOCMD
2266 int other_buffer = FALSE;
2267#endif
2268 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 int dir;
2270 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002271 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002272 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002274 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
2276 EMSG(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002280#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002281 if (win->w_closing || (win->w_buffer != NULL
2282 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002283 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002284 if (win == aucmd_win)
2285 {
2286 EMSG(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002287 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002288 }
2289 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2290 {
2291 EMSG(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002292 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002293 }
2294#endif
2295
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002296 /* When closing the last window in a tab page first go to another tab page
2297 * and then close the window and the tab page to avoid that curwin and
2298 * curtab are invalid while we are freeing memory. */
2299 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002300 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002301
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 /* When closing the help window, try restoring a snapshot after closing
2303 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002304 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 help_window = TRUE;
2306 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002307 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309#ifdef FEAT_AUTOCMD
2310 if (win == curwin)
2311 {
2312 /*
2313 * Guess which window is going to be the new current window.
2314 * This may change because of the autocommands (sigh).
2315 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002316 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002319 * Be careful: If autocommands delete the window or cause this window
2320 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 */
2322 if (wp->w_buffer != curbuf)
2323 {
2324 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002325 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002327 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002328 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002329 win->w_closing = FALSE;
2330 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002331 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002333 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002335 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002336 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002337 win->w_closing = FALSE;
2338 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340# ifdef FEAT_EVAL
2341 /* autocmds may abort script processing */
2342 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344# endif
2345 }
2346#endif
2347
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002348#ifdef FEAT_GUI
2349 /* Avoid trouble with scrollbars that are going to be deleted in
2350 * win_free(). */
2351 if (gui.in_use)
2352 out_flush();
2353#endif
2354
Bram Moolenaara971b822011-09-14 14:43:25 +02002355#ifdef FEAT_SYN_HL
2356 /* Free independent synblock before the buffer is freed. */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002357 if (win->w_buffer != NULL)
2358 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002359#endif
2360
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 /*
2362 * Close the link to the buffer.
2363 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002364 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002365 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002366 bufref_T bufref;
2367
2368 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002369#ifdef FEAT_AUTOCMD
2370 win->w_closing = TRUE;
2371#endif
Bram Moolenaar8f913992012-08-29 15:50:26 +02002372 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002373#ifdef FEAT_AUTOCMD
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002374 if (win_valid_any_tab(win))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002375 win->w_closing = FALSE;
2376#endif
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002377 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2378 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002379 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002380 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002381 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002382
Bram Moolenaar802418d2013-01-17 14:00:11 +01002383 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2384 && (last_window() || curtab != prev_curtab
2385 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002386 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002387 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002388 * curwin->w_buffer, otherwise writing viminfo may fail. */
2389 if (curwin->w_buffer == NULL)
2390 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002391 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002392 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002393
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002394 /* Autocommands may have moved to another tab page. */
2395 if (curtab != prev_curtab && win_valid_any_tab(win)
2396 && win->w_buffer == NULL)
2397 {
2398 /* Need to close the window anyway, since the buffer is NULL. */
2399 win_close_othertab(win, FALSE, prev_curtab);
2400 return FAIL;
2401 }
2402
2403 /* Autocommands may have closed the window already or closed the only
2404 * other window. */
2405 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002406 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
Bram Moolenaara971b822011-09-14 14:43:25 +02002409 /* Free the memory used for the window and get the window that received
2410 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002411 wp = win_free_mem(win, &dir, NULL);
2412
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 /* Make sure curwin isn't invalid. It can cause severe trouble when
2414 * printing an error message. For win_equal() curbuf needs to be valid
2415 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002416 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 curwin = wp;
2419#ifdef FEAT_QUICKFIX
2420 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2421 {
2422 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002423 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 * finding another window to go to.
2425 */
2426 for (;;)
2427 {
2428 if (wp->w_next == NULL)
2429 wp = firstwin;
2430 else
2431 wp = wp->w_next;
2432 if (wp == curwin)
2433 break;
2434 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2435 {
2436 curwin = wp;
2437 break;
2438 }
2439 }
2440 }
2441#endif
2442 curbuf = curwin->w_buffer;
2443 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002444
2445 /* The cursor position may be invalid if the buffer changed after last
2446 * using the window. */
2447 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002449 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002450 /* If the frame of the closed window contains the new current window,
2451 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002452 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else
2454 win_comp_pos();
2455 if (close_curwin)
2456 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002457 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458#ifdef FEAT_AUTOCMD
2459 if (other_buffer)
2460 /* careful: after this wp and win may be invalid! */
2461 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
2462#endif
2463 }
2464
2465 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002466 * If last window has a status line now and we don't want one,
2467 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
2469 last_status(FALSE);
2470
2471 /* After closing the help window, try restoring the window layout from
2472 * before it was opened. */
2473 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002474 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002476#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2478 if (gui.in_use && !win_hasvertsplit())
2479 gui_init_which_components(NULL);
2480#endif
2481
2482 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002483 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484}
2485
2486/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002487 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002488 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002489 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002490 * Caller must check if buffer is hidden and whether the tabline needs to be
2491 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002492 */
2493 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002494win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002495{
2496 win_T *wp;
2497 int dir;
2498 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002499 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002500
Bram Moolenaar362ce482012-06-06 19:02:45 +02002501#ifdef FEAT_AUTOCMD
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002502 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2503 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002504 if (win->w_closing || (win->w_buffer != NULL
2505 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002506 return; /* window is already being closed */
2507#endif
2508
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002509 if (win->w_buffer != NULL)
2510 /* Close the link to the buffer. */
2511 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002512
2513 /* Careful: Autocommands may have closed the tab page or made it the
2514 * current tab page. */
2515 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2516 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002517 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002518 return;
2519
2520 /* Autocommands may have closed the window already. */
2521 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2522 ;
2523 if (wp == NULL)
2524 return;
2525
Bram Moolenaarf740b292006-02-16 22:11:02 +00002526 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002527 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002528 {
2529 if (tp == first_tabpage)
2530 first_tabpage = tp->tp_next;
2531 else
2532 {
2533 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2534 ptp = ptp->tp_next)
2535 ;
2536 if (ptp == NULL)
2537 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002538 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002539 return;
2540 }
2541 ptp->tp_next = tp->tp_next;
2542 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002543 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002544 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002545
2546 /* Free the memory used for the window. */
2547 win_free_mem(win, &dir, tp);
2548
2549 if (free_tp)
2550 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002551}
2552
2553/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002554 * Free the memory used for a window.
2555 * Returns a pointer to the window that got the freed up space.
2556 */
2557 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002558win_free_mem(
2559 win_T *win,
2560 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2561 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002562{
2563 frame_T *frp;
2564 win_T *wp;
2565
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002566 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002567 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002568 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002569 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002570 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002571
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002572 /* When deleting the current window of another tab page select a new
2573 * current window. */
2574 if (tp != NULL && win == tp->tp_curwin)
2575 tp->tp_curwin = wp;
2576
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002577 return wp;
2578}
2579
2580#if defined(EXITFREE) || defined(PROTO)
2581 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002582win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002583{
2584 int dummy;
2585
Bram Moolenaarf740b292006-02-16 22:11:02 +00002586 while (first_tabpage->tp_next != NULL)
2587 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002588
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002589# ifdef FEAT_AUTOCMD
2590 if (aucmd_win != NULL)
2591 {
2592 (void)win_free_mem(aucmd_win, &dummy, NULL);
2593 aucmd_win = NULL;
2594 }
2595# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002596
2597 while (firstwin != NULL)
2598 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002599
2600 /* No window should be used after this. Set curwin to NULL to crash
2601 * instead of using freed memory. */
2602 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002603}
2604#endif
2605
2606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 * Remove a window and its frame from the tree of frames.
2608 * Returns a pointer to the window that got the freed up space.
2609 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002610 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002611winframe_remove(
2612 win_T *win,
2613 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2614 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 frame_T *frp, *frp2, *frp3;
2617 frame_T *frp_close = win->w_frame;
2618 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619
2620 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002621 * If there is only one window there is nothing to remove.
2622 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002623 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002624 return NULL;
2625
2626 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 * Remove the window from its frame.
2628 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002629 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 wp = frame2win(frp2);
2631
2632 /* Remove this frame from the list of frames. */
2633 frame_remove(frp_close);
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (frp_close->fr_parent->fr_layout == FR_COL)
2636 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002637 /* When 'winfixheight' is set, try to find another frame in the column
2638 * (as close to the closed frame as possible) to distribute the height
2639 * to. */
2640 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2641 {
2642 frp = frp_close->fr_prev;
2643 frp3 = frp_close->fr_next;
2644 while (frp != NULL || frp3 != NULL)
2645 {
2646 if (frp != NULL)
2647 {
2648 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfh)
2649 {
2650 frp2 = frp;
2651 wp = frp->fr_win;
2652 break;
2653 }
2654 frp = frp->fr_prev;
2655 }
2656 if (frp3 != NULL)
2657 {
2658 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2659 {
2660 frp2 = frp3;
2661 wp = frp3->fr_win;
2662 break;
2663 }
2664 frp3 = frp3->fr_next;
2665 }
2666 }
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2669 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 *dirp = 'v';
2671 }
2672 else
2673 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002674 /* When 'winfixwidth' is set, try to find another frame in the column
2675 * (as close to the closed frame as possible) to distribute the width
2676 * to. */
2677 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2678 {
2679 frp = frp_close->fr_prev;
2680 frp3 = frp_close->fr_next;
2681 while (frp != NULL || frp3 != NULL)
2682 {
2683 if (frp != NULL)
2684 {
2685 if (frp->fr_win != NULL && !frp->fr_win->w_p_wfw)
2686 {
2687 frp2 = frp;
2688 wp = frp->fr_win;
2689 break;
2690 }
2691 frp = frp->fr_prev;
2692 }
2693 if (frp3 != NULL)
2694 {
2695 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2696 {
2697 frp2 = frp3;
2698 wp = frp3->fr_win;
2699 break;
2700 }
2701 frp3 = frp3->fr_next;
2702 }
2703 }
2704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002706 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 *dirp = 'h';
2708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709
2710 /* If rows/columns go to a window below/right its positions need to be
2711 * updated. Can only be done after the sizes have been updated. */
2712 if (frp2 == frp_close->fr_next)
2713 {
2714 int row = win->w_winrow;
2715 int col = W_WINCOL(win);
2716
2717 frame_comp_pos(frp2, &row, &col);
2718 }
2719
2720 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2721 {
2722 /* There is no other frame in this list, move its info to the parent
2723 * and remove it. */
2724 frp2->fr_parent->fr_layout = frp2->fr_layout;
2725 frp2->fr_parent->fr_child = frp2->fr_child;
2726 for (frp = frp2->fr_child; frp != NULL; frp = frp->fr_next)
2727 frp->fr_parent = frp2->fr_parent;
2728 frp2->fr_parent->fr_win = frp2->fr_win;
2729 if (frp2->fr_win != NULL)
2730 frp2->fr_win->w_frame = frp2->fr_parent;
2731 frp = frp2->fr_parent;
2732 vim_free(frp2);
2733
2734 frp2 = frp->fr_parent;
2735 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2736 {
2737 /* The frame above the parent has the same layout, have to merge
2738 * the frames into this list. */
2739 if (frp2->fr_child == frp)
2740 frp2->fr_child = frp->fr_child;
2741 frp->fr_child->fr_prev = frp->fr_prev;
2742 if (frp->fr_prev != NULL)
2743 frp->fr_prev->fr_next = frp->fr_child;
2744 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2745 {
2746 frp3->fr_parent = frp2;
2747 if (frp3->fr_next == NULL)
2748 {
2749 frp3->fr_next = frp->fr_next;
2750 if (frp->fr_next != NULL)
2751 frp->fr_next->fr_prev = frp3;
2752 break;
2753 }
2754 }
2755 vim_free(frp);
2756 }
2757 }
2758
2759 return wp;
2760}
2761
2762/*
2763 * Find out which frame is going to get the freed up space when "win" is
2764 * closed.
2765 * if 'splitbelow'/'splitleft' the space goes to the window above/left.
2766 * if 'nosplitbelow'/'nosplitleft' the space goes to the window below/right.
2767 * This makes opening a window and closing it immediately keep the same window
2768 * layout.
2769 */
2770 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002771win_altframe(
2772 win_T *win,
2773 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 frame_T *frp;
2776 int b;
2777
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002778 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002779 /* Last window in this tab page, will go to next tab page. */
2780 return alt_tabpage()->tp_curwin->w_frame;
2781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 frp = win->w_frame;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002783 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 b = p_spr;
2785 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 b = p_sb;
2787 if ((!b && frp->fr_next != NULL) || frp->fr_prev == NULL)
2788 return frp->fr_next;
2789 return frp->fr_prev;
2790}
2791
2792/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002793 * Return the tabpage that will be used if the current one is closed.
2794 */
2795 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002796alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002797{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002798 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002799
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002800 /* Use the next tab page if possible. */
2801 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002802 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002803
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002804 /* Find the last but one tab page. */
2805 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2806 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002807 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002808}
2809
2810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 * Find the left-upper window in frame "frp".
2812 */
2813 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002814frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 while (frp->fr_win == NULL)
2817 frp = frp->fr_child;
2818 return frp->fr_win;
2819}
2820
2821/*
2822 * Return TRUE if frame "frp" contains window "wp".
2823 */
2824 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002825frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
2827 frame_T *p;
2828
2829 if (frp->fr_layout == FR_LEAF)
2830 return frp->fr_win == wp;
2831
2832 for (p = frp->fr_child; p != NULL; p = p->fr_next)
2833 if (frame_has_win(p, wp))
2834 return TRUE;
2835 return FALSE;
2836}
2837
2838/*
2839 * Set a new height for a frame. Recursively sets the height for contained
2840 * frames and windows. Caller must take care of positions.
2841 */
2842 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002843frame_new_height(
2844 frame_T *topfrp,
2845 int height,
2846 int topfirst, /* resize topmost contained frame first */
2847 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 may cause the height not to be set */
2849{
2850 frame_T *frp;
2851 int extra_lines;
2852 int h;
2853
2854 if (topfrp->fr_win != NULL)
2855 {
2856 /* Simple case: just one window. */
2857 win_new_height(topfrp->fr_win,
2858 height - topfrp->fr_win->w_status_height);
2859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 else if (topfrp->fr_layout == FR_ROW)
2861 {
2862 do
2863 {
2864 /* All frames in this row get the same new height. */
2865 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
2866 {
2867 frame_new_height(frp, height, topfirst, wfh);
2868 if (frp->fr_height > height)
2869 {
2870 /* Could not fit the windows, make the whole row higher. */
2871 height = frp->fr_height;
2872 break;
2873 }
2874 }
2875 }
2876 while (frp != NULL);
2877 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002878 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
2880 /* Complicated case: Resize a column of frames. Resize the bottom
2881 * frame first, frames above that when needed. */
2882
2883 frp = topfrp->fr_child;
2884 if (wfh)
2885 /* Advance past frames with one window with 'wfh' set. */
2886 while (frame_fixed_height(frp))
2887 {
2888 frp = frp->fr_next;
2889 if (frp == NULL)
2890 return; /* no frame without 'wfh', give up */
2891 }
2892 if (!topfirst)
2893 {
2894 /* Find the bottom frame of this column */
2895 while (frp->fr_next != NULL)
2896 frp = frp->fr_next;
2897 if (wfh)
2898 /* Advance back for frames with one window with 'wfh' set. */
2899 while (frame_fixed_height(frp))
2900 frp = frp->fr_prev;
2901 }
2902
2903 extra_lines = height - topfrp->fr_height;
2904 if (extra_lines < 0)
2905 {
2906 /* reduce height of contained frames, bottom or top frame first */
2907 while (frp != NULL)
2908 {
2909 h = frame_minheight(frp, NULL);
2910 if (frp->fr_height + extra_lines < h)
2911 {
2912 extra_lines += frp->fr_height - h;
2913 frame_new_height(frp, h, topfirst, wfh);
2914 }
2915 else
2916 {
2917 frame_new_height(frp, frp->fr_height + extra_lines,
2918 topfirst, wfh);
2919 break;
2920 }
2921 if (topfirst)
2922 {
2923 do
2924 frp = frp->fr_next;
2925 while (wfh && frp != NULL && frame_fixed_height(frp));
2926 }
2927 else
2928 {
2929 do
2930 frp = frp->fr_prev;
2931 while (wfh && frp != NULL && frame_fixed_height(frp));
2932 }
2933 /* Increase "height" if we could not reduce enough frames. */
2934 if (frp == NULL)
2935 height -= extra_lines;
2936 }
2937 }
2938 else if (extra_lines > 0)
2939 {
2940 /* increase height of bottom or top frame */
2941 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
2942 }
2943 }
2944 topfrp->fr_height = height;
2945}
2946
2947/*
2948 * Return TRUE if height of frame "frp" should not be changed because of
2949 * the 'winfixheight' option.
2950 */
2951 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002952frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953{
2954 /* frame with one window: fixed height if 'winfixheight' set. */
2955 if (frp->fr_win != NULL)
2956 return frp->fr_win->w_p_wfh;
2957
2958 if (frp->fr_layout == FR_ROW)
2959 {
2960 /* The frame is fixed height if one of the frames in the row is fixed
2961 * height. */
2962 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2963 if (frame_fixed_height(frp))
2964 return TRUE;
2965 return FALSE;
2966 }
2967
2968 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
2969 * frames in the row are fixed height. */
2970 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2971 if (!frame_fixed_height(frp))
2972 return FALSE;
2973 return TRUE;
2974}
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002977 * Return TRUE if width of frame "frp" should not be changed because of
2978 * the 'winfixwidth' option.
2979 */
2980 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002981frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002982{
2983 /* frame with one window: fixed width if 'winfixwidth' set. */
2984 if (frp->fr_win != NULL)
2985 return frp->fr_win->w_p_wfw;
2986
2987 if (frp->fr_layout == FR_COL)
2988 {
2989 /* The frame is fixed width if one of the frames in the row is fixed
2990 * width. */
2991 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
2992 if (frame_fixed_width(frp))
2993 return TRUE;
2994 return FALSE;
2995 }
2996
2997 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
2998 * frames in the row are fixed width. */
2999 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3000 if (!frame_fixed_width(frp))
3001 return FALSE;
3002 return TRUE;
3003}
3004
3005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 * Add a status line to windows at the bottom of "frp".
3007 * Note: Does not check if there is room!
3008 */
3009 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003010frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 win_T *wp;
3013
3014 if (frp->fr_layout == FR_LEAF)
3015 {
3016 wp = frp->fr_win;
3017 if (wp->w_status_height == 0)
3018 {
3019 if (wp->w_height > 0) /* don't make it negative */
3020 --wp->w_height;
3021 wp->w_status_height = STATUS_HEIGHT;
3022 }
3023 }
3024 else if (frp->fr_layout == FR_ROW)
3025 {
3026 /* Handle all the frames in the row. */
3027 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3028 frame_add_statusline(frp);
3029 }
3030 else /* frp->fr_layout == FR_COL */
3031 {
3032 /* Only need to handle the last frame in the column. */
3033 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3034 ;
3035 frame_add_statusline(frp);
3036 }
3037}
3038
3039/*
3040 * Set width of a frame. Handles recursively going through contained frames.
3041 * May remove separator line for windows at the right side (for win_close()).
3042 */
3043 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003044frame_new_width(
3045 frame_T *topfrp,
3046 int width,
3047 int leftfirst, /* resize leftmost contained frame first */
3048 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003049 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050{
3051 frame_T *frp;
3052 int extra_cols;
3053 int w;
3054 win_T *wp;
3055
3056 if (topfrp->fr_layout == FR_LEAF)
3057 {
3058 /* Simple case: just one window. */
3059 wp = topfrp->fr_win;
3060 /* Find out if there are any windows right of this one. */
3061 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3062 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3063 break;
3064 if (frp->fr_parent == NULL)
3065 wp->w_vsep_width = 0;
3066 win_new_width(wp, width - wp->w_vsep_width);
3067 }
3068 else if (topfrp->fr_layout == FR_COL)
3069 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003070 do
3071 {
3072 /* All frames in this column get the same new width. */
3073 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3074 {
3075 frame_new_width(frp, width, leftfirst, wfw);
3076 if (frp->fr_width > width)
3077 {
3078 /* Could not fit the windows, make whole column wider. */
3079 width = frp->fr_width;
3080 break;
3081 }
3082 }
3083 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 }
3085 else /* fr_layout == FR_ROW */
3086 {
3087 /* Complicated case: Resize a row of frames. Resize the rightmost
3088 * frame first, frames left of it when needed. */
3089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003091 if (wfw)
3092 /* Advance past frames with one window with 'wfw' set. */
3093 while (frame_fixed_width(frp))
3094 {
3095 frp = frp->fr_next;
3096 if (frp == NULL)
3097 return; /* no frame without 'wfw', give up */
3098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003100 {
3101 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 while (frp->fr_next != NULL)
3103 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003104 if (wfw)
3105 /* Advance back for frames with one window with 'wfw' set. */
3106 while (frame_fixed_width(frp))
3107 frp = frp->fr_prev;
3108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110 extra_cols = width - topfrp->fr_width;
3111 if (extra_cols < 0)
3112 {
3113 /* reduce frame width, rightmost frame first */
3114 while (frp != NULL)
3115 {
3116 w = frame_minwidth(frp, NULL);
3117 if (frp->fr_width + extra_cols < w)
3118 {
3119 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003120 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
3122 else
3123 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003124 frame_new_width(frp, frp->fr_width + extra_cols,
3125 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 break;
3127 }
3128 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003129 {
3130 do
3131 frp = frp->fr_next;
3132 while (wfw && frp != NULL && frame_fixed_width(frp));
3133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003135 {
3136 do
3137 frp = frp->fr_prev;
3138 while (wfw && frp != NULL && frame_fixed_width(frp));
3139 }
3140 /* Increase "width" if we could not reduce enough frames. */
3141 if (frp == NULL)
3142 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 }
3144 }
3145 else if (extra_cols > 0)
3146 {
3147 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003148 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 }
3150 }
3151 topfrp->fr_width = width;
3152}
3153
3154/*
3155 * Add the vertical separator to windows at the right side of "frp".
3156 * Note: Does not check if there is room!
3157 */
3158 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003159frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160{
3161 win_T *wp;
3162
3163 if (frp->fr_layout == FR_LEAF)
3164 {
3165 wp = frp->fr_win;
3166 if (wp->w_vsep_width == 0)
3167 {
3168 if (wp->w_width > 0) /* don't make it negative */
3169 --wp->w_width;
3170 wp->w_vsep_width = 1;
3171 }
3172 }
3173 else if (frp->fr_layout == FR_COL)
3174 {
3175 /* Handle all the frames in the column. */
3176 for (frp = frp->fr_child; frp != NULL; frp = frp->fr_next)
3177 frame_add_vsep(frp);
3178 }
3179 else /* frp->fr_layout == FR_ROW */
3180 {
3181 /* Only need to handle the last frame in the row. */
3182 frp = frp->fr_child;
3183 while (frp->fr_next != NULL)
3184 frp = frp->fr_next;
3185 frame_add_vsep(frp);
3186 }
3187}
3188
3189/*
3190 * Set frame width from the window it contains.
3191 */
3192 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003193frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194{
3195 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198/*
3199 * Set frame height from the window it contains.
3200 */
3201 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003202frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
3204 wp->w_frame->fr_height = wp->w_height + wp->w_status_height;
3205}
3206
3207/*
3208 * Compute the minimal height for frame "topfrp".
3209 * Uses the 'winminheight' option.
3210 * When "next_curwin" isn't NULL, use p_wh for this window.
3211 * When "next_curwin" is NOWIN, don't use at least one line for the current
3212 * window.
3213 */
3214 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003215frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216{
3217 frame_T *frp;
3218 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220
3221 if (topfrp->fr_win != NULL)
3222 {
3223 if (topfrp->fr_win == next_curwin)
3224 m = p_wh + topfrp->fr_win->w_status_height;
3225 else
3226 {
3227 /* window: minimal height of the window plus status line */
3228 m = p_wmh + topfrp->fr_win->w_status_height;
3229 /* Current window is minimal one line high */
3230 if (p_wmh == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3231 ++m;
3232 }
3233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 else if (topfrp->fr_layout == FR_ROW)
3235 {
3236 /* get the minimal height from each frame in this row */
3237 m = 0;
3238 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3239 {
3240 n = frame_minheight(frp, next_curwin);
3241 if (n > m)
3242 m = n;
3243 }
3244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 else
3246 {
3247 /* Add up the minimal heights for all frames in this column. */
3248 m = 0;
3249 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3250 m += frame_minheight(frp, next_curwin);
3251 }
3252
3253 return m;
3254}
3255
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256/*
3257 * Compute the minimal width for frame "topfrp".
3258 * When "next_curwin" isn't NULL, use p_wiw for this window.
3259 * When "next_curwin" is NOWIN, don't use at least one column for the current
3260 * window.
3261 */
3262 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003263frame_minwidth(
3264 frame_T *topfrp,
3265 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266{
3267 frame_T *frp;
3268 int m, n;
3269
3270 if (topfrp->fr_win != NULL)
3271 {
3272 if (topfrp->fr_win == next_curwin)
3273 m = p_wiw + topfrp->fr_win->w_vsep_width;
3274 else
3275 {
3276 /* window: minimal width of the window plus separator column */
3277 m = p_wmw + topfrp->fr_win->w_vsep_width;
3278 /* Current window is minimal one column wide */
3279 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3280 ++m;
3281 }
3282 }
3283 else if (topfrp->fr_layout == FR_COL)
3284 {
3285 /* get the minimal width from each frame in this column */
3286 m = 0;
3287 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3288 {
3289 n = frame_minwidth(frp, next_curwin);
3290 if (n > m)
3291 m = n;
3292 }
3293 }
3294 else
3295 {
3296 /* Add up the minimal widths for all frames in this row. */
3297 m = 0;
3298 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
3299 m += frame_minwidth(frp, next_curwin);
3300 }
3301
3302 return m;
3303}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304
3305
3306/*
3307 * Try to close all windows except current one.
3308 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3309 * used and the buffer was modified.
3310 *
3311 * Used by ":bdel" and ":only".
3312 */
3313 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003314close_others(
3315 int message,
3316 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317{
3318 win_T *wp;
3319 win_T *nextwp;
3320 int r;
3321
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003322 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
3324 if (message
3325#ifdef FEAT_AUTOCMD
3326 && !autocmd_busy
3327#endif
3328 )
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003329 MSG(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 return;
3331 }
3332
3333 /* Be very careful here: autocommands may change the window layout. */
3334 for (wp = firstwin; win_valid(wp); wp = nextwp)
3335 {
3336 nextwp = wp->w_next;
3337 if (wp != curwin) /* don't close current window */
3338 {
3339
3340 /* Check if it's allowed to abandon this window */
3341 r = can_abandon(wp->w_buffer, forceit);
3342#ifdef FEAT_AUTOCMD
3343 if (!win_valid(wp)) /* autocommands messed wp up */
3344 {
3345 nextwp = firstwin;
3346 continue;
3347 }
3348#endif
3349 if (!r)
3350 {
3351#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3352 if (message && (p_confirm || cmdmod.confirm) && p_write)
3353 {
3354 dialog_changed(wp->w_buffer, FALSE);
3355# ifdef FEAT_AUTOCMD
3356 if (!win_valid(wp)) /* autocommands messed wp up */
3357 {
3358 nextwp = firstwin;
3359 continue;
3360 }
3361# endif
3362 }
3363 if (bufIsChanged(wp->w_buffer))
3364#endif
3365 continue;
3366 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003367 win_close(wp, !buf_hide(wp->w_buffer)
3368 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 }
3370 }
3371
Bram Moolenaar459ca562016-11-10 18:16:33 +01003372 if (message && !ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 EMSG(_("E445: Other window contains changes"));
3374}
3375
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003377 * Init the current window "curwin".
3378 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 */
3380 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003381curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003383 win_init_empty(curwin);
3384}
3385
3386 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003387win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003388{
3389 redraw_win_later(wp, NOT_VALID);
3390 wp->w_lines_valid = 0;
3391 wp->w_cursor.lnum = 1;
3392 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393#ifdef FEAT_VIRTUALEDIT
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003394 wp->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003396 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3397 wp->w_pcmark.col = 0;
3398 wp->w_prev_pcmark.lnum = 0;
3399 wp->w_prev_pcmark.col = 0;
3400 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003402 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003404 wp->w_botline = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#ifdef FEAT_FKMAP
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003406 if (wp->w_p_rl)
3407 wp->w_farsi = W_CONV + W_R_L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 else
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003409 wp->w_farsi = W_CONV;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#endif
Bram Moolenaara971b822011-09-14 14:43:25 +02003411#ifdef FEAT_SYN_HL
3412 wp->w_s = &wp->w_buffer->b_s;
3413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414}
3415
3416/*
3417 * Allocate the first window and put an empty buffer in it.
3418 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003419 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003421 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003422win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003424 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003425 return FAIL;
3426
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003427 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003428 if (first_tabpage == NULL)
3429 return FAIL;
3430 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003431 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003432
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003433 return OK;
3434}
3435
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003436#if defined(FEAT_AUTOCMD) || defined(PROTO)
3437/*
3438 * Init "aucmd_win". This can only be done after the first
3439 * window is fully initialized, thus it can't be in win_alloc_first().
3440 */
3441 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003442win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003443{
3444 aucmd_win = win_alloc(NULL, TRUE);
3445 if (aucmd_win != NULL)
3446 {
3447 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003448 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003449 new_frame(aucmd_win);
3450 }
3451}
3452#endif
3453
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003454/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003455 * Allocate the first window or the first window in a new tab page.
3456 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003457 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003458 * Return FAIL when something goes wrong (out of memory).
3459 */
3460 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003461win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003462{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003463 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003464 if (oldwin == NULL)
3465 {
3466 /* Very first window, need to create an empty buffer for it and
3467 * initialize from scratch. */
3468 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3469 if (curwin == NULL || curbuf == NULL)
3470 return FAIL;
3471 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003472#ifdef FEAT_SYN_HL
3473 curwin->w_s = &(curbuf->b_s);
3474#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003475 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003476 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003477 curwin_init(); /* init current window */
3478 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003479 else
3480 {
3481 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003482 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003483
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003484 /* We don't want cursor- and scroll-binding in the first window. */
3485 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003488 new_frame(curwin);
3489 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003490 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003491 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 topframe->fr_height = Rows - p_ch;
3494 topframe->fr_win = curwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003495
3496 return OK;
3497}
3498
3499/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003500 * Create a frame for window "wp".
3501 */
3502 static void
3503new_frame(win_T *wp)
3504{
3505 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3506
3507 wp->w_frame = frp;
3508 if (frp != NULL)
3509 {
3510 frp->fr_layout = FR_LEAF;
3511 frp->fr_win = wp;
3512 }
3513}
3514
3515/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003516 * Initialize the window and frame size to the maximum.
3517 */
3518 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003519win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003520{
3521 firstwin->w_height = ROWS_AVAIL;
3522 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003523 firstwin->w_width = Columns;
3524 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525}
3526
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003527/*
3528 * Allocate a new tabpage_T and init the values.
3529 * Returns NULL when out of memory.
3530 */
3531 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003532alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003533{
3534 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003535# ifdef FEAT_GUI
3536 int i;
3537# endif
3538
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003539
3540 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003541 if (tp == NULL)
3542 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003543
Bram Moolenaar429fa852013-04-15 12:27:36 +02003544# ifdef FEAT_EVAL
3545 /* init t: variables */
3546 tp->tp_vars = dict_alloc();
3547 if (tp->tp_vars == NULL)
3548 {
3549 vim_free(tp);
3550 return NULL;
3551 }
3552 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3553# endif
3554
3555# ifdef FEAT_GUI
3556 for (i = 0; i < 3; i++)
3557 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003558# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003559# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003560 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003561# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003562 tp->tp_ch_used = p_ch;
3563
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003564 return tp;
3565}
3566
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003567 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003568free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003569{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003570 int idx;
3571
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003572# ifdef FEAT_DIFF
3573 diff_clear(tp);
3574# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003575 for (idx = 0; idx < SNAP_COUNT; ++idx)
3576 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003577#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003578 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3579 hash_init(&tp->tp_vars->dv_hashtab);
3580 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003581#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003582
3583#ifdef FEAT_PYTHON
3584 python_tabpage_free(tp);
3585#endif
3586
3587#ifdef FEAT_PYTHON3
3588 python3_tabpage_free(tp);
3589#endif
3590
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003591 vim_free(tp);
3592}
3593
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003594/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003595 * Create a new Tab page with one window.
3596 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003597 * When "after" is 0 put it just after the current Tab page.
3598 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003599 * Return FAIL or OK.
3600 */
3601 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003602win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003603{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003604 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003605 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003606 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003607
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003608 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003609 if (newtp == NULL)
3610 return FAIL;
3611
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003612 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003613 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003614 {
3615 vim_free(newtp);
3616 return FAIL;
3617 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003618 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003619
3620 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003621 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003622 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003623 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003624 if (after == 1)
3625 {
3626 /* New tab page becomes the first one. */
3627 newtp->tp_next = first_tabpage;
3628 first_tabpage = newtp;
3629 }
3630 else
3631 {
3632 if (after > 0)
3633 {
3634 /* Put new tab page before tab page "after". */
3635 n = 2;
3636 for (tp = first_tabpage; tp->tp_next != NULL
3637 && n < after; tp = tp->tp_next)
3638 ++n;
3639 }
3640 newtp->tp_next = tp->tp_next;
3641 tp->tp_next = newtp;
3642 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003644 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003645 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003646
3647 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003648 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003649
3650#if defined(FEAT_GUI)
3651 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3652 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003653 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003654#endif
3655
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003656 redraw_all_later(CLEAR);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003657#ifdef FEAT_AUTOCMD
Bram Moolenaarc917da42016-07-19 22:31:36 +02003658 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003659 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003660 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003661 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003662#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003663 return OK;
3664 }
3665
3666 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003667 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003668 return FAIL;
3669}
3670
3671/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003672 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3673 * like with ":split".
3674 * Returns OK if a new tab page was created, FAIL otherwise.
3675 */
3676 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003677may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003678{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003679 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003680
Bram Moolenaard326ce82007-03-11 14:48:29 +00003681 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003682 {
3683 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003684 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003685 return win_new_tabpage(n);
3686 }
3687 return FAIL;
3688}
3689
3690/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003691 * Create up to "maxcount" tabpages with empty windows.
3692 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003693 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003694 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003695make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003696{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003697 int count = maxcount;
3698 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003699
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003700 /* Limit to 'tabpagemax' tabs. */
3701 if (count > p_tpm)
3702 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003703
3704#ifdef FEAT_AUTOCMD
3705 /*
3706 * Don't execute autocommands while creating the tab pages. Must do that
3707 * when putting the buffers in the windows.
3708 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003709 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003710#endif
3711
3712 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003713 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003714 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003715
3716#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003717 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003718#endif
3719
3720 /* return actual number of tab pages */
3721 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003722}
3723
3724/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003725 * Return TRUE when "tpc" points to a valid tab page.
3726 */
3727 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003728valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003729{
3730 tabpage_T *tp;
3731
Bram Moolenaar29323592016-07-24 22:04:11 +02003732 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003733 if (tp == tpc)
3734 return TRUE;
3735 return FALSE;
3736}
3737
3738/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003739 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3740 * valid.
3741 */
3742 int
3743valid_tabpage_win(tabpage_T *tpc)
3744{
3745 tabpage_T *tp;
3746 win_T *wp;
3747
3748 FOR_ALL_TABPAGES(tp)
3749 {
3750 if (tp == tpc)
3751 {
3752 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3753 {
3754 if (win_valid_any_tab(wp))
3755 return TRUE;
3756 }
3757 return FALSE;
3758 }
3759 }
3760 /* shouldn't happen */
3761 return FALSE;
3762}
3763
3764/*
3765 * Close tabpage "tab", assuming it has no windows in it.
3766 * There must be another tabpage or this will crash.
3767 */
3768 void
3769close_tabpage(tabpage_T *tab)
3770{
3771 tabpage_T *ptp;
3772
3773 if (tab == first_tabpage)
3774 {
3775 first_tabpage = tab->tp_next;
3776 ptp = first_tabpage;
3777 }
3778 else
3779 {
3780 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3781 ptp = ptp->tp_next)
3782 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003783 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003784 ptp->tp_next = tab->tp_next;
3785 }
3786
3787 goto_tabpage_tp(ptp, FALSE, FALSE);
3788 free_tabpage(tab);
3789}
3790
3791/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003792 * Find tab page "n" (first one is 1). Returns NULL when not found.
3793 */
3794 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003795find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003796{
3797 tabpage_T *tp;
3798 int i = 1;
3799
3800 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3801 ++i;
3802 return tp;
3803}
3804
3805/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003806 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003807 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003808 */
3809 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003810tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003811{
3812 int i = 1;
3813 tabpage_T *tp;
3814
3815 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3816 ++i;
3817 return i;
3818}
3819
3820/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003821 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003822 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003823 * FAIL.
3824 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003825 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003826 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003827leave_tabpage(
3828 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003829 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003830 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003832 tabpage_T *tp = curtab;
3833
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003834 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003835#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003836 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003837 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003838 if (new_curbuf != curbuf)
3839 {
3840 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3841 if (curtab != tp)
3842 return FAIL;
3843 }
3844 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3845 if (curtab != tp)
3846 return FAIL;
3847 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003848 if (curtab != tp)
3849 return FAIL;
3850 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003851#endif
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003852#if defined(FEAT_GUI)
3853 /* Remove the scrollbars. They may be added back later. */
3854 if (gui.in_use)
3855 gui_remove_scrollbars();
3856#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003857 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003858 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003859 tp->tp_firstwin = firstwin;
3860 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003861 tp->tp_old_Rows = Rows;
3862 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003863 firstwin = NULL;
3864 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003865 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003866}
3867
3868/*
3869 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003870 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003871 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3872 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003873 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003874 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003875enter_tabpage(
3876 tabpage_T *tp,
3877 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003878 int trigger_enter_autocmds,
3879 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003880{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003881 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003882 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003883
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003884 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003885 firstwin = tp->tp_firstwin;
3886 lastwin = tp->tp_lastwin;
3887 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003888
3889 /* We would like doing the TabEnter event first, but we don't have a
3890 * valid current window yet, which may break some commands.
3891 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003892 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003893 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003894 prevwin = next_prevwin;
3895
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003896 last_status(FALSE); /* status line may appear or disappear */
3897 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003898 must_redraw = CLEAR; /* need to redraw everything */
3899#ifdef FEAT_DIFF
3900 diff_need_scrollbind = TRUE;
3901#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003902
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003903 /* The tabpage line may have appeared or disappeared, may need to resize
3904 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003905 * frame sizes too. Use the stored value of p_ch, so that it can be
3906 * different for each tab page. */
3907 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003908 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3909#ifdef FEAT_GUI_TABLINE
3910 && !gui_use_tabline()
3911#endif
3912 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003913 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003914 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003915 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003916
3917#if defined(FEAT_GUI)
3918 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3919 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003920 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003921#endif
3922
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003923#ifdef FEAT_AUTOCMD
3924 /* Apply autocommands after updating the display, when 'rows' and
3925 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003926 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003927 {
3928 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3929 if (old_curbuf != curbuf)
3930 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3931 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003932#endif
3933
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003934 redraw_all_later(CLEAR);
3935}
3936
3937/*
3938 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003939 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003940 */
3941 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003942goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003943{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003944 tabpage_T *tp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003945 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003946 int i;
3947
Bram Moolenaard68071d2006-05-02 22:08:30 +00003948 if (text_locked())
3949 {
3950 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02003951 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00003952 return;
3953 }
3954
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003955 /* If there is only one it can't work. */
3956 if (first_tabpage->tp_next == NULL)
3957 {
3958 if (n > 1)
3959 beep_flush();
3960 return;
3961 }
3962
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003963 if (n == 0)
3964 {
3965 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003966 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003967 tp = first_tabpage;
3968 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003969 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003970 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003971 else if (n < 0)
3972 {
3973 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
3974 * this N times. */
3975 ttp = curtab;
3976 for (i = n; i < 0; ++i)
3977 {
3978 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
3979 tp = tp->tp_next)
3980 ;
3981 ttp = tp;
3982 }
3983 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003984 else if (n == 9999)
3985 {
3986 /* Go to last tab page. */
3987 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
3988 ;
3989 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003990 else
3991 {
3992 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003993 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00003994 if (tp == NULL)
3995 {
3996 beep_flush();
3997 return;
3998 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003999 }
4000
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004001 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004002
4003#ifdef FEAT_GUI_TABLINE
4004 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004005 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004006#endif
4007}
4008
4009/*
4010 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004011 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4012 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004013 * Note: doesn't update the GUI tab.
4014 */
4015 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004016goto_tabpage_tp(
4017 tabpage_T *tp,
4018 int trigger_enter_autocmds,
4019 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004020{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004021 /* Don't repeat a message in another tab page. */
4022 set_keep_msg(NULL, 0);
4023
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004024 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4025 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004026 {
4027 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004028 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4029 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004030 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004031 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4032 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004033 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
4036/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004037 * Enter window "wp" in tab page "tp".
4038 * Also updates the GUI tab.
4039 */
4040 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004041goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004042{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004043 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004044 if (curtab == tp && win_valid(wp))
4045 {
4046 win_enter(wp, TRUE);
4047# ifdef FEAT_GUI_TABLINE
4048 if (gui_use_tabline())
4049 gui_mch_set_curtab(tabpage_index(curtab));
4050# endif
4051 }
4052}
4053
4054/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004055 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004056 */
4057 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004058tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004059{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004060 int n = 1;
4061 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004062
4063 if (first_tabpage->tp_next == NULL)
4064 return;
4065
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004066 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4067 ++n;
4068
4069 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4070 && tp->tp_next == curtab))
4071 return;
4072
4073 tp_dst = tp;
4074
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004075 /* Remove the current tab page from the list of tab pages. */
4076 if (curtab == first_tabpage)
4077 first_tabpage = curtab->tp_next;
4078 else
4079 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004080 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004081 if (tp->tp_next == curtab)
4082 break;
4083 if (tp == NULL) /* "cannot happen" */
4084 return;
4085 tp->tp_next = curtab->tp_next;
4086 }
4087
4088 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004089 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004090 {
4091 curtab->tp_next = first_tabpage;
4092 first_tabpage = curtab;
4093 }
4094 else
4095 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004096 curtab->tp_next = tp_dst->tp_next;
4097 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004098 }
4099
4100 /* Need to redraw the tabline. Tab page contents doesn't change. */
4101 redraw_tabline = TRUE;
4102}
4103
4104
4105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 * Go to another window.
4107 * When jumping to another buffer, stop Visual mode. Do this before
4108 * changing windows so we can yank the selection into the '*' register.
4109 * When jumping to another window on the same buffer, adjust its cursor
4110 * position to keep the same Visual area.
4111 */
4112 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004113win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004115#ifdef FEAT_CONCEAL
4116 win_T *owp = curwin;
4117#endif
4118
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004119 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 {
4121 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004122 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return;
4124 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004125#ifdef FEAT_AUTOCMD
4126 if (curbuf_locked())
4127 return;
4128#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004129
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 if (wp->w_buffer != curbuf)
4131 reset_VIsual_and_resel();
4132 else if (VIsual_active)
4133 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134
4135#ifdef FEAT_GUI
4136 need_mouse_correct = TRUE;
4137#endif
4138 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004139
4140#ifdef FEAT_CONCEAL
4141 /* Conceal cursor line in previous window, unconceal in current window. */
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004142 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004143 update_single_line(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004144 if (curwin->w_p_cole > 0 && !msg_scrolled)
4145 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147}
4148
4149#if defined(FEAT_PERL) || defined(PROTO)
4150/*
4151 * Find window number "winnr" (counting top to bottom).
4152 */
4153 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004154win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155{
4156 win_T *wp;
4157
Bram Moolenaar29323592016-07-24 22:04:11 +02004158 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if (--winnr == 0)
4160 break;
4161 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162}
4163#endif
4164
Bram Moolenaar4033c552017-09-16 20:54:51 +02004165#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004166/*
4167 * Find the tabpage for window "win".
4168 */
4169 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004170win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004171{
4172 win_T *wp;
4173 tabpage_T *tp;
4174
Bram Moolenaar29323592016-07-24 22:04:11 +02004175 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004176 if (wp == win)
4177 return tp;
4178 return NULL;
4179}
4180#endif
4181
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182/*
4183 * Move to window above or below "count" times.
4184 */
4185 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004186win_goto_ver(
4187 int up, /* TRUE to go to win above */
4188 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
4190 frame_T *fr;
4191 frame_T *nfr;
4192 frame_T *foundfr;
4193
4194 foundfr = curwin->w_frame;
4195 while (count--)
4196 {
4197 /*
4198 * First go upwards in the tree of frames until we find a upwards or
4199 * downwards neighbor.
4200 */
4201 fr = foundfr;
4202 for (;;)
4203 {
4204 if (fr == topframe)
4205 goto end;
4206 if (up)
4207 nfr = fr->fr_prev;
4208 else
4209 nfr = fr->fr_next;
4210 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4211 break;
4212 fr = fr->fr_parent;
4213 }
4214
4215 /*
4216 * Now go downwards to find the bottom or top frame in it.
4217 */
4218 for (;;)
4219 {
4220 if (nfr->fr_layout == FR_LEAF)
4221 {
4222 foundfr = nfr;
4223 break;
4224 }
4225 fr = nfr->fr_child;
4226 if (nfr->fr_layout == FR_ROW)
4227 {
4228 /* Find the frame at the cursor row. */
4229 while (fr->fr_next != NULL
4230 && frame2win(fr)->w_wincol + fr->fr_width
4231 <= curwin->w_wincol + curwin->w_wcol)
4232 fr = fr->fr_next;
4233 }
4234 if (nfr->fr_layout == FR_COL && up)
4235 while (fr->fr_next != NULL)
4236 fr = fr->fr_next;
4237 nfr = fr;
4238 }
4239 }
4240end:
4241 if (foundfr != NULL)
4242 win_goto(foundfr->fr_win);
4243}
4244
4245/*
4246 * Move to left or right window.
4247 */
4248 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004249win_goto_hor(
4250 int left, /* TRUE to go to left win */
4251 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252{
4253 frame_T *fr;
4254 frame_T *nfr;
4255 frame_T *foundfr;
4256
4257 foundfr = curwin->w_frame;
4258 while (count--)
4259 {
4260 /*
4261 * First go upwards in the tree of frames until we find a left or
4262 * right neighbor.
4263 */
4264 fr = foundfr;
4265 for (;;)
4266 {
4267 if (fr == topframe)
4268 goto end;
4269 if (left)
4270 nfr = fr->fr_prev;
4271 else
4272 nfr = fr->fr_next;
4273 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4274 break;
4275 fr = fr->fr_parent;
4276 }
4277
4278 /*
4279 * Now go downwards to find the leftmost or rightmost frame in it.
4280 */
4281 for (;;)
4282 {
4283 if (nfr->fr_layout == FR_LEAF)
4284 {
4285 foundfr = nfr;
4286 break;
4287 }
4288 fr = nfr->fr_child;
4289 if (nfr->fr_layout == FR_COL)
4290 {
4291 /* Find the frame at the cursor row. */
4292 while (fr->fr_next != NULL
4293 && frame2win(fr)->w_winrow + fr->fr_height
4294 <= curwin->w_winrow + curwin->w_wrow)
4295 fr = fr->fr_next;
4296 }
4297 if (nfr->fr_layout == FR_ROW && left)
4298 while (fr->fr_next != NULL)
4299 fr = fr->fr_next;
4300 nfr = fr;
4301 }
4302 }
4303end:
4304 if (foundfr != NULL)
4305 win_goto(foundfr->fr_win);
4306}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308/*
4309 * Make window "wp" the current window.
4310 */
4311 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004312win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004314 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315}
4316
4317/*
4318 * Make window wp the current window.
4319 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4320 * been closed and isn't valid.
4321 */
4322 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004323win_enter_ext(
4324 win_T *wp,
4325 int undo_sync,
4326 int curwin_invalid,
Bram Moolenaarc917da42016-07-19 22:31:36 +02004327 int trigger_new_autocmds UNUSED,
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004328 int trigger_enter_autocmds UNUSED,
4329 int trigger_leave_autocmds UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
4331#ifdef FEAT_AUTOCMD
4332 int other_buffer = FALSE;
4333#endif
4334
4335 if (wp == curwin && !curwin_invalid) /* nothing to do */
4336 return;
4337
4338#ifdef FEAT_AUTOCMD
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004339 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
4341 /*
4342 * Be careful: If autocommands delete the window, return now.
4343 */
4344 if (wp->w_buffer != curbuf)
4345 {
4346 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4347 other_buffer = TRUE;
4348 if (!win_valid(wp))
4349 return;
4350 }
4351 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4352 if (!win_valid(wp))
4353 return;
4354# ifdef FEAT_EVAL
4355 /* autocmds may abort script processing */
4356 if (aborting())
4357 return;
4358# endif
4359 }
4360#endif
4361
4362 /* sync undo before leaving the current buffer */
4363 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004364 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004365
4366 /* Might need to scroll the old window before switching, e.g., when the
4367 * cursor was moved. */
4368 update_topline();
4369
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 /* may have to copy the buffer options when 'cpo' contains 'S' */
4371 if (wp->w_buffer != curbuf)
4372 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4373 if (!curwin_invalid)
4374 {
4375 prevwin = curwin; /* remember for CTRL-W p */
4376 curwin->w_redr_status = TRUE;
4377 }
4378 curwin = wp;
4379 curbuf = wp->w_buffer;
4380 check_cursor();
4381#ifdef FEAT_VIRTUALEDIT
4382 if (!virtual_active())
4383 curwin->w_cursor.coladd = 0;
4384#endif
4385 changed_line_abv_curs(); /* assume cursor position needs updating */
4386
4387 if (curwin->w_localdir != NULL)
4388 {
4389 /* Window has a local directory: Save current directory as global
4390 * directory (unless that was done already) and change to the local
4391 * directory. */
4392 if (globaldir == NULL)
4393 {
4394 char_u cwd[MAXPATHL];
4395
4396 if (mch_dirname(cwd, MAXPATHL) == OK)
4397 globaldir = vim_strsave(cwd);
4398 }
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004399 if (mch_chdir((char *)curwin->w_localdir) == 0)
4400 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 else if (globaldir != NULL)
4403 {
4404 /* Window doesn't have a local directory and we are not in the global
4405 * directory: Change to the global directory. */
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004406 ignored = mch_chdir((char *)globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 vim_free(globaldir);
4408 globaldir = NULL;
4409 shorten_fnames(TRUE);
4410 }
4411
4412#ifdef FEAT_AUTOCMD
Bram Moolenaarc917da42016-07-19 22:31:36 +02004413 if (trigger_new_autocmds)
4414 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004415 if (trigger_enter_autocmds)
4416 {
4417 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4418 if (other_buffer)
4419 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421#endif
4422
4423#ifdef FEAT_TITLE
4424 maketitle();
4425#endif
4426 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004427 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 if (restart_edit)
4429 redraw_later(VALID); /* causes status line redraw */
4430
4431 /* set window height to desired minimal value */
4432 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4433 win_setheight((int)p_wh);
4434 else if (curwin->w_height == 0)
4435 win_setheight(1);
4436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004438 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441#ifdef FEAT_MOUSE
4442 setmouse(); /* in case jumped to/from help buffer */
4443#endif
4444
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004445 /* Change directories when the 'acd' option is set. */
4446 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447}
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004451 * Jump to the first open window that contains buffer "buf", if one exists.
4452 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 */
4454 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004455buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004457 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004459 if (curwin->w_buffer == buf)
4460 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004461 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004462 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004463 if (wp->w_buffer == buf)
4464 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (wp != NULL)
4466 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004467 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004469
4470/*
4471 * Jump to the first open window in any tab page that contains buffer "buf",
4472 * if one exists.
4473 * Returns a pointer to the window found, otherwise NULL.
4474 */
4475 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004476buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004477{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004478 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004479 tabpage_T *tp;
4480
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004481 if (wp != NULL)
4482 return wp;
4483
Bram Moolenaar29323592016-07-24 22:04:11 +02004484 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004485 if (tp != curtab)
4486 {
4487 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4488 if (wp->w_buffer == buf)
4489 break;
4490 if (wp != NULL)
4491 {
4492 goto_tabpage_win(tp, wp);
4493 if (curwin != wp)
4494 wp = NULL; /* something went wrong */
4495 break;
4496 }
4497 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004498 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004499}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004501static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004504 * Allocate a window structure and link it in the window list when "hidden" is
4505 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004508win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004510 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511
4512 /*
4513 * allocate window structure and linesizes arrays
4514 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004515 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004516 if (new_wp == NULL)
4517 return NULL;
4518
4519 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004521 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004522 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524
Bram Moolenaar86edef62016-03-13 18:07:30 +01004525 new_wp->w_id = ++last_win_id;
4526
Bram Moolenaar429fa852013-04-15 12:27:36 +02004527#ifdef FEAT_EVAL
4528 /* init w: variables */
4529 new_wp->w_vars = dict_alloc();
4530 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004532 win_free_lsize(new_wp);
4533 vim_free(new_wp);
4534 return NULL;
4535 }
4536 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004537#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004538
4539#ifdef FEAT_AUTOCMD
4540 /* Don't execute autocommands while the window is not properly
4541 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4542 * event. */
4543 block_autocmds();
4544#endif
4545 /*
4546 * link the window in the window list
4547 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004548 if (!hidden)
4549 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004550 new_wp->w_wincol = 0;
4551 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaar429fa852013-04-15 12:27:36 +02004553 /* position the display and the cursor at the top of the file. */
4554 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004556 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004558 new_wp->w_botline = 2;
4559 new_wp->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560#ifdef FEAT_SCROLLBIND
Bram Moolenaar429fa852013-04-15 12:27:36 +02004561 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562#endif
4563
Bram Moolenaar429fa852013-04-15 12:27:36 +02004564 /* We won't calculate w_fraction until resizing the window */
4565 new_wp->w_fraction = 0;
4566 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567
4568#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004569 if (gui.in_use)
4570 {
4571 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4572 SBAR_LEFT, new_wp);
4573 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4574 SBAR_RIGHT, new_wp);
4575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#endif
4577#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004578 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579#endif
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004580#ifdef FEAT_AUTOCMD
Bram Moolenaar429fa852013-04-15 12:27:36 +02004581 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004582#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004583#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004584 new_wp->w_match_head = NULL;
4585 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004586#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004587 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588}
4589
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004591 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 */
4593 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004594win_free(
4595 win_T *wp,
4596 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597{
4598 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004599 buf_T *buf;
4600 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004602#ifdef FEAT_FOLDING
4603 clearFolding(wp);
4604#endif
4605
4606 /* reduce the reference count to the argument list. */
4607 alist_unlink(wp->w_alist);
4608
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004609#ifdef FEAT_AUTOCMD
4610 /* Don't execute autocommands while the window is halfway being deleted.
4611 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004612 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004613#endif
4614
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004615#ifdef FEAT_LUA
4616 lua_window_free(wp);
4617#endif
4618
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004619#ifdef FEAT_MZSCHEME
4620 mzscheme_window_free(wp);
4621#endif
4622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623#ifdef FEAT_PERL
4624 perl_win_free(wp);
4625#endif
4626
4627#ifdef FEAT_PYTHON
4628 python_window_free(wp);
4629#endif
4630
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004631#ifdef FEAT_PYTHON3
4632 python3_window_free(wp);
4633#endif
4634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635#ifdef FEAT_TCL
4636 tcl_window_free(wp);
4637#endif
4638
4639#ifdef FEAT_RUBY
4640 ruby_window_free(wp);
4641#endif
4642
4643 clear_winopt(&wp->w_onebuf_opt);
4644 clear_winopt(&wp->w_allbuf_opt);
4645
4646#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004647 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4648 hash_init(&wp->w_vars->dv_hashtab);
4649 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650#endif
4651
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004652 {
4653 tabpage_T *ttp;
4654
4655 if (prevwin == wp)
4656 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004657 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004658 if (ttp->tp_prevwin == wp)
4659 ttp->tp_prevwin = NULL;
4660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 win_free_lsize(wp);
4662
4663 for (i = 0; i < wp->w_tagstacklen; ++i)
4664 vim_free(wp->w_tagstack[i].tagname);
4665
4666 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004667
Bram Moolenaarff18df02013-07-24 17:51:57 +02004668 /* Remove the window from the b_wininfo lists, it may happen that the
4669 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004670 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004671 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4672 if (wip->wi_win == wp)
4673 wip->wi_win = NULL;
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004676 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#ifdef FEAT_JUMPLIST
4680 free_jumplist(wp);
4681#endif
4682
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004683#ifdef FEAT_QUICKFIX
4684 qf_free_all(wp);
4685#endif
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#ifdef FEAT_GUI
4688 if (gui.in_use)
4689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4691 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4692 }
4693#endif /* FEAT_GUI */
4694
Bram Moolenaar860cae12010-06-05 23:22:07 +02004695#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004696 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004697#endif
4698
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004699#ifdef FEAT_AUTOCMD
4700 if (wp != aucmd_win)
4701#endif
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004702 win_remove(wp, tp);
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004703#ifdef FEAT_AUTOCMD
Bram Moolenaar3be85852014-06-12 14:01:31 +02004704 if (autocmd_busy)
4705 {
4706 wp->w_next = au_pending_free_win;
4707 au_pending_free_win = wp;
4708 }
4709 else
Bram Moolenaarf0224c92014-06-14 12:53:33 +02004710#endif
Bram Moolenaar3be85852014-06-12 14:01:31 +02004711 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004712
4713#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004714 unblock_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716}
4717
4718/*
4719 * Append window "wp" in the window list after window "after".
4720 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004721 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004722win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 win_T *before;
4725
4726 if (after == NULL) /* after NULL is in front of the first */
4727 before = firstwin;
4728 else
4729 before = after->w_next;
4730
4731 wp->w_next = before;
4732 wp->w_prev = after;
4733 if (after == NULL)
4734 firstwin = wp;
4735 else
4736 after->w_next = wp;
4737 if (before == NULL)
4738 lastwin = wp;
4739 else
4740 before->w_prev = wp;
4741}
4742
4743/*
4744 * Remove a window from the window list.
4745 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004746 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004747win_remove(
4748 win_T *wp,
4749 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
4751 if (wp->w_prev != NULL)
4752 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004753 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004755 else
4756 tp->tp_firstwin = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (wp->w_next != NULL)
4758 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004759 else if (tp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004761 else
4762 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763}
4764
4765/*
4766 * Append frame "frp" in a frame list after frame "after".
4767 */
4768 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004769frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 frp->fr_next = after->fr_next;
4772 after->fr_next = frp;
4773 if (frp->fr_next != NULL)
4774 frp->fr_next->fr_prev = frp;
4775 frp->fr_prev = after;
4776}
4777
4778/*
4779 * Insert frame "frp" in a frame list before frame "before".
4780 */
4781 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004782frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783{
4784 frp->fr_next = before;
4785 frp->fr_prev = before->fr_prev;
4786 before->fr_prev = frp;
4787 if (frp->fr_prev != NULL)
4788 frp->fr_prev->fr_next = frp;
4789 else
4790 frp->fr_parent->fr_child = frp;
4791}
4792
4793/*
4794 * Remove a frame from a frame list.
4795 */
4796 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004797frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798{
4799 if (frp->fr_prev != NULL)
4800 frp->fr_prev->fr_next = frp->fr_next;
4801 else
4802 frp->fr_parent->fr_child = frp->fr_next;
4803 if (frp->fr_next != NULL)
4804 frp->fr_next->fr_prev = frp->fr_prev;
4805}
4806
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807/*
4808 * Allocate w_lines[] for window "wp".
4809 * Return FAIL for failure, OK for success.
4810 */
4811 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004812win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813{
4814 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004815 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (wp->w_lines == NULL)
4817 return FAIL;
4818 return OK;
4819}
4820
4821/*
4822 * free lsize arrays for a window
4823 */
4824 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004825win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004827 /* TODO: why would wp be NULL here? */
4828 if (wp != NULL)
4829 {
4830 vim_free(wp->w_lines);
4831 wp->w_lines = NULL;
4832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833}
4834
4835/*
4836 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004837 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 */
4839 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004840shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004842 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843
4844 if (firstwin == NULL) /* not initialized yet */
4845 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 if (h < frame_minheight(topframe, NULL))
4847 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004848
4849 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 * that doesn't result in the right height, forget about that option. */
4851 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004852 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 frame_new_height(topframe, h, FALSE, FALSE);
4854
4855 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004857 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004858
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859#if 0
4860 /* Disabled: don't want making the screen smaller make a window larger. */
4861 if (p_ea)
4862 win_equal(curwin, FALSE, 'v');
4863#endif
4864}
4865
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866/*
4867 * Called from win_new_shellsize() after Columns changed.
4868 */
4869 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004870shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871{
4872 if (firstwin == NULL) /* not initialized yet */
4873 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004874
4875 /* First try setting the widths of windows with 'winfixwidth'. If that
4876 * doesn't result in the right width, forget about that option. */
4877 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004878 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004879 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4880
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4882#if 0
4883 /* Disabled: don't want making the screen smaller make a window larger. */
4884 if (p_ea)
4885 win_equal(curwin, FALSE, 'h');
4886#endif
4887}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888
4889#if defined(FEAT_CMDWIN) || defined(PROTO)
4890/*
4891 * Save the size of all windows in "gap".
4892 */
4893 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004894win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
4896{
4897 win_T *wp;
4898
4899 ga_init2(gap, (int)sizeof(int), 1);
4900 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004901 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
4903 ((int *)gap->ga_data)[gap->ga_len++] =
4904 wp->w_width + wp->w_vsep_width;
4905 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4906 }
4907}
4908
4909/*
4910 * Restore window sizes, but only if the number of windows is still the same.
4911 * Does not free the growarray.
4912 */
4913 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004914win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915{
4916 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004917 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 if (win_count() * 2 == gap->ga_len)
4920 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004921 /* The order matters, because frames contain other frames, but it's
4922 * difficult to get right. The easy way out is to do it twice. */
4923 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02004925 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02004926 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02004927 {
4928 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
4929 win_setheight_win(((int *)gap->ga_data)[i++], wp);
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 /* recompute the window positions */
4933 (void)win_comp_pos();
4934 }
4935}
4936#endif /* FEAT_CMDWIN */
4937
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938/*
4939 * Update the position for all windows, using the width and height of the
4940 * frames.
4941 * Returns the row just after the last window.
4942 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004943 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004944win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004946 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 int col = 0;
4948
4949 frame_comp_pos(topframe, &row, &col);
4950 return row;
4951}
4952
4953/*
4954 * Update the position of the windows in frame "topfrp", using the width and
4955 * height of the frames.
4956 * "*row" and "*col" are the top-left position of the frame. They are updated
4957 * to the bottom-right position plus one.
4958 */
4959 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004960frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961{
4962 win_T *wp;
4963 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 int startcol;
4965 int startrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
4967 wp = topfrp->fr_win;
4968 if (wp != NULL)
4969 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004970 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 {
4972 /* position changed, redraw */
4973 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 redraw_win_later(wp, NOT_VALID);
4976 wp->w_redr_status = TRUE;
4977 }
4978 *row += wp->w_height + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
4981 else
4982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 startrow = *row;
4984 startcol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
4986 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 if (topfrp->fr_layout == FR_ROW)
4988 *row = startrow; /* all frames are at the same row */
4989 else
4990 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 frame_comp_pos(frp, row, col);
4992 }
4993 }
4994}
4995
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996/*
4997 * Set current window height and take care of repositioning other windows to
4998 * fit around it.
4999 */
5000 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005001win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
5003 win_setheight_win(height, curwin);
5004}
5005
5006/*
5007 * Set the window height of window "win" and take care of repositioning other
5008 * windows to fit around it.
5009 */
5010 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005011win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 int row;
5014
5015 if (win == curwin)
5016 {
5017 /* Always keep current window at least one line high, even when
5018 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 if (height < p_wmh)
5020 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (height == 0)
5022 height = 1;
5023 }
5024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 frame_setheight(win->w_frame, height + win->w_status_height);
5026
5027 /* recompute the window positions */
5028 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
5030 /*
5031 * If there is extra space created between the last window and the command
5032 * line, clear it.
5033 */
5034 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5035 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5036 cmdline_row = row;
5037 msg_row = row;
5038 msg_col = 0;
5039
5040 redraw_all_later(NOT_VALID);
5041}
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043/*
5044 * Set the height of a frame to "height" and take care that all frames and
5045 * windows inside it are resized. Also resize frames on the left and right if
5046 * the are in the same FR_ROW frame.
5047 *
5048 * Strategy:
5049 * If the frame is part of a FR_COL frame, try fitting the frame in that
5050 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5051 * go to containing frames to resize them and make room.
5052 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5053 * Check for the minimal height of the FR_ROW frame.
5054 * At the top level we can also use change the command line height.
5055 */
5056 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005057frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
5059 int room; /* total number of lines available */
5060 int take; /* number of lines taken from other windows */
5061 int room_cmdline; /* lines available from cmdline */
5062 int run;
5063 frame_T *frp;
5064 int h;
5065 int room_reserved;
5066
5067 /* If the height already is the desired value, nothing to do. */
5068 if (curfrp->fr_height == height)
5069 return;
5070
5071 if (curfrp->fr_parent == NULL)
5072 {
5073 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005074 if (height > ROWS_AVAIL)
5075 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (height > 0)
5077 frame_new_height(curfrp, height, FALSE, FALSE);
5078 }
5079 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5080 {
5081 /* Row of frames: Also need to resize frames left and right of this
5082 * one. First check for the minimal height of these. */
5083 h = frame_minheight(curfrp->fr_parent, NULL);
5084 if (height < h)
5085 height = h;
5086 frame_setheight(curfrp->fr_parent, height);
5087 }
5088 else
5089 {
5090 /*
5091 * Column of frames: try to change only frames in this column.
5092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 /*
5094 * Do this twice:
5095 * 1: compute room available, if it's not enough try resizing the
5096 * containing frame.
5097 * 2: compute the room available and adjust the height to it.
5098 * Try not to reduce the height of a window with 'winfixheight' set.
5099 */
5100 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
5102 room = 0;
5103 room_reserved = 0;
5104 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5105 frp = frp->fr_next)
5106 {
5107 if (frp != curfrp
5108 && frp->fr_win != NULL
5109 && frp->fr_win->w_p_wfh)
5110 room_reserved += frp->fr_height;
5111 room += frp->fr_height;
5112 if (frp != curfrp)
5113 room -= frame_minheight(frp, NULL);
5114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 if (curfrp->fr_width != Columns)
5116 room_cmdline = 0;
5117 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 room_cmdline = Rows - p_ch - (lastwin->w_winrow
5120 + lastwin->w_height + lastwin->w_status_height);
5121 if (room_cmdline < 0)
5122 room_cmdline = 0;
5123 }
5124
5125 if (height <= room + room_cmdline)
5126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 if (height > room + room_cmdline)
5130 height = room + room_cmdline;
5131 break;
5132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 frame_setheight(curfrp->fr_parent, height
5134 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136
5137 /*
5138 * Compute the number of lines we will take from others frames (can be
5139 * negative!).
5140 */
5141 take = height - curfrp->fr_height;
5142
5143 /* If there is not enough room, also reduce the height of a window
5144 * with 'winfixheight' set. */
5145 if (height > room + room_cmdline - room_reserved)
5146 room_reserved = room + room_cmdline - height;
5147 /* If there is only a 'winfixheight' window and making the
5148 * window smaller, need to make the other window taller. */
5149 if (take < 0 && room - curfrp->fr_height < room_reserved)
5150 room_reserved = 0;
5151
5152 if (take > 0 && room_cmdline > 0)
5153 {
5154 /* use lines from cmdline first */
5155 if (take < room_cmdline)
5156 room_cmdline = take;
5157 take -= room_cmdline;
5158 topframe->fr_height += room_cmdline;
5159 }
5160
5161 /*
5162 * set the current frame to the new height
5163 */
5164 frame_new_height(curfrp, height, FALSE, FALSE);
5165
5166 /*
5167 * First take lines from the frames after the current frame. If
5168 * that is not enough, takes lines from frames above the current
5169 * frame.
5170 */
5171 for (run = 0; run < 2; ++run)
5172 {
5173 if (run == 0)
5174 frp = curfrp->fr_next; /* 1st run: start with next window */
5175 else
5176 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5177 while (frp != NULL && take != 0)
5178 {
5179 h = frame_minheight(frp, NULL);
5180 if (room_reserved > 0
5181 && frp->fr_win != NULL
5182 && frp->fr_win->w_p_wfh)
5183 {
5184 if (room_reserved >= frp->fr_height)
5185 room_reserved -= frp->fr_height;
5186 else
5187 {
5188 if (frp->fr_height - room_reserved > take)
5189 room_reserved = frp->fr_height - take;
5190 take -= frp->fr_height - room_reserved;
5191 frame_new_height(frp, room_reserved, FALSE, FALSE);
5192 room_reserved = 0;
5193 }
5194 }
5195 else
5196 {
5197 if (frp->fr_height - take < h)
5198 {
5199 take -= frp->fr_height - h;
5200 frame_new_height(frp, h, FALSE, FALSE);
5201 }
5202 else
5203 {
5204 frame_new_height(frp, frp->fr_height - take,
5205 FALSE, FALSE);
5206 take = 0;
5207 }
5208 }
5209 if (run == 0)
5210 frp = frp->fr_next;
5211 else
5212 frp = frp->fr_prev;
5213 }
5214 }
5215 }
5216}
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218/*
5219 * Set current window width and take care of repositioning other windows to
5220 * fit around it.
5221 */
5222 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005223win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
5225 win_setwidth_win(width, curwin);
5226}
5227
5228 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005229win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
5231 /* Always keep current window at least one column wide, even when
5232 * 'winminwidth' is zero. */
5233 if (wp == curwin)
5234 {
5235 if (width < p_wmw)
5236 width = p_wmw;
5237 if (width == 0)
5238 width = 1;
5239 }
5240
5241 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5242
5243 /* recompute the window positions */
5244 (void)win_comp_pos();
5245
5246 redraw_all_later(NOT_VALID);
5247}
5248
5249/*
5250 * Set the width of a frame to "width" and take care that all frames and
5251 * windows inside it are resized. Also resize frames above and below if the
5252 * are in the same FR_ROW frame.
5253 *
5254 * Strategy is similar to frame_setheight().
5255 */
5256 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005257frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 int room; /* total number of lines available */
5260 int take; /* number of lines taken from other windows */
5261 int run;
5262 frame_T *frp;
5263 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005264 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266 /* If the width already is the desired value, nothing to do. */
5267 if (curfrp->fr_width == width)
5268 return;
5269
5270 if (curfrp->fr_parent == NULL)
5271 /* topframe: can't change width */
5272 return;
5273
5274 if (curfrp->fr_parent->fr_layout == FR_COL)
5275 {
5276 /* Column of frames: Also need to resize frames above and below of
5277 * this one. First check for the minimal width of these. */
5278 w = frame_minwidth(curfrp->fr_parent, NULL);
5279 if (width < w)
5280 width = w;
5281 frame_setwidth(curfrp->fr_parent, width);
5282 }
5283 else
5284 {
5285 /*
5286 * Row of frames: try to change only frames in this row.
5287 *
5288 * Do this twice:
5289 * 1: compute room available, if it's not enough try resizing the
5290 * containing frame.
5291 * 2: compute the room available and adjust the width to it.
5292 */
5293 for (run = 1; run <= 2; ++run)
5294 {
5295 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005296 room_reserved = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 for (frp = curfrp->fr_parent->fr_child; frp != NULL;
5298 frp = frp->fr_next)
5299 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005300 if (frp != curfrp
5301 && frp->fr_win != NULL
5302 && frp->fr_win->w_p_wfw)
5303 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 room += frp->fr_width;
5305 if (frp != curfrp)
5306 room -= frame_minwidth(frp, NULL);
5307 }
5308
5309 if (width <= room)
5310 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005311 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 {
5313 if (width > room)
5314 width = room;
5315 break;
5316 }
5317 frame_setwidth(curfrp->fr_parent, width
5318 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5319 }
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 /*
5322 * Compute the number of lines we will take from others frames (can be
5323 * negative!).
5324 */
5325 take = width - curfrp->fr_width;
5326
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005327 /* If there is not enough room, also reduce the width of a window
5328 * with 'winfixwidth' set. */
5329 if (width > room - room_reserved)
5330 room_reserved = room - width;
5331 /* If there is only a 'winfixwidth' window and making the
5332 * window smaller, need to make the other window narrower. */
5333 if (take < 0 && room - curfrp->fr_width < room_reserved)
5334 room_reserved = 0;
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 /*
5337 * set the current frame to the new width
5338 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005339 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 /*
5342 * First take lines from the frames right of the current frame. If
5343 * that is not enough, takes lines from frames left of the current
5344 * frame.
5345 */
5346 for (run = 0; run < 2; ++run)
5347 {
5348 if (run == 0)
5349 frp = curfrp->fr_next; /* 1st run: start with next window */
5350 else
5351 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5352 while (frp != NULL && take != 0)
5353 {
5354 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005355 if (room_reserved > 0
5356 && frp->fr_win != NULL
5357 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005359 if (room_reserved >= frp->fr_width)
5360 room_reserved -= frp->fr_width;
5361 else
5362 {
5363 if (frp->fr_width - room_reserved > take)
5364 room_reserved = frp->fr_width - take;
5365 take -= frp->fr_width - room_reserved;
5366 frame_new_width(frp, room_reserved, FALSE, FALSE);
5367 room_reserved = 0;
5368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370 else
5371 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005372 if (frp->fr_width - take < w)
5373 {
5374 take -= frp->fr_width - w;
5375 frame_new_width(frp, w, FALSE, FALSE);
5376 }
5377 else
5378 {
5379 frame_new_width(frp, frp->fr_width - take,
5380 FALSE, FALSE);
5381 take = 0;
5382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384 if (run == 0)
5385 frp = frp->fr_next;
5386 else
5387 frp = frp->fr_prev;
5388 }
5389 }
5390 }
5391}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392
5393/*
5394 * Check 'winminheight' for a valid value.
5395 */
5396 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005397win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 int room;
5400 int first = TRUE;
5401 win_T *wp;
5402
5403 /* loop until there is a 'winminheight' that is possible */
5404 while (p_wmh > 0)
5405 {
5406 /* TODO: handle vertical splits */
5407 room = -p_wh;
Bram Moolenaar29323592016-07-24 22:04:11 +02005408 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 room += wp->w_height - p_wmh;
5410 if (room >= 0)
5411 break;
5412 --p_wmh;
5413 if (first)
5414 {
5415 EMSG(_(e_noroom));
5416 first = FALSE;
5417 }
5418 }
5419}
5420
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005421#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423/*
5424 * Status line of dragwin is dragged "offset" lines down (negative is up).
5425 */
5426 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005427win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 frame_T *curfr;
5430 frame_T *fr;
5431 int room;
5432 int row;
5433 int up; /* if TRUE, drag status line up, otherwise down */
5434 int n;
5435
5436 fr = dragwin->w_frame;
5437 curfr = fr;
5438 if (fr != topframe) /* more than one window */
5439 {
5440 fr = fr->fr_parent;
5441 /* When the parent frame is not a column of frames, its parent should
5442 * be. */
5443 if (fr->fr_layout != FR_COL)
5444 {
5445 curfr = fr;
5446 if (fr != topframe) /* only a row of windows, may drag statusline */
5447 fr = fr->fr_parent;
5448 }
5449 }
5450
5451 /* If this is the last frame in a column, may want to resize the parent
5452 * frame instead (go two up to skip a row of frames). */
5453 while (curfr != topframe && curfr->fr_next == NULL)
5454 {
5455 if (fr != topframe)
5456 fr = fr->fr_parent;
5457 curfr = fr;
5458 if (fr != topframe)
5459 fr = fr->fr_parent;
5460 }
5461
5462 if (offset < 0) /* drag up */
5463 {
5464 up = TRUE;
5465 offset = -offset;
5466 /* sum up the room of the current frame and above it */
5467 if (fr == curfr)
5468 {
5469 /* only one window */
5470 room = fr->fr_height - frame_minheight(fr, NULL);
5471 }
5472 else
5473 {
5474 room = 0;
5475 for (fr = fr->fr_child; ; fr = fr->fr_next)
5476 {
5477 room += fr->fr_height - frame_minheight(fr, NULL);
5478 if (fr == curfr)
5479 break;
5480 }
5481 }
5482 fr = curfr->fr_next; /* put fr at frame that grows */
5483 }
5484 else /* drag down */
5485 {
5486 up = FALSE;
5487 /*
5488 * Only dragging the last status line can reduce p_ch.
5489 */
5490 room = Rows - cmdline_row;
5491 if (curfr->fr_next == NULL)
5492 room -= 1;
5493 else
5494 room -= p_ch;
5495 if (room < 0)
5496 room = 0;
5497 /* sum up the room of frames below of the current one */
5498 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5499 room += fr->fr_height - frame_minheight(fr, NULL);
5500 fr = curfr; /* put fr at window that grows */
5501 }
5502
5503 if (room < offset) /* Not enough room */
5504 offset = room; /* Move as far as we can */
5505 if (offset <= 0)
5506 return;
5507
5508 /*
5509 * Grow frame fr by "offset" lines.
5510 * Doesn't happen when dragging the last status line up.
5511 */
5512 if (fr != NULL)
5513 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5514
5515 if (up)
5516 fr = curfr; /* current frame gets smaller */
5517 else
5518 fr = curfr->fr_next; /* next frame gets smaller */
5519
5520 /*
5521 * Now make the other frames smaller.
5522 */
5523 while (fr != NULL && offset > 0)
5524 {
5525 n = frame_minheight(fr, NULL);
5526 if (fr->fr_height - offset <= n)
5527 {
5528 offset -= fr->fr_height - n;
5529 frame_new_height(fr, n, !up, FALSE);
5530 }
5531 else
5532 {
5533 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5534 break;
5535 }
5536 if (up)
5537 fr = fr->fr_prev;
5538 else
5539 fr = fr->fr_next;
5540 }
5541 row = win_comp_pos();
5542 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5543 cmdline_row = row;
5544 p_ch = Rows - cmdline_row;
5545 if (p_ch < 1)
5546 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005547 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005548 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 showmode();
5550}
5551
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552/*
5553 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5554 */
5555 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005556win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557{
5558 frame_T *curfr;
5559 frame_T *fr;
5560 int room;
5561 int left; /* if TRUE, drag separator line left, otherwise right */
5562 int n;
5563
5564 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005565 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 return;
5567 curfr = fr;
5568 fr = fr->fr_parent;
5569 /* When the parent frame is not a row of frames, its parent should be. */
5570 if (fr->fr_layout != FR_ROW)
5571 {
5572 if (fr == topframe) /* only a column of windows (cannot happen?) */
5573 return;
5574 curfr = fr;
5575 fr = fr->fr_parent;
5576 }
5577
5578 /* If this is the last frame in a row, may want to resize a parent
5579 * frame instead. */
5580 while (curfr->fr_next == NULL)
5581 {
5582 if (fr == topframe)
5583 break;
5584 curfr = fr;
5585 fr = fr->fr_parent;
5586 if (fr != topframe)
5587 {
5588 curfr = fr;
5589 fr = fr->fr_parent;
5590 }
5591 }
5592
5593 if (offset < 0) /* drag left */
5594 {
5595 left = TRUE;
5596 offset = -offset;
5597 /* sum up the room of the current frame and left of it */
5598 room = 0;
5599 for (fr = fr->fr_child; ; fr = fr->fr_next)
5600 {
5601 room += fr->fr_width - frame_minwidth(fr, NULL);
5602 if (fr == curfr)
5603 break;
5604 }
5605 fr = curfr->fr_next; /* put fr at frame that grows */
5606 }
5607 else /* drag right */
5608 {
5609 left = FALSE;
5610 /* sum up the room of frames right of the current one */
5611 room = 0;
5612 for (fr = curfr->fr_next; fr != NULL; fr = fr->fr_next)
5613 room += fr->fr_width - frame_minwidth(fr, NULL);
5614 fr = curfr; /* put fr at window that grows */
5615 }
5616
5617 if (room < offset) /* Not enough room */
5618 offset = room; /* Move as far as we can */
5619 if (offset <= 0) /* No room at all, quit. */
5620 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005621 if (fr == NULL)
5622 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005625 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 /* shrink other frames: current and at the left or at the right */
5628 if (left)
5629 fr = curfr; /* current frame gets smaller */
5630 else
5631 fr = curfr->fr_next; /* next frame gets smaller */
5632
5633 while (fr != NULL && offset > 0)
5634 {
5635 n = frame_minwidth(fr, NULL);
5636 if (fr->fr_width - offset <= n)
5637 {
5638 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005639 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641 else
5642 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005643 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 break;
5645 }
5646 if (left)
5647 fr = fr->fr_prev;
5648 else
5649 fr = fr->fr_next;
5650 }
5651 (void)win_comp_pos();
5652 redraw_all_later(NOT_VALID);
5653}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654#endif /* FEAT_MOUSE */
5655
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005656#define FRACTION_MULT 16384L
5657
5658/*
5659 * Set wp->w_fraction for the current w_wrow and w_height.
5660 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005661 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005662set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005663{
5664 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005665 + wp->w_height / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005666}
5667
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668/*
5669 * Set the height of a window.
5670 * This takes care of the things inside the window, not what happens to the
5671 * window position, the frame or to other windows.
5672 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005673 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005674win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005676 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
5678 /* Don't want a negative height. Happens when splitting a tiny window.
5679 * Will equalize heights soon to fix it. */
5680 if (height < 0)
5681 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005682 if (wp->w_height == height)
5683 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005685 if (wp->w_height > 0)
5686 {
5687 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005688 /* w_wrow needs to be valid. When setting 'laststatus' this may
5689 * call win_new_height() recursively. */
5690 validate_cursor();
5691 if (wp->w_height != prev_height)
5692 return; /* Recursive call already changed the size, bail out here
5693 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005694 if (wp->w_wrow != wp->w_prev_fraction_row)
5695 set_fraction(wp);
5696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 wp->w_height = height;
5699 wp->w_skipcol = 0;
5700
Bram Moolenaar955f1982017-02-05 15:10:51 +01005701 /* There is no point in adjusting the scroll position when exiting. Some
5702 * values might be invalid. */
5703 if (!exiting)
5704 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005705}
5706
5707 void
5708scroll_to_fraction(win_T *wp, int prev_height)
5709{
5710 linenr_T lnum;
5711 int sline, line_size;
5712 int height = wp->w_height;
5713
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 /* Don't change w_topline when height is zero. Don't set w_topline when
5715 * 'scrollbind' is set and this isn't the current window. */
5716 if (height > 0
5717#ifdef FEAT_SCROLLBIND
5718 && (!wp->w_p_scb || wp == curwin)
5719#endif
5720 )
5721 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005722 /*
5723 * Find a value for w_topline that shows the cursor at the same
5724 * relative position in the window as before (more or less).
5725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 lnum = wp->w_cursor.lnum;
5727 if (lnum < 1) /* can happen when starting up */
5728 lnum = 1;
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005729 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L
5730 + FRACTION_MULT / 2) / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5732 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005733
5734 if (sline >= 0)
5735 {
5736 /* Make sure the whole cursor line is visible, if possible. */
5737 int rows = plines_win(wp, lnum, FALSE);
5738
5739 if (sline > wp->w_height - rows)
5740 {
5741 sline = wp->w_height - rows;
5742 wp->w_wrow -= rows - line_size;
5743 }
5744 }
5745
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 if (sline < 0)
5747 {
5748 /*
5749 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005750 * Make cursor line the first line in the window. If not enough
5751 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 */
5753 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005754 if (wp->w_wrow >= wp->w_height
5755 && (W_WIDTH(wp) - win_col_off(wp)) > 0)
5756 {
5757 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp);
5758 --wp->w_wrow;
5759 while (wp->w_wrow >= wp->w_height)
5760 {
5761 wp->w_skipcol += W_WIDTH(wp) - win_col_off(wp)
5762 + win_col_off2(wp);
5763 --wp->w_wrow;
5764 }
5765 }
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005766 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005768 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005770 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
5772#ifdef FEAT_FOLDING
5773 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5774 if (lnum == 1)
5775 {
5776 /* first line in buffer is folded */
5777 line_size = 1;
5778 --sline;
5779 break;
5780 }
5781#endif
5782 --lnum;
5783#ifdef FEAT_DIFF
5784 if (lnum == wp->w_topline)
5785 line_size = plines_win_nofill(wp, lnum, TRUE)
5786 + wp->w_topfill;
5787 else
5788#endif
5789 line_size = plines_win(wp, lnum, TRUE);
5790 sline -= line_size;
5791 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 if (sline < 0)
5794 {
5795 /*
5796 * Line we want at top would go off top of screen. Use next
5797 * line instead.
5798 */
5799#ifdef FEAT_FOLDING
5800 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5801#endif
5802 lnum++;
5803 wp->w_wrow -= line_size + sline;
5804 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005805 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
5807 /* First line of file reached, use that as topline. */
5808 lnum = 1;
5809 wp->w_wrow -= sline;
5810 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005811
Bram Moolenaarb4d21352014-07-16 14:16:46 +02005812 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
5815
5816 if (wp == curwin)
5817 {
5818 if (p_so)
5819 update_topline();
5820 curs_columns(FALSE); /* validate w_wrow */
5821 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005822 if (prev_height > 0)
5823 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
5825 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005826 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 invalidate_botline_win(wp);
5829}
5830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831/*
5832 * Set the width of a window.
5833 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005834 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005835win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
5837 wp->w_width = width;
5838 wp->w_lines_valid = 0;
5839 changed_line_abv_curs_win(wp);
5840 invalidate_botline_win(wp);
5841 if (wp == curwin)
5842 {
5843 update_topline();
5844 curs_columns(TRUE); /* validate w_wrow */
5845 }
5846 redraw_win_later(wp, NOT_VALID);
5847 wp->w_redr_status = TRUE;
5848}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005851win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
5853 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5854 if (wp->w_p_scr == 0)
5855 wp->w_p_scr = 1;
5856}
5857
5858/*
5859 * command_height: called whenever p_ch has been changed
5860 */
5861 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005862command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 int h;
5865 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005866 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005868 /* Use the value of p_ch that we remembered. This is needed for when the
5869 * GUI starts up, we can't be sure in what order things happen. And when
5870 * p_ch was changed in another tab page. */
5871 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 /* Find bottom frame with width of screen. */
5874 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5876 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878 /* Avoid changing the height of a window with 'winfixheight' set. */
5879 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5880 && frp->fr_win->w_p_wfh)
5881 frp = frp->fr_prev;
5882
5883 if (starting != NO_SCREEN)
5884 {
5885 cmdline_row = Rows - p_ch;
5886
5887 if (p_ch > old_p_ch) /* p_ch got bigger */
5888 {
5889 while (p_ch > old_p_ch)
5890 {
5891 if (frp == NULL)
5892 {
5893 EMSG(_(e_noroom));
5894 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00005895 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 cmdline_row = Rows - p_ch;
5897 break;
5898 }
5899 h = frp->fr_height - frame_minheight(frp, NULL);
5900 if (h > p_ch - old_p_ch)
5901 h = p_ch - old_p_ch;
5902 old_p_ch += h;
5903 frame_add_height(frp, -h);
5904 frp = frp->fr_prev;
5905 }
5906
5907 /* Recompute window positions. */
5908 (void)win_comp_pos();
5909
5910 /* clear the lines added to cmdline */
5911 if (full_screen)
5912 screen_fill((int)(cmdline_row), (int)Rows, 0,
5913 (int)Columns, ' ', ' ', 0);
5914 msg_row = cmdline_row;
5915 redraw_cmdline = TRUE;
5916 return;
5917 }
5918
5919 if (msg_row < cmdline_row)
5920 msg_row = cmdline_row;
5921 redraw_cmdline = TRUE;
5922 }
5923 frame_add_height(frp, (int)(old_p_ch - p_ch));
5924
5925 /* Recompute window positions. */
5926 if (frp != lastwin->w_frame)
5927 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928}
5929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930/*
5931 * Resize frame "frp" to be "n" lines higher (negative for less high).
5932 * Also resize the frames it is contained in.
5933 */
5934 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005935frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
5938 for (;;)
5939 {
5940 frp = frp->fr_parent;
5941 if (frp == NULL)
5942 break;
5943 frp->fr_height += n;
5944 }
5945}
5946
5947/*
5948 * Add or remove a status line for the bottom window(s), according to the
5949 * value of 'laststatus'.
5950 */
5951 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005952last_status(
5953 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954{
5955 /* Don't make a difference between horizontal or vertical split. */
5956 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01005957 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958}
5959
5960 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005961last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 frame_T *fp;
5964 win_T *wp;
5965
5966 if (fr->fr_layout == FR_LEAF)
5967 {
5968 wp = fr->fr_win;
5969 if (wp->w_status_height != 0 && !statusline)
5970 {
5971 /* remove status line */
5972 win_new_height(wp, wp->w_height + 1);
5973 wp->w_status_height = 0;
5974 comp_col();
5975 }
5976 else if (wp->w_status_height == 0 && statusline)
5977 {
5978 /* Find a frame to take a line from. */
5979 fp = fr;
5980 while (fp->fr_height <= frame_minheight(fp, NULL))
5981 {
5982 if (fp == topframe)
5983 {
5984 EMSG(_(e_noroom));
5985 return;
5986 }
5987 /* In a column of frames: go to frame above. If already at
5988 * the top or in a row of frames: go to parent. */
5989 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
5990 fp = fp->fr_prev;
5991 else
5992 fp = fp->fr_parent;
5993 }
5994 wp->w_status_height = 1;
5995 if (fp != fr)
5996 {
5997 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
5998 frame_fix_height(wp);
5999 (void)win_comp_pos();
6000 }
6001 else
6002 win_new_height(wp, wp->w_height - 1);
6003 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006004 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 }
6006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 else if (fr->fr_layout == FR_ROW)
6008 {
6009 /* vertically split windows, set status line for each one */
6010 for (fp = fr->fr_child; fp != NULL; fp = fp->fr_next)
6011 last_status_rec(fp, statusline);
6012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 else
6014 {
6015 /* horizontally split window, set status line for last one */
6016 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6017 ;
6018 last_status_rec(fp, statusline);
6019 }
6020}
6021
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006022/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006023 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006024 */
6025 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006026tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006027{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006028#ifdef FEAT_GUI_TABLINE
6029 /* When the GUI has the tabline then this always returns zero. */
6030 if (gui_use_tabline())
6031 return 0;
6032#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006033 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006034 {
6035 case 0: return 0;
6036 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6037 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006038 return 1;
6039}
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041#if defined(FEAT_SEARCHPATH) || defined(PROTO)
6042/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006043 * Get the file name at the cursor.
6044 * If Visual mode is active, use the selected text if it's in one line.
6045 * Returns the name in allocated memory, NULL for failure.
6046 */
6047 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006048grab_file_name(long count, linenr_T *file_lnum)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006049{
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006050 int options = FNAME_MESS|FNAME_EXP|FNAME_REL|FNAME_UNESC;
6051
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006052 if (VIsual_active)
6053 {
6054 int len;
6055 char_u *ptr;
6056
6057 if (get_visual_text(NULL, &ptr, &len) == FAIL)
6058 return NULL;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006059 return find_file_name_in_path(ptr, len, options,
6060 count, curbuf->b_ffname);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006061 }
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006062 return file_name_at_cursor(options | FNAME_HYP, count, file_lnum);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006063
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006064}
6065
6066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 * Return the file name under or after the cursor.
6068 *
6069 * The 'path' option is searched if the file name is not absolute.
6070 * The string returned has been alloc'ed and should be freed by the caller.
6071 * NULL is returned if the file name or file is not found.
6072 *
6073 * options:
6074 * FNAME_MESS give error messages
6075 * FNAME_EXP expand to path
6076 * FNAME_HYP check for hypertext link
6077 * FNAME_INCL apply "includeexpr"
6078 */
6079 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006080file_name_at_cursor(int options, long count, linenr_T *file_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 return file_name_in_line(ml_get_curline(),
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006083 curwin->w_cursor.col, options, count, curbuf->b_ffname,
6084 file_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085}
6086
6087/*
6088 * Return the name of the file under or after ptr[col].
6089 * Otherwise like file_name_at_cursor().
6090 */
6091 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006092file_name_in_line(
6093 char_u *line,
6094 int col,
6095 int options,
6096 long count,
6097 char_u *rel_fname, /* file we are searching relative to */
6098 linenr_T *file_lnum) /* line number after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099{
6100 char_u *ptr;
6101 int len;
6102
6103 /*
6104 * search forward for what could be the start of a file name
6105 */
6106 ptr = line + col;
6107 while (*ptr != NUL && !vim_isfilec(*ptr))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006108 MB_PTR_ADV(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 if (*ptr == NUL) /* nothing found */
6110 {
6111 if (options & FNAME_MESS)
6112 EMSG(_("E446: No file name under cursor"));
6113 return NULL;
6114 }
6115
6116 /*
6117 * Search backward for first char of the file name.
6118 * Go one char back to ":" before "//" even when ':' is not in 'isfname'.
6119 */
6120 while (ptr > line)
6121 {
6122#ifdef FEAT_MBYTE
6123 if (has_mbyte && (len = (*mb_head_off)(line, ptr - 1)) > 0)
6124 ptr -= len + 1;
6125 else
6126#endif
6127 if (vim_isfilec(ptr[-1])
6128 || ((options & FNAME_HYP) && path_is_url(ptr - 1)))
6129 --ptr;
6130 else
6131 break;
6132 }
6133
6134 /*
6135 * Search forward for the last char of the file name.
6136 * Also allow "://" when ':' is not in 'isfname'.
6137 */
6138 len = 0;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006139 while (vim_isfilec(ptr[len]) || (ptr[len] == '\\' && ptr[len + 1] == ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 || ((options & FNAME_HYP) && path_is_url(ptr + len)))
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006141 {
6142 if (ptr[len] == '\\')
6143 /* Skip over the "\" in "\ ". */
6144 ++len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145#ifdef FEAT_MBYTE
6146 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006147 len += (*mb_ptr2len)(ptr + len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 else
6149#endif
6150 ++len;
Bram Moolenaard45c07a2015-02-27 17:19:10 +01006151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
6153 /*
6154 * If there is trailing punctuation, remove it.
6155 * But don't remove "..", could be a directory name.
6156 */
6157 if (len > 2 && vim_strchr((char_u *)".,:;!", ptr[len - 1]) != NULL
6158 && ptr[len - 2] != '.')
6159 --len;
6160
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006161 if (file_lnum != NULL)
6162 {
6163 char_u *p;
6164
6165 /* Get the number after the file name and a separator character */
6166 p = ptr + len;
6167 p = skipwhite(p);
6168 if (*p != NUL)
6169 {
6170 if (!isdigit(*p))
6171 ++p; /* skip the separator */
6172 p = skipwhite(p);
6173 if (isdigit(*p))
6174 *file_lnum = (int)getdigits(&p);
6175 }
6176 }
6177
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 return find_file_name_in_path(ptr, len, options, count, rel_fname);
6179}
6180
6181# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006182static char_u *eval_includeexpr(char_u *ptr, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
6184 static char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006185eval_includeexpr(char_u *ptr, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186{
6187 char_u *res;
6188
6189 set_vim_var_string(VV_FNAME, ptr, len);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006190 res = eval_to_string_safe(curbuf->b_p_inex, NULL,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00006191 was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 set_vim_var_string(VV_FNAME, NULL, 0);
6193 return res;
6194}
6195#endif
6196
6197/*
6198 * Return the name of the file ptr[len] in 'path'.
6199 * Otherwise like file_name_at_cursor().
6200 */
6201 char_u *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006202find_file_name_in_path(
6203 char_u *ptr,
6204 int len,
6205 int options,
6206 long count,
6207 char_u *rel_fname) /* file we are searching relative to */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 char_u *file_name;
6210 int c;
6211# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6212 char_u *tofree = NULL;
6213
6214 if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6215 {
6216 tofree = eval_includeexpr(ptr, len);
6217 if (tofree != NULL)
6218 {
6219 ptr = tofree;
6220 len = (int)STRLEN(ptr);
6221 }
6222 }
6223# endif
6224
6225 if (options & FNAME_EXP)
6226 {
6227 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6228 TRUE, rel_fname);
6229
6230# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6231 /*
6232 * If the file could not be found in a normal way, try applying
6233 * 'includeexpr' (unless done already).
6234 */
6235 if (file_name == NULL
6236 && !(options & FNAME_INCL) && *curbuf->b_p_inex != NUL)
6237 {
6238 tofree = eval_includeexpr(ptr, len);
6239 if (tofree != NULL)
6240 {
6241 ptr = tofree;
6242 len = (int)STRLEN(ptr);
6243 file_name = find_file_in_path(ptr, len, options & ~FNAME_MESS,
6244 TRUE, rel_fname);
6245 }
6246 }
6247# endif
6248 if (file_name == NULL && (options & FNAME_MESS))
6249 {
6250 c = ptr[len];
6251 ptr[len] = NUL;
6252 EMSG2(_("E447: Can't find file \"%s\" in path"), ptr);
6253 ptr[len] = c;
6254 }
6255
6256 /* Repeat finding the file "count" times. This matters when it
6257 * appears several times in the path. */
6258 while (file_name != NULL && --count > 0)
6259 {
6260 vim_free(file_name);
6261 file_name = find_file_in_path(ptr, len, options, FALSE, rel_fname);
6262 }
6263 }
6264 else
6265 file_name = vim_strnsave(ptr, len);
6266
6267# if defined(FEAT_FIND_ID) && defined(FEAT_EVAL)
6268 vim_free(tofree);
6269# endif
6270
6271 return file_name;
6272}
6273#endif /* FEAT_SEARCHPATH */
6274
6275/*
6276 * Check if the "://" of a URL is at the pointer, return URL_SLASH.
6277 * Also check for ":\\", which MS Internet Explorer accepts, return
6278 * URL_BACKSLASH.
6279 */
6280 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006281path_is_url(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 if (STRNCMP(p, "://", (size_t)3) == 0)
6284 return URL_SLASH;
6285 else if (STRNCMP(p, ":\\\\", (size_t)3) == 0)
6286 return URL_BACKSLASH;
6287 return 0;
6288}
6289
6290/*
6291 * Check if "fname" starts with "name://". Return URL_SLASH if it does.
6292 * Return URL_BACKSLASH for "name:\\".
6293 * Return zero otherwise.
6294 */
6295 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006296path_with_url(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
6298 char_u *p;
6299
6300 for (p = fname; isalpha(*p); ++p)
6301 ;
6302 return path_is_url(p);
6303}
6304
6305/*
6306 * Return TRUE if "name" is a full (absolute) path name or URL.
6307 */
6308 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006309vim_isAbsName(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 return (path_with_url(name) != 0 || mch_isFullName(name));
6312}
6313
6314/*
Bram Moolenaar5b962cf2005-12-12 21:58:40 +00006315 * Get absolute file name into buffer "buf[len]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 *
6317 * return FAIL for failure, OK otherwise
6318 */
6319 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006320vim_FullName(
6321 char_u *fname,
6322 char_u *buf,
6323 int len,
6324 int force) /* force expansion even when already absolute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 int retval = OK;
6327 int url;
6328
6329 *buf = NUL;
6330 if (fname == NULL)
6331 return FAIL;
6332
6333 url = path_with_url(fname);
6334 if (!url)
6335 retval = mch_FullName(fname, buf, len, force);
6336 if (url || retval == FAIL)
6337 {
6338 /* something failed; use the file name (truncate when too long) */
Bram Moolenaarb6356332005-07-18 21:40:44 +00006339 vim_strncpy(buf, fname, len - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 }
Bram Moolenaar48e330a2016-02-23 14:53:34 +01006341#if defined(MACOS_CLASSIC) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 slash_adjust(buf);
6343#endif
6344 return retval;
6345}
6346
6347/*
6348 * Return the minimal number of rows that is needed on the screen to display
6349 * the current number of windows.
6350 */
6351 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006352min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006355 tabpage_T *tp;
6356 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357
6358 if (firstwin == NULL) /* not initialized yet */
6359 return MIN_LINES;
6360
Bram Moolenaarf740b292006-02-16 22:11:02 +00006361 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006362 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006363 {
6364 n = frame_minheight(tp->tp_topframe, NULL);
6365 if (total < n)
6366 total = n;
6367 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006368 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006369 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 return total;
6371}
6372
6373/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006374 * Return TRUE if there is only one window (in the current tab page), not
6375 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006376 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 */
6378 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006379only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 int count = 0;
6382 win_T *wp;
6383
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006384 /* If there is another tab page there always is another window. */
6385 if (first_tabpage->tp_next != NULL)
6386 return FALSE;
6387
Bram Moolenaar29323592016-07-24 22:04:11 +02006388 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006389 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006390 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391# ifdef FEAT_QUICKFIX
6392 || wp->w_p_pvw
6393# endif
6394 ) || wp == curwin)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006395# ifdef FEAT_AUTOCMD
6396 && wp != aucmd_win
6397# endif
6398 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 ++count;
6400 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401}
6402
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403/*
6404 * Correct the cursor line number in other windows. Used after changing the
6405 * current buffer, and before applying autocommands.
6406 * When "do_curwin" is TRUE, also check current window.
6407 */
6408 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006409check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
6411 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006412 tabpage_T *tp;
6413
6414 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 {
6417 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6418 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6419 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6420 wp->w_topline = curbuf->b_ml.ml_line_count;
6421 }
6422}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423
6424/*
6425 * A snapshot of the window sizes, to restore them after closing the help
6426 * window.
6427 * Only these fields are used:
6428 * fr_layout
6429 * fr_width
6430 * fr_height
6431 * fr_next
6432 * fr_child
6433 * fr_win (only valid for the old curwin, NULL otherwise)
6434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435
6436/*
6437 * Create a snapshot of the current frame sizes.
6438 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006439 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006440make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006442 clear_snapshot(curtab, idx);
6443 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444}
6445
6446 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006447make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6450 if (*frp == NULL)
6451 return;
6452 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 (*frp)->fr_height = fr->fr_height;
6455 if (fr->fr_next != NULL)
6456 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6457 if (fr->fr_child != NULL)
6458 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6459 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6460 (*frp)->fr_win = curwin;
6461}
6462
6463/*
6464 * Remove any existing snapshot.
6465 */
6466 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006467clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006469 clear_snapshot_rec(tp->tp_snapshot[idx]);
6470 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471}
6472
6473 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006474clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475{
6476 if (fr != NULL)
6477 {
6478 clear_snapshot_rec(fr->fr_next);
6479 clear_snapshot_rec(fr->fr_child);
6480 vim_free(fr);
6481 }
6482}
6483
6484/*
6485 * Restore a previously created snapshot, if there is any.
6486 * This is only done if the screen size didn't change and the window layout is
6487 * still the same.
6488 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006489 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006490restore_snapshot(
6491 int idx,
6492 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 win_T *wp;
6495
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006496 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006497 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006498 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6499 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006501 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 win_comp_pos();
6503 if (wp != NULL && close_curwin)
6504 win_goto(wp);
6505 redraw_all_later(CLEAR);
6506 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006507 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508}
6509
6510/*
6511 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006512 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 */
6514 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006515check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 if (sn->fr_layout != fr->fr_layout
6518 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6519 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6520 || (sn->fr_next != NULL
6521 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6522 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006523 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006524 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 return FAIL;
6526 return OK;
6527}
6528
6529/*
6530 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6531 * following frames and children.
6532 * Returns a pointer to the old current window, or NULL.
6533 */
6534 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006535restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 win_T *wp = NULL;
6538 win_T *wp2;
6539
6540 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 if (fr->fr_layout == FR_LEAF)
6543 {
6544 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006545 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 wp = sn->fr_win;
6547 }
6548 if (sn->fr_next != NULL)
6549 {
6550 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6551 if (wp2 != NULL)
6552 wp = wp2;
6553 }
6554 if (sn->fr_child != NULL)
6555 {
6556 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6557 if (wp2 != NULL)
6558 wp = wp2;
6559 }
6560 return wp;
6561}
6562
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006563#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6564 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006565/*
6566 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006567 * restore_win() MUST be called to undo, also when FAIL is returned.
6568 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006569 * When "no_display" is TRUE the display won't be affected, no redraw is
6570 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006571 * Returns FAIL if switching to "win" failed.
6572 */
6573 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006574switch_win(
6575 win_T **save_curwin UNUSED,
6576 tabpage_T **save_curtab UNUSED,
6577 win_T *win UNUSED,
6578 tabpage_T *tp UNUSED,
6579 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006580{
6581# ifdef FEAT_AUTOCMD
6582 block_autocmds();
6583# endif
Bram Moolenaar105bc352013-05-17 16:03:57 +02006584 *save_curwin = curwin;
6585 if (tp != NULL)
6586 {
6587 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006588 if (no_display)
6589 {
6590 curtab->tp_firstwin = firstwin;
6591 curtab->tp_lastwin = lastwin;
6592 curtab = tp;
6593 firstwin = curtab->tp_firstwin;
6594 lastwin = curtab->tp_lastwin;
6595 }
6596 else
6597 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006598 }
6599 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006600 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006601 curwin = win;
6602 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006603 return OK;
6604}
6605
6606/*
6607 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006608 * When "no_display" is TRUE the display won't be affected, no redraw is
6609 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006610 */
6611 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006612restore_win(
6613 win_T *save_curwin UNUSED,
6614 tabpage_T *save_curtab UNUSED,
6615 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006616{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006617 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006618 {
6619 if (no_display)
6620 {
6621 curtab->tp_firstwin = firstwin;
6622 curtab->tp_lastwin = lastwin;
6623 curtab = save_curtab;
6624 firstwin = curtab->tp_firstwin;
6625 lastwin = curtab->tp_lastwin;
6626 }
6627 else
6628 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6629 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006630 if (win_valid(save_curwin))
6631 {
6632 curwin = save_curwin;
6633 curbuf = curwin->w_buffer;
6634 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006635# ifdef FEAT_AUTOCMD
6636 unblock_autocmds();
6637# endif
6638}
6639
6640/*
6641 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6642 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6643 */
6644 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006645switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006646{
6647# ifdef FEAT_AUTOCMD
6648 block_autocmds();
6649# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006650 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006651 --curbuf->b_nwindows;
6652 curbuf = buf;
6653 curwin->w_buffer = buf;
6654 ++curbuf->b_nwindows;
6655}
6656
6657/*
6658 * Restore the current buffer after using switch_buffer().
6659 */
6660 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006661restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006662{
6663# ifdef FEAT_AUTOCMD
6664 unblock_autocmds();
6665# endif
6666 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006667 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006668 {
6669 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006670 curwin->w_buffer = save_curbuf->br_buf;
6671 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006672 ++curbuf->b_nwindows;
6673 }
6674}
6675#endif
6676
Bram Moolenaar4033c552017-09-16 20:54:51 +02006677#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678/*
6679 * Return TRUE if there is any vertically split window.
6680 */
6681 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006682win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 frame_T *fr;
6685
6686 if (topframe->fr_layout == FR_ROW)
6687 return TRUE;
6688
6689 if (topframe->fr_layout == FR_COL)
6690 for (fr = topframe->fr_child; fr != NULL; fr = fr->fr_next)
6691 if (fr->fr_layout == FR_ROW)
6692 return TRUE;
6693
6694 return FALSE;
6695}
6696#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006697
6698#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6699/*
6700 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006701 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006702 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6703 * If no particular ID is desired, -1 must be specified for 'id'.
6704 * Return ID of added match, -1 on failure.
6705 */
6706 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006707match_add(
6708 win_T *wp,
6709 char_u *grp,
6710 char_u *pat,
6711 int prio,
6712 int id,
6713 list_T *pos_list,
6714 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006715{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006716 matchitem_T *cur;
6717 matchitem_T *prev;
6718 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006719 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006720 regprog_T *regprog = NULL;
6721 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006722
Bram Moolenaarb3414592014-06-17 17:48:32 +02006723 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006724 return -1;
6725 if (id < -1 || id == 0)
6726 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006727 EMSGN(_("E799: Invalid ID: %ld (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006728 return -1;
6729 }
6730 if (id != -1)
6731 {
6732 cur = wp->w_match_head;
6733 while (cur != NULL)
6734 {
6735 if (cur->id == id)
6736 {
Bram Moolenaar5b302912016-08-24 22:11:55 +02006737 EMSGN(_("E801: ID already taken: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006738 return -1;
6739 }
6740 cur = cur->next;
6741 }
6742 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006743 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006744 {
6745 EMSG2(_(e_nogroup), grp);
6746 return -1;
6747 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006748 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006749 {
6750 EMSG2(_(e_invarg2), pat);
6751 return -1;
6752 }
6753
6754 /* Find available match ID. */
6755 while (id == -1)
6756 {
6757 cur = wp->w_match_head;
6758 while (cur != NULL && cur->id != wp->w_next_match_id)
6759 cur = cur->next;
6760 if (cur == NULL)
6761 id = wp->w_next_match_id;
6762 wp->w_next_match_id++;
6763 }
6764
6765 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006766 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006767 m->id = id;
6768 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006769 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006770 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006771 m->match.regprog = regprog;
6772 m->match.rmm_ic = FALSE;
6773 m->match.rmm_maxcol = 0;
Bram Moolenaar42356152016-03-31 22:27:40 +02006774# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006775 m->conceal_char = 0;
6776 if (conceal_char != NULL)
6777 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006778# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006779
Bram Moolenaarb3414592014-06-17 17:48:32 +02006780 /* Set up position matches */
6781 if (pos_list != NULL)
6782 {
6783 linenr_T toplnum = 0;
6784 linenr_T botlnum = 0;
6785 listitem_T *li;
6786 int i;
6787
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006788 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006789 i++, li = li->li_next)
6790 {
6791 linenr_T lnum = 0;
6792 colnr_T col = 0;
6793 int len = 1;
6794 list_T *subl;
6795 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006796 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006797
Bram Moolenaarb3414592014-06-17 17:48:32 +02006798 if (li->li_tv.v_type == VAR_LIST)
6799 {
6800 subl = li->li_tv.vval.v_list;
6801 if (subl == NULL)
6802 goto fail;
6803 subli = subl->lv_first;
6804 if (subli == NULL)
6805 goto fail;
6806 lnum = get_tv_number_chk(&subli->li_tv, &error);
6807 if (error == TRUE)
6808 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006809 if (lnum == 0)
6810 {
6811 --i;
6812 continue;
6813 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006814 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006815 subli = subli->li_next;
6816 if (subli != NULL)
6817 {
6818 col = get_tv_number_chk(&subli->li_tv, &error);
6819 if (error == TRUE)
6820 goto fail;
6821 subli = subli->li_next;
6822 if (subli != NULL)
6823 {
6824 len = get_tv_number_chk(&subli->li_tv, &error);
6825 if (error == TRUE)
6826 goto fail;
6827 }
6828 }
6829 m->pos.pos[i].col = col;
6830 m->pos.pos[i].len = len;
6831 }
6832 else if (li->li_tv.v_type == VAR_NUMBER)
6833 {
6834 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006835 {
6836 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006837 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006838 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006839 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6840 m->pos.pos[i].col = 0;
6841 m->pos.pos[i].len = 0;
6842 }
6843 else
6844 {
6845 EMSG(_("List or number required"));
6846 goto fail;
6847 }
6848 if (toplnum == 0 || lnum < toplnum)
6849 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006850 if (botlnum == 0 || lnum >= botlnum)
6851 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006852 }
6853
6854 /* Calculate top and bottom lines for redrawing area */
6855 if (toplnum != 0)
6856 {
6857 if (wp->w_buffer->b_mod_set)
6858 {
6859 if (wp->w_buffer->b_mod_top > toplnum)
6860 wp->w_buffer->b_mod_top = toplnum;
6861 if (wp->w_buffer->b_mod_bot < botlnum)
6862 wp->w_buffer->b_mod_bot = botlnum;
6863 }
6864 else
6865 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006866 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006867 wp->w_buffer->b_mod_top = toplnum;
6868 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006869 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006870 }
6871 m->pos.toplnum = toplnum;
6872 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006873 rtype = VALID;
6874 }
6875 }
6876
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006877 /* Insert new match. The match list is in ascending order with regard to
6878 * the match priorities. */
6879 cur = wp->w_match_head;
6880 prev = cur;
6881 while (cur != NULL && prio >= cur->priority)
6882 {
6883 prev = cur;
6884 cur = cur->next;
6885 }
6886 if (cur == prev)
6887 wp->w_match_head = m;
6888 else
6889 prev->next = m;
6890 m->next = cur;
6891
Bram Moolenaarb3414592014-06-17 17:48:32 +02006892 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006893 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006894
6895fail:
6896 vim_free(m);
6897 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006898}
6899
6900/*
6901 * Delete match with ID 'id' in the match list of window 'wp'.
6902 * Print error messages if 'perr' is TRUE.
6903 */
6904 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006905match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006906{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006907 matchitem_T *cur = wp->w_match_head;
6908 matchitem_T *prev = cur;
6909 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006910
6911 if (id < 1)
6912 {
6913 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006914 EMSGN(_("E802: Invalid ID: %ld (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006915 id);
6916 return -1;
6917 }
6918 while (cur != NULL && cur->id != id)
6919 {
6920 prev = cur;
6921 cur = cur->next;
6922 }
6923 if (cur == NULL)
6924 {
6925 if (perr == TRUE)
Bram Moolenaar5b302912016-08-24 22:11:55 +02006926 EMSGN(_("E803: ID not found: %ld"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006927 return -1;
6928 }
6929 if (cur == prev)
6930 wp->w_match_head = cur->next;
6931 else
6932 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006933 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006934 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006935 if (cur->pos.toplnum != 0)
6936 {
6937 if (wp->w_buffer->b_mod_set)
6938 {
6939 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6940 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6941 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6942 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6943 }
6944 else
6945 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006946 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006947 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6948 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006949 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006950 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006951 rtype = VALID;
6952 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006953 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006954 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006955 return 0;
6956}
6957
6958/*
6959 * Delete all matches in the match list of window 'wp'.
6960 */
6961 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006962clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006963{
6964 matchitem_T *m;
6965
6966 while (wp->w_match_head != NULL)
6967 {
6968 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006969 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006970 vim_free(wp->w_match_head->pattern);
6971 vim_free(wp->w_match_head);
6972 wp->w_match_head = m;
6973 }
6974 redraw_later(SOME_VALID);
6975}
6976
6977/*
6978 * Get match from ID 'id' in window 'wp'.
6979 * Return NULL if match not found.
6980 */
6981 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006982get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006983{
6984 matchitem_T *cur = wp->w_match_head;
6985
6986 while (cur != NULL && cur->id != id)
6987 cur = cur->next;
6988 return cur;
6989}
6990#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006991
6992#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6993 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006994get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006995{
6996 int i = 1;
6997 win_T *w;
6998
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006999 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02007000 ++i;
7001
7002 if (w == NULL)
7003 return 0;
7004 else
7005 return i;
7006}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007007
7008 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02007009get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02007010{
7011 int i = 1;
7012 tabpage_T *t;
7013
7014 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
7015 ++i;
7016
7017 if (t == NULL)
7018 return 0;
7019 else
7020 return i;
7021}
Bram Moolenaar6d216452013-05-12 19:00:41 +02007022#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007023
7024/*
7025 * Return TRUE if "topfrp" and its children are at the right height.
7026 */
7027 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007028frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007029{
7030 frame_T *frp;
7031
7032 if (topfrp->fr_height != height)
7033 return FALSE;
7034
7035 if (topfrp->fr_layout == FR_ROW)
7036 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7037 if (frp->fr_height != height)
7038 return FALSE;
7039
7040 return TRUE;
7041}
7042
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007043/*
7044 * Return TRUE if "topfrp" and its children are at the right width.
7045 */
7046 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01007047frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007048{
7049 frame_T *frp;
7050
7051 if (topfrp->fr_width != width)
7052 return FALSE;
7053
7054 if (topfrp->fr_layout == FR_COL)
7055 for (frp = topfrp->fr_child; frp != NULL; frp = frp->fr_next)
7056 if (frp->fr_width != width)
7057 return FALSE;
7058
7059 return TRUE;
7060}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02007061
Bram Moolenaar86edef62016-03-13 18:07:30 +01007062#if defined(FEAT_EVAL) || defined(PROTO)
7063 int
7064win_getid(typval_T *argvars)
7065{
7066 int winnr;
7067 win_T *wp;
7068
7069 if (argvars[0].v_type == VAR_UNKNOWN)
7070 return curwin->w_id;
7071 winnr = get_tv_number(&argvars[0]);
7072 if (winnr > 0)
7073 {
7074 if (argvars[1].v_type == VAR_UNKNOWN)
7075 wp = firstwin;
7076 else
7077 {
7078 tabpage_T *tp;
7079 int tabnr = get_tv_number(&argvars[1]);
7080
Bram Moolenaar29323592016-07-24 22:04:11 +02007081 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007082 if (--tabnr == 0)
7083 break;
7084 if (tp == NULL)
7085 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01007086 if (tp == curtab)
7087 wp = firstwin;
7088 else
7089 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01007090 }
7091 for ( ; wp != NULL; wp = wp->w_next)
7092 if (--winnr == 0)
7093 return wp->w_id;
7094 }
7095 return 0;
7096}
7097
7098 int
7099win_gotoid(typval_T *argvars)
7100{
7101 win_T *wp;
7102 tabpage_T *tp;
7103 int id = get_tv_number(&argvars[0]);
7104
Bram Moolenaar29323592016-07-24 22:04:11 +02007105 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007106 if (wp->w_id == id)
7107 {
7108 goto_tabpage_win(tp, wp);
7109 return 1;
7110 }
7111 return 0;
7112}
7113
7114 void
7115win_id2tabwin(typval_T *argvars, list_T *list)
7116{
7117 win_T *wp;
7118 tabpage_T *tp;
7119 int winnr = 1;
7120 int tabnr = 1;
7121 int id = get_tv_number(&argvars[0]);
7122
Bram Moolenaar29323592016-07-24 22:04:11 +02007123 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007124 {
Bram Moolenaar29323592016-07-24 22:04:11 +02007125 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007126 {
7127 if (wp->w_id == id)
7128 {
7129 list_append_number(list, tabnr);
7130 list_append_number(list, winnr);
7131 return;
7132 }
7133 ++winnr;
7134 }
7135 ++tabnr;
7136 winnr = 1;
7137 }
7138 list_append_number(list, 0);
7139 list_append_number(list, 0);
7140}
7141
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02007142 win_T *
7143win_id2wp(typval_T *argvars)
7144{
7145 win_T *wp;
7146 tabpage_T *tp;
7147 int id = get_tv_number(&argvars[0]);
7148
7149 FOR_ALL_TAB_WINDOWS(tp, wp)
7150 if (wp->w_id == id)
7151 return wp;
7152
7153 return NULL;
7154}
7155
Bram Moolenaar86edef62016-03-13 18:07:30 +01007156 int
7157win_id2win(typval_T *argvars)
7158{
7159 win_T *wp;
7160 int nr = 1;
7161 int id = get_tv_number(&argvars[0]);
7162
Bram Moolenaar29323592016-07-24 22:04:11 +02007163 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01007164 {
7165 if (wp->w_id == id)
7166 return nr;
7167 ++nr;
7168 }
7169 return 0;
7170}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007171
7172 void
7173win_findbuf(typval_T *argvars, list_T *list)
7174{
7175 win_T *wp;
7176 tabpage_T *tp;
7177 int bufnr = get_tv_number(&argvars[0]);
7178
Bram Moolenaar29323592016-07-24 22:04:11 +02007179 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007180 if (wp->w_buffer->b_fnum == bufnr)
7181 list_append_number(list, wp->w_id);
7182}
7183
Bram Moolenaar86edef62016-03-13 18:07:30 +01007184#endif