blob: fabd196efb17f5147cf70bcbfa8f106f45d97ba6 [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 Moolenaar5843f5f2019-08-20 20:13:45 +020042static int may_open_tabpage(void);
Bram Moolenaarc917da42016-07-19 22:31:36 +020043static 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 +010044static void win_free(win_T *wp, tabpage_T *tp);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020045static int win_unlisted(win_T *wp);
46static void win_append(win_T *after, win_T *wp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010047static void frame_append(frame_T *after, frame_T *frp);
48static void frame_insert(frame_T *before, frame_T *frp);
49static void frame_remove(frame_T *frp);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010050static void win_goto_ver(int up, long count);
51static void win_goto_hor(int left, long count);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010052static void frame_add_height(frame_T *frp, int n);
53static void last_status_rec(frame_T *fr, int statusline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010055static void make_snapshot_rec(frame_T *fr, frame_T **frp);
56static void clear_snapshot(tabpage_T *tp, int idx);
57static void clear_snapshot_rec(frame_T *fr);
58static int check_snapshot_rec(frame_T *sn, frame_T *fr);
59static win_T *restore_snapshot_rec(frame_T *sn, frame_T *fr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010061static int frame_check_height(frame_T *topfrp, int height);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010062static int frame_check_width(frame_T *topfrp, int width);
Bram Moolenaarb893ac22013-06-26 14:04:47 +020063
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +010064static win_T *win_alloc(win_T *after, int hidden);
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
Bram Moolenaarb6799ac2007-05-10 16:44:05 +000066#define NOWIN (win_T *)-1 /* non-existing window */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaar4033c552017-09-16 20:54:51 +020068#define ROWS_AVAIL (Rows - p_ch - tabline_height())
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000069
70static char *m_onlyone = N_("Already only one window");
71
Bram Moolenaar1417c762019-07-27 17:31:36 +020072// When non-zero splitting a window is forbidden. Used to avoid that nasty
73// autocommands mess up the window structure.
74static int split_disallowed = 0;
75
76// #define WIN_DEBUG
77#ifdef WIN_DEBUG
78/*
79 * Call this method to log the current window layout.
80 */
81 static void
82log_frame_layout(frame_T *frame)
83{
84 ch_log(NULL, "layout %s, wi: %d, he: %d, wwi: %d, whe: %d, id: %d",
85 frame->fr_layout == FR_LEAF ? "LEAF"
86 : frame->fr_layout == FR_ROW ? "ROW" : "COL",
87 frame->fr_width,
88 frame->fr_height,
89 frame->fr_win == NULL ? -1 : frame->fr_win->w_width,
90 frame->fr_win == NULL ? -1 : frame->fr_win->w_height,
91 frame->fr_win == NULL ? -1 : frame->fr_win->w_id);
92 if (frame->fr_child != NULL)
93 {
94 ch_log(NULL, "children");
95 log_frame_layout(frame->fr_child);
96 if (frame->fr_next != NULL)
97 ch_log(NULL, "END of children");
98 }
99 if (frame->fr_next != NULL)
100 log_frame_layout(frame->fr_next);
101}
102#endif
103
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104/*
Bram Moolenaar72e83c12019-02-22 16:09:52 +0100105 * All CTRL-W window commands are handled here, called from normal_cmd().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 */
107 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100108do_window(
109 int nchar,
110 long Prenum,
111 int xchar) /* extra char from ":wincmd gx" or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112{
113 long Prenum1;
114 win_T *wp;
115#if defined(FEAT_SEARCHPATH) || defined(FEAT_FIND_ID)
116 char_u *ptr;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000117 linenr_T lnum = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118#endif
119#ifdef FEAT_FIND_ID
120 int type = FIND_DEFINE;
121 int len;
122#endif
123 char_u cbuf[40];
124
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +0200125 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200126 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128#ifdef FEAT_CMDWIN
Bram Moolenaar6f470022018-04-10 18:47:20 +0200129# define CHECK_CMDWIN \
130 do { \
131 if (cmdwin_type != 0) \
132 { \
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100133 emsg(_(e_cmdwin)); \
Bram Moolenaar6f470022018-04-10 18:47:20 +0200134 return; \
135 } \
136 } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#else
Bram Moolenaar6f470022018-04-10 18:47:20 +0200138# define CHECK_CMDWIN do { /**/ } while (0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200141 Prenum1 = Prenum == 0 ? 1 : Prenum;
142
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 switch (nchar)
144 {
145/* split current window in two parts, horizontally */
146 case 'S':
147 case Ctrl_S:
148 case 's':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200149 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaarb1b715d2006-01-21 22:09:43 +0000151#ifdef FEAT_QUICKFIX
152 /* When splitting the quickfix window open a new buffer in it,
153 * don't replicate the quickfix buffer. */
154 if (bt_quickfix(curbuf))
155 goto newwindow;
156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#ifdef FEAT_GUI
158 need_mouse_correct = TRUE;
159#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200160 (void)win_split((int)Prenum, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 break;
162
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163/* split current window in two parts, vertically */
164 case Ctrl_V:
165 case 'v':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200166 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100168#ifdef FEAT_QUICKFIX
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000169 /* When splitting the quickfix window open a new buffer in it,
170 * don't replicate the quickfix buffer. */
171 if (bt_quickfix(curbuf))
172 goto newwindow;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100173#endif
174#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100176#endif
Bram Moolenaarcde88542015-08-11 19:14:00 +0200177 (void)win_split((int)Prenum, WSP_VERT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179
180/* split current window and edit alternate file */
181 case Ctrl_HAT:
182 case '^':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200183 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100185
186 if (buflist_findnr(Prenum == 0
187 ? curwin->w_alt_fnum : Prenum) == NULL)
188 {
189 if (Prenum == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100190 emsg(_(e_noalt));
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100191 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100192 semsg(_("E92: Buffer %ld not found"), Prenum);
Bram Moolenaar1bbb6192018-11-10 16:02:01 +0100193 break;
194 }
195
196 if (!curbuf_locked() && win_split(0, 0) == OK)
197 (void)buflist_getfile(
198 Prenum == 0 ? curwin->w_alt_fnum : Prenum,
199 (linenr_T)0, GETF_ALT, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 break;
201
202/* open new window */
203 case Ctrl_N:
204 case 'n':
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 Moolenaarb1b715d2006-01-21 22:09:43 +0000207#ifdef FEAT_QUICKFIX
208newwindow:
209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210 if (Prenum)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000211 /* window height */
212 vim_snprintf((char *)cbuf, sizeof(cbuf) - 5, "%ld", Prenum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 else
214 cbuf[0] = NUL;
Bram Moolenaar44a2f922016-03-19 22:11:51 +0100215#if defined(FEAT_QUICKFIX)
Bram Moolenaar990d95c2008-07-07 19:23:37 +0000216 if (nchar == 'v' || nchar == Ctrl_V)
217 STRCAT(cbuf, "v");
218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 STRCAT(cbuf, "new");
220 do_cmdline_cmd(cbuf);
221 break;
222
223/* quit current window */
224 case Ctrl_Q:
225 case 'q':
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("quit", 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/* close current window */
232 case Ctrl_C:
233 case 'c':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100235 cmd_with_count("close", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100236 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 break;
238
Bram Moolenaar4033c552017-09-16 20:54:51 +0200239#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240/* close preview window */
241 case Ctrl_Z:
242 case 'z':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200243 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 do_cmdline_cmd((char_u *)"pclose");
246 break;
247
248/* cursor to preview window */
249 case 'P':
Bram Moolenaar29323592016-07-24 22:04:11 +0200250 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 if (wp->w_p_pvw)
252 break;
253 if (wp == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100254 emsg(_("E441: There is no preview window"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255 else
256 win_goto(wp);
257 break;
258#endif
259
260/* close all but current window */
261 case Ctrl_O:
262 case 'o':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200263 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100265 cmd_with_count("only", cbuf, sizeof(cbuf), Prenum);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +0100266 do_cmdline_cmd(cbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267 break;
268
269/* cursor to next window with wrap around */
270 case Ctrl_W:
271 case 'w':
272/* cursor to previous window with wrap around */
273 case 'W':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200274 CHECK_CMDWIN;
Bram Moolenaara1f4cb92016-11-06 15:25:42 +0100275 if (ONE_WINDOW && Prenum != 1) /* just one window */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 beep_flush();
277 else
278 {
279 if (Prenum) /* go to specified window */
280 {
281 for (wp = firstwin; --Prenum > 0; )
282 {
283 if (wp->w_next == NULL)
284 break;
285 else
286 wp = wp->w_next;
287 }
288 }
289 else
290 {
291 if (nchar == 'W') /* go to previous window */
292 {
293 wp = curwin->w_prev;
294 if (wp == NULL)
295 wp = lastwin; /* wrap around */
296 }
297 else /* go to next window */
298 {
299 wp = curwin->w_next;
300 if (wp == NULL)
301 wp = firstwin; /* wrap around */
302 }
303 }
304 win_goto(wp);
305 }
306 break;
307
308/* cursor to window below */
309 case 'j':
310 case K_DOWN:
311 case Ctrl_J:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200312 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 win_goto_ver(FALSE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 break;
315
316/* cursor to window above */
317 case 'k':
318 case K_UP:
319 case Ctrl_K:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200320 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 win_goto_ver(TRUE, Prenum1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 break;
323
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324/* cursor to left window */
325 case 'h':
326 case K_LEFT:
327 case Ctrl_H:
328 case K_BS:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200329 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 win_goto_hor(TRUE, Prenum1);
331 break;
332
333/* cursor to right window */
334 case 'l':
335 case K_RIGHT:
336 case Ctrl_L:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200337 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 win_goto_hor(FALSE, Prenum1);
339 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000341/* move window to new tab page */
342 case 'T':
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000343 if (one_window())
Bram Moolenaar32526b32019-01-19 17:43:09 +0100344 msg(_(m_onlyone));
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000345 else
346 {
347 tabpage_T *oldtab = curtab;
348 tabpage_T *newtab;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000349
350 /* First create a new tab with the window, then go back to
351 * the old tab and close the window there. */
Bram Moolenaar89d40322006-08-29 15:30:07 +0000352 wp = curwin;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000353 if (win_new_tabpage((int)Prenum) == OK
354 && valid_tabpage(oldtab))
355 {
356 newtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200357 goto_tabpage_tp(oldtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000358 if (curwin == wp)
359 win_close(curwin, FALSE);
360 if (valid_tabpage(newtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +0200361 goto_tabpage_tp(newtab, TRUE, TRUE);
Bram Moolenaar4c3f5362006-04-11 21:38:50 +0000362 }
363 }
364 break;
365
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366/* cursor to top-left window */
367 case 't':
368 case Ctrl_T:
369 win_goto(firstwin);
370 break;
371
372/* cursor to bottom-right window */
373 case 'b':
374 case Ctrl_B:
375 win_goto(lastwin);
376 break;
377
378/* cursor to last accessed (previous) window */
379 case 'p':
380 case Ctrl_P:
Bram Moolenaar3dda7db2016-04-03 21:22:58 +0200381 if (!win_valid(prevwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 beep_flush();
383 else
384 win_goto(prevwin);
385 break;
386
387/* exchange current and next window */
388 case 'x':
389 case Ctrl_X:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200390 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 win_exchange(Prenum);
392 break;
393
394/* rotate windows downwards */
395 case Ctrl_R:
396 case 'r':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200397 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 win_rotate(FALSE, (int)Prenum1); /* downwards */
400 break;
401
402/* rotate windows upwards */
403 case 'R':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200404 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 win_rotate(TRUE, (int)Prenum1); /* upwards */
407 break;
408
409/* move window to the very top/bottom/left/right */
410 case 'K':
411 case 'J':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 case 'H':
413 case 'L':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200414 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 win_totop((int)Prenum,
416 ((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
417 | ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
418 break;
419
420/* make all windows the same height */
421 case '=':
422#ifdef FEAT_GUI
423 need_mouse_correct = TRUE;
424#endif
425 win_equal(NULL, FALSE, 'b');
426 break;
427
428/* increase current window height */
429 case '+':
430#ifdef FEAT_GUI
431 need_mouse_correct = TRUE;
432#endif
433 win_setheight(curwin->w_height + (int)Prenum1);
434 break;
435
436/* decrease current window height */
437 case '-':
438#ifdef FEAT_GUI
439 need_mouse_correct = TRUE;
440#endif
441 win_setheight(curwin->w_height - (int)Prenum1);
442 break;
443
444/* set current window height */
445 case Ctrl__:
446 case '_':
447#ifdef FEAT_GUI
448 need_mouse_correct = TRUE;
449#endif
450 win_setheight(Prenum ? (int)Prenum : 9999);
451 break;
452
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453/* increase current window width */
454 case '>':
455#ifdef FEAT_GUI
456 need_mouse_correct = TRUE;
457#endif
458 win_setwidth(curwin->w_width + (int)Prenum1);
459 break;
460
461/* decrease current window width */
462 case '<':
463#ifdef FEAT_GUI
464 need_mouse_correct = TRUE;
465#endif
466 win_setwidth(curwin->w_width - (int)Prenum1);
467 break;
468
469/* set current window width */
470 case '|':
471#ifdef FEAT_GUI
472 need_mouse_correct = TRUE;
473#endif
474 win_setwidth(Prenum != 0 ? (int)Prenum : 9999);
475 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476
477/* jump to tag and split window if tag exists (in preview window) */
478#if defined(FEAT_QUICKFIX)
479 case '}':
Bram Moolenaar6f470022018-04-10 18:47:20 +0200480 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 if (Prenum)
482 g_do_tagpreview = Prenum;
483 else
484 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200486 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 case ']':
488 case Ctrl_RSB:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200489 CHECK_CMDWIN;
Bram Moolenaard355c502014-09-23 13:48:43 +0200490 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 if (Prenum)
492 postponed_split = Prenum;
493 else
494 postponed_split = -1;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200495#ifdef FEAT_QUICKFIX
Bram Moolenaar56095e12014-10-09 10:44:37 +0200496 if (nchar != '}')
497 g_do_tagpreview = 0;
Bram Moolenaarda014b92014-09-24 13:26:44 +0200498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaard355c502014-09-23 13:48:43 +0200500 /* Execute the command right here, required when "wincmd ]"
501 * was used in a function. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 do_nv_ident(Ctrl_RSB, NUL);
503 break;
504
505#ifdef FEAT_SEARCHPATH
506/* edit file name under cursor in a new window */
507 case 'f':
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000508 case 'F':
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 case Ctrl_F:
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000510wingotofile:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200511 CHECK_CMDWIN;
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000512
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000513 ptr = grab_file_name(Prenum1, &lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (ptr != NULL)
515 {
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200516 tabpage_T *oldtab = curtab;
517 win_T *oldwin = curwin;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000518# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 need_mouse_correct = TRUE;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000520# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 setpcmark();
522 if (win_split(0, 0) == OK)
523 {
Bram Moolenaar3368ea22010-09-21 16:56:35 +0200524 RESET_BINDING(curwin);
Bram Moolenaar5d2ca042016-06-26 17:11:21 +0200525 if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL,
526 ECMD_HIDE, NULL) == FAIL)
527 {
528 /* Failed to open the file, close the window
529 * opened for it. */
530 win_close(curwin, FALSE);
531 goto_tabpage_win(oldtab, oldwin);
532 }
533 else if (nchar == 'F' && lnum >= 0)
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000534 {
535 curwin->w_cursor.lnum = lnum;
536 check_cursor_lnum();
537 beginline(BL_SOL | BL_FIX);
538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
540 vim_free(ptr);
541 }
542 break;
543#endif
544
545#ifdef FEAT_FIND_ID
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000546/* Go to the first occurrence of the identifier under cursor along path in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 * new window -- webb
548 */
549 case 'i': /* Go to any match */
550 case Ctrl_I:
551 type = FIND_ANY;
552 /* FALLTHROUGH */
553 case 'd': /* Go to definition, using 'define' */
554 case Ctrl_D:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200555 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0)
557 break;
558 find_pattern_in_path(ptr, 0, len, TRUE,
559 Prenum == 0 ? TRUE : FALSE, type,
560 Prenum1, ACTION_SPLIT, (linenr_T)1, (linenr_T)MAXLNUM);
561 curwin->w_set_curswant = TRUE;
562 break;
563#endif
564
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200565/* Quickfix window only: view the result under the cursor in a new split. */
566#if defined(FEAT_QUICKFIX)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000567 case K_KENTER:
568 case CAR:
Bram Moolenaar05159a02005-02-26 23:04:13 +0000569 if (bt_quickfix(curbuf))
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200570 qf_view_result(TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000571 break;
Bram Moolenaar0a08c632018-07-25 22:36:52 +0200572#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000573
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574/* CTRL-W g extended commands */
575 case 'g':
576 case Ctrl_G:
Bram Moolenaar6f470022018-04-10 18:47:20 +0200577 CHECK_CMDWIN;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578#ifdef USE_ON_FLY_SCROLL
579 dont_scroll = TRUE; /* disallow scrolling here */
580#endif
581 ++no_mapping;
582 ++allow_keys; /* no mapping for xchar, but allow key codes */
583 if (xchar == NUL)
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000584 xchar = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 LANGMAP_ADJUST(xchar, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 --no_mapping;
587 --allow_keys;
588#ifdef FEAT_CMDL_INFO
589 (void)add_to_showcmd(xchar);
590#endif
591 switch (xchar)
592 {
593#if defined(FEAT_QUICKFIX)
594 case '}':
595 xchar = Ctrl_RSB;
596 if (Prenum)
597 g_do_tagpreview = Prenum;
598 else
599 g_do_tagpreview = p_pvh;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600#endif
Bram Moolenaar2f40d122017-10-24 21:49:36 +0200601 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 case ']':
603 case Ctrl_RSB:
Bram Moolenaard355c502014-09-23 13:48:43 +0200604 /* keep Visual mode, can select words to use as a tag */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 if (Prenum)
606 postponed_split = Prenum;
607 else
608 postponed_split = -1;
609
610 /* Execute the command right here, required when
611 * "wincmd g}" was used in a function. */
612 do_nv_ident('g', xchar);
613 break;
614
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000615#ifdef FEAT_SEARCHPATH
616 case 'f': /* CTRL-W gf: "gf" in a new tab page */
Bram Moolenaar57657d82006-04-21 22:12:41 +0000617 case 'F': /* CTRL-W gF: "gF" in a new tab page */
Bram Moolenaar6d1dcff2010-01-27 20:26:46 +0100618 cmdmod.tab = tabpage_index(curtab) + 1;
Bram Moolenaar57657d82006-04-21 22:12:41 +0000619 nchar = xchar;
Bram Moolenaar8dff8182006-04-06 20:18:50 +0000620 goto wingotofile;
621#endif
Bram Moolenaar72e83c12019-02-22 16:09:52 +0100622 case 't': // CTRL-W gt: go to next tab page
623 goto_tabpage((int)Prenum);
624 break;
625
Bram Moolenaar882d02e2019-02-22 17:56:43 +0100626 case 'T': // CTRL-W gT: go to previous tab page
627 goto_tabpage(-(int)Prenum1);
628 break;
629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 default:
631 beep_flush();
632 break;
633 }
634 break;
635
636 default: beep_flush();
637 break;
638 }
639}
640
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100641/*
Bram Moolenaarb7316892019-05-01 18:08:42 +0200642 * Figure out the address type for ":wincmd".
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100643 */
644 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100645get_wincmd_addr_type(char_u *arg, exarg_T *eap)
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100646{
647 switch (*arg)
648 {
649 case 'S':
650 case Ctrl_S:
651 case 's':
652 case Ctrl_N:
653 case 'n':
654 case 'j':
655 case Ctrl_J:
656 case 'k':
657 case Ctrl_K:
658 case 'T':
659 case Ctrl_R:
660 case 'r':
661 case 'R':
662 case 'K':
663 case 'J':
664 case '+':
665 case '-':
666 case Ctrl__:
667 case '_':
668 case '|':
669 case ']':
670 case Ctrl_RSB:
671 case 'g':
672 case Ctrl_G:
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100673 case Ctrl_V:
674 case 'v':
675 case 'h':
676 case Ctrl_H:
677 case 'l':
678 case Ctrl_L:
679 case 'H':
680 case 'L':
681 case '>':
682 case '<':
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100683#if defined(FEAT_QUICKFIX)
684 case '}':
685#endif
686#ifdef FEAT_SEARCHPATH
687 case 'f':
688 case 'F':
689 case Ctrl_F:
690#endif
691#ifdef FEAT_FIND_ID
692 case 'i':
693 case Ctrl_I:
694 case 'd':
695 case Ctrl_D:
696#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +0200697 // window size or any count
698 eap->addr_type = ADDR_OTHER;
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100699 break;
700
701 case Ctrl_HAT:
702 case '^':
Bram Moolenaarb7316892019-05-01 18:08:42 +0200703 // buffer number
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100704 eap->addr_type = ADDR_BUFFERS;
705 break;
706
707 case Ctrl_Q:
708 case 'q':
709 case Ctrl_C:
710 case 'c':
711 case Ctrl_O:
712 case 'o':
713 case Ctrl_W:
714 case 'w':
715 case 'W':
716 case 'x':
717 case Ctrl_X:
Bram Moolenaarb7316892019-05-01 18:08:42 +0200718 // window number
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100719 eap->addr_type = ADDR_WINDOWS;
720 break;
721
722#if defined(FEAT_QUICKFIX)
723 case Ctrl_Z:
724 case 'z':
725 case 'P':
726#endif
727 case 't':
728 case Ctrl_T:
729 case 'b':
730 case Ctrl_B:
731 case 'p':
732 case Ctrl_P:
733 case '=':
734 case CAR:
Bram Moolenaarb7316892019-05-01 18:08:42 +0200735 // no count
736 eap->addr_type = ADDR_NONE;
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +0100737 break;
738 }
739}
740
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100741 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100742cmd_with_count(
743 char *cmd,
744 char_u *bufp,
745 size_t bufsize,
746 long Prenum)
Bram Moolenaar2f1e51a2014-12-13 03:58:09 +0100747{
748 size_t len = STRLEN(cmd);
749
750 STRCPY(bufp, cmd);
751 if (Prenum > 0)
752 vim_snprintf((char *)bufp + len, bufsize - len, "%ld", Prenum);
753}
754
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755/*
Bram Moolenaar1417c762019-07-27 17:31:36 +0200756 * If "split_disallowed" is set given an error and return FAIL.
757 * Otherwise return OK.
758 */
759 static int
760check_split_disallowed()
761{
762 if (split_disallowed > 0)
763 {
764 emsg(_("E242: Can't split a window while closing another"));
765 return FAIL;
766 }
767 return OK;
768}
769
770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 * split the current window, implements CTRL-W s and :split
772 *
773 * "size" is the height or width for the new window, 0 to use half of current
774 * height or width.
775 *
776 * "flags":
777 * WSP_ROOM: require enough room for new window
778 * WSP_VERT: vertical split.
779 * WSP_TOP: open window at the top-left of the shell (help window).
780 * WSP_BOT: open window at the bottom-right of the shell (quickfix window).
781 * WSP_HELP: creating the help window, keep layout snapshot
782 *
783 * return FAIL for failure, OK otherwise
784 */
785 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100786win_split(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +0200788 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +0200789 return FAIL;
790
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000791 /* When the ":tab" modifier was used open a new tab page instead. */
792 if (may_open_tabpage() == OK)
793 return OK;
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /* Add flags from ":vertical", ":topleft" and ":botright". */
796 flags |= cmdmod.split;
797 if ((flags & WSP_TOP) && (flags & WSP_BOT))
798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100799 emsg(_("E442: Can't split topleft and botright at the same time"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 return FAIL;
801 }
Bram Moolenaar1417c762019-07-27 17:31:36 +0200802 if (check_split_disallowed() == FAIL)
803 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804
805 /* When creating the help window make a snapshot of the window layout.
806 * Otherwise clear the snapshot, it's now invalid. */
807 if (flags & WSP_HELP)
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000808 make_snapshot(SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000810 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
812 return win_split_ins(size, flags, NULL, 0);
813}
814
815/*
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100816 * When "new_wp" is NULL: split the current window in two.
817 * When "new_wp" is not NULL: insert this window at the far
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 * top/left/right/bottom.
819 * return FAIL for failure, OK otherwise
820 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +0000821 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +0100822win_split_ins(
823 int size,
824 int flags,
825 win_T *new_wp,
826 int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100828 win_T *wp = new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 win_T *oldwin;
830 int new_size = size;
831 int i;
832 int need_status = 0;
833 int do_equal = FALSE;
834 int needed;
835 int available;
836 int oldwin_height = 0;
837 int layout;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200838 frame_T *frp, *curfrp, *frp2, *prevfrp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 int before;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200840 int minheight;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200841 int wmh1;
Bram Moolenaar98da6ec2018-04-13 22:15:46 +0200842 int did_set_fraction = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
844 if (flags & WSP_TOP)
845 oldwin = firstwin;
846 else if (flags & WSP_BOT)
847 oldwin = lastwin;
848 else
849 oldwin = curwin;
850
851 /* add a status line when p_ls == 1 and splitting the first window */
Bram Moolenaar459ca562016-11-10 18:16:33 +0100852 if (ONE_WINDOW && p_ls == 1 && oldwin->w_status_height == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar415a6932017-12-05 20:31:07 +0100854 if (VISIBLE_HEIGHT(oldwin) <= p_wmh && 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 need_status = STATUS_HEIGHT;
860 }
861
Bram Moolenaaree79cbc2007-05-02 19:50:14 +0000862#ifdef FEAT_GUI
863 /* May be needed for the scrollbars that are going to change. */
864 if (gui.in_use)
865 out_flush();
866#endif
867
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (flags & WSP_VERT)
869 {
Bram Moolenaara0485492014-07-16 23:39:54 +0200870 int wmw1;
871 int minwidth;
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 layout = FR_ROW;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874
875 /*
876 * Check if we are able to split the current window and compute its
877 * width.
878 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200879 /* Current window requires at least 1 space. */
880 wmw1 = (p_wmw == 0 ? 1 : p_wmw);
881 needed = wmw1 + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200883 needed += p_wiw - wmw1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200884 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200886 minwidth = frame_minwidth(topframe, NOWIN);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 available = topframe->fr_width;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200888 needed += minwidth;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200890 else if (p_ea)
891 {
892 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
893 prevfrp = oldwin->w_frame;
894 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
895 frp = frp->fr_parent)
896 {
897 if (frp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +0100898 FOR_ALL_FRAMES(frp2, frp->fr_child)
Bram Moolenaar54368f22014-07-23 15:21:20 +0200899 if (frp2 != prevfrp)
900 minwidth += frame_minwidth(frp2, NOWIN);
901 prevfrp = frp;
902 }
903 available = topframe->fr_width;
904 needed += minwidth;
905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 else
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200907 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200908 minwidth = frame_minwidth(oldwin->w_frame, NOWIN);
909 available = oldwin->w_frame->fr_width;
910 needed += minwidth;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200911 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100912 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 return FAIL;
916 }
917 if (new_size == 0)
918 new_size = oldwin->w_width / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200919 if (new_size > available - minwidth - 1)
920 new_size = available - minwidth - 1;
Bram Moolenaar1f538352014-07-16 18:19:27 +0200921 if (new_size < wmw1)
922 new_size = wmw1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923
924 /* if it doesn't fit in the current window, need win_equal() */
925 if (oldwin->w_width - new_size - 1 < p_wmw)
926 do_equal = TRUE;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000927
928 /* We don't like to take lines for the new window from a
929 * 'winfixwidth' window. Take them from a window to the left or right
Bram Moolenaar38e34832017-03-19 20:22:36 +0100930 * instead, if possible. Add one for the separator. */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +0000931 if (oldwin->w_p_wfw)
Bram Moolenaar38e34832017-03-19 20:22:36 +0100932 win_setwidth_win(oldwin->w_width + new_size + 1, oldwin);
Bram Moolenaar67f71312007-08-12 14:55:56 +0000933
934 /* Only make all windows the same width if one of them (except oldwin)
935 * is wider than one of the split windows. */
936 if (!do_equal && p_ea && size == 0 && *p_ead != 'v'
Bram Moolenaar1417c762019-07-27 17:31:36 +0200937 && oldwin->w_frame->fr_parent != NULL)
Bram Moolenaar67f71312007-08-12 14:55:56 +0000938 {
939 frp = oldwin->w_frame->fr_parent->fr_child;
940 while (frp != NULL)
941 {
942 if (frp->fr_win != oldwin && frp->fr_win != NULL
943 && (frp->fr_win->w_width > new_size
944 || frp->fr_win->w_width > oldwin->w_width
Bram Moolenaardf46f6f2014-11-19 13:40:08 +0100945 - new_size - 1))
Bram Moolenaar67f71312007-08-12 14:55:56 +0000946 {
947 do_equal = TRUE;
948 break;
949 }
950 frp = frp->fr_next;
951 }
952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
954 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 {
956 layout = FR_COL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
958 /*
959 * Check if we are able to split the current window and compute its
960 * height.
961 */
Bram Moolenaar1f538352014-07-16 18:19:27 +0200962 /* Current window requires at least 1 space. */
Bram Moolenaar415a6932017-12-05 20:31:07 +0100963 wmh1 = (p_wmh == 0 ? 1 : p_wmh) + WINBAR_HEIGHT(curwin);
Bram Moolenaar1f538352014-07-16 18:19:27 +0200964 needed = wmh1 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (flags & WSP_ROOM)
Bram Moolenaar1f538352014-07-16 18:19:27 +0200966 needed += p_wh - wmh1;
Bram Moolenaar54368f22014-07-23 15:21:20 +0200967 if (flags & (WSP_BOT | WSP_TOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200969 minheight = frame_minheight(topframe, NOWIN) + need_status;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 available = topframe->fr_height;
Bram Moolenaarb4d21352014-07-16 14:16:46 +0200971 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 }
Bram Moolenaar54368f22014-07-23 15:21:20 +0200973 else if (p_ea)
974 {
975 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
976 prevfrp = oldwin->w_frame;
977 for (frp = oldwin->w_frame->fr_parent; frp != NULL;
978 frp = frp->fr_parent)
979 {
980 if (frp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +0100981 FOR_ALL_FRAMES(frp2, frp->fr_child)
Bram Moolenaar54368f22014-07-23 15:21:20 +0200982 if (frp2 != prevfrp)
983 minheight += frame_minheight(frp2, NOWIN);
984 prevfrp = frp;
985 }
986 available = topframe->fr_height;
987 needed += minheight;
988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 else
990 {
Bram Moolenaar1f538352014-07-16 18:19:27 +0200991 minheight = frame_minheight(oldwin->w_frame, NOWIN) + need_status;
992 available = oldwin->w_frame->fr_height;
993 needed += minheight;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
Bram Moolenaar70b2a562012-01-10 22:26:17 +0100995 if (available < needed && new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100997 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 return FAIL;
999 }
1000 oldwin_height = oldwin->w_height;
1001 if (need_status)
1002 {
1003 oldwin->w_status_height = STATUS_HEIGHT;
1004 oldwin_height -= STATUS_HEIGHT;
1005 }
1006 if (new_size == 0)
1007 new_size = oldwin_height / 2;
Bram Moolenaarb4d21352014-07-16 14:16:46 +02001008 if (new_size > available - minheight - STATUS_HEIGHT)
1009 new_size = available - minheight - STATUS_HEIGHT;
Bram Moolenaar1f538352014-07-16 18:19:27 +02001010 if (new_size < wmh1)
1011 new_size = wmh1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 /* if it doesn't fit in the current window, need win_equal() */
1014 if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh)
1015 do_equal = TRUE;
1016
1017 /* We don't like to take lines for the new window from a
1018 * 'winfixheight' window. Take them from a window above or below
1019 * instead, if possible. */
1020 if (oldwin->w_p_wfh)
1021 {
Bram Moolenaar98da6ec2018-04-13 22:15:46 +02001022 /* Set w_fraction now so that the cursor keeps the same relative
1023 * vertical position using the old height. */
1024 set_fraction(oldwin);
1025 did_set_fraction = TRUE;
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 win_setheight_win(oldwin->w_height + new_size + STATUS_HEIGHT,
1028 oldwin);
1029 oldwin_height = oldwin->w_height;
1030 if (need_status)
1031 oldwin_height -= STATUS_HEIGHT;
1032 }
Bram Moolenaar67f71312007-08-12 14:55:56 +00001033
1034 /* Only make all windows the same height if one of them (except oldwin)
1035 * is higher than one of the split windows. */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001036 if (!do_equal && p_ea && size == 0 && *p_ead != 'h'
Bram Moolenaar67f71312007-08-12 14:55:56 +00001037 && oldwin->w_frame->fr_parent != NULL)
1038 {
1039 frp = oldwin->w_frame->fr_parent->fr_child;
1040 while (frp != NULL)
1041 {
1042 if (frp->fr_win != oldwin && frp->fr_win != NULL
1043 && (frp->fr_win->w_height > new_size
1044 || frp->fr_win->w_height > oldwin_height - new_size
1045 - STATUS_HEIGHT))
1046 {
1047 do_equal = TRUE;
1048 break;
1049 }
1050 frp = frp->fr_next;
1051 }
1052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
1054
1055 /*
1056 * allocate new window structure and link it in the window list
1057 */
1058 if ((flags & WSP_TOP) == 0
1059 && ((flags & WSP_BOT)
1060 || (flags & WSP_BELOW)
1061 || (!(flags & WSP_ABOVE)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001062 && ( (flags & WSP_VERT) ? p_spr : p_sb))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
1064 /* new window below/right of current one */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001065 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001066 wp = win_alloc(oldwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 else
1068 win_append(oldwin, wp);
1069 }
1070 else
1071 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001072 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001073 wp = win_alloc(oldwin->w_prev, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 else
1075 win_append(oldwin->w_prev, wp);
1076 }
1077
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001078 if (new_wp == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
1080 if (wp == NULL)
1081 return FAIL;
1082
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001083 new_frame(wp);
1084 if (wp->w_frame == NULL)
1085 {
1086 win_free(wp, NULL);
1087 return FAIL;
1088 }
1089
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001090 /* make the contents of the new window the same as the current one */
Bram Moolenaar884ae642009-02-22 01:37:59 +00001091 win_init(wp, curwin, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
1093
1094 /*
1095 * Reorganise the tree of frames to insert the new window.
1096 */
1097 if (flags & (WSP_TOP | WSP_BOT))
1098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if ((topframe->fr_layout == FR_COL && (flags & WSP_VERT) == 0)
1100 || (topframe->fr_layout == FR_ROW && (flags & WSP_VERT) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
1102 curfrp = topframe->fr_child;
1103 if (flags & WSP_BOT)
1104 while (curfrp->fr_next != NULL)
1105 curfrp = curfrp->fr_next;
1106 }
1107 else
1108 curfrp = topframe;
1109 before = (flags & WSP_TOP);
1110 }
1111 else
1112 {
1113 curfrp = oldwin->w_frame;
1114 if (flags & WSP_BELOW)
1115 before = FALSE;
1116 else if (flags & WSP_ABOVE)
1117 before = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001118 else if (flags & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 before = !p_spr;
1120 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 before = !p_sb;
1122 }
1123 if (curfrp->fr_parent == NULL || curfrp->fr_parent->fr_layout != layout)
1124 {
1125 /* Need to create a new frame in the tree to make a branch. */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001126 frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 *frp = *curfrp;
1128 curfrp->fr_layout = layout;
1129 frp->fr_parent = curfrp;
1130 frp->fr_next = NULL;
1131 frp->fr_prev = NULL;
1132 curfrp->fr_child = frp;
1133 curfrp->fr_win = NULL;
1134 curfrp = frp;
1135 if (frp->fr_win != NULL)
1136 oldwin->w_frame = frp;
1137 else
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001138 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 frp->fr_parent = curfrp;
1140 }
1141
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001142 if (new_wp == NULL)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001143 frp = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +01001145 frp = new_wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 frp->fr_parent = curfrp->fr_parent;
1147
1148 /* Insert the new frame at the right place in the frame list. */
1149 if (before)
1150 frame_insert(curfrp, frp);
1151 else
1152 frame_append(curfrp, frp);
1153
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001154 /* Set w_fraction now so that the cursor keeps the same relative
1155 * vertical position. */
Bram Moolenaar98da6ec2018-04-13 22:15:46 +02001156 if (!did_set_fraction)
1157 set_fraction(oldwin);
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001158 wp->w_fraction = oldwin->w_fraction;
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 if (flags & WSP_VERT)
1161 {
1162 wp->w_p_scr = curwin->w_p_scr;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01001163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 if (need_status)
1165 {
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001166 win_new_height(oldwin, oldwin->w_height - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 oldwin->w_status_height = need_status;
1168 }
1169 if (flags & (WSP_TOP | WSP_BOT))
1170 {
1171 /* set height and row of new window to full height */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001172 wp->w_winrow = tabline_height();
Bram Moolenaard326ad62017-09-18 20:31:41 +02001173 win_new_height(wp, curfrp->fr_height - (p_ls > 0)
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001174 - WINBAR_HEIGHT(wp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 wp->w_status_height = (p_ls > 0);
1176 }
1177 else
1178 {
1179 /* height and row of new window is same as current window */
1180 wp->w_winrow = oldwin->w_winrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01001181 win_new_height(wp, VISIBLE_HEIGHT(oldwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 wp->w_status_height = oldwin->w_status_height;
1183 }
1184 frp->fr_height = curfrp->fr_height;
1185
1186 /* "new_size" of the current window goes to the new window, use
1187 * one column for the vertical separator */
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001188 win_new_width(wp, new_size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 if (before)
1190 wp->w_vsep_width = 1;
1191 else
1192 {
1193 wp->w_vsep_width = oldwin->w_vsep_width;
1194 oldwin->w_vsep_width = 1;
1195 }
1196 if (flags & (WSP_TOP | WSP_BOT))
1197 {
1198 if (flags & WSP_BOT)
1199 frame_add_vsep(curfrp);
1200 /* Set width of neighbor frame */
1201 frame_new_width(curfrp, curfrp->fr_width
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001202 - (new_size + ((flags & WSP_TOP) != 0)), flags & WSP_TOP,
1203 FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001206 win_new_width(oldwin, oldwin->w_width - (new_size + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 if (before) /* new window left of current one */
1208 {
1209 wp->w_wincol = oldwin->w_wincol;
1210 oldwin->w_wincol += new_size + 1;
1211 }
1212 else /* new window right of current one */
1213 wp->w_wincol = oldwin->w_wincol + oldwin->w_width + 1;
1214 frame_fix_width(oldwin);
1215 frame_fix_width(wp);
1216 }
1217 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
1219 /* width and column of new window is same as current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 if (flags & (WSP_TOP | WSP_BOT))
1221 {
1222 wp->w_wincol = 0;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001223 win_new_width(wp, Columns);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 wp->w_vsep_width = 0;
1225 }
1226 else
1227 {
1228 wp->w_wincol = oldwin->w_wincol;
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001229 win_new_width(wp, oldwin->w_width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 wp->w_vsep_width = oldwin->w_vsep_width;
1231 }
1232 frp->fr_width = curfrp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233
1234 /* "new_size" of the current window goes to the new window, use
1235 * one row for the status line */
1236 win_new_height(wp, new_size);
1237 if (flags & (WSP_TOP | WSP_BOT))
Bram Moolenaar991dea32016-05-24 11:31:32 +02001238 {
Bram Moolenaard326ad62017-09-18 20:31:41 +02001239 int new_fr_height = curfrp->fr_height - new_size
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01001240 + WINBAR_HEIGHT(wp) ;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001241
1242 if (!((flags & WSP_BOT) && p_ls == 0))
1243 new_fr_height -= STATUS_HEIGHT;
1244 frame_new_height(curfrp, new_fr_height, flags & WSP_TOP, FALSE);
1245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 else
1247 win_new_height(oldwin, oldwin_height - (new_size + STATUS_HEIGHT));
1248 if (before) /* new window above current one */
1249 {
1250 wp->w_winrow = oldwin->w_winrow;
1251 wp->w_status_height = STATUS_HEIGHT;
1252 oldwin->w_winrow += wp->w_height + STATUS_HEIGHT;
1253 }
1254 else /* new window below current one */
1255 {
Bram Moolenaar415a6932017-12-05 20:31:07 +01001256 wp->w_winrow = oldwin->w_winrow + VISIBLE_HEIGHT(oldwin)
1257 + STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 wp->w_status_height = oldwin->w_status_height;
Bram Moolenaar991dea32016-05-24 11:31:32 +02001259 if (!(flags & WSP_BOT))
1260 oldwin->w_status_height = STATUS_HEIGHT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (flags & WSP_BOT)
1263 frame_add_statusline(curfrp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 frame_fix_height(wp);
1265 frame_fix_height(oldwin);
1266 }
1267
1268 if (flags & (WSP_TOP | WSP_BOT))
1269 (void)win_comp_pos();
1270
1271 /*
1272 * Both windows need redrawing
1273 */
1274 redraw_win_later(wp, NOT_VALID);
1275 wp->w_redr_status = TRUE;
1276 redraw_win_later(oldwin, NOT_VALID);
1277 oldwin->w_redr_status = TRUE;
1278
1279 if (need_status)
1280 {
1281 msg_row = Rows - 1;
1282 msg_col = sc_col;
1283 msg_clr_eos_force(); /* Old command/ruler may still be there */
1284 comp_col();
1285 msg_row = Rows - 1;
1286 msg_col = 0; /* put position back at start of line */
1287 }
1288
1289 /*
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001290 * equalize the window sizes.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 */
1292 if (do_equal || dir != 0)
1293 win_equal(wp, TRUE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h')
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001295 : dir == 'h' ? 'b' : 'v');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297 /* Don't change the window height/width to 'winheight' / 'winwidth' if a
1298 * size was given. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (flags & WSP_VERT)
1300 {
1301 i = p_wiw;
1302 if (size != 0)
1303 p_wiw = size;
1304
1305# ifdef FEAT_GUI
1306 /* When 'guioptions' includes 'L' or 'R' may have to add scrollbars. */
1307 if (gui.in_use)
1308 gui_init_which_components(NULL);
1309# endif
1310 }
1311 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 i = p_wh;
1314 if (size != 0)
1315 p_wh = size;
1316 }
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001317
Bram Moolenaar23fb7a92014-07-30 14:05:00 +02001318#ifdef FEAT_JUMPLIST
1319 /* Keep same changelist position in new window. */
1320 wp->w_changelistidx = oldwin->w_changelistidx;
1321#endif
1322
Bram Moolenaar9b73a782010-03-17 16:54:57 +01001323 /*
1324 * make the new window the current window
1325 */
Bram Moolenaarc917da42016-07-19 22:31:36 +02001326 win_enter_ext(wp, FALSE, FALSE, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (flags & WSP_VERT)
1328 p_wiw = i;
1329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 p_wh = i;
1331
1332 return OK;
1333}
1334
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001335
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001336/*
1337 * Initialize window "newp" from window "oldp".
1338 * Used when splitting a window and when creating a new tab page.
1339 * The windows will both edit the same buffer.
Bram Moolenaar884ae642009-02-22 01:37:59 +00001340 * WSP_NEWLOC may be specified in flags to prevent the location list from
1341 * being copied.
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001342 */
1343 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001344win_init(win_T *newp, win_T *oldp, int flags UNUSED)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001345{
1346 int i;
1347
1348 newp->w_buffer = oldp->w_buffer;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001349#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001350 newp->w_s = &(oldp->w_buffer->b_s);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001351#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001352 oldp->w_buffer->b_nwindows++;
1353 newp->w_cursor = oldp->w_cursor;
1354 newp->w_valid = 0;
1355 newp->w_curswant = oldp->w_curswant;
1356 newp->w_set_curswant = oldp->w_set_curswant;
1357 newp->w_topline = oldp->w_topline;
1358#ifdef FEAT_DIFF
1359 newp->w_topfill = oldp->w_topfill;
1360#endif
1361 newp->w_leftcol = oldp->w_leftcol;
1362 newp->w_pcmark = oldp->w_pcmark;
1363 newp->w_prev_pcmark = oldp->w_prev_pcmark;
1364 newp->w_alt_fnum = oldp->w_alt_fnum;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00001365 newp->w_wrow = oldp->w_wrow;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001366 newp->w_fraction = oldp->w_fraction;
1367 newp->w_prev_fraction_row = oldp->w_prev_fraction_row;
1368#ifdef FEAT_JUMPLIST
1369 copy_jumplist(oldp, newp);
1370#endif
1371#ifdef FEAT_QUICKFIX
Bram Moolenaar884ae642009-02-22 01:37:59 +00001372 if (flags & WSP_NEWLOC)
1373 {
1374 /* Don't copy the location list. */
1375 newp->w_llist = NULL;
1376 newp->w_llist_ref = NULL;
1377 }
1378 else
Bram Moolenaar09037502018-09-25 22:08:14 +02001379 copy_loclist_stack(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001380#endif
Bram Moolenaarbd2dc342014-01-10 15:53:13 +01001381 newp->w_localdir = (oldp->w_localdir == NULL)
1382 ? NULL : vim_strsave(oldp->w_localdir);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001383
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001384 /* copy tagstack and folds */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001385 for (i = 0; i < oldp->w_tagstacklen; i++)
1386 {
Bram Moolenaar45e18cb2019-04-28 18:05:35 +02001387 taggy_T *tag = &newp->w_tagstack[i];
1388 *tag = oldp->w_tagstack[i];
1389 if (tag->tagname != NULL)
1390 tag->tagname = vim_strsave(tag->tagname);
1391 if (tag->user_data != NULL)
1392 tag->user_data = vim_strsave(tag->user_data);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001393 }
1394 newp->w_tagstackidx = oldp->w_tagstackidx;
1395 newp->w_tagstacklen = oldp->w_tagstacklen;
Bram Moolenaara971b822011-09-14 14:43:25 +02001396#ifdef FEAT_FOLDING
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001397 copyFoldingState(oldp, newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001398#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001399
1400 win_init_some(newp, oldp);
Bram Moolenaar1a384422010-07-14 19:53:30 +02001401
Bram Moolenaara971b822011-09-14 14:43:25 +02001402#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02001403 check_colorcolumn(newp);
Bram Moolenaara971b822011-09-14 14:43:25 +02001404#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001405}
1406
1407/*
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02001408 * Initialize window "newp" from window "old".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001409 * Only the essential things are copied.
1410 */
1411 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001412win_init_some(win_T *newp, win_T *oldp)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001413{
1414 /* Use the same argument list. */
1415 newp->w_alist = oldp->w_alist;
1416 ++newp->w_alist->al_refcount;
1417 newp->w_arg_idx = oldp->w_arg_idx;
1418
1419 /* copy options from existing window */
1420 win_copy_options(oldp, newp);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00001421}
1422
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001423/*
1424 * Return TRUE if "win" is a global popup or a popup in the current tab page.
1425 */
Bram Moolenaarb53fb312019-06-13 23:59:52 +02001426 int
Bram Moolenaar682725c2019-05-25 20:10:37 +02001427win_valid_popup(win_T *win UNUSED)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001428{
1429#ifdef FEAT_TEXT_PROP
1430 win_T *wp;
1431
1432 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
1433 if (wp == win)
1434 return TRUE;
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02001435 for (wp = curtab->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001436 if (wp == win)
1437 return TRUE;
1438#endif
1439 return FALSE;
1440}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001443 * Check if "win" is a pointer to an existing window in the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 */
1445 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001446win_valid(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447{
1448 win_T *wp;
1449
1450 if (win == NULL)
1451 return FALSE;
Bram Moolenaar29323592016-07-24 22:04:11 +02001452 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 if (wp == win)
1454 return TRUE;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001455 return win_valid_popup(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456}
1457
1458/*
Bram Moolenaare59215c2016-08-14 19:08:45 +02001459 * Check if "win" is a pointer to an existing window in any tab page.
1460 */
1461 int
1462win_valid_any_tab(win_T *win)
1463{
1464 win_T *wp;
1465 tabpage_T *tp;
1466
1467 if (win == NULL)
1468 return FALSE;
1469 FOR_ALL_TABPAGES(tp)
1470 {
1471 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
1472 {
1473 if (wp == win)
1474 return TRUE;
1475 }
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001476#ifdef FEAT_TEXT_PROP
1477 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
1478 if (wp == win)
1479 return TRUE;
1480#endif
Bram Moolenaare59215c2016-08-14 19:08:45 +02001481 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001482 return win_valid_popup(win);
Bram Moolenaare59215c2016-08-14 19:08:45 +02001483}
1484
1485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 * Return the number of windows.
1487 */
1488 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001489win_count(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 win_T *wp;
1492 int count = 0;
1493
Bram Moolenaar29323592016-07-24 22:04:11 +02001494 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 ++count;
1496 return count;
1497}
1498
1499/*
1500 * Make "count" windows on the screen.
1501 * Return actual number of windows on the screen.
1502 * Must be called when there is just one window, filling the whole screen
1503 * (excluding the command line).
1504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001506make_windows(
1507 int count,
1508 int vertical UNUSED) /* split windows vertically if TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509{
1510 int maxcount;
1511 int todo;
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 if (vertical)
1514 {
1515 /* Each windows needs at least 'winminwidth' lines and a separator
1516 * column. */
1517 maxcount = (curwin->w_width + curwin->w_vsep_width
1518 - (p_wiw - p_wmw)) / (p_wmw + 1);
1519 }
1520 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 {
1522 /* Each window needs at least 'winminheight' lines and a status line. */
Bram Moolenaar415a6932017-12-05 20:31:07 +01001523 maxcount = (VISIBLE_HEIGHT(curwin) + curwin->w_status_height
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 - (p_wh - p_wmh)) / (p_wmh + STATUS_HEIGHT);
1525 }
1526
1527 if (maxcount < 2)
1528 maxcount = 2;
1529 if (count > maxcount)
1530 count = maxcount;
1531
1532 /*
1533 * add status line now, otherwise first window will be too big
1534 */
1535 if (count > 1)
1536 last_status(TRUE);
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 /*
1539 * Don't execute autocommands while creating the windows. Must do that
1540 * when putting the buffers in the windows.
1541 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001542 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543
1544 /* todo is number of windows left to create */
1545 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 if (vertical)
1547 {
1548 if (win_split(curwin->w_width - (curwin->w_width - todo)
1549 / (todo + 1) - 1, WSP_VERT | WSP_ABOVE) == FAIL)
1550 break;
1551 }
1552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
1554 if (win_split(curwin->w_height - (curwin->w_height - todo
1555 * STATUS_HEIGHT) / (todo + 1)
1556 - STATUS_HEIGHT, WSP_ABOVE) == FAIL)
1557 break;
1558 }
1559
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001560 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561
1562 /* return actual number of windows */
1563 return (count - todo);
1564}
1565
1566/*
1567 * Exchange current and next window
1568 */
1569 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001570win_exchange(long Prenum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
1572 frame_T *frp;
1573 frame_T *frp2;
1574 win_T *wp;
1575 win_T *wp2;
1576 int temp;
1577
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02001578 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02001579 return;
1580 if (ONE_WINDOW) // just one window
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 {
1582 beep_flush();
1583 return;
1584 }
1585
1586#ifdef FEAT_GUI
1587 need_mouse_correct = TRUE;
1588#endif
1589
1590 /*
1591 * find window to exchange with
1592 */
1593 if (Prenum)
1594 {
1595 frp = curwin->w_frame->fr_parent->fr_child;
1596 while (frp != NULL && --Prenum > 0)
1597 frp = frp->fr_next;
1598 }
1599 else if (curwin->w_frame->fr_next != NULL) /* Swap with next */
1600 frp = curwin->w_frame->fr_next;
1601 else /* Swap last window in row/col with previous */
1602 frp = curwin->w_frame->fr_prev;
1603
1604 /* We can only exchange a window with another window, not with a frame
1605 * containing windows. */
1606 if (frp == NULL || frp->fr_win == NULL || frp->fr_win == curwin)
1607 return;
1608 wp = frp->fr_win;
1609
1610/*
1611 * 1. remove curwin from the list. Remember after which window it was in wp2
1612 * 2. insert curwin before wp in the list
1613 * if wp != wp2
1614 * 3. remove wp from the list
1615 * 4. insert wp after wp2
1616 * 5. exchange the status line height and vsep width.
1617 */
1618 wp2 = curwin->w_prev;
1619 frp2 = curwin->w_frame->fr_prev;
1620 if (wp->w_prev != curwin)
1621 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001622 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 frame_remove(curwin->w_frame);
1624 win_append(wp->w_prev, curwin);
1625 frame_insert(frp, curwin->w_frame);
1626 }
1627 if (wp != wp2)
1628 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001629 win_remove(wp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 frame_remove(wp->w_frame);
1631 win_append(wp2, wp);
1632 if (frp2 == NULL)
1633 frame_insert(wp->w_frame->fr_parent->fr_child, wp->w_frame);
1634 else
1635 frame_append(frp2, wp->w_frame);
1636 }
1637 temp = curwin->w_status_height;
1638 curwin->w_status_height = wp->w_status_height;
1639 wp->w_status_height = temp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 temp = curwin->w_vsep_width;
1641 curwin->w_vsep_width = wp->w_vsep_width;
1642 wp->w_vsep_width = temp;
1643
1644 /* If the windows are not in the same frame, exchange the sizes to avoid
1645 * messing up the window layout. Otherwise fix the frame sizes. */
1646 if (curwin->w_frame->fr_parent != wp->w_frame->fr_parent)
1647 {
1648 temp = curwin->w_height;
1649 curwin->w_height = wp->w_height;
1650 wp->w_height = temp;
1651 temp = curwin->w_width;
1652 curwin->w_width = wp->w_width;
1653 wp->w_width = temp;
1654 }
1655 else
1656 {
1657 frame_fix_height(curwin);
1658 frame_fix_height(wp);
1659 frame_fix_width(curwin);
1660 frame_fix_width(wp);
1661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 (void)win_comp_pos(); /* recompute window positions */
1664
1665 win_enter(wp, TRUE);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001666 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
1669/*
1670 * rotate windows: if upwards TRUE the second window becomes the first one
1671 * if upwards FALSE the first window becomes the second one
1672 */
1673 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001674win_rotate(int upwards, int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675{
1676 win_T *wp1;
1677 win_T *wp2;
1678 frame_T *frp;
1679 int n;
1680
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01001681 if (ONE_WINDOW) /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 {
1683 beep_flush();
1684 return;
1685 }
1686
1687#ifdef FEAT_GUI
1688 need_mouse_correct = TRUE;
1689#endif
1690
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 /* Check if all frames in this row/col have one window. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001692 FOR_ALL_FRAMES(frp, curwin->w_frame->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (frp->fr_win == NULL)
1694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001695 emsg(_("E443: Cannot rotate when another window is split"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 return;
1697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699 while (count--)
1700 {
1701 if (upwards) /* first window becomes last window */
1702 {
1703 /* remove first window/frame from the list */
1704 frp = curwin->w_frame->fr_parent->fr_child;
1705 wp1 = frp->fr_win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001706 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 frame_remove(frp);
1708
1709 /* find last frame and append removed window/frame after it */
1710 for ( ; frp->fr_next != NULL; frp = frp->fr_next)
1711 ;
1712 win_append(frp->fr_win, wp1);
1713 frame_append(frp, wp1->w_frame);
1714
1715 wp2 = frp->fr_win; /* previously last window */
1716 }
1717 else /* last window becomes first window */
1718 {
1719 /* find last window/frame in the list and remove it */
1720 for (frp = curwin->w_frame; frp->fr_next != NULL;
1721 frp = frp->fr_next)
1722 ;
1723 wp1 = frp->fr_win;
1724 wp2 = wp1->w_prev; /* will become last window */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001725 win_remove(wp1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 frame_remove(frp);
1727
1728 /* append the removed window/frame before the first in the list */
1729 win_append(frp->fr_parent->fr_child->fr_win->w_prev, wp1);
1730 frame_insert(frp->fr_parent->fr_child, frp);
1731 }
1732
1733 /* exchange status height and vsep width of old and new last window */
1734 n = wp2->w_status_height;
1735 wp2->w_status_height = wp1->w_status_height;
1736 wp1->w_status_height = n;
1737 frame_fix_height(wp1);
1738 frame_fix_height(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 n = wp2->w_vsep_width;
1740 wp2->w_vsep_width = wp1->w_vsep_width;
1741 wp1->w_vsep_width = n;
1742 frame_fix_width(wp1);
1743 frame_fix_width(wp2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001745 /* recompute w_winrow and w_wincol for all windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 (void)win_comp_pos();
1747 }
1748
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001749 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750}
1751
1752/*
1753 * Move the current window to the very top/bottom/left/right of the screen.
1754 */
1755 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001756win_totop(int size, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 int dir;
1759 int height = curwin->w_height;
1760
Bram Moolenaar459ca562016-11-10 18:16:33 +01001761 if (ONE_WINDOW)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 {
1763 beep_flush();
1764 return;
1765 }
Bram Moolenaar1417c762019-07-27 17:31:36 +02001766 if (check_split_disallowed() == FAIL)
1767 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769 /* Remove the window and frame from the tree of frames. */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001770 (void)winframe_remove(curwin, &dir, NULL);
1771 win_remove(curwin, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772 last_status(FALSE); /* may need to remove last status line */
1773 (void)win_comp_pos(); /* recompute window positions */
1774
1775 /* Split a window on the desired side and put the window there. */
1776 (void)win_split_ins(size, flags, curwin, dir);
1777 if (!(flags & WSP_VERT))
1778 {
1779 win_setheight(height);
1780 if (p_ea)
1781 win_equal(curwin, TRUE, 'v');
1782 }
1783
Bram Moolenaar44a2f922016-03-19 22:11:51 +01001784#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
1786 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00001787 gui_may_update_scrollbars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789}
1790
1791/*
1792 * Move window "win1" to below/right of "win2" and make "win1" the current
1793 * window. Only works within the same frame!
1794 */
1795 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001796win_move_after(win_T *win1, win_T *win2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 int height;
1799
1800 /* check if the arguments are reasonable */
1801 if (win1 == win2)
1802 return;
1803
1804 /* check if there is something to do */
1805 if (win2->w_next != win1)
1806 {
Bram Moolenaar1417c762019-07-27 17:31:36 +02001807 if (win1->w_frame->fr_parent != win2->w_frame->fr_parent)
1808 {
1809 iemsg("INTERNAL: trying to move a window into another frame");
1810 return;
1811 }
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 /* may need move the status line/vertical separator of the last window
1814 * */
1815 if (win1 == lastwin)
1816 {
1817 height = win1->w_prev->w_status_height;
1818 win1->w_prev->w_status_height = win1->w_status_height;
1819 win1->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001820 if (win1->w_prev->w_vsep_width == 1)
1821 {
1822 /* Remove the vertical separator from the last-but-one window,
1823 * add it to the last window. Adjust the frame widths. */
1824 win1->w_prev->w_vsep_width = 0;
1825 win1->w_prev->w_frame->fr_width -= 1;
1826 win1->w_vsep_width = 1;
1827 win1->w_frame->fr_width += 1;
1828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 }
1830 else if (win2 == lastwin)
1831 {
1832 height = win1->w_status_height;
1833 win1->w_status_height = win2->w_status_height;
1834 win2->w_status_height = height;
Bram Moolenaar0396ab02007-02-19 23:14:18 +00001835 if (win1->w_vsep_width == 1)
1836 {
1837 /* Remove the vertical separator from win1, add it to the last
1838 * window, win2. Adjust the frame widths. */
1839 win2->w_vsep_width = 1;
1840 win2->w_frame->fr_width += 1;
1841 win1->w_vsep_width = 0;
1842 win1->w_frame->fr_width -= 1;
1843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001845 win_remove(win1, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 frame_remove(win1->w_frame);
1847 win_append(win2, win1);
1848 frame_append(win2->w_frame, win1->w_frame);
1849
1850 (void)win_comp_pos(); /* recompute w_winrow for all windows */
1851 redraw_later(NOT_VALID);
1852 }
1853 win_enter(win1, FALSE);
1854}
1855
1856/*
1857 * Make all windows the same height.
1858 * 'next_curwin' will soon be the current window, make sure it has enough
1859 * rows.
1860 */
1861 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001862win_equal(
1863 win_T *next_curwin, /* pointer to current window to be or NULL */
1864 int current, /* do only frame with current window */
1865 int dir) /* 'v' for vertically, 'h' for horizontally,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 'b' for both, 0 for using p_ead */
1867{
1868 if (dir == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 dir = *p_ead;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001871 topframe, dir, 0, tabline_height(),
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001872 (int)Columns, topframe->fr_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873}
1874
1875/*
1876 * Set a frame to a new position and height, spreading the available room
1877 * equally over contained frames.
1878 * The window "next_curwin" (if not NULL) should at least get the size from
1879 * 'winheight' and 'winwidth' if possible.
1880 */
1881 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01001882win_equal_rec(
1883 win_T *next_curwin, /* pointer to current window to be or NULL */
1884 int current, /* do only frame with current window */
1885 frame_T *topfr, /* frame to set size off */
1886 int dir, /* 'v', 'h' or 'b', see win_equal() */
1887 int col, /* horizontal position for frame */
1888 int row, /* vertical position for frame */
1889 int width, /* new width of frame */
1890 int height) /* new height of frame */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891{
1892 int n, m;
1893 int extra_sep = 0;
1894 int wincount, totwincount = 0;
1895 frame_T *fr;
1896 int next_curwin_size = 0;
1897 int room = 0;
1898 int new_size;
1899 int has_next_curwin = 0;
1900 int hnc;
1901
1902 if (topfr->fr_layout == FR_LEAF)
1903 {
1904 /* Set the width/height of this frame.
1905 * Redraw when size or position changes */
1906 if (topfr->fr_height != height || topfr->fr_win->w_winrow != row
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 || topfr->fr_width != width || topfr->fr_win->w_wincol != col
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 )
1909 {
1910 topfr->fr_win->w_winrow = row;
1911 frame_new_height(topfr, height, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 topfr->fr_win->w_wincol = col;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001913 frame_new_width(topfr, width, FALSE, FALSE);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01001914 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 else if (topfr->fr_layout == FR_ROW)
1918 {
1919 topfr->fr_width = width;
1920 topfr->fr_height = height;
1921
1922 if (dir != 'v') /* equalize frame widths */
1923 {
1924 /* Compute the maximum number of windows horizontally in this
1925 * frame. */
1926 n = frame_minwidth(topfr, NOWIN);
1927 /* add one for the rightmost window, it doesn't have a separator */
1928 if (col + width == Columns)
1929 extra_sep = 1;
1930 else
1931 extra_sep = 0;
1932 totwincount = (n + extra_sep) / (p_wmw + 1);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001933 has_next_curwin = frame_has_win(topfr, next_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001935 /*
1936 * Compute width for "next_curwin" window and room available for
1937 * other windows.
1938 * "m" is the minimal width when counting p_wiw for "next_curwin".
1939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 m = frame_minwidth(topfr, next_curwin);
1941 room = width - m;
1942 if (room < 0)
1943 {
1944 next_curwin_size = p_wiw + room;
1945 room = 0;
1946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 else
1948 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001949 next_curwin_size = -1;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01001950 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001951 {
1952 /* If 'winfixwidth' set keep the window width if
1953 * possible.
1954 * Watch out for this window being the next_curwin. */
1955 if (frame_fixed_width(fr))
1956 {
1957 n = frame_minwidth(fr, NOWIN);
1958 new_size = fr->fr_width;
1959 if (frame_has_win(fr, next_curwin))
1960 {
1961 room += p_wiw - p_wmw;
1962 next_curwin_size = 0;
1963 if (new_size < p_wiw)
1964 new_size = p_wiw;
1965 }
1966 else
1967 /* These windows don't use up room. */
1968 totwincount -= (n + (fr->fr_next == NULL
1969 ? extra_sep : 0)) / (p_wmw + 1);
1970 room -= new_size - n;
1971 if (room < 0)
1972 {
1973 new_size += room;
1974 room = 0;
1975 }
1976 fr->fr_newwidth = new_size;
1977 }
1978 }
1979 if (next_curwin_size == -1)
1980 {
1981 if (!has_next_curwin)
1982 next_curwin_size = 0;
1983 else if (totwincount > 1
1984 && (room + (totwincount - 2))
1985 / (totwincount - 1) > p_wiw)
1986 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001987 /* Can make all windows wider than 'winwidth', spread
1988 * the room equally. */
1989 next_curwin_size = (room + p_wiw
1990 + (totwincount - 1) * p_wmw
1991 + (totwincount - 1)) / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001992 room -= next_curwin_size - p_wiw;
1993 }
1994 else
1995 next_curwin_size = p_wiw;
1996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00001998
1999 if (has_next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 --totwincount; /* don't count curwin */
2001 }
2002
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002003 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 wincount = 1;
2006 if (fr->fr_next == NULL)
2007 /* last frame gets all that remains (avoid roundoff error) */
2008 new_size = width;
2009 else if (dir == 'v')
2010 new_size = fr->fr_width;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002011 else if (frame_fixed_width(fr))
2012 {
2013 new_size = fr->fr_newwidth;
2014 wincount = 0; /* doesn't count as a sizeable window */
2015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 else
2017 {
2018 /* Compute the maximum number of windows horiz. in "fr". */
2019 n = frame_minwidth(fr, NOWIN);
2020 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2021 / (p_wmw + 1);
2022 m = frame_minwidth(fr, next_curwin);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002023 if (has_next_curwin)
2024 hnc = frame_has_win(fr, next_curwin);
2025 else
2026 hnc = FALSE;
2027 if (hnc) /* don't count next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 --wincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002029 if (totwincount == 0)
2030 new_size = room;
2031 else
2032 new_size = (wincount * room + ((unsigned)totwincount >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 / totwincount;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002034 if (hnc) /* add next_curwin size */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
2036 next_curwin_size -= p_wiw - (m - n);
2037 new_size += next_curwin_size;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002038 room -= new_size - next_curwin_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002040 else
2041 room -= new_size;
2042 new_size += n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002045 /* Skip frame that is full width when splitting or closing a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 * window, unless equalizing all frames. */
2047 if (!current || dir != 'v' || topfr->fr_parent != NULL
2048 || (new_size != fr->fr_width)
2049 || frame_has_win(fr, next_curwin))
2050 win_equal_rec(next_curwin, current, fr, dir, col, row,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002051 new_size, height);
2052 col += new_size;
2053 width -= new_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 totwincount -= wincount;
2055 }
2056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 else /* topfr->fr_layout == FR_COL */
2058 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 topfr->fr_width = width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 topfr->fr_height = height;
2061
2062 if (dir != 'h') /* equalize frame heights */
2063 {
2064 /* Compute maximum number of windows vertically in this frame. */
2065 n = frame_minheight(topfr, NOWIN);
2066 /* add one for the bottom window if it doesn't have a statusline */
2067 if (row + height == cmdline_row && p_ls == 0)
2068 extra_sep = 1;
2069 else
2070 extra_sep = 0;
2071 totwincount = (n + extra_sep) / (p_wmh + 1);
2072 has_next_curwin = frame_has_win(topfr, next_curwin);
2073
2074 /*
2075 * Compute height for "next_curwin" window and room available for
2076 * other windows.
2077 * "m" is the minimal height when counting p_wh for "next_curwin".
2078 */
2079 m = frame_minheight(topfr, next_curwin);
2080 room = height - m;
2081 if (room < 0)
2082 {
2083 /* The room is less then 'winheight', use all space for the
2084 * current window. */
2085 next_curwin_size = p_wh + room;
2086 room = 0;
2087 }
2088 else
2089 {
2090 next_curwin_size = -1;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002091 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
2093 /* If 'winfixheight' set keep the window height if
2094 * possible.
2095 * Watch out for this window being the next_curwin. */
2096 if (frame_fixed_height(fr))
2097 {
2098 n = frame_minheight(fr, NOWIN);
2099 new_size = fr->fr_height;
2100 if (frame_has_win(fr, next_curwin))
2101 {
2102 room += p_wh - p_wmh;
2103 next_curwin_size = 0;
2104 if (new_size < p_wh)
2105 new_size = p_wh;
2106 }
2107 else
2108 /* These windows don't use up room. */
2109 totwincount -= (n + (fr->fr_next == NULL
2110 ? extra_sep : 0)) / (p_wmh + 1);
2111 room -= new_size - n;
2112 if (room < 0)
2113 {
2114 new_size += room;
2115 room = 0;
2116 }
2117 fr->fr_newheight = new_size;
2118 }
2119 }
2120 if (next_curwin_size == -1)
2121 {
2122 if (!has_next_curwin)
2123 next_curwin_size = 0;
2124 else if (totwincount > 1
2125 && (room + (totwincount - 2))
2126 / (totwincount - 1) > p_wh)
2127 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00002128 /* can make all windows higher than 'winheight',
2129 * spread the room equally. */
2130 next_curwin_size = (room + p_wh
2131 + (totwincount - 1) * p_wmh
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 + (totwincount - 1)) / totwincount;
2133 room -= next_curwin_size - p_wh;
2134 }
2135 else
2136 next_curwin_size = p_wh;
2137 }
2138 }
2139
2140 if (has_next_curwin)
2141 --totwincount; /* don't count curwin */
2142 }
2143
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002144 FOR_ALL_FRAMES(fr, topfr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 wincount = 1;
2147 if (fr->fr_next == NULL)
2148 /* last frame gets all that remains (avoid roundoff error) */
2149 new_size = height;
2150 else if (dir == 'h')
2151 new_size = fr->fr_height;
2152 else if (frame_fixed_height(fr))
2153 {
2154 new_size = fr->fr_newheight;
2155 wincount = 0; /* doesn't count as a sizeable window */
2156 }
2157 else
2158 {
2159 /* Compute the maximum number of windows vert. in "fr". */
2160 n = frame_minheight(fr, NOWIN);
2161 wincount = (n + (fr->fr_next == NULL ? extra_sep : 0))
2162 / (p_wmh + 1);
2163 m = frame_minheight(fr, next_curwin);
2164 if (has_next_curwin)
2165 hnc = frame_has_win(fr, next_curwin);
2166 else
2167 hnc = FALSE;
2168 if (hnc) /* don't count next_curwin */
2169 --wincount;
2170 if (totwincount == 0)
2171 new_size = room;
2172 else
2173 new_size = (wincount * room + ((unsigned)totwincount >> 1))
2174 / totwincount;
2175 if (hnc) /* add next_curwin size */
2176 {
2177 next_curwin_size -= p_wh - (m - n);
2178 new_size += next_curwin_size;
2179 room -= new_size - next_curwin_size;
2180 }
2181 else
2182 room -= new_size;
2183 new_size += n;
2184 }
2185 /* Skip frame that is full width when splitting or closing a
2186 * window, unless equalizing all frames. */
2187 if (!current || dir != 'h' || topfr->fr_parent != NULL
2188 || (new_size != fr->fr_height)
2189 || frame_has_win(fr, next_curwin))
2190 win_equal_rec(next_curwin, current, fr, dir, col, row,
2191 width, new_size);
2192 row += new_size;
2193 height -= new_size;
2194 totwincount -= wincount;
2195 }
2196 }
2197}
2198
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002199#ifdef FEAT_JOB_CHANNEL
2200 static void
2201leaving_window(win_T *win)
2202{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002203 // Only matters for a prompt window.
2204 if (!bt_prompt(win->w_buffer))
2205 return;
2206
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002207 // When leaving a prompt window stop Insert mode and perhaps restart
2208 // it when entering that window again.
2209 win->w_buffer->b_prompt_insert = restart_edit;
Bram Moolenaar942b4542018-06-17 16:23:34 +02002210 if (restart_edit != 0 && mode_displayed)
2211 clear_cmdline = TRUE; /* unshow mode later */
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002212 restart_edit = NUL;
2213
2214 // When leaving the window (or closing the window) was done from a
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002215 // callback we need to break out of the Insert mode loop and restart Insert
2216 // mode when entering the window again.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002217 if (State & INSERT)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002218 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002219 stop_insert_mode = TRUE;
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002220 if (win->w_buffer->b_prompt_insert == NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002221 win->w_buffer->b_prompt_insert = 'A';
2222 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002223}
2224
2225 static void
2226entering_window(win_T *win)
2227{
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002228 // Only matters for a prompt window.
2229 if (!bt_prompt(win->w_buffer))
2230 return;
2231
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002232 // When switching to a prompt buffer that was in Insert mode, don't stop
2233 // Insert mode, it may have been set in leaving_window().
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002234 if (win->w_buffer->b_prompt_insert != NUL)
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02002235 stop_insert_mode = FALSE;
2236
Bram Moolenaarf98b8452018-06-10 14:39:52 +02002237 // When entering the prompt window restart Insert mode if we were in Insert
2238 // mode when we left it.
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002239 restart_edit = win->w_buffer->b_prompt_insert;
2240}
2241#endif
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002244 * Close all windows for buffer "buf".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 */
2246 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002247close_windows(
2248 buf_T *buf,
2249 int keep_curwin) /* don't close "curwin" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250{
Bram Moolenaarf740b292006-02-16 22:11:02 +00002251 win_T *wp;
2252 tabpage_T *tp, *nexttp;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002253 int h = tabline_height();
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002254 int count = tabpage_index(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
2256 ++RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002257
Bram Moolenaar459ca562016-11-10 18:16:33 +01002258 for (wp = firstwin; wp != NULL && !ONE_WINDOW; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaar362ce482012-06-06 19:02:45 +02002260 if (wp->w_buffer == buf && (!keep_curwin || wp != curwin)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002261 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01002263 if (win_close(wp, FALSE) == FAIL)
2264 /* If closing the window fails give up, to avoid looping
2265 * forever. */
2266 break;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002267
2268 /* Start all over, autocommands may change the window layout. */
2269 wp = firstwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 else
Bram Moolenaarf740b292006-02-16 22:11:02 +00002272 wp = wp->w_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00002274
2275 /* Also check windows in other tab pages. */
2276 for (tp = first_tabpage; tp != NULL; tp = nexttp)
2277 {
2278 nexttp = tp->tp_next;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002279 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002280 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar362ce482012-06-06 19:02:45 +02002281 if (wp->w_buffer == buf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002282 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaarf740b292006-02-16 22:11:02 +00002283 {
2284 win_close_othertab(wp, FALSE, tp);
2285
2286 /* Start all over, the tab page may be closed and
2287 * autocommands may change the window layout. */
2288 nexttp = first_tabpage;
2289 break;
2290 }
2291 }
2292
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 --RedrawingDisabled;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002294
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002295 if (count != tabpage_index(NULL))
2296 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002297
Bram Moolenaar4c7e9db2013-04-15 15:55:19 +02002298 redraw_tabline = TRUE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002299 if (h != tabline_height())
Bram Moolenaarf740b292006-02-16 22:11:02 +00002300 shell_new_rows();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301}
2302
2303/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002304 * Return TRUE if the current window is the only window that exists (ignoring
2305 * "aucmd_win").
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002306 * Returns FALSE if there is a window, possibly in another tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002307 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002308 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002309last_window(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002310{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002311 return (one_window() && first_tabpage->tp_next == NULL);
2312}
2313
2314/*
2315 * Return TRUE if there is only one window other than "aucmd_win" in the
2316 * current tab page.
2317 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002318 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002319one_window(void)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002320{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002321 win_T *wp;
2322 int seen_one = FALSE;
2323
2324 FOR_ALL_WINDOWS(wp)
2325 {
2326 if (wp != aucmd_win)
2327 {
2328 if (seen_one)
2329 return FALSE;
2330 seen_one = TRUE;
2331 }
2332 }
2333 return TRUE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002334}
2335
2336/*
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002337 * Close the possibly last window in a tab page.
2338 * Returns TRUE when the window was closed already.
2339 */
2340 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002341close_last_window_tabpage(
2342 win_T *win,
2343 int free_buf,
2344 tabpage_T *prev_curtab)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002345{
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002346 if (ONE_WINDOW)
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002347 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002348 buf_T *old_curbuf = curbuf;
2349
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002350 /*
2351 * Closing the last window in a tab page. First go to another tab
2352 * page and then close the window and the tab page. This avoids that
2353 * curwin and curtab are invalid while we are freeing memory, they may
2354 * be used in GUI events.
Bram Moolenaara8596c42012-06-13 14:28:20 +02002355 * Don't trigger autocommands yet, they may use wrong values, so do
2356 * that below.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002357 */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002358 goto_tabpage_tp(alt_tabpage(), FALSE, TRUE);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002359 redraw_tabline = TRUE;
2360
2361 /* Safety check: Autocommands may have closed the window when jumping
2362 * to the other tab page. */
2363 if (valid_tabpage(prev_curtab) && prev_curtab->tp_firstwin == win)
2364 {
2365 int h = tabline_height();
2366
2367 win_close_othertab(win, free_buf, prev_curtab);
2368 if (h != tabline_height())
2369 shell_new_rows();
2370 }
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002371#ifdef FEAT_JOB_CHANNEL
2372 entering_window(curwin);
2373#endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02002374 /* Since goto_tabpage_tp above did not trigger *Enter autocommands, do
2375 * that now. */
Bram Moolenaar12c11d52016-07-19 23:13:03 +02002376 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaara8596c42012-06-13 14:28:20 +02002377 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02002378 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
2379 if (old_curbuf != curbuf)
2380 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002381 return TRUE;
2382 }
2383 return FALSE;
2384}
2385
2386/*
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002387 * Close the buffer of "win" and unload it if "action" is DOBUF_UNLOAD.
2388 * "action" can also be zero (do nothing) or DOBUF_WIPE.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002389 * "abort_if_last" is passed to close_buffer(): abort closing if all other
2390 * windows are closed.
2391 */
2392 static void
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002393win_close_buffer(win_T *win, int action, int abort_if_last)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002394{
2395#ifdef FEAT_SYN_HL
2396 // Free independent synblock before the buffer is freed.
2397 if (win->w_buffer != NULL)
2398 reset_synblock(win);
2399#endif
2400
2401#ifdef FEAT_QUICKFIX
2402 // When the quickfix/location list window is closed, unlist the buffer.
2403 if (win->w_buffer != NULL && bt_quickfix(win->w_buffer))
2404 win->w_buffer->b_p_bl = FALSE;
2405#endif
2406
2407 // Close the link to the buffer.
2408 if (win->w_buffer != NULL)
2409 {
2410 bufref_T bufref;
2411
2412 set_bufref(&bufref, curbuf);
2413 win->w_closing = TRUE;
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002414 close_buffer(win, win->w_buffer, action, abort_if_last);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002415 if (win_valid_any_tab(win))
2416 win->w_closing = FALSE;
2417 // Make sure curbuf is valid. It can become invalid if 'bufhidden' is
2418 // "wipe".
2419 if (!bufref_valid(&bufref))
2420 curbuf = firstbuf;
2421 }
2422}
2423
2424/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002425 * Close window "win". Only works for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 * If "free_buf" is TRUE related buffer may be unloaded.
2427 *
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002428 * Called by :quit, :close, :xit, :wq and findtag().
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002429 * Returns FAIL when the window was not closed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 */
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002431 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002432win_close(win_T *win, int free_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 int close_curwin = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 int dir;
2438 int help_window = FALSE;
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002439 tabpage_T *prev_curtab = curtab;
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002440 frame_T *win_frame = win->w_frame->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002442 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002443 return FAIL;
2444
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002445 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002447 emsg(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002448 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002451 if (win->w_closing || (win->w_buffer != NULL
2452 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002453 return FAIL; /* window is already being closed */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002454 if (win_unlisted(win))
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002455 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002456 emsg(_("E813: Cannot close autocmd or popup window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002457 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002458 }
2459 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2460 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002461 emsg(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002462 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002463 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002464
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002465 /* When closing the last window in a tab page first go to another tab page
2466 * and then close the window and the tab page to avoid that curwin and
2467 * curtab are invalid while we are freeing memory. */
2468 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002469 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002470
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 /* When closing the help window, try restoring a snapshot after closing
2472 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002473 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 help_window = TRUE;
2475 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002476 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 if (win == curwin)
2479 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002480#ifdef FEAT_JOB_CHANNEL
2481 leaving_window(curwin);
2482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 /*
2484 * Guess which window is going to be the new current window.
2485 * This may change because of the autocommands (sigh).
2486 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002487 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
2489 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002490 * Be careful: If autocommands delete the window or cause this window
2491 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 */
2493 if (wp->w_buffer != curbuf)
2494 {
2495 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002496 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002498 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002499 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002500 win->w_closing = FALSE;
2501 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002504 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002506 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002507 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002508 win->w_closing = FALSE;
2509 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002510 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002511#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /* autocmds may abort script processing */
2513 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002514 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002518#ifdef FEAT_GUI
Bram Moolenaar647e24b2019-03-17 16:39:46 +01002519 // Avoid trouble with scrollbars that are going to be deleted in
2520 // win_free().
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002521 if (gui.in_use)
2522 out_flush();
2523#endif
2524
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002525 win_close_buffer(win, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002526
Bram Moolenaar802418d2013-01-17 14:00:11 +01002527 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2528 && (last_window() || curtab != prev_curtab
2529 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002530 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002531 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002532 * curwin->w_buffer, otherwise writing viminfo may fail. */
2533 if (curwin->w_buffer == NULL)
2534 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002535 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002536 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002537
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002538 /* Autocommands may have moved to another tab page. */
2539 if (curtab != prev_curtab && win_valid_any_tab(win)
2540 && win->w_buffer == NULL)
2541 {
2542 /* Need to close the window anyway, since the buffer is NULL. */
2543 win_close_othertab(win, FALSE, prev_curtab);
2544 return FAIL;
2545 }
2546
2547 /* Autocommands may have closed the window already or closed the only
2548 * other window. */
2549 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002550 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002551 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552
Bram Moolenaar1417c762019-07-27 17:31:36 +02002553 // Now we are really going to close the window. Disallow any autocommand
2554 // to split a window to avoid trouble.
2555 ++split_disallowed;
2556
Bram Moolenaara971b822011-09-14 14:43:25 +02002557 /* Free the memory used for the window and get the window that received
2558 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002559 wp = win_free_mem(win, &dir, NULL);
2560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 /* Make sure curwin isn't invalid. It can cause severe trouble when
2562 * printing an error message. For win_equal() curbuf needs to be valid
2563 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002564 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
2566 curwin = wp;
2567#ifdef FEAT_QUICKFIX
2568 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2569 {
2570 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002571 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 * finding another window to go to.
2573 */
2574 for (;;)
2575 {
2576 if (wp->w_next == NULL)
2577 wp = firstwin;
2578 else
2579 wp = wp->w_next;
2580 if (wp == curwin)
2581 break;
2582 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2583 {
2584 curwin = wp;
2585 break;
2586 }
2587 }
2588 }
2589#endif
2590 curbuf = curwin->w_buffer;
2591 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002592
2593 /* The cursor position may be invalid if the buffer changed after last
2594 * using the window. */
2595 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002597 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002598 /* If the frame of the closed window contains the new current window,
2599 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002600 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 else
2602 win_comp_pos();
2603 if (close_curwin)
2604 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002605 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (other_buffer)
2607 /* careful: after this wp and win may be invalid! */
2608 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610
Bram Moolenaar1417c762019-07-27 17:31:36 +02002611 --split_disallowed;
2612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002614 * If last window has a status line now and we don't want one,
2615 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 */
2617 last_status(FALSE);
2618
2619 /* After closing the help window, try restoring the window layout from
2620 * before it was opened. */
2621 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002622 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002624#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2626 if (gui.in_use && !win_hasvertsplit())
2627 gui_init_which_components(NULL);
2628#endif
2629
2630 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002631 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632}
2633
2634/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002635 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002636 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002637 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002638 * Caller must check if buffer is hidden and whether the tabline needs to be
2639 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002640 */
2641 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002642win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002643{
2644 win_T *wp;
2645 int dir;
2646 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002647 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002648
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002649 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2650 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002651 if (win->w_closing || (win->w_buffer != NULL
2652 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002653 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002654
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002655 if (win->w_buffer != NULL)
2656 /* Close the link to the buffer. */
2657 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002658
2659 /* Careful: Autocommands may have closed the tab page or made it the
2660 * current tab page. */
2661 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2662 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002663 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002664 return;
2665
2666 /* Autocommands may have closed the window already. */
2667 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2668 ;
2669 if (wp == NULL)
2670 return;
2671
Bram Moolenaarf740b292006-02-16 22:11:02 +00002672 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002673 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002674 {
2675 if (tp == first_tabpage)
2676 first_tabpage = tp->tp_next;
2677 else
2678 {
2679 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2680 ptp = ptp->tp_next)
2681 ;
2682 if (ptp == NULL)
2683 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002684 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002685 return;
2686 }
2687 ptp->tp_next = tp->tp_next;
2688 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002689 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002690 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002691
2692 /* Free the memory used for the window. */
2693 win_free_mem(win, &dir, tp);
2694
2695 if (free_tp)
2696 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002697}
2698
2699/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002700 * Free the memory used for a window.
2701 * Returns a pointer to the window that got the freed up space.
2702 */
2703 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002704win_free_mem(
2705 win_T *win,
2706 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2707 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002708{
2709 frame_T *frp;
2710 win_T *wp;
2711
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002712 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002713 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002714 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002715 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002716 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002717
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002718 /* When deleting the current window of another tab page select a new
2719 * current window. */
2720 if (tp != NULL && win == tp->tp_curwin)
2721 tp->tp_curwin = wp;
2722
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002723 return wp;
2724}
2725
2726#if defined(EXITFREE) || defined(PROTO)
2727 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002728win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002729{
2730 int dummy;
2731
Bram Moolenaarf740b292006-02-16 22:11:02 +00002732 while (first_tabpage->tp_next != NULL)
2733 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002734
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002735 if (aucmd_win != NULL)
2736 {
2737 (void)win_free_mem(aucmd_win, &dummy, NULL);
2738 aucmd_win = NULL;
2739 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002740# ifdef FEAT_TEXT_PROP
2741 close_all_popups();
2742# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002743
2744 while (firstwin != NULL)
2745 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002746
2747 /* No window should be used after this. Set curwin to NULL to crash
2748 * instead of using freed memory. */
2749 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002750}
2751#endif
2752
2753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 * Remove a window and its frame from the tree of frames.
2755 * Returns a pointer to the window that got the freed up space.
2756 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002757 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002758winframe_remove(
2759 win_T *win,
2760 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2761 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762{
2763 frame_T *frp, *frp2, *frp3;
2764 frame_T *frp_close = win->w_frame;
2765 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
2767 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002768 * If there is only one window there is nothing to remove.
2769 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002770 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002771 return NULL;
2772
2773 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 * Remove the window from its frame.
2775 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002776 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 wp = frame2win(frp2);
2778
2779 /* Remove this frame from the list of frames. */
2780 frame_remove(frp_close);
2781
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 if (frp_close->fr_parent->fr_layout == FR_COL)
2783 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002784 /* When 'winfixheight' is set, try to find another frame in the column
2785 * (as close to the closed frame as possible) to distribute the height
2786 * to. */
2787 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2788 {
2789 frp = frp_close->fr_prev;
2790 frp3 = frp_close->fr_next;
2791 while (frp != NULL || frp3 != NULL)
2792 {
2793 if (frp != NULL)
2794 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002795 if (!frame_fixed_height(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002796 {
2797 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002798 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002799 break;
2800 }
2801 frp = frp->fr_prev;
2802 }
2803 if (frp3 != NULL)
2804 {
2805 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2806 {
2807 frp2 = frp3;
2808 wp = frp3->fr_win;
2809 break;
2810 }
2811 frp3 = frp3->fr_next;
2812 }
2813 }
2814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2816 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 *dirp = 'v';
2818 }
2819 else
2820 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002821 /* When 'winfixwidth' is set, try to find another frame in the column
2822 * (as close to the closed frame as possible) to distribute the width
2823 * to. */
2824 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2825 {
2826 frp = frp_close->fr_prev;
2827 frp3 = frp_close->fr_next;
2828 while (frp != NULL || frp3 != NULL)
2829 {
2830 if (frp != NULL)
2831 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002832 if (!frame_fixed_width(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002833 {
2834 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002835 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002836 break;
2837 }
2838 frp = frp->fr_prev;
2839 }
2840 if (frp3 != NULL)
2841 {
2842 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2843 {
2844 frp2 = frp3;
2845 wp = frp3->fr_win;
2846 break;
2847 }
2848 frp3 = frp3->fr_next;
2849 }
2850 }
2851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002853 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 *dirp = 'h';
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856
2857 /* If rows/columns go to a window below/right its positions need to be
2858 * updated. Can only be done after the sizes have been updated. */
2859 if (frp2 == frp_close->fr_next)
2860 {
2861 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002862 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864 frame_comp_pos(frp2, &row, &col);
2865 }
2866
2867 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2868 {
2869 /* There is no other frame in this list, move its info to the parent
2870 * and remove it. */
2871 frp2->fr_parent->fr_layout = frp2->fr_layout;
2872 frp2->fr_parent->fr_child = frp2->fr_child;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002873 FOR_ALL_FRAMES(frp, frp2->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 frp->fr_parent = frp2->fr_parent;
2875 frp2->fr_parent->fr_win = frp2->fr_win;
2876 if (frp2->fr_win != NULL)
2877 frp2->fr_win->w_frame = frp2->fr_parent;
2878 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002879 if (topframe->fr_child == frp2)
2880 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 vim_free(frp2);
2882
2883 frp2 = frp->fr_parent;
2884 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2885 {
2886 /* The frame above the parent has the same layout, have to merge
2887 * the frames into this list. */
2888 if (frp2->fr_child == frp)
2889 frp2->fr_child = frp->fr_child;
2890 frp->fr_child->fr_prev = frp->fr_prev;
2891 if (frp->fr_prev != NULL)
2892 frp->fr_prev->fr_next = frp->fr_child;
2893 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2894 {
2895 frp3->fr_parent = frp2;
2896 if (frp3->fr_next == NULL)
2897 {
2898 frp3->fr_next = frp->fr_next;
2899 if (frp->fr_next != NULL)
2900 frp->fr_next->fr_prev = frp3;
2901 break;
2902 }
2903 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002904 if (topframe->fr_child == frp)
2905 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 vim_free(frp);
2907 }
2908 }
2909
2910 return wp;
2911}
2912
2913/*
Bram Moolenaarc136af22018-05-04 20:15:38 +02002914 * Return a pointer to the frame that will receive the empty screen space that
2915 * is left over after "win" is closed.
2916 *
2917 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
2918 * by default. Otherwise, the free space goes below or to the right. The
2919 * result is that opening a window and then immediately closing it will
2920 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
2921 * respected when possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 */
2923 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002924win_altframe(
2925 win_T *win,
2926 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 frame_T *frp;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002929 frame_T *other_fr, *target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002931 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002932 return alt_tabpage()->tp_curwin->w_frame;
2933
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 frp = win->w_frame;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002935
2936 if (frp->fr_prev == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 return frp->fr_next;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002938 if (frp->fr_next == NULL)
2939 return frp->fr_prev;
2940
2941 target_fr = frp->fr_next;
2942 other_fr = frp->fr_prev;
2943 if (p_spr || p_sb)
2944 {
2945 target_fr = frp->fr_prev;
2946 other_fr = frp->fr_next;
2947 }
2948
2949 /* If 'wfh' or 'wfw' is set for the target and not for the alternate
2950 * window, reverse the selection. */
2951 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2952 {
2953 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
2954 target_fr = other_fr;
2955 }
2956 else
2957 {
2958 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
2959 target_fr = other_fr;
2960 }
2961
2962 return target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963}
2964
2965/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002966 * Return the tabpage that will be used if the current one is closed.
2967 */
2968 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002969alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002970{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002971 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002972
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002973 /* Use the next tab page if possible. */
2974 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002975 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002976
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002977 /* Find the last but one tab page. */
2978 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2979 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002980 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002981}
2982
2983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 * Find the left-upper window in frame "frp".
2985 */
2986 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002987frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 while (frp->fr_win == NULL)
2990 frp = frp->fr_child;
2991 return frp->fr_win;
2992}
2993
2994/*
2995 * Return TRUE if frame "frp" contains window "wp".
2996 */
2997 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002998frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 frame_T *p;
3001
3002 if (frp->fr_layout == FR_LEAF)
3003 return frp->fr_win == wp;
3004
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003005 FOR_ALL_FRAMES(p, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 if (frame_has_win(p, wp))
3007 return TRUE;
3008 return FALSE;
3009}
3010
3011/*
3012 * Set a new height for a frame. Recursively sets the height for contained
3013 * frames and windows. Caller must take care of positions.
3014 */
3015 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003016frame_new_height(
3017 frame_T *topfrp,
3018 int height,
3019 int topfirst, /* resize topmost contained frame first */
3020 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 may cause the height not to be set */
3022{
3023 frame_T *frp;
3024 int extra_lines;
3025 int h;
3026
3027 if (topfrp->fr_win != NULL)
3028 {
3029 /* Simple case: just one window. */
3030 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02003031 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01003032 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 else if (topfrp->fr_layout == FR_ROW)
3035 {
3036 do
3037 {
3038 /* All frames in this row get the same new height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003039 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 {
3041 frame_new_height(frp, height, topfirst, wfh);
3042 if (frp->fr_height > height)
3043 {
3044 /* Could not fit the windows, make the whole row higher. */
3045 height = frp->fr_height;
3046 break;
3047 }
3048 }
3049 }
3050 while (frp != NULL);
3051 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003052 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 {
3054 /* Complicated case: Resize a column of frames. Resize the bottom
3055 * frame first, frames above that when needed. */
3056
3057 frp = topfrp->fr_child;
3058 if (wfh)
3059 /* Advance past frames with one window with 'wfh' set. */
3060 while (frame_fixed_height(frp))
3061 {
3062 frp = frp->fr_next;
3063 if (frp == NULL)
3064 return; /* no frame without 'wfh', give up */
3065 }
3066 if (!topfirst)
3067 {
3068 /* Find the bottom frame of this column */
3069 while (frp->fr_next != NULL)
3070 frp = frp->fr_next;
3071 if (wfh)
3072 /* Advance back for frames with one window with 'wfh' set. */
3073 while (frame_fixed_height(frp))
3074 frp = frp->fr_prev;
3075 }
3076
3077 extra_lines = height - topfrp->fr_height;
3078 if (extra_lines < 0)
3079 {
3080 /* reduce height of contained frames, bottom or top frame first */
3081 while (frp != NULL)
3082 {
3083 h = frame_minheight(frp, NULL);
3084 if (frp->fr_height + extra_lines < h)
3085 {
3086 extra_lines += frp->fr_height - h;
3087 frame_new_height(frp, h, topfirst, wfh);
3088 }
3089 else
3090 {
3091 frame_new_height(frp, frp->fr_height + extra_lines,
3092 topfirst, wfh);
3093 break;
3094 }
3095 if (topfirst)
3096 {
3097 do
3098 frp = frp->fr_next;
3099 while (wfh && frp != NULL && frame_fixed_height(frp));
3100 }
3101 else
3102 {
3103 do
3104 frp = frp->fr_prev;
3105 while (wfh && frp != NULL && frame_fixed_height(frp));
3106 }
3107 /* Increase "height" if we could not reduce enough frames. */
3108 if (frp == NULL)
3109 height -= extra_lines;
3110 }
3111 }
3112 else if (extra_lines > 0)
3113 {
3114 /* increase height of bottom or top frame */
3115 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3116 }
3117 }
3118 topfrp->fr_height = height;
3119}
3120
3121/*
3122 * Return TRUE if height of frame "frp" should not be changed because of
3123 * the 'winfixheight' option.
3124 */
3125 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003126frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127{
3128 /* frame with one window: fixed height if 'winfixheight' set. */
3129 if (frp->fr_win != NULL)
3130 return frp->fr_win->w_p_wfh;
3131
3132 if (frp->fr_layout == FR_ROW)
3133 {
3134 /* The frame is fixed height if one of the frames in the row is fixed
3135 * height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003136 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (frame_fixed_height(frp))
3138 return TRUE;
3139 return FALSE;
3140 }
3141
3142 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3143 * frames in the row are fixed height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003144 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 if (!frame_fixed_height(frp))
3146 return FALSE;
3147 return TRUE;
3148}
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003151 * Return TRUE if width of frame "frp" should not be changed because of
3152 * the 'winfixwidth' option.
3153 */
3154 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003155frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003156{
3157 /* frame with one window: fixed width if 'winfixwidth' set. */
3158 if (frp->fr_win != NULL)
3159 return frp->fr_win->w_p_wfw;
3160
3161 if (frp->fr_layout == FR_COL)
3162 {
3163 /* The frame is fixed width if one of the frames in the row is fixed
3164 * width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003165 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003166 if (frame_fixed_width(frp))
3167 return TRUE;
3168 return FALSE;
3169 }
3170
3171 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3172 * frames in the row are fixed width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003173 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003174 if (!frame_fixed_width(frp))
3175 return FALSE;
3176 return TRUE;
3177}
3178
3179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 * Add a status line to windows at the bottom of "frp".
3181 * Note: Does not check if there is room!
3182 */
3183 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003184frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185{
3186 win_T *wp;
3187
3188 if (frp->fr_layout == FR_LEAF)
3189 {
3190 wp = frp->fr_win;
3191 if (wp->w_status_height == 0)
3192 {
3193 if (wp->w_height > 0) /* don't make it negative */
3194 --wp->w_height;
3195 wp->w_status_height = STATUS_HEIGHT;
3196 }
3197 }
3198 else if (frp->fr_layout == FR_ROW)
3199 {
3200 /* Handle all the frames in the row. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003201 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 frame_add_statusline(frp);
3203 }
3204 else /* frp->fr_layout == FR_COL */
3205 {
3206 /* Only need to handle the last frame in the column. */
3207 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3208 ;
3209 frame_add_statusline(frp);
3210 }
3211}
3212
3213/*
3214 * Set width of a frame. Handles recursively going through contained frames.
3215 * May remove separator line for windows at the right side (for win_close()).
3216 */
3217 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003218frame_new_width(
3219 frame_T *topfrp,
3220 int width,
3221 int leftfirst, /* resize leftmost contained frame first */
3222 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003223 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224{
3225 frame_T *frp;
3226 int extra_cols;
3227 int w;
3228 win_T *wp;
3229
3230 if (topfrp->fr_layout == FR_LEAF)
3231 {
3232 /* Simple case: just one window. */
3233 wp = topfrp->fr_win;
3234 /* Find out if there are any windows right of this one. */
3235 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3236 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3237 break;
3238 if (frp->fr_parent == NULL)
3239 wp->w_vsep_width = 0;
3240 win_new_width(wp, width - wp->w_vsep_width);
3241 }
3242 else if (topfrp->fr_layout == FR_COL)
3243 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003244 do
3245 {
3246 /* All frames in this column get the same new width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003247 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003248 {
3249 frame_new_width(frp, width, leftfirst, wfw);
3250 if (frp->fr_width > width)
3251 {
3252 /* Could not fit the windows, make whole column wider. */
3253 width = frp->fr_width;
3254 break;
3255 }
3256 }
3257 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 }
3259 else /* fr_layout == FR_ROW */
3260 {
3261 /* Complicated case: Resize a row of frames. Resize the rightmost
3262 * frame first, frames left of it when needed. */
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003265 if (wfw)
3266 /* Advance past frames with one window with 'wfw' set. */
3267 while (frame_fixed_width(frp))
3268 {
3269 frp = frp->fr_next;
3270 if (frp == NULL)
3271 return; /* no frame without 'wfw', give up */
3272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003274 {
3275 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 while (frp->fr_next != NULL)
3277 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003278 if (wfw)
3279 /* Advance back for frames with one window with 'wfw' set. */
3280 while (frame_fixed_width(frp))
3281 frp = frp->fr_prev;
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 extra_cols = width - topfrp->fr_width;
3285 if (extra_cols < 0)
3286 {
3287 /* reduce frame width, rightmost frame first */
3288 while (frp != NULL)
3289 {
3290 w = frame_minwidth(frp, NULL);
3291 if (frp->fr_width + extra_cols < w)
3292 {
3293 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003294 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296 else
3297 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003298 frame_new_width(frp, frp->fr_width + extra_cols,
3299 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 break;
3301 }
3302 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003303 {
3304 do
3305 frp = frp->fr_next;
3306 while (wfw && frp != NULL && frame_fixed_width(frp));
3307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003309 {
3310 do
3311 frp = frp->fr_prev;
3312 while (wfw && frp != NULL && frame_fixed_width(frp));
3313 }
3314 /* Increase "width" if we could not reduce enough frames. */
3315 if (frp == NULL)
3316 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318 }
3319 else if (extra_cols > 0)
3320 {
3321 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003322 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
3324 }
3325 topfrp->fr_width = width;
3326}
3327
3328/*
3329 * Add the vertical separator to windows at the right side of "frp".
3330 * Note: Does not check if there is room!
3331 */
3332 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003333frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 win_T *wp;
3336
3337 if (frp->fr_layout == FR_LEAF)
3338 {
3339 wp = frp->fr_win;
3340 if (wp->w_vsep_width == 0)
3341 {
3342 if (wp->w_width > 0) /* don't make it negative */
3343 --wp->w_width;
3344 wp->w_vsep_width = 1;
3345 }
3346 }
3347 else if (frp->fr_layout == FR_COL)
3348 {
3349 /* Handle all the frames in the column. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003350 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 frame_add_vsep(frp);
3352 }
3353 else /* frp->fr_layout == FR_ROW */
3354 {
3355 /* Only need to handle the last frame in the row. */
3356 frp = frp->fr_child;
3357 while (frp->fr_next != NULL)
3358 frp = frp->fr_next;
3359 frame_add_vsep(frp);
3360 }
3361}
3362
3363/*
3364 * Set frame width from the window it contains.
3365 */
3366 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003367frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
3372/*
3373 * Set frame height from the window it contains.
3374 */
3375 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003376frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003378 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380
3381/*
3382 * Compute the minimal height for frame "topfrp".
3383 * Uses the 'winminheight' option.
3384 * When "next_curwin" isn't NULL, use p_wh for this window.
3385 * When "next_curwin" is NOWIN, don't use at least one line for the current
3386 * window.
3387 */
3388 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003389frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390{
3391 frame_T *frp;
3392 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
3395 if (topfrp->fr_win != NULL)
3396 {
3397 if (topfrp->fr_win == next_curwin)
3398 m = p_wh + topfrp->fr_win->w_status_height;
3399 else
3400 {
3401 /* window: minimal height of the window plus status line */
3402 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003403 if (topfrp->fr_win == curwin && next_curwin == NULL)
3404 {
3405 /* Current window is minimal one line high and WinBar is
3406 * visible. */
3407 if (p_wmh == 0)
3408 ++m;
3409 m += WINBAR_HEIGHT(curwin);
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 }
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 else if (topfrp->fr_layout == FR_ROW)
3414 {
3415 /* get the minimal height from each frame in this row */
3416 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003417 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 {
3419 n = frame_minheight(frp, next_curwin);
3420 if (n > m)
3421 m = n;
3422 }
3423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 else
3425 {
3426 /* Add up the minimal heights for all frames in this column. */
3427 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003428 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 m += frame_minheight(frp, next_curwin);
3430 }
3431
3432 return m;
3433}
3434
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435/*
3436 * Compute the minimal width for frame "topfrp".
3437 * When "next_curwin" isn't NULL, use p_wiw for this window.
3438 * When "next_curwin" is NOWIN, don't use at least one column for the current
3439 * window.
3440 */
3441 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003442frame_minwidth(
3443 frame_T *topfrp,
3444 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 frame_T *frp;
3447 int m, n;
3448
3449 if (topfrp->fr_win != NULL)
3450 {
3451 if (topfrp->fr_win == next_curwin)
3452 m = p_wiw + topfrp->fr_win->w_vsep_width;
3453 else
3454 {
3455 /* window: minimal width of the window plus separator column */
3456 m = p_wmw + topfrp->fr_win->w_vsep_width;
3457 /* Current window is minimal one column wide */
3458 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3459 ++m;
3460 }
3461 }
3462 else if (topfrp->fr_layout == FR_COL)
3463 {
3464 /* get the minimal width from each frame in this column */
3465 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003466 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
3468 n = frame_minwidth(frp, next_curwin);
3469 if (n > m)
3470 m = n;
3471 }
3472 }
3473 else
3474 {
3475 /* Add up the minimal widths for all frames in this row. */
3476 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003477 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 m += frame_minwidth(frp, next_curwin);
3479 }
3480
3481 return m;
3482}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483
3484
3485/*
3486 * Try to close all windows except current one.
3487 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3488 * used and the buffer was modified.
3489 *
3490 * Used by ":bdel" and ":only".
3491 */
3492 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003493close_others(
3494 int message,
3495 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 win_T *wp;
3498 win_T *nextwp;
3499 int r;
3500
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003501 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003503 if (message && !autocmd_busy)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003504 msg(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 return;
3506 }
3507
3508 /* Be very careful here: autocommands may change the window layout. */
3509 for (wp = firstwin; win_valid(wp); wp = nextwp)
3510 {
3511 nextwp = wp->w_next;
3512 if (wp != curwin) /* don't close current window */
3513 {
3514
3515 /* Check if it's allowed to abandon this window */
3516 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 if (!win_valid(wp)) /* autocommands messed wp up */
3518 {
3519 nextwp = firstwin;
3520 continue;
3521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 if (!r)
3523 {
3524#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3525 if (message && (p_confirm || cmdmod.confirm) && p_write)
3526 {
3527 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (!win_valid(wp)) /* autocommands messed wp up */
3529 {
3530 nextwp = firstwin;
3531 continue;
3532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 if (bufIsChanged(wp->w_buffer))
3535#endif
3536 continue;
3537 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003538 win_close(wp, !buf_hide(wp->w_buffer)
3539 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 }
3541 }
3542
Bram Moolenaar459ca562016-11-10 18:16:33 +01003543 if (message && !ONE_WINDOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003544 emsg(_("E445: Other window contains changes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545}
3546
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003547 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003548win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003549{
3550 redraw_win_later(wp, NOT_VALID);
3551 wp->w_lines_valid = 0;
3552 wp->w_cursor.lnum = 1;
3553 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003554 wp->w_cursor.coladd = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003555 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3556 wp->w_pcmark.col = 0;
3557 wp->w_prev_pcmark.lnum = 0;
3558 wp->w_prev_pcmark.col = 0;
3559 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003561 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003563 wp->w_botline = 2;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003564#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003565 wp->w_s = &wp->w_buffer->b_s;
3566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567}
3568
3569/*
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003570 * Init the current window "curwin".
3571 * Called when a new file is being edited.
3572 */
3573 void
3574curwin_init(void)
3575{
3576 win_init_empty(curwin);
3577}
3578
3579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 * Allocate the first window and put an empty buffer in it.
3581 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003582 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003584 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003585win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003587 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003588 return FAIL;
3589
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003590 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003591 if (first_tabpage == NULL)
3592 return FAIL;
3593 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003594 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003595
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003596 return OK;
3597}
3598
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003599/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003600 * Allocate and init a window that is not a regular window.
3601 * This can only be done after the first window is fully initialized, thus it
3602 * can't be in win_alloc_first().
3603 */
3604 win_T *
3605win_alloc_popup_win(void)
3606{
3607 win_T *wp;
3608
3609 wp = win_alloc(NULL, TRUE);
3610 if (wp != NULL)
3611 {
3612 // We need to initialize options with something, using the current
3613 // window makes most sense.
3614 win_init_some(wp, curwin);
3615
3616 RESET_BINDING(wp);
3617 new_frame(wp);
3618 }
3619 return wp;
3620}
3621
3622/*
3623 * Initialize window "wp" to display buffer "buf".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003624 */
3625 void
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003626win_init_popup_win(win_T *wp, buf_T *buf)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003627{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003628 wp->w_buffer = buf;
3629 ++buf->b_nwindows;
3630 win_init_empty(wp); // set cursor and topline to safe values
3631
3632 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in
3633 // win_enter_ext().
3634 VIM_CLEAR(wp->w_localdir);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003635}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003636
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003637/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003638 * Allocate the first window or the first window in a new tab page.
3639 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003640 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003641 * Return FAIL when something goes wrong (out of memory).
3642 */
3643 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003644win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003646 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003647 if (oldwin == NULL)
3648 {
3649 /* Very first window, need to create an empty buffer for it and
3650 * initialize from scratch. */
3651 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3652 if (curwin == NULL || curbuf == NULL)
3653 return FAIL;
3654 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003655#ifdef FEAT_SYN_HL
3656 curwin->w_s = &(curbuf->b_s);
3657#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003658 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003659 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003660 curwin_init(); /* init current window */
3661 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003662 else
3663 {
3664 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003665 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003666
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003667 /* We don't want cursor- and scroll-binding in the first window. */
3668 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003671 new_frame(curwin);
3672 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003673 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003674 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003677
3678 return OK;
3679}
3680
3681/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003682 * Create a frame for window "wp".
3683 */
3684 static void
3685new_frame(win_T *wp)
3686{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003687 frame_T *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003688
3689 wp->w_frame = frp;
3690 if (frp != NULL)
3691 {
3692 frp->fr_layout = FR_LEAF;
3693 frp->fr_win = wp;
3694 }
3695}
3696
3697/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003698 * Initialize the window and frame size to the maximum.
3699 */
3700 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003701win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003702{
3703 firstwin->w_height = ROWS_AVAIL;
3704 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705 firstwin->w_width = Columns;
3706 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707}
3708
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003709/*
3710 * Allocate a new tabpage_T and init the values.
3711 * Returns NULL when out of memory.
3712 */
3713 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003714alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003715{
3716 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003717# ifdef FEAT_GUI
3718 int i;
3719# endif
3720
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003721
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003722 tp = ALLOC_CLEAR_ONE(tabpage_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02003723 if (tp == NULL)
3724 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003725
Bram Moolenaar429fa852013-04-15 12:27:36 +02003726# ifdef FEAT_EVAL
3727 /* init t: variables */
3728 tp->tp_vars = dict_alloc();
3729 if (tp->tp_vars == NULL)
3730 {
3731 vim_free(tp);
3732 return NULL;
3733 }
3734 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3735# endif
3736
3737# ifdef FEAT_GUI
3738 for (i = 0; i < 3; i++)
3739 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003740# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003741# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003742 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003743# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003744 tp->tp_ch_used = p_ch;
3745
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003746 return tp;
3747}
3748
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003749 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003750free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003751{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003752 int idx;
3753
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003754# ifdef FEAT_DIFF
3755 diff_clear(tp);
3756# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003757# ifdef FEAT_TEXT_PROP
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02003758 while (tp->tp_first_popupwin != NULL)
3759 popup_close_tabpage(tp, tp->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003760#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003761 for (idx = 0; idx < SNAP_COUNT; ++idx)
3762 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003763#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003764 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3765 hash_init(&tp->tp_vars->dv_hashtab);
3766 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003768
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003769 vim_free(tp->tp_localdir);
3770
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003771#ifdef FEAT_PYTHON
3772 python_tabpage_free(tp);
3773#endif
3774
3775#ifdef FEAT_PYTHON3
3776 python3_tabpage_free(tp);
3777#endif
3778
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003779 vim_free(tp);
3780}
3781
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003782/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003783 * Create a new Tab page with one window.
3784 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003785 * When "after" is 0 put it just after the current Tab page.
3786 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003787 * Return FAIL or OK.
3788 */
3789 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003790win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003791{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003792 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003793 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003794 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003795
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003796 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003797 if (newtp == NULL)
3798 return FAIL;
3799
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003800 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003801 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003802 {
3803 vim_free(newtp);
3804 return FAIL;
3805 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003806 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003807
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003808 newtp->tp_localdir = (tp->tp_localdir == NULL)
3809 ? NULL : vim_strsave(tp->tp_localdir);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003811 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003812 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003813 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003814 if (after == 1)
3815 {
3816 /* New tab page becomes the first one. */
3817 newtp->tp_next = first_tabpage;
3818 first_tabpage = newtp;
3819 }
3820 else
3821 {
3822 if (after > 0)
3823 {
3824 /* Put new tab page before tab page "after". */
3825 n = 2;
3826 for (tp = first_tabpage; tp->tp_next != NULL
3827 && n < after; tp = tp->tp_next)
3828 ++n;
3829 }
3830 newtp->tp_next = tp->tp_next;
3831 tp->tp_next = newtp;
3832 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003833 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003834 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003835 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003836
3837 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003838 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003839
3840#if defined(FEAT_GUI)
3841 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3842 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003843 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003844#endif
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003845#ifdef FEAT_JOB_CHANNEL
3846 entering_window(curwin);
3847#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003848
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01003849 redraw_all_later(NOT_VALID);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003850 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003851 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003852 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003853 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003854 return OK;
3855 }
3856
3857 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003858 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003859 return FAIL;
3860}
3861
3862/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003863 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3864 * like with ":split".
3865 * Returns OK if a new tab page was created, FAIL otherwise.
3866 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003867 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003868may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003869{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003870 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003871
Bram Moolenaard326ce82007-03-11 14:48:29 +00003872 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003873 {
3874 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003875 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003876 return win_new_tabpage(n);
3877 }
3878 return FAIL;
3879}
3880
3881/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003882 * Create up to "maxcount" tabpages with empty windows.
3883 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003884 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003885 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003886make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003887{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003888 int count = maxcount;
3889 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003890
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003891 /* Limit to 'tabpagemax' tabs. */
3892 if (count > p_tpm)
3893 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003894
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003895 /*
3896 * Don't execute autocommands while creating the tab pages. Must do that
3897 * when putting the buffers in the windows.
3898 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003899 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003900
3901 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003902 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003903 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003904
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003905 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003906
3907 /* return actual number of tab pages */
3908 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003909}
3910
3911/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003912 * Return TRUE when "tpc" points to a valid tab page.
3913 */
3914 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003915valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003916{
3917 tabpage_T *tp;
3918
Bram Moolenaar29323592016-07-24 22:04:11 +02003919 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003920 if (tp == tpc)
3921 return TRUE;
3922 return FALSE;
3923}
3924
3925/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003926 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3927 * valid.
3928 */
3929 int
3930valid_tabpage_win(tabpage_T *tpc)
3931{
3932 tabpage_T *tp;
3933 win_T *wp;
3934
3935 FOR_ALL_TABPAGES(tp)
3936 {
3937 if (tp == tpc)
3938 {
3939 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3940 {
3941 if (win_valid_any_tab(wp))
3942 return TRUE;
3943 }
3944 return FALSE;
3945 }
3946 }
3947 /* shouldn't happen */
3948 return FALSE;
3949}
3950
3951/*
3952 * Close tabpage "tab", assuming it has no windows in it.
3953 * There must be another tabpage or this will crash.
3954 */
3955 void
3956close_tabpage(tabpage_T *tab)
3957{
3958 tabpage_T *ptp;
3959
3960 if (tab == first_tabpage)
3961 {
3962 first_tabpage = tab->tp_next;
3963 ptp = first_tabpage;
3964 }
3965 else
3966 {
3967 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3968 ptp = ptp->tp_next)
3969 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003970 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003971 ptp->tp_next = tab->tp_next;
3972 }
3973
3974 goto_tabpage_tp(ptp, FALSE, FALSE);
3975 free_tabpage(tab);
3976}
3977
3978/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003979 * Find tab page "n" (first one is 1). Returns NULL when not found.
3980 */
3981 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003982find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003983{
3984 tabpage_T *tp;
3985 int i = 1;
3986
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003987 if (n == 0)
3988 return curtab;
3989
Bram Moolenaarf740b292006-02-16 22:11:02 +00003990 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3991 ++i;
3992 return tp;
3993}
3994
3995/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003996 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003997 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003998 */
3999 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004000tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004001{
4002 int i = 1;
4003 tabpage_T *tp;
4004
4005 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
4006 ++i;
4007 return i;
4008}
4009
4010/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004011 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004012 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004013 * FAIL.
4014 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004015 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004016 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004017leave_tabpage(
4018 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004019 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004020 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004021{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004022 tabpage_T *tp = curtab;
4023
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004024#ifdef FEAT_JOB_CHANNEL
4025 leaving_window(curwin);
4026#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004027 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004028 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004029 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004030 if (new_curbuf != curbuf)
4031 {
4032 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4033 if (curtab != tp)
4034 return FAIL;
4035 }
4036 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4037 if (curtab != tp)
4038 return FAIL;
4039 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004040 if (curtab != tp)
4041 return FAIL;
4042 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004043#if defined(FEAT_GUI)
4044 /* Remove the scrollbars. They may be added back later. */
4045 if (gui.in_use)
4046 gui_remove_scrollbars();
4047#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004048 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004049 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004050 tp->tp_firstwin = firstwin;
4051 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004052 tp->tp_old_Rows = Rows;
4053 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004054 firstwin = NULL;
4055 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004056 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004057}
4058
4059/*
4060 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004061 * Only to be used after leave_tabpage() or freeing the current tab page.
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 Moolenaar1d2ba7f2006-02-14 22:29:30 +00004064 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004065 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004066enter_tabpage(
4067 tabpage_T *tp,
4068 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01004069 int trigger_enter_autocmds,
4070 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004071{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004072 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004073 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004074
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004075 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004076 firstwin = tp->tp_firstwin;
4077 lastwin = tp->tp_lastwin;
4078 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004079
4080 /* We would like doing the TabEnter event first, but we don't have a
4081 * valid current window yet, which may break some commands.
4082 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02004083 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02004084 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00004085 prevwin = next_prevwin;
4086
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004087 last_status(FALSE); /* status line may appear or disappear */
4088 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004089#ifdef FEAT_DIFF
4090 diff_need_scrollbind = TRUE;
4091#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004092
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004093 /* The tabpage line may have appeared or disappeared, may need to resize
4094 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004095 * frame sizes too. Use the stored value of p_ch, so that it can be
4096 * different for each tab page. */
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +02004097 if (p_ch != curtab->tp_ch_used)
4098 clear_cmdline = TRUE;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004099 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004100 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
4101#ifdef FEAT_GUI_TABLINE
4102 && !gui_use_tabline()
4103#endif
4104 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004105 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004106 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004107 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004108
4109#if defined(FEAT_GUI)
4110 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
4111 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004112 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004113#endif
4114
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004115 /* Apply autocommands after updating the display, when 'rows' and
4116 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004117 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004118 {
4119 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
4120 if (old_curbuf != curbuf)
4121 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4122 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004123
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01004124 redraw_all_later(NOT_VALID);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004125}
4126
4127/*
4128 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004129 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004130 */
4131 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004132goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004133{
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004134 tabpage_T *tp = NULL; // shut up compiler
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004135 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004136 int i;
4137
Bram Moolenaard68071d2006-05-02 22:08:30 +00004138 if (text_locked())
4139 {
4140 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004141 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00004142 return;
4143 }
4144
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004145 /* If there is only one it can't work. */
4146 if (first_tabpage->tp_next == NULL)
4147 {
4148 if (n > 1)
4149 beep_flush();
4150 return;
4151 }
4152
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004153 if (n == 0)
4154 {
4155 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004156 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004157 tp = first_tabpage;
4158 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004159 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004160 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004161 else if (n < 0)
4162 {
4163 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4164 * this N times. */
4165 ttp = curtab;
4166 for (i = n; i < 0; ++i)
4167 {
4168 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4169 tp = tp->tp_next)
4170 ;
4171 ttp = tp;
4172 }
4173 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004174 else if (n == 9999)
4175 {
4176 /* Go to last tab page. */
4177 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4178 ;
4179 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004180 else
4181 {
4182 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004183 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004184 if (tp == NULL)
4185 {
4186 beep_flush();
4187 return;
4188 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004189 }
4190
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004191 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004192
4193#ifdef FEAT_GUI_TABLINE
4194 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004195 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004196#endif
4197}
4198
4199/*
4200 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004201 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4202 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004203 * Note: doesn't update the GUI tab.
4204 */
4205 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004206goto_tabpage_tp(
4207 tabpage_T *tp,
4208 int trigger_enter_autocmds,
4209 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004210{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004211 /* Don't repeat a message in another tab page. */
4212 set_keep_msg(NULL, 0);
4213
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004214 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4215 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004216 {
4217 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004218 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4219 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004220 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004221 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4222 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004223 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004227 * Enter window "wp" in tab page "tp".
4228 * Also updates the GUI tab.
4229 */
4230 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004231goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004232{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004233 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004234 if (curtab == tp && win_valid(wp))
4235 {
4236 win_enter(wp, TRUE);
4237# ifdef FEAT_GUI_TABLINE
4238 if (gui_use_tabline())
4239 gui_mch_set_curtab(tabpage_index(curtab));
4240# endif
4241 }
4242}
4243
4244/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004245 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004246 */
4247 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004248tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004249{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004250 int n = 1;
4251 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004252
4253 if (first_tabpage->tp_next == NULL)
4254 return;
4255
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004256 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4257 ++n;
4258
4259 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4260 && tp->tp_next == curtab))
4261 return;
4262
4263 tp_dst = tp;
4264
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004265 /* Remove the current tab page from the list of tab pages. */
4266 if (curtab == first_tabpage)
4267 first_tabpage = curtab->tp_next;
4268 else
4269 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004270 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004271 if (tp->tp_next == curtab)
4272 break;
4273 if (tp == NULL) /* "cannot happen" */
4274 return;
4275 tp->tp_next = curtab->tp_next;
4276 }
4277
4278 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004279 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004280 {
4281 curtab->tp_next = first_tabpage;
4282 first_tabpage = curtab;
4283 }
4284 else
4285 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004286 curtab->tp_next = tp_dst->tp_next;
4287 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004288 }
4289
4290 /* Need to redraw the tabline. Tab page contents doesn't change. */
4291 redraw_tabline = TRUE;
4292}
4293
4294
4295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 * Go to another window.
4297 * When jumping to another buffer, stop Visual mode. Do this before
4298 * changing windows so we can yank the selection into the '*' register.
4299 * When jumping to another window on the same buffer, adjust its cursor
4300 * position to keep the same Visual area.
4301 */
4302 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004303win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004305#ifdef FEAT_CONCEAL
4306 win_T *owp = curwin;
4307#endif
4308
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004309 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004310 return;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004311 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004314 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return;
4316 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004317 if (curbuf_locked())
4318 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 if (wp->w_buffer != curbuf)
4321 reset_VIsual_and_resel();
4322 else if (VIsual_active)
4323 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325#ifdef FEAT_GUI
4326 need_mouse_correct = TRUE;
4327#endif
4328 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004329
4330#ifdef FEAT_CONCEAL
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004331 // Conceal cursor line in previous window, unconceal in current window.
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004332 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004333 redrawWinline(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004334 if (curwin->w_p_cole > 0 && !msg_scrolled)
4335 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337}
4338
4339#if defined(FEAT_PERL) || defined(PROTO)
4340/*
4341 * Find window number "winnr" (counting top to bottom).
4342 */
4343 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004344win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345{
4346 win_T *wp;
4347
Bram Moolenaar29323592016-07-24 22:04:11 +02004348 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 if (--winnr == 0)
4350 break;
4351 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352}
4353#endif
4354
Bram Moolenaar4033c552017-09-16 20:54:51 +02004355#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004356/*
4357 * Find the tabpage for window "win".
4358 */
4359 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004360win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004361{
4362 win_T *wp;
4363 tabpage_T *tp;
4364
Bram Moolenaar29323592016-07-24 22:04:11 +02004365 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004366 if (wp == win)
4367 return tp;
4368 return NULL;
4369}
4370#endif
4371
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004373 * Get the above or below neighbor window of the specified window.
4374 * up - TRUE for the above neighbor
4375 * count - nth neighbor window
4376 * Returns the specified window if the neighbor is not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004378 win_T *
4379win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380{
4381 frame_T *fr;
4382 frame_T *nfr;
4383 frame_T *foundfr;
4384
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004385 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 while (count--)
4387 {
4388 /*
4389 * First go upwards in the tree of frames until we find a upwards or
4390 * downwards neighbor.
4391 */
4392 fr = foundfr;
4393 for (;;)
4394 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004395 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 goto end;
4397 if (up)
4398 nfr = fr->fr_prev;
4399 else
4400 nfr = fr->fr_next;
4401 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4402 break;
4403 fr = fr->fr_parent;
4404 }
4405
4406 /*
4407 * Now go downwards to find the bottom or top frame in it.
4408 */
4409 for (;;)
4410 {
4411 if (nfr->fr_layout == FR_LEAF)
4412 {
4413 foundfr = nfr;
4414 break;
4415 }
4416 fr = nfr->fr_child;
4417 if (nfr->fr_layout == FR_ROW)
4418 {
4419 /* Find the frame at the cursor row. */
4420 while (fr->fr_next != NULL
4421 && frame2win(fr)->w_wincol + fr->fr_width
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004422 <= wp->w_wincol + wp->w_wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 fr = fr->fr_next;
4424 }
4425 if (nfr->fr_layout == FR_COL && up)
4426 while (fr->fr_next != NULL)
4427 fr = fr->fr_next;
4428 nfr = fr;
4429 }
4430 }
4431end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004432 return foundfr != NULL ? foundfr->fr_win : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433}
4434
4435/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004436 * Move to window above or below "count" times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 */
4438 static void
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004439win_goto_ver(
4440 int up, // TRUE to go to win above
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004441 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442{
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004443 win_T *win;
4444
4445 win = win_vert_neighbor(curtab, curwin, up, count);
4446 if (win != NULL)
4447 win_goto(win);
4448}
4449
4450/*
4451 * Get the left or right neighbor window of the specified window.
4452 * left - TRUE for the left neighbor
4453 * count - nth neighbor window
4454 * Returns the specified window if the neighbor is not found.
4455 */
4456 win_T *
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02004457win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count)
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004458{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 frame_T *fr;
4460 frame_T *nfr;
4461 frame_T *foundfr;
4462
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004463 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 while (count--)
4465 {
4466 /*
4467 * First go upwards in the tree of frames until we find a left or
4468 * right neighbor.
4469 */
4470 fr = foundfr;
4471 for (;;)
4472 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004473 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 goto end;
4475 if (left)
4476 nfr = fr->fr_prev;
4477 else
4478 nfr = fr->fr_next;
4479 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4480 break;
4481 fr = fr->fr_parent;
4482 }
4483
4484 /*
4485 * Now go downwards to find the leftmost or rightmost frame in it.
4486 */
4487 for (;;)
4488 {
4489 if (nfr->fr_layout == FR_LEAF)
4490 {
4491 foundfr = nfr;
4492 break;
4493 }
4494 fr = nfr->fr_child;
4495 if (nfr->fr_layout == FR_COL)
4496 {
4497 /* Find the frame at the cursor row. */
4498 while (fr->fr_next != NULL
4499 && frame2win(fr)->w_winrow + fr->fr_height
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004500 <= wp->w_winrow + wp->w_wrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 fr = fr->fr_next;
4502 }
4503 if (nfr->fr_layout == FR_ROW && left)
4504 while (fr->fr_next != NULL)
4505 fr = fr->fr_next;
4506 nfr = fr;
4507 }
4508 }
4509end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004510 return foundfr != NULL ? foundfr->fr_win : NULL;
4511}
4512
4513/*
4514 * Move to left or right window.
4515 */
4516 static void
4517win_goto_hor(
4518 int left, // TRUE to go to left win
4519 long count)
4520{
4521 win_T *win;
4522
4523 win = win_horz_neighbor(curtab, curwin, left, count);
4524 if (win != NULL)
4525 win_goto(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527
4528/*
4529 * Make window "wp" the current window.
4530 */
4531 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004532win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004534 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535}
4536
4537/*
4538 * Make window wp the current window.
4539 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4540 * been closed and isn't valid.
4541 */
4542 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004543win_enter_ext(
4544 win_T *wp,
4545 int undo_sync,
4546 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004547 int trigger_new_autocmds,
4548 int trigger_enter_autocmds,
4549 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 if (wp == curwin && !curwin_invalid) /* nothing to do */
4554 return;
4555
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004556#ifdef FEAT_JOB_CHANNEL
4557 if (!curwin_invalid)
4558 leaving_window(curwin);
4559#endif
4560
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004561 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 /*
4564 * Be careful: If autocommands delete the window, return now.
4565 */
4566 if (wp->w_buffer != curbuf)
4567 {
4568 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4569 other_buffer = TRUE;
4570 if (!win_valid(wp))
4571 return;
4572 }
4573 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4574 if (!win_valid(wp))
4575 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004576#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 /* autocmds may abort script processing */
4578 if (aborting())
4579 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582
4583 /* sync undo before leaving the current buffer */
4584 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004585 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004586
4587 /* Might need to scroll the old window before switching, e.g., when the
4588 * cursor was moved. */
4589 update_topline();
4590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 /* may have to copy the buffer options when 'cpo' contains 'S' */
4592 if (wp->w_buffer != curbuf)
4593 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4594 if (!curwin_invalid)
4595 {
4596 prevwin = curwin; /* remember for CTRL-W p */
4597 curwin->w_redr_status = TRUE;
4598 }
4599 curwin = wp;
4600 curbuf = wp->w_buffer;
4601 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 if (!virtual_active())
4603 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 changed_line_abv_curs(); /* assume cursor position needs updating */
4605
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004606 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004608 char_u *dirname;
4609
4610 // Window or tab has a local directory: Save current directory as
4611 // global directory (unless that was done already) and change to the
4612 // local directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if (globaldir == NULL)
4614 {
4615 char_u cwd[MAXPATHL];
4616
4617 if (mch_dirname(cwd, MAXPATHL) == OK)
4618 globaldir = vim_strsave(cwd);
4619 }
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004620 if (curwin->w_localdir != NULL)
4621 dirname = curwin->w_localdir;
4622 else
4623 dirname = curtab->tp_localdir;
4624
4625 if (mch_chdir((char *)dirname) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004626 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 else if (globaldir != NULL)
4629 {
4630 /* Window doesn't have a local directory and we are not in the global
4631 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004632 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004633 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 shorten_fnames(TRUE);
4635 }
4636
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004637#ifdef FEAT_JOB_CHANNEL
4638 entering_window(curwin);
4639#endif
Bram Moolenaarc917da42016-07-19 22:31:36 +02004640 if (trigger_new_autocmds)
4641 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004642 if (trigger_enter_autocmds)
4643 {
4644 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4645 if (other_buffer)
4646 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649#ifdef FEAT_TITLE
4650 maketitle();
4651#endif
4652 curwin->w_redr_status = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004653 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 if (restart_edit)
4655 redraw_later(VALID); /* causes status line redraw */
4656
4657 /* set window height to desired minimal value */
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02004658 if (curwin->w_height < p_wh && !curwin->w_p_wfh
4659#ifdef FEAT_TEXT_PROP
4660 && !popup_is_popup(curwin)
4661#endif
4662 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 win_setheight((int)p_wh);
4664 else if (curwin->w_height == 0)
4665 win_setheight(1);
4666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004668 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
4671#ifdef FEAT_MOUSE
4672 setmouse(); /* in case jumped to/from help buffer */
4673#endif
4674
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004675 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004676 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677}
4678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004681 * Jump to the first open window that contains buffer "buf", if one exists.
4682 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 */
4684 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004685buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004687 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004689 if (curwin->w_buffer == buf)
4690 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004691 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004692 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004693 if (wp->w_buffer == buf)
4694 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 if (wp != NULL)
4696 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004697 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004699
4700/*
4701 * Jump to the first open window in any tab page that contains buffer "buf",
4702 * if one exists.
4703 * Returns a pointer to the window found, otherwise NULL.
4704 */
4705 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004706buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004707{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004708 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004709 tabpage_T *tp;
4710
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004711 if (wp != NULL)
4712 return wp;
4713
Bram Moolenaar29323592016-07-24 22:04:11 +02004714 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004715 if (tp != curtab)
4716 {
4717 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4718 if (wp->w_buffer == buf)
4719 break;
4720 if (wp != NULL)
4721 {
4722 goto_tabpage_win(tp, wp);
4723 if (curwin != wp)
4724 wp = NULL; /* something went wrong */
4725 break;
4726 }
4727 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004728 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004729}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004731static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004734 * Allocate a window structure and link it in the window list when "hidden" is
4735 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004738win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004740 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741
4742 /*
4743 * allocate window structure and linesizes arrays
4744 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004745 new_wp = ALLOC_CLEAR_ONE(win_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004746 if (new_wp == NULL)
4747 return NULL;
4748
4749 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004751 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004752 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754
Bram Moolenaar86edef62016-03-13 18:07:30 +01004755 new_wp->w_id = ++last_win_id;
4756
Bram Moolenaar429fa852013-04-15 12:27:36 +02004757#ifdef FEAT_EVAL
4758 /* init w: variables */
4759 new_wp->w_vars = dict_alloc();
4760 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004762 win_free_lsize(new_wp);
4763 vim_free(new_wp);
4764 return NULL;
4765 }
4766 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004767#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004768
Bram Moolenaar429fa852013-04-15 12:27:36 +02004769 /* Don't execute autocommands while the window is not properly
4770 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4771 * event. */
4772 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004773
Bram Moolenaar429fa852013-04-15 12:27:36 +02004774 /*
4775 * link the window in the window list
4776 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004777 if (!hidden)
4778 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004779 new_wp->w_wincol = 0;
4780 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar429fa852013-04-15 12:27:36 +02004782 /* position the display and the cursor at the top of the file. */
4783 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004785 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004787 new_wp->w_botline = 2;
4788 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004789 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
Bram Moolenaar375e3392019-01-31 18:26:10 +01004791 // use global option value for global-local options
4792 new_wp->w_p_so = -1;
4793 new_wp->w_p_siso = -1;
4794
Bram Moolenaar429fa852013-04-15 12:27:36 +02004795 /* We won't calculate w_fraction until resizing the window */
4796 new_wp->w_fraction = 0;
4797 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
4799#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004800 if (gui.in_use)
4801 {
4802 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4803 SBAR_LEFT, new_wp);
4804 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4805 SBAR_RIGHT, new_wp);
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807#endif
4808#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004809 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004811 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004812#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004813 new_wp->w_match_head = NULL;
4814 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004815#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004816 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817}
4818
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004820 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 */
4822 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004823win_free(
4824 win_T *wp,
4825 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
4827 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004828 buf_T *buf;
4829 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004831#ifdef FEAT_FOLDING
4832 clearFolding(wp);
4833#endif
4834
4835 /* reduce the reference count to the argument list. */
4836 alist_unlink(wp->w_alist);
4837
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004838 /* Don't execute autocommands while the window is halfway being deleted.
4839 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004840 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004841
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004842#ifdef FEAT_LUA
4843 lua_window_free(wp);
4844#endif
4845
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004846#ifdef FEAT_MZSCHEME
4847 mzscheme_window_free(wp);
4848#endif
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850#ifdef FEAT_PERL
4851 perl_win_free(wp);
4852#endif
4853
4854#ifdef FEAT_PYTHON
4855 python_window_free(wp);
4856#endif
4857
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004858#ifdef FEAT_PYTHON3
4859 python3_window_free(wp);
4860#endif
4861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#ifdef FEAT_TCL
4863 tcl_window_free(wp);
4864#endif
4865
4866#ifdef FEAT_RUBY
4867 ruby_window_free(wp);
4868#endif
4869
4870 clear_winopt(&wp->w_onebuf_opt);
4871 clear_winopt(&wp->w_allbuf_opt);
4872
4873#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004874 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4875 hash_init(&wp->w_vars->dv_hashtab);
4876 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877#endif
4878
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004879 {
4880 tabpage_T *ttp;
4881
4882 if (prevwin == wp)
4883 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004884 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004885 if (ttp->tp_prevwin == wp)
4886 ttp->tp_prevwin = NULL;
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 win_free_lsize(wp);
4889
4890 for (i = 0; i < wp->w_tagstacklen; ++i)
4891 vim_free(wp->w_tagstack[i].tagname);
4892
4893 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004894
Bram Moolenaarff18df02013-07-24 17:51:57 +02004895 /* Remove the window from the b_wininfo lists, it may happen that the
4896 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004897 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004898 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4899 if (wip->wi_win == wp)
4900 wip->wi_win = NULL;
4901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004903 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004905
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906#ifdef FEAT_JUMPLIST
4907 free_jumplist(wp);
4908#endif
4909
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004910#ifdef FEAT_QUICKFIX
4911 qf_free_all(wp);
4912#endif
4913
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914#ifdef FEAT_GUI
4915 if (gui.in_use)
4916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4918 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4919 }
4920#endif /* FEAT_GUI */
4921
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004922#ifdef FEAT_MENU
4923 remove_winbar(wp);
4924#endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004925#ifdef FEAT_TEXT_PROP
Bram Moolenaar9eaac892019-06-01 22:49:29 +02004926 free_callback(&wp->w_close_cb);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004927 free_callback(&wp->w_filter_cb);
Bram Moolenaar790498b2019-06-01 22:15:29 +02004928 for (i = 0; i < 4; ++i)
4929 VIM_CLEAR(wp->w_border_highlight[i]);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004930 vim_free(wp->w_scrollbar_highlight);
4931 vim_free(wp->w_thumb_highlight);
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004932 vim_free(wp->w_popup_title);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004933 list_unref(wp->w_popup_mask);
Bram Moolenaar0aca2932019-07-26 22:22:38 +02004934 vim_free(wp->w_popup_mask_cells);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004935#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004936
Bram Moolenaar860cae12010-06-05 23:22:07 +02004937#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004938 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004939#endif
4940
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004941 if (win_valid_any_tab(wp))
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004942 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004943 if (autocmd_busy)
4944 {
4945 wp->w_next = au_pending_free_win;
4946 au_pending_free_win = wp;
4947 }
4948 else
4949 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004950
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004951 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952}
4953
4954/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004955 * Return TRUE if "wp" is not in the list of windows: the autocmd window or a
4956 * popup window.
4957 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004958 static int
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004959win_unlisted(win_T *wp)
4960{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004961 return wp == aucmd_win || WIN_IS_POPUP(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004962}
4963
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004964#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004965/*
4966 * Free a popup window. This does not take the window out of the window list
4967 * and assumes there is only one toplevel frame, no split.
4968 */
4969 void
4970win_free_popup(win_T *win)
4971{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004972 if (bt_popup(win->w_buffer))
4973 win_close_buffer(win, DOBUF_WIPE, FALSE);
4974 else
4975 close_buffer(win, win->w_buffer, 0, FALSE);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004976# if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02004977 if (win->w_popup_timer != NULL)
4978 stop_timer(win->w_popup_timer);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004979# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004980 vim_free(win->w_frame);
4981 win_free(win, NULL);
4982}
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004983#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004984
4985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 * Append window "wp" in the window list after window "after".
4987 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004988 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004989win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
4991 win_T *before;
4992
4993 if (after == NULL) /* after NULL is in front of the first */
4994 before = firstwin;
4995 else
4996 before = after->w_next;
4997
4998 wp->w_next = before;
4999 wp->w_prev = after;
5000 if (after == NULL)
5001 firstwin = wp;
5002 else
5003 after->w_next = wp;
5004 if (before == NULL)
5005 lastwin = wp;
5006 else
5007 before->w_prev = wp;
5008}
5009
5010/*
5011 * Remove a window from the window list.
5012 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00005013 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005014win_remove(
5015 win_T *wp,
5016 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
5018 if (wp->w_prev != NULL)
5019 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005020 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02005021 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005022 else
5023 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02005024
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (wp->w_next != NULL)
5026 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005027 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02005028 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005029 else
5030 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031}
5032
5033/*
5034 * Append frame "frp" in a frame list after frame "after".
5035 */
5036 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005037frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 frp->fr_next = after->fr_next;
5040 after->fr_next = frp;
5041 if (frp->fr_next != NULL)
5042 frp->fr_next->fr_prev = frp;
5043 frp->fr_prev = after;
5044}
5045
5046/*
5047 * Insert frame "frp" in a frame list before frame "before".
5048 */
5049 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005050frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
5052 frp->fr_next = before;
5053 frp->fr_prev = before->fr_prev;
5054 before->fr_prev = frp;
5055 if (frp->fr_prev != NULL)
5056 frp->fr_prev->fr_next = frp;
5057 else
5058 frp->fr_parent->fr_child = frp;
5059}
5060
5061/*
5062 * Remove a frame from a frame list.
5063 */
5064 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005065frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066{
5067 if (frp->fr_prev != NULL)
5068 frp->fr_prev->fr_next = frp->fr_next;
5069 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01005070 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01005072 /* special case: topframe->fr_child == frp */
5073 if (topframe->fr_child == frp)
5074 topframe->fr_child = frp->fr_next;
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 if (frp->fr_next != NULL)
5077 frp->fr_next->fr_prev = frp->fr_prev;
5078}
5079
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080/*
5081 * Allocate w_lines[] for window "wp".
5082 * Return FAIL for failure, OK for success.
5083 */
5084 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005085win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 wp->w_lines_valid = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005088 wp->w_lines = ALLOC_CLEAR_MULT(wline_T, Rows );
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 if (wp->w_lines == NULL)
5090 return FAIL;
5091 return OK;
5092}
5093
5094/*
5095 * free lsize arrays for a window
5096 */
5097 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005098win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02005100 /* TODO: why would wp be NULL here? */
5101 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005102 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103}
5104
5105/*
5106 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005107 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 */
5109 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005110shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005112 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 if (firstwin == NULL) /* not initialized yet */
5115 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (h < frame_minheight(topframe, NULL))
5117 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005118
5119 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 * that doesn't result in the right height, forget about that option. */
5121 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005122 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 frame_new_height(topframe, h, FALSE, FALSE);
5124
5125 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005127 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005128
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129#if 0
5130 /* Disabled: don't want making the screen smaller make a window larger. */
5131 if (p_ea)
5132 win_equal(curwin, FALSE, 'v');
5133#endif
5134}
5135
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136/*
5137 * Called from win_new_shellsize() after Columns changed.
5138 */
5139 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005140shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
5142 if (firstwin == NULL) /* not initialized yet */
5143 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005144
5145 /* First try setting the widths of windows with 'winfixwidth'. If that
5146 * doesn't result in the right width, forget about that option. */
5147 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005148 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005149 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
5150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
5152#if 0
5153 /* Disabled: don't want making the screen smaller make a window larger. */
5154 if (p_ea)
5155 win_equal(curwin, FALSE, 'h');
5156#endif
5157}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158
5159#if defined(FEAT_CMDWIN) || defined(PROTO)
5160/*
5161 * Save the size of all windows in "gap".
5162 */
5163 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005164win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165
5166{
5167 win_T *wp;
5168
5169 ga_init2(gap, (int)sizeof(int), 1);
5170 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02005171 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
5173 ((int *)gap->ga_data)[gap->ga_len++] =
5174 wp->w_width + wp->w_vsep_width;
5175 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5176 }
5177}
5178
5179/*
5180 * Restore window sizes, but only if the number of windows is still the same.
5181 * Does not free the growarray.
5182 */
5183 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005184win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
5186 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005187 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 if (win_count() * 2 == gap->ga_len)
5190 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005191 /* The order matters, because frames contain other frames, but it's
5192 * difficult to get right. The easy way out is to do it twice. */
5193 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005195 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02005196 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02005197 {
5198 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5199 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 /* recompute the window positions */
5203 (void)win_comp_pos();
5204 }
5205}
5206#endif /* FEAT_CMDWIN */
5207
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208/*
5209 * Update the position for all windows, using the width and height of the
5210 * frames.
5211 * Returns the row just after the last window.
5212 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005213 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005214win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005216 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 int col = 0;
5218
5219 frame_comp_pos(topframe, &row, &col);
5220 return row;
5221}
5222
5223/*
5224 * Update the position of the windows in frame "topfrp", using the width and
5225 * height of the frames.
5226 * "*row" and "*col" are the top-left position of the frame. They are updated
5227 * to the bottom-right position plus one.
5228 */
5229 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005230frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231{
5232 win_T *wp;
5233 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 int startcol;
5235 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005236 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237
5238 wp = topfrp->fr_win;
5239 if (wp != NULL)
5240 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005241 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 {
5243 /* position changed, redraw */
5244 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 redraw_win_later(wp, NOT_VALID);
5247 wp->w_redr_status = TRUE;
5248 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005249 /* WinBar will not show if the window height is zero */
5250 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5251 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 }
5254 else
5255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 startrow = *row;
5257 startcol = *col;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005258 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 if (topfrp->fr_layout == FR_ROW)
5261 *row = startrow; /* all frames are at the same row */
5262 else
5263 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 frame_comp_pos(frp, row, col);
5265 }
5266 }
5267}
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269/*
5270 * Set current window height and take care of repositioning other windows to
5271 * fit around it.
5272 */
5273 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005274win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275{
5276 win_setheight_win(height, curwin);
5277}
5278
5279/*
5280 * Set the window height of window "win" and take care of repositioning other
5281 * windows to fit around it.
5282 */
5283 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005284win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285{
5286 int row;
5287
5288 if (win == curwin)
5289 {
5290 /* Always keep current window at least one line high, even when
5291 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 if (height < p_wmh)
5293 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (height == 0)
5295 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005296 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 }
5298
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 frame_setheight(win->w_frame, height + win->w_status_height);
5300
5301 /* recompute the window positions */
5302 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 /*
5305 * If there is extra space created between the last window and the command
5306 * line, clear it.
5307 */
5308 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5309 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5310 cmdline_row = row;
5311 msg_row = row;
5312 msg_col = 0;
5313
5314 redraw_all_later(NOT_VALID);
5315}
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317/*
5318 * Set the height of a frame to "height" and take care that all frames and
5319 * windows inside it are resized. Also resize frames on the left and right if
5320 * the are in the same FR_ROW frame.
5321 *
5322 * Strategy:
5323 * If the frame is part of a FR_COL frame, try fitting the frame in that
5324 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5325 * go to containing frames to resize them and make room.
5326 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5327 * Check for the minimal height of the FR_ROW frame.
5328 * At the top level we can also use change the command line height.
5329 */
5330 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005331frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332{
5333 int room; /* total number of lines available */
5334 int take; /* number of lines taken from other windows */
5335 int room_cmdline; /* lines available from cmdline */
5336 int run;
5337 frame_T *frp;
5338 int h;
5339 int room_reserved;
5340
5341 /* If the height already is the desired value, nothing to do. */
5342 if (curfrp->fr_height == height)
5343 return;
5344
5345 if (curfrp->fr_parent == NULL)
5346 {
5347 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005348 if (height > ROWS_AVAIL)
5349 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 if (height > 0)
5351 frame_new_height(curfrp, height, FALSE, FALSE);
5352 }
5353 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5354 {
5355 /* Row of frames: Also need to resize frames left and right of this
5356 * one. First check for the minimal height of these. */
5357 h = frame_minheight(curfrp->fr_parent, NULL);
5358 if (height < h)
5359 height = h;
5360 frame_setheight(curfrp->fr_parent, height);
5361 }
5362 else
5363 {
5364 /*
5365 * Column of frames: try to change only frames in this column.
5366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 /*
5368 * Do this twice:
5369 * 1: compute room available, if it's not enough try resizing the
5370 * containing frame.
5371 * 2: compute the room available and adjust the height to it.
5372 * Try not to reduce the height of a window with 'winfixheight' set.
5373 */
5374 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
5376 room = 0;
5377 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005378 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 if (frp != curfrp
5381 && frp->fr_win != NULL
5382 && frp->fr_win->w_p_wfh)
5383 room_reserved += frp->fr_height;
5384 room += frp->fr_height;
5385 if (frp != curfrp)
5386 room -= frame_minheight(frp, NULL);
5387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 if (curfrp->fr_width != Columns)
5389 room_cmdline = 0;
5390 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
5392 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005393 + VISIBLE_HEIGHT(lastwin)
5394 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 if (room_cmdline < 0)
5396 room_cmdline = 0;
5397 }
5398
5399 if (height <= room + room_cmdline)
5400 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
5403 if (height > room + room_cmdline)
5404 height = room + room_cmdline;
5405 break;
5406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 frame_setheight(curfrp->fr_parent, height
5408 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 }
5410
5411 /*
5412 * Compute the number of lines we will take from others frames (can be
5413 * negative!).
5414 */
5415 take = height - curfrp->fr_height;
5416
5417 /* If there is not enough room, also reduce the height of a window
5418 * with 'winfixheight' set. */
5419 if (height > room + room_cmdline - room_reserved)
5420 room_reserved = room + room_cmdline - height;
5421 /* If there is only a 'winfixheight' window and making the
5422 * window smaller, need to make the other window taller. */
5423 if (take < 0 && room - curfrp->fr_height < room_reserved)
5424 room_reserved = 0;
5425
5426 if (take > 0 && room_cmdline > 0)
5427 {
5428 /* use lines from cmdline first */
5429 if (take < room_cmdline)
5430 room_cmdline = take;
5431 take -= room_cmdline;
5432 topframe->fr_height += room_cmdline;
5433 }
5434
5435 /*
5436 * set the current frame to the new height
5437 */
5438 frame_new_height(curfrp, height, FALSE, FALSE);
5439
5440 /*
5441 * First take lines from the frames after the current frame. If
5442 * that is not enough, takes lines from frames above the current
5443 * frame.
5444 */
5445 for (run = 0; run < 2; ++run)
5446 {
5447 if (run == 0)
5448 frp = curfrp->fr_next; /* 1st run: start with next window */
5449 else
5450 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5451 while (frp != NULL && take != 0)
5452 {
5453 h = frame_minheight(frp, NULL);
5454 if (room_reserved > 0
5455 && frp->fr_win != NULL
5456 && frp->fr_win->w_p_wfh)
5457 {
5458 if (room_reserved >= frp->fr_height)
5459 room_reserved -= frp->fr_height;
5460 else
5461 {
5462 if (frp->fr_height - room_reserved > take)
5463 room_reserved = frp->fr_height - take;
5464 take -= frp->fr_height - room_reserved;
5465 frame_new_height(frp, room_reserved, FALSE, FALSE);
5466 room_reserved = 0;
5467 }
5468 }
5469 else
5470 {
5471 if (frp->fr_height - take < h)
5472 {
5473 take -= frp->fr_height - h;
5474 frame_new_height(frp, h, FALSE, FALSE);
5475 }
5476 else
5477 {
5478 frame_new_height(frp, frp->fr_height - take,
5479 FALSE, FALSE);
5480 take = 0;
5481 }
5482 }
5483 if (run == 0)
5484 frp = frp->fr_next;
5485 else
5486 frp = frp->fr_prev;
5487 }
5488 }
5489 }
5490}
5491
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492/*
5493 * Set current window width and take care of repositioning other windows to
5494 * fit around it.
5495 */
5496 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005497win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498{
5499 win_setwidth_win(width, curwin);
5500}
5501
5502 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005503win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 /* Always keep current window at least one column wide, even when
5506 * 'winminwidth' is zero. */
5507 if (wp == curwin)
5508 {
5509 if (width < p_wmw)
5510 width = p_wmw;
5511 if (width == 0)
5512 width = 1;
5513 }
5514
5515 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5516
5517 /* recompute the window positions */
5518 (void)win_comp_pos();
5519
5520 redraw_all_later(NOT_VALID);
5521}
5522
5523/*
5524 * Set the width of a frame to "width" and take care that all frames and
5525 * windows inside it are resized. Also resize frames above and below if the
5526 * are in the same FR_ROW frame.
5527 *
5528 * Strategy is similar to frame_setheight().
5529 */
5530 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005531frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 int room; /* total number of lines available */
5534 int take; /* number of lines taken from other windows */
5535 int run;
5536 frame_T *frp;
5537 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005538 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 /* If the width already is the desired value, nothing to do. */
5541 if (curfrp->fr_width == width)
5542 return;
5543
5544 if (curfrp->fr_parent == NULL)
5545 /* topframe: can't change width */
5546 return;
5547
5548 if (curfrp->fr_parent->fr_layout == FR_COL)
5549 {
5550 /* Column of frames: Also need to resize frames above and below of
5551 * this one. First check for the minimal width of these. */
5552 w = frame_minwidth(curfrp->fr_parent, NULL);
5553 if (width < w)
5554 width = w;
5555 frame_setwidth(curfrp->fr_parent, width);
5556 }
5557 else
5558 {
5559 /*
5560 * Row of frames: try to change only frames in this row.
5561 *
5562 * Do this twice:
5563 * 1: compute room available, if it's not enough try resizing the
5564 * containing frame.
5565 * 2: compute the room available and adjust the width to it.
5566 */
5567 for (run = 1; run <= 2; ++run)
5568 {
5569 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005570 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005571 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005573 if (frp != curfrp
5574 && frp->fr_win != NULL
5575 && frp->fr_win->w_p_wfw)
5576 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 room += frp->fr_width;
5578 if (frp != curfrp)
5579 room -= frame_minwidth(frp, NULL);
5580 }
5581
5582 if (width <= room)
5583 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005584 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
5586 if (width > room)
5587 width = room;
5588 break;
5589 }
5590 frame_setwidth(curfrp->fr_parent, width
5591 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5592 }
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 /*
5595 * Compute the number of lines we will take from others frames (can be
5596 * negative!).
5597 */
5598 take = width - curfrp->fr_width;
5599
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005600 /* If there is not enough room, also reduce the width of a window
5601 * with 'winfixwidth' set. */
5602 if (width > room - room_reserved)
5603 room_reserved = room - width;
5604 /* If there is only a 'winfixwidth' window and making the
5605 * window smaller, need to make the other window narrower. */
5606 if (take < 0 && room - curfrp->fr_width < room_reserved)
5607 room_reserved = 0;
5608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 /*
5610 * set the current frame to the new width
5611 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005612 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 /*
5615 * First take lines from the frames right of the current frame. If
5616 * that is not enough, takes lines from frames left of the current
5617 * frame.
5618 */
5619 for (run = 0; run < 2; ++run)
5620 {
5621 if (run == 0)
5622 frp = curfrp->fr_next; /* 1st run: start with next window */
5623 else
5624 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5625 while (frp != NULL && take != 0)
5626 {
5627 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005628 if (room_reserved > 0
5629 && frp->fr_win != NULL
5630 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005632 if (room_reserved >= frp->fr_width)
5633 room_reserved -= frp->fr_width;
5634 else
5635 {
5636 if (frp->fr_width - room_reserved > take)
5637 room_reserved = frp->fr_width - take;
5638 take -= frp->fr_width - room_reserved;
5639 frame_new_width(frp, room_reserved, FALSE, FALSE);
5640 room_reserved = 0;
5641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 }
5643 else
5644 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005645 if (frp->fr_width - take < w)
5646 {
5647 take -= frp->fr_width - w;
5648 frame_new_width(frp, w, FALSE, FALSE);
5649 }
5650 else
5651 {
5652 frame_new_width(frp, frp->fr_width - take,
5653 FALSE, FALSE);
5654 take = 0;
5655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 }
5657 if (run == 0)
5658 frp = frp->fr_next;
5659 else
5660 frp = frp->fr_prev;
5661 }
5662 }
5663 }
5664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
5666/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005667 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 */
5669 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005670win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
5672 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005673 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005676 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 while (p_wmh > 0)
5678 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005679 room = Rows - p_ch;
5680 needed = frame_minheight(topframe, NULL);
5681 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 break;
5683 --p_wmh;
5684 if (first)
5685 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005686 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 first = FALSE;
5688 }
5689 }
5690}
5691
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005692/*
5693 * Check 'winminwidth' for a valid value and reduce it if needed.
5694 */
5695 void
5696win_setminwidth(void)
5697{
5698 int room;
5699 int needed;
5700 int first = TRUE;
5701
5702 // loop until there is a 'winminheight' that is possible
5703 while (p_wmw > 0)
5704 {
5705 room = Columns;
5706 needed = frame_minwidth(topframe, NULL);
5707 if (room >= needed)
5708 break;
5709 --p_wmw;
5710 if (first)
5711 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005712 emsg(_(e_noroom));
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005713 first = FALSE;
5714 }
5715 }
5716}
5717
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005718#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720/*
5721 * Status line of dragwin is dragged "offset" lines down (negative is up).
5722 */
5723 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005724win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 frame_T *curfr;
5727 frame_T *fr;
5728 int room;
5729 int row;
5730 int up; /* if TRUE, drag status line up, otherwise down */
5731 int n;
5732
5733 fr = dragwin->w_frame;
5734 curfr = fr;
5735 if (fr != topframe) /* more than one window */
5736 {
5737 fr = fr->fr_parent;
5738 /* When the parent frame is not a column of frames, its parent should
5739 * be. */
5740 if (fr->fr_layout != FR_COL)
5741 {
5742 curfr = fr;
5743 if (fr != topframe) /* only a row of windows, may drag statusline */
5744 fr = fr->fr_parent;
5745 }
5746 }
5747
5748 /* If this is the last frame in a column, may want to resize the parent
5749 * frame instead (go two up to skip a row of frames). */
5750 while (curfr != topframe && curfr->fr_next == NULL)
5751 {
5752 if (fr != topframe)
5753 fr = fr->fr_parent;
5754 curfr = fr;
5755 if (fr != topframe)
5756 fr = fr->fr_parent;
5757 }
5758
5759 if (offset < 0) /* drag up */
5760 {
5761 up = TRUE;
5762 offset = -offset;
5763 /* sum up the room of the current frame and above it */
5764 if (fr == curfr)
5765 {
5766 /* only one window */
5767 room = fr->fr_height - frame_minheight(fr, NULL);
5768 }
5769 else
5770 {
5771 room = 0;
5772 for (fr = fr->fr_child; ; fr = fr->fr_next)
5773 {
5774 room += fr->fr_height - frame_minheight(fr, NULL);
5775 if (fr == curfr)
5776 break;
5777 }
5778 }
5779 fr = curfr->fr_next; /* put fr at frame that grows */
5780 }
5781 else /* drag down */
5782 {
5783 up = FALSE;
5784 /*
5785 * Only dragging the last status line can reduce p_ch.
5786 */
5787 room = Rows - cmdline_row;
5788 if (curfr->fr_next == NULL)
5789 room -= 1;
5790 else
5791 room -= p_ch;
5792 if (room < 0)
5793 room = 0;
5794 /* sum up the room of frames below of the current one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005795 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 room += fr->fr_height - frame_minheight(fr, NULL);
5797 fr = curfr; /* put fr at window that grows */
5798 }
5799
5800 if (room < offset) /* Not enough room */
5801 offset = room; /* Move as far as we can */
5802 if (offset <= 0)
5803 return;
5804
5805 /*
5806 * Grow frame fr by "offset" lines.
5807 * Doesn't happen when dragging the last status line up.
5808 */
5809 if (fr != NULL)
5810 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5811
5812 if (up)
5813 fr = curfr; /* current frame gets smaller */
5814 else
5815 fr = curfr->fr_next; /* next frame gets smaller */
5816
5817 /*
5818 * Now make the other frames smaller.
5819 */
5820 while (fr != NULL && offset > 0)
5821 {
5822 n = frame_minheight(fr, NULL);
5823 if (fr->fr_height - offset <= n)
5824 {
5825 offset -= fr->fr_height - n;
5826 frame_new_height(fr, n, !up, FALSE);
5827 }
5828 else
5829 {
5830 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5831 break;
5832 }
5833 if (up)
5834 fr = fr->fr_prev;
5835 else
5836 fr = fr->fr_next;
5837 }
5838 row = win_comp_pos();
5839 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5840 cmdline_row = row;
5841 p_ch = Rows - cmdline_row;
5842 if (p_ch < 1)
5843 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005844 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005845 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 showmode();
5847}
5848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849/*
5850 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5851 */
5852 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005853win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 frame_T *curfr;
5856 frame_T *fr;
5857 int room;
5858 int left; /* if TRUE, drag separator line left, otherwise right */
5859 int n;
5860
5861 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005862 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 return;
5864 curfr = fr;
5865 fr = fr->fr_parent;
5866 /* When the parent frame is not a row of frames, its parent should be. */
5867 if (fr->fr_layout != FR_ROW)
5868 {
5869 if (fr == topframe) /* only a column of windows (cannot happen?) */
5870 return;
5871 curfr = fr;
5872 fr = fr->fr_parent;
5873 }
5874
5875 /* If this is the last frame in a row, may want to resize a parent
5876 * frame instead. */
5877 while (curfr->fr_next == NULL)
5878 {
5879 if (fr == topframe)
5880 break;
5881 curfr = fr;
5882 fr = fr->fr_parent;
5883 if (fr != topframe)
5884 {
5885 curfr = fr;
5886 fr = fr->fr_parent;
5887 }
5888 }
5889
5890 if (offset < 0) /* drag left */
5891 {
5892 left = TRUE;
5893 offset = -offset;
5894 /* sum up the room of the current frame and left of it */
5895 room = 0;
5896 for (fr = fr->fr_child; ; fr = fr->fr_next)
5897 {
5898 room += fr->fr_width - frame_minwidth(fr, NULL);
5899 if (fr == curfr)
5900 break;
5901 }
5902 fr = curfr->fr_next; /* put fr at frame that grows */
5903 }
5904 else /* drag right */
5905 {
5906 left = FALSE;
5907 /* sum up the room of frames right of the current one */
5908 room = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005909 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 room += fr->fr_width - frame_minwidth(fr, NULL);
5911 fr = curfr; /* put fr at window that grows */
5912 }
5913
5914 if (room < offset) /* Not enough room */
5915 offset = room; /* Move as far as we can */
5916 if (offset <= 0) /* No room at all, quit. */
5917 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005918 if (fr == NULL)
5919 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005922 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
5924 /* shrink other frames: current and at the left or at the right */
5925 if (left)
5926 fr = curfr; /* current frame gets smaller */
5927 else
5928 fr = curfr->fr_next; /* next frame gets smaller */
5929
5930 while (fr != NULL && offset > 0)
5931 {
5932 n = frame_minwidth(fr, NULL);
5933 if (fr->fr_width - offset <= n)
5934 {
5935 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005936 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938 else
5939 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005940 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 break;
5942 }
5943 if (left)
5944 fr = fr->fr_prev;
5945 else
5946 fr = fr->fr_next;
5947 }
5948 (void)win_comp_pos();
5949 redraw_all_later(NOT_VALID);
5950}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951#endif /* FEAT_MOUSE */
5952
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005953#define FRACTION_MULT 16384L
5954
5955/*
5956 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005957 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005958 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005959 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005960set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005961{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005962 if (wp->w_height > 1)
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005963 // When cursor is in the first line the percentage is computed as if
5964 // it's halfway that line. Thus with two lines it is 25%, with three
5965 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005966 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005967 + FRACTION_MULT / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005968}
5969
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970/*
5971 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005972 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 * This takes care of the things inside the window, not what happens to the
5974 * window position, the frame or to other windows.
5975 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005976 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005977win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005979 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
5981 /* Don't want a negative height. Happens when splitting a tiny window.
5982 * Will equalize heights soon to fix it. */
5983 if (height < 0)
5984 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005985 if (wp->w_height == height)
5986 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005988 if (wp->w_height > 0)
5989 {
5990 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02005991 /* w_wrow needs to be valid. When setting 'laststatus' this may
5992 * call win_new_height() recursively. */
5993 validate_cursor();
5994 if (wp->w_height != prev_height)
5995 return; /* Recursive call already changed the size, bail out here
5996 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005997 if (wp->w_wrow != wp->w_prev_fraction_row)
5998 set_fraction(wp);
5999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
6001 wp->w_height = height;
6002 wp->w_skipcol = 0;
6003
Bram Moolenaar955f1982017-02-05 15:10:51 +01006004 /* There is no point in adjusting the scroll position when exiting. Some
6005 * values might be invalid. */
6006 if (!exiting)
6007 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02006008}
6009
6010 void
6011scroll_to_fraction(win_T *wp, int prev_height)
6012{
6013 linenr_T lnum;
6014 int sline, line_size;
6015 int height = wp->w_height;
6016
Bram Moolenaara9b25352019-05-12 14:25:30 +02006017 // Don't change w_topline in any of these cases:
6018 // - window height is 0
6019 // - 'scrollbind' is set and this isn't the current window
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02006020 // - window height is sufficient to display the whole buffer and first line
6021 // is visible.
Bram Moolenaara9b25352019-05-12 14:25:30 +02006022 if (height > 0
6023 && (!wp->w_p_scb || wp == curwin)
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02006024 && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 {
Bram Moolenaar34114692005-01-02 11:28:13 +00006026 /*
6027 * Find a value for w_topline that shows the cursor at the same
6028 * relative position in the window as before (more or less).
6029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 lnum = wp->w_cursor.lnum;
6031 if (lnum < 1) /* can happen when starting up */
6032 lnum = 1;
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006033 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L)
6034 / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
6036 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00006037
6038 if (sline >= 0)
6039 {
6040 /* Make sure the whole cursor line is visible, if possible. */
6041 int rows = plines_win(wp, lnum, FALSE);
6042
6043 if (sline > wp->w_height - rows)
6044 {
6045 sline = wp->w_height - rows;
6046 wp->w_wrow -= rows - line_size;
6047 }
6048 }
6049
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 if (sline < 0)
6051 {
6052 /*
6053 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00006054 * Make cursor line the first line in the window. If not enough
6055 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 */
6057 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00006058 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02006059 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00006060 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006061 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00006062 --wp->w_wrow;
6063 while (wp->w_wrow >= wp->w_height)
6064 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006065 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00006066 + win_col_off2(wp);
6067 --wp->w_wrow;
6068 }
6069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006071 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 {
Bram Moolenaar26470632006-10-24 19:12:40 +00006073 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 {
6075#ifdef FEAT_FOLDING
6076 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
6077 if (lnum == 1)
6078 {
6079 /* first line in buffer is folded */
6080 line_size = 1;
6081 --sline;
6082 break;
6083 }
6084#endif
6085 --lnum;
6086#ifdef FEAT_DIFF
6087 if (lnum == wp->w_topline)
6088 line_size = plines_win_nofill(wp, lnum, TRUE)
6089 + wp->w_topfill;
6090 else
6091#endif
6092 line_size = plines_win(wp, lnum, TRUE);
6093 sline -= line_size;
6094 }
Bram Moolenaar34114692005-01-02 11:28:13 +00006095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (sline < 0)
6097 {
6098 /*
6099 * Line we want at top would go off top of screen. Use next
6100 * line instead.
6101 */
6102#ifdef FEAT_FOLDING
6103 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
6104#endif
6105 lnum++;
6106 wp->w_wrow -= line_size + sline;
6107 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006108 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 {
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006110 // First line of file reached, use that as topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 lnum = 1;
6112 wp->w_wrow -= sline;
6113 }
6114 }
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006115 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 }
6117
6118 if (wp == curwin)
6119 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006120 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 update_topline();
6122 curs_columns(FALSE); /* validate w_wrow */
6123 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006124 if (prev_height > 0)
6125 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126
6127 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006128 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 invalidate_botline_win(wp);
6131}
6132
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133/*
6134 * Set the width of a window.
6135 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02006136 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006137win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 wp->w_width = width;
6140 wp->w_lines_valid = 0;
6141 changed_line_abv_curs_win(wp);
6142 invalidate_botline_win(wp);
6143 if (wp == curwin)
6144 {
6145 update_topline();
6146 curs_columns(TRUE); /* validate w_wrow */
6147 }
6148 redraw_win_later(wp, NOT_VALID);
6149 wp->w_redr_status = TRUE;
6150}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151
6152 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006153win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154{
6155 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6156 if (wp->w_p_scr == 0)
6157 wp->w_p_scr = 1;
6158}
6159
6160/*
6161 * command_height: called whenever p_ch has been changed
6162 */
6163 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006164command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 int h;
6167 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006168 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006170 /* Use the value of p_ch that we remembered. This is needed for when the
6171 * GUI starts up, we can't be sure in what order things happen. And when
6172 * p_ch was changed in another tab page. */
6173 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006174
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 /* Find bottom frame with width of screen. */
6176 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6178 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179
6180 /* Avoid changing the height of a window with 'winfixheight' set. */
6181 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6182 && frp->fr_win->w_p_wfh)
6183 frp = frp->fr_prev;
6184
6185 if (starting != NO_SCREEN)
6186 {
6187 cmdline_row = Rows - p_ch;
6188
6189 if (p_ch > old_p_ch) /* p_ch got bigger */
6190 {
6191 while (p_ch > old_p_ch)
6192 {
6193 if (frp == NULL)
6194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006195 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006197 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 cmdline_row = Rows - p_ch;
6199 break;
6200 }
6201 h = frp->fr_height - frame_minheight(frp, NULL);
6202 if (h > p_ch - old_p_ch)
6203 h = p_ch - old_p_ch;
6204 old_p_ch += h;
6205 frame_add_height(frp, -h);
6206 frp = frp->fr_prev;
6207 }
6208
6209 /* Recompute window positions. */
6210 (void)win_comp_pos();
6211
6212 /* clear the lines added to cmdline */
6213 if (full_screen)
6214 screen_fill((int)(cmdline_row), (int)Rows, 0,
6215 (int)Columns, ' ', ' ', 0);
6216 msg_row = cmdline_row;
6217 redraw_cmdline = TRUE;
6218 return;
6219 }
6220
6221 if (msg_row < cmdline_row)
6222 msg_row = cmdline_row;
6223 redraw_cmdline = TRUE;
6224 }
6225 frame_add_height(frp, (int)(old_p_ch - p_ch));
6226
6227 /* Recompute window positions. */
6228 if (frp != lastwin->w_frame)
6229 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230}
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232/*
6233 * Resize frame "frp" to be "n" lines higher (negative for less high).
6234 * Also resize the frames it is contained in.
6235 */
6236 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006237frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
6239 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6240 for (;;)
6241 {
6242 frp = frp->fr_parent;
6243 if (frp == NULL)
6244 break;
6245 frp->fr_height += n;
6246 }
6247}
6248
6249/*
6250 * Add or remove a status line for the bottom window(s), according to the
6251 * value of 'laststatus'.
6252 */
6253 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006254last_status(
6255 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256{
6257 /* Don't make a difference between horizontal or vertical split. */
6258 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006259 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260}
6261
6262 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006263last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264{
6265 frame_T *fp;
6266 win_T *wp;
6267
6268 if (fr->fr_layout == FR_LEAF)
6269 {
6270 wp = fr->fr_win;
6271 if (wp->w_status_height != 0 && !statusline)
6272 {
6273 /* remove status line */
6274 win_new_height(wp, wp->w_height + 1);
6275 wp->w_status_height = 0;
6276 comp_col();
6277 }
6278 else if (wp->w_status_height == 0 && statusline)
6279 {
6280 /* Find a frame to take a line from. */
6281 fp = fr;
6282 while (fp->fr_height <= frame_minheight(fp, NULL))
6283 {
6284 if (fp == topframe)
6285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006286 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 return;
6288 }
6289 /* In a column of frames: go to frame above. If already at
6290 * the top or in a row of frames: go to parent. */
6291 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6292 fp = fp->fr_prev;
6293 else
6294 fp = fp->fr_parent;
6295 }
6296 wp->w_status_height = 1;
6297 if (fp != fr)
6298 {
6299 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6300 frame_fix_height(wp);
6301 (void)win_comp_pos();
6302 }
6303 else
6304 win_new_height(wp, wp->w_height - 1);
6305 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006306 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 }
6308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 else if (fr->fr_layout == FR_ROW)
6310 {
6311 /* vertically split windows, set status line for each one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006312 FOR_ALL_FRAMES(fp, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 last_status_rec(fp, statusline);
6314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 else
6316 {
6317 /* horizontally split window, set status line for last one */
6318 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6319 ;
6320 last_status_rec(fp, statusline);
6321 }
6322}
6323
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006324/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006325 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006326 */
6327 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006328tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006329{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006330#ifdef FEAT_GUI_TABLINE
6331 /* When the GUI has the tabline then this always returns zero. */
6332 if (gui_use_tabline())
6333 return 0;
6334#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006335 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006336 {
6337 case 0: return 0;
6338 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6339 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006340 return 1;
6341}
6342
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343/*
6344 * Return the minimal number of rows that is needed on the screen to display
6345 * the current number of windows.
6346 */
6347 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006348min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
6350 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006351 tabpage_T *tp;
6352 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
6354 if (firstwin == NULL) /* not initialized yet */
6355 return MIN_LINES;
6356
Bram Moolenaarf740b292006-02-16 22:11:02 +00006357 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006358 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006359 {
6360 n = frame_minheight(tp->tp_topframe, NULL);
6361 if (total < n)
6362 total = n;
6363 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006364 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006365 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 return total;
6367}
6368
6369/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006370 * Return TRUE if there is only one window (in the current tab page), not
6371 * counting a help or preview window, unless it is the current window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006372 * Does not count unlisted windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 */
6374 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006375only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 int count = 0;
6378 win_T *wp;
6379
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006380 /* If there is another tab page there always is another window. */
6381 if (first_tabpage->tp_next != NULL)
6382 return FALSE;
6383
Bram Moolenaar29323592016-07-24 22:04:11 +02006384 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006385 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006386 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387# ifdef FEAT_QUICKFIX
6388 || wp->w_p_pvw
6389# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006390 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 ++count;
6392 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393}
6394
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395/*
6396 * Correct the cursor line number in other windows. Used after changing the
6397 * current buffer, and before applying autocommands.
6398 * When "do_curwin" is TRUE, also check current window.
6399 */
6400 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006401check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006404 tabpage_T *tp;
6405
6406 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 {
Bram Moolenaara68e5952019-04-25 22:22:01 +02006409 // save the original cursor position and topline
6410 wp->w_save_cursor.w_cursor_save = wp->w_cursor;
6411 wp->w_save_cursor.w_topline_save = wp->w_topline;
6412
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6414 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6415 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6416 wp->w_topline = curbuf->b_ml.ml_line_count;
Bram Moolenaara68e5952019-04-25 22:22:01 +02006417
6418 // save the corrected cursor position and topline
6419 wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
6420 wp->w_save_cursor.w_topline_corr = wp->w_topline;
6421 }
6422}
6423
6424/*
6425 * Reset cursor and topline to its stored values from check_lnums().
6426 * check_lnums() must have been called first!
6427 */
6428 void
6429reset_lnums()
6430{
6431 win_T *wp;
6432 tabpage_T *tp;
6433
6434 FOR_ALL_TAB_WINDOWS(tp, wp)
6435 if (wp->w_buffer == curbuf)
6436 {
6437 // Restore the value if the autocommand didn't change it.
6438 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
6439 wp->w_cursor = wp->w_save_cursor.w_cursor_save;
6440 if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
6441 wp->w_topline = wp->w_save_cursor.w_topline_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 }
6443}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
6445/*
6446 * A snapshot of the window sizes, to restore them after closing the help
6447 * window.
6448 * Only these fields are used:
6449 * fr_layout
6450 * fr_width
6451 * fr_height
6452 * fr_next
6453 * fr_child
6454 * fr_win (only valid for the old curwin, NULL otherwise)
6455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
6457/*
6458 * Create a snapshot of the current frame sizes.
6459 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006460 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006461make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006463 clear_snapshot(curtab, idx);
6464 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465}
6466
6467 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006468make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006470 *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 if (*frp == NULL)
6472 return;
6473 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 (*frp)->fr_height = fr->fr_height;
6476 if (fr->fr_next != NULL)
6477 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6478 if (fr->fr_child != NULL)
6479 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6480 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6481 (*frp)->fr_win = curwin;
6482}
6483
6484/*
6485 * Remove any existing snapshot.
6486 */
6487 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006488clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006490 clear_snapshot_rec(tp->tp_snapshot[idx]);
6491 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492}
6493
6494 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006495clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 if (fr != NULL)
6498 {
6499 clear_snapshot_rec(fr->fr_next);
6500 clear_snapshot_rec(fr->fr_child);
6501 vim_free(fr);
6502 }
6503}
6504
6505/*
6506 * Restore a previously created snapshot, if there is any.
6507 * This is only done if the screen size didn't change and the window layout is
6508 * still the same.
6509 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006510 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006511restore_snapshot(
6512 int idx,
6513 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514{
6515 win_T *wp;
6516
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006517 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006518 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006519 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6520 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006522 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 win_comp_pos();
6524 if (wp != NULL && close_curwin)
6525 win_goto(wp);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01006526 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006528 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529}
6530
6531/*
6532 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006533 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 */
6535 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006536check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 if (sn->fr_layout != fr->fr_layout
6539 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6540 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6541 || (sn->fr_next != NULL
6542 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6543 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006544 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006545 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 return FAIL;
6547 return OK;
6548}
6549
6550/*
6551 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6552 * following frames and children.
6553 * Returns a pointer to the old current window, or NULL.
6554 */
6555 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006556restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557{
6558 win_T *wp = NULL;
6559 win_T *wp2;
6560
6561 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 if (fr->fr_layout == FR_LEAF)
6564 {
6565 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006566 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 wp = sn->fr_win;
6568 }
6569 if (sn->fr_next != NULL)
6570 {
6571 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6572 if (wp2 != NULL)
6573 wp = wp2;
6574 }
6575 if (sn->fr_child != NULL)
6576 {
6577 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6578 if (wp2 != NULL)
6579 wp = wp2;
6580 }
6581 return wp;
6582}
6583
Bram Moolenaar95064ec2013-07-17 17:15:25 +02006584#if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6585 || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006586/*
6587 * Set "win" to be the curwin and "tp" to be the current tab page.
Bram Moolenaar5d2bae82014-09-19 14:26:36 +02006588 * restore_win() MUST be called to undo, also when FAIL is returned.
6589 * No autocommands will be executed until restore_win() is called.
Bram Moolenaard6949742013-06-16 14:18:28 +02006590 * When "no_display" is TRUE the display won't be affected, no redraw is
6591 * triggered, another tabpage access is limited.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006592 * Returns FAIL if switching to "win" failed.
6593 */
6594 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006595switch_win(
Bram Moolenaar816968d2017-09-29 21:29:18 +02006596 win_T **save_curwin,
6597 tabpage_T **save_curtab,
6598 win_T *win,
6599 tabpage_T *tp,
6600 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006601{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006602 block_autocmds();
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006603 return switch_win_noblock(save_curwin, save_curtab, win, tp, no_display);
6604}
6605
6606/*
6607 * As switch_win() but without blocking autocommands.
6608 */
6609 int
6610switch_win_noblock(
6611 win_T **save_curwin,
6612 tabpage_T **save_curtab,
6613 win_T *win,
6614 tabpage_T *tp,
6615 int no_display)
6616{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006617 *save_curwin = curwin;
6618 if (tp != NULL)
6619 {
6620 *save_curtab = curtab;
Bram Moolenaard6949742013-06-16 14:18:28 +02006621 if (no_display)
6622 {
6623 curtab->tp_firstwin = firstwin;
6624 curtab->tp_lastwin = lastwin;
6625 curtab = tp;
6626 firstwin = curtab->tp_firstwin;
6627 lastwin = curtab->tp_lastwin;
6628 }
6629 else
6630 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006631 }
6632 if (!win_valid(win))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006633 return FAIL;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006634 curwin = win;
6635 curbuf = curwin->w_buffer;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006636 return OK;
6637}
6638
6639/*
6640 * Restore current tabpage and window saved by switch_win(), if still valid.
Bram Moolenaard6949742013-06-16 14:18:28 +02006641 * When "no_display" is TRUE the display won't be affected, no redraw is
6642 * triggered.
Bram Moolenaar105bc352013-05-17 16:03:57 +02006643 */
6644 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006645restore_win(
Bram Moolenaar89adc3a2019-05-30 17:29:40 +02006646 win_T *save_curwin,
6647 tabpage_T *save_curtab,
6648 int no_display)
6649{
6650 restore_win_noblock(save_curwin, save_curtab, no_display);
6651 unblock_autocmds();
6652}
6653
6654/*
6655 * As restore_win() but without unblocking autocommands.
6656 */
6657 void
6658restore_win_noblock(
6659 win_T *save_curwin,
6660 tabpage_T *save_curtab,
6661 int no_display)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006662{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006663 if (save_curtab != NULL && valid_tabpage(save_curtab))
Bram Moolenaard6949742013-06-16 14:18:28 +02006664 {
6665 if (no_display)
6666 {
6667 curtab->tp_firstwin = firstwin;
6668 curtab->tp_lastwin = lastwin;
6669 curtab = save_curtab;
6670 firstwin = curtab->tp_firstwin;
6671 lastwin = curtab->tp_lastwin;
6672 }
6673 else
6674 goto_tabpage_tp(save_curtab, FALSE, FALSE);
6675 }
Bram Moolenaar105bc352013-05-17 16:03:57 +02006676 if (win_valid(save_curwin))
6677 {
6678 curwin = save_curwin;
6679 curbuf = curwin->w_buffer;
6680 }
Bram Moolenaareea16992019-05-31 17:34:48 +02006681#ifdef FEAT_TEXT_PROP
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02006682 else if (WIN_IS_POPUP(curwin))
Bram Moolenaareea16992019-05-31 17:34:48 +02006683 // original window was closed and now we're in a popup window: Go
6684 // to the first valid window.
6685 win_goto(firstwin);
6686#endif
Bram Moolenaar105bc352013-05-17 16:03:57 +02006687}
6688
6689/*
6690 * Make "buf" the current buffer. restore_buffer() MUST be called to undo.
6691 * No autocommands will be executed. Use aucmd_prepbuf() if there are any.
6692 */
6693 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006694switch_buffer(bufref_T *save_curbuf, buf_T *buf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006695{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006696 block_autocmds();
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006697 set_bufref(save_curbuf, curbuf);
Bram Moolenaar105bc352013-05-17 16:03:57 +02006698 --curbuf->b_nwindows;
6699 curbuf = buf;
6700 curwin->w_buffer = buf;
6701 ++curbuf->b_nwindows;
6702}
6703
6704/*
6705 * Restore the current buffer after using switch_buffer().
6706 */
6707 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006708restore_buffer(bufref_T *save_curbuf)
Bram Moolenaar105bc352013-05-17 16:03:57 +02006709{
Bram Moolenaar105bc352013-05-17 16:03:57 +02006710 unblock_autocmds();
Bram Moolenaar105bc352013-05-17 16:03:57 +02006711 /* Check for valid buffer, just in case. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006712 if (bufref_valid(save_curbuf))
Bram Moolenaar105bc352013-05-17 16:03:57 +02006713 {
6714 --curbuf->b_nwindows;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006715 curwin->w_buffer = save_curbuf->br_buf;
6716 curbuf = save_curbuf->br_buf;
Bram Moolenaar105bc352013-05-17 16:03:57 +02006717 ++curbuf->b_nwindows;
6718 }
6719}
6720#endif
6721
Bram Moolenaar4033c552017-09-16 20:54:51 +02006722#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723/*
6724 * Return TRUE if there is any vertically split window.
6725 */
6726 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006727win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728{
6729 frame_T *fr;
6730
6731 if (topframe->fr_layout == FR_ROW)
6732 return TRUE;
6733
6734 if (topframe->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006735 FOR_ALL_FRAMES(fr, topframe->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (fr->fr_layout == FR_ROW)
6737 return TRUE;
6738
6739 return FALSE;
6740}
6741#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006742
Bram Moolenaar6d216452013-05-12 19:00:41 +02006743#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6744 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006745get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006746{
6747 int i = 1;
6748 win_T *w;
6749
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006750 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006751 ++i;
6752
6753 if (w == NULL)
6754 return 0;
6755 else
6756 return i;
6757}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006758
6759 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006760get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006761{
6762 int i = 1;
6763 tabpage_T *t;
6764
6765 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6766 ++i;
6767
6768 if (t == NULL)
6769 return 0;
6770 else
6771 return i;
6772}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006773#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006774
6775/*
6776 * Return TRUE if "topfrp" and its children are at the right height.
6777 */
6778 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006779frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006780{
6781 frame_T *frp;
6782
6783 if (topfrp->fr_height != height)
6784 return FALSE;
6785
6786 if (topfrp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006787 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006788 if (frp->fr_height != height)
6789 return FALSE;
6790
6791 return TRUE;
6792}
6793
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006794/*
6795 * Return TRUE if "topfrp" and its children are at the right width.
6796 */
6797 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006798frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006799{
6800 frame_T *frp;
6801
6802 if (topfrp->fr_width != width)
6803 return FALSE;
6804
6805 if (topfrp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006806 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006807 if (frp->fr_width != width)
6808 return FALSE;
6809
6810 return TRUE;
6811}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006812
Bram Moolenaar86edef62016-03-13 18:07:30 +01006813#if defined(FEAT_EVAL) || defined(PROTO)
6814 int
6815win_getid(typval_T *argvars)
6816{
6817 int winnr;
6818 win_T *wp;
6819
6820 if (argvars[0].v_type == VAR_UNKNOWN)
6821 return curwin->w_id;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006822 winnr = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006823 if (winnr > 0)
6824 {
6825 if (argvars[1].v_type == VAR_UNKNOWN)
6826 wp = firstwin;
6827 else
6828 {
6829 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006830 int tabnr = tv_get_number(&argvars[1]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006831
Bram Moolenaar29323592016-07-24 22:04:11 +02006832 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006833 if (--tabnr == 0)
6834 break;
6835 if (tp == NULL)
6836 return -1;
Bram Moolenaar8e639052016-11-13 14:31:40 +01006837 if (tp == curtab)
6838 wp = firstwin;
6839 else
6840 wp = tp->tp_firstwin;
Bram Moolenaar86edef62016-03-13 18:07:30 +01006841 }
6842 for ( ; wp != NULL; wp = wp->w_next)
6843 if (--winnr == 0)
6844 return wp->w_id;
6845 }
6846 return 0;
6847}
6848
6849 int
6850win_gotoid(typval_T *argvars)
6851{
6852 win_T *wp;
6853 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006854 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006855
Bram Moolenaar29323592016-07-24 22:04:11 +02006856 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006857 if (wp->w_id == id)
6858 {
6859 goto_tabpage_win(tp, wp);
6860 return 1;
6861 }
6862 return 0;
6863}
6864
6865 void
6866win_id2tabwin(typval_T *argvars, list_T *list)
6867{
6868 win_T *wp;
6869 tabpage_T *tp;
6870 int winnr = 1;
6871 int tabnr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006872 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006873
Bram Moolenaar29323592016-07-24 22:04:11 +02006874 FOR_ALL_TABPAGES(tp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006875 {
Bram Moolenaar29323592016-07-24 22:04:11 +02006876 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006877 {
6878 if (wp->w_id == id)
6879 {
6880 list_append_number(list, tabnr);
6881 list_append_number(list, winnr);
6882 return;
6883 }
6884 ++winnr;
6885 }
6886 ++tabnr;
6887 winnr = 1;
6888 }
6889 list_append_number(list, 0);
6890 list_append_number(list, 0);
6891}
6892
Bram Moolenaar820680b2019-08-09 14:56:22 +02006893/*
6894 * Return the window pointer of window "id".
6895 */
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006896 win_T *
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01006897win_id2wp(int id)
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006898{
Bram Moolenaar820680b2019-08-09 14:56:22 +02006899 return win_id2wp_tp(id, NULL);
6900}
6901
6902/*
6903 * Return the window and tab pointer of window "id".
6904 */
6905 win_T *
6906win_id2wp_tp(int id, tabpage_T **tpp)
6907{
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006908 win_T *wp;
6909 tabpage_T *tp;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006910
6911 FOR_ALL_TAB_WINDOWS(tp, wp)
6912 if (wp->w_id == id)
Bram Moolenaar820680b2019-08-09 14:56:22 +02006913 {
6914 if (tpp != NULL)
6915 *tpp = tp;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006916 return wp;
Bram Moolenaar820680b2019-08-09 14:56:22 +02006917 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006918#ifdef FEAT_TEXT_PROP
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006919 // popup windows are in separate lists
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006920 FOR_ALL_TABPAGES(tp)
6921 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
6922 if (wp->w_id == id)
Bram Moolenaar820680b2019-08-09 14:56:22 +02006923 {
6924 if (tpp != NULL)
6925 *tpp = tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006926 return wp;
Bram Moolenaar820680b2019-08-09 14:56:22 +02006927 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006928 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
6929 if (wp->w_id == id)
Bram Moolenaar820680b2019-08-09 14:56:22 +02006930 {
6931 if (tpp != NULL)
6932 *tpp = tp;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006933 return wp;
Bram Moolenaar820680b2019-08-09 14:56:22 +02006934 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006935#endif
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +02006936
6937 return NULL;
6938}
6939
Bram Moolenaar86edef62016-03-13 18:07:30 +01006940 int
6941win_id2win(typval_T *argvars)
6942{
6943 win_T *wp;
6944 int nr = 1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006945 int id = tv_get_number(&argvars[0]);
Bram Moolenaar86edef62016-03-13 18:07:30 +01006946
Bram Moolenaar29323592016-07-24 22:04:11 +02006947 FOR_ALL_WINDOWS(wp)
Bram Moolenaar86edef62016-03-13 18:07:30 +01006948 {
6949 if (wp->w_id == id)
6950 return nr;
6951 ++nr;
6952 }
6953 return 0;
6954}
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006955
6956 void
6957win_findbuf(typval_T *argvars, list_T *list)
6958{
6959 win_T *wp;
6960 tabpage_T *tp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006961 int bufnr = tv_get_number(&argvars[0]);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006962
Bram Moolenaar29323592016-07-24 22:04:11 +02006963 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01006964 if (wp->w_buffer->b_fnum == bufnr)
6965 list_append_number(list, wp->w_id);
6966}
6967
Bram Moolenaar0f6b4f02018-08-21 16:56:34 +02006968/*
6969 * Get the layout of the given tab page for winlayout().
6970 */
6971 void
6972get_framelayout(frame_T *fr, list_T *l, int outer)
6973{
6974 frame_T *child;
6975 list_T *fr_list;
6976 list_T *win_list;
6977
6978 if (fr == NULL)
6979 return;
6980
6981 if (outer)
6982 // outermost call from f_winlayout()
6983 fr_list = l;
6984 else
6985 {
6986 fr_list = list_alloc();
6987 if (fr_list == NULL)
6988 return;
6989 list_append_list(l, fr_list);
6990 }
6991
6992 if (fr->fr_layout == FR_LEAF)
6993 {
6994 if (fr->fr_win != NULL)
6995 {
6996 list_append_string(fr_list, (char_u *)"leaf", -1);
6997 list_append_number(fr_list, fr->fr_win->w_id);
6998 }
6999 }
7000 else
7001 {
7002 list_append_string(fr_list,
7003 fr->fr_layout == FR_ROW ? (char_u *)"row" : (char_u *)"col", -1);
7004
7005 win_list = list_alloc();
7006 if (win_list == NULL)
7007 return;
7008 list_append_list(fr_list, win_list);
7009 child = fr->fr_child;
7010 while (child != NULL)
7011 {
7012 get_framelayout(child, win_list, FALSE);
7013 child = child->fr_next;
7014 }
7015 }
7016}
Bram Moolenaar86edef62016-03-13 18:07:30 +01007017#endif