blob: 85312ba2883b34a0e362acd37c26e54e4e4e0c32 [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{
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001429#ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +02001430 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 Moolenaar05ad5ff2019-11-30 22:48:27 +01001476#ifdef FEAT_PROP_POPUP
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +02001477 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 Moolenaarc8234772019-11-10 21:00:27 +01002441#ifdef FEAT_DIFF
2442 int had_diffmode = win->w_p_diff;
2443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02002445 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02002446 return FAIL;
2447
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002448 if (last_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002450 emsg(_("E444: Cannot close last window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002451 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002454 if (win->w_closing || (win->w_buffer != NULL
2455 && win->w_buffer->b_locked > 0))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002456 return FAIL; /* window is already being closed */
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002457 if (win_unlisted(win))
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002458 {
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002459 emsg(_("E813: Cannot close autocmd or popup window"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002460 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002461 }
2462 if ((firstwin == aucmd_win || lastwin == aucmd_win) && one_window())
2463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002464 emsg(_("E814: Cannot close window, only autocmd window would remain"));
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002465 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002466 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002467
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002468 /* When closing the last window in a tab page first go to another tab page
2469 * and then close the window and the tab page to avoid that curwin and
2470 * curtab are invalid while we are freeing memory. */
2471 if (close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002472 return FAIL;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002473
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 /* When closing the help window, try restoring a snapshot after closing
2475 * the window. Otherwise clear the snapshot, it's now invalid. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002476 if (bt_help(win->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 help_window = TRUE;
2478 else
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002479 clear_snapshot(curtab, SNAP_HELP_IDX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 if (win == curwin)
2482 {
Bram Moolenaar6d41c782018-06-06 09:11:12 +02002483#ifdef FEAT_JOB_CHANNEL
2484 leaving_window(curwin);
2485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 /*
2487 * Guess which window is going to be the new current window.
2488 * This may change because of the autocommands (sigh).
2489 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002490 wp = frame2win(win_altframe(win, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
2492 /*
Bram Moolenaar362ce482012-06-06 19:02:45 +02002493 * Be careful: If autocommands delete the window or cause this window
2494 * to be the last one left, return now.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 */
2496 if (wp->w_buffer != curbuf)
2497 {
2498 other_buffer = TRUE;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002499 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002501 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002502 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002503 win->w_closing = FALSE;
2504 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 }
Bram Moolenaar362ce482012-06-06 19:02:45 +02002507 win->w_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar362ce482012-06-06 19:02:45 +02002509 if (!win_valid(win))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002510 return FAIL;
Bram Moolenaar362ce482012-06-06 19:02:45 +02002511 win->w_closing = FALSE;
2512 if (last_window())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002513 return FAIL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002514#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 /* autocmds may abort script processing */
2516 if (aborting())
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002517 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002521#ifdef FEAT_GUI
Bram Moolenaar647e24b2019-03-17 16:39:46 +01002522 // Avoid trouble with scrollbars that are going to be deleted in
2523 // win_free().
Bram Moolenaar053b9fa2007-04-26 14:09:42 +00002524 if (gui.in_use)
2525 out_flush();
2526#endif
2527
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002528#ifdef FEAT_PROP_POPUP
Bram Moolenaar12034e22019-08-25 22:25:02 +02002529 if (popup_win_closed(win) && !win_valid(win))
2530 return FAIL;
2531#endif
Bram Moolenaar7c7f01e2019-06-12 21:06:32 +02002532 win_close_buffer(win, free_buf ? DOBUF_UNLOAD : 0, TRUE);
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002533
Bram Moolenaar802418d2013-01-17 14:00:11 +01002534 if (only_one_window() && win_valid(win) && win->w_buffer == NULL
2535 && (last_window() || curtab != prev_curtab
2536 || close_last_window_tabpage(win, free_buf, prev_curtab)))
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002537 {
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002538 /* Autocommands have closed all windows, quit now. Restore
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002539 * curwin->w_buffer, otherwise writing viminfo may fail. */
2540 if (curwin->w_buffer == NULL)
2541 curwin->w_buffer = curbuf;
Bram Moolenaar802418d2013-01-17 14:00:11 +01002542 getout(0);
Bram Moolenaar2b90ed22013-07-24 16:02:36 +02002543 }
Bram Moolenaar802418d2013-01-17 14:00:11 +01002544
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002545 /* Autocommands may have moved to another tab page. */
2546 if (curtab != prev_curtab && win_valid_any_tab(win)
2547 && win->w_buffer == NULL)
2548 {
2549 /* Need to close the window anyway, since the buffer is NULL. */
2550 win_close_othertab(win, FALSE, prev_curtab);
2551 return FAIL;
2552 }
2553
2554 /* Autocommands may have closed the window already or closed the only
2555 * other window. */
2556 if (!win_valid(win) || last_window()
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002557 || close_last_window_tabpage(win, free_buf, prev_curtab))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002558 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
Bram Moolenaar1417c762019-07-27 17:31:36 +02002560 // Now we are really going to close the window. Disallow any autocommand
2561 // to split a window to avoid trouble.
2562 ++split_disallowed;
2563
Bram Moolenaara971b822011-09-14 14:43:25 +02002564 /* Free the memory used for the window and get the window that received
2565 * the screen space. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002566 wp = win_free_mem(win, &dir, NULL);
2567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 /* Make sure curwin isn't invalid. It can cause severe trouble when
2569 * printing an error message. For win_equal() curbuf needs to be valid
2570 * too. */
Bram Moolenaarc1b52862006-04-28 22:32:28 +00002571 if (win == curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 {
2573 curwin = wp;
2574#ifdef FEAT_QUICKFIX
2575 if (wp->w_p_pvw || bt_quickfix(wp->w_buffer))
2576 {
2577 /*
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002578 * If the cursor goes to the preview or the quickfix window, try
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 * finding another window to go to.
2580 */
2581 for (;;)
2582 {
2583 if (wp->w_next == NULL)
2584 wp = firstwin;
2585 else
2586 wp = wp->w_next;
2587 if (wp == curwin)
2588 break;
2589 if (!wp->w_p_pvw && !bt_quickfix(wp->w_buffer))
2590 {
2591 curwin = wp;
2592 break;
2593 }
2594 }
2595 }
2596#endif
2597 curbuf = curwin->w_buffer;
2598 close_curwin = TRUE;
Bram Moolenaarf79225e2017-03-18 23:11:04 +01002599
2600 /* The cursor position may be invalid if the buffer changed after last
2601 * using the window. */
2602 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002604 if (p_ea && (*p_ead == 'b' || *p_ead == dir))
Bram Moolenaar8eeeba82017-06-25 22:45:39 +02002605 /* If the frame of the closed window contains the new current window,
2606 * only resize that frame. Otherwise resize all windows. */
Bram Moolenaar41cc0382017-06-26 09:59:35 +02002607 win_equal(curwin, curwin->w_frame->fr_parent == win_frame, dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 else
2609 win_comp_pos();
2610 if (close_curwin)
2611 {
Bram Moolenaarc917da42016-07-19 22:31:36 +02002612 win_enter_ext(wp, FALSE, TRUE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if (other_buffer)
2614 /* careful: after this wp and win may be invalid! */
2615 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
2617
Bram Moolenaar1417c762019-07-27 17:31:36 +02002618 --split_disallowed;
2619
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002621 * If last window has a status line now and we don't want one,
2622 * remove the status line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 */
2624 last_status(FALSE);
2625
2626 /* After closing the help window, try restoring the window layout from
2627 * before it was opened. */
2628 if (help_window)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002629 restore_snapshot(SNAP_HELP_IDX, close_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630
Bram Moolenaarc8234772019-11-10 21:00:27 +01002631#ifdef FEAT_DIFF
2632 // If the window had 'diff' set and now there is only one window left in
2633 // the tab page with 'diff' set, and "closeoff" is in 'diffopt', then
2634 // execute ":diffoff!".
2635 if (diffopt_closeoff() && had_diffmode && curtab == prev_curtab)
2636 {
2637 int diffcount = 0;
2638 win_T *dwin;
2639
2640 FOR_ALL_WINDOWS(dwin)
2641 if (dwin->w_p_diff)
2642 ++diffcount;
2643 if (diffcount == 1)
2644 do_cmdline_cmd((char_u *)"diffoff!");
2645 }
2646#endif
2647
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002648#if defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 /* When 'guioptions' includes 'L' or 'R' may have to remove scrollbars. */
2650 if (gui.in_use && !win_hasvertsplit())
2651 gui_init_which_components(NULL);
2652#endif
2653
2654 redraw_all_later(NOT_VALID);
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02002655 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656}
2657
2658/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002659 * Close window "win" in tab page "tp", which is not the current tab page.
Bram Moolenaarbef1c362012-05-25 12:39:00 +02002660 * This may be the last window in that tab page and result in closing the tab,
Bram Moolenaarf740b292006-02-16 22:11:02 +00002661 * thus "tp" may become invalid!
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002662 * Caller must check if buffer is hidden and whether the tabline needs to be
2663 * updated.
Bram Moolenaarf740b292006-02-16 22:11:02 +00002664 */
2665 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002666win_close_othertab(win_T *win, int free_buf, tabpage_T *tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002667{
2668 win_T *wp;
2669 int dir;
2670 tabpage_T *ptp = NULL;
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002671 int free_tp = FALSE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002672
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002673 /* Get here with win->w_buffer == NULL when win_close() detects the tab
2674 * page changed. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02002675 if (win->w_closing || (win->w_buffer != NULL
2676 && win->w_buffer->b_locked > 0))
Bram Moolenaar362ce482012-06-06 19:02:45 +02002677 return; /* window is already being closed */
Bram Moolenaar362ce482012-06-06 19:02:45 +02002678
Bram Moolenaar11fbc282016-09-02 21:48:32 +02002679 if (win->w_buffer != NULL)
2680 /* Close the link to the buffer. */
2681 close_buffer(win, win->w_buffer, free_buf ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002682
2683 /* Careful: Autocommands may have closed the tab page or made it the
2684 * current tab page. */
2685 for (ptp = first_tabpage; ptp != NULL && ptp != tp; ptp = ptp->tp_next)
2686 ;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002687 if (ptp == NULL || tp == curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002688 return;
2689
2690 /* Autocommands may have closed the window already. */
2691 for (wp = tp->tp_firstwin; wp != NULL && wp != win; wp = wp->w_next)
2692 ;
2693 if (wp == NULL)
2694 return;
2695
Bram Moolenaarf740b292006-02-16 22:11:02 +00002696 /* When closing the last window in a tab page remove the tab page. */
Bram Moolenaarcde88542015-08-11 19:14:00 +02002697 if (tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002698 {
2699 if (tp == first_tabpage)
2700 first_tabpage = tp->tp_next;
2701 else
2702 {
2703 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tp;
2704 ptp = ptp->tp_next)
2705 ;
2706 if (ptp == NULL)
2707 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01002708 internal_error("win_close_othertab()");
Bram Moolenaarf740b292006-02-16 22:11:02 +00002709 return;
2710 }
2711 ptp->tp_next = tp->tp_next;
2712 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002713 free_tp = TRUE;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002714 }
Bram Moolenaar4d770fb2010-07-12 21:38:19 +02002715
2716 /* Free the memory used for the window. */
2717 win_free_mem(win, &dir, tp);
2718
2719 if (free_tp)
2720 free_tabpage(tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002721}
2722
2723/*
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002724 * Free the memory used for a window.
2725 * Returns a pointer to the window that got the freed up space.
2726 */
2727 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002728win_free_mem(
2729 win_T *win,
2730 int *dirp, /* set to 'v' or 'h' for direction if 'ea' */
2731 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002732{
2733 frame_T *frp;
2734 win_T *wp;
2735
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002736 /* Remove the window and its frame from the tree of frames. */
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002737 frp = win->w_frame;
Bram Moolenaarf740b292006-02-16 22:11:02 +00002738 wp = winframe_remove(win, dirp, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002739 vim_free(frp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002740 win_free(win, tp);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002741
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002742 /* When deleting the current window of another tab page select a new
2743 * current window. */
2744 if (tp != NULL && win == tp->tp_curwin)
2745 tp->tp_curwin = wp;
2746
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002747 return wp;
2748}
2749
2750#if defined(EXITFREE) || defined(PROTO)
2751 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002752win_free_all(void)
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002753{
2754 int dummy;
2755
Bram Moolenaarf740b292006-02-16 22:11:02 +00002756 while (first_tabpage->tp_next != NULL)
2757 tabpage_close(TRUE);
Bram Moolenaarf740b292006-02-16 22:11:02 +00002758
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002759 if (aucmd_win != NULL)
2760 {
2761 (void)win_free_mem(aucmd_win, &dummy, NULL);
2762 aucmd_win = NULL;
2763 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01002764# ifdef FEAT_PROP_POPUP
Bram Moolenaar4d784b22019-05-25 19:51:39 +02002765 close_all_popups();
2766# endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00002767
2768 while (firstwin != NULL)
2769 (void)win_free_mem(firstwin, &dummy, NULL);
Bram Moolenaar4e036c92014-07-16 16:30:28 +02002770
2771 /* No window should be used after this. Set curwin to NULL to crash
2772 * instead of using freed memory. */
2773 curwin = NULL;
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00002774}
2775#endif
2776
2777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 * Remove a window and its frame from the tree of frames.
2779 * Returns a pointer to the window that got the freed up space.
2780 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00002781 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002782winframe_remove(
2783 win_T *win,
2784 int *dirp UNUSED, /* set to 'v' or 'h' for direction if 'ea' */
2785 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786{
2787 frame_T *frp, *frp2, *frp3;
2788 frame_T *frp_close = win->w_frame;
2789 win_T *wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790
2791 /*
Bram Moolenaarf740b292006-02-16 22:11:02 +00002792 * If there is only one window there is nothing to remove.
2793 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002794 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaarf740b292006-02-16 22:11:02 +00002795 return NULL;
2796
2797 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 * Remove the window from its frame.
2799 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00002800 frp2 = win_altframe(win, tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 wp = frame2win(frp2);
2802
2803 /* Remove this frame from the list of frames. */
2804 frame_remove(frp_close);
2805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (frp_close->fr_parent->fr_layout == FR_COL)
2807 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002808 /* When 'winfixheight' is set, try to find another frame in the column
2809 * (as close to the closed frame as possible) to distribute the height
2810 * to. */
2811 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfh)
2812 {
2813 frp = frp_close->fr_prev;
2814 frp3 = frp_close->fr_next;
2815 while (frp != NULL || frp3 != NULL)
2816 {
2817 if (frp != NULL)
2818 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002819 if (!frame_fixed_height(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002820 {
2821 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002822 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002823 break;
2824 }
2825 frp = frp->fr_prev;
2826 }
2827 if (frp3 != NULL)
2828 {
2829 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfh)
2830 {
2831 frp2 = frp3;
2832 wp = frp3->fr_win;
2833 break;
2834 }
2835 frp3 = frp3->fr_next;
2836 }
2837 }
2838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 frame_new_height(frp2, frp2->fr_height + frp_close->fr_height,
2840 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 *dirp = 'v';
2842 }
2843 else
2844 {
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002845 /* When 'winfixwidth' is set, try to find another frame in the column
2846 * (as close to the closed frame as possible) to distribute the width
2847 * to. */
2848 if (frp2->fr_win != NULL && frp2->fr_win->w_p_wfw)
2849 {
2850 frp = frp_close->fr_prev;
2851 frp3 = frp_close->fr_next;
2852 while (frp != NULL || frp3 != NULL)
2853 {
2854 if (frp != NULL)
2855 {
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002856 if (!frame_fixed_width(frp))
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002857 {
2858 frp2 = frp;
Bram Moolenaar9e1e3582019-03-30 19:49:06 +01002859 wp = frame2win(frp2);
Bram Moolenaar48cc5fe2007-08-11 11:39:45 +00002860 break;
2861 }
2862 frp = frp->fr_prev;
2863 }
2864 if (frp3 != NULL)
2865 {
2866 if (frp3->fr_win != NULL && !frp3->fr_win->w_p_wfw)
2867 {
2868 frp2 = frp3;
2869 wp = frp3->fr_win;
2870 break;
2871 }
2872 frp3 = frp3->fr_next;
2873 }
2874 }
2875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 frame_new_width(frp2, frp2->fr_width + frp_close->fr_width,
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00002877 frp2 == frp_close->fr_next ? TRUE : FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 *dirp = 'h';
2879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880
2881 /* If rows/columns go to a window below/right its positions need to be
2882 * updated. Can only be done after the sizes have been updated. */
2883 if (frp2 == frp_close->fr_next)
2884 {
2885 int row = win->w_winrow;
Bram Moolenaar53f81742017-09-22 14:35:51 +02002886 int col = win->w_wincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887
2888 frame_comp_pos(frp2, &row, &col);
2889 }
2890
2891 if (frp2->fr_next == NULL && frp2->fr_prev == NULL)
2892 {
2893 /* There is no other frame in this list, move its info to the parent
2894 * and remove it. */
2895 frp2->fr_parent->fr_layout = frp2->fr_layout;
2896 frp2->fr_parent->fr_child = frp2->fr_child;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01002897 FOR_ALL_FRAMES(frp, frp2->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 frp->fr_parent = frp2->fr_parent;
2899 frp2->fr_parent->fr_win = frp2->fr_win;
2900 if (frp2->fr_win != NULL)
2901 frp2->fr_win->w_frame = frp2->fr_parent;
2902 frp = frp2->fr_parent;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002903 if (topframe->fr_child == frp2)
2904 topframe->fr_child = frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 vim_free(frp2);
2906
2907 frp2 = frp->fr_parent;
2908 if (frp2 != NULL && frp2->fr_layout == frp->fr_layout)
2909 {
2910 /* The frame above the parent has the same layout, have to merge
2911 * the frames into this list. */
2912 if (frp2->fr_child == frp)
2913 frp2->fr_child = frp->fr_child;
2914 frp->fr_child->fr_prev = frp->fr_prev;
2915 if (frp->fr_prev != NULL)
2916 frp->fr_prev->fr_next = frp->fr_child;
2917 for (frp3 = frp->fr_child; ; frp3 = frp3->fr_next)
2918 {
2919 frp3->fr_parent = frp2;
2920 if (frp3->fr_next == NULL)
2921 {
2922 frp3->fr_next = frp->fr_next;
2923 if (frp->fr_next != NULL)
2924 frp->fr_next->fr_prev = frp3;
2925 break;
2926 }
2927 }
Bram Moolenaar6f361c92018-01-31 19:06:50 +01002928 if (topframe->fr_child == frp)
2929 topframe->fr_child = frp2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 vim_free(frp);
2931 }
2932 }
2933
2934 return wp;
2935}
2936
2937/*
Bram Moolenaarc136af22018-05-04 20:15:38 +02002938 * Return a pointer to the frame that will receive the empty screen space that
2939 * is left over after "win" is closed.
2940 *
2941 * If 'splitbelow' or 'splitright' is set, the space goes above or to the left
2942 * by default. Otherwise, the free space goes below or to the right. The
2943 * result is that opening a window and then immediately closing it will
2944 * preserve the initial window layout. The 'wfh' and 'wfw' settings are
2945 * respected when possible.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 */
2947 static frame_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002948win_altframe(
2949 win_T *win,
2950 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951{
2952 frame_T *frp;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002953 frame_T *other_fr, *target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01002955 if (tp == NULL ? ONE_WINDOW : tp->tp_firstwin == tp->tp_lastwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002956 return alt_tabpage()->tp_curwin->w_frame;
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 frp = win->w_frame;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002959
2960 if (frp->fr_prev == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 return frp->fr_next;
Bram Moolenaarc136af22018-05-04 20:15:38 +02002962 if (frp->fr_next == NULL)
2963 return frp->fr_prev;
2964
2965 target_fr = frp->fr_next;
2966 other_fr = frp->fr_prev;
2967 if (p_spr || p_sb)
2968 {
2969 target_fr = frp->fr_prev;
2970 other_fr = frp->fr_next;
2971 }
2972
2973 /* If 'wfh' or 'wfw' is set for the target and not for the alternate
2974 * window, reverse the selection. */
2975 if (frp->fr_parent != NULL && frp->fr_parent->fr_layout == FR_ROW)
2976 {
2977 if (frame_fixed_width(target_fr) && !frame_fixed_width(other_fr))
2978 target_fr = other_fr;
2979 }
2980 else
2981 {
2982 if (frame_fixed_height(target_fr) && !frame_fixed_height(other_fr))
2983 target_fr = other_fr;
2984 }
2985
2986 return target_fr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987}
2988
2989/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002990 * Return the tabpage that will be used if the current one is closed.
2991 */
2992 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01002993alt_tabpage(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002994{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002995 tabpage_T *tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002996
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002997 /* Use the next tab page if possible. */
2998 if (curtab->tp_next != NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002999 return curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003000
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003001 /* Find the last but one tab page. */
3002 for (tp = first_tabpage; tp->tp_next != curtab; tp = tp->tp_next)
3003 ;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003004 return tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003005}
3006
3007/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 * Find the left-upper window in frame "frp".
3009 */
3010 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003011frame2win(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012{
3013 while (frp->fr_win == NULL)
3014 frp = frp->fr_child;
3015 return frp->fr_win;
3016}
3017
3018/*
3019 * Return TRUE if frame "frp" contains window "wp".
3020 */
3021 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003022frame_has_win(frame_T *frp, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 frame_T *p;
3025
3026 if (frp->fr_layout == FR_LEAF)
3027 return frp->fr_win == wp;
3028
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003029 FOR_ALL_FRAMES(p, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (frame_has_win(p, wp))
3031 return TRUE;
3032 return FALSE;
3033}
3034
3035/*
3036 * Set a new height for a frame. Recursively sets the height for contained
3037 * frames and windows. Caller must take care of positions.
3038 */
3039 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003040frame_new_height(
3041 frame_T *topfrp,
3042 int height,
3043 int topfirst, /* resize topmost contained frame first */
3044 int wfh) /* obey 'winfixheight' when there is a choice;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 may cause the height not to be set */
3046{
3047 frame_T *frp;
3048 int extra_lines;
3049 int h;
3050
3051 if (topfrp->fr_win != NULL)
3052 {
3053 /* Simple case: just one window. */
3054 win_new_height(topfrp->fr_win,
Bram Moolenaard326ad62017-09-18 20:31:41 +02003055 height - topfrp->fr_win->w_status_height
Bram Moolenaar3167c3e2017-11-25 14:19:43 +01003056 - WINBAR_HEIGHT(topfrp->fr_win));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 else if (topfrp->fr_layout == FR_ROW)
3059 {
3060 do
3061 {
3062 /* All frames in this row get the same new height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003063 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
3065 frame_new_height(frp, height, topfirst, wfh);
3066 if (frp->fr_height > height)
3067 {
3068 /* Could not fit the windows, make the whole row higher. */
3069 height = frp->fr_height;
3070 break;
3071 }
3072 }
3073 }
3074 while (frp != NULL);
3075 }
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003076 else /* fr_layout == FR_COL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 {
3078 /* Complicated case: Resize a column of frames. Resize the bottom
3079 * frame first, frames above that when needed. */
3080
3081 frp = topfrp->fr_child;
3082 if (wfh)
3083 /* Advance past frames with one window with 'wfh' set. */
3084 while (frame_fixed_height(frp))
3085 {
3086 frp = frp->fr_next;
3087 if (frp == NULL)
3088 return; /* no frame without 'wfh', give up */
3089 }
3090 if (!topfirst)
3091 {
3092 /* Find the bottom frame of this column */
3093 while (frp->fr_next != NULL)
3094 frp = frp->fr_next;
3095 if (wfh)
3096 /* Advance back for frames with one window with 'wfh' set. */
3097 while (frame_fixed_height(frp))
3098 frp = frp->fr_prev;
3099 }
3100
3101 extra_lines = height - topfrp->fr_height;
3102 if (extra_lines < 0)
3103 {
3104 /* reduce height of contained frames, bottom or top frame first */
3105 while (frp != NULL)
3106 {
3107 h = frame_minheight(frp, NULL);
3108 if (frp->fr_height + extra_lines < h)
3109 {
3110 extra_lines += frp->fr_height - h;
3111 frame_new_height(frp, h, topfirst, wfh);
3112 }
3113 else
3114 {
3115 frame_new_height(frp, frp->fr_height + extra_lines,
3116 topfirst, wfh);
3117 break;
3118 }
3119 if (topfirst)
3120 {
3121 do
3122 frp = frp->fr_next;
3123 while (wfh && frp != NULL && frame_fixed_height(frp));
3124 }
3125 else
3126 {
3127 do
3128 frp = frp->fr_prev;
3129 while (wfh && frp != NULL && frame_fixed_height(frp));
3130 }
3131 /* Increase "height" if we could not reduce enough frames. */
3132 if (frp == NULL)
3133 height -= extra_lines;
3134 }
3135 }
3136 else if (extra_lines > 0)
3137 {
3138 /* increase height of bottom or top frame */
3139 frame_new_height(frp, frp->fr_height + extra_lines, topfirst, wfh);
3140 }
3141 }
3142 topfrp->fr_height = height;
3143}
3144
3145/*
3146 * Return TRUE if height of frame "frp" should not be changed because of
3147 * the 'winfixheight' option.
3148 */
3149 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003150frame_fixed_height(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 /* frame with one window: fixed height if 'winfixheight' set. */
3153 if (frp->fr_win != NULL)
3154 return frp->fr_win->w_p_wfh;
3155
3156 if (frp->fr_layout == FR_ROW)
3157 {
3158 /* The frame is fixed height if one of the frames in the row is fixed
3159 * height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003160 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (frame_fixed_height(frp))
3162 return TRUE;
3163 return FALSE;
3164 }
3165
3166 /* frp->fr_layout == FR_COL: The frame is fixed height if all of the
3167 * frames in the row are fixed height. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003168 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (!frame_fixed_height(frp))
3170 return FALSE;
3171 return TRUE;
3172}
3173
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174/*
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003175 * Return TRUE if width of frame "frp" should not be changed because of
3176 * the 'winfixwidth' option.
3177 */
3178 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003179frame_fixed_width(frame_T *frp)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003180{
3181 /* frame with one window: fixed width if 'winfixwidth' set. */
3182 if (frp->fr_win != NULL)
3183 return frp->fr_win->w_p_wfw;
3184
3185 if (frp->fr_layout == FR_COL)
3186 {
3187 /* The frame is fixed width if one of the frames in the row is fixed
3188 * width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003189 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003190 if (frame_fixed_width(frp))
3191 return TRUE;
3192 return FALSE;
3193 }
3194
3195 /* frp->fr_layout == FR_ROW: The frame is fixed width if all of the
3196 * frames in the row are fixed width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003197 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003198 if (!frame_fixed_width(frp))
3199 return FALSE;
3200 return TRUE;
3201}
3202
3203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 * Add a status line to windows at the bottom of "frp".
3205 * Note: Does not check if there is room!
3206 */
3207 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003208frame_add_statusline(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 win_T *wp;
3211
3212 if (frp->fr_layout == FR_LEAF)
3213 {
3214 wp = frp->fr_win;
3215 if (wp->w_status_height == 0)
3216 {
3217 if (wp->w_height > 0) /* don't make it negative */
3218 --wp->w_height;
3219 wp->w_status_height = STATUS_HEIGHT;
3220 }
3221 }
3222 else if (frp->fr_layout == FR_ROW)
3223 {
3224 /* Handle all the frames in the row. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003225 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 frame_add_statusline(frp);
3227 }
3228 else /* frp->fr_layout == FR_COL */
3229 {
3230 /* Only need to handle the last frame in the column. */
3231 for (frp = frp->fr_child; frp->fr_next != NULL; frp = frp->fr_next)
3232 ;
3233 frame_add_statusline(frp);
3234 }
3235}
3236
3237/*
3238 * Set width of a frame. Handles recursively going through contained frames.
3239 * May remove separator line for windows at the right side (for win_close()).
3240 */
3241 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003242frame_new_width(
3243 frame_T *topfrp,
3244 int width,
3245 int leftfirst, /* resize leftmost contained frame first */
3246 int wfw) /* obey 'winfixwidth' when there is a choice;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003247 may cause the width not to be set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
3249 frame_T *frp;
3250 int extra_cols;
3251 int w;
3252 win_T *wp;
3253
3254 if (topfrp->fr_layout == FR_LEAF)
3255 {
3256 /* Simple case: just one window. */
3257 wp = topfrp->fr_win;
3258 /* Find out if there are any windows right of this one. */
3259 for (frp = topfrp; frp->fr_parent != NULL; frp = frp->fr_parent)
3260 if (frp->fr_parent->fr_layout == FR_ROW && frp->fr_next != NULL)
3261 break;
3262 if (frp->fr_parent == NULL)
3263 wp->w_vsep_width = 0;
3264 win_new_width(wp, width - wp->w_vsep_width);
3265 }
3266 else if (topfrp->fr_layout == FR_COL)
3267 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003268 do
3269 {
3270 /* All frames in this column get the same new width. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003271 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003272 {
3273 frame_new_width(frp, width, leftfirst, wfw);
3274 if (frp->fr_width > width)
3275 {
3276 /* Could not fit the windows, make whole column wider. */
3277 width = frp->fr_width;
3278 break;
3279 }
3280 }
3281 } while (frp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 }
3283 else /* fr_layout == FR_ROW */
3284 {
3285 /* Complicated case: Resize a row of frames. Resize the rightmost
3286 * frame first, frames left of it when needed. */
3287
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 frp = topfrp->fr_child;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003289 if (wfw)
3290 /* Advance past frames with one window with 'wfw' set. */
3291 while (frame_fixed_width(frp))
3292 {
3293 frp = frp->fr_next;
3294 if (frp == NULL)
3295 return; /* no frame without 'wfw', give up */
3296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 if (!leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003298 {
3299 /* Find the rightmost frame of this row */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 while (frp->fr_next != NULL)
3301 frp = frp->fr_next;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003302 if (wfw)
3303 /* Advance back for frames with one window with 'wfw' set. */
3304 while (frame_fixed_width(frp))
3305 frp = frp->fr_prev;
3306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307
3308 extra_cols = width - topfrp->fr_width;
3309 if (extra_cols < 0)
3310 {
3311 /* reduce frame width, rightmost frame first */
3312 while (frp != NULL)
3313 {
3314 w = frame_minwidth(frp, NULL);
3315 if (frp->fr_width + extra_cols < w)
3316 {
3317 extra_cols += frp->fr_width - w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003318 frame_new_width(frp, w, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 }
3320 else
3321 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003322 frame_new_width(frp, frp->fr_width + extra_cols,
3323 leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 break;
3325 }
3326 if (leftfirst)
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003327 {
3328 do
3329 frp = frp->fr_next;
3330 while (wfw && frp != NULL && frame_fixed_width(frp));
3331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003333 {
3334 do
3335 frp = frp->fr_prev;
3336 while (wfw && frp != NULL && frame_fixed_width(frp));
3337 }
3338 /* Increase "width" if we could not reduce enough frames. */
3339 if (frp == NULL)
3340 width -= extra_cols;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
3342 }
3343 else if (extra_cols > 0)
3344 {
3345 /* increase width of rightmost frame */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00003346 frame_new_width(frp, frp->fr_width + extra_cols, leftfirst, wfw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
3348 }
3349 topfrp->fr_width = width;
3350}
3351
3352/*
3353 * Add the vertical separator to windows at the right side of "frp".
3354 * Note: Does not check if there is room!
3355 */
3356 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003357frame_add_vsep(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 win_T *wp;
3360
3361 if (frp->fr_layout == FR_LEAF)
3362 {
3363 wp = frp->fr_win;
3364 if (wp->w_vsep_width == 0)
3365 {
3366 if (wp->w_width > 0) /* don't make it negative */
3367 --wp->w_width;
3368 wp->w_vsep_width = 1;
3369 }
3370 }
3371 else if (frp->fr_layout == FR_COL)
3372 {
3373 /* Handle all the frames in the column. */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003374 FOR_ALL_FRAMES(frp, frp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 frame_add_vsep(frp);
3376 }
3377 else /* frp->fr_layout == FR_ROW */
3378 {
3379 /* Only need to handle the last frame in the row. */
3380 frp = frp->fr_child;
3381 while (frp->fr_next != NULL)
3382 frp = frp->fr_next;
3383 frame_add_vsep(frp);
3384 }
3385}
3386
3387/*
3388 * Set frame width from the window it contains.
3389 */
3390 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003391frame_fix_width(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 wp->w_frame->fr_width = wp->w_width + wp->w_vsep_width;
3394}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396/*
3397 * Set frame height from the window it contains.
3398 */
3399 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003400frame_fix_height(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401{
Bram Moolenaar415a6932017-12-05 20:31:07 +01003402 wp->w_frame->fr_height = VISIBLE_HEIGHT(wp) + wp->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403}
3404
3405/*
3406 * Compute the minimal height for frame "topfrp".
3407 * Uses the 'winminheight' option.
3408 * When "next_curwin" isn't NULL, use p_wh for this window.
3409 * When "next_curwin" is NOWIN, don't use at least one line for the current
3410 * window.
3411 */
3412 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003413frame_minheight(frame_T *topfrp, win_T *next_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
3415 frame_T *frp;
3416 int m;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418
3419 if (topfrp->fr_win != NULL)
3420 {
3421 if (topfrp->fr_win == next_curwin)
3422 m = p_wh + topfrp->fr_win->w_status_height;
3423 else
3424 {
3425 /* window: minimal height of the window plus status line */
3426 m = p_wmh + topfrp->fr_win->w_status_height;
Bram Moolenaar415a6932017-12-05 20:31:07 +01003427 if (topfrp->fr_win == curwin && next_curwin == NULL)
3428 {
3429 /* Current window is minimal one line high and WinBar is
3430 * visible. */
3431 if (p_wmh == 0)
3432 ++m;
3433 m += WINBAR_HEIGHT(curwin);
3434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
3436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 else if (topfrp->fr_layout == FR_ROW)
3438 {
3439 /* get the minimal height from each frame in this row */
3440 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003441 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 {
3443 n = frame_minheight(frp, next_curwin);
3444 if (n > m)
3445 m = n;
3446 }
3447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 else
3449 {
3450 /* Add up the minimal heights for all frames in this column. */
3451 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003452 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 m += frame_minheight(frp, next_curwin);
3454 }
3455
3456 return m;
3457}
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459/*
3460 * Compute the minimal width for frame "topfrp".
3461 * When "next_curwin" isn't NULL, use p_wiw for this window.
3462 * When "next_curwin" is NOWIN, don't use at least one column for the current
3463 * window.
3464 */
3465 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003466frame_minwidth(
3467 frame_T *topfrp,
3468 win_T *next_curwin) /* use p_wh and p_wiw for next_curwin */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 frame_T *frp;
3471 int m, n;
3472
3473 if (topfrp->fr_win != NULL)
3474 {
3475 if (topfrp->fr_win == next_curwin)
3476 m = p_wiw + topfrp->fr_win->w_vsep_width;
3477 else
3478 {
3479 /* window: minimal width of the window plus separator column */
3480 m = p_wmw + topfrp->fr_win->w_vsep_width;
3481 /* Current window is minimal one column wide */
3482 if (p_wmw == 0 && topfrp->fr_win == curwin && next_curwin == NULL)
3483 ++m;
3484 }
3485 }
3486 else if (topfrp->fr_layout == FR_COL)
3487 {
3488 /* get the minimal width from each frame in this column */
3489 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003490 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 n = frame_minwidth(frp, next_curwin);
3493 if (n > m)
3494 m = n;
3495 }
3496 }
3497 else
3498 {
3499 /* Add up the minimal widths for all frames in this row. */
3500 m = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01003501 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 m += frame_minwidth(frp, next_curwin);
3503 }
3504
3505 return m;
3506}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508
3509/*
3510 * Try to close all windows except current one.
3511 * Buffers in the other windows become hidden if 'hidden' is set, or '!' is
3512 * used and the buffer was modified.
3513 *
3514 * Used by ":bdel" and ":only".
3515 */
3516 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003517close_others(
3518 int message,
3519 int forceit) /* always hide all other windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 win_T *wp;
3522 win_T *nextwp;
3523 int r;
3524
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003525 if (one_window())
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003527 if (message && !autocmd_busy)
Bram Moolenaar32526b32019-01-19 17:43:09 +01003528 msg(_(m_onlyone));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 return;
3530 }
3531
3532 /* Be very careful here: autocommands may change the window layout. */
3533 for (wp = firstwin; win_valid(wp); wp = nextwp)
3534 {
3535 nextwp = wp->w_next;
3536 if (wp != curwin) /* don't close current window */
3537 {
3538
3539 /* Check if it's allowed to abandon this window */
3540 r = can_abandon(wp->w_buffer, forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 if (!win_valid(wp)) /* autocommands messed wp up */
3542 {
3543 nextwp = firstwin;
3544 continue;
3545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 if (!r)
3547 {
3548#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3549 if (message && (p_confirm || cmdmod.confirm) && p_write)
3550 {
3551 dialog_changed(wp->w_buffer, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 if (!win_valid(wp)) /* autocommands messed wp up */
3553 {
3554 nextwp = firstwin;
3555 continue;
3556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 if (bufIsChanged(wp->w_buffer))
3559#endif
3560 continue;
3561 }
Bram Moolenaareb44a682017-08-03 22:44:55 +02003562 win_close(wp, !buf_hide(wp->w_buffer)
3563 && !bufIsChanged(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 }
3566
Bram Moolenaar459ca562016-11-10 18:16:33 +01003567 if (message && !ONE_WINDOW)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003568 emsg(_("E445: Other window contains changes"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569}
3570
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003571 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003572win_init_empty(win_T *wp)
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003573{
3574 redraw_win_later(wp, NOT_VALID);
3575 wp->w_lines_valid = 0;
3576 wp->w_cursor.lnum = 1;
3577 wp->w_curswant = wp->w_cursor.col = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003578 wp->w_cursor.coladd = 0;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003579 wp->w_pcmark.lnum = 1; /* pcmark not cleared but set to line 1 */
3580 wp->w_pcmark.col = 0;
3581 wp->w_prev_pcmark.lnum = 0;
3582 wp->w_prev_pcmark.col = 0;
3583 wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584#ifdef FEAT_DIFF
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003585 wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00003587 wp->w_botline = 2;
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003588#if defined(FEAT_SYN_HL) || defined(FEAT_SPELL)
Bram Moolenaara971b822011-09-14 14:43:25 +02003589 wp->w_s = &wp->w_buffer->b_s;
3590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591}
3592
3593/*
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003594 * Init the current window "curwin".
3595 * Called when a new file is being edited.
3596 */
3597 void
3598curwin_init(void)
3599{
3600 win_init_empty(curwin);
3601}
3602
3603/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 * Allocate the first window and put an empty buffer in it.
3605 * Called from main().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003606 * Return FAIL when something goes wrong (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003608 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003609win_alloc_first(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003611 if (win_alloc_firstwin(NULL) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003612 return FAIL;
3613
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003614 first_tabpage = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003615 if (first_tabpage == NULL)
3616 return FAIL;
3617 first_tabpage->tp_topframe = topframe;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003618 curtab = first_tabpage;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003619
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003620 return OK;
3621}
3622
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003623/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003624 * Allocate and init a window that is not a regular window.
3625 * This can only be done after the first window is fully initialized, thus it
3626 * can't be in win_alloc_first().
3627 */
3628 win_T *
3629win_alloc_popup_win(void)
3630{
3631 win_T *wp;
3632
3633 wp = win_alloc(NULL, TRUE);
3634 if (wp != NULL)
3635 {
3636 // We need to initialize options with something, using the current
3637 // window makes most sense.
3638 win_init_some(wp, curwin);
3639
3640 RESET_BINDING(wp);
3641 new_frame(wp);
3642 }
3643 return wp;
3644}
3645
3646/*
3647 * Initialize window "wp" to display buffer "buf".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003648 */
3649 void
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003650win_init_popup_win(win_T *wp, buf_T *buf)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003651{
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003652 wp->w_buffer = buf;
3653 ++buf->b_nwindows;
3654 win_init_empty(wp); // set cursor and topline to safe values
3655
3656 // Make sure w_localdir and globaldir are NULL to avoid a chdir() in
3657 // win_enter_ext().
3658 VIM_CLEAR(wp->w_localdir);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003659}
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003660
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003661/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003662 * Allocate the first window or the first window in a new tab page.
3663 * When "oldwin" is NULL create an empty buffer for it.
Bram Moolenaar4033c552017-09-16 20:54:51 +02003664 * When "oldwin" is not NULL copy info from it to the new window.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003665 * Return FAIL when something goes wrong (out of memory).
3666 */
3667 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003668win_alloc_firstwin(win_T *oldwin)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003669{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003670 curwin = win_alloc(NULL, FALSE);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003671 if (oldwin == NULL)
3672 {
3673 /* Very first window, need to create an empty buffer for it and
3674 * initialize from scratch. */
3675 curbuf = buflist_new(NULL, NULL, 1L, BLN_LISTED);
3676 if (curwin == NULL || curbuf == NULL)
3677 return FAIL;
3678 curwin->w_buffer = curbuf;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003679#ifdef FEAT_SYN_HL
3680 curwin->w_s = &(curbuf->b_s);
3681#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003682 curbuf->b_nwindows = 1; /* there is one window */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003683 curwin->w_alist = &global_alist;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003684 curwin_init(); /* init current window */
3685 }
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003686 else
3687 {
3688 /* First window in new tab page, initialize it from "oldwin". */
Bram Moolenaar884ae642009-02-22 01:37:59 +00003689 win_init(curwin, oldwin, 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003690
Bram Moolenaar3368ea22010-09-21 16:56:35 +02003691 /* We don't want cursor- and scroll-binding in the first window. */
3692 RESET_BINDING(curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003695 new_frame(curwin);
3696 if (curwin->w_frame == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003697 return FAIL;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003698 topframe = curwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 topframe->fr_height = Rows - p_ch;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003701
3702 return OK;
3703}
3704
3705/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003706 * Create a frame for window "wp".
3707 */
3708 static void
3709new_frame(win_T *wp)
3710{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003711 frame_T *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003712
3713 wp->w_frame = frp;
3714 if (frp != NULL)
3715 {
3716 frp->fr_layout = FR_LEAF;
3717 frp->fr_win = wp;
3718 }
3719}
3720
3721/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003722 * Initialize the window and frame size to the maximum.
3723 */
3724 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003725win_init_size(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003726{
3727 firstwin->w_height = ROWS_AVAIL;
3728 topframe->fr_height = ROWS_AVAIL;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003729 firstwin->w_width = Columns;
3730 topframe->fr_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731}
3732
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003733/*
3734 * Allocate a new tabpage_T and init the values.
3735 * Returns NULL when out of memory.
3736 */
3737 static tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003738alloc_tabpage(void)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003739{
3740 tabpage_T *tp;
Bram Moolenaar429fa852013-04-15 12:27:36 +02003741# ifdef FEAT_GUI
3742 int i;
3743# endif
3744
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003745
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003746 tp = ALLOC_CLEAR_ONE(tabpage_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02003747 if (tp == NULL)
3748 return NULL;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003749
Bram Moolenaar429fa852013-04-15 12:27:36 +02003750# ifdef FEAT_EVAL
3751 /* init t: variables */
3752 tp->tp_vars = dict_alloc();
3753 if (tp->tp_vars == NULL)
3754 {
3755 vim_free(tp);
3756 return NULL;
3757 }
3758 init_var_dict(tp->tp_vars, &tp->tp_winvar, VAR_SCOPE);
3759# endif
3760
3761# ifdef FEAT_GUI
3762 for (i = 0; i < 3; i++)
3763 tp->tp_prev_which_scrollbars[i] = -1;
Bram Moolenaar371d5402006-03-20 21:47:49 +00003764# endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003765# ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02003766 tp->tp_diff_invalid = TRUE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003767# endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02003768 tp->tp_ch_used = p_ch;
3769
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003770 return tp;
3771}
3772
Bram Moolenaard8fc5c02006-04-29 21:55:22 +00003773 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003774free_tabpage(tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003775{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003776 int idx;
3777
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003778# ifdef FEAT_DIFF
3779 diff_clear(tp);
3780# endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003781# ifdef FEAT_PROP_POPUP
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02003782 while (tp->tp_first_popupwin != NULL)
3783 popup_close_tabpage(tp, tp->tp_first_popupwin->w_id);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003784#endif
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003785 for (idx = 0; idx < SNAP_COUNT; ++idx)
3786 clear_snapshot(tp, idx);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003787#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02003788 vars_clear(&tp->tp_vars->dv_hashtab); /* free all t: variables */
3789 hash_init(&tp->tp_vars->dv_hashtab);
3790 unref_var_dict(tp->tp_vars);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003791#endif
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003792
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003793 vim_free(tp->tp_localdir);
3794
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02003795#ifdef FEAT_PYTHON
3796 python_tabpage_free(tp);
3797#endif
3798
3799#ifdef FEAT_PYTHON3
3800 python3_tabpage_free(tp);
3801#endif
3802
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003803 vim_free(tp);
3804}
3805
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003806/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003807 * Create a new Tab page with one window.
3808 * It will edit the current buffer, like after ":split".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003809 * When "after" is 0 put it just after the current Tab page.
3810 * Otherwise put it just before tab page "after".
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003811 * Return FAIL or OK.
3812 */
3813 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003814win_new_tabpage(int after)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003815{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003816 tabpage_T *tp = curtab;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003817 tabpage_T *newtp;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003818 int n;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003819
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003820 newtp = alloc_tabpage();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003821 if (newtp == NULL)
3822 return FAIL;
3823
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003824 /* Remember the current windows in this Tab page. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003825 if (leave_tabpage(curbuf, TRUE) == FAIL)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003826 {
3827 vim_free(newtp);
3828 return FAIL;
3829 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003830 curtab = newtp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003832 newtp->tp_localdir = (tp->tp_localdir == NULL)
3833 ? NULL : vim_strsave(tp->tp_localdir);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003834 /* Create a new empty window. */
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003835 if (win_alloc_firstwin(tp->tp_curwin) == OK)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003836 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003837 /* Make the new Tab page the new topframe. */
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003838 if (after == 1)
3839 {
3840 /* New tab page becomes the first one. */
3841 newtp->tp_next = first_tabpage;
3842 first_tabpage = newtp;
3843 }
3844 else
3845 {
3846 if (after > 0)
3847 {
3848 /* Put new tab page before tab page "after". */
3849 n = 2;
3850 for (tp = first_tabpage; tp->tp_next != NULL
3851 && n < after; tp = tp->tp_next)
3852 ++n;
3853 }
3854 newtp->tp_next = tp->tp_next;
3855 tp->tp_next = newtp;
3856 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003857 win_init_size();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003858 firstwin->w_winrow = tabline_height();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003859 win_comp_scroll(curwin);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003860
3861 newtp->tp_topframe = topframe;
Bram Moolenaarf740b292006-02-16 22:11:02 +00003862 last_status(FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003863
3864#if defined(FEAT_GUI)
3865 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
3866 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00003867 gui_may_update_scrollbars();
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003868#endif
Bram Moolenaar6d41c782018-06-06 09:11:12 +02003869#ifdef FEAT_JOB_CHANNEL
3870 entering_window(curwin);
3871#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00003872
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01003873 redraw_all_later(NOT_VALID);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003874 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003875 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc917da42016-07-19 22:31:36 +02003876 apply_autocmds(EVENT_TABNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003877 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003878 return OK;
3879 }
3880
3881 /* Failed, get back the previous Tab page */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02003882 enter_tabpage(curtab, curbuf, TRUE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003883 return FAIL;
3884}
3885
3886/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003887 * Open a new tab page if ":tab cmd" was used. It will edit the same buffer,
3888 * like with ":split".
3889 * Returns OK if a new tab page was created, FAIL otherwise.
3890 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02003891 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003892may_open_tabpage(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003893{
Bram Moolenaard326ce82007-03-11 14:48:29 +00003894 int n = (cmdmod.tab == 0) ? postponed_split_tab : cmdmod.tab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003895
Bram Moolenaard326ce82007-03-11 14:48:29 +00003896 if (n != 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003897 {
3898 cmdmod.tab = 0; /* reset it to avoid doing it twice */
Bram Moolenaard326ce82007-03-11 14:48:29 +00003899 postponed_split_tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003900 return win_new_tabpage(n);
3901 }
3902 return FAIL;
3903}
3904
3905/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003906 * Create up to "maxcount" tabpages with empty windows.
3907 * Returns the number of resulting tab pages.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003908 */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003909 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003910make_tabpages(int maxcount)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003911{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003912 int count = maxcount;
3913 int todo;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003914
Bram Moolenaare1438bb2006-03-01 22:01:55 +00003915 /* Limit to 'tabpagemax' tabs. */
3916 if (count > p_tpm)
3917 count = p_tpm;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003918
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003919 /*
3920 * Don't execute autocommands while creating the tab pages. Must do that
3921 * when putting the buffers in the windows.
3922 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003923 block_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003924
3925 for (todo = count - 1; todo > 0; --todo)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00003926 if (win_new_tabpage(0) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003927 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003928
Bram Moolenaar78ab3312007-09-29 12:16:41 +00003929 unblock_autocmds();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003930
3931 /* return actual number of tab pages */
3932 return (count - todo);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003933}
3934
3935/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00003936 * Return TRUE when "tpc" points to a valid tab page.
3937 */
3938 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01003939valid_tabpage(tabpage_T *tpc)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003940{
3941 tabpage_T *tp;
3942
Bram Moolenaar29323592016-07-24 22:04:11 +02003943 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00003944 if (tp == tpc)
3945 return TRUE;
3946 return FALSE;
3947}
3948
3949/*
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003950 * Return TRUE when "tpc" points to a valid tab page and at least one window is
3951 * valid.
3952 */
3953 int
3954valid_tabpage_win(tabpage_T *tpc)
3955{
3956 tabpage_T *tp;
3957 win_T *wp;
3958
3959 FOR_ALL_TABPAGES(tp)
3960 {
3961 if (tp == tpc)
3962 {
3963 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
3964 {
3965 if (win_valid_any_tab(wp))
3966 return TRUE;
3967 }
3968 return FALSE;
3969 }
3970 }
3971 /* shouldn't happen */
3972 return FALSE;
3973}
3974
3975/*
3976 * Close tabpage "tab", assuming it has no windows in it.
3977 * There must be another tabpage or this will crash.
3978 */
3979 void
3980close_tabpage(tabpage_T *tab)
3981{
3982 tabpage_T *ptp;
3983
3984 if (tab == first_tabpage)
3985 {
3986 first_tabpage = tab->tp_next;
3987 ptp = first_tabpage;
3988 }
3989 else
3990 {
3991 for (ptp = first_tabpage; ptp != NULL && ptp->tp_next != tab;
3992 ptp = ptp->tp_next)
3993 ;
Bram Moolenaara37ffaa2017-03-21 21:58:00 +01003994 assert(ptp != NULL);
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01003995 ptp->tp_next = tab->tp_next;
3996 }
3997
3998 goto_tabpage_tp(ptp, FALSE, FALSE);
3999 free_tabpage(tab);
4000}
4001
4002/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00004003 * Find tab page "n" (first one is 1). Returns NULL when not found.
4004 */
4005 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004006find_tabpage(int n)
Bram Moolenaarf740b292006-02-16 22:11:02 +00004007{
4008 tabpage_T *tp;
4009 int i = 1;
4010
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004011 if (n == 0)
4012 return curtab;
4013
Bram Moolenaarf740b292006-02-16 22:11:02 +00004014 for (tp = first_tabpage; tp != NULL && i != n; tp = tp->tp_next)
4015 ++i;
4016 return tp;
4017}
4018
4019/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004020 * Get index of tab page "tp". First one has index 1.
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004021 * When not found returns number of tab pages plus one.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004022 */
4023 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004024tabpage_index(tabpage_T *ftp)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004025{
4026 int i = 1;
4027 tabpage_T *tp;
4028
4029 for (tp = first_tabpage; tp != NULL && tp != ftp; tp = tp->tp_next)
4030 ++i;
4031 return i;
4032}
4033
4034/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004035 * Prepare for leaving the current tab page.
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02004036 * When autocommands change "curtab" we don't leave the tab page and return
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004037 * FAIL.
4038 * Careful: When OK is returned need to get a new tab page very very soon!
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004039 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004040 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004041leave_tabpage(
4042 buf_T *new_curbuf UNUSED, /* what is going to be the new curbuf,
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004043 NULL if unknown */
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004044 int trigger_leave_autocmds UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004045{
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004046 tabpage_T *tp = curtab;
4047
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004048#ifdef FEAT_JOB_CHANNEL
4049 leaving_window(curwin);
4050#endif
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004051 reset_VIsual_and_resel(); /* stop Visual mode */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004052 if (trigger_leave_autocmds)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004053 {
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004054 if (new_curbuf != curbuf)
4055 {
4056 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4057 if (curtab != tp)
4058 return FAIL;
4059 }
4060 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4061 if (curtab != tp)
4062 return FAIL;
4063 apply_autocmds(EVENT_TABLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004064 if (curtab != tp)
4065 return FAIL;
4066 }
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004067#if defined(FEAT_GUI)
4068 /* Remove the scrollbars. They may be added back later. */
4069 if (gui.in_use)
4070 gui_remove_scrollbars();
4071#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004072 tp->tp_curwin = curwin;
Bram Moolenaarf740b292006-02-16 22:11:02 +00004073 tp->tp_prevwin = prevwin;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004074 tp->tp_firstwin = firstwin;
4075 tp->tp_lastwin = lastwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004076 tp->tp_old_Rows = Rows;
4077 tp->tp_old_Columns = Columns;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004078 firstwin = NULL;
4079 lastwin = NULL;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004080 return OK;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004081}
4082
4083/*
4084 * Start using tab page "tp".
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004085 * Only to be used after leave_tabpage() or freeing the current tab page.
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004086 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4087 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004088 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004089 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004090enter_tabpage(
4091 tabpage_T *tp,
4092 buf_T *old_curbuf UNUSED,
Bram Moolenaarf1d25012016-03-03 12:22:53 +01004093 int trigger_enter_autocmds,
4094 int trigger_leave_autocmds)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004095{
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004096 int old_off = tp->tp_firstwin->w_winrow;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004097 win_T *next_prevwin = tp->tp_prevwin;
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004098
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004099 curtab = tp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004100 firstwin = tp->tp_firstwin;
4101 lastwin = tp->tp_lastwin;
4102 topframe = tp->tp_topframe;
Bram Moolenaar773560b2006-05-06 21:38:18 +00004103
4104 /* We would like doing the TabEnter event first, but we don't have a
4105 * valid current window yet, which may break some commands.
4106 * This triggers autocommands, thus may make "tp" invalid. */
Bram Moolenaarc917da42016-07-19 22:31:36 +02004107 win_enter_ext(tp->tp_curwin, FALSE, TRUE, FALSE,
Bram Moolenaard6949742013-06-16 14:18:28 +02004108 trigger_enter_autocmds, trigger_leave_autocmds);
Bram Moolenaar773560b2006-05-06 21:38:18 +00004109 prevwin = next_prevwin;
4110
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004111 last_status(FALSE); /* status line may appear or disappear */
4112 (void)win_comp_pos(); /* recompute w_winrow for all windows */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004113#ifdef FEAT_DIFF
4114 diff_need_scrollbind = TRUE;
4115#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004116
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004117 /* The tabpage line may have appeared or disappeared, may need to resize
4118 * the frames for that. When the Vim window was resized need to update
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004119 * frame sizes too. Use the stored value of p_ch, so that it can be
4120 * different for each tab page. */
Bram Moolenaar0fef0ae2019-05-01 20:30:40 +02004121 if (p_ch != curtab->tp_ch_used)
4122 clear_cmdline = TRUE;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00004123 p_ch = curtab->tp_ch_used;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004124 if (curtab->tp_old_Rows != Rows || (old_off != firstwin->w_winrow
4125#ifdef FEAT_GUI_TABLINE
4126 && !gui_use_tabline()
4127#endif
4128 ))
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004129 shell_new_rows();
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004130 if (curtab->tp_old_Columns != Columns && starting == 0)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004131 shell_new_columns(); /* update window widths */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00004132
4133#if defined(FEAT_GUI)
4134 /* When 'guioptions' includes 'L' or 'R' may have to remove or add
4135 * scrollbars. Have to update them anyway. */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004136 gui_may_update_scrollbars();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004137#endif
4138
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004139 /* Apply autocommands after updating the display, when 'rows' and
4140 * 'columns' have been set correctly. */
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004141 if (trigger_enter_autocmds)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004142 {
4143 apply_autocmds(EVENT_TABENTER, NULL, NULL, FALSE, curbuf);
4144 if (old_curbuf != curbuf)
4145 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4146 }
Bram Moolenaar5ad15df2012-03-16 19:07:58 +01004147
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01004148 redraw_all_later(NOT_VALID);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004149}
4150
4151/*
4152 * Go to tab page "n". For ":tab N" and "Ngt".
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004153 * When "n" is 9999 go to the last tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004154 */
4155 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004156goto_tabpage(int n)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004157{
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02004158 tabpage_T *tp = NULL; // shut up compiler
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004159 tabpage_T *ttp;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004160 int i;
4161
Bram Moolenaard68071d2006-05-02 22:08:30 +00004162 if (text_locked())
4163 {
4164 /* Not allowed when editing the command line. */
Bram Moolenaar5a497892016-09-03 16:29:04 +02004165 text_locked_msg();
Bram Moolenaard68071d2006-05-02 22:08:30 +00004166 return;
4167 }
4168
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004169 /* If there is only one it can't work. */
4170 if (first_tabpage->tp_next == NULL)
4171 {
4172 if (n > 1)
4173 beep_flush();
4174 return;
4175 }
4176
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004177 if (n == 0)
4178 {
4179 /* No count, go to next tab page, wrap around end. */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004180 if (curtab->tp_next == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004181 tp = first_tabpage;
4182 else
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004183 tp = curtab->tp_next;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004184 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004185 else if (n < 0)
4186 {
4187 /* "gT": go to previous tab page, wrap around end. "N gT" repeats
4188 * this N times. */
4189 ttp = curtab;
4190 for (i = n; i < 0; ++i)
4191 {
4192 for (tp = first_tabpage; tp->tp_next != ttp && tp->tp_next != NULL;
4193 tp = tp->tp_next)
4194 ;
4195 ttp = tp;
4196 }
4197 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004198 else if (n == 9999)
4199 {
4200 /* Go to last tab page. */
4201 for (tp = first_tabpage; tp->tp_next != NULL; tp = tp->tp_next)
4202 ;
4203 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004204 else
4205 {
4206 /* Go to tab page "n". */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004207 tp = find_tabpage(n);
Bram Moolenaarf740b292006-02-16 22:11:02 +00004208 if (tp == NULL)
4209 {
4210 beep_flush();
4211 return;
4212 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004213 }
4214
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004215 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004216
4217#ifdef FEAT_GUI_TABLINE
4218 if (gui_use_tabline())
Bram Moolenaara226a6d2006-02-26 23:59:20 +00004219 gui_mch_set_curtab(tabpage_index(curtab));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004220#endif
4221}
4222
4223/*
4224 * Go to tabpage "tp".
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004225 * Only trigger *Enter autocommands when trigger_enter_autocmds is TRUE.
4226 * Only trigger *Leave autocommands when trigger_leave_autocmds is TRUE.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004227 * Note: doesn't update the GUI tab.
4228 */
4229 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004230goto_tabpage_tp(
4231 tabpage_T *tp,
4232 int trigger_enter_autocmds,
4233 int trigger_leave_autocmds)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004234{
Bram Moolenaarc6af8122010-05-21 12:04:55 +02004235 /* Don't repeat a message in another tab page. */
4236 set_keep_msg(NULL, 0);
4237
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004238 if (tp != curtab && leave_tabpage(tp->tp_curwin->w_buffer,
4239 trigger_leave_autocmds) == OK)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004240 {
4241 if (valid_tabpage(tp))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004242 enter_tabpage(tp, curbuf, trigger_enter_autocmds,
4243 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004244 else
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004245 enter_tabpage(curtab, curbuf, trigger_enter_autocmds,
4246 trigger_leave_autocmds);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00004247 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249
4250/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004251 * Enter window "wp" in tab page "tp".
4252 * Also updates the GUI tab.
4253 */
4254 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004255goto_tabpage_win(tabpage_T *tp, win_T *wp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004256{
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004257 goto_tabpage_tp(tp, TRUE, TRUE);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004258 if (curtab == tp && win_valid(wp))
4259 {
4260 win_enter(wp, TRUE);
4261# ifdef FEAT_GUI_TABLINE
4262 if (gui_use_tabline())
4263 gui_mch_set_curtab(tabpage_index(curtab));
4264# endif
4265 }
4266}
4267
4268/*
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004269 * Move the current tab page to after tab page "nr".
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004270 */
4271 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004272tabpage_move(int nr)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004273{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004274 int n = 1;
4275 tabpage_T *tp, *tp_dst;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004276
4277 if (first_tabpage->tp_next == NULL)
4278 return;
4279
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004280 for (tp = first_tabpage; tp->tp_next != NULL && n < nr; tp = tp->tp_next)
4281 ++n;
4282
4283 if (tp == curtab || (nr > 0 && tp->tp_next != NULL
4284 && tp->tp_next == curtab))
4285 return;
4286
4287 tp_dst = tp;
4288
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004289 /* Remove the current tab page from the list of tab pages. */
4290 if (curtab == first_tabpage)
4291 first_tabpage = curtab->tp_next;
4292 else
4293 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004294 FOR_ALL_TABPAGES(tp)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004295 if (tp->tp_next == curtab)
4296 break;
4297 if (tp == NULL) /* "cannot happen" */
4298 return;
4299 tp->tp_next = curtab->tp_next;
4300 }
4301
4302 /* Re-insert it at the specified position. */
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004303 if (nr <= 0)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004304 {
4305 curtab->tp_next = first_tabpage;
4306 first_tabpage = curtab;
4307 }
4308 else
4309 {
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02004310 curtab->tp_next = tp_dst->tp_next;
4311 tp_dst->tp_next = curtab;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00004312 }
4313
4314 /* Need to redraw the tabline. Tab page contents doesn't change. */
4315 redraw_tabline = TRUE;
4316}
4317
4318
4319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 * Go to another window.
4321 * When jumping to another buffer, stop Visual mode. Do this before
4322 * changing windows so we can yank the selection into the '*' register.
4323 * When jumping to another window on the same buffer, adjust its cursor
4324 * position to keep the same Visual area.
4325 */
4326 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004327win_goto(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328{
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004329#ifdef FEAT_CONCEAL
4330 win_T *owp = curwin;
4331#endif
4332
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004333 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004334 return;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004335 if (text_locked())
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 {
4337 beep_flush();
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004338 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 return;
4340 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004341 if (curbuf_locked())
4342 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004343
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 if (wp->w_buffer != curbuf)
4345 reset_VIsual_and_resel();
4346 else if (VIsual_active)
4347 wp->w_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349#ifdef FEAT_GUI
4350 need_mouse_correct = TRUE;
4351#endif
4352 win_enter(wp, TRUE);
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004353
4354#ifdef FEAT_CONCEAL
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004355 // Conceal cursor line in previous window, unconceal in current window.
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004356 if (win_valid(owp) && owp->w_p_cole > 0 && !msg_scrolled)
Bram Moolenaar535d5b62019-01-11 20:45:36 +01004357 redrawWinline(owp, owp->w_cursor.lnum);
Bram Moolenaar530e7df2013-02-06 13:38:02 +01004358 if (curwin->w_p_cole > 0 && !msg_scrolled)
4359 need_cursor_line_redraw = TRUE;
Bram Moolenaar23c347c2010-07-14 20:57:00 +02004360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361}
4362
4363#if defined(FEAT_PERL) || defined(PROTO)
4364/*
4365 * Find window number "winnr" (counting top to bottom).
4366 */
4367 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004368win_find_nr(int winnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369{
4370 win_T *wp;
4371
Bram Moolenaar29323592016-07-24 22:04:11 +02004372 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 if (--winnr == 0)
4374 break;
4375 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376}
4377#endif
4378
Bram Moolenaar4033c552017-09-16 20:54:51 +02004379#if ((defined(FEAT_PYTHON) || defined(FEAT_PYTHON3))) || defined(PROTO)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004380/*
4381 * Find the tabpage for window "win".
4382 */
4383 tabpage_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004384win_find_tabpage(win_T *win)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004385{
4386 win_T *wp;
4387 tabpage_T *tp;
4388
Bram Moolenaar29323592016-07-24 22:04:11 +02004389 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar105bc352013-05-17 16:03:57 +02004390 if (wp == win)
4391 return tp;
4392 return NULL;
4393}
4394#endif
4395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004397 * Get the above or below neighbor window of the specified window.
4398 * up - TRUE for the above neighbor
4399 * count - nth neighbor window
4400 * Returns the specified window if the neighbor is not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 */
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004402 win_T *
4403win_vert_neighbor(tabpage_T *tp, win_T *wp, int up, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
4405 frame_T *fr;
4406 frame_T *nfr;
4407 frame_T *foundfr;
4408
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004409 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 while (count--)
4411 {
4412 /*
4413 * First go upwards in the tree of frames until we find a upwards or
4414 * downwards neighbor.
4415 */
4416 fr = foundfr;
4417 for (;;)
4418 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004419 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 goto end;
4421 if (up)
4422 nfr = fr->fr_prev;
4423 else
4424 nfr = fr->fr_next;
4425 if (fr->fr_parent->fr_layout == FR_COL && nfr != NULL)
4426 break;
4427 fr = fr->fr_parent;
4428 }
4429
4430 /*
4431 * Now go downwards to find the bottom or top frame in it.
4432 */
4433 for (;;)
4434 {
4435 if (nfr->fr_layout == FR_LEAF)
4436 {
4437 foundfr = nfr;
4438 break;
4439 }
4440 fr = nfr->fr_child;
4441 if (nfr->fr_layout == FR_ROW)
4442 {
4443 /* Find the frame at the cursor row. */
4444 while (fr->fr_next != NULL
4445 && frame2win(fr)->w_wincol + fr->fr_width
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004446 <= wp->w_wincol + wp->w_wcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 fr = fr->fr_next;
4448 }
4449 if (nfr->fr_layout == FR_COL && up)
4450 while (fr->fr_next != NULL)
4451 fr = fr->fr_next;
4452 nfr = fr;
4453 }
4454 }
4455end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004456 return foundfr != NULL ? foundfr->fr_win : NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457}
4458
4459/*
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004460 * Move to window above or below "count" times.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
4462 static void
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004463win_goto_ver(
4464 int up, // TRUE to go to win above
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004465 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466{
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004467 win_T *win;
4468
4469 win = win_vert_neighbor(curtab, curwin, up, count);
4470 if (win != NULL)
4471 win_goto(win);
4472}
4473
4474/*
4475 * Get the left or right neighbor window of the specified window.
4476 * left - TRUE for the left neighbor
4477 * count - nth neighbor window
4478 * Returns the specified window if the neighbor is not found.
4479 */
4480 win_T *
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02004481win_horz_neighbor(tabpage_T *tp, win_T *wp, int left, long count)
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004482{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 frame_T *fr;
4484 frame_T *nfr;
4485 frame_T *foundfr;
4486
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004487 foundfr = wp->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 while (count--)
4489 {
4490 /*
4491 * First go upwards in the tree of frames until we find a left or
4492 * right neighbor.
4493 */
4494 fr = foundfr;
4495 for (;;)
4496 {
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004497 if (fr == tp->tp_topframe)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 goto end;
4499 if (left)
4500 nfr = fr->fr_prev;
4501 else
4502 nfr = fr->fr_next;
4503 if (fr->fr_parent->fr_layout == FR_ROW && nfr != NULL)
4504 break;
4505 fr = fr->fr_parent;
4506 }
4507
4508 /*
4509 * Now go downwards to find the leftmost or rightmost frame in it.
4510 */
4511 for (;;)
4512 {
4513 if (nfr->fr_layout == FR_LEAF)
4514 {
4515 foundfr = nfr;
4516 break;
4517 }
4518 fr = nfr->fr_child;
4519 if (nfr->fr_layout == FR_COL)
4520 {
4521 /* Find the frame at the cursor row. */
4522 while (fr->fr_next != NULL
4523 && frame2win(fr)->w_winrow + fr->fr_height
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004524 <= wp->w_winrow + wp->w_wrow)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 fr = fr->fr_next;
4526 }
4527 if (nfr->fr_layout == FR_ROW && left)
4528 while (fr->fr_next != NULL)
4529 fr = fr->fr_next;
4530 nfr = fr;
4531 }
4532 }
4533end:
Bram Moolenaar46ad2882019-04-08 20:01:47 +02004534 return foundfr != NULL ? foundfr->fr_win : NULL;
4535}
4536
4537/*
4538 * Move to left or right window.
4539 */
4540 static void
4541win_goto_hor(
4542 int left, // TRUE to go to left win
4543 long count)
4544{
4545 win_T *win;
4546
4547 win = win_horz_neighbor(curtab, curwin, left, count);
4548 if (win != NULL)
4549 win_goto(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552/*
4553 * Make window "wp" the current window.
4554 */
4555 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004556win_enter(win_T *wp, int undo_sync)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
Bram Moolenaarc917da42016-07-19 22:31:36 +02004558 win_enter_ext(wp, undo_sync, FALSE, FALSE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559}
4560
4561/*
4562 * Make window wp the current window.
4563 * Can be called with "curwin_invalid" TRUE, which means that curwin has just
4564 * been closed and isn't valid.
4565 */
4566 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004567win_enter_ext(
4568 win_T *wp,
4569 int undo_sync,
4570 int curwin_invalid,
Bram Moolenaar6f470022018-04-10 18:47:20 +02004571 int trigger_new_autocmds,
4572 int trigger_enter_autocmds,
4573 int trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 int other_buffer = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576
4577 if (wp == curwin && !curwin_invalid) /* nothing to do */
4578 return;
4579
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004580#ifdef FEAT_JOB_CHANNEL
4581 if (!curwin_invalid)
4582 leaving_window(curwin);
4583#endif
4584
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004585 if (!curwin_invalid && trigger_leave_autocmds)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
4587 /*
4588 * Be careful: If autocommands delete the window, return now.
4589 */
4590 if (wp->w_buffer != curbuf)
4591 {
4592 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
4593 other_buffer = TRUE;
4594 if (!win_valid(wp))
4595 return;
4596 }
4597 apply_autocmds(EVENT_WINLEAVE, NULL, NULL, FALSE, curbuf);
4598 if (!win_valid(wp))
4599 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004600#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 /* autocmds may abort script processing */
4602 if (aborting())
4603 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606
4607 /* sync undo before leaving the current buffer */
4608 if (undo_sync && curbuf != wp->w_buffer)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004609 u_sync(FALSE);
Bram Moolenaarec1561c2014-06-17 13:52:40 +02004610
4611 /* Might need to scroll the old window before switching, e.g., when the
4612 * cursor was moved. */
4613 update_topline();
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 /* may have to copy the buffer options when 'cpo' contains 'S' */
4616 if (wp->w_buffer != curbuf)
4617 buf_copy_options(wp->w_buffer, BCO_ENTER | BCO_NOHELP);
4618 if (!curwin_invalid)
4619 {
4620 prevwin = curwin; /* remember for CTRL-W p */
4621 curwin->w_redr_status = TRUE;
4622 }
4623 curwin = wp;
4624 curbuf = wp->w_buffer;
4625 check_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 if (!virtual_active())
4627 curwin->w_cursor.coladd = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 changed_line_abv_curs(); /* assume cursor position needs updating */
4629
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004630 if (curwin->w_localdir != NULL || curtab->tp_localdir != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004632 char_u *dirname;
4633
4634 // Window or tab has a local directory: Save current directory as
4635 // global directory (unless that was done already) and change to the
4636 // local directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (globaldir == NULL)
4638 {
4639 char_u cwd[MAXPATHL];
4640
4641 if (mch_dirname(cwd, MAXPATHL) == OK)
4642 globaldir = vim_strsave(cwd);
4643 }
Bram Moolenaar00aa0692019-04-27 20:37:57 +02004644 if (curwin->w_localdir != NULL)
4645 dirname = curwin->w_localdir;
4646 else
4647 dirname = curtab->tp_localdir;
4648
4649 if (mch_chdir((char *)dirname) == 0)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004650 shorten_fnames(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 else if (globaldir != NULL)
4653 {
4654 /* Window doesn't have a local directory and we are not in the global
4655 * directory: Change to the global directory. */
Bram Moolenaar42335f52018-09-13 15:33:43 +02004656 vim_ignored = mch_chdir((char *)globaldir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004657 VIM_CLEAR(globaldir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 shorten_fnames(TRUE);
4659 }
4660
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004661#ifdef FEAT_JOB_CHANNEL
4662 entering_window(curwin);
4663#endif
Bram Moolenaarec66c412019-10-11 21:19:13 +02004664 // Careful: autocommands may close the window and make "wp" invalid
Bram Moolenaarc917da42016-07-19 22:31:36 +02004665 if (trigger_new_autocmds)
4666 apply_autocmds(EVENT_WINNEW, NULL, NULL, FALSE, curbuf);
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004667 if (trigger_enter_autocmds)
4668 {
4669 apply_autocmds(EVENT_WINENTER, NULL, NULL, FALSE, curbuf);
4670 if (other_buffer)
4671 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
4672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673
4674#ifdef FEAT_TITLE
4675 maketitle();
4676#endif
4677 curwin->w_redr_status = TRUE;
Bram Moolenaarfbbd1022019-10-07 22:38:58 +02004678#ifdef FEAT_TERMINAL
Bram Moolenaarec66c412019-10-11 21:19:13 +02004679 if (bt_terminal(curwin->w_buffer))
Bram Moolenaara27e1dc2019-10-07 22:27:36 +02004680 // terminal is likely in another mode
4681 redraw_mode = TRUE;
Bram Moolenaarfbbd1022019-10-07 22:38:58 +02004682#endif
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004683 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (restart_edit)
4685 redraw_later(VALID); /* causes status line redraw */
4686
4687 /* set window height to desired minimal value */
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02004688 if (curwin->w_height < p_wh && !curwin->w_p_wfh
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004689#ifdef FEAT_PROP_POPUP
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02004690 && !popup_is_popup(curwin)
4691#endif
4692 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 win_setheight((int)p_wh);
4694 else if (curwin->w_height == 0)
4695 win_setheight(1);
4696
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 /* set window width to desired minimal value */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00004698 if (curwin->w_width < p_wiw && !curwin->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 win_setwidth((int)p_wiw);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
Bram Moolenaarb20b9e12019-09-21 20:48:04 +02004701 setmouse(); // in case jumped to/from help buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
Bram Moolenaar498efdb2006-09-05 14:31:54 +00004703 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02004704 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705}
4706
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708/*
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004709 * Jump to the first open window that contains buffer "buf", if one exists.
4710 * Returns a pointer to the window found, otherwise NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 */
4712 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004713buf_jump_open_win(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004715 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004717 if (curwin->w_buffer == buf)
4718 wp = curwin;
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004719 else
Bram Moolenaar29323592016-07-24 22:04:11 +02004720 FOR_ALL_WINDOWS(wp)
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004721 if (wp->w_buffer == buf)
4722 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 if (wp != NULL)
4724 win_enter(wp, FALSE);
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004725 return wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726}
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004727
4728/*
4729 * Jump to the first open window in any tab page that contains buffer "buf",
4730 * if one exists.
4731 * Returns a pointer to the window found, otherwise NULL.
4732 */
4733 win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004734buf_jump_open_tab(buf_T *buf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004735{
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004736 win_T *wp = buf_jump_open_win(buf);
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004737 tabpage_T *tp;
4738
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004739 if (wp != NULL)
4740 return wp;
4741
Bram Moolenaar29323592016-07-24 22:04:11 +02004742 FOR_ALL_TABPAGES(tp)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004743 if (tp != curtab)
4744 {
4745 for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
4746 if (wp->w_buffer == buf)
4747 break;
4748 if (wp != NULL)
4749 {
4750 goto_tabpage_win(tp, wp);
4751 if (curwin != wp)
4752 wp = NULL; /* something went wrong */
4753 break;
4754 }
4755 }
Bram Moolenaar482a2b52014-10-21 20:57:15 +02004756 return wp;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
Bram Moolenaar888ccac2016-06-04 18:49:36 +02004759static int last_win_id = LOWEST_WIN_ID - 1;
Bram Moolenaar86edef62016-03-13 18:07:30 +01004760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761/*
Bram Moolenaar746ebd32009-06-16 14:01:43 +00004762 * Allocate a window structure and link it in the window list when "hidden" is
4763 * FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004766win_alloc(win_T *after UNUSED, int hidden UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004768 win_T *new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770 /*
4771 * allocate window structure and linesizes arrays
4772 */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02004773 new_wp = ALLOC_CLEAR_ONE(win_T);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004774 if (new_wp == NULL)
4775 return NULL;
4776
4777 if (win_alloc_lines(new_wp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004779 vim_free(new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004780 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
4782
Bram Moolenaar86edef62016-03-13 18:07:30 +01004783 new_wp->w_id = ++last_win_id;
4784
Bram Moolenaar429fa852013-04-15 12:27:36 +02004785#ifdef FEAT_EVAL
4786 /* init w: variables */
4787 new_wp->w_vars = dict_alloc();
4788 if (new_wp->w_vars == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
Bram Moolenaar429fa852013-04-15 12:27:36 +02004790 win_free_lsize(new_wp);
4791 vim_free(new_wp);
4792 return NULL;
4793 }
4794 init_var_dict(new_wp->w_vars, &new_wp->w_winvar, VAR_SCOPE);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004795#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004796
Bram Moolenaar429fa852013-04-15 12:27:36 +02004797 /* Don't execute autocommands while the window is not properly
4798 * initialized yet. gui_create_scrollbar() may trigger a FocusGained
4799 * event. */
4800 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004801
Bram Moolenaar429fa852013-04-15 12:27:36 +02004802 /*
4803 * link the window in the window list
4804 */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004805 if (!hidden)
4806 win_append(after, new_wp);
Bram Moolenaar429fa852013-04-15 12:27:36 +02004807 new_wp->w_wincol = 0;
4808 new_wp->w_width = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaar429fa852013-04-15 12:27:36 +02004810 /* position the display and the cursor at the top of the file. */
4811 new_wp->w_topline = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812#ifdef FEAT_DIFF
Bram Moolenaar429fa852013-04-15 12:27:36 +02004813 new_wp->w_topfill = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004815 new_wp->w_botline = 2;
4816 new_wp->w_cursor.lnum = 1;
Bram Moolenaar429fa852013-04-15 12:27:36 +02004817 new_wp->w_scbind_pos = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
Bram Moolenaar375e3392019-01-31 18:26:10 +01004819 // use global option value for global-local options
4820 new_wp->w_p_so = -1;
4821 new_wp->w_p_siso = -1;
4822
Bram Moolenaar429fa852013-04-15 12:27:36 +02004823 /* We won't calculate w_fraction until resizing the window */
4824 new_wp->w_fraction = 0;
4825 new_wp->w_prev_fraction_row = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826
4827#ifdef FEAT_GUI
Bram Moolenaar429fa852013-04-15 12:27:36 +02004828 if (gui.in_use)
4829 {
4830 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_LEFT],
4831 SBAR_LEFT, new_wp);
4832 gui_create_scrollbar(&new_wp->w_scrollbars[SBAR_RIGHT],
4833 SBAR_RIGHT, new_wp);
4834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835#endif
4836#ifdef FEAT_FOLDING
Bram Moolenaar429fa852013-04-15 12:27:36 +02004837 foldInitWin(new_wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#endif
Bram Moolenaar429fa852013-04-15 12:27:36 +02004839 unblock_autocmds();
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004840#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar429fa852013-04-15 12:27:36 +02004841 new_wp->w_match_head = NULL;
4842 new_wp->w_next_match_id = 4;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004843#endif
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004844 return new_wp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845}
4846
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847/*
Bram Moolenaarff18df02013-07-24 17:51:57 +02004848 * Remove window 'wp' from the window list and free the structure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 */
4850 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01004851win_free(
4852 win_T *wp,
4853 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 int i;
Bram Moolenaarff18df02013-07-24 17:51:57 +02004856 buf_T *buf;
4857 wininfo_T *wip;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00004859#ifdef FEAT_FOLDING
4860 clearFolding(wp);
4861#endif
4862
4863 /* reduce the reference count to the argument list. */
4864 alist_unlink(wp->w_alist);
4865
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004866 /* Don't execute autocommands while the window is halfway being deleted.
4867 * gui_mch_destroy_scrollbar() may trigger a FocusGained event. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004868 block_autocmds();
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004869
Bram Moolenaar0ba04292010-07-14 23:23:17 +02004870#ifdef FEAT_LUA
4871 lua_window_free(wp);
4872#endif
4873
Bram Moolenaar325b7a22004-07-05 15:58:32 +00004874#ifdef FEAT_MZSCHEME
4875 mzscheme_window_free(wp);
4876#endif
4877
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#ifdef FEAT_PERL
4879 perl_win_free(wp);
4880#endif
4881
4882#ifdef FEAT_PYTHON
4883 python_window_free(wp);
4884#endif
4885
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02004886#ifdef FEAT_PYTHON3
4887 python3_window_free(wp);
4888#endif
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#ifdef FEAT_TCL
4891 tcl_window_free(wp);
4892#endif
4893
4894#ifdef FEAT_RUBY
4895 ruby_window_free(wp);
4896#endif
4897
4898 clear_winopt(&wp->w_onebuf_opt);
4899 clear_winopt(&wp->w_allbuf_opt);
4900
4901#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +02004902 vars_clear(&wp->w_vars->dv_hashtab); /* free all w: variables */
4903 hash_init(&wp->w_vars->dv_hashtab);
4904 unref_var_dict(wp->w_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905#endif
4906
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004907 {
4908 tabpage_T *ttp;
4909
4910 if (prevwin == wp)
4911 prevwin = NULL;
Bram Moolenaar29323592016-07-24 22:04:11 +02004912 FOR_ALL_TABPAGES(ttp)
Bram Moolenaar3dda7db2016-04-03 21:22:58 +02004913 if (ttp->tp_prevwin == wp)
4914 ttp->tp_prevwin = NULL;
4915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 win_free_lsize(wp);
4917
4918 for (i = 0; i < wp->w_tagstacklen; ++i)
Bram Moolenaar55008aa2019-09-01 20:21:56 +02004919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 vim_free(wp->w_tagstack[i].tagname);
Bram Moolenaar55008aa2019-09-01 20:21:56 +02004921 vim_free(wp->w_tagstack[i].user_data);
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 vim_free(wp->w_localdir);
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004924
Bram Moolenaarff18df02013-07-24 17:51:57 +02004925 /* Remove the window from the b_wininfo lists, it may happen that the
4926 * freed memory is re-used for another window. */
Bram Moolenaar29323592016-07-24 22:04:11 +02004927 FOR_ALL_BUFFERS(buf)
Bram Moolenaarff18df02013-07-24 17:51:57 +02004928 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
4929 if (wip->wi_win == wp)
4930 wip->wi_win = NULL;
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004933 clear_matches(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00004935
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#ifdef FEAT_JUMPLIST
4937 free_jumplist(wp);
4938#endif
4939
Bram Moolenaar28c258f2006-01-25 22:02:51 +00004940#ifdef FEAT_QUICKFIX
4941 qf_free_all(wp);
4942#endif
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#ifdef FEAT_GUI
4945 if (gui.in_use)
4946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_LEFT]);
4948 gui_mch_destroy_scrollbar(&wp->w_scrollbars[SBAR_RIGHT]);
4949 }
4950#endif /* FEAT_GUI */
4951
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004952#ifdef FEAT_MENU
4953 remove_winbar(wp);
4954#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004955#ifdef FEAT_PROP_POPUP
Bram Moolenaar9eaac892019-06-01 22:49:29 +02004956 free_callback(&wp->w_close_cb);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004957 free_callback(&wp->w_filter_cb);
Bram Moolenaar790498b2019-06-01 22:15:29 +02004958 for (i = 0; i < 4; ++i)
4959 VIM_CLEAR(wp->w_border_highlight[i]);
Bram Moolenaar4cd583c2019-06-26 05:13:57 +02004960 vim_free(wp->w_scrollbar_highlight);
4961 vim_free(wp->w_thumb_highlight);
Bram Moolenaareb2310d2019-06-16 20:09:10 +02004962 vim_free(wp->w_popup_title);
Bram Moolenaarc662ec92019-06-23 00:15:57 +02004963 list_unref(wp->w_popup_mask);
Bram Moolenaar0aca2932019-07-26 22:22:38 +02004964 vim_free(wp->w_popup_mask_cells);
Bram Moolenaarbf0eff02019-06-01 17:13:36 +02004965#endif
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02004966
Bram Moolenaar860cae12010-06-05 23:22:07 +02004967#ifdef FEAT_SYN_HL
Bram Moolenaar1a384422010-07-14 19:53:30 +02004968 vim_free(wp->w_p_cc_cols);
Bram Moolenaar860cae12010-06-05 23:22:07 +02004969#endif
4970
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004971 if (win_valid_any_tab(wp))
Bram Moolenaarfd29f462010-06-06 16:11:09 +02004972 win_remove(wp, tp);
Bram Moolenaar3be85852014-06-12 14:01:31 +02004973 if (autocmd_busy)
4974 {
4975 wp->w_next = au_pending_free_win;
4976 au_pending_free_win = wp;
4977 }
4978 else
4979 vim_free(wp);
Bram Moolenaaree79cbc2007-05-02 19:50:14 +00004980
Bram Moolenaar78ab3312007-09-29 12:16:41 +00004981 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982}
4983
4984/*
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004985 * Return TRUE if "wp" is not in the list of windows: the autocmd window or a
4986 * popup window.
4987 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02004988 static int
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004989win_unlisted(win_T *wp)
4990{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02004991 return wp == aucmd_win || WIN_IS_POPUP(wp);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004992}
4993
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004994#if defined(FEAT_PROP_POPUP) || defined(PROTO)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004995/*
4996 * Free a popup window. This does not take the window out of the window list
4997 * and assumes there is only one toplevel frame, no split.
4998 */
4999 void
5000win_free_popup(win_T *win)
5001{
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02005002 if (bt_popup(win->w_buffer))
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02005003 win_close_buffer(win, DOBUF_WIPE_REUSE, FALSE);
Bram Moolenaar5b8cfed2019-06-30 22:16:10 +02005004 else
5005 close_buffer(win, win->w_buffer, 0, FALSE);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02005006# if defined(FEAT_TIMERS)
Bram Moolenaar51fe3b12019-05-26 20:10:06 +02005007 if (win->w_popup_timer != NULL)
5008 stop_timer(win->w_popup_timer);
Bram Moolenaar35d5af62019-05-26 20:44:10 +02005009# endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005010 vim_free(win->w_frame);
5011 win_free(win, NULL);
5012}
Bram Moolenaar35d5af62019-05-26 20:44:10 +02005013#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005014
5015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 * Append window "wp" in the window list after window "after".
5017 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005018 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005019win_append(win_T *after, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 win_T *before;
5022
5023 if (after == NULL) /* after NULL is in front of the first */
5024 before = firstwin;
5025 else
5026 before = after->w_next;
5027
5028 wp->w_next = before;
5029 wp->w_prev = after;
5030 if (after == NULL)
5031 firstwin = wp;
5032 else
5033 after->w_next = wp;
5034 if (before == NULL)
5035 lastwin = wp;
5036 else
5037 before->w_prev = wp;
5038}
5039
5040/*
5041 * Remove a window from the window list.
5042 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00005043 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005044win_remove(
5045 win_T *wp,
5046 tabpage_T *tp) /* tab page "win" is in, NULL for current */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
5048 if (wp->w_prev != NULL)
5049 wp->w_prev->w_next = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005050 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02005051 firstwin = curtab->tp_firstwin = wp->w_next;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005052 else
5053 tp->tp_firstwin = wp->w_next;
Bram Moolenaar816968d2017-09-29 21:29:18 +02005054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (wp->w_next != NULL)
5056 wp->w_next->w_prev = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005057 else if (tp == NULL)
Bram Moolenaar816968d2017-09-29 21:29:18 +02005058 lastwin = curtab->tp_lastwin = wp->w_prev;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005059 else
5060 tp->tp_lastwin = wp->w_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061}
5062
5063/*
5064 * Append frame "frp" in a frame list after frame "after".
5065 */
5066 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005067frame_append(frame_T *after, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
5069 frp->fr_next = after->fr_next;
5070 after->fr_next = frp;
5071 if (frp->fr_next != NULL)
5072 frp->fr_next->fr_prev = frp;
5073 frp->fr_prev = after;
5074}
5075
5076/*
5077 * Insert frame "frp" in a frame list before frame "before".
5078 */
5079 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005080frame_insert(frame_T *before, frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
5082 frp->fr_next = before;
5083 frp->fr_prev = before->fr_prev;
5084 before->fr_prev = frp;
5085 if (frp->fr_prev != NULL)
5086 frp->fr_prev->fr_next = frp;
5087 else
5088 frp->fr_parent->fr_child = frp;
5089}
5090
5091/*
5092 * Remove a frame from a frame list.
5093 */
5094 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005095frame_remove(frame_T *frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096{
5097 if (frp->fr_prev != NULL)
5098 frp->fr_prev->fr_next = frp->fr_next;
5099 else
Bram Moolenaar6f361c92018-01-31 19:06:50 +01005100 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 frp->fr_parent->fr_child = frp->fr_next;
Bram Moolenaar6f361c92018-01-31 19:06:50 +01005102 /* special case: topframe->fr_child == frp */
5103 if (topframe->fr_child == frp)
5104 topframe->fr_child = frp->fr_next;
5105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 if (frp->fr_next != NULL)
5107 frp->fr_next->fr_prev = frp->fr_prev;
5108}
5109
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110/*
5111 * Allocate w_lines[] for window "wp".
5112 * Return FAIL for failure, OK for success.
5113 */
5114 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005115win_alloc_lines(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
5117 wp->w_lines_valid = 0;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005118 wp->w_lines = ALLOC_CLEAR_MULT(wline_T, Rows );
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 if (wp->w_lines == NULL)
5120 return FAIL;
5121 return OK;
5122}
5123
5124/*
5125 * free lsize arrays for a window
5126 */
5127 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005128win_free_lsize(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129{
Bram Moolenaar06e4a6d2014-06-12 11:49:46 +02005130 /* TODO: why would wp be NULL here? */
5131 if (wp != NULL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01005132 VIM_CLEAR(wp->w_lines);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133}
5134
5135/*
5136 * Called from win_new_shellsize() after Rows changed.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005137 * This only does the current tab page, others must be done when made active.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 */
5139 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005140shell_new_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005142 int h = (int)ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 if (firstwin == NULL) /* not initialized yet */
5145 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 if (h < frame_minheight(topframe, NULL))
5147 h = frame_minheight(topframe, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005148
5149 /* First try setting the heights of windows with 'winfixheight'. If
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 * that doesn't result in the right height, forget about that option. */
5151 frame_new_height(topframe, h, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005152 if (!frame_check_height(topframe, h))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 frame_new_height(topframe, h, FALSE, FALSE);
5154
5155 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 compute_cmdrow();
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005157 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005158
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159#if 0
5160 /* Disabled: don't want making the screen smaller make a window larger. */
5161 if (p_ea)
5162 win_equal(curwin, FALSE, 'v');
5163#endif
5164}
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166/*
5167 * Called from win_new_shellsize() after Columns changed.
5168 */
5169 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005170shell_new_columns(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 if (firstwin == NULL) /* not initialized yet */
5173 return;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005174
5175 /* First try setting the widths of windows with 'winfixwidth'. If that
5176 * doesn't result in the right width, forget about that option. */
5177 frame_new_width(topframe, (int)Columns, FALSE, TRUE);
Bram Moolenaarb893ac22013-06-26 14:04:47 +02005178 if (!frame_check_width(topframe, Columns))
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005179 frame_new_width(topframe, (int)Columns, FALSE, FALSE);
5180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 (void)win_comp_pos(); /* recompute w_winrow and w_wincol */
5182#if 0
5183 /* Disabled: don't want making the screen smaller make a window larger. */
5184 if (p_ea)
5185 win_equal(curwin, FALSE, 'h');
5186#endif
5187}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189#if defined(FEAT_CMDWIN) || defined(PROTO)
5190/*
5191 * Save the size of all windows in "gap".
5192 */
5193 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005194win_size_save(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196{
5197 win_T *wp;
5198
5199 ga_init2(gap, (int)sizeof(int), 1);
Bram Moolenaar1c329c02019-10-27 20:37:35 +01005200 if (ga_grow(gap, win_count() * 2 + 1) == OK)
5201 {
5202 // first entry is value of 'lines'
5203 ((int *)gap->ga_data)[gap->ga_len++] = Rows;
5204
Bram Moolenaar29323592016-07-24 22:04:11 +02005205 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
5207 ((int *)gap->ga_data)[gap->ga_len++] =
5208 wp->w_width + wp->w_vsep_width;
5209 ((int *)gap->ga_data)[gap->ga_len++] = wp->w_height;
5210 }
Bram Moolenaar1c329c02019-10-27 20:37:35 +01005211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212}
5213
5214/*
Bram Moolenaar1c329c02019-10-27 20:37:35 +01005215 * Restore window sizes, but only if the number of windows is still the same
5216 * and 'lines' didn't change.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 * Does not free the growarray.
5218 */
5219 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005220win_size_restore(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221{
5222 win_T *wp;
Bram Moolenaarb643e772014-07-16 15:18:26 +02005223 int i, j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
Bram Moolenaar1c329c02019-10-27 20:37:35 +01005225 if (win_count() * 2 + 1 == gap->ga_len
5226 && ((int *)gap->ga_data)[0] == Rows)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
Bram Moolenaarb643e772014-07-16 15:18:26 +02005228 /* The order matters, because frames contain other frames, but it's
5229 * difficult to get right. The easy way out is to do it twice. */
5230 for (j = 0; j < 2; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 {
Bram Moolenaar1c329c02019-10-27 20:37:35 +01005232 i = 1;
Bram Moolenaar29323592016-07-24 22:04:11 +02005233 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb643e772014-07-16 15:18:26 +02005234 {
5235 frame_setwidth(wp->w_frame, ((int *)gap->ga_data)[i++]);
5236 win_setheight_win(((int *)gap->ga_data)[i++], wp);
5237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
5239 /* recompute the window positions */
5240 (void)win_comp_pos();
5241 }
5242}
5243#endif /* FEAT_CMDWIN */
5244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245/*
5246 * Update the position for all windows, using the width and height of the
5247 * frames.
5248 * Returns the row just after the last window.
5249 */
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00005250 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005251win_comp_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005253 int row = tabline_height();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 int col = 0;
5255
5256 frame_comp_pos(topframe, &row, &col);
5257 return row;
5258}
5259
5260/*
5261 * Update the position of the windows in frame "topfrp", using the width and
5262 * height of the frames.
5263 * "*row" and "*col" are the top-left position of the frame. They are updated
5264 * to the bottom-right position plus one.
5265 */
5266 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005267frame_comp_pos(frame_T *topfrp, int *row, int *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 win_T *wp;
5270 frame_T *frp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 int startcol;
5272 int startrow;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005273 int h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 wp = topfrp->fr_win;
5276 if (wp != NULL)
5277 {
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005278 if (wp->w_winrow != *row || wp->w_wincol != *col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 /* position changed, redraw */
5281 wp->w_winrow = *row;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 wp->w_wincol = *col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 redraw_win_later(wp, NOT_VALID);
5284 wp->w_redr_status = TRUE;
5285 }
Bram Moolenaar415a6932017-12-05 20:31:07 +01005286 /* WinBar will not show if the window height is zero */
5287 h = VISIBLE_HEIGHT(wp) + wp->w_status_height;
5288 *row += h > topfrp->fr_height ? topfrp->fr_height : h;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 *col += wp->w_width + wp->w_vsep_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 }
5291 else
5292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 startrow = *row;
5294 startcol = *col;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005295 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 if (topfrp->fr_layout == FR_ROW)
5298 *row = startrow; /* all frames are at the same row */
5299 else
5300 *col = startcol; /* all frames are at the same col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 frame_comp_pos(frp, row, col);
5302 }
5303 }
5304}
5305
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306/*
5307 * Set current window height and take care of repositioning other windows to
5308 * fit around it.
5309 */
5310 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005311win_setheight(int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312{
5313 win_setheight_win(height, curwin);
5314}
5315
5316/*
5317 * Set the window height of window "win" and take care of repositioning other
5318 * windows to fit around it.
5319 */
5320 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005321win_setheight_win(int height, win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322{
5323 int row;
5324
5325 if (win == curwin)
5326 {
5327 /* Always keep current window at least one line high, even when
5328 * 'winminheight' is zero. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (height < p_wmh)
5330 height = p_wmh;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (height == 0)
5332 height = 1;
Bram Moolenaar415a6932017-12-05 20:31:07 +01005333 height += WINBAR_HEIGHT(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 frame_setheight(win->w_frame, height + win->w_status_height);
5337
5338 /* recompute the window positions */
5339 row = win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 /*
5342 * If there is extra space created between the last window and the command
5343 * line, clear it.
5344 */
5345 if (full_screen && msg_scrolled == 0 && row < cmdline_row)
5346 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5347 cmdline_row = row;
5348 msg_row = row;
5349 msg_col = 0;
5350
5351 redraw_all_later(NOT_VALID);
5352}
5353
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354/*
5355 * Set the height of a frame to "height" and take care that all frames and
5356 * windows inside it are resized. Also resize frames on the left and right if
5357 * the are in the same FR_ROW frame.
5358 *
5359 * Strategy:
5360 * If the frame is part of a FR_COL frame, try fitting the frame in that
5361 * frame. If that doesn't work (the FR_COL frame is too small), recursively
5362 * go to containing frames to resize them and make room.
5363 * If the frame is part of a FR_ROW frame, all frames must be resized as well.
5364 * Check for the minimal height of the FR_ROW frame.
5365 * At the top level we can also use change the command line height.
5366 */
5367 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005368frame_setheight(frame_T *curfrp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 int room; /* total number of lines available */
5371 int take; /* number of lines taken from other windows */
5372 int room_cmdline; /* lines available from cmdline */
5373 int run;
5374 frame_T *frp;
5375 int h;
5376 int room_reserved;
5377
5378 /* If the height already is the desired value, nothing to do. */
5379 if (curfrp->fr_height == height)
5380 return;
5381
5382 if (curfrp->fr_parent == NULL)
5383 {
5384 /* topframe: can only change the command line */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005385 if (height > ROWS_AVAIL)
5386 height = ROWS_AVAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 if (height > 0)
5388 frame_new_height(curfrp, height, FALSE, FALSE);
5389 }
5390 else if (curfrp->fr_parent->fr_layout == FR_ROW)
5391 {
5392 /* Row of frames: Also need to resize frames left and right of this
5393 * one. First check for the minimal height of these. */
5394 h = frame_minheight(curfrp->fr_parent, NULL);
5395 if (height < h)
5396 height = h;
5397 frame_setheight(curfrp->fr_parent, height);
5398 }
5399 else
5400 {
5401 /*
5402 * Column of frames: try to change only frames in this column.
5403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 /*
5405 * Do this twice:
5406 * 1: compute room available, if it's not enough try resizing the
5407 * containing frame.
5408 * 2: compute the room available and adjust the height to it.
5409 * Try not to reduce the height of a window with 'winfixheight' set.
5410 */
5411 for (run = 1; run <= 2; ++run)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 {
5413 room = 0;
5414 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005415 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 if (frp != curfrp
5418 && frp->fr_win != NULL
5419 && frp->fr_win->w_p_wfh)
5420 room_reserved += frp->fr_height;
5421 room += frp->fr_height;
5422 if (frp != curfrp)
5423 room -= frame_minheight(frp, NULL);
5424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 if (curfrp->fr_width != Columns)
5426 room_cmdline = 0;
5427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
5429 room_cmdline = Rows - p_ch - (lastwin->w_winrow
Bram Moolenaar415a6932017-12-05 20:31:07 +01005430 + VISIBLE_HEIGHT(lastwin)
5431 + lastwin->w_status_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 if (room_cmdline < 0)
5433 room_cmdline = 0;
5434 }
5435
5436 if (height <= room + room_cmdline)
5437 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 if (run == 2 || curfrp->fr_width == Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
5440 if (height > room + room_cmdline)
5441 height = room + room_cmdline;
5442 break;
5443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 frame_setheight(curfrp->fr_parent, height
5445 + frame_minheight(curfrp->fr_parent, NOWIN) - (int)p_wmh - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 }
5447
5448 /*
5449 * Compute the number of lines we will take from others frames (can be
5450 * negative!).
5451 */
5452 take = height - curfrp->fr_height;
5453
5454 /* If there is not enough room, also reduce the height of a window
5455 * with 'winfixheight' set. */
5456 if (height > room + room_cmdline - room_reserved)
5457 room_reserved = room + room_cmdline - height;
5458 /* If there is only a 'winfixheight' window and making the
5459 * window smaller, need to make the other window taller. */
5460 if (take < 0 && room - curfrp->fr_height < room_reserved)
5461 room_reserved = 0;
5462
5463 if (take > 0 && room_cmdline > 0)
5464 {
5465 /* use lines from cmdline first */
5466 if (take < room_cmdline)
5467 room_cmdline = take;
5468 take -= room_cmdline;
5469 topframe->fr_height += room_cmdline;
5470 }
5471
5472 /*
5473 * set the current frame to the new height
5474 */
5475 frame_new_height(curfrp, height, FALSE, FALSE);
5476
5477 /*
5478 * First take lines from the frames after the current frame. If
5479 * that is not enough, takes lines from frames above the current
5480 * frame.
5481 */
5482 for (run = 0; run < 2; ++run)
5483 {
5484 if (run == 0)
5485 frp = curfrp->fr_next; /* 1st run: start with next window */
5486 else
5487 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5488 while (frp != NULL && take != 0)
5489 {
5490 h = frame_minheight(frp, NULL);
5491 if (room_reserved > 0
5492 && frp->fr_win != NULL
5493 && frp->fr_win->w_p_wfh)
5494 {
5495 if (room_reserved >= frp->fr_height)
5496 room_reserved -= frp->fr_height;
5497 else
5498 {
5499 if (frp->fr_height - room_reserved > take)
5500 room_reserved = frp->fr_height - take;
5501 take -= frp->fr_height - room_reserved;
5502 frame_new_height(frp, room_reserved, FALSE, FALSE);
5503 room_reserved = 0;
5504 }
5505 }
5506 else
5507 {
5508 if (frp->fr_height - take < h)
5509 {
5510 take -= frp->fr_height - h;
5511 frame_new_height(frp, h, FALSE, FALSE);
5512 }
5513 else
5514 {
5515 frame_new_height(frp, frp->fr_height - take,
5516 FALSE, FALSE);
5517 take = 0;
5518 }
5519 }
5520 if (run == 0)
5521 frp = frp->fr_next;
5522 else
5523 frp = frp->fr_prev;
5524 }
5525 }
5526 }
5527}
5528
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529/*
5530 * Set current window width and take care of repositioning other windows to
5531 * fit around it.
5532 */
5533 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005534win_setwidth(int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536 win_setwidth_win(width, curwin);
5537}
5538
5539 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005540win_setwidth_win(int width, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542 /* Always keep current window at least one column wide, even when
5543 * 'winminwidth' is zero. */
5544 if (wp == curwin)
5545 {
5546 if (width < p_wmw)
5547 width = p_wmw;
5548 if (width == 0)
5549 width = 1;
5550 }
5551
5552 frame_setwidth(wp->w_frame, width + wp->w_vsep_width);
5553
5554 /* recompute the window positions */
5555 (void)win_comp_pos();
5556
5557 redraw_all_later(NOT_VALID);
5558}
5559
5560/*
5561 * Set the width of a frame to "width" and take care that all frames and
5562 * windows inside it are resized. Also resize frames above and below if the
5563 * are in the same FR_ROW frame.
5564 *
5565 * Strategy is similar to frame_setheight().
5566 */
5567 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005568frame_setwidth(frame_T *curfrp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 int room; /* total number of lines available */
5571 int take; /* number of lines taken from other windows */
5572 int run;
5573 frame_T *frp;
5574 int w;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005575 int room_reserved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576
5577 /* If the width already is the desired value, nothing to do. */
5578 if (curfrp->fr_width == width)
5579 return;
5580
5581 if (curfrp->fr_parent == NULL)
5582 /* topframe: can't change width */
5583 return;
5584
5585 if (curfrp->fr_parent->fr_layout == FR_COL)
5586 {
5587 /* Column of frames: Also need to resize frames above and below of
5588 * this one. First check for the minimal width of these. */
5589 w = frame_minwidth(curfrp->fr_parent, NULL);
5590 if (width < w)
5591 width = w;
5592 frame_setwidth(curfrp->fr_parent, width);
5593 }
5594 else
5595 {
5596 /*
5597 * Row of frames: try to change only frames in this row.
5598 *
5599 * Do this twice:
5600 * 1: compute room available, if it's not enough try resizing the
5601 * containing frame.
5602 * 2: compute the room available and adjust the width to it.
5603 */
5604 for (run = 1; run <= 2; ++run)
5605 {
5606 room = 0;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005607 room_reserved = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005608 FOR_ALL_FRAMES(frp, curfrp->fr_parent->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005610 if (frp != curfrp
5611 && frp->fr_win != NULL
5612 && frp->fr_win->w_p_wfw)
5613 room_reserved += frp->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 room += frp->fr_width;
5615 if (frp != curfrp)
5616 room -= frame_minwidth(frp, NULL);
5617 }
5618
5619 if (width <= room)
5620 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005621 if (run == 2 || curfrp->fr_height >= ROWS_AVAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 {
5623 if (width > room)
5624 width = room;
5625 break;
5626 }
5627 frame_setwidth(curfrp->fr_parent, width
5628 + frame_minwidth(curfrp->fr_parent, NOWIN) - (int)p_wmw - 1);
5629 }
5630
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 /*
5632 * Compute the number of lines we will take from others frames (can be
5633 * negative!).
5634 */
5635 take = width - curfrp->fr_width;
5636
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005637 /* If there is not enough room, also reduce the width of a window
5638 * with 'winfixwidth' set. */
5639 if (width > room - room_reserved)
5640 room_reserved = room - width;
5641 /* If there is only a 'winfixwidth' window and making the
5642 * window smaller, need to make the other window narrower. */
5643 if (take < 0 && room - curfrp->fr_width < room_reserved)
5644 room_reserved = 0;
5645
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 /*
5647 * set the current frame to the new width
5648 */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005649 frame_new_width(curfrp, width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 /*
5652 * First take lines from the frames right of the current frame. If
5653 * that is not enough, takes lines from frames left of the current
5654 * frame.
5655 */
5656 for (run = 0; run < 2; ++run)
5657 {
5658 if (run == 0)
5659 frp = curfrp->fr_next; /* 1st run: start with next window */
5660 else
5661 frp = curfrp->fr_prev; /* 2nd run: start with prev window */
5662 while (frp != NULL && take != 0)
5663 {
5664 w = frame_minwidth(frp, NULL);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005665 if (room_reserved > 0
5666 && frp->fr_win != NULL
5667 && frp->fr_win->w_p_wfw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005669 if (room_reserved >= frp->fr_width)
5670 room_reserved -= frp->fr_width;
5671 else
5672 {
5673 if (frp->fr_width - room_reserved > take)
5674 room_reserved = frp->fr_width - take;
5675 take -= frp->fr_width - room_reserved;
5676 frame_new_width(frp, room_reserved, FALSE, FALSE);
5677 room_reserved = 0;
5678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 }
5680 else
5681 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005682 if (frp->fr_width - take < w)
5683 {
5684 take -= frp->fr_width - w;
5685 frame_new_width(frp, w, FALSE, FALSE);
5686 }
5687 else
5688 {
5689 frame_new_width(frp, frp->fr_width - take,
5690 FALSE, FALSE);
5691 take = 0;
5692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694 if (run == 0)
5695 frp = frp->fr_next;
5696 else
5697 frp = frp->fr_prev;
5698 }
5699 }
5700 }
5701}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703/*
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005704 * Check 'winminheight' for a valid value and reduce it if needed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 */
5706 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005707win_setminheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 int room;
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005710 int needed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005713 // loop until there is a 'winminheight' that is possible
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 while (p_wmh > 0)
5715 {
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005716 room = Rows - p_ch;
5717 needed = frame_minheight(topframe, NULL);
5718 if (room >= needed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720 --p_wmh;
5721 if (first)
5722 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005723 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 first = FALSE;
5725 }
5726 }
5727}
5728
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005729/*
5730 * Check 'winminwidth' for a valid value and reduce it if needed.
5731 */
5732 void
5733win_setminwidth(void)
5734{
5735 int room;
5736 int needed;
5737 int first = TRUE;
5738
5739 // loop until there is a 'winminheight' that is possible
5740 while (p_wmw > 0)
5741 {
5742 room = Columns;
5743 needed = frame_minwidth(topframe, NULL);
5744 if (room >= needed)
5745 break;
5746 --p_wmw;
5747 if (first)
5748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005749 emsg(_(e_noroom));
Bram Moolenaar1c3c1042018-06-12 16:49:30 +02005750 first = FALSE;
5751 }
5752 }
5753}
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755/*
5756 * Status line of dragwin is dragged "offset" lines down (negative is up).
5757 */
5758 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005759win_drag_status_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760{
5761 frame_T *curfr;
5762 frame_T *fr;
5763 int room;
5764 int row;
5765 int up; /* if TRUE, drag status line up, otherwise down */
5766 int n;
5767
5768 fr = dragwin->w_frame;
5769 curfr = fr;
5770 if (fr != topframe) /* more than one window */
5771 {
5772 fr = fr->fr_parent;
5773 /* When the parent frame is not a column of frames, its parent should
5774 * be. */
5775 if (fr->fr_layout != FR_COL)
5776 {
5777 curfr = fr;
5778 if (fr != topframe) /* only a row of windows, may drag statusline */
5779 fr = fr->fr_parent;
5780 }
5781 }
5782
5783 /* If this is the last frame in a column, may want to resize the parent
5784 * frame instead (go two up to skip a row of frames). */
5785 while (curfr != topframe && curfr->fr_next == NULL)
5786 {
5787 if (fr != topframe)
5788 fr = fr->fr_parent;
5789 curfr = fr;
5790 if (fr != topframe)
5791 fr = fr->fr_parent;
5792 }
5793
5794 if (offset < 0) /* drag up */
5795 {
5796 up = TRUE;
5797 offset = -offset;
5798 /* sum up the room of the current frame and above it */
5799 if (fr == curfr)
5800 {
5801 /* only one window */
5802 room = fr->fr_height - frame_minheight(fr, NULL);
5803 }
5804 else
5805 {
5806 room = 0;
5807 for (fr = fr->fr_child; ; fr = fr->fr_next)
5808 {
5809 room += fr->fr_height - frame_minheight(fr, NULL);
5810 if (fr == curfr)
5811 break;
5812 }
5813 }
5814 fr = curfr->fr_next; /* put fr at frame that grows */
5815 }
5816 else /* drag down */
5817 {
5818 up = FALSE;
5819 /*
5820 * Only dragging the last status line can reduce p_ch.
5821 */
5822 room = Rows - cmdline_row;
5823 if (curfr->fr_next == NULL)
5824 room -= 1;
5825 else
5826 room -= p_ch;
5827 if (room < 0)
5828 room = 0;
5829 /* sum up the room of frames below of the current one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005830 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 room += fr->fr_height - frame_minheight(fr, NULL);
5832 fr = curfr; /* put fr at window that grows */
5833 }
5834
5835 if (room < offset) /* Not enough room */
5836 offset = room; /* Move as far as we can */
5837 if (offset <= 0)
5838 return;
5839
5840 /*
5841 * Grow frame fr by "offset" lines.
5842 * Doesn't happen when dragging the last status line up.
5843 */
5844 if (fr != NULL)
5845 frame_new_height(fr, fr->fr_height + offset, up, FALSE);
5846
5847 if (up)
5848 fr = curfr; /* current frame gets smaller */
5849 else
5850 fr = curfr->fr_next; /* next frame gets smaller */
5851
5852 /*
5853 * Now make the other frames smaller.
5854 */
5855 while (fr != NULL && offset > 0)
5856 {
5857 n = frame_minheight(fr, NULL);
5858 if (fr->fr_height - offset <= n)
5859 {
5860 offset -= fr->fr_height - n;
5861 frame_new_height(fr, n, !up, FALSE);
5862 }
5863 else
5864 {
5865 frame_new_height(fr, fr->fr_height - offset, !up, FALSE);
5866 break;
5867 }
5868 if (up)
5869 fr = fr->fr_prev;
5870 else
5871 fr = fr->fr_next;
5872 }
5873 row = win_comp_pos();
5874 screen_fill(row, cmdline_row, 0, (int)Columns, ' ', ' ', 0);
5875 cmdline_row = row;
5876 p_ch = Rows - cmdline_row;
5877 if (p_ch < 1)
5878 p_ch = 1;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00005879 curtab->tp_ch_used = p_ch;
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00005880 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 showmode();
5882}
5883
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884/*
5885 * Separator line of dragwin is dragged "offset" lines right (negative is left).
5886 */
5887 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005888win_drag_vsep_line(win_T *dragwin, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890 frame_T *curfr;
5891 frame_T *fr;
5892 int room;
5893 int left; /* if TRUE, drag separator line left, otherwise right */
5894 int n;
5895
5896 fr = dragwin->w_frame;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005897 if (fr == topframe) /* only one window (cannot happen?) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 return;
5899 curfr = fr;
5900 fr = fr->fr_parent;
5901 /* When the parent frame is not a row of frames, its parent should be. */
5902 if (fr->fr_layout != FR_ROW)
5903 {
5904 if (fr == topframe) /* only a column of windows (cannot happen?) */
5905 return;
5906 curfr = fr;
5907 fr = fr->fr_parent;
5908 }
5909
5910 /* If this is the last frame in a row, may want to resize a parent
5911 * frame instead. */
5912 while (curfr->fr_next == NULL)
5913 {
5914 if (fr == topframe)
5915 break;
5916 curfr = fr;
5917 fr = fr->fr_parent;
5918 if (fr != topframe)
5919 {
5920 curfr = fr;
5921 fr = fr->fr_parent;
5922 }
5923 }
5924
5925 if (offset < 0) /* drag left */
5926 {
5927 left = TRUE;
5928 offset = -offset;
5929 /* sum up the room of the current frame and left of it */
5930 room = 0;
5931 for (fr = fr->fr_child; ; fr = fr->fr_next)
5932 {
5933 room += fr->fr_width - frame_minwidth(fr, NULL);
5934 if (fr == curfr)
5935 break;
5936 }
5937 fr = curfr->fr_next; /* put fr at frame that grows */
5938 }
5939 else /* drag right */
5940 {
5941 left = FALSE;
5942 /* sum up the room of frames right of the current one */
5943 room = 0;
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01005944 FOR_ALL_FRAMES(fr, curfr->fr_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 room += fr->fr_width - frame_minwidth(fr, NULL);
5946 fr = curfr; /* put fr at window that grows */
5947 }
5948
5949 if (room < offset) /* Not enough room */
5950 offset = room; /* Move as far as we can */
5951 if (offset <= 0) /* No room at all, quit. */
5952 return;
Bram Moolenaar294a7e52015-11-22 19:39:38 +01005953 if (fr == NULL)
5954 return; /* Safety check, should not happen. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955
5956 /* grow frame fr by offset lines */
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005957 frame_new_width(fr, fr->fr_width + offset, left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958
5959 /* shrink other frames: current and at the left or at the right */
5960 if (left)
5961 fr = curfr; /* current frame gets smaller */
5962 else
5963 fr = curfr->fr_next; /* next frame gets smaller */
5964
5965 while (fr != NULL && offset > 0)
5966 {
5967 n = frame_minwidth(fr, NULL);
5968 if (fr->fr_width - offset <= n)
5969 {
5970 offset -= fr->fr_width - n;
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005971 frame_new_width(fr, n, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 }
5973 else
5974 {
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00005975 frame_new_width(fr, fr->fr_width - offset, !left, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 break;
5977 }
5978 if (left)
5979 fr = fr->fr_prev;
5980 else
5981 fr = fr->fr_next;
5982 }
5983 (void)win_comp_pos();
5984 redraw_all_later(NOT_VALID);
5985}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005987#define FRACTION_MULT 16384L
5988
5989/*
5990 * Set wp->w_fraction for the current w_wrow and w_height.
Bram Moolenaar3679c172017-11-22 22:22:11 +01005991 * Has no effect when the window is less than two lines.
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005992 */
Bram Moolenaar9dc2ce32015-12-05 19:47:04 +01005993 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01005994set_fraction(win_T *wp)
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01005995{
Bram Moolenaar3679c172017-11-22 22:22:11 +01005996 if (wp->w_height > 1)
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01005997 // When cursor is in the first line the percentage is computed as if
5998 // it's halfway that line. Thus with two lines it is 25%, with three
5999 // lines 17%, etc. Similarly for the last line: 75%, 83%, etc.
Bram Moolenaar3679c172017-11-22 22:22:11 +01006000 wp->w_fraction = ((long)wp->w_wrow * FRACTION_MULT
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006001 + FRACTION_MULT / 2) / (long)wp->w_height;
Bram Moolenaar0215e8e2010-12-17 17:35:10 +01006002}
6003
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004/*
6005 * Set the height of a window.
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006006 * "height" excludes any window toolbar.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 * This takes care of the things inside the window, not what happens to the
6008 * window position, the frame or to other windows.
6009 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02006010 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006011win_new_height(win_T *wp, int height)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012{
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006013 int prev_height = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014
6015 /* Don't want a negative height. Happens when splitting a tiny window.
6016 * Will equalize heights soon to fix it. */
6017 if (height < 0)
6018 height = 0;
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006019 if (wp->w_height == height)
6020 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006022 if (wp->w_height > 0)
6023 {
6024 if (wp == curwin)
Bram Moolenaar0ae36a52014-06-13 20:08:45 +02006025 /* w_wrow needs to be valid. When setting 'laststatus' this may
6026 * call win_new_height() recursively. */
6027 validate_cursor();
6028 if (wp->w_height != prev_height)
6029 return; /* Recursive call already changed the size, bail out here
6030 to avoid the following to mess things up. */
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006031 if (wp->w_wrow != wp->w_prev_fraction_row)
6032 set_fraction(wp);
6033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034
6035 wp->w_height = height;
6036 wp->w_skipcol = 0;
6037
Bram Moolenaar955f1982017-02-05 15:10:51 +01006038 /* There is no point in adjusting the scroll position when exiting. Some
6039 * values might be invalid. */
6040 if (!exiting)
6041 scroll_to_fraction(wp, prev_height);
Bram Moolenaar46328f92016-08-28 15:39:57 +02006042}
6043
6044 void
6045scroll_to_fraction(win_T *wp, int prev_height)
6046{
6047 linenr_T lnum;
6048 int sline, line_size;
6049 int height = wp->w_height;
6050
Bram Moolenaara9b25352019-05-12 14:25:30 +02006051 // Don't change w_topline in any of these cases:
6052 // - window height is 0
6053 // - 'scrollbind' is set and this isn't the current window
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02006054 // - window height is sufficient to display the whole buffer and first line
6055 // is visible.
Bram Moolenaara9b25352019-05-12 14:25:30 +02006056 if (height > 0
6057 && (!wp->w_p_scb || wp == curwin)
Bram Moolenaarbd2d68c2019-05-18 15:36:11 +02006058 && (height < wp->w_buffer->b_ml.ml_line_count || wp->w_topline > 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 {
Bram Moolenaar34114692005-01-02 11:28:13 +00006060 /*
6061 * Find a value for w_topline that shows the cursor at the same
6062 * relative position in the window as before (more or less).
6063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 lnum = wp->w_cursor.lnum;
6065 if (lnum < 1) /* can happen when starting up */
6066 lnum = 1;
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006067 wp->w_wrow = ((long)wp->w_fraction * (long)height - 1L)
6068 / FRACTION_MULT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 line_size = plines_win_col(wp, lnum, (long)(wp->w_cursor.col)) - 1;
6070 sline = wp->w_wrow - line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00006071
6072 if (sline >= 0)
6073 {
6074 /* Make sure the whole cursor line is visible, if possible. */
6075 int rows = plines_win(wp, lnum, FALSE);
6076
6077 if (sline > wp->w_height - rows)
6078 {
6079 sline = wp->w_height - rows;
6080 wp->w_wrow -= rows - line_size;
6081 }
6082 }
6083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (sline < 0)
6085 {
6086 /*
6087 * Cursor line would go off top of screen if w_wrow was this high.
Bram Moolenaar26470632006-10-24 19:12:40 +00006088 * Make cursor line the first line in the window. If not enough
6089 * room use w_skipcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 */
6091 wp->w_wrow = line_size;
Bram Moolenaar26470632006-10-24 19:12:40 +00006092 if (wp->w_wrow >= wp->w_height
Bram Moolenaar02631462017-09-22 15:20:32 +02006093 && (wp->w_width - win_col_off(wp)) > 0)
Bram Moolenaar26470632006-10-24 19:12:40 +00006094 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006095 wp->w_skipcol += wp->w_width - win_col_off(wp);
Bram Moolenaar26470632006-10-24 19:12:40 +00006096 --wp->w_wrow;
6097 while (wp->w_wrow >= wp->w_height)
6098 {
Bram Moolenaar02631462017-09-22 15:20:32 +02006099 wp->w_skipcol += wp->w_width - win_col_off(wp)
Bram Moolenaar26470632006-10-24 19:12:40 +00006100 + win_col_off2(wp);
6101 --wp->w_wrow;
6102 }
6103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006105 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
Bram Moolenaar26470632006-10-24 19:12:40 +00006107 while (sline > 0 && lnum > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 {
6109#ifdef FEAT_FOLDING
6110 hasFoldingWin(wp, lnum, &lnum, NULL, TRUE, NULL);
6111 if (lnum == 1)
6112 {
6113 /* first line in buffer is folded */
6114 line_size = 1;
6115 --sline;
6116 break;
6117 }
6118#endif
6119 --lnum;
6120#ifdef FEAT_DIFF
6121 if (lnum == wp->w_topline)
6122 line_size = plines_win_nofill(wp, lnum, TRUE)
6123 + wp->w_topfill;
6124 else
6125#endif
6126 line_size = plines_win(wp, lnum, TRUE);
6127 sline -= line_size;
6128 }
Bram Moolenaar34114692005-01-02 11:28:13 +00006129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (sline < 0)
6131 {
6132 /*
6133 * Line we want at top would go off top of screen. Use next
6134 * line instead.
6135 */
6136#ifdef FEAT_FOLDING
6137 hasFoldingWin(wp, lnum, NULL, &lnum, TRUE, NULL);
6138#endif
6139 lnum++;
6140 wp->w_wrow -= line_size + sline;
6141 }
Bram Moolenaardd0402a2014-05-28 13:43:04 +02006142 else if (sline > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 {
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006144 // First line of file reached, use that as topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 lnum = 1;
6146 wp->w_wrow -= sline;
6147 }
6148 }
Bram Moolenaar8fcb60f2019-03-04 13:18:30 +01006149 set_topline(wp, lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 }
6151
6152 if (wp == curwin)
6153 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006154 if (get_scrolloff_value())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 update_topline();
6156 curs_columns(FALSE); /* validate w_wrow */
6157 }
Bram Moolenaar56b3bf82014-05-07 20:25:35 +02006158 if (prev_height > 0)
6159 wp->w_prev_fraction_row = wp->w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 win_comp_scroll(wp);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006162 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 wp->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 invalidate_botline_win(wp);
6165}
6166
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167/*
6168 * Set the width of a window.
6169 */
Bram Moolenaar6763c142012-07-19 18:05:44 +02006170 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006171win_new_width(win_T *wp, int width)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173 wp->w_width = width;
6174 wp->w_lines_valid = 0;
6175 changed_line_abv_curs_win(wp);
6176 invalidate_botline_win(wp);
6177 if (wp == curwin)
6178 {
6179 update_topline();
6180 curs_columns(TRUE); /* validate w_wrow */
6181 }
6182 redraw_win_later(wp, NOT_VALID);
6183 wp->w_redr_status = TRUE;
6184}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
6186 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006187win_comp_scroll(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
6189 wp->w_p_scr = ((unsigned)wp->w_height >> 1);
6190 if (wp->w_p_scr == 0)
6191 wp->w_p_scr = 1;
6192}
6193
6194/*
6195 * command_height: called whenever p_ch has been changed
6196 */
6197 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006198command_height(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 int h;
6201 frame_T *frp;
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006202 int old_p_ch = curtab->tp_ch_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00006204 /* Use the value of p_ch that we remembered. This is needed for when the
6205 * GUI starts up, we can't be sure in what order things happen. And when
6206 * p_ch was changed in another tab page. */
6207 curtab->tp_ch_used = p_ch;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 /* Find bottom frame with width of screen. */
6210 frp = lastwin->w_frame;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 while (frp->fr_width != Columns && frp->fr_parent != NULL)
6212 frp = frp->fr_parent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213
6214 /* Avoid changing the height of a window with 'winfixheight' set. */
6215 while (frp->fr_prev != NULL && frp->fr_layout == FR_LEAF
6216 && frp->fr_win->w_p_wfh)
6217 frp = frp->fr_prev;
6218
6219 if (starting != NO_SCREEN)
6220 {
6221 cmdline_row = Rows - p_ch;
6222
6223 if (p_ch > old_p_ch) /* p_ch got bigger */
6224 {
6225 while (p_ch > old_p_ch)
6226 {
6227 if (frp == NULL)
6228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006229 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 p_ch = old_p_ch;
Bram Moolenaar719939c2007-09-25 12:51:28 +00006231 curtab->tp_ch_used = p_ch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 cmdline_row = Rows - p_ch;
6233 break;
6234 }
6235 h = frp->fr_height - frame_minheight(frp, NULL);
6236 if (h > p_ch - old_p_ch)
6237 h = p_ch - old_p_ch;
6238 old_p_ch += h;
6239 frame_add_height(frp, -h);
6240 frp = frp->fr_prev;
6241 }
6242
6243 /* Recompute window positions. */
6244 (void)win_comp_pos();
6245
6246 /* clear the lines added to cmdline */
6247 if (full_screen)
6248 screen_fill((int)(cmdline_row), (int)Rows, 0,
6249 (int)Columns, ' ', ' ', 0);
6250 msg_row = cmdline_row;
6251 redraw_cmdline = TRUE;
6252 return;
6253 }
6254
6255 if (msg_row < cmdline_row)
6256 msg_row = cmdline_row;
6257 redraw_cmdline = TRUE;
6258 }
6259 frame_add_height(frp, (int)(old_p_ch - p_ch));
6260
6261 /* Recompute window positions. */
6262 if (frp != lastwin->w_frame)
6263 (void)win_comp_pos();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264}
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266/*
6267 * Resize frame "frp" to be "n" lines higher (negative for less high).
6268 * Also resize the frames it is contained in.
6269 */
6270 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006271frame_add_height(frame_T *frp, int n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272{
6273 frame_new_height(frp, frp->fr_height + n, FALSE, FALSE);
6274 for (;;)
6275 {
6276 frp = frp->fr_parent;
6277 if (frp == NULL)
6278 break;
6279 frp->fr_height += n;
6280 }
6281}
6282
6283/*
6284 * Add or remove a status line for the bottom window(s), according to the
6285 * value of 'laststatus'.
6286 */
6287 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006288last_status(
6289 int morewin) /* pretend there are two or more windows */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 /* Don't make a difference between horizontal or vertical split. */
6292 last_status_rec(topframe, (p_ls == 2
Bram Moolenaar459ca562016-11-10 18:16:33 +01006293 || (p_ls == 1 && (morewin || !ONE_WINDOW))));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294}
6295
6296 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006297last_status_rec(frame_T *fr, int statusline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298{
6299 frame_T *fp;
6300 win_T *wp;
6301
6302 if (fr->fr_layout == FR_LEAF)
6303 {
6304 wp = fr->fr_win;
6305 if (wp->w_status_height != 0 && !statusline)
6306 {
6307 /* remove status line */
6308 win_new_height(wp, wp->w_height + 1);
6309 wp->w_status_height = 0;
6310 comp_col();
6311 }
6312 else if (wp->w_status_height == 0 && statusline)
6313 {
6314 /* Find a frame to take a line from. */
6315 fp = fr;
6316 while (fp->fr_height <= frame_minheight(fp, NULL))
6317 {
6318 if (fp == topframe)
6319 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006320 emsg(_(e_noroom));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 return;
6322 }
6323 /* In a column of frames: go to frame above. If already at
6324 * the top or in a row of frames: go to parent. */
6325 if (fp->fr_parent->fr_layout == FR_COL && fp->fr_prev != NULL)
6326 fp = fp->fr_prev;
6327 else
6328 fp = fp->fr_parent;
6329 }
6330 wp->w_status_height = 1;
6331 if (fp != fr)
6332 {
6333 frame_new_height(fp, fp->fr_height - 1, FALSE, FALSE);
6334 frame_fix_height(wp);
6335 (void)win_comp_pos();
6336 }
6337 else
6338 win_new_height(wp, wp->w_height - 1);
6339 comp_col();
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00006340 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
6342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 else if (fr->fr_layout == FR_ROW)
6344 {
6345 /* vertically split windows, set status line for each one */
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006346 FOR_ALL_FRAMES(fp, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 last_status_rec(fp, statusline);
6348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 else
6350 {
6351 /* horizontally split window, set status line for last one */
6352 for (fp = fr->fr_child; fp->fr_next != NULL; fp = fp->fr_next)
6353 ;
6354 last_status_rec(fp, statusline);
6355 }
6356}
6357
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358/*
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006359 * Return the number of lines used by the tab page line.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006360 */
6361 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006362tabline_height(void)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006363{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006364#ifdef FEAT_GUI_TABLINE
6365 /* When the GUI has the tabline then this always returns zero. */
6366 if (gui_use_tabline())
6367 return 0;
6368#endif
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006369 switch (p_stal)
Bram Moolenaar98ea5de2006-02-15 22:11:25 +00006370 {
6371 case 0: return 0;
6372 case 1: return (first_tabpage->tp_next == NULL) ? 0 : 1;
6373 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006374 return 1;
6375}
6376
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377/*
6378 * Return the minimal number of rows that is needed on the screen to display
6379 * the current number of windows.
6380 */
6381 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006382min_rows(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 int total;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006385 tabpage_T *tp;
6386 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388 if (firstwin == NULL) /* not initialized yet */
6389 return MIN_LINES;
6390
Bram Moolenaarf740b292006-02-16 22:11:02 +00006391 total = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02006392 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006393 {
6394 n = frame_minheight(tp->tp_topframe, NULL);
6395 if (total < n)
6396 total = n;
6397 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006398 total += tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006399 total += 1; /* count the room for the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 return total;
6401}
6402
6403/*
Bram Moolenaar1c329c02019-10-27 20:37:35 +01006404 * Return TRUE if there is only one window and only one tab page, not
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006405 * counting a help or preview window, unless it is the current window.
Bram Moolenaar4d784b22019-05-25 19:51:39 +02006406 * Does not count unlisted windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 */
6408 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006409only_one_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 int count = 0;
6412 win_T *wp;
6413
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006414 /* If there is another tab page there always is another window. */
6415 if (first_tabpage->tp_next != NULL)
6416 return FALSE;
6417
Bram Moolenaar29323592016-07-24 22:04:11 +02006418 FOR_ALL_WINDOWS(wp)
Bram Moolenaar802418d2013-01-17 14:00:11 +01006419 if (wp->w_buffer != NULL
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02006420 && (!((bt_help(wp->w_buffer) && !bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421# ifdef FEAT_QUICKFIX
6422 || wp->w_p_pvw
6423# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006424 ) || wp == curwin) && wp != aucmd_win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 ++count;
6426 return (count <= 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427}
6428
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429/*
6430 * Correct the cursor line number in other windows. Used after changing the
6431 * current buffer, and before applying autocommands.
6432 * When "do_curwin" is TRUE, also check current window.
6433 */
6434 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006435check_lnums(int do_curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006438 tabpage_T *tp;
6439
6440 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 if ((do_curwin || wp != curwin) && wp->w_buffer == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
Bram Moolenaara68e5952019-04-25 22:22:01 +02006443 // save the original cursor position and topline
6444 wp->w_save_cursor.w_cursor_save = wp->w_cursor;
6445 wp->w_save_cursor.w_topline_save = wp->w_topline;
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if (wp->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6448 wp->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6449 if (wp->w_topline > curbuf->b_ml.ml_line_count)
6450 wp->w_topline = curbuf->b_ml.ml_line_count;
Bram Moolenaara68e5952019-04-25 22:22:01 +02006451
6452 // save the corrected cursor position and topline
6453 wp->w_save_cursor.w_cursor_corr = wp->w_cursor;
6454 wp->w_save_cursor.w_topline_corr = wp->w_topline;
6455 }
6456}
6457
6458/*
6459 * Reset cursor and topline to its stored values from check_lnums().
6460 * check_lnums() must have been called first!
6461 */
6462 void
6463reset_lnums()
6464{
6465 win_T *wp;
6466 tabpage_T *tp;
6467
6468 FOR_ALL_TAB_WINDOWS(tp, wp)
6469 if (wp->w_buffer == curbuf)
6470 {
6471 // Restore the value if the autocommand didn't change it.
6472 if (EQUAL_POS(wp->w_save_cursor.w_cursor_corr, wp->w_cursor))
6473 wp->w_cursor = wp->w_save_cursor.w_cursor_save;
6474 if (wp->w_save_cursor.w_topline_corr == wp->w_topline)
6475 wp->w_topline = wp->w_save_cursor.w_topline_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 }
6477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
6479/*
6480 * A snapshot of the window sizes, to restore them after closing the help
6481 * window.
6482 * Only these fields are used:
6483 * fr_layout
6484 * fr_width
6485 * fr_height
6486 * fr_next
6487 * fr_child
6488 * fr_win (only valid for the old curwin, NULL otherwise)
6489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
6491/*
6492 * Create a snapshot of the current frame sizes.
6493 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006494 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006495make_snapshot(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006497 clear_snapshot(curtab, idx);
6498 make_snapshot_rec(topframe, &curtab->tp_snapshot[idx]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499}
6500
6501 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006502make_snapshot_rec(frame_T *fr, frame_T **frp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006504 *frp = ALLOC_CLEAR_ONE(frame_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (*frp == NULL)
6506 return;
6507 (*frp)->fr_layout = fr->fr_layout;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 (*frp)->fr_width = fr->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 (*frp)->fr_height = fr->fr_height;
6510 if (fr->fr_next != NULL)
6511 make_snapshot_rec(fr->fr_next, &((*frp)->fr_next));
6512 if (fr->fr_child != NULL)
6513 make_snapshot_rec(fr->fr_child, &((*frp)->fr_child));
6514 if (fr->fr_layout == FR_LEAF && fr->fr_win == curwin)
6515 (*frp)->fr_win = curwin;
6516}
6517
6518/*
6519 * Remove any existing snapshot.
6520 */
6521 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006522clear_snapshot(tabpage_T *tp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006524 clear_snapshot_rec(tp->tp_snapshot[idx]);
6525 tp->tp_snapshot[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526}
6527
6528 static void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006529clear_snapshot_rec(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530{
6531 if (fr != NULL)
6532 {
6533 clear_snapshot_rec(fr->fr_next);
6534 clear_snapshot_rec(fr->fr_child);
6535 vim_free(fr);
6536 }
6537}
6538
6539/*
6540 * Restore a previously created snapshot, if there is any.
6541 * This is only done if the screen size didn't change and the window layout is
6542 * still the same.
6543 */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006544 void
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006545restore_snapshot(
6546 int idx,
6547 int close_curwin) /* closing current window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548{
6549 win_T *wp;
6550
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006551 if (curtab->tp_snapshot[idx] != NULL
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006552 && curtab->tp_snapshot[idx]->fr_width == topframe->fr_width
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006553 && curtab->tp_snapshot[idx]->fr_height == topframe->fr_height
6554 && check_snapshot_rec(curtab->tp_snapshot[idx], topframe) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006556 wp = restore_snapshot_rec(curtab->tp_snapshot[idx], topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 win_comp_pos();
6558 if (wp != NULL && close_curwin)
6559 win_goto(wp);
Bram Moolenaarbf3250a2019-01-06 17:25:29 +01006560 redraw_all_later(NOT_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
Bram Moolenaar746ebd32009-06-16 14:01:43 +00006562 clear_snapshot(curtab, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563}
6564
6565/*
6566 * Check if frames "sn" and "fr" have the same layout, same following frames
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006567 * and same children. And the window pointer is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 */
6569 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006570check_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571{
6572 if (sn->fr_layout != fr->fr_layout
6573 || (sn->fr_next == NULL) != (fr->fr_next == NULL)
6574 || (sn->fr_child == NULL) != (fr->fr_child == NULL)
6575 || (sn->fr_next != NULL
6576 && check_snapshot_rec(sn->fr_next, fr->fr_next) == FAIL)
6577 || (sn->fr_child != NULL
Bram Moolenaar343b8c02017-02-17 12:04:56 +01006578 && check_snapshot_rec(sn->fr_child, fr->fr_child) == FAIL)
Bram Moolenaar2c90d512017-03-18 22:35:30 +01006579 || (sn->fr_win != NULL && !win_valid(sn->fr_win)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 return FAIL;
6581 return OK;
6582}
6583
6584/*
6585 * Copy the size of snapshot frame "sn" to frame "fr". Do the same for all
6586 * following frames and children.
6587 * Returns a pointer to the old current window, or NULL.
6588 */
6589 static win_T *
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006590restore_snapshot_rec(frame_T *sn, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 win_T *wp = NULL;
6593 win_T *wp2;
6594
6595 fr->fr_height = sn->fr_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 fr->fr_width = sn->fr_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 if (fr->fr_layout == FR_LEAF)
6598 {
6599 frame_new_height(fr, fr->fr_height, FALSE, FALSE);
Bram Moolenaarbe4d5062006-03-18 21:30:13 +00006600 frame_new_width(fr, fr->fr_width, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 wp = sn->fr_win;
6602 }
6603 if (sn->fr_next != NULL)
6604 {
6605 wp2 = restore_snapshot_rec(sn->fr_next, fr->fr_next);
6606 if (wp2 != NULL)
6607 wp = wp2;
6608 }
6609 if (sn->fr_child != NULL)
6610 {
6611 wp2 = restore_snapshot_rec(sn->fr_child, fr->fr_child);
6612 if (wp2 != NULL)
6613 wp = wp2;
6614 }
6615 return wp;
6616}
6617
Bram Moolenaar4033c552017-09-16 20:54:51 +02006618#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619/*
6620 * Return TRUE if there is any vertically split window.
6621 */
6622 int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006623win_hasvertsplit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625 frame_T *fr;
6626
6627 if (topframe->fr_layout == FR_ROW)
6628 return TRUE;
6629
6630 if (topframe->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006631 FOR_ALL_FRAMES(fr, topframe->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 if (fr->fr_layout == FR_ROW)
6633 return TRUE;
6634
6635 return FALSE;
6636}
6637#endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +00006638
Bram Moolenaar6d216452013-05-12 19:00:41 +02006639#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
6640 int
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006641get_win_number(win_T *wp, win_T *first_win)
Bram Moolenaar6d216452013-05-12 19:00:41 +02006642{
6643 int i = 1;
6644 win_T *w;
6645
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006646 for (w = first_win; w != NULL && w != wp; w = W_NEXT(w))
Bram Moolenaar6d216452013-05-12 19:00:41 +02006647 ++i;
6648
6649 if (w == NULL)
6650 return 0;
6651 else
6652 return i;
6653}
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006654
6655 int
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02006656get_tab_number(tabpage_T *tp UNUSED)
Bram Moolenaar5e538ec2013-05-15 15:12:29 +02006657{
6658 int i = 1;
6659 tabpage_T *t;
6660
6661 for (t = first_tabpage; t != NULL && t != tp; t = t->tp_next)
6662 ++i;
6663
6664 if (t == NULL)
6665 return 0;
6666 else
6667 return i;
6668}
Bram Moolenaar6d216452013-05-12 19:00:41 +02006669#endif
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006670
6671/*
6672 * Return TRUE if "topfrp" and its children are at the right height.
6673 */
6674 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006675frame_check_height(frame_T *topfrp, int height)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006676{
6677 frame_T *frp;
6678
6679 if (topfrp->fr_height != height)
6680 return FALSE;
6681
6682 if (topfrp->fr_layout == FR_ROW)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006683 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006684 if (frp->fr_height != height)
6685 return FALSE;
6686
6687 return TRUE;
6688}
6689
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006690/*
6691 * Return TRUE if "topfrp" and its children are at the right width.
6692 */
6693 static int
Bram Moolenaarb638a7b2016-01-30 21:29:58 +01006694frame_check_width(frame_T *topfrp, int width)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006695{
6696 frame_T *frp;
6697
6698 if (topfrp->fr_width != width)
6699 return FALSE;
6700
6701 if (topfrp->fr_layout == FR_COL)
Bram Moolenaar3d1491e2018-12-22 17:07:50 +01006702 FOR_ALL_FRAMES(frp, topfrp->fr_child)
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006703 if (frp->fr_width != width)
6704 return FALSE;
6705
6706 return TRUE;
6707}
Bram Moolenaarb893ac22013-06-26 14:04:47 +02006708
Bram Moolenaare677df82019-09-02 22:31:11 +02006709#if defined(FEAT_SYN_HL) || defined(PROTO)
6710/*
6711 * Simple int comparison function for use with qsort()
6712 */
6713 static int
6714int_cmp(const void *a, const void *b)
6715{
6716 return *(const int *)a - *(const int *)b;
6717}
6718
6719/*
6720 * Handle setting 'colorcolumn' or 'textwidth' in window "wp".
6721 * Returns error message, NULL if it's OK.
6722 */
6723 char *
6724check_colorcolumn(win_T *wp)
6725{
6726 char_u *s;
6727 int col;
6728 int count = 0;
6729 int color_cols[256];
6730 int i;
6731 int j = 0;
6732
6733 if (wp->w_buffer == NULL)
6734 return NULL; // buffer was closed
6735
6736 for (s = wp->w_p_cc; *s != NUL && count < 255;)
6737 {
6738 if (*s == '-' || *s == '+')
6739 {
6740 // -N and +N: add to 'textwidth'
6741 col = (*s == '-') ? -1 : 1;
6742 ++s;
6743 if (!VIM_ISDIGIT(*s))
6744 return e_invarg;
6745 col = col * getdigits(&s);
6746 if (wp->w_buffer->b_p_tw == 0)
6747 goto skip; // 'textwidth' not set, skip this item
6748 col += wp->w_buffer->b_p_tw;
6749 if (col < 0)
6750 goto skip;
6751 }
6752 else if (VIM_ISDIGIT(*s))
6753 col = getdigits(&s);
6754 else
6755 return e_invarg;
6756 color_cols[count++] = col - 1; // 1-based to 0-based
6757skip:
6758 if (*s == NUL)
6759 break;
6760 if (*s != ',')
6761 return e_invarg;
6762 if (*++s == NUL)
6763 return e_invarg; // illegal trailing comma as in "set cc=80,"
6764 }
6765
6766 vim_free(wp->w_p_cc_cols);
6767 if (count == 0)
6768 wp->w_p_cc_cols = NULL;
6769 else
6770 {
6771 wp->w_p_cc_cols = ALLOC_MULT(int, count + 1);
6772 if (wp->w_p_cc_cols != NULL)
6773 {
6774 // sort the columns for faster usage on screen redraw inside
6775 // win_line()
6776 qsort(color_cols, count, sizeof(int), int_cmp);
6777
6778 for (i = 0; i < count; ++i)
6779 // skip duplicates
6780 if (j == 0 || wp->w_p_cc_cols[j - 1] != color_cols[i])
6781 wp->w_p_cc_cols[j++] = color_cols[i];
6782 wp->w_p_cc_cols[j] = -1; // end marker
6783 }
6784 }
6785
6786 return NULL; // no error
6787}
6788#endif