blob: f8df1cc315c60d1052ed74c52effa54cbe432824 [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. */
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +02003958 if (p_ch != curtab->tp_ch_used)
3959 clear_cmdline = TRUE;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003960 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003961 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
3962#ifdef FEAT_GUI_TABLINE
3963 && !gui_use_tabline()
3964#endif
3965 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003966 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003967 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003968 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00003969
3970#if defined(FEAT_GUI)
3971 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3972 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003973 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003974#endif
3975
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003976 /* Apply autocommands after updating the display, when 'rows' and
3977 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003978 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02003979 {
3980 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
3981 if (old_curbuf != curbuf)
3982 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
3983 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01003984
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01003985 redraw_all_later(NOT_VALID);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003986}
3987
3988/*
3989 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003990 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003991 */
3992 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003993goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003994{
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02003995 tabpage_T *tp = NULL; // shut up compiler
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003996 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003997 int i;
3998
Bram Moolenaard68071d2006-05-02 22:08:30 +00003999 if (text_locked())
4000 {
4001 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004002 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00004003 return;
4004 }
4005
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004006 /* If there is only one it can't work. */
4007 if (first_tabpage->tp_next == NULL)
4008 {
4009 if (n > 1)
4010 beep_flush();
4011 return;
4012 }
4013
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004014 if (n == 0)
4015 {
4016 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004017 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004018 tp = first_tabpage;
4019 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004020 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004021 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004022 else if (n < 0)
4023 {
4024 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4025 * this N times. */
4026 ttp = curtab;
4027 for (i = n; i < 0; ++i)
4028 {
4029 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4030 tp = tp->tp_next)
4031 ;
4032 ttp = tp;
4033 }
4034 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004035 else if (n == 9999)
4036 {
4037 /* Go to last tab page. */
4038 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4039 ;
4040 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004041 else
4042 {
4043 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004044 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004045 if (tp == NULL)
4046 {
4047 beep_flush();
4048 return;
4049 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004050 }
4051
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004052 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004053
4054#ifdef FEAT_GUI_TABLINE
4055 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004056 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004057#endif
4058}
4059
4060/*
4061 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004062 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4063 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004064 * Note: doesn't update the GUI tab.
4065 */
4066 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004067goto_tabpage_tp(
4068 tabpage_T *tp,
4069 int trigger_enter_autocmds,
4070 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004071{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004072 /* Don't repeat a message in another tab page. */
4073 set_keep_msg(NULL, 0);
4074
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004075 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4076 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004077 {
4078 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004079 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4080 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004081 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004082 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4083 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004084 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004085}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086
4087/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004088 * Enter window "wp" in tab page "tp".
4089 * Also updates the GUI tab.
4090 */
4091 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004092goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004093{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004094 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004095 if (curtab == tp && win_valid(wp))
4096 {
4097 win_enter(wp, TRUE);
4098# ifdef FEAT_GUI_TABLINE
4099 if (gui_use_tabline())
4100 gui_mch_set_curtab(tabpage_index(curtab));
4101# endif
4102 }
4103}
4104
4105/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004106 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004107 */
4108 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004109tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004110{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004111 int n = 1;
4112 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004113
4114 if (first_tabpage->tp_next == NULL)
4115 return;
4116
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004117 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4118 ++n;
4119
4120 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4121 && tp->tp_next == curtab))
4122 return;
4123
4124 tp_dst = tp;
4125
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004126 /* Remove the current tab page from the list of tab pages. */
4127 if (curtab == first_tabpage)
4128 first_tabpage = curtab->tp_next;
4129 else
4130 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004131 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004132 if (tp->tp_next == curtab)
4133 break;
4134 if (tp == NULL) /* "cannot happen" */
4135 return;
4136 tp->tp_next = curtab->tp_next;
4137 }
4138
4139 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004140 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004141 {
4142 curtab->tp_next = first_tabpage;
4143 first_tabpage = curtab;
4144 }
4145 else
4146 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004147 curtab->tp_next = tp_dst->tp_next;
4148 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004149 }
4150
4151 /* Need to redraw the tabline. Tab page contents doesn't change. */
4152 redraw_tabline = TRUE;
4153}
4154
4155
4156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 * Go to another window.
4158 * When jumping to another buffer, stop Visual mode. Do this before
4159 * changing windows so we can yank the selection into the '*' register.
4160 * When jumping to another window on the same buffer, adjust its cursor
4161 * position to keep the same Visual area.
4162 */
4163 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004164win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004166#ifdef FEAT_CONCEAL
4167 win_T *owp = curwin;
4168#endif
4169
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004170 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
4172 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004173 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 return;
4175 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004176 if (curbuf_locked())
4177 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004178
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 if (wp->w_buffer != curbuf)
4180 reset_VIsual_and_resel();
4181 else if (VIsual_active)
4182 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184#ifdef FEAT_GUI
4185 need_mouse_correct = TRUE;
4186#endif
4187 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004188
4189#ifdef FEAT_CONCEAL
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004190 // Conceal cursor line in previous window, unconceal in current window.
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004191 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004192 redrawWinline(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004193 if (curwin->w_p_cole > 0 && !msg_scrolled)
4194 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196}
4197
4198#if defined(FEAT_PERL) || defined(PROTO)
4199/*
4200 * Find window number "winnr" (counting top to bottom).
4201 */
4202 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004203win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
4205 win_T *wp;
4206
Bram Moolenaar29323592016-07-24 22:04:11 +02004207 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (--winnr == 0)
4209 break;
4210 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211}
4212#endif
4213
Bram Moolenaar4033c552017-09-16 20:54:51 +02004214#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004215/*
4216 * Find the tabpage for window "win".
4217 */
4218 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004219win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004220{
4221 win_T *wp;
4222 tabpage_T *tp;
4223
Bram Moolenaar29323592016-07-24 22:04:11 +02004224 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004225 if (wp == win)
4226 return tp;
4227 return NULL;
4228}
4229#endif
4230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004232 * Get the above or below neighbor window of the specified window.
4233 * up - TRUE for the above neighbor
4234 * count - nth neighbor window
4235 * Returns the specified window if the neighbor is not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 */
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004237 win_T *
4238win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
4240 frame_T *fr;
4241 frame_T *nfr;
4242 frame_T *foundfr;
4243
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004244 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 while (count--)
4246 {
4247 /*
4248 * First go upwards in the tree of frames until we find a upwards or
4249 * downwards neighbor.
4250 */
4251 fr = foundfr;
4252 for (;;)
4253 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004254 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 goto end;
4256 if (up)
4257 nfr = fr->fr_prev;
4258 else
4259 nfr = fr->fr_next;
4260 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4261 break;
4262 fr = fr->fr_parent;
4263 }
4264
4265 /*
4266 * Now go downwards to find the bottom or top frame in it.
4267 */
4268 for (;;)
4269 {
4270 if (nfr->fr_layout == FR_LEAF)
4271 {
4272 foundfr = nfr;
4273 break;
4274 }
4275 fr = nfr->fr_child;
4276 if (nfr->fr_layout == FR_ROW)
4277 {
4278 /* Find the frame at the cursor row. */
4279 while (fr->fr_next != NULL
4280 && frame2win(fr)->w_wincol + fr->fr_width
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004281 <= wp->w_wincol + wp->w_wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 fr = fr->fr_next;
4283 }
4284 if (nfr->fr_layout == FR_COL && up)
4285 while (fr->fr_next != NULL)
4286 fr = fr->fr_next;
4287 nfr = fr;
4288 }
4289 }
4290end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004291 return foundfr != NULL ? foundfr->fr_win : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292}
4293
4294/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004295 * Move to window above or below "count" times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 */
4297 static void
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004298win_goto_ver(
4299 int up, // TRUE to go to win above
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004300 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004302 win_T *win;
4303
4304 win = win_vert_neighbor(curtab, curwin, up, count);
4305 if (win != NULL)
4306 win_goto(win);
4307}
4308
4309/*
4310 * Get the left or right neighbor window of the specified window.
4311 * left - TRUE for the left neighbor
4312 * count - nth neighbor window
4313 * Returns the specified window if the neighbor is not found.
4314 */
4315 win_T *
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02004316win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count)
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004317{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 frame_T *fr;
4319 frame_T *nfr;
4320 frame_T *foundfr;
4321
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004322 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 while (count--)
4324 {
4325 /*
4326 * First go upwards in the tree of frames until we find a left or
4327 * right neighbor.
4328 */
4329 fr = foundfr;
4330 for (;;)
4331 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004332 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 goto end;
4334 if (left)
4335 nfr = fr->fr_prev;
4336 else
4337 nfr = fr->fr_next;
4338 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4339 break;
4340 fr = fr->fr_parent;
4341 }
4342
4343 /*
4344 * Now go downwards to find the leftmost or rightmost frame in it.
4345 */
4346 for (;;)
4347 {
4348 if (nfr->fr_layout == FR_LEAF)
4349 {
4350 foundfr = nfr;
4351 break;
4352 }
4353 fr = nfr->fr_child;
4354 if (nfr->fr_layout == FR_COL)
4355 {
4356 /* Find the frame at the cursor row. */
4357 while (fr->fr_next != NULL
4358 && frame2win(fr)->w_winrow + fr->fr_height
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004359 <= wp->w_winrow + wp->w_wrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 fr = fr->fr_next;
4361 }
4362 if (nfr->fr_layout == FR_ROW && left)
4363 while (fr->fr_next != NULL)
4364 fr = fr->fr_next;
4365 nfr = fr;
4366 }
4367 }
4368end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004369 return foundfr != NULL ? foundfr->fr_win : NULL;
4370}
4371
4372/*
4373 * Move to left or right window.
4374 */
4375 static void
4376win_goto_hor(
4377 int left, // TRUE to go to left win
4378 long count)
4379{
4380 win_T *win;
4381
4382 win = win_horz_neighbor(curtab, curwin, left, count);
4383 if (win != NULL)
4384 win_goto(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
4387/*
4388 * Make window "wp" the current window.
4389 */
4390 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004391win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004393 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394}
4395
4396/*
4397 * Make window wp the current window.
4398 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4399 * been closed and isn't valid.
4400 */
4401 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004402win_enter_ext(
4403 win_T *wp,
4404 int undo_sync,
4405 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004406 int trigger_new_autocmds,
4407 int trigger_enter_autocmds,
4408 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411
4412 if (wp == curwin && !curwin_invalid) /* nothing to do */
4413 return;
4414
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004415#ifdef FEAT_JOB_CHANNEL
4416 if (!curwin_invalid)
4417 leaving_window(curwin);
4418#endif
4419
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004420 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 {
4422 /*
4423 * Be careful: If autocommands delete the window, return now.
4424 */
4425 if (wp->w_buffer != curbuf)
4426 {
4427 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4428 other_buffer = TRUE;
4429 if (!win_valid(wp))
4430 return;
4431 }
4432 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4433 if (!win_valid(wp))
4434 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004435#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 /* autocmds may abort script processing */
4437 if (aborting())
4438 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 /* sync undo before leaving the current buffer */
4443 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004444 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004445
4446 /* Might need to scroll the old window before switching, e.g., when the
4447 * cursor was moved. */
4448 update_topline();
4449
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 /* may have to copy the buffer options when 'cpo' contains 'S' */
4451 if (wp->w_buffer != curbuf)
4452 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4453 if (!curwin_invalid)
4454 {
4455 prevwin = curwin; /* remember for CTRL-W p */
4456 curwin->w_redr_status = TRUE;
4457 }
4458 curwin = wp;
4459 curbuf = wp->w_buffer;
4460 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (!virtual_active())
4462 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 changed_line_abv_curs(); /* assume cursor position needs updating */
4464
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004465 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004467 char_u *dirname;
4468
4469 // Window or tab has a local directory: Save current directory as
4470 // global directory (unless that was done already) and change to the
4471 // local directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (globaldir == NULL)
4473 {
4474 char_u cwd[MAXPATHL];
4475
4476 if (mch_dirname(cwd, MAXPATHL) == OK)
4477 globaldir = vim_strsave(cwd);
4478 }
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004479 if (curwin->w_localdir != NULL)
4480 dirname = curwin->w_localdir;
4481 else
4482 dirname = curtab->tp_localdir;
4483
4484 if (mch_chdir((char *)dirname) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004485 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 else if (globaldir != NULL)
4488 {
4489 /* Window doesn't have a local directory and we are not in the global
4490 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004491 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004492 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 shorten_fnames(TRUE);
4494 }
4495
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004496#ifdef FEAT_JOB_CHANNEL
4497 entering_window(curwin);
4498#endif
Bram Moolenaarc917da42016-07-19 22:31:36 +02004499 if (trigger_new_autocmds)
4500 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004501 if (trigger_enter_autocmds)
4502 {
4503 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4504 if (other_buffer)
4505 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
4508#ifdef FEAT_TITLE
4509 maketitle();
4510#endif
4511 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004512 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (restart_edit)
4514 redraw_later(VALID); /* causes status line redraw */
4515
4516 /* set window height to desired minimal value */
4517 if (curwin->w_height < p_wh && !curwin->w_p_wfh)
4518 win_setheight((int)p_wh);
4519 else if (curwin->w_height == 0)
4520 win_setheight(1);
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004523 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525
4526#ifdef FEAT_MOUSE
4527 setmouse(); /* in case jumped to/from help buffer */
4528#endif
4529
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004530 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004531 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532}
4533
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004536 * Jump to the first open window that contains buffer "buf", if one exists.
4537 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 */
4539 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004540buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004542 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004544 if (curwin->w_buffer == buf)
4545 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004546 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004547 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004548 if (wp->w_buffer == buf)
4549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (wp != NULL)
4551 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004552 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004554
4555/*
4556 * Jump to the first open window in any tab page that contains buffer "buf",
4557 * if one exists.
4558 * Returns a pointer to the window found, otherwise NULL.
4559 */
4560 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004561buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004562{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004563 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004564 tabpage_T *tp;
4565
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004566 if (wp != NULL)
4567 return wp;
4568
Bram Moolenaar29323592016-07-24 22:04:11 +02004569 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004570 if (tp != curtab)
4571 {
4572 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4573 if (wp->w_buffer == buf)
4574 break;
4575 if (wp != NULL)
4576 {
4577 goto_tabpage_win(tp, wp);
4578 if (curwin != wp)
4579 wp = NULL; /* something went wrong */
4580 break;
4581 }
4582 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004583 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004586static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004587
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004589 * Allocate a window structure and link it in the window list when "hidden" is
4590 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004593win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004595 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
4597 /*
4598 * allocate window structure and linesizes arrays
4599 */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004600 new_wp = (win_T *)alloc_clear((unsigned)sizeof(win_T));
Bram Moolenaar429fa852013-04-15 12:27:36 +02004601 if (new_wp == NULL)
4602 return NULL;
4603
4604 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004606 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004607 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
4609
Bram Moolenaar86edef62016-03-13 18:07:30 +01004610 new_wp->w_id = ++last_win_id;
4611
Bram Moolenaar429fa852013-04-15 12:27:36 +02004612#ifdef FEAT_EVAL
4613 /* init w: variables */
4614 new_wp->w_vars = dict_alloc();
4615 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004617 win_free_lsize(new_wp);
4618 vim_free(new_wp);
4619 return NULL;
4620 }
4621 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004622#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004623
Bram Moolenaar429fa852013-04-15 12:27:36 +02004624 /* Don't execute autocommands while the window is not properly
4625 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4626 * event. */
4627 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004628
Bram Moolenaar429fa852013-04-15 12:27:36 +02004629 /*
4630 * link the window in the window list
4631 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004632 if (!hidden)
4633 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004634 new_wp->w_wincol = 0;
4635 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
Bram Moolenaar429fa852013-04-15 12:27:36 +02004637 /* position the display and the cursor at the top of the file. */
4638 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004640 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004642 new_wp->w_botline = 2;
4643 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004644 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645
Bram Moolenaar375e3392019-01-31 18:26:10 +01004646 // use global option value for global-local options
4647 new_wp->w_p_so = -1;
4648 new_wp->w_p_siso = -1;
4649
Bram Moolenaar429fa852013-04-15 12:27:36 +02004650 /* We won't calculate w_fraction until resizing the window */
4651 new_wp->w_fraction = 0;
4652 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004655 if (gui.in_use)
4656 {
4657 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4658 SBAR_LEFT, new_wp);
4659 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4660 SBAR_RIGHT, new_wp);
4661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662#endif
4663#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004664 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004666 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004667#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004668 new_wp->w_match_head = NULL;
4669 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004670#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004671 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672}
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004675 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 */
4677 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004678win_free(
4679 win_T *wp,
4680 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681{
4682 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004683 buf_T *buf;
4684 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004686#ifdef FEAT_FOLDING
4687 clearFolding(wp);
4688#endif
4689
4690 /* reduce the reference count to the argument list. */
4691 alist_unlink(wp->w_alist);
4692
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004693 /* Don't execute autocommands while the window is halfway being deleted.
4694 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004695 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004696
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004697#ifdef FEAT_LUA
4698 lua_window_free(wp);
4699#endif
4700
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004701#ifdef FEAT_MZSCHEME
4702 mzscheme_window_free(wp);
4703#endif
4704
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#ifdef FEAT_PERL
4706 perl_win_free(wp);
4707#endif
4708
4709#ifdef FEAT_PYTHON
4710 python_window_free(wp);
4711#endif
4712
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004713#ifdef FEAT_PYTHON3
4714 python3_window_free(wp);
4715#endif
4716
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_TCL
4718 tcl_window_free(wp);
4719#endif
4720
4721#ifdef FEAT_RUBY
4722 ruby_window_free(wp);
4723#endif
4724
4725 clear_winopt(&wp->w_onebuf_opt);
4726 clear_winopt(&wp->w_allbuf_opt);
4727
4728#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004729 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4730 hash_init(&wp->w_vars->dv_hashtab);
4731 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732#endif
4733
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004734 {
4735 tabpage_T *ttp;
4736
4737 if (prevwin == wp)
4738 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004739 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004740 if (ttp->tp_prevwin == wp)
4741 ttp->tp_prevwin = NULL;
4742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 win_free_lsize(wp);
4744
4745 for (i = 0; i < wp->w_tagstacklen; ++i)
4746 vim_free(wp->w_tagstack[i].tagname);
4747
4748 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004749
Bram Moolenaarff18df02013-07-24 17:51:57 +02004750 /* Remove the window from the b_wininfo lists, it may happen that the
4751 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004752 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004753 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4754 if (wip->wi_win == wp)
4755 wip->wi_win = NULL;
4756
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004758 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#ifdef FEAT_JUMPLIST
4762 free_jumplist(wp);
4763#endif
4764
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004765#ifdef FEAT_QUICKFIX
4766 qf_free_all(wp);
4767#endif
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769#ifdef FEAT_GUI
4770 if (gui.in_use)
4771 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4773 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4774 }
4775#endif /* FEAT_GUI */
4776
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004777#ifdef FEAT_MENU
4778 remove_winbar(wp);
4779#endif
4780
Bram Moolenaar860cae12010-06-05 23:22:07 +02004781#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004782 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004783#endif
4784
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004785 if (wp != aucmd_win)
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004786 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004787 if (autocmd_busy)
4788 {
4789 wp->w_next = au_pending_free_win;
4790 au_pending_free_win = wp;
4791 }
4792 else
4793 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004794
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004795 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796}
4797
4798/*
4799 * Append window "wp" in the window list after window "after".
4800 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004801 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004802win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803{
4804 win_T *before;
4805
4806 if (after == NULL) /* after NULL is in front of the first */
4807 before = firstwin;
4808 else
4809 before = after->w_next;
4810
4811 wp->w_next = before;
4812 wp->w_prev = after;
4813 if (after == NULL)
4814 firstwin = wp;
4815 else
4816 after->w_next = wp;
4817 if (before == NULL)
4818 lastwin = wp;
4819 else
4820 before->w_prev = wp;
4821}
4822
4823/*
4824 * Remove a window from the window list.
4825 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004826 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004827win_remove(
4828 win_T *wp,
4829 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830{
4831 if (wp->w_prev != NULL)
4832 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004833 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004834 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004835 else
4836 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02004837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 if (wp->w_next != NULL)
4839 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004840 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02004841 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004842 else
4843 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844}
4845
4846/*
4847 * Append frame "frp" in a frame list after frame "after".
4848 */
4849 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004850frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851{
4852 frp->fr_next = after->fr_next;
4853 after->fr_next = frp;
4854 if (frp->fr_next != NULL)
4855 frp->fr_next->fr_prev = frp;
4856 frp->fr_prev = after;
4857}
4858
4859/*
4860 * Insert frame "frp" in a frame list before frame "before".
4861 */
4862 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004863frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 frp->fr_next = before;
4866 frp->fr_prev = before->fr_prev;
4867 before->fr_prev = frp;
4868 if (frp->fr_prev != NULL)
4869 frp->fr_prev->fr_next = frp;
4870 else
4871 frp->fr_parent->fr_child = frp;
4872}
4873
4874/*
4875 * Remove a frame from a frame list.
4876 */
4877 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004878frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
4880 if (frp->fr_prev != NULL)
4881 frp->fr_prev->fr_next = frp->fr_next;
4882 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01004885 /* special case: topframe->fr_child == frp */
4886 if (topframe->fr_child == frp)
4887 topframe->fr_child = frp->fr_next;
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 if (frp->fr_next != NULL)
4890 frp->fr_next->fr_prev = frp->fr_prev;
4891}
4892
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893/*
4894 * Allocate w_lines[] for window "wp".
4895 * Return FAIL for failure, OK for success.
4896 */
4897 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004898win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
4900 wp->w_lines_valid = 0;
Bram Moolenaar9334c342006-11-21 19:57:30 +00004901 wp->w_lines = (wline_T *)alloc_clear((unsigned)(Rows * sizeof(wline_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 if (wp->w_lines == NULL)
4903 return FAIL;
4904 return OK;
4905}
4906
4907/*
4908 * free lsize arrays for a window
4909 */
4910 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004911win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02004913 /* TODO: why would wp be NULL here? */
4914 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01004915 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916}
4917
4918/*
4919 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00004920 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 */
4922 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004923shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004925 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927 if (firstwin == NULL) /* not initialized yet */
4928 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if (h < frame_minheight(topframe, NULL))
4930 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004931
4932 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 * that doesn't result in the right height, forget about that option. */
4934 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004935 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 frame_new_height(topframe, h, FALSE, FALSE);
4937
4938 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004940 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004941
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#if 0
4943 /* Disabled: don't want making the screen smaller make a window larger. */
4944 if (p_ea)
4945 win_equal(curwin, FALSE, 'v');
4946#endif
4947}
4948
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949/*
4950 * Called from win_new_shellsize() after Columns changed.
4951 */
4952 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004953shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 if (firstwin == NULL) /* not initialized yet */
4956 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004957
4958 /* First try setting the widths of windows with 'winfixwidth'. If that
4959 * doesn't result in the right width, forget about that option. */
4960 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02004961 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004962 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
4963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
4965#if 0
4966 /* Disabled: don't want making the screen smaller make a window larger. */
4967 if (p_ea)
4968 win_equal(curwin, FALSE, 'h');
4969#endif
4970}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971
4972#if defined(FEAT_CMDWIN) || defined(PROTO)
4973/*
4974 * Save the size of all windows in "gap".
4975 */
4976 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004977win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
4979{
4980 win_T *wp;
4981
4982 ga_init2(gap, (int)sizeof(int), 1);
4983 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02004984 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 ((int *)gap->ga_data)[gap->ga_len++] =
4987 wp->w_width + wp->w_vsep_width;
4988 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
4989 }
4990}
4991
4992/*
4993 * Restore window sizes, but only if the number of windows is still the same.
4994 * Does not free the growarray.
4995 */
4996 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004997win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
4999 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005000 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001
5002 if (win_count() * 2 == gap->ga_len)
5003 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005004 /* The order matters, because frames contain other frames, but it's
5005 * difficult to get right. The easy way out is to do it twice. */
5006 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005008 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02005009 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02005010 {
5011 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5012 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 }
5015 /* recompute the window positions */
5016 (void)win_comp_pos();
5017 }
5018}
5019#endif /* FEAT_CMDWIN */
5020
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021/*
5022 * Update the position for all windows, using the width and height of the
5023 * frames.
5024 * Returns the row just after the last window.
5025 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005026 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005027win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005029 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 int col = 0;
5031
5032 frame_comp_pos(topframe, &row, &col);
5033 return row;
5034}
5035
5036/*
5037 * Update the position of the windows in frame "topfrp", using the width and
5038 * height of the frames.
5039 * "*row" and "*col" are the top-left position of the frame. They are updated
5040 * to the bottom-right position plus one.
5041 */
5042 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005043frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044{
5045 win_T *wp;
5046 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 int startcol;
5048 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005049 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050
5051 wp = topfrp->fr_win;
5052 if (wp != NULL)
5053 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005054 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 /* position changed, redraw */
5057 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 redraw_win_later(wp, NOT_VALID);
5060 wp->w_redr_status = TRUE;
5061 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005062 /* WinBar will not show if the window height is zero */
5063 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5064 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067 else
5068 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 startrow = *row;
5070 startcol = *col;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005071 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 if (topfrp->fr_layout == FR_ROW)
5074 *row = startrow; /* all frames are at the same row */
5075 else
5076 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 frame_comp_pos(frp, row, col);
5078 }
5079 }
5080}
5081
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082/*
5083 * Set current window height and take care of repositioning other windows to
5084 * fit around it.
5085 */
5086 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005087win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088{
5089 win_setheight_win(height, curwin);
5090}
5091
5092/*
5093 * Set the window height of window "win" and take care of repositioning other
5094 * windows to fit around it.
5095 */
5096 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005097win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
5099 int row;
5100
5101 if (win == curwin)
5102 {
5103 /* Always keep current window at least one line high, even when
5104 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (height < p_wmh)
5106 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 if (height == 0)
5108 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005109 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 frame_setheight(win->w_frame, height + win->w_status_height);
5113
5114 /* recompute the window positions */
5115 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116
5117 /*
5118 * If there is extra space created between the last window and the command
5119 * line, clear it.
5120 */
5121 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5122 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5123 cmdline_row = row;
5124 msg_row = row;
5125 msg_col = 0;
5126
5127 redraw_all_later(NOT_VALID);
5128}
5129
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130/*
5131 * Set the height of a frame to "height" and take care that all frames and
5132 * windows inside it are resized. Also resize frames on the left and right if
5133 * the are in the same FR_ROW frame.
5134 *
5135 * Strategy:
5136 * If the frame is part of a FR_COL frame, try fitting the frame in that
5137 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5138 * go to containing frames to resize them and make room.
5139 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5140 * Check for the minimal height of the FR_ROW frame.
5141 * At the top level we can also use change the command line height.
5142 */
5143 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005144frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145{
5146 int room; /* total number of lines available */
5147 int take; /* number of lines taken from other windows */
5148 int room_cmdline; /* lines available from cmdline */
5149 int run;
5150 frame_T *frp;
5151 int h;
5152 int room_reserved;
5153
5154 /* If the height already is the desired value, nothing to do. */
5155 if (curfrp->fr_height == height)
5156 return;
5157
5158 if (curfrp->fr_parent == NULL)
5159 {
5160 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005161 if (height > ROWS_AVAIL)
5162 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 if (height > 0)
5164 frame_new_height(curfrp, height, FALSE, FALSE);
5165 }
5166 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5167 {
5168 /* Row of frames: Also need to resize frames left and right of this
5169 * one. First check for the minimal height of these. */
5170 h = frame_minheight(curfrp->fr_parent, NULL);
5171 if (height < h)
5172 height = h;
5173 frame_setheight(curfrp->fr_parent, height);
5174 }
5175 else
5176 {
5177 /*
5178 * Column of frames: try to change only frames in this column.
5179 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 /*
5181 * Do this twice:
5182 * 1: compute room available, if it's not enough try resizing the
5183 * containing frame.
5184 * 2: compute the room available and adjust the height to it.
5185 * Try not to reduce the height of a window with 'winfixheight' set.
5186 */
5187 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189 room = 0;
5190 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005191 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
5193 if (frp != curfrp
5194 && frp->fr_win != NULL
5195 && frp->fr_win->w_p_wfh)
5196 room_reserved += frp->fr_height;
5197 room += frp->fr_height;
5198 if (frp != curfrp)
5199 room -= frame_minheight(frp, NULL);
5200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (curfrp->fr_width != Columns)
5202 room_cmdline = 0;
5203 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
5205 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005206 + VISIBLE_HEIGHT(lastwin)
5207 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (room_cmdline < 0)
5209 room_cmdline = 0;
5210 }
5211
5212 if (height <= room + room_cmdline)
5213 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
5216 if (height > room + room_cmdline)
5217 height = room + room_cmdline;
5218 break;
5219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 frame_setheight(curfrp->fr_parent, height
5221 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223
5224 /*
5225 * Compute the number of lines we will take from others frames (can be
5226 * negative!).
5227 */
5228 take = height - curfrp->fr_height;
5229
5230 /* If there is not enough room, also reduce the height of a window
5231 * with 'winfixheight' set. */
5232 if (height > room + room_cmdline - room_reserved)
5233 room_reserved = room + room_cmdline - height;
5234 /* If there is only a 'winfixheight' window and making the
5235 * window smaller, need to make the other window taller. */
5236 if (take < 0 && room - curfrp->fr_height < room_reserved)
5237 room_reserved = 0;
5238
5239 if (take > 0 && room_cmdline > 0)
5240 {
5241 /* use lines from cmdline first */
5242 if (take < room_cmdline)
5243 room_cmdline = take;
5244 take -= room_cmdline;
5245 topframe->fr_height += room_cmdline;
5246 }
5247
5248 /*
5249 * set the current frame to the new height
5250 */
5251 frame_new_height(curfrp, height, FALSE, FALSE);
5252
5253 /*
5254 * First take lines from the frames after the current frame. If
5255 * that is not enough, takes lines from frames above the current
5256 * frame.
5257 */
5258 for (run = 0; run < 2; ++run)
5259 {
5260 if (run == 0)
5261 frp = curfrp->fr_next; /* 1st run: start with next window */
5262 else
5263 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5264 while (frp != NULL && take != 0)
5265 {
5266 h = frame_minheight(frp, NULL);
5267 if (room_reserved > 0
5268 && frp->fr_win != NULL
5269 && frp->fr_win->w_p_wfh)
5270 {
5271 if (room_reserved >= frp->fr_height)
5272 room_reserved -= frp->fr_height;
5273 else
5274 {
5275 if (frp->fr_height - room_reserved > take)
5276 room_reserved = frp->fr_height - take;
5277 take -= frp->fr_height - room_reserved;
5278 frame_new_height(frp, room_reserved, FALSE, FALSE);
5279 room_reserved = 0;
5280 }
5281 }
5282 else
5283 {
5284 if (frp->fr_height - take < h)
5285 {
5286 take -= frp->fr_height - h;
5287 frame_new_height(frp, h, FALSE, FALSE);
5288 }
5289 else
5290 {
5291 frame_new_height(frp, frp->fr_height - take,
5292 FALSE, FALSE);
5293 take = 0;
5294 }
5295 }
5296 if (run == 0)
5297 frp = frp->fr_next;
5298 else
5299 frp = frp->fr_prev;
5300 }
5301 }
5302 }
5303}
5304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305/*
5306 * Set current window width and take care of repositioning other windows to
5307 * fit around it.
5308 */
5309 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005310win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311{
5312 win_setwidth_win(width, curwin);
5313}
5314
5315 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005316win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318 /* Always keep current window at least one column wide, even when
5319 * 'winminwidth' is zero. */
5320 if (wp == curwin)
5321 {
5322 if (width < p_wmw)
5323 width = p_wmw;
5324 if (width == 0)
5325 width = 1;
5326 }
5327
5328 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5329
5330 /* recompute the window positions */
5331 (void)win_comp_pos();
5332
5333 redraw_all_later(NOT_VALID);
5334}
5335
5336/*
5337 * Set the width of a frame to "width" and take care that all frames and
5338 * windows inside it are resized. Also resize frames above and below if the
5339 * are in the same FR_ROW frame.
5340 *
5341 * Strategy is similar to frame_setheight().
5342 */
5343 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005344frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345{
5346 int room; /* total number of lines available */
5347 int take; /* number of lines taken from other windows */
5348 int run;
5349 frame_T *frp;
5350 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005351 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
5353 /* If the width already is the desired value, nothing to do. */
5354 if (curfrp->fr_width == width)
5355 return;
5356
5357 if (curfrp->fr_parent == NULL)
5358 /* topframe: can't change width */
5359 return;
5360
5361 if (curfrp->fr_parent->fr_layout == FR_COL)
5362 {
5363 /* Column of frames: Also need to resize frames above and below of
5364 * this one. First check for the minimal width of these. */
5365 w = frame_minwidth(curfrp->fr_parent, NULL);
5366 if (width < w)
5367 width = w;
5368 frame_setwidth(curfrp->fr_parent, width);
5369 }
5370 else
5371 {
5372 /*
5373 * Row of frames: try to change only frames in this row.
5374 *
5375 * Do this twice:
5376 * 1: compute room available, if it's not enough try resizing the
5377 * containing frame.
5378 * 2: compute the room available and adjust the width to it.
5379 */
5380 for (run = 1; run <= 2; ++run)
5381 {
5382 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005383 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005384 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005386 if (frp != curfrp
5387 && frp->fr_win != NULL
5388 && frp->fr_win->w_p_wfw)
5389 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 room += frp->fr_width;
5391 if (frp != curfrp)
5392 room -= frame_minwidth(frp, NULL);
5393 }
5394
5395 if (width <= room)
5396 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005397 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
5399 if (width > room)
5400 width = room;
5401 break;
5402 }
5403 frame_setwidth(curfrp->fr_parent, width
5404 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5405 }
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 /*
5408 * Compute the number of lines we will take from others frames (can be
5409 * negative!).
5410 */
5411 take = width - curfrp->fr_width;
5412
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005413 /* If there is not enough room, also reduce the width of a window
5414 * with 'winfixwidth' set. */
5415 if (width > room - room_reserved)
5416 room_reserved = room - width;
5417 /* If there is only a 'winfixwidth' window and making the
5418 * window smaller, need to make the other window narrower. */
5419 if (take < 0 && room - curfrp->fr_width < room_reserved)
5420 room_reserved = 0;
5421
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 /*
5423 * set the current frame to the new width
5424 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005425 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426
5427 /*
5428 * First take lines from the frames right of the current frame. If
5429 * that is not enough, takes lines from frames left of the current
5430 * frame.
5431 */
5432 for (run = 0; run < 2; ++run)
5433 {
5434 if (run == 0)
5435 frp = curfrp->fr_next; /* 1st run: start with next window */
5436 else
5437 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5438 while (frp != NULL && take != 0)
5439 {
5440 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005441 if (room_reserved > 0
5442 && frp->fr_win != NULL
5443 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005445 if (room_reserved >= frp->fr_width)
5446 room_reserved -= frp->fr_width;
5447 else
5448 {
5449 if (frp->fr_width - room_reserved > take)
5450 room_reserved = frp->fr_width - take;
5451 take -= frp->fr_width - room_reserved;
5452 frame_new_width(frp, room_reserved, FALSE, FALSE);
5453 room_reserved = 0;
5454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 }
5456 else
5457 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005458 if (frp->fr_width - take < w)
5459 {
5460 take -= frp->fr_width - w;
5461 frame_new_width(frp, w, FALSE, FALSE);
5462 }
5463 else
5464 {
5465 frame_new_width(frp, frp->fr_width - take,
5466 FALSE, FALSE);
5467 take = 0;
5468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 }
5470 if (run == 0)
5471 frp = frp->fr_next;
5472 else
5473 frp = frp->fr_prev;
5474 }
5475 }
5476 }
5477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
5479/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005480 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 */
5482 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005483win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005486 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005489 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 while (p_wmh > 0)
5491 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005492 room = Rows - p_ch;
5493 needed = frame_minheight(topframe, NULL);
5494 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 break;
5496 --p_wmh;
5497 if (first)
5498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005499 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 first = FALSE;
5501 }
5502 }
5503}
5504
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005505/*
5506 * Check 'winminwidth' for a valid value and reduce it if needed.
5507 */
5508 void
5509win_setminwidth(void)
5510{
5511 int room;
5512 int needed;
5513 int first = TRUE;
5514
5515 // loop until there is a 'winminheight' that is possible
5516 while (p_wmw > 0)
5517 {
5518 room = Columns;
5519 needed = frame_minwidth(topframe, NULL);
5520 if (room >= needed)
5521 break;
5522 --p_wmw;
5523 if (first)
5524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005525 emsg(_(e_noroom));
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005526 first = FALSE;
5527 }
5528 }
5529}
5530
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005531#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
5533/*
5534 * Status line of dragwin is dragged "offset" lines down (negative is up).
5535 */
5536 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005537win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
5539 frame_T *curfr;
5540 frame_T *fr;
5541 int room;
5542 int row;
5543 int up; /* if TRUE, drag status line up, otherwise down */
5544 int n;
5545
5546 fr = dragwin->w_frame;
5547 curfr = fr;
5548 if (fr != topframe) /* more than one window */
5549 {
5550 fr = fr->fr_parent;
5551 /* When the parent frame is not a column of frames, its parent should
5552 * be. */
5553 if (fr->fr_layout != FR_COL)
5554 {
5555 curfr = fr;
5556 if (fr != topframe) /* only a row of windows, may drag statusline */
5557 fr = fr->fr_parent;
5558 }
5559 }
5560
5561 /* If this is the last frame in a column, may want to resize the parent
5562 * frame instead (go two up to skip a row of frames). */
5563 while (curfr != topframe && curfr->fr_next == NULL)
5564 {
5565 if (fr != topframe)
5566 fr = fr->fr_parent;
5567 curfr = fr;
5568 if (fr != topframe)
5569 fr = fr->fr_parent;
5570 }
5571
5572 if (offset < 0) /* drag up */
5573 {
5574 up = TRUE;
5575 offset = -offset;
5576 /* sum up the room of the current frame and above it */
5577 if (fr == curfr)
5578 {
5579 /* only one window */
5580 room = fr->fr_height - frame_minheight(fr, NULL);
5581 }
5582 else
5583 {
5584 room = 0;
5585 for (fr = fr->fr_child; ; fr = fr->fr_next)
5586 {
5587 room += fr->fr_height - frame_minheight(fr, NULL);
5588 if (fr == curfr)
5589 break;
5590 }
5591 }
5592 fr = curfr->fr_next; /* put fr at frame that grows */
5593 }
5594 else /* drag down */
5595 {
5596 up = FALSE;
5597 /*
5598 * Only dragging the last status line can reduce p_ch.
5599 */
5600 room = Rows - cmdline_row;
5601 if (curfr->fr_next == NULL)
5602 room -= 1;
5603 else
5604 room -= p_ch;
5605 if (room < 0)
5606 room = 0;
5607 /* sum up the room of frames below of the current one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005608 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 room += fr->fr_height - frame_minheight(fr, NULL);
5610 fr = curfr; /* put fr at window that grows */
5611 }
5612
5613 if (room < offset) /* Not enough room */
5614 offset = room; /* Move as far as we can */
5615 if (offset <= 0)
5616 return;
5617
5618 /*
5619 * Grow frame fr by "offset" lines.
5620 * Doesn't happen when dragging the last status line up.
5621 */
5622 if (fr != NULL)
5623 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5624
5625 if (up)
5626 fr = curfr; /* current frame gets smaller */
5627 else
5628 fr = curfr->fr_next; /* next frame gets smaller */
5629
5630 /*
5631 * Now make the other frames smaller.
5632 */
5633 while (fr != NULL && offset > 0)
5634 {
5635 n = frame_minheight(fr, NULL);
5636 if (fr->fr_height - offset <= n)
5637 {
5638 offset -= fr->fr_height - n;
5639 frame_new_height(fr, n, !up, FALSE);
5640 }
5641 else
5642 {
5643 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5644 break;
5645 }
5646 if (up)
5647 fr = fr->fr_prev;
5648 else
5649 fr = fr->fr_next;
5650 }
5651 row = win_comp_pos();
5652 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5653 cmdline_row = row;
5654 p_ch = Rows - cmdline_row;
5655 if (p_ch < 1)
5656 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005657 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005658 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 showmode();
5660}
5661
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662/*
5663 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5664 */
5665 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005666win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 frame_T *curfr;
5669 frame_T *fr;
5670 int room;
5671 int left; /* if TRUE, drag separator line left, otherwise right */
5672 int n;
5673
5674 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005675 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 return;
5677 curfr = fr;
5678 fr = fr->fr_parent;
5679 /* When the parent frame is not a row of frames, its parent should be. */
5680 if (fr->fr_layout != FR_ROW)
5681 {
5682 if (fr == topframe) /* only a column of windows (cannot happen?) */
5683 return;
5684 curfr = fr;
5685 fr = fr->fr_parent;
5686 }
5687
5688 /* If this is the last frame in a row, may want to resize a parent
5689 * frame instead. */
5690 while (curfr->fr_next == NULL)
5691 {
5692 if (fr == topframe)
5693 break;
5694 curfr = fr;
5695 fr = fr->fr_parent;
5696 if (fr != topframe)
5697 {
5698 curfr = fr;
5699 fr = fr->fr_parent;
5700 }
5701 }
5702
5703 if (offset < 0) /* drag left */
5704 {
5705 left = TRUE;
5706 offset = -offset;
5707 /* sum up the room of the current frame and left of it */
5708 room = 0;
5709 for (fr = fr->fr_child; ; fr = fr->fr_next)
5710 {
5711 room += fr->fr_width - frame_minwidth(fr, NULL);
5712 if (fr == curfr)
5713 break;
5714 }
5715 fr = curfr->fr_next; /* put fr at frame that grows */
5716 }
5717 else /* drag right */
5718 {
5719 left = FALSE;
5720 /* sum up the room of frames right of the current one */
5721 room = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005722 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 room += fr->fr_width - frame_minwidth(fr, NULL);
5724 fr = curfr; /* put fr at window that grows */
5725 }
5726
5727 if (room < offset) /* Not enough room */
5728 offset = room; /* Move as far as we can */
5729 if (offset <= 0) /* No room at all, quit. */
5730 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005731 if (fr == NULL)
5732 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733
5734 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005735 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 /* shrink other frames: current and at the left or at the right */
5738 if (left)
5739 fr = curfr; /* current frame gets smaller */
5740 else
5741 fr = curfr->fr_next; /* next frame gets smaller */
5742
5743 while (fr != NULL && offset > 0)
5744 {
5745 n = frame_minwidth(fr, NULL);
5746 if (fr->fr_width - offset <= n)
5747 {
5748 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005749 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
5751 else
5752 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005753 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 break;
5755 }
5756 if (left)
5757 fr = fr->fr_prev;
5758 else
5759 fr = fr->fr_next;
5760 }
5761 (void)win_comp_pos();
5762 redraw_all_later(NOT_VALID);
5763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764#endif /* FEAT_MOUSE */
5765
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005766#define FRACTION_MULT 16384L
5767
5768/*
5769 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005770 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005771 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005772 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005773set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005774{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005775 if (wp->w_height > 1)
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005776 // When cursor is in the first line the percentage is computed as if
5777 // it's halfway that line. Thus with two lines it is 25%, with three
5778 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005779 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005780 + FRACTION_MULT / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005781}
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783/*
5784 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005785 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 * This takes care of the things inside the window, not what happens to the
5787 * window position, the frame or to other windows.
5788 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005789 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005790win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005792 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 /* Don't want a negative height. Happens when splitting a tiny window.
5795 * Will equalize heights soon to fix it. */
5796 if (height < 0)
5797 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005798 if (wp->w_height == height)
5799 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005801 if (wp->w_height > 0)
5802 {
5803 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005804 /* w_wrow needs to be valid. When setting 'laststatus' this may
5805 * call win_new_height() recursively. */
5806 validate_cursor();
5807 if (wp->w_height != prev_height)
5808 return; /* Recursive call already changed the size, bail out here
5809 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005810 if (wp->w_wrow != wp->w_prev_fraction_row)
5811 set_fraction(wp);
5812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813
5814 wp->w_height = height;
5815 wp->w_skipcol = 0;
5816
Bram Moolenaar955f1982017-02-05 15:10:51 +01005817 /* There is no point in adjusting the scroll position when exiting. Some
5818 * values might be invalid. */
5819 if (!exiting)
5820 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02005821}
5822
5823 void
5824scroll_to_fraction(win_T *wp, int prev_height)
5825{
5826 linenr_T lnum;
5827 int sline, line_size;
5828 int height = wp->w_height;
5829
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005830 // Don't change w_topline when height is zero. Don't set w_topline when
5831 // 'scrollbind' is set and this isn't the current window.
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005832 if (height > 0 && (!wp->w_p_scb || wp == curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
Bram Moolenaar34114692005-01-02 11:28:13 +00005834 /*
5835 * Find a value for w_topline that shows the cursor at the same
5836 * relative position in the window as before (more or less).
5837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 lnum = wp->w_cursor.lnum;
5839 if (lnum < 1) /* can happen when starting up */
5840 lnum = 1;
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005841 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L)
5842 / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
5844 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005845
5846 if (sline >= 0)
5847 {
5848 /* Make sure the whole cursor line is visible, if possible. */
5849 int rows = plines_win(wp, lnum, FALSE);
5850
5851 if (sline > wp->w_height - rows)
5852 {
5853 sline = wp->w_height - rows;
5854 wp->w_wrow -= rows - line_size;
5855 }
5856 }
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 if (sline < 0)
5859 {
5860 /*
5861 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00005862 * Make cursor line the first line in the window. If not enough
5863 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 */
5865 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00005866 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02005867 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00005868 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005869 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00005870 --wp->w_wrow;
5871 while (wp->w_wrow >= wp->w_height)
5872 {
Bram Moolenaar02631462017-09-22 15:20:32 +02005873 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00005874 + win_col_off2(wp);
5875 --wp->w_wrow;
5876 }
5877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005879 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
Bram Moolenaar26470632006-10-24 19:12:40 +00005881 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
5883#ifdef FEAT_FOLDING
5884 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
5885 if (lnum == 1)
5886 {
5887 /* first line in buffer is folded */
5888 line_size = 1;
5889 --sline;
5890 break;
5891 }
5892#endif
5893 --lnum;
5894#ifdef FEAT_DIFF
5895 if (lnum == wp->w_topline)
5896 line_size = plines_win_nofill(wp, lnum, TRUE)
5897 + wp->w_topfill;
5898 else
5899#endif
5900 line_size = plines_win(wp, lnum, TRUE);
5901 sline -= line_size;
5902 }
Bram Moolenaar34114692005-01-02 11:28:13 +00005903
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 if (sline < 0)
5905 {
5906 /*
5907 * Line we want at top would go off top of screen. Use next
5908 * line instead.
5909 */
5910#ifdef FEAT_FOLDING
5911 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
5912#endif
5913 lnum++;
5914 wp->w_wrow -= line_size + sline;
5915 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02005916 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005918 // First line of file reached, use that as topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 lnum = 1;
5920 wp->w_wrow -= sline;
5921 }
5922 }
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005923 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 }
5925
5926 if (wp == curwin)
5927 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01005928 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 update_topline();
5930 curs_columns(FALSE); /* validate w_wrow */
5931 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005932 if (prev_height > 0)
5933 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
5935 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005936 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 invalidate_botline_win(wp);
5939}
5940
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941/*
5942 * Set the width of a window.
5943 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005944 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005945win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
5947 wp->w_width = width;
5948 wp->w_lines_valid = 0;
5949 changed_line_abv_curs_win(wp);
5950 invalidate_botline_win(wp);
5951 if (wp == curwin)
5952 {
5953 update_topline();
5954 curs_columns(TRUE); /* validate w_wrow */
5955 }
5956 redraw_win_later(wp, NOT_VALID);
5957 wp->w_redr_status = TRUE;
5958}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959
5960 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005961win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962{
5963 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
5964 if (wp->w_p_scr == 0)
5965 wp->w_p_scr = 1;
5966}
5967
5968/*
5969 * command_height: called whenever p_ch has been changed
5970 */
5971 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005972command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 int h;
5975 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005976 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005978 /* Use the value of p_ch that we remembered. This is needed for when the
5979 * GUI starts up, we can't be sure in what order things happen. And when
5980 * p_ch was changed in another tab page. */
5981 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005982
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 /* Find bottom frame with width of screen. */
5984 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 while (frp->fr_width != Columns && frp->fr_parent != NULL)
5986 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
5988 /* Avoid changing the height of a window with 'winfixheight' set. */
5989 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
5990 && frp->fr_win->w_p_wfh)
5991 frp = frp->fr_prev;
5992
5993 if (starting != NO_SCREEN)
5994 {
5995 cmdline_row = Rows - p_ch;
5996
5997 if (p_ch > old_p_ch) /* p_ch got bigger */
5998 {
5999 while (p_ch > old_p_ch)
6000 {
6001 if (frp == NULL)
6002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006003 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006005 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 cmdline_row = Rows - p_ch;
6007 break;
6008 }
6009 h = frp->fr_height - frame_minheight(frp, NULL);
6010 if (h > p_ch - old_p_ch)
6011 h = p_ch - old_p_ch;
6012 old_p_ch += h;
6013 frame_add_height(frp, -h);
6014 frp = frp->fr_prev;
6015 }
6016
6017 /* Recompute window positions. */
6018 (void)win_comp_pos();
6019
6020 /* clear the lines added to cmdline */
6021 if (full_screen)
6022 screen_fill((int)(cmdline_row), (int)Rows, 0,
6023 (int)Columns, ' ', ' ', 0);
6024 msg_row = cmdline_row;
6025 redraw_cmdline = TRUE;
6026 return;
6027 }
6028
6029 if (msg_row < cmdline_row)
6030 msg_row = cmdline_row;
6031 redraw_cmdline = TRUE;
6032 }
6033 frame_add_height(frp, (int)(old_p_ch - p_ch));
6034
6035 /* Recompute window positions. */
6036 if (frp != lastwin->w_frame)
6037 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038}
6039
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040/*
6041 * Resize frame "frp" to be "n" lines higher (negative for less high).
6042 * Also resize the frames it is contained in.
6043 */
6044 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006045frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6048 for (;;)
6049 {
6050 frp = frp->fr_parent;
6051 if (frp == NULL)
6052 break;
6053 frp->fr_height += n;
6054 }
6055}
6056
6057/*
6058 * Add or remove a status line for the bottom window(s), according to the
6059 * value of 'laststatus'.
6060 */
6061 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006062last_status(
6063 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 /* Don't make a difference between horizontal or vertical split. */
6066 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006067 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068}
6069
6070 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006071last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 frame_T *fp;
6074 win_T *wp;
6075
6076 if (fr->fr_layout == FR_LEAF)
6077 {
6078 wp = fr->fr_win;
6079 if (wp->w_status_height != 0 && !statusline)
6080 {
6081 /* remove status line */
6082 win_new_height(wp, wp->w_height + 1);
6083 wp->w_status_height = 0;
6084 comp_col();
6085 }
6086 else if (wp->w_status_height == 0 && statusline)
6087 {
6088 /* Find a frame to take a line from. */
6089 fp = fr;
6090 while (fp->fr_height <= frame_minheight(fp, NULL))
6091 {
6092 if (fp == topframe)
6093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006094 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 return;
6096 }
6097 /* In a column of frames: go to frame above. If already at
6098 * the top or in a row of frames: go to parent. */
6099 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6100 fp = fp->fr_prev;
6101 else
6102 fp = fp->fr_parent;
6103 }
6104 wp->w_status_height = 1;
6105 if (fp != fr)
6106 {
6107 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6108 frame_fix_height(wp);
6109 (void)win_comp_pos();
6110 }
6111 else
6112 win_new_height(wp, wp->w_height - 1);
6113 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006114 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 }
6116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 else if (fr->fr_layout == FR_ROW)
6118 {
6119 /* vertically split windows, set status line for each one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006120 FOR_ALL_FRAMES(fp, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 last_status_rec(fp, statusline);
6122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 else
6124 {
6125 /* horizontally split window, set status line for last one */
6126 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6127 ;
6128 last_status_rec(fp, statusline);
6129 }
6130}
6131
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006132/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006133 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006134 */
6135 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006136tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006137{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006138#ifdef FEAT_GUI_TABLINE
6139 /* When the GUI has the tabline then this always returns zero. */
6140 if (gui_use_tabline())
6141 return 0;
6142#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006143 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006144 {
6145 case 0: return 0;
6146 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6147 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006148 return 1;
6149}
6150
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151/*
6152 * Return the minimal number of rows that is needed on the screen to display
6153 * the current number of windows.
6154 */
6155 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006156min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006159 tabpage_T *tp;
6160 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161
6162 if (firstwin == NULL) /* not initialized yet */
6163 return MIN_LINES;
6164
Bram Moolenaarf740b292006-02-16 22:11:02 +00006165 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006166 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006167 {
6168 n = frame_minheight(tp->tp_topframe, NULL);
6169 if (total < n)
6170 total = n;
6171 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006172 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006173 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 return total;
6175}
6176
6177/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006178 * Return TRUE if there is only one window (in the current tab page), not
6179 * counting a help or preview window, unless it is the current window.
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006180 * Does not count "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 */
6182 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006183only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 int count = 0;
6186 win_T *wp;
6187
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006188 /* If there is another tab page there always is another window. */
6189 if (first_tabpage->tp_next != NULL)
6190 return FALSE;
6191
Bram Moolenaar29323592016-07-24 22:04:11 +02006192 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006193 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006194 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195# ifdef FEAT_QUICKFIX
6196 || wp->w_p_pvw
6197# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006198 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 ++count;
6200 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201}
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203/*
6204 * Correct the cursor line number in other windows. Used after changing the
6205 * current buffer, and before applying autocommands.
6206 * When "do_curwin" is TRUE, also check current window.
6207 */
6208 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006209check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
6211 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006212 tabpage_T *tp;
6213
6214 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 {
Bram Moolenaara68e5952019-04-25 22:22:01 +02006217 // save the original cursor position and topline
6218 wp->w_save_cursor.w_cursor_save = wp->w_cursor;
6219 wp->w_save_cursor.w_topline_save = wp->w_topline;
6220
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6222 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6223 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6224 wp->w_topline = curbuf->b_ml.ml_line_count;
Bram Moolenaara68e5952019-04-25 22:22:01 +02006225
6226 // save the corrected cursor position and topline
6227 wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
6228 wp->w_save_cursor.w_topline_corr = wp->w_topline;
6229 }
6230}
6231
6232/*
6233 * Reset cursor and topline to its stored values from check_lnums().
6234 * check_lnums() must have been called first!
6235 */
6236 void
6237reset_lnums()
6238{
6239 win_T *wp;
6240 tabpage_T *tp;
6241
6242 FOR_ALL_TAB_WINDOWS(tp, wp)
6243 if (wp->w_buffer == curbuf)
6244 {
6245 // Restore the value if the autocommand didn't change it.
6246 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
6247 wp->w_cursor = wp->w_save_cursor.w_cursor_save;
6248 if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
6249 wp->w_topline = wp->w_save_cursor.w_topline_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252
6253/*
6254 * A snapshot of the window sizes, to restore them after closing the help
6255 * window.
6256 * Only these fields are used:
6257 * fr_layout
6258 * fr_width
6259 * fr_height
6260 * fr_next
6261 * fr_child
6262 * fr_win (only valid for the old curwin, NULL otherwise)
6263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
6265/*
6266 * Create a snapshot of the current frame sizes.
6267 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006268 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006269make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006271 clear_snapshot(curtab, idx);
6272 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273}
6274
6275 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006276make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 *frp = (frame_T *)alloc_clear((unsigned)sizeof(frame_T));
6279 if (*frp == NULL)
6280 return;
6281 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 (*frp)->fr_height = fr->fr_height;
6284 if (fr->fr_next != NULL)
6285 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6286 if (fr->fr_child != NULL)
6287 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6288 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6289 (*frp)->fr_win = curwin;
6290}
6291
6292/*
6293 * Remove any existing snapshot.
6294 */
6295 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006296clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006298 clear_snapshot_rec(tp->tp_snapshot[idx]);
6299 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}
6301
6302 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006303clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
6305 if (fr != NULL)
6306 {
6307 clear_snapshot_rec(fr->fr_next);
6308 clear_snapshot_rec(fr->fr_child);
6309 vim_free(fr);
6310 }
6311}
6312
6313/*
6314 * Restore a previously created snapshot, if there is any.
6315 * This is only done if the screen size didn't change and the window layout is
6316 * still the same.
6317 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006318 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006319restore_snapshot(
6320 int idx,
6321 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 win_T *wp;
6324
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006325 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006326 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006327 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6328 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006330 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 win_comp_pos();
6332 if (wp != NULL && close_curwin)
6333 win_goto(wp);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01006334 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006336 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337}
6338
6339/*
6340 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006341 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 */
6343 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006344check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 if (sn->fr_layout != fr->fr_layout
6347 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6348 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6349 || (sn->fr_next != NULL
6350 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6351 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006352 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006353 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 return FAIL;
6355 return OK;
6356}
6357
6358/*
6359 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6360 * following frames and children.
6361 * Returns a pointer to the old current window, or NULL.
6362 */
6363 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006364restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365{
6366 win_T *wp = NULL;
6367 win_T *wp2;
6368
6369 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 if (fr->fr_layout == FR_LEAF)
6372 {
6373 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006374 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 wp = sn->fr_win;
6376 }
6377 if (sn->fr_next != NULL)
6378 {
6379 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6380 if (wp2 != NULL)
6381 wp = wp2;
6382 }
6383 if (sn->fr_child != NULL)
6384 {
6385 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6386 if (wp2 != NULL)
6387 wp = wp2;
6388 }
6389 return wp;
6390}
6391
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006392#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6393 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006394/*
6395 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006396 * restore_win() MUST be called to undo, also when FAIL is returned.
6397 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006398 * When "no_display" is TRUE the display won't be affected, no redraw is
6399 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006400 * Returns FAIL if switching to "win" failed.
6401 */
6402 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006403switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006404 win_T **save_curwin,
6405 tabpage_T **save_curtab,
6406 win_T *win,
6407 tabpage_T *tp,
6408 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006409{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006410 block_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006411 *save_curwin = curwin;
6412 if (tp != NULL)
6413 {
6414 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006415 if (no_display)
6416 {
6417 curtab->tp_firstwin = firstwin;
6418 curtab->tp_lastwin = lastwin;
6419 curtab = tp;
6420 firstwin = curtab->tp_firstwin;
6421 lastwin = curtab->tp_lastwin;
6422 }
6423 else
6424 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006425 }
6426 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006427 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006428 curwin = win;
6429 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006430 return OK;
6431}
6432
6433/*
6434 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006435 * When "no_display" is TRUE the display won't be affected, no redraw is
6436 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006437 */
6438 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006439restore_win(
6440 win_T *save_curwin UNUSED,
6441 tabpage_T *save_curtab UNUSED,
6442 int no_display UNUSED)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006443{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006444 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006445 {
6446 if (no_display)
6447 {
6448 curtab->tp_firstwin = firstwin;
6449 curtab->tp_lastwin = lastwin;
6450 curtab = save_curtab;
6451 firstwin = curtab->tp_firstwin;
6452 lastwin = curtab->tp_lastwin;
6453 }
6454 else
6455 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6456 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006457 if (win_valid(save_curwin))
6458 {
6459 curwin = save_curwin;
6460 curbuf = curwin->w_buffer;
6461 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006462 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006463}
6464
6465/*
6466 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6467 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6468 */
6469 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006470switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006471{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006472 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006473 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006474 --curbuf->b_nwindows;
6475 curbuf = buf;
6476 curwin->w_buffer = buf;
6477 ++curbuf->b_nwindows;
6478}
6479
6480/*
6481 * Restore the current buffer after using switch_buffer().
6482 */
6483 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006484restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006485{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006486 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006487 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006488 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006489 {
6490 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006491 curwin->w_buffer = save_curbuf->br_buf;
6492 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006493 ++curbuf->b_nwindows;
6494 }
6495}
6496#endif
6497
Bram Moolenaar4033c552017-09-16 20:54:51 +02006498#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499/*
6500 * Return TRUE if there is any vertically split window.
6501 */
6502 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006503win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504{
6505 frame_T *fr;
6506
6507 if (topframe->fr_layout == FR_ROW)
6508 return TRUE;
6509
6510 if (topframe->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006511 FOR_ALL_FRAMES(fr, topframe->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (fr->fr_layout == FR_ROW)
6513 return TRUE;
6514
6515 return FALSE;
6516}
6517#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006518
6519#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
6520/*
6521 * Add match to the match list of window 'wp'. The pattern 'pat' will be
Bram Moolenaare37d50a2008-08-06 17:06:04 +00006522 * highlighted with the group 'grp' with priority 'prio'.
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006523 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
6524 * If no particular ID is desired, -1 must be specified for 'id'.
6525 * Return ID of added match, -1 on failure.
6526 */
6527 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006528match_add(
6529 win_T *wp,
6530 char_u *grp,
6531 char_u *pat,
6532 int prio,
6533 int id,
6534 list_T *pos_list,
6535 char_u *conceal_char UNUSED) /* pointer to conceal replacement char */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006536{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006537 matchitem_T *cur;
6538 matchitem_T *prev;
6539 matchitem_T *m;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006540 int hlg_id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006541 regprog_T *regprog = NULL;
6542 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006543
Bram Moolenaarb3414592014-06-17 17:48:32 +02006544 if (*grp == NUL || (pat != NULL && *pat == NUL))
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006545 return -1;
6546 if (id < -1 || id == 0)
6547 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006548 semsg(_("E799: Invalid ID: %d (must be greater than or equal to 1)"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006549 return -1;
6550 }
6551 if (id != -1)
6552 {
6553 cur = wp->w_match_head;
6554 while (cur != NULL)
6555 {
6556 if (cur->id == id)
6557 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006558 semsg(_("E801: ID already taken: %d"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006559 return -1;
6560 }
6561 cur = cur->next;
6562 }
6563 }
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00006564 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006566 semsg(_(e_nogroup), grp);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006567 return -1;
6568 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006569 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006570 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006571 semsg(_(e_invarg2), pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006572 return -1;
6573 }
6574
6575 /* Find available match ID. */
6576 while (id == -1)
6577 {
6578 cur = wp->w_match_head;
6579 while (cur != NULL && cur->id != wp->w_next_match_id)
6580 cur = cur->next;
6581 if (cur == NULL)
6582 id = wp->w_next_match_id;
6583 wp->w_next_match_id++;
6584 }
6585
6586 /* Build new match. */
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006587 m = (matchitem_T *)alloc_clear(sizeof(matchitem_T));
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006588 m->id = id;
6589 m->priority = prio;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006590 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006591 m->hlg_id = hlg_id;
Bram Moolenaar0963cd92007-08-05 16:49:43 +00006592 m->match.regprog = regprog;
6593 m->match.rmm_ic = FALSE;
6594 m->match.rmm_maxcol = 0;
Bram Moolenaar264b74f2019-01-24 17:18:42 +01006595# if defined(FEAT_CONCEAL)
Bram Moolenaar6561d522015-07-21 15:48:27 +02006596 m->conceal_char = 0;
6597 if (conceal_char != NULL)
6598 m->conceal_char = (*mb_ptr2char)(conceal_char);
Bram Moolenaar42356152016-03-31 22:27:40 +02006599# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006600
Bram Moolenaarb3414592014-06-17 17:48:32 +02006601 /* Set up position matches */
6602 if (pos_list != NULL)
6603 {
6604 linenr_T toplnum = 0;
6605 linenr_T botlnum = 0;
6606 listitem_T *li;
6607 int i;
6608
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006609 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006610 i++, li = li->li_next)
6611 {
6612 linenr_T lnum = 0;
6613 colnr_T col = 0;
6614 int len = 1;
6615 list_T *subl;
6616 listitem_T *subli;
Bram Moolenaardeae0f22014-06-18 21:20:11 +02006617 int error = FALSE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006618
Bram Moolenaarb3414592014-06-17 17:48:32 +02006619 if (li->li_tv.v_type == VAR_LIST)
6620 {
6621 subl = li->li_tv.vval.v_list;
6622 if (subl == NULL)
6623 goto fail;
6624 subli = subl->lv_first;
6625 if (subli == NULL)
6626 goto fail;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006627 lnum = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006628 if (error == TRUE)
6629 goto fail;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006630 if (lnum == 0)
6631 {
6632 --i;
6633 continue;
6634 }
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006635 m->pos.pos[i].lnum = lnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006636 subli = subli->li_next;
6637 if (subli != NULL)
6638 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006639 col = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006640 if (error == TRUE)
6641 goto fail;
6642 subli = subli->li_next;
6643 if (subli != NULL)
6644 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006645 len = tv_get_number_chk(&subli->li_tv, &error);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006646 if (error == TRUE)
6647 goto fail;
6648 }
6649 }
6650 m->pos.pos[i].col = col;
6651 m->pos.pos[i].len = len;
6652 }
6653 else if (li->li_tv.v_type == VAR_NUMBER)
6654 {
6655 if (li->li_tv.vval.v_number == 0)
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006656 {
6657 --i;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006658 continue;
Bram Moolenaarb6da44a2014-06-25 18:15:22 +02006659 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006660 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
6661 m->pos.pos[i].col = 0;
6662 m->pos.pos[i].len = 0;
6663 }
6664 else
6665 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006666 emsg(_("List or number required"));
Bram Moolenaarb3414592014-06-17 17:48:32 +02006667 goto fail;
6668 }
6669 if (toplnum == 0 || lnum < toplnum)
6670 toplnum = lnum;
Bram Moolenaar41d75232014-06-25 17:58:11 +02006671 if (botlnum == 0 || lnum >= botlnum)
6672 botlnum = lnum + 1;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006673 }
6674
6675 /* Calculate top and bottom lines for redrawing area */
6676 if (toplnum != 0)
6677 {
6678 if (wp->w_buffer->b_mod_set)
6679 {
6680 if (wp->w_buffer->b_mod_top > toplnum)
6681 wp->w_buffer->b_mod_top = toplnum;
6682 if (wp->w_buffer->b_mod_bot < botlnum)
6683 wp->w_buffer->b_mod_bot = botlnum;
6684 }
6685 else
6686 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006687 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006688 wp->w_buffer->b_mod_top = toplnum;
6689 wp->w_buffer->b_mod_bot = botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006690 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006691 }
6692 m->pos.toplnum = toplnum;
6693 m->pos.botlnum = botlnum;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006694 rtype = VALID;
6695 }
6696 }
6697
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006698 /* Insert new match. The match list is in ascending order with regard to
6699 * the match priorities. */
6700 cur = wp->w_match_head;
6701 prev = cur;
6702 while (cur != NULL && prio >= cur->priority)
6703 {
6704 prev = cur;
6705 cur = cur->next;
6706 }
6707 if (cur == prev)
6708 wp->w_match_head = m;
6709 else
6710 prev->next = m;
6711 m->next = cur;
6712
Bram Moolenaarb3414592014-06-17 17:48:32 +02006713 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006714 return id;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006715
6716fail:
6717 vim_free(m);
6718 return -1;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006719}
6720
6721/*
6722 * Delete match with ID 'id' in the match list of window 'wp'.
6723 * Print error messages if 'perr' is TRUE.
6724 */
6725 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006726match_delete(win_T *wp, int id, int perr)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006727{
Bram Moolenaarb3414592014-06-17 17:48:32 +02006728 matchitem_T *cur = wp->w_match_head;
6729 matchitem_T *prev = cur;
6730 int rtype = SOME_VALID;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006731
6732 if (id < 1)
6733 {
6734 if (perr == TRUE)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006735 semsg(_("E802: Invalid ID: %d (must be greater than or equal to 1)"),
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006736 id);
6737 return -1;
6738 }
6739 while (cur != NULL && cur->id != id)
6740 {
6741 prev = cur;
6742 cur = cur->next;
6743 }
6744 if (cur == NULL)
6745 {
6746 if (perr == TRUE)
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006747 semsg(_("E803: ID not found: %d"), id);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006748 return -1;
6749 }
6750 if (cur == prev)
6751 wp->w_match_head = cur->next;
6752 else
6753 prev->next = cur->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006754 vim_regfree(cur->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006755 vim_free(cur->pattern);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006756 if (cur->pos.toplnum != 0)
6757 {
6758 if (wp->w_buffer->b_mod_set)
6759 {
6760 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
6761 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6762 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
6763 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
6764 }
6765 else
6766 {
Bram Moolenaardab70c62014-07-02 17:16:58 +02006767 wp->w_buffer->b_mod_set = TRUE;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006768 wp->w_buffer->b_mod_top = cur->pos.toplnum;
6769 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
Bram Moolenaardab70c62014-07-02 17:16:58 +02006770 wp->w_buffer->b_mod_xlines = 0;
Bram Moolenaarb3414592014-06-17 17:48:32 +02006771 }
Bram Moolenaarb3414592014-06-17 17:48:32 +02006772 rtype = VALID;
6773 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006774 vim_free(cur);
Bram Moolenaarb3414592014-06-17 17:48:32 +02006775 redraw_later(rtype);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006776 return 0;
6777}
6778
6779/*
6780 * Delete all matches in the match list of window 'wp'.
6781 */
6782 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006783clear_matches(win_T *wp)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006784{
6785 matchitem_T *m;
6786
6787 while (wp->w_match_head != NULL)
6788 {
6789 m = wp->w_match_head->next;
Bram Moolenaar473de612013-06-08 18:19:48 +02006790 vim_regfree(wp->w_match_head->match.regprog);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006791 vim_free(wp->w_match_head->pattern);
6792 vim_free(wp->w_match_head);
6793 wp->w_match_head = m;
6794 }
6795 redraw_later(SOME_VALID);
6796}
6797
6798/*
6799 * Get match from ID 'id' in window 'wp'.
6800 * Return NULL if match not found.
6801 */
6802 matchitem_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006803get_match(win_T *wp, int id)
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006804{
6805 matchitem_T *cur = wp->w_match_head;
6806
6807 while (cur != NULL && cur->id != id)
6808 cur = cur->next;
6809 return cur;
6810}
6811#endif
Bram Moolenaar6d216452013-05-12 19:00:41 +02006812
6813#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6814 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006815get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006816{
6817 int i = 1;
6818 win_T *w;
6819
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006820 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006821 ++i;
6822
6823 if (w == NULL)
6824 return 0;
6825 else
6826 return i;
6827}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006828
6829 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006830get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006831{
6832 int i = 1;
6833 tabpage_T *t;
6834
6835 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6836 ++i;
6837
6838 if (t == NULL)
6839 return 0;
6840 else
6841 return i;
6842}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006843#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006844
6845/*
6846 * Return TRUE if "topfrp" and its children are at the right height.
6847 */
6848 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006849frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006850{
6851 frame_T *frp;
6852
6853 if (topfrp->fr_height != height)
6854 return FALSE;
6855
6856 if (topfrp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006857 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006858 if (frp->fr_height != height)
6859 return FALSE;
6860
6861 return TRUE;
6862}
6863
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006864/*
6865 * Return TRUE if "topfrp" and its children are at the right width.
6866 */
6867 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006868frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006869{
6870 frame_T *frp;
6871
6872 if (topfrp->fr_width != width)
6873 return FALSE;
6874
6875 if (topfrp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006876 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006877 if (frp->fr_width != width)
6878 return FALSE;
6879
6880 return TRUE;
6881}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006882
Bram Moolenaar86edef62016-03-13 18:07:30 +01006883#if defined(FEAT_EVAL) || defined(PROTO)
6884 int
6885win_getid(typval_T *argvars)
6886{
6887 int winnr;
6888 win_T *wp;
6889
6890 if (argvars[0].v_type == VAR_UNKNOWN)
6891 return curwin->w_id;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006892 winnr = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006893 if (winnr > 0)
6894 {
6895 if (argvars[1].v_type == VAR_UNKNOWN)
6896 wp = firstwin;
6897 else
6898 {
6899 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006900 int tabnr = tv_get_number(&argvars[1]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006901
Bram Moolenaar29323592016-07-24 22:04:11 +02006902 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006903 if (--tabnr == 0)
6904 break;
6905 if (tp == NULL)
6906 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01006907 if (tp == curtab)
6908 wp = firstwin;
6909 else
6910 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01006911 }
6912 for ( ; wp != NULL; wp = wp->w_next)
6913 if (--winnr == 0)
6914 return wp->w_id;
6915 }
6916 return 0;
6917}
6918
6919 int
6920win_gotoid(typval_T *argvars)
6921{
6922 win_T *wp;
6923 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006924 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006925
Bram Moolenaar29323592016-07-24 22:04:11 +02006926 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006927 if (wp->w_id == id)
6928 {
6929 goto_tabpage_win(tp, wp);
6930 return 1;
6931 }
6932 return 0;
6933}
6934
6935 void
6936win_id2tabwin(typval_T *argvars, list_T *list)
6937{
6938 win_T *wp;
6939 tabpage_T *tp;
6940 int winnr = 1;
6941 int tabnr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006942 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006943
Bram Moolenaar29323592016-07-24 22:04:11 +02006944 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006945 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006946 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006947 {
6948 if (wp->w_id == id)
6949 {
6950 list_append_number(list, tabnr);
6951 list_append_number(list, winnr);
6952 return;
6953 }
6954 ++winnr;
6955 }
6956 ++tabnr;
6957 winnr = 1;
6958 }
6959 list_append_number(list, 0);
6960 list_append_number(list, 0);
6961}
6962
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006963 win_T *
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006964win_id2wp(int id)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006965{
6966 win_T *wp;
6967 tabpage_T *tp;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006968
6969 FOR_ALL_TAB_WINDOWS(tp, wp)
6970 if (wp->w_id == id)
6971 return wp;
6972
6973 return NULL;
6974}
6975
Bram Moolenaar86edef62016-03-13 18:07:30 +01006976 int
6977win_id2win(typval_T *argvars)
6978{
6979 win_T *wp;
6980 int nr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006981 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006982
Bram Moolenaar29323592016-07-24 22:04:11 +02006983 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006984 {
6985 if (wp->w_id == id)
6986 return nr;
6987 ++nr;
6988 }
6989 return 0;
6990}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006991
6992 void
6993win_findbuf(typval_T *argvars, list_T *list)
6994{
6995 win_T *wp;
6996 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006997 int bufnr = tv_get_number(&argvars[0]);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006998
Bram Moolenaar29323592016-07-24 22:04:11 +02006999 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01007000 if (wp->w_buffer->b_fnum == bufnr)
7001 list_append_number(list, wp->w_id);
7002}
7003
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02007004/*
7005 * Get the layout of the given tab page for winlayout().
7006 */
7007 void
7008get_framelayout(frame_T *fr, list_T *l, int outer)
7009{
7010 frame_T *child;
7011 list_T *fr_list;
7012 list_T *win_list;
7013
7014 if (fr == NULL)
7015 return;
7016
7017 if (outer)
7018 // outermost call from f_winlayout()
7019 fr_list = l;
7020 else
7021 {
7022 fr_list = list_alloc();
7023 if (fr_list == NULL)
7024 return;
7025 list_append_list(l, fr_list);
7026 }
7027
7028 if (fr->fr_layout == FR_LEAF)
7029 {
7030 if (fr->fr_win != NULL)
7031 {
7032 list_append_string(fr_list, (char_u *)"leaf", -1);
7033 list_append_number(fr_list, fr->fr_win->w_id);
7034 }
7035 }
7036 else
7037 {
7038 list_append_string(fr_list,
7039 fr->fr_layout == FR_ROW ? (char_u *)"row" : (char_u *)"col", -1);
7040
7041 win_list = list_alloc();
7042 if (win_list == NULL)
7043 return;
7044 list_append_list(fr_list, win_list);
7045 child = fr->fr_child;
7046 while (child != NULL)
7047 {
7048 get_framelayout(child, win_list, FALSE);
7049 child = child->fr_next;
7050 }
7051 }
7052}
Bram Moolenaar86edef62016-03-13 18:07:30 +01007053#endif