blob: 0fda9f05c666485d2f43448ed0a8a9960584e4e3 [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 Moolenaar12034e22019-08-25 22:25:02 +02002525#ifdef FEAT_TEXT_PROP
2526 if (popup_win_closed(win) && !win_valid(win))
2527 return FAIL;
2528#endif
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002529 win_close_buffer(win, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002530
Bram Moolenaar802418d2013-01-17 14:00:11 +01002531 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2532 && (last_window() || curtab != prev_curtab
2533 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002534 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002535 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002536 * curwin->w_buffer, otherwise writing viminfo may fail. */
2537 if (curwin->w_buffer == NULL)
2538 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002539 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002540 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002541
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002542 /* Autocommands may have moved to another tab page. */
2543 if (curtab != prev_curtab && win_valid_any_tab(win)
2544 && win->w_buffer == NULL)
2545 {
2546 /* Need to close the window anyway, since the buffer is NULL. */
2547 win_close_othertab(win, FALSE, prev_curtab);
2548 return FAIL;
2549 }
2550
2551 /* Autocommands may have closed the window already or closed the only
2552 * other window. */
2553 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002554 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
Bram Moolenaar1417c762019-07-27 17:31:36 +02002557 // Now we are really going to close the window. Disallow any autocommand
2558 // to split a window to avoid trouble.
2559 ++split_disallowed;
2560
Bram Moolenaara971b822011-09-14 14:43:25 +02002561 /* Free the memory used for the window and get the window that received
2562 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002563 wp = win_free_mem(win, &dir, NULL);
2564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 /* Make sure curwin isn't invalid. It can cause severe trouble when
2566 * printing an error message. For win_equal() curbuf needs to be valid
2567 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002568 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 curwin = wp;
2571#ifdef FEAT_QUICKFIX
2572 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2573 {
2574 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002575 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 * finding another window to go to.
2577 */
2578 for (;;)
2579 {
2580 if (wp->w_next == NULL)
2581 wp = firstwin;
2582 else
2583 wp = wp->w_next;
2584 if (wp == curwin)
2585 break;
2586 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2587 {
2588 curwin = wp;
2589 break;
2590 }
2591 }
2592 }
2593#endif
2594 curbuf = curwin->w_buffer;
2595 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002596
2597 /* The cursor position may be invalid if the buffer changed after last
2598 * using the window. */
2599 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002601 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002602 /* If the frame of the closed window contains the new current window,
2603 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002604 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 else
2606 win_comp_pos();
2607 if (close_curwin)
2608 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002609 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (other_buffer)
2611 /* careful: after this wp and win may be invalid! */
2612 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
2614
Bram Moolenaar1417c762019-07-27 17:31:36 +02002615 --split_disallowed;
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002618 * If last window has a status line now and we don't want one,
2619 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
2621 last_status(FALSE);
2622
2623 /* After closing the help window, try restoring the window layout from
2624 * before it was opened. */
2625 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002626 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002628#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2630 if (gui.in_use && !win_hasvertsplit())
2631 gui_init_which_components(NULL);
2632#endif
2633
2634 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002635 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636}
2637
2638/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002639 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002640 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002641 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002642 * Caller must check if buffer is hidden and whether the tabline needs to be
2643 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002644 */
2645 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002646win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002647{
2648 win_T *wp;
2649 int dir;
2650 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002651 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002652
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002653 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2654 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002655 if (win->w_closing || (win->w_buffer != NULL
2656 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002657 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002658
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002659 if (win->w_buffer != NULL)
2660 /* Close the link to the buffer. */
2661 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002662
2663 /* Careful: Autocommands may have closed the tab page or made it the
2664 * current tab page. */
2665 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2666 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002667 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002668 return;
2669
2670 /* Autocommands may have closed the window already. */
2671 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2672 ;
2673 if (wp == NULL)
2674 return;
2675
Bram Moolenaarf740b292006-02-16 22:11:02 +00002676 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002677 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002678 {
2679 if (tp == first_tabpage)
2680 first_tabpage = tp->tp_next;
2681 else
2682 {
2683 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2684 ptp = ptp->tp_next)
2685 ;
2686 if (ptp == NULL)
2687 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002688 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002689 return;
2690 }
2691 ptp->tp_next = tp->tp_next;
2692 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002693 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002694 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002695
2696 /* Free the memory used for the window. */
2697 win_free_mem(win, &dir, tp);
2698
2699 if (free_tp)
2700 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002701}
2702
2703/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002704 * Free the memory used for a window.
2705 * Returns a pointer to the window that got the freed up space.
2706 */
2707 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002708win_free_mem(
2709 win_T *win,
2710 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2711 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002712{
2713 frame_T *frp;
2714 win_T *wp;
2715
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002716 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002717 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002718 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002719 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002720 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002721
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002722 /* When deleting the current window of another tab page select a new
2723 * current window. */
2724 if (tp != NULL && win == tp->tp_curwin)
2725 tp->tp_curwin = wp;
2726
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002727 return wp;
2728}
2729
2730#if defined(EXITFREE) || defined(PROTO)
2731 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002732win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002733{
2734 int dummy;
2735
Bram Moolenaarf740b292006-02-16 22:11:02 +00002736 while (first_tabpage->tp_next != NULL)
2737 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002738
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002739 if (aucmd_win != NULL)
2740 {
2741 (void)win_free_mem(aucmd_win, &dummy, NULL);
2742 aucmd_win = NULL;
2743 }
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002744# ifdef FEAT_TEXT_PROP
2745 close_all_popups();
2746# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002747
2748 while (firstwin != NULL)
2749 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002750
2751 /* No window should be used after this. Set curwin to NULL to crash
2752 * instead of using freed memory. */
2753 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002754}
2755#endif
2756
2757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 * Remove a window and its frame from the tree of frames.
2759 * Returns a pointer to the window that got the freed up space.
2760 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002761 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002762winframe_remove(
2763 win_T *win,
2764 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2765 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766{
2767 frame_T *frp, *frp2, *frp3;
2768 frame_T *frp_close = win->w_frame;
2769 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
2771 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002772 * If there is only one window there is nothing to remove.
2773 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002774 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002775 return NULL;
2776
2777 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 * Remove the window from its frame.
2779 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002780 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 wp = frame2win(frp2);
2782
2783 /* Remove this frame from the list of frames. */
2784 frame_remove(frp_close);
2785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 if (frp_close->fr_parent->fr_layout == FR_COL)
2787 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002788 /* When 'winfixheight' is set, try to find another frame in the column
2789 * (as close to the closed frame as possible) to distribute the height
2790 * to. */
2791 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2792 {
2793 frp = frp_close->fr_prev;
2794 frp3 = frp_close->fr_next;
2795 while (frp != NULL || frp3 != NULL)
2796 {
2797 if (frp != NULL)
2798 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002799 if (!frame_fixed_height(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002800 {
2801 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002802 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002803 break;
2804 }
2805 frp = frp->fr_prev;
2806 }
2807 if (frp3 != NULL)
2808 {
2809 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2810 {
2811 frp2 = frp3;
2812 wp = frp3->fr_win;
2813 break;
2814 }
2815 frp3 = frp3->fr_next;
2816 }
2817 }
2818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2820 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 *dirp = 'v';
2822 }
2823 else
2824 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002825 /* When 'winfixwidth' is set, try to find another frame in the column
2826 * (as close to the closed frame as possible) to distribute the width
2827 * to. */
2828 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2829 {
2830 frp = frp_close->fr_prev;
2831 frp3 = frp_close->fr_next;
2832 while (frp != NULL || frp3 != NULL)
2833 {
2834 if (frp != NULL)
2835 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002836 if (!frame_fixed_width(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002837 {
2838 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002839 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002840 break;
2841 }
2842 frp = frp->fr_prev;
2843 }
2844 if (frp3 != NULL)
2845 {
2846 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2847 {
2848 frp2 = frp3;
2849 wp = frp3->fr_win;
2850 break;
2851 }
2852 frp3 = frp3->fr_next;
2853 }
2854 }
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002857 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 *dirp = 'h';
2859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861 /* If rows/columns go to a window below/right its positions need to be
2862 * updated. Can only be done after the sizes have been updated. */
2863 if (frp2 == frp_close->fr_next)
2864 {
2865 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002866 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867
2868 frame_comp_pos(frp2, &row, &col);
2869 }
2870
2871 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2872 {
2873 /* There is no other frame in this list, move its info to the parent
2874 * and remove it. */
2875 frp2->fr_parent->fr_layout = frp2->fr_layout;
2876 frp2->fr_parent->fr_child = frp2->fr_child;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002877 FOR_ALL_FRAMES(frp, frp2->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 frp->fr_parent = frp2->fr_parent;
2879 frp2->fr_parent->fr_win = frp2->fr_win;
2880 if (frp2->fr_win != NULL)
2881 frp2->fr_win->w_frame = frp2->fr_parent;
2882 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002883 if (topframe->fr_child == frp2)
2884 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 vim_free(frp2);
2886
2887 frp2 = frp->fr_parent;
2888 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2889 {
2890 /* The frame above the parent has the same layout, have to merge
2891 * the frames into this list. */
2892 if (frp2->fr_child == frp)
2893 frp2->fr_child = frp->fr_child;
2894 frp->fr_child->fr_prev = frp->fr_prev;
2895 if (frp->fr_prev != NULL)
2896 frp->fr_prev->fr_next = frp->fr_child;
2897 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2898 {
2899 frp3->fr_parent = frp2;
2900 if (frp3->fr_next == NULL)
2901 {
2902 frp3->fr_next = frp->fr_next;
2903 if (frp->fr_next != NULL)
2904 frp->fr_next->fr_prev = frp3;
2905 break;
2906 }
2907 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002908 if (topframe->fr_child == frp)
2909 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 vim_free(frp);
2911 }
2912 }
2913
2914 return wp;
2915}
2916
2917/*
Bram Moolenaarc136af22018-05-04 20:15:38 +02002918 * Return a pointer to the frame that will receive the empty screen space that
2919 * is left over after "win" is closed.
2920 *
2921 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
2922 * by default. Otherwise, the free space goes below or to the right. The
2923 * result is that opening a window and then immediately closing it will
2924 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
2925 * respected when possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 */
2927 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002928win_altframe(
2929 win_T *win,
2930 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 frame_T *frp;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002933 frame_T *other_fr, *target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002935 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002936 return alt_tabpage()->tp_curwin->w_frame;
2937
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 frp = win->w_frame;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002939
2940 if (frp->fr_prev == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 return frp->fr_next;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002942 if (frp->fr_next == NULL)
2943 return frp->fr_prev;
2944
2945 target_fr = frp->fr_next;
2946 other_fr = frp->fr_prev;
2947 if (p_spr || p_sb)
2948 {
2949 target_fr = frp->fr_prev;
2950 other_fr = frp->fr_next;
2951 }
2952
2953 /* If 'wfh' or 'wfw' is set for the target and not for the alternate
2954 * window, reverse the selection. */
2955 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2956 {
2957 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
2958 target_fr = other_fr;
2959 }
2960 else
2961 {
2962 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
2963 target_fr = other_fr;
2964 }
2965
2966 return target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967}
2968
2969/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002970 * Return the tabpage that will be used if the current one is closed.
2971 */
2972 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002973alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002974{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002975 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002976
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002977 /* Use the next tab page if possible. */
2978 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002979 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002980
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002981 /* Find the last but one tab page. */
2982 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
2983 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002984 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002985}
2986
2987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 * Find the left-upper window in frame "frp".
2989 */
2990 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002991frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 while (frp->fr_win == NULL)
2994 frp = frp->fr_child;
2995 return frp->fr_win;
2996}
2997
2998/*
2999 * Return TRUE if frame "frp" contains window "wp".
3000 */
3001 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003002frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 frame_T *p;
3005
3006 if (frp->fr_layout == FR_LEAF)
3007 return frp->fr_win == wp;
3008
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003009 FOR_ALL_FRAMES(p, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010 if (frame_has_win(p, wp))
3011 return TRUE;
3012 return FALSE;
3013}
3014
3015/*
3016 * Set a new height for a frame. Recursively sets the height for contained
3017 * frames and windows. Caller must take care of positions.
3018 */
3019 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003020frame_new_height(
3021 frame_T *topfrp,
3022 int height,
3023 int topfirst, /* resize topmost contained frame first */
3024 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 may cause the height not to be set */
3026{
3027 frame_T *frp;
3028 int extra_lines;
3029 int h;
3030
3031 if (topfrp->fr_win != NULL)
3032 {
3033 /* Simple case: just one window. */
3034 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02003035 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01003036 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else if (topfrp->fr_layout == FR_ROW)
3039 {
3040 do
3041 {
3042 /* All frames in this row get the same new height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003043 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 {
3045 frame_new_height(frp, height, topfirst, wfh);
3046 if (frp->fr_height > height)
3047 {
3048 /* Could not fit the windows, make the whole row higher. */
3049 height = frp->fr_height;
3050 break;
3051 }
3052 }
3053 }
3054 while (frp != NULL);
3055 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003056 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 {
3058 /* Complicated case: Resize a column of frames. Resize the bottom
3059 * frame first, frames above that when needed. */
3060
3061 frp = topfrp->fr_child;
3062 if (wfh)
3063 /* Advance past frames with one window with 'wfh' set. */
3064 while (frame_fixed_height(frp))
3065 {
3066 frp = frp->fr_next;
3067 if (frp == NULL)
3068 return; /* no frame without 'wfh', give up */
3069 }
3070 if (!topfirst)
3071 {
3072 /* Find the bottom frame of this column */
3073 while (frp->fr_next != NULL)
3074 frp = frp->fr_next;
3075 if (wfh)
3076 /* Advance back for frames with one window with 'wfh' set. */
3077 while (frame_fixed_height(frp))
3078 frp = frp->fr_prev;
3079 }
3080
3081 extra_lines = height - topfrp->fr_height;
3082 if (extra_lines < 0)
3083 {
3084 /* reduce height of contained frames, bottom or top frame first */
3085 while (frp != NULL)
3086 {
3087 h = frame_minheight(frp, NULL);
3088 if (frp->fr_height + extra_lines < h)
3089 {
3090 extra_lines += frp->fr_height - h;
3091 frame_new_height(frp, h, topfirst, wfh);
3092 }
3093 else
3094 {
3095 frame_new_height(frp, frp->fr_height + extra_lines,
3096 topfirst, wfh);
3097 break;
3098 }
3099 if (topfirst)
3100 {
3101 do
3102 frp = frp->fr_next;
3103 while (wfh && frp != NULL && frame_fixed_height(frp));
3104 }
3105 else
3106 {
3107 do
3108 frp = frp->fr_prev;
3109 while (wfh && frp != NULL && frame_fixed_height(frp));
3110 }
3111 /* Increase "height" if we could not reduce enough frames. */
3112 if (frp == NULL)
3113 height -= extra_lines;
3114 }
3115 }
3116 else if (extra_lines > 0)
3117 {
3118 /* increase height of bottom or top frame */
3119 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3120 }
3121 }
3122 topfrp->fr_height = height;
3123}
3124
3125/*
3126 * Return TRUE if height of frame "frp" should not be changed because of
3127 * the 'winfixheight' option.
3128 */
3129 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003130frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131{
3132 /* frame with one window: fixed height if 'winfixheight' set. */
3133 if (frp->fr_win != NULL)
3134 return frp->fr_win->w_p_wfh;
3135
3136 if (frp->fr_layout == FR_ROW)
3137 {
3138 /* The frame is fixed height if one of the frames in the row is fixed
3139 * height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003140 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 if (frame_fixed_height(frp))
3142 return TRUE;
3143 return FALSE;
3144 }
3145
3146 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3147 * frames in the row are fixed height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003148 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (!frame_fixed_height(frp))
3150 return FALSE;
3151 return TRUE;
3152}
3153
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003155 * Return TRUE if width of frame "frp" should not be changed because of
3156 * the 'winfixwidth' option.
3157 */
3158 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003159frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003160{
3161 /* frame with one window: fixed width if 'winfixwidth' set. */
3162 if (frp->fr_win != NULL)
3163 return frp->fr_win->w_p_wfw;
3164
3165 if (frp->fr_layout == FR_COL)
3166 {
3167 /* The frame is fixed width if one of the frames in the row is fixed
3168 * width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003169 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003170 if (frame_fixed_width(frp))
3171 return TRUE;
3172 return FALSE;
3173 }
3174
3175 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3176 * frames in the row are fixed width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003177 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003178 if (!frame_fixed_width(frp))
3179 return FALSE;
3180 return TRUE;
3181}
3182
3183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 * Add a status line to windows at the bottom of "frp".
3185 * Note: Does not check if there is room!
3186 */
3187 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003188frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189{
3190 win_T *wp;
3191
3192 if (frp->fr_layout == FR_LEAF)
3193 {
3194 wp = frp->fr_win;
3195 if (wp->w_status_height == 0)
3196 {
3197 if (wp->w_height > 0) /* don't make it negative */
3198 --wp->w_height;
3199 wp->w_status_height = STATUS_HEIGHT;
3200 }
3201 }
3202 else if (frp->fr_layout == FR_ROW)
3203 {
3204 /* Handle all the frames in the row. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003205 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 frame_add_statusline(frp);
3207 }
3208 else /* frp->fr_layout == FR_COL */
3209 {
3210 /* Only need to handle the last frame in the column. */
3211 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3212 ;
3213 frame_add_statusline(frp);
3214 }
3215}
3216
3217/*
3218 * Set width of a frame. Handles recursively going through contained frames.
3219 * May remove separator line for windows at the right side (for win_close()).
3220 */
3221 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003222frame_new_width(
3223 frame_T *topfrp,
3224 int width,
3225 int leftfirst, /* resize leftmost contained frame first */
3226 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003227 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 frame_T *frp;
3230 int extra_cols;
3231 int w;
3232 win_T *wp;
3233
3234 if (topfrp->fr_layout == FR_LEAF)
3235 {
3236 /* Simple case: just one window. */
3237 wp = topfrp->fr_win;
3238 /* Find out if there are any windows right of this one. */
3239 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3240 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3241 break;
3242 if (frp->fr_parent == NULL)
3243 wp->w_vsep_width = 0;
3244 win_new_width(wp, width - wp->w_vsep_width);
3245 }
3246 else if (topfrp->fr_layout == FR_COL)
3247 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003248 do
3249 {
3250 /* All frames in this column get the same new width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003251 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003252 {
3253 frame_new_width(frp, width, leftfirst, wfw);
3254 if (frp->fr_width > width)
3255 {
3256 /* Could not fit the windows, make whole column wider. */
3257 width = frp->fr_width;
3258 break;
3259 }
3260 }
3261 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 else /* fr_layout == FR_ROW */
3264 {
3265 /* Complicated case: Resize a row of frames. Resize the rightmost
3266 * frame first, frames left of it when needed. */
3267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003269 if (wfw)
3270 /* Advance past frames with one window with 'wfw' set. */
3271 while (frame_fixed_width(frp))
3272 {
3273 frp = frp->fr_next;
3274 if (frp == NULL)
3275 return; /* no frame without 'wfw', give up */
3276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003278 {
3279 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280 while (frp->fr_next != NULL)
3281 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003282 if (wfw)
3283 /* Advance back for frames with one window with 'wfw' set. */
3284 while (frame_fixed_width(frp))
3285 frp = frp->fr_prev;
3286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287
3288 extra_cols = width - topfrp->fr_width;
3289 if (extra_cols < 0)
3290 {
3291 /* reduce frame width, rightmost frame first */
3292 while (frp != NULL)
3293 {
3294 w = frame_minwidth(frp, NULL);
3295 if (frp->fr_width + extra_cols < w)
3296 {
3297 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003298 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 }
3300 else
3301 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003302 frame_new_width(frp, frp->fr_width + extra_cols,
3303 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 break;
3305 }
3306 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003307 {
3308 do
3309 frp = frp->fr_next;
3310 while (wfw && frp != NULL && frame_fixed_width(frp));
3311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003313 {
3314 do
3315 frp = frp->fr_prev;
3316 while (wfw && frp != NULL && frame_fixed_width(frp));
3317 }
3318 /* Increase "width" if we could not reduce enough frames. */
3319 if (frp == NULL)
3320 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
3322 }
3323 else if (extra_cols > 0)
3324 {
3325 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003326 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 }
3329 topfrp->fr_width = width;
3330}
3331
3332/*
3333 * Add the vertical separator to windows at the right side of "frp".
3334 * Note: Does not check if there is room!
3335 */
3336 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003337frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 win_T *wp;
3340
3341 if (frp->fr_layout == FR_LEAF)
3342 {
3343 wp = frp->fr_win;
3344 if (wp->w_vsep_width == 0)
3345 {
3346 if (wp->w_width > 0) /* don't make it negative */
3347 --wp->w_width;
3348 wp->w_vsep_width = 1;
3349 }
3350 }
3351 else if (frp->fr_layout == FR_COL)
3352 {
3353 /* Handle all the frames in the column. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003354 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 frame_add_vsep(frp);
3356 }
3357 else /* frp->fr_layout == FR_ROW */
3358 {
3359 /* Only need to handle the last frame in the row. */
3360 frp = frp->fr_child;
3361 while (frp->fr_next != NULL)
3362 frp = frp->fr_next;
3363 frame_add_vsep(frp);
3364 }
3365}
3366
3367/*
3368 * Set frame width from the window it contains.
3369 */
3370 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003371frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
3373 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3374}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376/*
3377 * Set frame height from the window it contains.
3378 */
3379 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003380frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003382 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383}
3384
3385/*
3386 * Compute the minimal height for frame "topfrp".
3387 * Uses the 'winminheight' option.
3388 * When "next_curwin" isn't NULL, use p_wh for this window.
3389 * When "next_curwin" is NOWIN, don't use at least one line for the current
3390 * window.
3391 */
3392 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003393frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 frame_T *frp;
3396 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
3399 if (topfrp->fr_win != NULL)
3400 {
3401 if (topfrp->fr_win == next_curwin)
3402 m = p_wh + topfrp->fr_win->w_status_height;
3403 else
3404 {
3405 /* window: minimal height of the window plus status line */
3406 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003407 if (topfrp->fr_win == curwin && next_curwin == NULL)
3408 {
3409 /* Current window is minimal one line high and WinBar is
3410 * visible. */
3411 if (p_wmh == 0)
3412 ++m;
3413 m += WINBAR_HEIGHT(curwin);
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 else if (topfrp->fr_layout == FR_ROW)
3418 {
3419 /* get the minimal height from each frame in this row */
3420 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003421 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 {
3423 n = frame_minheight(frp, next_curwin);
3424 if (n > m)
3425 m = n;
3426 }
3427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 else
3429 {
3430 /* Add up the minimal heights for all frames in this column. */
3431 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003432 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 m += frame_minheight(frp, next_curwin);
3434 }
3435
3436 return m;
3437}
3438
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439/*
3440 * Compute the minimal width for frame "topfrp".
3441 * When "next_curwin" isn't NULL, use p_wiw for this window.
3442 * When "next_curwin" is NOWIN, don't use at least one column for the current
3443 * window.
3444 */
3445 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003446frame_minwidth(
3447 frame_T *topfrp,
3448 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 frame_T *frp;
3451 int m, n;
3452
3453 if (topfrp->fr_win != NULL)
3454 {
3455 if (topfrp->fr_win == next_curwin)
3456 m = p_wiw + topfrp->fr_win->w_vsep_width;
3457 else
3458 {
3459 /* window: minimal width of the window plus separator column */
3460 m = p_wmw + topfrp->fr_win->w_vsep_width;
3461 /* Current window is minimal one column wide */
3462 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3463 ++m;
3464 }
3465 }
3466 else if (topfrp->fr_layout == FR_COL)
3467 {
3468 /* get the minimal width from each frame in this column */
3469 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003470 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
3472 n = frame_minwidth(frp, next_curwin);
3473 if (n > m)
3474 m = n;
3475 }
3476 }
3477 else
3478 {
3479 /* Add up the minimal widths for all frames in this row. */
3480 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003481 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 m += frame_minwidth(frp, next_curwin);
3483 }
3484
3485 return m;
3486}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488
3489/*
3490 * Try to close all windows except current one.
3491 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3492 * used and the buffer was modified.
3493 *
3494 * Used by ":bdel" and ":only".
3495 */
3496 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003497close_others(
3498 int message,
3499 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500{
3501 win_T *wp;
3502 win_T *nextwp;
3503 int r;
3504
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003505 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003507 if (message && !autocmd_busy)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003508 msg(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 return;
3510 }
3511
3512 /* Be very careful here: autocommands may change the window layout. */
3513 for (wp = firstwin; win_valid(wp); wp = nextwp)
3514 {
3515 nextwp = wp->w_next;
3516 if (wp != curwin) /* don't close current window */
3517 {
3518
3519 /* Check if it's allowed to abandon this window */
3520 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (!win_valid(wp)) /* autocommands messed wp up */
3522 {
3523 nextwp = firstwin;
3524 continue;
3525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 if (!r)
3527 {
3528#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3529 if (message && (p_confirm || cmdmod.confirm) && p_write)
3530 {
3531 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 if (!win_valid(wp)) /* autocommands messed wp up */
3533 {
3534 nextwp = firstwin;
3535 continue;
3536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538 if (bufIsChanged(wp->w_buffer))
3539#endif
3540 continue;
3541 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003542 win_close(wp, !buf_hide(wp->w_buffer)
3543 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545 }
3546
Bram Moolenaar459ca562016-11-10 18:16:33 +01003547 if (message && !ONE_WINDOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003548 emsg(_("E445: Other window contains changes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549}
3550
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003551 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003552win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003553{
3554 redraw_win_later(wp, NOT_VALID);
3555 wp->w_lines_valid = 0;
3556 wp->w_cursor.lnum = 1;
3557 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003558 wp->w_cursor.coladd = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003559 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3560 wp->w_pcmark.col = 0;
3561 wp->w_prev_pcmark.lnum = 0;
3562 wp->w_prev_pcmark.col = 0;
3563 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003565 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003567 wp->w_botline = 2;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003568#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003569 wp->w_s = &wp->w_buffer->b_s;
3570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573/*
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003574 * Init the current window "curwin".
3575 * Called when a new file is being edited.
3576 */
3577 void
3578curwin_init(void)
3579{
3580 win_init_empty(curwin);
3581}
3582
3583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 * Allocate the first window and put an empty buffer in it.
3585 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003586 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003588 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003589win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003591 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003592 return FAIL;
3593
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003594 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003595 if (first_tabpage == NULL)
3596 return FAIL;
3597 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003598 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003599
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003600 return OK;
3601}
3602
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003603/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003604 * Allocate and init a window that is not a regular window.
3605 * This can only be done after the first window is fully initialized, thus it
3606 * can't be in win_alloc_first().
3607 */
3608 win_T *
3609win_alloc_popup_win(void)
3610{
3611 win_T *wp;
3612
3613 wp = win_alloc(NULL, TRUE);
3614 if (wp != NULL)
3615 {
3616 // We need to initialize options with something, using the current
3617 // window makes most sense.
3618 win_init_some(wp, curwin);
3619
3620 RESET_BINDING(wp);
3621 new_frame(wp);
3622 }
3623 return wp;
3624}
3625
3626/*
3627 * Initialize window "wp" to display buffer "buf".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003628 */
3629 void
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003630win_init_popup_win(win_T *wp, buf_T *buf)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003631{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003632 wp->w_buffer = buf;
3633 ++buf->b_nwindows;
3634 win_init_empty(wp); // set cursor and topline to safe values
3635
3636 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in
3637 // win_enter_ext().
3638 VIM_CLEAR(wp->w_localdir);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003639}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003640
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003641/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003642 * Allocate the first window or the first window in a new tab page.
3643 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003644 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003645 * Return FAIL when something goes wrong (out of memory).
3646 */
3647 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003648win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003649{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003650 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003651 if (oldwin == NULL)
3652 {
3653 /* Very first window, need to create an empty buffer for it and
3654 * initialize from scratch. */
3655 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3656 if (curwin == NULL || curbuf == NULL)
3657 return FAIL;
3658 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003659#ifdef FEAT_SYN_HL
3660 curwin->w_s = &(curbuf->b_s);
3661#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003662 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003663 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003664 curwin_init(); /* init current window */
3665 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003666 else
3667 {
3668 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003669 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003670
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003671 /* We don't want cursor- and scroll-binding in the first window. */
3672 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003675 new_frame(curwin);
3676 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003677 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003678 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003681
3682 return OK;
3683}
3684
3685/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003686 * Create a frame for window "wp".
3687 */
3688 static void
3689new_frame(win_T *wp)
3690{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003691 frame_T *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003692
3693 wp->w_frame = frp;
3694 if (frp != NULL)
3695 {
3696 frp->fr_layout = FR_LEAF;
3697 frp->fr_win = wp;
3698 }
3699}
3700
3701/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003702 * Initialize the window and frame size to the maximum.
3703 */
3704 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003705win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003706{
3707 firstwin->w_height = ROWS_AVAIL;
3708 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003709 firstwin->w_width = Columns;
3710 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711}
3712
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003713/*
3714 * Allocate a new tabpage_T and init the values.
3715 * Returns NULL when out of memory.
3716 */
3717 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003718alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003719{
3720 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003721# ifdef FEAT_GUI
3722 int i;
3723# endif
3724
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003725
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003726 tp = ALLOC_CLEAR_ONE(tabpage_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02003727 if (tp == NULL)
3728 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003729
Bram Moolenaar429fa852013-04-15 12:27:36 +02003730# ifdef FEAT_EVAL
3731 /* init t: variables */
3732 tp->tp_vars = dict_alloc();
3733 if (tp->tp_vars == NULL)
3734 {
3735 vim_free(tp);
3736 return NULL;
3737 }
3738 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3739# endif
3740
3741# ifdef FEAT_GUI
3742 for (i = 0; i < 3; i++)
3743 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003744# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003745# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003746 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003747# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003748 tp->tp_ch_used = p_ch;
3749
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003750 return tp;
3751}
3752
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003753 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003754free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003755{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003756 int idx;
3757
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003758# ifdef FEAT_DIFF
3759 diff_clear(tp);
3760# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003761# ifdef FEAT_TEXT_PROP
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02003762 while (tp->tp_first_popupwin != NULL)
3763 popup_close_tabpage(tp, tp->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003764#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003765 for (idx = 0; idx < SNAP_COUNT; ++idx)
3766 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003768 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3769 hash_init(&tp->tp_vars->dv_hashtab);
3770 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003771#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003772
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003773 vim_free(tp->tp_localdir);
3774
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003775#ifdef FEAT_PYTHON
3776 python_tabpage_free(tp);
3777#endif
3778
3779#ifdef FEAT_PYTHON3
3780 python3_tabpage_free(tp);
3781#endif
3782
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003783 vim_free(tp);
3784}
3785
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003786/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003787 * Create a new Tab page with one window.
3788 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003789 * When "after" is 0 put it just after the current Tab page.
3790 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003791 * Return FAIL or OK.
3792 */
3793 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003794win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003795{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003796 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003797 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003798 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003799
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003800 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003801 if (newtp == NULL)
3802 return FAIL;
3803
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003804 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003805 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003806 {
3807 vim_free(newtp);
3808 return FAIL;
3809 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003810 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003811
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003812 newtp->tp_localdir = (tp->tp_localdir == NULL)
3813 ? NULL : vim_strsave(tp->tp_localdir);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003814 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003815 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003816 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003817 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003818 if (after == 1)
3819 {
3820 /* New tab page becomes the first one. */
3821 newtp->tp_next = first_tabpage;
3822 first_tabpage = newtp;
3823 }
3824 else
3825 {
3826 if (after > 0)
3827 {
3828 /* Put new tab page before tab page "after". */
3829 n = 2;
3830 for (tp = first_tabpage; tp->tp_next != NULL
3831 && n < after; tp = tp->tp_next)
3832 ++n;
3833 }
3834 newtp->tp_next = tp->tp_next;
3835 tp->tp_next = newtp;
3836 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003837 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003838 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003839 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003840
3841 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003842 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003843
3844#if defined(FEAT_GUI)
3845 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3846 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003847 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003848#endif
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003849#ifdef FEAT_JOB_CHANNEL
3850 entering_window(curwin);
3851#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003852
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01003853 redraw_all_later(NOT_VALID);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003854 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003855 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003856 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003857 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003858 return OK;
3859 }
3860
3861 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003862 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003863 return FAIL;
3864}
3865
3866/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003867 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3868 * like with ":split".
3869 * Returns OK if a new tab page was created, FAIL otherwise.
3870 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003871 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003872may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003873{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003874 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003875
Bram Moolenaard326ce82007-03-11 14:48:29 +00003876 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003877 {
3878 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003879 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003880 return win_new_tabpage(n);
3881 }
3882 return FAIL;
3883}
3884
3885/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003886 * Create up to "maxcount" tabpages with empty windows.
3887 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003888 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003889 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003890make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003891{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003892 int count = maxcount;
3893 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003894
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003895 /* Limit to 'tabpagemax' tabs. */
3896 if (count > p_tpm)
3897 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003898
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003899 /*
3900 * Don't execute autocommands while creating the tab pages. Must do that
3901 * when putting the buffers in the windows.
3902 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003903 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003904
3905 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003906 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003907 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003908
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003909 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003910
3911 /* return actual number of tab pages */
3912 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003913}
3914
3915/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003916 * Return TRUE when "tpc" points to a valid tab page.
3917 */
3918 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003919valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003920{
3921 tabpage_T *tp;
3922
Bram Moolenaar29323592016-07-24 22:04:11 +02003923 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003924 if (tp == tpc)
3925 return TRUE;
3926 return FALSE;
3927}
3928
3929/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003930 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3931 * valid.
3932 */
3933 int
3934valid_tabpage_win(tabpage_T *tpc)
3935{
3936 tabpage_T *tp;
3937 win_T *wp;
3938
3939 FOR_ALL_TABPAGES(tp)
3940 {
3941 if (tp == tpc)
3942 {
3943 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3944 {
3945 if (win_valid_any_tab(wp))
3946 return TRUE;
3947 }
3948 return FALSE;
3949 }
3950 }
3951 /* shouldn't happen */
3952 return FALSE;
3953}
3954
3955/*
3956 * Close tabpage "tab", assuming it has no windows in it.
3957 * There must be another tabpage or this will crash.
3958 */
3959 void
3960close_tabpage(tabpage_T *tab)
3961{
3962 tabpage_T *ptp;
3963
3964 if (tab == first_tabpage)
3965 {
3966 first_tabpage = tab->tp_next;
3967 ptp = first_tabpage;
3968 }
3969 else
3970 {
3971 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3972 ptp = ptp->tp_next)
3973 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003974 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003975 ptp->tp_next = tab->tp_next;
3976 }
3977
3978 goto_tabpage_tp(ptp, FALSE, FALSE);
3979 free_tabpage(tab);
3980}
3981
3982/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003983 * Find tab page "n" (first one is 1). Returns NULL when not found.
3984 */
3985 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003986find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003987{
3988 tabpage_T *tp;
3989 int i = 1;
3990
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003991 if (n == 0)
3992 return curtab;
3993
Bram Moolenaarf740b292006-02-16 22:11:02 +00003994 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
3995 ++i;
3996 return tp;
3997}
3998
3999/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004000 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004001 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004002 */
4003 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004004tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004005{
4006 int i = 1;
4007 tabpage_T *tp;
4008
4009 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
4010 ++i;
4011 return i;
4012}
4013
4014/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004015 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004016 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004017 * FAIL.
4018 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004019 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004020 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004021leave_tabpage(
4022 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004023 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004024 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004025{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004026 tabpage_T *tp = curtab;
4027
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004028#ifdef FEAT_JOB_CHANNEL
4029 leaving_window(curwin);
4030#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004031 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004032 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004033 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004034 if (new_curbuf != curbuf)
4035 {
4036 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4037 if (curtab != tp)
4038 return FAIL;
4039 }
4040 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4041 if (curtab != tp)
4042 return FAIL;
4043 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004044 if (curtab != tp)
4045 return FAIL;
4046 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004047#if defined(FEAT_GUI)
4048 /* Remove the scrollbars. They may be added back later. */
4049 if (gui.in_use)
4050 gui_remove_scrollbars();
4051#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004052 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004053 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004054 tp->tp_firstwin = firstwin;
4055 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004056 tp->tp_old_Rows = Rows;
4057 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004058 firstwin = NULL;
4059 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004060 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004061}
4062
4063/*
4064 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004065 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004066 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4067 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004068 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004069 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004070enter_tabpage(
4071 tabpage_T *tp,
4072 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01004073 int trigger_enter_autocmds,
4074 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004075{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004076 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004077 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004078
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004079 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004080 firstwin = tp->tp_firstwin;
4081 lastwin = tp->tp_lastwin;
4082 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004083
4084 /* We would like doing the TabEnter event first, but we don't have a
4085 * valid current window yet, which may break some commands.
4086 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02004087 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02004088 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00004089 prevwin = next_prevwin;
4090
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004091 last_status(FALSE); /* status line may appear or disappear */
4092 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004093#ifdef FEAT_DIFF
4094 diff_need_scrollbind = TRUE;
4095#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004096
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004097 /* The tabpage line may have appeared or disappeared, may need to resize
4098 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004099 * frame sizes too. Use the stored value of p_ch, so that it can be
4100 * different for each tab page. */
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +02004101 if (p_ch != curtab->tp_ch_used)
4102 clear_cmdline = TRUE;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004103 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004104 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
4105#ifdef FEAT_GUI_TABLINE
4106 && !gui_use_tabline()
4107#endif
4108 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004109 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004110 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004111 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004112
4113#if defined(FEAT_GUI)
4114 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
4115 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004116 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004117#endif
4118
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004119 /* Apply autocommands after updating the display, when 'rows' and
4120 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004121 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004122 {
4123 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
4124 if (old_curbuf != curbuf)
4125 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4126 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004127
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01004128 redraw_all_later(NOT_VALID);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004129}
4130
4131/*
4132 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004133 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004134 */
4135 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004136goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004137{
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004138 tabpage_T *tp = NULL; // shut up compiler
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004139 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004140 int i;
4141
Bram Moolenaard68071d2006-05-02 22:08:30 +00004142 if (text_locked())
4143 {
4144 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004145 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00004146 return;
4147 }
4148
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004149 /* If there is only one it can't work. */
4150 if (first_tabpage->tp_next == NULL)
4151 {
4152 if (n > 1)
4153 beep_flush();
4154 return;
4155 }
4156
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004157 if (n == 0)
4158 {
4159 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004160 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004161 tp = first_tabpage;
4162 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004163 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004164 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004165 else if (n < 0)
4166 {
4167 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4168 * this N times. */
4169 ttp = curtab;
4170 for (i = n; i < 0; ++i)
4171 {
4172 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4173 tp = tp->tp_next)
4174 ;
4175 ttp = tp;
4176 }
4177 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004178 else if (n == 9999)
4179 {
4180 /* Go to last tab page. */
4181 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4182 ;
4183 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004184 else
4185 {
4186 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004187 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004188 if (tp == NULL)
4189 {
4190 beep_flush();
4191 return;
4192 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004193 }
4194
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004195 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004196
4197#ifdef FEAT_GUI_TABLINE
4198 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004199 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004200#endif
4201}
4202
4203/*
4204 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004205 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4206 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004207 * Note: doesn't update the GUI tab.
4208 */
4209 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004210goto_tabpage_tp(
4211 tabpage_T *tp,
4212 int trigger_enter_autocmds,
4213 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004214{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004215 /* Don't repeat a message in another tab page. */
4216 set_keep_msg(NULL, 0);
4217
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004218 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4219 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004220 {
4221 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004222 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4223 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004224 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004225 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4226 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004227 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004228}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
4230/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004231 * Enter window "wp" in tab page "tp".
4232 * Also updates the GUI tab.
4233 */
4234 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004235goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004236{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004237 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004238 if (curtab == tp && win_valid(wp))
4239 {
4240 win_enter(wp, TRUE);
4241# ifdef FEAT_GUI_TABLINE
4242 if (gui_use_tabline())
4243 gui_mch_set_curtab(tabpage_index(curtab));
4244# endif
4245 }
4246}
4247
4248/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004249 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004250 */
4251 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004252tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004253{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004254 int n = 1;
4255 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004256
4257 if (first_tabpage->tp_next == NULL)
4258 return;
4259
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004260 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4261 ++n;
4262
4263 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4264 && tp->tp_next == curtab))
4265 return;
4266
4267 tp_dst = tp;
4268
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004269 /* Remove the current tab page from the list of tab pages. */
4270 if (curtab == first_tabpage)
4271 first_tabpage = curtab->tp_next;
4272 else
4273 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004274 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004275 if (tp->tp_next == curtab)
4276 break;
4277 if (tp == NULL) /* "cannot happen" */
4278 return;
4279 tp->tp_next = curtab->tp_next;
4280 }
4281
4282 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004283 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004284 {
4285 curtab->tp_next = first_tabpage;
4286 first_tabpage = curtab;
4287 }
4288 else
4289 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004290 curtab->tp_next = tp_dst->tp_next;
4291 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004292 }
4293
4294 /* Need to redraw the tabline. Tab page contents doesn't change. */
4295 redraw_tabline = TRUE;
4296}
4297
4298
4299/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 * Go to another window.
4301 * When jumping to another buffer, stop Visual mode. Do this before
4302 * changing windows so we can yank the selection into the '*' register.
4303 * When jumping to another window on the same buffer, adjust its cursor
4304 * position to keep the same Visual area.
4305 */
4306 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004307win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004309#ifdef FEAT_CONCEAL
4310 win_T *owp = curwin;
4311#endif
4312
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004313 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004314 return;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004315 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 {
4317 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004318 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 return;
4320 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004321 if (curbuf_locked())
4322 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004323
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 if (wp->w_buffer != curbuf)
4325 reset_VIsual_and_resel();
4326 else if (VIsual_active)
4327 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329#ifdef FEAT_GUI
4330 need_mouse_correct = TRUE;
4331#endif
4332 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004333
4334#ifdef FEAT_CONCEAL
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004335 // Conceal cursor line in previous window, unconceal in current window.
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004336 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004337 redrawWinline(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004338 if (curwin->w_p_cole > 0 && !msg_scrolled)
4339 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341}
4342
4343#if defined(FEAT_PERL) || defined(PROTO)
4344/*
4345 * Find window number "winnr" (counting top to bottom).
4346 */
4347 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004348win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349{
4350 win_T *wp;
4351
Bram Moolenaar29323592016-07-24 22:04:11 +02004352 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 if (--winnr == 0)
4354 break;
4355 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356}
4357#endif
4358
Bram Moolenaar4033c552017-09-16 20:54:51 +02004359#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004360/*
4361 * Find the tabpage for window "win".
4362 */
4363 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004364win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004365{
4366 win_T *wp;
4367 tabpage_T *tp;
4368
Bram Moolenaar29323592016-07-24 22:04:11 +02004369 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004370 if (wp == win)
4371 return tp;
4372 return NULL;
4373}
4374#endif
4375
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004377 * Get the above or below neighbor window of the specified window.
4378 * up - TRUE for the above neighbor
4379 * count - nth neighbor window
4380 * Returns the specified window if the neighbor is not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 */
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004382 win_T *
4383win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 frame_T *fr;
4386 frame_T *nfr;
4387 frame_T *foundfr;
4388
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004389 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 while (count--)
4391 {
4392 /*
4393 * First go upwards in the tree of frames until we find a upwards or
4394 * downwards neighbor.
4395 */
4396 fr = foundfr;
4397 for (;;)
4398 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004399 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 goto end;
4401 if (up)
4402 nfr = fr->fr_prev;
4403 else
4404 nfr = fr->fr_next;
4405 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4406 break;
4407 fr = fr->fr_parent;
4408 }
4409
4410 /*
4411 * Now go downwards to find the bottom or top frame in it.
4412 */
4413 for (;;)
4414 {
4415 if (nfr->fr_layout == FR_LEAF)
4416 {
4417 foundfr = nfr;
4418 break;
4419 }
4420 fr = nfr->fr_child;
4421 if (nfr->fr_layout == FR_ROW)
4422 {
4423 /* Find the frame at the cursor row. */
4424 while (fr->fr_next != NULL
4425 && frame2win(fr)->w_wincol + fr->fr_width
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004426 <= wp->w_wincol + wp->w_wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 fr = fr->fr_next;
4428 }
4429 if (nfr->fr_layout == FR_COL && up)
4430 while (fr->fr_next != NULL)
4431 fr = fr->fr_next;
4432 nfr = fr;
4433 }
4434 }
4435end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004436 return foundfr != NULL ? foundfr->fr_win : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437}
4438
4439/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004440 * Move to window above or below "count" times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 */
4442 static void
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004443win_goto_ver(
4444 int up, // TRUE to go to win above
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004445 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004447 win_T *win;
4448
4449 win = win_vert_neighbor(curtab, curwin, up, count);
4450 if (win != NULL)
4451 win_goto(win);
4452}
4453
4454/*
4455 * Get the left or right neighbor window of the specified window.
4456 * left - TRUE for the left neighbor
4457 * count - nth neighbor window
4458 * Returns the specified window if the neighbor is not found.
4459 */
4460 win_T *
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02004461win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count)
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004462{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 frame_T *fr;
4464 frame_T *nfr;
4465 frame_T *foundfr;
4466
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004467 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 while (count--)
4469 {
4470 /*
4471 * First go upwards in the tree of frames until we find a left or
4472 * right neighbor.
4473 */
4474 fr = foundfr;
4475 for (;;)
4476 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004477 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 goto end;
4479 if (left)
4480 nfr = fr->fr_prev;
4481 else
4482 nfr = fr->fr_next;
4483 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4484 break;
4485 fr = fr->fr_parent;
4486 }
4487
4488 /*
4489 * Now go downwards to find the leftmost or rightmost frame in it.
4490 */
4491 for (;;)
4492 {
4493 if (nfr->fr_layout == FR_LEAF)
4494 {
4495 foundfr = nfr;
4496 break;
4497 }
4498 fr = nfr->fr_child;
4499 if (nfr->fr_layout == FR_COL)
4500 {
4501 /* Find the frame at the cursor row. */
4502 while (fr->fr_next != NULL
4503 && frame2win(fr)->w_winrow + fr->fr_height
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004504 <= wp->w_winrow + wp->w_wrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 fr = fr->fr_next;
4506 }
4507 if (nfr->fr_layout == FR_ROW && left)
4508 while (fr->fr_next != NULL)
4509 fr = fr->fr_next;
4510 nfr = fr;
4511 }
4512 }
4513end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004514 return foundfr != NULL ? foundfr->fr_win : NULL;
4515}
4516
4517/*
4518 * Move to left or right window.
4519 */
4520 static void
4521win_goto_hor(
4522 int left, // TRUE to go to left win
4523 long count)
4524{
4525 win_T *win;
4526
4527 win = win_horz_neighbor(curtab, curwin, left, count);
4528 if (win != NULL)
4529 win_goto(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532/*
4533 * Make window "wp" the current window.
4534 */
4535 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004536win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004538 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539}
4540
4541/*
4542 * Make window wp the current window.
4543 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4544 * been closed and isn't valid.
4545 */
4546 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004547win_enter_ext(
4548 win_T *wp,
4549 int undo_sync,
4550 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004551 int trigger_new_autocmds,
4552 int trigger_enter_autocmds,
4553 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557 if (wp == curwin && !curwin_invalid) /* nothing to do */
4558 return;
4559
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004560#ifdef FEAT_JOB_CHANNEL
4561 if (!curwin_invalid)
4562 leaving_window(curwin);
4563#endif
4564
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004565 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 {
4567 /*
4568 * Be careful: If autocommands delete the window, return now.
4569 */
4570 if (wp->w_buffer != curbuf)
4571 {
4572 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4573 other_buffer = TRUE;
4574 if (!win_valid(wp))
4575 return;
4576 }
4577 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4578 if (!win_valid(wp))
4579 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004580#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 /* autocmds may abort script processing */
4582 if (aborting())
4583 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 /* sync undo before leaving the current buffer */
4588 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004589 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004590
4591 /* Might need to scroll the old window before switching, e.g., when the
4592 * cursor was moved. */
4593 update_topline();
4594
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 /* may have to copy the buffer options when 'cpo' contains 'S' */
4596 if (wp->w_buffer != curbuf)
4597 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4598 if (!curwin_invalid)
4599 {
4600 prevwin = curwin; /* remember for CTRL-W p */
4601 curwin->w_redr_status = TRUE;
4602 }
4603 curwin = wp;
4604 curbuf = wp->w_buffer;
4605 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (!virtual_active())
4607 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 changed_line_abv_curs(); /* assume cursor position needs updating */
4609
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004610 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004612 char_u *dirname;
4613
4614 // Window or tab has a local directory: Save current directory as
4615 // global directory (unless that was done already) and change to the
4616 // local directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (globaldir == NULL)
4618 {
4619 char_u cwd[MAXPATHL];
4620
4621 if (mch_dirname(cwd, MAXPATHL) == OK)
4622 globaldir = vim_strsave(cwd);
4623 }
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004624 if (curwin->w_localdir != NULL)
4625 dirname = curwin->w_localdir;
4626 else
4627 dirname = curtab->tp_localdir;
4628
4629 if (mch_chdir((char *)dirname) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004630 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
4632 else if (globaldir != NULL)
4633 {
4634 /* Window doesn't have a local directory and we are not in the global
4635 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004636 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004637 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 shorten_fnames(TRUE);
4639 }
4640
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004641#ifdef FEAT_JOB_CHANNEL
4642 entering_window(curwin);
4643#endif
Bram Moolenaarc917da42016-07-19 22:31:36 +02004644 if (trigger_new_autocmds)
4645 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004646 if (trigger_enter_autocmds)
4647 {
4648 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4649 if (other_buffer)
4650 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652
4653#ifdef FEAT_TITLE
4654 maketitle();
4655#endif
4656 curwin->w_redr_status = TRUE;
Bram Moolenaarfbbd1022019-10-07 22:38:58 +02004657#ifdef FEAT_TERMINAL
Bram Moolenaara27e1dc2019-10-07 22:27:36 +02004658 if (bt_terminal(wp->w_buffer))
4659 // terminal is likely in another mode
4660 redraw_mode = TRUE;
Bram Moolenaarfbbd1022019-10-07 22:38:58 +02004661#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004662 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (restart_edit)
4664 redraw_later(VALID); /* causes status line redraw */
4665
4666 /* set window height to desired minimal value */
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02004667 if (curwin->w_height < p_wh && !curwin->w_p_wfh
4668#ifdef FEAT_TEXT_PROP
4669 && !popup_is_popup(curwin)
4670#endif
4671 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 win_setheight((int)p_wh);
4673 else if (curwin->w_height == 0)
4674 win_setheight(1);
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004677 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004680 setmouse(); // in case jumped to/from help buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004682 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004683 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684}
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004688 * Jump to the first open window that contains buffer "buf", if one exists.
4689 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 */
4691 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004692buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004694 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004696 if (curwin->w_buffer == buf)
4697 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004698 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004699 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004700 if (wp->w_buffer == buf)
4701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 if (wp != NULL)
4703 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004704 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004706
4707/*
4708 * Jump to the first open window in any tab page that contains buffer "buf",
4709 * if one exists.
4710 * Returns a pointer to the window found, otherwise NULL.
4711 */
4712 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004713buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004714{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004715 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004716 tabpage_T *tp;
4717
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004718 if (wp != NULL)
4719 return wp;
4720
Bram Moolenaar29323592016-07-24 22:04:11 +02004721 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004722 if (tp != curtab)
4723 {
4724 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4725 if (wp->w_buffer == buf)
4726 break;
4727 if (wp != NULL)
4728 {
4729 goto_tabpage_win(tp, wp);
4730 if (curwin != wp)
4731 wp = NULL; /* something went wrong */
4732 break;
4733 }
4734 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004735 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004736}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004738static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004741 * Allocate a window structure and link it in the window list when "hidden" is
4742 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004745win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004747 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
4749 /*
4750 * allocate window structure and linesizes arrays
4751 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004752 new_wp = ALLOC_CLEAR_ONE(win_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004753 if (new_wp == NULL)
4754 return NULL;
4755
4756 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004758 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004759 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761
Bram Moolenaar86edef62016-03-13 18:07:30 +01004762 new_wp->w_id = ++last_win_id;
4763
Bram Moolenaar429fa852013-04-15 12:27:36 +02004764#ifdef FEAT_EVAL
4765 /* init w: variables */
4766 new_wp->w_vars = dict_alloc();
4767 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004769 win_free_lsize(new_wp);
4770 vim_free(new_wp);
4771 return NULL;
4772 }
4773 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004774#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004775
Bram Moolenaar429fa852013-04-15 12:27:36 +02004776 /* Don't execute autocommands while the window is not properly
4777 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4778 * event. */
4779 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004780
Bram Moolenaar429fa852013-04-15 12:27:36 +02004781 /*
4782 * link the window in the window list
4783 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004784 if (!hidden)
4785 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004786 new_wp->w_wincol = 0;
4787 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
Bram Moolenaar429fa852013-04-15 12:27:36 +02004789 /* position the display and the cursor at the top of the file. */
4790 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004792 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004794 new_wp->w_botline = 2;
4795 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004796 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
Bram Moolenaar375e3392019-01-31 18:26:10 +01004798 // use global option value for global-local options
4799 new_wp->w_p_so = -1;
4800 new_wp->w_p_siso = -1;
4801
Bram Moolenaar429fa852013-04-15 12:27:36 +02004802 /* We won't calculate w_fraction until resizing the window */
4803 new_wp->w_fraction = 0;
4804 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
4806#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004807 if (gui.in_use)
4808 {
4809 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4810 SBAR_LEFT, new_wp);
4811 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4812 SBAR_RIGHT, new_wp);
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#endif
4815#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004816 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004818 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004819#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004820 new_wp->w_match_head = NULL;
4821 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004822#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004823 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824}
4825
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004827 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 */
4829 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004830win_free(
4831 win_T *wp,
4832 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
4834 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004835 buf_T *buf;
4836 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004838#ifdef FEAT_FOLDING
4839 clearFolding(wp);
4840#endif
4841
4842 /* reduce the reference count to the argument list. */
4843 alist_unlink(wp->w_alist);
4844
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004845 /* Don't execute autocommands while the window is halfway being deleted.
4846 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004847 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004848
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004849#ifdef FEAT_LUA
4850 lua_window_free(wp);
4851#endif
4852
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004853#ifdef FEAT_MZSCHEME
4854 mzscheme_window_free(wp);
4855#endif
4856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#ifdef FEAT_PERL
4858 perl_win_free(wp);
4859#endif
4860
4861#ifdef FEAT_PYTHON
4862 python_window_free(wp);
4863#endif
4864
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004865#ifdef FEAT_PYTHON3
4866 python3_window_free(wp);
4867#endif
4868
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869#ifdef FEAT_TCL
4870 tcl_window_free(wp);
4871#endif
4872
4873#ifdef FEAT_RUBY
4874 ruby_window_free(wp);
4875#endif
4876
4877 clear_winopt(&wp->w_onebuf_opt);
4878 clear_winopt(&wp->w_allbuf_opt);
4879
4880#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004881 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4882 hash_init(&wp->w_vars->dv_hashtab);
4883 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#endif
4885
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004886 {
4887 tabpage_T *ttp;
4888
4889 if (prevwin == wp)
4890 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004891 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004892 if (ttp->tp_prevwin == wp)
4893 ttp->tp_prevwin = NULL;
4894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 win_free_lsize(wp);
4896
4897 for (i = 0; i < wp->w_tagstacklen; ++i)
Bram Moolenaar55008aa2019-09-01 20:21:56 +02004898 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 vim_free(wp->w_tagstack[i].tagname);
Bram Moolenaar55008aa2019-09-01 20:21:56 +02004900 vim_free(wp->w_tagstack[i].user_data);
4901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004903
Bram Moolenaarff18df02013-07-24 17:51:57 +02004904 /* Remove the window from the b_wininfo lists, it may happen that the
4905 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004906 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004907 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4908 if (wip->wi_win == wp)
4909 wip->wi_win = NULL;
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004912 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#ifdef FEAT_JUMPLIST
4916 free_jumplist(wp);
4917#endif
4918
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004919#ifdef FEAT_QUICKFIX
4920 qf_free_all(wp);
4921#endif
4922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923#ifdef FEAT_GUI
4924 if (gui.in_use)
4925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4927 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4928 }
4929#endif /* FEAT_GUI */
4930
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004931#ifdef FEAT_MENU
4932 remove_winbar(wp);
4933#endif
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004934#ifdef FEAT_TEXT_PROP
Bram Moolenaar9eaac892019-06-01 22:49:29 +02004935 free_callback(&wp->w_close_cb);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004936 free_callback(&wp->w_filter_cb);
Bram Moolenaar790498b2019-06-01 22:15:29 +02004937 for (i = 0; i < 4; ++i)
4938 VIM_CLEAR(wp->w_border_highlight[i]);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004939 vim_free(wp->w_scrollbar_highlight);
4940 vim_free(wp->w_thumb_highlight);
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004941 vim_free(wp->w_popup_title);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004942 list_unref(wp->w_popup_mask);
Bram Moolenaar0aca2932019-07-26 22:22:38 +02004943 vim_free(wp->w_popup_mask_cells);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004944#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004945
Bram Moolenaar860cae12010-06-05 23:22:07 +02004946#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004947 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004948#endif
4949
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004950 if (win_valid_any_tab(wp))
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004951 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004952 if (autocmd_busy)
4953 {
4954 wp->w_next = au_pending_free_win;
4955 au_pending_free_win = wp;
4956 }
4957 else
4958 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004959
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004960 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961}
4962
4963/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004964 * Return TRUE if "wp" is not in the list of windows: the autocmd window or a
4965 * popup window.
4966 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004967 static int
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004968win_unlisted(win_T *wp)
4969{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004970 return wp == aucmd_win || WIN_IS_POPUP(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004971}
4972
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004973#if defined(FEAT_TEXT_PROP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004974/*
4975 * Free a popup window. This does not take the window out of the window list
4976 * and assumes there is only one toplevel frame, no split.
4977 */
4978 void
4979win_free_popup(win_T *win)
4980{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004981 if (bt_popup(win->w_buffer))
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004982 win_close_buffer(win, DOBUF_WIPE_REUSE, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004983 else
4984 close_buffer(win, win->w_buffer, 0, FALSE);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004985# if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02004986 if (win->w_popup_timer != NULL)
4987 stop_timer(win->w_popup_timer);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004988# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004989 vim_free(win->w_frame);
4990 win_free(win, NULL);
4991}
Bram Moolenaar35d5af62019-05-26 20:44:10 +02004992#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004993
4994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 * Append window "wp" in the window list after window "after".
4996 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004997 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004998win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
5000 win_T *before;
5001
5002 if (after == NULL) /* after NULL is in front of the first */
5003 before = firstwin;
5004 else
5005 before = after->w_next;
5006
5007 wp->w_next = before;
5008 wp->w_prev = after;
5009 if (after == NULL)
5010 firstwin = wp;
5011 else
5012 after->w_next = wp;
5013 if (before == NULL)
5014 lastwin = wp;
5015 else
5016 before->w_prev = wp;
5017}
5018
5019/*
5020 * Remove a window from the window list.
5021 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00005022 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005023win_remove(
5024 win_T *wp,
5025 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
5027 if (wp->w_prev != NULL)
5028 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005029 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02005030 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005031 else
5032 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02005033
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 if (wp->w_next != NULL)
5035 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005036 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02005037 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005038 else
5039 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040}
5041
5042/*
5043 * Append frame "frp" in a frame list after frame "after".
5044 */
5045 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005046frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
5048 frp->fr_next = after->fr_next;
5049 after->fr_next = frp;
5050 if (frp->fr_next != NULL)
5051 frp->fr_next->fr_prev = frp;
5052 frp->fr_prev = after;
5053}
5054
5055/*
5056 * Insert frame "frp" in a frame list before frame "before".
5057 */
5058 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005059frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 frp->fr_next = before;
5062 frp->fr_prev = before->fr_prev;
5063 before->fr_prev = frp;
5064 if (frp->fr_prev != NULL)
5065 frp->fr_prev->fr_next = frp;
5066 else
5067 frp->fr_parent->fr_child = frp;
5068}
5069
5070/*
5071 * Remove a frame from a frame list.
5072 */
5073 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005074frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
5076 if (frp->fr_prev != NULL)
5077 frp->fr_prev->fr_next = frp->fr_next;
5078 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01005079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01005081 /* special case: topframe->fr_child == frp */
5082 if (topframe->fr_child == frp)
5083 topframe->fr_child = frp->fr_next;
5084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (frp->fr_next != NULL)
5086 frp->fr_next->fr_prev = frp->fr_prev;
5087}
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089/*
5090 * Allocate w_lines[] for window "wp".
5091 * Return FAIL for failure, OK for success.
5092 */
5093 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005094win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
5096 wp->w_lines_valid = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005097 wp->w_lines = ALLOC_CLEAR_MULT(wline_T, Rows );
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 if (wp->w_lines == NULL)
5099 return FAIL;
5100 return OK;
5101}
5102
5103/*
5104 * free lsize arrays for a window
5105 */
5106 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005107win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02005109 /* TODO: why would wp be NULL here? */
5110 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005111 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112}
5113
5114/*
5115 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005116 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 */
5118 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005119shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005121 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 if (firstwin == NULL) /* not initialized yet */
5124 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (h < frame_minheight(topframe, NULL))
5126 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005127
5128 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 * that doesn't result in the right height, forget about that option. */
5130 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005131 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 frame_new_height(topframe, h, FALSE, FALSE);
5133
5134 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005136 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005137
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138#if 0
5139 /* Disabled: don't want making the screen smaller make a window larger. */
5140 if (p_ea)
5141 win_equal(curwin, FALSE, 'v');
5142#endif
5143}
5144
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145/*
5146 * Called from win_new_shellsize() after Columns changed.
5147 */
5148 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005149shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150{
5151 if (firstwin == NULL) /* not initialized yet */
5152 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005153
5154 /* First try setting the widths of windows with 'winfixwidth'. If that
5155 * doesn't result in the right width, forget about that option. */
5156 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005157 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005158 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
5159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
5161#if 0
5162 /* Disabled: don't want making the screen smaller make a window larger. */
5163 if (p_ea)
5164 win_equal(curwin, FALSE, 'h');
5165#endif
5166}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
5168#if defined(FEAT_CMDWIN) || defined(PROTO)
5169/*
5170 * Save the size of all windows in "gap".
5171 */
5172 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005173win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174
5175{
5176 win_T *wp;
5177
5178 ga_init2(gap, (int)sizeof(int), 1);
5179 if (ga_grow(gap, win_count() * 2) == OK)
Bram Moolenaar29323592016-07-24 22:04:11 +02005180 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
5182 ((int *)gap->ga_data)[gap->ga_len++] =
5183 wp->w_width + wp->w_vsep_width;
5184 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5185 }
5186}
5187
5188/*
5189 * Restore window sizes, but only if the number of windows is still the same.
5190 * Does not free the growarray.
5191 */
5192 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005193win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194{
5195 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005196 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198 if (win_count() * 2 == gap->ga_len)
5199 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005200 /* The order matters, because frames contain other frames, but it's
5201 * difficult to get right. The easy way out is to do it twice. */
5202 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005204 i = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02005205 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02005206 {
5207 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5208 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
5211 /* recompute the window positions */
5212 (void)win_comp_pos();
5213 }
5214}
5215#endif /* FEAT_CMDWIN */
5216
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217/*
5218 * Update the position for all windows, using the width and height of the
5219 * frames.
5220 * Returns the row just after the last window.
5221 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005222 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005223win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005225 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 int col = 0;
5227
5228 frame_comp_pos(topframe, &row, &col);
5229 return row;
5230}
5231
5232/*
5233 * Update the position of the windows in frame "topfrp", using the width and
5234 * height of the frames.
5235 * "*row" and "*col" are the top-left position of the frame. They are updated
5236 * to the bottom-right position plus one.
5237 */
5238 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005239frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240{
5241 win_T *wp;
5242 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 int startcol;
5244 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005245 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246
5247 wp = topfrp->fr_win;
5248 if (wp != NULL)
5249 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005250 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
5252 /* position changed, redraw */
5253 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 redraw_win_later(wp, NOT_VALID);
5256 wp->w_redr_status = TRUE;
5257 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005258 /* WinBar will not show if the window height is zero */
5259 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5260 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 }
5263 else
5264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 startrow = *row;
5266 startcol = *col;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005267 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 if (topfrp->fr_layout == FR_ROW)
5270 *row = startrow; /* all frames are at the same row */
5271 else
5272 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 frame_comp_pos(frp, row, col);
5274 }
5275 }
5276}
5277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278/*
5279 * Set current window height and take care of repositioning other windows to
5280 * fit around it.
5281 */
5282 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005283win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284{
5285 win_setheight_win(height, curwin);
5286}
5287
5288/*
5289 * Set the window height of window "win" and take care of repositioning other
5290 * windows to fit around it.
5291 */
5292 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005293win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294{
5295 int row;
5296
5297 if (win == curwin)
5298 {
5299 /* Always keep current window at least one line high, even when
5300 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (height < p_wmh)
5302 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (height == 0)
5304 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005305 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 frame_setheight(win->w_frame, height + win->w_status_height);
5309
5310 /* recompute the window positions */
5311 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
5313 /*
5314 * If there is extra space created between the last window and the command
5315 * line, clear it.
5316 */
5317 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5318 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5319 cmdline_row = row;
5320 msg_row = row;
5321 msg_col = 0;
5322
5323 redraw_all_later(NOT_VALID);
5324}
5325
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326/*
5327 * Set the height of a frame to "height" and take care that all frames and
5328 * windows inside it are resized. Also resize frames on the left and right if
5329 * the are in the same FR_ROW frame.
5330 *
5331 * Strategy:
5332 * If the frame is part of a FR_COL frame, try fitting the frame in that
5333 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5334 * go to containing frames to resize them and make room.
5335 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5336 * Check for the minimal height of the FR_ROW frame.
5337 * At the top level we can also use change the command line height.
5338 */
5339 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005340frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341{
5342 int room; /* total number of lines available */
5343 int take; /* number of lines taken from other windows */
5344 int room_cmdline; /* lines available from cmdline */
5345 int run;
5346 frame_T *frp;
5347 int h;
5348 int room_reserved;
5349
5350 /* If the height already is the desired value, nothing to do. */
5351 if (curfrp->fr_height == height)
5352 return;
5353
5354 if (curfrp->fr_parent == NULL)
5355 {
5356 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005357 if (height > ROWS_AVAIL)
5358 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 if (height > 0)
5360 frame_new_height(curfrp, height, FALSE, FALSE);
5361 }
5362 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5363 {
5364 /* Row of frames: Also need to resize frames left and right of this
5365 * one. First check for the minimal height of these. */
5366 h = frame_minheight(curfrp->fr_parent, NULL);
5367 if (height < h)
5368 height = h;
5369 frame_setheight(curfrp->fr_parent, height);
5370 }
5371 else
5372 {
5373 /*
5374 * Column of frames: try to change only frames in this column.
5375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 /*
5377 * Do this twice:
5378 * 1: compute room available, if it's not enough try resizing the
5379 * containing frame.
5380 * 2: compute the room available and adjust the height to it.
5381 * Try not to reduce the height of a window with 'winfixheight' set.
5382 */
5383 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 room = 0;
5386 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005387 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
5389 if (frp != curfrp
5390 && frp->fr_win != NULL
5391 && frp->fr_win->w_p_wfh)
5392 room_reserved += frp->fr_height;
5393 room += frp->fr_height;
5394 if (frp != curfrp)
5395 room -= frame_minheight(frp, NULL);
5396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 if (curfrp->fr_width != Columns)
5398 room_cmdline = 0;
5399 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
5401 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005402 + VISIBLE_HEIGHT(lastwin)
5403 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 if (room_cmdline < 0)
5405 room_cmdline = 0;
5406 }
5407
5408 if (height <= room + room_cmdline)
5409 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
5412 if (height > room + room_cmdline)
5413 height = room + room_cmdline;
5414 break;
5415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 frame_setheight(curfrp->fr_parent, height
5417 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
5419
5420 /*
5421 * Compute the number of lines we will take from others frames (can be
5422 * negative!).
5423 */
5424 take = height - curfrp->fr_height;
5425
5426 /* If there is not enough room, also reduce the height of a window
5427 * with 'winfixheight' set. */
5428 if (height > room + room_cmdline - room_reserved)
5429 room_reserved = room + room_cmdline - height;
5430 /* If there is only a 'winfixheight' window and making the
5431 * window smaller, need to make the other window taller. */
5432 if (take < 0 && room - curfrp->fr_height < room_reserved)
5433 room_reserved = 0;
5434
5435 if (take > 0 && room_cmdline > 0)
5436 {
5437 /* use lines from cmdline first */
5438 if (take < room_cmdline)
5439 room_cmdline = take;
5440 take -= room_cmdline;
5441 topframe->fr_height += room_cmdline;
5442 }
5443
5444 /*
5445 * set the current frame to the new height
5446 */
5447 frame_new_height(curfrp, height, FALSE, FALSE);
5448
5449 /*
5450 * First take lines from the frames after the current frame. If
5451 * that is not enough, takes lines from frames above the current
5452 * frame.
5453 */
5454 for (run = 0; run < 2; ++run)
5455 {
5456 if (run == 0)
5457 frp = curfrp->fr_next; /* 1st run: start with next window */
5458 else
5459 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5460 while (frp != NULL && take != 0)
5461 {
5462 h = frame_minheight(frp, NULL);
5463 if (room_reserved > 0
5464 && frp->fr_win != NULL
5465 && frp->fr_win->w_p_wfh)
5466 {
5467 if (room_reserved >= frp->fr_height)
5468 room_reserved -= frp->fr_height;
5469 else
5470 {
5471 if (frp->fr_height - room_reserved > take)
5472 room_reserved = frp->fr_height - take;
5473 take -= frp->fr_height - room_reserved;
5474 frame_new_height(frp, room_reserved, FALSE, FALSE);
5475 room_reserved = 0;
5476 }
5477 }
5478 else
5479 {
5480 if (frp->fr_height - take < h)
5481 {
5482 take -= frp->fr_height - h;
5483 frame_new_height(frp, h, FALSE, FALSE);
5484 }
5485 else
5486 {
5487 frame_new_height(frp, frp->fr_height - take,
5488 FALSE, FALSE);
5489 take = 0;
5490 }
5491 }
5492 if (run == 0)
5493 frp = frp->fr_next;
5494 else
5495 frp = frp->fr_prev;
5496 }
5497 }
5498 }
5499}
5500
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501/*
5502 * Set current window width and take care of repositioning other windows to
5503 * fit around it.
5504 */
5505 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005506win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 win_setwidth_win(width, curwin);
5509}
5510
5511 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005512win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 /* Always keep current window at least one column wide, even when
5515 * 'winminwidth' is zero. */
5516 if (wp == curwin)
5517 {
5518 if (width < p_wmw)
5519 width = p_wmw;
5520 if (width == 0)
5521 width = 1;
5522 }
5523
5524 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5525
5526 /* recompute the window positions */
5527 (void)win_comp_pos();
5528
5529 redraw_all_later(NOT_VALID);
5530}
5531
5532/*
5533 * Set the width of a frame to "width" and take care that all frames and
5534 * windows inside it are resized. Also resize frames above and below if the
5535 * are in the same FR_ROW frame.
5536 *
5537 * Strategy is similar to frame_setheight().
5538 */
5539 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005540frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 int room; /* total number of lines available */
5543 int take; /* number of lines taken from other windows */
5544 int run;
5545 frame_T *frp;
5546 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005547 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548
5549 /* If the width already is the desired value, nothing to do. */
5550 if (curfrp->fr_width == width)
5551 return;
5552
5553 if (curfrp->fr_parent == NULL)
5554 /* topframe: can't change width */
5555 return;
5556
5557 if (curfrp->fr_parent->fr_layout == FR_COL)
5558 {
5559 /* Column of frames: Also need to resize frames above and below of
5560 * this one. First check for the minimal width of these. */
5561 w = frame_minwidth(curfrp->fr_parent, NULL);
5562 if (width < w)
5563 width = w;
5564 frame_setwidth(curfrp->fr_parent, width);
5565 }
5566 else
5567 {
5568 /*
5569 * Row of frames: try to change only frames in this row.
5570 *
5571 * Do this twice:
5572 * 1: compute room available, if it's not enough try resizing the
5573 * containing frame.
5574 * 2: compute the room available and adjust the width to it.
5575 */
5576 for (run = 1; run <= 2; ++run)
5577 {
5578 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005579 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005580 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005582 if (frp != curfrp
5583 && frp->fr_win != NULL
5584 && frp->fr_win->w_p_wfw)
5585 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 room += frp->fr_width;
5587 if (frp != curfrp)
5588 room -= frame_minwidth(frp, NULL);
5589 }
5590
5591 if (width <= room)
5592 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005593 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
5595 if (width > room)
5596 width = room;
5597 break;
5598 }
5599 frame_setwidth(curfrp->fr_parent, width
5600 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5601 }
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 /*
5604 * Compute the number of lines we will take from others frames (can be
5605 * negative!).
5606 */
5607 take = width - curfrp->fr_width;
5608
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005609 /* If there is not enough room, also reduce the width of a window
5610 * with 'winfixwidth' set. */
5611 if (width > room - room_reserved)
5612 room_reserved = room - width;
5613 /* If there is only a 'winfixwidth' window and making the
5614 * window smaller, need to make the other window narrower. */
5615 if (take < 0 && room - curfrp->fr_width < room_reserved)
5616 room_reserved = 0;
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 /*
5619 * set the current frame to the new width
5620 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005621 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
5623 /*
5624 * First take lines from the frames right of the current frame. If
5625 * that is not enough, takes lines from frames left of the current
5626 * frame.
5627 */
5628 for (run = 0; run < 2; ++run)
5629 {
5630 if (run == 0)
5631 frp = curfrp->fr_next; /* 1st run: start with next window */
5632 else
5633 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5634 while (frp != NULL && take != 0)
5635 {
5636 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005637 if (room_reserved > 0
5638 && frp->fr_win != NULL
5639 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005641 if (room_reserved >= frp->fr_width)
5642 room_reserved -= frp->fr_width;
5643 else
5644 {
5645 if (frp->fr_width - room_reserved > take)
5646 room_reserved = frp->fr_width - take;
5647 take -= frp->fr_width - room_reserved;
5648 frame_new_width(frp, room_reserved, FALSE, FALSE);
5649 room_reserved = 0;
5650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
5652 else
5653 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005654 if (frp->fr_width - take < w)
5655 {
5656 take -= frp->fr_width - w;
5657 frame_new_width(frp, w, FALSE, FALSE);
5658 }
5659 else
5660 {
5661 frame_new_width(frp, frp->fr_width - take,
5662 FALSE, FALSE);
5663 take = 0;
5664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 }
5666 if (run == 0)
5667 frp = frp->fr_next;
5668 else
5669 frp = frp->fr_prev;
5670 }
5671 }
5672 }
5673}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
5675/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005676 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 */
5678 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005679win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
5681 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005682 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005685 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 while (p_wmh > 0)
5687 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005688 room = Rows - p_ch;
5689 needed = frame_minheight(topframe, NULL);
5690 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 break;
5692 --p_wmh;
5693 if (first)
5694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005695 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 first = FALSE;
5697 }
5698 }
5699}
5700
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005701/*
5702 * Check 'winminwidth' for a valid value and reduce it if needed.
5703 */
5704 void
5705win_setminwidth(void)
5706{
5707 int room;
5708 int needed;
5709 int first = TRUE;
5710
5711 // loop until there is a 'winminheight' that is possible
5712 while (p_wmw > 0)
5713 {
5714 room = Columns;
5715 needed = frame_minwidth(topframe, NULL);
5716 if (room >= needed)
5717 break;
5718 --p_wmw;
5719 if (first)
5720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005721 emsg(_(e_noroom));
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005722 first = FALSE;
5723 }
5724 }
5725}
5726
Bram Moolenaar6a2697f2015-11-19 13:14:30 +01005727#if defined(FEAT_MOUSE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728
5729/*
5730 * Status line of dragwin is dragged "offset" lines down (negative is up).
5731 */
5732 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005733win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735 frame_T *curfr;
5736 frame_T *fr;
5737 int room;
5738 int row;
5739 int up; /* if TRUE, drag status line up, otherwise down */
5740 int n;
5741
5742 fr = dragwin->w_frame;
5743 curfr = fr;
5744 if (fr != topframe) /* more than one window */
5745 {
5746 fr = fr->fr_parent;
5747 /* When the parent frame is not a column of frames, its parent should
5748 * be. */
5749 if (fr->fr_layout != FR_COL)
5750 {
5751 curfr = fr;
5752 if (fr != topframe) /* only a row of windows, may drag statusline */
5753 fr = fr->fr_parent;
5754 }
5755 }
5756
5757 /* If this is the last frame in a column, may want to resize the parent
5758 * frame instead (go two up to skip a row of frames). */
5759 while (curfr != topframe && curfr->fr_next == NULL)
5760 {
5761 if (fr != topframe)
5762 fr = fr->fr_parent;
5763 curfr = fr;
5764 if (fr != topframe)
5765 fr = fr->fr_parent;
5766 }
5767
5768 if (offset < 0) /* drag up */
5769 {
5770 up = TRUE;
5771 offset = -offset;
5772 /* sum up the room of the current frame and above it */
5773 if (fr == curfr)
5774 {
5775 /* only one window */
5776 room = fr->fr_height - frame_minheight(fr, NULL);
5777 }
5778 else
5779 {
5780 room = 0;
5781 for (fr = fr->fr_child; ; fr = fr->fr_next)
5782 {
5783 room += fr->fr_height - frame_minheight(fr, NULL);
5784 if (fr == curfr)
5785 break;
5786 }
5787 }
5788 fr = curfr->fr_next; /* put fr at frame that grows */
5789 }
5790 else /* drag down */
5791 {
5792 up = FALSE;
5793 /*
5794 * Only dragging the last status line can reduce p_ch.
5795 */
5796 room = Rows - cmdline_row;
5797 if (curfr->fr_next == NULL)
5798 room -= 1;
5799 else
5800 room -= p_ch;
5801 if (room < 0)
5802 room = 0;
5803 /* sum up the room of frames below of the current one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005804 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 room += fr->fr_height - frame_minheight(fr, NULL);
5806 fr = curfr; /* put fr at window that grows */
5807 }
5808
5809 if (room < offset) /* Not enough room */
5810 offset = room; /* Move as far as we can */
5811 if (offset <= 0)
5812 return;
5813
5814 /*
5815 * Grow frame fr by "offset" lines.
5816 * Doesn't happen when dragging the last status line up.
5817 */
5818 if (fr != NULL)
5819 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5820
5821 if (up)
5822 fr = curfr; /* current frame gets smaller */
5823 else
5824 fr = curfr->fr_next; /* next frame gets smaller */
5825
5826 /*
5827 * Now make the other frames smaller.
5828 */
5829 while (fr != NULL && offset > 0)
5830 {
5831 n = frame_minheight(fr, NULL);
5832 if (fr->fr_height - offset <= n)
5833 {
5834 offset -= fr->fr_height - n;
5835 frame_new_height(fr, n, !up, FALSE);
5836 }
5837 else
5838 {
5839 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5840 break;
5841 }
5842 if (up)
5843 fr = fr->fr_prev;
5844 else
5845 fr = fr->fr_next;
5846 }
5847 row = win_comp_pos();
5848 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5849 cmdline_row = row;
5850 p_ch = Rows - cmdline_row;
5851 if (p_ch < 1)
5852 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005853 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005854 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 showmode();
5856}
5857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858/*
5859 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5860 */
5861 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005862win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863{
5864 frame_T *curfr;
5865 frame_T *fr;
5866 int room;
5867 int left; /* if TRUE, drag separator line left, otherwise right */
5868 int n;
5869
5870 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005871 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 return;
5873 curfr = fr;
5874 fr = fr->fr_parent;
5875 /* When the parent frame is not a row of frames, its parent should be. */
5876 if (fr->fr_layout != FR_ROW)
5877 {
5878 if (fr == topframe) /* only a column of windows (cannot happen?) */
5879 return;
5880 curfr = fr;
5881 fr = fr->fr_parent;
5882 }
5883
5884 /* If this is the last frame in a row, may want to resize a parent
5885 * frame instead. */
5886 while (curfr->fr_next == NULL)
5887 {
5888 if (fr == topframe)
5889 break;
5890 curfr = fr;
5891 fr = fr->fr_parent;
5892 if (fr != topframe)
5893 {
5894 curfr = fr;
5895 fr = fr->fr_parent;
5896 }
5897 }
5898
5899 if (offset < 0) /* drag left */
5900 {
5901 left = TRUE;
5902 offset = -offset;
5903 /* sum up the room of the current frame and left of it */
5904 room = 0;
5905 for (fr = fr->fr_child; ; fr = fr->fr_next)
5906 {
5907 room += fr->fr_width - frame_minwidth(fr, NULL);
5908 if (fr == curfr)
5909 break;
5910 }
5911 fr = curfr->fr_next; /* put fr at frame that grows */
5912 }
5913 else /* drag right */
5914 {
5915 left = FALSE;
5916 /* sum up the room of frames right of the current one */
5917 room = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005918 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 room += fr->fr_width - frame_minwidth(fr, NULL);
5920 fr = curfr; /* put fr at window that grows */
5921 }
5922
5923 if (room < offset) /* Not enough room */
5924 offset = room; /* Move as far as we can */
5925 if (offset <= 0) /* No room at all, quit. */
5926 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005927 if (fr == NULL)
5928 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
5930 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005931 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
5933 /* shrink other frames: current and at the left or at the right */
5934 if (left)
5935 fr = curfr; /* current frame gets smaller */
5936 else
5937 fr = curfr->fr_next; /* next frame gets smaller */
5938
5939 while (fr != NULL && offset > 0)
5940 {
5941 n = frame_minwidth(fr, NULL);
5942 if (fr->fr_width - offset <= n)
5943 {
5944 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005945 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 }
5947 else
5948 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005949 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 break;
5951 }
5952 if (left)
5953 fr = fr->fr_prev;
5954 else
5955 fr = fr->fr_next;
5956 }
5957 (void)win_comp_pos();
5958 redraw_all_later(NOT_VALID);
5959}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960#endif /* FEAT_MOUSE */
5961
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005962#define FRACTION_MULT 16384L
5963
5964/*
5965 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005966 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005967 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005968 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005969set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005970{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005971 if (wp->w_height > 1)
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005972 // When cursor is in the first line the percentage is computed as if
5973 // it's halfway that line. Thus with two lines it is 25%, with three
5974 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005975 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005976 + FRACTION_MULT / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005977}
5978
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979/*
5980 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02005981 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 * This takes care of the things inside the window, not what happens to the
5983 * window position, the frame or to other windows.
5984 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02005985 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005986win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005988 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989
5990 /* Don't want a negative height. Happens when splitting a tiny window.
5991 * Will equalize heights soon to fix it. */
5992 if (height < 0)
5993 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005994 if (wp->w_height == height)
5995 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02005997 if (wp->w_height > 0)
5998 {
5999 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02006000 /* w_wrow needs to be valid. When setting 'laststatus' this may
6001 * call win_new_height() recursively. */
6002 validate_cursor();
6003 if (wp->w_height != prev_height)
6004 return; /* Recursive call already changed the size, bail out here
6005 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006006 if (wp->w_wrow != wp->w_prev_fraction_row)
6007 set_fraction(wp);
6008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009
6010 wp->w_height = height;
6011 wp->w_skipcol = 0;
6012
Bram Moolenaar955f1982017-02-05 15:10:51 +01006013 /* There is no point in adjusting the scroll position when exiting. Some
6014 * values might be invalid. */
6015 if (!exiting)
6016 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02006017}
6018
6019 void
6020scroll_to_fraction(win_T *wp, int prev_height)
6021{
6022 linenr_T lnum;
6023 int sline, line_size;
6024 int height = wp->w_height;
6025
Bram Moolenaara9b25352019-05-12 14:25:30 +02006026 // Don't change w_topline in any of these cases:
6027 // - window height is 0
6028 // - 'scrollbind' is set and this isn't the current window
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02006029 // - window height is sufficient to display the whole buffer and first line
6030 // is visible.
Bram Moolenaara9b25352019-05-12 14:25:30 +02006031 if (height > 0
6032 && (!wp->w_p_scb || wp == curwin)
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02006033 && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 {
Bram Moolenaar34114692005-01-02 11:28:13 +00006035 /*
6036 * Find a value for w_topline that shows the cursor at the same
6037 * relative position in the window as before (more or less).
6038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 lnum = wp->w_cursor.lnum;
6040 if (lnum < 1) /* can happen when starting up */
6041 lnum = 1;
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006042 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L)
6043 / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
6045 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00006046
6047 if (sline >= 0)
6048 {
6049 /* Make sure the whole cursor line is visible, if possible. */
6050 int rows = plines_win(wp, lnum, FALSE);
6051
6052 if (sline > wp->w_height - rows)
6053 {
6054 sline = wp->w_height - rows;
6055 wp->w_wrow -= rows - line_size;
6056 }
6057 }
6058
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 if (sline < 0)
6060 {
6061 /*
6062 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00006063 * Make cursor line the first line in the window. If not enough
6064 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 */
6066 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00006067 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02006068 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00006069 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006070 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00006071 --wp->w_wrow;
6072 while (wp->w_wrow >= wp->w_height)
6073 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006074 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00006075 + win_col_off2(wp);
6076 --wp->w_wrow;
6077 }
6078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006080 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
Bram Moolenaar26470632006-10-24 19:12:40 +00006082 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
6084#ifdef FEAT_FOLDING
6085 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
6086 if (lnum == 1)
6087 {
6088 /* first line in buffer is folded */
6089 line_size = 1;
6090 --sline;
6091 break;
6092 }
6093#endif
6094 --lnum;
6095#ifdef FEAT_DIFF
6096 if (lnum == wp->w_topline)
6097 line_size = plines_win_nofill(wp, lnum, TRUE)
6098 + wp->w_topfill;
6099 else
6100#endif
6101 line_size = plines_win(wp, lnum, TRUE);
6102 sline -= line_size;
6103 }
Bram Moolenaar34114692005-01-02 11:28:13 +00006104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (sline < 0)
6106 {
6107 /*
6108 * Line we want at top would go off top of screen. Use next
6109 * line instead.
6110 */
6111#ifdef FEAT_FOLDING
6112 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
6113#endif
6114 lnum++;
6115 wp->w_wrow -= line_size + sline;
6116 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006117 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006119 // First line of file reached, use that as topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 lnum = 1;
6121 wp->w_wrow -= sline;
6122 }
6123 }
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006124 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 }
6126
6127 if (wp == curwin)
6128 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006129 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 update_topline();
6131 curs_columns(FALSE); /* validate w_wrow */
6132 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006133 if (prev_height > 0)
6134 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
6136 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006137 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 invalidate_botline_win(wp);
6140}
6141
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142/*
6143 * Set the width of a window.
6144 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02006145 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006146win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 wp->w_width = width;
6149 wp->w_lines_valid = 0;
6150 changed_line_abv_curs_win(wp);
6151 invalidate_botline_win(wp);
6152 if (wp == curwin)
6153 {
6154 update_topline();
6155 curs_columns(TRUE); /* validate w_wrow */
6156 }
6157 redraw_win_later(wp, NOT_VALID);
6158 wp->w_redr_status = TRUE;
6159}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006162win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6165 if (wp->w_p_scr == 0)
6166 wp->w_p_scr = 1;
6167}
6168
6169/*
6170 * command_height: called whenever p_ch has been changed
6171 */
6172 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006173command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 int h;
6176 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006177 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006179 /* Use the value of p_ch that we remembered. This is needed for when the
6180 * GUI starts up, we can't be sure in what order things happen. And when
6181 * p_ch was changed in another tab page. */
6182 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006183
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 /* Find bottom frame with width of screen. */
6185 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6187 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188
6189 /* Avoid changing the height of a window with 'winfixheight' set. */
6190 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6191 && frp->fr_win->w_p_wfh)
6192 frp = frp->fr_prev;
6193
6194 if (starting != NO_SCREEN)
6195 {
6196 cmdline_row = Rows - p_ch;
6197
6198 if (p_ch > old_p_ch) /* p_ch got bigger */
6199 {
6200 while (p_ch > old_p_ch)
6201 {
6202 if (frp == NULL)
6203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006204 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006206 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 cmdline_row = Rows - p_ch;
6208 break;
6209 }
6210 h = frp->fr_height - frame_minheight(frp, NULL);
6211 if (h > p_ch - old_p_ch)
6212 h = p_ch - old_p_ch;
6213 old_p_ch += h;
6214 frame_add_height(frp, -h);
6215 frp = frp->fr_prev;
6216 }
6217
6218 /* Recompute window positions. */
6219 (void)win_comp_pos();
6220
6221 /* clear the lines added to cmdline */
6222 if (full_screen)
6223 screen_fill((int)(cmdline_row), (int)Rows, 0,
6224 (int)Columns, ' ', ' ', 0);
6225 msg_row = cmdline_row;
6226 redraw_cmdline = TRUE;
6227 return;
6228 }
6229
6230 if (msg_row < cmdline_row)
6231 msg_row = cmdline_row;
6232 redraw_cmdline = TRUE;
6233 }
6234 frame_add_height(frp, (int)(old_p_ch - p_ch));
6235
6236 /* Recompute window positions. */
6237 if (frp != lastwin->w_frame)
6238 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239}
6240
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241/*
6242 * Resize frame "frp" to be "n" lines higher (negative for less high).
6243 * Also resize the frames it is contained in.
6244 */
6245 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006246frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6249 for (;;)
6250 {
6251 frp = frp->fr_parent;
6252 if (frp == NULL)
6253 break;
6254 frp->fr_height += n;
6255 }
6256}
6257
6258/*
6259 * Add or remove a status line for the bottom window(s), according to the
6260 * value of 'laststatus'.
6261 */
6262 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006263last_status(
6264 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 /* Don't make a difference between horizontal or vertical split. */
6267 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006268 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269}
6270
6271 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006272last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 frame_T *fp;
6275 win_T *wp;
6276
6277 if (fr->fr_layout == FR_LEAF)
6278 {
6279 wp = fr->fr_win;
6280 if (wp->w_status_height != 0 && !statusline)
6281 {
6282 /* remove status line */
6283 win_new_height(wp, wp->w_height + 1);
6284 wp->w_status_height = 0;
6285 comp_col();
6286 }
6287 else if (wp->w_status_height == 0 && statusline)
6288 {
6289 /* Find a frame to take a line from. */
6290 fp = fr;
6291 while (fp->fr_height <= frame_minheight(fp, NULL))
6292 {
6293 if (fp == topframe)
6294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006295 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 return;
6297 }
6298 /* In a column of frames: go to frame above. If already at
6299 * the top or in a row of frames: go to parent. */
6300 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6301 fp = fp->fr_prev;
6302 else
6303 fp = fp->fr_parent;
6304 }
6305 wp->w_status_height = 1;
6306 if (fp != fr)
6307 {
6308 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6309 frame_fix_height(wp);
6310 (void)win_comp_pos();
6311 }
6312 else
6313 win_new_height(wp, wp->w_height - 1);
6314 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006315 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 }
6317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 else if (fr->fr_layout == FR_ROW)
6319 {
6320 /* vertically split windows, set status line for each one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006321 FOR_ALL_FRAMES(fp, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 last_status_rec(fp, statusline);
6323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 else
6325 {
6326 /* horizontally split window, set status line for last one */
6327 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6328 ;
6329 last_status_rec(fp, statusline);
6330 }
6331}
6332
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006333/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006334 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006335 */
6336 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006337tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006338{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006339#ifdef FEAT_GUI_TABLINE
6340 /* When the GUI has the tabline then this always returns zero. */
6341 if (gui_use_tabline())
6342 return 0;
6343#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006344 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006345 {
6346 case 0: return 0;
6347 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6348 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006349 return 1;
6350}
6351
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352/*
6353 * Return the minimal number of rows that is needed on the screen to display
6354 * the current number of windows.
6355 */
6356 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006357min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006360 tabpage_T *tp;
6361 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363 if (firstwin == NULL) /* not initialized yet */
6364 return MIN_LINES;
6365
Bram Moolenaarf740b292006-02-16 22:11:02 +00006366 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006367 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006368 {
6369 n = frame_minheight(tp->tp_topframe, NULL);
6370 if (total < n)
6371 total = n;
6372 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006373 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006374 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 return total;
6376}
6377
6378/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006379 * Return TRUE if there is only one window (in the current tab page), not
6380 * counting a help or preview window, unless it is the current window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006381 * Does not count unlisted windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 */
6383 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006384only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 int count = 0;
6387 win_T *wp;
6388
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389 /* If there is another tab page there always is another window. */
6390 if (first_tabpage->tp_next != NULL)
6391 return FALSE;
6392
Bram Moolenaar29323592016-07-24 22:04:11 +02006393 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006394 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006395 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396# ifdef FEAT_QUICKFIX
6397 || wp->w_p_pvw
6398# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006399 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 ++count;
6401 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402}
6403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404/*
6405 * Correct the cursor line number in other windows. Used after changing the
6406 * current buffer, and before applying autocommands.
6407 * When "do_curwin" is TRUE, also check current window.
6408 */
6409 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006410check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006413 tabpage_T *tp;
6414
6415 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 {
Bram Moolenaara68e5952019-04-25 22:22:01 +02006418 // save the original cursor position and topline
6419 wp->w_save_cursor.w_cursor_save = wp->w_cursor;
6420 wp->w_save_cursor.w_topline_save = wp->w_topline;
6421
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6423 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6424 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6425 wp->w_topline = curbuf->b_ml.ml_line_count;
Bram Moolenaara68e5952019-04-25 22:22:01 +02006426
6427 // save the corrected cursor position and topline
6428 wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
6429 wp->w_save_cursor.w_topline_corr = wp->w_topline;
6430 }
6431}
6432
6433/*
6434 * Reset cursor and topline to its stored values from check_lnums().
6435 * check_lnums() must have been called first!
6436 */
6437 void
6438reset_lnums()
6439{
6440 win_T *wp;
6441 tabpage_T *tp;
6442
6443 FOR_ALL_TAB_WINDOWS(tp, wp)
6444 if (wp->w_buffer == curbuf)
6445 {
6446 // Restore the value if the autocommand didn't change it.
6447 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
6448 wp->w_cursor = wp->w_save_cursor.w_cursor_save;
6449 if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
6450 wp->w_topline = wp->w_save_cursor.w_topline_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 }
6452}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453
6454/*
6455 * A snapshot of the window sizes, to restore them after closing the help
6456 * window.
6457 * Only these fields are used:
6458 * fr_layout
6459 * fr_width
6460 * fr_height
6461 * fr_next
6462 * fr_child
6463 * fr_win (only valid for the old curwin, NULL otherwise)
6464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465
6466/*
6467 * Create a snapshot of the current frame sizes.
6468 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006469 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006470make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006472 clear_snapshot(curtab, idx);
6473 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474}
6475
6476 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006477make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006479 *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (*frp == NULL)
6481 return;
6482 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 (*frp)->fr_height = fr->fr_height;
6485 if (fr->fr_next != NULL)
6486 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6487 if (fr->fr_child != NULL)
6488 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6489 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6490 (*frp)->fr_win = curwin;
6491}
6492
6493/*
6494 * Remove any existing snapshot.
6495 */
6496 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006497clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006499 clear_snapshot_rec(tp->tp_snapshot[idx]);
6500 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501}
6502
6503 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006504clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
6506 if (fr != NULL)
6507 {
6508 clear_snapshot_rec(fr->fr_next);
6509 clear_snapshot_rec(fr->fr_child);
6510 vim_free(fr);
6511 }
6512}
6513
6514/*
6515 * Restore a previously created snapshot, if there is any.
6516 * This is only done if the screen size didn't change and the window layout is
6517 * still the same.
6518 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006519 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006520restore_snapshot(
6521 int idx,
6522 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
6524 win_T *wp;
6525
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006526 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006527 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006528 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6529 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006531 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 win_comp_pos();
6533 if (wp != NULL && close_curwin)
6534 win_goto(wp);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01006535 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006537 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538}
6539
6540/*
6541 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006542 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 */
6544 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006545check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546{
6547 if (sn->fr_layout != fr->fr_layout
6548 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6549 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6550 || (sn->fr_next != NULL
6551 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6552 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006553 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006554 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 return FAIL;
6556 return OK;
6557}
6558
6559/*
6560 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6561 * following frames and children.
6562 * Returns a pointer to the old current window, or NULL.
6563 */
6564 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006565restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 win_T *wp = NULL;
6568 win_T *wp2;
6569
6570 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 if (fr->fr_layout == FR_LEAF)
6573 {
6574 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006575 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 wp = sn->fr_win;
6577 }
6578 if (sn->fr_next != NULL)
6579 {
6580 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6581 if (wp2 != NULL)
6582 wp = wp2;
6583 }
6584 if (sn->fr_child != NULL)
6585 {
6586 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6587 if (wp2 != NULL)
6588 wp = wp2;
6589 }
6590 return wp;
6591}
6592
Bram Moolenaar4033c552017-09-16 20:54:51 +02006593#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594/*
6595 * Return TRUE if there is any vertically split window.
6596 */
6597 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006598win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599{
6600 frame_T *fr;
6601
6602 if (topframe->fr_layout == FR_ROW)
6603 return TRUE;
6604
6605 if (topframe->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006606 FOR_ALL_FRAMES(fr, topframe->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 if (fr->fr_layout == FR_ROW)
6608 return TRUE;
6609
6610 return FALSE;
6611}
6612#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006613
Bram Moolenaar6d216452013-05-12 19:00:41 +02006614#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6615 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006616get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006617{
6618 int i = 1;
6619 win_T *w;
6620
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006621 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006622 ++i;
6623
6624 if (w == NULL)
6625 return 0;
6626 else
6627 return i;
6628}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006629
6630 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006631get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006632{
6633 int i = 1;
6634 tabpage_T *t;
6635
6636 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6637 ++i;
6638
6639 if (t == NULL)
6640 return 0;
6641 else
6642 return i;
6643}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006644#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006645
6646/*
6647 * Return TRUE if "topfrp" and its children are at the right height.
6648 */
6649 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006650frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006651{
6652 frame_T *frp;
6653
6654 if (topfrp->fr_height != height)
6655 return FALSE;
6656
6657 if (topfrp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006658 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006659 if (frp->fr_height != height)
6660 return FALSE;
6661
6662 return TRUE;
6663}
6664
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006665/*
6666 * Return TRUE if "topfrp" and its children are at the right width.
6667 */
6668 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006669frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006670{
6671 frame_T *frp;
6672
6673 if (topfrp->fr_width != width)
6674 return FALSE;
6675
6676 if (topfrp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006677 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006678 if (frp->fr_width != width)
6679 return FALSE;
6680
6681 return TRUE;
6682}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006683
Bram Moolenaare677df82019-09-02 22:31:11 +02006684#if defined(FEAT_SYN_HL) || defined(PROTO)
6685/*
6686 * Simple int comparison function for use with qsort()
6687 */
6688 static int
6689int_cmp(const void *a, const void *b)
6690{
6691 return *(const int *)a - *(const int *)b;
6692}
6693
6694/*
6695 * Handle setting 'colorcolumn' or 'textwidth' in window "wp".
6696 * Returns error message, NULL if it's OK.
6697 */
6698 char *
6699check_colorcolumn(win_T *wp)
6700{
6701 char_u *s;
6702 int col;
6703 int count = 0;
6704 int color_cols[256];
6705 int i;
6706 int j = 0;
6707
6708 if (wp->w_buffer == NULL)
6709 return NULL; // buffer was closed
6710
6711 for (s = wp->w_p_cc; *s != NUL && count < 255;)
6712 {
6713 if (*s == '-' || *s == '+')
6714 {
6715 // -N and +N: add to 'textwidth'
6716 col = (*s == '-') ? -1 : 1;
6717 ++s;
6718 if (!VIM_ISDIGIT(*s))
6719 return e_invarg;
6720 col = col * getdigits(&s);
6721 if (wp->w_buffer->b_p_tw == 0)
6722 goto skip; // 'textwidth' not set, skip this item
6723 col += wp->w_buffer->b_p_tw;
6724 if (col < 0)
6725 goto skip;
6726 }
6727 else if (VIM_ISDIGIT(*s))
6728 col = getdigits(&s);
6729 else
6730 return e_invarg;
6731 color_cols[count++] = col - 1; // 1-based to 0-based
6732skip:
6733 if (*s == NUL)
6734 break;
6735 if (*s != ',')
6736 return e_invarg;
6737 if (*++s == NUL)
6738 return e_invarg; // illegal trailing comma as in "set cc=80,"
6739 }
6740
6741 vim_free(wp->w_p_cc_cols);
6742 if (count == 0)
6743 wp->w_p_cc_cols = NULL;
6744 else
6745 {
6746 wp->w_p_cc_cols = ALLOC_MULT(int, count + 1);
6747 if (wp->w_p_cc_cols != NULL)
6748 {
6749 // sort the columns for faster usage on screen redraw inside
6750 // win_line()
6751 qsort(color_cols, count, sizeof(int), int_cmp);
6752
6753 for (i = 0; i < count; ++i)
6754 // skip duplicates
6755 if (j == 0 || wp->w_p_cc_cols[j - 1] != color_cols[i])
6756 wp->w_p_cc_cols[j++] = color_cols[i];
6757 wp->w_p_cc_cols[j] = -1; // end marker
6758 }
6759 }
6760
6761 return NULL; // no error
6762}
6763#endif