blob: 6c278d9e5664860fb2a6bba9bc0339b8d4c8d004 [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 void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, long Prenum);
13static void win_init(win_T *newp, win_T *oldp, int flags);
14static void win_init_some(win_T *newp, win_T *oldp);
15static void frame_comp_pos(frame_T *topfrp, int *row, int *col);
16static void frame_setheight(frame_T *curfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010017static void frame_setwidth(frame_T *curfrp, int width);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010018static void win_exchange(long);
19static void win_rotate(int, int);
20static void win_totop(int size, int flags);
21static void win_equal_rec(win_T *next_curwin, int current, frame_T *topfr, int dir, int col, int row, int width, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010022static win_T *win_free_mem(win_T *win, int *dirp, tabpage_T *tp);
23static frame_T *win_altframe(win_T *win, tabpage_T *tp);
24static tabpage_T *alt_tabpage(void);
25static win_T *frame2win(frame_T *frp);
26static int frame_has_win(frame_T *frp, win_T *wp);
27static void frame_new_height(frame_T *topfrp, int height, int topfirst, int wfh);
28static int frame_fixed_height(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010029static int frame_fixed_width(frame_T *frp);
30static void frame_add_statusline(frame_T *frp);
31static void frame_new_width(frame_T *topfrp, int width, int leftfirst, int wfw);
32static void frame_add_vsep(frame_T *frp);
33static int frame_minwidth(frame_T *topfrp, win_T *next_curwin);
34static void frame_fix_width(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010035static int win_alloc_firstwin(win_T *oldwin);
36static void new_frame(win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010037static tabpage_T *alloc_tabpage(void);
38static int leave_tabpage(buf_T *new_curbuf, int trigger_leave_autocmds);
39static void enter_tabpage(tabpage_T *tp, buf_T *old_curbuf, int trigger_enter_autocmds, int trigger_leave_autocmds);
40static void frame_fix_height(win_T *wp);
41static int frame_minheight(frame_T *topfrp, win_T *next_curwin);
Bram Moolenaarc917da42016-07-19 22:31:36 +020042static 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 +010043static void win_free(win_T *wp, tabpage_T *tp);
44static void frame_append(frame_T *after, frame_T *frp);
45static void frame_insert(frame_T *before, frame_T *frp);
46static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static void win_goto_ver(int up, long count);
48static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010049static void frame_add_height(frame_T *frp, int n);
50static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void make_snapshot_rec(frame_T *fr, frame_T **frp);
53static void clear_snapshot(tabpage_T *tp, int idx);
54static void clear_snapshot_rec(frame_T *fr);
55static int check_snapshot_rec(frame_T *sn, frame_T *fr);
56static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010058static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010059static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020060
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000062
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000063#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
Bram Moolenaar4033c552017-09-16 20:54:51 +020065#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000066
67static char *m_onlyone = N_("Already only one window");
68
Bram Moolenaar071d4272004-06-13 20:20:40 +000069/*
Bram Moolenaar72e83c12019-02-22 16:09:52 +010070 * All CTRL-W window commands are handled here, called from normal_cmd().
Bram Moolenaar071d4272004-06-13 20:20:40 +000071 */
72 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +010073do_window(
74 int nchar,
75 long Prenum,
76 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077{
78 long Prenum1;
79 win_T *wp;
80#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
81 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +000082 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000083#endif
84#ifdef FEAT_FIND_ID
85 int type = FIND_DEFINE;
86 int len;
87#endif
88 char_u cbuf[40];
89
Bram Moolenaar882d02e2019-02-22 17:56:43 +010090 Prenum1 = Prenum == 0 ? 1 : Prenum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92#ifdef FEAT_CMDWIN
Bram Moolenaar6f470022018-04-10 18:47:20 +020093# define CHECK_CMDWIN \
94 do { \
95 if (cmdwin_type != 0) \
96 { \
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010097 emsg(_(e_cmdwin)); \
Bram Moolenaar6f470022018-04-10 18:47:20 +020098 return; \
99 } \
100 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200102# define CHECK_CMDWIN do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
104
105 switch (nchar)
106 {
107/* split current window in two parts, horizontally */
108 case 'S':
109 case Ctrl_S:
110 case 's':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200111 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000113#ifdef FEAT_QUICKFIX
114 /* When splitting the quickfix window open a new buffer in it,
115 * don't replicate the quickfix buffer. */
116 if (bt_quickfix(curbuf))
117 goto newwindow;
118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#ifdef FEAT_GUI
120 need_mouse_correct = TRUE;
121#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200122 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 break;
124
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125/* split current window in two parts, vertically */
126 case Ctrl_V:
127 case 'v':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200128 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100130#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000131 /* When splitting the quickfix window open a new buffer in it,
132 * don't replicate the quickfix buffer. */
133 if (bt_quickfix(curbuf))
134 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100135#endif
136#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100138#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200139 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141
142/* split current window and edit alternate file */
143 case Ctrl_HAT:
144 case '^':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200145 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100147
148 if (buflist_findnr(Prenum == 0
149 ? curwin->w_alt_fnum : Prenum) == NULL)
150 {
151 if (Prenum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100152 emsg(_(e_noalt));
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100153 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100154 semsg(_("E92: Buffer %ld not found"), Prenum);
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100155 break;
156 }
157
158 if (!curbuf_locked() && win_split(0, 0) == OK)
159 (void)buflist_getfile(
160 Prenum == 0 ? curwin->w_alt_fnum : Prenum,
161 (linenr_T)0, GETF_ALT, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 break;
163
164/* open new window */
165 case Ctrl_N:
166 case 'n':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200167 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000169#ifdef FEAT_QUICKFIX
170newwindow:
171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000173 /* window height */
174 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 else
176 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100177#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000178 if (nchar == 'v' || nchar == Ctrl_V)
179 STRCAT(cbuf, "v");
180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181 STRCAT(cbuf, "new");
182 do_cmdline_cmd(cbuf);
183 break;
184
185/* quit current window */
186 case Ctrl_Q:
187 case 'q':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100189 cmd_with_count("quit", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100190 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 break;
192
193/* close current window */
194 case Ctrl_C:
195 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100197 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100198 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199 break;
200
Bram Moolenaar4033c552017-09-16 20:54:51 +0200201#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202/* close preview window */
203 case Ctrl_Z:
204 case 'z':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200205 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 do_cmdline_cmd((char_u *)"pclose");
208 break;
209
210/* cursor to preview window */
211 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200212 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 if (wp->w_p_pvw)
214 break;
215 if (wp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100216 emsg(_("E441: There is no preview window"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217 else
218 win_goto(wp);
219 break;
220#endif
221
222/* close all but current window */
223 case Ctrl_O:
224 case 'o':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200225 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100227 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100228 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 break;
230
231/* cursor to next window with wrap around */
232 case Ctrl_W:
233 case 'w':
234/* cursor to previous window with wrap around */
235 case 'W':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200236 CHECK_CMDWIN;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100237 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 beep_flush();
239 else
240 {
241 if (Prenum) /* go to specified window */
242 {
243 for (wp = firstwin; --Prenum > 0; )
244 {
245 if (wp->w_next == NULL)
246 break;
247 else
248 wp = wp->w_next;
249 }
250 }
251 else
252 {
253 if (nchar == 'W') /* go to previous window */
254 {
255 wp = curwin->w_prev;
256 if (wp == NULL)
257 wp = lastwin; /* wrap around */
258 }
259 else /* go to next window */
260 {
261 wp = curwin->w_next;
262 if (wp == NULL)
263 wp = firstwin; /* wrap around */
264 }
265 }
266 win_goto(wp);
267 }
268 break;
269
270/* cursor to window below */
271 case 'j':
272 case K_DOWN:
273 case Ctrl_J:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200274 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 break;
277
278/* cursor to window above */
279 case 'k':
280 case K_UP:
281 case Ctrl_K:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200282 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 break;
285
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286/* cursor to left window */
287 case 'h':
288 case K_LEFT:
289 case Ctrl_H:
290 case K_BS:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200291 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 win_goto_hor(TRUE, Prenum1);
293 break;
294
295/* cursor to right window */
296 case 'l':
297 case K_RIGHT:
298 case Ctrl_L:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200299 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300 win_goto_hor(FALSE, Prenum1);
301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000303/* move window to new tab page */
304 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000305 if (one_window())
Bram Moolenaar32526b32019-01-19 17:43:09 +0100306 msg(_(m_onlyone));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000307 else
308 {
309 tabpage_T *oldtab = curtab;
310 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000311
312 /* First create a new tab with the window, then go back to
313 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000314 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000315 if (win_new_tabpage((int)Prenum) == OK
316 && valid_tabpage(oldtab))
317 {
318 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200319 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000320 if (curwin == wp)
321 win_close(curwin, FALSE);
322 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200323 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000324 }
325 }
326 break;
327
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328/* cursor to top-left window */
329 case 't':
330 case Ctrl_T:
331 win_goto(firstwin);
332 break;
333
334/* cursor to bottom-right window */
335 case 'b':
336 case Ctrl_B:
337 win_goto(lastwin);
338 break;
339
340/* cursor to last accessed (previous) window */
341 case 'p':
342 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200343 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 beep_flush();
345 else
346 win_goto(prevwin);
347 break;
348
349/* exchange current and next window */
350 case 'x':
351 case Ctrl_X:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200352 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353 win_exchange(Prenum);
354 break;
355
356/* rotate windows downwards */
357 case Ctrl_R:
358 case 'r':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200359 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 win_rotate(FALSE, (int)Prenum1); /* downwards */
362 break;
363
364/* rotate windows upwards */
365 case 'R':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200366 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 win_rotate(TRUE, (int)Prenum1); /* upwards */
369 break;
370
371/* move window to the very top/bottom/left/right */
372 case 'K':
373 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 case 'H':
375 case 'L':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200376 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 win_totop((int)Prenum,
378 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
379 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
380 break;
381
382/* make all windows the same height */
383 case '=':
384#ifdef FEAT_GUI
385 need_mouse_correct = TRUE;
386#endif
387 win_equal(NULL, FALSE, 'b');
388 break;
389
390/* increase current window height */
391 case '+':
392#ifdef FEAT_GUI
393 need_mouse_correct = TRUE;
394#endif
395 win_setheight(curwin->w_height + (int)Prenum1);
396 break;
397
398/* decrease current window height */
399 case '-':
400#ifdef FEAT_GUI
401 need_mouse_correct = TRUE;
402#endif
403 win_setheight(curwin->w_height - (int)Prenum1);
404 break;
405
406/* set current window height */
407 case Ctrl__:
408 case '_':
409#ifdef FEAT_GUI
410 need_mouse_correct = TRUE;
411#endif
412 win_setheight(Prenum ? (int)Prenum : 9999);
413 break;
414
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415/* increase current window width */
416 case '>':
417#ifdef FEAT_GUI
418 need_mouse_correct = TRUE;
419#endif
420 win_setwidth(curwin->w_width + (int)Prenum1);
421 break;
422
423/* decrease current window width */
424 case '<':
425#ifdef FEAT_GUI
426 need_mouse_correct = TRUE;
427#endif
428 win_setwidth(curwin->w_width - (int)Prenum1);
429 break;
430
431/* set current window width */
432 case '|':
433#ifdef FEAT_GUI
434 need_mouse_correct = TRUE;
435#endif
436 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438
439/* jump to tag and split window if tag exists (in preview window) */
440#if defined(FEAT_QUICKFIX)
441 case '}':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200442 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 if (Prenum)
444 g_do_tagpreview = Prenum;
445 else
446 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200448 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 case ']':
450 case Ctrl_RSB:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200451 CHECK_CMDWIN;
Bram Moolenaard355c502014-09-23 13:48:43 +0200452 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 if (Prenum)
454 postponed_split = Prenum;
455 else
456 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200457#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200458 if (nchar != '}')
459 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461
Bram Moolenaard355c502014-09-23 13:48:43 +0200462 /* Execute the command right here, required when "wincmd ]"
463 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 do_nv_ident(Ctrl_RSB, NUL);
465 break;
466
467#ifdef FEAT_SEARCHPATH
468/* edit file name under cursor in a new window */
469 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000470 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000472wingotofile:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200473 CHECK_CMDWIN;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000474
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000475 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 if (ptr != NULL)
477 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200478 tabpage_T *oldtab = curtab;
479 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000480# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000482# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000483 setpcmark();
484 if (win_split(0, 0) == OK)
485 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200486 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200487 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
488 ECMD_HIDE, NULL) == FAIL)
489 {
490 /* Failed to open the file, close the window
491 * opened for it. */
492 win_close(curwin, FALSE);
493 goto_tabpage_win(oldtab, oldwin);
494 }
495 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000496 {
497 curwin->w_cursor.lnum = lnum;
498 check_cursor_lnum();
499 beginline(BL_SOL | BL_FIX);
500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 }
502 vim_free(ptr);
503 }
504 break;
505#endif
506
507#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000508/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 * new window -- webb
510 */
511 case 'i': /* Go to any match */
512 case Ctrl_I:
513 type = FIND_ANY;
514 /* FALLTHROUGH */
515 case 'd': /* Go to definition, using 'define' */
516 case Ctrl_D:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200517 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
519 break;
520 find_pattern_in_path(ptr, 0, len, TRUE,
521 Prenum == 0 ? TRUE : FALSE, type,
522 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
523 curwin->w_set_curswant = TRUE;
524 break;
525#endif
526
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200527/* Quickfix window only: view the result under the cursor in a new split. */
528#if defined(FEAT_QUICKFIX)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000529 case K_KENTER:
530 case CAR:
Bram Moolenaar05159a02005-02-26 23:04:13 +0000531 if (bt_quickfix(curbuf))
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200532 qf_view_result(TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000533 break;
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200534#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000535
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536/* CTRL-W g extended commands */
537 case 'g':
538 case Ctrl_G:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200539 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540#ifdef USE_ON_FLY_SCROLL
541 dont_scroll = TRUE; /* disallow scrolling here */
542#endif
543 ++no_mapping;
544 ++allow_keys; /* no mapping for xchar, but allow key codes */
545 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000546 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 --no_mapping;
549 --allow_keys;
550#ifdef FEAT_CMDL_INFO
551 (void)add_to_showcmd(xchar);
552#endif
553 switch (xchar)
554 {
555#if defined(FEAT_QUICKFIX)
556 case '}':
557 xchar = Ctrl_RSB;
558 if (Prenum)
559 g_do_tagpreview = Prenum;
560 else
561 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200563 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 case ']':
565 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200566 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 if (Prenum)
568 postponed_split = Prenum;
569 else
570 postponed_split = -1;
571
572 /* Execute the command right here, required when
573 * "wincmd g}" was used in a function. */
574 do_nv_ident('g', xchar);
575 break;
576
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000577#ifdef FEAT_SEARCHPATH
578 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000579 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100580 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000581 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000582 goto wingotofile;
583#endif
Bram Moolenaar72e83c12019-02-22 16:09:52 +0100584 case 't': // CTRL-W gt: go to next tab page
585 goto_tabpage((int)Prenum);
586 break;
587
Bram Moolenaar882d02e2019-02-22 17:56:43 +0100588 case 'T': // CTRL-W gT: go to previous tab page
589 goto_tabpage(-(int)Prenum1);
590 break;
591
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 default:
593 beep_flush();
594 break;
595 }
596 break;
597
598 default: beep_flush();
599 break;
600 }
601}
602
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100603/*
Bram Moolenaarb7316892019-05-01 18:08:42 +0200604 * Figure out the address type for ":wincmd".
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100605 */
606 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100607get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100608{
609 switch (*arg)
610 {
611 case 'S':
612 case Ctrl_S:
613 case 's':
614 case Ctrl_N:
615 case 'n':
616 case 'j':
617 case Ctrl_J:
618 case 'k':
619 case Ctrl_K:
620 case 'T':
621 case Ctrl_R:
622 case 'r':
623 case 'R':
624 case 'K':
625 case 'J':
626 case '+':
627 case '-':
628 case Ctrl__:
629 case '_':
630 case '|':
631 case ']':
632 case Ctrl_RSB:
633 case 'g':
634 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100635 case Ctrl_V:
636 case 'v':
637 case 'h':
638 case Ctrl_H:
639 case 'l':
640 case Ctrl_L:
641 case 'H':
642 case 'L':
643 case '>':
644 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100645#if defined(FEAT_QUICKFIX)
646 case '}':
647#endif
648#ifdef FEAT_SEARCHPATH
649 case 'f':
650 case 'F':
651 case Ctrl_F:
652#endif
653#ifdef FEAT_FIND_ID
654 case 'i':
655 case Ctrl_I:
656 case 'd':
657 case Ctrl_D:
658#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +0200659 // window size or any count
660 eap->addr_type = ADDR_OTHER;
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100661 break;
662
663 case Ctrl_HAT:
664 case '^':
Bram Moolenaarb7316892019-05-01 18:08:42 +0200665 // buffer number
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100666 eap->addr_type = ADDR_BUFFERS;
667 break;
668
669 case Ctrl_Q:
670 case 'q':
671 case Ctrl_C:
672 case 'c':
673 case Ctrl_O:
674 case 'o':
675 case Ctrl_W:
676 case 'w':
677 case 'W':
678 case 'x':
679 case Ctrl_X:
Bram Moolenaarb7316892019-05-01 18:08:42 +0200680 // window number
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100681 eap->addr_type = ADDR_WINDOWS;
682 break;
683
684#if defined(FEAT_QUICKFIX)
685 case Ctrl_Z:
686 case 'z':
687 case 'P':
688#endif
689 case 't':
690 case Ctrl_T:
691 case 'b':
692 case Ctrl_B:
693 case 'p':
694 case Ctrl_P:
695 case '=':
696 case CAR:
Bram Moolenaarb7316892019-05-01 18:08:42 +0200697 // no count
698 eap->addr_type = ADDR_NONE;
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100699 break;
700 }
701}
702
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100703 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100704cmd_with_count(
705 char *cmd,
706 char_u *bufp,
707 size_t bufsize,
708 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100709{
710 size_t len = STRLEN(cmd);
711
712 STRCPY(bufp, cmd);
713 if (Prenum > 0)
714 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
715}
716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717/*
718 * split the current window, implements CTRL-W s and :split
719 *
720 * "size" is the height or width for the new window, 0 to use half of current
721 * height or width.
722 *
723 * "flags":
724 * WSP_ROOM: require enough room for new window
725 * WSP_VERT: vertical split.
726 * WSP_TOP: open window at the top-left of the shell (help window).
727 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
728 * WSP_HELP: creating the help window, keep layout snapshot
729 *
730 * return FAIL for failure, OK otherwise
731 */
732 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100733win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000735 /* When the ":tab" modifier was used open a new tab page instead. */
736 if (may_open_tabpage() == OK)
737 return OK;
738
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 /* Add flags from ":vertical", ":topleft" and ":botright". */
740 flags |= cmdmod.split;
741 if ((flags & WSP_TOP) && (flags & WSP_BOT))
742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100743 emsg(_("E442: Can't split topleft and botright at the same time"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 return FAIL;
745 }
746
747 /* When creating the help window make a snapshot of the window layout.
748 * Otherwise clear the snapshot, it's now invalid. */
749 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000750 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000752 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753
754 return win_split_ins(size, flags, NULL, 0);
755}
756
757/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100758 * When "new_wp" is NULL: split the current window in two.
759 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * top/left/right/bottom.
761 * return FAIL for failure, OK otherwise
762 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000763 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100764win_split_ins(
765 int size,
766 int flags,
767 win_T *new_wp,
768 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100770 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 win_T *oldwin;
772 int new_size = size;
773 int i;
774 int need_status = 0;
775 int do_equal = FALSE;
776 int needed;
777 int available;
778 int oldwin_height = 0;
779 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200780 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200782 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200783 int wmh1;
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200784 int did_set_fraction = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
786 if (flags & WSP_TOP)
787 oldwin = firstwin;
788 else if (flags & WSP_BOT)
789 oldwin = lastwin;
790 else
791 oldwin = curwin;
792
793 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100794 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100796 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100798 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 return FAIL;
800 }
801 need_status = STATUS_HEIGHT;
802 }
803
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000804#ifdef FEAT_GUI
805 /* May be needed for the scrollbars that are going to change. */
806 if (gui.in_use)
807 out_flush();
808#endif
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 if (flags & WSP_VERT)
811 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200812 int wmw1;
813 int minwidth;
814
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817 /*
818 * Check if we are able to split the current window and compute its
819 * width.
820 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200821 /* Current window requires at least 1 space. */
822 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
823 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200825 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200826 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200828 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200830 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200832 else if (p_ea)
833 {
834 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
835 prevfrp = oldwin->w_frame;
836 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
837 frp = frp->fr_parent)
838 {
839 if (frp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +0100840 FOR_ALL_FRAMES(frp2, frp->fr_child)
Bram Moolenaar54368f22014-07-23 15:21:20 +0200841 if (frp2 != prevfrp)
842 minwidth += frame_minwidth(frp2, NOWIN);
843 prevfrp = frp;
844 }
845 available = topframe->fr_width;
846 needed += minwidth;
847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200849 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200850 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
851 available = oldwin->w_frame->fr_width;
852 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200853 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100854 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100856 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 return FAIL;
858 }
859 if (new_size == 0)
860 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200861 if (new_size > available - minwidth - 1)
862 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200863 if (new_size < wmw1)
864 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865
866 /* if it doesn't fit in the current window, need win_equal() */
867 if (oldwin->w_width - new_size - 1 < p_wmw)
868 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000869
870 /* We don't like to take lines for the new window from a
871 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100872 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000873 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100874 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000875
876 /* Only make all windows the same width if one of them (except oldwin)
877 * is wider than one of the split windows. */
878 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
879 && oldwin->w_frame->fr_parent != NULL)
880 {
881 frp = oldwin->w_frame->fr_parent->fr_child;
882 while (frp != NULL)
883 {
884 if (frp->fr_win != oldwin && frp->fr_win != NULL
885 && (frp->fr_win->w_width > new_size
886 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100887 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000888 {
889 do_equal = TRUE;
890 break;
891 }
892 frp = frp->fr_next;
893 }
894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
896 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 {
898 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900 /*
901 * Check if we are able to split the current window and compute its
902 * height.
903 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200904 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100905 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200906 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200908 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200909 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200911 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200913 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200915 else if (p_ea)
916 {
917 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
918 prevfrp = oldwin->w_frame;
919 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
920 frp = frp->fr_parent)
921 {
922 if (frp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +0100923 FOR_ALL_FRAMES(frp2, frp->fr_child)
Bram Moolenaar54368f22014-07-23 15:21:20 +0200924 if (frp2 != prevfrp)
925 minheight += frame_minheight(frp2, NOWIN);
926 prevfrp = frp;
927 }
928 available = topframe->fr_height;
929 needed += minheight;
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 else
932 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200933 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
934 available = oldwin->w_frame->fr_height;
935 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100937 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100939 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 return FAIL;
941 }
942 oldwin_height = oldwin->w_height;
943 if (need_status)
944 {
945 oldwin->w_status_height = STATUS_HEIGHT;
946 oldwin_height -= STATUS_HEIGHT;
947 }
948 if (new_size == 0)
949 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200950 if (new_size > available - minheight - STATUS_HEIGHT)
951 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200952 if (new_size < wmh1)
953 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954
955 /* if it doesn't fit in the current window, need win_equal() */
956 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
957 do_equal = TRUE;
958
959 /* We don't like to take lines for the new window from a
960 * 'winfixheight' window. Take them from a window above or below
961 * instead, if possible. */
962 if (oldwin->w_p_wfh)
963 {
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200964 /* Set w_fraction now so that the cursor keeps the same relative
965 * vertical position using the old height. */
966 set_fraction(oldwin);
967 did_set_fraction = TRUE;
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
970 oldwin);
971 oldwin_height = oldwin->w_height;
972 if (need_status)
973 oldwin_height -= STATUS_HEIGHT;
974 }
Bram Moolenaar67f71312007-08-12 14:55:56 +0000975
976 /* Only make all windows the same height if one of them (except oldwin)
977 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100978 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +0000979 && oldwin->w_frame->fr_parent != NULL)
980 {
981 frp = oldwin->w_frame->fr_parent->fr_child;
982 while (frp != NULL)
983 {
984 if (frp->fr_win != oldwin && frp->fr_win != NULL
985 && (frp->fr_win->w_height > new_size
986 || frp->fr_win->w_height > oldwin_height - new_size
987 - STATUS_HEIGHT))
988 {
989 do_equal = TRUE;
990 break;
991 }
992 frp = frp->fr_next;
993 }
994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996
997 /*
998 * allocate new window structure and link it in the window list
999 */
1000 if ((flags & WSP_TOP) == 0
1001 && ((flags & WSP_BOT)
1002 || (flags & WSP_BELOW)
1003 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001004 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 {
1006 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001007 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001008 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 else
1010 win_append(oldwin, wp);
1011 }
1012 else
1013 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001014 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001015 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 else
1017 win_append(oldwin->w_prev, wp);
1018 }
1019
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001020 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 {
1022 if (wp == NULL)
1023 return FAIL;
1024
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001025 new_frame(wp);
1026 if (wp->w_frame == NULL)
1027 {
1028 win_free(wp, NULL);
1029 return FAIL;
1030 }
1031
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001032 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001033 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 }
1035
1036 /*
1037 * Reorganise the tree of frames to insert the new window.
1038 */
1039 if (flags & (WSP_TOP | WSP_BOT))
1040 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1042 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 {
1044 curfrp = topframe->fr_child;
1045 if (flags & WSP_BOT)
1046 while (curfrp->fr_next != NULL)
1047 curfrp = curfrp->fr_next;
1048 }
1049 else
1050 curfrp = topframe;
1051 before = (flags & WSP_TOP);
1052 }
1053 else
1054 {
1055 curfrp = oldwin->w_frame;
1056 if (flags & WSP_BELOW)
1057 before = FALSE;
1058 else if (flags & WSP_ABOVE)
1059 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001060 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 before = !p_spr;
1062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 before = !p_sb;
1064 }
1065 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1066 {
1067 /* Need to create a new frame in the tree to make a branch. */
1068 frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
1069 *frp = *curfrp;
1070 curfrp->fr_layout = layout;
1071 frp->fr_parent = curfrp;
1072 frp->fr_next = NULL;
1073 frp->fr_prev = NULL;
1074 curfrp->fr_child = frp;
1075 curfrp->fr_win = NULL;
1076 curfrp = frp;
1077 if (frp->fr_win != NULL)
1078 oldwin->w_frame = frp;
1079 else
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001080 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 frp->fr_parent = curfrp;
1082 }
1083
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001084 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001085 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001087 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 frp->fr_parent = curfrp->fr_parent;
1089
1090 /* Insert the new frame at the right place in the frame list. */
1091 if (before)
1092 frame_insert(curfrp, frp);
1093 else
1094 frame_append(curfrp, frp);
1095
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001096 /* Set w_fraction now so that the cursor keeps the same relative
1097 * vertical position. */
Bram Moolenaar98da6ec2018-04-13 22:15:46 +02001098 if (!did_set_fraction)
1099 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001100 wp->w_fraction = oldwin->w_fraction;
1101
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (flags & WSP_VERT)
1103 {
1104 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001105
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 if (need_status)
1107 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001108 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 oldwin->w_status_height = need_status;
1110 }
1111 if (flags & (WSP_TOP | WSP_BOT))
1112 {
1113 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001114 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001115 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001116 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 wp->w_status_height = (p_ls > 0);
1118 }
1119 else
1120 {
1121 /* height and row of new window is same as current window */
1122 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001123 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 wp->w_status_height = oldwin->w_status_height;
1125 }
1126 frp->fr_height = curfrp->fr_height;
1127
1128 /* "new_size" of the current window goes to the new window, use
1129 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001130 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 if (before)
1132 wp->w_vsep_width = 1;
1133 else
1134 {
1135 wp->w_vsep_width = oldwin->w_vsep_width;
1136 oldwin->w_vsep_width = 1;
1137 }
1138 if (flags & (WSP_TOP | WSP_BOT))
1139 {
1140 if (flags & WSP_BOT)
1141 frame_add_vsep(curfrp);
1142 /* Set width of neighbor frame */
1143 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001144 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1145 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 }
1147 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001148 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (before) /* new window left of current one */
1150 {
1151 wp->w_wincol = oldwin->w_wincol;
1152 oldwin->w_wincol += new_size + 1;
1153 }
1154 else /* new window right of current one */
1155 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1156 frame_fix_width(oldwin);
1157 frame_fix_width(wp);
1158 }
1159 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 {
1161 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 if (flags & (WSP_TOP | WSP_BOT))
1163 {
1164 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001165 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 wp->w_vsep_width = 0;
1167 }
1168 else
1169 {
1170 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001171 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 wp->w_vsep_width = oldwin->w_vsep_width;
1173 }
1174 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176 /* "new_size" of the current window goes to the new window, use
1177 * one row for the status line */
1178 win_new_height(wp, new_size);
1179 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001180 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001181 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001182 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001183
1184 if (!((flags & WSP_BOT) && p_ls == 0))
1185 new_fr_height -= STATUS_HEIGHT;
1186 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 else
1189 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1190 if (before) /* new window above current one */
1191 {
1192 wp->w_winrow = oldwin->w_winrow;
1193 wp->w_status_height = STATUS_HEIGHT;
1194 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1195 }
1196 else /* new window below current one */
1197 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001198 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1199 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001201 if (!(flags & WSP_BOT))
1202 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 if (flags & WSP_BOT)
1205 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 frame_fix_height(wp);
1207 frame_fix_height(oldwin);
1208 }
1209
1210 if (flags & (WSP_TOP | WSP_BOT))
1211 (void)win_comp_pos();
1212
1213 /*
1214 * Both windows need redrawing
1215 */
1216 redraw_win_later(wp, NOT_VALID);
1217 wp->w_redr_status = TRUE;
1218 redraw_win_later(oldwin, NOT_VALID);
1219 oldwin->w_redr_status = TRUE;
1220
1221 if (need_status)
1222 {
1223 msg_row = Rows - 1;
1224 msg_col = sc_col;
1225 msg_clr_eos_force(); /* Old command/ruler may still be there */
1226 comp_col();
1227 msg_row = Rows - 1;
1228 msg_col = 0; /* put position back at start of line */
1229 }
1230
1231 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001232 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 */
1234 if (do_equal || dir != 0)
1235 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001237 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
1239 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1240 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (flags & WSP_VERT)
1242 {
1243 i = p_wiw;
1244 if (size != 0)
1245 p_wiw = size;
1246
1247# ifdef FEAT_GUI
1248 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1249 if (gui.in_use)
1250 gui_init_which_components(NULL);
1251# endif
1252 }
1253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
1255 i = p_wh;
1256 if (size != 0)
1257 p_wh = size;
1258 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001259
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001260#ifdef FEAT_JUMPLIST
1261 /* Keep same changelist position in new window. */
1262 wp->w_changelistidx = oldwin->w_changelistidx;
1263#endif
1264
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001265 /*
1266 * make the new window the current window
1267 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001268 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (flags & WSP_VERT)
1270 p_wiw = i;
1271 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 p_wh = i;
1273
1274 return OK;
1275}
1276
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001277
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001278/*
1279 * Initialize window "newp" from window "oldp".
1280 * Used when splitting a window and when creating a new tab page.
1281 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001282 * WSP_NEWLOC may be specified in flags to prevent the location list from
1283 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001284 */
1285 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001286win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001287{
1288 int i;
1289
1290 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001291#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001292 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001293#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001294 oldp->w_buffer->b_nwindows++;
1295 newp->w_cursor = oldp->w_cursor;
1296 newp->w_valid = 0;
1297 newp->w_curswant = oldp->w_curswant;
1298 newp->w_set_curswant = oldp->w_set_curswant;
1299 newp->w_topline = oldp->w_topline;
1300#ifdef FEAT_DIFF
1301 newp->w_topfill = oldp->w_topfill;
1302#endif
1303 newp->w_leftcol = oldp->w_leftcol;
1304 newp->w_pcmark = oldp->w_pcmark;
1305 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1306 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001307 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001308 newp->w_fraction = oldp->w_fraction;
1309 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1310#ifdef FEAT_JUMPLIST
1311 copy_jumplist(oldp, newp);
1312#endif
1313#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001314 if (flags & WSP_NEWLOC)
1315 {
1316 /* Don't copy the location list. */
1317 newp->w_llist = NULL;
1318 newp->w_llist_ref = NULL;
1319 }
1320 else
Bram Moolenaar09037502018-09-25 22:08:14 +02001321 copy_loclist_stack(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001322#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001323 newp->w_localdir = (oldp->w_localdir == NULL)
1324 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001325
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001326 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001327 for (i = 0; i < oldp->w_tagstacklen; i++)
1328 {
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02001329 taggy_T *tag = &newp->w_tagstack[i];
1330 *tag = oldp->w_tagstack[i];
1331 if (tag->tagname != NULL)
1332 tag->tagname = vim_strsave(tag->tagname);
1333 if (tag->user_data != NULL)
1334 tag->user_data = vim_strsave(tag->user_data);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001335 }
1336 newp->w_tagstackidx = oldp->w_tagstackidx;
1337 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001338#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001339 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001340#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001341
1342 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001343
Bram Moolenaara971b822011-09-14 14:43:25 +02001344#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001345 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001346#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001347}
1348
1349/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001350 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001351 * Only the essential things are copied.
1352 */
1353 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001354win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001355{
1356 /* Use the same argument list. */
1357 newp->w_alist = oldp->w_alist;
1358 ++newp->w_alist->al_refcount;
1359 newp->w_arg_idx = oldp->w_arg_idx;
1360
1361 /* copy options from existing window */
1362 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001363}
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001367 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 */
1369 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001370win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371{
1372 win_T *wp;
1373
1374 if (win == NULL)
1375 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001376 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 if (wp == win)
1378 return TRUE;
1379 return FALSE;
1380}
1381
1382/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001383 * Check if "win" is a pointer to an existing window in any tab page.
1384 */
1385 int
1386win_valid_any_tab(win_T *win)
1387{
1388 win_T *wp;
1389 tabpage_T *tp;
1390
1391 if (win == NULL)
1392 return FALSE;
1393 FOR_ALL_TABPAGES(tp)
1394 {
1395 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1396 {
1397 if (wp == win)
1398 return TRUE;
1399 }
1400 }
1401 return FALSE;
1402}
1403
1404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 * Return the number of windows.
1406 */
1407 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001408win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 win_T *wp;
1411 int count = 0;
1412
Bram Moolenaar29323592016-07-24 22:04:11 +02001413 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 ++count;
1415 return count;
1416}
1417
1418/*
1419 * Make "count" windows on the screen.
1420 * Return actual number of windows on the screen.
1421 * Must be called when there is just one window, filling the whole screen
1422 * (excluding the command line).
1423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001425make_windows(
1426 int count,
1427 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
1429 int maxcount;
1430 int todo;
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (vertical)
1433 {
1434 /* Each windows needs at least 'winminwidth' lines and a separator
1435 * column. */
1436 maxcount = (curwin->w_width + curwin->w_vsep_width
1437 - (p_wiw - p_wmw)) / (p_wmw + 1);
1438 }
1439 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 {
1441 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001442 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1444 }
1445
1446 if (maxcount < 2)
1447 maxcount = 2;
1448 if (count > maxcount)
1449 count = maxcount;
1450
1451 /*
1452 * add status line now, otherwise first window will be too big
1453 */
1454 if (count > 1)
1455 last_status(TRUE);
1456
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 /*
1458 * Don't execute autocommands while creating the windows. Must do that
1459 * when putting the buffers in the windows.
1460 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001461 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462
1463 /* todo is number of windows left to create */
1464 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 if (vertical)
1466 {
1467 if (win_split(curwin->w_width - (curwin->w_width - todo)
1468 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1469 break;
1470 }
1471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 {
1473 if (win_split(curwin->w_height - (curwin->w_height - todo
1474 * STATUS_HEIGHT) / (todo + 1)
1475 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1476 break;
1477 }
1478
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001479 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
1481 /* return actual number of windows */
1482 return (count - todo);
1483}
1484
1485/*
1486 * Exchange current and next window
1487 */
1488 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001489win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 frame_T *frp;
1492 frame_T *frp2;
1493 win_T *wp;
1494 win_T *wp2;
1495 int temp;
1496
Bram Moolenaar459ca562016-11-10 18:16:33 +01001497 if (ONE_WINDOW) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 {
1499 beep_flush();
1500 return;
1501 }
1502
1503#ifdef FEAT_GUI
1504 need_mouse_correct = TRUE;
1505#endif
1506
1507 /*
1508 * find window to exchange with
1509 */
1510 if (Prenum)
1511 {
1512 frp = curwin->w_frame->fr_parent->fr_child;
1513 while (frp != NULL && --Prenum > 0)
1514 frp = frp->fr_next;
1515 }
1516 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1517 frp = curwin->w_frame->fr_next;
1518 else /* Swap last window in row/col with previous */
1519 frp = curwin->w_frame->fr_prev;
1520
1521 /* We can only exchange a window with another window, not with a frame
1522 * containing windows. */
1523 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1524 return;
1525 wp = frp->fr_win;
1526
1527/*
1528 * 1. remove curwin from the list. Remember after which window it was in wp2
1529 * 2. insert curwin before wp in the list
1530 * if wp != wp2
1531 * 3. remove wp from the list
1532 * 4. insert wp after wp2
1533 * 5. exchange the status line height and vsep width.
1534 */
1535 wp2 = curwin->w_prev;
1536 frp2 = curwin->w_frame->fr_prev;
1537 if (wp->w_prev != curwin)
1538 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001539 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 frame_remove(curwin->w_frame);
1541 win_append(wp->w_prev, curwin);
1542 frame_insert(frp, curwin->w_frame);
1543 }
1544 if (wp != wp2)
1545 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001546 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 frame_remove(wp->w_frame);
1548 win_append(wp2, wp);
1549 if (frp2 == NULL)
1550 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1551 else
1552 frame_append(frp2, wp->w_frame);
1553 }
1554 temp = curwin->w_status_height;
1555 curwin->w_status_height = wp->w_status_height;
1556 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 temp = curwin->w_vsep_width;
1558 curwin->w_vsep_width = wp->w_vsep_width;
1559 wp->w_vsep_width = temp;
1560
1561 /* If the windows are not in the same frame, exchange the sizes to avoid
1562 * messing up the window layout. Otherwise fix the frame sizes. */
1563 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1564 {
1565 temp = curwin->w_height;
1566 curwin->w_height = wp->w_height;
1567 wp->w_height = temp;
1568 temp = curwin->w_width;
1569 curwin->w_width = wp->w_width;
1570 wp->w_width = temp;
1571 }
1572 else
1573 {
1574 frame_fix_height(curwin);
1575 frame_fix_height(wp);
1576 frame_fix_width(curwin);
1577 frame_fix_width(wp);
1578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 (void)win_comp_pos(); /* recompute window positions */
1581
1582 win_enter(wp, TRUE);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001583 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584}
1585
1586/*
1587 * rotate windows: if upwards TRUE the second window becomes the first one
1588 * if upwards FALSE the first window becomes the second one
1589 */
1590 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001591win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592{
1593 win_T *wp1;
1594 win_T *wp2;
1595 frame_T *frp;
1596 int n;
1597
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001598 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 {
1600 beep_flush();
1601 return;
1602 }
1603
1604#ifdef FEAT_GUI
1605 need_mouse_correct = TRUE;
1606#endif
1607
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 /* Check if all frames in this row/col have one window. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001609 FOR_ALL_FRAMES(frp, curwin->w_frame->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 if (frp->fr_win == NULL)
1611 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001612 emsg(_("E443: Cannot rotate when another window is split"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 return;
1614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 while (count--)
1617 {
1618 if (upwards) /* first window becomes last window */
1619 {
1620 /* remove first window/frame from the list */
1621 frp = curwin->w_frame->fr_parent->fr_child;
1622 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001623 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 frame_remove(frp);
1625
1626 /* find last frame and append removed window/frame after it */
1627 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1628 ;
1629 win_append(frp->fr_win, wp1);
1630 frame_append(frp, wp1->w_frame);
1631
1632 wp2 = frp->fr_win; /* previously last window */
1633 }
1634 else /* last window becomes first window */
1635 {
1636 /* find last window/frame in the list and remove it */
1637 for (frp = curwin->w_frame; frp->fr_next != NULL;
1638 frp = frp->fr_next)
1639 ;
1640 wp1 = frp->fr_win;
1641 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001642 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 frame_remove(frp);
1644
1645 /* append the removed window/frame before the first in the list */
1646 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1647 frame_insert(frp->fr_parent->fr_child, frp);
1648 }
1649
1650 /* exchange status height and vsep width of old and new last window */
1651 n = wp2->w_status_height;
1652 wp2->w_status_height = wp1->w_status_height;
1653 wp1->w_status_height = n;
1654 frame_fix_height(wp1);
1655 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 n = wp2->w_vsep_width;
1657 wp2->w_vsep_width = wp1->w_vsep_width;
1658 wp1->w_vsep_width = n;
1659 frame_fix_width(wp1);
1660 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001662 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 (void)win_comp_pos();
1664 }
1665
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001666 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
1669/*
1670 * Move the current window to the very top/bottom/left/right of the screen.
1671 */
1672 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001673win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674{
1675 int dir;
1676 int height = curwin->w_height;
1677
Bram Moolenaar459ca562016-11-10 18:16:33 +01001678 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 {
1680 beep_flush();
1681 return;
1682 }
1683
1684 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001685 (void)winframe_remove(curwin, &dir, NULL);
1686 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 last_status(FALSE); /* may need to remove last status line */
1688 (void)win_comp_pos(); /* recompute window positions */
1689
1690 /* Split a window on the desired side and put the window there. */
1691 (void)win_split_ins(size, flags, curwin, dir);
1692 if (!(flags & WSP_VERT))
1693 {
1694 win_setheight(height);
1695 if (p_ea)
1696 win_equal(curwin, TRUE, 'v');
1697 }
1698
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001699#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1701 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001702 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704}
1705
1706/*
1707 * Move window "win1" to below/right of "win2" and make "win1" the current
1708 * window. Only works within the same frame!
1709 */
1710 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001711win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712{
1713 int height;
1714
1715 /* check if the arguments are reasonable */
1716 if (win1 == win2)
1717 return;
1718
1719 /* check if there is something to do */
1720 if (win2->w_next != win1)
1721 {
1722 /* may need move the status line/vertical separator of the last window
1723 * */
1724 if (win1 == lastwin)
1725 {
1726 height = win1->w_prev->w_status_height;
1727 win1->w_prev->w_status_height = win1->w_status_height;
1728 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001729 if (win1->w_prev->w_vsep_width == 1)
1730 {
1731 /* Remove the vertical separator from the last-but-one window,
1732 * add it to the last window. Adjust the frame widths. */
1733 win1->w_prev->w_vsep_width = 0;
1734 win1->w_prev->w_frame->fr_width -= 1;
1735 win1->w_vsep_width = 1;
1736 win1->w_frame->fr_width += 1;
1737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 }
1739 else if (win2 == lastwin)
1740 {
1741 height = win1->w_status_height;
1742 win1->w_status_height = win2->w_status_height;
1743 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001744 if (win1->w_vsep_width == 1)
1745 {
1746 /* Remove the vertical separator from win1, add it to the last
1747 * window, win2. Adjust the frame widths. */
1748 win2->w_vsep_width = 1;
1749 win2->w_frame->fr_width += 1;
1750 win1->w_vsep_width = 0;
1751 win1->w_frame->fr_width -= 1;
1752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001754 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 frame_remove(win1->w_frame);
1756 win_append(win2, win1);
1757 frame_append(win2->w_frame, win1->w_frame);
1758
1759 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1760 redraw_later(NOT_VALID);
1761 }
1762 win_enter(win1, FALSE);
1763}
1764
1765/*
1766 * Make all windows the same height.
1767 * 'next_curwin' will soon be the current window, make sure it has enough
1768 * rows.
1769 */
1770 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001771win_equal(
1772 win_T *next_curwin, /* pointer to current window to be or NULL */
1773 int current, /* do only frame with current window */
1774 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 'b' for both, 0 for using p_ead */
1776{
1777 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001780 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001781 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782}
1783
1784/*
1785 * Set a frame to a new position and height, spreading the available room
1786 * equally over contained frames.
1787 * The window "next_curwin" (if not NULL) should at least get the size from
1788 * 'winheight' and 'winwidth' if possible.
1789 */
1790 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001791win_equal_rec(
1792 win_T *next_curwin, /* pointer to current window to be or NULL */
1793 int current, /* do only frame with current window */
1794 frame_T *topfr, /* frame to set size off */
1795 int dir, /* 'v', 'h' or 'b', see win_equal() */
1796 int col, /* horizontal position for frame */
1797 int row, /* vertical position for frame */
1798 int width, /* new width of frame */
1799 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800{
1801 int n, m;
1802 int extra_sep = 0;
1803 int wincount, totwincount = 0;
1804 frame_T *fr;
1805 int next_curwin_size = 0;
1806 int room = 0;
1807 int new_size;
1808 int has_next_curwin = 0;
1809 int hnc;
1810
1811 if (topfr->fr_layout == FR_LEAF)
1812 {
1813 /* Set the width/height of this frame.
1814 * Redraw when size or position changes */
1815 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 )
1818 {
1819 topfr->fr_win->w_winrow = row;
1820 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001822 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001823 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 }
1825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 else if (topfr->fr_layout == FR_ROW)
1827 {
1828 topfr->fr_width = width;
1829 topfr->fr_height = height;
1830
1831 if (dir != 'v') /* equalize frame widths */
1832 {
1833 /* Compute the maximum number of windows horizontally in this
1834 * frame. */
1835 n = frame_minwidth(topfr, NOWIN);
1836 /* add one for the rightmost window, it doesn't have a separator */
1837 if (col + width == Columns)
1838 extra_sep = 1;
1839 else
1840 extra_sep = 0;
1841 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001842 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001844 /*
1845 * Compute width for "next_curwin" window and room available for
1846 * other windows.
1847 * "m" is the minimal width when counting p_wiw for "next_curwin".
1848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 m = frame_minwidth(topfr, next_curwin);
1850 room = width - m;
1851 if (room < 0)
1852 {
1853 next_curwin_size = p_wiw + room;
1854 room = 0;
1855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001858 next_curwin_size = -1;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001859 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001860 {
1861 /* If 'winfixwidth' set keep the window width if
1862 * possible.
1863 * Watch out for this window being the next_curwin. */
1864 if (frame_fixed_width(fr))
1865 {
1866 n = frame_minwidth(fr, NOWIN);
1867 new_size = fr->fr_width;
1868 if (frame_has_win(fr, next_curwin))
1869 {
1870 room += p_wiw - p_wmw;
1871 next_curwin_size = 0;
1872 if (new_size < p_wiw)
1873 new_size = p_wiw;
1874 }
1875 else
1876 /* These windows don't use up room. */
1877 totwincount -= (n + (fr->fr_next == NULL
1878 ? extra_sep : 0)) / (p_wmw + 1);
1879 room -= new_size - n;
1880 if (room < 0)
1881 {
1882 new_size += room;
1883 room = 0;
1884 }
1885 fr->fr_newwidth = new_size;
1886 }
1887 }
1888 if (next_curwin_size == -1)
1889 {
1890 if (!has_next_curwin)
1891 next_curwin_size = 0;
1892 else if (totwincount > 1
1893 && (room + (totwincount - 2))
1894 / (totwincount - 1) > p_wiw)
1895 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001896 /* Can make all windows wider than 'winwidth', spread
1897 * the room equally. */
1898 next_curwin_size = (room + p_wiw
1899 + (totwincount - 1) * p_wmw
1900 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001901 room -= next_curwin_size - p_wiw;
1902 }
1903 else
1904 next_curwin_size = p_wiw;
1905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001907
1908 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 --totwincount; /* don't count curwin */
1910 }
1911
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001912 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 wincount = 1;
1915 if (fr->fr_next == NULL)
1916 /* last frame gets all that remains (avoid roundoff error) */
1917 new_size = width;
1918 else if (dir == 'v')
1919 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001920 else if (frame_fixed_width(fr))
1921 {
1922 new_size = fr->fr_newwidth;
1923 wincount = 0; /* doesn't count as a sizeable window */
1924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 else
1926 {
1927 /* Compute the maximum number of windows horiz. in "fr". */
1928 n = frame_minwidth(fr, NOWIN);
1929 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
1930 / (p_wmw + 1);
1931 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001932 if (has_next_curwin)
1933 hnc = frame_has_win(fr, next_curwin);
1934 else
1935 hnc = FALSE;
1936 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001938 if (totwincount == 0)
1939 new_size = room;
1940 else
1941 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001943 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 next_curwin_size -= p_wiw - (m - n);
1946 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001947 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001949 else
1950 room -= new_size;
1951 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001954 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 * window, unless equalizing all frames. */
1956 if (!current || dir != 'v' || topfr->fr_parent != NULL
1957 || (new_size != fr->fr_width)
1958 || frame_has_win(fr, next_curwin))
1959 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001960 new_size, height);
1961 col += new_size;
1962 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 totwincount -= wincount;
1964 }
1965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 else /* topfr->fr_layout == FR_COL */
1967 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 topfr->fr_height = height;
1970
1971 if (dir != 'h') /* equalize frame heights */
1972 {
1973 /* Compute maximum number of windows vertically in this frame. */
1974 n = frame_minheight(topfr, NOWIN);
1975 /* add one for the bottom window if it doesn't have a statusline */
1976 if (row + height == cmdline_row && p_ls == 0)
1977 extra_sep = 1;
1978 else
1979 extra_sep = 0;
1980 totwincount = (n + extra_sep) / (p_wmh + 1);
1981 has_next_curwin = frame_has_win(topfr, next_curwin);
1982
1983 /*
1984 * Compute height for "next_curwin" window and room available for
1985 * other windows.
1986 * "m" is the minimal height when counting p_wh for "next_curwin".
1987 */
1988 m = frame_minheight(topfr, next_curwin);
1989 room = height - m;
1990 if (room < 0)
1991 {
1992 /* The room is less then 'winheight', use all space for the
1993 * current window. */
1994 next_curwin_size = p_wh + room;
1995 room = 0;
1996 }
1997 else
1998 {
1999 next_curwin_size = -1;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002000 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 /* If 'winfixheight' set keep the window height if
2003 * possible.
2004 * Watch out for this window being the next_curwin. */
2005 if (frame_fixed_height(fr))
2006 {
2007 n = frame_minheight(fr, NOWIN);
2008 new_size = fr->fr_height;
2009 if (frame_has_win(fr, next_curwin))
2010 {
2011 room += p_wh - p_wmh;
2012 next_curwin_size = 0;
2013 if (new_size < p_wh)
2014 new_size = p_wh;
2015 }
2016 else
2017 /* These windows don't use up room. */
2018 totwincount -= (n + (fr->fr_next == NULL
2019 ? extra_sep : 0)) / (p_wmh + 1);
2020 room -= new_size - n;
2021 if (room < 0)
2022 {
2023 new_size += room;
2024 room = 0;
2025 }
2026 fr->fr_newheight = new_size;
2027 }
2028 }
2029 if (next_curwin_size == -1)
2030 {
2031 if (!has_next_curwin)
2032 next_curwin_size = 0;
2033 else if (totwincount > 1
2034 && (room + (totwincount - 2))
2035 / (totwincount - 1) > p_wh)
2036 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002037 /* can make all windows higher than 'winheight',
2038 * spread the room equally. */
2039 next_curwin_size = (room + p_wh
2040 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 + (totwincount - 1)) / totwincount;
2042 room -= next_curwin_size - p_wh;
2043 }
2044 else
2045 next_curwin_size = p_wh;
2046 }
2047 }
2048
2049 if (has_next_curwin)
2050 --totwincount; /* don't count curwin */
2051 }
2052
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002053 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 wincount = 1;
2056 if (fr->fr_next == NULL)
2057 /* last frame gets all that remains (avoid roundoff error) */
2058 new_size = height;
2059 else if (dir == 'h')
2060 new_size = fr->fr_height;
2061 else if (frame_fixed_height(fr))
2062 {
2063 new_size = fr->fr_newheight;
2064 wincount = 0; /* doesn't count as a sizeable window */
2065 }
2066 else
2067 {
2068 /* Compute the maximum number of windows vert. in "fr". */
2069 n = frame_minheight(fr, NOWIN);
2070 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2071 / (p_wmh + 1);
2072 m = frame_minheight(fr, next_curwin);
2073 if (has_next_curwin)
2074 hnc = frame_has_win(fr, next_curwin);
2075 else
2076 hnc = FALSE;
2077 if (hnc) /* don't count next_curwin */
2078 --wincount;
2079 if (totwincount == 0)
2080 new_size = room;
2081 else
2082 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2083 / totwincount;
2084 if (hnc) /* add next_curwin size */
2085 {
2086 next_curwin_size -= p_wh - (m - n);
2087 new_size += next_curwin_size;
2088 room -= new_size - next_curwin_size;
2089 }
2090 else
2091 room -= new_size;
2092 new_size += n;
2093 }
2094 /* Skip frame that is full width when splitting or closing a
2095 * window, unless equalizing all frames. */
2096 if (!current || dir != 'h' || topfr->fr_parent != NULL
2097 || (new_size != fr->fr_height)
2098 || frame_has_win(fr, next_curwin))
2099 win_equal_rec(next_curwin, current, fr, dir, col, row,
2100 width, new_size);
2101 row += new_size;
2102 height -= new_size;
2103 totwincount -= wincount;
2104 }
2105 }
2106}
2107
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002108#ifdef FEAT_JOB_CHANNEL
2109 static void
2110leaving_window(win_T *win)
2111{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002112 // Only matters for a prompt window.
2113 if (!bt_prompt(win->w_buffer))
2114 return;
2115
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002116 // When leaving a prompt window stop Insert mode and perhaps restart
2117 // it when entering that window again.
2118 win->w_buffer->b_prompt_insert = restart_edit;
Bram Moolenaar942b4542018-06-17 16:23:34 +02002119 if (restart_edit != 0 && mode_displayed)
2120 clear_cmdline = TRUE; /* unshow mode later */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002121 restart_edit = NUL;
2122
2123 // When leaving the window (or closing the window) was done from a
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002124 // callback we need to break out of the Insert mode loop and restart Insert
2125 // mode when entering the window again.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002126 if (State & INSERT)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002127 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002128 stop_insert_mode = TRUE;
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002129 if (win->w_buffer->b_prompt_insert == NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002130 win->w_buffer->b_prompt_insert = 'A';
2131 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002132}
2133
2134 static void
2135entering_window(win_T *win)
2136{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002137 // Only matters for a prompt window.
2138 if (!bt_prompt(win->w_buffer))
2139 return;
2140
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002141 // When switching to a prompt buffer that was in Insert mode, don't stop
2142 // Insert mode, it may have been set in leaving_window().
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002143 if (win->w_buffer->b_prompt_insert != NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002144 stop_insert_mode = FALSE;
2145
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002146 // When entering the prompt window restart Insert mode if we were in Insert
2147 // mode when we left it.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002148 restart_edit = win->w_buffer->b_prompt_insert;
2149}
2150#endif
2151
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002153 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 */
2155 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002156close_windows(
2157 buf_T *buf,
2158 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002160 win_T *wp;
2161 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002162 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002163 int count = tabpage_index(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002166
Bram Moolenaar459ca562016-11-10 18:16:33 +01002167 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002169 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002170 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002172 if (win_close(wp, FALSE) == FAIL)
2173 /* If closing the window fails give up, to avoid looping
2174 * forever. */
2175 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002176
2177 /* Start all over, autocommands may change the window layout. */
2178 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 }
2180 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002181 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002183
2184 /* Also check windows in other tab pages. */
2185 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2186 {
2187 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002188 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002189 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002190 if (wp->w_buffer == buf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002191 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaarf740b292006-02-16 22:11:02 +00002192 {
2193 win_close_othertab(wp, FALSE, tp);
2194
2195 /* Start all over, the tab page may be closed and
2196 * autocommands may change the window layout. */
2197 nexttp = first_tabpage;
2198 break;
2199 }
2200 }
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002203
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002204 if (count != tabpage_index(NULL))
2205 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002206
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002207 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002208 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002209 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210}
2211
2212/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002213 * Return TRUE if the current window is the only window that exists (ignoring
2214 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002215 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002216 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002217 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002218last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002219{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002220 return (one_window() && first_tabpage->tp_next == NULL);
2221}
2222
2223/*
2224 * Return TRUE if there is only one window other than "aucmd_win" in the
2225 * current tab page.
2226 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002227 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002228one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002229{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002230 win_T *wp;
2231 int seen_one = FALSE;
2232
2233 FOR_ALL_WINDOWS(wp)
2234 {
2235 if (wp != aucmd_win)
2236 {
2237 if (seen_one)
2238 return FALSE;
2239 seen_one = TRUE;
2240 }
2241 }
2242 return TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002243}
2244
2245/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002246 * Close the possibly last window in a tab page.
2247 * Returns TRUE when the window was closed already.
2248 */
2249 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002250close_last_window_tabpage(
2251 win_T *win,
2252 int free_buf,
2253 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002254{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002255 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002256 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002257 buf_T *old_curbuf = curbuf;
2258
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002259 /*
2260 * Closing the last window in a tab page. First go to another tab
2261 * page and then close the window and the tab page. This avoids that
2262 * curwin and curtab are invalid while we are freeing memory, they may
2263 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002264 * Don't trigger autocommands yet, they may use wrong values, so do
2265 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002266 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002267 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002268 redraw_tabline = TRUE;
2269
2270 /* Safety check: Autocommands may have closed the window when jumping
2271 * to the other tab page. */
2272 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2273 {
2274 int h = tabline_height();
2275
2276 win_close_othertab(win, free_buf, prev_curtab);
2277 if (h != tabline_height())
2278 shell_new_rows();
2279 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002280#ifdef FEAT_JOB_CHANNEL
2281 entering_window(curwin);
2282#endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02002283 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2284 * that now. */
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002285 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002286 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002287 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2288 if (old_curbuf != curbuf)
2289 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002290 return TRUE;
2291 }
2292 return FALSE;
2293}
2294
2295/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002296 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 * If "free_buf" is TRUE related buffer may be unloaded.
2298 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002299 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002300 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002302 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002303win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 int dir;
2309 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002310 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002311 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002313 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002315 emsg(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002319 if (win->w_closing || (win->w_buffer != NULL
2320 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002321 return FAIL; /* window is already being closed */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002322 if (win == aucmd_win)
2323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002324 emsg(_("E813: Cannot close autocmd window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002325 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002326 }
2327 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002329 emsg(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002330 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002331 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002332
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002333 /* When closing the last window in a tab page first go to another tab page
2334 * and then close the window and the tab page to avoid that curwin and
2335 * curtab are invalid while we are freeing memory. */
2336 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002337 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 /* When closing the help window, try restoring a snapshot after closing
2340 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002341 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 help_window = TRUE;
2343 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002344 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (win == curwin)
2347 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002348#ifdef FEAT_JOB_CHANNEL
2349 leaving_window(curwin);
2350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 /*
2352 * Guess which window is going to be the new current window.
2353 * This may change because of the autocommands (sigh).
2354 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002355 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002358 * Be careful: If autocommands delete the window or cause this window
2359 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 */
2361 if (wp->w_buffer != curbuf)
2362 {
2363 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002364 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002366 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002367 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002368 win->w_closing = FALSE;
2369 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002370 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002372 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002374 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002375 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002376 win->w_closing = FALSE;
2377 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002378 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002379#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 /* autocmds may abort script processing */
2381 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002386#ifdef FEAT_GUI
Bram Moolenaar647e24b2019-03-17 16:39:46 +01002387 // Avoid trouble with scrollbars that are going to be deleted in
2388 // win_free().
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002389 if (gui.in_use)
2390 out_flush();
2391#endif
2392
Bram Moolenaara971b822011-09-14 14:43:25 +02002393#ifdef FEAT_SYN_HL
Bram Moolenaar647e24b2019-03-17 16:39:46 +01002394 // Free independent synblock before the buffer is freed.
Bram Moolenaarfc573802011-12-30 15:01:59 +01002395 if (win->w_buffer != NULL)
2396 reset_synblock(win);
Bram Moolenaara971b822011-09-14 14:43:25 +02002397#endif
2398
Bram Moolenaar647e24b2019-03-17 16:39:46 +01002399#ifdef FEAT_QUICKFIX
2400 // When the quickfix/location list window is closed, unlist the buffer.
2401 if (win->w_buffer != NULL && bt_quickfix(win->w_buffer))
2402 win->w_buffer->b_p_bl = FALSE;
2403#endif
2404
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 /*
2406 * Close the link to the buffer.
2407 */
Bram Moolenaarfc573802011-12-30 15:01:59 +01002408 if (win->w_buffer != NULL)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002409 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002410 bufref_T bufref;
2411
2412 set_bufref(&bufref, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002413 win->w_closing = TRUE;
Bram Moolenaar8f913992012-08-29 15:50:26 +02002414 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002415 if (win_valid_any_tab(win))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002416 win->w_closing = FALSE;
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002417 /* Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2418 * "wipe". */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002419 if (!bufref_valid(&bufref))
Bram Moolenaar62ef7972016-01-19 14:51:54 +01002420 curbuf = firstbuf;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002421 }
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002422
Bram Moolenaar802418d2013-01-17 14:00:11 +01002423 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2424 && (last_window() || curtab != prev_curtab
2425 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002426 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002427 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002428 * curwin->w_buffer, otherwise writing viminfo may fail. */
2429 if (curwin->w_buffer == NULL)
2430 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002431 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002432 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002433
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002434 /* Autocommands may have moved to another tab page. */
2435 if (curtab != prev_curtab && win_valid_any_tab(win)
2436 && win->w_buffer == NULL)
2437 {
2438 /* Need to close the window anyway, since the buffer is NULL. */
2439 win_close_othertab(win, FALSE, prev_curtab);
2440 return FAIL;
2441 }
2442
2443 /* Autocommands may have closed the window already or closed the only
2444 * other window. */
2445 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002446 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002447 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
Bram Moolenaara971b822011-09-14 14:43:25 +02002449 /* Free the memory used for the window and get the window that received
2450 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002451 wp = win_free_mem(win, &dir, NULL);
2452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 /* Make sure curwin isn't invalid. It can cause severe trouble when
2454 * printing an error message. For win_equal() curbuf needs to be valid
2455 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002456 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 {
2458 curwin = wp;
2459#ifdef FEAT_QUICKFIX
2460 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2461 {
2462 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002463 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 * finding another window to go to.
2465 */
2466 for (;;)
2467 {
2468 if (wp->w_next == NULL)
2469 wp = firstwin;
2470 else
2471 wp = wp->w_next;
2472 if (wp == curwin)
2473 break;
2474 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2475 {
2476 curwin = wp;
2477 break;
2478 }
2479 }
2480 }
2481#endif
2482 curbuf = curwin->w_buffer;
2483 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002484
2485 /* The cursor position may be invalid if the buffer changed after last
2486 * using the window. */
2487 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002489 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002490 /* If the frame of the closed window contains the new current window,
2491 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002492 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 else
2494 win_comp_pos();
2495 if (close_curwin)
2496 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002497 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (other_buffer)
2499 /* careful: after this wp and win may be invalid! */
2500 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 }
2502
2503 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002504 * If last window has a status line now and we don't want one,
2505 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 */
2507 last_status(FALSE);
2508
2509 /* After closing the help window, try restoring the window layout from
2510 * before it was opened. */
2511 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002512 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002514#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2516 if (gui.in_use && !win_hasvertsplit())
2517 gui_init_which_components(NULL);
2518#endif
2519
2520 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002521 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522}
2523
2524/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002525 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002526 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002527 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002528 * Caller must check if buffer is hidden and whether the tabline needs to be
2529 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002530 */
2531 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002532win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002533{
2534 win_T *wp;
2535 int dir;
2536 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002537 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002538
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002539 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2540 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002541 if (win->w_closing || (win->w_buffer != NULL
2542 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002543 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002544
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002545 if (win->w_buffer != NULL)
2546 /* Close the link to the buffer. */
2547 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002548
2549 /* Careful: Autocommands may have closed the tab page or made it the
2550 * current tab page. */
2551 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2552 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002553 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002554 return;
2555
2556 /* Autocommands may have closed the window already. */
2557 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2558 ;
2559 if (wp == NULL)
2560 return;
2561
Bram Moolenaarf740b292006-02-16 22:11:02 +00002562 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002563 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002564 {
2565 if (tp == first_tabpage)
2566 first_tabpage = tp->tp_next;
2567 else
2568 {
2569 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2570 ptp = ptp->tp_next)
2571 ;
2572 if (ptp == NULL)
2573 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002574 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002575 return;
2576 }
2577 ptp->tp_next = tp->tp_next;
2578 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002579 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002580 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002581
2582 /* Free the memory used for the window. */
2583 win_free_mem(win, &dir, tp);
2584
2585 if (free_tp)
2586 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002587}
2588
2589/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002590 * Free the memory used for a window.
2591 * Returns a pointer to the window that got the freed up space.
2592 */
2593 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002594win_free_mem(
2595 win_T *win,
2596 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2597 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002598{
2599 frame_T *frp;
2600 win_T *wp;
2601
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002602 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002603 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002604 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002605 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002606 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002607
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002608 /* When deleting the current window of another tab page select a new
2609 * current window. */
2610 if (tp != NULL && win == tp->tp_curwin)
2611 tp->tp_curwin = wp;
2612
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002613 return wp;
2614}
2615
2616#if defined(EXITFREE) || defined(PROTO)
2617 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002618win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002619{
2620 int dummy;
2621
Bram Moolenaarf740b292006-02-16 22:11:02 +00002622 while (first_tabpage->tp_next != NULL)
2623 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002624
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002625 if (aucmd_win != NULL)
2626 {
2627 (void)win_free_mem(aucmd_win, &dummy, NULL);
2628 aucmd_win = NULL;
2629 }
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002630
2631 while (firstwin != NULL)
2632 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002633
2634 /* No window should be used after this. Set curwin to NULL to crash
2635 * instead of using freed memory. */
2636 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002637}
2638#endif
2639
2640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 * Remove a window and its frame from the tree of frames.
2642 * Returns a pointer to the window that got the freed up space.
2643 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002644 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002645winframe_remove(
2646 win_T *win,
2647 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2648 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649{
2650 frame_T *frp, *frp2, *frp3;
2651 frame_T *frp_close = win->w_frame;
2652 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002655 * If there is only one window there is nothing to remove.
2656 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002657 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002658 return NULL;
2659
2660 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 * Remove the window from its frame.
2662 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002663 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 wp = frame2win(frp2);
2665
2666 /* Remove this frame from the list of frames. */
2667 frame_remove(frp_close);
2668
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 if (frp_close->fr_parent->fr_layout == FR_COL)
2670 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002671 /* When 'winfixheight' is set, try to find another frame in the column
2672 * (as close to the closed frame as possible) to distribute the height
2673 * to. */
2674 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2675 {
2676 frp = frp_close->fr_prev;
2677 frp3 = frp_close->fr_next;
2678 while (frp != NULL || frp3 != NULL)
2679 {
2680 if (frp != NULL)
2681 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002682 if (!frame_fixed_height(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002683 {
2684 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002685 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002686 break;
2687 }
2688 frp = frp->fr_prev;
2689 }
2690 if (frp3 != NULL)
2691 {
2692 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2693 {
2694 frp2 = frp3;
2695 wp = frp3->fr_win;
2696 break;
2697 }
2698 frp3 = frp3->fr_next;
2699 }
2700 }
2701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2703 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 *dirp = 'v';
2705 }
2706 else
2707 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002708 /* When 'winfixwidth' is set, try to find another frame in the column
2709 * (as close to the closed frame as possible) to distribute the width
2710 * to. */
2711 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2712 {
2713 frp = frp_close->fr_prev;
2714 frp3 = frp_close->fr_next;
2715 while (frp != NULL || frp3 != NULL)
2716 {
2717 if (frp != NULL)
2718 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002719 if (!frame_fixed_width(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002720 {
2721 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002722 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002723 break;
2724 }
2725 frp = frp->fr_prev;
2726 }
2727 if (frp3 != NULL)
2728 {
2729 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2730 {
2731 frp2 = frp3;
2732 wp = frp3->fr_win;
2733 break;
2734 }
2735 frp3 = frp3->fr_next;
2736 }
2737 }
2738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002740 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 *dirp = 'h';
2742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743
2744 /* If rows/columns go to a window below/right its positions need to be
2745 * updated. Can only be done after the sizes have been updated. */
2746 if (frp2 == frp_close->fr_next)
2747 {
2748 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002749 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
2751 frame_comp_pos(frp2, &row, &col);
2752 }
2753
2754 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2755 {
2756 /* There is no other frame in this list, move its info to the parent
2757 * and remove it. */
2758 frp2->fr_parent->fr_layout = frp2->fr_layout;
2759 frp2->fr_parent->fr_child = frp2->fr_child;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002760 FOR_ALL_FRAMES(frp, frp2->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 frp->fr_parent = frp2->fr_parent;
2762 frp2->fr_parent->fr_win = frp2->fr_win;
2763 if (frp2->fr_win != NULL)
2764 frp2->fr_win->w_frame = frp2->fr_parent;
2765 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002766 if (topframe->fr_child == frp2)
2767 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 vim_free(frp2);
2769
2770 frp2 = frp->fr_parent;
2771 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2772 {
2773 /* The frame above the parent has the same layout, have to merge
2774 * the frames into this list. */
2775 if (frp2->fr_child == frp)
2776 frp2->fr_child = frp->fr_child;
2777 frp->fr_child->fr_prev = frp->fr_prev;
2778 if (frp->fr_prev != NULL)
2779 frp->fr_prev->fr_next = frp->fr_child;
2780 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2781 {
2782 frp3->fr_parent = frp2;
2783 if (frp3->fr_next == NULL)
2784 {
2785 frp3->fr_next = frp->fr_next;
2786 if (frp->fr_next != NULL)
2787 frp->fr_next->fr_prev = frp3;
2788 break;
2789 }
2790 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002791 if (topframe->fr_child == frp)
2792 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 vim_free(frp);
2794 }
2795 }
2796
2797 return wp;
2798}
2799
2800/*
Bram Moolenaarc136af22018-05-04 20:15:38 +02002801 * Return a pointer to the frame that will receive the empty screen space that
2802 * is left over after "win" is closed.
2803 *
2804 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
2805 * by default. Otherwise, the free space goes below or to the right. The
2806 * result is that opening a window and then immediately closing it will
2807 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
2808 * respected when possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 */
2810 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002811win_altframe(
2812 win_T *win,
2813 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814{
2815 frame_T *frp;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002816 frame_T *other_fr, *target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002818 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002819 return alt_tabpage()->tp_curwin->w_frame;
2820
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 frp = win->w_frame;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002822
2823 if (frp->fr_prev == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 return frp->fr_next;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002825 if (frp->fr_next == NULL)
2826 return frp->fr_prev;
2827
2828 target_fr = frp->fr_next;
2829 other_fr = frp->fr_prev;
2830 if (p_spr || p_sb)
2831 {
2832 target_fr = frp->fr_prev;
2833 other_fr = frp->fr_next;
2834 }
2835
2836 /* If 'wfh' or 'wfw' is set for the target and not for the alternate
2837 * window, reverse the selection. */
2838 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2839 {
2840 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
2841 target_fr = other_fr;
2842 }
2843 else
2844 {
2845 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
2846 target_fr = other_fr;
2847 }
2848
2849 return target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850}
2851
2852/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002853 * Return the tabpage that will be used if the current one is closed.
2854 */
2855 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002856alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002857{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002858 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002859
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002860 /* Use the next tab page if possible. */
2861 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002862 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002863
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002864 /* Find the last but one tab page. */
2865 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2866 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002867 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002868}
2869
2870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 * Find the left-upper window in frame "frp".
2872 */
2873 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002874frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 while (frp->fr_win == NULL)
2877 frp = frp->fr_child;
2878 return frp->fr_win;
2879}
2880
2881/*
2882 * Return TRUE if frame "frp" contains window "wp".
2883 */
2884 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002885frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 frame_T *p;
2888
2889 if (frp->fr_layout == FR_LEAF)
2890 return frp->fr_win == wp;
2891
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002892 FOR_ALL_FRAMES(p, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 if (frame_has_win(p, wp))
2894 return TRUE;
2895 return FALSE;
2896}
2897
2898/*
2899 * Set a new height for a frame. Recursively sets the height for contained
2900 * frames and windows. Caller must take care of positions.
2901 */
2902 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002903frame_new_height(
2904 frame_T *topfrp,
2905 int height,
2906 int topfirst, /* resize topmost contained frame first */
2907 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 may cause the height not to be set */
2909{
2910 frame_T *frp;
2911 int extra_lines;
2912 int h;
2913
2914 if (topfrp->fr_win != NULL)
2915 {
2916 /* Simple case: just one window. */
2917 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02002918 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01002919 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 else if (topfrp->fr_layout == FR_ROW)
2922 {
2923 do
2924 {
2925 /* All frames in this row get the same new height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002926 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
2928 frame_new_height(frp, height, topfirst, wfh);
2929 if (frp->fr_height > height)
2930 {
2931 /* Could not fit the windows, make the whole row higher. */
2932 height = frp->fr_height;
2933 break;
2934 }
2935 }
2936 }
2937 while (frp != NULL);
2938 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002939 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
2941 /* Complicated case: Resize a column of frames. Resize the bottom
2942 * frame first, frames above that when needed. */
2943
2944 frp = topfrp->fr_child;
2945 if (wfh)
2946 /* Advance past frames with one window with 'wfh' set. */
2947 while (frame_fixed_height(frp))
2948 {
2949 frp = frp->fr_next;
2950 if (frp == NULL)
2951 return; /* no frame without 'wfh', give up */
2952 }
2953 if (!topfirst)
2954 {
2955 /* Find the bottom frame of this column */
2956 while (frp->fr_next != NULL)
2957 frp = frp->fr_next;
2958 if (wfh)
2959 /* Advance back for frames with one window with 'wfh' set. */
2960 while (frame_fixed_height(frp))
2961 frp = frp->fr_prev;
2962 }
2963
2964 extra_lines = height - topfrp->fr_height;
2965 if (extra_lines < 0)
2966 {
2967 /* reduce height of contained frames, bottom or top frame first */
2968 while (frp != NULL)
2969 {
2970 h = frame_minheight(frp, NULL);
2971 if (frp->fr_height + extra_lines < h)
2972 {
2973 extra_lines += frp->fr_height - h;
2974 frame_new_height(frp, h, topfirst, wfh);
2975 }
2976 else
2977 {
2978 frame_new_height(frp, frp->fr_height + extra_lines,
2979 topfirst, wfh);
2980 break;
2981 }
2982 if (topfirst)
2983 {
2984 do
2985 frp = frp->fr_next;
2986 while (wfh && frp != NULL && frame_fixed_height(frp));
2987 }
2988 else
2989 {
2990 do
2991 frp = frp->fr_prev;
2992 while (wfh && frp != NULL && frame_fixed_height(frp));
2993 }
2994 /* Increase "height" if we could not reduce enough frames. */
2995 if (frp == NULL)
2996 height -= extra_lines;
2997 }
2998 }
2999 else if (extra_lines > 0)
3000 {
3001 /* increase height of bottom or top frame */
3002 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3003 }
3004 }
3005 topfrp->fr_height = height;
3006}
3007
3008/*
3009 * Return TRUE if height of frame "frp" should not be changed because of
3010 * the 'winfixheight' option.
3011 */
3012 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003013frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 /* frame with one window: fixed height if 'winfixheight' set. */
3016 if (frp->fr_win != NULL)
3017 return frp->fr_win->w_p_wfh;
3018
3019 if (frp->fr_layout == FR_ROW)
3020 {
3021 /* The frame is fixed height if one of the frames in the row is fixed
3022 * height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003023 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (frame_fixed_height(frp))
3025 return TRUE;
3026 return FALSE;
3027 }
3028
3029 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3030 * frames in the row are fixed height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003031 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 if (!frame_fixed_height(frp))
3033 return FALSE;
3034 return TRUE;
3035}
3036
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003038 * Return TRUE if width of frame "frp" should not be changed because of
3039 * the 'winfixwidth' option.
3040 */
3041 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003042frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003043{
3044 /* frame with one window: fixed width if 'winfixwidth' set. */
3045 if (frp->fr_win != NULL)
3046 return frp->fr_win->w_p_wfw;
3047
3048 if (frp->fr_layout == FR_COL)
3049 {
3050 /* The frame is fixed width if one of the frames in the row is fixed
3051 * width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003052 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003053 if (frame_fixed_width(frp))
3054 return TRUE;
3055 return FALSE;
3056 }
3057
3058 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3059 * frames in the row are fixed width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003060 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003061 if (!frame_fixed_width(frp))
3062 return FALSE;
3063 return TRUE;
3064}
3065
3066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 * Add a status line to windows at the bottom of "frp".
3068 * Note: Does not check if there is room!
3069 */
3070 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003071frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072{
3073 win_T *wp;
3074
3075 if (frp->fr_layout == FR_LEAF)
3076 {
3077 wp = frp->fr_win;
3078 if (wp->w_status_height == 0)
3079 {
3080 if (wp->w_height > 0) /* don't make it negative */
3081 --wp->w_height;
3082 wp->w_status_height = STATUS_HEIGHT;
3083 }
3084 }
3085 else if (frp->fr_layout == FR_ROW)
3086 {
3087 /* Handle all the frames in the row. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003088 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 frame_add_statusline(frp);
3090 }
3091 else /* frp->fr_layout == FR_COL */
3092 {
3093 /* Only need to handle the last frame in the column. */
3094 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3095 ;
3096 frame_add_statusline(frp);
3097 }
3098}
3099
3100/*
3101 * Set width of a frame. Handles recursively going through contained frames.
3102 * May remove separator line for windows at the right side (for win_close()).
3103 */
3104 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003105frame_new_width(
3106 frame_T *topfrp,
3107 int width,
3108 int leftfirst, /* resize leftmost contained frame first */
3109 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003110 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 frame_T *frp;
3113 int extra_cols;
3114 int w;
3115 win_T *wp;
3116
3117 if (topfrp->fr_layout == FR_LEAF)
3118 {
3119 /* Simple case: just one window. */
3120 wp = topfrp->fr_win;
3121 /* Find out if there are any windows right of this one. */
3122 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3123 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3124 break;
3125 if (frp->fr_parent == NULL)
3126 wp->w_vsep_width = 0;
3127 win_new_width(wp, width - wp->w_vsep_width);
3128 }
3129 else if (topfrp->fr_layout == FR_COL)
3130 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003131 do
3132 {
3133 /* All frames in this column get the same new width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003134 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003135 {
3136 frame_new_width(frp, width, leftfirst, wfw);
3137 if (frp->fr_width > width)
3138 {
3139 /* Could not fit the windows, make whole column wider. */
3140 width = frp->fr_width;
3141 break;
3142 }
3143 }
3144 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
3146 else /* fr_layout == FR_ROW */
3147 {
3148 /* Complicated case: Resize a row of frames. Resize the rightmost
3149 * frame first, frames left of it when needed. */
3150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003152 if (wfw)
3153 /* Advance past frames with one window with 'wfw' set. */
3154 while (frame_fixed_width(frp))
3155 {
3156 frp = frp->fr_next;
3157 if (frp == NULL)
3158 return; /* no frame without 'wfw', give up */
3159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003161 {
3162 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 while (frp->fr_next != NULL)
3164 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003165 if (wfw)
3166 /* Advance back for frames with one window with 'wfw' set. */
3167 while (frame_fixed_width(frp))
3168 frp = frp->fr_prev;
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
3171 extra_cols = width - topfrp->fr_width;
3172 if (extra_cols < 0)
3173 {
3174 /* reduce frame width, rightmost frame first */
3175 while (frp != NULL)
3176 {
3177 w = frame_minwidth(frp, NULL);
3178 if (frp->fr_width + extra_cols < w)
3179 {
3180 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003181 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 }
3183 else
3184 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003185 frame_new_width(frp, frp->fr_width + extra_cols,
3186 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 break;
3188 }
3189 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003190 {
3191 do
3192 frp = frp->fr_next;
3193 while (wfw && frp != NULL && frame_fixed_width(frp));
3194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003196 {
3197 do
3198 frp = frp->fr_prev;
3199 while (wfw && frp != NULL && frame_fixed_width(frp));
3200 }
3201 /* Increase "width" if we could not reduce enough frames. */
3202 if (frp == NULL)
3203 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 }
3205 }
3206 else if (extra_cols > 0)
3207 {
3208 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003209 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 }
3211 }
3212 topfrp->fr_width = width;
3213}
3214
3215/*
3216 * Add the vertical separator to windows at the right side of "frp".
3217 * Note: Does not check if there is room!
3218 */
3219 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003220frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 win_T *wp;
3223
3224 if (frp->fr_layout == FR_LEAF)
3225 {
3226 wp = frp->fr_win;
3227 if (wp->w_vsep_width == 0)
3228 {
3229 if (wp->w_width > 0) /* don't make it negative */
3230 --wp->w_width;
3231 wp->w_vsep_width = 1;
3232 }
3233 }
3234 else if (frp->fr_layout == FR_COL)
3235 {
3236 /* Handle all the frames in the column. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003237 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 frame_add_vsep(frp);
3239 }
3240 else /* frp->fr_layout == FR_ROW */
3241 {
3242 /* Only need to handle the last frame in the row. */
3243 frp = frp->fr_child;
3244 while (frp->fr_next != NULL)
3245 frp = frp->fr_next;
3246 frame_add_vsep(frp);
3247 }
3248}
3249
3250/*
3251 * Set frame width from the window it contains.
3252 */
3253 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003254frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259/*
3260 * Set frame height from the window it contains.
3261 */
3262 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003263frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003265 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266}
3267
3268/*
3269 * Compute the minimal height for frame "topfrp".
3270 * Uses the 'winminheight' option.
3271 * When "next_curwin" isn't NULL, use p_wh for this window.
3272 * When "next_curwin" is NOWIN, don't use at least one line for the current
3273 * window.
3274 */
3275 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003276frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277{
3278 frame_T *frp;
3279 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281
3282 if (topfrp->fr_win != NULL)
3283 {
3284 if (topfrp->fr_win == next_curwin)
3285 m = p_wh + topfrp->fr_win->w_status_height;
3286 else
3287 {
3288 /* window: minimal height of the window plus status line */
3289 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003290 if (topfrp->fr_win == curwin && next_curwin == NULL)
3291 {
3292 /* Current window is minimal one line high and WinBar is
3293 * visible. */
3294 if (p_wmh == 0)
3295 ++m;
3296 m += WINBAR_HEIGHT(curwin);
3297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 else if (topfrp->fr_layout == FR_ROW)
3301 {
3302 /* get the minimal height from each frame in this row */
3303 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003304 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 {
3306 n = frame_minheight(frp, next_curwin);
3307 if (n > m)
3308 m = n;
3309 }
3310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 else
3312 {
3313 /* Add up the minimal heights for all frames in this column. */
3314 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003315 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 m += frame_minheight(frp, next_curwin);
3317 }
3318
3319 return m;
3320}
3321
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322/*
3323 * Compute the minimal width for frame "topfrp".
3324 * When "next_curwin" isn't NULL, use p_wiw for this window.
3325 * When "next_curwin" is NOWIN, don't use at least one column for the current
3326 * window.
3327 */
3328 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003329frame_minwidth(
3330 frame_T *topfrp,
3331 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 frame_T *frp;
3334 int m, n;
3335
3336 if (topfrp->fr_win != NULL)
3337 {
3338 if (topfrp->fr_win == next_curwin)
3339 m = p_wiw + topfrp->fr_win->w_vsep_width;
3340 else
3341 {
3342 /* window: minimal width of the window plus separator column */
3343 m = p_wmw + topfrp->fr_win->w_vsep_width;
3344 /* Current window is minimal one column wide */
3345 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3346 ++m;
3347 }
3348 }
3349 else if (topfrp->fr_layout == FR_COL)
3350 {
3351 /* get the minimal width from each frame in this column */
3352 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003353 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 {
3355 n = frame_minwidth(frp, next_curwin);
3356 if (n > m)
3357 m = n;
3358 }
3359 }
3360 else
3361 {
3362 /* Add up the minimal widths for all frames in this row. */
3363 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003364 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 m += frame_minwidth(frp, next_curwin);
3366 }
3367
3368 return m;
3369}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371
3372/*
3373 * Try to close all windows except current one.
3374 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3375 * used and the buffer was modified.
3376 *
3377 * Used by ":bdel" and ":only".
3378 */
3379 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003380close_others(
3381 int message,
3382 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383{
3384 win_T *wp;
3385 win_T *nextwp;
3386 int r;
3387
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003388 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003390 if (message && !autocmd_busy)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003391 msg(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 return;
3393 }
3394
3395 /* Be very careful here: autocommands may change the window layout. */
3396 for (wp = firstwin; win_valid(wp); wp = nextwp)
3397 {
3398 nextwp = wp->w_next;
3399 if (wp != curwin) /* don't close current window */
3400 {
3401
3402 /* Check if it's allowed to abandon this window */
3403 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (!win_valid(wp)) /* autocommands messed wp up */
3405 {
3406 nextwp = firstwin;
3407 continue;
3408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (!r)
3410 {
3411#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3412 if (message && (p_confirm || cmdmod.confirm) && p_write)
3413 {
3414 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (!win_valid(wp)) /* autocommands messed wp up */
3416 {
3417 nextwp = firstwin;
3418 continue;
3419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421 if (bufIsChanged(wp->w_buffer))
3422#endif
3423 continue;
3424 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003425 win_close(wp, !buf_hide(wp->w_buffer)
3426 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
3428 }
3429
Bram Moolenaar459ca562016-11-10 18:16:33 +01003430 if (message && !ONE_WINDOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003431 emsg(_("E445: Other window contains changes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432}
3433
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003435 * Init the current window "curwin".
3436 * Called when a new file is being edited.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 */
3438 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003439curwin_init(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440{
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003441 win_init_empty(curwin);
3442}
3443
3444 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003445win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003446{
3447 redraw_win_later(wp, NOT_VALID);
3448 wp->w_lines_valid = 0;
3449 wp->w_cursor.lnum = 1;
3450 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003451 wp->w_cursor.coladd = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003452 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3453 wp->w_pcmark.col = 0;
3454 wp->w_prev_pcmark.lnum = 0;
3455 wp->w_prev_pcmark.col = 0;
3456 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003458 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003460 wp->w_botline = 2;
Bram Moolenaara971b822011-09-14 14:43:25 +02003461#ifdef FEAT_SYN_HL
3462 wp->w_s = &wp->w_buffer->b_s;
3463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464}
3465
3466/*
3467 * Allocate the first window and put an empty buffer in it.
3468 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003469 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003471 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003472win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003474 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003475 return FAIL;
3476
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003477 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003478 if (first_tabpage == NULL)
3479 return FAIL;
3480 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003481 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003482
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003483 return OK;
3484}
3485
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003486/*
3487 * Init "aucmd_win". This can only be done after the first
3488 * window is fully initialized, thus it can't be in win_alloc_first().
3489 */
3490 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003491win_alloc_aucmd_win(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003492{
3493 aucmd_win = win_alloc(NULL, TRUE);
3494 if (aucmd_win != NULL)
3495 {
3496 win_init_some(aucmd_win, curwin);
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003497 RESET_BINDING(aucmd_win);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003498 new_frame(aucmd_win);
3499 }
3500}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003501
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003502/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003503 * Allocate the first window or the first window in a new tab page.
3504 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003505 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003506 * Return FAIL when something goes wrong (out of memory).
3507 */
3508 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003509win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003510{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003511 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003512 if (oldwin == NULL)
3513 {
3514 /* Very first window, need to create an empty buffer for it and
3515 * initialize from scratch. */
3516 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3517 if (curwin == NULL || curbuf == NULL)
3518 return FAIL;
3519 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003520#ifdef FEAT_SYN_HL
3521 curwin->w_s = &(curbuf->b_s);
3522#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003523 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003524 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003525 curwin_init(); /* init current window */
3526 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003527 else
3528 {
3529 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003530 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003531
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003532 /* We don't want cursor- and scroll-binding in the first window. */
3533 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003536 new_frame(curwin);
3537 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003538 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003539 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003542
3543 return OK;
3544}
3545
3546/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003547 * Create a frame for window "wp".
3548 */
3549 static void
3550new_frame(win_T *wp)
3551{
3552 frame_T *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
3553
3554 wp->w_frame = frp;
3555 if (frp != NULL)
3556 {
3557 frp->fr_layout = FR_LEAF;
3558 frp->fr_win = wp;
3559 }
3560}
3561
3562/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003563 * Initialize the window and frame size to the maximum.
3564 */
3565 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003566win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003567{
3568 firstwin->w_height = ROWS_AVAIL;
3569 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003570 firstwin->w_width = Columns;
3571 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572}
3573
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003574/*
3575 * Allocate a new tabpage_T and init the values.
3576 * Returns NULL when out of memory.
3577 */
3578 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003579alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003580{
3581 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003582# ifdef FEAT_GUI
3583 int i;
3584# endif
3585
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003586
3587 tp = (tabpage_T *)alloc_clear((unsigned)sizeof(tabpage_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02003588 if (tp == NULL)
3589 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003590
Bram Moolenaar429fa852013-04-15 12:27:36 +02003591# ifdef FEAT_EVAL
3592 /* init t: variables */
3593 tp->tp_vars = dict_alloc();
3594 if (tp->tp_vars == NULL)
3595 {
3596 vim_free(tp);
3597 return NULL;
3598 }
3599 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3600# endif
3601
3602# ifdef FEAT_GUI
3603 for (i = 0; i < 3; i++)
3604 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003605# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003606# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003607 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003608# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003609 tp->tp_ch_used = p_ch;
3610
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003611 return tp;
3612}
3613
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003614 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003615free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003616{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003617 int idx;
3618
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003619# ifdef FEAT_DIFF
3620 diff_clear(tp);
3621# endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003622 for (idx = 0; idx < SNAP_COUNT; ++idx)
3623 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003624#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003625 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3626 hash_init(&tp->tp_vars->dv_hashtab);
3627 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003628#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003629
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003630 vim_free(tp->tp_localdir);
3631
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003632#ifdef FEAT_PYTHON
3633 python_tabpage_free(tp);
3634#endif
3635
3636#ifdef FEAT_PYTHON3
3637 python3_tabpage_free(tp);
3638#endif
3639
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003640 vim_free(tp);
3641}
3642
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003643/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003644 * Create a new Tab page with one window.
3645 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003646 * When "after" is 0 put it just after the current Tab page.
3647 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003648 * Return FAIL or OK.
3649 */
3650 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003651win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003652{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003653 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003654 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003655 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003656
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003657 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003658 if (newtp == NULL)
3659 return FAIL;
3660
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003661 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003662 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003663 {
3664 vim_free(newtp);
3665 return FAIL;
3666 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003667 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003668
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003669 newtp->tp_localdir = (tp->tp_localdir == NULL)
3670 ? NULL : vim_strsave(tp->tp_localdir);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003671 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003672 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003673 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003674 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003675 if (after == 1)
3676 {
3677 /* New tab page becomes the first one. */
3678 newtp->tp_next = first_tabpage;
3679 first_tabpage = newtp;
3680 }
3681 else
3682 {
3683 if (after > 0)
3684 {
3685 /* Put new tab page before tab page "after". */
3686 n = 2;
3687 for (tp = first_tabpage; tp->tp_next != NULL
3688 && n < after; tp = tp->tp_next)
3689 ++n;
3690 }
3691 newtp->tp_next = tp->tp_next;
3692 tp->tp_next = newtp;
3693 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003694 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003695 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003696 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003697
3698 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003699 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003700
3701#if defined(FEAT_GUI)
3702 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3703 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003704 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003705#endif
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003706#ifdef FEAT_JOB_CHANNEL
3707 entering_window(curwin);
3708#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003709
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01003710 redraw_all_later(NOT_VALID);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003711 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003712 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003713 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003714 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003715 return OK;
3716 }
3717
3718 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003719 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003720 return FAIL;
3721}
3722
3723/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003724 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3725 * like with ":split".
3726 * Returns OK if a new tab page was created, FAIL otherwise.
3727 */
3728 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003729may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003730{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003731 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003732
Bram Moolenaard326ce82007-03-11 14:48:29 +00003733 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003734 {
3735 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003736 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003737 return win_new_tabpage(n);
3738 }
3739 return FAIL;
3740}
3741
3742/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003743 * Create up to "maxcount" tabpages with empty windows.
3744 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003745 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003746 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003747make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003748{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003749 int count = maxcount;
3750 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003751
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003752 /* Limit to 'tabpagemax' tabs. */
3753 if (count > p_tpm)
3754 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003755
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003756 /*
3757 * Don't execute autocommands while creating the tab pages. Must do that
3758 * when putting the buffers in the windows.
3759 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003760 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003761
3762 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003763 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003764 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003765
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003766 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003767
3768 /* return actual number of tab pages */
3769 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003770}
3771
3772/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003773 * Return TRUE when "tpc" points to a valid tab page.
3774 */
3775 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003776valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003777{
3778 tabpage_T *tp;
3779
Bram Moolenaar29323592016-07-24 22:04:11 +02003780 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003781 if (tp == tpc)
3782 return TRUE;
3783 return FALSE;
3784}
3785
3786/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003787 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3788 * valid.
3789 */
3790 int
3791valid_tabpage_win(tabpage_T *tpc)
3792{
3793 tabpage_T *tp;
3794 win_T *wp;
3795
3796 FOR_ALL_TABPAGES(tp)
3797 {
3798 if (tp == tpc)
3799 {
3800 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3801 {
3802 if (win_valid_any_tab(wp))
3803 return TRUE;
3804 }
3805 return FALSE;
3806 }
3807 }
3808 /* shouldn't happen */
3809 return FALSE;
3810}
3811
3812/*
3813 * Close tabpage "tab", assuming it has no windows in it.
3814 * There must be another tabpage or this will crash.
3815 */
3816 void
3817close_tabpage(tabpage_T *tab)
3818{
3819 tabpage_T *ptp;
3820
3821 if (tab == first_tabpage)
3822 {
3823 first_tabpage = tab->tp_next;
3824 ptp = first_tabpage;
3825 }
3826 else
3827 {
3828 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3829 ptp = ptp->tp_next)
3830 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003831 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003832 ptp->tp_next = tab->tp_next;
3833 }
3834
3835 goto_tabpage_tp(ptp, FALSE, FALSE);
3836 free_tabpage(tab);
3837}
3838
3839/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003840 * Find tab page "n" (first one is 1). Returns NULL when not found.
3841 */
3842 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003843find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003844{
3845 tabpage_T *tp;
3846 int i = 1;
3847
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003848 if (n == 0)
3849 return curtab;
3850
Bram Moolenaarf740b292006-02-16 22:11:02 +00003851 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3852 ++i;
3853 return tp;
3854}
3855
3856/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003857 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003858 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003859 */
3860 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003861tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003862{
3863 int i = 1;
3864 tabpage_T *tp;
3865
3866 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
3867 ++i;
3868 return i;
3869}
3870
3871/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003872 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02003873 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003874 * FAIL.
3875 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003876 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003877 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003878leave_tabpage(
3879 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003880 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003881 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003882{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003883 tabpage_T *tp = curtab;
3884
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003885#ifdef FEAT_JOB_CHANNEL
3886 leaving_window(curwin);
3887#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003888 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003889 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003890 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003891 if (new_curbuf != curbuf)
3892 {
3893 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
3894 if (curtab != tp)
3895 return FAIL;
3896 }
3897 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
3898 if (curtab != tp)
3899 return FAIL;
3900 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003901 if (curtab != tp)
3902 return FAIL;
3903 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003904#if defined(FEAT_GUI)
3905 /* Remove the scrollbars. They may be added back later. */
3906 if (gui.in_use)
3907 gui_remove_scrollbars();
3908#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003909 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003910 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003911 tp->tp_firstwin = firstwin;
3912 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003913 tp->tp_old_Rows = Rows;
3914 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003915 firstwin = NULL;
3916 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003917 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003918}
3919
3920/*
3921 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003922 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003923 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
3924 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003925 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003926 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003927enter_tabpage(
3928 tabpage_T *tp,
3929 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01003930 int trigger_enter_autocmds,
3931 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003932{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003933 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003934 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003935
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003936 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003937 firstwin = tp->tp_firstwin;
3938 lastwin = tp->tp_lastwin;
3939 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00003940
3941 /* We would like doing the TabEnter event first, but we don't have a
3942 * valid current window yet, which may break some commands.
3943 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02003944 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02003945 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00003946 prevwin = next_prevwin;
3947
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003948 last_status(FALSE); /* status line may appear or disappear */
3949 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003950#ifdef FEAT_DIFF
3951 diff_need_scrollbind = TRUE;
3952#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003953
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003954 /* The tabpage line may have appeared or disappeared, may need to resize
3955 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003956 * frame sizes too. Use the stored value of p_ch, so that it can be
3957 * different for each tab page. */
3958 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003959 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3960#ifdef FEAT_GUI_TABLINE
3961 && !gui_use_tabline()
3962#endif
3963 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003964 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003965 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003966 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003967
3968#if defined(FEAT_GUI)
3969 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3970 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003971 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003972#endif
3973
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003974 /* Apply autocommands after updating the display, when 'rows' and
3975 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003976 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003977 {
3978 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3979 if (old_curbuf != curbuf)
3980 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3981 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003982
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01003983 redraw_all_later(NOT_VALID);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003984}
3985
3986/*
3987 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003988 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003989 */
3990 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003991goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003992{
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02003993 tabpage_T *tp = NULL; // shut up compiler
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003994 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003995 int i;
3996
Bram Moolenaard68071d2006-05-02 22:08:30 +00003997 if (text_locked())
3998 {
3999 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004000 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00004001 return;
4002 }
4003
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004004 /* If there is only one it can't work. */
4005 if (first_tabpage->tp_next == NULL)
4006 {
4007 if (n > 1)
4008 beep_flush();
4009 return;
4010 }
4011
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004012 if (n == 0)
4013 {
4014 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004015 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004016 tp = first_tabpage;
4017 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004018 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004019 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004020 else if (n < 0)
4021 {
4022 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4023 * this N times. */
4024 ttp = curtab;
4025 for (i = n; i < 0; ++i)
4026 {
4027 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4028 tp = tp->tp_next)
4029 ;
4030 ttp = tp;
4031 }
4032 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004033 else if (n == 9999)
4034 {
4035 /* Go to last tab page. */
4036 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4037 ;
4038 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004039 else
4040 {
4041 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004042 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004043 if (tp == NULL)
4044 {
4045 beep_flush();
4046 return;
4047 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004048 }
4049
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004050 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004051
4052#ifdef FEAT_GUI_TABLINE
4053 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004054 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004055#endif
4056}
4057
4058/*
4059 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004060 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4061 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004062 * Note: doesn't update the GUI tab.
4063 */
4064 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004065goto_tabpage_tp(
4066 tabpage_T *tp,
4067 int trigger_enter_autocmds,
4068 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004069{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004070 /* Don't repeat a message in another tab page. */
4071 set_keep_msg(NULL, 0);
4072
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004073 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4074 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004075 {
4076 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004077 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4078 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004079 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004080 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4081 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004082 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004083}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
4085/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004086 * Enter window "wp" in tab page "tp".
4087 * Also updates the GUI tab.
4088 */
4089 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004090goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004091{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004092 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004093 if (curtab == tp && win_valid(wp))
4094 {
4095 win_enter(wp, TRUE);
4096# ifdef FEAT_GUI_TABLINE
4097 if (gui_use_tabline())
4098 gui_mch_set_curtab(tabpage_index(curtab));
4099# endif
4100 }
4101}
4102
4103/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004104 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004105 */
4106 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004107tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004108{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004109 int n = 1;
4110 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004111
4112 if (first_tabpage->tp_next == NULL)
4113 return;
4114
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004115 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4116 ++n;
4117
4118 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4119 && tp->tp_next == curtab))
4120 return;
4121
4122 tp_dst = tp;
4123
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004124 /* Remove the current tab page from the list of tab pages. */
4125 if (curtab == first_tabpage)
4126 first_tabpage = curtab->tp_next;
4127 else
4128 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004129 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004130 if (tp->tp_next == curtab)
4131 break;
4132 if (tp == NULL) /* "cannot happen" */
4133 return;
4134 tp->tp_next = curtab->tp_next;
4135 }
4136
4137 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004138 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004139 {
4140 curtab->tp_next = first_tabpage;
4141 first_tabpage = curtab;
4142 }
4143 else
4144 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004145 curtab->tp_next = tp_dst->tp_next;
4146 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004147 }
4148
4149 /* Need to redraw the tabline. Tab page contents doesn't change. */
4150 redraw_tabline = TRUE;
4151}
4152
4153
4154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 * Go to another window.
4156 * When jumping to another buffer, stop Visual mode. Do this before
4157 * changing windows so we can yank the selection into the '*' register.
4158 * When jumping to another window on the same buffer, adjust its cursor
4159 * position to keep the same Visual area.
4160 */
4161 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004162win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004164#ifdef FEAT_CONCEAL
4165 win_T *owp = curwin;
4166#endif
4167
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004168 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
4170 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004171 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 return;
4173 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004174 if (curbuf_locked())
4175 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 if (wp->w_buffer != curbuf)
4178 reset_VIsual_and_resel();
4179 else if (VIsual_active)
4180 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182#ifdef FEAT_GUI
4183 need_mouse_correct = TRUE;
4184#endif
4185 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004186
4187#ifdef FEAT_CONCEAL
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004188 // Conceal cursor line in previous window, unconceal in current window.
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004189 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004190 redrawWinline(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004191 if (curwin->w_p_cole > 0 && !msg_scrolled)
4192 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194}
4195
4196#if defined(FEAT_PERL) || defined(PROTO)
4197/*
4198 * Find window number "winnr" (counting top to bottom).
4199 */
4200 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004201win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
4203 win_T *wp;
4204
Bram Moolenaar29323592016-07-24 22:04:11 +02004205 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 if (--winnr == 0)
4207 break;
4208 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209}
4210#endif
4211
Bram Moolenaar4033c552017-09-16 20:54:51 +02004212#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004213/*
4214 * Find the tabpage for window "win".
4215 */
4216 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004217win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004218{
4219 win_T *wp;
4220 tabpage_T *tp;
4221
Bram Moolenaar29323592016-07-24 22:04:11 +02004222 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004223 if (wp == win)
4224 return tp;
4225 return NULL;
4226}
4227#endif
4228
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004230 * Get the above or below neighbor window of the specified window.
4231 * up - TRUE for the above neighbor
4232 * count - nth neighbor window
4233 * Returns the specified window if the neighbor is not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 */
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004235 win_T *
4236win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 frame_T *fr;
4239 frame_T *nfr;
4240 frame_T *foundfr;
4241
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004242 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 while (count--)
4244 {
4245 /*
4246 * First go upwards in the tree of frames until we find a upwards or
4247 * downwards neighbor.
4248 */
4249 fr = foundfr;
4250 for (;;)
4251 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004252 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 goto end;
4254 if (up)
4255 nfr = fr->fr_prev;
4256 else
4257 nfr = fr->fr_next;
4258 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4259 break;
4260 fr = fr->fr_parent;
4261 }
4262
4263 /*
4264 * Now go downwards to find the bottom or top frame in it.
4265 */
4266 for (;;)
4267 {
4268 if (nfr->fr_layout == FR_LEAF)
4269 {
4270 foundfr = nfr;
4271 break;
4272 }
4273 fr = nfr->fr_child;
4274 if (nfr->fr_layout == FR_ROW)
4275 {
4276 /* Find the frame at the cursor row. */
4277 while (fr->fr_next != NULL
4278 && frame2win(fr)->w_wincol + fr->fr_width
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004279 <= wp->w_wincol + wp->w_wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 fr = fr->fr_next;
4281 }
4282 if (nfr->fr_layout == FR_COL && up)
4283 while (fr->fr_next != NULL)
4284 fr = fr->fr_next;
4285 nfr = fr;
4286 }
4287 }
4288end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004289 return foundfr != NULL ? foundfr->fr_win : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290}
4291
4292/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004293 * Move to window above or below "count" times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 */
4295 static void
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004296win_goto_ver(
4297 int up, // TRUE to go to win above
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004298 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004300 win_T *win;
4301
4302 win = win_vert_neighbor(curtab, curwin, up, count);
4303 if (win != NULL)
4304 win_goto(win);
4305}
4306
4307/*
4308 * Get the left or right neighbor window of the specified window.
4309 * left - TRUE for the left neighbor
4310 * count - nth neighbor window
4311 * Returns the specified window if the neighbor is not found.
4312 */
4313 win_T *
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02004314win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count)
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004315{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 frame_T *fr;
4317 frame_T *nfr;
4318 frame_T *foundfr;
4319
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004320 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 while (count--)
4322 {
4323 /*
4324 * First go upwards in the tree of frames until we find a left or
4325 * right neighbor.
4326 */
4327 fr = foundfr;
4328 for (;;)
4329 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004330 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 goto end;
4332 if (left)
4333 nfr = fr->fr_prev;
4334 else
4335 nfr = fr->fr_next;
4336 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4337 break;
4338 fr = fr->fr_parent;
4339 }
4340
4341 /*
4342 * Now go downwards to find the leftmost or rightmost frame in it.
4343 */
4344 for (;;)
4345 {
4346 if (nfr->fr_layout == FR_LEAF)
4347 {
4348 foundfr = nfr;
4349 break;
4350 }
4351 fr = nfr->fr_child;
4352 if (nfr->fr_layout == FR_COL)
4353 {
4354 /* Find the frame at the cursor row. */
4355 while (fr->fr_next != NULL
4356 && frame2win(fr)->w_winrow + fr->fr_height
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004357 <= wp->w_winrow + wp->w_wrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 fr = fr->fr_next;
4359 }
4360 if (nfr->fr_layout == FR_ROW && left)
4361 while (fr->fr_next != NULL)
4362 fr = fr->fr_next;
4363 nfr = fr;
4364 }
4365 }
4366end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004367 return foundfr != NULL ? foundfr->fr_win : NULL;
4368}
4369
4370/*
4371 * Move to left or right window.
4372 */
4373 static void
4374win_goto_hor(
4375 int left, // TRUE to go to left win
4376 long count)
4377{
4378 win_T *win;
4379
4380 win = win_horz_neighbor(curtab, curwin, left, count);
4381 if (win != NULL)
4382 win_goto(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385/*
4386 * Make window "wp" the current window.
4387 */
4388 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004389win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004391 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392}
4393
4394/*
4395 * Make window wp the current window.
4396 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4397 * been closed and isn't valid.
4398 */
4399 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004400win_enter_ext(
4401 win_T *wp,
4402 int undo_sync,
4403 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004404 int trigger_new_autocmds,
4405 int trigger_enter_autocmds,
4406 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
4410 if (wp == curwin && !curwin_invalid) /* nothing to do */
4411 return;
4412
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004413#ifdef FEAT_JOB_CHANNEL
4414 if (!curwin_invalid)
4415 leaving_window(curwin);
4416#endif
4417
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004418 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420 /*
4421 * Be careful: If autocommands delete the window, return now.
4422 */
4423 if (wp->w_buffer != curbuf)
4424 {
4425 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4426 other_buffer = TRUE;
4427 if (!win_valid(wp))
4428 return;
4429 }
4430 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4431 if (!win_valid(wp))
4432 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004433#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 /* autocmds may abort script processing */
4435 if (aborting())
4436 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 /* sync undo before leaving the current buffer */
4441 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004442 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004443
4444 /* Might need to scroll the old window before switching, e.g., when the
4445 * cursor was moved. */
4446 update_topline();
4447
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 /* may have to copy the buffer options when 'cpo' contains 'S' */
4449 if (wp->w_buffer != curbuf)
4450 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4451 if (!curwin_invalid)
4452 {
4453 prevwin = curwin; /* remember for CTRL-W p */
4454 curwin->w_redr_status = TRUE;
4455 }
4456 curwin = wp;
4457 curbuf = wp->w_buffer;
4458 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 if (!virtual_active())
4460 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 changed_line_abv_curs(); /* assume cursor position needs updating */
4462
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004463 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004465 char_u *dirname;
4466
4467 // Window or tab has a local directory: Save current directory as
4468 // global directory (unless that was done already) and change to the
4469 // local directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 if (globaldir == NULL)
4471 {
4472 char_u cwd[MAXPATHL];
4473
4474 if (mch_dirname(cwd, MAXPATHL) == OK)
4475 globaldir = vim_strsave(cwd);
4476 }
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004477 if (curwin->w_localdir != NULL)
4478 dirname = curwin->w_localdir;
4479 else
4480 dirname = curtab->tp_localdir;
4481
4482 if (mch_chdir((char *)dirname) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004483 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 }
4485 else if (globaldir != NULL)
4486 {
4487 /* Window doesn't have a local directory and we are not in the global
4488 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004489 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004490 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 shorten_fnames(TRUE);
4492 }
4493
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004494#ifdef FEAT_JOB_CHANNEL
4495 entering_window(curwin);
4496#endif
Bram Moolenaarc917da42016-07-19 22:31:36 +02004497 if (trigger_new_autocmds)
4498 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004499 if (trigger_enter_autocmds)
4500 {
4501 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4502 if (other_buffer)
4503 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506#ifdef FEAT_TITLE
4507 maketitle();
4508#endif
4509 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004510 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 if (restart_edit)
4512 redraw_later(VALID); /* causes status line redraw */
4513
4514 /* set window height to desired minimal value */
4515 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4516 win_setheight((int)p_wh);
4517 else if (curwin->w_height == 0)
4518 win_setheight(1);
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004521 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524#ifdef FEAT_MOUSE
4525 setmouse(); /* in case jumped to/from help buffer */
4526#endif
4527
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004528 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004529 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530}
4531
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004534 * Jump to the first open window that contains buffer "buf", if one exists.
4535 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 */
4537 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004538buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004540 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004542 if (curwin->w_buffer == buf)
4543 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004544 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004545 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004546 if (wp->w_buffer == buf)
4547 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (wp != NULL)
4549 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004550 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004552
4553/*
4554 * Jump to the first open window in any tab page that contains buffer "buf",
4555 * if one exists.
4556 * Returns a pointer to the window found, otherwise NULL.
4557 */
4558 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004559buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004560{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004561 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004562 tabpage_T *tp;
4563
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004564 if (wp != NULL)
4565 return wp;
4566
Bram Moolenaar29323592016-07-24 22:04:11 +02004567 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004568 if (tp != curtab)
4569 {
4570 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4571 if (wp->w_buffer == buf)
4572 break;
4573 if (wp != NULL)
4574 {
4575 goto_tabpage_win(tp, wp);
4576 if (curwin != wp)
4577 wp = NULL; /* something went wrong */
4578 break;
4579 }
4580 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004581 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004582}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004584static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004585
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004587 * Allocate a window structure and link it in the window list when "hidden" is
4588 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004591win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004593 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
4595 /*
4596 * allocate window structure and linesizes arrays
4597 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004598 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004599 if (new_wp == NULL)
4600 return NULL;
4601
4602 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004604 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004605 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607
Bram Moolenaar86edef62016-03-13 18:07:30 +01004608 new_wp->w_id = ++last_win_id;
4609
Bram Moolenaar429fa852013-04-15 12:27:36 +02004610#ifdef FEAT_EVAL
4611 /* init w: variables */
4612 new_wp->w_vars = dict_alloc();
4613 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004615 win_free_lsize(new_wp);
4616 vim_free(new_wp);
4617 return NULL;
4618 }
4619 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004620#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004621
Bram Moolenaar429fa852013-04-15 12:27:36 +02004622 /* Don't execute autocommands while the window is not properly
4623 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4624 * event. */
4625 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004626
Bram Moolenaar429fa852013-04-15 12:27:36 +02004627 /*
4628 * link the window in the window list
4629 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004630 if (!hidden)
4631 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004632 new_wp->w_wincol = 0;
4633 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
Bram Moolenaar429fa852013-04-15 12:27:36 +02004635 /* position the display and the cursor at the top of the file. */
4636 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004638 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004640 new_wp->w_botline = 2;
4641 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004642 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
Bram Moolenaar375e3392019-01-31 18:26:10 +01004644 // use global option value for global-local options
4645 new_wp->w_p_so = -1;
4646 new_wp->w_p_siso = -1;
4647
Bram Moolenaar429fa852013-04-15 12:27:36 +02004648 /* We won't calculate w_fraction until resizing the window */
4649 new_wp->w_fraction = 0;
4650 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
4652#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004653 if (gui.in_use)
4654 {
4655 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4656 SBAR_LEFT, new_wp);
4657 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4658 SBAR_RIGHT, new_wp);
4659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660#endif
4661#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004662 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004664 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004665#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004666 new_wp->w_match_head = NULL;
4667 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004668#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004669 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670}
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004673 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 */
4675 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004676win_free(
4677 win_T *wp,
4678 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
4680 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004681 buf_T *buf;
4682 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004684#ifdef FEAT_FOLDING
4685 clearFolding(wp);
4686#endif
4687
4688 /* reduce the reference count to the argument list. */
4689 alist_unlink(wp->w_alist);
4690
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004691 /* Don't execute autocommands while the window is halfway being deleted.
4692 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004693 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004694
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004695#ifdef FEAT_LUA
4696 lua_window_free(wp);
4697#endif
4698
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004699#ifdef FEAT_MZSCHEME
4700 mzscheme_window_free(wp);
4701#endif
4702
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703#ifdef FEAT_PERL
4704 perl_win_free(wp);
4705#endif
4706
4707#ifdef FEAT_PYTHON
4708 python_window_free(wp);
4709#endif
4710
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004711#ifdef FEAT_PYTHON3
4712 python3_window_free(wp);
4713#endif
4714
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715#ifdef FEAT_TCL
4716 tcl_window_free(wp);
4717#endif
4718
4719#ifdef FEAT_RUBY
4720 ruby_window_free(wp);
4721#endif
4722
4723 clear_winopt(&wp->w_onebuf_opt);
4724 clear_winopt(&wp->w_allbuf_opt);
4725
4726#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004727 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4728 hash_init(&wp->w_vars->dv_hashtab);
4729 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730#endif
4731
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004732 {
4733 tabpage_T *ttp;
4734
4735 if (prevwin == wp)
4736 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004737 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004738 if (ttp->tp_prevwin == wp)
4739 ttp->tp_prevwin = NULL;
4740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 win_free_lsize(wp);
4742
4743 for (i = 0; i < wp->w_tagstacklen; ++i)
4744 vim_free(wp->w_tagstack[i].tagname);
4745
4746 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004747
Bram Moolenaarff18df02013-07-24 17:51:57 +02004748 /* Remove the window from the b_wininfo lists, it may happen that the
4749 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004750 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004751 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4752 if (wip->wi_win == wp)
4753 wip->wi_win = NULL;
4754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004756 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#ifdef FEAT_JUMPLIST
4760 free_jumplist(wp);
4761#endif
4762
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004763#ifdef FEAT_QUICKFIX
4764 qf_free_all(wp);
4765#endif
4766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767#ifdef FEAT_GUI
4768 if (gui.in_use)
4769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4771 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4772 }
4773#endif /* FEAT_GUI */
4774
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004775#ifdef FEAT_MENU
4776 remove_winbar(wp);
4777#endif
4778
Bram Moolenaar860cae12010-06-05 23:22:07 +02004779#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004780 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004781#endif
4782
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004783 if (wp != aucmd_win)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004784 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004785 if (autocmd_busy)
4786 {
4787 wp->w_next = au_pending_free_win;
4788 au_pending_free_win = wp;
4789 }
4790 else
4791 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004792
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004793 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794}
4795
4796/*
4797 * Append window "wp" in the window list after window "after".
4798 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004799 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004800win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
4802 win_T *before;
4803
4804 if (after == NULL) /* after NULL is in front of the first */
4805 before = firstwin;
4806 else
4807 before = after->w_next;
4808
4809 wp->w_next = before;
4810 wp->w_prev = after;
4811 if (after == NULL)
4812 firstwin = wp;
4813 else
4814 after->w_next = wp;
4815 if (before == NULL)
4816 lastwin = wp;
4817 else
4818 before->w_prev = wp;
4819}
4820
4821/*
4822 * Remove a window from the window list.
4823 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004824 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004825win_remove(
4826 win_T *wp,
4827 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
4829 if (wp->w_prev != NULL)
4830 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004831 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004832 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004833 else
4834 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004835
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 if (wp->w_next != NULL)
4837 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004838 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004839 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004840 else
4841 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842}
4843
4844/*
4845 * Append frame "frp" in a frame list after frame "after".
4846 */
4847 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004848frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
4850 frp->fr_next = after->fr_next;
4851 after->fr_next = frp;
4852 if (frp->fr_next != NULL)
4853 frp->fr_next->fr_prev = frp;
4854 frp->fr_prev = after;
4855}
4856
4857/*
4858 * Insert frame "frp" in a frame list before frame "before".
4859 */
4860 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004861frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 frp->fr_next = before;
4864 frp->fr_prev = before->fr_prev;
4865 before->fr_prev = frp;
4866 if (frp->fr_prev != NULL)
4867 frp->fr_prev->fr_next = frp;
4868 else
4869 frp->fr_parent->fr_child = frp;
4870}
4871
4872/*
4873 * Remove a frame from a frame list.
4874 */
4875 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004876frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
4878 if (frp->fr_prev != NULL)
4879 frp->fr_prev->fr_next = frp->fr_next;
4880 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004883 /* special case: topframe->fr_child == frp */
4884 if (topframe->fr_child == frp)
4885 topframe->fr_child = frp->fr_next;
4886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (frp->fr_next != NULL)
4888 frp->fr_next->fr_prev = frp->fr_prev;
4889}
4890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891/*
4892 * Allocate w_lines[] for window "wp".
4893 * Return FAIL for failure, OK for success.
4894 */
4895 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004896win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
4898 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004899 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 if (wp->w_lines == NULL)
4901 return FAIL;
4902 return OK;
4903}
4904
4905/*
4906 * free lsize arrays for a window
4907 */
4908 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004909win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004911 /* TODO: why would wp be NULL here? */
4912 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004913 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914}
4915
4916/*
4917 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004918 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 */
4920 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004921shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004923 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
4925 if (firstwin == NULL) /* not initialized yet */
4926 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (h < frame_minheight(topframe, NULL))
4928 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004929
4930 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 * that doesn't result in the right height, forget about that option. */
4932 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004933 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 frame_new_height(topframe, h, FALSE, FALSE);
4935
4936 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004938 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004939
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#if 0
4941 /* Disabled: don't want making the screen smaller make a window larger. */
4942 if (p_ea)
4943 win_equal(curwin, FALSE, 'v');
4944#endif
4945}
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947/*
4948 * Called from win_new_shellsize() after Columns changed.
4949 */
4950 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004951shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952{
4953 if (firstwin == NULL) /* not initialized yet */
4954 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004955
4956 /* First try setting the widths of windows with 'winfixwidth'. If that
4957 * doesn't result in the right width, forget about that option. */
4958 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004959 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004960 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4961
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4963#if 0
4964 /* Disabled: don't want making the screen smaller make a window larger. */
4965 if (p_ea)
4966 win_equal(curwin, FALSE, 'h');
4967#endif
4968}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969
4970#if defined(FEAT_CMDWIN) || defined(PROTO)
4971/*
4972 * Save the size of all windows in "gap".
4973 */
4974 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004975win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977{
4978 win_T *wp;
4979
4980 ga_init2(gap, (int)sizeof(int), 1);
4981 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004982 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 ((int *)gap->ga_data)[gap->ga_len++] =
4985 wp->w_width + wp->w_vsep_width;
4986 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4987 }
4988}
4989
4990/*
4991 * Restore window sizes, but only if the number of windows is still the same.
4992 * Does not free the growarray.
4993 */
4994 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004995win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
4997 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02004998 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
5000 if (win_count() * 2 == gap->ga_len)
5001 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005002 /* The order matters, because frames contain other frames, but it's
5003 * difficult to get right. The easy way out is to do it twice. */
5004 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005006 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02005007 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02005008 {
5009 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5010 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013 /* recompute the window positions */
5014 (void)win_comp_pos();
5015 }
5016}
5017#endif /* FEAT_CMDWIN */
5018
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019/*
5020 * Update the position for all windows, using the width and height of the
5021 * frames.
5022 * Returns the row just after the last window.
5023 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005024 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005025win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005027 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 int col = 0;
5029
5030 frame_comp_pos(topframe, &row, &col);
5031 return row;
5032}
5033
5034/*
5035 * Update the position of the windows in frame "topfrp", using the width and
5036 * height of the frames.
5037 * "*row" and "*col" are the top-left position of the frame. They are updated
5038 * to the bottom-right position plus one.
5039 */
5040 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005041frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
5043 win_T *wp;
5044 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 int startcol;
5046 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005047 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 wp = topfrp->fr_win;
5050 if (wp != NULL)
5051 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005052 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 /* position changed, redraw */
5055 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 redraw_win_later(wp, NOT_VALID);
5058 wp->w_redr_status = TRUE;
5059 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005060 /* WinBar will not show if the window height is zero */
5061 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5062 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 else
5066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 startrow = *row;
5068 startcol = *col;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005069 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (topfrp->fr_layout == FR_ROW)
5072 *row = startrow; /* all frames are at the same row */
5073 else
5074 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 frame_comp_pos(frp, row, col);
5076 }
5077 }
5078}
5079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080/*
5081 * Set current window height and take care of repositioning other windows to
5082 * fit around it.
5083 */
5084 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005085win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 win_setheight_win(height, curwin);
5088}
5089
5090/*
5091 * Set the window height of window "win" and take care of repositioning other
5092 * windows to fit around it.
5093 */
5094 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005095win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
5097 int row;
5098
5099 if (win == curwin)
5100 {
5101 /* Always keep current window at least one line high, even when
5102 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 if (height < p_wmh)
5104 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (height == 0)
5106 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005107 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 frame_setheight(win->w_frame, height + win->w_status_height);
5111
5112 /* recompute the window positions */
5113 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 /*
5116 * If there is extra space created between the last window and the command
5117 * line, clear it.
5118 */
5119 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5120 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5121 cmdline_row = row;
5122 msg_row = row;
5123 msg_col = 0;
5124
5125 redraw_all_later(NOT_VALID);
5126}
5127
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128/*
5129 * Set the height of a frame to "height" and take care that all frames and
5130 * windows inside it are resized. Also resize frames on the left and right if
5131 * the are in the same FR_ROW frame.
5132 *
5133 * Strategy:
5134 * If the frame is part of a FR_COL frame, try fitting the frame in that
5135 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5136 * go to containing frames to resize them and make room.
5137 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5138 * Check for the minimal height of the FR_ROW frame.
5139 * At the top level we can also use change the command line height.
5140 */
5141 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005142frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143{
5144 int room; /* total number of lines available */
5145 int take; /* number of lines taken from other windows */
5146 int room_cmdline; /* lines available from cmdline */
5147 int run;
5148 frame_T *frp;
5149 int h;
5150 int room_reserved;
5151
5152 /* If the height already is the desired value, nothing to do. */
5153 if (curfrp->fr_height == height)
5154 return;
5155
5156 if (curfrp->fr_parent == NULL)
5157 {
5158 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005159 if (height > ROWS_AVAIL)
5160 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 if (height > 0)
5162 frame_new_height(curfrp, height, FALSE, FALSE);
5163 }
5164 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5165 {
5166 /* Row of frames: Also need to resize frames left and right of this
5167 * one. First check for the minimal height of these. */
5168 h = frame_minheight(curfrp->fr_parent, NULL);
5169 if (height < h)
5170 height = h;
5171 frame_setheight(curfrp->fr_parent, height);
5172 }
5173 else
5174 {
5175 /*
5176 * Column of frames: try to change only frames in this column.
5177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 /*
5179 * Do this twice:
5180 * 1: compute room available, if it's not enough try resizing the
5181 * containing frame.
5182 * 2: compute the room available and adjust the height to it.
5183 * Try not to reduce the height of a window with 'winfixheight' set.
5184 */
5185 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
5187 room = 0;
5188 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005189 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
5191 if (frp != curfrp
5192 && frp->fr_win != NULL
5193 && frp->fr_win->w_p_wfh)
5194 room_reserved += frp->fr_height;
5195 room += frp->fr_height;
5196 if (frp != curfrp)
5197 room -= frame_minheight(frp, NULL);
5198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (curfrp->fr_width != Columns)
5200 room_cmdline = 0;
5201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
5203 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005204 + VISIBLE_HEIGHT(lastwin)
5205 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (room_cmdline < 0)
5207 room_cmdline = 0;
5208 }
5209
5210 if (height <= room + room_cmdline)
5211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
5214 if (height > room + room_cmdline)
5215 height = room + room_cmdline;
5216 break;
5217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 frame_setheight(curfrp->fr_parent, height
5219 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221
5222 /*
5223 * Compute the number of lines we will take from others frames (can be
5224 * negative!).
5225 */
5226 take = height - curfrp->fr_height;
5227
5228 /* If there is not enough room, also reduce the height of a window
5229 * with 'winfixheight' set. */
5230 if (height > room + room_cmdline - room_reserved)
5231 room_reserved = room + room_cmdline - height;
5232 /* If there is only a 'winfixheight' window and making the
5233 * window smaller, need to make the other window taller. */
5234 if (take < 0 && room - curfrp->fr_height < room_reserved)
5235 room_reserved = 0;
5236
5237 if (take > 0 && room_cmdline > 0)
5238 {
5239 /* use lines from cmdline first */
5240 if (take < room_cmdline)
5241 room_cmdline = take;
5242 take -= room_cmdline;
5243 topframe->fr_height += room_cmdline;
5244 }
5245
5246 /*
5247 * set the current frame to the new height
5248 */
5249 frame_new_height(curfrp, height, FALSE, FALSE);
5250
5251 /*
5252 * First take lines from the frames after the current frame. If
5253 * that is not enough, takes lines from frames above the current
5254 * frame.
5255 */
5256 for (run = 0; run < 2; ++run)
5257 {
5258 if (run == 0)
5259 frp = curfrp->fr_next; /* 1st run: start with next window */
5260 else
5261 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5262 while (frp != NULL && take != 0)
5263 {
5264 h = frame_minheight(frp, NULL);
5265 if (room_reserved > 0
5266 && frp->fr_win != NULL
5267 && frp->fr_win->w_p_wfh)
5268 {
5269 if (room_reserved >= frp->fr_height)
5270 room_reserved -= frp->fr_height;
5271 else
5272 {
5273 if (frp->fr_height - room_reserved > take)
5274 room_reserved = frp->fr_height - take;
5275 take -= frp->fr_height - room_reserved;
5276 frame_new_height(frp, room_reserved, FALSE, FALSE);
5277 room_reserved = 0;
5278 }
5279 }
5280 else
5281 {
5282 if (frp->fr_height - take < h)
5283 {
5284 take -= frp->fr_height - h;
5285 frame_new_height(frp, h, FALSE, FALSE);
5286 }
5287 else
5288 {
5289 frame_new_height(frp, frp->fr_height - take,
5290 FALSE, FALSE);
5291 take = 0;
5292 }
5293 }
5294 if (run == 0)
5295 frp = frp->fr_next;
5296 else
5297 frp = frp->fr_prev;
5298 }
5299 }
5300 }
5301}
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303/*
5304 * Set current window width and take care of repositioning other windows to
5305 * fit around it.
5306 */
5307 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005308win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
5310 win_setwidth_win(width, curwin);
5311}
5312
5313 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005314win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
5316 /* Always keep current window at least one column wide, even when
5317 * 'winminwidth' is zero. */
5318 if (wp == curwin)
5319 {
5320 if (width < p_wmw)
5321 width = p_wmw;
5322 if (width == 0)
5323 width = 1;
5324 }
5325
5326 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5327
5328 /* recompute the window positions */
5329 (void)win_comp_pos();
5330
5331 redraw_all_later(NOT_VALID);
5332}
5333
5334/*
5335 * Set the width of a frame to "width" and take care that all frames and
5336 * windows inside it are resized. Also resize frames above and below if the
5337 * are in the same FR_ROW frame.
5338 *
5339 * Strategy is similar to frame_setheight().
5340 */
5341 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005342frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
5344 int room; /* total number of lines available */
5345 int take; /* number of lines taken from other windows */
5346 int run;
5347 frame_T *frp;
5348 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005349 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
5351 /* If the width already is the desired value, nothing to do. */
5352 if (curfrp->fr_width == width)
5353 return;
5354
5355 if (curfrp->fr_parent == NULL)
5356 /* topframe: can't change width */
5357 return;
5358
5359 if (curfrp->fr_parent->fr_layout == FR_COL)
5360 {
5361 /* Column of frames: Also need to resize frames above and below of
5362 * this one. First check for the minimal width of these. */
5363 w = frame_minwidth(curfrp->fr_parent, NULL);
5364 if (width < w)
5365 width = w;
5366 frame_setwidth(curfrp->fr_parent, width);
5367 }
5368 else
5369 {
5370 /*
5371 * Row of frames: try to change only frames in this row.
5372 *
5373 * Do this twice:
5374 * 1: compute room available, if it's not enough try resizing the
5375 * containing frame.
5376 * 2: compute the room available and adjust the width to it.
5377 */
5378 for (run = 1; run <= 2; ++run)
5379 {
5380 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005381 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005382 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005384 if (frp != curfrp
5385 && frp->fr_win != NULL
5386 && frp->fr_win->w_p_wfw)
5387 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 room += frp->fr_width;
5389 if (frp != curfrp)
5390 room -= frame_minwidth(frp, NULL);
5391 }
5392
5393 if (width <= room)
5394 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005395 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 {
5397 if (width > room)
5398 width = room;
5399 break;
5400 }
5401 frame_setwidth(curfrp->fr_parent, width
5402 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5403 }
5404
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 /*
5406 * Compute the number of lines we will take from others frames (can be
5407 * negative!).
5408 */
5409 take = width - curfrp->fr_width;
5410
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005411 /* If there is not enough room, also reduce the width of a window
5412 * with 'winfixwidth' set. */
5413 if (width > room - room_reserved)
5414 room_reserved = room - width;
5415 /* If there is only a 'winfixwidth' window and making the
5416 * window smaller, need to make the other window narrower. */
5417 if (take < 0 && room - curfrp->fr_width < room_reserved)
5418 room_reserved = 0;
5419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 /*
5421 * set the current frame to the new width
5422 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005423 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424
5425 /*
5426 * First take lines from the frames right of the current frame. If
5427 * that is not enough, takes lines from frames left of the current
5428 * frame.
5429 */
5430 for (run = 0; run < 2; ++run)
5431 {
5432 if (run == 0)
5433 frp = curfrp->fr_next; /* 1st run: start with next window */
5434 else
5435 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5436 while (frp != NULL && take != 0)
5437 {
5438 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005439 if (room_reserved > 0
5440 && frp->fr_win != NULL
5441 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005443 if (room_reserved >= frp->fr_width)
5444 room_reserved -= frp->fr_width;
5445 else
5446 {
5447 if (frp->fr_width - room_reserved > take)
5448 room_reserved = frp->fr_width - take;
5449 take -= frp->fr_width - room_reserved;
5450 frame_new_width(frp, room_reserved, FALSE, FALSE);
5451 room_reserved = 0;
5452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
5454 else
5455 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005456 if (frp->fr_width - take < w)
5457 {
5458 take -= frp->fr_width - w;
5459 frame_new_width(frp, w, FALSE, FALSE);
5460 }
5461 else
5462 {
5463 frame_new_width(frp, frp->fr_width - take,
5464 FALSE, FALSE);
5465 take = 0;
5466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468 if (run == 0)
5469 frp = frp->fr_next;
5470 else
5471 frp = frp->fr_prev;
5472 }
5473 }
5474 }
5475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005478 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
5480 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005481win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
5483 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005484 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005487 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 while (p_wmh > 0)
5489 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005490 room = Rows - p_ch;
5491 needed = frame_minheight(topframe, NULL);
5492 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 break;
5494 --p_wmh;
5495 if (first)
5496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005497 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 first = FALSE;
5499 }
5500 }
5501}
5502
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005503/*
5504 * Check 'winminwidth' for a valid value and reduce it if needed.
5505 */
5506 void
5507win_setminwidth(void)
5508{
5509 int room;
5510 int needed;
5511 int first = TRUE;
5512
5513 // loop until there is a 'winminheight' that is possible
5514 while (p_wmw > 0)
5515 {
5516 room = Columns;
5517 needed = frame_minwidth(topframe, NULL);
5518 if (room >= needed)
5519 break;
5520 --p_wmw;
5521 if (first)
5522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005523 emsg(_(e_noroom));
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005524 first = FALSE;
5525 }
5526 }
5527}
5528
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005529#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531/*
5532 * Status line of dragwin is dragged "offset" lines down (negative is up).
5533 */
5534 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005535win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 frame_T *curfr;
5538 frame_T *fr;
5539 int room;
5540 int row;
5541 int up; /* if TRUE, drag status line up, otherwise down */
5542 int n;
5543
5544 fr = dragwin->w_frame;
5545 curfr = fr;
5546 if (fr != topframe) /* more than one window */
5547 {
5548 fr = fr->fr_parent;
5549 /* When the parent frame is not a column of frames, its parent should
5550 * be. */
5551 if (fr->fr_layout != FR_COL)
5552 {
5553 curfr = fr;
5554 if (fr != topframe) /* only a row of windows, may drag statusline */
5555 fr = fr->fr_parent;
5556 }
5557 }
5558
5559 /* If this is the last frame in a column, may want to resize the parent
5560 * frame instead (go two up to skip a row of frames). */
5561 while (curfr != topframe && curfr->fr_next == NULL)
5562 {
5563 if (fr != topframe)
5564 fr = fr->fr_parent;
5565 curfr = fr;
5566 if (fr != topframe)
5567 fr = fr->fr_parent;
5568 }
5569
5570 if (offset < 0) /* drag up */
5571 {
5572 up = TRUE;
5573 offset = -offset;
5574 /* sum up the room of the current frame and above it */
5575 if (fr == curfr)
5576 {
5577 /* only one window */
5578 room = fr->fr_height - frame_minheight(fr, NULL);
5579 }
5580 else
5581 {
5582 room = 0;
5583 for (fr = fr->fr_child; ; fr = fr->fr_next)
5584 {
5585 room += fr->fr_height - frame_minheight(fr, NULL);
5586 if (fr == curfr)
5587 break;
5588 }
5589 }
5590 fr = curfr->fr_next; /* put fr at frame that grows */
5591 }
5592 else /* drag down */
5593 {
5594 up = FALSE;
5595 /*
5596 * Only dragging the last status line can reduce p_ch.
5597 */
5598 room = Rows - cmdline_row;
5599 if (curfr->fr_next == NULL)
5600 room -= 1;
5601 else
5602 room -= p_ch;
5603 if (room < 0)
5604 room = 0;
5605 /* sum up the room of frames below of the current one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005606 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 room += fr->fr_height - frame_minheight(fr, NULL);
5608 fr = curfr; /* put fr at window that grows */
5609 }
5610
5611 if (room < offset) /* Not enough room */
5612 offset = room; /* Move as far as we can */
5613 if (offset <= 0)
5614 return;
5615
5616 /*
5617 * Grow frame fr by "offset" lines.
5618 * Doesn't happen when dragging the last status line up.
5619 */
5620 if (fr != NULL)
5621 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5622
5623 if (up)
5624 fr = curfr; /* current frame gets smaller */
5625 else
5626 fr = curfr->fr_next; /* next frame gets smaller */
5627
5628 /*
5629 * Now make the other frames smaller.
5630 */
5631 while (fr != NULL && offset > 0)
5632 {
5633 n = frame_minheight(fr, NULL);
5634 if (fr->fr_height - offset <= n)
5635 {
5636 offset -= fr->fr_height - n;
5637 frame_new_height(fr, n, !up, FALSE);
5638 }
5639 else
5640 {
5641 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5642 break;
5643 }
5644 if (up)
5645 fr = fr->fr_prev;
5646 else
5647 fr = fr->fr_next;
5648 }
5649 row = win_comp_pos();
5650 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5651 cmdline_row = row;
5652 p_ch = Rows - cmdline_row;
5653 if (p_ch < 1)
5654 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005655 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005656 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 showmode();
5658}
5659
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660/*
5661 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5662 */
5663 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005664win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 frame_T *curfr;
5667 frame_T *fr;
5668 int room;
5669 int left; /* if TRUE, drag separator line left, otherwise right */
5670 int n;
5671
5672 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005673 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 return;
5675 curfr = fr;
5676 fr = fr->fr_parent;
5677 /* When the parent frame is not a row of frames, its parent should be. */
5678 if (fr->fr_layout != FR_ROW)
5679 {
5680 if (fr == topframe) /* only a column of windows (cannot happen?) */
5681 return;
5682 curfr = fr;
5683 fr = fr->fr_parent;
5684 }
5685
5686 /* If this is the last frame in a row, may want to resize a parent
5687 * frame instead. */
5688 while (curfr->fr_next == NULL)
5689 {
5690 if (fr == topframe)
5691 break;
5692 curfr = fr;
5693 fr = fr->fr_parent;
5694 if (fr != topframe)
5695 {
5696 curfr = fr;
5697 fr = fr->fr_parent;
5698 }
5699 }
5700
5701 if (offset < 0) /* drag left */
5702 {
5703 left = TRUE;
5704 offset = -offset;
5705 /* sum up the room of the current frame and left of it */
5706 room = 0;
5707 for (fr = fr->fr_child; ; fr = fr->fr_next)
5708 {
5709 room += fr->fr_width - frame_minwidth(fr, NULL);
5710 if (fr == curfr)
5711 break;
5712 }
5713 fr = curfr->fr_next; /* put fr at frame that grows */
5714 }
5715 else /* drag right */
5716 {
5717 left = FALSE;
5718 /* sum up the room of frames right of the current one */
5719 room = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005720 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 room += fr->fr_width - frame_minwidth(fr, NULL);
5722 fr = curfr; /* put fr at window that grows */
5723 }
5724
5725 if (room < offset) /* Not enough room */
5726 offset = room; /* Move as far as we can */
5727 if (offset <= 0) /* No room at all, quit. */
5728 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005729 if (fr == NULL)
5730 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005733 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734
5735 /* shrink other frames: current and at the left or at the right */
5736 if (left)
5737 fr = curfr; /* current frame gets smaller */
5738 else
5739 fr = curfr->fr_next; /* next frame gets smaller */
5740
5741 while (fr != NULL && offset > 0)
5742 {
5743 n = frame_minwidth(fr, NULL);
5744 if (fr->fr_width - offset <= n)
5745 {
5746 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005747 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749 else
5750 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005751 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 break;
5753 }
5754 if (left)
5755 fr = fr->fr_prev;
5756 else
5757 fr = fr->fr_next;
5758 }
5759 (void)win_comp_pos();
5760 redraw_all_later(NOT_VALID);
5761}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762#endif /* FEAT_MOUSE */
5763
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005764#define FRACTION_MULT 16384L
5765
5766/*
5767 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005768 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005769 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005770 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005771set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005772{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005773 if (wp->w_height > 1)
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005774 // When cursor is in the first line the percentage is computed as if
5775 // it's halfway that line. Thus with two lines it is 25%, with three
5776 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005777 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005778 + FRACTION_MULT / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005779}
5780
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781/*
5782 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005783 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 * This takes care of the things inside the window, not what happens to the
5785 * window position, the frame or to other windows.
5786 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005787 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005788win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005790 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
5792 /* Don't want a negative height. Happens when splitting a tiny window.
5793 * Will equalize heights soon to fix it. */
5794 if (height < 0)
5795 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005796 if (wp->w_height == height)
5797 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005799 if (wp->w_height > 0)
5800 {
5801 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005802 /* w_wrow needs to be valid. When setting 'laststatus' this may
5803 * call win_new_height() recursively. */
5804 validate_cursor();
5805 if (wp->w_height != prev_height)
5806 return; /* Recursive call already changed the size, bail out here
5807 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005808 if (wp->w_wrow != wp->w_prev_fraction_row)
5809 set_fraction(wp);
5810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
5812 wp->w_height = height;
5813 wp->w_skipcol = 0;
5814
Bram Moolenaar955f1982017-02-05 15:10:51 +01005815 /* There is no point in adjusting the scroll position when exiting. Some
5816 * values might be invalid. */
5817 if (!exiting)
5818 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005819}
5820
5821 void
5822scroll_to_fraction(win_T *wp, int prev_height)
5823{
5824 linenr_T lnum;
5825 int sline, line_size;
5826 int height = wp->w_height;
5827
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005828 // Don't change w_topline when height is zero. Don't set w_topline when
5829 // 'scrollbind' is set and this isn't the current window.
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005830 if (height > 0 && (!wp->w_p_scb || wp == curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005832 /*
5833 * Find a value for w_topline that shows the cursor at the same
5834 * relative position in the window as before (more or less).
5835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 lnum = wp->w_cursor.lnum;
5837 if (lnum < 1) /* can happen when starting up */
5838 lnum = 1;
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005839 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L)
5840 / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5842 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005843
5844 if (sline >= 0)
5845 {
5846 /* Make sure the whole cursor line is visible, if possible. */
5847 int rows = plines_win(wp, lnum, FALSE);
5848
5849 if (sline > wp->w_height - rows)
5850 {
5851 sline = wp->w_height - rows;
5852 wp->w_wrow -= rows - line_size;
5853 }
5854 }
5855
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 if (sline < 0)
5857 {
5858 /*
5859 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005860 * Make cursor line the first line in the window. If not enough
5861 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 */
5863 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005864 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005865 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005866 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005867 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005868 --wp->w_wrow;
5869 while (wp->w_wrow >= wp->w_height)
5870 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005871 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005872 + win_col_off2(wp);
5873 --wp->w_wrow;
5874 }
5875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005877 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005879 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
5881#ifdef FEAT_FOLDING
5882 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5883 if (lnum == 1)
5884 {
5885 /* first line in buffer is folded */
5886 line_size = 1;
5887 --sline;
5888 break;
5889 }
5890#endif
5891 --lnum;
5892#ifdef FEAT_DIFF
5893 if (lnum == wp->w_topline)
5894 line_size = plines_win_nofill(wp, lnum, TRUE)
5895 + wp->w_topfill;
5896 else
5897#endif
5898 line_size = plines_win(wp, lnum, TRUE);
5899 sline -= line_size;
5900 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (sline < 0)
5903 {
5904 /*
5905 * Line we want at top would go off top of screen. Use next
5906 * line instead.
5907 */
5908#ifdef FEAT_FOLDING
5909 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5910#endif
5911 lnum++;
5912 wp->w_wrow -= line_size + sline;
5913 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005914 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 {
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005916 // First line of file reached, use that as topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 lnum = 1;
5918 wp->w_wrow -= sline;
5919 }
5920 }
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005921 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 }
5923
5924 if (wp == curwin)
5925 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01005926 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 update_topline();
5928 curs_columns(FALSE); /* validate w_wrow */
5929 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005930 if (prev_height > 0)
5931 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005934 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 invalidate_botline_win(wp);
5937}
5938
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939/*
5940 * Set the width of a window.
5941 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005942 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005943win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 wp->w_width = width;
5946 wp->w_lines_valid = 0;
5947 changed_line_abv_curs_win(wp);
5948 invalidate_botline_win(wp);
5949 if (wp == curwin)
5950 {
5951 update_topline();
5952 curs_columns(TRUE); /* validate w_wrow */
5953 }
5954 redraw_win_later(wp, NOT_VALID);
5955 wp->w_redr_status = TRUE;
5956}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957
5958 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005959win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960{
5961 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5962 if (wp->w_p_scr == 0)
5963 wp->w_p_scr = 1;
5964}
5965
5966/*
5967 * command_height: called whenever p_ch has been changed
5968 */
5969 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005970command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 int h;
5973 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005974 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005976 /* Use the value of p_ch that we remembered. This is needed for when the
5977 * GUI starts up, we can't be sure in what order things happen. And when
5978 * p_ch was changed in another tab page. */
5979 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005980
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 /* Find bottom frame with width of screen. */
5982 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5984 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985
5986 /* Avoid changing the height of a window with 'winfixheight' set. */
5987 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5988 && frp->fr_win->w_p_wfh)
5989 frp = frp->fr_prev;
5990
5991 if (starting != NO_SCREEN)
5992 {
5993 cmdline_row = Rows - p_ch;
5994
5995 if (p_ch > old_p_ch) /* p_ch got bigger */
5996 {
5997 while (p_ch > old_p_ch)
5998 {
5999 if (frp == NULL)
6000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006001 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006003 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 cmdline_row = Rows - p_ch;
6005 break;
6006 }
6007 h = frp->fr_height - frame_minheight(frp, NULL);
6008 if (h > p_ch - old_p_ch)
6009 h = p_ch - old_p_ch;
6010 old_p_ch += h;
6011 frame_add_height(frp, -h);
6012 frp = frp->fr_prev;
6013 }
6014
6015 /* Recompute window positions. */
6016 (void)win_comp_pos();
6017
6018 /* clear the lines added to cmdline */
6019 if (full_screen)
6020 screen_fill((int)(cmdline_row), (int)Rows, 0,
6021 (int)Columns, ' ', ' ', 0);
6022 msg_row = cmdline_row;
6023 redraw_cmdline = TRUE;
6024 return;
6025 }
6026
6027 if (msg_row < cmdline_row)
6028 msg_row = cmdline_row;
6029 redraw_cmdline = TRUE;
6030 }
6031 frame_add_height(frp, (int)(old_p_ch - p_ch));
6032
6033 /* Recompute window positions. */
6034 if (frp != lastwin->w_frame)
6035 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036}
6037
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038/*
6039 * Resize frame "frp" to be "n" lines higher (negative for less high).
6040 * Also resize the frames it is contained in.
6041 */
6042 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006043frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6046 for (;;)
6047 {
6048 frp = frp->fr_parent;
6049 if (frp == NULL)
6050 break;
6051 frp->fr_height += n;
6052 }
6053}
6054
6055/*
6056 * Add or remove a status line for the bottom window(s), according to the
6057 * value of 'laststatus'.
6058 */
6059 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006060last_status(
6061 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
6063 /* Don't make a difference between horizontal or vertical split. */
6064 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006065 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066}
6067
6068 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006069last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 frame_T *fp;
6072 win_T *wp;
6073
6074 if (fr->fr_layout == FR_LEAF)
6075 {
6076 wp = fr->fr_win;
6077 if (wp->w_status_height != 0 && !statusline)
6078 {
6079 /* remove status line */
6080 win_new_height(wp, wp->w_height + 1);
6081 wp->w_status_height = 0;
6082 comp_col();
6083 }
6084 else if (wp->w_status_height == 0 && statusline)
6085 {
6086 /* Find a frame to take a line from. */
6087 fp = fr;
6088 while (fp->fr_height <= frame_minheight(fp, NULL))
6089 {
6090 if (fp == topframe)
6091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006092 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 return;
6094 }
6095 /* In a column of frames: go to frame above. If already at
6096 * the top or in a row of frames: go to parent. */
6097 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6098 fp = fp->fr_prev;
6099 else
6100 fp = fp->fr_parent;
6101 }
6102 wp->w_status_height = 1;
6103 if (fp != fr)
6104 {
6105 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6106 frame_fix_height(wp);
6107 (void)win_comp_pos();
6108 }
6109 else
6110 win_new_height(wp, wp->w_height - 1);
6111 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006112 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 }
6114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 else if (fr->fr_layout == FR_ROW)
6116 {
6117 /* vertically split windows, set status line for each one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006118 FOR_ALL_FRAMES(fp, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 last_status_rec(fp, statusline);
6120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 else
6122 {
6123 /* horizontally split window, set status line for last one */
6124 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6125 ;
6126 last_status_rec(fp, statusline);
6127 }
6128}
6129
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006130/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006131 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006132 */
6133 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006134tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006135{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006136#ifdef FEAT_GUI_TABLINE
6137 /* When the GUI has the tabline then this always returns zero. */
6138 if (gui_use_tabline())
6139 return 0;
6140#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006141 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006142 {
6143 case 0: return 0;
6144 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6145 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006146 return 1;
6147}
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149/*
6150 * Return the minimal number of rows that is needed on the screen to display
6151 * the current number of windows.
6152 */
6153 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006154min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006157 tabpage_T *tp;
6158 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 if (firstwin == NULL) /* not initialized yet */
6161 return MIN_LINES;
6162
Bram Moolenaarf740b292006-02-16 22:11:02 +00006163 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006164 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006165 {
6166 n = frame_minheight(tp->tp_topframe, NULL);
6167 if (total < n)
6168 total = n;
6169 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006170 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006171 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 return total;
6173}
6174
6175/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006176 * Return TRUE if there is only one window (in the current tab page), not
6177 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006178 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 */
6180 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006181only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 int count = 0;
6184 win_T *wp;
6185
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186 /* If there is another tab page there always is another window. */
6187 if (first_tabpage->tp_next != NULL)
6188 return FALSE;
6189
Bram Moolenaar29323592016-07-24 22:04:11 +02006190 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006191 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006192 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193# ifdef FEAT_QUICKFIX
6194 || wp->w_p_pvw
6195# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006196 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 ++count;
6198 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199}
6200
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201/*
6202 * Correct the cursor line number in other windows. Used after changing the
6203 * current buffer, and before applying autocommands.
6204 * When "do_curwin" is TRUE, also check current window.
6205 */
6206 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006207check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006210 tabpage_T *tp;
6211
6212 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 {
Bram Moolenaara68e5952019-04-25 22:22:01 +02006215 // save the original cursor position and topline
6216 wp->w_save_cursor.w_cursor_save = wp->w_cursor;
6217 wp->w_save_cursor.w_topline_save = wp->w_topline;
6218
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6220 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6221 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6222 wp->w_topline = curbuf->b_ml.ml_line_count;
Bram Moolenaara68e5952019-04-25 22:22:01 +02006223
6224 // save the corrected cursor position and topline
6225 wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
6226 wp->w_save_cursor.w_topline_corr = wp->w_topline;
6227 }
6228}
6229
6230/*
6231 * Reset cursor and topline to its stored values from check_lnums().
6232 * check_lnums() must have been called first!
6233 */
6234 void
6235reset_lnums()
6236{
6237 win_T *wp;
6238 tabpage_T *tp;
6239
6240 FOR_ALL_TAB_WINDOWS(tp, wp)
6241 if (wp->w_buffer == curbuf)
6242 {
6243 // Restore the value if the autocommand didn't change it.
6244 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
6245 wp->w_cursor = wp->w_save_cursor.w_cursor_save;
6246 if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
6247 wp->w_topline = wp->w_save_cursor.w_topline_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 }
6249}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
6251/*
6252 * A snapshot of the window sizes, to restore them after closing the help
6253 * window.
6254 * Only these fields are used:
6255 * fr_layout
6256 * fr_width
6257 * fr_height
6258 * fr_next
6259 * fr_child
6260 * fr_win (only valid for the old curwin, NULL otherwise)
6261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
6263/*
6264 * Create a snapshot of the current frame sizes.
6265 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006266 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006267make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006269 clear_snapshot(curtab, idx);
6270 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271}
6272
6273 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006274make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
6276 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6277 if (*frp == NULL)
6278 return;
6279 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 (*frp)->fr_height = fr->fr_height;
6282 if (fr->fr_next != NULL)
6283 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6284 if (fr->fr_child != NULL)
6285 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6286 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6287 (*frp)->fr_win = curwin;
6288}
6289
6290/*
6291 * Remove any existing snapshot.
6292 */
6293 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006294clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006296 clear_snapshot_rec(tp->tp_snapshot[idx]);
6297 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298}
6299
6300 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006301clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 if (fr != NULL)
6304 {
6305 clear_snapshot_rec(fr->fr_next);
6306 clear_snapshot_rec(fr->fr_child);
6307 vim_free(fr);
6308 }
6309}
6310
6311/*
6312 * Restore a previously created snapshot, if there is any.
6313 * This is only done if the screen size didn't change and the window layout is
6314 * still the same.
6315 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006316 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006317restore_snapshot(
6318 int idx,
6319 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 win_T *wp;
6322
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006323 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006324 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006325 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6326 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006328 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 win_comp_pos();
6330 if (wp != NULL && close_curwin)
6331 win_goto(wp);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01006332 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006334 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335}
6336
6337/*
6338 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006339 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 */
6341 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006342check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 if (sn->fr_layout != fr->fr_layout
6345 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6346 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6347 || (sn->fr_next != NULL
6348 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6349 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006350 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006351 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 return FAIL;
6353 return OK;
6354}
6355
6356/*
6357 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6358 * following frames and children.
6359 * Returns a pointer to the old current window, or NULL.
6360 */
6361 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006362restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
6364 win_T *wp = NULL;
6365 win_T *wp2;
6366
6367 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 if (fr->fr_layout == FR_LEAF)
6370 {
6371 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006372 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 wp = sn->fr_win;
6374 }
6375 if (sn->fr_next != NULL)
6376 {
6377 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6378 if (wp2 != NULL)
6379 wp = wp2;
6380 }
6381 if (sn->fr_child != NULL)
6382 {
6383 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6384 if (wp2 != NULL)
6385 wp = wp2;
6386 }
6387 return wp;
6388}
6389
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006390#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6391 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006392/*
6393 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006394 * restore_win() MUST be called to undo, also when FAIL is returned.
6395 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006396 * When "no_display" is TRUE the display won't be affected, no redraw is
6397 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006398 * Returns FAIL if switching to "win" failed.
6399 */
6400 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006401switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006402 win_T **save_curwin,
6403 tabpage_T **save_curtab,
6404 win_T *win,
6405 tabpage_T *tp,
6406 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006407{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006408 block_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006409 *save_curwin = curwin;
6410 if (tp != NULL)
6411 {
6412 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006413 if (no_display)
6414 {
6415 curtab->tp_firstwin = firstwin;
6416 curtab->tp_lastwin = lastwin;
6417 curtab = tp;
6418 firstwin = curtab->tp_firstwin;
6419 lastwin = curtab->tp_lastwin;
6420 }
6421 else
6422 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006423 }
6424 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006425 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006426 curwin = win;
6427 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006428 return OK;
6429}
6430
6431/*
6432 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006433 * When "no_display" is TRUE the display won't be affected, no redraw is
6434 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006435 */
6436 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006437restore_win(
6438 win_T *save_curwin UNUSED,
6439 tabpage_T *save_curtab UNUSED,
6440 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006441{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006442 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006443 {
6444 if (no_display)
6445 {
6446 curtab->tp_firstwin = firstwin;
6447 curtab->tp_lastwin = lastwin;
6448 curtab = save_curtab;
6449 firstwin = curtab->tp_firstwin;
6450 lastwin = curtab->tp_lastwin;
6451 }
6452 else
6453 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6454 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006455 if (win_valid(save_curwin))
6456 {
6457 curwin = save_curwin;
6458 curbuf = curwin->w_buffer;
6459 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006460 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006461}
6462
6463/*
6464 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6465 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6466 */
6467 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006468switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006469{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006470 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006471 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006472 --curbuf->b_nwindows;
6473 curbuf = buf;
6474 curwin->w_buffer = buf;
6475 ++curbuf->b_nwindows;
6476}
6477
6478/*
6479 * Restore the current buffer after using switch_buffer().
6480 */
6481 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006482restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006483{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006484 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006485 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006486 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006487 {
6488 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006489 curwin->w_buffer = save_curbuf->br_buf;
6490 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006491 ++curbuf->b_nwindows;
6492 }
6493}
6494#endif
6495
Bram Moolenaar4033c552017-09-16 20:54:51 +02006496#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497/*
6498 * Return TRUE if there is any vertically split window.
6499 */
6500 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006501win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 frame_T *fr;
6504
6505 if (topframe->fr_layout == FR_ROW)
6506 return TRUE;
6507
6508 if (topframe->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006509 FOR_ALL_FRAMES(fr, topframe->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 if (fr->fr_layout == FR_ROW)
6511 return TRUE;
6512
6513 return FALSE;
6514}
6515#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006516
6517#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6518/*
6519 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006520 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006521 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6522 * If no particular ID is desired, -1 must be specified for 'id'.
6523 * Return ID of added match, -1 on failure.
6524 */
6525 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006526match_add(
6527 win_T *wp,
6528 char_u *grp,
6529 char_u *pat,
6530 int prio,
6531 int id,
6532 list_T *pos_list,
6533 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006534{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006535 matchitem_T *cur;
6536 matchitem_T *prev;
6537 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006538 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006539 regprog_T *regprog = NULL;
6540 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006541
Bram Moolenaarb3414592014-06-17 17:48:32 +02006542 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006543 return -1;
6544 if (id < -1 || id == 0)
6545 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006546 semsg(_("E799: Invalid ID: %d (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006547 return -1;
6548 }
6549 if (id != -1)
6550 {
6551 cur = wp->w_match_head;
6552 while (cur != NULL)
6553 {
6554 if (cur->id == id)
6555 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006556 semsg(_("E801: ID already taken: %d"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006557 return -1;
6558 }
6559 cur = cur->next;
6560 }
6561 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006562 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006564 semsg(_(e_nogroup), grp);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006565 return -1;
6566 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006567 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006569 semsg(_(e_invarg2), pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006570 return -1;
6571 }
6572
6573 /* Find available match ID. */
6574 while (id == -1)
6575 {
6576 cur = wp->w_match_head;
6577 while (cur != NULL && cur->id != wp->w_next_match_id)
6578 cur = cur->next;
6579 if (cur == NULL)
6580 id = wp->w_next_match_id;
6581 wp->w_next_match_id++;
6582 }
6583
6584 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006585 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006586 m->id = id;
6587 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006588 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006589 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006590 m->match.regprog = regprog;
6591 m->match.rmm_ic = FALSE;
6592 m->match.rmm_maxcol = 0;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006593# if defined(FEAT_CONCEAL)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006594 m->conceal_char = 0;
6595 if (conceal_char != NULL)
6596 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006597# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006598
Bram Moolenaarb3414592014-06-17 17:48:32 +02006599 /* Set up position matches */
6600 if (pos_list != NULL)
6601 {
6602 linenr_T toplnum = 0;
6603 linenr_T botlnum = 0;
6604 listitem_T *li;
6605 int i;
6606
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006607 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006608 i++, li = li->li_next)
6609 {
6610 linenr_T lnum = 0;
6611 colnr_T col = 0;
6612 int len = 1;
6613 list_T *subl;
6614 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006615 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006616
Bram Moolenaarb3414592014-06-17 17:48:32 +02006617 if (li->li_tv.v_type == VAR_LIST)
6618 {
6619 subl = li->li_tv.vval.v_list;
6620 if (subl == NULL)
6621 goto fail;
6622 subli = subl->lv_first;
6623 if (subli == NULL)
6624 goto fail;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006625 lnum = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006626 if (error == TRUE)
6627 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006628 if (lnum == 0)
6629 {
6630 --i;
6631 continue;
6632 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006633 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006634 subli = subli->li_next;
6635 if (subli != NULL)
6636 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006637 col = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006638 if (error == TRUE)
6639 goto fail;
6640 subli = subli->li_next;
6641 if (subli != NULL)
6642 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006643 len = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006644 if (error == TRUE)
6645 goto fail;
6646 }
6647 }
6648 m->pos.pos[i].col = col;
6649 m->pos.pos[i].len = len;
6650 }
6651 else if (li->li_tv.v_type == VAR_NUMBER)
6652 {
6653 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006654 {
6655 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006656 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006657 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006658 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6659 m->pos.pos[i].col = 0;
6660 m->pos.pos[i].len = 0;
6661 }
6662 else
6663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006664 emsg(_("List or number required"));
Bram Moolenaarb3414592014-06-17 17:48:32 +02006665 goto fail;
6666 }
6667 if (toplnum == 0 || lnum < toplnum)
6668 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006669 if (botlnum == 0 || lnum >= botlnum)
6670 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006671 }
6672
6673 /* Calculate top and bottom lines for redrawing area */
6674 if (toplnum != 0)
6675 {
6676 if (wp->w_buffer->b_mod_set)
6677 {
6678 if (wp->w_buffer->b_mod_top > toplnum)
6679 wp->w_buffer->b_mod_top = toplnum;
6680 if (wp->w_buffer->b_mod_bot < botlnum)
6681 wp->w_buffer->b_mod_bot = botlnum;
6682 }
6683 else
6684 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006685 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006686 wp->w_buffer->b_mod_top = toplnum;
6687 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006688 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006689 }
6690 m->pos.toplnum = toplnum;
6691 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006692 rtype = VALID;
6693 }
6694 }
6695
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006696 /* Insert new match. The match list is in ascending order with regard to
6697 * the match priorities. */
6698 cur = wp->w_match_head;
6699 prev = cur;
6700 while (cur != NULL && prio >= cur->priority)
6701 {
6702 prev = cur;
6703 cur = cur->next;
6704 }
6705 if (cur == prev)
6706 wp->w_match_head = m;
6707 else
6708 prev->next = m;
6709 m->next = cur;
6710
Bram Moolenaarb3414592014-06-17 17:48:32 +02006711 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006712 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006713
6714fail:
6715 vim_free(m);
6716 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006717}
6718
6719/*
6720 * Delete match with ID 'id' in the match list of window 'wp'.
6721 * Print error messages if 'perr' is TRUE.
6722 */
6723 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006724match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006725{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006726 matchitem_T *cur = wp->w_match_head;
6727 matchitem_T *prev = cur;
6728 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006729
6730 if (id < 1)
6731 {
6732 if (perr == TRUE)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006733 semsg(_("E802: Invalid ID: %d (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006734 id);
6735 return -1;
6736 }
6737 while (cur != NULL && cur->id != id)
6738 {
6739 prev = cur;
6740 cur = cur->next;
6741 }
6742 if (cur == NULL)
6743 {
6744 if (perr == TRUE)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006745 semsg(_("E803: ID not found: %d"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006746 return -1;
6747 }
6748 if (cur == prev)
6749 wp->w_match_head = cur->next;
6750 else
6751 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006752 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006753 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006754 if (cur->pos.toplnum != 0)
6755 {
6756 if (wp->w_buffer->b_mod_set)
6757 {
6758 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6759 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6760 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6761 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6762 }
6763 else
6764 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006765 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006766 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6767 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006768 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006769 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006770 rtype = VALID;
6771 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006772 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006773 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006774 return 0;
6775}
6776
6777/*
6778 * Delete all matches in the match list of window 'wp'.
6779 */
6780 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006781clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006782{
6783 matchitem_T *m;
6784
6785 while (wp->w_match_head != NULL)
6786 {
6787 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006788 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006789 vim_free(wp->w_match_head->pattern);
6790 vim_free(wp->w_match_head);
6791 wp->w_match_head = m;
6792 }
6793 redraw_later(SOME_VALID);
6794}
6795
6796/*
6797 * Get match from ID 'id' in window 'wp'.
6798 * Return NULL if match not found.
6799 */
6800 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006801get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006802{
6803 matchitem_T *cur = wp->w_match_head;
6804
6805 while (cur != NULL && cur->id != id)
6806 cur = cur->next;
6807 return cur;
6808}
6809#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006810
6811#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6812 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006813get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006814{
6815 int i = 1;
6816 win_T *w;
6817
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006818 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006819 ++i;
6820
6821 if (w == NULL)
6822 return 0;
6823 else
6824 return i;
6825}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006826
6827 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006828get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006829{
6830 int i = 1;
6831 tabpage_T *t;
6832
6833 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6834 ++i;
6835
6836 if (t == NULL)
6837 return 0;
6838 else
6839 return i;
6840}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006841#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006842
6843/*
6844 * Return TRUE if "topfrp" and its children are at the right height.
6845 */
6846 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006847frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006848{
6849 frame_T *frp;
6850
6851 if (topfrp->fr_height != height)
6852 return FALSE;
6853
6854 if (topfrp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006855 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006856 if (frp->fr_height != height)
6857 return FALSE;
6858
6859 return TRUE;
6860}
6861
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006862/*
6863 * Return TRUE if "topfrp" and its children are at the right width.
6864 */
6865 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006866frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006867{
6868 frame_T *frp;
6869
6870 if (topfrp->fr_width != width)
6871 return FALSE;
6872
6873 if (topfrp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006874 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006875 if (frp->fr_width != width)
6876 return FALSE;
6877
6878 return TRUE;
6879}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006880
Bram Moolenaar86edef62016-03-13 18:07:30 +01006881#if defined(FEAT_EVAL) || defined(PROTO)
6882 int
6883win_getid(typval_T *argvars)
6884{
6885 int winnr;
6886 win_T *wp;
6887
6888 if (argvars[0].v_type == VAR_UNKNOWN)
6889 return curwin->w_id;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006890 winnr = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006891 if (winnr > 0)
6892 {
6893 if (argvars[1].v_type == VAR_UNKNOWN)
6894 wp = firstwin;
6895 else
6896 {
6897 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006898 int tabnr = tv_get_number(&argvars[1]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006899
Bram Moolenaar29323592016-07-24 22:04:11 +02006900 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006901 if (--tabnr == 0)
6902 break;
6903 if (tp == NULL)
6904 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01006905 if (tp == curtab)
6906 wp = firstwin;
6907 else
6908 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01006909 }
6910 for ( ; wp != NULL; wp = wp->w_next)
6911 if (--winnr == 0)
6912 return wp->w_id;
6913 }
6914 return 0;
6915}
6916
6917 int
6918win_gotoid(typval_T *argvars)
6919{
6920 win_T *wp;
6921 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006922 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006923
Bram Moolenaar29323592016-07-24 22:04:11 +02006924 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006925 if (wp->w_id == id)
6926 {
6927 goto_tabpage_win(tp, wp);
6928 return 1;
6929 }
6930 return 0;
6931}
6932
6933 void
6934win_id2tabwin(typval_T *argvars, list_T *list)
6935{
6936 win_T *wp;
6937 tabpage_T *tp;
6938 int winnr = 1;
6939 int tabnr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006940 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006941
Bram Moolenaar29323592016-07-24 22:04:11 +02006942 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006943 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006944 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006945 {
6946 if (wp->w_id == id)
6947 {
6948 list_append_number(list, tabnr);
6949 list_append_number(list, winnr);
6950 return;
6951 }
6952 ++winnr;
6953 }
6954 ++tabnr;
6955 winnr = 1;
6956 }
6957 list_append_number(list, 0);
6958 list_append_number(list, 0);
6959}
6960
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006961 win_T *
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006962win_id2wp(int id)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006963{
6964 win_T *wp;
6965 tabpage_T *tp;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006966
6967 FOR_ALL_TAB_WINDOWS(tp, wp)
6968 if (wp->w_id == id)
6969 return wp;
6970
6971 return NULL;
6972}
6973
Bram Moolenaar86edef62016-03-13 18:07:30 +01006974 int
6975win_id2win(typval_T *argvars)
6976{
6977 win_T *wp;
6978 int nr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006979 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006980
Bram Moolenaar29323592016-07-24 22:04:11 +02006981 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006982 {
6983 if (wp->w_id == id)
6984 return nr;
6985 ++nr;
6986 }
6987 return 0;
6988}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006989
6990 void
6991win_findbuf(typval_T *argvars, list_T *list)
6992{
6993 win_T *wp;
6994 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006995 int bufnr = tv_get_number(&argvars[0]);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006996
Bram Moolenaar29323592016-07-24 22:04:11 +02006997 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006998 if (wp->w_buffer->b_fnum == bufnr)
6999 list_append_number(list, wp->w_id);
7000}
7001
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02007002/*
7003 * Get the layout of the given tab page for winlayout().
7004 */
7005 void
7006get_framelayout(frame_T *fr, list_T *l, int outer)
7007{
7008 frame_T *child;
7009 list_T *fr_list;
7010 list_T *win_list;
7011
7012 if (fr == NULL)
7013 return;
7014
7015 if (outer)
7016 // outermost call from f_winlayout()
7017 fr_list = l;
7018 else
7019 {
7020 fr_list = list_alloc();
7021 if (fr_list == NULL)
7022 return;
7023 list_append_list(l, fr_list);
7024 }
7025
7026 if (fr->fr_layout == FR_LEAF)
7027 {
7028 if (fr->fr_win != NULL)
7029 {
7030 list_append_string(fr_list, (char_u *)"leaf", -1);
7031 list_append_number(fr_list, fr->fr_win->w_id);
7032 }
7033 }
7034 else
7035 {
7036 list_append_string(fr_list,
7037 fr->fr_layout == FR_ROW ? (char_u *)"row" : (char_u *)"col", -1);
7038
7039 win_list = list_alloc();
7040 if (win_list == NULL)
7041 return;
7042 list_append_list(fr_list, win_list);
7043 child = fr->fr_child;
7044 while (child != NULL)
7045 {
7046 get_framelayout(child, win_list, FALSE);
7047 child = child->fr_next;
7048 }
7049 }
7050}
Bram Moolenaar86edef62016-03-13 18:07:30 +01007051#endif